From 0f6008d75882e7d529efe0fbda03dd7c6ef3b177 Mon Sep 17 00:00:00 2001 From: dtb Date: Thu, 10 Aug 2023 07:29:57 -0400 Subject: [PATCH] UNTESTED IF BROKEN ROLL BACK TO LAST COMMIT TESTING THIS IS BORING offload work to awk --- com/com | 41 +++++++++++++++++++++++------------------ 1 file changed, 23 insertions(+), 18 deletions(-) diff --git a/com/com b/com/com index 3d7e328..f8f85ac 100644 --- a/com/com +++ b/com/com @@ -5,7 +5,7 @@ set -e # http://www.iq0.com/duffgram/com.html implementation # (consulted source to see what -n did) -arv0="$0"; shell=sh +arv0="$0"; shell='sh -c' usage(){ printf 'Usage: %s (-n) (file...)\n' "$argv0" >&2 @@ -20,7 +20,7 @@ do shift printf '%s\n' "$arg" \ | grep 'n' >/dev/null 2>&1 \ - && shell=cat \ + && shell=echo \ || true printf '%s\n' "$arg" \ | grep '[^-n]' >/dev/null 2>&1 \ @@ -41,31 +41,36 @@ test -n "$1" \ while test -n "$1" do - c="$(<"$1" grep '/\*%' 2>/dev/null | head -n 1)" - test -z "$c" \ - && printf '%s: no command\n' "$arv0" >&2 \ - && exit 65 \ - || true #^- sysexits(3) EX_DATAERR - - printf '%s\n' "$c" \ - | sed -e 's,.*/\*%[space]*,,' \ - | awk \ - -v name="$1" \ - -v stem="$(printf '%s\n' "$1" | sed 's,\.[^.]*$,,')" ' -{ + cmd="$(<"$1" awk ' +processed == 0 && /\/\*%/ { + sub(/.*\/\*%[space]*/, ""); for(i = 0; i < length($0) - 1; ++i){ if(substr($0, i, 2) == "##" || substr($0, i, 2) == "%%") printf("%s", substr($0, i++, 1)) - else if(substr($0, i, 1) == "#") + else if(substr($0, i, 1) == "#"){ + if(stem == 0){ + stem = ARGV[1]; + sub(/\.[^.]*$/, "", stem) + } printf("%s", stem) - else if(substr($0, i, 1) == "%") - printf("%s", name) + }else if(substr($0, i, 1) == "%") + printf("%s", ARGV[1]) else printf("%s", substr($0, i, 1)) } printf("%s\n", substr($0, i, 2)) + processed = 1 } - ' | $shell +END { + if(processed == 0){ + # https://www.gnu.org/software/gawk/manual/html_node/ + # Special-FD.html + printf("%s: no command\n", ARGV[2]) > "/dev/stderr" + exit 65 # sysexits(3) EX_DATAERR + } +} + ' "$1" "$argv0")" && exit 65 # sysexits(3) DATA_ERR + $shell "$cmd" printf '%s\n' "$1" >>.comfile shift done