File: avoid.sh
   1 #!/bin/sh
   2 
   3 # The MIT License (MIT)
   4 #
   5 # Copyright (c) 2026 pacman64
   6 #
   7 # Permission is hereby granted, free of charge, to any person obtaining a copy
   8 # of this software and associated documentation files (the "Software"), to deal
   9 # in the Software without restriction, including without limitation the rights
  10 # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  11 # copies of the Software, and to permit persons to whom the Software is
  12 # furnished to do so, subject to the following conditions:
  13 #
  14 # The above copyright notice and this permission notice shall be included in
  15 # all copies or substantial portions of the Software.
  16 #
  17 # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  18 # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  19 # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  20 # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  21 # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  22 # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  23 # SOFTWARE.
  24 
  25 
  26 # avoid [options...] [regular expressions...]
  27 #
  28 # Avoid/ignore lines which match any of the extended-mode regular expressions
  29 # given. When not given any regex, all empty lines are ignored by default.
  30 # Input lines always come from the standard input.
  31 #
  32 # The handy case-insensitive shortcut options may cause this tool to fail,
  33 # if the main `awk` tool installed doesn't support the special `IGNORECASE`
  34 # variable.
  35 #
  36 # The options are, available both in single and double-dash versions
  37 #
  38 #   -h, -help     show this help message
  39 #   -i, -ins      match regexes case-insensitively
  40 #   -q, -quiet    never emit output: exit code still succeeds/fails
  41 
  42 
  43 buf=0
  44 ins=0
  45 quiet=0
  46 
  47 while [ $# -gt 0 ]; do
  48     case "$1" in
  49         -b|--b|-buffered|--buffered) buf=1; shift; continue ;;
  50 
  51         -h|--h|-help|--help)
  52             awk '/^# +avoid /, /^$/ { gsub(/^# ?/, ""); print }' "$0"
  53             exit 0
  54         ;;
  55 
  56         -i|--i|-ins|--ins|-insensitive|--insensitive) ins=1; shift; continue ;;
  57 
  58         -iq|-qi|-is|-si) ins=1; quiet=1; shift; continue ;;
  59 
  60         -q|--q|-quiet|--quiet|-s|--s|-silent|--silent)
  61             quiet=1; shift; continue
  62         ;;
  63 
  64         -) break ;;
  65 
  66         --) shift; break ;;
  67 
  68         -*)
  69             printf "%s: unsupported option %s\n" "$0" "$1" >&2
  70             exit 1
  71         ;;
  72     esac
  73 
  74     break
  75 done
  76 
  77 if [ "${quiet}" -eq 1 ]; then
  78     buf=1
  79 fi
  80 
  81 flush=0
  82 if [ "${buf}" -eq 0 ] && { [ -p /dev/stdout ] || [ -t 1 ]; }; then
  83     flush=1
  84 fi
  85 
  86 awk -v flush="${flush}" -v ins="${ins}" -v quiet="${quiet}" '
  87     BEGIN {
  88         if (ins && IGNORECASE == "") {
  89             msg = "'"$0"': default AWK lacks case-insensitive regex-matching"
  90             print(msg) > "/dev/stderr"
  91             exit 125
  92         }
  93         if (ins) IGNORECASE = 1
  94 
  95         for (i = 1; i < ARGC; i++) {
  96             re[i] = ARGV[i]
  97             delete ARGV[i]
  98         }
  99 
 100         if (quiet) flush = 0
 101     }
 102 
 103     {
 104         plain = $0
 105         # ignore cursor-movers, style-changers, and OSC sequences
 106         gsub(/\x1b\[[0-9;]*[A-Za-z]/, "", plain)
 107         gsub(/\x1b\][^\x1b]+\x1b\\/, "", plain)
 108 
 109         for (i = 1; i < ARGC; i++) if (plain ~ re[i]) next
 110         got++
 111         if (quiet) next
 112         print
 113         if (flush) fflush()
 114     }
 115 
 116     END { exit(got == 0) }
 117 ' "${@:-^\r?$}"