diff src/sdl/getopt1.c @ 1:f9f4f1b99eed

importing src directory
author Robert McIntyre <rlm@mit.edu>
date Sat, 03 Mar 2012 10:31:27 -0600
parents
children
line wrap: on
line diff
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/src/sdl/getopt1.c	Sat Mar 03 10:31:27 2012 -0600
     1.3 @@ -0,0 +1,190 @@
     1.4 +/* getopt_long and getopt_long_only entry points for GNU getopt.
     1.5 +   Copyright (C) 1987,88,89,90,91,92,93,94,96,97,98
     1.6 +     Free Software Foundation, Inc.
     1.7 +
     1.8 +   NOTE: This source is derived from an old version taken from the GNU C
     1.9 +   Library (glibc).
    1.10 +
    1.11 +   This program is free software; you can redistribute it and/or modify it
    1.12 +   under the terms of the GNU General Public License as published by the
    1.13 +   Free Software Foundation; either version 2, or (at your option) any
    1.14 +   later version.
    1.15 +
    1.16 +   This program is distributed in the hope that it will be useful,
    1.17 +   but WITHOUT ANY WARRANTY; without even the implied warranty of
    1.18 +   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    1.19 +   GNU General Public License for more details.
    1.20 +
    1.21 +   You should have received a copy of the GNU General Public License
    1.22 +   along with this program; if not, write to the Free Software
    1.23 +   Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
    1.24 +   USA.  */
    1.25 +
    1.26 +#ifdef HAVE_CONFIG_H
    1.27 +#include <config.h>
    1.28 +#endif
    1.29 +
    1.30 +#include "getopt.h"
    1.31 +
    1.32 +#if !defined __STDC__ || !__STDC__
    1.33 +/* This is a separate conditional since some stdc systems
    1.34 +   reject `defined (const)'.  */
    1.35 +#ifndef const
    1.36 +#define const
    1.37 +#endif
    1.38 +#endif
    1.39 +
    1.40 +#include <stdio.h>
    1.41 +
    1.42 +/* Comment out all this code if we are using the GNU C Library, and are not
    1.43 +   actually compiling the library itself.  This code is part of the GNU C
    1.44 +   Library, but also included in many other GNU distributions.  Compiling
    1.45 +   and linking in this code is a waste when using the GNU C library
    1.46 +   (especially if it is a shared library).  Rather than having every GNU
    1.47 +   program understand `configure --with-gnu-libc' and omit the object files,
    1.48 +   it is simpler to just do this in the source for each such file.  */
    1.49 +
    1.50 +#define GETOPT_INTERFACE_VERSION 2
    1.51 +#if !defined _LIBC && defined __GLIBC__ && __GLIBC__ >= 2
    1.52 +#include <gnu-versions.h>
    1.53 +#if _GNU_GETOPT_INTERFACE_VERSION == GETOPT_INTERFACE_VERSION
    1.54 +#define ELIDE_CODE
    1.55 +#endif
    1.56 +#endif
    1.57 +
    1.58 +#ifndef ELIDE_CODE
    1.59 +
    1.60 +
    1.61 +/* This needs to come after some library #include
    1.62 +   to get __GNU_LIBRARY__ defined.  */
    1.63 +#ifdef __GNU_LIBRARY__
    1.64 +#include <stdlib.h>
    1.65 +#endif
    1.66 +
    1.67 +#ifndef	NULL
    1.68 +#define NULL 0
    1.69 +#endif
    1.70 +
    1.71 +int
    1.72 +getopt_long (argc, argv, options, long_options, opt_index)
    1.73 +     int argc;
    1.74 +     char *const *argv;
    1.75 +     const char *options;
    1.76 +     const struct option *long_options;
    1.77 +     int *opt_index;
    1.78 +{
    1.79 +  return _getopt_internal (argc, argv, options, long_options, opt_index, 0);
    1.80 +}
    1.81 +
    1.82 +/* Like getopt_long, but '-' as well as '--' can indicate a long option.
    1.83 +   If an option that starts with '-' (not '--') doesn't match a long option,
    1.84 +   but does match a short option, it is parsed as a short option
    1.85 +   instead.  */
    1.86 +
    1.87 +int
    1.88 +getopt_long_only (argc, argv, options, long_options, opt_index)
    1.89 +     int argc;
    1.90 +     char *const *argv;
    1.91 +     const char *options;
    1.92 +     const struct option *long_options;
    1.93 +     int *opt_index;
    1.94 +{
    1.95 +  return _getopt_internal (argc, argv, options, long_options, opt_index, 1);
    1.96 +}
    1.97 +
    1.98 +
    1.99 +#endif	/* Not ELIDE_CODE.  */
   1.100 +
   1.101 +#ifdef TEST
   1.102 +
   1.103 +#include <stdio.h>
   1.104 +
   1.105 +int
   1.106 +main (argc, argv)
   1.107 +     int argc;
   1.108 +     char **argv;
   1.109 +{
   1.110 +  int c;
   1.111 +  int digit_optind = 0;
   1.112 +
   1.113 +  while (1)
   1.114 +    {
   1.115 +      int this_option_optind = optind ? optind : 1;
   1.116 +      int option_index = 0;
   1.117 +      static struct option long_options[] =
   1.118 +      {
   1.119 +	{"add", 1, 0, 0},
   1.120 +	{"append", 0, 0, 0},
   1.121 +	{"delete", 1, 0, 0},
   1.122 +	{"verbose", 0, 0, 0},
   1.123 +	{"create", 0, 0, 0},
   1.124 +	{"file", 1, 0, 0},
   1.125 +	{0, 0, 0, 0}
   1.126 +      };
   1.127 +
   1.128 +      c = getopt_long (argc, argv, "abc:d:0123456789",
   1.129 +		       long_options, &option_index);
   1.130 +      if (c == -1)
   1.131 +	break;
   1.132 +
   1.133 +      switch (c)
   1.134 +	{
   1.135 +	case 0:
   1.136 +	  printf ("option %s", long_options[option_index].name);
   1.137 +	  if (optarg)
   1.138 +	    printf (" with arg %s", optarg);
   1.139 +	  printf ("\n");
   1.140 +	  break;
   1.141 +
   1.142 +	case '0':
   1.143 +	case '1':
   1.144 +	case '2':
   1.145 +	case '3':
   1.146 +	case '4':
   1.147 +	case '5':
   1.148 +	case '6':
   1.149 +	case '7':
   1.150 +	case '8':
   1.151 +	case '9':
   1.152 +	  if (digit_optind != 0 && digit_optind != this_option_optind)
   1.153 +	    printf ("digits occur in two different argv-elements.\n");
   1.154 +	  digit_optind = this_option_optind;
   1.155 +	  printf ("option %c\n", c);
   1.156 +	  break;
   1.157 +
   1.158 +	case 'a':
   1.159 +	  printf ("option a\n");
   1.160 +	  break;
   1.161 +
   1.162 +	case 'b':
   1.163 +	  printf ("option b\n");
   1.164 +	  break;
   1.165 +
   1.166 +	case 'c':
   1.167 +	  printf ("option c with value `%s'\n", optarg);
   1.168 +	  break;
   1.169 +
   1.170 +	case 'd':
   1.171 +	  printf ("option d with value `%s'\n", optarg);
   1.172 +	  break;
   1.173 +
   1.174 +	case '?':
   1.175 +	  break;
   1.176 +
   1.177 +	default:
   1.178 +	  printf ("?? getopt returned character code 0%o ??\n", c);
   1.179 +	}
   1.180 +    }
   1.181 +
   1.182 +  if (optind < argc)
   1.183 +    {
   1.184 +      printf ("non-option ARGV-elements: ");
   1.185 +      while (optind < argc)
   1.186 +	printf ("%s ", argv[optind++]);
   1.187 +      printf ("\n");
   1.188 +    }
   1.189 +
   1.190 +  exit (0);
   1.191 +}
   1.192 +
   1.193 +#endif /* TEST */