Blame lib/getopt.c

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