Blame lib/getopt.c

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