This is the mail archive of the gcc@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

Re: Set environment variable on remote target


Andreas Schwab wrote:
Jie Zhang <jie.zhang@analog.com> writes:

So we have to use single quotes. The updated patch is attached.

This will break if the value can contain single quotes.


How about using double quotes but escaping ", \, $, and ` using backslash? The patch is attached.


Jie
diff --git a/lib/rsh.exp b/lib/rsh.exp
index 1a207a8..d846887 100644
--- a/lib/rsh.exp
+++ b/lib/rsh.exp
@@ -225,6 +225,7 @@ proc rsh_upload {desthost srcfile destfile} {
 #
 proc rsh_exec { boardname program pargs inp outp } {
     global timeout
+    global remote_env
 
     verbose "Executing $boardname:$program $pargs < $inp"
 
@@ -261,7 +262,14 @@ proc rsh_exec { boardname program pargs inp outp } {
 	set inp "/dev/null"
     }
 
-    set ret [local_exec "$RSH $rsh_useropts $hostname sh -c '$program $pargs \\; echo XYZ\\\${?}ZYX'" $inp $outp $timeout]
+    set remote_envs ""
+    foreach envvar [array names remote_env] {
+	set tmp_env "$remote_env($envvar)"
+	# Escape ", \, $, and `, which cannot be protected by double quotes.
+	regsub -all "(\[\"\\\\$`])" $tmp_env "\\\\\\1" tmp_env
+	set remote_envs "$remote_envs $envvar=\"$tmp_env\""
+    }
+    set ret [local_exec "$RSH $rsh_useropts $hostname sh -c '$remote_envs $program $pargs \\; echo XYZ\\\${?}ZYX'" $inp $outp $timeout]
     set status [lindex $ret 0]
     set output [lindex $ret 1]
 
diff --git a/lib/utils.exp b/lib/utils.exp
index 6c9ff98..6325dd8 100644
--- a/lib/utils.exp
+++ b/lib/utils.exp
@@ -414,3 +414,12 @@ proc getenv { var } {
     }
 }
 
+#
+# Set an environment variable remotely
+#
+proc remote_setenv { var val } {
+    global remote_env
+
+    set remote_env($var) $val
+}
+

Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]