991
+ ��膩��� 1 /* Getopt for GNU.
+ ��膩��� 2 NOTE: getopt is now part of the C library, so if you don't know what
+ ��膩��� 3 "Keep this file name-space clean" means, talk to roland@gnu.ai.mit.edu
+ ��膩��� 4 before changing it!
+ ��膩��� 5
+ ��膩��� 6 Copyright (C) 1987, 88, 89, 90, 91, 92, 1993
+ ��膩��� 7 Free Software Foundation, Inc.
+ ��膩��� 8
+ ��膩��� 9 This program is free software; you can redistribute it and/or modify it
+ ��膩��� 10 under the terms of the GNU General Public License as published by the
+ ��膩��� 11 Free Software Foundation; either version 2, or (at your option) any
+ ��膩��� 12 later version.
+ ��膩��� 13
+ ��膩��� 14 This program is distributed in the hope that it will be useful,
+ ��膩��� 15 but WITHOUT ANY WARRANTY; without even the implied warranty of
+ ��膩��� 16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ ��膩��� 17 GNU General Public License for more details.
+ ��膩��� 18
+ ��膩��� 19 You should have received a copy of the GNU General Public License
+ ��膩��� 20 along with this program; if not, write to the Free Software
+ ��膩��� 21 Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. */
+ ��膩��� 22
+ ��膩��� 23 /* NOTE!!! AIX requires this to be the first thing in the file.
+ ��膩��� 24 Do not put ANYTHING before it! */
+ ��膩��� 25 #if !defined (__GNUC__) && defined (_AIX)
+ ��膩��� 26 #pragma alloca
+ ��膩��� 27 #endif
+ ��膩��� 28
+ ��膩��� 29 #ifdef HAVE_CONFIG_H
+ ��膩��� 30 #include "config.h"
+ ��膩��� 31 #endif
+ ��膩��� 32
+ ��膩��� 33 #ifdef __GNUC__
+ ��膩��� 34 #define alloca __builtin_alloca
+ ��膩��� 35 #else /* not __GNUC__ */
+ ��膩��� 36 #if defined (HAVE_ALLOCA_H) || (defined(sparc) && (defined(sun) || (!defined(USG) && !defined(SVR4) && !defined(__svr4__))))
+ ��膩��� 37 #include <alloca.h>
+ ��膩��� 38 #else
+ ��膩��� 39 #ifndef _AIX
+ ��膩��� 40 char *alloca ();
+ ��膩��� 41 #endif
+ ��膩��� 42 #endif /* alloca.h */
+ ��膩��� 43 #endif /* not __GNUC__ */
+ ��膩��� 44
+ ��膩��� 45 #if !__STDC__ && !defined(const) && IN_GCC
+ ��膩��� 46 #define const
+ ��膩��� 47 #endif
+ ��膩��� 48
+ ��膩��� 49 /* This tells Alpha OSF/1 not to define a getopt prototype in <stdio.h>. */
+ ��膩��� 50 #ifndef _NO_PROTO
+ ��膩��� 51 #define _NO_PROTO
+ ��膩��� 52 #endif
+ ��膩��� 53
+ ��膩��� 54 #include <stdio.h>
+ ��膩��� 55
+ ��膩��� 56 /* Comment out all this code if we are using the GNU C Library, and are not
+ ��膩��� 57 actually compiling the library itself. This code is part of the GNU C
+ ��膩��� 58 Library, but also included in many other GNU distributions. Compiling
+ ��膩��� 59 and linking in this code is a waste when using the GNU C library
+ ��膩��� 60 (especially if it is a shared library). Rather than having every GNU
+ ��膩��� 61 program understand `configure --with-gnu-libc' and omit the object files,
+ ��膩��� 62 it is simpler to just do this in the source for each such file. */
+ ��膩��� 63
+ ��膩��� 64 #if defined (_LIBC) || !defined (__GNU_LIBRARY__)
+ ��膩��� 65
+ ��膩��� 66
+ ��膩��� 67 /* This needs to come after some library #include
+ ��膩��� 68 to get __GNU_LIBRARY__ defined. */
+ ��膩��� 69 #ifdef __GNU_LIBRARY__
+ ��膩��� 70 #undef alloca
+ ��膩��� 71 /* Don't include stdlib.h for non-GNU C libraries because some of them
+ ��膩��� 72 contain conflicting prototypes for getopt. */
+ ��膩��� 73 #include <stdlib.h>
+ ��膩��� 74 #else /* Not GNU C library. */
+ ��膩��� 75 #define __alloca alloca
+ ��膩��� 76 #endif /* GNU C library. */
+ ��膩��� 77
+ ��膩��� 78 /* If GETOPT_COMPAT is defined, `+' as well as `--' can introduce a
+ ��膩��� 79 long-named option. Because this is not POSIX.2 compliant, it is
+ ��膩��� 80 being phased out. */
+ ��膩��� 81 /* #define GETOPT_COMPAT */
+ ��膩��� 82
+ ��膩��� 83 /* This version of `getopt' appears to the caller like standard Unix `getopt'
+ ��膩��� 84 but it behaves differently for the user, since it allows the user
+ ��膩��� 85 to intersperse the options with the other arguments.
+ ��膩��� 86
+ ��膩��� 87 As `getopt' works, it permutes the elements of ARGV so that,
+ ��膩��� 88 when it is done, all the options precede everything else. Thus
+ ��膩��� 89 all application programs are extended to handle flexible argument order.
+ ��膩��� 90
+ ��膩��� 91 Setting the environment variable POSIXLY_CORRECT disables permutation.
+ ��膩��� 92 Then the behavior is completely standard.
+ ��膩��� 93
+ ��膩��� 94 GNU application programs can use a third alternative mode in which
+ ��膩��� 95 they can distinguish the relative order of options and other arguments. */
+ ��膩��� 96
+ ��膩��� 97 #include "getopt.h"
+ ��膩��� 98
+ ��膩��� 99 /* For communication from `getopt' to the caller.
+ ��膩��� 100 When `getopt' finds an option that takes an argument,
+ ��膩��� 101 the argument value is returned here.
+ ��膩��� 102 Also, when `ordering' is RETURN_IN_ORDER,
+ ��膩��� 103 each non-option ARGV-element is returned here. */
+ ��膩��� 104
+ ��膩��� 105 char *optarg = 0;
+ ��膩��� 106
+ ��膩��� 107 /* Index in ARGV of the next element to be scanned.
+ ��膩��� 108 This is used for communication to and from the caller
+ ��膩��� 109 and for communication between successive calls to `getopt'.
+ ��膩��� 110
+ ��膩��� 111 On entry to `getopt', zero means this is the first call; initialize.
+ ��膩��� 112
+ ��膩��� 113 When `getopt' returns EOF, this is the index of the first of the
+ ��膩��� 114 non-option elements that the caller should itself scan.
+ ��膩��� 115
+ ��膩��� 116 Otherwise, `optind' communicates from one call to the next
+ ��膩��� 117 how much of ARGV has been scanned so far. */
+ ��膩��� 118
+ ��膩��� 119 /* XXX 1003.2 says this must be 1 before any call. */
+ ��膩��� 120 int optind = 0;
+ ��膩��� 121
+ ��膩��� 122 /* The next char to be scanned in the option-element
+ ��膩��� 123 in which the last option character we returned was found.
+ ��膩��� 124 This allows us to pick up the scan where we left off.
+ ��膩��� 125
+ ��膩��� 126 If this is zero, or a null string, it means resume the scan
+ ��膩��� 127 by advancing to the next ARGV-element. */
+ ��膩��� 128
+ ��膩��� 129 static char *nextchar;
+ ��膩��� 130
+ ��膩��� 131 /* Callers store zero here to inhibit the error message
+ ��膩��� 132 for unrecognized options. */
+ ��膩��� 133
+ ��膩��� 134 int opterr = 1;
+ ��膩��� 135
+ ��膩��� 136 /* Set to an option character which was unrecognized.
+ ��膩��� 137 This must be initialized on some systems to avoid linking in the
+ ��膩��� 138 system's own getopt implementation. */
+ ��膩��� 139
+ ��膩��� 140 int optopt = '?';
+ ��膩��� 141
+ ��膩��� 142 /* Describe how to deal with options that follow non-option ARGV-elements.
+ ��膩��� 143
+ ��膩��� 144 If the caller did not specify anything,
+ ��膩��� 145 the default is REQUIRE_ORDER if the environment variable
+ ��膩��� 146 POSIXLY_CORRECT is defined, PERMUTE otherwise.
+ ��膩��� 147
+ ��膩��� 148 REQUIRE_ORDER means don't recognize them as options;
+ ��膩��� 149 stop option processing when the first non-option is seen.
+ ��膩��� 150 This is what Unix does.
+ ��膩��� 151 This mode of operation is selected by either setting the environment
+ ��膩��� 152 variable POSIXLY_CORRECT, or using `+' as the first character
+ ��膩��� 153 of the list of option characters.
+ ��膩��� 154
+ ��膩��� 155 PERMUTE is the default. We permute the contents of ARGV as we scan,
+ ��膩��� 156 so that eventually all the non-options are at the end. This allows options
+ ��膩��� 157 to be given in any order, even with programs that were not written to
+ ��膩��� 158 expect this.
+ ��膩��� 159
+ ��膩��� 160 RETURN_IN_ORDER is an option available to programs that were written
+ ��膩��� 161 to expect options and other ARGV-elements in any order and that care about
+ ��膩��� 162 the ordering of the two. We describe each non-option ARGV-element
+ ��膩��� 163 as if it were the argument of an option with character code 1.
+ ��膩��� 164 Using `-' as the first character of the list of option characters
+ ��膩��� 165 selects this mode of operation.
+ ��膩��� 166
+ ��膩��� 167 The special argument `--' forces an end of option-scanning regardless
+ ��膩��� 168 of the value of `ordering'. In the case of RETURN_IN_ORDER, only
+ ��膩��� 169 `--' can cause `getopt' to return EOF with `optind' != ARGC. */
+ ��膩��� 170
+ ��膩��� 171 static enum
+ ��膩��� 172 {
+ ��膩��� 173 REQUIRE_ORDER, PERMUTE, RETURN_IN_ORDER
+ ��膩��� 174 } ordering;
+ ��膩��� 175
+ ��膩��� 176 #ifdef __GNU_LIBRARY__
+ ��膩��� 177 /* We want to avoid inclusion of string.h with non-GNU libraries
+ ��膩��� 178 because there are many ways it can cause trouble.
+ ��膩��� 179 On some systems, it contains special magic macros that don't work
+ ��膩��� 180 in GCC. */
+ ��膩��� 181 #include <string.h>
+ ��膩��� 182 #define my_index strchr
+ ��膩��� 183 #define my_bcopy(src, dst, n) memcpy ((dst), (src), (n))
+ ��膩��� 184 #else
+ ��膩��� 185
+ ��膩��� 186 /* Avoid depending on library functions or files
+ ��膩��� 187 whose names are inconsistent. */
+ ��膩��� 188
+ ��膩��� 189 char *getenv ();
+ ��膩��� 190
+ ��膩��� 191 static char *
+ ��膩��� 192 my_index (str, chr)
+ ��膩��� 193 const char *str;
+ ��膩��� 194 int chr;
+ ��膩��� 195 {
+ ��膩��� 196 while (*str)
+ ��膩��� 197 {
+ ��膩��� 198 if (*str == chr)
+ ��膩��� 199 return (char *) str;
+ ��膩��� 200 str++;
+ ��膩��� 201 }
+ ��膩��� 202 return 0;
+ ��膩��� 203 }
+ ��膩��� 204
+ ��膩��� 205 static void
+ ��膩��� 206 my_bcopy (from, to, size)
+ ��膩��� 207 const char *from;
+ ��膩��� 208 char *to;
+ ��膩��� 209 int size;
+ ��膩��� 210 {
+ ��膩��� 211 int i;
+ ��膩��� 212 for (i = 0; i < size; i++)
+ ��膩��� 213 to[i] = from[i];
+ ��膩��� 214 }
+ ��膩��� 215 #endif /* GNU C library. */
+ ��膩��� 216
+ ��膩��� 217 /* Handle permutation of arguments. */
+ ��膩��� 218
+ ��膩��� 219 /* Describe the part of ARGV that contains non-options that have
+ ��膩��� 220 been skipped. `first_nonopt' is the index in ARGV of the first of them;
+ ��膩��� 221 `last_nonopt' is the index after the last of them. */
+ ��膩��� 222
+ ��膩��� 223 static int first_nonopt;
+ ��膩��� 224 static int last_nonopt;
+ ��膩��� 225
+ ��膩��� 226 /* Exchange two adjacent subsequences of ARGV.
+ ��膩��� 227 One subsequence is elements [first_nonopt,last_nonopt)
+ ��膩��� 228 which contains all the non-options that have been skipped so far.
+ ��膩��� 229 The other is elements [last_nonopt,optind), which contains all
+ ��膩��� 230 the options processed since those non-options were skipped.
+ ��膩��� 231
+ ��膩��� 232 `first_nonopt' and `last_nonopt' are relocated so that they describe
+ ��膩��� 233 the new indices of the non-options in ARGV after they are moved. */
+ ��膩��� 234
+ ��膩��� 235 static void
+ ��膩��� 236 exchange (argv)
+ ��膩��� 237 char **argv;
+ ��膩��� 238 {
+ ��膩��� 239 int nonopts_size = (last_nonopt - first_nonopt) * sizeof (char *);
+ ��膩��� 240 char **temp = (char **) __alloca (nonopts_size);
+ ��膩��� 241
+ ��膩��� 242 /* Interchange the two blocks of data in ARGV. */
+ ��膩��� 243
+ ��膩��� 244 my_bcopy ((char *) &argv[first_nonopt], (char *) temp, nonopts_size);
+ ��膩��� 245 my_bcopy ((char *) &argv[last_nonopt], (char *) &argv[first_nonopt],
+ ��膩��� 246 (optind - last_nonopt) * sizeof (char *));
+ ��膩��� 247 my_bcopy ((char *) temp,
+ ��膩��� 248 (char *) &argv[first_nonopt + optind - last_nonopt],
+ ��膩��� 249 nonopts_size);
+ ��膩��� 250
+ ��膩��� 251 /* Update records for the slots the non-options now occupy. */
+ ��膩��� 252
+ ��膩��� 253 first_nonopt += (optind - last_nonopt);
+ ��膩��� 254 last_nonopt = optind;
+ ��膩��� 255 }
+ ��膩��� 256
+ ��膩��� 257 /* Scan elements of ARGV (whose length is ARGC) for option characters
+ ��膩��� 258 given in OPTSTRING.
+ ��膩��� 259
+ ��膩��� 260 If an element of ARGV starts with '-', and is not exactly "-" or "--",
+ ��膩��� 261 then it is an option element. The characters of this element
+ ��膩��� 262 (aside from the initial '-') are option characters. If `getopt'
+ ��膩��� 263 is called repeatedly, it returns successively each of the option characters
+ ��膩��� 264 from each of the option elements.
+ ��膩��� 265
+ ��膩��� 266 If `getopt' finds another option character, it returns that character,
+ ��膩��� 267 updating `optind' and `nextchar' so that the next call to `getopt' can
+ ��膩��� 268 resume the scan with the following option character or ARGV-element.
+ ��膩��� 269
+ ��膩��� 270 If there are no more option characters, `getopt' returns `EOF'.
+ ��膩��� 271 Then `optind' is the index in ARGV of the first ARGV-element
+ ��膩��� 272 that is not an option. (The ARGV-elements have been permuted
+ ��膩��� 273 so that those that are not options now come last.)
+ ��膩��� 274
+ ��膩��� 275 OPTSTRING is a string containing the legitimate option characters.
+ ��膩��� 276 If an option character is seen that is not listed in OPTSTRING,
+ ��膩��� 277 return '?' after printing an error message. If you set `opterr' to
+ ��膩��� 278 zero, the error message is suppressed but we still return '?'.
+ ��膩��� 279
+ ��膩��� 280 If a char in OPTSTRING is followed by a colon, that means it wants an arg,
+ ��膩��� 281 so the following text in the same ARGV-element, or the text of the following
+ ��膩��� 282 ARGV-element, is returned in `optarg'. Two colons mean an option that
+ ��膩��� 283 wants an optional arg; if there is text in the current ARGV-element,
+ ��膩��� 284 it is returned in `optarg', otherwise `optarg' is set to zero.
+ ��膩��� 285
+ ��膩��� 286 If OPTSTRING starts with `-' or `+', it requests different methods of
+ ��膩��� 287 handling the non-option ARGV-elements.
+ ��膩��� 288 See the comments about RETURN_IN_ORDER and REQUIRE_ORDER, above.
+ ��膩��� 289
+ ��膩��� 290 Long-named options begin with `--' instead of `-'.
+ ��膩��� 291 Their names may be abbreviated as long as the abbreviation is unique
+ ��膩��� 292 or is an exact match for some defined option. If they have an
+ ��膩��� 293 argument, it follows the option name in the same ARGV-element, separated
+ ��膩��� 294 from the option name by a `=', or else the in next ARGV-element.
+ ��膩��� 295 When `getopt' finds a long-named option, it returns 0 if that option's
+ ��膩��� 296 `flag' field is nonzero, the value of the option's `val' field
+ ��膩��� 297 if the `flag' field is zero.
+ ��膩��� 298
+ ��膩��� 299 The elements of ARGV aren't really const, because we permute them.
+ ��膩��� 300 But we pretend they're const in the prototype to be compatible
+ ��膩��� 301 with other systems.
+ ��膩��� 302
+ ��膩��� 303 LONGOPTS is a vector of `struct option' terminated by an
+ ��膩��� 304 element containing a name which is zero.
+ ��膩��� 305
+ ��膩��� 306 LONGIND returns the index in LONGOPT of the long-named option found.
+ ��膩��� 307 It is only valid when a long-named option has been found by the most
+ ��膩��� 308 recent call.
+ ��膩��� 309
+ ��膩��� 310 If LONG_ONLY is nonzero, '-' as well as '--' can introduce
+ ��膩��� 311 long-named options. */
+ ��膩��� 312
+ ��膩��� 313 int
+ ��膩��� 314 _getopt_internal (argc, argv, optstring, longopts, longind, long_only)
+ ��膩��� 315 int argc;
+ ��膩��� 316 char *const *argv;
+ ��膩��� 317 const char *optstring;
+ ��膩��� 318 const struct option *longopts;
+ ��膩��� 319 int *longind;
+ ��膩��� 320 int long_only;
+ ��膩��� 321 {
+ ��膩��� 322 int option_index;
+ ��膩��� 323
+ ��膩��� 324 optarg = 0;
+ ��膩��� 325
+ ��膩��� 326 /* Initialize the internal data when the first call is made.
+ ��膩��� 327 Start processing options with ARGV-element 1 (since ARGV-element 0
+ ��膩��� 328 is the program name); the sequence of previously skipped
+ ��膩��� 329 non-option ARGV-elements is empty. */
+ ��膩��� 330
+ ��膩��� 331 if (optind == 0)
+ ��膩��� 332 {
+ ��膩��� 333 first_nonopt = last_nonopt = optind = 1;
+ ��膩��� 334
+ ��膩��� 335 nextchar = NULL;
+ ��膩��� 336
+ ��膩��� 337 /* Determine how to handle the ordering of options and nonoptions. */
+ ��膩��� 338
+ ��膩��� 339 if (optstring[0] == '-')
+ ��膩��� 340 {
+ ��膩��� 341 ordering = RETURN_IN_ORDER;
+ ��膩��� 342 ++optstring;
+ ��膩��� 343 }
+ ��膩��� 344 else if (optstring[0] == '+')
+ ��膩��� 345 {
+ ��膩��� 346 ordering = REQUIRE_ORDER;
+ ��膩��� 347 ++optstring;
+ ��膩��� 348 }
+ ��膩��� 349 else if (getenv ("POSIXLY_CORRECT") != NULL)
+ ��膩��� 350 ordering = REQUIRE_ORDER;
+ ��膩��� 351 else
+ ��膩��� 352 ordering = PERMUTE;
+ ��膩��� 353 }
+ ��膩��� 354
+ ��膩��� 355 if (nextchar == NULL || *nextchar == '\0')
+ ��膩��� 356 {
+ ��膩��� 357 if (ordering == PERMUTE)
+ ��膩��� 358 {
+ ��膩��� 359 /* If we have just processed some options following some non-options,
+ ��膩��� 360 exchange them so that the options come first. */
+ ��膩��� 361
+ ��膩��� 362 if (first_nonopt != last_nonopt && last_nonopt != optind)
+ ��膩��� 363 exchange ((char **) argv);
+ ��膩��� 364 else if (last_nonopt != optind)
+ ��膩��� 365 first_nonopt = optind;
+ ��膩��� 366
+ ��膩��� 367 /* Now skip any additional non-options
+ ��膩��� 368 and extend the range of non-options previously skipped. */
+ ��膩��� 369
+ ��膩��� 370 while (optind < argc
+ ��膩��� 371 && (argv[optind][0] != '-' || argv[optind][1] == '\0')
+ ��膩��� 372 #ifdef GETOPT_COMPAT
+ ��膩��� 373 && (longopts == NULL
+ ��膩��� 374 || argv[optind][0] != '+' || argv[optind][1] == '\0')
+ ��膩��� 375 #endif /* GETOPT_COMPAT */
+ ��膩��� 376 )
+ ��膩��� 377 optind++;
+ ��膩��� 378 last_nonopt = optind;
+ ��膩��� 379 }
+ ��膩��� 380
+ ��膩��� 381 /* Special ARGV-element `--' means premature end of options.
+ ��膩��� 382 Skip it like a null option,
+ ��膩��� 383 then exchange with previous non-options as if it were an option,
+ ��膩��� 384 then skip everything else like a non-option. */
+ ��膩��� 385
+ ��膩��� 386 if (optind != argc && !strcmp (argv[optind], "--"))
+ ��膩��� 387 {
+ ��膩��� 388 optind++;
+ ��膩��� 389
+ ��膩��� 390 if (first_nonopt != last_nonopt && last_nonopt != optind)
+ ��膩��� 391 exchange ((char **) argv);
+ ��膩��� 392 else if (first_nonopt == last_nonopt)
+ ��膩��� 393 first_nonopt = optind;
+ ��膩��� 394 last_nonopt = argc;
+ ��膩��� 395
+ ��膩��� 396 optind = argc;
+ ��膩��� 397 }
+ ��膩��� 398
+ ��膩��� 399 /* If we have done all the ARGV-elements, stop the scan
+ ��膩��� 400 and back over any non-options that we skipped and permuted. */
+ ��膩��� 401
+ ��膩��� 402 if (optind == argc)
+ ��膩��� 403 {
+ ��膩��� 404 /* Set the next-arg-index to point at the non-options
+ ��膩��� 405 that we previously skipped, so the caller will digest them. */
+ ��膩��� 406 if (first_nonopt != last_nonopt)
+ ��膩��� 407 optind = first_nonopt;
+ ��膩��� 408 return EOF;
+ ��膩��� 409 }
+ ��膩��� 410
+ ��膩��� 411 /* If we have come to a non-option and did not permute it,
+ ��膩��� 412 either stop the scan or describe it to the caller and pass it by. */
+ ��膩��� 413
+ ��膩��� 414 if ((argv[optind][0] != '-' || argv[optind][1] == '\0')
+ ��膩��� 415 #ifdef GETOPT_COMPAT
+ ��膩��� 416 && (longopts == NULL
+ ��膩��� 417 || argv[optind][0] != '+' || argv[optind][1] == '\0')
+ ��膩��� 418 #endif /* GETOPT_COMPAT */
+ ��膩��� 419 )
+ ��膩��� 420 {
+ ��膩��� 421 if (ordering == REQUIRE_ORDER)
+ ��膩��� 422 return EOF;
+ ��膩��� 423 optarg = argv[optind++];
+ ��膩��� 424 return 1;
+ ��膩��� 425 }
+ ��膩��� 426
+ ��膩��� 427 /* We have found another option-ARGV-element.
+ ��膩��� 428 Start decoding its characters. */
+ ��膩��� 429
+ ��膩��� 430 nextchar = (argv[optind] + 1
+ ��膩��� 431 + (longopts != NULL && argv[optind][1] == '-'));
+ ��膩��� 432 }
+ ��膩��� 433
+ ��膩��� 434 if (longopts != NULL
+ ��膩��� 435 && ((argv[optind][0] == '-'
+ ��膩��� 436 && (argv[optind][1] == '-' || long_only))
+ ��膩��� 437 #ifdef GETOPT_COMPAT
+ ��膩��� 438 || argv[optind][0] == '+'
+ ��膩��� 439 #endif /* GETOPT_COMPAT */
+ ��膩��� 440 ))
+ ��膩��� 441 {
+ ��膩��� 442 const struct option *p;
+ ��膩��� 443 char *s = nextchar;
+ ��膩��� 444 int exact = 0;
+ ��膩��� 445 int ambig = 0;
+ ��膩��� 446 const struct option *pfound = NULL;
+ ��膩��� 447 int indfound;
+ ��膩��� 448
+ ��膩��� 449 while (*s && *s != '=')
+ ��膩��� 450 s++;
+ ��膩��� 451
+ ��膩��� 452 /* Test all options for either exact match or abbreviated matches. */
+ ��膩��� 453 for (p = longopts, option_index = 0; p->name;
+ ��膩��� 454 p++, option_index++)
+ ��膩��� 455 if (!strncmp (p->name, nextchar, s - nextchar))
+ ��膩��� 456 {
+ ��膩��� 457 if (s - nextchar == strlen (p->name))
+ ��膩��� 458 {
+ ��膩��� 459 /* Exact match found. */
+ ��膩��� 460 pfound = p;
+ ��膩��� 461 indfound = option_index;
+ ��膩��� 462 exact = 1;
+ ��膩��� 463 break;
+ ��膩��� 464 }
+ ��膩��� 465 else if (pfound == NULL)
+ ��膩��� 466 {
+ ��膩��� 467 /* First nonexact match found. */
+ ��膩��� 468 pfound = p;
+ ��膩��� 469 indfound = option_index;
+ ��膩��� 470 }
+ ��膩��� 471 else
+ ��膩��� 472 /* Second nonexact match found. */
+ ��膩��� 473 ambig = 1;
+ ��膩��� 474 }
+ ��膩��� 475
+ ��膩��� 476 if (ambig && !exact)
+ ��膩��� 477 {
+ ��膩��� 478 if (opterr)
+ ��膩��� 479 fprintf (stderr, "%s: option `%s' is ambiguous\n",
+ ��膩��� 480 argv[0], argv[optind]);
+ ��膩��� 481 nextchar += strlen (nextchar);
+ ��膩��� 482 optind++;
+ ��膩��� 483 return '?';
+ ��膩��� 484 }
+ ��膩��� 485
+ ��膩��� 486 if (pfound != NULL)
+ ��膩��� 487 {
+ ��膩��� 488 option_index = indfound;
+ ��膩��� 489 optind++;
+ ��膩��� 490 if (*s)
+ ��膩��� 491 {
+ ��膩��� 492 /* Don't test has_arg with >, because some C compilers don't
+ ��膩��� 493 allow it to be used on enums. */
+ ��膩��� 494 if (pfound->has_arg)
+ ��膩��� 495 optarg = s + 1;
+ ��膩��� 496 else
+ ��膩��� 497 {
+ ��膩��� 498 if (opterr)
+ ��膩��� 499 {
+ ��膩��� 500 if (argv[optind - 1][1] == '-')
+ ��膩��� 501 /* --option */
+ ��膩��� 502 fprintf (stderr,
+ ��膩��� 503 "%s: option `--%s' doesn't allow an argument\n",
+ ��膩��� 504 argv[0], pfound->name);
+ ��膩��� 505 else
+ ��膩��� 506 /* +option or -option */
+ ��膩��� 507 fprintf (stderr,
+ ��膩��� 508 "%s: option `%c%s' doesn't allow an argument\n",
+ ��膩��� 509 argv[0], argv[optind - 1][0], pfound->name);
+ ��膩��� 510 }
+ ��膩��� 511 nextchar += strlen (nextchar);
+ ��膩��� 512 return '?';
+ ��膩��� 513 }
+ ��膩��� 514 }
+ ��膩��� 515 else if (pfound->has_arg == 1)
+ ��膩��� 516 {
+ ��膩��� 517 if (optind < argc)
+ ��膩��� 518 optarg = argv[optind++];
+ ��膩��� 519 else
+ ��膩��� 520 {
+ ��膩��� 521 if (opterr)
+ ��膩��� 522 fprintf (stderr, "%s: option `%s' requires an argument\n",
+ ��膩��� 523 argv[0], argv[optind - 1]);
+ ��膩��� 524 nextchar += strlen (nextchar);
+ ��膩��� 525 return optstring[0] == ':' ? ':' : '?';
+ ��膩��� 526 }
+ ��膩��� 527 }
+ ��膩��� 528 nextchar += strlen (nextchar);
+ ��膩��� 529 if (longind != NULL)
+ ��膩��� 530 *longind = option_index;
+ ��膩��� 531 if (pfound->flag)
+ ��膩��� 532 {
+ ��膩��� 533 *(pfound->flag) = pfound->val;
+ ��膩��� 534 return 0;
+ ��膩��� 535 }
+ ��膩��� 536 return pfound->val;
+ ��膩��� 537 }
+ ��膩��� 538 /* Can't find it as a long option. If this is not getopt_long_only,
+ ��膩��� 539 or the option starts with '--' or is not a valid short
+ ��膩��� 540 option, then it's an error.
+ ��膩��� 541 Otherwise interpret it as a short option. */
+ ��膩��� 542 if (!long_only || argv[optind][1] == '-'
+ ��膩��� 543 #ifdef GETOPT_COMPAT
+ ��膩��� 544 || argv[optind][0] == '+'
+ ��膩��� 545 #endif /* GETOPT_COMPAT */
+ ��膩��� 546 || my_index (optstring, *nextchar) == NULL)
+ ��膩��� 547 {
+ ��膩��� 548 if (opterr)
+ ��膩��� 549 {
+ ��膩��� 550 if (argv[optind][1] == '-')
+ ��膩��� 551 /* --option */
+ ��膩��� 552 fprintf (stderr, "%s: unrecognized option `--%s'\n",
+ ��膩��� 553 argv[0], nextchar);
+ ��膩��� 554 else
+ ��膩��� 555 /* +option or -option */
+ ��膩��� 556 fprintf (stderr, "%s: unrecognized option `%c%s'\n",
+ ��膩��� 557 argv[0], argv[optind][0], nextchar);
+ ��膩��� 558 }
+ ��膩��� 559 nextchar = (char *) "";
+ ��膩��� 560 optind++;
+ ��膩��� 561 return '?';
+ ��膩��� 562 }
+ ��膩��� 563 }
+ ��膩��� 564
+ ��膩��� 565 /* Look at and handle the next option-character. */
+ ��膩��� 566
+ ��膩��� 567 {
+ ��膩��� 568 char c = *nextchar++;
+ ��膩��� 569 char *temp = my_index (optstring, c);
+ ��膩��� 570
+ ��膩��� 571 /* Increment `optind' when we start to process its last character. */
+ ��膩��� 572 if (*nextchar == '\0')
+ ��膩��� 573 ++optind;
+ ��膩��� 574
+ ��膩��� 575 if (temp == NULL || c == ':')
+ ��膩��� 576 {
+ ��膩��� 577 if (opterr)
+ ��膩��� 578 {
+ ��膩��� 579 #if 0
+ ��膩��� 580 if (c < 040 || c >= 0177)
+ ��膩��� 581 fprintf (stderr, "%s: unrecognized option, character code 0%o\n",
+ ��膩��� 582 argv[0], c);
+ ��膩��� 583 else
+ ��膩��� 584 fprintf (stderr, "%s: unrecognized option `-%c'\n", argv[0], c);
+ ��膩��� 585 #else
+ ��膩��� 586 /* 1003.2 specifies the format of this message. */
+ ��膩��� 587 fprintf (stderr, "%s: illegal option -- %c\n", argv[0], c);
+ ��膩��� 588 #endif
+ ��膩��� 589 }
+ ��膩��� 590 optopt = c;
+ ��膩��� 591 return '?';
+ ��膩��� 592 }
+ ��膩��� 593 if (temp[1] == ':')
+ ��膩��� 594 {
+ ��膩��� 595 if (temp[2] == ':')
+ ��膩��� 596 {
+ ��膩��� 597 /* This is an option that accepts an argument optionally. */
+ ��膩��� 598 if (*nextchar != '\0')
+ ��膩��� 599 {
+ ��膩��� 600 optarg = nextchar;
+ ��膩��� 601 optind++;
+ ��膩��� 602 }
+ ��膩��� 603 else
+ ��膩��� 604 optarg = 0;
+ ��膩��� 605 nextchar = NULL;
+ ��膩��� 606 }
+ ��膩��� 607 else
+ ��膩��� 608 {
+ ��膩��� 609 /* This is an option that requires an argument. */
+ ��膩��� 610 if (*nextchar != '\0')
+ ��膩��� 611 {
+ ��膩��� 612 optarg = nextchar;
+ ��膩��� 613 /* If we end this ARGV-element by taking the rest as an arg,
+ ��膩��� 614 we must advance to the next element now. */
+ ��膩��� 615 optind++;
+ ��膩��� 616 }
+ ��膩��� 617 else if (optind == argc)
+ ��膩��� 618 {
+ ��膩��� 619 if (opterr)
+ ��膩��� 620 {
+ ��膩��� 621 #if 0
+ ��膩��� 622 fprintf (stderr, "%s: option `-%c' requires an argument\n",
+ ��膩��� 623 argv[0], c);
+ ��膩��� 624 #else
+ ��膩��� 625 /* 1003.2 specifies the format of this message. */
+ ��膩��� 626 fprintf (stderr, "%s: option requires an argument -- %c\n",
+ ��膩��� 627 argv[0], c);
+ ��膩��� 628 #endif
+ ��膩��� 629 }
+ ��膩��� 630 optopt = c;
+ ��膩��� 631 if (optstring[0] == ':')
+ ��膩��� 632 c = ':';
+ ��膩��� 633 else
+ ��膩��� 634 c = '?';
+ ��膩��� 635 }
+ ��膩��� 636 else
+ ��膩��� 637 /* We already incremented `optind' once;
+ ��膩��� 638 increment it again when taking next ARGV-elt as argument. */
+ ��膩��� 639 optarg = argv[optind++];
+ ��膩��� 640 nextchar = NULL;
+ ��膩��� 641 }
+ ��膩��� 642 }
+ ��膩��� 643 return c;
+ ��膩��� 644 }
+ ��膩��� 645 }
+ ��膩��� 646
+ ��膩��� 647 int
+ ��膩��� 648 getopt (argc, argv, optstring)
+ ��膩��� 649 int argc;
+ ��膩��� 650 char *const *argv;
+ ��膩��� 651 const char *optstring;
+ ��膩��� 652 {
+ ��膩��� 653 return _getopt_internal (argc, argv, optstring,
+ ��膩��� 654 (const struct option *) 0,
+ ��膩��� 655 (int *) 0,
+ ��膩��� 656 0);
+ ��膩��� 657 }
+ ��膩��� 658
+ ��膩��� 659 #endif /* _LIBC or not __GNU_LIBRARY__. */
+ ��膩��� 660
+ ��膩��� 661 #ifdef TEST
+ ��膩��� 662
+ ��膩��� 663 /* Compile with -DTEST to make an executable for use in testing
+ ��膩��� 664 the above definition of `getopt'. */
+ ��膩��� 665
+ ��膩��� 666 int
+ ��膩��� 667 main (argc, argv)
+ ��膩��� 668 int argc;
+ ��膩��� 669 char **argv;
+ ��膩��� 670 {
+ ��膩��� 671 int c;
+ ��膩��� 672 int digit_optind = 0;
+ ��膩��� 673
+ ��膩��� 674 while (1)
+ ��膩��� 675 {
+ ��膩��� 676 int this_option_optind = optind ? optind : 1;
+ ��膩��� 677
+ ��膩��� 678 c = getopt (argc, argv, "abc:d:0123456789");
+ ��膩��� 679 if (c == EOF)
+ ��膩��� 680 break;
+ ��膩��� 681
+ ��膩��� 682 switch (c)
+ ��膩��� 683 {
+ ��膩��� 684 case '0':
+ ��膩��� 685 case '1':
+ ��膩��� 686 case '2':
+ ��膩��� 687 case '3':
+ ��膩��� 688 case '4':
+ ��膩��� 689 case '5':
+ ��膩��� 690 case '6':
+ ��膩��� 691 case '7':
+ ��膩��� 692 case '8':
+ ��膩��� 693 case '9':
+ ��膩��� 694 if (digit_optind != 0 && digit_optind != this_option_optind)
+ ��膩��� 695 printf ("digits occur in two different argv-elements.\n");
+ ��膩��� 696 digit_optind = this_option_optind;
+ ��膩��� 697 printf ("option %c\n", c);
+ ��膩��� 698 break;
+ ��膩��� 699
+ ��膩��� 700 case 'a':
+ ��膩��� 701 printf ("option a\n");
+ ��膩��� 702 break;
+ ��膩��� 703
+ ��膩��� 704 case 'b':
+ ��膩��� 705 printf ("option b\n");
+ ��膩��� 706 break;
+ ��膩��� 707
+ ��膩��� 708 case 'c':
+ ��膩��� 709 printf ("option c with value `%s'\n", optarg);
+ ��膩��� 710 break;
+ ��膩��� 711
+ ��膩��� 712 case '?':
+ ��膩��� 713 break;
+ ��膩��� 714
+ ��膩��� 715 default:
+ ��膩��� 716 printf ("?? getopt returned character code 0%o ??\n", c);
+ ��膩��� 717 }
+ ��膩��� 718 }
+ ��膩��� 719
+ ��膩��� 720 if (optind < argc)
+ ��膩��� 721 {
+ ��膩��� 722 printf ("non-option ARGV-elements: ");
+ ��膩��� 723 while (optind < argc)
+ ��膩��� 724 printf ("%s ", argv[optind++]);
+ ��膩��� 725 printf ("\n");
+ ��膩��� 726 }
+ ��膩��� 727
+ ��膩��� 728 exit (0);
+ ��膩��� 729 }
+ ��膩��� 730
+ ��膩��� 731 #endif /* TEST */