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 */
14 #include "simgrid_config.h" //For getline, keep that include first
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 int coverage = 0; /* whether the code coverage is enable */
28 const char *testsuite_name;
32 static void handle_line(const char *filepos, char *line)
35 xbt_str_rtrim(line + 2, "\n");
38 XBT_DEBUG("rctx={%s,in={%d,>>%10s<<},exp={%d,>>%10s<<},got={%d,>>%10s<<}}",
40 rctx->input->used, rctx->input->data,
41 rctx->output_wanted->used,rctx->output_wanted->data,
42 rctx->output_got->used, rctx->output_got->data);
44 XBT_DEBUG("[%s] %s", filepos, line);
51 /* further trim useless chars which are significant for in/output */
52 xbt_str_rtrim(line + 2, " \t");
54 /* Deal with CD commands here, not in rctx */
55 if (!strncmp("cd ", line + 2, 3)) {
61 /* search beginning */
62 while (*(dir++) == ' ');
64 XBT_VERB("Saw cd '%s'", dir);
66 XBT_ERROR("Chdir to %s failed: %s", dir, strerror(errno));
67 XBT_ERROR("Test suite `%s': NOK (system error)", testsuite_name);
68 rctx_armageddon(rctx, 4);
71 } /* else, pushline */
76 rctx_pushline(filepos, line[0], line + 2 /* pass '$ ' stuff */ );
80 XBT_INFO("[%s] %s", filepos, line + 2);
83 XBT_CRITICAL("[%s] %s", filepos, line + 2);
87 XBT_ERROR("[%s] Syntax error: %s", filepos, line);
88 XBT_ERROR("Test suite `%s': NOK (syntax error)", testsuite_name);
89 rctx_armageddon(rctx, 1);
94 static void handle_suite(const char *filename, FILE * IN)
104 xbt_strbuff_t buff = NULL;
106 buff = xbt_strbuff_new();
109 while (getline(&line, &len, IN) != -1) {
112 /* Count the line length while checking wheather it's blank */
116 while (line[linelen] != '\0') {
117 if (line[linelen] != ' ' && line[linelen] != '\t'
118 && line[linelen] != '\n')
124 if (!rctx->cmd && !rctx->is_empty) {
125 XBT_ERROR("[%d] Error: no command found in this chunk of lines.",
127 XBT_ERROR("Test suite `%s': NOK (syntax error)", testsuite_name);
128 rctx_armageddon(rctx, 1);
136 /* Deal with \ at the end of the line, and call handle_line on result */
138 if (linelen > 1 && line[linelen - 2] == '\\') {
139 if (linelen > 2 && line[linelen - 3] == '\\') {
140 /* Damn. Escaped \ */
141 line[linelen - 2] = '\n';
142 line[linelen - 1] = '\0';
145 line[linelen - 2] = '\0';
148 buffbegin = line_num;
152 if (buff->used || to_be_continued) {
153 xbt_strbuff_append(buff, line);
155 if (!to_be_continued) {
156 snprintf(file_pos, 256, "%s:%d", filename, buffbegin);
157 handle_line(file_pos, buff->data);
158 xbt_strbuff_empty(buff);
162 snprintf(file_pos, 256, "%s:%d", filename, line_num);
163 handle_line(file_pos, line);
166 /* Check that last command of the file ran well */
170 /* Wait all background commands */
176 xbt_strbuff_free(buff);
180 static void parse_environ()
186 env = xbt_dict_new_homogeneous(xbt_free_f);
187 for (i = 0; environ[i]; i++) {
190 key = bprintf("%.*s", (int) (eq - p), p);
191 xbt_dict_set(env, key, xbt_strdup(eq + 1), NULL);
196 int main(int argc, char *argv[])
200 char *suitename = NULL;
201 struct sigaction newact;
203 XBT_LOG_CONNECT(tesh);
204 xbt_init(&argc, argv);
208 /* Ignore pipe issues.
209 They will show up when we try to send data to dead buddies,
210 but we will stop doing so when we're done with provided input */
211 memset(&newact, 0, sizeof(newact));
212 newact.sa_handler = SIG_IGN;
213 sigaction(SIGPIPE, &newact, NULL);
216 for (i = 1; i < argc; i++) {
217 if (!strcmp(argv[i], "--cd")) {
219 XBT_ERROR("--cd argument requires an argument");
222 if (chdir(argv[i + 1])) {
223 XBT_ERROR("Cannot change directory to %s: %s", argv[i + 1],
227 XBT_INFO("Change directory to %s", argv[i + 1]);
228 memmove(argv + i, argv + i + 2, (argc - i - 1) * sizeof(char *));
231 } else if (!strcmp(argv[i], "--setenv" )) {
233 XBT_ERROR("--setenv argument requires an argument");
236 char *eq = strchr(argv[i+1], '=');
237 xbt_assert(eq,"The argument of --setenv must contain a '=' (got %s instead)",argv[i+1]);
238 char *key = bprintf("%.*s", (int) (eq - argv[i+1]), argv[i+1]);
239 xbt_dict_set(env, key, xbt_strdup(eq + 1), NULL);
240 XBT_INFO("setting environment variable '%s' to '%s'", key, eq+1);
242 memmove(argv + i, argv + i + 2, (argc - i - 1) * sizeof(char *));
245 } else if (!strcmp(argv[i], "--cfg" )) {
247 XBT_ERROR("--cfg argument requires an argument");
250 if (!option){ //if option is NULL
251 option = bprintf("--cfg=%s",argv[i+1]);
253 char *newoption = bprintf("%s --cfg=%s", option, argv[i+1]);
257 XBT_INFO("Add option \'--cfg=%s\' to command line",argv[i+1]);
258 memmove(argv + i, argv + i + 2, (argc - i - 1) * sizeof(char *));
262 else if (!strcmp(argv[i], "--enable-coverage" )){
264 XBT_INFO("Enable coverage");
265 memmove(argv + i, argv + i + 1, (argc - i - 1) * sizeof(char *));
271 /* Find the description file */
273 XBT_INFO("Test suite from stdin");
274 testsuite_name = "(stdin)";
275 handle_suite(testsuite_name, stdin);
277 XBT_INFO("Test suite from stdin OK");
280 for (i = 1; i < argc; i++) {
281 suitename = xbt_strdup(argv[i]);
282 if (!strncmp("./", suitename, 2))
283 memmove(suitename, suitename + 2, strlen(suitename + 2));
285 if (strlen(suitename) > 5 &&
286 !strcmp(".tesh", suitename + strlen(suitename) - 5))
287 suitename[strlen(suitename) - 5] = '\0';
289 XBT_INFO("Test suite `%s'", suitename);
290 testsuite_name = suitename;
291 IN = fopen(argv[i], "r");
293 perror(bprintf("Impossible to open the suite file `%s'", argv[i]));
294 XBT_ERROR("Test suite `%s': NOK (system error)", testsuite_name);
295 rctx_armageddon(rctx, 1);
297 handle_suite(suitename, IN);
300 XBT_INFO("Test suite `%s' OK", suitename);