Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
model-checker : get hash of local and global variables which are not pointers
[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 int sort_len = 19;              /* length of the prefix to sort */
22 char *option;
23 int coverage = 0;        /* whether the code coverage is enable */
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         linelen -= 2;
145         if (!buff->used)
146           buffbegin = line_num;
147       }
148     }
149
150     if (buff->used || to_be_continued) {
151       xbt_strbuff_append(buff, line);
152
153       if (!to_be_continued) {
154         snprintf(file_pos, 256, "%s:%d", filename, buffbegin);
155         handle_line(file_pos, buff->data);
156         xbt_strbuff_empty(buff);
157       }
158
159     } else {
160       snprintf(file_pos, 256, "%s:%d", filename, line_num);
161       handle_line(file_pos, line);
162     }
163   }
164   /* Check that last command of the file ran well */
165   if (rctx->cmd)
166     rctx_start();
167
168   /* Wait all background commands */
169
170   rctx_free(rctx);
171
172   /* Clear buffers */
173   free(line);
174   xbt_strbuff_free(buff);
175
176 }
177
178 static void parse_environ()
179 {
180   char *p;
181   int i;
182   char *eq = NULL;
183   char *key = NULL;
184   env = xbt_dict_new_homogeneous(xbt_free_f);
185   for (i = 0; environ[i]; i++) {
186     p = environ[i];
187     eq = strchr(p, '=');
188     key = bprintf("%.*s", (int) (eq - p), p);
189     xbt_dict_set(env, key, xbt_strdup(eq + 1), NULL);
190     free(key);
191   }
192 }
193
194 int main(int argc, char *argv[])
195 {
196   FILE *IN = NULL;
197   int i;
198   char *suitename = NULL;
199   struct sigaction newact;
200
201   XBT_LOG_CONNECT(tesh);
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), NULL);
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         char *newoption = bprintf("%s --cfg=%s", option, argv[i+1]);
252         free(option);
253         option = newoption;
254       }
255       XBT_INFO("Add option \'--cfg=%s\' to command line",argv[i+1]);
256       memmove(argv + i, argv + i + 2, (argc - i - 1) * sizeof(char *));
257       argc -= 2;
258       i -= 2;
259     }
260     else if (!strcmp(argv[i], "--enable-coverage" )){
261       coverage = 1;
262       XBT_INFO("Enable coverage");
263       memmove(argv + i, argv + i + 1, (argc - i - 1) * sizeof(char *));
264       argc -= 1;
265       i -= 1;
266     }
267   }
268
269   /* Find the description file */
270   if (argc == 1) {
271     XBT_INFO("Test suite from stdin");
272     testsuite_name = "(stdin)";
273     handle_suite(testsuite_name, stdin);
274     rctx_wait_bg();
275     XBT_INFO("Test suite from stdin OK");
276
277   } else {
278     for (i = 1; i < argc; i++) {
279       suitename = xbt_strdup(argv[i]);
280       if (!strncmp("./", suitename, 2))
281         memmove(suitename, suitename + 2, strlen(suitename + 2));
282
283       if (strlen(suitename) > 5 &&
284           !strcmp(".tesh", suitename + strlen(suitename) - 5))
285         suitename[strlen(suitename) - 5] = '\0';
286
287       XBT_INFO("Test suite `%s'", suitename);
288       testsuite_name = suitename;
289       IN = fopen(argv[i], "r");
290       if (!IN) {
291         perror(bprintf("Impossible to open the suite file `%s'", argv[i]));
292         XBT_ERROR("Test suite `%s': NOK (system error)", testsuite_name);
293         rctx_armageddon(rctx, 1);
294       }
295       handle_suite(suitename, IN);
296       rctx_wait_bg();
297       fclose(IN);
298       XBT_INFO("Test suite `%s' OK", suitename);
299       free(suitename);
300     }
301   }
302
303   rctx_exit();
304   xbt_dict_free(&env);
305   free(option);
306   return 0;
307 }