Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Minor optimization.
[simgrid.git] / tools / tesh / run_context.c
index 36547cc..7f61ae1 100644 (file)
@@ -102,12 +102,33 @@ void rctx_armageddon(rctx_t initiator, int exitcode) {
  * Memory management
  */
 
+# ifdef __APPLE__
+/* under darwin, the environment gets added to the process at startup time. So, it's not defined at library link time, forcing us to extra tricks */
+# include <crt_externs.h>
+# define environ (*_NSGetEnviron())
+# else
+ /* the environment, as specified by the opengroup, used to initialize the process properties */
+ extern char **environ;
+# endif
+
 void rctx_empty(rctx_t rc) {
+  int i;
+  char **env_it=environ;
+   
   if (rc->cmd)
     free(rc->cmd);
   rc->cmd = NULL;
   if (rc->filepos)
     free(rc->filepos);
+  if (rc->env)
+     free(rc->env);
+   
+  for (i=0;*env_it;i++,env_it++);
+  i++;
+  rc->env_size = i;
+  rc->env = malloc(i*sizeof(char*));
+  memcpy(rc->env,environ,i*sizeof(char*)); 
+
   rc->filepos = NULL;
   rc->is_empty = 1;
   rc->is_background = 0;
@@ -121,6 +142,7 @@ void rctx_empty(rctx_t rc) {
   xbt_strbuff_empty(rc->output_got);
 }
 
+
 rctx_t rctx_new() {
   rctx_t res = xbt_new0(s_rctx_t,1);
 
@@ -142,6 +164,8 @@ void rctx_free(rctx_t rctx) {
     free(rctx->cmd);
   if (rctx->filepos)
     free(rctx->filepos);
+  if (rctx->env)
+    free(rctx->env);
   xbt_os_mutex_destroy(rctx->interruption);
   xbt_strbuff_free(rctx->input);
   xbt_strbuff_free(rctx->output_got);
@@ -235,6 +259,12 @@ void rctx_pushline(const char* filepos, char kind, char *line) {
     } else if (!strncmp(line,"output display",strlen("output display"))) {
       rctx->output = e_output_display;
       VERB1("[%s] (ignore output of next command)", filepos);
+
+    } else if (!strncmp(line,"setenv ",strlen("setenv "))) {
+      rctx->env = realloc(rctx->env,++(rctx->env_size)*sizeof(char*));
+      rctx->env[rctx->env_size-2] = xbt_strdup(line+strlen("setenv "));
+      rctx->env[rctx->env_size-1] = NULL;
+      VERB1("[%s] (ignore output of next command)", filepos);
        
     } else {
       ERROR2("%s: Malformed metacommand: %s",filepos,line);
@@ -368,7 +398,7 @@ void rctx_start(void) {
     dup2(child_out[1],2);
     close(child_out[1]);
 
-    execlp ("/bin/sh", "sh", "-c", rctx->cmd, NULL);
+    execle ("/bin/sh", "sh", "-c", rctx->cmd, NULL, rctx->env);
   }
 
   rctx->is_stoppable = 1;