Blame utils/getopt.c

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