File: match.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 # match [options...] [regular expressions...]
  27 #
  28 # Only keep lines which match any of the extended-mode regular expressions
  29 # given. When not given any regex, match non-empty lines by default.
  30 #
  31 # Input lines always come from the standard input.
  32 #
  33 # The options are, available both in single and double-dash versions
  34 #
  35 #   -h, -help     show this help message
  36 #   -i, -ins      match regexes case-insensitively
  37 #   -l, -links    add a regex to match HTTP/HTTPS links case-insensitively
  38 #   -q, -quiet    never emit output: exit code still succeeds/fails
  39 
  40 
  41 links=0
  42 avoid=0
  43 buffered=0
  44 case_ins=0
  45 quiet=0
  46 
  47 while [ $# -gt 0 ]; do
  48     case "$1" in
  49         -b|--b|-buffered|--buffered) buffered=1; shift; continue ;;
  50 
  51         -h|--h|-help|--help)
  52             awk '/^# +match /, /^$/ { gsub(/^# ?/, ""); print }' "$0"
  53             exit 0
  54         ;;
  55 
  56         -i|--i|-ins|--ins|-insensitive|--insensitive)
  57             case_ins=1
  58             shift
  59             continue
  60         ;;
  61 
  62         -iv|-vi)
  63             case_ins=1
  64             avoid=1
  65             shift
  66             continue
  67         ;;
  68 
  69         -l|--l|-links|--links)
  70             links=1
  71             shift
  72             continue
  73         ;;
  74 
  75         -q|--q|-quiet|--quiet)
  76             quiet=1
  77             shift
  78             continue
  79         ;;
  80 
  81         -v|--v|-inv|--inv|-neg|--neg)
  82             avoid=1
  83             shift
  84             continue
  85         ;;
  86 
  87         --) shift; break ;;
  88 
  89         -*)
  90             printf "unsupported option '%s'\n" "$1" >&2
  91             exit 1
  92         ;;
  93     esac
  94 
  95     break
  96 done
  97 
  98 if [ "${quiet}" -eq 1 ]; then
  99     buffered=1
 100 fi
 101 
 102 flush=0
 103 if [ "${buffered}" -eq 0 ] && { [ -p /dev/stdout ] || [ -t 1 ]; }; then
 104     flush=1
 105 fi
 106 
 107 re='[Hh][Tt][Tt][Pp][Ss]?://[A-Za-z0-9+_.:%-]+(/[A-Za-z0-9+_.%/,#?&=-]*)*'
 108 
 109 awk -v flush="${flush}" -v ci="${case_ins}" -v add_links="${links}" \
 110     -v links="${re}" -v quiet="${quiet}" -v avoid="${avoid}" '
 111     BEGIN {
 112         if (ci && IGNORECASE == "") {
 113             msg = "this variant of AWK lacks case-insensitive regex-matching"
 114             print(msg) > "/dev/stderr"
 115             exit 125
 116         }
 117         if (ci) IGNORECASE = 1
 118 
 119         for (i = 1; i < ARGC; i++) {
 120             re[i] = ARGV[i]
 121             delete ARGV[i]
 122         }
 123 
 124         n = ARGC - 1
 125         if (add_links) re[++n] = links
 126         if (n == 0) re[++n] = "[^\\r]"
 127         if (quiet) flush = 0
 128     }
 129 
 130     {
 131         plain = $0
 132         gsub(/\x1b\[([0-9]{4,}[A-Za-z])/, "", plain)
 133 
 134         for (i = 1; i <= n; i++) {
 135             if (plain ~ re[i]) {
 136                 if (avoid) next
 137                 if (!quiet) print
 138                 if (flush) fflush()
 139                 got++
 140                 next
 141             }
 142         }
 143 
 144         if (avoid) { print; if (flush) fflush() }
 145     }
 146 
 147     END { exit(got == 0) }
 148 ' "$@"