Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
size_t is long, so don't play it smart and get it working on amd64
[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
78   default:
79     ERROR2("[%s] Syntax error: %s",filepos, line);
80     exit(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   buff_t buff=buff_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         exit(1);
113       }
114       if (rctx->cmd)
115         rctx_start();
116
117       continue;
118     }
119
120     /* Deal with \ at the end of the line, and call handle_line on result */
121     int to_be_continued = 0;
122     if (linelen>1 && line[linelen-2]=='\\') {
123       if (linelen>2 && line[linelen-3] == '\\') {
124         /* Damn. Escaped \ */
125         line[linelen-2] = '\n';
126         line[linelen-1] = '\0';
127       } else {
128         to_be_continued = 1;
129         line[linelen-2] = '\0';
130         linelen -= 2;  
131         if (!buff->used)
132           buffbegin = line_num;
133       }
134     }
135
136     if (buff->used || to_be_continued) { 
137       buff_append(buff,line);
138
139       if (!to_be_continued) {
140         snprintf(file_pos,256,"%s:%d",filename,buffbegin);
141         handle_line(file_pos, buff->data);    
142         buff_empty(buff);
143       }
144         
145     } else {
146       snprintf(file_pos,256,"%s:%d",filename,line_num);
147       handle_line(file_pos, line);    
148     }
149   }
150   /* Check that last command of the file ran well */
151   if (rctx->cmd) 
152     rctx_start();
153
154   /* Wait all background commands */
155
156   rctx_free(rctx);
157
158   /* Clear buffers */
159   if (line)
160     free(line);
161   buff_free(buff);
162
163 }
164
165 int main(int argc,char *argv[]) {
166
167   FILE *IN;
168
169   /* Ignore pipe issues.
170      They will show up when we try to send data to dead buddies, 
171      but we will stop doing so when we're done with provided input */
172   struct sigaction newact;
173   memset(&newact,0, sizeof(newact));
174   newact.sa_handler=SIG_IGN;
175   sigaction(SIGPIPE,&newact,NULL);
176    
177   xbt_init(&argc,argv);
178   rctx_init();
179
180   /* Find the description file */
181   if (argc == 1) {
182     INFO0("Test suite from stdin");
183     handle_suite("stdin",stdin);
184     INFO0("Test suite from stdin OK");
185      
186   } else {
187     int i;
188      
189     for (i=1; i<argc; i++) {
190       char *suitename=xbt_strdup(argv[i]);
191       if (!strcmp("./",suitename))
192         memmove(suitename, suitename+2, strlen(suitename+2));
193
194       if (!strcmp(".tesh",suitename+strlen(suitename)-5))
195         suitename[strlen(suitename)-5] = '\0';
196
197       INFO1("Test suite `%s'",suitename);
198       IN=fopen(argv[i], "r");
199       if (!IN) {
200         perror(bprintf("Impossible to open the suite file `%s'",argv[i]));
201         exit(1);
202        }
203       handle_suite(suitename,IN);
204       rctx_wait_bg();
205       fclose(IN); //->leads to segfault on amd64...
206       INFO1("Test suite `%s' OK",suitename);
207       free(suitename);
208     }
209   }
210
211   rctx_exit();
212   xbt_exit();
213   return 0;  
214 }
215