tools: Use single-quotes for argument escaping in "art" script.

(Also fixes other quoting/whitespace issues for argument forwarding)

Test: `FOO=EXPANDEDFOO art '$FOO'` searches for class $FOO.
Bug: 63693291
Change-Id: Ib520f57c47cc8868635c0b2ecf41659f927a1e09
diff --git a/tools/art b/tools/art
index 2e5df91..68e82ad 100644
--- a/tools/art
+++ b/tools/art
@@ -24,7 +24,7 @@
 LIBART=libart.so
 JIT_PROFILE="no"
 VERBOSE="no"
-EXTRA_OPTIONS=""
+EXTRA_OPTIONS=()
 
 # Follow all sym links to get the program name.
 if [ z"$BASH_SOURCE" != z ]; then
@@ -108,11 +108,16 @@
   fi
 }
 
+# Given 'VAR1=VAL VAR2=VAL2 ... cmd arg1 arg2 ... argN' run the 'cmd' with the args
+# with the modified environment {VAR1=VAL,VAL2=,...}.
+#
+# Also prints the command to be run if verbose mode is enabled.
 function verbose_run() {
   if [ "$VERBOSE" = "yes" ]; then
     echo "$@"
   fi
-  eval "$@"
+
+  env "$@"
 }
 
 function run_art() {
@@ -129,7 +134,7 @@
 }
 
 while [[ "$1" = "-"* ]]; do
-  case $1 in
+  case "$1" in
   --)
     # No more arguments for this script.
     shift
@@ -149,7 +154,7 @@
   --debug)
     LIBART="libartd.so"
     # Expect that debug mode wants all checks.
-    EXTRA_OPTIONS="${EXTRA_OPTIONS} -XX:SlowDebug=true"
+    EXTRA_OPTIONS+=(-XX:SlowDebug=true)
     ;;
   --gdb)
     LIBART="libartd.so"
@@ -217,7 +222,7 @@
 
 if [ "$PERF" != "" ]; then
   LAUNCH_WRAPPER="perf record -g -o $ANDROID_DATA/perf.data -e cycles:u $LAUNCH_WRAPPER"
-  EXTRA_OPTIONS="-Xcompiler-option --generate-debug-info"
+  EXTRA_OPTIONS+=(-Xcompiler-option --generate-debug-info)
 fi
 
 if [ "$JIT_PROFILE" = "yes" ]; then
@@ -251,18 +256,15 @@
   rm -rf $ANDROID_DATA/dalvik-cache/$ARCHS/*
 
   # Append arguments so next invocation of run_art uses the profile.
-  EXTRA_OPTIONS="$EXTRA_OPTIONS -Xcompiler-option --profile-file=$PROFILE_PATH"
+  EXTRA_OPTIONS+=(-Xcompiler-option --profile-file="$PROFILE_PATH")
 fi
 
-# Protect additional arguments in quotes to preserve whitespaces when evaluated.
-# This is for run-jdwp-test.sh which uses this script and has arguments with
-# whitespaces when running on device.
-while [ $# -gt 0 ]; do
-  EXTRA_OPTIONS="$EXTRA_OPTIONS \"$1\""
-  shift
-done
+# Protect additional arguments in quotes to preserve whitespaces (used by
+# run-jdwp-test.sh when running on device), '$' (may be used as part of
+# classpath) and other special characters when evaluated.
+EXTRA_OPTIONS+=("$@")
 
-run_art $EXTRA_OPTIONS
+run_art "${EXTRA_OPTIONS[@]}"
 EXIT_STATUS=$?
 
 if [ "$PERF" != "" ]; then