/* The MIT License (MIT) Copyright © 2020-2025 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 -O3 -march=native -mtune=native -flto -o ./plain ./plain.c */ #include #include #include #include #include #include #include #ifdef _WIN32 #include #include #endif #ifdef RED_ERRORS #define ERROR_STYLE "\x1b[38;2;204;0;0m" #ifdef __APPLE__ #define ERROR_STYLE "\x1b[31m" #endif #define RESET_STYLE "\x1b[0m" #else #define ERROR_STYLE #define RESET_STYLE #endif #define ERROR_LINE(MSG) (ERROR_STYLE MSG RESET_STYLE "\n") #define BAD_ALLOC 2 const char* info = "" "plain [options...] [filepaths...]\n" "\n" "Ignore all ANSI codes, leaving just the plain-text. All input is assumed to\n" "be UTF-8. When not given any filepaths, the standard input is used.\n" "\n" "Options, all of which can start with either 1 or 2 dashes:\n" "\n" " -h show this help message\n" " -help show this help message\n" ""; // span is a region of bytes in memory typedef struct span { // ptr is the starting place of the region unsigned char* ptr; // len is how many bytes are in the region size_t len; } span; // advance updates a span so it starts after the number of bytes given static inline void advance(span* src, size_t n) { src->ptr += n; src->len -= n; } // slice is a growable region of bytes in memory typedef struct slice { // ptr is the starting place of the region unsigned char* ptr; // cap is how many bytes the memory region has available size_t cap; } slice; // find_esc_pair tries to find the starting index of 2-byte substring "\x1b[" static inline int64_t find_esc_pair(span 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 == '[') { return i - 1; } esc = false; } return -1; } // find_alpha tries to find the position of the first letter in a string static inline int64_t find_alpha(span 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 static inline int64_t find_byte(span line, unsigned char what) { for (size_t i = 0; i < line.len; i++) { if (line.ptr[i] == what) { return i; } } return -1; } // find_osc_end tries to find the first position after the end of OSC bytes int64_t find_osc_end(span line) { size_t prev = 0; for (size_t i = 0; i < line.len; i++) { if (line.ptr[i] == '\a') { return i; } if (prev == '\x1b' && line.ptr[i] == '\\') { return i; } prev = line.ptr[i]; } return -1; } // destyle_line renders the line given, omitting ANSI-styles void destyle_line(FILE* w, span line) { while (line.len > 0) { int64_t j = find_esc_pair(line, 0); if (j < 0) { fwrite(line.ptr, 1, line.len, w); return; } fwrite(line.ptr, 1, j, w); advance(&line, j); j = find_alpha(line); if (j < 0) { fwrite(line.ptr, 1, line.len, w); return; } advance(&line, j + 1); } } bool starts_with_bom(span 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 void handle_lines(FILE* w, slice* line, FILE* src, bool live_lines) { span trimmed; for (size_t i = 0; !feof(w); i++) { ssize_t len = getline((char**)&line->ptr, &line->cap, src); if (line->ptr == NULL) { fprintf(stderr, "\n"); fprintf(stderr, ERROR_LINE("out of memory")); exit(BAD_ALLOC); } if (len < 0) { break; } trimmed.ptr = line->ptr; trimmed.len = 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); fputc('\n', w); if (live_lines) { fflush(w); } } } // 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, const char* path, bool live_lines) { FILE* f = fopen(path, "rb"); if (f == NULL) { fprintf(stderr, ERROR_LINE("can't open file named '%s'"), path); return false; } handle_lines(w, line, f, live_lines); fclose(f); return true; } // run returns the number of errors int run(int argc, char** argv, FILE* w, bool live_lines) { size_t errors = 0; slice line; line.cap = 32 * 1024; line.ptr = malloc(line.cap); if (line.ptr == NULL) { fprintf(stderr, ERROR_LINE("out of memory")); exit(BAD_ALLOC); } for (size_t i = 1; i < (size_t)argc && !feof(w); i++) { if (argv[i][0] == '-' && argv[i][1] == 0) { // `-` means standard input handle_lines(w, &line, stdin, live_lines); continue; } if (!handle_file(w, &line, argv[i], live_lines)) { errors++; } } // use stdin when not given any filepaths if (argc < 2) { handle_lines(w, &line, stdin, live_lines); } if (!live_lines) { fflush(w); } free(line.ptr); return errors; } // is_help_option simplifies control-flow for func main bool is_help_option(const 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])) { printf("%s", info); return 0; } const bool live_lines = lseek(fileno(stdout), 0, SEEK_CUR) != 0; if (!live_lines) { setvbuf(stdout, NULL, _IOFBF, 0); } return run(argc, argv, stdout, live_lines) == 0 ? 0 : 1; }