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')",
47 } cmdline_parser_arg_type;
50 void clear_given (struct gengetopt_args_info *args_info);
52 void clear_args (struct gengetopt_args_info *args_info);
55 cmdline_parser_internal (int argc, char * const *argv, struct gengetopt_args_info *args_info,
56 struct cmdline_parser_params *params, const char *additional_error);
59 cmdline_parser_required2 (struct gengetopt_args_info *args_info, const char *prog_name, const char *additional_error);
63 struct line_list * next;
66 static struct line_list *cmd_line_list = 0;
67 static struct line_list *cmd_line_list_tmp = 0;
72 /* free the list of a previous call */
75 while (cmd_line_list) {
76 cmd_line_list_tmp = cmd_line_list;
77 cmd_line_list = cmd_line_list->next;
78 free (cmd_line_list_tmp->string_arg);
79 free (cmd_line_list_tmp);
86 gengetopt_strdup (const char *s);
89 void clear_given (struct gengetopt_args_info *args_info)
91 args_info->help_given = 0 ;
92 args_info->version_given = 0 ;
93 args_info->print_given = 0 ;
94 args_info->file_given = 0 ;
95 args_info->dir_given = 0 ;
96 args_info->recurse_given = 0 ;
97 args_info->verbose_given = 0 ;
101 void clear_args (struct gengetopt_args_info *args_info)
103 args_info->print_flag = 0;
104 args_info->file_arg = NULL;
105 args_info->file_orig = NULL;
106 args_info->dir_arg = NULL;
107 args_info->dir_orig = NULL;
108 args_info->recurse_flag = 0;
109 args_info->verbose_arg = 1;
110 args_info->verbose_orig = NULL;
115 void init_args_info(struct gengetopt_args_info *args_info)
119 args_info->help_help = gengetopt_args_info_help[0] ;
120 args_info->version_help = gengetopt_args_info_help[1] ;
121 args_info->print_help = gengetopt_args_info_help[3] ;
122 args_info->file_help = gengetopt_args_info_help[4] ;
123 args_info->dir_help = gengetopt_args_info_help[5] ;
124 args_info->recurse_help = gengetopt_args_info_help[6] ;
125 args_info->verbose_help = gengetopt_args_info_help[8] ;
130 cmdline_parser_print_version (void)
132 printf ("%s %s\n", CMDLINE_PARSER_PACKAGE, CMDLINE_PARSER_VERSION);
135 static void print_help_common(void) {
136 cmdline_parser_print_version ();
138 if (strlen(gengetopt_args_info_purpose) > 0)
139 printf("\n%s\n", gengetopt_args_info_purpose);
141 if (strlen(gengetopt_args_info_usage) > 0)
142 printf("\n%s\n", gengetopt_args_info_usage);
146 if (strlen(gengetopt_args_info_description) > 0)
147 printf("%s\n", gengetopt_args_info_description);
151 cmdline_parser_print_help (void)
155 while (gengetopt_args_info_help[i])
156 printf("%s\n", gengetopt_args_info_help[i++]);
160 cmdline_parser_init (struct gengetopt_args_info *args_info)
162 clear_given (args_info);
163 clear_args (args_info);
164 init_args_info (args_info);
166 args_info->inputs = NULL;
167 args_info->inputs_num = 0;
171 cmdline_parser_params_init(struct cmdline_parser_params *params)
175 params->override = 0;
176 params->initialize = 1;
177 params->check_required = 1;
178 params->check_ambiguity = 0;
179 params->print_errors = 1;
183 struct cmdline_parser_params *
184 cmdline_parser_params_create(void)
186 struct cmdline_parser_params *params =
187 (struct cmdline_parser_params *)malloc(sizeof(struct cmdline_parser_params));
188 cmdline_parser_params_init(params);
193 free_string_field (char **s)
204 cmdline_parser_release (struct gengetopt_args_info *args_info)
207 free_string_field (&(args_info->file_arg));
208 free_string_field (&(args_info->file_orig));
209 free_string_field (&(args_info->dir_arg));
210 free_string_field (&(args_info->dir_orig));
211 free_string_field (&(args_info->verbose_orig));
214 for (i = 0; i < args_info->inputs_num; ++i)
215 free (args_info->inputs [i]);
217 if (args_info->inputs_num)
218 free (args_info->inputs);
220 clear_given (args_info);
225 write_into_file(FILE *outfile, const char *opt, const char *arg, char *values[])
228 fprintf(outfile, "%s=\"%s\"\n", opt, arg);
230 fprintf(outfile, "%s\n", opt);
236 cmdline_parser_dump(FILE *outfile, struct gengetopt_args_info *args_info)
242 fprintf (stderr, "%s: cannot dump options to stream\n", CMDLINE_PARSER_PACKAGE);
246 if (args_info->help_given)
247 write_into_file(outfile, "help", 0, 0 );
248 if (args_info->version_given)
249 write_into_file(outfile, "version", 0, 0 );
250 if (args_info->print_given)
251 write_into_file(outfile, "print", 0, 0 );
252 if (args_info->file_given)
253 write_into_file(outfile, "file", args_info->file_orig, 0);
254 if (args_info->dir_given)
255 write_into_file(outfile, "dir", args_info->dir_orig, 0);
256 if (args_info->recurse_given)
257 write_into_file(outfile, "recurse", 0, 0 );
258 if (args_info->verbose_given)
259 write_into_file(outfile, "verbose", args_info->verbose_orig, 0);
267 cmdline_parser_file_save(const char *filename, struct gengetopt_args_info *args_info)
272 outfile = fopen(filename, "w");
276 fprintf (stderr, "%s: cannot open file for writing: %s\n", CMDLINE_PARSER_PACKAGE, filename);
280 i = cmdline_parser_dump(outfile, args_info);
287 cmdline_parser_free (struct gengetopt_args_info *args_info)
289 cmdline_parser_release (args_info);
292 /** @brief replacement of strdup, which is not standard */
294 gengetopt_strdup (const char *s)
300 result = (char*)malloc(strlen(s) + 1);
301 if (result == (char*)0)
308 cmdline_parser (int argc, char * const *argv, struct gengetopt_args_info *args_info)
310 return cmdline_parser2 (argc, argv, args_info, 0, 1, 1);
314 cmdline_parser_ext (int argc, char * const *argv, struct gengetopt_args_info *args_info,
315 struct cmdline_parser_params *params)
318 result = cmdline_parser_internal (argc, argv, args_info, params, NULL);
320 if (result == EXIT_FAILURE)
322 cmdline_parser_free (args_info);
330 cmdline_parser2 (int argc, char * const *argv, struct gengetopt_args_info *args_info, int override, int initialize, int check_required)
333 struct cmdline_parser_params params;
335 params.override = override;
336 params.initialize = initialize;
337 params.check_required = check_required;
338 params.check_ambiguity = 0;
339 params.print_errors = 1;
341 result = cmdline_parser_internal (argc, argv, args_info, ¶ms, NULL);
343 if (result == EXIT_FAILURE)
345 cmdline_parser_free (args_info);
353 cmdline_parser_required (struct gengetopt_args_info *args_info, const char *prog_name)
355 int result = EXIT_SUCCESS;
357 if (cmdline_parser_required2(args_info, prog_name, NULL) > 0)
358 result = EXIT_FAILURE;
360 if (result == EXIT_FAILURE)
362 cmdline_parser_free (args_info);
370 cmdline_parser_required2 (struct gengetopt_args_info *args_info, const char *prog_name, const char *additional_error)
374 /* checks for required options */
376 /* checks for dependences among options */
377 if (args_info->recurse_given && ! args_info->dir_given)
379 fprintf (stderr, "%s: '--recurse' ('-r') option depends on option 'dir'%s\n", prog_name, (additional_error ? additional_error : ""));
387 static char *package_name = 0;
390 * @brief updates an option
391 * @param field the generic pointer to the field to update
392 * @param orig_field the pointer to the orig field
393 * @param field_given the pointer to the number of occurrence of this option
394 * @param prev_given the pointer to the number of occurrence already seen
395 * @param value the argument for this option (if null no arg was specified)
396 * @param possible_values the possible values for this option (if specified)
397 * @param default_value the default value (in case the option only accepts fixed values)
398 * @param arg_type the type of this option
399 * @param check_ambiguity @see cmdline_parser_params.check_ambiguity
400 * @param override @see cmdline_parser_params.override
401 * @param no_free whether to free a possible previous value
402 * @param multiple_option whether this is a multiple option
403 * @param long_opt the corresponding long option
404 * @param short_opt the corresponding short option (or '-' if none)
405 * @param additional_error possible further error specification
408 int update_arg(void *field, char **orig_field,
409 unsigned int *field_given, unsigned int *prev_given,
410 char *value, char *possible_values[], const char *default_value,
411 cmdline_parser_arg_type arg_type,
412 int check_ambiguity, int override,
413 int no_free, int multiple_option,
414 const char *long_opt, char short_opt,
415 const char *additional_error)
418 const char *val = value;
425 if (!multiple_option && prev_given && (*prev_given || (check_ambiguity && *field_given)))
427 if (short_opt != '-')
428 fprintf (stderr, "%s: `--%s' (`-%c') option given more than once%s\n",
429 package_name, long_opt, short_opt,
430 (additional_error ? additional_error : ""));
432 fprintf (stderr, "%s: `--%s' option given more than once%s\n",
433 package_name, long_opt,
434 (additional_error ? additional_error : ""));
435 return 1; /* failure */
439 if (field_given && *field_given && ! override)
446 val = possible_values[found];
450 *((int *)field) = !*((int *)field);
453 if (val) *((int *)field) = strtol (val, &stop_char, 0);
457 string_field = (char **)field;
458 if (!no_free && *string_field)
459 free (*string_field); /* free previous string */
460 *string_field = gengetopt_strdup (val);
467 /* check numeric conversion */
470 if (val && !(stop_char && *stop_char == '\0')) {
471 fprintf(stderr, "%s: invalid numeric value: %s\n", package_name, val);
472 return 1; /* failure */
479 /* store the original value */
485 if (value && orig_field) {
490 free (*orig_field); /* free previous string */
491 *orig_field = gengetopt_strdup (value);
501 cmdline_parser_internal (int argc, char * const *argv, struct gengetopt_args_info *args_info,
502 struct cmdline_parser_params *params, const char *additional_error)
504 int c; /* Character of the parsed option. */
507 struct gengetopt_args_info local_args_info;
514 package_name = argv[0];
516 override = params->override;
517 initialize = params->initialize;
518 check_required = params->check_required;
519 check_ambiguity = params->check_ambiguity;
522 cmdline_parser_init (args_info);
524 cmdline_parser_init (&local_args_info);
528 opterr = params->print_errors;
533 int option_index = 0;
535 static struct option long_options[] = {
536 { "help", 0, NULL, 'h' },
537 { "version", 0, NULL, 'V' },
538 { "print", 0, NULL, 'p' },
539 { "file", 1, NULL, 'f' },
540 { "dir", 1, NULL, 'd' },
541 { "recurse", 0, NULL, 'r' },
542 { "verbose", 1, NULL, 'v' },
546 c = getopt_long (argc, argv, "hVpf:d:rv:", long_options, &option_index);
548 if (c == -1) break; /* Exit from `while (1)' loop. */
552 case 'h': /* Print help and exit. */
553 cmdline_parser_print_help ();
554 cmdline_parser_free (&local_args_info);
557 case 'V': /* Print version and exit. */
558 cmdline_parser_print_version ();
559 cmdline_parser_free (&local_args_info);
562 case 'p': /* Prints the local database tree. */
565 if (update_arg((void *)&(args_info->print_flag), 0, &(args_info->print_given),
566 &(local_args_info.print_given), optarg, 0, 0, ARG_FLAG,
567 check_ambiguity, override, 1, 0, "print", 'p',
572 case 'f': /* Adds the file to local database. */
575 if (update_arg( (void *)&(args_info->file_arg),
576 &(args_info->file_orig), &(args_info->file_given),
577 &(local_args_info.file_given), optarg, 0, 0, ARG_STRING,
578 check_ambiguity, override, 0, 0,
584 case 'd': /* Adds the directory to local database. */
587 if (update_arg( (void *)&(args_info->dir_arg),
588 &(args_info->dir_orig), &(args_info->dir_given),
589 &(local_args_info.dir_given), optarg, 0, 0, ARG_STRING,
590 check_ambiguity, override, 0, 0,
596 case 'r': /* Recurse into sub-directories. */
599 if (update_arg((void *)&(args_info->recurse_flag), 0, &(args_info->recurse_given),
600 &(local_args_info.recurse_given), optarg, 0, 0, ARG_FLAG,
601 check_ambiguity, override, 1, 0, "recurse", 'r',
606 case 'v': /* Verbosity level. */
609 if (update_arg( (void *)&(args_info->verbose_arg),
610 &(args_info->verbose_orig), &(args_info->verbose_given),
611 &(local_args_info.verbose_given), optarg, 0, "1", ARG_INT,
612 check_ambiguity, override, 0, 0,
619 case 0: /* Long option with no short option */
620 case '?': /* Invalid option. */
621 /* `getopt_long' already printed an error message. */
624 default: /* bug: option not considered. */
625 fprintf (stderr, "%s: option unknown: %c%s\n", CMDLINE_PARSER_PACKAGE, c, (additional_error ? additional_error : ""));
634 error += cmdline_parser_required2 (args_info, argv[0], additional_error);
637 cmdline_parser_release (&local_args_info);
640 return (EXIT_FAILURE);
645 int found_prog_name = 0;
646 /* whether program name, i.e., argv[0], is in the remaining args
647 (this may happen with some implementations of getopt,
648 but surely not with the one included by gengetopt) */
652 if (argv[i++] == argv[0]) {
658 args_info->inputs_num = argc - optind - found_prog_name;
660 (char **)(malloc ((args_info->inputs_num)*sizeof(char *))) ;
661 while (optind < argc)
662 if (argv[optind++] != argv[0])
663 args_info->inputs[ i++ ] = gengetopt_strdup (argv[optind-1]) ;
670 cmdline_parser_release (&local_args_info);
671 return (EXIT_FAILURE);
674 #ifndef CONFIG_FILE_LINE_SIZE
675 #define CONFIG_FILE_LINE_SIZE 2048
677 #define ADDITIONAL_ERROR " in configuration file "
679 #define CONFIG_FILE_LINE_BUFFER_SIZE (CONFIG_FILE_LINE_SIZE+3)
680 /* 3 is for "--" and "=" */
683 _cmdline_parser_configfile (char * const filename, int *my_argc)
686 char my_argv[CONFIG_FILE_LINE_BUFFER_SIZE+1];
687 char linebuf[CONFIG_FILE_LINE_SIZE];
689 int result = 0, equal;
692 size_t len, next_token;
695 if ((file = fopen(filename, "r")) == NULL)
697 fprintf (stderr, "%s: Error opening configuration file '%s'\n",
698 CMDLINE_PARSER_PACKAGE, filename);
702 while ((fgets(linebuf, CONFIG_FILE_LINE_SIZE, file)) != NULL)
706 len = strlen(linebuf);
707 if (len > (CONFIG_FILE_LINE_BUFFER_SIZE-1))
709 fprintf (stderr, "%s:%s:%d: Line too long in configuration file\n",
710 CMDLINE_PARSER_PACKAGE, filename, line_num);
711 result = EXIT_FAILURE;
715 /* find first non-whitespace character in the line */
716 next_token = strspn (linebuf, " \t\r\n");
717 str_index = linebuf + next_token;
719 if ( str_index[0] == '\0' || str_index[0] == '#')
720 continue; /* empty line or comment line is skipped */
724 /* truncate fopt at the end of the first non-valid character */
725 next_token = strcspn (fopt, " \t\r\n=");
727 if (fopt[next_token] == '\0') /* the line is over */
734 /* remember if equal sign is present */
735 equal = (fopt[next_token] == '=');
736 fopt[next_token++] = '\0';
738 /* advance pointers to the next token after the end of fopt */
739 next_token += strspn (fopt + next_token, " \t\r\n");
741 /* check for the presence of equal sign, and if so, skip it */
743 if ((equal = (fopt[next_token] == '=')))
746 next_token += strspn (fopt + next_token, " \t\r\n");
748 str_index += next_token;
752 if ( farg[0] == '\"' || farg[0] == '\'' )
753 { /* quoted argument */
754 str_index = strchr (++farg, str_index[0] ); /* skip opening quote */
759 "%s:%s:%d: unterminated string in configuration file\n",
760 CMDLINE_PARSER_PACKAGE, filename, line_num);
761 result = EXIT_FAILURE;
766 { /* read up the remaining part up to a delimiter */
767 next_token = strcspn (farg, " \t\r\n#\'\"");
768 str_index += next_token;
771 /* truncate farg at the delimiter and store it for further check */
772 delimiter = *str_index, *str_index++ = '\0';
774 /* everything but comment is illegal at the end of line */
775 if (delimiter != '\0' && delimiter != '#')
777 str_index += strspn(str_index, " \t\r\n");
778 if (*str_index != '\0' && *str_index != '#')
782 "%s:%s:%d: malformed string in configuration file\n",
783 CMDLINE_PARSER_PACKAGE, filename, line_num);
784 result = EXIT_FAILURE;
790 if (!strcmp(fopt,"include")) {
792 result = _cmdline_parser_configfile(farg, my_argc);
794 fprintf(stderr, "%s:%s:%d: include requires a filename argument.\n",
795 CMDLINE_PARSER_PACKAGE, filename, line_num);
800 strcat (my_argv, len > 1 ? "--" : "-");
801 strcat (my_argv, fopt);
802 if (len > 1 && ((farg && *farg) || equal))
803 strcat (my_argv, "=");
805 strcat (my_argv, farg);
808 cmd_line_list_tmp = (struct line_list *) malloc (sizeof (struct line_list));
809 cmd_line_list_tmp->next = cmd_line_list;
810 cmd_line_list = cmd_line_list_tmp;
811 cmd_line_list->string_arg = gengetopt_strdup(my_argv);
820 cmdline_parser_configfile (char * const filename,
821 struct gengetopt_args_info *args_info,
822 int override, int initialize, int check_required)
824 struct cmdline_parser_params params;
826 params.override = override;
827 params.initialize = initialize;
828 params.check_required = check_required;
829 params.check_ambiguity = 0;
830 params.print_errors = 1;
832 return cmdline_parser_config_file (filename, args_info, ¶ms);
836 cmdline_parser_config_file (char * const filename,
837 struct gengetopt_args_info *args_info,
838 struct cmdline_parser_params *params)
843 char *additional_error;
845 /* store the program name */
846 cmd_line_list_tmp = (struct line_list *) malloc (sizeof (struct line_list));
847 cmd_line_list_tmp->next = cmd_line_list;
848 cmd_line_list = cmd_line_list_tmp;
849 cmd_line_list->string_arg = gengetopt_strdup (CMDLINE_PARSER_PACKAGE);
851 result = _cmdline_parser_configfile(filename, &my_argc);
853 if (result != EXIT_FAILURE) {
854 my_argv_arg = (char **) malloc((my_argc+1) * sizeof(char *));
855 cmd_line_list_tmp = cmd_line_list;
857 for (i = my_argc - 1; i >= 0; --i) {
858 my_argv_arg[i] = cmd_line_list_tmp->string_arg;
859 cmd_line_list_tmp = cmd_line_list_tmp->next;
862 my_argv_arg[my_argc] = 0;
864 additional_error = (char *)malloc(strlen(filename) + strlen(ADDITIONAL_ERROR) + 1);
865 strcpy (additional_error, ADDITIONAL_ERROR);
866 strcat (additional_error, filename);
868 cmdline_parser_internal (my_argc, my_argv_arg, args_info,
872 free (additional_error);
877 if (result == EXIT_FAILURE)
879 cmdline_parser_free (args_info);