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)
28 xbt_str_rtrim(line + 2, "\n");
31 DEBUG7("rctx={%s,in={%d,>>%10s<<},exp={%d,>>%10s<<},got={%d,>>%10s<<}}",
33 rctx->input->used, rctx->input->data,
34 rctx->output_wanted->used,rctx->output_wanted->data,
35 rctx->output_got->used, rctx->output_got->data);
37 DEBUG2("[%s] %s", filepos, line);
44 /* further trim useless chars which are significant for in/output */
45 xbt_str_rtrim(line + 2, " \t");
47 /* Deal with CD commands here, not in rctx */
48 if (!strncmp("cd ", line + 2, 3)) {
54 /* search beginning */
55 while (*(dir++) == ' ');
57 VERB1("Saw cd '%s'", dir);
59 ERROR2("Chdir to %s failed: %s", dir, strerror(errno));
60 ERROR1("Test suite `%s': NOK (system error)", testsuite_name);
61 rctx_armageddon(rctx, 4);
64 } /* else, pushline */
69 rctx_pushline(filepos, line[0], line + 2 /* pass '$ ' stuff */ );
73 INFO2("[%s] %s", filepos, line + 2);
76 CRITICAL2("[%s] %s", filepos, line + 2);
80 ERROR2("[%s] Syntax error: %s", filepos, line);
81 ERROR1("Test suite `%s': NOK (syntax error)", testsuite_name);
82 rctx_armageddon(rctx, 1);
87 static void handle_suite(const char *filename, FILE * IN)
94 xbt_strbuff_t buff = xbt_strbuff_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'
107 && line[linelen] != '\n')
113 if (!rctx->cmd && !rctx->is_empty) {
114 ERROR1("[%d] Error: no command found in this chunk of lines.",
116 ERROR1("Test suite `%s': NOK (syntax error)", testsuite_name);
117 rctx_armageddon(rctx, 1);
125 /* Deal with \ at the end of the line, and call handle_line on result */
126 int to_be_continued = 0;
127 if (linelen > 1 && line[linelen - 2] == '\\') {
128 if (linelen > 2 && line[linelen - 3] == '\\') {
129 /* Damn. Escaped \ */
130 line[linelen - 2] = '\n';
131 line[linelen - 1] = '\0';
134 line[linelen - 2] = '\0';
137 buffbegin = line_num;
141 if (buff->used || to_be_continued) {
142 xbt_strbuff_append(buff, line);
144 if (!to_be_continued) {
145 snprintf(file_pos, 256, "%s:%d", filename, buffbegin);
146 handle_line(file_pos, buff->data);
147 xbt_strbuff_empty(buff);
151 snprintf(file_pos, 256, "%s:%d", filename, line_num);
152 handle_line(file_pos, line);
155 /* Check that last command of the file ran well */
159 /* Wait all background commands */
166 xbt_strbuff_free(buff);
170 static void parse_environ()
174 env = xbt_dict_new();
175 for (i = 0; environ[i]; i++) {
177 char *eq = strchr(p, '=');
178 char *key = bprintf("%.*s", (int) (eq - p), p);
179 xbt_dict_set(env, key, xbt_strdup(eq + 1), xbt_free_f);
184 int main(int argc, char *argv[])
190 /* Ignore pipe issues.
191 They will show up when we try to send data to dead buddies,
192 but we will stop doing so when we're done with provided input */
193 struct sigaction newact;
194 memset(&newact, 0, sizeof(newact));
195 newact.sa_handler = SIG_IGN;
196 sigaction(SIGPIPE, &newact, NULL);
198 xbt_init(&argc, argv);
203 for (i = 1; i < argc; i++) {
204 if (!strncmp(argv[i], "--cd", strlen("--cd") + 1)) {
206 ERROR0("--cd argument requires an argument");
209 if (chdir(argv[i + 1])) {
210 ERROR2("Cannot change directory to %s: %s", argv[i + 1],
214 INFO1("Change directory to %s", argv[i + 1]);
215 memmove(argv + i, argv + i + 2, argc - i - 1);
220 /* Find the description file */
222 INFO0("Test suite from stdin");
223 testsuite_name = xbt_strdup("(stdin)");
224 handle_suite("stdin", stdin);
225 INFO0("Test suite from stdin OK");
228 for (i = 1; i < argc; i++) {
229 char *suitename = xbt_strdup(argv[i]);
230 if (!strcmp("./", suitename))
231 memmove(suitename, suitename + 2, strlen(suitename + 2));
233 if (!strcmp(".tesh", suitename + strlen(suitename) - 5))
234 suitename[strlen(suitename) - 5] = '\0';
236 INFO1("Test suite `%s'", suitename);
237 testsuite_name = suitename;
238 IN = fopen(argv[i], "r");
240 perror(bprintf("Impossible to open the suite file `%s'", argv[i]));
241 ERROR1("Test suite `%s': NOK (system error)", testsuite_name);
242 rctx_armageddon(rctx, 1);
244 handle_suite(suitename, IN);
247 INFO1("Test suite `%s' OK", suitename);