- register const struct s_optentry* entry;
- register int c;
- directory_t directory;
- fstream_t fstream;
-
- /* initialize the options string of tesh */
- init_options();
-
- /* let the function getopt_long display the errors if any */
- opterr = 1;
-
- /* set option index to zero */
- optind = 0;
-
- while (optind < argc)
- {
- c = getopt_long(argc, argv, optstring, longopts, (int *) 0);
-
- if(c == EOF)
- {
- /* end of the command line or "--". */
- break;
- }
- else if (c == 1)
- {
- /* no option specified, assume it's a tesh file to run */
- char* path;
- char* delimiter;
-
- /* getpath returns -1 when the file to get the path doesn't exist */
- if(getpath(optarg, &path) < 0)
- {
- exit_code = errno;
-
- if(ENOENT == errno)
- ERROR1("File %s does not exist", optarg);
- else
- ERROR0("Insufficient memory is available to parse the command line : system error");
-
- return -1;
- }
-
- /* get to the last / (if any) to get the short name of the file */
- delimiter = strrchr(optarg,'/');
-
- /* create a new file stream which represents the tesh file to run */
- fstream = fstream_new(path, delimiter ? delimiter + 1 : optarg);
-
- free(path);
-
- /* if the list of all tesh files to run already contains this file
- * destroy it and display a warning, otherwise add it in the list.
- */
- if(fstreams_contains(fstreams, fstream))
- {
- fstream_free((void**)&fstream);
- WARN1("File %s already specified to be run", optarg);
- }
- else
- fstreams_add(fstreams, fstream);
-
-
-
-
- }
- else if (c == '?')
- {
- /* unknown option, let getopt_long() displays the error */
- return -1;
- }
- else
- {
- for(entry = opt_entries; entry->c != '\0'; ++entry)
-
- if(c == entry->c)
- {
-
- switch (entry->type)
- {
- /* impossible */
- default:
- ERROR0("Command line processing failed : internal error");
- exit_code = EPROCCMDLINE;
- return -1;
-
-
- /* flag options */
- case flag:
- /* set the flag to one */
- *(int*) entry->value = 1;
-
- break;
-
- /* string options */
- case string:
-
- if(!optarg)
- {
- /* an option with a optional arg is specified use the entry->optional_value */
- optarg = (char*)entry->optional_value;
- }
- else if (*optarg == '\0')
- {
- /* a non optional argument is not specified */
- ERROR2("Option %c \"%s\"requires an argument",entry->c,entry->long_name);
- exit_code = ENOARG;
- return -1;
- }
-
- /* --load-directory option */
- if(!strcmp(entry->long_name,"load-directory"))
- {
- char* path;
-
- if(translatepath(optarg, &path) < 0)
- {
- exit_code = errno;
-
- if(ENOTDIR == errno)
- ERROR1("%s is not a directory",optarg);
- else
- ERROR0("Insufficient memory is available to process the command line - system error");
-
- return -1;
-
- }
- else
- {
- directory = directory_new(path);
- free(path);
-
- if(directories_contains(directories, directory))
- {
- directory_free((void**)&directory);
- WARN1("Directory %s already specified to be load",optarg);
- }
- else
- directories_add(directories, directory);
-
-
- }
- }
- else if(!strcmp(entry->long_name,"directory"))
- {
- char* path ;
-
- if(translatepath(optarg, &path) < 0)
- {
- exit_code = errno;
-
- if(ENOTDIR == errno)
- ERROR1("%s is not a directory",optarg);
- else
- ERROR0("Insufficient memory is available to process the command line - system error");
-
- return -1;
- }
- else
- {
- if(!dont_want_display_directory)
- INFO1("Entering directory \"%s\"",path);
-
- chdir(path);
- free(path);
-
-
- }
- }
-
- /* --suffix option */
- else if(!strcmp(entry->long_name,"suffix"))
- {
- if(strlen(optarg) > MAX_SUFFIX)
- {
- ERROR1("Suffix %s too long",optarg);
- exit_code = ESUFFIXTOOLONG;
- return -1;
- }
-
- if(optarg[0] == '.')
- {
- char suffix[MAX_SUFFIX + 2] = {0};
- sprintf(suffix,".%s",optarg);
-
- if(lstrings_contains(suffixes, suffix))
- WARN1("Suffix %s already specified to be used", optarg);
- else
- lstrings_push_back(suffixes, suffix);
- }
- else
- {
- if(lstrings_contains(suffixes, optarg))
- WARN1("Suffix %s already specified to be used", optarg);
- else
- lstrings_push_back(suffixes, optarg);
- }
- }
- /* --file option */
- else if(!strcmp(entry->long_name,"file"))
- {
- char* path;
- char* delimiter;
-
- if(getpath(optarg, &path) < 0)
- {
- exit_code = errno;
-
- if(ENOENT == errno)
- ERROR1("File %s does not exist", optarg);
- else
- ERROR0("Insufficient memory is available to process the command line - system error");
-
- return -1;
- }
-
- delimiter = strrchr(optarg,'/');
-
- fstream = fstream_new(path, delimiter ? delimiter + 1 : optarg);
-
- free(path);
-
- if(fstreams_contains(fstreams, fstream))
- {
- fstream_free((void**)&fstream);
- WARN1("File %s already specified to run", optarg);
- }
- else
- fstreams_add(fstreams, fstream);
- }
- /* --include-dir option */
- else if(!strcmp(entry->long_name,"include-dir"))
- {
-
- char* path ;
-
- if(translatepath(optarg, &path) < 0)
- {
- exit_code = errno;
-
- if(ENOTDIR == errno)
- ERROR1("%s is not a directory",optarg);
- else
- ERROR0("Insufficient memory is available to process the command line - system error");
-
- return -1;
- }
- else
- {
-
- directory = directory_new(path);
- free(path);
-
- if(vector_contains(include_dirs, directory))
- {
- directory_free((void**)&directory);
- WARN1("Include directory %s already specified to be used",optarg);
-
- }
- else
- vector_push_back(include_dirs, directory);
- }
- }
- /* --exclude option */
- else if(!strcmp(entry->long_name,"exclude"))
- {
-
- char* path;
- char* delimiter;
-
- if(getpath(optarg, &path) < 0)
- {
- exit_code = errno;
-
- if(ENOENT == errno)
- ERROR1("file %s does not exist", optarg);
- else
- ERROR0("Insufficient memory is available to process the command line - system error");
-
- return -1;
- }
-
- delimiter = strrchr(optarg,'/');
-
- fstream = fstream_new(path, delimiter ? delimiter + 1 : optarg);
- free(path);
-
- if(excludes_contains(excludes, fstream))
- {
- fstream_free((void**)&fstream);
- WARN1("File %s already specified to be exclude", optarg);
- }
- else
- excludes_add(excludes, fstream);
-
- }
- /* --log option */
- else if(!strcmp(entry->long_name,"log"))
- {
- lstrings_push_back(logs, optarg);
- }
- else
- {
- INFO1("Unexpected option %s", optarg);
- return -1;
- }
-
-
- break;
-
- /* strictly positve number options */
- case number:
-
- if ((NULL == optarg) && (argc > optind))
- {
- const char* cp;
-
- for (cp = argv[optind]; isdigit(cp[0]); ++cp)
- if(cp[0] == '\0')
- optarg = argv[optind++];
- }
-
- /* the number option is specified */
- if(NULL != optarg)
- {
- int i = atoi(optarg);
- const char *cp;
-
- for (cp = optarg; isdigit(cp[0]); ++cp);
-
- if (i < 1 || cp[0] != '\0')
- {
- ERROR2("Option %c \"%s\" requires an strictly positive integer as argument",entry->c, entry->long_name);
- exit_code = ENOTPOSITIVENUM;
- return -1;
- }
- else
- *(int*)entry->value = i;
- }
- /* the number option is specified but has no arg, use the optional value*/
- else
- *(int*)entry->value = *(int*) entry->optional_value;
-
- break;
-
- }
- break;
- }
- }
- }
-
- return 0;
+ register const struct s_optentry *entry;
+ register int c;
+ directory_t directory;
+ fstream_t fstream;
+
+ /* initialize the options string of tesh */
+ init_options();
+
+ /* let the function getopt_long display the errors if any */
+ opterr = 1;
+
+ /* set option index to zero */
+ optind = 0;
+
+ while (optind < argc) {
+ c = getopt_long(argc, argv, optstring, longopts, (int *) 0);
+
+ if (c == EOF) {
+ /* end of the command line or "--". */
+ break;
+ } else if (c == 1) {
+ /* no option specified, assume it's a tesh file to run */
+ char *path;
+ char *delimiter;
+
+ /* getpath returns -1 when the file to get the path doesn't exist */
+ if (getpath(optarg, &path) < 0) {
+ exit_code = errno;
+ err_kind = 0;
+
+ if (ENOENT == errno)
+ ERROR1("File %s does not exist", optarg);
+ else
+ ERROR0
+ ("Insufficient memory is available to parse the command line : system error");
+
+ return -1;
+ }
+
+ /* get to the last / (if any) to get the short name of the file */
+ delimiter = strrchr(optarg, '/');
+
+#ifdef _XBT_WIN32
+ if (!delimiter)
+ delimiter = strrchr(optarg, '\\');
+#endif
+
+ /* create a new file stream which represents the tesh file to run */
+ fstream = fstream_new(path, delimiter ? delimiter + 1 : optarg);
+
+ free(path);
+
+ /* if the list of all tesh files to run already contains this file
+ * destroy it and display a warning, otherwise add it in the list.
+ */
+ if (fstreams_contains(fstreams, fstream)) {
+ fstream_free(&fstream);
+ WARN1("File %s already specified to be run", optarg);
+ } else
+ fstreams_add(fstreams, fstream);
+
+ } else if (c == '?') {
+ /* unknown option, let getopt_long() displays the error */
+ ERROR0("Command line processing failed : invalid command line");
+ exit_code = EINVCMDLINE;
+ err_kind = 1;
+ return -1;
+ } else {
+ for (entry = opt_entries; entry->c != '\0'; ++entry)
+
+ if (c == entry->c) {
+
+ switch (entry->type) {
+ /* impossible */
+ default:
+ ERROR0("Command line processing failed : internal error");
+ exit_code = EPROCCMDLINE;
+ err_kind = 1;
+ return -1;
+
+
+ /* flag options */
+ case flag:
+ /* set the flag to one */
+ *(int *) entry->value = 1;
+
+ break;
+
+ /* string options */
+ case string:
+
+ if (!optarg) {
+ /* an option with a optional arg is specified use the entry->optional_value */
+ optarg = (char *) entry->optional_value;
+ } else if (*optarg == '\0') {
+ /* a non optional argument is not specified */
+ ERROR2("Option %c \"%s\"requires an argument", entry->c,
+ entry->long_name);
+ exit_code = ENOARG;
+ err_kind = 1;
+ return -1;
+ }
+
+ /* --load-directory option */
+ if (!strcmp(entry->long_name, "load-directory")) {
+ char *path;
+
+ if (translatepath(optarg, &path) < 0) {
+ exit_code = errno;
+ err_kind = 0;
+
+ if (ENOTDIR == errno)
+ ERROR1("%s is not a directory", optarg);
+ else
+ ERROR0
+ ("Insufficient memory is available to process the command line - system error");
+
+ return -1;
+
+ } else {
+
+ directory = directory_new(path);
+ free(path);
+
+ if (directories_contains(directories, directory)) {
+ directory_free((void **) &directory);
+ WARN1("Directory %s already specified to be load",
+ optarg);
+ } else
+ directories_add(directories, directory);
+
+
+ }
+ } else if (!strcmp(entry->long_name, "directory")) {
+ char *path;
+
+ if (translatepath(optarg, &path) < 0) {
+ exit_code = errno;
+ err_kind = 0;
+
+ if (ENOTDIR == errno)
+ ERROR1("%s is not a directory", optarg);
+ else
+ ERROR0
+ ("Insufficient memory is available to process the command line - system error");
+
+ return -1;
+ } else {
+ char *buffer = getcwd(NULL, 0);
+
+
+ if (!strcmp(buffer, path))
+ WARN1("Already in the directory %s", optarg);
+ else if (!print_directory_flag)
+ INFO1("Entering directory \"%s\"", path);
+
+ chdir(path);
+ free(path);
+
+ free(buffer);
+ }
+ }
+
+ /* --suffix option */
+ else if (!strcmp(entry->long_name, "suffix")) {
+ if (strlen(optarg) > MAX_SUFFIX) {
+ ERROR1("Suffix %s too long", optarg);
+ exit_code = ESUFFIXTOOLONG;
+ err_kind = 1;
+ return -1;
+ }
+
+ if (optarg[0] == '.') {
+ char *cur;
+ unsigned int i;
+ int exists = 0;
+
+ char *suffix = xbt_new0(char, MAX_SUFFIX + 2);
+ sprintf(suffix, ".%s", optarg);
+
+ xbt_dynar_foreach(suffixes, i, cur) {
+ if (!strcmp(suffix, cur)) {
+ exists = 1;
+ break;
+ }
+ }
+
+ if (exists)
+ WARN1("Suffix %s already specified to be used", optarg);
+ else
+ xbt_dynar_push(suffixes, &suffix);
+ } else {
+ char *cur;
+ unsigned int i;
+ int exists = 0;
+
+ xbt_dynar_foreach(suffixes, i, cur) {
+ if (!strcmp(optarg, cur)) {
+ exists = 1;
+ break;
+ }
+ }
+
+ if (exists)
+ WARN1("Suffix %s already specified to be used", optarg);
+ else {
+ char *suffix = strdup(optarg);
+ xbt_dynar_push(suffixes, &suffix);
+ }
+ }
+ }
+ /* --file option */
+ else if (!strcmp(entry->long_name, "file")) {
+ char *path;
+ char *delimiter;
+
+ if (getpath(optarg, &path) < 0) {
+ exit_code = errno;
+ err_kind = 0;
+
+ if (ENOENT == errno)
+ ERROR1("File %s does not exist", optarg);
+ else
+ ERROR1("System error :`(%s)'", strerror(errno));
+
+ return -1;
+ }
+
+ delimiter = strrchr(optarg, '/');
+
+#ifdef _XBT_WIN32
+ if (!delimiter)
+ delimiter = strrchr(optarg, '\\');
+#endif
+
+ fstream =
+ fstream_new(path, delimiter ? delimiter + 1 : optarg);
+
+ free(path);
+
+ if (fstreams_contains(fstreams, fstream)) {
+ fstream_free(&fstream);
+ WARN1("File %s already specified to run", optarg);
+ } else
+ fstreams_add(fstreams, fstream);
+ }
+ /* --include-dir option */
+ else if (!strcmp(entry->long_name, "include-dir")) {
+
+ char *path;
+
+ if (translatepath(optarg, &path) < 0) {
+ exit_code = errno;
+ err_kind = 0;
+
+ if (ENOTDIR == errno)
+ ERROR1("%s is not a directory", optarg);
+ else
+ ERROR0
+ ("Insufficient memory is available to process the command line - system error");
+
+ return -1;
+ }
+
+ else {
+ int exists = 0;
+ unsigned int i;
+ directory_t cur;
+ directory = directory_new(path);
+ free(path);
+
+ xbt_dynar_foreach(include_dirs, i, cur) {
+ if (!strcmp(cur->name, optarg)) {
+ exists = 1;
+ break;
+ }
+ }
+
+ if (exists) {
+ directory_free((void **) &directory);
+ WARN1
+ ("Include directory %s already specified to be used",
+ optarg);
+
+ } else
+ xbt_dynar_push(include_dirs, &directory);
+
+ }
+ }
+ /* --exclude option */
+ else if (!strcmp(entry->long_name, "exclude")) {
+ char *path;
+ char *delimiter;
+
+ if (getpath(optarg, &path) < 0) {
+ exit_code = errno;
+ err_kind = 0;
+
+ if (ENOENT == errno)
+ ERROR1("file %s does not exist", optarg);
+ else
+ ERROR0
+ ("Insufficient memory is available to process the command line - system error");
+
+ return -1;
+ }
+
+ delimiter = strrchr(optarg, '/');
+
+#ifdef _XBT_WIN32
+ if (!delimiter)
+ delimiter = strrchr(optarg, '\\');
+#endif
+
+ fstream =
+ fstream_new(path, delimiter ? delimiter + 1 : optarg);
+ free(path);
+
+ if (excludes_contains(excludes, fstream)) {
+ fstream_free(&fstream);
+ WARN1("File %s already specified to be exclude", optarg);
+ } else
+ excludes_add(excludes, fstream);
+
+ } else if (!strcmp(entry->long_name, "log")) {
+ /* NOTHING TODO : log for xbt */
+ } else {
+ INFO1("Unexpected option %s", optarg);
+ return -1;
+ }
+
+
+ break;
+
+ /* strictly positve number options */
+ case number:
+
+ if ((NULL == optarg) && (argc > optind)) {
+ const char *cp;
+
+ for (cp = argv[optind]; isdigit(cp[0]); ++cp)
+ if (cp[0] == '\0')
+ optarg = argv[optind++];
+ }
+
+ /* the number option is specified */
+ if (NULL != optarg) {
+ int i = atoi(optarg);
+ const char *cp;
+
+ for (cp = optarg; isdigit(cp[0]); ++cp);
+
+ if (i < 1 || cp[0] != '\0') {
+ ERROR2
+ ("Option %c \"%s\" requires an strictly positive integer as argument",
+ entry->c, entry->long_name);
+ exit_code = ENOTPOSITIVENUM;
+ err_kind = 1;
+ return -1;
+ } else
+ *(int *) entry->value = i;
+ }
+ /* the number option is specified but has no arg, use the optional value */
+ else
+ *(int *) entry->value = *(int *) entry->optional_value;
+
+ break;
+
+ }
+ break;
+ }
+ }
+ }
+
+ return 0;