From 61e1cccf303dd7b1c42026e9544c3733031a3249 Mon Sep 17 00:00:00 2001 From: Stefan Bodewig Date: Wed, 26 Oct 2016 15:43:10 +0200 Subject: [PATCH] latest iteration of Jeff Adamson's script fixes known to not completely break in Solaris --- src/script/ant | 27 +++++++++++++++++++-------- 1 file changed, 19 insertions(+), 8 deletions(-) diff --git a/src/script/ant b/src/script/ant index 8cc8f8e93..0f83e183f 100644 --- a/src/script/ant +++ b/src/script/ant @@ -24,6 +24,15 @@ show_help=false esc_tool=sed +# if awk esc_tool is chosen, use nawk when available +if [ "$esc_tool" = "awk" ] +then + awk_exec=awk + # Solaris_awk does not support gsub, but Solaris_nawk does + # `command -v` behavior is part of posix spec + command -v nawk >/dev/null && awk_exec=nawk +fi + for arg in "$@" ; do if [ "$arg" = "--noconfig" ] ; then no_config=true @@ -40,19 +49,21 @@ for arg in "$@" ; do fi # wrap all arguments as "" strings, escape any internal back-slash, double-quote, $, or back-tick characters - # use printf to avoid echo modification behaviors such as escape and line continuation - # pad the value with leading/trailing X to protect trailing newlines and whitespace from awk and sed + # use printf to avoid echo interpretation behaviors such as escapes and line continuation + # pad the value with leading/trailing X to protect whitespace esc_arg="X${arg}X" case "$esc_tool" in 'sed') - # mac sed does not support group-0, so pattern uses group-1 - esc_arg="$(printf '%s' "$esc_arg" | sed -e 's@\([$"\\`]\)@\\\1@g')" + # Mac bsd_sed does not support group-0, so pattern uses group-1 + # Solaris sed only proceses lines with trailing newline, passing in an extra newline + # sed will consume the trailing newline + esc_arg="$(printf '%s\n' "$esc_arg" | sed -e 's@\([$"\\`]\)@\\\1@g')" ;; 'awk') - esc_arg="$(printf '%s' "$esc_arg" | awk '{ gsub(/\\/, "\\\\"); print }' )" - esc_arg="$(printf '%s' "$esc_arg" | awk '{ gsub(/\$/, "\\$"); print }' )" - esc_arg="$(printf '%s' "$esc_arg" | awk '{ gsub(/\"/, "\\\""); print }' )" - esc_arg="$(printf '%s' "$esc_arg" | awk '{ gsub(/`/, "\\`"); print }' )" + esc_arg="$(printf '%s' "$esc_arg" | "$awk_exec" '{ gsub(/\\/, "\\\\"); print }' )" + esc_arg="$(printf '%s' "$esc_arg" | "$awk_exec" '{ gsub(/\$/, "\\$"); print }' )" + esc_arg="$(printf '%s' "$esc_arg" | "$awk_exec" '{ gsub(/\"/, "\\\""); print }' )" + esc_arg="$(printf '%s' "$esc_arg" | "$awk_exec" '{ gsub(/`/, "\\`"); print }' )" ;; # 'bash') # # does not depend upon `sed` or `echo` quirks