X-Git-Url: http://info.iut-bm.univ-fcomte.fr/pub/gitweb/simgrid.git/blobdiff_plain/86051ed1b66342bae0e21fd6643e66331ac2c07d..9a4f8255848ed4b9ccfd54d1d635de698889d86c:/tools/tesh/tesh.c diff --git a/tools/tesh/tesh.c b/tools/tesh/tesh.c index 98a6d14da5..7f752291ac 100644 --- a/tools/tesh/tesh.c +++ b/tools/tesh/tesh.c @@ -1,9 +1,7 @@ -/* $Id$ */ - /* TESH (Test Shell) -- mini shell specialized in running test units */ -/* Copyright (c) 2007 Martin Quinson. */ -/* All rights reserved. */ +/* Copyright (c) 2007-2013. The SimGrid Team. + * All rights reserved. */ /* This program is free software; you can redistribute it and/or modify it * under the terms of the license (GNU LGPL) which comes with this package. */ @@ -16,139 +14,154 @@ #include "tesh.h" #include "xbt.h" -XBT_LOG_NEW_DEFAULT_CATEGORY(tesh,"TEst SHell utility"); +XBT_LOG_NEW_DEFAULT_CATEGORY(tesh, "TEst SHell utility"); /*** Options ***/ -int timeout_value = 5; /* child timeout value */ +int timeout_value = 5; /* child timeout value */ +int sort_len = 19; /* length of the prefix to sort */ +char *option; +int coverage = 0; /* whether the code coverage is enable */ +rctx_t rctx; +const char *testsuite_name; -static void handle_line(const char * filepos, char *line) { - int pos; +xbt_dict_t env; +static void handle_line(const char *filepos, char *line) +{ /* Search end */ - xbt_str_rtrim(line+2,"\n"); + xbt_str_rtrim(line + 2, "\n"); /* - DEBUG7("rctx={%s,in={%d,>>%10s<<},exp={%d,>>%10s<<},got={%d,>>%10s<<}}", - rctx->cmd, - rctx->input->used, rctx->input->data, - rctx->output_wanted->used,rctx->output_wanted->data, - rctx->output_got->used, rctx->output_got->data); - */ - DEBUG2("[%s] %s",filepos,line); + XBT_DEBUG("rctx={%s,in={%d,>>%10s<<},exp={%d,>>%10s<<},got={%d,>>%10s<<}}", + rctx->cmd, + rctx->input->used, rctx->input->data, + rctx->output_wanted->used,rctx->output_wanted->data, + rctx->output_got->used, rctx->output_got->data); + */ + XBT_DEBUG("[%s] %s", filepos, line); switch (line[0]) { - case '#': break; + case '#': + break; case '$': /* further trim useless chars which are significant for in/output */ - xbt_str_rtrim(line+2," \t"); + xbt_str_rtrim(line + 2, " \t"); /* Deal with CD commands here, not in rctx */ - if (!strncmp("cd ",line+2,3)) { - char *dir=line+4; + if (!strncmp("cd ", line + 2, 3)) { + char *dir = line + 4; if (rctx->cmd) - rctx_start(); - - /* search begining */ + rctx_start(); + + /* search beginning */ while (*(dir++) == ' '); dir--; - VERB1("Saw cd '%s'",dir); + XBT_VERB("Saw cd '%s'", dir); if (chdir(dir)) { - char buff[256]; - strerror_r(errno, buff, 256); - - ERROR2("Chdir to %s failed: %s",dir+pos+2,buff); - exit(4); + XBT_ERROR("Chdir to %s failed: %s", dir, strerror(errno)); + XBT_ERROR("Test suite `%s': NOK (system error)", testsuite_name); + rctx_armageddon(rctx, 4); } break; - } /* else, pushline */ + } /* else, pushline */ case '&': case '<': case '>': case '!': - rctx_pushline(filepos, line[0], line+2 /* pass '$ ' stuff*/); + rctx_pushline(filepos, line[0], line + 2 /* pass '$ ' stuff */ ); break; case 'p': - INFO2("[%s] %s",filepos,line+2); + XBT_INFO("[%s] %s", filepos, line + 2); + break; + case 'P': + XBT_CRITICAL("[%s] %s", filepos, line + 2); break; default: - ERROR2("[%s] Syntax error: %s",filepos, line); - exit(1); + XBT_ERROR("[%s] Syntax error: %s", filepos, line); + XBT_ERROR("Test suite `%s': NOK (syntax error)", testsuite_name); + rctx_armageddon(rctx, 1); break; } } -static void handle_suite(const char* filename, FILE* IN) { - int len; - char * line = NULL; - int line_num=0; +static void handle_suite(const char *filename, FILE * IN) +{ + size_t len; + int blankline; + int linelen; + char *line = NULL; + int line_num = 0; char file_pos[256]; + int to_be_continued; + int buffbegin = 0; + xbt_strbuff_t buff = NULL; - buff_t buff=buff_new(); - int buffbegin = 0; - + buff = xbt_strbuff_new(); rctx = rctx_new(); - while (getline(&line,(size_t*) &len, IN) != -1) { + while (xbt_getline(&line, &len, IN) != -1) { line_num++; /* Count the line length while checking wheather it's blank */ - int blankline=1; - int linelen = 0; + blankline = 1; + linelen = 0; + while (line[linelen] != '\0') { - if (line[linelen] != ' ' && line[linelen] != '\t' && line[linelen]!='\n') - blankline = 0; + if (line[linelen] != ' ' && line[linelen] != '\t' + && line[linelen] != '\n') + blankline = 0; linelen++; } - + if (blankline) { if (!rctx->cmd && !rctx->is_empty) { - ERROR1("[%d] Error: no command found in this chunk of lines.", - buffbegin); - exit(1); + XBT_ERROR("[%d] Error: no command found in this chunk of lines.", + buffbegin); + XBT_ERROR("Test suite `%s': NOK (syntax error)", testsuite_name); + rctx_armageddon(rctx, 1); } if (rctx->cmd) - rctx_start(); + rctx_start(); continue; } /* Deal with \ at the end of the line, and call handle_line on result */ - int to_be_continued = 0; - if (linelen>1 && line[linelen-2]=='\\') { - if (linelen>2 && line[linelen-3] == '\\') { - /* Damn. Escaped \ */ - line[linelen-2] = '\n'; - line[linelen-1] = '\0'; + to_be_continued = 0; + if (linelen > 1 && line[linelen - 2] == '\\') { + if (linelen > 2 && line[linelen - 3] == '\\') { + /* Damn. Escaped \ */ + line[linelen - 2] = '\n'; + line[linelen - 1] = '\0'; } else { - to_be_continued = 1; - line[linelen-2] = '\0'; - linelen -= 2; - if (!buff->used) - buffbegin = line_num; + to_be_continued = 1; + line[linelen - 2] = '\0'; + if (!buff->used) + buffbegin = line_num; } } - if (buff->used || to_be_continued) { - buff_append(buff,line); + if (buff->used || to_be_continued) { + xbt_strbuff_append(buff, line); if (!to_be_continued) { - snprintf(file_pos,256,"%s:%d",filename,buffbegin); - handle_line(file_pos, buff->data); - buff_empty(buff); + snprintf(file_pos, 256, "%s:%d", filename, buffbegin); + handle_line(file_pos, buff->data); + xbt_strbuff_empty(buff); } - + } else { - snprintf(file_pos,256,"%s:%d",filename,line_num); - handle_line(file_pos, line); + snprintf(file_pos, 256, "%s:%d", filename, line_num); + handle_line(file_pos, line); } } /* Check that last command of the file ran well */ - if (rctx->cmd) + if (rctx->cmd) rctx_start(); /* Wait all background commands */ @@ -156,60 +169,138 @@ static void handle_suite(const char* filename, FILE* IN) { rctx_free(rctx); /* Clear buffers */ - if (line) - free(line); - buff_free(buff); + free(line); + xbt_strbuff_free(buff); } -int main(int argc,char *argv[]) { +static void parse_environ() +{ + char *p; + int i; + char *eq = NULL; + char *key = NULL; + env = xbt_dict_new_homogeneous(xbt_free_f); + for (i = 0; environ[i]; i++) { + p = environ[i]; + eq = strchr(p, '='); + key = bprintf("%.*s", (int) (eq - p), p); + xbt_dict_set(env, key, xbt_strdup(eq + 1), NULL); + free(key); + } +} - FILE *IN; +int main(int argc, char *argv[]) +{ + FILE *IN = NULL; + int i; + char *suitename = NULL; + struct sigaction newact; + + XBT_LOG_CONNECT(tesh); + xbt_init(&argc, argv); + rctx_init(); + parse_environ(); /* Ignore pipe issues. - They will show up when we try to send data to dead buddies, + They will show up when we try to send data to dead buddies, but we will stop doing so when we're done with provided input */ - struct sigaction newact; - memset(&newact,0, sizeof(newact)); - newact.sa_handler=SIG_IGN; - sigaction(SIGPIPE,&newact,NULL); - - xbt_init(&argc,argv); - rctx_init(); + memset(&newact, 0, sizeof(newact)); + newact.sa_handler = SIG_IGN; + sigaction(SIGPIPE, &newact, NULL); + + /* Get args */ + for (i = 1; i < argc; i++) { + if (!strcmp(argv[i], "--cd")) { + if (i == argc - 1) { + XBT_ERROR("--cd argument requires an argument"); + exit(1); + } + if (chdir(argv[i + 1])) { + XBT_ERROR("Cannot change directory to %s: %s", argv[i + 1], + strerror(errno)); + exit(1); + } + XBT_INFO("Change directory to %s", argv[i + 1]); + memmove(argv + i, argv + i + 2, (argc - i - 1) * sizeof(char *)); + argc -= 2; + i -= 2; + } else if (!strcmp(argv[i], "--setenv" )) { + if (i == argc - 1) { + XBT_ERROR("--setenv argument requires an argument"); + exit(1); + } + char *eq = strchr(argv[i+1], '='); + xbt_assert(eq,"The argument of --setenv must contain a '=' (got %s instead)",argv[i+1]); + char *key = bprintf("%.*s", (int) (eq - argv[i+1]), argv[i+1]); + xbt_dict_set(env, key, xbt_strdup(eq + 1), NULL); + XBT_INFO("setting environment variable '%s' to '%s'", key, eq+1); + free(key); + memmove(argv + i, argv + i + 2, (argc - i - 1) * sizeof(char *)); + argc -= 2; + i -= 2; + } else if (!strcmp(argv[i], "--cfg" )) { + if (i == argc - 1) { + XBT_ERROR("--cfg argument requires an argument"); + exit(1); + } + if (!option){ //if option is NULL + option = bprintf("--cfg=%s",argv[i+1]); + } else { + char *newoption = bprintf("%s --cfg=%s", option, argv[i+1]); + free(option); + option = newoption; + } + XBT_INFO("Add option \'--cfg=%s\' to command line",argv[i+1]); + memmove(argv + i, argv + i + 2, (argc - i - 1) * sizeof(char *)); + argc -= 2; + i -= 2; + } + else if (!strcmp(argv[i], "--enable-coverage" )){ + coverage = 1; + XBT_INFO("Enable coverage"); + memmove(argv + i, argv + i + 1, (argc - i - 1) * sizeof(char *)); + argc -= 1; + i -= 1; + } + } /* Find the description file */ if (argc == 1) { - INFO0("Test suite from stdin"); - handle_suite("stdin",stdin); - INFO0("Test suite from stdin OK"); - + XBT_INFO("Test suite from stdin"); + testsuite_name = "(stdin)"; + handle_suite(testsuite_name, stdin); + rctx_wait_bg(); + XBT_INFO("Test suite from stdin OK"); + } else { - int i; - - for (i=1; i 5 && + !strcmp(".tesh", suitename + strlen(suitename) - 5)) + suitename[strlen(suitename) - 5] = '\0'; + + XBT_INFO("Test suite `%s'", suitename); + testsuite_name = suitename; + IN = fopen(argv[i], "r"); if (!IN) { - perror(bprintf("Impossible to open the suite file `%s'",argv[i])); - exit(1); - } - handle_suite(suitename,IN); + perror(bprintf("Impossible to open the suite file `%s'", argv[i])); + XBT_ERROR("Test suite `%s': NOK (system error)", testsuite_name); + rctx_armageddon(rctx, 1); + } + handle_suite(suitename, IN); rctx_wait_bg(); - fclose(IN); //->leads to segfault on amd64... - INFO1("Test suite `%s' OK",suitename); + fclose(IN); + XBT_INFO("Test suite `%s' OK", suitename); free(suitename); } } rctx_exit(); - xbt_exit(); - return 0; + xbt_dict_free(&env); + free(option); + return 0; } -