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 */
21 int sort_len = 19; /* length of the prefix to sort */
23 const char *testsuite_name;
24 static void handle_line(const char *filepos, char *line)
27 xbt_str_rtrim(line + 2, "\n");
30 XBT_DEBUG("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 XBT_DEBUG("[%s] %s", filepos, line);
43 /* further trim useless chars which are significant for in/output */
44 xbt_str_rtrim(line + 2, " \t");
46 /* Deal with CD commands here, not in rctx */
47 if (!strncmp("cd ", line + 2, 3)) {
53 /* search beginning */
54 while (*(dir++) == ' ');
56 XBT_VERB("Saw cd '%s'", dir);
58 XBT_ERROR("Chdir to %s failed: %s", dir, strerror(errno));
59 XBT_ERROR("Test suite `%s': NOK (system error)", testsuite_name);
60 rctx_armageddon(rctx, 4);
63 } /* else, pushline */
68 rctx_pushline(filepos, line[0], line + 2 /* pass '$ ' stuff */ );
72 XBT_INFO("[%s] %s", filepos, line + 2);
75 XBT_CRITICAL("[%s] %s", filepos, line + 2);
79 XBT_ERROR("[%s] Syntax error: %s", filepos, line);
80 XBT_ERROR("Test suite `%s': NOK (syntax error)", testsuite_name);
81 rctx_armageddon(rctx, 1);
86 static void handle_suite(const char *filename, FILE * IN)
96 xbt_strbuff_t buff = NULL;
98 buff = xbt_strbuff_new();
101 while (getline(&line, &len, IN) != -1) {
104 /* Count the line length while checking wheather it's blank */
108 while (line[linelen] != '\0') {
109 if (line[linelen] != ' ' && line[linelen] != '\t'
110 && line[linelen] != '\n')
116 if (!rctx->cmd && !rctx->is_empty) {
117 XBT_ERROR("[%d] Error: no command found in this chunk of lines.",
119 XBT_ERROR("Test suite `%s': NOK (syntax error)", testsuite_name);
120 rctx_armageddon(rctx, 1);
128 /* Deal with \ at the end of the line, and call handle_line on result */
130 if (linelen > 1 && line[linelen - 2] == '\\') {
131 if (linelen > 2 && line[linelen - 3] == '\\') {
132 /* Damn. Escaped \ */
133 line[linelen - 2] = '\n';
134 line[linelen - 1] = '\0';
137 line[linelen - 2] = '\0';
140 buffbegin = line_num;
144 if (buff->used || to_be_continued) {
145 xbt_strbuff_append(buff, line);
147 if (!to_be_continued) {
148 snprintf(file_pos, 256, "%s:%d", filename, buffbegin);
149 handle_line(file_pos, buff->data);
150 xbt_strbuff_empty(buff);
154 snprintf(file_pos, 256, "%s:%d", filename, line_num);
155 handle_line(file_pos, line);
158 /* Check that last command of the file ran well */
162 /* Wait all background commands */
169 xbt_strbuff_free(buff);
173 static void parse_environ()
179 env = xbt_dict_new();
180 for (i = 0; environ[i]; i++) {
183 key = bprintf("%.*s", (int) (eq - p), p);
184 xbt_dict_set(env, key, xbt_strdup(eq + 1), xbt_free_f);
189 int main(int argc, char *argv[])
193 char *suitename = NULL;
194 struct sigaction newact;
195 xbt_init(&argc, argv);
199 /* Ignore pipe issues.
200 They will show up when we try to send data to dead buddies,
201 but we will stop doing so when we're done with provided input */
202 memset(&newact, 0, sizeof(newact));
203 newact.sa_handler = SIG_IGN;
204 sigaction(SIGPIPE, &newact, NULL);
207 for (i = 1; i < argc; i++) {
208 if (!strcmp(argv[i], "--cd")) {
210 XBT_ERROR("--cd argument requires an argument");
213 if (chdir(argv[i + 1])) {
214 XBT_ERROR("Cannot change directory to %s: %s", argv[i + 1],
218 XBT_INFO("Change directory to %s", argv[i + 1]);
219 memmove(argv + i, argv + i + 2, (argc - i - 1) * sizeof(char *));
222 } else if (!strcmp(argv[i], "--setenv" )) {
224 XBT_ERROR("--setenv argument requires an argument");
227 char *eq = strchr(argv[i+1], '=');
228 xbt_assert1(eq,"The argument of --setenv must contain a '=' (got %s instead)",argv[i+1]);
229 char *key = bprintf("%.*s", (int) (eq - argv[i+1]), argv[i+1]);
230 xbt_dict_set(env, key, xbt_strdup(eq + 1), xbt_free_f);
231 XBT_INFO("setting environment variable '%s' to '%s'", key, eq+1);
233 memmove(argv + i, argv + i + 2, (argc - i - 1) * sizeof(char *));
236 } else if (!strcmp(argv[i], "--cfg" )) {
238 XBT_ERROR("--cfg argument requires an argument");
241 if(!option){ //if option is NULL
242 option = bprintf("--cfg=%s",argv[i+1]);
244 option = bprintf("%s --cfg=%s",option,argv[i+1]);
246 XBT_INFO("Add option \'--cfg=%s\' to command line",argv[i+1]);
247 memmove(argv + i, argv + i + 2, (argc - i - 1) * sizeof(char *));
253 /* Find the description file */
255 XBT_INFO("Test suite from stdin");
256 testsuite_name = "(stdin)";
257 handle_suite(testsuite_name, stdin);
259 XBT_INFO("Test suite from stdin OK");
262 for (i = 1; i < argc; i++) {
263 suitename = xbt_strdup(argv[i]);
264 if (!strncmp("./", suitename, 2))
265 memmove(suitename, suitename + 2, strlen(suitename + 2));
267 if (strlen(suitename) > 5 &&
268 !strcmp(".tesh", suitename + strlen(suitename) - 5))
269 suitename[strlen(suitename) - 5] = '\0';
271 XBT_INFO("Test suite `%s'", suitename);
272 testsuite_name = suitename;
273 IN = fopen(argv[i], "r");
275 perror(bprintf("Impossible to open the suite file `%s'", argv[i]));
276 XBT_ERROR("Test suite `%s': NOK (system error)", testsuite_name);
277 rctx_armageddon(rctx, 1);
279 handle_suite(suitename, IN);
282 XBT_INFO("Test suite `%s' OK", suitename);