Blame src/share/getopt/getopt1.c

Packit Service 065837
/*
Packit Service 065837
	NOTE:
Packit Service 065837
	I cannot get the vanilla getopt code to work (i.e. compile only what
Packit Service 065837
	is needed and not duplicate symbols found in the standard library)
Packit Service 065837
	on all the platforms that FLAC supports.  In particular the gating
Packit Service 065837
	of code with the ELIDE_CODE #define is not accurate enough on systems
Packit Service 065837
	that are POSIX but not glibc.  If someone has a patch that works on
Packit Service 065837
	GNU/Linux, Darwin, AND Solaris please submit it on the project page:
Packit Service 065837
		https://sourceforge.net/p/flac/patches/
Packit Service 065837
Packit Service 065837
	In the meantime I have munged the global symbols and removed gates
Packit Service 065837
	around code, while at the same time trying to touch the original as
Packit Service 065837
	little as possible.
Packit Service 065837
*/
Packit Service 065837
/* getopt_long and getopt_long_only entry points for GNU getopt.
Packit Service 065837
   Copyright (C) 1987,88,89,90,91,92,93,94,96,97,98
Packit Service 065837
     Free Software Foundation, Inc.
Packit Service 065837
   This file is part of the GNU C Library.
Packit Service 065837
Packit Service 065837
   The GNU C Library is free software; you can redistribute it and/or
Packit Service 065837
   modify it under the terms of the GNU Library General Public License as
Packit Service 065837
   published by the Free Software Foundation; either version 2 of the
Packit Service 065837
   License, or (at your option) any later version.
Packit Service 065837
Packit Service 065837
   The GNU C Library is distributed in the hope that it will be useful,
Packit Service 065837
   but WITHOUT ANY WARRANTY; without even the implied warranty of
Packit Service 065837
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
Packit Service 065837
   Library General Public License for more details.
Packit Service 065837
Packit Service 065837
   You should have received a copy of the GNU Library General Public
Packit Service 065837
   License along with the GNU C Library; see the file COPYING.LIB.  If not,
Packit Service 065837
   write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
Packit Service 065837
   Boston, MA 02110-1301, USA.  */
Packit Service 065837

Packit Service 065837
#ifdef HAVE_CONFIG_H
Packit Service 065837
#  include <config.h>
Packit Service 065837
#endif
Packit Service 065837
Packit Service 065837
#include "share/getopt.h"
Packit Service 065837
/*[JEC] was:#include "getopt.h"*/
Packit Service 065837
Packit Service 065837
#if !defined __STDC__ || !__STDC__
Packit Service 065837
/* This is a separate conditional since some stdc systems
Packit Service 065837
   reject `defined (const)'.  */
Packit Service 065837
#ifndef const
Packit Service 065837
#define const
Packit Service 065837
#endif
Packit Service 065837
#endif
Packit Service 065837
Packit Service 065837
#include <stdio.h>
Packit Service 065837
Packit Service 065837
/* Comment out all this code if we are using the GNU C Library, and are not
Packit Service 065837
   actually compiling the library itself.  This code is part of the GNU C
Packit Service 065837
   Library, but also included in many other GNU distributions.  Compiling
Packit Service 065837
   and linking in this code is a waste when using the GNU C library
Packit Service 065837
   (especially if it is a shared library).  Rather than having every GNU
Packit Service 065837
   program understand `configure --with-gnu-libc' and omit the object files,
Packit Service 065837
   it is simpler to just do this in the source for each such file.  */
Packit Service 065837
Packit Service 065837
#define GETOPT_INTERFACE_VERSION 2
Packit Service 065837
#if !defined _LIBC && defined __GLIBC__ && __GLIBC__ >= 2
Packit Service 065837
#include <gnu-versions.h>
Packit Service 065837
#if _GNU_GETOPT_INTERFACE_VERSION == GETOPT_INTERFACE_VERSION
Packit Service 065837
#define ELIDE_CODE
Packit Service 065837
#endif
Packit Service 065837
#endif
Packit Service 065837
Packit Service 065837
#if 1
Packit Service 065837
/*[JEC] was:#ifndef ELIDE_CODE*/
Packit Service 065837
Packit Service 065837
Packit Service 065837
/* This needs to come after some library #include
Packit Service 065837
   to get __GNU_LIBRARY__ defined.  */
Packit Service 065837
#ifdef __GNU_LIBRARY__
Packit Service 065837
#include <stdlib.h>
Packit Service 065837
#endif
Packit Service 065837
Packit Service 065837
#ifndef	NULL
Packit Service 065837
#define NULL 0
Packit Service 065837
#endif
Packit Service 065837
Packit Service 065837
int
Packit Service 065837
share__getopt_long (argc, argv, options, long_options, opt_index)
Packit Service 065837
     int argc;
Packit Service 065837
     char *const *argv;
Packit Service 065837
     const char *options;
Packit Service 065837
     const struct share__option *long_options;
Packit Service 065837
     int *opt_index;
Packit Service 065837
{
Packit Service 065837
  return share___getopt_internal (argc, argv, options, long_options, opt_index, 0);
Packit Service 065837
}
Packit Service 065837
Packit Service 065837
/* Like share__getopt_long, but '-' as well as '--' can indicate a long option.
Packit Service 065837
   If an option that starts with '-' (not '--') doesn't match a long option,
Packit Service 065837
   but does match a short option, it is parsed as a short option
Packit Service 065837
   instead.  */
Packit Service 065837
Packit Service 065837
int
Packit Service 065837
share__getopt_long_only (argc, argv, options, long_options, opt_index)
Packit Service 065837
     int argc;
Packit Service 065837
     char *const *argv;
Packit Service 065837
     const char *options;
Packit Service 065837
     const struct share__option *long_options;
Packit Service 065837
     int *opt_index;
Packit Service 065837
{
Packit Service 065837
  return share___getopt_internal (argc, argv, options, long_options, opt_index, 1);
Packit Service 065837
}
Packit Service 065837
Packit Service 065837
Packit Service 065837
#endif	/* Not ELIDE_CODE.  */
Packit Service 065837

Packit Service 065837
#ifdef TEST
Packit Service 065837
Packit Service 065837
#include <stdio.h>
Packit Service 065837
Packit Service 065837
int
Packit Service 065837
main (argc, argv)
Packit Service 065837
     int argc;
Packit Service 065837
     char **argv;
Packit Service 065837
{
Packit Service 065837
  int c;
Packit Service 065837
  int digit_optind = 0;
Packit Service 065837
Packit Service 065837
  while (1)
Packit Service 065837
    {
Packit Service 065837
      int this_option_optind = share__optind ? share__optind : 1;
Packit Service 065837
      int option_index = 0;
Packit Service 065837
      static struct share__option long_options[] =
Packit Service 065837
      {
Packit Service 065837
	{"add", 1, 0, 0},
Packit Service 065837
	{"append", 0, 0, 0},
Packit Service 065837
	{"delete", 1, 0, 0},
Packit Service 065837
	{"verbose", 0, 0, 0},
Packit Service 065837
	{"create", 0, 0, 0},
Packit Service 065837
	{"file", 1, 0, 0},
Packit Service 065837
	{0, 0, 0, 0}
Packit Service 065837
      };
Packit Service 065837
Packit Service 065837
      c = share__getopt_long (argc, argv, "abc:d:0123456789",
Packit Service 065837
		       long_options, &option_index);
Packit Service 065837
      if (c == -1)
Packit Service 065837
	break;
Packit Service 065837
Packit Service 065837
      switch (c)
Packit Service 065837
	{
Packit Service 065837
	case 0:
Packit Service 065837
	  printf ("option %s", long_options[option_index].name);
Packit Service 065837
	  if (share__optarg)
Packit Service 065837
	    printf (" with arg %s", share__optarg);
Packit Service 065837
	  printf ("\n");
Packit Service 065837
	  break;
Packit Service 065837
Packit Service 065837
	case '0':
Packit Service 065837
	case '1':
Packit Service 065837
	case '2':
Packit Service 065837
	case '3':
Packit Service 065837
	case '4':
Packit Service 065837
	case '5':
Packit Service 065837
	case '6':
Packit Service 065837
	case '7':
Packit Service 065837
	case '8':
Packit Service 065837
	case '9':
Packit Service 065837
	  if (digit_optind != 0 && digit_optind != this_option_optind)
Packit Service 065837
	    printf ("digits occur in two different argv-elements.\n");
Packit Service 065837
	  digit_optind = this_option_optind;
Packit Service 065837
	  printf ("option %c\n", c);
Packit Service 065837
	  break;
Packit Service 065837
Packit Service 065837
	case 'a':
Packit Service 065837
	  printf ("option a\n");
Packit Service 065837
	  break;
Packit Service 065837
Packit Service 065837
	case 'b':
Packit Service 065837
	  printf ("option b\n");
Packit Service 065837
	  break;
Packit Service 065837
Packit Service 065837
	case 'c':
Packit Service 065837
	  printf ("option c with value `%s'\n", share__optarg);
Packit Service 065837
	  break;
Packit Service 065837
Packit Service 065837
	case 'd':
Packit Service 065837
	  printf ("option d with value `%s'\n", share__optarg);
Packit Service 065837
	  break;
Packit Service 065837
Packit Service 065837
	case '?':
Packit Service 065837
	  break;
Packit Service 065837
Packit Service 065837
	default:
Packit Service 065837
	  printf ("?? getopt returned character code 0%o ??\n", c);
Packit Service 065837
	}
Packit Service 065837
    }
Packit Service 065837
Packit Service 065837
  if (share__optind < argc)
Packit Service 065837
    {
Packit Service 065837
      printf ("non-option ARGV-elements: ");
Packit Service 065837
      while (share__optind < argc)
Packit Service 065837
	printf ("%s ", argv[share__optind++]);
Packit Service 065837
      printf ("\n");
Packit Service 065837
    }
Packit Service 065837
Packit Service 065837
  exit (0);
Packit Service 065837
}
Packit Service 065837
Packit Service 065837
#endif /* TEST */