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 */
192 /* the table of the entries of the options */
193 static const struct s_optentry opt_entries[] =
195 { 'C', string, (byte*)NULL, 0, "directory" },
196 { 'x', string, (byte*)&suffixes, 0, "suffix" },
197 { 'f', string, (byte*)&fstreams, 0, "file" },
198 { 'h', flag, (byte*)&print_usage_flag, 0, "help" },
199 { 'a', flag, (byte*)&print_readme_flag, 0, "README" },
200 { 'k', flag, (byte*)&keep_going_flag, 0, "keep-going" },
201 { 'i', flag, (byte*)&keep_going_unit_flag, 0, "keep-going-unit"},
202 { 'I', string, (byte*)&include_dirs, 0, "include-dir" },
203 { 'j', number, (byte*)&jobs_nb, (byte*) &optional_jobs_nb, "jobs" },
204 { 'm', flag, (byte*)&detail_summary_flag, 0, "detail-summary" },
205 { 'c', flag, (byte*)&just_print_flag, 0, "just-print" },
206 { 's', flag, (byte*)&silent_flag, 0, "silent" },
207 { 'V', flag, (byte*)&print_version_flag, 0, "version" },
208 { 'w', flag, (byte*)&print_directory_flag, 0,"dont-print-directory" },
209 { 'n', flag, (byte*)&dry_run_flag, 0, "dry-run"},
210 { 't', number, (byte*)&timeout, 0, "timeout" },
211 { 'r', string, (byte*)&directories, 0, "load-directory"},
212 { 'v', flag, (byte*)&summary_flag, 0, "summary"},
213 { 'F', string,(byte*)&excludes, 0, "exclude"},
214 { 'l', string, (byte*)NULL, 0, "log" },
219 static const char* usage[] =
222 " -C DIRECTORY, --directory=DIRECTORY Change to DIRECTORY before running any commands.\n",
223 " -e, --environment-overrides Environment variables override files.\n",
224 " -f FILE, --file=FILE Read FILE as a teshfile.\n",
226 " all argument of the command line without\n",
227 " option is dealed as a tesh file.\n",
228 " -h, --help Print this message and exit.\n",
229 " -i, --keep-going-unit Ignore failures from commands.\n",
230 " The possible failures are :\n",
231 " - the exit code differ from the expected\n",
232 " - the signal throw differ from the expected\n",
233 " - the output differ from the expected\n",
234 " - the read pipe is broken\n",
235 " - the write pipe is broken\n",
236 " - the command assigned delay is outdated\n",
237 " -I DIRECTORY, --include-dir=DIRECTORY Search DIRECTORY for included files.\n",
238 " -j [N], --jobs[=N] Allow N units at once; infinite units with\n"
240 " -k, --keep-going Keep going when some commands can't be made or\n",
242 " -c, --just-print Don't actually run any commands; just print them.\n",
243 " -s, --silent, Don't echo commands.\n",
244 " -V, --version Print the version number of tesh and exit.\n",
245 " -d, --dont-print-directory Don't display the current directory.\n",
246 " -n, --dry-run Check the syntax of the specified tesh files, display the result and exit.\n",
247 " -t, --timeout Wait the end of the commands at most timeout seconds.\n",
248 " -x, --suffix Consider the new suffix for the tesh files.\n"
250 " the default suffix for the tesh files is \".tesh\".\n",
251 " -a, --README Print the read me file and exit.\n",
252 " -r, --load-directory Run all the tesh files located in the directories specified by the option --directory.\n",
253 " -v, --summary Display the status of the commands.\n",
254 " -m, --detail-summary Detail the summary of the run.\n",
255 " -F file , --exclude=FILE Ignore the tesh file FILE.\n",
256 " -l format, --log Format of the xbt logs.\n",
260 /* the string of options of tesh */
262 optstring[1 + sizeof (opt_entries) / sizeof (opt_entries[0]) * 3];
264 /* the option table of tesh */
266 option longopts[(sizeof (opt_entries) / sizeof (s_optentry_t))];
272 process_command_line(int argc, char** argv);
303 sig_int_handler(int signum)
317 free_string(void* str)
323 main(int argc, char* argv[])
325 /* set the locale to the default*/
326 setlocale(LC_ALL,"");
328 /* xbt initialization */
329 xbt_init(&argc, argv);
331 /* initialize tesh */
335 /* process the command line */
336 if(process_command_line(argc, argv) < 0)
339 /* move to the root directory (the directory may change during the command line processing) */
340 chdir(root_directory->name);
342 /* the user wants to display the usage of tesh */
343 if(print_version_flag)
347 if(!print_usage_flag)
351 /* the user wants to display the usage of tesh */
358 /* the user wants to display the semantic of the tesh file metacommands */
359 if(print_readme_flag)
365 /* load tesh files */
369 /* use by the finalize function to known if it must display the tesh usage */
373 {/* --jobs is not specified (use the default value) */
374 jobs_nb = default_jobs_nb;
376 else if(optional_jobs_nb == jobs_nb)
377 {/* --jobs option is specified with no args (use one job per unit) */
378 jobs_nb = fstreams_get_size(fstreams);
381 if(jobs_nb > fstreams_get_size(fstreams))
382 {/* --jobs = N is specified and N is more than the number of tesh files */
384 WARN0("Number of requested jobs exceed the number of files to run");
386 /* assume one job per file */
387 jobs_nb = fstreams_get_size(fstreams);
390 if(jobs_nb != 1 && dry_run_flag)
393 INFO0("Dry run specified : force jobs count to 1");
396 /* initialize the semaphore used to synchronize all the units */
397 jobs_sem = xbt_os_sem_init(jobs_nb);
399 /* initialize the semaphore used by the runner to wait for the end of all units */
400 units_sem = xbt_os_sem_init(0);
402 /* initialize the runner */
403 if(runner_init(/*check_syntax_flag,*/ timeout, fstreams) < 0)
406 if(just_print_flag && silent_flag)
409 if(just_print_flag && dry_run_flag)
410 WARN0("mismatch in the syntax : --just-check-syntax and --just-display options at same time");
412 /* run all the units */
415 if(runner_is_timedout())
416 ERROR1("Tesh timed out after `(%d)' seconds", timeout);
418 /* show the result of the units */
419 if(detail_summary_flag ||summary_flag || dry_run_flag)
422 /* all the test are runned, destroy the runner */
425 /* then, finalize tesh (release all the allocated memory and exits) */
434 /* init -- initialize tesh : allocated all the objects needed by tesh to run
435 * the tesh files specified in the command line.
437 * return If successful the function returns zero. Otherwise the function returns
438 * -1 and sets the global variable errno to the appropriate error code.
447 char* suffix = strdup(".tesh");
450 /* Windows specific : don't display the general-protection-fault message box and
451 * the the critical-error-handler message box (instead the system send the error
452 * to the calling process : tesh)
454 prev_error_mode = SetErrorMode(SEM_FAILCRITICALERRORS | SEM_NOGPFAULTERRORBOX);
456 /* handle the interrupt signal */
457 signal(SIGINT, sig_int_handler);
459 /* Ignore pipe issues.
460 * They will show up when we try to send data to dead buddies,
461 * but we will stop doing so when we're done with provided input
464 struct sigaction act;
465 memset(&act,0, sizeof(struct sigaction));
466 act.sa_handler = SIG_IGN;
467 sigaction(SIGPIPE, &act, NULL);
469 memset(&act,0, sizeof(struct sigaction));
470 act.sa_handler = sig_int_handler;
471 sigaction(SIGINT, &act, NULL);*/
478 /* used to store the files to run */
479 if(!(fstreams = fstreams_new((void_f_pvoid_t)fstream_free)))
481 ERROR1("(system error) %s", strerror(errno));
485 /* register the current directory */
486 if(!(buffer = getcwd(NULL, 0)))
488 ERROR1("(system error) %s", strerror(errno));
492 /* save the root directory */
493 if(!(root_directory = directory_new(buffer)))
495 ERROR1("(system error) %s", strerror(errno));
501 /* the directories to loads */
502 if(!(directories = directories_new()))
504 ERROR1("(system error) %s", strerror(errno));
508 /* the include directories */
509 include_dirs = xbt_dynar_new(sizeof(directory_t), (void_f_pvoid_t)directory_free);
511 /* the excluded files */
512 if(!(excludes = excludes_new()))
514 ERROR1("(system error) %s", strerror(errno));
519 suffixes = xbt_dynar_new(sizeof(char*),free_string);
521 /* register the default suffix ".tesh" */
522 xbt_dynar_push(suffixes, &suffix);
527 /* load -- load the tesh files to run */
532 /* if the directories object is not empty load all the tesh files contained in
533 * the directories specified in the command line (this tesh files must have the
534 * a suffix specified in the suffixes object.
536 if(!directories_is_empty(directories))
537 directories_load(directories, fstreams, suffixes);
539 /* if no tesh file has been specified in the command line try to load the default tesh file
540 * teshfile from the current directory
542 if(fstreams_is_empty(fstreams))
544 struct stat buffer = {0};
546 /* add the default tesh file if it exists in the current directory */
547 if(!stat("teshfile", &buffer) && S_ISREG(buffer.st_mode))
548 fstreams_add(fstreams, fstream_new(getcwd(NULL, 0), "teshfile"));
551 /* excludes the files specified in the command line and stored in the excludes object */
552 if(!excludes_is_empty(excludes) && !fstreams_is_empty(fstreams))
554 /* check the files to excludes before */
555 excludes_check(excludes, fstreams);
557 /* exclude the specified tesh files */
558 fstreams_exclude(fstreams, excludes);
561 /* if the fstreams object is empty use the stdin */
562 if(fstreams_is_empty(fstreams))
563 fstreams_add(fstreams, fstream_new(NULL, "stdin"));
565 /* load the tesh files (open them) */
566 fstreams_load(fstreams);
570 /* finalize -- cleanup all the allocated objects and display the tesh usage if needed */
574 /* delete the fstreams object */
576 fstreams_free((void**)&fstreams);
578 /* delete the excludes object */
580 excludes_free((void**)&excludes);
582 /* delete the directories object */
584 directories_free((void**)&directories);
586 /* delete the root directory object */
588 directory_free((void**)&root_directory);
590 /* delete the include directories object */
592 xbt_dynar_free(&include_dirs);
594 /* delete the list of tesh files suffixes */
596 xbt_dynar_free(&suffixes);
598 /* destroy the semaphore used to synchronize the units */
600 xbt_os_sem_destroy(jobs_sem);
602 /* destroy the semaphore used by the runner used to wait for the end of the units */
604 xbt_os_sem_destroy(units_sem);
606 /* Windows specific (restore the previouse error mode */
608 SetErrorMode(prev_error_mode);
612 INFO0("Tesh interrupted (receive a SIGINT)");
613 else if(!summary_flag && !dry_run_flag && !silent_flag && !just_print_flag && !print_version_flag && !print_usage_flag && is_tesh_root)
616 INFO2("Tesh terminated with exit code %d : %s",exit_code, "success");
618 ERROR2("Tesh terminated with exit code `(%s)' (%d)",error_to_string(exit_code, err_kind), exit_code);
621 /* exit from the xbt framework */
626 /* exit with the last error code */
631 /* init_options -- initialize the options string */
638 /* the function has been already called */
639 if(optstring[0] != '\0')
646 for (i = 0; opt_entries[i].c != '\0'; ++i)
648 /* initialize the long name of the option*/
649 longopts[i].name = (opt_entries[i].long_name == 0 ? "" : opt_entries[i].long_name);
651 /* getopt_long returns the value of val */
652 longopts[i].flag = 0;
654 /* the short option */
655 longopts[i].val = opt_entries[i].c;
657 /* add the short option in the options string */
658 *p++ = opt_entries[i].c;
660 switch (opt_entries[i].type)
662 /* if this option is used to set a flag or if the argument must be ignored
663 * the option has no argument
666 longopts[i].has_arg = no_argument;
669 /* the option has an argument */
675 if(opt_entries[i].optional_value != 0)
679 longopts[i].has_arg = optional_argument;
682 longopts[i].has_arg = required_argument;
689 longopts[i].name = 0;
692 /* process_command_line -- process the command line
694 * param argc the number of the arguments contained by the command line.
695 * param The array of C strings containing all the arguments of the command
698 * return If successful, the function returns 0. Otherwise -1 is returned
699 * and sets the global variable errno to indicate the error.
701 * errors [ENOENT] A file name specified in the command line does not exist
705 process_command_line(int argc, char** argv)
707 register const struct s_optentry* entry;
709 directory_t directory;
712 /* initialize the options string of tesh */
715 /* let the function getopt_long display the errors if any */
718 /* set option index to zero */
721 while (optind < argc)
723 c = getopt_long(argc, argv, optstring, longopts, (int *) 0);
727 /* end of the command line or "--". */
732 /* no option specified, assume it's a tesh file to run */
736 /* getpath returns -1 when the file to get the path doesn't exist */
737 if(getpath(optarg, &path) < 0)
743 ERROR1("File %s does not exist", optarg);
745 ERROR0("Insufficient memory is available to parse the command line : system error");
750 /* get to the last / (if any) to get the short name of the file */
751 delimiter = strrchr(optarg,'/');
755 delimiter = strrchr(optarg,'\\');
758 /* create a new file stream which represents the tesh file to run */
759 fstream = fstream_new(path, delimiter ? delimiter + 1 : optarg);
763 /* if the list of all tesh files to run already contains this file
764 * destroy it and display a warning, otherwise add it in the list.
766 if(fstreams_contains(fstreams, fstream))
768 fstream_free(&fstream);
769 WARN1("File %s already specified to be run", optarg);
772 fstreams_add(fstreams, fstream);
777 /* unknown option, let getopt_long() displays the error */
778 ERROR0("Command line processing failed : invalid command line");
779 exit_code = EINVCMDLINE;
785 for(entry = opt_entries; entry->c != '\0'; ++entry)
794 ERROR0("Command line processing failed : internal error");
795 exit_code = EPROCCMDLINE;
802 /* set the flag to one */
803 *(int*) entry->value = 1;
812 /* an option with a optional arg is specified use the entry->optional_value */
813 optarg = (char*)entry->optional_value;
815 else if (*optarg == '\0')
817 /* a non optional argument is not specified */
818 ERROR2("Option %c \"%s\"requires an argument",entry->c,entry->long_name);
824 /* --load-directory option */
825 if(!strcmp(entry->long_name,"load-directory"))
829 if(translatepath(optarg, &path) < 0)
835 ERROR1("%s is not a directory",optarg);
837 ERROR0("Insufficient memory is available to process the command line - system error");
845 directory = directory_new(path);
848 if(directories_contains(directories, directory))
850 directory_free((void**)&directory);
851 WARN1("Directory %s already specified to be load",optarg);
854 directories_add(directories, directory);
859 else if(!strcmp(entry->long_name,"directory"))
863 if(translatepath(optarg, &path) < 0)
869 ERROR1("%s is not a directory",optarg);
871 ERROR0("Insufficient memory is available to process the command line - system error");
877 char* buffer = getcwd(NULL, 0);
880 if(!strcmp(buffer, path))
881 WARN1("Already in the directory %s", optarg);
882 else if(!print_directory_flag)
883 INFO1("Entering directory \"%s\"",path);
892 /* --suffix option */
893 else if(!strcmp(entry->long_name,"suffix"))
895 if(strlen(optarg) > MAX_SUFFIX)
897 ERROR1("Suffix %s too long",optarg);
898 exit_code = ESUFFIXTOOLONG;
909 char* suffix = xbt_new0(char, MAX_SUFFIX + 2);
910 sprintf(suffix,".%s",optarg);
912 xbt_dynar_foreach(suffixes, i, cur)
914 if(!strcmp(suffix, cur))
922 WARN1("Suffix %s already specified to be used", optarg);
924 xbt_dynar_push(suffixes, &suffix);
932 xbt_dynar_foreach(suffixes, i, cur)
934 if(!strcmp(optarg, cur))
942 WARN1("Suffix %s already specified to be used", optarg);
945 char* suffix = strdup(optarg);
946 xbt_dynar_push(suffixes, &suffix);
951 else if(!strcmp(entry->long_name,"file"))
956 if(getpath(optarg, &path) < 0)
962 ERROR1("File %s does not exist", optarg);
964 ERROR0("Insufficient memory is available to process the command line - system error");
969 delimiter = strrchr(optarg,'/');
973 delimiter = strrchr(optarg,'\\');
976 fstream = fstream_new(path, delimiter ? delimiter + 1 : optarg);
980 if(fstreams_contains(fstreams, fstream))
982 fstream_free(&fstream);
983 WARN1("File %s already specified to run", optarg);
986 fstreams_add(fstreams, fstream);
988 /* --include-dir option */
989 else if(!strcmp(entry->long_name,"include-dir"))
994 if(translatepath(optarg, &path) < 0)
1000 ERROR1("%s is not a directory",optarg);
1002 ERROR0("Insufficient memory is available to process the command line - system error");
1012 directory = directory_new(path);
1015 xbt_dynar_foreach(include_dirs, i , cur)
1017 if(!strcmp(cur->name, optarg))
1026 directory_free((void**)&directory);
1027 WARN1("Include directory %s already specified to be used",optarg);
1031 xbt_dynar_push(include_dirs, &directory);
1035 /* --exclude option */
1036 else if(!strcmp(entry->long_name,"exclude"))
1041 if(getpath(optarg, &path) < 0)
1047 ERROR1("file %s does not exist", optarg);
1049 ERROR0("Insufficient memory is available to process the command line - system error");
1054 delimiter = strrchr(optarg,'/');
1058 delimiter = strrchr(optarg,'\\');
1061 fstream = fstream_new(path, delimiter ? delimiter + 1 : optarg);
1064 if(excludes_contains(excludes, fstream))
1066 fstream_free(&fstream);
1067 WARN1("File %s already specified to be exclude", optarg);
1070 excludes_add(excludes, fstream);
1073 else if(!strcmp(entry->long_name,"log"))
1075 /* NOTHING TODO : log for xbt */
1079 INFO1("Unexpected option %s", optarg);
1086 /* strictly positve number options */
1089 if ((NULL == optarg) && (argc > optind))
1093 for (cp = argv[optind]; isdigit(cp[0]); ++cp)
1095 optarg = argv[optind++];
1098 /* the number option is specified */
1101 int i = atoi(optarg);
1104 for (cp = optarg; isdigit(cp[0]); ++cp);
1106 if (i < 1 || cp[0] != '\0')
1108 ERROR2("Option %c \"%s\" requires an strictly positive integer as argument",entry->c, entry->long_name);
1109 exit_code = ENOTPOSITIVENUM;
1114 *(int*)entry->value = i;
1116 /* the number option is specified but has no arg, use the optional value*/
1118 *(int*)entry->value = *(int*) entry->optional_value;
1137 stream = exit_code ? stderr : stdout;
1149 fprintf (stream, "Usage: tesh [options] [file] ...\n");
1151 for (cpp = usage; *cpp; ++cpp)
1152 fputs (*cpp, stream);
1154 fprintf(stream, "\nReport bugs to <martin.quinson@loria.fr | malek.cherier@loria.fr>\n");
1170 /* TODO : display the version of tesh */
1171 printf("Version :\n");
1172 printf(" tesh version %s : Mini shell specialized in running test units by Martin Quinson \n", version);
1173 printf(" and Malek Cherier\n");
1174 printf(" Copyright (c) 2007, 2008 Martin Quinson, Malek Cherier\n");
1175 printf(" All rights reserved\n");
1176 printf(" This program is free software; you can redistribute it and/or modify it\n");
1177 printf(" under the terms of the license (GNU LGPL) which comes with this package.\n\n");
1179 if(!print_usage_flag)
1180 printf("Report bugs to <martin.quinson@loria.fr | malek.cherier@loria.fr>");
1189 FILE* stream = fopen("examples/README.tesh", "r");
1193 ERROR0("Unable to locate the README.txt file");
1194 exit_code = EREADMENOTFOUND;
1199 while(getline(&line, &len, stream) != -1)