Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Export public data in a way where we can specify that they are extern C
[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         char buff[256];
58         strerror_r(errno, buff, 256);
59
60         perror(bprintf("Chdir to %s failed: %s",dir,buff));
61         ERROR1("Test suite `%s': NOK (system error)",testsuite_name);    
62         exit(4);
63       }
64       break;
65     } /* else, pushline */
66   case '&':
67   case '<':
68   case '>':
69   case '!':
70     rctx_pushline(filepos, line[0], line+2 /* pass '$ ' stuff*/);    
71     break;
72
73   case 'p':
74     INFO2("[%s] %s",filepos,line+2);
75     break;
76   case 'P':
77     CRITICAL2("[%s] %s",filepos,line+2);
78     break;
79
80   default:
81     ERROR2("[%s] Syntax error: %s",filepos, line);
82     ERROR1("Test suite `%s': NOK (syntax error)",testsuite_name);
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         ERROR1("Test suite `%s': NOK (syntax error)",testsuite_name);
116         exit(1);
117       }
118       if (rctx->cmd)
119         rctx_start();
120
121       continue;
122     }
123
124     /* Deal with \ at the end of the line, and call handle_line on result */
125     int to_be_continued = 0;
126     if (linelen>1 && line[linelen-2]=='\\') {
127       if (linelen>2 && line[linelen-3] == '\\') {
128         /* Damn. Escaped \ */
129         line[linelen-2] = '\n';
130         line[linelen-1] = '\0';
131       } else {
132         to_be_continued = 1;
133         line[linelen-2] = '\0';
134         linelen -= 2;  
135         if (!buff->used)
136           buffbegin = line_num;
137       }
138     }
139
140     if (buff->used || to_be_continued) { 
141       buff_append(buff,line);
142
143       if (!to_be_continued) {
144         snprintf(file_pos,256,"%s:%d",filename,buffbegin);
145         handle_line(file_pos, buff->data);    
146         buff_empty(buff);
147       }
148         
149     } else {
150       snprintf(file_pos,256,"%s:%d",filename,line_num);
151       handle_line(file_pos, line);    
152     }
153   }
154   /* Check that last command of the file ran well */
155   if (rctx->cmd) 
156     rctx_start();
157
158   /* Wait all background commands */
159
160   rctx_free(rctx);
161
162   /* Clear buffers */
163   if (line)
164     free(line);
165   buff_free(buff);
166
167 }
168
169 int main(int argc,char *argv[]) {
170
171   FILE *IN;
172
173   /* Ignore pipe issues.
174      They will show up when we try to send data to dead buddies, 
175      but we will stop doing so when we're done with provided input */
176   struct sigaction newact;
177   memset(&newact,0, sizeof(newact));
178   newact.sa_handler=SIG_IGN;
179   sigaction(SIGPIPE,&newact,NULL);
180    
181   xbt_init(&argc,argv);
182   rctx_init();
183
184   /* Find the description file */
185   if (argc == 1) {
186     INFO0("Test suite from stdin");
187     handle_suite("stdin",stdin);
188     INFO0("Test suite from stdin OK");
189      
190   } else {
191     int i;
192      
193     for (i=1; i<argc; i++) {
194       char *suitename=xbt_strdup(argv[i]);
195       if (!strcmp("./",suitename))
196         memmove(suitename, suitename+2, strlen(suitename+2));
197
198       if (!strcmp(".tesh",suitename+strlen(suitename)-5))
199         suitename[strlen(suitename)-5] = '\0';
200
201       INFO1("Test suite `%s'",suitename);
202       testsuite_name = suitename;
203       IN=fopen(argv[i], "r");
204       if (!IN) {
205         perror(bprintf("Impossible to open the suite file `%s'",argv[i]));
206         ERROR1("Test suite `%s': NOK (system error)",testsuite_name);
207         exit(1);
208        }
209       handle_suite(suitename,IN);
210       rctx_wait_bg();
211       fclose(IN); //->leads to segfault on amd64...
212       INFO1("Test suite `%s' OK",suitename);
213       free(suitename);
214     }
215   }
216
217   rctx_exit();
218   xbt_exit();
219   return 0;  
220 }
221