/* The MIT License (MIT) Copyright © 2024 pacman64 Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the “Software”), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ /* You can build this command-line app by running cc -Wall -s -O2 -o ./plain ./plain.c */ #include #include #include #include #include #include #include #ifdef _WIN32 #include #endif // info is the message shown when this app is given any of its help options const char* info = "" "plain [options...] [filepaths...]\n" "\n" "\n" "Ignore all ANSI codes, leaving just the plain-text.\n" "\n" "All input is assumed to be UTF-8. When not given any filepaths, input is read\n" "from the standard input.\n" "\n" "\n" "Options, all of which can start with either 1 or 2 dashes:\n" "\n" "\n" " -h show this help message\n" " -help show this help message\n" ""; const char* line_memory_error_msg = "" "\x1b[31mcan't get memory for the line-scanner\x1b[0m\n"; // slice is a growable region of bytes in memory typedef struct slice { // ptr is the starting place of the region unsigned char* ptr; // len is how many bytes are currently being used size_t len; // cap is how many bytes the memory region has available size_t cap; } slice; // init_slice is the constructor for type slice void init_slice(slice* s, size_t cap) { s->ptr = malloc(cap); s->len = 0; s->cap = cap; } // advance updates a slice so it starts after the number of bytes given inline void advance(slice* src, size_t n) { src->ptr += n; src->len -= n; } // find_esc_pair tries to find the starting index of 2-byte substrings // "\x1b[" or "\x1b]", whichever comes first, if at all int64_t find_esc_pair(slice line, size_t start) { bool esc = false; for (size_t i = start; i < line.len; i++) { unsigned char cur = line.ptr[i]; if (cur == '\x1b') { esc = true; continue; } if (esc && (cur == '[' || cur == ']')) { return i - 1; } esc = false; } return -1; } // find_alpha tries to find the position of the first letter in a string int64_t find_alpha(slice line) { for (size_t i = 0; i < line.len; i++) { if (isalpha(line.ptr[i])) { return i; } } return -1; } // find_byte tries to find the first position of the value given in a string int64_t find_byte(slice line, unsigned char what) { for (size_t i = 0; i < line.len; i++) { if (line.ptr[i] == what) { return i; } } return -1; } inline void write_bytes(FILE* w, const unsigned char* src, size_t len) { fwrite(src, len, 1, w); } typedef struct skip_state { bool skip_alpha; bool skip_bell; } skip_state; // destyle_line renders the line given, omitting ANSI-styles void destyle_line(FILE* w, slice line, skip_state* state) { if (state->skip_alpha) { int64_t j = find_alpha(line); if (j < 0) { return; } state->skip_alpha = false; advance(&line, j + 1); } if (state->skip_bell) { int64_t j = find_byte(line, '\a'); if (j < 0) { return; } state->skip_bell = false; advance(&line, j + 1); } while (line.len > 0) { int64_t j = find_esc_pair(line, 0); if (j < 0) { write_bytes(w, line.ptr, line.len); return; } write_bytes(w, line.ptr, j); advance(&line, j); switch (line.ptr[1]) { case '[': j = find_alpha(line); if (j < 0) { state->skip_alpha = true; return; } advance(&line, j + 1); continue; case ']': j = find_byte(line, '\a'); if (j < 0) { state->skip_bell = true; return; } advance(&line, j + 1); continue; } } } bool starts_with_bom(slice s) { const unsigned char* p = s.ptr; return s.len >= 3 && p[0] == 0xef && p[1] == 0xbb && p[2] == 0xbf; } // handle_lines loops over input lines, restyling all digit-runs as more // readable `nice numbers`, fulfilling the app's purpose bool handle_lines(FILE* w, slice* line, FILE* src) { slice trimmed; skip_state state; trimmed.cap = 0; state.skip_alpha = false; state.skip_bell = false; for (size_t i = 0; !feof(stdout); i++) { int len = getline((char**)&line->ptr, &line->cap, src); if (len < 0) { break; } if (line->ptr == NULL) { putc('\n', w); fflush(w); fprintf(stderr, line_memory_error_msg); exit(1); } line->len = len; trimmed.ptr = line->ptr; trimmed.len = line->len; // get rid of leading UTF-8 BOM (byte-order mark) if 1st line has it if (i == 0 && starts_with_bom(trimmed)) { trimmed.ptr += 3; trimmed.len -= 3; len = trimmed.len; } const unsigned char* p = trimmed.ptr; // get rid of trailing line-feeds and CRLF end-of-line byte-pairs if (len >= 2 && p[len - 2] == '\r' && p[len - 1] == '\n') { trimmed.len -= 2; } else if (len >= 1 && p[len - 1] == '\n') { trimmed.len--; } destyle_line(w, trimmed, &state); if (!state.skip_alpha && !state.skip_bell) { putc('\n', w); fflush(w); } } if (state.skip_alpha || state.skip_bell) { putc('\n', w); } fflush(w); return true; } // handle_file handles data from the filename given; returns false only when // the file can't be opened bool handle_file(FILE* w, slice* line, char* path) { FILE* f = fopen(path, "rb"); if (f == NULL) { fprintf(stderr, "\x1b[31mcan't open file named %s\x1b[0m\n", path); return false; } const bool ok = handle_lines(w, line, f); fclose(f); return ok; } // run returns the number of errors int run(int argc, char** argv, FILE* w, slice* line) { size_t errors = 0; // use stdin when not given any filepaths if (argc < 2) { if (!handle_lines(w, line, stdin)) { errors++; } return errors; } for (size_t i = 1; i < (size_t)argc && !feof(w); i++) { if (argv[i][0] == '-' && argv[i][1] == 0) { // `-` means standard input if (!handle_lines(w, line, stdin)) { errors++; } continue; } if (!handle_file(w, line, argv[i])) { errors++; } } return errors; } // is_help_option simplifies control-flow for func main bool is_help_option(char* s) { return (s[0] == '-') && ( strcmp(s, "-h") == 0 || strcmp(s, "-help") == 0 || strcmp(s, "--h") == 0 || strcmp(s, "--help") == 0 ); } int main(int argc, char** argv) { #ifdef _WIN32 setmode(fileno(stdin), O_BINARY); // ensure output lines end in LF instead of CRLF on windows setmode(fileno(stdout), O_BINARY); setmode(fileno(stderr), O_BINARY); #endif // handle any of the help options, if given if (argc > 1 && is_help_option(argv[1])) { puts(info); return 0; } slice line; init_slice(&line, 32 * 1024); if (line.ptr == NULL) { fprintf(stderr, line_memory_error_msg); return 1; } const int res = run(argc, argv, stdout, &line) == 0 ? 0 : 1; free(line.ptr); return res; }