Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Cople with ctest 2.8 still compatible with previous versions (2.6.x).
[simgrid.git] / tools / tesh / tesh.c
1 /* $Id$ */
2
3 /* TESH (Test Shell) -- mini shell specialized in running test units        */
4
5 /* Copyright (c) 2007 Martin Quinson.                                       */
6 /* All rights reserved.                                                     */
7
8 /* This program is free software; you can redistribute it and/or modify it
9  * under the terms of the license (GNU LGPL) which comes with this package. */
10
11 /* specific to Borland Compiler */
12 #ifdef __BORLANDDC__
13 #pragma hdrstop
14 #endif
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
24 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      DEBUG7("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   DEBUG2("[%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       VERB1("Saw cd '%s'", dir);
58       if (chdir(dir)) {
59         ERROR2("Chdir to %s failed: %s", dir, strerror(errno));
60         ERROR1("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     INFO2("[%s] %s", filepos, line + 2);
74     break;
75   case 'P':
76     CRITICAL2("[%s] %s", filepos, line + 2);
77     break;
78
79   default:
80     ERROR2("[%s] Syntax error: %s", filepos, line);
81     ERROR1("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   char *line = NULL;
91   int line_num = 0;
92   char file_pos[256];
93
94   xbt_strbuff_t buff = xbt_strbuff_new();
95   int buffbegin = 0;
96
97   rctx = rctx_new();
98
99   while (getline(&line, &len, IN) != -1) {
100     line_num++;
101
102     /* Count the line length while checking wheather it's blank */
103     int blankline = 1;
104     int linelen = 0;
105     while (line[linelen] != '\0') {
106       if (line[linelen] != ' ' && line[linelen] != '\t'
107           && line[linelen] != '\n')
108         blankline = 0;
109       linelen++;
110     }
111
112     if (blankline) {
113       if (!rctx->cmd && !rctx->is_empty) {
114         ERROR1("[%d] Error: no command found in this chunk of lines.",
115                buffbegin);
116         ERROR1("Test suite `%s': NOK (syntax error)", testsuite_name);
117         rctx_armageddon(rctx, 1);
118       }
119       if (rctx->cmd)
120         rctx_start();
121
122       continue;
123     }
124
125     /* Deal with \ at the end of the line, and call handle_line on result */
126     int to_be_continued = 0;
127     if (linelen > 1 && line[linelen - 2] == '\\') {
128       if (linelen > 2 && line[linelen - 3] == '\\') {
129         /* Damn. Escaped \ */
130         line[linelen - 2] = '\n';
131         line[linelen - 1] = '\0';
132       } else {
133         to_be_continued = 1;
134         line[linelen - 2] = '\0';
135         linelen -= 2;
136         if (!buff->used)
137           buffbegin = line_num;
138       }
139     }
140
141     if (buff->used || to_be_continued) {
142       xbt_strbuff_append(buff, line);
143
144       if (!to_be_continued) {
145         snprintf(file_pos, 256, "%s:%d", filename, buffbegin);
146         handle_line(file_pos, buff->data);
147         xbt_strbuff_empty(buff);
148       }
149
150     } else {
151       snprintf(file_pos, 256, "%s:%d", filename, line_num);
152       handle_line(file_pos, line);
153     }
154   }
155   /* Check that last command of the file ran well */
156   if (rctx->cmd)
157     rctx_start();
158
159   /* Wait all background commands */
160
161   rctx_free(rctx);
162
163   /* Clear buffers */
164   if (line)
165     free(line);
166   xbt_strbuff_free(buff);
167
168 }
169
170 static void parse_environ()
171 {
172   char *p;
173   int i;
174   env = xbt_dict_new();
175   for (i = 0; environ[i]; i++) {
176     p = environ[i];
177     char *eq = strchr(p, '=');
178     char *key = bprintf("%.*s", (int) (eq - p), p);
179     xbt_dict_set(env, key, xbt_strdup(eq + 1), xbt_free_f);
180     free(key);
181   }
182 }
183
184 int main(int argc, char *argv[])
185 {
186
187   FILE *IN;
188   int i;
189
190   /* Ignore pipe issues.
191      They will show up when we try to send data to dead buddies,
192      but we will stop doing so when we're done with provided input */
193   struct sigaction newact;
194   memset(&newact, 0, sizeof(newact));
195   newact.sa_handler = SIG_IGN;
196   sigaction(SIGPIPE, &newact, NULL);
197
198   xbt_init(&argc, argv);
199   rctx_init();
200   parse_environ();
201
202   /* Get args */
203   for (i = 1; i < argc; i++) {
204     if (!strcmp(argv[i], "--cd")) {
205       if (i == argc - 1) {
206         ERROR0("--cd argument requires an argument");
207         exit(1);
208       }
209       if (chdir(argv[i + 1])) {
210         ERROR2("Cannot change directory to %s: %s", argv[i + 1],
211                strerror(errno));
212         exit(1);
213       }
214       INFO1("Change directory to %s", argv[i + 1]);
215       memmove(argv + i, argv + i + 2, (argc - i - 1)*sizeof(char*));
216       argc -= 2;
217       i -= 2;
218     }
219   }
220
221   /* Find the description file */
222   if (argc == 1) {
223     INFO0("Test suite from stdin");
224     testsuite_name = xbt_strdup("(stdin)");
225     handle_suite("stdin", stdin);
226     INFO0("Test suite from stdin OK");
227
228   } else {
229     for (i = 1; i < argc; i++) {
230       char *suitename = xbt_strdup(argv[i]);
231       if (!strcmp("./", suitename))
232         memmove(suitename, suitename + 2, strlen(suitename + 2));
233
234       if (!strcmp(".tesh", suitename + strlen(suitename) - 5))
235         suitename[strlen(suitename) - 5] = '\0';
236
237       INFO1("Test suite `%s'", suitename);
238       testsuite_name = suitename;
239       IN = fopen(argv[i], "r");
240       if (!IN) {
241         perror(bprintf("Impossible to open the suite file `%s'", argv[i]));
242         ERROR1("Test suite `%s': NOK (system error)", testsuite_name);
243         rctx_armageddon(rctx, 1);
244       }
245       handle_suite(suitename, IN);
246       rctx_wait_bg();
247       fclose(IN);
248       INFO1("Test suite `%s' OK", suitename);
249       free(suitename);
250     }
251   }
252
253   rctx_exit();
254   xbt_exit();
255   return 0;
256 }