File: pa.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 # pa [options...] [awk expressions...] 27 # 28 # Print Awk evaluates and prints the results of all the expressions given. 29 # 30 # All expressions are evaluated and printed in a `BEGIN` block, which prevents 31 # the usual record/line-processing mode. 32 # 33 # The options are, available both in single and double-dash versions 34 # 35 # -d, -debug show code given to `awk` on the standard error 36 # -h, -help show this help message 37 # -v, -verbose show an ANSI-styled banner for each command being run 38 39 40 debug=0 41 verbose=0 42 cmd='awk' 43 44 while [ $# -gt 0 ]; do 45 case "$1" in 46 --bignum) cmd="${cmd} --bignum"; shift ;; 47 48 -d|--d|-debug|--debug) debug=1; shift ;; 49 50 -h|--h|-help|--help) 51 awk '/^# +pa /, /^$/ { gsub(/^# ?/, ""); print }' "$0" 52 exit 0 53 ;; 54 55 -v|--v|-verbose|--verbose) verbose=1; shift ;; 56 57 --) shift; break ;; 58 59 -*) 60 printf "unsupported option '%s'\n" "$1" >&2 61 exit 1 62 ;; 63 esac 64 65 break 66 done 67 68 if [ $# -eq 0 ]; then 69 awk '/^# +pa /, /^$/ { gsub(/^# ?/, ""); print }' "$0" >&2 70 exit 1 71 fi 72 73 code="$(awk -v verbose="${verbose}" ' 74 BEGIN { 75 vfmt = " print \"\\x1b[7m%s\\x1b[0m\" > \"/dev/stderr\"\n" 76 print " BEGIN {" 77 for (i = 1; i < ARGC; i++) { 78 if (verbose) printf(vfmt, ARGV[i]) 79 printf(" print(%s)\n", ARGV[i]) 80 } 81 print " exit" 82 print " }" 83 exit 84 } 85 ' "$@")" 86 87 [ "${debug}" -eq 1 ] && printf "%s\n" "${code}" >&2 88 ${cmd} "${code}"