Add shell expansion for output lines

Do shell expansion for output lines (|, @ and ?), so that it is possible to
evaluate arbitrary shell expressions.

Other fixes included:
- Don't assume ksh is in bin.
- Eval commands using the standard shell instead of ksh.
- Make sure we delete temp files; also avoid creating a temp file per line.
- Don't print ? output if return status is 0.
- Fix literal \? match when evaluating lines.
This commit is contained in:
2025-11-28 18:12:06 +01:00
parent 8afe36016a
commit 6061556d39

43
ts
View File

@@ -1,18 +1,14 @@
#!/bin/ksh #!/usr/bin/env ksh
function tsrun function tsrun
{ {
typeset err out ret typeset ret
err=$(mktemp /tmp/XXXXXXXXXX) sh -c "$@" >$out 2>$err
out=$(mktemp /tmp/XXXXXXXXXX)
trap "rm $err $out" EXIT
eval "$@" >$out 2>$err
ret=$? ret=$?
sed 's/^/| /' $out sed 's/^/| /' $out
sed 's/^/@ /' $err sed 's/^/@ /' $err
print '? '$ret (( ret != 0 )) && print "? $ret"
} }
function tseval function tseval
@@ -26,15 +22,37 @@ function tseval
print "$ln" print "$ln"
tsrun "${ln#$}" tsrun "${ln#$}"
;; ;;
!(?|@|\|)) !(\?|@|\|))
print "$ln" print "$ln"
;; ;;
esac esac
done done
} }
function tsexpand
{
typeset ln
while read ln
do
case ${ln%% *}
in
\?|@|\|)
eval print \""$ln"\"
;;
*)
print "$ln"
;;
esac
done
}
(( $# == 0 )) && exit 64 (( $# == 0 )) && exit 64
err=$(mktemp /tmp/XXXXXXXXXX)
out=$(mktemp /tmp/XXXXXXXXXX)
trap "rm $err $out" HUP INT QUIT TERM
if [[ $1 = -b ]] if [[ $1 = -b ]]
then then
(( $# == 1 )) && exit 64 (( $# == 1 )) && exit 64
@@ -45,8 +63,13 @@ then
(rm $fn && tseval >$fn) <$fn (rm $fn && tseval >$fn) <$fn
done done
else else
exp=$(mktemp /tmp/XXXXXXXXXX)
trap "rm $exp" HUP INT QUIT TERM
for fn for fn
do do
tseval <$fn | diff -u $fn - tsexpand <$fn >$exp
tseval <$fn | diff -u $exp -
done done
rm $exp
fi fi
rm $err $out