Logo AND Algorithmique Numérique Distribuée

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