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