2 File autogenerated by gengetopt version 2.20
3 generated with the following command:
4 gengetopt --file-name=gimmick_ggo --include-getopt
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. */
21 #include "gimmick_ggo.h"
23 const char *gengetopt_args_info_purpose = "";
25 const char *gengetopt_args_info_usage = "Usage: gimmick COMMAND [OPTIONS]";
27 const char *gengetopt_args_info_description = "";
29 const char *gengetopt_args_info_help[] = {
30 " -h, --help Print help and exit",
31 " -V, --version Print version and exit",
33 " -p, --print Prints the local database tree (default=off)",
34 " -f, --file=<file name> Adds the file to local database",
35 " -d, --dir=<path> Adds the directory to local database",
36 " -r, --recurse Recurse into sub-directories (default=off)",
38 " -v, --verbose=INT Verbosity level (default=`1')",
39 " -D, --debug=INT Debug messages level (default=`0')",
44 void clear_given (struct gengetopt_args_info *args_info);
46 void clear_args (struct gengetopt_args_info *args_info);
49 cmdline_parser_internal (int argc, char * const *argv, struct gengetopt_args_info *args_info, int override, int initialize, int check_required, const char *additional_error);
52 cmdline_parser_required2 (struct gengetopt_args_info *args_info, const char *prog_name, const char *additional_error);
55 gengetopt_strdup (const char *s);
58 void clear_given (struct gengetopt_args_info *args_info)
60 args_info->help_given = 0 ;
61 args_info->version_given = 0 ;
62 args_info->print_given = 0 ;
63 args_info->file_given = 0 ;
64 args_info->dir_given = 0 ;
65 args_info->recurse_given = 0 ;
66 args_info->verbose_given = 0 ;
67 args_info->debug_given = 0 ;
71 void clear_args (struct gengetopt_args_info *args_info)
73 args_info->print_flag = 0;
74 args_info->file_arg = NULL;
75 args_info->file_orig = NULL;
76 args_info->dir_arg = NULL;
77 args_info->dir_orig = NULL;
78 args_info->recurse_flag = 0;
79 args_info->verbose_arg = 1;
80 args_info->verbose_orig = NULL;
81 args_info->debug_arg = 0;
82 args_info->debug_orig = NULL;
87 void init_args_info(struct gengetopt_args_info *args_info)
89 args_info->help_help = gengetopt_args_info_help[0] ;
90 args_info->version_help = gengetopt_args_info_help[1] ;
91 args_info->print_help = gengetopt_args_info_help[3] ;
92 args_info->file_help = gengetopt_args_info_help[4] ;
93 args_info->dir_help = gengetopt_args_info_help[5] ;
94 args_info->recurse_help = gengetopt_args_info_help[6] ;
95 args_info->verbose_help = gengetopt_args_info_help[8] ;
96 args_info->debug_help = gengetopt_args_info_help[9] ;
101 cmdline_parser_print_version (void)
103 printf ("%s %s\n", CMDLINE_PARSER_PACKAGE, CMDLINE_PARSER_VERSION);
107 cmdline_parser_print_help (void)
110 cmdline_parser_print_version ();
112 if (strlen(gengetopt_args_info_purpose) > 0)
113 printf("\n%s\n", gengetopt_args_info_purpose);
115 printf("\n%s\n\n", gengetopt_args_info_usage);
117 if (strlen(gengetopt_args_info_description) > 0)
118 printf("%s\n", gengetopt_args_info_description);
120 while (gengetopt_args_info_help[i])
121 printf("%s\n", gengetopt_args_info_help[i++]);
125 cmdline_parser_init (struct gengetopt_args_info *args_info)
127 clear_given (args_info);
128 clear_args (args_info);
129 init_args_info (args_info);
133 cmdline_parser_release (struct gengetopt_args_info *args_info)
136 if (args_info->file_arg)
138 free (args_info->file_arg); /* free previous argument */
139 args_info->file_arg = 0;
141 if (args_info->file_orig)
143 free (args_info->file_orig); /* free previous argument */
144 args_info->file_orig = 0;
146 if (args_info->dir_arg)
148 free (args_info->dir_arg); /* free previous argument */
149 args_info->dir_arg = 0;
151 if (args_info->dir_orig)
153 free (args_info->dir_orig); /* free previous argument */
154 args_info->dir_orig = 0;
156 if (args_info->verbose_orig)
158 free (args_info->verbose_orig); /* free previous argument */
159 args_info->verbose_orig = 0;
161 if (args_info->debug_orig)
163 free (args_info->debug_orig); /* free previous argument */
164 args_info->debug_orig = 0;
167 clear_given (args_info);
171 cmdline_parser_file_save(const char *filename, struct gengetopt_args_info *args_info)
176 outfile = fopen(filename, "w");
180 fprintf (stderr, "%s: cannot open file for writing: %s\n", CMDLINE_PARSER_PACKAGE, filename);
184 if (args_info->help_given) {
185 fprintf(outfile, "%s\n", "help");
187 if (args_info->version_given) {
188 fprintf(outfile, "%s\n", "version");
190 if (args_info->print_given) {
191 fprintf(outfile, "%s\n", "print");
193 if (args_info->file_given) {
194 if (args_info->file_orig) {
195 fprintf(outfile, "%s=\"%s\"\n", "file", args_info->file_orig);
197 fprintf(outfile, "%s\n", "file");
200 if (args_info->dir_given) {
201 if (args_info->dir_orig) {
202 fprintf(outfile, "%s=\"%s\"\n", "dir", args_info->dir_orig);
204 fprintf(outfile, "%s\n", "dir");
207 if (args_info->recurse_given) {
208 fprintf(outfile, "%s\n", "recurse");
210 if (args_info->verbose_given) {
211 if (args_info->verbose_orig) {
212 fprintf(outfile, "%s=\"%s\"\n", "verbose", args_info->verbose_orig);
214 fprintf(outfile, "%s\n", "verbose");
217 if (args_info->debug_given) {
218 if (args_info->debug_orig) {
219 fprintf(outfile, "%s=\"%s\"\n", "debug", args_info->debug_orig);
221 fprintf(outfile, "%s\n", "debug");
232 cmdline_parser_free (struct gengetopt_args_info *args_info)
234 cmdline_parser_release (args_info);
238 /* gengetopt_strdup() */
239 /* strdup.c replacement of strdup, which is not standard */
241 gengetopt_strdup (const char *s)
247 result = (char*)malloc(strlen(s) + 1);
248 if (result == (char*)0)
255 cmdline_parser (int argc, char * const *argv, struct gengetopt_args_info *args_info)
257 return cmdline_parser2 (argc, argv, args_info, 0, 1, 1);
261 cmdline_parser2 (int argc, char * const *argv, struct gengetopt_args_info *args_info, int override, int initialize, int check_required)
265 result = cmdline_parser_internal (argc, argv, args_info, override, initialize, check_required, NULL);
267 if (result == EXIT_FAILURE)
269 cmdline_parser_free (args_info);
277 cmdline_parser_required (struct gengetopt_args_info *args_info, const char *prog_name)
279 int result = EXIT_SUCCESS;
281 if (cmdline_parser_required2(args_info, prog_name, NULL) > 0)
282 result = EXIT_FAILURE;
284 if (result == EXIT_FAILURE)
286 cmdline_parser_free (args_info);
294 cmdline_parser_required2 (struct gengetopt_args_info *args_info, const char *prog_name, const char *additional_error)
298 /* checks for required options */
300 /* checks for dependences among options */
301 if (args_info->recurse_given && ! args_info->dir_given)
303 fprintf (stderr, "%s: '--recurse' ('-r') option depends on option 'dir'%s\n", prog_name, (additional_error ? additional_error : ""));
311 * Extracted from the glibc source tree, version 2.3.6
313 * Licensed under the GPL as per the whole glibc source tree.
315 * This file was modified so that getopt_long can be called
316 * many times without risking previous memory to be spoiled.
318 * Modified by Andre Noll and Lorenzo Bettini for use in
319 * GNU gengetopt generated files.
324 * we must include anything we need since this file is not thought to be
325 * inserted in a file already using getopt.h
333 /* has_arg can't be an enum because some compilers complain about
334 type mismatches in all the code that assumes it is an int. */
340 /* For communication from `getopt' to the caller.
341 When `getopt' finds an option that takes an argument,
342 the argument value is returned here.
343 Also, when `ordering' is RETURN_IN_ORDER,
344 each non-option ARGV-element is returned here. */
348 /* Index in ARGV of the next element to be scanned.
349 This is used for communication to and from the caller
350 and for communication between successive calls to `getopt'.
352 On entry to `getopt', zero means this is the first call; initialize.
354 When `getopt' returns -1, this is the index of the first of the
355 non-option elements that the caller should itself scan.
357 Otherwise, `optind' communicates from one call to the next
358 how much of ARGV has been scanned so far. */
362 /* Callers store zero here to inhibit the error message `getopt' prints
363 for unrecognized options. */
367 /* Set to an option character which was unrecognized. */
371 /* This version of `getopt' appears to the caller like standard Unix `getopt'
372 but it behaves differently for the user, since it allows the user
373 to intersperse the options with the other arguments.
375 As `getopt' works, it permutes the elements of ARGV so that,
376 when it is done, all the options precede everything else. Thus
377 all application programs are extended to handle flexible argument order.
380 If the field `flag' is not NULL, it points to a variable that is set
381 to the value given in the field `val' when the option is found, but
382 left unchanged if the option is not found.
384 To have a long-named option do something other than set an `int' to
385 a compiled-in constant, such as set a value from `custom_optarg', set the
386 option's `flag' field to zero and its `val' field to a nonzero
387 value (the equivalent single-letter option character, if there is
388 one). For long options that have a zero `flag' field, `getopt'
389 returns the contents of the `val' field. */
391 /* Names for the values of the `has_arg' field of `struct option'. */
393 #define no_argument 0
396 #ifndef required_argument
397 #define required_argument 1
400 #ifndef optional_argument
401 #define optional_argument 2
404 struct custom_getopt_data {
406 * These have exactly the same meaning as the corresponding global variables,
407 * except that they are used for the reentrant versions of getopt.
414 /* True if the internal members have been initialized. */
418 * The next char to be scanned in the option-element in which the last option
419 * character we returned was found. This allows us to pick up the scan where
420 * we left off. If this is zero, or a null string, it means resume the scan by
421 * advancing to the next ARGV-element.
426 * Describe the part of ARGV that contains non-options that have been skipped.
427 * `first_nonopt' is the index in ARGV of the first of them; `last_nonopt' is
428 * the index after the last of them.
435 * the variables optarg, optind, opterr and optopt are renamed with
436 * the custom_ prefix so that they don't interfere with getopt ones.
438 * Moreover they're static so they are visible only from within the
439 * file where this very file will be included.
443 * For communication from `custom_getopt' to the caller. When `custom_getopt' finds an
444 * option that takes an argument, the argument value is returned here.
446 static char *custom_optarg;
449 * Index in ARGV of the next element to be scanned. This is used for
450 * communication to and from the caller and for communication between
451 * successive calls to `custom_getopt'.
453 * On entry to `custom_getopt', 1 means this is the first call; initialize.
455 * When `custom_getopt' returns -1, this is the index of the first of the non-option
456 * elements that the caller should itself scan.
458 * Otherwise, `custom_optind' communicates from one call to the next how much of ARGV
459 * has been scanned so far.
461 * 1003.2 says this must be 1 before any call.
463 static int custom_optind = 1;
466 * Callers store zero here to inhibit the error message for unrecognized
469 static int custom_opterr = 1;
472 * Set to an option character which was unrecognized. This must be initialized
473 * on some systems to avoid linking in the system's own getopt implementation.
475 static int custom_optopt = '?';
478 * Exchange two adjacent subsequences of ARGV. One subsequence is elements
479 * [first_nonopt,last_nonopt) which contains all the non-options that have been
480 * skipped so far. The other is elements [last_nonopt,custom_optind), which contains
481 * all the options processed since those non-options were skipped.
482 * `first_nonopt' and `last_nonopt' are relocated so that they describe the new
483 * indices of the non-options in ARGV after they are moved.
485 static void exchange(char **argv, struct custom_getopt_data *d)
487 int bottom = d->first_nonopt;
488 int middle = d->last_nonopt;
489 int top = d->custom_optind;
493 * Exchange the shorter segment with the far end of the longer segment.
494 * That puts the shorter segment into the right place. It leaves the
495 * longer segment in the right place overall, but it consists of two
496 * parts that need to be swapped next.
498 while (top > middle && middle > bottom) {
499 if (top - middle > middle - bottom) {
500 /* Bottom segment is the short one. */
501 int len = middle - bottom;
504 /* Swap it with the top part of the top segment. */
505 for (i = 0; i < len; i++) {
506 tem = argv[bottom + i];
508 argv[top - (middle - bottom) + i];
509 argv[top - (middle - bottom) + i] = tem;
511 /* Exclude the moved bottom segment from further swapping. */
514 /* Top segment is the short one. */
515 int len = top - middle;
518 /* Swap it with the bottom part of the bottom segment. */
519 for (i = 0; i < len; i++) {
520 tem = argv[bottom + i];
521 argv[bottom + i] = argv[middle + i];
522 argv[middle + i] = tem;
524 /* Exclude the moved top segment from further swapping. */
528 /* Update records for the slots the non-options now occupy. */
529 d->first_nonopt += (d->custom_optind - d->last_nonopt);
530 d->last_nonopt = d->custom_optind;
533 /* Initialize the internal data when the first call is made. */
534 static void custom_getopt_initialize(struct custom_getopt_data *d)
537 * Start processing options with ARGV-element 1 (since ARGV-element 0
538 * is the program name); the sequence of previously skipped non-option
539 * ARGV-elements is empty.
541 d->first_nonopt = d->last_nonopt = d->custom_optind;
546 #define NONOPTION_P (argv[d->custom_optind][0] != '-' || argv[d->custom_optind][1] == '\0')
548 /* return: zero: continue, nonzero: return given value to user */
549 static int shuffle_argv(int argc, char *const *argv,const struct option *longopts,
550 struct custom_getopt_data *d)
553 * Give FIRST_NONOPT & LAST_NONOPT rational values if CUSTOM_OPTIND has been
554 * moved back by the user (who may also have changed the arguments).
556 if (d->last_nonopt > d->custom_optind)
557 d->last_nonopt = d->custom_optind;
558 if (d->first_nonopt > d->custom_optind)
559 d->first_nonopt = d->custom_optind;
561 * If we have just processed some options following some
562 * non-options, exchange them so that the options come first.
564 if (d->first_nonopt != d->last_nonopt &&
565 d->last_nonopt != d->custom_optind)
566 exchange((char **) argv, d);
567 else if (d->last_nonopt != d->custom_optind)
568 d->first_nonopt = d->custom_optind;
570 * Skip any additional non-options and extend the range of
571 * non-options previously skipped.
573 while (d->custom_optind < argc && NONOPTION_P)
575 d->last_nonopt = d->custom_optind;
577 * The special ARGV-element `--' means premature end of options. Skip
578 * it like a null option, then exchange with previous non-options as if
579 * it were an option, then skip everything else like a non-option.
581 if (d->custom_optind != argc && !strcmp(argv[d->custom_optind], "--")) {
583 if (d->first_nonopt != d->last_nonopt
584 && d->last_nonopt != d->custom_optind)
585 exchange((char **) argv, d);
586 else if (d->first_nonopt == d->last_nonopt)
587 d->first_nonopt = d->custom_optind;
588 d->last_nonopt = argc;
589 d->custom_optind = argc;
592 * If we have done all the ARGV-elements, stop the scan and back over
593 * any non-options that we skipped and permuted.
595 if (d->custom_optind == argc) {
597 * Set the next-arg-index to point at the non-options that we
598 * previously skipped, so the caller will digest them.
600 if (d->first_nonopt != d->last_nonopt)
601 d->custom_optind = d->first_nonopt;
605 * If we have come to a non-option and did not permute it, either stop
606 * the scan or describe it to the caller and pass it by.
609 d->custom_optarg = argv[d->custom_optind++];
613 * We have found another option-ARGV-element. Skip the initial
616 d->nextchar = (argv[d->custom_optind] + 1 + (longopts != NULL && argv[d->custom_optind][1] == '-'));
621 * Check whether the ARGV-element is a long option.
623 * If there's a long option "fubar" and the ARGV-element is "-fu", consider
624 * that an abbreviation of the long option, just like "--fu", and not "-f" with
627 * This distinction seems to be the most useful approach.
630 static int check_long_opt(int argc, char *const *argv, const char *optstring,
631 const struct option *longopts, int *longind,
632 int print_errors, struct custom_getopt_data *d)
635 const struct option *p;
636 const struct option *pfound = NULL;
642 for (nameend = d->nextchar; *nameend && *nameend != '='; nameend++)
645 /* Test all long options for either exact match or abbreviated matches */
646 for (p = longopts, option_index = 0; p->name; p++, option_index++)
647 if (!strncmp(p->name, d->nextchar, nameend - d->nextchar)) {
648 if ((unsigned int) (nameend - d->nextchar)
649 == (unsigned int) strlen(p->name)) {
650 /* Exact match found. */
652 indfound = option_index;
655 } else if (pfound == NULL) {
656 /* First nonexact match found. */
658 indfound = option_index;
659 } else if (pfound->has_arg != p->has_arg
660 || pfound->flag != p->flag
661 || pfound->val != p->val)
662 /* Second or later nonexact match found. */
665 if (ambig && !exact) {
668 "%s: option `%s' is ambiguous\n",
669 argv[0], argv[d->custom_optind]);
671 d->nextchar += strlen(d->nextchar);
673 d->custom_optopt = 0;
677 option_index = indfound;
680 if (pfound->has_arg != no_argument)
681 d->custom_optarg = nameend + 1;
684 if (argv[d->custom_optind - 1][1] == '-') {
686 fprintf(stderr, "%s: option `--%s' doesn't allow an argument\n",
687 argv[0], pfound->name);
689 /* +option or -option */
690 fprintf(stderr, "%s: option `%c%s' doesn't allow an argument\n",
691 argv[0], argv[d->custom_optind - 1][0], pfound->name);
695 d->nextchar += strlen(d->nextchar);
696 d->custom_optopt = pfound->val;
699 } else if (pfound->has_arg == required_argument) {
700 if (d->custom_optind < argc)
701 d->custom_optarg = argv[d->custom_optind++];
705 "%s: option `%s' requires an argument\n",
707 argv[d->custom_optind - 1]);
709 d->nextchar += strlen(d->nextchar);
710 d->custom_optopt = pfound->val;
711 return optstring[0] == ':' ? ':' : '?';
714 d->nextchar += strlen(d->nextchar);
716 *longind = option_index;
718 *(pfound->flag) = pfound->val;
724 * Can't find it as a long option. If this is not getopt_long_only, or
725 * the option starts with '--' or is not a valid short option, then
726 * it's an error. Otherwise interpret it as a short option.
729 if (argv[d->custom_optind][1] == '-') {
732 "%s: unrecognized option `--%s'\n",
733 argv[0], d->nextchar);
735 /* +option or -option */
737 "%s: unrecognized option `%c%s'\n",
738 argv[0], argv[d->custom_optind][0],
742 d->nextchar = (char *) "";
744 d->custom_optopt = 0;
748 static int check_short_opt(int argc, char *const *argv, const char *optstring,
749 int print_errors, struct custom_getopt_data *d)
751 char c = *d->nextchar++;
752 char *temp = strchr(optstring, c);
754 /* Increment `custom_optind' when we start to process its last character. */
755 if (*d->nextchar == '\0')
757 if (!temp || c == ':') {
759 fprintf(stderr, "%s: invalid option -- %c\n", argv[0], c);
761 d->custom_optopt = c;
764 if (temp[1] == ':') {
765 if (temp[2] == ':') {
766 /* This is an option that accepts an argument optionally. */
767 if (*d->nextchar != '\0') {
768 d->custom_optarg = d->nextchar;
771 d->custom_optarg = NULL;
774 /* This is an option that requires an argument. */
775 if (*d->nextchar != '\0') {
776 d->custom_optarg = d->nextchar;
778 * If we end this ARGV-element by taking the
779 * rest as an arg, we must advance to the next
783 } else if (d->custom_optind == argc) {
786 "%s: option requires an argument -- %c\n",
789 d->custom_optopt = c;
790 if (optstring[0] == ':')
796 * We already incremented `custom_optind' once;
797 * increment it again when taking next ARGV-elt
800 d->custom_optarg = argv[d->custom_optind++];
808 * Scan elements of ARGV for option characters given in OPTSTRING.
810 * If an element of ARGV starts with '-', and is not exactly "-" or "--",
811 * then it is an option element. The characters of this element
812 * (aside from the initial '-') are option characters. If `getopt'
813 * is called repeatedly, it returns successively each of the option characters
814 * from each of the option elements.
816 * If `getopt' finds another option character, it returns that character,
817 * updating `custom_optind' and `nextchar' so that the next call to `getopt' can
818 * resume the scan with the following option character or ARGV-element.
820 * If there are no more option characters, `getopt' returns -1.
821 * Then `custom_optind' is the index in ARGV of the first ARGV-element
822 * that is not an option. (The ARGV-elements have been permuted
823 * so that those that are not options now come last.)
825 * OPTSTRING is a string containing the legitimate option characters.
826 * If an option character is seen that is not listed in OPTSTRING,
827 * return '?' after printing an error message. If you set `custom_opterr' to
828 * zero, the error message is suppressed but we still return '?'.
830 * If a char in OPTSTRING is followed by a colon, that means it wants an arg,
831 * so the following text in the same ARGV-element, or the text of the following
832 * ARGV-element, is returned in `custom_optarg'. Two colons mean an option that
833 * wants an optional arg; if there is text in the current ARGV-element,
834 * it is returned in `custom_optarg', otherwise `custom_optarg' is set to zero.
836 * If OPTSTRING starts with `-' or `+', it requests different methods of
837 * handling the non-option ARGV-elements.
838 * See the comments about RETURN_IN_ORDER and REQUIRE_ORDER, above.
840 * Long-named options begin with `--' instead of `-'.
841 * Their names may be abbreviated as long as the abbreviation is unique
842 * or is an exact match for some defined option. If they have an
843 * argument, it follows the option name in the same ARGV-element, separated
844 * from the option name by a `=', or else the in next ARGV-element.
845 * When `getopt' finds a long-named option, it returns 0 if that option's
846 * `flag' field is nonzero, the value of the option's `val' field
847 * if the `flag' field is zero.
849 * The elements of ARGV aren't really const, because we permute them.
850 * But we pretend they're const in the prototype to be compatible
851 * with other systems.
853 * LONGOPTS is a vector of `struct option' terminated by an
854 * element containing a name which is zero.
856 * LONGIND returns the index in LONGOPT of the long-named option found.
857 * It is only valid when a long-named option has been found by the most
860 * Return the option character from OPTS just read. Return -1 when there are
861 * no more options. For unrecognized options, or options missing arguments,
862 * `custom_optopt' is set to the option letter, and '?' is returned.
864 * The OPTS string is a list of characters which are recognized option letters,
865 * optionally followed by colons, specifying that that letter takes an
866 * argument, to be placed in `custom_optarg'.
868 * If a letter in OPTS is followed by two colons, its argument is optional.
869 * This behavior is specific to the GNU `getopt'.
871 * The argument `--' causes premature termination of argument scanning,
872 * explicitly telling `getopt' that there are no more options. If OPTS begins
873 * with `--', then non-option arguments are treated as arguments to the option
874 * '\0'. This behavior is specific to the GNU `getopt'.
877 static int getopt_internal_r(int argc, char *const *argv, const char *optstring,
878 const struct option *longopts, int *longind,
879 struct custom_getopt_data *d)
881 int ret, print_errors = d->custom_opterr;
883 if (optstring[0] == ':')
887 d->custom_optarg = NULL;
890 * This is a big difference with GNU getopt, since optind == 0
891 * means initialization while here 1 means first call.
893 if (d->custom_optind == 0 || !d->initialized) {
894 if (d->custom_optind == 0)
895 d->custom_optind = 1; /* Don't scan ARGV[0], the program name. */
896 custom_getopt_initialize(d);
898 if (d->nextchar == NULL || *d->nextchar == '\0') {
899 ret = shuffle_argv(argc, argv, longopts, d);
903 if (longopts && (argv[d->custom_optind][1] == '-' ))
904 return check_long_opt(argc, argv, optstring, longopts,
905 longind, print_errors, d);
906 return check_short_opt(argc, argv, optstring, print_errors, d);
909 static int custom_getopt_internal(int argc, char *const *argv, const char *optstring,
910 const struct option *longopts, int *longind)
913 /* Keep a global copy of all internal members of d */
914 static struct custom_getopt_data d;
916 d.custom_optind = custom_optind;
917 d.custom_opterr = custom_opterr;
918 result = getopt_internal_r(argc, argv, optstring, longopts,
920 custom_optind = d.custom_optind;
921 custom_optarg = d.custom_optarg;
922 custom_optopt = d.custom_optopt;
926 static int custom_getopt_long (int argc, char *const *argv, const char *options,
927 const struct option *long_options, int *opt_index)
929 return custom_getopt_internal(argc, argv, options, long_options,
934 cmdline_parser_internal (int argc, char * const *argv, struct gengetopt_args_info *args_info, int override, int initialize, int check_required, const char *additional_error)
936 int c; /* Character of the parsed option. */
939 struct gengetopt_args_info local_args_info;
942 cmdline_parser_init (args_info);
944 cmdline_parser_init (&local_args_info);
953 int option_index = 0;
956 static struct option long_options[] = {
957 { "help", 0, NULL, 'h' },
958 { "version", 0, NULL, 'V' },
959 { "print", 0, NULL, 'p' },
960 { "file", 1, NULL, 'f' },
961 { "dir", 1, NULL, 'd' },
962 { "recurse", 0, NULL, 'r' },
963 { "verbose", 1, NULL, 'v' },
964 { "debug", 1, NULL, 'D' },
969 custom_optarg = optarg;
970 custom_optind = optind;
971 custom_opterr = opterr;
972 custom_optopt = optopt;
974 c = custom_getopt_long (argc, argv, "hVpf:d:rv:D:", long_options, &option_index);
976 optarg = custom_optarg;
977 optind = custom_optind;
978 opterr = custom_opterr;
979 optopt = custom_optopt;
981 if (c == -1) break; /* Exit from `while (1)' loop. */
985 case 'h': /* Print help and exit. */
986 cmdline_parser_print_help ();
987 cmdline_parser_free (&local_args_info);
990 case 'V': /* Print version and exit. */
991 cmdline_parser_print_version ();
992 cmdline_parser_free (&local_args_info);
995 case 'p': /* Prints the local database tree. */
996 if (local_args_info.print_given)
998 fprintf (stderr, "%s: `--print' (`-p') option given more than once%s\n", argv[0], (additional_error ? additional_error : ""));
1001 if (args_info->print_given && ! override)
1003 local_args_info.print_given = 1;
1004 args_info->print_given = 1;
1005 args_info->print_flag = !(args_info->print_flag);
1008 case 'f': /* Adds the file to local database. */
1009 if (local_args_info.file_given)
1011 fprintf (stderr, "%s: `--file' (`-f') option given more than once%s\n", argv[0], (additional_error ? additional_error : ""));
1014 if (args_info->file_given && ! override)
1016 local_args_info.file_given = 1;
1017 args_info->file_given = 1;
1018 if (args_info->file_arg)
1019 free (args_info->file_arg); /* free previous string */
1020 args_info->file_arg = gengetopt_strdup (optarg);
1021 if (args_info->file_orig)
1022 free (args_info->file_orig); /* free previous string */
1023 args_info->file_orig = gengetopt_strdup (optarg);
1026 case 'd': /* Adds the directory to local database. */
1027 if (local_args_info.dir_given)
1029 fprintf (stderr, "%s: `--dir' (`-d') option given more than once%s\n", argv[0], (additional_error ? additional_error : ""));
1032 if (args_info->dir_given && ! override)
1034 local_args_info.dir_given = 1;
1035 args_info->dir_given = 1;
1036 if (args_info->dir_arg)
1037 free (args_info->dir_arg); /* free previous string */
1038 args_info->dir_arg = gengetopt_strdup (optarg);
1039 if (args_info->dir_orig)
1040 free (args_info->dir_orig); /* free previous string */
1041 args_info->dir_orig = gengetopt_strdup (optarg);
1044 case 'r': /* Recurse into sub-directories. */
1045 if (local_args_info.recurse_given)
1047 fprintf (stderr, "%s: `--recurse' (`-r') option given more than once%s\n", argv[0], (additional_error ? additional_error : ""));
1050 if (args_info->recurse_given && ! override)
1052 local_args_info.recurse_given = 1;
1053 args_info->recurse_given = 1;
1054 args_info->recurse_flag = !(args_info->recurse_flag);
1057 case 'v': /* Verbosity level. */
1058 if (local_args_info.verbose_given)
1060 fprintf (stderr, "%s: `--verbose' (`-v') option given more than once%s\n", argv[0], (additional_error ? additional_error : ""));
1063 if (args_info->verbose_given && ! override)
1065 local_args_info.verbose_given = 1;
1066 args_info->verbose_given = 1;
1067 args_info->verbose_arg = strtol (optarg, &stop_char, 0);
1068 if (!(stop_char && *stop_char == '\0')) {
1069 fprintf(stderr, "%s: invalid numeric value: %s\n", argv[0], optarg);
1072 if (args_info->verbose_orig)
1073 free (args_info->verbose_orig); /* free previous string */
1074 args_info->verbose_orig = gengetopt_strdup (optarg);
1077 case 'D': /* Debug messages level. */
1078 if (local_args_info.debug_given)
1080 fprintf (stderr, "%s: `--debug' (`-D') option given more than once%s\n", argv[0], (additional_error ? additional_error : ""));
1083 if (args_info->debug_given && ! override)
1085 local_args_info.debug_given = 1;
1086 args_info->debug_given = 1;
1087 args_info->debug_arg = strtol (optarg, &stop_char, 0);
1088 if (!(stop_char && *stop_char == '\0')) {
1089 fprintf(stderr, "%s: invalid numeric value: %s\n", argv[0], optarg);
1092 if (args_info->debug_orig)
1093 free (args_info->debug_orig); /* free previous string */
1094 args_info->debug_orig = gengetopt_strdup (optarg);
1098 case 0: /* Long option with no short option */
1099 case '?': /* Invalid option. */
1100 /* `getopt_long' already printed an error message. */
1103 default: /* bug: option not considered. */
1104 fprintf (stderr, "%s: option unknown: %c%s\n", CMDLINE_PARSER_PACKAGE, c, (additional_error ? additional_error : ""));
1113 error += cmdline_parser_required2 (args_info, argv[0], additional_error);
1116 cmdline_parser_release (&local_args_info);
1119 return (EXIT_FAILURE);
1125 cmdline_parser_release (&local_args_info);
1126 return (EXIT_FAILURE);