Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
2b1068f93fd70633f5dedd6ad3847189adce51e9
[simgrid.git] / tools / tesh / tesh.c
1 /* TESH (Test Shell) -- mini shell specialized in running test units        */
2
3 /* Copyright (c) 2007, 2008, 2009, 2010. The SimGrid Team.
4  * All rights reserved.                                                     */
5
6 /* This program is free software; you can redistribute it and/or modify it
7  * under the terms of the license (GNU LGPL) which comes with this package. */
8
9 /* specific to Borland Compiler */
10 #ifdef __BORLANDDC__
11 #pragma hdrstop
12 #endif
13
14 #include "tesh.h"
15 #include "xbt.h"
16
17 XBT_LOG_NEW_DEFAULT_CATEGORY(tesh, "TEst SHell utility");
18
19 /*** Options ***/
20 int timeout_value = 5;          /* child timeout value */
21
22 char *testsuite_name;
23 static void handle_line(const char *filepos, char *line)
24 {
25   /* Search end */
26   xbt_str_rtrim(line + 2, "\n");
27
28   /*
29      DEBUG7("rctx={%s,in={%d,>>%10s<<},exp={%d,>>%10s<<},got={%d,>>%10s<<}}",
30      rctx->cmd,
31      rctx->input->used,        rctx->input->data,
32      rctx->output_wanted->used,rctx->output_wanted->data,
33      rctx->output_got->used,   rctx->output_got->data);
34    */
35   DEBUG2("[%s] %s", filepos, line);
36
37   switch (line[0]) {
38   case '#':
39     break;
40
41   case '$':
42     /* further trim useless chars which are significant for in/output */
43     xbt_str_rtrim(line + 2, " \t");
44
45     /* Deal with CD commands here, not in rctx */
46     if (!strncmp("cd ", line + 2, 3)) {
47       char *dir = line + 4;
48
49       if (rctx->cmd)
50         rctx_start();
51
52       /* search beginning */
53       while (*(dir++) == ' ');
54       dir--;
55       VERB1("Saw cd '%s'", dir);
56       if (chdir(dir)) {
57         ERROR2("Chdir to %s failed: %s", dir, strerror(errno));
58         ERROR1("Test suite `%s': NOK (system error)", testsuite_name);
59         rctx_armageddon(rctx, 4);
60       }
61       break;
62     }                           /* else, pushline */
63   case '&':
64   case '<':
65   case '>':
66   case '!':
67     rctx_pushline(filepos, line[0], line + 2 /* pass '$ ' stuff */ );
68     break;
69
70   case 'p':
71     INFO2("[%s] %s", filepos, line + 2);
72     break;
73   case 'P':
74     CRITICAL2("[%s] %s", filepos, line + 2);
75     break;
76
77   default:
78     ERROR2("[%s] Syntax error: %s", filepos, line);
79     ERROR1("Test suite `%s': NOK (syntax error)", testsuite_name);
80     rctx_armageddon(rctx, 1);
81     break;
82   }
83 }
84
85 static void handle_suite(const char *filename, FILE * IN)
86 {
87   size_t len;
88   int blankline;
89   int linelen;
90   char *line = NULL;
91   int line_num = 0;
92   char file_pos[256];
93   int to_be_continued;
94   int buffbegin = 0;
95   xbt_strbuff_t buff = NULL;
96
97   buff = xbt_strbuff_new();
98   rctx = rctx_new();
99
100   while (getline(&line, &len, IN) != -1) {
101     line_num++;
102
103     /* Count the line length while checking wheather it's blank */
104     blankline = 1;
105     linelen = 0;
106
107     while (line[linelen] != '\0') {
108       if (line[linelen] != ' ' && line[linelen] != '\t'
109           && line[linelen] != '\n')
110         blankline = 0;
111       linelen++;
112     }
113
114     if (blankline) {
115       if (!rctx->cmd && !rctx->is_empty) {
116         ERROR1("[%d] Error: no command found in this chunk of lines.",
117                buffbegin);
118         ERROR1("Test suite `%s': NOK (syntax error)", testsuite_name);
119         rctx_armageddon(rctx, 1);
120       }
121       if (rctx->cmd)
122         rctx_start();
123
124       continue;
125     }
126
127     /* Deal with \ at the end of the line, and call handle_line on result */
128     to_be_continued = 0;
129     if (linelen > 1 && line[linelen - 2] == '\\') {
130       if (linelen > 2 && line[linelen - 3] == '\\') {
131         /* Damn. Escaped \ */
132         line[linelen - 2] = '\n';
133         line[linelen - 1] = '\0';
134       } else {
135         to_be_continued = 1;
136         line[linelen - 2] = '\0';
137         linelen -= 2;
138         if (!buff->used)
139           buffbegin = line_num;
140       }
141     }
142
143     if (buff->used || to_be_continued) {
144       xbt_strbuff_append(buff, line);
145
146       if (!to_be_continued) {
147         snprintf(file_pos, 256, "%s:%d", filename, buffbegin);
148         handle_line(file_pos, buff->data);
149         xbt_strbuff_empty(buff);
150       }
151
152     } else {
153       snprintf(file_pos, 256, "%s:%d", filename, line_num);
154       handle_line(file_pos, line);
155     }
156   }
157   /* Check that last command of the file ran well */
158   if (rctx->cmd)
159     rctx_start();
160
161   /* Wait all background commands */
162
163   rctx_free(rctx);
164
165   /* Clear buffers */
166   if (line)
167     free(line);
168   xbt_strbuff_free(buff);
169
170 }
171
172 static void parse_environ()
173 {
174   char *p;
175   int i;
176   char *eq = NULL;
177   char *key = NULL;
178   env = xbt_dict_new();
179   for (i = 0; environ[i]; i++) {
180     p = environ[i];
181     eq = strchr(p, '=');
182     key = bprintf("%.*s", (int) (eq - p), p);
183     xbt_dict_set(env, key, xbt_strdup(eq + 1), xbt_free_f);
184     free(key);
185   }
186 }
187
188 int main(int argc, char *argv[])
189 {
190   FILE *IN = NULL;
191   int i;
192   char *suitename = NULL;
193
194   /* Ignore pipe issues.
195      They will show up when we try to send data to dead buddies,
196      but we will stop doing so when we're done with provided input */
197   struct sigaction newact;
198   memset(&newact, 0, sizeof(newact));
199   newact.sa_handler = SIG_IGN;
200   sigaction(SIGPIPE, &newact, NULL);
201
202   xbt_init(&argc, argv);
203   rctx_init();
204   parse_environ();
205
206   /* Get args */
207   for (i = 1; i < argc; i++) {
208     if (!strcmp(argv[i], "--cd")) {
209       if (i == argc - 1) {
210         ERROR0("--cd argument requires an argument");
211         exit(1);
212       }
213       if (chdir(argv[i + 1])) {
214         ERROR2("Cannot change directory to %s: %s", argv[i + 1],
215                strerror(errno));
216         exit(1);
217       }
218       INFO1("Change directory to %s", argv[i + 1]);
219       memmove(argv + i, argv + i + 2, (argc - i - 1)*sizeof(char*));
220       argc -= 2;
221       i -= 2;
222     }
223   }
224
225   /* Find the description file */
226   if (argc == 1) {
227     INFO0("Test suite from stdin");
228     testsuite_name = xbt_strdup("(stdin)");
229     handle_suite("stdin", stdin);
230     INFO0("Test suite from stdin OK");
231
232   } else {
233     for (i = 1; i < argc; i++) {
234       suitename = xbt_strdup(argv[i]);
235       if (!strcmp("./", suitename))
236         memmove(suitename, suitename + 2, strlen(suitename + 2));
237
238       if (!strcmp(".tesh", suitename + strlen(suitename) - 5))
239         suitename[strlen(suitename) - 5] = '\0';
240
241       INFO1("Test suite `%s'", suitename);
242       testsuite_name = suitename;
243       IN = fopen(argv[i], "r");
244       if (!IN) {
245         perror(bprintf("Impossible to open the suite file `%s'", argv[i]));
246         ERROR1("Test suite `%s': NOK (system error)", testsuite_name);
247         rctx_armageddon(rctx, 1);
248       }
249       handle_suite(suitename, IN);
250       rctx_wait_bg();
251       fclose(IN);
252       INFO1("Test suite `%s' OK", suitename);
253       free(suitename);
254     }
255   }
256
257   rctx_exit();
258   return 0;
259 }