2 File autogenerated by gengetopt version 2.22
3 generated with the following command:
4 gengetopt --file-name=/home/guigues/coding/CreaTools/creaImageIO/appli/gimmick/gimmick_ggo -u --conf-parser
6 The developers of gengetopt consider the fixed text that goes in all
7 gengetopt output files to be in the public domain:
8 we make no copyright claims on it.
11 /* If we use autoconf. */
22 #include "/home/guigues/coding/CreaTools/creaImageIO/appli/gimmick/gimmick_ggo.h"
24 const char *gengetopt_args_info_purpose = "";
26 const char *gengetopt_args_info_usage = "Usage: gimmick COMMAND [OPTIONS]";
28 const char *gengetopt_args_info_description = "";
30 const char *gengetopt_args_info_help[] = {
31 " -h, --help Print help and exit",
32 " -V, --version Print version and exit",
34 " -p, --print Prints the local database tree (default=off)",
35 " -f, --file=<file name> Adds the file to local database",
36 " -d, --dir=<path> Adds the directory to local database",
37 " -r, --recurse Recurse into sub-directories (default=off)",
39 " -v, --verbose=INT Verbosity level (default=`1')",
40 " -D, --debug=INT Debug messages level (default=`0')",
48 } cmdline_parser_arg_type;
51 void clear_given (struct gengetopt_args_info *args_info);
53 void clear_args (struct gengetopt_args_info *args_info);
56 cmdline_parser_internal (int argc, char * const *argv, struct gengetopt_args_info *args_info,
57 struct cmdline_parser_params *params, const char *additional_error);
60 cmdline_parser_required2 (struct gengetopt_args_info *args_info, const char *prog_name, const char *additional_error);
64 struct line_list * next;
67 static struct line_list *cmd_line_list = 0;
68 static struct line_list *cmd_line_list_tmp = 0;
73 /* free the list of a previous call */
76 while (cmd_line_list) {
77 cmd_line_list_tmp = cmd_line_list;
78 cmd_line_list = cmd_line_list->next;
79 free (cmd_line_list_tmp->string_arg);
80 free (cmd_line_list_tmp);
87 gengetopt_strdup (const char *s);
90 void clear_given (struct gengetopt_args_info *args_info)
92 args_info->help_given = 0 ;
93 args_info->version_given = 0 ;
94 args_info->print_given = 0 ;
95 args_info->file_given = 0 ;
96 args_info->dir_given = 0 ;
97 args_info->recurse_given = 0 ;
98 args_info->verbose_given = 0 ;
99 args_info->debug_given = 0 ;
103 void clear_args (struct gengetopt_args_info *args_info)
105 args_info->print_flag = 0;
106 args_info->file_arg = NULL;
107 args_info->file_orig = NULL;
108 args_info->dir_arg = NULL;
109 args_info->dir_orig = NULL;
110 args_info->recurse_flag = 0;
111 args_info->verbose_arg = 1;
112 args_info->verbose_orig = NULL;
113 args_info->debug_arg = 0;
114 args_info->debug_orig = NULL;
119 void init_args_info(struct gengetopt_args_info *args_info)
123 args_info->help_help = gengetopt_args_info_help[0] ;
124 args_info->version_help = gengetopt_args_info_help[1] ;
125 args_info->print_help = gengetopt_args_info_help[3] ;
126 args_info->file_help = gengetopt_args_info_help[4] ;
127 args_info->dir_help = gengetopt_args_info_help[5] ;
128 args_info->recurse_help = gengetopt_args_info_help[6] ;
129 args_info->verbose_help = gengetopt_args_info_help[8] ;
130 args_info->debug_help = gengetopt_args_info_help[9] ;
135 cmdline_parser_print_version (void)
137 printf ("%s %s\n", CMDLINE_PARSER_PACKAGE, CMDLINE_PARSER_VERSION);
140 static void print_help_common(void) {
141 cmdline_parser_print_version ();
143 if (strlen(gengetopt_args_info_purpose) > 0)
144 printf("\n%s\n", gengetopt_args_info_purpose);
146 if (strlen(gengetopt_args_info_usage) > 0)
147 printf("\n%s\n", gengetopt_args_info_usage);
151 if (strlen(gengetopt_args_info_description) > 0)
152 printf("%s\n", gengetopt_args_info_description);
156 cmdline_parser_print_help (void)
160 while (gengetopt_args_info_help[i])
161 printf("%s\n", gengetopt_args_info_help[i++]);
165 cmdline_parser_init (struct gengetopt_args_info *args_info)
167 clear_given (args_info);
168 clear_args (args_info);
169 init_args_info (args_info);
171 args_info->inputs = NULL;
172 args_info->inputs_num = 0;
176 cmdline_parser_params_init(struct cmdline_parser_params *params)
180 params->override = 0;
181 params->initialize = 1;
182 params->check_required = 1;
183 params->check_ambiguity = 0;
184 params->print_errors = 1;
188 struct cmdline_parser_params *
189 cmdline_parser_params_create(void)
191 struct cmdline_parser_params *params =
192 (struct cmdline_parser_params *)malloc(sizeof(struct cmdline_parser_params));
193 cmdline_parser_params_init(params);
198 free_string_field (char **s)
209 cmdline_parser_release (struct gengetopt_args_info *args_info)
212 free_string_field (&(args_info->file_arg));
213 free_string_field (&(args_info->file_orig));
214 free_string_field (&(args_info->dir_arg));
215 free_string_field (&(args_info->dir_orig));
216 free_string_field (&(args_info->verbose_orig));
217 free_string_field (&(args_info->debug_orig));
220 for (i = 0; i < args_info->inputs_num; ++i)
221 free (args_info->inputs [i]);
223 if (args_info->inputs_num)
224 free (args_info->inputs);
226 clear_given (args_info);
231 write_into_file(FILE *outfile, const char *opt, const char *arg, char *values[])
234 fprintf(outfile, "%s=\"%s\"\n", opt, arg);
236 fprintf(outfile, "%s\n", opt);
242 cmdline_parser_dump(FILE *outfile, struct gengetopt_args_info *args_info)
248 fprintf (stderr, "%s: cannot dump options to stream\n", CMDLINE_PARSER_PACKAGE);
252 if (args_info->help_given)
253 write_into_file(outfile, "help", 0, 0 );
254 if (args_info->version_given)
255 write_into_file(outfile, "version", 0, 0 );
256 if (args_info->print_given)
257 write_into_file(outfile, "print", 0, 0 );
258 if (args_info->file_given)
259 write_into_file(outfile, "file", args_info->file_orig, 0);
260 if (args_info->dir_given)
261 write_into_file(outfile, "dir", args_info->dir_orig, 0);
262 if (args_info->recurse_given)
263 write_into_file(outfile, "recurse", 0, 0 );
264 if (args_info->verbose_given)
265 write_into_file(outfile, "verbose", args_info->verbose_orig, 0);
266 if (args_info->debug_given)
267 write_into_file(outfile, "debug", args_info->debug_orig, 0);
275 cmdline_parser_file_save(const char *filename, struct gengetopt_args_info *args_info)
280 outfile = fopen(filename, "w");
284 fprintf (stderr, "%s: cannot open file for writing: %s\n", CMDLINE_PARSER_PACKAGE, filename);
288 i = cmdline_parser_dump(outfile, args_info);
295 cmdline_parser_free (struct gengetopt_args_info *args_info)
297 cmdline_parser_release (args_info);
300 /** @brief replacement of strdup, which is not standard */
302 gengetopt_strdup (const char *s)
308 result = (char*)malloc(strlen(s) + 1);
309 if (result == (char*)0)
316 cmdline_parser (int argc, char * const *argv, struct gengetopt_args_info *args_info)
318 return cmdline_parser2 (argc, argv, args_info, 0, 1, 1);
322 cmdline_parser_ext (int argc, char * const *argv, struct gengetopt_args_info *args_info,
323 struct cmdline_parser_params *params)
326 result = cmdline_parser_internal (argc, argv, args_info, params, NULL);
328 if (result == EXIT_FAILURE)
330 cmdline_parser_free (args_info);
338 cmdline_parser2 (int argc, char * const *argv, struct gengetopt_args_info *args_info, int override, int initialize, int check_required)
341 struct cmdline_parser_params params;
343 params.override = override;
344 params.initialize = initialize;
345 params.check_required = check_required;
346 params.check_ambiguity = 0;
347 params.print_errors = 1;
349 result = cmdline_parser_internal (argc, argv, args_info, ¶ms, NULL);
351 if (result == EXIT_FAILURE)
353 cmdline_parser_free (args_info);
361 cmdline_parser_required (struct gengetopt_args_info *args_info, const char *prog_name)
363 int result = EXIT_SUCCESS;
365 if (cmdline_parser_required2(args_info, prog_name, NULL) > 0)
366 result = EXIT_FAILURE;
368 if (result == EXIT_FAILURE)
370 cmdline_parser_free (args_info);
378 cmdline_parser_required2 (struct gengetopt_args_info *args_info, const char *prog_name, const char *additional_error)
382 /* checks for required options */
384 /* checks for dependences among options */
385 if (args_info->recurse_given && ! args_info->dir_given)
387 fprintf (stderr, "%s: '--recurse' ('-r') option depends on option 'dir'%s\n", prog_name, (additional_error ? additional_error : ""));
395 static char *package_name = 0;
398 * @brief updates an option
399 * @param field the generic pointer to the field to update
400 * @param orig_field the pointer to the orig field
401 * @param field_given the pointer to the number of occurrence of this option
402 * @param prev_given the pointer to the number of occurrence already seen
403 * @param value the argument for this option (if null no arg was specified)
404 * @param possible_values the possible values for this option (if specified)
405 * @param default_value the default value (in case the option only accepts fixed values)
406 * @param arg_type the type of this option
407 * @param check_ambiguity @see cmdline_parser_params.check_ambiguity
408 * @param override @see cmdline_parser_params.override
409 * @param no_free whether to free a possible previous value
410 * @param multiple_option whether this is a multiple option
411 * @param long_opt the corresponding long option
412 * @param short_opt the corresponding short option (or '-' if none)
413 * @param additional_error possible further error specification
416 int update_arg(void *field, char **orig_field,
417 unsigned int *field_given, unsigned int *prev_given,
418 char *value, char *possible_values[], const char *default_value,
419 cmdline_parser_arg_type arg_type,
420 int check_ambiguity, int override,
421 int no_free, int multiple_option,
422 const char *long_opt, char short_opt,
423 const char *additional_error)
426 const char *val = value;
433 if (!multiple_option && prev_given && (*prev_given || (check_ambiguity && *field_given)))
435 if (short_opt != '-')
436 fprintf (stderr, "%s: `--%s' (`-%c') option given more than once%s\n",
437 package_name, long_opt, short_opt,
438 (additional_error ? additional_error : ""));
440 fprintf (stderr, "%s: `--%s' option given more than once%s\n",
441 package_name, long_opt,
442 (additional_error ? additional_error : ""));
443 return 1; /* failure */
447 if (field_given && *field_given && ! override)
454 val = possible_values[found];
458 *((int *)field) = !*((int *)field);
461 if (val) *((int *)field) = strtol (val, &stop_char, 0);
465 string_field = (char **)field;
466 if (!no_free && *string_field)
467 free (*string_field); /* free previous string */
468 *string_field = gengetopt_strdup (val);
475 /* check numeric conversion */
478 if (val && !(stop_char && *stop_char == '\0')) {
479 fprintf(stderr, "%s: invalid numeric value: %s\n", package_name, val);
480 return 1; /* failure */
487 /* store the original value */
493 if (value && orig_field) {
498 free (*orig_field); /* free previous string */
499 *orig_field = gengetopt_strdup (value);
509 cmdline_parser_internal (int argc, char * const *argv, struct gengetopt_args_info *args_info,
510 struct cmdline_parser_params *params, const char *additional_error)
512 int c; /* Character of the parsed option. */
515 struct gengetopt_args_info local_args_info;
522 package_name = argv[0];
524 override = params->override;
525 initialize = params->initialize;
526 check_required = params->check_required;
527 check_ambiguity = params->check_ambiguity;
530 cmdline_parser_init (args_info);
532 cmdline_parser_init (&local_args_info);
536 opterr = params->print_errors;
541 int option_index = 0;
543 static struct option long_options[] = {
544 { "help", 0, NULL, 'h' },
545 { "version", 0, NULL, 'V' },
546 { "print", 0, NULL, 'p' },
547 { "file", 1, NULL, 'f' },
548 { "dir", 1, NULL, 'd' },
549 { "recurse", 0, NULL, 'r' },
550 { "verbose", 1, NULL, 'v' },
551 { "debug", 1, NULL, 'D' },
555 c = getopt_long (argc, argv, "hVpf:d:rv:D:", long_options, &option_index);
557 if (c == -1) break; /* Exit from `while (1)' loop. */
561 case 'h': /* Print help and exit. */
562 cmdline_parser_print_help ();
563 cmdline_parser_free (&local_args_info);
566 case 'V': /* Print version and exit. */
567 cmdline_parser_print_version ();
568 cmdline_parser_free (&local_args_info);
571 case 'p': /* Prints the local database tree. */
574 if (update_arg((void *)&(args_info->print_flag), 0, &(args_info->print_given),
575 &(local_args_info.print_given), optarg, 0, 0, ARG_FLAG,
576 check_ambiguity, override, 1, 0, "print", 'p',
581 case 'f': /* Adds the file to local database. */
584 if (update_arg( (void *)&(args_info->file_arg),
585 &(args_info->file_orig), &(args_info->file_given),
586 &(local_args_info.file_given), optarg, 0, 0, ARG_STRING,
587 check_ambiguity, override, 0, 0,
593 case 'd': /* Adds the directory to local database. */
596 if (update_arg( (void *)&(args_info->dir_arg),
597 &(args_info->dir_orig), &(args_info->dir_given),
598 &(local_args_info.dir_given), optarg, 0, 0, ARG_STRING,
599 check_ambiguity, override, 0, 0,
605 case 'r': /* Recurse into sub-directories. */
608 if (update_arg((void *)&(args_info->recurse_flag), 0, &(args_info->recurse_given),
609 &(local_args_info.recurse_given), optarg, 0, 0, ARG_FLAG,
610 check_ambiguity, override, 1, 0, "recurse", 'r',
615 case 'v': /* Verbosity level. */
618 if (update_arg( (void *)&(args_info->verbose_arg),
619 &(args_info->verbose_orig), &(args_info->verbose_given),
620 &(local_args_info.verbose_given), optarg, 0, "1", ARG_INT,
621 check_ambiguity, override, 0, 0,
627 case 'D': /* Debug messages level. */
630 if (update_arg( (void *)&(args_info->debug_arg),
631 &(args_info->debug_orig), &(args_info->debug_given),
632 &(local_args_info.debug_given), optarg, 0, "0", ARG_INT,
633 check_ambiguity, override, 0, 0,
640 case 0: /* Long option with no short option */
641 case '?': /* Invalid option. */
642 /* `getopt_long' already printed an error message. */
645 default: /* bug: option not considered. */
646 fprintf (stderr, "%s: option unknown: %c%s\n", CMDLINE_PARSER_PACKAGE, c, (additional_error ? additional_error : ""));
655 error += cmdline_parser_required2 (args_info, argv[0], additional_error);
658 cmdline_parser_release (&local_args_info);
661 return (EXIT_FAILURE);
666 int found_prog_name = 0;
667 /* whether program name, i.e., argv[0], is in the remaining args
668 (this may happen with some implementations of getopt,
669 but surely not with the one included by gengetopt) */
673 if (argv[i++] == argv[0]) {
679 args_info->inputs_num = argc - optind - found_prog_name;
681 (char **)(malloc ((args_info->inputs_num)*sizeof(char *))) ;
682 while (optind < argc)
683 if (argv[optind++] != argv[0])
684 args_info->inputs[ i++ ] = gengetopt_strdup (argv[optind-1]) ;
691 cmdline_parser_release (&local_args_info);
692 return (EXIT_FAILURE);
695 #ifndef CONFIG_FILE_LINE_SIZE
696 #define CONFIG_FILE_LINE_SIZE 2048
698 #define ADDITIONAL_ERROR " in configuration file "
700 #define CONFIG_FILE_LINE_BUFFER_SIZE (CONFIG_FILE_LINE_SIZE+3)
701 /* 3 is for "--" and "=" */
704 _cmdline_parser_configfile (char * const filename, int *my_argc)
707 char my_argv[CONFIG_FILE_LINE_BUFFER_SIZE+1];
708 char linebuf[CONFIG_FILE_LINE_SIZE];
710 int result = 0, equal;
713 size_t len, next_token;
716 if ((file = fopen(filename, "r")) == NULL)
718 fprintf (stderr, "%s: Error opening configuration file '%s'\n",
719 CMDLINE_PARSER_PACKAGE, filename);
723 while ((fgets(linebuf, CONFIG_FILE_LINE_SIZE, file)) != NULL)
727 len = strlen(linebuf);
728 if (len > (CONFIG_FILE_LINE_BUFFER_SIZE-1))
730 fprintf (stderr, "%s:%s:%d: Line too long in configuration file\n",
731 CMDLINE_PARSER_PACKAGE, filename, line_num);
732 result = EXIT_FAILURE;
736 /* find first non-whitespace character in the line */
737 next_token = strspn (linebuf, " \t\r\n");
738 str_index = linebuf + next_token;
740 if ( str_index[0] == '\0' || str_index[0] == '#')
741 continue; /* empty line or comment line is skipped */
745 /* truncate fopt at the end of the first non-valid character */
746 next_token = strcspn (fopt, " \t\r\n=");
748 if (fopt[next_token] == '\0') /* the line is over */
755 /* remember if equal sign is present */
756 equal = (fopt[next_token] == '=');
757 fopt[next_token++] = '\0';
759 /* advance pointers to the next token after the end of fopt */
760 next_token += strspn (fopt + next_token, " \t\r\n");
762 /* check for the presence of equal sign, and if so, skip it */
764 if ((equal = (fopt[next_token] == '=')))
767 next_token += strspn (fopt + next_token, " \t\r\n");
769 str_index += next_token;
773 if ( farg[0] == '\"' || farg[0] == '\'' )
774 { /* quoted argument */
775 str_index = strchr (++farg, str_index[0] ); /* skip opening quote */
780 "%s:%s:%d: unterminated string in configuration file\n",
781 CMDLINE_PARSER_PACKAGE, filename, line_num);
782 result = EXIT_FAILURE;
787 { /* read up the remaining part up to a delimiter */
788 next_token = strcspn (farg, " \t\r\n#\'\"");
789 str_index += next_token;
792 /* truncate farg at the delimiter and store it for further check */
793 delimiter = *str_index, *str_index++ = '\0';
795 /* everything but comment is illegal at the end of line */
796 if (delimiter != '\0' && delimiter != '#')
798 str_index += strspn(str_index, " \t\r\n");
799 if (*str_index != '\0' && *str_index != '#')
803 "%s:%s:%d: malformed string in configuration file\n",
804 CMDLINE_PARSER_PACKAGE, filename, line_num);
805 result = EXIT_FAILURE;
811 if (!strcmp(fopt,"include")) {
813 result = _cmdline_parser_configfile(farg, my_argc);
815 fprintf(stderr, "%s:%s:%d: include requires a filename argument.\n",
816 CMDLINE_PARSER_PACKAGE, filename, line_num);
821 strcat (my_argv, len > 1 ? "--" : "-");
822 strcat (my_argv, fopt);
823 if (len > 1 && ((farg && *farg) || equal))
824 strcat (my_argv, "=");
826 strcat (my_argv, farg);
829 cmd_line_list_tmp = (struct line_list *) malloc (sizeof (struct line_list));
830 cmd_line_list_tmp->next = cmd_line_list;
831 cmd_line_list = cmd_line_list_tmp;
832 cmd_line_list->string_arg = gengetopt_strdup(my_argv);
841 cmdline_parser_configfile (char * const filename,
842 struct gengetopt_args_info *args_info,
843 int override, int initialize, int check_required)
845 struct cmdline_parser_params params;
847 params.override = override;
848 params.initialize = initialize;
849 params.check_required = check_required;
850 params.check_ambiguity = 0;
851 params.print_errors = 1;
853 return cmdline_parser_config_file (filename, args_info, ¶ms);
857 cmdline_parser_config_file (char * const filename,
858 struct gengetopt_args_info *args_info,
859 struct cmdline_parser_params *params)
864 char *additional_error;
866 /* store the program name */
867 cmd_line_list_tmp = (struct line_list *) malloc (sizeof (struct line_list));
868 cmd_line_list_tmp->next = cmd_line_list;
869 cmd_line_list = cmd_line_list_tmp;
870 cmd_line_list->string_arg = gengetopt_strdup (CMDLINE_PARSER_PACKAGE);
872 result = _cmdline_parser_configfile(filename, &my_argc);
874 if (result != EXIT_FAILURE) {
875 my_argv_arg = (char **) malloc((my_argc+1) * sizeof(char *));
876 cmd_line_list_tmp = cmd_line_list;
878 for (i = my_argc - 1; i >= 0; --i) {
879 my_argv_arg[i] = cmd_line_list_tmp->string_arg;
880 cmd_line_list_tmp = cmd_line_list_tmp->next;
883 my_argv_arg[my_argc] = 0;
885 additional_error = (char *)malloc(strlen(filename) + strlen(ADDITIONAL_ERROR) + 1);
886 strcpy (additional_error, ADDITIONAL_ERROR);
887 strcat (additional_error, filename);
889 cmdline_parser_internal (my_argc, my_argv_arg, args_info,
893 free (additional_error);
898 if (result == EXIT_FAILURE)
900 cmdline_parser_free (args_info);