-/*
- * Extracted from the glibc source tree, version 2.3.6
- *
- * Licensed under the GPL as per the whole glibc source tree.
- *
- * This file was modified so that getopt_long can be called
- * many times without risking previous memory to be spoiled.
- *
- * Modified by Andre Noll and Lorenzo Bettini for use in
- * GNU gengetopt generated files.
- *
- */
-
-/*
- * we must include anything we need since this file is not thought to be
- * inserted in a file already using getopt.h
- *
- * Lorenzo
- */
-
-struct option
-{
- const char *name;
- /* has_arg can't be an enum because some compilers complain about
- type mismatches in all the code that assumes it is an int. */
- int has_arg;
- int *flag;
- int val;
-};
-
-/* For communication from `getopt' to the caller.
- When `getopt' finds an option that takes an argument,
- the argument value is returned here.
- Also, when `ordering' is RETURN_IN_ORDER,
- each non-option ARGV-element is returned here. */
-
-static char *optarg;
-
-/* Index in ARGV of the next element to be scanned.
- This is used for communication to and from the caller
- and for communication between successive calls to `getopt'.
-
- On entry to `getopt', zero means this is the first call; initialize.
-
- When `getopt' returns -1, this is the index of the first of the
- non-option elements that the caller should itself scan.
-
- Otherwise, `optind' communicates from one call to the next
- how much of ARGV has been scanned so far. */
-
-static int optind;
-
-/* Callers store zero here to inhibit the error message `getopt' prints
- for unrecognized options. */
-
-static int opterr;
-
-/* Set to an option character which was unrecognized. */
-
-static int optopt;
-
-/* This version of `getopt' appears to the caller like standard Unix `getopt'
- but it behaves differently for the user, since it allows the user
- to intersperse the options with the other arguments.
-
- As `getopt' works, it permutes the elements of ARGV so that,
- when it is done, all the options precede everything else. Thus
- all application programs are extended to handle flexible argument order.
-*/
-/*
- If the field `flag' is not NULL, it points to a variable that is set
- to the value given in the field `val' when the option is found, but
- left unchanged if the option is not found.
-
- To have a long-named option do something other than set an `int' to
- a compiled-in constant, such as set a value from `custom_optarg', set the
- option's `flag' field to zero and its `val' field to a nonzero
- value (the equivalent single-letter option character, if there is
- one). For long options that have a zero `flag' field, `getopt'
- returns the contents of the `val' field. */
-
-/* Names for the values of the `has_arg' field of `struct option'. */
-#ifndef no_argument
-#define no_argument 0
-#endif
-
-#ifndef required_argument
-#define required_argument 1
-#endif
-
-#ifndef optional_argument
-#define optional_argument 2
-#endif
-
-struct custom_getopt_data {
- /*
- * These have exactly the same meaning as the corresponding global variables,
- * except that they are used for the reentrant versions of getopt.
- */
- int custom_optind;
- int custom_opterr;
- int custom_optopt;
- char *custom_optarg;
-
- /* True if the internal members have been initialized. */
- int initialized;
-
- /*
- * The next char to be scanned in the option-element in which the last option
- * character we returned was found. This allows us to pick up the scan where
- * we left off. If this is zero, or a null string, it means resume the scan by
- * advancing to the next ARGV-element.
- */
- char *nextchar;
-
- /*
- * Describe the part of ARGV that contains non-options that have been skipped.
- * `first_nonopt' is the index in ARGV of the first of them; `last_nonopt' is
- * the index after the last of them.
- */
- int first_nonopt;
- int last_nonopt;
-};
-
-/*
- * the variables optarg, optind, opterr and optopt are renamed with
- * the custom_ prefix so that they don't interfere with getopt ones.
- *
- * Moreover they're static so they are visible only from within the
- * file where this very file will be included.
- */
-
-/*
- * For communication from `custom_getopt' to the caller. When `custom_getopt' finds an
- * option that takes an argument, the argument value is returned here.
- */
-static char *custom_optarg;
-
-/*
- * Index in ARGV of the next element to be scanned. This is used for
- * communication to and from the caller and for communication between
- * successive calls to `custom_getopt'.
- *
- * On entry to `custom_getopt', 1 means this is the first call; initialize.
- *
- * When `custom_getopt' returns -1, this is the index of the first of the non-option
- * elements that the caller should itself scan.
- *
- * Otherwise, `custom_optind' communicates from one call to the next how much of ARGV
- * has been scanned so far.
- *
- * 1003.2 says this must be 1 before any call.
- */
-static int custom_optind = 1;
-
-/*
- * Callers store zero here to inhibit the error message for unrecognized
- * options.
- */
-static int custom_opterr = 1;
-
-/*
- * Set to an option character which was unrecognized. This must be initialized
- * on some systems to avoid linking in the system's own getopt implementation.
- */
-static int custom_optopt = '?';
-
-/*
- * Exchange two adjacent subsequences of ARGV. One subsequence is elements
- * [first_nonopt,last_nonopt) which contains all the non-options that have been
- * skipped so far. The other is elements [last_nonopt,custom_optind), which contains
- * all the options processed since those non-options were skipped.
- * `first_nonopt' and `last_nonopt' are relocated so that they describe the new
- * indices of the non-options in ARGV after they are moved.
- */
-static void exchange(char **argv, struct custom_getopt_data *d)
-{
- int bottom = d->first_nonopt;
- int middle = d->last_nonopt;
- int top = d->custom_optind;
- char *tem;
-
- /*
- * Exchange the shorter segment with the far end of the longer segment.
- * That puts the shorter segment into the right place. It leaves the
- * longer segment in the right place overall, but it consists of two
- * parts that need to be swapped next.
- */
- while (top > middle && middle > bottom) {
- if (top - middle > middle - bottom) {
- /* Bottom segment is the short one. */
- int len = middle - bottom;
- int i;
-
- /* Swap it with the top part of the top segment. */
- for (i = 0; i < len; i++) {
- tem = argv[bottom + i];
- argv[bottom + i] =
- argv[top - (middle - bottom) + i];
- argv[top - (middle - bottom) + i] = tem;
- }
- /* Exclude the moved bottom segment from further swapping. */
- top -= len;
- } else {
- /* Top segment is the short one. */
- int len = top - middle;
- int i;
-
- /* Swap it with the bottom part of the bottom segment. */
- for (i = 0; i < len; i++) {
- tem = argv[bottom + i];
- argv[bottom + i] = argv[middle + i];
- argv[middle + i] = tem;
- }
- /* Exclude the moved top segment from further swapping. */
- bottom += len;
- }
- }
- /* Update records for the slots the non-options now occupy. */
- d->first_nonopt += (d->custom_optind - d->last_nonopt);
- d->last_nonopt = d->custom_optind;
-}
-
-/* Initialize the internal data when the first call is made. */
-static void custom_getopt_initialize(struct custom_getopt_data *d)
-{
- /*
- * Start processing options with ARGV-element 1 (since ARGV-element 0
- * is the program name); the sequence of previously skipped non-option
- * ARGV-elements is empty.
- */
- d->first_nonopt = d->last_nonopt = d->custom_optind;
- d->nextchar = NULL;
- d->initialized = 1;
-}
-
-#define NONOPTION_P (argv[d->custom_optind][0] != '-' || argv[d->custom_optind][1] == '\0')