Logo AND Algorithmique Numérique Distribuée

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