Logo AND Algorithmique Numérique Distribuée

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