File: match.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 match [regular expressions...]
  27 
  28 Only keep lines which match any of the regexes given. When not given any
  29 regex, match non-empty lines by default.
  30 */
  31 
  32 // c++ -Wall -O3 -s -o match match.cpp
  33 
  34 #include <algorithm>
  35 #include <iostream>
  36 #include <regex>
  37 #include <string>
  38 #include <unistd.h>
  39 #include <vector>
  40 
  41 #ifdef RED_ERRORS
  42 #define ERROR_STYLE "\x1b[38;2;204;0;0m"
  43 #ifdef __APPLE__
  44 #define ERROR_STYLE "\x1b[31m"
  45 #endif
  46 #define RESET_STYLE "\x1b[0m"
  47 #else
  48 #define ERROR_STYLE ""
  49 #define RESET_STYLE ""
  50 #endif
  51 
  52 using namespace std;
  53 
  54 const auto regex_opts = regex_constants::ECMAScript;
  55 
  56 int compile(int argc, char** argv, vector<regex>& res) {
  57     size_t errors = 0;
  58 
  59     for (int i = 1; i < argc; i++) {
  60         try {
  61             regex expr(argv[i], regex_opts);
  62             res.push_back(expr);
  63         } catch (const regex_error& e) {
  64             cerr << ERROR_STYLE << e.code() << ": " << e.what() << RESET_STYLE << endl;
  65             errors++;
  66         }
  67     }
  68 
  69     return errors;
  70 }
  71 
  72 bool match(string& s, const vector<regex>& expressions) {
  73     return any_of(expressions.begin(), expressions.end(), [&s](regex& e) {
  74         return regex_search(s, e);
  75     });
  76     // for (const regex& e : expressions) {
  77     //     if (regex_search(s, e)) {
  78     //         return true;
  79     //     }
  80     // }
  81     // return false;
  82 }
  83 
  84 int run(int argc, char** argv, bool live_lines) {
  85     if (argc == 1) {
  86         return 0;
  87     }
  88 
  89     vector<regex> expressions;
  90     size_t errors = compile(argc, argv, expressions);
  91     if (errors > 0) {
  92         return 3;
  93     }
  94 
  95     string line;
  96     size_t matches = 0;
  97 
  98     while (getline(cin, line)) {
  99         if (match(line, expressions)) {
 100             matches++;
 101             if (live_lines) {
 102                 cout << line << endl;
 103             } else {
 104                 cout << line << '\n';
 105             }
 106             if (cout.eof()) {
 107                 break;
 108             }
 109         }
 110     }
 111 
 112     if (!live_lines) {
 113         cout.flush();
 114     }
 115     return matches > 0 ? 0 : 1;
 116 }
 117 
 118 int main(int argc, char** argv) {
 119     const bool live_lines = lseek(fileno(stdout), 0, SEEK_CUR) != 0;
 120     if (!live_lines) {
 121         setvbuf(stdout, NULL, _IOFBF, 0);
 122     }
 123     cin.tie(NULL);
 124     ios_base::sync_with_stdio(false);
 125 
 126     try {
 127         char* fallback[2] = {argv[0], const_cast<char*>("[^\\r]")};
 128         if (argc == 1) {
 129             argc = 2;
 130             argv = fallback;
 131         }
 132         return run(argc, argv, live_lines) == 0 ? 0 : 1;
 133     } catch (const bad_alloc& e) {
 134         cerr << ERROR_STYLE "out of memory" RESET_STYLE << endl;
 135         return 2;
 136     }
 137 }