File: gsub.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 # gsub [options...] [regexp-substitution pairs / regexps...]
  27 #
  28 # Global extended regex SUBstitute, using the AWK function of the same name:
  29 # arguments are used as regex-replacement pairs, in that order. Input lines
  30 # are always rid of ANSI-styles beforehand, if present.
  31 #
  32 # When run in erase/remove mode (see options below), arguments are all used
  33 # as regexes (instead of regex-substitution pairs), and all replacements are
  34 # empty strings.
  35 #
  36 # The options are, available both in single and double-dash versions
  37 #
  38 #   -e, -erase, -r, -remove    run in erase/remove mode
  39 #   -h, -help                  show this help message
  40 #   -i, -ins, -insensitive     match regexes case-insensitively
  41 
  42 
  43 erase=0
  44 case_insensitive=0
  45 
  46 case "$1" in
  47     -h|--h|-help|--help)
  48         awk '/^# +gsub /, /^$/ { gsub(/^# ?/, ""); print }' "$0"
  49         exit 0
  50     ;;
  51 
  52     -e|--e|-erase|--erase|-r|--r|-remove|--remove)
  53         erase=1
  54         shift
  55     ;;
  56 
  57     -i|--i|-ins|--ins|-insensitive|--insensitive)
  58         case_insensitive=1
  59         shift
  60     ;;
  61 esac
  62 
  63 [ "$1" = '--' ] && shift
  64 
  65 command='awk'
  66 if { [ -p /dev/stdout ] || [ -t 1 ]; } && [ -e /usr/bin/stdbuf ]; then
  67     command='stdbuf -oL awk'
  68 fi
  69 
  70 ${command} -v ci="${case_insensitive}" -v erase="${erase}" '
  71     BEGIN {
  72         if (ci && IGNORECASE == "") {
  73             m = "your `awk` command lacks case-insensitive regex-matching"
  74             print(m) > "/dev/stderr"
  75             exit 125
  76         }
  77         if (ci) IGNORECASE = 1
  78 
  79         for (i = 1; i < ARGC; i++) {
  80             args[++n] = ARGV[i]
  81             delete ARGV[i]
  82             if (erase) args[++n] = ""
  83         }
  84     }
  85 
  86     {
  87         # ignore cursor-movers and style-changers
  88         gsub(/\x1b\[[0-9;]*[A-Za-z]/, "")
  89 
  90         # ignore OSC sequences
  91         gsub(/\x1b\][^\x1b]+\x1b\\/, "")
  92 
  93         for (i = 1; i <= n; i += 2) gsub(args[i], args[i + 1])
  94         print
  95     }
  96 ' "$@"