Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Cosmetics: handle exception inside exception handler.
[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 "simgrid_config.h" //For getline, keep that include first
15
16 #include "tesh.h"
17 #include "xbt.h"
18
19 XBT_LOG_NEW_DEFAULT_CATEGORY(tesh, "TEst SHell utility");
20
21 /*** Options ***/
22 int timeout_value = 5;          /* child timeout value */
23 int sort_len = 19;              /* length of the prefix to sort */
24 char *option;
25
26 rctx_t rctx;
27 const char *testsuite_name;
28
29 xbt_dict_t env;
30
31 static void handle_line(const char *filepos, char *line)
32 {
33   /* Search end */
34   xbt_str_rtrim(line + 2, "\n");
35
36   /*
37      XBT_DEBUG("rctx={%s,in={%d,>>%10s<<},exp={%d,>>%10s<<},got={%d,>>%10s<<}}",
38      rctx->cmd,
39      rctx->input->used,        rctx->input->data,
40      rctx->output_wanted->used,rctx->output_wanted->data,
41      rctx->output_got->used,   rctx->output_got->data);
42    */
43   XBT_DEBUG("[%s] %s", filepos, line);
44
45   switch (line[0]) {
46   case '#':
47     break;
48
49   case '$':
50     /* further trim useless chars which are significant for in/output */
51     xbt_str_rtrim(line + 2, " \t");
52
53     /* Deal with CD commands here, not in rctx */
54     if (!strncmp("cd ", line + 2, 3)) {
55       char *dir = line + 4;
56
57       if (rctx->cmd)
58         rctx_start();
59
60       /* search beginning */
61       while (*(dir++) == ' ');
62       dir--;
63       XBT_VERB("Saw cd '%s'", dir);
64       if (chdir(dir)) {
65         XBT_ERROR("Chdir to %s failed: %s", dir, strerror(errno));
66         XBT_ERROR("Test suite `%s': NOK (system error)", testsuite_name);
67         rctx_armageddon(rctx, 4);
68       }
69       break;
70     }                           /* else, pushline */
71   case '&':
72   case '<':
73   case '>':
74   case '!':
75     rctx_pushline(filepos, line[0], line + 2 /* pass '$ ' stuff */ );
76     break;
77
78   case 'p':
79     XBT_INFO("[%s] %s", filepos, line + 2);
80     break;
81   case 'P':
82     XBT_CRITICAL("[%s] %s", filepos, line + 2);
83     break;
84
85   default:
86     XBT_ERROR("[%s] Syntax error: %s", filepos, line);
87     XBT_ERROR("Test suite `%s': NOK (syntax error)", testsuite_name);
88     rctx_armageddon(rctx, 1);
89     break;
90   }
91 }
92
93 static void handle_suite(const char *filename, FILE * IN)
94 {
95   size_t len;
96   int blankline;
97   int linelen;
98   char *line = NULL;
99   int line_num = 0;
100   char file_pos[256];
101   int to_be_continued;
102   int buffbegin = 0;
103   xbt_strbuff_t buff = NULL;
104
105   buff = xbt_strbuff_new();
106   rctx = rctx_new();
107
108   while (getline(&line, &len, IN) != -1) {
109     line_num++;
110
111     /* Count the line length while checking wheather it's blank */
112     blankline = 1;
113     linelen = 0;
114
115     while (line[linelen] != '\0') {
116       if (line[linelen] != ' ' && line[linelen] != '\t'
117           && line[linelen] != '\n')
118         blankline = 0;
119       linelen++;
120     }
121
122     if (blankline) {
123       if (!rctx->cmd && !rctx->is_empty) {
124         XBT_ERROR("[%d] Error: no command found in this chunk of lines.",
125                buffbegin);
126         XBT_ERROR("Test suite `%s': NOK (syntax error)", testsuite_name);
127         rctx_armageddon(rctx, 1);
128       }
129       if (rctx->cmd)
130         rctx_start();
131
132       continue;
133     }
134
135     /* Deal with \ at the end of the line, and call handle_line on result */
136     to_be_continued = 0;
137     if (linelen > 1 && line[linelen - 2] == '\\') {
138       if (linelen > 2 && line[linelen - 3] == '\\') {
139         /* Damn. Escaped \ */
140         line[linelen - 2] = '\n';
141         line[linelen - 1] = '\0';
142       } else {
143         to_be_continued = 1;
144         line[linelen - 2] = '\0';
145         linelen -= 2;
146         if (!buff->used)
147           buffbegin = line_num;
148       }
149     }
150
151     if (buff->used || to_be_continued) {
152       xbt_strbuff_append(buff, line);
153
154       if (!to_be_continued) {
155         snprintf(file_pos, 256, "%s:%d", filename, buffbegin);
156         handle_line(file_pos, buff->data);
157         xbt_strbuff_empty(buff);
158       }
159
160     } else {
161       snprintf(file_pos, 256, "%s:%d", filename, line_num);
162       handle_line(file_pos, line);
163     }
164   }
165   /* Check that last command of the file ran well */
166   if (rctx->cmd)
167     rctx_start();
168
169   /* Wait all background commands */
170
171   rctx_free(rctx);
172
173   /* Clear buffers */
174   if (line)
175     free(line);
176   xbt_strbuff_free(buff);
177
178 }
179
180 static void parse_environ()
181 {
182   char *p;
183   int i;
184   char *eq = NULL;
185   char *key = NULL;
186   env = xbt_dict_new();
187   for (i = 0; environ[i]; i++) {
188     p = environ[i];
189     eq = strchr(p, '=');
190     key = bprintf("%.*s", (int) (eq - p), p);
191     xbt_dict_set(env, key, xbt_strdup(eq + 1), xbt_free_f);
192     free(key);
193   }
194 }
195
196 int main(int argc, char *argv[])
197 {
198   FILE *IN = NULL;
199   int i;
200   char *suitename = NULL;
201   struct sigaction newact;
202   xbt_init(&argc, argv);
203   rctx_init();
204   parse_environ();
205
206   /* Ignore pipe issues.
207      They will show up when we try to send data to dead buddies,
208      but we will stop doing so when we're done with provided input */
209   memset(&newact, 0, sizeof(newact));
210   newact.sa_handler = SIG_IGN;
211   sigaction(SIGPIPE, &newact, NULL);
212
213   /* Get args */
214   for (i = 1; i < argc; i++) {
215     if (!strcmp(argv[i], "--cd")) {
216       if (i == argc - 1) {
217         XBT_ERROR("--cd argument requires an argument");
218         exit(1);
219       }
220       if (chdir(argv[i + 1])) {
221         XBT_ERROR("Cannot change directory to %s: %s", argv[i + 1],
222                strerror(errno));
223         exit(1);
224       }
225       XBT_INFO("Change directory to %s", argv[i + 1]);
226       memmove(argv + i, argv + i + 2, (argc - i - 1) * sizeof(char *));
227       argc -= 2;
228       i -= 2;
229     } else if (!strcmp(argv[i], "--setenv" )) {
230       if (i == argc - 1) {
231         XBT_ERROR("--setenv argument requires an argument");
232         exit(1);
233       }
234       char *eq = strchr(argv[i+1], '=');
235       xbt_assert(eq,"The argument of --setenv must contain a '=' (got %s instead)",argv[i+1]);
236       char *key = bprintf("%.*s", (int) (eq - argv[i+1]), argv[i+1]);
237       xbt_dict_set(env, key, xbt_strdup(eq + 1), xbt_free_f);
238       XBT_INFO("setting environment variable '%s' to '%s'", key, eq+1);
239       free(key);
240       memmove(argv + i, argv + i + 2, (argc - i - 1) * sizeof(char *));
241       argc -= 2;
242       i -= 2;
243     } else if (!strcmp(argv[i], "--cfg" )) {
244       if (i == argc - 1) {
245             XBT_ERROR("--cfg argument requires an argument");
246             exit(1);
247       }
248       if(!option){ //if option is NULL
249         option = bprintf("--cfg=%s",argv[i+1]);
250       }else{
251         option = bprintf("%s --cfg=%s",option,argv[i+1]);
252       }
253       XBT_INFO("Add option \'--cfg=%s\' to command line",argv[i+1]);
254       memmove(argv + i, argv + i + 2, (argc - i - 1) * sizeof(char *));
255       argc -= 2;
256       i -= 2;
257     }
258   }
259
260   /* Find the description file */
261   if (argc == 1) {
262     XBT_INFO("Test suite from stdin");
263     testsuite_name = "(stdin)";
264     handle_suite(testsuite_name, stdin);
265     rctx_wait_bg();
266     XBT_INFO("Test suite from stdin OK");
267
268   } else {
269     for (i = 1; i < argc; i++) {
270       suitename = xbt_strdup(argv[i]);
271       if (!strncmp("./", suitename, 2))
272         memmove(suitename, suitename + 2, strlen(suitename + 2));
273
274       if (strlen(suitename) > 5 &&
275           !strcmp(".tesh", suitename + strlen(suitename) - 5))
276         suitename[strlen(suitename) - 5] = '\0';
277
278       XBT_INFO("Test suite `%s'", suitename);
279       testsuite_name = suitename;
280       IN = fopen(argv[i], "r");
281       if (!IN) {
282         perror(bprintf("Impossible to open the suite file `%s'", argv[i]));
283         XBT_ERROR("Test suite `%s': NOK (system error)", testsuite_name);
284         rctx_armageddon(rctx, 1);
285       }
286       handle_suite(suitename, IN);
287       rctx_wait_bg();
288       fclose(IN);
289       XBT_INFO("Test suite `%s' OK", suitename);
290       free(suitename);
291     }
292   }
293
294   rctx_exit();
295   xbt_dict_free(&env);
296   xbt_free_f(option);
297   return 0;
298 }