Blame support/getopt1.c

Packit 575503
/* getopt_long and getopt_long_only entry points for GNU getopt.
Packit 575503
   Copyright (C) 1987-2016 Free Software Foundation, Inc.
Packit 575503
   This file is part of the GNU C Library.
Packit 575503
Packit 575503
   The GNU C Library is free software; you can redistribute it and/or
Packit 575503
   modify it under the terms of the GNU Lesser General Public
Packit 575503
   License as published by the Free Software Foundation; either
Packit 575503
   version 2.1 of the License, or (at your option) any later version.
Packit 575503
Packit 575503
   The GNU C Library is distributed in the hope that it will be useful,
Packit 575503
   but WITHOUT ANY WARRANTY; without even the implied warranty of
Packit 575503
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
Packit 575503
   Lesser General Public License for more details.
Packit 575503
Packit 575503
   You should have received a copy of the GNU Lesser General Public
Packit 575503
   License along with the GNU C Library; if not, see
Packit 575503
   <http://www.gnu.org/licenses/>.  */
Packit 575503

Packit 575503
#ifdef HAVE_CONFIG_H
Packit 575503
#include <config.h>
Packit 575503
#endif
Packit 575503
Packit 575503
#ifdef _LIBC
Packit 575503
# include <getopt.h>
Packit 575503
#else
Packit 575503
# include "getopt.h"
Packit 575503
#endif
Packit 575503
#include "getopt_int.h"
Packit 575503
Packit 575503
#include <stdio.h>
Packit 575503
Packit 575503
/* Comment out all this code if we are using the GNU C Library, and are not
Packit 575503
   actually compiling the library itself.  This code is part of the GNU C
Packit 575503
   Library, but also included in many other GNU distributions.  Compiling
Packit 575503
   and linking in this code is a waste when using the GNU C library
Packit 575503
   (especially if it is a shared library).  Rather than having every GNU
Packit 575503
   program understand `configure --with-gnu-libc' and omit the object files,
Packit 575503
   it is simpler to just do this in the source for each such file.  */
Packit 575503
Packit 575503
#define GETOPT_INTERFACE_VERSION 2
Packit 575503
#if !defined _LIBC && defined __GLIBC__ && __GLIBC__ >= 2
Packit 575503
#include <gnu-versions.h>
Packit 575503
#if _GNU_GETOPT_INTERFACE_VERSION == GETOPT_INTERFACE_VERSION
Packit 575503
#define ELIDE_CODE
Packit 575503
#endif
Packit 575503
#endif
Packit 575503
Packit 575503
/* !@#$%^&*() !!!!!!!! */
Packit 575503
#ifdef GAWK
Packit 575503
#undef ELIDE_CODE
Packit 575503
#endif
Packit 575503
Packit 575503
#ifndef ELIDE_CODE
Packit 575503
Packit 575503
Packit 575503
/* This needs to come after some library #include
Packit 575503
   to get __GNU_LIBRARY__ defined.  */
Packit 575503
#ifdef __GNU_LIBRARY__
Packit 575503
#include <stdlib.h>
Packit 575503
#endif
Packit 575503
Packit 575503
#ifndef	NULL
Packit 575503
#define NULL 0
Packit 575503
#endif
Packit 575503
Packit 575503
int
Packit 575503
getopt_long (int argc, char *const *argv, const char *options,
Packit 575503
	     const struct option *long_options, int *opt_index)
Packit 575503
{
Packit 575503
  return _getopt_internal (argc, argv, options, long_options, opt_index, 0, 0);
Packit 575503
}
Packit 575503
Packit 575503
int
Packit 575503
_getopt_long_r (int argc, char *const *argv, const char *options,
Packit 575503
		const struct option *long_options, int *opt_index,
Packit 575503
		struct _getopt_data *d)
Packit 575503
{
Packit 575503
  return _getopt_internal_r (argc, argv, options, long_options, opt_index,
Packit 575503
			     0, d, 0);
Packit 575503
}
Packit 575503
Packit 575503
/* Like getopt_long, but '-' as well as '--' can indicate a long option.
Packit 575503
   If an option that starts with '-' (not '--') doesn't match a long option,
Packit 575503
   but does match a short option, it is parsed as a short option
Packit 575503
   instead.  */
Packit 575503
Packit 575503
int
Packit 575503
getopt_long_only (int argc, char *const *argv, const char *options,
Packit 575503
		  const struct option *long_options, int *opt_index)
Packit 575503
{
Packit 575503
  return _getopt_internal (argc, argv, options, long_options, opt_index, 1, 0);
Packit 575503
}
Packit 575503
Packit 575503
int
Packit 575503
_getopt_long_only_r (int argc, char *const *argv, const char *options,
Packit 575503
		     const struct option *long_options, int *opt_index,
Packit 575503
		     struct _getopt_data *d)
Packit 575503
{
Packit 575503
  return _getopt_internal_r (argc, argv, options, long_options, opt_index,
Packit 575503
			     1, d, 0);
Packit 575503
}
Packit 575503
Packit 575503
#endif	/* Not ELIDE_CODE.  */
Packit 575503

Packit 575503
#ifdef TEST
Packit 575503
Packit 575503
#include <stdio.h>
Packit 575503
Packit 575503
int
Packit 575503
main (int argc, char **argv)
Packit 575503
{
Packit 575503
  int c;
Packit 575503
  int digit_optind = 0;
Packit 575503
Packit 575503
  while (1)
Packit 575503
    {
Packit 575503
      int this_option_optind = optind ? optind : 1;
Packit 575503
      int option_index = 0;
Packit 575503
      static struct option long_options[] =
Packit 575503
      {
Packit 575503
	{"add", 1, 0, 0},
Packit 575503
	{"append", 0, 0, 0},
Packit 575503
	{"delete", 1, 0, 0},
Packit 575503
	{"verbose", 0, 0, 0},
Packit 575503
	{"create", 0, 0, 0},
Packit 575503
	{"file", 1, 0, 0},
Packit 575503
	{0, 0, 0, 0}
Packit 575503
      };
Packit 575503
Packit 575503
      c = getopt_long (argc, argv, "abc:d:0123456789",
Packit 575503
		       long_options, &option_index);
Packit 575503
      if (c == -1)
Packit 575503
	break;
Packit 575503
Packit 575503
      switch (c)
Packit 575503
	{
Packit 575503
	case 0:
Packit 575503
	  printf ("option %s", long_options[option_index].name);
Packit 575503
	  if (optarg)
Packit 575503
	    printf (" with arg %s", optarg);
Packit 575503
	  printf ("\n");
Packit 575503
	  break;
Packit 575503
Packit 575503
	case '0':
Packit 575503
	case '1':
Packit 575503
	case '2':
Packit 575503
	case '3':
Packit 575503
	case '4':
Packit 575503
	case '5':
Packit 575503
	case '6':
Packit 575503
	case '7':
Packit 575503
	case '8':
Packit 575503
	case '9':
Packit 575503
	  if (digit_optind != 0 && digit_optind != this_option_optind)
Packit 575503
	    printf ("digits occur in two different argv-elements.\n");
Packit 575503
	  digit_optind = this_option_optind;
Packit 575503
	  printf ("option %c\n", c);
Packit 575503
	  break;
Packit 575503
Packit 575503
	case 'a':
Packit 575503
	  printf ("option a\n");
Packit 575503
	  break;
Packit 575503
Packit 575503
	case 'b':
Packit 575503
	  printf ("option b\n");
Packit 575503
	  break;
Packit 575503
Packit 575503
	case 'c':
Packit 575503
	  printf ("option c with value `%s'\n", optarg);
Packit 575503
	  break;
Packit 575503
Packit 575503
	case 'd':
Packit 575503
	  printf ("option d with value `%s'\n", optarg);
Packit 575503
	  break;
Packit 575503
Packit 575503
	case '?':
Packit 575503
	  break;
Packit 575503
Packit 575503
	default:
Packit 575503
	  printf ("?? getopt returned character code 0%o ??\n", c);
Packit 575503
	}
Packit 575503
    }
Packit 575503
Packit 575503
  if (optind < argc)
Packit 575503
    {
Packit 575503
      printf ("non-option ARGV-elements: ");
Packit 575503
      while (optind < argc)
Packit 575503
	printf ("%s ", argv[optind++]);
Packit 575503
      printf ("\n");
Packit 575503
    }
Packit 575503
Packit 575503
  exit (0);
Packit 575503
}
Packit 575503
Packit 575503
#endif /* TEST */