aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorKaz Kylheku <kaz@kylheku.com>2022-03-24 17:41:49 -0700
committerKaz Kylheku <kaz@kylheku.com>2022-03-24 17:41:49 -0700
commit96d19551c0ceaf98ad065930ee82b8ff7961d826 (patch)
treecf166e7853f8907caa9eca752e70bf85dead9b68
parentf4faff773cf7eb2b45d2658b5244d0a4c6a10c10 (diff)
downloadcppawk-96d19551c0ceaf98ad065930ee82b8ff7961d826.tar.gz
cppawk-96d19551c0ceaf98ad065930ee82b8ff7961d826.tar.bz2
cppawk-96d19551c0ceaf98ad065930ee82b8ff7961d826.zip
If Bash is available, use process substitution.
-rwxr-xr-xcppawk18
-rw-r--r--cppawk.16
-rw-r--r--testcases8
3 files changed, 29 insertions, 3 deletions
diff --git a/cppawk b/cppawk
index 3eab7e8..af17e5c 100755
--- a/cppawk
+++ b/cppawk
@@ -34,6 +34,12 @@ awk=gawk
prepro_opts="-D__gawk__=1 -D__cppawk_ver=20220324"
# globals
+bash=${BASH_VERSION+y}
+# if this isn't bash, and there is a /bin/bash, let's re-execute ourselves
+# with Bash, so we can use process substitution instead of temp files.
+if ! [ $bash ] && [ -x /bin/bash ] ; then
+ exec /bin/bash "$0" "$@"
+fi
awk_file=
awk_opts=
tmp_file=
@@ -128,6 +134,9 @@ while [ $# -gt 0 ] ; do
;;
esac
;;
+ --nobash )
+ bash=
+ ;;
-U* | -D* | -I* | -iquote* )
prepro_opts="$prepro_opts $(quote "$1")"
;;
@@ -171,6 +180,10 @@ if [ -n "$awk_file" ] ; then
if [ $prepro_only ] ; then
delhashbang_setline "$awk_file" \
| eval "$prepro $incopt\"$awk_file_dir\" $prepro_opts - | collapse"
+ elif [ $bash ] ; then
+ prep='delhashbang_setline "$awk_file" \
+ | eval "$prepro $incopt\"$awk_file_dir\" $prepro_opts - | collapse"'
+ eval "$awk $awk_opts -f <($prep) -- \"\$@\""
else
trap 'rm -f $tmp_file' EXIT INT TERM
tmp_file=$(mktemp)
@@ -183,6 +196,11 @@ elif [ $# -gt 0 ] ; then
if [ $prepro_only ] ; then
printf "%s" "$1" | delhashbang \
| eval "$prepro $incopt"$(pwd)" $prepro_opts - | collapse"
+ elif [ $bash ] ; then
+ code=$1; shift
+ prep='printf "%s" "$code" | delhashbang \
+ | eval "$prepro $incopt"$(pwd)" $prepro_opts - | collapse"'
+ eval "$awk $awk_opts -f <($prep) -- \"\$@\""
else
trap 'rm -f $tmp_file' EXIT INT TERM
tmp_file=$(mktemp)
diff --git a/cppawk.1 b/cppawk.1
index 4a70a17..9aed0b5 100644
--- a/cppawk.1
+++ b/cppawk.1
@@ -84,6 +84,12 @@ is then invoked on this file. The file is deleted when
.B awk
terminates.
+.IP "\fB\-\-nobash\fR"
+Pretend that the shell which executes
+.B cppawk
+isn't GNU Bash, even if it is. This has the effect of disabling
+the use of process substitution in favor of the use of a temporary file.
+
.IP "\fB\-M\fR, \fB\--bignum\fR"
These two equivalent GNU Awk options are passed through to
.BR awk ,
diff --git a/testcases b/testcases
index 40933af..bf22d66 100644
--- a/testcases
+++ b/testcases
@@ -177,8 +177,10 @@ foo_bar
foo_bar
--
32:
-./cppawk --awk=./testdir/testawk foo
+./cppawk --awk=./testdir/testawk foo ;
+./cppawk --nobash --awk=./testdir/testawk foo
:
+[-f][/d][--]
[-f][/t][--]
--
33:
@@ -197,9 +199,9 @@ mawk=1
gawk=__gawk__
--
36:
-./cppawk --prepro=die --prepro-only x 2>&1 | grep 'not found'
+./cppawk --prepro=die --prepro-only x 2>&1 | (grep -q 'die.*not found' && echo "good")
:
-./cppawk: 1: eval: die: not found
+good
--
37:
tmp=$(./cppawk --awk=testdir/testdel 1); [ -e $tmp ] || echo gone