groke6@381: /* from http://www.pwilson.net/getopt.html */ groke6@381: groke6@381: /* Getopt for GNU. groke6@381: NOTE: getopt is now part of the C library, so if you don't know what groke6@381: "Keep this file name-space clean" means, talk to drepper@gnu.org groke6@381: before changing it! groke6@381: Copyright (C) 1987,88,89,90,91,92,93,94,95,96,98,99,2000,2001 groke6@381: Free Software Foundation, Inc. groke6@381: This file is part of the GNU C Library. groke6@381: groke6@381: The GNU C Library is free software; you can redistribute it and/or groke6@381: modify it under the terms of the GNU Lesser General Public groke6@381: License as published by the Free Software Foundation; either groke6@381: version 2.1 of the License, or (at your option) any later version. groke6@381: groke6@381: The GNU C Library is distributed in the hope that it will be useful, groke6@381: but WITHOUT ANY WARRANTY; without even the implied warranty of groke6@381: MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU groke6@381: Lesser General Public License for more details. groke6@381: groke6@381: You should have received a copy of the GNU Lesser General Public groke6@381: License along with the GNU C Library; if not, write to the Free groke6@381: Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA groke6@381: 02111-1307 USA. */ groke6@381: groke6@381: /* This tells Alpha OSF/1 not to define a getopt prototype in . groke6@381: Ditto for AIX 3.2 and . */ groke6@381: #ifndef _NO_PROTO groke6@381: # define _NO_PROTO groke6@381: #endif groke6@381: groke6@381: #ifdef HAVE_CONFIG_H groke6@381: # include groke6@381: #endif groke6@381: groke6@381: #if !defined __STDC__ || !__STDC__ groke6@381: /* This is a separate conditional since some stdc systems groke6@381: reject `defined (const)'. */ groke6@381: # ifndef const groke6@381: # define const groke6@381: # endif groke6@381: #endif groke6@381: groke6@381: #include groke6@381: groke6@381: /* Comment out all this code if we are using the GNU C Library, and are not groke6@381: actually compiling the library itself. This code is part of the GNU C groke6@381: Library, but also included in many other GNU distributions. Compiling groke6@381: and linking in this code is a waste when using the GNU C library groke6@381: (especially if it is a shared library). Rather than having every GNU groke6@381: program understand `configure --with-gnu-libc' and omit the object files, groke6@381: it is simpler to just do this in the source for each such file. */ groke6@381: groke6@381: #define GETOPT_INTERFACE_VERSION 2 groke6@381: #if !defined _LIBC && defined __GLIBC__ && __GLIBC__ >= 2 groke6@381: # include groke6@381: # if _GNU_GETOPT_INTERFACE_VERSION == GETOPT_INTERFACE_VERSION groke6@381: # define ELIDE_CODE groke6@381: # endif groke6@381: #endif groke6@381: groke6@381: #ifndef ELIDE_CODE groke6@381: groke6@381: groke6@381: /* This needs to come after some library #include groke6@381: to get __GNU_LIBRARY__ defined. */ groke6@381: #ifdef __GNU_LIBRARY__ groke6@381: /* Don't include stdlib.h for non-GNU C libraries because some of them groke6@381: contain conflicting prototypes for getopt. */ groke6@381: # include groke6@381: # include groke6@381: #endif /* GNU C library. */ groke6@381: groke6@381: #ifdef VMS groke6@381: # include groke6@381: # if HAVE_STRING_H - 0 groke6@381: # include groke6@381: # endif groke6@381: #endif groke6@381: groke6@381: #ifndef _ groke6@381: /* This is for other GNU distributions with internationalized messages. */ groke6@381: # if (HAVE_LIBINTL_H && ENABLE_NLS) || defined _LIBC groke6@381: # include groke6@381: # ifndef _ groke6@381: # define _(msgid) gettext (msgid) groke6@381: # endif groke6@381: # else groke6@381: # define _(msgid) (msgid) groke6@381: # endif groke6@381: # if defined _LIBC && defined USE_IN_LIBIO groke6@381: # include groke6@381: # endif groke6@381: #endif groke6@381: groke6@381: /* This version of `getopt' appears to the caller like standard Unix `getopt' groke6@381: but it behaves differently for the user, since it allows the user groke6@381: to intersperse the options with the other arguments. groke6@381: groke6@381: As `getopt' works, it permutes the elements of ARGV so that, groke6@381: when it is done, all the options precede everything else. Thus groke6@381: all application programs are extended to handle flexible argument order. groke6@381: groke6@381: Setting the environment variable POSIXLY_CORRECT disables permutation. groke6@381: Then the behavior is completely standard. groke6@381: groke6@381: GNU application programs can use a third alternative mode in which groke6@381: they can distinguish the relative order of options and other arguments. */ groke6@381: groke6@381: #include "getopt.h" groke6@381: groke6@381: /* For communication from `getopt' to the caller. groke6@381: When `getopt' finds an option that takes an argument, groke6@381: the argument value is returned here. groke6@381: Also, when `ordering' is RETURN_IN_ORDER, groke6@381: each non-option ARGV-element is returned here. */ groke6@381: groke6@381: char *optarg; groke6@381: groke6@381: /* Index in ARGV of the next element to be scanned. groke6@381: This is used for communication to and from the caller groke6@381: and for communication between successive calls to `getopt'. groke6@381: groke6@381: On entry to `getopt', zero means this is the first call; initialize. groke6@381: groke6@381: When `getopt' returns -1, this is the index of the first of the groke6@381: non-option elements that the caller should itself scan. groke6@381: groke6@381: Otherwise, `optind' communicates from one call to the next groke6@381: how much of ARGV has been scanned so far. */ groke6@381: groke6@381: /* 1003.2 says this must be 1 before any call. */ groke6@381: int optind = 1; groke6@381: groke6@381: /* Formerly, initialization of getopt depended on optind==0, which groke6@381: causes problems with re-calling getopt as programs generally don't groke6@381: know that. */ groke6@381: groke6@381: int __getopt_initialized; groke6@381: groke6@381: /* The next char to be scanned in the option-element groke6@381: in which the last option character we returned was found. groke6@381: This allows us to pick up the scan where we left off. groke6@381: groke6@381: If this is zero, or a null string, it means resume the scan groke6@381: by advancing to the next ARGV-element. */ groke6@381: groke6@381: static char *nextchar; groke6@381: groke6@381: /* Callers store zero here to inhibit the error message groke6@381: for unrecognized options. */ groke6@381: groke6@381: int opterr = 1; groke6@381: groke6@381: /* Set to an option character which was unrecognized. groke6@381: This must be initialized on some systems to avoid linking in the groke6@381: system's own getopt implementation. */ groke6@381: groke6@381: int optopt = '?'; groke6@381: groke6@381: /* Describe how to deal with options that follow non-option ARGV-elements. groke6@381: groke6@381: If the caller did not specify anything, groke6@381: the default is REQUIRE_ORDER if the environment variable groke6@381: POSIXLY_CORRECT is defined, PERMUTE otherwise. groke6@381: groke6@381: REQUIRE_ORDER means don't recognize them as options; groke6@381: stop option processing when the first non-option is seen. groke6@381: This is what Unix does. groke6@381: This mode of operation is selected by either setting the environment groke6@381: variable POSIXLY_CORRECT, or using `+' as the first character groke6@381: of the list of option characters. groke6@381: groke6@381: PERMUTE is the default. We permute the contents of ARGV as we scan, groke6@381: so that eventually all the non-options are at the end. This allows options groke6@381: to be given in any order, even with programs that were not written to groke6@381: expect this. groke6@381: groke6@381: RETURN_IN_ORDER is an option available to programs that were written groke6@381: to expect options and other ARGV-elements in any order and that care about groke6@381: the ordering of the two. We describe each non-option ARGV-element groke6@381: as if it were the argument of an option with character code 1. groke6@381: Using `-' as the first character of the list of option characters groke6@381: selects this mode of operation. groke6@381: groke6@381: The special argument `--' forces an end of option-scanning regardless groke6@381: of the value of `ordering'. In the case of RETURN_IN_ORDER, only groke6@381: `--' can cause `getopt' to return -1 with `optind' != ARGC. */ groke6@381: groke6@381: static enum groke6@381: { groke6@381: REQUIRE_ORDER, PERMUTE, RETURN_IN_ORDER groke6@381: } ordering; groke6@381: groke6@381: /* Value of POSIXLY_CORRECT environment variable. */ groke6@381: static char *posixly_correct; groke6@381: groke6@381: #ifdef __GNU_LIBRARY__ groke6@381: /* We want to avoid inclusion of string.h with non-GNU libraries groke6@381: because there are many ways it can cause trouble. groke6@381: On some systems, it contains special magic macros that don't work groke6@381: in GCC. */ groke6@381: # include groke6@381: # define my_index strchr groke6@381: #else groke6@381: groke6@381: # if HAVE_STRING_H || WIN32 /* Pete Wilson mod 7/28/02 */ groke6@381: # include groke6@381: # else groke6@381: # include groke6@381: # endif groke6@381: groke6@381: /* Avoid depending on library functions or files groke6@381: whose names are inconsistent. */ groke6@381: groke6@381: #ifndef getenv groke6@381: extern char *getenv (); groke6@381: #endif groke6@381: groke6@381: static char * groke6@381: my_index (str, chr) groke6@381: const char *str; groke6@381: int chr; groke6@381: { groke6@381: while (*str) groke6@381: { groke6@381: if (*str == chr) groke6@381: return (char *) str; groke6@381: str++; groke6@381: } groke6@381: return 0; groke6@381: } groke6@381: groke6@381: /* If using GCC, we can safely declare strlen this way. groke6@381: If not using GCC, it is ok not to declare it. */ groke6@381: #ifdef __GNUC__ groke6@381: /* Note that Motorola Delta 68k R3V7 comes with GCC but not stddef.h. groke6@381: That was relevant to code that was here before. */ groke6@381: # if (!defined __STDC__ || !__STDC__) && !defined strlen groke6@381: /* gcc with -traditional declares the built-in strlen to return int, groke6@381: and has done so at least since version 2.4.5. -- rms. */ groke6@381: extern int strlen (const char *); groke6@381: # endif /* not __STDC__ */ groke6@381: #endif /* __GNUC__ */ groke6@381: groke6@381: #endif /* not __GNU_LIBRARY__ */ groke6@381: groke6@381: /* Handle permutation of arguments. */ groke6@381: groke6@381: /* Describe the part of ARGV that contains non-options that have groke6@381: been skipped. `first_nonopt' is the index in ARGV of the first of them; groke6@381: `last_nonopt' is the index after the last of them. */ groke6@381: groke6@381: static int first_nonopt; groke6@381: static int last_nonopt; groke6@381: groke6@381: #ifdef _LIBC groke6@381: /* Stored original parameters. groke6@381: XXX This is no good solution. We should rather copy the args so groke6@381: that we can compare them later. But we must not use malloc(3). */ groke6@381: extern int __libc_argc; groke6@381: extern char **__libc_argv; groke6@381: groke6@381: /* Bash 2.0 gives us an environment variable containing flags groke6@381: indicating ARGV elements that should not be considered arguments. */ groke6@381: groke6@381: # ifdef USE_NONOPTION_FLAGS groke6@381: /* Defined in getopt_init.c */ groke6@381: extern char *__getopt_nonoption_flags; groke6@381: groke6@381: static int nonoption_flags_max_len; groke6@381: static int nonoption_flags_len; groke6@381: # endif groke6@381: groke6@381: # ifdef USE_NONOPTION_FLAGS groke6@381: # define SWAP_FLAGS(ch1, ch2) \ groke6@381: if (nonoption_flags_len > 0) \ groke6@381: { \ groke6@381: char __tmp = __getopt_nonoption_flags[ch1]; \ groke6@381: __getopt_nonoption_flags[ch1] = __getopt_nonoption_flags[ch2]; \ groke6@381: __getopt_nonoption_flags[ch2] = __tmp; \ groke6@381: } groke6@381: # else groke6@381: # define SWAP_FLAGS(ch1, ch2) groke6@381: # endif groke6@381: #else /* !_LIBC */ groke6@381: # define SWAP_FLAGS(ch1, ch2) groke6@381: #endif /* _LIBC */ groke6@381: groke6@381: /* Exchange two adjacent subsequences of ARGV. groke6@381: One subsequence is elements [first_nonopt,last_nonopt) groke6@381: which contains all the non-options that have been skipped so far. groke6@381: The other is elements [last_nonopt,optind), which contains all groke6@381: the options processed since those non-options were skipped. groke6@381: groke6@381: `first_nonopt' and `last_nonopt' are relocated so that they describe groke6@381: the new indices of the non-options in ARGV after they are moved. */ groke6@381: groke6@381: #if defined __STDC__ && __STDC__ groke6@381: static void exchange (char **); groke6@381: #endif groke6@381: groke6@381: static void groke6@381: exchange (argv) groke6@381: char **argv; groke6@381: { groke6@381: int bottom = first_nonopt; groke6@381: int middle = last_nonopt; groke6@381: int top = optind; groke6@381: char *tem; groke6@381: groke6@381: /* Exchange the shorter segment with the far end of the longer segment. groke6@381: That puts the shorter segment into the right place. groke6@381: It leaves the longer segment in the right place overall, groke6@381: but it consists of two parts that need to be swapped next. */ groke6@381: groke6@381: #if defined _LIBC && defined USE_NONOPTION_FLAGS groke6@381: /* First make sure the handling of the `__getopt_nonoption_flags' groke6@381: string can work normally. Our top argument must be in the range groke6@381: of the string. */ groke6@381: if (nonoption_flags_len > 0 && top >= nonoption_flags_max_len) groke6@381: { groke6@381: /* We must extend the array. The user plays games with us and groke6@381: presents new arguments. */ groke6@381: char *new_str = malloc (top + 1); groke6@381: if (new_str == NULL) groke6@381: nonoption_flags_len = nonoption_flags_max_len = 0; groke6@381: else groke6@381: { groke6@381: memset (__mempcpy (new_str, __getopt_nonoption_flags, groke6@381: nonoption_flags_max_len), groke6@381: '\0', top + 1 - nonoption_flags_max_len); groke6@381: nonoption_flags_max_len = top + 1; groke6@381: __getopt_nonoption_flags = new_str; groke6@381: } groke6@381: } groke6@381: #endif groke6@381: groke6@381: while (top > middle && middle > bottom) groke6@381: { groke6@381: if (top - middle > middle - bottom) groke6@381: { groke6@381: /* Bottom segment is the short one. */ groke6@381: int len = middle - bottom; groke6@381: register int i; groke6@381: groke6@381: /* Swap it with the top part of the top segment. */ groke6@381: for (i = 0; i < len; i++) groke6@381: { groke6@381: tem = argv[bottom + i]; groke6@381: argv[bottom + i] = argv[top - (middle - bottom) + i]; groke6@381: argv[top - (middle - bottom) + i] = tem; groke6@381: SWAP_FLAGS (bottom + i, top - (middle - bottom) + i); groke6@381: } groke6@381: /* Exclude the moved bottom segment from further swapping. */ groke6@381: top -= len; groke6@381: } groke6@381: else groke6@381: { groke6@381: /* Top segment is the short one. */ groke6@381: int len = top - middle; groke6@381: register int i; groke6@381: groke6@381: /* Swap it with the bottom part of the bottom segment. */ groke6@381: for (i = 0; i < len; i++) groke6@381: { groke6@381: tem = argv[bottom + i]; groke6@381: argv[bottom + i] = argv[middle + i]; groke6@381: argv[middle + i] = tem; groke6@381: SWAP_FLAGS (bottom + i, middle + i); groke6@381: } groke6@381: /* Exclude the moved top segment from further swapping. */ groke6@381: bottom += len; groke6@381: } groke6@381: } groke6@381: groke6@381: /* Update records for the slots the non-options now occupy. */ groke6@381: groke6@381: first_nonopt += (optind - last_nonopt); groke6@381: last_nonopt = optind; groke6@381: } groke6@381: groke6@381: /* Initialize the internal data when the first call is made. */ groke6@381: groke6@381: #if defined __STDC__ && __STDC__ groke6@381: static const char *_getopt_initialize (int, char *const *, const char *); groke6@381: #endif groke6@381: static const char * groke6@381: _getopt_initialize (argc, argv, optstring) groke6@381: int argc; groke6@381: char *const *argv; groke6@381: const char *optstring; groke6@381: { groke6@381: /* Start processing options with ARGV-element 1 (since ARGV-element 0 groke6@381: is the program name); the sequence of previously skipped groke6@381: non-option ARGV-elements is empty. */ groke6@381: groke6@381: first_nonopt = last_nonopt = optind; groke6@381: groke6@381: nextchar = NULL; groke6@381: groke6@381: posixly_correct = getenv ("POSIXLY_CORRECT"); groke6@381: groke6@381: /* Determine how to handle the ordering of options and nonoptions. */ groke6@381: groke6@381: if (optstring[0] == '-') groke6@381: { groke6@381: ordering = RETURN_IN_ORDER; groke6@381: ++optstring; groke6@381: } groke6@381: else if (optstring[0] == '+') groke6@381: { groke6@381: ordering = REQUIRE_ORDER; groke6@381: ++optstring; groke6@381: } groke6@381: else if (posixly_correct != NULL) groke6@381: ordering = REQUIRE_ORDER; groke6@381: else groke6@381: ordering = PERMUTE; groke6@381: groke6@381: #if defined _LIBC && defined USE_NONOPTION_FLAGS groke6@381: if (posixly_correct == NULL groke6@381: && argc == __libc_argc && argv == __libc_argv) groke6@381: { groke6@381: if (nonoption_flags_max_len == 0) groke6@381: { groke6@381: if (__getopt_nonoption_flags == NULL groke6@381: || __getopt_nonoption_flags[0] == '\0') groke6@381: nonoption_flags_max_len = -1; groke6@381: else groke6@381: { groke6@381: const char *orig_str = __getopt_nonoption_flags; groke6@381: int len = nonoption_flags_max_len = strlen (orig_str); groke6@381: if (nonoption_flags_max_len < argc) groke6@381: nonoption_flags_max_len = argc; groke6@381: __getopt_nonoption_flags = groke6@381: (char *) malloc (nonoption_flags_max_len); groke6@381: if (__getopt_nonoption_flags == NULL) groke6@381: nonoption_flags_max_len = -1; groke6@381: else groke6@381: memset (__mempcpy (__getopt_nonoption_flags, orig_str, len), groke6@381: '\0', nonoption_flags_max_len - len); groke6@381: } groke6@381: } groke6@381: nonoption_flags_len = nonoption_flags_max_len; groke6@381: } groke6@381: else groke6@381: nonoption_flags_len = 0; groke6@381: #endif groke6@381: groke6@381: return optstring; groke6@381: } groke6@381: groke6@381: /* Scan elements of ARGV (whose length is ARGC) for option characters groke6@381: given in OPTSTRING. groke6@381: groke6@381: If an element of ARGV starts with '-', and is not exactly "-" or "--", groke6@381: then it is an option element. The characters of this element groke6@381: (aside from the initial '-') are option characters. If `getopt' groke6@381: is called repeatedly, it returns successively each of the option characters groke6@381: from each of the option elements. groke6@381: groke6@381: If `getopt' finds another option character, it returns that character, groke6@381: updating `optind' and `nextchar' so that the next call to `getopt' can groke6@381: resume the scan with the following option character or ARGV-element. groke6@381: groke6@381: If there are no more option characters, `getopt' returns -1. groke6@381: Then `optind' is the index in ARGV of the first ARGV-element groke6@381: that is not an option. (The ARGV-elements have been permuted groke6@381: so that those that are not options now come last.) groke6@381: groke6@381: OPTSTRING is a string containing the legitimate option characters. groke6@381: If an option character is seen that is not listed in OPTSTRING, groke6@381: return '?' after printing an error message. If you set `opterr' to groke6@381: zero, the error message is suppressed but we still return '?'. groke6@381: groke6@381: If a char in OPTSTRING is followed by a colon, that means it wants an arg, groke6@381: so the following text in the same ARGV-element, or the text of the following groke6@381: ARGV-element, is returned in `optarg'. Two colons mean an option that groke6@381: wants an optional arg; if there is text in the current ARGV-element, groke6@381: it is returned in `optarg', otherwise `optarg' is set to zero. groke6@381: groke6@381: If OPTSTRING starts with `-' or `+', it requests different methods of groke6@381: handling the non-option ARGV-elements. groke6@381: See the comments about RETURN_IN_ORDER and REQUIRE_ORDER, above. groke6@381: groke6@381: Long-named options begin with `--' instead of `-'. groke6@381: Their names may be abbreviated as long as the abbreviation is unique groke6@381: or is an exact match for some defined option. If they have an groke6@381: argument, it follows the option name in the same ARGV-element, separated groke6@381: from the option name by a `=', or else the in next ARGV-element. groke6@381: When `getopt' finds a long-named option, it returns 0 if that option's groke6@381: `flag' field is nonzero, the value of the option's `val' field groke6@381: if the `flag' field is zero. groke6@381: groke6@381: The elements of ARGV aren't really const, because we permute them. groke6@381: But we pretend they're const in the prototype to be compatible groke6@381: with other systems. groke6@381: groke6@381: LONGOPTS is a vector of `struct option' terminated by an groke6@381: element containing a name which is zero. groke6@381: groke6@381: LONGIND returns the index in LONGOPT of the long-named option found. groke6@381: It is only valid when a long-named option has been found by the most groke6@381: recent call. groke6@381: groke6@381: If LONG_ONLY is nonzero, '-' as well as '--' can introduce groke6@381: long-named options. */ groke6@381: groke6@381: int groke6@381: _getopt_internal (argc, argv, optstring, longopts, longind, long_only) groke6@381: int argc; groke6@381: char *const *argv; groke6@381: const char *optstring; groke6@381: const struct option *longopts; groke6@381: int *longind; groke6@381: int long_only; groke6@381: { groke6@381: int print_errors = opterr; groke6@381: if (optstring[0] == ':') groke6@381: print_errors = 0; groke6@381: groke6@381: if (argc < 1) groke6@381: return -1; groke6@381: groke6@381: optarg = NULL; groke6@381: groke6@381: if (optind == 0 || !__getopt_initialized) groke6@381: { groke6@381: if (optind == 0) groke6@381: optind = 1; /* Don't scan ARGV[0], the program name. */ groke6@381: optstring = _getopt_initialize (argc, argv, optstring); groke6@381: __getopt_initialized = 1; groke6@381: } groke6@381: groke6@381: /* Test whether ARGV[optind] points to a non-option argument. groke6@381: Either it does not have option syntax, or there is an environment flag groke6@381: from the shell indicating it is not an option. The later information groke6@381: is only used when the used in the GNU libc. */ groke6@381: #if defined _LIBC && defined USE_NONOPTION_FLAGS groke6@381: # define NONOPTION_P (argv[optind][0] != '-' || argv[optind][1] == '\0' \ groke6@381: || (optind < nonoption_flags_len \ groke6@381: && __getopt_nonoption_flags[optind] == '1')) groke6@381: #else groke6@381: # define NONOPTION_P (argv[optind][0] != '-' || argv[optind][1] == '\0') groke6@381: #endif groke6@381: groke6@381: if (nextchar == NULL || *nextchar == '\0') groke6@381: { groke6@381: /* Advance to the next ARGV-element. */ groke6@381: groke6@381: /* Give FIRST_NONOPT and LAST_NONOPT rational values if OPTIND has been groke6@381: moved back by the user (who may also have changed the arguments). */ groke6@381: if (last_nonopt > optind) groke6@381: last_nonopt = optind; groke6@381: if (first_nonopt > optind) groke6@381: first_nonopt = optind; groke6@381: groke6@381: if (ordering == PERMUTE) groke6@381: { groke6@381: /* If we have just processed some options following some non-options, groke6@381: exchange them so that the options come first. */ groke6@381: groke6@381: if (first_nonopt != last_nonopt && last_nonopt != optind) groke6@381: exchange ((char **) argv); groke6@381: else if (last_nonopt != optind) groke6@381: first_nonopt = optind; groke6@381: groke6@381: /* Skip any additional non-options groke6@381: and extend the range of non-options previously skipped. */ groke6@381: groke6@381: while (optind < argc && NONOPTION_P) groke6@381: optind++; groke6@381: last_nonopt = optind; groke6@381: } groke6@381: groke6@381: /* The special ARGV-element `--' means premature end of options. groke6@381: Skip it like a null option, groke6@381: then exchange with previous non-options as if it were an option, groke6@381: then skip everything else like a non-option. */ groke6@381: groke6@381: if (optind != argc && !strcmp (argv[optind], "--")) groke6@381: { groke6@381: optind++; groke6@381: groke6@381: if (first_nonopt != last_nonopt && last_nonopt != optind) groke6@381: exchange ((char **) argv); groke6@381: else if (first_nonopt == last_nonopt) groke6@381: first_nonopt = optind; groke6@381: last_nonopt = argc; groke6@381: groke6@381: optind = argc; groke6@381: } groke6@381: groke6@381: /* If we have done all the ARGV-elements, stop the scan groke6@381: and back over any non-options that we skipped and permuted. */ groke6@381: groke6@381: if (optind == argc) groke6@381: { groke6@381: /* Set the next-arg-index to point at the non-options groke6@381: that we previously skipped, so the caller will digest them. */ groke6@381: if (first_nonopt != last_nonopt) groke6@381: optind = first_nonopt; groke6@381: return -1; groke6@381: } groke6@381: groke6@381: /* If we have come to a non-option and did not permute it, groke6@381: either stop the scan or describe it to the caller and pass it by. */ groke6@381: groke6@381: if (NONOPTION_P) groke6@381: { groke6@381: if (ordering == REQUIRE_ORDER) groke6@381: return -1; groke6@381: optarg = argv[optind++]; groke6@381: return 1; groke6@381: } groke6@381: groke6@381: /* We have found another option-ARGV-element. groke6@381: Skip the initial punctuation. */ groke6@381: groke6@381: nextchar = (argv[optind] + 1 groke6@381: + (longopts != NULL && argv[optind][1] == '-')); groke6@381: } groke6@381: groke6@381: /* Decode the current option-ARGV-element. */ groke6@381: groke6@381: /* Check whether the ARGV-element is a long option. groke6@381: groke6@381: If long_only and the ARGV-element has the form "-f", where f is groke6@381: a valid short option, don't consider it an abbreviated form of groke6@381: a long option that starts with f. Otherwise there would be no groke6@381: way to give the -f short option. groke6@381: groke6@381: On the other hand, if there's a long option "fubar" and groke6@381: the ARGV-element is "-fu", do consider that an abbreviation of groke6@381: the long option, just like "--fu", and not "-f" with arg "u". groke6@381: groke6@381: This distinction seems to be the most useful approach. */ groke6@381: groke6@381: if (longopts != NULL groke6@381: && (argv[optind][1] == '-' groke6@381: || (long_only && (argv[optind][2] || !my_index (optstring, argv[optind][1]))))) groke6@381: { groke6@381: char *nameend; groke6@381: const struct option *p; groke6@381: const struct option *pfound = NULL; groke6@381: int exact = 0; groke6@381: int ambig = 0; groke6@381: int indfound = -1; groke6@381: int option_index; groke6@381: groke6@381: for (nameend = nextchar; *nameend && *nameend != '='; nameend++) groke6@381: /* Do nothing. */ ; groke6@381: groke6@381: /* Test all long options for either exact match groke6@381: or abbreviated matches. */ groke6@381: for (p = longopts, option_index = 0; p->name; p++, option_index++) groke6@381: if (!strncmp (p->name, nextchar, nameend - nextchar)) groke6@381: { groke6@381: if ((unsigned int) (nameend - nextchar) groke6@381: == (unsigned int) strlen (p->name)) groke6@381: { groke6@381: /* Exact match found. */ groke6@381: pfound = p; groke6@381: indfound = option_index; groke6@381: exact = 1; groke6@381: break; groke6@381: } groke6@381: else if (pfound == NULL) groke6@381: { groke6@381: /* First nonexact match found. */ groke6@381: pfound = p; groke6@381: indfound = option_index; groke6@381: } groke6@381: else if (long_only groke6@381: || pfound->has_arg != p->has_arg groke6@381: || pfound->flag != p->flag groke6@381: || pfound->val != p->val) groke6@381: /* Second or later nonexact match found. */ groke6@381: ambig = 1; groke6@381: } groke6@381: groke6@381: if (ambig && !exact) groke6@381: { groke6@381: if (print_errors) groke6@381: { groke6@381: #if defined _LIBC && defined USE_IN_LIBIO groke6@381: char *buf; groke6@381: groke6@381: __asprintf (&buf, _("%s: option `%s' is ambiguous\n"), groke6@381: argv[0], argv[optind]); groke6@381: groke6@381: if (_IO_fwide (stderr, 0) > 0) groke6@381: __fwprintf (stderr, L"%s", buf); groke6@381: else groke6@381: fputs (buf, stderr); groke6@381: groke6@381: free (buf); groke6@381: #else groke6@381: fprintf (stderr, _("%s: option `%s' is ambiguous\n"), groke6@381: argv[0], argv[optind]); groke6@381: #endif groke6@381: } groke6@381: nextchar += strlen (nextchar); groke6@381: optind++; groke6@381: optopt = 0; groke6@381: return '?'; groke6@381: } groke6@381: groke6@381: if (pfound != NULL) groke6@381: { groke6@381: option_index = indfound; groke6@381: optind++; groke6@381: if (*nameend) groke6@381: { groke6@381: /* Don't test has_arg with >, because some C compilers don't groke6@381: allow it to be used on enums. */ groke6@381: if (pfound->has_arg) groke6@381: optarg = nameend + 1; groke6@381: else groke6@381: { groke6@381: if (print_errors) groke6@381: { groke6@381: #if defined _LIBC && defined USE_IN_LIBIO groke6@381: char *buf; groke6@381: #endif groke6@381: groke6@381: if (argv[optind - 1][1] == '-') groke6@381: { groke6@381: /* --option */ groke6@381: #if defined _LIBC && defined USE_IN_LIBIO groke6@381: __asprintf (&buf, _("\ groke6@381: %s: option `--%s' doesn't allow an argument\n"), groke6@381: argv[0], pfound->name); groke6@381: #else groke6@381: fprintf (stderr, _("\ groke6@381: %s: option `--%s' doesn't allow an argument\n"), groke6@381: argv[0], pfound->name); groke6@381: #endif groke6@381: } groke6@381: else groke6@381: { groke6@381: /* +option or -option */ groke6@381: #if defined _LIBC && defined USE_IN_LIBIO groke6@381: __asprintf (&buf, _("\ groke6@381: %s: option `%c%s' doesn't allow an argument\n"), groke6@381: argv[0], argv[optind - 1][0], groke6@381: pfound->name); groke6@381: #else groke6@381: fprintf (stderr, _("\ groke6@381: %s: option `%c%s' doesn't allow an argument\n"), groke6@381: argv[0], argv[optind - 1][0], pfound->name); groke6@381: #endif groke6@381: } groke6@381: groke6@381: #if defined _LIBC && defined USE_IN_LIBIO groke6@381: if (_IO_fwide (stderr, 0) > 0) groke6@381: __fwprintf (stderr, L"%s", buf); groke6@381: else groke6@381: fputs (buf, stderr); groke6@381: groke6@381: free (buf); groke6@381: #endif groke6@381: } groke6@381: groke6@381: nextchar += strlen (nextchar); groke6@381: groke6@381: optopt = pfound->val; groke6@381: return '?'; groke6@381: } groke6@381: } groke6@381: else if (pfound->has_arg == 1) groke6@381: { groke6@381: if (optind < argc) groke6@381: optarg = argv[optind++]; groke6@381: else groke6@381: { groke6@381: if (print_errors) groke6@381: { groke6@381: #if defined _LIBC && defined USE_IN_LIBIO groke6@381: char *buf; groke6@381: groke6@381: __asprintf (&buf, groke6@381: _("%s: option `%s' requires an argument\n"), groke6@381: argv[0], argv[optind - 1]); groke6@381: groke6@381: if (_IO_fwide (stderr, 0) > 0) groke6@381: __fwprintf (stderr, L"%s", buf); groke6@381: else groke6@381: fputs (buf, stderr); groke6@381: groke6@381: free (buf); groke6@381: #else groke6@381: fprintf (stderr, groke6@381: _("%s: option `%s' requires an argument\n"), groke6@381: argv[0], argv[optind - 1]); groke6@381: #endif groke6@381: } groke6@381: nextchar += strlen (nextchar); groke6@381: optopt = pfound->val; groke6@381: return optstring[0] == ':' ? ':' : '?'; groke6@381: } groke6@381: } groke6@381: nextchar += strlen (nextchar); groke6@381: if (longind != NULL) groke6@381: *longind = option_index; groke6@381: if (pfound->flag) groke6@381: { groke6@381: *(pfound->flag) = pfound->val; groke6@381: return 0; groke6@381: } groke6@381: return pfound->val; groke6@381: } groke6@381: groke6@381: /* Can't find it as a long option. If this is not getopt_long_only, groke6@381: or the option starts with '--' or is not a valid short groke6@381: option, then it's an error. groke6@381: Otherwise interpret it as a short option. */ groke6@381: if (!long_only || argv[optind][1] == '-' groke6@381: || my_index (optstring, *nextchar) == NULL) groke6@381: { groke6@381: if (print_errors) groke6@381: { groke6@381: #if defined _LIBC && defined USE_IN_LIBIO groke6@381: char *buf; groke6@381: #endif groke6@381: groke6@381: if (argv[optind][1] == '-') groke6@381: { groke6@381: /* --option */ groke6@381: #if defined _LIBC && defined USE_IN_LIBIO groke6@381: __asprintf (&buf, _("%s: unrecognized option `--%s'\n"), groke6@381: argv[0], nextchar); groke6@381: #else groke6@381: fprintf (stderr, _("%s: unrecognized option `--%s'\n"), groke6@381: argv[0], nextchar); groke6@381: #endif groke6@381: } groke6@381: else groke6@381: { groke6@381: /* +option or -option */ groke6@381: #if defined _LIBC && defined USE_IN_LIBIO groke6@381: __asprintf (&buf, _("%s: unrecognized option `%c%s'\n"), groke6@381: argv[0], argv[optind][0], nextchar); groke6@381: #else groke6@381: fprintf (stderr, _("%s: unrecognized option `%c%s'\n"), groke6@381: argv[0], argv[optind][0], nextchar); groke6@381: #endif groke6@381: } groke6@381: groke6@381: #if defined _LIBC && defined USE_IN_LIBIO groke6@381: if (_IO_fwide (stderr, 0) > 0) groke6@381: __fwprintf (stderr, L"%s", buf); groke6@381: else groke6@381: fputs (buf, stderr); groke6@381: groke6@381: free (buf); groke6@381: #endif groke6@381: } groke6@381: nextchar = (char *) ""; groke6@381: optind++; groke6@381: optopt = 0; groke6@381: return '?'; groke6@381: } groke6@381: } groke6@381: groke6@381: /* Look at and handle the next short option-character. */ groke6@381: groke6@381: { groke6@381: char c = *nextchar++; groke6@381: char *temp = my_index (optstring, c); groke6@381: groke6@381: /* Increment `optind' when we start to process its last character. */ groke6@381: if (*nextchar == '\0') groke6@381: ++optind; groke6@381: groke6@381: if (temp == NULL || c == ':') groke6@381: { groke6@381: if (print_errors) groke6@381: { groke6@381: #if defined _LIBC && defined USE_IN_LIBIO groke6@381: char *buf; groke6@381: #endif groke6@381: groke6@381: if (posixly_correct) groke6@381: { groke6@381: /* 1003.2 specifies the format of this message. */ groke6@381: #if defined _LIBC && defined USE_IN_LIBIO groke6@381: __asprintf (&buf, _("%s: illegal option -- %c\n"), groke6@381: argv[0], c); groke6@381: #else groke6@381: fprintf (stderr, _("%s: illegal option -- %c\n"), argv[0], c); groke6@381: #endif groke6@381: } groke6@381: else groke6@381: { groke6@381: #if defined _LIBC && defined USE_IN_LIBIO groke6@381: __asprintf (&buf, _("%s: invalid option -- %c\n"), groke6@381: argv[0], c); groke6@381: #else groke6@381: fprintf (stderr, _("%s: invalid option -- %c\n"), argv[0], c); groke6@381: #endif groke6@381: } groke6@381: groke6@381: #if defined _LIBC && defined USE_IN_LIBIO groke6@381: if (_IO_fwide (stderr, 0) > 0) groke6@381: __fwprintf (stderr, L"%s", buf); groke6@381: else groke6@381: fputs (buf, stderr); groke6@381: groke6@381: free (buf); groke6@381: #endif groke6@381: } groke6@381: optopt = c; groke6@381: return '?'; groke6@381: } groke6@381: /* Convenience. Treat POSIX -W foo same as long option --foo */ groke6@381: if (temp[0] == 'W' && temp[1] == ';') groke6@381: { groke6@381: char *nameend; groke6@381: const struct option *p; groke6@381: const struct option *pfound = NULL; groke6@381: int exact = 0; groke6@381: int ambig = 0; groke6@381: int indfound = 0; groke6@381: int option_index; groke6@381: groke6@381: /* This is an option that requires an argument. */ groke6@381: if (*nextchar != '\0') groke6@381: { groke6@381: optarg = nextchar; groke6@381: /* If we end this ARGV-element by taking the rest as an arg, groke6@381: we must advance to the next element now. */ groke6@381: optind++; groke6@381: } groke6@381: else if (optind == argc) groke6@381: { groke6@381: if (print_errors) groke6@381: { groke6@381: /* 1003.2 specifies the format of this message. */ groke6@381: #if defined _LIBC && defined USE_IN_LIBIO groke6@381: char *buf; groke6@381: groke6@381: __asprintf (&buf, _("%s: option requires an argument -- %c\n"), groke6@381: argv[0], c); groke6@381: groke6@381: if (_IO_fwide (stderr, 0) > 0) groke6@381: __fwprintf (stderr, L"%s", buf); groke6@381: else groke6@381: fputs (buf, stderr); groke6@381: groke6@381: free (buf); groke6@381: #else groke6@381: fprintf (stderr, _("%s: option requires an argument -- %c\n"), groke6@381: argv[0], c); groke6@381: #endif groke6@381: } groke6@381: optopt = c; groke6@381: if (optstring[0] == ':') groke6@381: c = ':'; groke6@381: else groke6@381: c = '?'; groke6@381: return c; groke6@381: } groke6@381: else groke6@381: /* We already incremented `optind' once; groke6@381: increment it again when taking next ARGV-elt as argument. */ groke6@381: optarg = argv[optind++]; groke6@381: groke6@381: /* optarg is now the argument, see if it's in the groke6@381: table of longopts. */ groke6@381: groke6@381: for (nextchar = nameend = optarg; *nameend && *nameend != '='; nameend++) groke6@381: /* Do nothing. */ ; groke6@381: groke6@381: /* Test all long options for either exact match groke6@381: or abbreviated matches. */ groke6@381: for (p = longopts, option_index = 0; p->name; p++, option_index++) groke6@381: if (!strncmp (p->name, nextchar, nameend - nextchar)) groke6@381: { groke6@381: if ((unsigned int) (nameend - nextchar) == strlen (p->name)) groke6@381: { groke6@381: /* Exact match found. */ groke6@381: pfound = p; groke6@381: indfound = option_index; groke6@381: exact = 1; groke6@381: break; groke6@381: } groke6@381: else if (pfound == NULL) groke6@381: { groke6@381: /* First nonexact match found. */ groke6@381: pfound = p; groke6@381: indfound = option_index; groke6@381: } groke6@381: else groke6@381: /* Second or later nonexact match found. */ groke6@381: ambig = 1; groke6@381: } groke6@381: if (ambig && !exact) groke6@381: { groke6@381: if (print_errors) groke6@381: { groke6@381: #if defined _LIBC && defined USE_IN_LIBIO groke6@381: char *buf; groke6@381: groke6@381: __asprintf (&buf, _("%s: option `-W %s' is ambiguous\n"), groke6@381: argv[0], argv[optind]); groke6@381: groke6@381: if (_IO_fwide (stderr, 0) > 0) groke6@381: __fwprintf (stderr, L"%s", buf); groke6@381: else groke6@381: fputs (buf, stderr); groke6@381: groke6@381: free (buf); groke6@381: #else groke6@381: fprintf (stderr, _("%s: option `-W %s' is ambiguous\n"), groke6@381: argv[0], argv[optind]); groke6@381: #endif groke6@381: } groke6@381: nextchar += strlen (nextchar); groke6@381: optind++; groke6@381: return '?'; groke6@381: } groke6@381: if (pfound != NULL) groke6@381: { groke6@381: option_index = indfound; groke6@381: if (*nameend) groke6@381: { groke6@381: /* Don't test has_arg with >, because some C compilers don't groke6@381: allow it to be used on enums. */ groke6@381: if (pfound->has_arg) groke6@381: optarg = nameend + 1; groke6@381: else groke6@381: { groke6@381: if (print_errors) groke6@381: { groke6@381: #if defined _LIBC && defined USE_IN_LIBIO groke6@381: char *buf; groke6@381: groke6@381: __asprintf (&buf, _("\ groke6@381: %s: option `-W %s' doesn't allow an argument\n"), groke6@381: argv[0], pfound->name); groke6@381: groke6@381: if (_IO_fwide (stderr, 0) > 0) groke6@381: __fwprintf (stderr, L"%s", buf); groke6@381: else groke6@381: fputs (buf, stderr); groke6@381: groke6@381: free (buf); groke6@381: #else groke6@381: fprintf (stderr, _("\ groke6@381: %s: option `-W %s' doesn't allow an argument\n"), groke6@381: argv[0], pfound->name); groke6@381: #endif groke6@381: } groke6@381: groke6@381: nextchar += strlen (nextchar); groke6@381: return '?'; groke6@381: } groke6@381: } groke6@381: else if (pfound->has_arg == 1) groke6@381: { groke6@381: if (optind < argc) groke6@381: optarg = argv[optind++]; groke6@381: else groke6@381: { groke6@381: if (print_errors) groke6@381: { groke6@381: #if defined _LIBC && defined USE_IN_LIBIO groke6@381: char *buf; groke6@381: groke6@381: __asprintf (&buf, _("\ groke6@381: %s: option `%s' requires an argument\n"), groke6@381: argv[0], argv[optind - 1]); groke6@381: groke6@381: if (_IO_fwide (stderr, 0) > 0) groke6@381: __fwprintf (stderr, L"%s", buf); groke6@381: else groke6@381: fputs (buf, stderr); groke6@381: groke6@381: free (buf); groke6@381: #else groke6@381: fprintf (stderr, groke6@381: _("%s: option `%s' requires an argument\n"), groke6@381: argv[0], argv[optind - 1]); groke6@381: #endif groke6@381: } groke6@381: nextchar += strlen (nextchar); groke6@381: return optstring[0] == ':' ? ':' : '?'; groke6@381: } groke6@381: } groke6@381: nextchar += strlen (nextchar); groke6@381: if (longind != NULL) groke6@381: *longind = option_index; groke6@381: if (pfound->flag) groke6@381: { groke6@381: *(pfound->flag) = pfound->val; groke6@381: return 0; groke6@381: } groke6@381: return pfound->val; groke6@381: } groke6@381: nextchar = NULL; groke6@381: return 'W'; /* Let the application handle it. */ groke6@381: } groke6@381: if (temp[1] == ':') groke6@381: { groke6@381: if (temp[2] == ':') groke6@381: { groke6@381: /* This is an option that accepts an argument optionally. */ groke6@381: if (*nextchar != '\0') groke6@381: { groke6@381: optarg = nextchar; groke6@381: optind++; groke6@381: } groke6@381: else groke6@381: optarg = NULL; groke6@381: nextchar = NULL; groke6@381: } groke6@381: else groke6@381: { groke6@381: /* This is an option that requires an argument. */ groke6@381: if (*nextchar != '\0') groke6@381: { groke6@381: optarg = nextchar; groke6@381: /* If we end this ARGV-element by taking the rest as an arg, groke6@381: we must advance to the next element now. */ groke6@381: optind++; groke6@381: } groke6@381: else if (optind == argc) groke6@381: { groke6@381: if (print_errors) groke6@381: { groke6@381: /* 1003.2 specifies the format of this message. */ groke6@381: #if defined _LIBC && defined USE_IN_LIBIO groke6@381: char *buf; groke6@381: groke6@381: __asprintf (&buf, groke6@381: _("%s: option requires an argument -- %c\n"), groke6@381: argv[0], c); groke6@381: groke6@381: if (_IO_fwide (stderr, 0) > 0) groke6@381: __fwprintf (stderr, L"%s", buf); groke6@381: else groke6@381: fputs (buf, stderr); groke6@381: groke6@381: free (buf); groke6@381: #else groke6@381: fprintf (stderr, groke6@381: _("%s: option requires an argument -- %c\n"), groke6@381: argv[0], c); groke6@381: #endif groke6@381: } groke6@381: optopt = c; groke6@381: if (optstring[0] == ':') groke6@381: c = ':'; groke6@381: else groke6@381: c = '?'; groke6@381: } groke6@381: else groke6@381: /* We already incremented `optind' once; groke6@381: increment it again when taking next ARGV-elt as argument. */ groke6@381: optarg = argv[optind++]; groke6@381: nextchar = NULL; groke6@381: } groke6@381: } groke6@381: return c; groke6@381: } groke6@381: } groke6@381: groke6@381: int groke6@381: getopt (argc, argv, optstring) groke6@381: int argc; groke6@381: char *const *argv; groke6@381: const char *optstring; groke6@381: { groke6@381: return _getopt_internal (argc, argv, optstring, groke6@381: (const struct option *) 0, groke6@381: (int *) 0, groke6@381: 0); groke6@381: } groke6@381: groke6@381: #endif /* Not ELIDE_CODE. */ groke6@381: groke6@381: groke6@381: /* Compile with -DTEST to make an executable for use in testing groke6@381: the above definition of `getopt'. */ groke6@381: groke6@381: /* #define TEST */ /* Pete Wilson mod 7/28/02 */ groke6@381: #ifdef TEST groke6@381: groke6@381: #ifndef exit /* Pete Wilson mod 7/28/02 */ groke6@381: int exit(int); /* Pete Wilson mod 7/28/02 */ groke6@381: #endif /* Pete Wilson mod 7/28/02 */ groke6@381: groke6@381: int groke6@381: main (argc, argv) groke6@381: int argc; groke6@381: char **argv; groke6@381: { groke6@381: int c; groke6@381: int digit_optind = 0; groke6@381: groke6@381: while (1) groke6@381: { groke6@381: int this_option_optind = optind ? optind : 1; groke6@381: groke6@381: c = getopt (argc, argv, "abc:d:0123456789"); groke6@381: if (c == -1) groke6@381: break; groke6@381: groke6@381: switch (c) groke6@381: { groke6@381: case '0': groke6@381: case '1': groke6@381: case '2': groke6@381: case '3': groke6@381: case '4': groke6@381: case '5': groke6@381: case '6': groke6@381: case '7': groke6@381: case '8': groke6@381: case '9': groke6@381: if (digit_optind != 0 && digit_optind != this_option_optind) groke6@381: printf ("digits occur in two different argv-elements.\n"); groke6@381: digit_optind = this_option_optind; groke6@381: printf ("option %c\n", c); groke6@381: break; groke6@381: groke6@381: case 'a': groke6@381: printf ("option a\n"); groke6@381: break; groke6@381: groke6@381: case 'b': groke6@381: printf ("option b\n"); groke6@381: break; groke6@381: groke6@381: case 'c': groke6@381: printf ("option c with value `%s'\n", optarg); groke6@381: break; groke6@381: groke6@381: case '?': groke6@381: break; groke6@381: groke6@381: default: groke6@381: printf ("?? getopt returned character code 0%o ??\n", c); groke6@381: } groke6@381: } groke6@381: groke6@381: if (optind < argc) groke6@381: { groke6@381: printf ("non-option ARGV-elements: "); groke6@381: while (optind < argc) groke6@381: printf ("%s ", argv[optind++]); groke6@381: printf ("\n"); groke6@381: } groke6@381: groke6@381: exit (0); groke6@381: } groke6@381: groke6@381: #endif /* TEST */