File: imatch.cpp 1 /* 2 The MIT License (MIT) 3 4 Copyright © 2020-2025 pacman64 5 6 Permission is hereby granted, free of charge, to any person obtaining a copy of 7 this software and associated documentation files (the “Software”), to deal 8 in the Software without restriction, including without limitation the rights to 9 use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies 10 of the Software, and to permit persons to whom the Software is furnished to do 11 so, subject to the following conditions: 12 13 The above copyright notice and this permission notice shall be included in all 14 copies or substantial portions of the Software. 15 16 THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 19 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 20 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 21 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 22 SOFTWARE. 23 */ 24 25 /* 26 imatch [regular expressions...] 27 28 Only keep lines which case-insensitively match any of the regexes given. 29 When not given any regex, match non-empty lines by default. 30 */ 31 32 // c++ -Wall -O3 -s -o imatch imatch.cpp 33 34 #include <iostream> 35 #include <regex> 36 #include <string> 37 #include <unistd.h> 38 #include <vector> 39 40 #ifdef RED_ERRORS 41 #define ERROR_STYLE "\x1b[38;2;204;0;0m" 42 #ifdef __APPLE__ 43 #define ERROR_STYLE "\x1b[31m" 44 #endif 45 #define RESET_STYLE "\x1b[0m" 46 #else 47 #define ERROR_STYLE "" 48 #define RESET_STYLE "" 49 #endif 50 51 using namespace std; 52 53 const auto regex_opts = regex_constants::ECMAScript | regex_constants::icase; 54 55 int compile(int argc, char** argv, vector<regex>& res) { 56 size_t errors = 0; 57 58 for (int i = 1; i < argc; i++) { 59 try { 60 regex expr(argv[i], regex_opts); 61 res.push_back(expr); 62 } catch (const regex_error& e) { 63 cerr << ERROR_STYLE << e.code() << ": " << e.what() << RESET_STYLE << endl; 64 errors++; 65 } 66 } 67 68 return errors; 69 } 70 71 bool match(string& s, const vector<regex>& expressions) { 72 for (const regex& e : expressions) { 73 if (regex_search(s, e)) { 74 return true; 75 } 76 } 77 return false; 78 } 79 80 int run(int argc, char** argv, bool live_lines) { 81 if (argc == 1) { 82 return 0; 83 } 84 85 vector<regex> expressions; 86 size_t errors = compile(argc, argv, expressions); 87 if (errors > 0) { 88 return 3; 89 } 90 91 string line; 92 size_t matches = 0; 93 94 while (getline(cin, line)) { 95 if (match(line, expressions)) { 96 matches++; 97 if (live_lines) { 98 cout << line << endl; 99 } else { 100 cout << line << '\n'; 101 } 102 if (cout.eof()) { 103 break; 104 } 105 } 106 } 107 108 if (!live_lines) { 109 cout.flush(); 110 } 111 return matches > 0 ? 0 : 1; 112 } 113 114 int main(int argc, char** argv) { 115 const bool live_lines = lseek(fileno(stdout), 0, SEEK_CUR) != 0; 116 if (!live_lines) { 117 setvbuf(stdout, NULL, _IOFBF, 0); 118 } 119 cin.tie(NULL); 120 ios_base::sync_with_stdio(false); 121 122 try { 123 if (argc == 1) { 124 char* fallback[2] = {argv[0], (char*)"^[^\\r]"}; 125 argc = 2; 126 argv = fallback; 127 } 128 return run(argc, argv, live_lines) == 0 ? 0 : 1; 129 } catch (const bad_alloc& e) { 130 cerr << ERROR_STYLE "out of memory" RESET_STYLE << endl; 131 return 2; 132 } 133 }