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 */