2N/A/* Getopt for GNU.
2N/A NOTE: getopt is part of the C library, so if you don't know what
2N/A "Keep this file name-space clean" means, talk to drepper@gnu.org
2N/A before changing it!
2N/A Copyright (C) 1987-1996, 1998-2004, 2006, 2008-2010 Free Software
2N/A Foundation, Inc.
2N/A This file is part of the GNU C Library.
2N/A
2N/A This program is free software: you can redistribute it and/or modify
2N/A it under the terms of the GNU General Public License as published by
2N/A the Free Software Foundation; either version 3 of the License, or
2N/A (at your option) any later version.
2N/A
2N/A This program is distributed in the hope that it will be useful,
2N/A but WITHOUT ANY WARRANTY; without even the implied warranty of
2N/A MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
2N/A GNU General Public License for more details.
2N/A
2N/A You should have received a copy of the GNU General Public License
2N/A along with this program. If not, see <http://www.gnu.org/licenses/>. */
2N/A
2N/A#ifndef _LIBC
2N/A# include <config.h>
2N/A#endif
2N/A
2N/A#include "getopt.h"
2N/A
2N/A#include <stdio.h>
2N/A#include <stdlib.h>
2N/A#include <string.h>
2N/A#include <unistd.h>
2N/A
2N/A#ifdef _LIBC
2N/A# include <libintl.h>
2N/A#else
2N/A# include "gettext.h"
2N/A# define _(msgid) gettext (msgid)
2N/A#endif
2N/A
2N/A#if defined _LIBC && defined USE_IN_LIBIO
2N/A# include <wchar.h>
2N/A#endif
2N/A
2N/A/* This version of `getopt' appears to the caller like standard Unix `getopt'
2N/A but it behaves differently for the user, since it allows the user
2N/A to intersperse the options with the other arguments.
2N/A
2N/A As `getopt_long' works, it permutes the elements of ARGV so that,
2N/A when it is done, all the options precede everything else. Thus
2N/A all application programs are extended to handle flexible argument order.
2N/A
2N/A Using `getopt' or setting the environment variable POSIXLY_CORRECT
2N/A disables permutation.
2N/A Then the behavior is completely standard.
2N/A
2N/A GNU application programs can use a third alternative mode in which
2N/A they can distinguish the relative order of options and other arguments. */
2N/A
2N/A#include "getopt_int.h"
2N/A
2N/A/* For communication from `getopt' to the caller.
2N/A When `getopt' finds an option that takes an argument,
2N/A the argument value is returned here.
2N/A Also, when `ordering' is RETURN_IN_ORDER,
2N/A each non-option ARGV-element is returned here. */
2N/A
2N/Achar *optarg;
2N/A
2N/A/* Index in ARGV of the next element to be scanned.
2N/A This is used for communication to and from the caller
2N/A and for communication between successive calls to `getopt'.
2N/A
2N/A On entry to `getopt', zero means this is the first call; initialize.
2N/A
2N/A When `getopt' returns -1, this is the index of the first of the
2N/A non-option elements that the caller should itself scan.
2N/A
2N/A Otherwise, `optind' communicates from one call to the next
2N/A how much of ARGV has been scanned so far. */
2N/A
2N/A/* 1003.2 says this must be 1 before any call. */
2N/Aint optind = 1;
2N/A
2N/A/* Callers store zero here to inhibit the error message
2N/A for unrecognized options. */
2N/A
2N/Aint opterr = 1;
2N/A
2N/A/* Set to an option character which was unrecognized.
2N/A This must be initialized on some systems to avoid linking in the
2N/A system's own getopt implementation. */
2N/A
2N/Aint optopt = '?';
2N/A
2N/A/* Keep a global copy of all internal members of getopt_data. */
2N/A
2N/Astatic struct _getopt_data getopt_data;
2N/A
2N/A
2N/A#if defined HAVE_DECL_GETENV && !HAVE_DECL_GETENV
2N/Aextern char *getenv ();
2N/A#endif
2N/A
2N/A#ifdef _LIBC
2N/A/* Stored original parameters.
2N/A XXX This is no good solution. We should rather copy the args so
2N/A that we can compare them later. But we must not use malloc(3). */
2N/Aextern int __libc_argc;
2N/Aextern char **__libc_argv;
2N/A
2N/A/* Bash 2.0 gives us an environment variable containing flags
2N/A indicating ARGV elements that should not be considered arguments. */
2N/A
2N/A# ifdef USE_NONOPTION_FLAGS
2N/A/* Defined in getopt_init.c */
2N/Aextern char *__getopt_nonoption_flags;
2N/A# endif
2N/A
2N/A# ifdef USE_NONOPTION_FLAGS
2N/A# define SWAP_FLAGS(ch1, ch2) \
2N/A if (d->__nonoption_flags_len > 0) \
2N/A { \
2N/A char __tmp = __getopt_nonoption_flags[ch1]; \
2N/A __getopt_nonoption_flags[ch1] = __getopt_nonoption_flags[ch2]; \
2N/A __getopt_nonoption_flags[ch2] = __tmp; \
2N/A }
2N/A# else
2N/A# define SWAP_FLAGS(ch1, ch2)
2N/A# endif
2N/A#else /* !_LIBC */
2N/A# define SWAP_FLAGS(ch1, ch2)
2N/A#endif /* _LIBC */
2N/A
2N/A/* Exchange two adjacent subsequences of ARGV.
2N/A One subsequence is elements [first_nonopt,last_nonopt)
2N/A which contains all the non-options that have been skipped so far.
2N/A The other is elements [last_nonopt,optind), which contains all
2N/A the options processed since those non-options were skipped.
2N/A
2N/A `first_nonopt' and `last_nonopt' are relocated so that they describe
2N/A the new indices of the non-options in ARGV after they are moved. */
2N/A
2N/Astatic void
2N/Aexchange (char **argv, struct _getopt_data *d)
2N/A{
2N/A int bottom = d->__first_nonopt;
2N/A int middle = d->__last_nonopt;
2N/A int top = d->optind;
2N/A char *tem;
2N/A
2N/A /* Exchange the shorter segment with the far end of the longer segment.
2N/A That puts the shorter segment into the right place.
2N/A It leaves the longer segment in the right place overall,
2N/A but it consists of two parts that need to be swapped next. */
2N/A
2N/A#if defined _LIBC && defined USE_NONOPTION_FLAGS
2N/A /* First make sure the handling of the `__getopt_nonoption_flags'
2N/A string can work normally. Our top argument must be in the range
2N/A of the string. */
2N/A if (d->__nonoption_flags_len > 0 && top >= d->__nonoption_flags_max_len)
2N/A {
2N/A /* We must extend the array. The user plays games with us and
2N/A presents new arguments. */
2N/A char *new_str = malloc (top + 1);
2N/A if (new_str == NULL)
2N/A d->__nonoption_flags_len = d->__nonoption_flags_max_len = 0;
2N/A else
2N/A {
2N/A memset (__mempcpy (new_str, __getopt_nonoption_flags,
2N/A d->__nonoption_flags_max_len),
2N/A '\0', top + 1 - d->__nonoption_flags_max_len);
2N/A d->__nonoption_flags_max_len = top + 1;
2N/A __getopt_nonoption_flags = new_str;
2N/A }
2N/A }
2N/A#endif
2N/A
2N/A while (top > middle && middle > bottom)
2N/A {
2N/A if (top - middle > middle - bottom)
2N/A {
2N/A /* Bottom segment is the short one. */
2N/A int len = middle - bottom;
2N/A register int i;
2N/A
2N/A /* Swap it with the top part of the top segment. */
2N/A for (i = 0; i < len; i++)
2N/A {
2N/A tem = argv[bottom + i];
2N/A argv[bottom + i] = argv[top - (middle - bottom) + i];
2N/A argv[top - (middle - bottom) + i] = tem;
2N/A SWAP_FLAGS (bottom + i, top - (middle - bottom) + i);
2N/A }
2N/A /* Exclude the moved bottom segment from further swapping. */
2N/A top -= len;
2N/A }
2N/A else
2N/A {
2N/A /* Top segment is the short one. */
2N/A int len = top - middle;
2N/A register int i;
2N/A
2N/A /* Swap it with the bottom part of the bottom segment. */
2N/A for (i = 0; i < len; i++)
2N/A {
2N/A tem = argv[bottom + i];
2N/A argv[bottom + i] = argv[middle + i];
2N/A argv[middle + i] = tem;
2N/A SWAP_FLAGS (bottom + i, middle + i);
2N/A }
2N/A /* Exclude the moved top segment from further swapping. */
2N/A bottom += len;
2N/A }
2N/A }
2N/A
2N/A /* Update records for the slots the non-options now occupy. */
2N/A
2N/A d->__first_nonopt += (d->optind - d->__last_nonopt);
2N/A d->__last_nonopt = d->optind;
2N/A}
2N/A
2N/A/* Initialize the internal data when the first call is made. */
2N/A
2N/Astatic const char *
2N/A_getopt_initialize (int argc _GL_UNUSED,
2N/A char **argv _GL_UNUSED, const char *optstring,
2N/A struct _getopt_data *d, int posixly_correct)
2N/A{
2N/A /* Start processing options with ARGV-element 1 (since ARGV-element 0
2N/A is the program name); the sequence of previously skipped
2N/A non-option ARGV-elements is empty. */
2N/A
2N/A d->__first_nonopt = d->__last_nonopt = d->optind;
2N/A
2N/A d->__nextchar = NULL;
2N/A
2N/A d->__posixly_correct = posixly_correct || !!getenv ("POSIXLY_CORRECT");
2N/A
2N/A /* Determine how to handle the ordering of options and nonoptions. */
2N/A
2N/A if (optstring[0] == '-')
2N/A {
2N/A d->__ordering = RETURN_IN_ORDER;
2N/A ++optstring;
2N/A }
2N/A else if (optstring[0] == '+')
2N/A {
2N/A d->__ordering = REQUIRE_ORDER;
2N/A ++optstring;
2N/A }
2N/A else if (d->__posixly_correct)
2N/A d->__ordering = REQUIRE_ORDER;
2N/A else
2N/A d->__ordering = PERMUTE;
2N/A
2N/A#if defined _LIBC && defined USE_NONOPTION_FLAGS
2N/A if (!d->__posixly_correct
2N/A && argc == __libc_argc && argv == __libc_argv)
2N/A {
2N/A if (d->__nonoption_flags_max_len == 0)
2N/A {
2N/A if (__getopt_nonoption_flags == NULL
2N/A || __getopt_nonoption_flags[0] == '\0')
2N/A d->__nonoption_flags_max_len = -1;
2N/A else
2N/A {
2N/A const char *orig_str = __getopt_nonoption_flags;
2N/A int len = d->__nonoption_flags_max_len = strlen (orig_str);
2N/A if (d->__nonoption_flags_max_len < argc)
2N/A d->__nonoption_flags_max_len = argc;
2N/A __getopt_nonoption_flags =
2N/A (char *) malloc (d->__nonoption_flags_max_len);
2N/A if (__getopt_nonoption_flags == NULL)
2N/A d->__nonoption_flags_max_len = -1;
2N/A else
2N/A memset (__mempcpy (__getopt_nonoption_flags, orig_str, len),
2N/A '\0', d->__nonoption_flags_max_len - len);
2N/A }
2N/A }
2N/A d->__nonoption_flags_len = d->__nonoption_flags_max_len;
2N/A }
2N/A else
2N/A d->__nonoption_flags_len = 0;
2N/A#endif
2N/A
2N/A return optstring;
2N/A}
2N/A
2N/A/* Scan elements of ARGV (whose length is ARGC) for option characters
2N/A given in OPTSTRING.
2N/A
2N/A If an element of ARGV starts with '-', and is not exactly "-" or "--",
2N/A then it is an option element. The characters of this element
2N/A (aside from the initial '-') are option characters. If `getopt'
2N/A is called repeatedly, it returns successively each of the option characters
2N/A from each of the option elements.
2N/A
2N/A If `getopt' finds another option character, it returns that character,
2N/A updating `optind' and `nextchar' so that the next call to `getopt' can
2N/A resume the scan with the following option character or ARGV-element.
2N/A
2N/A If there are no more option characters, `getopt' returns -1.
2N/A Then `optind' is the index in ARGV of the first ARGV-element
2N/A that is not an option. (The ARGV-elements have been permuted
2N/A so that those that are not options now come last.)
2N/A
2N/A OPTSTRING is a string containing the legitimate option characters.
2N/A If an option character is seen that is not listed in OPTSTRING,
2N/A return '?' after printing an error message. If you set `opterr' to
2N/A zero, the error message is suppressed but we still return '?'.
2N/A
2N/A If a char in OPTSTRING is followed by a colon, that means it wants an arg,
2N/A so the following text in the same ARGV-element, or the text of the following
2N/A ARGV-element, is returned in `optarg'. Two colons mean an option that
2N/A wants an optional arg; if there is text in the current ARGV-element,
2N/A it is returned in `optarg', otherwise `optarg' is set to zero.
2N/A
2N/A If OPTSTRING starts with `-' or `+', it requests different methods of
2N/A handling the non-option ARGV-elements.
2N/A See the comments about RETURN_IN_ORDER and REQUIRE_ORDER, above.
2N/A
2N/A Long-named options begin with `--' instead of `-'.
2N/A Their names may be abbreviated as long as the abbreviation is unique
2N/A or is an exact match for some defined option. If they have an
2N/A argument, it follows the option name in the same ARGV-element, separated
2N/A from the option name by a `=', or else the in next ARGV-element.
2N/A When `getopt' finds a long-named option, it returns 0 if that option's
2N/A `flag' field is nonzero, the value of the option's `val' field
2N/A if the `flag' field is zero.
2N/A
2N/A The elements of ARGV aren't really const, because we permute them.
2N/A But we pretend they're const in the prototype to be compatible
2N/A with other systems.
2N/A
2N/A LONGOPTS is a vector of `struct option' terminated by an
2N/A element containing a name which is zero.
2N/A
2N/A LONGIND returns the index in LONGOPT of the long-named option found.
2N/A It is only valid when a long-named option has been found by the most
2N/A recent call.
2N/A
2N/A If LONG_ONLY is nonzero, '-' as well as '--' can introduce
2N/A long-named options. */
2N/A
2N/Aint
2N/A_getopt_internal_r (int argc, char **argv, const char *optstring,
2N/A const struct option *longopts, int *longind,
2N/A int long_only, struct _getopt_data *d, int posixly_correct)
2N/A{
2N/A int print_errors = d->opterr;
2N/A
2N/A if (argc < 1)
2N/A return -1;
2N/A
2N/A d->optarg = NULL;
2N/A
2N/A if (d->optind == 0 || !d->__initialized)
2N/A {
2N/A if (d->optind == 0)
2N/A d->optind = 1; /* Don't scan ARGV[0], the program name. */
2N/A optstring = _getopt_initialize (argc, argv, optstring, d,
2N/A posixly_correct);
2N/A d->__initialized = 1;
2N/A }
2N/A else if (optstring[0] == '-' || optstring[0] == '+')
2N/A optstring++;
2N/A if (optstring[0] == ':')
2N/A print_errors = 0;
2N/A
2N/A /* Test whether ARGV[optind] points to a non-option argument.
2N/A Either it does not have option syntax, or there is an environment flag
2N/A from the shell indicating it is not an option. The later information
2N/A is only used when the used in the GNU libc. */
2N/A#if defined _LIBC && defined USE_NONOPTION_FLAGS
2N/A# define NONOPTION_P (argv[d->optind][0] != '-' || argv[d->optind][1] == '\0' \
2N/A || (d->optind < d->__nonoption_flags_len \
2N/A && __getopt_nonoption_flags[d->optind] == '1'))
2N/A#else
2N/A# define NONOPTION_P (argv[d->optind][0] != '-' || argv[d->optind][1] == '\0')
2N/A#endif
2N/A
2N/A if (d->__nextchar == NULL || *d->__nextchar == '\0')
2N/A {
2N/A /* Advance to the next ARGV-element. */
2N/A
2N/A /* Give FIRST_NONOPT & LAST_NONOPT rational values if OPTIND has been
2N/A moved back by the user (who may also have changed the arguments). */
2N/A if (d->__last_nonopt > d->optind)
2N/A d->__last_nonopt = d->optind;
2N/A if (d->__first_nonopt > d->optind)
2N/A d->__first_nonopt = d->optind;
2N/A
2N/A if (d->__ordering == PERMUTE)
2N/A {
2N/A /* If we have just processed some options following some non-options,
2N/A exchange them so that the options come first. */
2N/A
2N/A if (d->__first_nonopt != d->__last_nonopt
2N/A && d->__last_nonopt != d->optind)
2N/A exchange ((char **) argv, d);
2N/A else if (d->__last_nonopt != d->optind)
2N/A d->__first_nonopt = d->optind;
2N/A
2N/A /* Skip any additional non-options
2N/A and extend the range of non-options previously skipped. */
2N/A
2N/A while (d->optind < argc && NONOPTION_P)
2N/A d->optind++;
2N/A d->__last_nonopt = d->optind;
2N/A }
2N/A
2N/A /* The special ARGV-element `--' means premature end of options.
2N/A Skip it like a null option,
2N/A then exchange with previous non-options as if it were an option,
2N/A then skip everything else like a non-option. */
2N/A
2N/A if (d->optind != argc && !strcmp (argv[d->optind], "--"))
2N/A {
2N/A d->optind++;
2N/A
2N/A if (d->__first_nonopt != d->__last_nonopt
2N/A && d->__last_nonopt != d->optind)
2N/A exchange ((char **) argv, d);
2N/A else if (d->__first_nonopt == d->__last_nonopt)
2N/A d->__first_nonopt = d->optind;
2N/A d->__last_nonopt = argc;
2N/A
2N/A d->optind = argc;
2N/A }
2N/A
2N/A /* If we have done all the ARGV-elements, stop the scan
2N/A and back over any non-options that we skipped and permuted. */
2N/A
2N/A if (d->optind == argc)
2N/A {
2N/A /* Set the next-arg-index to point at the non-options
2N/A that we previously skipped, so the caller will digest them. */
2N/A if (d->__first_nonopt != d->__last_nonopt)
2N/A d->optind = d->__first_nonopt;
2N/A return -1;
2N/A }
2N/A
2N/A /* If we have come to a non-option and did not permute it,
2N/A either stop the scan or describe it to the caller and pass it by. */
2N/A
2N/A if (NONOPTION_P)
2N/A {
2N/A if (d->__ordering == REQUIRE_ORDER)
2N/A return -1;
2N/A d->optarg = argv[d->optind++];
2N/A return 1;
2N/A }
2N/A
2N/A /* We have found another option-ARGV-element.
2N/A Skip the initial punctuation. */
2N/A
2N/A d->__nextchar = (argv[d->optind] + 1
2N/A + (longopts != NULL && argv[d->optind][1] == '-'));
2N/A }
2N/A
2N/A /* Decode the current option-ARGV-element. */
2N/A
2N/A /* Check whether the ARGV-element is a long option.
2N/A
2N/A If long_only and the ARGV-element has the form "-f", where f is
2N/A a valid short option, don't consider it an abbreviated form of
2N/A a long option that starts with f. Otherwise there would be no
2N/A way to give the -f short option.
2N/A
2N/A On the other hand, if there's a long option "fubar" and
2N/A the ARGV-element is "-fu", do consider that an abbreviation of
2N/A the long option, just like "--fu", and not "-f" with arg "u".
2N/A
2N/A This distinction seems to be the most useful approach. */
2N/A
2N/A if (longopts != NULL
2N/A && (argv[d->optind][1] == '-'
2N/A || (long_only && (argv[d->optind][2]
2N/A || !strchr (optstring, argv[d->optind][1])))))
2N/A {
2N/A char *nameend;
2N/A const struct option *p;
2N/A const struct option *pfound = NULL;
2N/A int exact = 0;
2N/A int ambig = 0;
2N/A int indfound = -1;
2N/A int option_index;
2N/A
2N/A for (nameend = d->__nextchar; *nameend && *nameend != '='; nameend++)
2N/A /* Do nothing. */ ;
2N/A
2N/A /* Test all long options for either exact match
2N/A or abbreviated matches. */
2N/A for (p = longopts, option_index = 0; p->name; p++, option_index++)
2N/A if (!strncmp (p->name, d->__nextchar, nameend - d->__nextchar))
2N/A {
2N/A if ((unsigned int) (nameend - d->__nextchar)
2N/A == (unsigned int) strlen (p->name))
2N/A {
2N/A /* Exact match found. */
2N/A pfound = p;
2N/A indfound = option_index;
2N/A exact = 1;
2N/A break;
2N/A }
2N/A else if (pfound == NULL)
2N/A {
2N/A /* First nonexact match found. */
2N/A pfound = p;
2N/A indfound = option_index;
2N/A }
2N/A else if (long_only
2N/A || pfound->has_arg != p->has_arg
2N/A || pfound->flag != p->flag
2N/A || pfound->val != p->val)
2N/A /* Second or later nonexact match found. */
2N/A ambig = 1;
2N/A }
2N/A
2N/A if (ambig && !exact)
2N/A {
2N/A if (print_errors)
2N/A {
2N/A#if defined _LIBC && defined USE_IN_LIBIO
2N/A char *buf;
2N/A
2N/A if (__asprintf (&buf, _("%s: option '%s' is ambiguous\n"),
2N/A argv[0], argv[d->optind]) >= 0)
2N/A {
2N/A _IO_flockfile (stderr);
2N/A
2N/A int old_flags2 = ((_IO_FILE *) stderr)->_flags2;
2N/A ((_IO_FILE *) stderr)->_flags2 |= _IO_FLAGS2_NOTCANCEL;
2N/A
2N/A __fxprintf (NULL, "%s", buf);
2N/A
2N/A ((_IO_FILE *) stderr)->_flags2 = old_flags2;
2N/A _IO_funlockfile (stderr);
2N/A
2N/A free (buf);
2N/A }
2N/A#else
2N/A fprintf (stderr, _("%s: option '%s' is ambiguous\n"),
2N/A argv[0], argv[d->optind]);
2N/A#endif
2N/A }
2N/A d->__nextchar += strlen (d->__nextchar);
2N/A d->optind++;
2N/A d->optopt = 0;
2N/A return '?';
2N/A }
2N/A
2N/A if (pfound != NULL)
2N/A {
2N/A option_index = indfound;
2N/A d->optind++;
2N/A if (*nameend)
2N/A {
2N/A /* Don't test has_arg with >, because some C compilers don't
2N/A allow it to be used on enums. */
2N/A if (pfound->has_arg)
2N/A d->optarg = nameend + 1;
2N/A else
2N/A {
2N/A if (print_errors)
2N/A {
2N/A#if defined _LIBC && defined USE_IN_LIBIO
2N/A char *buf;
2N/A int n;
2N/A#endif
2N/A
2N/A if (argv[d->optind - 1][1] == '-')
2N/A {
2N/A /* --option */
2N/A#if defined _LIBC && defined USE_IN_LIBIO
2N/A n = __asprintf (&buf, _("\
2N/A%s: option '--%s' doesn't allow an argument\n"),
2N/A argv[0], pfound->name);
2N/A#else
2N/A fprintf (stderr, _("\
2N/A%s: option '--%s' doesn't allow an argument\n"),
2N/A argv[0], pfound->name);
2N/A#endif
2N/A }
2N/A else
2N/A {
2N/A /* +option or -option */
2N/A#if defined _LIBC && defined USE_IN_LIBIO
2N/A n = __asprintf (&buf, _("\
2N/A%s: option '%c%s' doesn't allow an argument\n"),
2N/A argv[0], argv[d->optind - 1][0],
2N/A pfound->name);
2N/A#else
2N/A fprintf (stderr, _("\
2N/A%s: option '%c%s' doesn't allow an argument\n"),
2N/A argv[0], argv[d->optind - 1][0],
2N/A pfound->name);
2N/A#endif
2N/A }
2N/A
2N/A#if defined _LIBC && defined USE_IN_LIBIO
2N/A if (n >= 0)
2N/A {
2N/A _IO_flockfile (stderr);
2N/A
2N/A int old_flags2 = ((_IO_FILE *) stderr)->_flags2;
2N/A ((_IO_FILE *) stderr)->_flags2
2N/A |= _IO_FLAGS2_NOTCANCEL;
2N/A
2N/A __fxprintf (NULL, "%s", buf);
2N/A
2N/A ((_IO_FILE *) stderr)->_flags2 = old_flags2;
2N/A _IO_funlockfile (stderr);
2N/A
2N/A free (buf);
2N/A }
2N/A#endif
2N/A }
2N/A
2N/A d->__nextchar += strlen (d->__nextchar);
2N/A
2N/A d->optopt = pfound->val;
2N/A return '?';
2N/A }
2N/A }
2N/A else if (pfound->has_arg == 1)
2N/A {
2N/A if (d->optind < argc)
2N/A d->optarg = argv[d->optind++];
2N/A else
2N/A {
2N/A if (print_errors)
2N/A {
2N/A#if defined _LIBC && defined USE_IN_LIBIO
2N/A char *buf;
2N/A
2N/A if (__asprintf (&buf, _("\
2N/A%s: option '--%s' requires an argument\n"),
2N/A argv[0], pfound->name) >= 0)
2N/A {
2N/A _IO_flockfile (stderr);
2N/A
2N/A int old_flags2 = ((_IO_FILE *) stderr)->_flags2;
2N/A ((_IO_FILE *) stderr)->_flags2
2N/A |= _IO_FLAGS2_NOTCANCEL;
2N/A
2N/A __fxprintf (NULL, "%s", buf);
2N/A
2N/A ((_IO_FILE *) stderr)->_flags2 = old_flags2;
2N/A _IO_funlockfile (stderr);
2N/A
2N/A free (buf);
2N/A }
2N/A#else
2N/A fprintf (stderr,
2N/A _("%s: option '--%s' requires an argument\n"),
2N/A argv[0], pfound->name);
2N/A#endif
2N/A }
2N/A d->__nextchar += strlen (d->__nextchar);
2N/A d->optopt = pfound->val;
2N/A return optstring[0] == ':' ? ':' : '?';
2N/A }
2N/A }
2N/A d->__nextchar += strlen (d->__nextchar);
2N/A if (longind != NULL)
2N/A *longind = option_index;
2N/A if (pfound->flag)
2N/A {
2N/A *(pfound->flag) = pfound->val;
2N/A return 0;
2N/A }
2N/A return pfound->val;
2N/A }
2N/A
2N/A /* Can't find it as a long option. If this is not getopt_long_only,
2N/A or the option starts with '--' or is not a valid short
2N/A option, then it's an error.
2N/A Otherwise interpret it as a short option. */
2N/A if (!long_only || argv[d->optind][1] == '-'
2N/A || strchr (optstring, *d->__nextchar) == NULL)
2N/A {
2N/A if (print_errors)
2N/A {
2N/A#if defined _LIBC && defined USE_IN_LIBIO
2N/A char *buf;
2N/A int n;
2N/A#endif
2N/A
2N/A if (argv[d->optind][1] == '-')
2N/A {
2N/A /* --option */
2N/A#if defined _LIBC && defined USE_IN_LIBIO
2N/A n = __asprintf (&buf, _("%s: unrecognized option '--%s'\n"),
2N/A argv[0], d->__nextchar);
2N/A#else
2N/A fprintf (stderr, _("%s: unrecognized option '--%s'\n"),
2N/A argv[0], d->__nextchar);
2N/A#endif
2N/A }
2N/A else
2N/A {
2N/A /* +option or -option */
2N/A#if defined _LIBC && defined USE_IN_LIBIO
2N/A n = __asprintf (&buf, _("%s: unrecognized option '%c%s'\n"),
2N/A argv[0], argv[d->optind][0], d->__nextchar);
2N/A#else
2N/A fprintf (stderr, _("%s: unrecognized option '%c%s'\n"),
2N/A argv[0], argv[d->optind][0], d->__nextchar);
2N/A#endif
2N/A }
2N/A
2N/A#if defined _LIBC && defined USE_IN_LIBIO
2N/A if (n >= 0)
2N/A {
2N/A _IO_flockfile (stderr);
2N/A
2N/A int old_flags2 = ((_IO_FILE *) stderr)->_flags2;
2N/A ((_IO_FILE *) stderr)->_flags2 |= _IO_FLAGS2_NOTCANCEL;
2N/A
2N/A __fxprintf (NULL, "%s", buf);
2N/A
2N/A ((_IO_FILE *) stderr)->_flags2 = old_flags2;
2N/A _IO_funlockfile (stderr);
2N/A
2N/A free (buf);
2N/A }
2N/A#endif
2N/A }
2N/A d->__nextchar = (char *) "";
2N/A d->optind++;
2N/A d->optopt = 0;
2N/A return '?';
2N/A }
2N/A }
2N/A
2N/A /* Look at and handle the next short option-character. */
2N/A
2N/A {
2N/A char c = *d->__nextchar++;
2N/A const char *temp = strchr (optstring, c);
2N/A
2N/A /* Increment `optind' when we start to process its last character. */
2N/A if (*d->__nextchar == '\0')
2N/A ++d->optind;
2N/A
2N/A if (temp == NULL || c == ':' || c == ';')
2N/A {
2N/A if (print_errors)
2N/A {
2N/A#if defined _LIBC && defined USE_IN_LIBIO
2N/A char *buf;
2N/A int n;
2N/A#endif
2N/A
2N/A#if defined _LIBC && defined USE_IN_LIBIO
2N/A n = __asprintf (&buf, _("%s: invalid option -- '%c'\n"),
2N/A argv[0], c);
2N/A#else
2N/A fprintf (stderr, _("%s: invalid option -- '%c'\n"), argv[0], c);
2N/A#endif
2N/A
2N/A#if defined _LIBC && defined USE_IN_LIBIO
2N/A if (n >= 0)
2N/A {
2N/A _IO_flockfile (stderr);
2N/A
2N/A int old_flags2 = ((_IO_FILE *) stderr)->_flags2;
2N/A ((_IO_FILE *) stderr)->_flags2 |= _IO_FLAGS2_NOTCANCEL;
2N/A
2N/A __fxprintf (NULL, "%s", buf);
2N/A
2N/A ((_IO_FILE *) stderr)->_flags2 = old_flags2;
2N/A _IO_funlockfile (stderr);
2N/A
2N/A free (buf);
2N/A }
2N/A#endif
2N/A }
2N/A d->optopt = c;
2N/A return '?';
2N/A }
2N/A /* Convenience. Treat POSIX -W foo same as long option --foo */
2N/A if (temp[0] == 'W' && temp[1] == ';')
2N/A {
2N/A char *nameend;
2N/A const struct option *p;
2N/A const struct option *pfound = NULL;
2N/A int exact = 0;
2N/A int ambig = 0;
2N/A int indfound = 0;
2N/A int option_index;
2N/A
2N/A /* This is an option that requires an argument. */
2N/A if (*d->__nextchar != '\0')
2N/A {
2N/A d->optarg = d->__nextchar;
2N/A /* If we end this ARGV-element by taking the rest as an arg,
2N/A we must advance to the next element now. */
2N/A d->optind++;
2N/A }
2N/A else if (d->optind == argc)
2N/A {
2N/A if (print_errors)
2N/A {
2N/A#if defined _LIBC && defined USE_IN_LIBIO
2N/A char *buf;
2N/A
2N/A if (__asprintf (&buf,
2N/A _("%s: option requires an argument -- '%c'\n"),
2N/A argv[0], c) >= 0)
2N/A {
2N/A _IO_flockfile (stderr);
2N/A
2N/A int old_flags2 = ((_IO_FILE *) stderr)->_flags2;
2N/A ((_IO_FILE *) stderr)->_flags2 |= _IO_FLAGS2_NOTCANCEL;
2N/A
2N/A __fxprintf (NULL, "%s", buf);
2N/A
2N/A ((_IO_FILE *) stderr)->_flags2 = old_flags2;
2N/A _IO_funlockfile (stderr);
2N/A
2N/A free (buf);
2N/A }
2N/A#else
2N/A fprintf (stderr,
2N/A _("%s: option requires an argument -- '%c'\n"),
2N/A argv[0], c);
2N/A#endif
2N/A }
2N/A d->optopt = c;
2N/A if (optstring[0] == ':')
2N/A c = ':';
2N/A else
2N/A c = '?';
2N/A return c;
2N/A }
2N/A else
2N/A /* We already incremented `d->optind' once;
2N/A increment it again when taking next ARGV-elt as argument. */
2N/A d->optarg = argv[d->optind++];
2N/A
2N/A /* optarg is now the argument, see if it's in the
2N/A table of longopts. */
2N/A
2N/A for (d->__nextchar = nameend = d->optarg; *nameend && *nameend != '=';
2N/A nameend++)
2N/A /* Do nothing. */ ;
2N/A
2N/A /* Test all long options for either exact match
2N/A or abbreviated matches. */
2N/A for (p = longopts, option_index = 0; p->name; p++, option_index++)
2N/A if (!strncmp (p->name, d->__nextchar, nameend - d->__nextchar))
2N/A {
2N/A if ((unsigned int) (nameend - d->__nextchar) == strlen (p->name))
2N/A {
2N/A /* Exact match found. */
2N/A pfound = p;
2N/A indfound = option_index;
2N/A exact = 1;
2N/A break;
2N/A }
2N/A else if (pfound == NULL)
2N/A {
2N/A /* First nonexact match found. */
2N/A pfound = p;
2N/A indfound = option_index;
2N/A }
2N/A else if (long_only
2N/A || pfound->has_arg != p->has_arg
2N/A || pfound->flag != p->flag
2N/A || pfound->val != p->val)
2N/A /* Second or later nonexact match found. */
2N/A ambig = 1;
2N/A }
2N/A if (ambig && !exact)
2N/A {
2N/A if (print_errors)
2N/A {
2N/A#if defined _LIBC && defined USE_IN_LIBIO
2N/A char *buf;
2N/A
2N/A if (__asprintf (&buf, _("%s: option '-W %s' is ambiguous\n"),
2N/A argv[0], d->optarg) >= 0)
2N/A {
2N/A _IO_flockfile (stderr);
2N/A
2N/A int old_flags2 = ((_IO_FILE *) stderr)->_flags2;
2N/A ((_IO_FILE *) stderr)->_flags2 |= _IO_FLAGS2_NOTCANCEL;
2N/A
2N/A __fxprintf (NULL, "%s", buf);
2N/A
2N/A ((_IO_FILE *) stderr)->_flags2 = old_flags2;
2N/A _IO_funlockfile (stderr);
2N/A
2N/A free (buf);
2N/A }
2N/A#else
2N/A fprintf (stderr, _("%s: option '-W %s' is ambiguous\n"),
2N/A argv[0], d->optarg);
2N/A#endif
2N/A }
2N/A d->__nextchar += strlen (d->__nextchar);
2N/A d->optind++;
2N/A return '?';
2N/A }
2N/A if (pfound != NULL)
2N/A {
2N/A option_index = indfound;
2N/A if (*nameend)
2N/A {
2N/A /* Don't test has_arg with >, because some C compilers don't
2N/A allow it to be used on enums. */
2N/A if (pfound->has_arg)
2N/A d->optarg = nameend + 1;
2N/A else
2N/A {
2N/A if (print_errors)
2N/A {
2N/A#if defined _LIBC && defined USE_IN_LIBIO
2N/A char *buf;
2N/A
2N/A if (__asprintf (&buf, _("\
2N/A%s: option '-W %s' doesn't allow an argument\n"),
2N/A argv[0], pfound->name) >= 0)
2N/A {
2N/A _IO_flockfile (stderr);
2N/A
2N/A int old_flags2 = ((_IO_FILE *) stderr)->_flags2;
2N/A ((_IO_FILE *) stderr)->_flags2
2N/A |= _IO_FLAGS2_NOTCANCEL;
2N/A
2N/A __fxprintf (NULL, "%s", buf);
2N/A
2N/A ((_IO_FILE *) stderr)->_flags2 = old_flags2;
2N/A _IO_funlockfile (stderr);
2N/A
2N/A free (buf);
2N/A }
2N/A#else
2N/A fprintf (stderr, _("\
2N/A%s: option '-W %s' doesn't allow an argument\n"),
2N/A argv[0], pfound->name);
2N/A#endif
2N/A }
2N/A
2N/A d->__nextchar += strlen (d->__nextchar);
2N/A return '?';
2N/A }
2N/A }
2N/A else if (pfound->has_arg == 1)
2N/A {
2N/A if (d->optind < argc)
2N/A d->optarg = argv[d->optind++];
2N/A else
2N/A {
2N/A if (print_errors)
2N/A {
2N/A#if defined _LIBC && defined USE_IN_LIBIO
2N/A char *buf;
2N/A
2N/A if (__asprintf (&buf, _("\
2N/A%s: option '-W %s' requires an argument\n"),
2N/A argv[0], pfound->name) >= 0)
2N/A {
2N/A _IO_flockfile (stderr);
2N/A
2N/A int old_flags2 = ((_IO_FILE *) stderr)->_flags2;
2N/A ((_IO_FILE *) stderr)->_flags2
2N/A |= _IO_FLAGS2_NOTCANCEL;
2N/A
2N/A __fxprintf (NULL, "%s", buf);
2N/A
2N/A ((_IO_FILE *) stderr)->_flags2 = old_flags2;
2N/A _IO_funlockfile (stderr);
2N/A
2N/A free (buf);
2N/A }
2N/A#else
2N/A fprintf (stderr, _("\
2N/A%s: option '-W %s' requires an argument\n"),
2N/A argv[0], pfound->name);
2N/A#endif
2N/A }
2N/A d->__nextchar += strlen (d->__nextchar);
2N/A return optstring[0] == ':' ? ':' : '?';
2N/A }
2N/A }
2N/A else
2N/A d->optarg = NULL;
2N/A d->__nextchar += strlen (d->__nextchar);
2N/A if (longind != NULL)
2N/A *longind = option_index;
2N/A if (pfound->flag)
2N/A {
2N/A *(pfound->flag) = pfound->val;
2N/A return 0;
2N/A }
2N/A return pfound->val;
2N/A }
2N/A d->__nextchar = NULL;
2N/A return 'W'; /* Let the application handle it. */
2N/A }
2N/A if (temp[1] == ':')
2N/A {
2N/A if (temp[2] == ':')
2N/A {
2N/A /* This is an option that accepts an argument optionally. */
2N/A if (*d->__nextchar != '\0')
2N/A {
2N/A d->optarg = d->__nextchar;
2N/A d->optind++;
2N/A }
2N/A else
2N/A d->optarg = NULL;
2N/A d->__nextchar = NULL;
2N/A }
2N/A else
2N/A {
2N/A /* This is an option that requires an argument. */
2N/A if (*d->__nextchar != '\0')
2N/A {
2N/A d->optarg = d->__nextchar;
2N/A /* If we end this ARGV-element by taking the rest as an arg,
2N/A we must advance to the next element now. */
2N/A d->optind++;
2N/A }
2N/A else if (d->optind == argc)
2N/A {
2N/A if (print_errors)
2N/A {
2N/A#if defined _LIBC && defined USE_IN_LIBIO
2N/A char *buf;
2N/A
2N/A if (__asprintf (&buf, _("\
2N/A%s: option requires an argument -- '%c'\n"),
2N/A argv[0], c) >= 0)
2N/A {
2N/A _IO_flockfile (stderr);
2N/A
2N/A int old_flags2 = ((_IO_FILE *) stderr)->_flags2;
2N/A ((_IO_FILE *) stderr)->_flags2 |= _IO_FLAGS2_NOTCANCEL;
2N/A
2N/A __fxprintf (NULL, "%s", buf);
2N/A
2N/A ((_IO_FILE *) stderr)->_flags2 = old_flags2;
2N/A _IO_funlockfile (stderr);
2N/A
2N/A free (buf);
2N/A }
2N/A#else
2N/A fprintf (stderr,
2N/A _("%s: option requires an argument -- '%c'\n"),
2N/A argv[0], c);
2N/A#endif
2N/A }
2N/A d->optopt = c;
2N/A if (optstring[0] == ':')
2N/A c = ':';
2N/A else
2N/A c = '?';
2N/A }
2N/A else
2N/A /* We already incremented `optind' once;
2N/A increment it again when taking next ARGV-elt as argument. */
2N/A d->optarg = argv[d->optind++];
2N/A d->__nextchar = NULL;
2N/A }
2N/A }
2N/A return c;
2N/A }
2N/A}
2N/A
2N/Aint
2N/A_getopt_internal (int argc, char **argv, const char *optstring,
2N/A const struct option *longopts, int *longind, int long_only,
2N/A int posixly_correct)
2N/A{
2N/A int result;
2N/A
2N/A getopt_data.optind = optind;
2N/A getopt_data.opterr = opterr;
2N/A
2N/A result = _getopt_internal_r (argc, argv, optstring, longopts,
2N/A longind, long_only, &getopt_data,
2N/A posixly_correct);
2N/A
2N/A optind = getopt_data.optind;
2N/A optarg = getopt_data.optarg;
2N/A optopt = getopt_data.optopt;
2N/A
2N/A return result;
2N/A}
2N/A
2N/A/* glibc gets a LSB-compliant getopt.
2N/A Standalone applications get a POSIX-compliant getopt. */
2N/A#if _LIBC
2N/Aenum { POSIXLY_CORRECT = 0 };
2N/A#else
2N/Aenum { POSIXLY_CORRECT = 1 };
2N/A#endif
2N/A
2N/Aint
2N/Agetopt (int argc, char *const *argv, const char *optstring)
2N/A{
2N/A return _getopt_internal (argc, (char **) argv, optstring,
2N/A (const struct option *) 0,
2N/A (int *) 0,
2N/A 0, POSIXLY_CORRECT);
2N/A}
2N/A
2N/A#ifdef _LIBC
2N/Aint
2N/A__posix_getopt (int argc, char *const *argv, const char *optstring)
2N/A{
2N/A return _getopt_internal (argc, argv, optstring,
2N/A (const struct option *) 0,
2N/A (int *) 0,
2N/A 0, 1);
2N/A}
2N/A#endif
2N/A
2N/A
2N/A#ifdef TEST
2N/A
2N/A/* Compile with -DTEST to make an executable for use in testing
2N/A the above definition of `getopt'. */
2N/A
2N/Aint
2N/Amain (int argc, char **argv)
2N/A{
2N/A int c;
2N/A int digit_optind = 0;
2N/A
2N/A while (1)
2N/A {
2N/A int this_option_optind = optind ? optind : 1;
2N/A
2N/A c = getopt (argc, argv, "abc:d:0123456789");
2N/A if (c == -1)
2N/A break;
2N/A
2N/A switch (c)
2N/A {
2N/A case '0':
2N/A case '1':
2N/A case '2':
2N/A case '3':
2N/A case '4':
2N/A case '5':
2N/A case '6':
2N/A case '7':
2N/A case '8':
2N/A case '9':
2N/A if (digit_optind != 0 && digit_optind != this_option_optind)
2N/A printf ("digits occur in two different argv-elements.\n");
2N/A digit_optind = this_option_optind;
2N/A printf ("option %c\n", c);
2N/A break;
2N/A
2N/A case 'a':
2N/A printf ("option a\n");
2N/A break;
2N/A
2N/A case 'b':
2N/A printf ("option b\n");
2N/A break;
2N/A
2N/A case 'c':
2N/A printf ("option c with value '%s'\n", optarg);
2N/A break;
2N/A
2N/A case '?':
2N/A break;
2N/A
2N/A default:
2N/A printf ("?? getopt returned character code 0%o ??\n", c);
2N/A }
2N/A }
2N/A
2N/A if (optind < argc)
2N/A {
2N/A printf ("non-option ARGV-elements: ");
2N/A while (optind < argc)
2N/A printf ("%s ", argv[optind++]);
2N/A printf ("\n");
2N/A }
2N/A
2N/A exit (0);
2N/A}
2N/A
2N/A#endif /* TEST */