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