Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
af0ff1a8eb93976d77c91080d73018307ecacbe3
[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   /* Search end */
27   xbt_str_rtrim(line+2,"\n");
28
29   /*
30   DEBUG7("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   DEBUG2("[%s] %s",filepos,line);
37
38   switch (line[0]) {
39     case '#': break;
40
41     case '$':
42       /* further trim useless chars which are significant for in/output */
43       xbt_str_rtrim(line+2," \t");
44
45       /* Deal with CD commands here, not in rctx */
46       if (!strncmp("cd ",line+2,3)) {
47         char *dir=line+4;
48
49         if (rctx->cmd)
50           rctx_start();
51
52         /* search begining */
53         while (*(dir++) == ' ');
54         dir--;
55         VERB1("Saw cd '%s'",dir);
56         if (chdir(dir)) {
57           ERROR2("Chdir to %s failed: %s",dir,strerror(errno));
58           ERROR1("Test suite `%s': NOK (system error)", testsuite_name);
59           rctx_armageddon(rctx,4);
60         }
61         break;
62       } /* else, pushline */
63     case '&':
64     case '<':
65     case '>':
66     case '!':
67       rctx_pushline(filepos, line[0], line+2 /* pass '$ ' stuff*/);
68       break;
69
70     case 'p':
71       INFO2("[%s] %s",filepos,line+2);
72       break;
73     case 'P':
74       CRITICAL2("[%s] %s",filepos,line+2);
75       break;
76
77     default:
78       ERROR2("[%s] Syntax error: %s",filepos, line);
79       ERROR1("Test suite `%s': NOK (syntax error)",testsuite_name);
80       rctx_armageddon(rctx,1);
81       break;
82   }
83 }
84
85 static void handle_suite(const char* filename, FILE* IN) {
86   size_t len;
87   char * line = NULL;
88   int line_num=0;
89   char file_pos[256];
90
91   xbt_strbuff_t buff=xbt_strbuff_new();
92   int buffbegin = 0;
93
94   rctx = rctx_new();
95
96   while (getline(&line, &len, IN) != -1) {
97     line_num++;
98
99     /* Count the line length while checking wheather it's blank */
100     int blankline=1;
101     int linelen = 0;
102     while (line[linelen] != '\0') {
103       if (line[linelen] != ' ' && line[linelen] != '\t' && line[linelen]!='\n')
104         blankline = 0;
105       linelen++;
106     }
107
108     if (blankline) {
109       if (!rctx->cmd && !rctx->is_empty) {
110         ERROR1("[%d] Error: no command found in this chunk of lines.",
111                buffbegin);
112         ERROR1("Test suite `%s': NOK (syntax error)",testsuite_name);
113         rctx_armageddon(rctx,1);
114       }
115       if (rctx->cmd)
116         rctx_start();
117
118       continue;
119     }
120
121     /* Deal with \ at the end of the line, and call handle_line on result */
122     int to_be_continued = 0;
123     if (linelen>1 && line[linelen-2]=='\\') {
124       if (linelen>2 && line[linelen-3] == '\\') {
125         /* Damn. Escaped \ */
126         line[linelen-2] = '\n';
127         line[linelen-1] = '\0';
128       } else {
129         to_be_continued = 1;
130         line[linelen-2] = '\0';
131         linelen -= 2;
132         if (!buff->used)
133           buffbegin = line_num;
134       }
135     }
136
137     if (buff->used || to_be_continued) {
138       xbt_strbuff_append(buff,line);
139
140       if (!to_be_continued) {
141         snprintf(file_pos,256,"%s:%d",filename,buffbegin);
142         handle_line(file_pos, buff->data);
143         xbt_strbuff_empty(buff);
144       }
145
146     } else {
147       snprintf(file_pos,256,"%s:%d",filename,line_num);
148       handle_line(file_pos, line);
149     }
150   }
151   /* Check that last command of the file ran well */
152   if (rctx->cmd)
153     rctx_start();
154
155   /* Wait all background commands */
156
157   rctx_free(rctx);
158
159   /* Clear buffers */
160   if (line)
161     free(line);
162   xbt_strbuff_free(buff);
163
164 }
165
166 static void parse_environ(){
167   char *p;
168   int i;
169   env = xbt_dict_new();
170   for (i=0; environ[i];i++) {
171     p=environ[i];
172     char *eq = strchr(p,'=');
173     char *key = bprintf("%.*s",(int)(eq-p),p);
174     xbt_dict_set(env,key,xbt_strdup(eq+1),xbt_free_f);
175     free(key);
176   }
177 }
178
179 int main(int argc,char *argv[]) {
180
181   FILE *IN;
182
183   /* Ignore pipe issues.
184      They will show up when we try to send data to dead buddies,
185      but we will stop doing so when we're done with provided input */
186   struct sigaction newact;
187   memset(&newact,0, sizeof(newact));
188   newact.sa_handler=SIG_IGN;
189   sigaction(SIGPIPE,&newact,NULL);
190
191   xbt_init(&argc,argv);
192   rctx_init();
193   parse_environ();
194
195   /* Find the description file */
196   if (argc == 1) {
197     INFO0("Test suite from stdin");
198     testsuite_name = xbt_strdup("(stdin)");
199     handle_suite("stdin",stdin);
200     INFO0("Test suite from stdin OK");
201
202   } else {
203     int i;
204
205     for (i=1; i<argc; i++) {
206       char *suitename=xbt_strdup(argv[i]);
207       if (!strcmp("./",suitename))
208         memmove(suitename, suitename+2, strlen(suitename+2));
209
210       if (!strcmp(".tesh",suitename+strlen(suitename)-5))
211         suitename[strlen(suitename)-5] = '\0';
212
213       INFO1("Test suite `%s'",suitename);
214       testsuite_name = suitename;
215       IN=fopen(argv[i], "r");
216       if (!IN) {
217         perror(bprintf("Impossible to open the suite file `%s'",argv[i]));
218         ERROR1("Test suite `%s': NOK (system error)",testsuite_name);
219         rctx_armageddon(rctx,1);
220       }
221       handle_suite(suitename,IN);
222       rctx_wait_bg();
223       fclose(IN);
224       INFO1("Test suite `%s' OK",suitename);
225       free(suitename);
226     }
227   }
228
229   rctx_exit();
230   xbt_exit();
231   return 0;
232 }
233