annotate src/sdl/getopt1.c @ 303:5bcda2d6d135

item manipulation program works!
author Robert McIntyre <rlm@mit.edu>
date Sat, 31 Mar 2012 00:33:35 -0500
parents f9f4f1b99eed
children
rev   line source
rlm@1 1 /* getopt_long and getopt_long_only entry points for GNU getopt.
rlm@1 2 Copyright (C) 1987,88,89,90,91,92,93,94,96,97,98
rlm@1 3 Free Software Foundation, Inc.
rlm@1 4
rlm@1 5 NOTE: This source is derived from an old version taken from the GNU C
rlm@1 6 Library (glibc).
rlm@1 7
rlm@1 8 This program is free software; you can redistribute it and/or modify it
rlm@1 9 under the terms of the GNU General Public License as published by the
rlm@1 10 Free Software Foundation; either version 2, or (at your option) any
rlm@1 11 later version.
rlm@1 12
rlm@1 13 This program is distributed in the hope that it will be useful,
rlm@1 14 but WITHOUT ANY WARRANTY; without even the implied warranty of
rlm@1 15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
rlm@1 16 GNU General Public License for more details.
rlm@1 17
rlm@1 18 You should have received a copy of the GNU General Public License
rlm@1 19 along with this program; if not, write to the Free Software
rlm@1 20 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
rlm@1 21 USA. */
rlm@1 22
rlm@1 23 #ifdef HAVE_CONFIG_H
rlm@1 24 #include <config.h>
rlm@1 25 #endif
rlm@1 26
rlm@1 27 #include "getopt.h"
rlm@1 28
rlm@1 29 #if !defined __STDC__ || !__STDC__
rlm@1 30 /* This is a separate conditional since some stdc systems
rlm@1 31 reject `defined (const)'. */
rlm@1 32 #ifndef const
rlm@1 33 #define const
rlm@1 34 #endif
rlm@1 35 #endif
rlm@1 36
rlm@1 37 #include <stdio.h>
rlm@1 38
rlm@1 39 /* Comment out all this code if we are using the GNU C Library, and are not
rlm@1 40 actually compiling the library itself. This code is part of the GNU C
rlm@1 41 Library, but also included in many other GNU distributions. Compiling
rlm@1 42 and linking in this code is a waste when using the GNU C library
rlm@1 43 (especially if it is a shared library). Rather than having every GNU
rlm@1 44 program understand `configure --with-gnu-libc' and omit the object files,
rlm@1 45 it is simpler to just do this in the source for each such file. */
rlm@1 46
rlm@1 47 #define GETOPT_INTERFACE_VERSION 2
rlm@1 48 #if !defined _LIBC && defined __GLIBC__ && __GLIBC__ >= 2
rlm@1 49 #include <gnu-versions.h>
rlm@1 50 #if _GNU_GETOPT_INTERFACE_VERSION == GETOPT_INTERFACE_VERSION
rlm@1 51 #define ELIDE_CODE
rlm@1 52 #endif
rlm@1 53 #endif
rlm@1 54
rlm@1 55 #ifndef ELIDE_CODE
rlm@1 56
rlm@1 57
rlm@1 58 /* This needs to come after some library #include
rlm@1 59 to get __GNU_LIBRARY__ defined. */
rlm@1 60 #ifdef __GNU_LIBRARY__
rlm@1 61 #include <stdlib.h>
rlm@1 62 #endif
rlm@1 63
rlm@1 64 #ifndef NULL
rlm@1 65 #define NULL 0
rlm@1 66 #endif
rlm@1 67
rlm@1 68 int
rlm@1 69 getopt_long (argc, argv, options, long_options, opt_index)
rlm@1 70 int argc;
rlm@1 71 char *const *argv;
rlm@1 72 const char *options;
rlm@1 73 const struct option *long_options;
rlm@1 74 int *opt_index;
rlm@1 75 {
rlm@1 76 return _getopt_internal (argc, argv, options, long_options, opt_index, 0);
rlm@1 77 }
rlm@1 78
rlm@1 79 /* Like getopt_long, but '-' as well as '--' can indicate a long option.
rlm@1 80 If an option that starts with '-' (not '--') doesn't match a long option,
rlm@1 81 but does match a short option, it is parsed as a short option
rlm@1 82 instead. */
rlm@1 83
rlm@1 84 int
rlm@1 85 getopt_long_only (argc, argv, options, long_options, opt_index)
rlm@1 86 int argc;
rlm@1 87 char *const *argv;
rlm@1 88 const char *options;
rlm@1 89 const struct option *long_options;
rlm@1 90 int *opt_index;
rlm@1 91 {
rlm@1 92 return _getopt_internal (argc, argv, options, long_options, opt_index, 1);
rlm@1 93 }
rlm@1 94
rlm@1 95
rlm@1 96 #endif /* Not ELIDE_CODE. */
rlm@1 97
rlm@1 98 #ifdef TEST
rlm@1 99
rlm@1 100 #include <stdio.h>
rlm@1 101
rlm@1 102 int
rlm@1 103 main (argc, argv)
rlm@1 104 int argc;
rlm@1 105 char **argv;
rlm@1 106 {
rlm@1 107 int c;
rlm@1 108 int digit_optind = 0;
rlm@1 109
rlm@1 110 while (1)
rlm@1 111 {
rlm@1 112 int this_option_optind = optind ? optind : 1;
rlm@1 113 int option_index = 0;
rlm@1 114 static struct option long_options[] =
rlm@1 115 {
rlm@1 116 {"add", 1, 0, 0},
rlm@1 117 {"append", 0, 0, 0},
rlm@1 118 {"delete", 1, 0, 0},
rlm@1 119 {"verbose", 0, 0, 0},
rlm@1 120 {"create", 0, 0, 0},
rlm@1 121 {"file", 1, 0, 0},
rlm@1 122 {0, 0, 0, 0}
rlm@1 123 };
rlm@1 124
rlm@1 125 c = getopt_long (argc, argv, "abc:d:0123456789",
rlm@1 126 long_options, &option_index);
rlm@1 127 if (c == -1)
rlm@1 128 break;
rlm@1 129
rlm@1 130 switch (c)
rlm@1 131 {
rlm@1 132 case 0:
rlm@1 133 printf ("option %s", long_options[option_index].name);
rlm@1 134 if (optarg)
rlm@1 135 printf (" with arg %s", optarg);
rlm@1 136 printf ("\n");
rlm@1 137 break;
rlm@1 138
rlm@1 139 case '0':
rlm@1 140 case '1':
rlm@1 141 case '2':
rlm@1 142 case '3':
rlm@1 143 case '4':
rlm@1 144 case '5':
rlm@1 145 case '6':
rlm@1 146 case '7':
rlm@1 147 case '8':
rlm@1 148 case '9':
rlm@1 149 if (digit_optind != 0 && digit_optind != this_option_optind)
rlm@1 150 printf ("digits occur in two different argv-elements.\n");
rlm@1 151 digit_optind = this_option_optind;
rlm@1 152 printf ("option %c\n", c);
rlm@1 153 break;
rlm@1 154
rlm@1 155 case 'a':
rlm@1 156 printf ("option a\n");
rlm@1 157 break;
rlm@1 158
rlm@1 159 case 'b':
rlm@1 160 printf ("option b\n");
rlm@1 161 break;
rlm@1 162
rlm@1 163 case 'c':
rlm@1 164 printf ("option c with value `%s'\n", optarg);
rlm@1 165 break;
rlm@1 166
rlm@1 167 case 'd':
rlm@1 168 printf ("option d with value `%s'\n", optarg);
rlm@1 169 break;
rlm@1 170
rlm@1 171 case '?':
rlm@1 172 break;
rlm@1 173
rlm@1 174 default:
rlm@1 175 printf ("?? getopt returned character code 0%o ??\n", c);
rlm@1 176 }
rlm@1 177 }
rlm@1 178
rlm@1 179 if (optind < argc)
rlm@1 180 {
rlm@1 181 printf ("non-option ARGV-elements: ");
rlm@1 182 while (optind < argc)
rlm@1 183 printf ("%s ", argv[optind++]);
rlm@1 184 printf ("\n");
rlm@1 185 }
rlm@1 186
rlm@1 187 exit (0);
rlm@1 188 }
rlm@1 189
rlm@1 190 #endif /* TEST */