From 57f350cf16b45ff02bbad16b96599891049ae41c Mon Sep 17 00:00:00 2001 From: Kaz Kylheku Date: Fri, 18 Mar 2022 17:53:22 -0700 Subject: Simplify and revise quoting. --- cppawk | 14 ++++---------- testcases | 21 +++++++++++++++++++-- 2 files changed, 23 insertions(+), 12 deletions(-) diff --git a/cppawk b/cppawk index 67cb152..ebf9ff1 100755 --- a/cppawk +++ b/cppawk @@ -48,22 +48,16 @@ quote() case $1 in *"'"* ) case $1 in - *'"'* | *'$'* ) - printf "%s" "'$(printf "%s" "$1" | sed -e "s/'/'\\\\''/g")'" + *[\"\$\\]* ) + printf "'%s'" "$(printf "%s" "$1" | sed -e "s/'/'\\\\''/g")" ;; * ) - printf "%s" "\"$1\"" + printf '"%s"' "$1" ;; esac ;; - *'"'* | *['$*?[(){};&|<>#']* | '~'* ) - printf "%s" "'$1'" - ;; - *' '* | *' '* ) - printf "%s" "\"$1\"" - ;; * ) - printf "%s" "$1" + printf "'%s'" "$1" ;; esac } diff --git a/testcases b/testcases index 1dd1f52..caef473 100644 --- a/testcases +++ b/testcases @@ -127,12 +127,29 @@ BEGIN { } : 73 -- -22: +24: ./cppawk -v foo="abc def" 'BEGIN { print foo }' : abc def -- -23: +25: ./cppawk 'BEGIN { printf("%s:%s:%s\n", ARGC, ARGV[1], ARGV[2]) }' "ab c" "'" : 3:ab c:' +-- +26: +./cppawk -v foo="abc\\ndef" 'BEGIN { print foo }' +: +abc +def +-- +27: +./cppawk -v foo='abc\n"def' 'BEGIN { print foo }' +: +abc +"def +-- +28: +./cppawk -v foo="abc'def" 'BEGIN { print foo }' +: +abc'def -- cgit v1.2.3