aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorKaz Kylheku <kaz@kylheku.com>2022-03-18 09:17:22 -0700
committerKaz Kylheku <kaz@kylheku.com>2022-03-18 09:17:22 -0700
commit9b9fdbda81790c06973d77c4b3c5b75be9dbebde (patch)
treedec76fce5d3c885eabc83ebedac9b64d9ac4e74e
parentfc1f328339f7366fab9d197a2876d8207f38656c (diff)
downloadcppawk-9b9fdbda81790c06973d77c4b3c5b75be9dbebde.tar.gz
cppawk-9b9fdbda81790c06973d77c4b3c5b75be9dbebde.tar.bz2
cppawk-9b9fdbda81790c06973d77c4b3c5b75be9dbebde.zip
Handle quoting properly.
The problem is that if $var holds escaped syntax, we cannot just use it as command $var; the quotes become part of the argument. We must get the shell to process the quoted syntax, which requires eval. For this we define a function which lets us do command $(syntax "$var").
-rwxr-xr-xcppawk20
-rw-r--r--testcases5
2 files changed, 20 insertions, 5 deletions
diff --git a/cppawk b/cppawk
index d91b2e1..d89059b 100755
--- a/cppawk
+++ b/cppawk
@@ -41,6 +41,8 @@ delhashbang="sed -e /^#!/d"
prepro_only=
# functions
+
+# convert argument into shell syntax that expands into that argument
shell_escape()
{
case $1 in
@@ -66,6 +68,12 @@ shell_escape()
esac
}
+# expand shell syntax for interpolation into command line
+syntax()
+{
+ eval printf \"%s \" "$1"
+}
+
die()
{
fmt="$0: $1\n" ; shift
@@ -120,18 +128,20 @@ trap 'rm -f $tmp_file' EXIT INT TERM
if [ -n "$awk_file" ] ; then
tmp_file=$(mktemp)
- $delhashbang "$awk_file" | $prepro $prepro_opts - > $tmp_file
- [ $prepro_only ] && cat $tmp_file || $awk $awk_opts -f $tmp_file "$@"
+ $delhashbang "$awk_file" | $prepro $(syntax "$prepro_opts") - > $tmp_file
+ [ $prepro_only ] \
+ && cat $tmp_file \
+ || $awk $(syntax "$awk_opts") -f $tmp_file "$@"
elif [ $# -gt 0 ] ; then
tmp_file=$(mktemp)
if [ $prepro_only ] ; then
printf "%s" "$1" | $delhashbang | \
- $prepro $incopt"$(pwd)" $prepro_opts -
+ $prepro $incopt"$(pwd)" $(syntax $prepro_opts) -
else
printf "%s" "$1" | $delhashbang | \
- $prepro $incopt"$(pwd)" $prepro_opts - > $tmp_file
+ $prepro $incopt"$(pwd)" $(syntax "$prepro_opts") - > $tmp_file
shift
- $awk $awk_opts -f $tmp_file "$@"
+ $awk $(syntax "$awk_opts") -f $tmp_file "$@"
fi
else
die "awk code must be specified"
diff --git a/testcases b/testcases
index 04e90dd..71b9169 100644
--- a/testcases
+++ b/testcases
@@ -116,3 +116,8 @@ gawk
./cppawk --prepro-only 'BEGIN { }' | grep BEGIN
:
BEGIN { }
+--
+22:
+./cppawk -v foo="'\"" 'BEGIN { print foo }'
+:
+'"