Blame lib/getopt.c

Packit 709fb3
/* Getopt for GNU.
Packit 709fb3
   Copyright (C) 1987-2017 Free Software Foundation, Inc.
Packit 709fb3
   This file is part of the GNU C Library and is also part of gnulib.
Packit 709fb3
   Patches to this file should be submitted to both projects.
Packit 709fb3
Packit 709fb3
   The GNU C Library is free software; you can redistribute it and/or
Packit 709fb3
   modify it under the terms of the GNU General Public
Packit 709fb3
   License as published by the Free Software Foundation; either
Packit 709fb3
   version 3 of the License, or (at your option) any later version.
Packit 709fb3
Packit 709fb3
   The GNU C Library is distributed in the hope that it will be useful,
Packit 709fb3
   but WITHOUT ANY WARRANTY; without even the implied warranty of
Packit 709fb3
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
Packit 709fb3
   General Public License for more details.
Packit 709fb3
Packit 709fb3
   You should have received a copy of the GNU General Public
Packit 709fb3
   License along with the GNU C Library; if not, see
Packit 709fb3
   <http://www.gnu.org/licenses/>.  */
Packit 709fb3

Packit 709fb3
#ifndef _LIBC
Packit 709fb3
# include <config.h>
Packit 709fb3
#endif
Packit 709fb3
Packit 709fb3
#include "getopt.h"
Packit 709fb3
Packit 709fb3
#include <stdio.h>
Packit 709fb3
#include <stdlib.h>
Packit 709fb3
#include <string.h>
Packit 709fb3
#include <unistd.h>
Packit 709fb3
Packit 709fb3
#ifdef _LIBC
Packit 709fb3
/* When used as part of glibc, error printing must be done differently
Packit 709fb3
   for standards compliance.  getopt is not a cancellation point, so
Packit 709fb3
   it must not call functions that are, and it is specified by an
Packit 709fb3
   older standard than stdio locking, so it must not refer to
Packit 709fb3
   functions in the "user namespace" related to stdio locking.
Packit 709fb3
   Finally, it must use glibc's internal message translation so that
Packit 709fb3
   the messages are looked up in the proper text domain.  */
Packit 709fb3
# include <libintl.h>
Packit 709fb3
# define fprintf __fxprintf_nocancel
Packit 709fb3
# define flockfile(fp) _IO_flockfile (fp)
Packit 709fb3
# define funlockfile(fp) _IO_funlockfile (fp)
Packit 709fb3
#else
Packit 709fb3
# include "gettext.h"
Packit 709fb3
# define _(msgid) gettext (msgid)
Packit 709fb3
/* When used standalone, flockfile and funlockfile might not be
Packit 709fb3
   available.  */
Packit 709fb3
# if (!defined _POSIX_THREAD_SAFE_FUNCTIONS \
Packit 709fb3
      || ((defined _WIN32 || defined __WIN32__) && ! defined __CYGWIN__))
Packit 709fb3
#  define flockfile(fp) /* nop */
Packit 709fb3
#  define funlockfile(fp) /* nop */
Packit 709fb3
# endif
Packit 709fb3
/* When used standalone, do not attempt to use alloca.  */
Packit 709fb3
# define __libc_use_alloca(size) 0
Packit 709fb3
# undef alloca
Packit 709fb3
# define alloca(size) (abort (), (void *)0)
Packit 709fb3
#endif
Packit 709fb3
Packit 709fb3
/* This implementation of 'getopt' has three modes for handling
Packit 709fb3
   options interspersed with non-option arguments.  It can stop
Packit 709fb3
   scanning for options at the first non-option argument encountered,
Packit 709fb3
   as POSIX specifies.  It can continue scanning for options after the
Packit 709fb3
   first non-option argument, but permute 'argv' as it goes so that,
Packit 709fb3
   after 'getopt' is done, all the options precede all the non-option
Packit 709fb3
   arguments and 'optind' points to the first non-option argument.
Packit 709fb3
   Or, it can report non-option arguments as if they were arguments to
Packit 709fb3
   the option character '\x01'.
Packit 709fb3
Packit 709fb3
   The default behavior of 'getopt_long' is to permute the argument list.
Packit 709fb3
   When this implementation is used standalone, the default behavior of
Packit 709fb3
   'getopt' is to stop at the first non-option argument, but when it is
Packit 709fb3
   used as part of GNU libc it also permutes the argument list.  In both
Packit 709fb3
   cases, setting the environment variable POSIXLY_CORRECT to any value
Packit 709fb3
   disables permutation.
Packit 709fb3
Packit 709fb3
   If the first character of the OPTSTRING argument to 'getopt' or
Packit 709fb3
   'getopt_long' is '+', both functions will stop at the first
Packit 709fb3
   non-option argument.  If it is '-', both functions will report
Packit 709fb3
   non-option arguments as arguments to the option character '\x01'.  */
Packit 709fb3
Packit 709fb3
#include "getopt_int.h"
Packit 709fb3
Packit 709fb3
/* For communication from 'getopt' to the caller.
Packit 709fb3
   When 'getopt' finds an option that takes an argument,
Packit 709fb3
   the argument value is returned here.
Packit 709fb3
   Also, when 'ordering' is RETURN_IN_ORDER,
Packit 709fb3
   each non-option ARGV-element is returned here.  */
Packit 709fb3
Packit 709fb3
char *optarg;
Packit 709fb3
Packit 709fb3
/* Index in ARGV of the next element to be scanned.
Packit 709fb3
   This is used for communication to and from the caller
Packit 709fb3
   and for communication between successive calls to 'getopt'.
Packit 709fb3
Packit 709fb3
   On entry to 'getopt', zero means this is the first call; initialize.
Packit 709fb3
Packit 709fb3
   When 'getopt' returns -1, this is the index of the first of the
Packit 709fb3
   non-option elements that the caller should itself scan.
Packit 709fb3
Packit 709fb3
   Otherwise, 'optind' communicates from one call to the next
Packit 709fb3
   how much of ARGV has been scanned so far.  */
Packit 709fb3
Packit 709fb3
/* 1003.2 says this must be 1 before any call.  */
Packit 709fb3
int optind = 1;
Packit 709fb3
Packit 709fb3
/* Callers store zero here to inhibit the error message
Packit 709fb3
   for unrecognized options.  */
Packit 709fb3
Packit 709fb3
int opterr = 1;
Packit 709fb3
Packit 709fb3
/* Set to an option character which was unrecognized.
Packit 709fb3
   This must be initialized on some systems to avoid linking in the
Packit 709fb3
   system's own getopt implementation.  */
Packit 709fb3
Packit 709fb3
int optopt = '?';
Packit 709fb3
Packit 709fb3
/* Keep a global copy of all internal members of getopt_data.  */
Packit 709fb3
Packit 709fb3
static struct _getopt_data getopt_data;
Packit 709fb3

Packit 709fb3
/* Exchange two adjacent subsequences of ARGV.
Packit 709fb3
   One subsequence is elements [first_nonopt,last_nonopt)
Packit 709fb3
   which contains all the non-options that have been skipped so far.
Packit 709fb3
   The other is elements [last_nonopt,optind), which contains all
Packit 709fb3
   the options processed since those non-options were skipped.
Packit 709fb3
Packit 709fb3
   'first_nonopt' and 'last_nonopt' are relocated so that they describe
Packit 709fb3
   the new indices of the non-options in ARGV after they are moved.  */
Packit 709fb3
Packit 709fb3
static void
Packit 709fb3
exchange (char **argv, struct _getopt_data *d)
Packit 709fb3
{
Packit 709fb3
  int bottom = d->__first_nonopt;
Packit 709fb3
  int middle = d->__last_nonopt;
Packit 709fb3
  int top = d->optind;
Packit 709fb3
  char *tem;
Packit 709fb3
Packit 709fb3
  /* Exchange the shorter segment with the far end of the longer segment.
Packit 709fb3
     That puts the shorter segment into the right place.
Packit 709fb3
     It leaves the longer segment in the right place overall,
Packit 709fb3
     but it consists of two parts that need to be swapped next.  */
Packit 709fb3
Packit 709fb3
  while (top > middle && middle > bottom)
Packit 709fb3
    {
Packit 709fb3
      if (top - middle > middle - bottom)
Packit 709fb3
	{
Packit 709fb3
	  /* Bottom segment is the short one.  */
Packit 709fb3
	  int len = middle - bottom;
Packit 709fb3
	  int i;
Packit 709fb3
Packit 709fb3
	  /* Swap it with the top part of the top segment.  */
Packit 709fb3
	  for (i = 0; i < len; i++)
Packit 709fb3
	    {
Packit 709fb3
	      tem = argv[bottom + i];
Packit 709fb3
	      argv[bottom + i] = argv[top - (middle - bottom) + i];
Packit 709fb3
	      argv[top - (middle - bottom) + i] = tem;
Packit 709fb3
	    }
Packit 709fb3
	  /* Exclude the moved bottom segment from further swapping.  */
Packit 709fb3
	  top -= len;
Packit 709fb3
	}
Packit 709fb3
      else
Packit 709fb3
	{
Packit 709fb3
	  /* Top segment is the short one.  */
Packit 709fb3
	  int len = top - middle;
Packit 709fb3
	  int i;
Packit 709fb3
Packit 709fb3
	  /* Swap it with the bottom part of the bottom segment.  */
Packit 709fb3
	  for (i = 0; i < len; i++)
Packit 709fb3
	    {
Packit 709fb3
	      tem = argv[bottom + i];
Packit 709fb3
	      argv[bottom + i] = argv[middle + i];
Packit 709fb3
	      argv[middle + i] = tem;
Packit 709fb3
	    }
Packit 709fb3
	  /* Exclude the moved top segment from further swapping.  */
Packit 709fb3
	  bottom += len;
Packit 709fb3
	}
Packit 709fb3
    }
Packit 709fb3
Packit 709fb3
  /* Update records for the slots the non-options now occupy.  */
Packit 709fb3
Packit 709fb3
  d->__first_nonopt += (d->optind - d->__last_nonopt);
Packit 709fb3
  d->__last_nonopt = d->optind;
Packit 709fb3
}
Packit 709fb3
Packit 709fb3
/* Process the argument starting with d->__nextchar as a long option.
Packit 709fb3
   d->optind should *not* have been advanced over this argument.
Packit 709fb3
Packit 709fb3
   If the value returned is -1, it was not actually a long option, the
Packit 709fb3
   state is unchanged, and the argument should be processed as a set
Packit 709fb3
   of short options (this can only happen when long_only is true).
Packit 709fb3
   Otherwise, the option (and its argument, if any) have been consumed
Packit 709fb3
   and the return value is the value to return from _getopt_internal_r.  */
Packit 709fb3
static int
Packit 709fb3
process_long_option (int argc, char **argv, const char *optstring,
Packit 709fb3
		     const struct option *longopts, int *longind,
Packit 709fb3
		     int long_only, struct _getopt_data *d,
Packit 709fb3
		     int print_errors, const char *prefix)
Packit 709fb3
{
Packit 709fb3
  char *nameend;
Packit 709fb3
  size_t namelen;
Packit 709fb3
  const struct option *p;
Packit 709fb3
  const struct option *pfound = NULL;
Packit 709fb3
  int n_options;
Packit 709fb3
  int option_index;
Packit 709fb3
Packit 709fb3
  for (nameend = d->__nextchar; *nameend && *nameend != '='; nameend++)
Packit 709fb3
    /* Do nothing.  */ ;
Packit 709fb3
  namelen = nameend - d->__nextchar;
Packit 709fb3
Packit 709fb3
  /* First look for an exact match, counting the options as a side
Packit 709fb3
     effect.  */
Packit 709fb3
  for (p = longopts, n_options = 0; p->name; p++, n_options++)
Packit 709fb3
    if (!strncmp (p->name, d->__nextchar, namelen)
Packit 709fb3
	&& namelen == strlen (p->name))
Packit 709fb3
      {
Packit 709fb3
	/* Exact match found.  */
Packit 709fb3
	pfound = p;
Packit 709fb3
	option_index = n_options;
Packit 709fb3
	break;
Packit 709fb3
      }
Packit 709fb3
Packit 709fb3
  if (pfound == NULL)
Packit 709fb3
    {
Packit 709fb3
      /* Didn't find an exact match, so look for abbreviations.  */
Packit 709fb3
      unsigned char *ambig_set = NULL;
Packit 709fb3
      int ambig_malloced = 0;
Packit 709fb3
      int ambig_fallback = 0;
Packit 709fb3
      int indfound = -1;
Packit 709fb3
Packit 709fb3
      for (p = longopts, option_index = 0; p->name; p++, option_index++)
Packit 709fb3
	if (!strncmp (p->name, d->__nextchar, namelen))
Packit 709fb3
	  {
Packit 709fb3
	    if (pfound == NULL)
Packit 709fb3
	      {
Packit 709fb3
		/* First nonexact match found.  */
Packit 709fb3
		pfound = p;
Packit 709fb3
		indfound = option_index;
Packit 709fb3
	      }
Packit 709fb3
	    else if (long_only
Packit 709fb3
		     || pfound->has_arg != p->has_arg
Packit 709fb3
		     || pfound->flag != p->flag
Packit 709fb3
		     || pfound->val != p->val)
Packit 709fb3
	      {
Packit 709fb3
		/* Second or later nonexact match found.  */
Packit 709fb3
		if (!ambig_fallback)
Packit 709fb3
		  {
Packit 709fb3
		    if (!print_errors)
Packit 709fb3
		      /* Don't waste effort tracking the ambig set if
Packit 709fb3
			 we're not going to print it anyway.  */
Packit 709fb3
		      ambig_fallback = 1;
Packit 709fb3
		    else if (!ambig_set)
Packit 709fb3
		      {
Packit 709fb3
			if (__libc_use_alloca (n_options))
Packit 709fb3
			  ambig_set = alloca (n_options);
Packit 709fb3
			else if ((ambig_set = malloc (n_options)) == NULL)
Packit 709fb3
			  /* Fall back to simpler error message.  */
Packit 709fb3
			  ambig_fallback = 1;
Packit 709fb3
			else
Packit 709fb3
			  ambig_malloced = 1;
Packit 709fb3
Packit 709fb3
			if (ambig_set)
Packit 709fb3
			  {
Packit 709fb3
			    memset (ambig_set, 0, n_options);
Packit 709fb3
			    ambig_set[indfound] = 1;
Packit 709fb3
			  }
Packit 709fb3
		      }
Packit 709fb3
		    if (ambig_set)
Packit 709fb3
		      ambig_set[option_index] = 1;
Packit 709fb3
		  }
Packit 709fb3
	      }
Packit 709fb3
	  }
Packit 709fb3
Packit 709fb3
      if (ambig_set || ambig_fallback)
Packit 709fb3
	{
Packit 709fb3
	  if (print_errors)
Packit 709fb3
	    {
Packit 709fb3
	      if (ambig_fallback)
Packit 709fb3
		fprintf (stderr, _("%s: option '%s%s' is ambiguous\n"),
Packit 709fb3
			 argv[0], prefix, d->__nextchar);
Packit 709fb3
	      else
Packit 709fb3
		{
Packit 709fb3
		  flockfile (stderr);
Packit 709fb3
		  fprintf (stderr,
Packit 709fb3
			   _("%s: option '%s%s' is ambiguous; possibilities:"),
Packit 709fb3
			   argv[0], prefix, d->__nextchar);
Packit 709fb3
Packit 709fb3
		  for (option_index = 0; option_index < n_options; option_index++)
Packit 709fb3
		    if (ambig_set[option_index])
Packit 709fb3
		      fprintf (stderr, " '%s%s'",
Packit 709fb3
			       prefix, longopts[option_index].name);
Packit 709fb3
Packit 709fb3
		  /* This must use 'fprintf' even though it's only
Packit 709fb3
		     printing a single character, so that it goes through
Packit 709fb3
		     __fxprintf_nocancel when compiled as part of glibc.  */
Packit 709fb3
		  fprintf (stderr, "\n");
Packit 709fb3
		  funlockfile (stderr);
Packit 709fb3
		}
Packit 709fb3
	    }
Packit 709fb3
	  if (ambig_malloced)
Packit 709fb3
	    free (ambig_set);
Packit 709fb3
	  d->__nextchar += strlen (d->__nextchar);
Packit 709fb3
	  d->optind++;
Packit 709fb3
	  d->optopt = 0;
Packit 709fb3
	  return '?';
Packit 709fb3
	}
Packit 709fb3
Packit 709fb3
      option_index = indfound;
Packit 709fb3
    }
Packit 709fb3
Packit 709fb3
  if (pfound == NULL)
Packit 709fb3
    {
Packit 709fb3
      /* Can't find it as a long option.  If this is not getopt_long_only,
Packit 709fb3
	 or the option starts with '--' or is not a valid short option,
Packit 709fb3
	 then it's an error.  */
Packit 709fb3
      if (!long_only || argv[d->optind][1] == '-'
Packit 709fb3
	  || strchr (optstring, *d->__nextchar) == NULL)
Packit 709fb3
	{
Packit 709fb3
	  if (print_errors)
Packit 709fb3
	    fprintf (stderr, _("%s: unrecognized option '%s%s'\n"),
Packit 709fb3
		     argv[0], prefix, d->__nextchar);
Packit 709fb3
Packit 709fb3
	  d->__nextchar = NULL;
Packit 709fb3
	  d->optind++;
Packit 709fb3
	  d->optopt = 0;
Packit 709fb3
	  return '?';
Packit 709fb3
	}
Packit 709fb3
Packit 709fb3
      /* Otherwise interpret it as a short option.  */
Packit 709fb3
      return -1;
Packit 709fb3
    }
Packit 709fb3
Packit 709fb3
  /* We have found a matching long option.  Consume it.  */
Packit 709fb3
  d->optind++;
Packit 709fb3
  d->__nextchar = NULL;
Packit 709fb3
  if (*nameend)
Packit 709fb3
    {
Packit 709fb3
      /* Don't test has_arg with >, because some C compilers don't
Packit 709fb3
	 allow it to be used on enums.  */
Packit 709fb3
      if (pfound->has_arg)
Packit 709fb3
	d->optarg = nameend + 1;
Packit 709fb3
      else
Packit 709fb3
	{
Packit 709fb3
	  if (print_errors)
Packit 709fb3
	    fprintf (stderr,
Packit 709fb3
		     _("%s: option '%s%s' doesn't allow an argument\n"),
Packit 709fb3
		     argv[0], prefix, pfound->name);
Packit 709fb3
Packit 709fb3
	  d->optopt = pfound->val;
Packit 709fb3
	  return '?';
Packit 709fb3
	}
Packit 709fb3
    }
Packit 709fb3
  else if (pfound->has_arg == 1)
Packit 709fb3
    {
Packit 709fb3
      if (d->optind < argc)
Packit 709fb3
	d->optarg = argv[d->optind++];
Packit 709fb3
      else
Packit 709fb3
	{
Packit 709fb3
	  if (print_errors)
Packit 709fb3
	    fprintf (stderr,
Packit 709fb3
		     _("%s: option '%s%s' requires an argument\n"),
Packit 709fb3
		     argv[0], prefix, pfound->name);
Packit 709fb3
Packit 709fb3
	  d->optopt = pfound->val;
Packit 709fb3
	  return optstring[0] == ':' ? ':' : '?';
Packit 709fb3
	}
Packit 709fb3
    }
Packit 709fb3
Packit 709fb3
  if (longind != NULL)
Packit 709fb3
    *longind = option_index;
Packit 709fb3
  if (pfound->flag)
Packit 709fb3
    {
Packit 709fb3
      *(pfound->flag) = pfound->val;
Packit 709fb3
      return 0;
Packit 709fb3
    }
Packit 709fb3
  return pfound->val;
Packit 709fb3
}
Packit 709fb3
Packit 709fb3
/* Initialize internal data upon the first call to getopt.  */
Packit 709fb3
Packit 709fb3
static const char *
Packit 709fb3
_getopt_initialize (int argc _GL_UNUSED,
Packit 709fb3
		    char **argv _GL_UNUSED, const char *optstring,
Packit 709fb3
		    struct _getopt_data *d, int posixly_correct)
Packit 709fb3
{
Packit 709fb3
  /* Start processing options with ARGV-element 1 (since ARGV-element 0
Packit 709fb3
     is the program name); the sequence of previously skipped
Packit 709fb3
     non-option ARGV-elements is empty.  */
Packit 709fb3
  if (d->optind == 0)
Packit 709fb3
    d->optind = 1;
Packit 709fb3
Packit 709fb3
  d->__first_nonopt = d->__last_nonopt = d->optind;
Packit 709fb3
  d->__nextchar = NULL;
Packit 709fb3
Packit 709fb3
  /* Determine how to handle the ordering of options and nonoptions.  */
Packit 709fb3
  if (optstring[0] == '-')
Packit 709fb3
    {
Packit 709fb3
      d->__ordering = RETURN_IN_ORDER;
Packit 709fb3
      ++optstring;
Packit 709fb3
    }
Packit 709fb3
  else if (optstring[0] == '+')
Packit 709fb3
    {
Packit 709fb3
      d->__ordering = REQUIRE_ORDER;
Packit 709fb3
      ++optstring;
Packit 709fb3
    }
Packit 709fb3
  else if (posixly_correct || !!getenv ("POSIXLY_CORRECT"))
Packit 709fb3
    d->__ordering = REQUIRE_ORDER;
Packit 709fb3
  else
Packit 709fb3
    d->__ordering = PERMUTE;
Packit 709fb3
Packit 709fb3
  d->__initialized = 1;
Packit 709fb3
  return optstring;
Packit 709fb3
}
Packit 709fb3

Packit 709fb3
/* Scan elements of ARGV (whose length is ARGC) for option characters
Packit 709fb3
   given in OPTSTRING.
Packit 709fb3
Packit 709fb3
   If an element of ARGV starts with '-', and is not exactly "-" or "--",
Packit 709fb3
   then it is an option element.  The characters of this element
Packit 709fb3
   (aside from the initial '-') are option characters.  If 'getopt'
Packit 709fb3
   is called repeatedly, it returns successively each of the option characters
Packit 709fb3
   from each of the option elements.
Packit 709fb3
Packit 709fb3
   If 'getopt' finds another option character, it returns that character,
Packit 709fb3
   updating 'optind' and 'nextchar' so that the next call to 'getopt' can
Packit 709fb3
   resume the scan with the following option character or ARGV-element.
Packit 709fb3
Packit 709fb3
   If there are no more option characters, 'getopt' returns -1.
Packit 709fb3
   Then 'optind' is the index in ARGV of the first ARGV-element
Packit 709fb3
   that is not an option.  (The ARGV-elements have been permuted
Packit 709fb3
   so that those that are not options now come last.)
Packit 709fb3
Packit 709fb3
   OPTSTRING is a string containing the legitimate option characters.
Packit 709fb3
   If an option character is seen that is not listed in OPTSTRING,
Packit 709fb3
   return '?' after printing an error message.  If you set 'opterr' to
Packit 709fb3
   zero, the error message is suppressed but we still return '?'.
Packit 709fb3
Packit 709fb3
   If a char in OPTSTRING is followed by a colon, that means it wants an arg,
Packit 709fb3
   so the following text in the same ARGV-element, or the text of the following
Packit 709fb3
   ARGV-element, is returned in 'optarg'.  Two colons mean an option that
Packit 709fb3
   wants an optional arg; if there is text in the current ARGV-element,
Packit 709fb3
   it is returned in 'optarg', otherwise 'optarg' is set to zero.
Packit 709fb3
Packit 709fb3
   If OPTSTRING starts with '-' or '+', it requests different methods of
Packit 709fb3
   handling the non-option ARGV-elements.
Packit 709fb3
   See the comments about RETURN_IN_ORDER and REQUIRE_ORDER, above.
Packit 709fb3
Packit 709fb3
   Long-named options begin with '--' instead of '-'.
Packit 709fb3
   Their names may be abbreviated as long as the abbreviation is unique
Packit 709fb3
   or is an exact match for some defined option.  If they have an
Packit 709fb3
   argument, it follows the option name in the same ARGV-element, separated
Packit 709fb3
   from the option name by a '=', or else the in next ARGV-element.
Packit 709fb3
   When 'getopt' finds a long-named option, it returns 0 if that option's
Packit 709fb3
   'flag' field is nonzero, the value of the option's 'val' field
Packit 709fb3
   if the 'flag' field is zero.
Packit 709fb3
Packit 709fb3
   The elements of ARGV aren't really const, because we permute them.
Packit 709fb3
   But we pretend they're const in the prototype to be compatible
Packit 709fb3
   with other systems.
Packit 709fb3
Packit 709fb3
   LONGOPTS is a vector of 'struct option' terminated by an
Packit 709fb3
   element containing a name which is zero.
Packit 709fb3
Packit 709fb3
   LONGIND returns the index in LONGOPT of the long-named option found.
Packit 709fb3
   It is only valid when a long-named option has been found by the most
Packit 709fb3
   recent call.
Packit 709fb3
Packit 709fb3
   If LONG_ONLY is nonzero, '-' as well as '--' can introduce
Packit 709fb3
   long-named options.  */
Packit 709fb3
Packit 709fb3
int
Packit 709fb3
_getopt_internal_r (int argc, char **argv, const char *optstring,
Packit 709fb3
		    const struct option *longopts, int *longind,
Packit 709fb3
		    int long_only, struct _getopt_data *d, int posixly_correct)
Packit 709fb3
{
Packit 709fb3
  int print_errors = d->opterr;
Packit 709fb3
Packit 709fb3
  if (argc < 1)
Packit 709fb3
    return -1;
Packit 709fb3
Packit 709fb3
  d->optarg = NULL;
Packit 709fb3
Packit 709fb3
  if (d->optind == 0 || !d->__initialized)
Packit 709fb3
    optstring = _getopt_initialize (argc, argv, optstring, d, posixly_correct);
Packit 709fb3
  else if (optstring[0] == '-' || optstring[0] == '+')
Packit 709fb3
    optstring++;
Packit 709fb3
Packit 709fb3
  if (optstring[0] == ':')
Packit 709fb3
    print_errors = 0;
Packit 709fb3
Packit 709fb3
  /* Test whether ARGV[optind] points to a non-option argument.  */
Packit 709fb3
#define NONOPTION_P (argv[d->optind][0] != '-' || argv[d->optind][1] == '\0')
Packit 709fb3
Packit 709fb3
  if (d->__nextchar == NULL || *d->__nextchar == '\0')
Packit 709fb3
    {
Packit 709fb3
      /* Advance to the next ARGV-element.  */
Packit 709fb3
Packit 709fb3
      /* Give FIRST_NONOPT & LAST_NONOPT rational values if OPTIND has been
Packit 709fb3
	 moved back by the user (who may also have changed the arguments).  */
Packit 709fb3
      if (d->__last_nonopt > d->optind)
Packit 709fb3
	d->__last_nonopt = d->optind;
Packit 709fb3
      if (d->__first_nonopt > d->optind)
Packit 709fb3
	d->__first_nonopt = d->optind;
Packit 709fb3
Packit 709fb3
      if (d->__ordering == PERMUTE)
Packit 709fb3
	{
Packit 709fb3
	  /* If we have just processed some options following some non-options,
Packit 709fb3
	     exchange them so that the options come first.  */
Packit 709fb3
Packit 709fb3
	  if (d->__first_nonopt != d->__last_nonopt
Packit 709fb3
	      && d->__last_nonopt != d->optind)
Packit 709fb3
	    exchange (argv, d);
Packit 709fb3
	  else if (d->__last_nonopt != d->optind)
Packit 709fb3
	    d->__first_nonopt = d->optind;
Packit 709fb3
Packit 709fb3
	  /* Skip any additional non-options
Packit 709fb3
	     and extend the range of non-options previously skipped.  */
Packit 709fb3
Packit 709fb3
	  while (d->optind < argc && NONOPTION_P)
Packit 709fb3
	    d->optind++;
Packit 709fb3
	  d->__last_nonopt = d->optind;
Packit 709fb3
	}
Packit 709fb3
Packit 709fb3
      /* The special ARGV-element '--' means premature end of options.
Packit 709fb3
	 Skip it like a null option,
Packit 709fb3
	 then exchange with previous non-options as if it were an option,
Packit 709fb3
	 then skip everything else like a non-option.  */
Packit 709fb3
Packit 709fb3
      if (d->optind != argc && !strcmp (argv[d->optind], "--"))
Packit 709fb3
	{
Packit 709fb3
	  d->optind++;
Packit 709fb3
Packit 709fb3
	  if (d->__first_nonopt != d->__last_nonopt
Packit 709fb3
	      && d->__last_nonopt != d->optind)
Packit 709fb3
	    exchange (argv, d);
Packit 709fb3
	  else if (d->__first_nonopt == d->__last_nonopt)
Packit 709fb3
	    d->__first_nonopt = d->optind;
Packit 709fb3
	  d->__last_nonopt = argc;
Packit 709fb3
Packit 709fb3
	  d->optind = argc;
Packit 709fb3
	}
Packit 709fb3
Packit 709fb3
      /* If we have done all the ARGV-elements, stop the scan
Packit 709fb3
	 and back over any non-options that we skipped and permuted.  */
Packit 709fb3
Packit 709fb3
      if (d->optind == argc)
Packit 709fb3
	{
Packit 709fb3
	  /* Set the next-arg-index to point at the non-options
Packit 709fb3
	     that we previously skipped, so the caller will digest them.  */
Packit 709fb3
	  if (d->__first_nonopt != d->__last_nonopt)
Packit 709fb3
	    d->optind = d->__first_nonopt;
Packit 709fb3
	  return -1;
Packit 709fb3
	}
Packit 709fb3
Packit 709fb3
      /* If we have come to a non-option and did not permute it,
Packit 709fb3
	 either stop the scan or describe it to the caller and pass it by.  */
Packit 709fb3
Packit 709fb3
      if (NONOPTION_P)
Packit 709fb3
	{
Packit 709fb3
	  if (d->__ordering == REQUIRE_ORDER)
Packit 709fb3
	    return -1;
Packit 709fb3
	  d->optarg = argv[d->optind++];
Packit 709fb3
	  return 1;
Packit 709fb3
	}
Packit 709fb3
Packit 709fb3
      /* We have found another option-ARGV-element.
Packit 709fb3
	 Check whether it might be a long option.  */
Packit 709fb3
      if (longopts)
Packit 709fb3
	{
Packit 709fb3
	  if (argv[d->optind][1] == '-')
Packit 709fb3
	    {
Packit 709fb3
	      /* "--foo" is always a long option.  The special option
Packit 709fb3
		 "--" was handled above.  */
Packit 709fb3
	      d->__nextchar = argv[d->optind] + 2;
Packit 709fb3
	      return process_long_option (argc, argv, optstring, longopts,
Packit 709fb3
					  longind, long_only, d,
Packit 709fb3
					  print_errors, "--");
Packit 709fb3
	    }
Packit 709fb3
Packit 709fb3
	  /* If long_only and the ARGV-element has the form "-f",
Packit 709fb3
	     where f is a valid short option, don't consider it an
Packit 709fb3
	     abbreviated form of a long option that starts with f.
Packit 709fb3
	     Otherwise there would be no way to give the -f short
Packit 709fb3
	     option.
Packit 709fb3
Packit 709fb3
	     On the other hand, if there's a long option "fubar" and
Packit 709fb3
	     the ARGV-element is "-fu", do consider that an
Packit 709fb3
	     abbreviation of the long option, just like "--fu", and
Packit 709fb3
	     not "-f" with arg "u".
Packit 709fb3
Packit 709fb3
	     This distinction seems to be the most useful approach.  */
Packit 709fb3
	  if (long_only && (argv[d->optind][2]
Packit 709fb3
			    || !strchr (optstring, argv[d->optind][1])))
Packit 709fb3
	    {
Packit 709fb3
	      int code;
Packit 709fb3
	      d->__nextchar = argv[d->optind] + 1;
Packit 709fb3
	      code = process_long_option (argc, argv, optstring, longopts,
Packit 709fb3
					  longind, long_only, d,
Packit 709fb3
					  print_errors, "-");
Packit 709fb3
	      if (code != -1)
Packit 709fb3
		return code;
Packit 709fb3
	    }
Packit 709fb3
	}
Packit 709fb3
Packit 709fb3
      /* It is not a long option.  Skip the initial punctuation.  */
Packit 709fb3
      d->__nextchar = argv[d->optind] + 1;
Packit 709fb3
    }
Packit 709fb3
Packit 709fb3
  /* Look at and handle the next short option-character.  */
Packit 709fb3
Packit 709fb3
  {
Packit 709fb3
    char c = *d->__nextchar++;
Packit 709fb3
    const char *temp = strchr (optstring, c);
Packit 709fb3
Packit 709fb3
    /* Increment 'optind' when we start to process its last character.  */
Packit 709fb3
    if (*d->__nextchar == '\0')
Packit 709fb3
      ++d->optind;
Packit 709fb3
Packit 709fb3
    if (temp == NULL || c == ':' || c == ';')
Packit 709fb3
      {
Packit 709fb3
	if (print_errors)
Packit 709fb3
	  fprintf (stderr, _("%s: invalid option -- '%c'\n"), argv[0], c);
Packit 709fb3
	d->optopt = c;
Packit 709fb3
	return '?';
Packit 709fb3
      }
Packit 709fb3
Packit 709fb3
    /* Convenience. Treat POSIX -W foo same as long option --foo */
Packit 709fb3
    if (temp[0] == 'W' && temp[1] == ';' && longopts != NULL)
Packit 709fb3
      {
Packit 709fb3
	/* This is an option that requires an argument.  */
Packit 709fb3
	if (*d->__nextchar != '\0')
Packit 709fb3
	  d->optarg = d->__nextchar;
Packit 709fb3
	else if (d->optind == argc)
Packit 709fb3
	  {
Packit 709fb3
	    if (print_errors)
Packit 709fb3
	      fprintf (stderr,
Packit 709fb3
		       _("%s: option requires an argument -- '%c'\n"),
Packit 709fb3
		       argv[0], c);
Packit 709fb3
Packit 709fb3
	    d->optopt = c;
Packit 709fb3
	    if (optstring[0] == ':')
Packit 709fb3
	      c = ':';
Packit 709fb3
	    else
Packit 709fb3
	      c = '?';
Packit 709fb3
	    return c;
Packit 709fb3
	  }
Packit 709fb3
	else
Packit 709fb3
	  d->optarg = argv[d->optind];
Packit 709fb3
Packit 709fb3
	d->__nextchar = d->optarg;
Packit 709fb3
	d->optarg = NULL;
Packit 709fb3
	return process_long_option (argc, argv, optstring, longopts, longind,
Packit 709fb3
				    0 /* long_only */, d, print_errors, "-W ");
Packit 709fb3
      }
Packit 709fb3
    if (temp[1] == ':')
Packit 709fb3
      {
Packit 709fb3
	if (temp[2] == ':')
Packit 709fb3
	  {
Packit 709fb3
	    /* This is an option that accepts an argument optionally.  */
Packit 709fb3
	    if (*d->__nextchar != '\0')
Packit 709fb3
	      {
Packit 709fb3
		d->optarg = d->__nextchar;
Packit 709fb3
		d->optind++;
Packit 709fb3
	      }
Packit 709fb3
	    else
Packit 709fb3
	      d->optarg = NULL;
Packit 709fb3
	    d->__nextchar = NULL;
Packit 709fb3
	  }
Packit 709fb3
	else
Packit 709fb3
	  {
Packit 709fb3
	    /* This is an option that requires an argument.  */
Packit 709fb3
	    if (*d->__nextchar != '\0')
Packit 709fb3
	      {
Packit 709fb3
		d->optarg = d->__nextchar;
Packit 709fb3
		/* If we end this ARGV-element by taking the rest as an arg,
Packit 709fb3
		   we must advance to the next element now.  */
Packit 709fb3
		d->optind++;
Packit 709fb3
	      }
Packit 709fb3
	    else if (d->optind == argc)
Packit 709fb3
	      {
Packit 709fb3
		if (print_errors)
Packit 709fb3
		  fprintf (stderr,
Packit 709fb3
			   _("%s: option requires an argument -- '%c'\n"),
Packit 709fb3
			   argv[0], c);
Packit 709fb3
Packit 709fb3
		d->optopt = c;
Packit 709fb3
		if (optstring[0] == ':')
Packit 709fb3
		  c = ':';
Packit 709fb3
		else
Packit 709fb3
		  c = '?';
Packit 709fb3
	      }
Packit 709fb3
	    else
Packit 709fb3
	      /* We already incremented 'optind' once;
Packit 709fb3
		 increment it again when taking next ARGV-elt as argument.  */
Packit 709fb3
	      d->optarg = argv[d->optind++];
Packit 709fb3
	    d->__nextchar = NULL;
Packit 709fb3
	  }
Packit 709fb3
      }
Packit 709fb3
    return c;
Packit 709fb3
  }
Packit 709fb3
}
Packit 709fb3
Packit 709fb3
int
Packit 709fb3
_getopt_internal (int argc, char **argv, const char *optstring,
Packit 709fb3
		  const struct option *longopts, int *longind, int long_only,
Packit 709fb3
		  int posixly_correct)
Packit 709fb3
{
Packit 709fb3
  int result;
Packit 709fb3
Packit 709fb3
  getopt_data.optind = optind;
Packit 709fb3
  getopt_data.opterr = opterr;
Packit 709fb3
Packit 709fb3
  result = _getopt_internal_r (argc, argv, optstring, longopts,
Packit 709fb3
			       longind, long_only, &getopt_data,
Packit 709fb3
			       posixly_correct);
Packit 709fb3
Packit 709fb3
  optind = getopt_data.optind;
Packit 709fb3
  optarg = getopt_data.optarg;
Packit 709fb3
  optopt = getopt_data.optopt;
Packit 709fb3
Packit 709fb3
  return result;
Packit 709fb3
}
Packit 709fb3
Packit 709fb3
/* glibc gets a LSB-compliant getopt and a POSIX-complaint __posix_getopt.
Packit 709fb3
   Standalone applications just get a POSIX-compliant getopt.
Packit 709fb3
   POSIX and LSB both require these functions to take 'char *const *argv'
Packit 709fb3
   even though this is incorrect (because of the permutation).  */
Packit 709fb3
#define GETOPT_ENTRY(NAME, POSIXLY_CORRECT)			\
Packit 709fb3
  int								\
Packit 709fb3
  NAME (int argc, char *const *argv, const char *optstring)	\
Packit 709fb3
  {								\
Packit 709fb3
    return _getopt_internal (argc, (char **)argv, optstring,	\
Packit 709fb3
			     0, 0, 0, POSIXLY_CORRECT);		\
Packit 709fb3
  }
Packit 709fb3
Packit 709fb3
#ifdef _LIBC
Packit 709fb3
GETOPT_ENTRY(getopt, 0)
Packit 709fb3
GETOPT_ENTRY(__posix_getopt, 1)
Packit 709fb3
#else
Packit 709fb3
GETOPT_ENTRY(getopt, 1)
Packit 709fb3
#endif
Packit 709fb3
Packit 709fb3

Packit 709fb3
#ifdef TEST
Packit 709fb3
Packit 709fb3
/* Compile with -DTEST to make an executable for use in testing
Packit 709fb3
   the above definition of 'getopt'.  */
Packit 709fb3
Packit 709fb3
int
Packit 709fb3
main (int argc, char **argv)
Packit 709fb3
{
Packit 709fb3
  int c;
Packit 709fb3
  int digit_optind = 0;
Packit 709fb3
Packit 709fb3
  while (1)
Packit 709fb3
    {
Packit 709fb3
      int this_option_optind = optind ? optind : 1;
Packit 709fb3
Packit 709fb3
      c = getopt (argc, argv, "abc:d:0123456789");
Packit 709fb3
      if (c == -1)
Packit 709fb3
	break;
Packit 709fb3
Packit 709fb3
      switch (c)
Packit 709fb3
	{
Packit 709fb3
	case '0':
Packit 709fb3
	case '1':
Packit 709fb3
	case '2':
Packit 709fb3
	case '3':
Packit 709fb3
	case '4':
Packit 709fb3
	case '5':
Packit 709fb3
	case '6':
Packit 709fb3
	case '7':
Packit 709fb3
	case '8':
Packit 709fb3
	case '9':
Packit 709fb3
	  if (digit_optind != 0 && digit_optind != this_option_optind)
Packit 709fb3
	    printf ("digits occur in two different argv-elements.\n");
Packit 709fb3
	  digit_optind = this_option_optind;
Packit 709fb3
	  printf ("option %c\n", c);
Packit 709fb3
	  break;
Packit 709fb3
Packit 709fb3
	case 'a':
Packit 709fb3
	  printf ("option a\n");
Packit 709fb3
	  break;
Packit 709fb3
Packit 709fb3
	case 'b':
Packit 709fb3
	  printf ("option b\n");
Packit 709fb3
	  break;
Packit 709fb3
Packit 709fb3
	case 'c':
Packit 709fb3
	  printf ("option c with value '%s'\n", optarg);
Packit 709fb3
	  break;
Packit 709fb3
Packit 709fb3
	case '?':
Packit 709fb3
	  break;
Packit 709fb3
Packit 709fb3
	default:
Packit 709fb3
	  printf ("?? getopt returned character code 0%o ??\n", c);
Packit 709fb3
	}
Packit 709fb3
    }
Packit 709fb3
Packit 709fb3
  if (optind < argc)
Packit 709fb3
    {
Packit 709fb3
      printf ("non-option ARGV-elements: ");
Packit 709fb3
      while (optind < argc)
Packit 709fb3
	printf ("%s ", argv[optind++]);
Packit 709fb3
      printf ("\n");
Packit 709fb3
    }
Packit 709fb3
Packit 709fb3
  exit (0);
Packit 709fb3
}
Packit 709fb3
Packit 709fb3
#endif /* TEST */