Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Do not duplicate rctx_wait_bg in rctx_exit
[simgrid.git] / tools / tesh / tesh.c
index 159fd4a..f45d0b1 100644 (file)
@@ -1,13 +1,13 @@
-/* $Id$ */
-
 /* TESH (Test Shell) -- mini shell specialized in running test units        */
 
-/* Copyright (c) 2007 Martin Quinson.                                       */
-/* All rights reserved.                                                     */
+/* Copyright (c) 2007, 2008, 2009, 2010. The SimGrid Team.
+ * All rights reserved.                                                     */
 
 /* This program is free software; you can redistribute it and/or modify it
  * under the terms of the license (GNU LGPL) which comes with this package. */
 
+#include "simgrid_config.h"     /* FILE for getline */
+
 /* specific to Borland Compiler */
 #ifdef __BORLANDDC__
 #pragma hdrstop
@@ -21,7 +21,7 @@ XBT_LOG_NEW_DEFAULT_CATEGORY(tesh, "TEst SHell utility");
 /*** Options ***/
 int timeout_value = 5;          /* child timeout value */
 
-char *testsuite_name;
+const char *testsuite_name;
 static void handle_line(const char *filepos, char *line)
 {
   /* Search end */
@@ -84,24 +84,28 @@ 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;
+  int linelen;
   char *line = NULL;
   int line_num = 0;
   char file_pos[256];
-
-  xbt_strbuff_t buff = xbt_strbuff_new();
+  int to_be_continued;
   int buffbegin = 0;
+  xbt_strbuff_t buff = NULL;
 
+  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 */
-    int blankline = 1;
-    int linelen = 0;
+    blankline = 1;
+    linelen = 0;
+
     while (line[linelen] != '\0') {
       if (line[linelen] != ' ' && line[linelen] != '\t'
           && line[linelen] != '\n')
@@ -123,7 +127,7 @@ static void handle_suite(const char *filename, FILE * IN)
     }
 
     /* Deal with \ at the end of the line, and call handle_line on result */
-    int to_be_continued = 0;
+    to_be_continued = 0;
     if (linelen > 1 && line[linelen - 2] == '\\') {
       if (linelen > 2 && line[linelen - 3] == '\\') {
         /* Damn. Escaped \ */
@@ -171,11 +175,13 @@ static void parse_environ()
 {
   char *p;
   int i;
+  char *eq = NULL;
+  char *key = NULL;
   env = xbt_dict_new();
   for (i = 0; environ[i]; i++) {
     p = environ[i];
-    char *eq = strchr(p, '=');
-    char *key = bprintf("%.*s", (int) (eq - p), p);
+    eq = strchr(p, '=');
+    key = bprintf("%.*s", (int) (eq - p), p);
     xbt_dict_set(env, key, xbt_strdup(eq + 1), xbt_free_f);
     free(key);
   }
@@ -183,22 +189,22 @@ static void parse_environ()
 
 int main(int argc, char *argv[])
 {
-
-  FILE *IN;
+  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")) {
@@ -212,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;
     }
@@ -221,36 +241,38 @@ int main(int argc, char *argv[])
   /* Find the description file */
   if (argc == 1) {
     INFO0("Test suite from stdin");
-    testsuite_name = xbt_strdup("(stdin)");
-    handle_suite("stdin", stdin);
+    testsuite_name = "(stdin)";
+    handle_suite(testsuite_name, stdin);
+    rctx_wait_bg();
     INFO0("Test suite from stdin OK");
 
   } else {
     for (i = 1; i < argc; i++) {
-      char *suitename = xbt_strdup(argv[i]);
-      if (!strcmp("./", suitename))
+      suitename = xbt_strdup(argv[i]);
+      if (!strncmp("./", suitename, 2))
         memmove(suitename, suitename + 2, strlen(suitename + 2));
 
-      if (!strcmp(".tesh", suitename + strlen(suitename) - 5))
+      if (strlen(suitename) > 5 &&
+          !strcmp(".tesh", suitename + strlen(suitename) - 5))
         suitename[strlen(suitename) - 5] = '\0';
 
       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_exit();
+  xbt_dict_free(&env);
   return 0;
 }