Blame posix/getopt.c

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