Blame portability/getopt.c

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