File: tla.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 # tla [options...] [awk expression...] [files...]
  27 #
  28 # Transform Lines with Awk expression: this is a convenient shortcut, to type
  29 # even less when using AWK.
  30 #
  31 # If a leading `=` (no quotes) is given as an argument, no inputs are read,
  32 # and the expression is evaluated/printed once, quitting right away.
  33 #
  34 # The options are, available both in single and double-dash versions
  35 #
  36 #   -h, -help             show this help message
  37 #   -ins, -insensitive    match regexes case-insensitively; fail if unsupported
  38 #   -tsv                  tab-separated values: split input fields using tabs
  39 
  40 
  41 case "$1" in
  42     -h|--h|-help|--help)
  43         awk '/^# +tla /, /^$/ { gsub(/^# ?/, ""); print }' "$0"
  44         exit 0
  45     ;;
  46 esac
  47 
  48 tsv=0
  49 buffered=0
  50 case_ins=0
  51 no_input=0
  52 cmd='awk'
  53 
  54 while [ $# -gt 0 ]; do
  55     case "$1" in
  56         =) no_input=1; shift; continue ;;
  57 
  58         --b|-buffered|--buffered) buffered=1; shift; continue ;;
  59 
  60         -F)
  61             if [ $# -lt 2 ]; then
  62                 printf "expected value after -F option\n" >&2
  63                 exit 1
  64             fi
  65             cmd="${cmd} -F $2"; shift 2; continue
  66         ;;
  67 
  68         -F*) cmd="${cmd} $1"; shift; continue ;;
  69 
  70         -v)
  71             shift
  72             if [ $# -eq 0 ]; then
  73                 printf "expected variable assignment after -v option\n" >&2
  74                 exit 1
  75             fi
  76             cmd="${cmd} -v $1"
  77             shift
  78             continue
  79         ;;
  80 
  81         -i|--i|-ins|--ins|-insensitive|--insensitive)
  82             case_ins=1; shift; continue
  83         ;;
  84 
  85         -tsv|--tsv) tsv=1; shift; continue ;;
  86 
  87         -) break ;;
  88 
  89         --) shift; break ;;
  90 
  91         -*)
  92             printf "unsupported option '%s'\n" "$1" >&2
  93             exit 1
  94         ;;
  95     esac
  96 
  97     break
  98 done
  99 
 100 flush=''
 101 if [ "${buffered}" -eq 0 ] && { [ -p /dev/stdout ] || [ -t 1 ]; }; then
 102     flush='fflush()'
 103 fi
 104 
 105 expr="${1:-\$0}"
 106 [ $# -gt 0 ] && shift
 107 
 108 # show all non-existing files given
 109 failed=0
 110 for arg in "$@"; do
 111     [ "${arg}" = "-" ] && continue
 112     [ -e "${arg}" ] && continue
 113     printf "no file named \"%s\"\n" "${arg}" >&2
 114     failed=1
 115 done
 116 
 117 [ "${failed}" -gt 0 ] && exit 2
 118 
 119 ci='
 120     BEGIN {
 121         if (IGNORECASE == "") {
 122             m = "your `awk` command lacks case-insensitive regex-matching"
 123             print(m) > "/dev/stderr"
 124             exit 125
 125         }
 126         IGNORECASE = 1
 127     }
 128 '
 129 if [ "${case_ins}" -eq 0 ]; then
 130     ci=''
 131 fi
 132 
 133 if [ "${no_input}" -eq 1 ]; then
 134     src="${ci}"'
 135     BEGIN { print ('"${expr}"'); exit }
 136 '
 137 else
 138     src="${ci}"'
 139     { print ('"${expr}"'); '"${flush}"' }
 140 '
 141 fi
 142 
 143 if [ "${tsv}" -eq 1 ]; then
 144     ${cmd} -F "\t" -v OFS="\t" "${src}" "$@"
 145 else
 146     ${cmd} "${src}" "$@"
 147 fi