From 9b9fdbda81790c06973d77c4b3c5b75be9dbebde Mon Sep 17 00:00:00 2001 From: Kaz Kylheku Date: Fri, 18 Mar 2022 09:17:22 -0700 Subject: 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"). --- cppawk | 20 +++++++++++++++----- testcases | 5 +++++ 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 }' +: +'" -- cgit v1.2.3