1
0

UNTESTED IF BROKEN ROLL BACK TO LAST COMMIT TESTING THIS IS BORING offload work to awk

This commit is contained in:
dtb 2023-08-10 07:29:57 -04:00
parent 5183158657
commit 0f6008d758

41
com/com
View File

@ -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