Blame lib/getopt_long.c

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