1 /* TESH (Test Shell) -- mini shell specialized in running test units */
3 /* Copyright (c) 2007, 2008, 2009, 2010. The SimGrid Team.
4 * All rights reserved. */
6 /* This program is free software; you can redistribute it and/or modify it
7 * under the terms of the license (GNU LGPL) which comes with this package. */
9 /* specific to Borland Compiler */
17 XBT_LOG_NEW_DEFAULT_CATEGORY(tesh, "TEst SHell utility");
20 int timeout_value = 5; /* child timeout value */
23 static void handle_line(const char *filepos, char *line)
26 xbt_str_rtrim(line + 2, "\n");
29 DEBUG7("rctx={%s,in={%d,>>%10s<<},exp={%d,>>%10s<<},got={%d,>>%10s<<}}",
31 rctx->input->used, rctx->input->data,
32 rctx->output_wanted->used,rctx->output_wanted->data,
33 rctx->output_got->used, rctx->output_got->data);
35 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)) {
52 /* search beginning */
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)
93 int to_be_continued = 0;
95 xbt_strbuff_t buff = NULL;
97 buff = xbt_strbuff_new();
100 while (getline(&line, &len, IN) != -1) {
103 /* 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 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 xbt_strbuff_append(buff, line);
143 if (!to_be_continued) {
144 snprintf(file_pos, 256, "%s:%d", filename, buffbegin);
145 handle_line(file_pos, buff->data);
146 xbt_strbuff_empty(buff);
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 */
165 xbt_strbuff_free(buff);
169 static void parse_environ()
175 env = xbt_dict_new();
176 for (i = 0; environ[i]; i++) {
179 key = bprintf("%.*s", (int) (eq - p), p);
180 xbt_dict_set(env, key, xbt_strdup(eq + 1), xbt_free_f);
185 int main(int argc, char *argv[])
189 char *suitename = NULL;
191 /* Ignore pipe issues.
192 They will show up when we try to send data to dead buddies,
193 but we will stop doing so when we're done with provided input */
194 struct sigaction newact;
195 memset(&newact, 0, sizeof(newact));
196 newact.sa_handler = SIG_IGN;
197 sigaction(SIGPIPE, &newact, NULL);
199 xbt_init(&argc, argv);
204 for (i = 1; i < argc; i++) {
205 if (!strcmp(argv[i], "--cd")) {
207 ERROR0("--cd argument requires an argument");
210 if (chdir(argv[i + 1])) {
211 ERROR2("Cannot change directory to %s: %s", argv[i + 1],
215 INFO1("Change directory to %s", argv[i + 1]);
216 memmove(argv + i, argv + i + 2, (argc - i - 1)*sizeof(char*));
222 /* Find the description file */
224 INFO0("Test suite from stdin");
225 testsuite_name = xbt_strdup("(stdin)");
226 handle_suite("stdin", stdin);
227 INFO0("Test suite from stdin OK");
230 for (i = 1; i < argc; i++) {
231 suitename = xbt_strdup(argv[i]);
232 if (!strcmp("./", suitename))
233 memmove(suitename, suitename + 2, strlen(suitename + 2));
235 if (!strcmp(".tesh", suitename + strlen(suitename) - 5))
236 suitename[strlen(suitename) - 5] = '\0';
238 INFO1("Test suite `%s'", suitename);
239 testsuite_name = suitename;
240 IN = fopen(argv[i], "r");
242 perror(bprintf("Impossible to open the suite file `%s'", argv[i]));
243 ERROR1("Test suite `%s': NOK (system error)", testsuite_name);
244 rctx_armageddon(rctx, 1);
246 handle_suite(suitename, IN);
249 INFO1("Test suite `%s' OK", suitename);