Logo AND Algorithmique Numérique Distribuée

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