Blame src/getopt1.c

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