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);
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);
62 } /* else, pushline */
67 rctx_pushline(filepos, line[0], line+2 /* pass '$ ' stuff*/);
71 INFO2("[%s] %s",filepos,line+2);
74 CRITICAL2("[%s] %s",filepos,line+2);
78 ERROR2("[%s] Syntax error: %s",filepos, line);
79 ERROR1("Test suite `%s': NOK (syntax error)",testsuite_name);
80 rctx_armageddon(rctx,1);
85 static void handle_suite(const char* filename, FILE* IN) {
91 buff_t buff=buff_new();
96 while (getline(&line, &len, IN) != -1) {
99 /* Count the line length while checking wheather it's blank */
102 while (line[linelen] != '\0') {
103 if (line[linelen] != ' ' && line[linelen] != '\t' && line[linelen]!='\n')
109 if (!rctx->cmd && !rctx->is_empty) {
110 ERROR1("[%d] Error: no command found in this chunk of lines.",
112 ERROR1("Test suite `%s': NOK (syntax error)",testsuite_name);
113 rctx_armageddon(rctx,1);
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';
130 line[linelen-2] = '\0';
133 buffbegin = line_num;
137 if (buff->used || to_be_continued) {
138 buff_append(buff,line);
140 if (!to_be_continued) {
141 snprintf(file_pos,256,"%s:%d",filename,buffbegin);
142 handle_line(file_pos, buff->data);
147 snprintf(file_pos,256,"%s:%d",filename,line_num);
148 handle_line(file_pos, line);
151 /* Check that last command of the file ran well */
155 /* Wait all background commands */
166 int main(int argc,char *argv[]) {
170 /* Ignore pipe issues.
171 They will show up when we try to send data to dead buddies,
172 but we will stop doing so when we're done with provided input */
173 struct sigaction newact;
174 memset(&newact,0, sizeof(newact));
175 newact.sa_handler=SIG_IGN;
176 sigaction(SIGPIPE,&newact,NULL);
178 xbt_init(&argc,argv);
181 /* Find the description file */
183 INFO0("Test suite from stdin");
184 testsuite_name = xbt_strdup("(stdin)");
185 handle_suite("stdin",stdin);
186 INFO0("Test suite from stdin OK");
191 for (i=1; i<argc; i++) {
192 char *suitename=xbt_strdup(argv[i]);
193 if (!strcmp("./",suitename))
194 memmove(suitename, suitename+2, strlen(suitename+2));
196 if (!strcmp(".tesh",suitename+strlen(suitename)-5))
197 suitename[strlen(suitename)-5] = '\0';
199 INFO1("Test suite `%s'",suitename);
200 testsuite_name = suitename;
201 IN=fopen(argv[i], "r");
203 perror(bprintf("Impossible to open the suite file `%s'",argv[i]));
204 ERROR1("Test suite `%s': NOK (system error)",testsuite_name);
205 rctx_armageddon(rctx,1);
207 handle_suite(suitename,IN);
210 INFO1("Test suite `%s' OK",suitename);