Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Add a way to the tesh syntax to log at CRITICAL level (to ease output grepping)
[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
25 static void handle_line(const char * filepos, char *line) {
26   int pos;
27
28   /* Search end */
29   xbt_str_rtrim(line+2,"\n");
30
31   /*
32   DEBUG7("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   DEBUG2("[%s] %s",filepos,line);
39
40   switch (line[0]) {
41   case '#': 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 begining */
55       while (*(dir++) == ' ');
56       dir--;
57       VERB1("Saw cd '%s'",dir);
58       if (chdir(dir)) {
59         char buff[256];
60         strerror_r(errno, buff, 256);
61
62         ERROR2("Chdir to %s failed: %s",dir+pos+2,buff);
63         exit(4);
64       }
65       break;
66     } /* else, pushline */
67   case '&':
68   case '<':
69   case '>':
70   case '!':
71     rctx_pushline(filepos, line[0], line+2 /* pass '$ ' stuff*/);    
72     break;
73
74   case 'p':
75     INFO2("[%s] %s",filepos,line+2);
76     break;
77   case 'P':
78     CRITICAL2("[%s] %s",filepos,line+2);
79     break;
80
81   default:
82     ERROR2("[%s] Syntax error: %s",filepos, line);
83     exit(1);
84     break;
85   }
86 }
87
88 static void handle_suite(const char* filename, FILE* IN) {
89   size_t len;
90   char * line = NULL;
91   int line_num=0;
92   char file_pos[256];
93
94   buff_t buff=buff_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' && line[linelen]!='\n')
107         blankline = 0;
108       linelen++;
109     }
110     
111     if (blankline) {
112       if (!rctx->cmd && !rctx->is_empty) {
113         ERROR1("[%d] Error: no command found in this chunk of lines.",
114                buffbegin);
115         exit(1);
116       }
117       if (rctx->cmd)
118         rctx_start();
119
120       continue;
121     }
122
123     /* Deal with \ at the end of the line, and call handle_line on result */
124     int to_be_continued = 0;
125     if (linelen>1 && line[linelen-2]=='\\') {
126       if (linelen>2 && line[linelen-3] == '\\') {
127         /* Damn. Escaped \ */
128         line[linelen-2] = '\n';
129         line[linelen-1] = '\0';
130       } else {
131         to_be_continued = 1;
132         line[linelen-2] = '\0';
133         linelen -= 2;  
134         if (!buff->used)
135           buffbegin = line_num;
136       }
137     }
138
139     if (buff->used || to_be_continued) { 
140       buff_append(buff,line);
141
142       if (!to_be_continued) {
143         snprintf(file_pos,256,"%s:%d",filename,buffbegin);
144         handle_line(file_pos, buff->data);    
145         buff_empty(buff);
146       }
147         
148     } else {
149       snprintf(file_pos,256,"%s:%d",filename,line_num);
150       handle_line(file_pos, line);    
151     }
152   }
153   /* Check that last command of the file ran well */
154   if (rctx->cmd) 
155     rctx_start();
156
157   /* Wait all background commands */
158
159   rctx_free(rctx);
160
161   /* Clear buffers */
162   if (line)
163     free(line);
164   buff_free(buff);
165
166 }
167
168 int main(int argc,char *argv[]) {
169
170   FILE *IN;
171
172   /* Ignore pipe issues.
173      They will show up when we try to send data to dead buddies, 
174      but we will stop doing so when we're done with provided input */
175   struct sigaction newact;
176   memset(&newact,0, sizeof(newact));
177   newact.sa_handler=SIG_IGN;
178   sigaction(SIGPIPE,&newact,NULL);
179    
180   xbt_init(&argc,argv);
181   rctx_init();
182
183   /* Find the description file */
184   if (argc == 1) {
185     INFO0("Test suite from stdin");
186     handle_suite("stdin",stdin);
187     INFO0("Test suite from stdin OK");
188      
189   } else {
190     int i;
191      
192     for (i=1; i<argc; i++) {
193       char *suitename=xbt_strdup(argv[i]);
194       if (!strcmp("./",suitename))
195         memmove(suitename, suitename+2, strlen(suitename+2));
196
197       if (!strcmp(".tesh",suitename+strlen(suitename)-5))
198         suitename[strlen(suitename)-5] = '\0';
199
200       INFO1("Test suite `%s'",suitename);
201       IN=fopen(argv[i], "r");
202       if (!IN) {
203         perror(bprintf("Impossible to open the suite file `%s'",argv[i]));
204         exit(1);
205        }
206       handle_suite(suitename,IN);
207       rctx_wait_bg();
208       fclose(IN); //->leads to segfault on amd64...
209       INFO1("Test suite `%s' OK",suitename);
210       free(suitename);
211     }
212   }
213
214   rctx_exit();
215   xbt_exit();
216   return 0;  
217 }
218