File: avoid.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 avoid [regular expressions...]
  27 
  28 Avoid/ignore lines which match any of the regexes given. When not given any
  29 regex, ignore all empty lines by default.
  30 */
  31 
  32 // c++ -Wall -O3 -s -o avoid avoid.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;
  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     vector<regex> expressions;
  82     size_t errors = compile(argc, argv, expressions);
  83     if (errors > 0) {
  84         return 1;
  85     }
  86 
  87     string line;
  88     size_t matches = 0;
  89 
  90     while (getline(cin, line)) {
  91         if (match(line, expressions)) {
  92             continue;
  93         }
  94 
  95         matches++;
  96         if (live_lines) {
  97             cout << line << endl;
  98         } else {
  99             cout << line << '\n';
 100         }
 101         if (cout.eof()) {
 102             break;
 103         }
 104     }
 105 
 106     if (!live_lines) {
 107         cout.flush();
 108     }
 109     return matches > 0 ? 0 : 1;
 110 }
 111 
 112 int main(int argc, char** argv) {
 113     const bool live_lines = lseek(fileno(stdout), 0, SEEK_CUR) != 0;
 114     if (!live_lines) {
 115         setvbuf(stdout, NULL, _IOFBF, 0);
 116     }
 117     cin.tie(NULL);
 118     ios_base::sync_with_stdio(false);
 119 
 120     try {
 121         char* fallback[2] = {argv[0], const_cast<char*>("^\r?$")};
 122         if (argc == 1) {
 123             argc = 2;
 124             argv = fallback;
 125         }
 126         return run(argc, argv, live_lines) == 0 ? 0 : 1;
 127     } catch (const bad_alloc& e) {
 128         cerr << ERROR_STYLE "out of memory" RESET_STYLE << endl;
 129         return 2;
 130     }
 131 }