Logo AND Algorithmique Numérique Distribuée

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