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 #include "simgrid_config.h" /* FILE for getline */
11 /* specific to Borland Compiler */
19 XBT_LOG_NEW_DEFAULT_CATEGORY(tesh, "TEst SHell utility");
22 int timeout_value = 5; /* child timeout value */
23 int sort_len = 19; /* length of the prefix to sort */
25 const char *testsuite_name;
26 static void handle_line(const char *filepos, char *line)
29 xbt_str_rtrim(line + 2, "\n");
32 DEBUG7("rctx={%s,in={%d,>>%10s<<},exp={%d,>>%10s<<},got={%d,>>%10s<<}}",
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);
38 DEBUG2("[%s] %s", filepos, line);
45 /* further trim useless chars which are significant for in/output */
46 xbt_str_rtrim(line + 2, " \t");
48 /* Deal with CD commands here, not in rctx */
49 if (!strncmp("cd ", line + 2, 3)) {
55 /* search beginning */
56 while (*(dir++) == ' ');
58 VERB1("Saw cd '%s'", dir);
60 ERROR2("Chdir to %s failed: %s", dir, strerror(errno));
61 ERROR1("Test suite `%s': NOK (system error)", testsuite_name);
62 rctx_armageddon(rctx, 4);
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);
83 rctx_armageddon(rctx, 1);
88 static void handle_suite(const char *filename, FILE * FICIN)
98 xbt_strbuff_t buff = NULL;
100 buff = xbt_strbuff_new();
103 while (getline(&line, &len, FICIN) != -1) {
106 /* Count the line length while checking wheather it's blank */
110 while (line[linelen] != '\0') {
111 if (line[linelen] != ' ' && line[linelen] != '\t'
112 && line[linelen] != '\n')
118 if (!rctx->cmd && !rctx->is_empty) {
119 ERROR1("[%d] Error: no command found in this chunk of lines.",
121 ERROR1("Test suite `%s': NOK (syntax error)", testsuite_name);
122 rctx_armageddon(rctx, 1);
130 /* Deal with \ at the end of the line, and call handle_line on result */
132 if (linelen > 1 && line[linelen - 2] == '\\') {
133 if (linelen > 2 && line[linelen - 3] == '\\') {
134 /* Damn. Escaped \ */
135 line[linelen - 2] = '\n';
136 line[linelen - 1] = '\0';
139 line[linelen - 2] = '\0';
142 buffbegin = line_num;
146 if (buff->used || to_be_continued) {
147 xbt_strbuff_append(buff, line);
149 if (!to_be_continued) {
150 snprintf(file_pos, 256, "%s:%d", filename, buffbegin);
151 handle_line(file_pos, buff->data);
152 xbt_strbuff_empty(buff);
156 snprintf(file_pos, 256, "%s:%d", filename, line_num);
157 handle_line(file_pos, line);
160 /* Check that last command of the file ran well */
164 /* Wait all background commands */
171 xbt_strbuff_free(buff);
175 static void parse_environ()
181 env = xbt_dict_new();
182 for (i = 0; environ[i]; i++) {
185 key = bprintf("%.*s", (int) (eq - p), p);
186 xbt_dict_set(env, key, xbt_strdup(eq + 1), xbt_free_f);
191 int main(int argc, char *argv[])
195 char *suitename = NULL;
196 struct sigaction newact;
198 xbt_init(&argc, argv);
202 /* Ignore pipe issues.
203 They will show up when we try to send data to dead buddies,
204 but we will stop doing so when we're done with provided input */
205 memset(&newact, 0, sizeof(newact));
206 newact.sa_handler = SIG_IGN;
207 sigaction(SIGPIPE, &newact, NULL);
210 for (i = 1; i < argc; i++) {
211 if (!strcmp(argv[i], "--cd")) {
213 ERROR0("--cd argument requires an argument");
216 if (chdir(argv[i + 1])) {
217 ERROR2("Cannot change directory to %s: %s", argv[i + 1],
221 INFO1("Change directory to %s", argv[i + 1]);
222 memmove(argv + i, argv + i + 2, (argc - i - 1) * sizeof(char *));
225 } else if (!strcmp(argv[i], "--setenv" )) {
227 ERROR0("--setenv argument requires an argument");
230 char *eq = strchr(argv[i+1], '=');
231 xbt_assert1(eq,"The argument of --setenv must contain a '=' (got %s instead)",argv[i+1]);
232 char *key = bprintf("%.*s", (int) (eq - argv[i+1]), argv[i+1]);
233 xbt_dict_set(env, key, xbt_strdup(eq + 1), xbt_free_f);
234 INFO2("setting environment variable '%s' to '%s'", key, eq+1);
236 memmove(argv + i, argv + i + 2, (argc - i - 1) * sizeof(char *));
242 /* Find the description file */
244 INFO0("Test suite from stdin");
245 testsuite_name = "(stdin)";
246 handle_suite(testsuite_name, stdin);
248 INFO0("Test suite from stdin OK");
251 for (i = 1; i < argc; i++) {
252 suitename = xbt_strdup(argv[i]);
253 if (!strncmp("./", suitename, 2))
254 memmove(suitename, suitename + 2, strlen(suitename + 2));
256 if (strlen(suitename) > 5 &&
257 !strcmp(".tesh", suitename + strlen(suitename) - 5))
258 suitename[strlen(suitename) - 5] = '\0';
260 INFO1("Test suite `%s'", suitename);
261 testsuite_name = suitename;
262 FICIN = fopen(argv[i], "r");
264 perror(bprintf("Impossible to open the suite file `%s'", argv[i]));
265 ERROR1("Test suite `%s': NOK (system error)", testsuite_name);
266 rctx_armageddon(rctx, 1);
268 handle_suite(suitename, FICIN);
271 INFO1("Test suite `%s' OK", suitename);