You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@ant.apache.org by bo...@apache.org on 2016/10/22 14:00:37 UTC

ant git commit: fix problems with argument handling of trailing space and sed/echo quirks

Repository: ant
Updated Branches:
  refs/heads/1.9.x c1bbf2fc0 -> 173c28f76


fix problems with argument handling of trailing space and sed/echo quirks


Project: http://git-wip-us.apache.org/repos/asf/ant/repo
Commit: http://git-wip-us.apache.org/repos/asf/ant/commit/173c28f7
Tree: http://git-wip-us.apache.org/repos/asf/ant/tree/173c28f7
Diff: http://git-wip-us.apache.org/repos/asf/ant/diff/173c28f7

Branch: refs/heads/1.9.x
Commit: 173c28f765f105226b8a45b301e13591a7e050f3
Parents: c1bbf2f
Author: Jeffrey Adamson <jw...@us.ibm.com>
Authored: Mon Oct 10 17:16:27 2016 -0400
Committer: Stefan Bodewig <bo...@apache.org>
Committed: Sat Oct 22 16:00:19 2016 +0200

----------------------------------------------------------------------
 src/script/ant | 54 ++++++++++++++++++++++++++++++++++++++++++++++-------
 1 file changed, 47 insertions(+), 7 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/ant/blob/173c28f7/src/script/ant
----------------------------------------------------------------------
diff --git a/src/script/ant b/src/script/ant
old mode 100755
new mode 100644
index 58e187c..8cc8f8e
--- a/src/script/ant
+++ b/src/script/ant
@@ -21,6 +21,9 @@ no_config=false
 use_jikes_default=false
 ant_exec_debug=false
 show_help=false
+
+esc_tool=sed
+
 for arg in "$@" ; do
   if [ "$arg" = "--noconfig" ] ; then
     no_config=true
@@ -35,12 +38,49 @@ for arg in "$@" ; do
     if [  my"$arg" = my"-h"  -o  my"$arg" = my"-help" ] ; then
       show_help=true
     fi
-    # escape $, ", and \ characters by inserting a \ 
-    esc_arg=`echo "$arg" | sed -e 's@[$"\\]@\\\\\\\\\0@g' `
-    # escape ` by inserting a \ 
-    esc_arg=`echo "$esc_arg" | sed -e 's@\`@\\\\\`@g'`
-    # wrap escaped arg as a quoted argument
+
+    # 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
+    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')"
+        ;;
+      '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 }' )"
+        ;;
+#      'bash')
+#        # does not depend upon `sed` or `echo` quirks
+#        # tested with bash `[ -n "${BASH_VERSION}" ]`
+#        # tested with zsh `[ -n "${ZSH_NAME}" ]`
+#        # tested with ksh93+ `ksh_ver="$(echo "$KSH_VERSION" | grep -m 1 -o '[0-9]\+' | head -n 1)"; [ "$ksh_ver" -gt 88 ]`
+#        # fails in ksh88, dash, ash
+#        esc_arg="${esc_arg//\\/\\\\}" # must be first since later patterns introduce backslash chars
+#        esc_arg="${esc_arg//\$/\\\$}"
+#        esc_arg="${esc_arg//\"/\\\"}"
+#        esc_arg="${esc_arg//\`/\\\`}"
+#        ;;
+      '*')
+        echo "could not determine escaping tool"
+        exit 1
+        ;;
+    esac
+    # remove the padding Xs added above
+    esc_arg="${esc_arg#X}"
+    esc_arg="${esc_arg%X}"
     quoted_arg="\"$esc_arg\""
+
+    if $ant_exec_debug
+    then
+        # using printf to avoid echo line continuation and escape interpretation
+        printf "arg       : %s\n" "$arg"
+        printf "quoted_arg: %s\n" "$quoted_arg"
+    fi
     ant_exec_args="$ant_exec_args $quoted_arg"
   fi
 done
@@ -337,7 +377,7 @@ else
 fi
 ant_exec_command="exec \"\$JAVACMD\" $ANT_OPTS -classpath \"\$LOCALCLASSPATH\" -Dant.home=\"\$ANT_HOME\" -Dant.library.dir=\"\$ANT_LIB\" $ant_sys_opts org.apache.tools.ant.launch.Launcher $ANT_ARGS -cp \"\$CLASSPATH\""
 if $ant_exec_debug ; then
-    echo $ant_exec_command $ant_exec_args
+    echo "$ant_exec_command $ant_exec_args"
 fi
 
-eval $ant_exec_command $ant_exec_args
+eval "$ant_exec_command $ant_exec_args"