Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
More informative messages when using the new --setenv tesh argument
[simgrid.git] / tools / tesh / tesh.c
index 27a658a..628f856 100644 (file)
@@ -84,7 +84,7 @@ static void handle_line(const char *filepos, char *line)
   }
 }
 
-static void handle_suite(const char *filename, FILE * IN)
+static void handle_suite(const char *filename, FILE * FICIN)
 {
   size_t len;
   int blankline;
@@ -99,7 +99,7 @@ static void handle_suite(const char *filename, FILE * IN)
   buff = xbt_strbuff_new();
   rctx = rctx_new();
 
-  while (getline(&line, &len, IN) != -1) {
+  while (getline(&line, &len, FICIN) != -1) {
     line_num++;
 
     /* Count the line length while checking wheather it's blank */
@@ -189,22 +189,22 @@ static void parse_environ()
 
 int main(int argc, char *argv[])
 {
-  FILE *IN = NULL;
+  FILE *FICIN = NULL;
   int i;
   char *suitename = NULL;
+  struct sigaction newact;
+
+  xbt_init(&argc, argv);
+  rctx_init();
+  parse_environ();
 
   /* Ignore pipe issues.
      They will show up when we try to send data to dead buddies,
      but we will stop doing so when we're done with provided input */
-  struct sigaction newact;
   memset(&newact, 0, sizeof(newact));
   newact.sa_handler = SIG_IGN;
   sigaction(SIGPIPE, &newact, NULL);
 
-  xbt_init(&argc, argv);
-  rctx_init();
-  parse_environ();
-
   /* Get args */
   for (i = 1; i < argc; i++) {
     if (!strcmp(argv[i], "--cd")) {
@@ -218,7 +218,21 @@ int main(int argc, char *argv[])
         exit(1);
       }
       INFO1("Change directory to %s", argv[i + 1]);
-      memmove(argv + i, argv + i + 2, (argc - i - 1)*sizeof(char*));
+      memmove(argv + i, argv + i + 2, (argc - i - 1) * sizeof(char *));
+      argc -= 2;
+      i -= 2;
+    } else if (!strcmp(argv[i], "--setenv" )) {
+      if (i == argc - 1) {
+        ERROR0("--setenv argument requires an argument");
+        exit(1);
+      }
+      char *eq = strchr(argv[i+1], '=');
+      xbt_assert1(eq,"The argument of --setenv must contain a '=' (got %s instead)",argv[i+1]);
+      char *key = bprintf("%.*s", (int) (eq - argv[i+1]), argv[i+1]);
+      xbt_dict_set(env, key, xbt_strdup(eq + 1), xbt_free_f);
+      INFO2("setting environment variable '%s' to '%s'", key, eq+1);
+      free(key);
+      memmove(argv + i, argv + i + 2, (argc - i - 1) * sizeof(char *));
       argc -= 2;
       i -= 2;
     }
@@ -243,20 +257,21 @@ int main(int argc, char *argv[])
 
       INFO1("Test suite `%s'", suitename);
       testsuite_name = suitename;
-      IN = fopen(argv[i], "r");
-      if (!IN) {
+      FICIN = fopen(argv[i], "r");
+      if (!FICIN) {
         perror(bprintf("Impossible to open the suite file `%s'", argv[i]));
         ERROR1("Test suite `%s': NOK (system error)", testsuite_name);
         rctx_armageddon(rctx, 1);
       }
-      handle_suite(suitename, IN);
+      handle_suite(suitename, FICIN);
       rctx_wait_bg();
-      fclose(IN);
+      fclose(FICIN);
       INFO1("Test suite `%s' OK", suitename);
       free(suitename);
     }
   }
 
   rctx_exit();
+  xbt_dict_free(&env);
   return 0;
 }