2 * src/main.c - this file contains the main function of tesh.
4 * Copyright 2008,2009 Martin Quinson, Malek Cherier All right 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.
14 #include <directory.h>
15 #include <directories.h>
23 * entry used to define the parameter of a tesh option.
25 typedef struct s_optentry
27 int c; /* the character of the option */
30 * the type of the argument of the option
34 flag, /* it's a flag option, by default the flag is set to zero */
35 string, /* the option has strings as argument */
36 number /* the option has an integral positive number as argument */
39 byte* value; /* the value of the option */
40 byte* optional_value; /* the optional value of the option if not specified */
41 const char * long_name; /* the long name of the command */
42 }s_optentry_t,* optentry_t;
46 XBT_LOG_NEW_DEFAULT_CATEGORY(tesh,"TEst SHell utility");
49 /* Windows specific : the previous process error mode */
54 /* this object represents the root directory */
56 root_directory = NULL;
58 /* the current version of tesh */
62 /* ------------------------------------------------------------ */
64 /* ------------------------------------------------------------ */
66 /* ------------------------------------------------------------ */
68 /* ------------------------------------------------------------ */
71 /* --jobs is specified with arg */
75 /* --jobs option is not specified (use the default job count) */
79 /* --jobs is specified but has no arg (one job per unit) */
81 optional_jobs_nb = -1;
83 /* the global timeout */
87 /* ------------------------------------------------------------ */
89 /* ------------------------------------------------------------ */
91 /* --C change the directory before running the units */
95 /* the include directories : see the !i metacommand */
99 /* the list of tesh files to run */
103 /* the list of tesh file suffixes */
111 /* ------------------------------------------------------------ */
113 /* ------------------------------------------------------------ */
115 /* if 1, keep going when some commands can't be found
116 * default value 0 : not keep going
121 /* if 1, ignore failures from commands
122 * default value : do not ignore failures
125 keep_going_unit_flag = 0;
127 /* if 1, display tesh usage */
129 print_usage_flag = 0;
131 /* if 1, display the tesh version */
133 print_version_flag = 0;
135 /* if 1, the status of all the units is display at
141 /* if 1 and the flag want_summay is set to 1 tesh display the detailed summary of the run */
143 detail_summary_flag = 0;
145 /* if 1, the directories are displayed */
147 print_directory_flag = 0;
149 /* if 1, just check the syntax of all the tesh files
155 /* if 1, display the tesh files syntax and exit */
157 print_readme_flag = 0;
165 /* the semaphore used to synchronize the jobs */
169 /* the semaphore used by the runner to wait the end of all the units */
195 /* the table of the entries of the options */
196 static const struct s_optentry opt_entries[] =
198 { 'C', string, (byte*)NULL, 0, "directory" },
199 { 'x', string, (byte*)&suffixes, 0, "suffix" },
200 { 'f', string, (byte*)&fstreams, 0, "file" },
201 { 'h', flag, (byte*)&print_usage_flag, 0, "help" },
202 { 'a', flag, (byte*)&print_readme_flag, 0, "README" },
203 { 'k', flag, (byte*)&keep_going_flag, 0, "keep-going" },
204 { 'i', flag, (byte*)&keep_going_unit_flag, 0, "keep-going-unit"},
205 { 'I', string, (byte*)&include_dirs, 0, "include-dir" },
206 { 'j', number, (byte*)&jobs_nb, (byte*) &optional_jobs_nb, "jobs" },
207 { 'm', flag, (byte*)&detail_summary_flag, 0, "detail-summary" },
208 { 'c', flag, (byte*)&just_print_flag, 0, "just-print" },
209 { 's', flag, (byte*)&silent_flag, 0, "silent" },
210 { 'V', flag, (byte*)&print_version_flag, 0, "version" },
211 { 'w', flag, (byte*)&print_directory_flag, 0,"dont-print-directory" },
212 { 'n', flag, (byte*)&dry_run_flag, 0, "dry-run"},
213 { 't', number, (byte*)&timeout, 0, "timeout" },
214 { 'r', string, (byte*)&directories, 0, "load-directory"},
215 { 'v', flag, (byte*)&summary_flag, 0, "summary"},
216 { 'F', string,(byte*)&excludes, 0, "exclude"},
217 { 'l', string, (byte*)NULL, 0, "log" },
222 static const char* usage[] =
225 " -C DIRECTORY, --directory=DIRECTORY Change to DIRECTORY before running any commands.\n",
226 " -e, --environment-overrides Environment variables override files.\n",
227 " -f FILE, --file=FILE Read FILE as a teshfile.\n",
229 " all argument of the command line without\n",
230 " option is dealed as a tesh file.\n",
231 " -h, --help Print this message and exit.\n",
232 " -i, --keep-going-unit Ignore failures from commands.\n",
233 " The possible failures are :\n",
234 " - the exit code differ from the expected\n",
235 " - the signal throw differ from the expected\n",
236 " - the output differ from the expected\n",
237 " - the read pipe is broken\n",
238 " - the write pipe is broken\n",
239 " - the command assigned delay is outdated\n",
240 " -I DIRECTORY, --include-dir=DIRECTORY Search DIRECTORY for included files.\n",
241 " -j [N], --jobs[=N] Allow N units at once; infinite units with\n"
243 " -k, --keep-going Keep going when some commands can't be made or\n",
245 " -c, --just-print Don't actually run any commands; just print them.\n",
246 " -s, --silent, Don't echo commands.\n",
247 " -V, --version Print the version number of tesh and exit.\n",
248 " -d, --dont-print-directory Don't display the current directory.\n",
249 " -n, --dry-run Check the syntax of the specified tesh files, display the result and exit.\n",
250 " -t, --timeout Wait the end of the commands at most timeout seconds.\n",
251 " -x, --suffix Consider the new suffix for the tesh files.\n"
253 " the default suffix for the tesh files is \".tesh\".\n",
254 " -a, --README Print the read me file and exit.\n",
255 " -r, --load-directory Run all the tesh files located in the directories specified by the option --directory.\n",
256 " -v, --summary Display the status of the commands.\n",
257 " -m, --detail-summary Detail the summary of the run.\n",
258 " -F file , --exclude=FILE Ignore the tesh file FILE.\n",
259 " -l format, --log Format of the xbt logs.\n",
263 /* the string of options of tesh */
265 optstring[1 + sizeof (opt_entries) / sizeof (opt_entries[0]) * 3];
267 /* the option table of tesh */
269 option longopts[(sizeof (opt_entries) / sizeof (s_optentry_t))];
275 process_command_line(int argc, char** argv);
306 sig_int_handler(int signum)
320 free_string(void* str)
326 main(int argc, char* argv[])
328 /* set the locale to the default*/
329 setlocale(LC_ALL,"");
331 /* xbt initialization */
332 xbt_init(&argc, argv);
334 /* initialize tesh */
338 /* process the command line */
339 if(process_command_line(argc, argv) < 0)
342 /* move to the root directory (the directory may change during the command line processing) */
343 chdir(root_directory->name);
345 /* the user wants to display the usage of tesh */
346 if(print_version_flag)
350 if(!print_usage_flag)
354 /* the user wants to display the usage of tesh */
361 /* the user wants to display the semantic of the tesh file metacommands */
362 if(print_readme_flag)
368 /* load tesh files */
372 /* use by the finalize function to known if it must display the tesh usage */
376 {/* --jobs is not specified (use the default value) */
377 jobs_nb = default_jobs_nb;
379 else if(optional_jobs_nb == jobs_nb)
380 {/* --jobs option is specified with no args (use one job per unit) */
381 jobs_nb = fstreams_get_size(fstreams);
384 if(jobs_nb > fstreams_get_size(fstreams))
385 {/* --jobs = N is specified and N is more than the number of tesh files */
387 WARN0("Number of requested jobs exceed the number of files to run");
389 /* assume one job per file */
390 jobs_nb = fstreams_get_size(fstreams);
393 if(jobs_nb != 1 && dry_run_flag)
396 INFO0("Dry run specified : force jobs count to 1");
399 /* initialize the semaphore used to synchronize all the units */
400 jobs_sem = xbt_os_sem_init(jobs_nb);
402 /* initialize the semaphore used by the runner to wait for the end of all units */
403 units_sem = xbt_os_sem_init(0);
405 /* initialize the runner */
406 if(runner_init(/*check_syntax_flag,*/ timeout, fstreams) < 0)
409 if(just_print_flag && silent_flag)
412 if(just_print_flag && dry_run_flag)
413 WARN0("mismatch in the syntax : --just-check-syntax and --just-display options at same time");
415 /* run all the units */
418 if(runner_is_timedout())
419 ERROR1("Tesh timed out after `(%d)' seconds", timeout);
421 /* show the result of the units */
422 if(detail_summary_flag ||summary_flag || dry_run_flag)
425 /* all the test are runned, destroy the runner */
428 /* then, finalize tesh (release all the allocated memory and exits) */
437 /* init -- initialize tesh : allocated all the objects needed by tesh to run
438 * the tesh files specified in the command line.
440 * return If successful the function returns zero. Otherwise the function returns
441 * -1 and sets the global variable errno to the appropriate error code.
450 char* suffix = strdup(".tesh");
453 /* Windows specific : don't display the general-protection-fault message box and
454 * the the critical-error-handler message box (instead the system send the error
455 * to the calling process : tesh)
457 prev_error_mode = SetErrorMode(SEM_FAILCRITICALERRORS | SEM_NOGPFAULTERRORBOX);
459 /* handle the interrupt signal */
460 signal(SIGINT, sig_int_handler);
462 /* Ignore pipe issues.
463 * They will show up when we try to send data to dead buddies,
464 * but we will stop doing so when we're done with provided input
467 struct sigaction act;
468 memset(&act,0, sizeof(struct sigaction));
469 act.sa_handler = SIG_IGN;
470 sigaction(SIGPIPE, &act, NULL);
472 memset(&act,0, sizeof(struct sigaction));
473 act.sa_handler = sig_int_handler;
474 sigaction(SIGINT, &act, NULL);*/
481 /* used to store the files to run */
482 if(!(fstreams = fstreams_new((void_f_pvoid_t)fstream_free)))
484 ERROR1("(system error) %s", strerror(errno));
488 /* register the current directory */
489 if(!(buffer = getcwd(NULL, 0)))
491 ERROR1("(system error) %s", strerror(errno));
495 /* save the root directory */
496 if(!(root_directory = directory_new(buffer)))
498 ERROR1("(system error) %s", strerror(errno));
504 /* the directories to loads */
505 if(!(directories = directories_new()))
507 ERROR1("(system error) %s", strerror(errno));
511 /* the include directories */
512 include_dirs = xbt_dynar_new(sizeof(directory_t), (void_f_pvoid_t)directory_free);
514 /* the excluded files */
515 if(!(excludes = excludes_new()))
517 ERROR1("(system error) %s", strerror(errno));
522 suffixes = xbt_dynar_new(sizeof(char*),free_string);
524 /* register the default suffix ".tesh" */
525 xbt_dynar_push(suffixes, &suffix);
530 /* load -- load the tesh files to run */
535 /* if the directories object is not empty load all the tesh files contained in
536 * the directories specified in the command line (this tesh files must have the
537 * a suffix specified in the suffixes object.
539 if(!directories_is_empty(directories))
540 directories_load(directories, fstreams, suffixes);
542 /* if no tesh file has been specified in the command line try to load the default tesh file
543 * teshfile from the current directory
545 if(fstreams_is_empty(fstreams))
547 struct stat buffer = {0};
549 /* add the default tesh file if it exists in the current directory */
550 if(!stat("teshfile", &buffer) && S_ISREG(buffer.st_mode))
551 fstreams_add(fstreams, fstream_new(getcwd(NULL, 0), "teshfile"));
554 /* excludes the files specified in the command line and stored in the excludes object */
555 if(!excludes_is_empty(excludes) && !fstreams_is_empty(fstreams))
557 /* check the files to excludes before */
558 excludes_check(excludes, fstreams);
560 /* exclude the specified tesh files */
561 fstreams_exclude(fstreams, excludes);
564 /* if the fstreams object is empty use the stdin */
565 if(fstreams_is_empty(fstreams))
566 fstreams_add(fstreams, fstream_new(NULL, "stdin"));
568 /* load the tesh files (open them) */
569 fstreams_load(fstreams);
573 /* finalize -- cleanup all the allocated objects and display the tesh usage if needed */
577 /* delete the fstreams object */
579 fstreams_free((void**)&fstreams);
581 /* delete the excludes object */
583 excludes_free((void**)&excludes);
585 /* delete the directories object */
587 directories_free((void**)&directories);
589 /* delete the root directory object */
591 directory_free((void**)&root_directory);
593 /* delete the include directories object */
595 xbt_dynar_free(&include_dirs);
597 /* delete the list of tesh files suffixes */
599 xbt_dynar_free(&suffixes);
601 /* destroy the semaphore used to synchronize the units */
603 xbt_os_sem_destroy(jobs_sem);
605 /* destroy the semaphore used by the runner used to wait for the end of the units */
607 xbt_os_sem_destroy(units_sem);
609 /* Windows specific (restore the previouse error mode */
611 SetErrorMode(prev_error_mode);
615 INFO0("Tesh interrupted (receive a SIGINT)");
616 else if(!summary_flag && !detail_summary_flag && !dry_run_flag && !silent_flag && !just_print_flag && !print_version_flag && !print_usage_flag && is_tesh_root)
619 INFO2("Tesh terminated with exit code %d : %s",exit_code, "success");
623 ERROR3("Tesh terminated with exit code `(<%s> %s)' (%d)",err_line, error_to_string(exit_code, err_kind), exit_code);
625 ERROR2("Tesh terminated with exit code `(%s)' (%d)", error_to_string(exit_code, err_kind), exit_code);
633 /* exit from the xbt framework */
638 /* exit with the last error code */
643 /* init_options -- initialize the options string */
650 /* the function has been already called */
651 if(optstring[0] != '\0')
658 for (i = 0; opt_entries[i].c != '\0'; ++i)
660 /* initialize the long name of the option*/
661 longopts[i].name = (opt_entries[i].long_name == 0 ? "" : opt_entries[i].long_name);
663 /* getopt_long returns the value of val */
664 longopts[i].flag = 0;
666 /* the short option */
667 longopts[i].val = opt_entries[i].c;
669 /* add the short option in the options string */
670 *p++ = opt_entries[i].c;
672 switch (opt_entries[i].type)
674 /* if this option is used to set a flag or if the argument must be ignored
675 * the option has no argument
678 longopts[i].has_arg = no_argument;
681 /* the option has an argument */
687 if(opt_entries[i].optional_value != 0)
691 longopts[i].has_arg = optional_argument;
694 longopts[i].has_arg = required_argument;
701 longopts[i].name = 0;
704 /* process_command_line -- process the command line
706 * param argc the number of the arguments contained by the command line.
707 * param The array of C strings containing all the arguments of the command
710 * return If successful, the function returns 0. Otherwise -1 is returned
711 * and sets the global variable errno to indicate the error.
713 * errors [ENOENT] A file name specified in the command line does not exist
717 process_command_line(int argc, char** argv)
719 register const struct s_optentry* entry;
721 directory_t directory;
724 /* initialize the options string of tesh */
727 /* let the function getopt_long display the errors if any */
730 /* set option index to zero */
733 while (optind < argc)
735 c = getopt_long(argc, argv, optstring, longopts, (int *) 0);
739 /* end of the command line or "--". */
744 /* no option specified, assume it's a tesh file to run */
748 /* getpath returns -1 when the file to get the path doesn't exist */
749 if(getpath(optarg, &path) < 0)
755 ERROR1("File %s does not exist", optarg);
757 ERROR0("Insufficient memory is available to parse the command line : system error");
762 /* get to the last / (if any) to get the short name of the file */
763 delimiter = strrchr(optarg,'/');
767 delimiter = strrchr(optarg,'\\');
770 /* create a new file stream which represents the tesh file to run */
771 fstream = fstream_new(path, delimiter ? delimiter + 1 : optarg);
775 /* if the list of all tesh files to run already contains this file
776 * destroy it and display a warning, otherwise add it in the list.
778 if(fstreams_contains(fstreams, fstream))
780 fstream_free(&fstream);
781 WARN1("File %s already specified to be run", optarg);
784 fstreams_add(fstreams, fstream);
789 /* unknown option, let getopt_long() displays the error */
790 ERROR0("Command line processing failed : invalid command line");
791 exit_code = EINVCMDLINE;
797 for(entry = opt_entries; entry->c != '\0'; ++entry)
806 ERROR0("Command line processing failed : internal error");
807 exit_code = EPROCCMDLINE;
814 /* set the flag to one */
815 *(int*) entry->value = 1;
824 /* an option with a optional arg is specified use the entry->optional_value */
825 optarg = (char*)entry->optional_value;
827 else if (*optarg == '\0')
829 /* a non optional argument is not specified */
830 ERROR2("Option %c \"%s\"requires an argument",entry->c,entry->long_name);
836 /* --load-directory option */
837 if(!strcmp(entry->long_name,"load-directory"))
841 if(translatepath(optarg, &path) < 0)
847 ERROR1("%s is not a directory",optarg);
849 ERROR0("Insufficient memory is available to process the command line - system error");
857 directory = directory_new(path);
860 if(directories_contains(directories, directory))
862 directory_free((void**)&directory);
863 WARN1("Directory %s already specified to be load",optarg);
866 directories_add(directories, directory);
871 else if(!strcmp(entry->long_name,"directory"))
875 if(translatepath(optarg, &path) < 0)
881 ERROR1("%s is not a directory",optarg);
883 ERROR0("Insufficient memory is available to process the command line - system error");
889 char* buffer = getcwd(NULL, 0);
892 if(!strcmp(buffer, path))
893 WARN1("Already in the directory %s", optarg);
894 else if(!print_directory_flag)
895 INFO1("Entering directory \"%s\"",path);
904 /* --suffix option */
905 else if(!strcmp(entry->long_name,"suffix"))
907 if(strlen(optarg) > MAX_SUFFIX)
909 ERROR1("Suffix %s too long",optarg);
910 exit_code = ESUFFIXTOOLONG;
921 char* suffix = xbt_new0(char, MAX_SUFFIX + 2);
922 sprintf(suffix,".%s",optarg);
924 xbt_dynar_foreach(suffixes, i, cur)
926 if(!strcmp(suffix, cur))
934 WARN1("Suffix %s already specified to be used", optarg);
936 xbt_dynar_push(suffixes, &suffix);
944 xbt_dynar_foreach(suffixes, i, cur)
946 if(!strcmp(optarg, cur))
954 WARN1("Suffix %s already specified to be used", optarg);
957 char* suffix = strdup(optarg);
958 xbt_dynar_push(suffixes, &suffix);
963 else if(!strcmp(entry->long_name,"file"))
968 if(getpath(optarg, &path) < 0)
974 ERROR1("File %s does not exist", optarg);
976 ERROR1("System error :`(%s)'", strerror(errno));
981 delimiter = strrchr(optarg,'/');
985 delimiter = strrchr(optarg,'\\');
988 fstream = fstream_new(path, delimiter ? delimiter + 1 : optarg);
992 if(fstreams_contains(fstreams, fstream))
994 fstream_free(&fstream);
995 WARN1("File %s already specified to run", optarg);
998 fstreams_add(fstreams, fstream);
1000 /* --include-dir option */
1001 else if(!strcmp(entry->long_name,"include-dir"))
1006 if(translatepath(optarg, &path) < 0)
1011 if(ENOTDIR == errno)
1012 ERROR1("%s is not a directory",optarg);
1014 ERROR0("Insufficient memory is available to process the command line - system error");
1024 directory = directory_new(path);
1027 xbt_dynar_foreach(include_dirs, i , cur)
1029 if(!strcmp(cur->name, optarg))
1038 directory_free((void**)&directory);
1039 WARN1("Include directory %s already specified to be used",optarg);
1043 xbt_dynar_push(include_dirs, &directory);
1047 /* --exclude option */
1048 else if(!strcmp(entry->long_name,"exclude"))
1053 if(getpath(optarg, &path) < 0)
1059 ERROR1("file %s does not exist", optarg);
1061 ERROR0("Insufficient memory is available to process the command line - system error");
1066 delimiter = strrchr(optarg,'/');
1070 delimiter = strrchr(optarg,'\\');
1073 fstream = fstream_new(path, delimiter ? delimiter + 1 : optarg);
1076 if(excludes_contains(excludes, fstream))
1078 fstream_free(&fstream);
1079 WARN1("File %s already specified to be exclude", optarg);
1082 excludes_add(excludes, fstream);
1085 else if(!strcmp(entry->long_name,"log"))
1087 /* NOTHING TODO : log for xbt */
1091 INFO1("Unexpected option %s", optarg);
1098 /* strictly positve number options */
1101 if ((NULL == optarg) && (argc > optind))
1105 for (cp = argv[optind]; isdigit(cp[0]); ++cp)
1107 optarg = argv[optind++];
1110 /* the number option is specified */
1113 int i = atoi(optarg);
1116 for (cp = optarg; isdigit(cp[0]); ++cp);
1118 if (i < 1 || cp[0] != '\0')
1120 ERROR2("Option %c \"%s\" requires an strictly positive integer as argument",entry->c, entry->long_name);
1121 exit_code = ENOTPOSITIVENUM;
1126 *(int*)entry->value = i;
1128 /* the number option is specified but has no arg, use the optional value*/
1130 *(int*)entry->value = *(int*) entry->optional_value;
1149 stream = exit_code ? stderr : stdout;
1161 fprintf (stream, "Usage: tesh [options] [file] ...\n");
1163 for (cpp = usage; *cpp; ++cpp)
1164 fputs (*cpp, stream);
1166 fprintf(stream, "\nReport bugs to <martin.quinson@loria.fr | malek.cherier@loria.fr>\n");
1182 /* TODO : display the version of tesh */
1183 printf("Version :\n");
1184 printf(" tesh version %s : Mini shell specialized in running test units by Martin Quinson \n", version);
1185 printf(" and Malek Cherier\n");
1186 printf(" Copyright (c) 2007, 2008 Martin Quinson, Malek Cherier\n");
1187 printf(" All rights reserved\n");
1188 printf(" This program is free software; you can redistribute it and/or modify it\n");
1189 printf(" under the terms of the license (GNU LGPL) which comes with this package.\n\n");
1191 if(!print_usage_flag)
1192 printf("Report bugs to <martin.quinson@loria.fr | malek.cherier@loria.fr>");
1201 FILE* stream = fopen("examples/README.tesh", "r");
1205 ERROR0("Unable to locate the README.txt file");
1206 exit_code = EREADMENOTFOUND;
1211 while(getline(&line, &len, stream) != -1)