6 #include <directories.h>
13 * entry used to define the parameter of a tesh option.
15 typedef struct s_optentry
17 int c; /* the character of the option */
20 * the type of the argument of the option
24 flag, /* it's a flag option, by default the flag is set to zero */
25 string, /* the option has strings as argument */
26 number /* the option has an integral positive number as argument */
29 byte* value; /* the value of the option */
30 byte* optional_value; /* the optional value of the option if not specified */
31 const char * long_name; /* the long name of the command */
32 }s_optentry_t,* optentry_t;
36 XBT_LOG_NEW_DEFAULT_CATEGORY(tesh,"TEst SHell utility");
39 /* Windows specific : the previous process error mode */
45 root_directory = NULL;
50 /* the current version of tesh */
54 /* ------------------------------------------------------------ */
56 /* ------------------------------------------------------------ */
58 /* ------------------------------------------------------------ */
60 /* ------------------------------------------------------------ */
63 /* --jobs is specified with arg */
67 /* --jobs option is not specified (use the default job count) */
69 default_number_of_jobs = 1;
71 /* --jobs is specified but has no arg (one job per unit) */
73 optional_number_of_jobs = -1;
75 /* the global timeout */
79 /* ------------------------------------------------------------ */
81 /* ------------------------------------------------------------ */
83 /* --C change the directory before running the units */
87 /* the include directories : see the !i metacommand */
91 /* the list of tesh files to run */
102 /* the ddlist of tesh file suffixes */
106 /* ------------------------------------------------------------ */
108 /* ------------------------------------------------------------ */
110 /* if 1, keep going when some commands can't be found
111 * default value 0 : not keep going
116 /* if 1, ignore failures from commands
117 * default value : do not ignore failures
120 want_keep_going_unit = 0;
122 /* if 1, display tesh usage */
124 want_display_usage = 0;
126 /* if 1, display the tesh version */
128 want_display_version = 0;
130 /* if 1, the syntax of all tesh files is checked
131 * before running them
134 want_check_syntax = 0;
136 /* if 1, all the tesh file of the current directory
140 want_load_directory = 0;
142 /* if 1, the status of all the units is display at
148 /* if 1, the directories are displayed */
150 dont_want_display_directory = 0;
152 /* if 1, just check the syntax of all the tesh files
158 /* if 1, display the tesh files syntax and exit */
160 want_display_semantic = 0;
166 want_just_display = 0;
172 display_data_base = 0;
177 /* the semaphore used to synchronize the jobs */
181 /* the semaphore used by the runner to wait the end of all the units */
192 /* the table of the entries of the options */
193 static const struct s_optentry opt_entries[] =
195 { 'C', string, (byte*)&directories, 0, "directory" },
196 { 'x', string, (byte*)&suffixes, 0, "suffix" },
197 { 'e', flag, (byte*)&env_overrides, 0, "environment-overrides", },
198 { 'f', string, (byte*)&fstreams, 0, "file" },
199 { 'h', flag, (byte*)&want_display_usage, 0, "help" },
200 { 'a', flag, (byte*)&want_display_semantic, 0, "semantic" },
201 { 'i', flag, (byte*)&want_keep_going_unit, 0, "keep-going-unit" },
202 { 'I', string, (byte*)&includes, 0, "include-dir" },
203 { 'j', number, (byte*)&number_of_jobs, (byte*) &optional_number_of_jobs, "jobs" },
204 { 'k', flag, (byte*)&want_keep_going, 0, "keep-going" },
205 { 'c', flag, (byte*)&want_just_display, 0, "just-display" },
206 { 'd', flag, (byte*)&display_data_base, 0,"display-data-base" },
207 { 'q', flag, (byte*)&question, 0, "question" },
208 { 's', flag, (byte*)&want_silent, 0, "silent" },
209 { 'V', flag, (byte*)&want_display_version, 0, "version" },
210 { 'w', flag, (byte*)&dont_want_display_directory, 0,"dont-display-directory" },
211 { 'n', flag, (byte*)&want_dry_run, 0, "dry-run"},
212 { 't', number, (byte*)&timeout, 0, "timeout" },
213 { 'S', flag, (byte*)&want_check_syntax, 0, "check-syntax"},
214 { 'r', flag, (byte*)&want_load_directory, 0, "load-directory"},
215 { 'v', flag, (byte*)&want_verbose, 0, "verbose"},
216 { 'F', string,(byte*)&excludes, 0, "exclude"},
217 { 'l', string,(byte*)&logs,0,"log"},
223 static const char* usage[] =
226 " -C DIRECTORY, --directory=DIRECTORY Change to DIRECTORY before running any commands.\n",
227 " -e, --environment-overrides Environment variables override files.\n",
228 " -f FILE, --file=FILE Read FILE as a teshfile.\n",
230 " all argument of the command line without\n",
231 " option is dealed as a tesh file.\n",
232 " -h, --help Display this message and exit.\n",
233 " -i, --keep-going-unit Ignore failures from commands.\n",
234 " The possible failures are :\n",
235 " - the exit code differ from the expected\n",
236 " - the signal throw differ from the expected\n",
237 " - the output differ from the expected\n",
238 " - the read pipe is broken\n",
239 " - the write pipe is broken\n",
240 " - the command assigned delay is outdated\n",
241 " -I DIRECTORY, --include-dir=DIRECTORY Search DIRECTORY for included files.\n",
242 " -j [N], --jobs[=N] Allow N commands at once; infinite commands with\n"
244 " -k, --keep-going Keep going when some commands can't be made or\n",
246 " -c, --just-display Don't actually run any commands; just display them.\n",
247 " -p, --display-data-base Display tesh's internal database.\n",
248 " -q, --question Run no commands; exit status says if up to date.\n",
249 " -s, --silent, Don't echo commands.\n",
250 " -V, --version Display the version number of tesh and exit.\n",
251 " -d, --dont-display-directory Don't display the current directory.\n",
252 " -n, --dry-run Check the syntax of the specified tesh files, display the result and exit.\n",
253 " -t, --timeout Wait the end of the commands at most timeout seconds.\n",
254 " -S, --check-syntax Check the syntax of the tesh files before run them. \n",
255 " -x, --suffix Consider the new suffix for the tesh files.\n"
257 " the default suffix for the tesh files is \".tesh\".\n",
258 " -a, --semantic Display the tesh file metacommands syntax and exit.\n",
259 " -b, --build-file Build a tesh file.\n",
260 " -r, --load-directory Run all the tesh files located in the directories specified by the option --directory.\n",
261 " -v, --verbose Display the status of the commands.\n",
262 " -F file , --exclude=FILE Ignore the tesh file FILE.\n",
263 " -l format, --log Format of the xbt logs.\n",
267 /* the string of options of tesh */
269 optstring[1 + sizeof (opt_entries) / sizeof (opt_entries[0]) * 3];
271 /* the option table of tesh */
273 option longopts[(sizeof (opt_entries) / sizeof (s_optentry_t))];
279 process_command_line(int argc, char** argv);
285 display_usage(int exit_code);
288 display_version(void);
294 display_semantic(void);
302 main(int argc, char* argv[])
306 /* process the command line */
307 if((exit_code = process_command_line(argc, argv)))
310 /* initialize the xbt library
311 * for thread portability layer
314 if(!lstrings_is_empty(logs))
316 int size = lstrings_get_size(logs);
317 char** cstr = lstrings_to_cstr(logs);
319 xbt_init(&size, cstr);
325 xbt_init(&argc, argv);
327 /* the user wants to display the usage of tesh */
328 if(want_display_usage)
331 /* the user wants to display the version of tesh */
332 if(want_display_version)
338 /* the user wants to display the semantic of the tesh file metacommands */
339 if(want_display_semantic)
345 if(!directories_has_directories_to_load(directories) && want_load_directory)
346 WARN0("--load-directory specified but no directory specified");
348 excludes_check(excludes, fstreams);
351 if((exit_code = load()))
356 if(-2 == number_of_jobs)
357 {/* --jobs is not specified (use the default value) */
358 number_of_jobs = default_number_of_jobs;
360 else if(optional_number_of_jobs == number_of_jobs)
361 {/* --jobs option is specified with no args (use one job per unit) */
362 number_of_jobs = fstreams_get_size(fstreams);
365 if(number_of_jobs > fstreams_get_size(fstreams))
366 {/* --jobs = N is specified and N is more than the number of tesh files */
368 WARN0("number of requested jobs exceed the number of files");
370 /* assume one job per file */
371 number_of_jobs = fstreams_get_size(fstreams);
374 /* initialize the semaphore used to synchronize the jobs */
375 jobs_sem = xbt_os_sem_init(number_of_jobs);
377 /* initialize the semaphore used by the runner to wait for the end of all units */
378 units_sem = xbt_os_sem_init(0);
380 /* initialize the runner */
381 if((0 != (exit_code = runner_init(
389 if(want_just_display && want_silent)
392 if(want_just_display && want_dry_run)
393 WARN0("mismatch in the syntax : --just-check-syntax and --just-display options at same time");
395 /* run all the units */
398 /* show the result of the units */
399 if(want_verbose || want_dry_run)
400 runner_display_status();
403 /* all the test are runned, destroy the runner */
406 /* then, finalize tesh */
418 char* buffer = getcwd(NULL, 0);
421 /* Windows specific : don't display the general-protection-fault message box and
422 * the the critical-error-handler message box (instead the system send the error
423 * to the calling process : tesh)
425 prev_error_mode = SetErrorMode(SEM_FAILCRITICALERRORS | SEM_NOGPFAULTERRORBOX);
428 /* used to store the file streams to run */
429 fstreams = fstreams_new(DEFAULT_FSTREAMS_CAPACITY, fstream_free);
431 root_directory = directory_new(buffer,want_load_directory);
433 /* used to store the directories to loads */
434 directories = directories_new();
436 /* register the current directory */
437 directories_add(directories, root_directory);
439 /* used to store the includes directories */
440 includes = vector_new(DEFAULT_INCLUDES_CAPACITY, directory_free);
443 logs = lstrings_new();
445 /* used to to store all the excluded file streams */
446 excludes = excludes_new();
448 /* list of file streams suffixes */
449 suffixes = lstrings_new();
451 lstrings_push_back(suffixes,".tesh");
459 chdir(directory_get_name(root_directory));
461 if(want_load_directory)
462 directories_load(directories, fstreams, suffixes);
464 /* on a aucun fichier specifie dans la ligne de commande
465 * l'option --run-current-directory n'a pas ete specifie ou aucun fichier ne se trouve dans le repertoire a charger
467 if(fstreams_is_empty(fstreams))
469 struct stat buffer = {0};
471 /* add the default tesh file if it exists */
472 if(!stat("teshfile", &buffer) && S_ISREG(buffer.st_mode))
473 fstreams_add(fstreams, fstream_new(getcwd(NULL, 0), "teshfile"));
476 if(!excludes_is_empty(excludes) && !fstreams_is_empty(fstreams))
477 fstreams_exclude(fstreams, excludes);
479 if(fstreams_is_empty(fstreams))
480 fstreams_add(fstreams, fstream_new(NULL, "stdin"));
482 fstreams_load(fstreams);
490 if((!exit_code && want_display_usage) || (!exit_code && !prepared))
491 display_usage(exit_code);
494 fstreams_free((void**)&fstreams);
497 excludes_free((void**)&excludes);
500 directories_free((void**)&directories);
503 vector_free(&includes);
506 lstrings_free(&suffixes);
509 lstrings_free(&logs);
511 /* destroy the semaphore used to synchronize the jobs */
513 xbt_os_sem_destroy(jobs_sem);
516 xbt_os_sem_destroy(units_sem);
518 /* exit from the xbt framework */
522 SetErrorMode(prev_error_mode);
525 if(!want_verbose && !want_dry_run && !want_silent && !want_just_display)
526 INFO2("tesh terminated with exit code %d : %s",exit_code, (!exit_code ? "success" : error_to_string(exit_code)));
537 if(optstring[0] != '\0')
543 /* Return switch and non-switch args in order, regardless of
544 POSIXLY_CORRECT. Non-switch args are returned as option 1. */
546 /* le premier caractère de la chaîne d'options vaut -.
547 * les arguments ne correspondant pas à une option sont
548 * manipulés comme s'ils étaient des arguments d'une option
549 * dont le caractère est le caractère de code 1
553 for (i = 0; opt_entries[i].c != '\0'; ++i)
555 /* initialize le nom de l'option longue*/
556 longopts[i].name = (opt_entries[i].long_name == 0 ? "" : opt_entries[i].long_name);
558 /* getopt_long() retourne la valeur de val */
559 longopts[i].flag = 0;
561 /* la valeur de l'option courte est le caractère spécifié dans opt_entries[i].c */
562 longopts[i].val = opt_entries[i].c;
564 /* on l'ajoute à la chaine des optstring */
565 *p++ = opt_entries[i].c;
567 switch (opt_entries[i].type)
569 /* si c'est une option qui sert a positionner un flag ou que l'on doit ignorée, elle n'a pas d'argument */
571 longopts[i].has_arg = no_argument;
574 /* c'est une option qui attent un argument :
575 * une chaine de caractères, un nombre flottant,
576 * ou un entier positif
583 if(opt_entries[i].optional_value != 0)
587 longopts[i].has_arg = optional_argument;
590 longopts[i].has_arg = required_argument;
597 longopts[i].name = 0;
601 process_command_line(int argc, char** argv)
603 register const struct s_optentry* entry;
605 directory_t directory;
608 /* initialize the options table of tesh */
611 /* display the errors of the function getopt_long() */
616 while (optind < argc)
618 c = getopt_long (argc, argv, optstring, longopts, (int *) 0);
622 /* end of the command line or "--". */
627 /* the argument of the command line is not an option (no "-"), assume it's a tesh file */
628 /*struct stat buffer = {0};
629 char* prev = getcwd(NULL, 0);
631 directory = directories_get_back(directories);
633 chdir(directory->name);
635 if(stat(optarg, &buffer) || !S_ISREG(buffer.st_mode))
639 ERROR1("file %s not found", optarg);
640 return EFILENOTFOUND;
646 directory = directories_search_fstream_directory(directories, optarg);
650 if(1 == directories_get_size(directories))
652 ERROR1("file %s not found in the current directory",optarg);
653 return EFILENOTINCURDIR;
657 ERROR1("file %s not found in the specified directories",optarg);
658 return EFILENOTINSPECDIR;
662 if(!(fstream = fstream_new(directory_get_name(directory), optarg)))
664 ERROR1("command line processing failed with the error code %d", errno);
665 return EPROCESSCMDLINE;
669 if(fstreams_contains(fstreams, fstream))
671 fstream_free((void**)&fstream);
672 WARN1("file %s already specified", optarg);
676 if((errno = fstreams_add(fstreams, fstream)))
678 fstream_free((void**)&fstream);
679 ERROR1("command line processing failed with the error code %d", errno);
680 return EPROCESSCMDLINE;
687 /* unknown option, getopt_long() displays the error */
692 for (entry = opt_entries; entry->c != '\0'; ++entry)
701 ERROR0("command line processing failed : internal error");
702 return EPROCESSCMDLINE;
707 /* set the flag to one */
708 *(int*) entry->value = 1;
717 /* an option with a optional arg is specified use the entry->optional_value */
718 optarg = (char*)entry->optional_value;
720 else if (*optarg == '\0')
722 /* a non optional argument is not specified */
723 ERROR2("the option %c \"%s\"requires an argument",entry->c,entry->long_name);
727 /* --directory option */
728 if(!strcmp(entry->long_name,"directory"))
730 if(!(directory = directory_new(optarg, want_load_directory)))
734 ERROR1("directory %s not found",optarg);
739 ERROR1("command line processing failed with the error code %d", errno);
740 return EPROCESSCMDLINE;
745 if(directories_contains(directories, directory))
747 directory_free((void**)&directory);
748 WARN1("directory %s already specified",optarg);
752 if((errno = directories_add(directories, directory)))
754 directory_free((void**)&directory);
755 ERROR1("command line processing failed with the error code %d", errno);
756 return EPROCESSCMDLINE;
761 /* --suffix option */
762 else if(!strcmp(entry->long_name,"suffix"))
764 if(strlen(optarg) > MAX_SUFFIX)
766 ERROR1("suffix %s too long",optarg);
767 return ESUFFIXTOOLONG;
772 char suffix[MAX_SUFFIX + 2] = {0};
773 sprintf(suffix,".%s",optarg);
775 if(lstrings_contains(suffixes, suffix))
776 WARN1("suffix %s already specified", optarg);
778 lstrings_push_back(suffixes, suffix);
782 if(lstrings_contains(suffixes, optarg))
783 WARN1("suffix %s already specified", optarg);
785 lstrings_push_back(suffixes, optarg);
789 else if(!strcmp(entry->long_name,"file"))
792 /* the argument of the command line is not an option (no "-"), assume it's a tesh file */
793 /*struct stat buffer = {0};
794 char* prev = getcwd(NULL, 0);
796 directory = directories_get_back(directories);
798 chdir(directory->name);
800 if(stat(optarg, &buffer) || !S_ISREG(buffer.st_mode))
804 ERROR1("file %s not found", optarg);
805 return EFILENOTFOUND;
811 directory = directories_search_fstream_directory(directories, optarg);
815 if(1 == directories_get_size(directories))
817 ERROR1("file %s not found in the current directory",optarg);
818 return EFILENOTINCURDIR;
822 ERROR1("file %s not found in the specified directories",optarg);
823 return EFILENOTINSPECDIR;
827 if(!(fstream = fstream_new(directory_get_name(directory),optarg)))
829 ERROR1("command line processing failed with the error code %d", errno);
830 return EPROCESSCMDLINE;
834 if(fstreams_contains(fstreams, fstream))
836 fstream_free((void**)&fstream);
837 WARN1("file %s already specified", optarg);
841 if((errno = fstreams_add(fstreams, fstream)))
843 fstream_free((void**)&fstream);
844 ERROR1("command line processing failed with the error code %d", errno);
845 return EPROCESSCMDLINE;
850 /* --include-dir option */
851 else if(!strcmp(entry->long_name,"include-dir"))
853 if(!(directory = directory_new(optarg, want_load_directory)))
857 ERROR1("%s is not a directory",optarg);
862 ERROR1("command line processing failed with the error code %d", errno);
863 return EPROCESSCMDLINE;
868 if(vector_contains(includes, directory))
870 directory_free((void**)&directory);
871 WARN1("include directory %s already specified",optarg);
876 if((errno = vector_push_back(includes, directory)))
878 directory_free((void**)&directory);
879 ERROR1("command line processing failed with the error code %d", errno);
880 return EPROCESSCMDLINE;
885 /* --exclude option */
886 else if(!strcmp(entry->long_name,"exclude"))
888 directory = directories_get_back(directories);
890 if(!(fstream = fstream_new(directory_get_name(directory), optarg)))
894 ERROR1("file to exclude %s not found", optarg);
895 return EFILENOTFOUND;
899 ERROR1("command line processing failed with the error code %d", errno);
900 return EPROCESSCMDLINE;
905 if(excludes_contains(excludes, fstream))
907 fstream_free((void**)&fstream);
908 WARN1("file to exclude %s already specified", optarg);
912 if((errno = excludes_add(excludes, fstream)))
914 fstream_free((void**)&fstream);
915 ERROR1("command line processing failed with the error code %d", errno);
916 return EPROCESSCMDLINE;
922 else if(!strcmp(entry->long_name,"log"))
924 lstrings_push_back(logs, optarg);
934 /* strictly positve number options */
937 if ((NULL == optarg) && (argc > optind))
941 for (cp = argv[optind]; isdigit(cp[0]); ++cp)
943 optarg = argv[optind++];
946 /* the number option is specified */
949 int i = atoi(optarg);
952 for (cp = optarg; isdigit(cp[0]); ++cp);
954 if (i < 1 || cp[0] != '\0')
956 ERROR2("option %c \"%s\" requires an strictly positive integer as argument",entry->c, entry->long_name);
957 return ENOTPOSITIVENUM;
960 *(int*)entry->value = i;
962 /* the number option is specified but has no arg, use the optional value*/
964 *(int*)entry->value = *(int*) entry->optional_value;
978 display_usage(int exit_code)
983 if (want_display_version)
986 stream = exit_code ? stderr : stdout;
988 fprintf (stream, "Usage: tesh [options] [file] ...\n");
990 for (cpp = usage; *cpp; ++cpp)
991 fputs (*cpp, stream);
993 fprintf(stream, "\nReport bugs to <martin.quinson@loria.fr | malek.cherier@loria.fr>\n");
997 display_version(void)
999 /* TODO : display the version of tesh */
1000 printf("Version :\n");
1001 printf(" tesh version %s : Mini shell specialized in running test units by Martin Quinson \n", version);
1002 printf(" and Malek Cherier\n");
1003 printf(" Copyright (c) 2007, 2008 Martin Quinson, Malek Cherier\n");
1004 printf(" All rights reserved\n");
1005 printf(" This program is free software; you can redistribute it and/or modify it\n");
1006 printf(" under the terms of the license (GNU LGPL) which comes with this package.\n\n");
1008 if(!want_display_usage)
1009 printf("Report bugs to <martin.quinson@loria.fr | malek.cherier@loria.fr>");
1013 display_semantic(void)
1018 FILE* stream = fopen("README.txt", "r");
1022 ERROR0("Unable to locate the README.txt file");
1023 exit_code = EREADMENOTFOUND;
1027 while(getline(&line, &len, stream) != -1)