/* 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. */ /* imatch [regular expressions...] Only keep lines which case-insensitively match any of the regexes given. When not given any regex, match non-empty lines by default. */ /* You can build this command-line app by running cc -Wall -s -O3 -march=native -mtune=native -flto -o ./imatch ./imatch.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 NO_MATCHES 1 #define BAD_ALLOC 2 #define BAD_REGEX 3 // flags is used to compile all regexes const int flags = REG_EXTENDED | REG_NEWLINE | REG_ICASE; // 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; // config just organizes most arguments given to the input-handling function typedef struct config { size_t nexprs; regex_t* exprs; slice* line; bool live_lines; } config; static inline bool match(const char* s, size_t nexprs, const regex_t* re) { regmatch_t match; for (size_t i = 0; i < nexprs; i++) { if (regexec(&re[i], s, sizeof(match), &match, 0) == 0) { return true; } } return false; } static inline bool emptyish(const unsigned char* s) { const unsigned char b = s[0]; return b == 0 || b == '\n' || b == '\r'; } size_t handle_reader(FILE* w, FILE* r, config cfg) { size_t matches = 0; slice* line = cfg.line; while (!feof(w)) { ssize_t len = getline((char**)&line->ptr, &line->cap, r); if (line->ptr == NULL) { fprintf(stderr, "\n"); fprintf(stderr, ERROR_LINE("out of memory")); exit(BAD_ALLOC); } if (len < 0) { break; } // when not given regexes, just avoid all empty(ish) lines if (cfg.nexprs == 0 && emptyish(line->ptr)) { continue; } // only show matching lines if (!match((char*)line->ptr, cfg.nexprs, cfg.exprs)) { continue; } matches++; fwrite(line->ptr, 1, len, w); const bool has_lf = len >= 1 && line->ptr[len - 1] == '\n'; if (!has_lf) { fputc('\n', w); } if (cfg.live_lines) { fflush(w); } } if (!cfg.live_lines && matches > 0) { fflush(w); } return matches; } // run returns the error code for the app int run(int argc, char** argv, bool live_lines) { 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); } size_t nexprs = argc - 1; regex_t* exprs = NULL; if (nexprs > 0) { const size_t n = nexprs * sizeof(regex_t); exprs = malloc(n); if (exprs == NULL) { fprintf(stderr, ERROR_LINE("out of memory")); exit(BAD_ALLOC); } memset(exprs, 0, n); } // compile all arguments as regexes size_t errors = 0; for (size_t i = 1; i < argc; i++) { char msg[1024]; regex_t* expr = &exprs[i - 1]; const int err = regcomp(expr, argv[i], flags); if (err == 0) { continue; } regerror(err, expr, msg, sizeof(msg)); fprintf(stderr, "%s\t%s\n", argv[i], msg); errors++; } if (errors > 0) { for (size_t i = 0; i < nexprs; i++) { regfree(&exprs[i]); } free(exprs); free(line.ptr); return BAD_REGEX; } // match lines from the standard input config cfg; cfg.line = &line; cfg.nexprs = nexprs; cfg.exprs = exprs; cfg.live_lines = live_lines; const size_t matches = handle_reader(stdout, stdin, cfg); for (size_t i = 0; i < nexprs; i++) { regfree(&exprs[i]); } free(exprs); free(line.ptr); return matches > 0 ? 0 : NO_MATCHES; } 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 const bool live_lines = lseek(fileno(stdout), 0, SEEK_CUR) != 0; if (!live_lines) { setvbuf(stdout, NULL, _IOFBF, 0); } return run(argc, argv, live_lines); }