Blame gnugetopt/getopt1.c

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