Blame lib/getopt.c

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