3 /* TESH (Test Shell) -- mini shell specialized in running test units */
5 /* Copyright (c) 2007 Martin Quinson. */
6 /* All rights reserved. */
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. */
11 /* specific to Borland Compiler */
19 XBT_LOG_NEW_DEFAULT_CATEGORY(tesh,"TEst SHell utility");
22 int timeout_value = 5; /* child timeout value */
25 static void handle_line(const char * filepos, char *line) {
27 xbt_str_rtrim(line+2,"\n");
30 DEBUG7("rctx={%s,in={%d,>>%10s<<},exp={%d,>>%10s<<},got={%d,>>%10s<<}}",
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);
36 DEBUG2("[%s] %s",filepos,line);
42 /* further trim useless chars which are significant for in/output */
43 xbt_str_rtrim(line+2," \t");
45 /* Deal with CD commands here, not in rctx */
46 if (!strncmp("cd ",line+2,3)) {
53 while (*(dir++) == ' ');
55 VERB1("Saw cd '%s'",dir);
58 strerror_r(errno, buff, 256);
60 perror(bprintf("Chdir to %s failed: %s",dir,buff));
61 ERROR1("Test suite `%s': NOK (system error)",testsuite_name);
65 } /* else, pushline */
70 rctx_pushline(filepos, line[0], line+2 /* pass '$ ' stuff*/);
74 INFO2("[%s] %s",filepos,line+2);
77 CRITICAL2("[%s] %s",filepos,line+2);
81 ERROR2("[%s] Syntax error: %s",filepos, line);
82 ERROR1("Test suite `%s': NOK (syntax error)",testsuite_name);
88 static void handle_suite(const char* filename, FILE* IN) {
94 buff_t buff=buff_new();
99 while (getline(&line, &len, IN) != -1) {
102 /* Count the line length while checking wheather it's blank */
105 while (line[linelen] != '\0') {
106 if (line[linelen] != ' ' && line[linelen] != '\t' && line[linelen]!='\n')
112 if (!rctx->cmd && !rctx->is_empty) {
113 ERROR1("[%d] Error: no command found in this chunk of lines.",
115 ERROR1("Test suite `%s': NOK (syntax error)",testsuite_name);
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';
133 line[linelen-2] = '\0';
136 buffbegin = line_num;
140 if (buff->used || to_be_continued) {
141 buff_append(buff,line);
143 if (!to_be_continued) {
144 snprintf(file_pos,256,"%s:%d",filename,buffbegin);
145 handle_line(file_pos, buff->data);
150 snprintf(file_pos,256,"%s:%d",filename,line_num);
151 handle_line(file_pos, line);
154 /* Check that last command of the file ran well */
158 /* Wait all background commands */
169 int main(int argc,char *argv[]) {
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);
181 xbt_init(&argc,argv);
184 /* Find the description file */
186 INFO0("Test suite from stdin");
187 handle_suite("stdin",stdin);
188 INFO0("Test suite from stdin OK");
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));
198 if (!strcmp(".tesh",suitename+strlen(suitename)-5))
199 suitename[strlen(suitename)-5] = '\0';
201 INFO1("Test suite `%s'",suitename);
202 testsuite_name = suitename;
203 IN=fopen(argv[i], "r");
205 perror(bprintf("Impossible to open the suite file `%s'",argv[i]));
206 ERROR1("Test suite `%s': NOK (system error)",testsuite_name);
209 handle_suite(suitename,IN);
211 fclose(IN); //->leads to segfault on amd64...
212 INFO1("Test suite `%s' OK",suitename);