diff options
Diffstat (limited to 'src/main/getopt.c')
-rw-r--r-- | src/main/getopt.c | 1139 |
1 files changed, 576 insertions, 563 deletions
diff --git a/src/main/getopt.c b/src/main/getopt.c index b64fdf5..890aa9b 100644 --- a/src/main/getopt.c +++ b/src/main/getopt.c | |||
@@ -4,7 +4,7 @@ | |||
4 | before changing it! | 4 | before changing it! |
5 | 5 | ||
6 | Copyright Copyright (C) 1987, 88, 89, 90, 91, 92, 93, 94, 95, 96, 97 | 6 | Copyright Copyright (C) 1987, 88, 89, 90, 91, 92, 93, 94, 95, 96, 97 |
7 | Free Software Foundation, Inc. | 7 | Free Software Foundation, Inc. |
8 | 8 | ||
9 | NOTE: The canonical source of this file is maintained with the GNU C Library. | 9 | NOTE: The canonical source of this file is maintained with the GNU C Library. |
10 | Bugs can be reported to bug-glibc@prep.ai.mit.edu. | 10 | Bugs can be reported to bug-glibc@prep.ai.mit.edu. |
@@ -32,7 +32,7 @@ USA. */ | |||
32 | 32 | ||
33 | #include "config.h" | 33 | #include "config.h" |
34 | 34 | ||
35 | #if !defined (__STDC__) || !__STDC__ | 35 | #if ! defined (__STDC__) || ! __STDC__ |
36 | /* This is a separate conditional since some stdc systems | 36 | /* This is a separate conditional since some stdc systems |
37 | reject `defined (const)'. */ | 37 | reject `defined (const)'. */ |
38 | #ifndef const | 38 | #ifndef const |
@@ -51,7 +51,7 @@ USA. */ | |||
51 | it is simpler to just do this in the source for each such file. */ | 51 | it is simpler to just do this in the source for each such file. */ |
52 | 52 | ||
53 | #define GETOPT_INTERFACE_VERSION 2 | 53 | #define GETOPT_INTERFACE_VERSION 2 |
54 | #if !defined (_LIBC) && defined (__GLIBC__) && __GLIBC__ >= 2 | 54 | #if ! defined (_LIBC) && defined (__GLIBC__) && __GLIBC__ >= 2 |
55 | #include <gnu-versions.h> | 55 | #include <gnu-versions.h> |
56 | #if _GNU_GETOPT_INTERFACE_VERSION == GETOPT_INTERFACE_VERSION | 56 | #if _GNU_GETOPT_INTERFACE_VERSION == GETOPT_INTERFACE_VERSION |
57 | #define ELIDE_CODE | 57 | #define ELIDE_CODE |
@@ -63,7 +63,7 @@ USA. */ | |||
63 | 63 | ||
64 | /* This needs to come after some library #include | 64 | /* This needs to come after some library #include |
65 | to get __GNU_LIBRARY__ defined. */ | 65 | to get __GNU_LIBRARY__ defined. */ |
66 | #ifdef __GNU_LIBRARY__ | 66 | #ifdef __GNU_LIBRARY__ |
67 | /* Don't include stdlib.h for non-GNU C libraries because some of them | 67 | /* Don't include stdlib.h for non-GNU C libraries because some of them |
68 | contain conflicting prototypes for getopt. */ | 68 | contain conflicting prototypes for getopt. */ |
69 | #include <stdlib.h> | 69 | #include <stdlib.h> |
@@ -77,10 +77,10 @@ USA. */ | |||
77 | #include <string.h> | 77 | #include <string.h> |
78 | #endif | 78 | #endif |
79 | 79 | ||
80 | #if defined (WIN32) && !defined (__CYGWIN32__) | 80 | #if defined (WIN32) && ! defined (__CYGWIN32__) |
81 | /* It's not Unix, really. See? Capital letters. */ | 81 | /* It's not Unix, really. See? Capital letters. */ |
82 | #include <windows.h> | 82 | #include <windows.h> |
83 | #define getpid() GetCurrentProcessId() | 83 | #define getpid() GetCurrentProcessId () |
84 | #endif | 84 | #endif |
85 | 85 | ||
86 | #ifndef _ | 86 | #ifndef _ |
@@ -88,9 +88,9 @@ USA. */ | |||
88 | When compiling libc, the _ macro is predefined. */ | 88 | When compiling libc, the _ macro is predefined. */ |
89 | #ifdef NEVER_HAVE_LIBINTL_H | 89 | #ifdef NEVER_HAVE_LIBINTL_H |
90 | # include <libintl.h> | 90 | # include <libintl.h> |
91 | # define _(msgid) gettext (msgid) | 91 | # define _(msgid) gettext (msgid) |
92 | #else | 92 | #else |
93 | # define _(msgid) (msgid) | 93 | # define _(msgid) (msgid) |
94 | #endif | 94 | #endif |
95 | #endif | 95 | #endif |
96 | 96 | ||
@@ -195,14 +195,14 @@ static enum | |||
195 | 195 | ||
196 | /* Value of POSIXLY_CORRECT environment variable. */ | 196 | /* Value of POSIXLY_CORRECT environment variable. */ |
197 | static char *posixly_correct; | 197 | static char *posixly_correct; |
198 | 198 | ||
199 | #ifdef __GNU_LIBRARY__ | 199 | #ifdef __GNU_LIBRARY__ |
200 | /* We want to avoid inclusion of string.h with non-GNU libraries | 200 | /* We want to avoid inclusion of string.h with non-GNU libraries |
201 | because there are many ways it can cause trouble. | 201 | because there are many ways it can cause trouble. |
202 | On some systems, it contains special magic macros that don't work | 202 | On some systems, it contains special magic macros that don't work |
203 | in GCC. */ | 203 | in GCC. */ |
204 | #include <string.h> | 204 | #include <string.h> |
205 | #define my_index strchr | 205 | #define my_index strchr |
206 | #else | 206 | #else |
207 | 207 | ||
208 | /* Avoid depending on library functions or files | 208 | /* Avoid depending on library functions or files |
@@ -212,15 +212,16 @@ char *getenv (); | |||
212 | 212 | ||
213 | static char * | 213 | static char * |
214 | my_index (str, chr) | 214 | my_index (str, chr) |
215 | const char *str; | 215 | const char *str; |
216 | int chr; | 216 | |
217 | int chr; | ||
217 | { | 218 | { |
218 | while (*str) | 219 | while (*str) |
219 | { | 220 | { |
220 | if (*str == chr) | 221 | if (*str == chr) |
221 | return (char *) str; | 222 | return (char *) str; |
222 | str++; | 223 | str++; |
223 | } | 224 | } |
224 | return 0; | 225 | return 0; |
225 | } | 226 | } |
226 | 227 | ||
@@ -229,18 +230,20 @@ my_index (str, chr) | |||
229 | #ifdef __GNUC__ | 230 | #ifdef __GNUC__ |
230 | /* Note that Motorola Delta 68k R3V7 comes with GCC but not stddef.h. | 231 | /* Note that Motorola Delta 68k R3V7 comes with GCC but not stddef.h. |
231 | That was relevant to code that was here before. */ | 232 | That was relevant to code that was here before. */ |
232 | #if !defined (__STDC__) || !__STDC__ | 233 | #if ! defined (__STDC__) || ! __STDC__ |
233 | /* gcc with -traditional declares the built-in strlen to return int, | 234 | /* gcc with -traditional declares the built-in strlen to return int, |
234 | and has done so at least since version 2.4.5. -- rms. */ | 235 | and has done so at least since version 2.4.5. -- rms. */ |
235 | extern int strlen (const char *); | 236 | extern int strlen (const char *); |
237 | |||
236 | #endif /* not __STDC__ */ | 238 | #endif /* not __STDC__ */ |
237 | #if defined(__APPLE__) | 239 | #if defined(__APPLE__) |
238 | extern size_t strlen (const char *); | 240 | extern size_t strlen (const char *); |
241 | |||
239 | #endif | 242 | #endif |
240 | #endif /* __GNUC__ */ | 243 | #endif /* __GNUC__ */ |
241 | 244 | ||
242 | #endif /* not __GNU_LIBRARY__ */ | 245 | #endif /* not __GNU_LIBRARY__ */ |
243 | 246 | ||
244 | /* Handle permutation of arguments. */ | 247 | /* Handle permutation of arguments. */ |
245 | 248 | ||
246 | /* Describe the part of ARGV that contains non-options that have | 249 | /* Describe the part of ARGV that contains non-options that have |
@@ -269,7 +272,8 @@ extern pid_t __libc_pid; | |||
269 | is valid for the getopt call we must make sure that the ARGV passed | 272 | is valid for the getopt call we must make sure that the ARGV passed |
270 | to getopt is that one passed to the process. */ | 273 | to getopt is that one passed to the process. */ |
271 | static void | 274 | static void |
272 | __attribute__ ((unused)) store_args_and_env (int argc, char *const *argv) | 275 | __attribute__ ((unused)) |
276 | store_args_and_env (int argc, char *const *argv) | ||
273 | { | 277 | { |
274 | /* XXX This is no good solution. We should rather copy the args so | 278 | /* XXX This is no good solution. We should rather copy the args so |
275 | that we can compare them later. But we must not use malloc(3). */ | 279 | that we can compare them later. But we must not use malloc(3). */ |
@@ -277,15 +281,16 @@ static void | |||
277 | original_argv = argv; | 281 | original_argv = argv; |
278 | } | 282 | } |
279 | 283 | ||
284 | |||
280 | text_set_element (__libc_subinit, store_args_and_env); | 285 | text_set_element (__libc_subinit, store_args_and_env); |
281 | 286 | ||
282 | # define SWAP_FLAGS(ch1, ch2) \ | 287 | # define SWAP_FLAGS(ch1, ch2) \ |
283 | if (nonoption_flags_len > 0) \ | 288 | if (nonoption_flags_len > 0) \ |
284 | { \ | 289 | { \ |
285 | char __tmp = __getopt_nonoption_flags[ch1]; \ | 290 | char __tmp = __getopt_nonoption_flags[ch1]; \ |
286 | __getopt_nonoption_flags[ch1] = __getopt_nonoption_flags[ch2]; \ | 291 | __getopt_nonoption_flags[ch1] = __getopt_nonoption_flags[ch2]; \ |
287 | __getopt_nonoption_flags[ch2] = __tmp; \ | 292 | __getopt_nonoption_flags[ch2] = __tmp; \ |
288 | } | 293 | } |
289 | #else /* !_LIBC */ | 294 | #else /* !_LIBC */ |
290 | # define SWAP_FLAGS(ch1, ch2) | 295 | # define SWAP_FLAGS(ch1, ch2) |
291 | #endif /* _LIBC */ | 296 | #endif /* _LIBC */ |
@@ -301,11 +306,13 @@ text_set_element (__libc_subinit, store_args_and_env); | |||
301 | 306 | ||
302 | #if defined (__STDC__) && __STDC__ | 307 | #if defined (__STDC__) && __STDC__ |
303 | static void exchange (char **); | 308 | static void exchange (char **); |
309 | |||
304 | #endif | 310 | #endif |
305 | 311 | ||
306 | static void | 312 | static void |
307 | exchange (argv) | 313 | exchange (argv) |
308 | char **argv; | 314 | char **argv; |
315 | |||
309 | { | 316 | { |
310 | int bottom = first_nonopt; | 317 | int bottom = first_nonopt; |
311 | int middle = last_nonopt; | 318 | int middle = last_nonopt; |
@@ -321,61 +328,61 @@ exchange (argv) | |||
321 | /* First make sure the handling of the `__getopt_nonoption_flags' | 328 | /* First make sure the handling of the `__getopt_nonoption_flags' |
322 | string can work normally. Our top argument must be in the range | 329 | string can work normally. Our top argument must be in the range |
323 | of the string. */ | 330 | of the string. */ |
324 | if (nonoption_flags_len > 0 && top >= nonoption_flags_max_len) | 331 | if ((nonoption_flags_len > 0) && (top >= nonoption_flags_max_len)) |
332 | { | ||
333 | /* We must extend the array. The user plays games with us and | ||
334 | presents new arguments. */ | ||
335 | char *new_str = malloc (top + 1); | ||
336 | if (new_str == NULL) | ||
337 | nonoption_flags_len = nonoption_flags_max_len = 0; | ||
338 | else | ||
325 | { | 339 | { |
326 | /* We must extend the array. The user plays games with us and | 340 | memcpy (new_str, __getopt_nonoption_flags, nonoption_flags_max_len); |
327 | presents new arguments. */ | 341 | memset (&new_str[nonoption_flags_max_len], '\0', |
328 | char *new_str = malloc (top + 1); | 342 | top + 1 - nonoption_flags_max_len); |
329 | if (new_str == NULL) | 343 | nonoption_flags_max_len = top + 1; |
330 | nonoption_flags_len = nonoption_flags_max_len = 0; | 344 | __getopt_nonoption_flags = new_str; |
331 | else | ||
332 | { | ||
333 | memcpy (new_str, __getopt_nonoption_flags, nonoption_flags_max_len); | ||
334 | memset (&new_str[nonoption_flags_max_len], '\0', | ||
335 | top + 1 - nonoption_flags_max_len); | ||
336 | nonoption_flags_max_len = top + 1; | ||
337 | __getopt_nonoption_flags = new_str; | ||
338 | } | ||
339 | } | 345 | } |
346 | } | ||
340 | #endif | 347 | #endif |
341 | 348 | ||
342 | while (top > middle && middle > bottom) | 349 | while (top > middle && middle > bottom) |
350 | { | ||
351 | if (top - middle > middle - bottom) | ||
343 | { | 352 | { |
344 | if (top - middle > middle - bottom) | 353 | /* Bottom segment is the short one. */ |
345 | { | 354 | int len = middle - bottom; |
346 | /* Bottom segment is the short one. */ | 355 | register int i; |
347 | int len = middle - bottom; | 356 | |
348 | register int i; | 357 | /* Swap it with the top part of the top segment. */ |
349 | 358 | for (i = 0; i < len; i++) | |
350 | /* Swap it with the top part of the top segment. */ | 359 | { |
351 | for (i = 0; i < len; i++) | 360 | tem = argv[bottom + i]; |
352 | { | 361 | argv[bottom + i] = argv[top - (middle - bottom) + i]; |
353 | tem = argv[bottom + i]; | 362 | argv[top - (middle - bottom) + i] = tem; |
354 | argv[bottom + i] = argv[top - (middle - bottom) + i]; | 363 | SWAP_FLAGS (bottom + i, top - (middle - bottom) + i); |
355 | argv[top - (middle - bottom) + i] = tem; | 364 | } |
356 | SWAP_FLAGS (bottom + i, top - (middle - bottom) + i); | 365 | /* Exclude the moved bottom segment from further swapping. */ |
357 | } | 366 | top -= len; |
358 | /* Exclude the moved bottom segment from further swapping. */ | 367 | } |
359 | top -= len; | 368 | else |
360 | } | 369 | { |
361 | else | 370 | /* Top segment is the short one. */ |
362 | { | 371 | int len = top - middle; |
363 | /* Top segment is the short one. */ | 372 | register int i; |
364 | int len = top - middle; | 373 | |
365 | register int i; | 374 | /* Swap it with the bottom part of the bottom segment. */ |
366 | 375 | for (i = 0; i < len; i++) | |
367 | /* Swap it with the bottom part of the bottom segment. */ | 376 | { |
368 | for (i = 0; i < len; i++) | 377 | tem = argv[bottom + i]; |
369 | { | 378 | argv[bottom + i] = argv[middle + i]; |
370 | tem = argv[bottom + i]; | 379 | argv[middle + i] = tem; |
371 | argv[bottom + i] = argv[middle + i]; | 380 | SWAP_FLAGS (bottom + i, middle + i); |
372 | argv[middle + i] = tem; | 381 | } |
373 | SWAP_FLAGS (bottom + i, middle + i); | 382 | /* Exclude the moved top segment from further swapping. */ |
374 | } | 383 | bottom += len; |
375 | /* Exclude the moved top segment from further swapping. */ | ||
376 | bottom += len; | ||
377 | } | ||
378 | } | 384 | } |
385 | } | ||
379 | 386 | ||
380 | /* Update records for the slots the non-options now occupy. */ | 387 | /* Update records for the slots the non-options now occupy. */ |
381 | 388 | ||
@@ -387,12 +394,14 @@ exchange (argv) | |||
387 | 394 | ||
388 | #if defined (__STDC__) && __STDC__ | 395 | #if defined (__STDC__) && __STDC__ |
389 | static const char *_getopt_initialize (int, char *const *, const char *); | 396 | static const char *_getopt_initialize (int, char *const *, const char *); |
397 | |||
390 | #endif | 398 | #endif |
391 | static const char * | 399 | static const char * |
392 | _getopt_initialize (argc, argv, optstring) | 400 | _getopt_initialize (argc, argv, optstring) |
393 | int argc; | 401 | int argc; |
394 | char *const *argv; | 402 | |
395 | const char *optstring; | 403 | char *const *argv; |
404 | const char *optstring; | ||
396 | { | 405 | { |
397 | /* Start processing options with ARGV-element 1 (since ARGV-element 0 | 406 | /* Start processing options with ARGV-element 1 (since ARGV-element 0 |
398 | is the program name); the sequence of previously skipped | 407 | is the program name); the sequence of previously skipped |
@@ -407,56 +416,56 @@ _getopt_initialize (argc, argv, optstring) | |||
407 | /* Determine how to handle the ordering of options and nonoptions. */ | 416 | /* Determine how to handle the ordering of options and nonoptions. */ |
408 | 417 | ||
409 | if (optstring[0] == '-') | 418 | if (optstring[0] == '-') |
410 | { | 419 | { |
411 | ordering = RETURN_IN_ORDER; | 420 | ordering = RETURN_IN_ORDER; |
412 | ++optstring; | 421 | ++optstring; |
413 | } | 422 | } |
414 | else if (optstring[0] == '+') | 423 | else if (optstring[0] == '+') |
415 | { | 424 | { |
416 | ordering = REQUIRE_ORDER; | 425 | ordering = REQUIRE_ORDER; |
417 | ++optstring; | 426 | ++optstring; |
418 | } | 427 | } |
419 | else if (posixly_correct != NULL) | 428 | else if (posixly_correct != NULL) |
420 | ordering = REQUIRE_ORDER; | 429 | ordering = REQUIRE_ORDER; |
421 | else | 430 | else |
422 | ordering = PERMUTE; | 431 | ordering = PERMUTE; |
423 | 432 | ||
424 | #ifdef _LIBC | 433 | #ifdef _LIBC |
425 | if (posixly_correct == NULL | 434 | if ((posixly_correct == NULL) |
426 | && argc == original_argc && argv == original_argv) | 435 | && (argc == original_argc) && (argv == original_argv)) |
436 | { | ||
437 | if (nonoption_flags_max_len == 0) | ||
427 | { | 438 | { |
428 | if (nonoption_flags_max_len == 0) | 439 | if ((__getopt_nonoption_flags == NULL) |
429 | { | 440 | || (__getopt_nonoption_flags[0] == '\0') ) |
430 | if (__getopt_nonoption_flags == NULL | 441 | nonoption_flags_max_len = -1; |
431 | || __getopt_nonoption_flags[0] == '\0') | 442 | else |
432 | nonoption_flags_max_len = -1; | 443 | { |
433 | else | 444 | const char *orig_str = __getopt_nonoption_flags; |
434 | { | 445 | int len = nonoption_flags_max_len = strlen (orig_str); |
435 | const char *orig_str = __getopt_nonoption_flags; | 446 | if (nonoption_flags_max_len < argc) |
436 | int len = nonoption_flags_max_len = strlen (orig_str); | 447 | nonoption_flags_max_len = argc; |
437 | if (nonoption_flags_max_len < argc) | 448 | __getopt_nonoption_flags = |
438 | nonoption_flags_max_len = argc; | 449 | (char *) malloc (nonoption_flags_max_len); |
439 | __getopt_nonoption_flags = | 450 | if (__getopt_nonoption_flags == NULL) |
440 | (char *) malloc (nonoption_flags_max_len); | 451 | nonoption_flags_max_len = -1; |
441 | if (__getopt_nonoption_flags == NULL) | 452 | else |
442 | nonoption_flags_max_len = -1; | 453 | { |
443 | else | 454 | memcpy (__getopt_nonoption_flags, orig_str, len); |
444 | { | 455 | memset (&__getopt_nonoption_flags[len], '\0', |
445 | memcpy (__getopt_nonoption_flags, orig_str, len); | 456 | nonoption_flags_max_len - len); |
446 | memset (&__getopt_nonoption_flags[len], '\0', | 457 | } |
447 | nonoption_flags_max_len - len); | 458 | } |
448 | } | ||
449 | } | ||
450 | } | ||
451 | nonoption_flags_len = nonoption_flags_max_len; | ||
452 | } | 459 | } |
460 | nonoption_flags_len = nonoption_flags_max_len; | ||
461 | } | ||
453 | else | 462 | else |
454 | nonoption_flags_len = 0; | 463 | nonoption_flags_len = 0; |
455 | #endif | 464 | #endif |
456 | 465 | ||
457 | return optstring; | 466 | return optstring; |
458 | } | 467 | } |
459 | 468 | ||
460 | /* Scan elements of ARGV (whose length is ARGC) for option characters | 469 | /* Scan elements of ARGV (whose length is ARGC) for option characters |
461 | given in OPTSTRING. | 470 | given in OPTSTRING. |
462 | 471 | ||
@@ -515,112 +524,113 @@ _getopt_initialize (argc, argv, optstring) | |||
515 | 524 | ||
516 | int | 525 | int |
517 | _getopt_internal (argc, argv, optstring, longopts, longind, long_only) | 526 | _getopt_internal (argc, argv, optstring, longopts, longind, long_only) |
518 | int argc; | 527 | int argc; |
519 | char *const *argv; | 528 | |
520 | const char *optstring; | 529 | char *const *argv; |
521 | const struct option *longopts; | 530 | const char *optstring; |
522 | int *longind; | 531 | const struct option *longopts; |
523 | int long_only; | 532 | int *longind; |
533 | int long_only; | ||
524 | { | 534 | { |
525 | optarg = NULL; | 535 | optarg = NULL; |
526 | 536 | ||
527 | if (optind == 0 || !__getopt_initialized) | 537 | if ((optind == 0) || ! __getopt_initialized) |
528 | { | 538 | { |
529 | if (optind == 0) | 539 | if (optind == 0) |
530 | optind = 1; /* Don't scan ARGV[0], the program name. */ | 540 | optind = 1; /* Don't scan ARGV[0], the program name. */ |
531 | optstring = _getopt_initialize (argc, argv, optstring); | 541 | optstring = _getopt_initialize (argc, argv, optstring); |
532 | __getopt_initialized = 1; | 542 | __getopt_initialized = 1; |
533 | } | 543 | } |
534 | 544 | ||
535 | /* Test whether ARGV[optind] points to a non-option argument. | 545 | /* Test whether ARGV[optind] points to a non-option argument. |
536 | Either it does not have option syntax, or there is an environment flag | 546 | Either it does not have option syntax, or there is an environment flag |
537 | from the shell indicating it is not an option. The later information | 547 | from the shell indicating it is not an option. The later information |
538 | is only used when the used in the GNU libc. */ | 548 | is only used when the used in the GNU libc. */ |
539 | #ifdef _LIBC | 549 | #ifdef _LIBC |
540 | #define NONOPTION_P (argv[optind][0] != '-' || argv[optind][1] == '\0' \ | 550 | #define NONOPTION_P (argv[optind][0] != '-' || argv[optind][1] == '\0' \ |
541 | || (optind < nonoption_flags_len \ | 551 | || (optind < nonoption_flags_len \ |
542 | && __getopt_nonoption_flags[optind] == '1')) | 552 | && __getopt_nonoption_flags[optind] == '1')) |
543 | #else | 553 | #else |
544 | #define NONOPTION_P (argv[optind][0] != '-' || argv[optind][1] == '\0') | 554 | #define NONOPTION_P (argv[optind][0] != '-' || argv[optind][1] == '\0') |
545 | #endif | 555 | #endif |
546 | 556 | ||
547 | if (nextchar == NULL || *nextchar == '\0') | 557 | if ((nextchar == NULL) || (*nextchar == '\0')) |
558 | { | ||
559 | /* Advance to the next ARGV-element. */ | ||
560 | |||
561 | /* Give FIRST_NONOPT & LAST_NONOPT rational values if OPTIND has been | ||
562 | moved back by the user (who may also have changed the arguments). */ | ||
563 | if (last_nonopt > optind) | ||
564 | last_nonopt = optind; | ||
565 | if (first_nonopt > optind) | ||
566 | first_nonopt = optind; | ||
567 | |||
568 | if (ordering == PERMUTE) | ||
548 | { | 569 | { |
549 | /* Advance to the next ARGV-element. */ | 570 | /* If we have just processed some options following some non-options, |
550 | 571 | exchange them so that the options come first. */ | |
551 | /* Give FIRST_NONOPT & LAST_NONOPT rational values if OPTIND has been | 572 | |
552 | moved back by the user (who may also have changed the arguments). */ | 573 | if ((first_nonopt != last_nonopt) && (last_nonopt != optind) ) |
553 | if (last_nonopt > optind) | 574 | exchange ((char **) argv); |
554 | last_nonopt = optind; | 575 | else if (last_nonopt != optind) |
555 | if (first_nonopt > optind) | 576 | first_nonopt = optind; |
556 | first_nonopt = optind; | 577 | |
557 | 578 | /* Skip any additional non-options | |
558 | if (ordering == PERMUTE) | 579 | and extend the range of non-options previously skipped. */ |
559 | { | 580 | |
560 | /* If we have just processed some options following some non-options, | 581 | while (optind < argc && NONOPTION_P) |
561 | exchange them so that the options come first. */ | 582 | optind++; |
562 | 583 | last_nonopt = optind; | |
563 | if (first_nonopt != last_nonopt && last_nonopt != optind) | ||
564 | exchange ((char **) argv); | ||
565 | else if (last_nonopt != optind) | ||
566 | first_nonopt = optind; | ||
567 | |||
568 | /* Skip any additional non-options | ||
569 | and extend the range of non-options previously skipped. */ | ||
570 | |||
571 | while (optind < argc && NONOPTION_P) | ||
572 | optind++; | ||
573 | last_nonopt = optind; | ||
574 | } | ||
575 | |||
576 | /* The special ARGV-element `--' means premature end of options. | ||
577 | Skip it like a null option, | ||
578 | then exchange with previous non-options as if it were an option, | ||
579 | then skip everything else like a non-option. */ | ||
580 | |||
581 | if (optind != argc && !strcmp (argv[optind], "--")) | ||
582 | { | ||
583 | optind++; | ||
584 | |||
585 | if (first_nonopt != last_nonopt && last_nonopt != optind) | ||
586 | exchange ((char **) argv); | ||
587 | else if (first_nonopt == last_nonopt) | ||
588 | first_nonopt = optind; | ||
589 | last_nonopt = argc; | ||
590 | |||
591 | optind = argc; | ||
592 | } | ||
593 | |||
594 | /* If we have done all the ARGV-elements, stop the scan | ||
595 | and back over any non-options that we skipped and permuted. */ | ||
596 | |||
597 | if (optind == argc) | ||
598 | { | ||
599 | /* Set the next-arg-index to point at the non-options | ||
600 | that we previously skipped, so the caller will digest them. */ | ||
601 | if (first_nonopt != last_nonopt) | ||
602 | optind = first_nonopt; | ||
603 | return -1; | ||
604 | } | ||
605 | |||
606 | /* If we have come to a non-option and did not permute it, | ||
607 | either stop the scan or describe it to the caller and pass it by. */ | ||
608 | |||
609 | if (NONOPTION_P) | ||
610 | { | ||
611 | if (ordering == REQUIRE_ORDER) | ||
612 | return -1; | ||
613 | optarg = argv[optind++]; | ||
614 | return 1; | ||
615 | } | ||
616 | |||
617 | /* We have found another option-ARGV-element. | ||
618 | Skip the initial punctuation. */ | ||
619 | |||
620 | nextchar = (argv[optind] + 1 | ||
621 | + (longopts != NULL && argv[optind][1] == '-')); | ||
622 | } | 584 | } |
623 | 585 | ||
586 | /* The special ARGV-element `--' means premature end of options. | ||
587 | Skip it like a null option, | ||
588 | then exchange with previous non-options as if it were an option, | ||
589 | then skip everything else like a non-option. */ | ||
590 | |||
591 | if ((optind != argc) && ! strcmp (argv[optind], "--")) | ||
592 | { | ||
593 | optind++; | ||
594 | |||
595 | if ((first_nonopt != last_nonopt) && (last_nonopt != optind) ) | ||
596 | exchange ((char **) argv); | ||
597 | else if (first_nonopt == last_nonopt) | ||
598 | first_nonopt = optind; | ||
599 | last_nonopt = argc; | ||
600 | |||
601 | optind = argc; | ||
602 | } | ||
603 | |||
604 | /* If we have done all the ARGV-elements, stop the scan | ||
605 | and back over any non-options that we skipped and permuted. */ | ||
606 | |||
607 | if (optind == argc) | ||
608 | { | ||
609 | /* Set the next-arg-index to point at the non-options | ||
610 | that we previously skipped, so the caller will digest them. */ | ||
611 | if (first_nonopt != last_nonopt) | ||
612 | optind = first_nonopt; | ||
613 | return -1; | ||
614 | } | ||
615 | |||
616 | /* If we have come to a non-option and did not permute it, | ||
617 | either stop the scan or describe it to the caller and pass it by. */ | ||
618 | |||
619 | if (NONOPTION_P) | ||
620 | { | ||
621 | if (ordering == REQUIRE_ORDER) | ||
622 | return -1; | ||
623 | optarg = argv[optind++]; | ||
624 | return 1; | ||
625 | } | ||
626 | |||
627 | /* We have found another option-ARGV-element. | ||
628 | Skip the initial punctuation. */ | ||
629 | |||
630 | nextchar = (argv[optind] + 1 | ||
631 | + (longopts != NULL && argv[optind][1] == '-')); | ||
632 | } | ||
633 | |||
624 | /* Decode the current option-ARGV-element. */ | 634 | /* Decode the current option-ARGV-element. */ |
625 | 635 | ||
626 | /* Check whether the ARGV-element is a long option. | 636 | /* Check whether the ARGV-element is a long option. |
@@ -636,141 +646,141 @@ _getopt_internal (argc, argv, optstring, longopts, longind, long_only) | |||
636 | 646 | ||
637 | This distinction seems to be the most useful approach. */ | 647 | This distinction seems to be the most useful approach. */ |
638 | 648 | ||
639 | if (longopts != NULL | 649 | if ((longopts != NULL) |
640 | && (argv[optind][1] == '-' | 650 | && ((argv[optind][1] == '-') |
641 | || (long_only | 651 | || (long_only |
642 | && (argv[optind][2] | 652 | && (argv[optind][2] |
643 | || !my_index (optstring, argv[optind][1]))))) | 653 | || ! my_index (optstring, argv[optind][1]))))) |
644 | { | 654 | { |
645 | char *nameend; | 655 | char *nameend; |
646 | const struct option *p; | 656 | const struct option *p; |
647 | const struct option *pfound = NULL; | 657 | const struct option *pfound = NULL; |
648 | int exact = 0; | 658 | int exact = 0; |
649 | int ambig = 0; | 659 | int ambig = 0; |
650 | int indfound = -1; | 660 | int indfound = -1; |
651 | int option_index; | 661 | int option_index; |
662 | |||
663 | for (nameend = nextchar; *nameend && *nameend != '='; nameend++) | ||
664 | /* Do nothing. */; | ||
665 | |||
666 | /* Test all long options for either exact match | ||
667 | or abbreviated matches. */ | ||
668 | for (p = longopts, option_index = 0; p->name; p++, option_index++) | ||
669 | if (! strncmp (p->name, nextchar, nameend - nextchar)) | ||
670 | { | ||
671 | if ((unsigned int) (nameend - nextchar) | ||
672 | == (unsigned int) strlen (p->name)) | ||
673 | { | ||
674 | /* Exact match found. */ | ||
675 | pfound = p; | ||
676 | indfound = option_index; | ||
677 | exact = 1; | ||
678 | break; | ||
679 | } | ||
680 | else if (pfound == NULL) | ||
681 | { | ||
682 | /* First nonexact match found. */ | ||
683 | pfound = p; | ||
684 | indfound = option_index; | ||
685 | } | ||
686 | else | ||
687 | /* Second or later nonexact match found. */ | ||
688 | ambig = 1; | ||
689 | } | ||
652 | 690 | ||
653 | for (nameend = nextchar; *nameend && *nameend != '='; nameend++) | 691 | if (ambig && ! exact) |
654 | /* Do nothing. */ ; | 692 | { |
693 | if (opterr) | ||
694 | fprintf (stderr, _ ("%s: option `%s' is ambiguous\n"), | ||
695 | argv[0], argv[optind]); | ||
696 | nextchar += strlen (nextchar); | ||
697 | optind++; | ||
698 | optopt = 0; | ||
699 | return '?'; | ||
700 | } | ||
655 | 701 | ||
656 | /* Test all long options for either exact match | 702 | if (pfound != NULL) |
657 | or abbreviated matches. */ | 703 | { |
658 | for (p = longopts, option_index = 0; p->name; p++, option_index++) | 704 | option_index = indfound; |
659 | if (!strncmp (p->name, nextchar, nameend - nextchar)) | 705 | optind++; |
660 | { | 706 | if (*nameend) |
661 | if ((unsigned int) (nameend - nextchar) | 707 | { |
662 | == (unsigned int) strlen (p->name)) | 708 | /* Don't test has_arg with >, because some C compilers don't |
663 | { | 709 | allow it to be used on enums. */ |
664 | /* Exact match found. */ | 710 | if (pfound->has_arg) |
665 | pfound = p; | 711 | optarg = nameend + 1; |
666 | indfound = option_index; | 712 | else |
667 | exact = 1; | 713 | { |
668 | break; | 714 | if (opterr) |
669 | } | 715 | if (argv[optind - 1][1] == '-') |
670 | else if (pfound == NULL) | 716 | /* --option */ |
671 | { | 717 | fprintf (stderr, |
672 | /* First nonexact match found. */ | 718 | _ |
673 | pfound = p; | 719 | ("%s: option `--%s' doesn't allow an argument\n"), |
674 | indfound = option_index; | 720 | argv[0], pfound->name); |
675 | } | 721 | else |
676 | else | 722 | /* +option or -option */ |
677 | /* Second or later nonexact match found. */ | 723 | fprintf (stderr, |
678 | ambig = 1; | 724 | _ |
679 | } | 725 | ("%s: option `%c%s' doesn't allow an argument\n"), |
680 | 726 | argv[0], argv[optind - 1][0], pfound->name); | |
681 | if (ambig && !exact) | 727 | |
682 | { | 728 | nextchar += strlen (nextchar); |
683 | if (opterr) | 729 | |
684 | fprintf (stderr, _("%s: option `%s' is ambiguous\n"), | 730 | optopt = pfound->val; |
685 | argv[0], argv[optind]); | 731 | return '?'; |
686 | nextchar += strlen (nextchar); | 732 | } |
687 | optind++; | 733 | } |
688 | optopt = 0; | 734 | else if (pfound->has_arg == 1) |
689 | return '?'; | 735 | { |
690 | } | 736 | if (optind < argc) |
737 | optarg = argv[optind++]; | ||
738 | else | ||
739 | { | ||
740 | if (opterr) | ||
741 | fprintf (stderr, | ||
742 | _ ("%s: option `%s' requires an argument\n"), | ||
743 | argv[0], argv[optind - 1]); | ||
744 | nextchar += strlen (nextchar); | ||
745 | optopt = pfound->val; | ||
746 | return optstring[0] == ':' ? ':' : '?'; | ||
747 | } | ||
748 | } | ||
749 | nextchar += strlen (nextchar); | ||
750 | if (longind != NULL) | ||
751 | *longind = option_index; | ||
752 | if (pfound->flag) | ||
753 | { | ||
754 | *(pfound->flag) = pfound->val; | ||
755 | return 0; | ||
756 | } | ||
757 | return pfound->val; | ||
758 | } | ||
691 | 759 | ||
692 | if (pfound != NULL) | 760 | /* Can't find it as a long option. If this is not getopt_long_only, |
693 | { | 761 | or the option starts with '--' or is not a valid short |
694 | option_index = indfound; | 762 | option, then it's an error. |
695 | optind++; | 763 | Otherwise interpret it as a short option. */ |
696 | if (*nameend) | 764 | if (! long_only || (argv[optind][1] == '-') |
697 | { | 765 | || (my_index (optstring, *nextchar) == NULL) ) |
698 | /* Don't test has_arg with >, because some C compilers don't | 766 | { |
699 | allow it to be used on enums. */ | 767 | if (opterr) |
700 | if (pfound->has_arg) | 768 | { |
701 | optarg = nameend + 1; | 769 | if (argv[optind][1] == '-') |
702 | else | 770 | /* --option */ |
703 | { | 771 | fprintf (stderr, _ ("%s: unrecognized option `--%s'\n"), |
704 | if (opterr) | 772 | argv[0], nextchar); |
705 | if (argv[optind - 1][1] == '-') | 773 | else |
706 | /* --option */ | 774 | /* +option or -option */ |
707 | fprintf (stderr, | 775 | fprintf (stderr, _ ("%s: unrecognized option `%c%s'\n"), |
708 | _ | 776 | argv[0], argv[optind][0], nextchar); |
709 | ("%s: option `--%s' doesn't allow an argument\n"), | 777 | } |
710 | argv[0], pfound->name); | 778 | nextchar = (char *) ""; |
711 | else | 779 | optind++; |
712 | /* +option or -option */ | 780 | optopt = 0; |
713 | fprintf (stderr, | 781 | return '?'; |
714 | _ | ||
715 | ("%s: option `%c%s' doesn't allow an argument\n"), | ||
716 | argv[0], argv[optind - 1][0], pfound->name); | ||
717 | |||
718 | nextchar += strlen (nextchar); | ||
719 | |||
720 | optopt = pfound->val; | ||
721 | return '?'; | ||
722 | } | ||
723 | } | ||
724 | else if (pfound->has_arg == 1) | ||
725 | { | ||
726 | if (optind < argc) | ||
727 | optarg = argv[optind++]; | ||
728 | else | ||
729 | { | ||
730 | if (opterr) | ||
731 | fprintf (stderr, | ||
732 | _("%s: option `%s' requires an argument\n"), | ||
733 | argv[0], argv[optind - 1]); | ||
734 | nextchar += strlen (nextchar); | ||
735 | optopt = pfound->val; | ||
736 | return optstring[0] == ':' ? ':' : '?'; | ||
737 | } | ||
738 | } | ||
739 | nextchar += strlen (nextchar); | ||
740 | if (longind != NULL) | ||
741 | *longind = option_index; | ||
742 | if (pfound->flag) | ||
743 | { | ||
744 | *(pfound->flag) = pfound->val; | ||
745 | return 0; | ||
746 | } | ||
747 | return pfound->val; | ||
748 | } | ||
749 | |||
750 | /* Can't find it as a long option. If this is not getopt_long_only, | ||
751 | or the option starts with '--' or is not a valid short | ||
752 | option, then it's an error. | ||
753 | Otherwise interpret it as a short option. */ | ||
754 | if (!long_only || argv[optind][1] == '-' | ||
755 | || my_index (optstring, *nextchar) == NULL) | ||
756 | { | ||
757 | if (opterr) | ||
758 | { | ||
759 | if (argv[optind][1] == '-') | ||
760 | /* --option */ | ||
761 | fprintf (stderr, _("%s: unrecognized option `--%s'\n"), | ||
762 | argv[0], nextchar); | ||
763 | else | ||
764 | /* +option or -option */ | ||
765 | fprintf (stderr, _("%s: unrecognized option `%c%s'\n"), | ||
766 | argv[0], argv[optind][0], nextchar); | ||
767 | } | ||
768 | nextchar = (char *) ""; | ||
769 | optind++; | ||
770 | optopt = 0; | ||
771 | return '?'; | ||
772 | } | ||
773 | } | 782 | } |
783 | } | ||
774 | 784 | ||
775 | /* Look at and handle the next short option-character. */ | 785 | /* Look at and handle the next short option-character. */ |
776 | 786 | ||
@@ -782,205 +792,207 @@ _getopt_internal (argc, argv, optstring, longopts, longind, long_only) | |||
782 | if (*nextchar == '\0') | 792 | if (*nextchar == '\0') |
783 | ++optind; | 793 | ++optind; |
784 | 794 | ||
785 | if (temp == NULL || c == ':') | 795 | if ((temp == NULL) || (c == ':')) |
796 | { | ||
797 | if (opterr) | ||
786 | { | 798 | { |
787 | if (opterr) | 799 | if (posixly_correct) |
788 | { | 800 | /* 1003.2 specifies the format of this message. */ |
789 | if (posixly_correct) | 801 | fprintf (stderr, _ ("%s: illegal option -- %c\n"), argv[0], c); |
790 | /* 1003.2 specifies the format of this message. */ | 802 | else |
791 | fprintf (stderr, _("%s: illegal option -- %c\n"), argv[0], c); | 803 | fprintf (stderr, _ ("%s: invalid option -- %c\n"), argv[0], c); |
792 | else | ||
793 | fprintf (stderr, _("%s: invalid option -- %c\n"), argv[0], c); | ||
794 | } | ||
795 | optopt = c; | ||
796 | return '?'; | ||
797 | } | 804 | } |
805 | optopt = c; | ||
806 | return '?'; | ||
807 | } | ||
798 | /* Convenience. Treat POSIX -W foo same as long option --foo */ | 808 | /* Convenience. Treat POSIX -W foo same as long option --foo */ |
799 | if (temp[0] == 'W' && temp[1] == ';') | 809 | if ((temp[0] == 'W') && (temp[1] == ';')) |
810 | { | ||
811 | char *nameend; | ||
812 | const struct option *p; | ||
813 | const struct option *pfound = NULL; | ||
814 | int exact = 0; | ||
815 | int ambig = 0; | ||
816 | int indfound = 0; | ||
817 | int option_index; | ||
818 | |||
819 | /* This is an option that requires an argument. */ | ||
820 | if (*nextchar != '\0') | ||
821 | { | ||
822 | optarg = nextchar; | ||
823 | /* If we end this ARGV-element by taking the rest as an arg, | ||
824 | we must advance to the next element now. */ | ||
825 | optind++; | ||
826 | } | ||
827 | else if (optind == argc) | ||
828 | { | ||
829 | if (opterr) | ||
830 | { | ||
831 | /* 1003.2 specifies the format of this message. */ | ||
832 | fprintf (stderr, _ ("%s: option requires an argument -- %c\n"), | ||
833 | argv[0], c); | ||
834 | } | ||
835 | optopt = c; | ||
836 | if (optstring[0] == ':') | ||
837 | c = ':'; | ||
838 | else | ||
839 | c = '?'; | ||
840 | return c; | ||
841 | } | ||
842 | else | ||
843 | /* We already incremented `optind' once; | ||
844 | increment it again when taking next ARGV-elt as argument. */ | ||
845 | optarg = argv[optind++]; | ||
846 | |||
847 | /* optarg is now the argument, see if it's in the | ||
848 | table of longopts. */ | ||
849 | |||
850 | for (nextchar = nameend = optarg; *nameend && *nameend != '='; | ||
851 | nameend++) | ||
852 | /* Do nothing. */; | ||
853 | |||
854 | /* Test all long options for either exact match | ||
855 | or abbreviated matches. */ | ||
856 | for (p = longopts, option_index = 0; p->name; p++, option_index++) | ||
857 | if (! strncmp (p->name, nextchar, nameend - nextchar)) | ||
858 | { | ||
859 | if ((unsigned int) (nameend - nextchar) == strlen (p->name)) | ||
860 | { | ||
861 | /* Exact match found. */ | ||
862 | pfound = p; | ||
863 | indfound = option_index; | ||
864 | exact = 1; | ||
865 | break; | ||
866 | } | ||
867 | else if (pfound == NULL) | ||
868 | { | ||
869 | /* First nonexact match found. */ | ||
870 | pfound = p; | ||
871 | indfound = option_index; | ||
872 | } | ||
873 | else | ||
874 | /* Second or later nonexact match found. */ | ||
875 | ambig = 1; | ||
876 | } | ||
877 | if (ambig && ! exact) | ||
878 | { | ||
879 | if (opterr) | ||
880 | fprintf (stderr, _ ("%s: option `-W %s' is ambiguous\n"), | ||
881 | argv[0], argv[optind]); | ||
882 | nextchar += strlen (nextchar); | ||
883 | optind++; | ||
884 | return '?'; | ||
885 | } | ||
886 | if (pfound != NULL) | ||
800 | { | 887 | { |
801 | char *nameend; | 888 | option_index = indfound; |
802 | const struct option *p; | 889 | if (*nameend) |
803 | const struct option *pfound = NULL; | 890 | { |
804 | int exact = 0; | 891 | /* Don't test has_arg with >, because some C compilers don't |
805 | int ambig = 0; | 892 | allow it to be used on enums. */ |
806 | int indfound = 0; | 893 | if (pfound->has_arg) |
807 | int option_index; | 894 | optarg = nameend + 1; |
808 | 895 | else | |
809 | /* This is an option that requires an argument. */ | 896 | { |
810 | if (*nextchar != '\0') | 897 | if (opterr) |
811 | { | 898 | fprintf (stderr, _ ( |
812 | optarg = nextchar; | 899 | "\ |
813 | /* If we end this ARGV-element by taking the rest as an arg, | ||
814 | we must advance to the next element now. */ | ||
815 | optind++; | ||
816 | } | ||
817 | else if (optind == argc) | ||
818 | { | ||
819 | if (opterr) | ||
820 | { | ||
821 | /* 1003.2 specifies the format of this message. */ | ||
822 | fprintf (stderr, _("%s: option requires an argument -- %c\n"), | ||
823 | argv[0], c); | ||
824 | } | ||
825 | optopt = c; | ||
826 | if (optstring[0] == ':') | ||
827 | c = ':'; | ||
828 | else | ||
829 | c = '?'; | ||
830 | return c; | ||
831 | } | ||
832 | else | ||
833 | /* We already incremented `optind' once; | ||
834 | increment it again when taking next ARGV-elt as argument. */ | ||
835 | optarg = argv[optind++]; | ||
836 | |||
837 | /* optarg is now the argument, see if it's in the | ||
838 | table of longopts. */ | ||
839 | |||
840 | for (nextchar = nameend = optarg; *nameend && *nameend != '='; | ||
841 | nameend++) | ||
842 | /* Do nothing. */ ; | ||
843 | |||
844 | /* Test all long options for either exact match | ||
845 | or abbreviated matches. */ | ||
846 | for (p = longopts, option_index = 0; p->name; p++, option_index++) | ||
847 | if (!strncmp (p->name, nextchar, nameend - nextchar)) | ||
848 | { | ||
849 | if ((unsigned int) (nameend - nextchar) == strlen (p->name)) | ||
850 | { | ||
851 | /* Exact match found. */ | ||
852 | pfound = p; | ||
853 | indfound = option_index; | ||
854 | exact = 1; | ||
855 | break; | ||
856 | } | ||
857 | else if (pfound == NULL) | ||
858 | { | ||
859 | /* First nonexact match found. */ | ||
860 | pfound = p; | ||
861 | indfound = option_index; | ||
862 | } | ||
863 | else | ||
864 | /* Second or later nonexact match found. */ | ||
865 | ambig = 1; | ||
866 | } | ||
867 | if (ambig && !exact) | ||
868 | { | ||
869 | if (opterr) | ||
870 | fprintf (stderr, _("%s: option `-W %s' is ambiguous\n"), | ||
871 | argv[0], argv[optind]); | ||
872 | nextchar += strlen (nextchar); | ||
873 | optind++; | ||
874 | return '?'; | ||
875 | } | ||
876 | if (pfound != NULL) | ||
877 | { | ||
878 | option_index = indfound; | ||
879 | if (*nameend) | ||
880 | { | ||
881 | /* Don't test has_arg with >, because some C compilers don't | ||
882 | allow it to be used on enums. */ | ||
883 | if (pfound->has_arg) | ||
884 | optarg = nameend + 1; | ||
885 | else | ||
886 | { | ||
887 | if (opterr) | ||
888 | fprintf (stderr, _("\ | ||
889 | %s: option `-W %s' doesn't allow an argument\n"), argv[0], pfound->name); | 900 | %s: option `-W %s' doesn't allow an argument\n"), argv[0], pfound->name); |
890 | 901 | ||
891 | nextchar += strlen (nextchar); | 902 | nextchar += strlen (nextchar); |
892 | return '?'; | 903 | return '?'; |
893 | } | 904 | } |
894 | } | 905 | } |
895 | else if (pfound->has_arg == 1) | 906 | else if (pfound->has_arg == 1) |
896 | { | 907 | { |
897 | if (optind < argc) | 908 | if (optind < argc) |
898 | optarg = argv[optind++]; | 909 | optarg = argv[optind++]; |
899 | else | 910 | else |
900 | { | 911 | { |
901 | if (opterr) | 912 | if (opterr) |
902 | fprintf (stderr, | 913 | fprintf (stderr, |
903 | _("%s: option `%s' requires an argument\n"), | 914 | _ ("%s: option `%s' requires an argument\n"), |
904 | argv[0], argv[optind - 1]); | 915 | argv[0], argv[optind - 1]); |
905 | nextchar += strlen (nextchar); | 916 | nextchar += strlen (nextchar); |
906 | return optstring[0] == ':' ? ':' : '?'; | 917 | return optstring[0] == ':' ? ':' : '?'; |
907 | } | 918 | } |
908 | } | 919 | } |
909 | nextchar += strlen (nextchar); | 920 | nextchar += strlen (nextchar); |
910 | if (longind != NULL) | 921 | if (longind != NULL) |
911 | *longind = option_index; | 922 | *longind = option_index; |
912 | if (pfound->flag) | 923 | if (pfound->flag) |
913 | { | 924 | { |
914 | *(pfound->flag) = pfound->val; | 925 | *(pfound->flag) = pfound->val; |
915 | return 0; | 926 | return 0; |
916 | } | 927 | } |
917 | return pfound->val; | 928 | return pfound->val; |
918 | } | ||
919 | nextchar = NULL; | ||
920 | return 'W'; /* Let the application handle it. */ | ||
921 | } | 929 | } |
930 | nextchar = NULL; | ||
931 | return 'W'; /* Let the application handle it. */ | ||
932 | } | ||
922 | if (temp[1] == ':') | 933 | if (temp[1] == ':') |
934 | { | ||
935 | if (temp[2] == ':') | ||
936 | { | ||
937 | /* This is an option that accepts an argument optionally. */ | ||
938 | if (*nextchar != '\0') | ||
939 | { | ||
940 | optarg = nextchar; | ||
941 | optind++; | ||
942 | } | ||
943 | else | ||
944 | optarg = NULL; | ||
945 | nextchar = NULL; | ||
946 | } | ||
947 | else | ||
923 | { | 948 | { |
924 | if (temp[2] == ':') | 949 | /* This is an option that requires an argument. */ |
925 | { | 950 | if (*nextchar != '\0') |
926 | /* This is an option that accepts an argument optionally. */ | 951 | { |
927 | if (*nextchar != '\0') | 952 | optarg = nextchar; |
928 | { | 953 | /* If we end this ARGV-element by taking the rest as an arg, |
929 | optarg = nextchar; | 954 | we must advance to the next element now. */ |
930 | optind++; | 955 | optind++; |
931 | } | 956 | } |
932 | else | 957 | else if (optind == argc) |
933 | optarg = NULL; | 958 | { |
934 | nextchar = NULL; | 959 | if (opterr) |
935 | } | 960 | { |
936 | else | 961 | /* 1003.2 specifies the format of this message. */ |
937 | { | 962 | fprintf (stderr, |
938 | /* This is an option that requires an argument. */ | 963 | _ ("%s: option requires an argument -- %c\n"), |
939 | if (*nextchar != '\0') | 964 | argv[0], c); |
940 | { | 965 | } |
941 | optarg = nextchar; | 966 | optopt = c; |
942 | /* If we end this ARGV-element by taking the rest as an arg, | 967 | if (optstring[0] == ':') |
943 | we must advance to the next element now. */ | 968 | c = ':'; |
944 | optind++; | 969 | else |
945 | } | 970 | c = '?'; |
946 | else if (optind == argc) | 971 | } |
947 | { | 972 | else |
948 | if (opterr) | 973 | /* We already incremented `optind' once; |
949 | { | 974 | increment it again when taking next ARGV-elt as argument. */ |
950 | /* 1003.2 specifies the format of this message. */ | 975 | optarg = argv[optind++]; |
951 | fprintf (stderr, | 976 | nextchar = NULL; |
952 | _("%s: option requires an argument -- %c\n"), | ||
953 | argv[0], c); | ||
954 | } | ||
955 | optopt = c; | ||
956 | if (optstring[0] == ':') | ||
957 | c = ':'; | ||
958 | else | ||
959 | c = '?'; | ||
960 | } | ||
961 | else | ||
962 | /* We already incremented `optind' once; | ||
963 | increment it again when taking next ARGV-elt as argument. */ | ||
964 | optarg = argv[optind++]; | ||
965 | nextchar = NULL; | ||
966 | } | ||
967 | } | 977 | } |
978 | } | ||
968 | return c; | 979 | return c; |
969 | } | 980 | } |
970 | } | 981 | } |
971 | 982 | ||
972 | int | 983 | int |
973 | getopt (argc, argv, optstring) | 984 | getopt (argc, argv, optstring) |
974 | int argc; | 985 | int argc; |
975 | char *const *argv; | 986 | |
976 | const char *optstring; | 987 | char *const *argv; |
988 | const char *optstring; | ||
977 | { | 989 | { |
978 | return _getopt_internal (argc, argv, optstring, | 990 | return _getopt_internal (argc, argv, optstring, |
979 | (const struct option *) 0, (int *) 0, 0); | 991 | (const struct option *) 0, (int *) 0, 0); |
980 | } | 992 | } |
981 | 993 | ||
982 | #endif /* Not ELIDE_CODE. */ | 994 | #endif /* Not ELIDE_CODE. */ |
983 | 995 | ||
984 | #ifdef TEST | 996 | #ifdef TEST |
985 | 997 | ||
986 | /* Compile with -DTEST to make an executable for use in testing | 998 | /* Compile with -DTEST to make an executable for use in testing |
@@ -988,65 +1000,66 @@ getopt (argc, argv, optstring) | |||
988 | 1000 | ||
989 | int | 1001 | int |
990 | main (argc, argv) | 1002 | main (argc, argv) |
991 | int argc; | 1003 | int argc; |
992 | char **argv; | 1004 | |
1005 | char **argv; | ||
993 | { | 1006 | { |
994 | int c; | 1007 | int c; |
995 | int digit_optind = 0; | 1008 | int digit_optind = 0; |
996 | 1009 | ||
997 | while (1) | 1010 | while (1) |
1011 | { | ||
1012 | int this_option_optind = optind ? optind : 1; | ||
1013 | |||
1014 | c = getopt (argc, argv, "abc:d:0123456789"); | ||
1015 | if (c == -1) | ||
1016 | break; | ||
1017 | |||
1018 | switch (c) | ||
998 | { | 1019 | { |
999 | int this_option_optind = optind ? optind : 1; | 1020 | case '0': |
1000 | 1021 | case '1': | |
1001 | c = getopt (argc, argv, "abc:d:0123456789"); | 1022 | case '2': |
1002 | if (c == -1) | 1023 | case '3': |
1003 | break; | 1024 | case '4': |
1004 | 1025 | case '5': | |
1005 | switch (c) | 1026 | case '6': |
1006 | { | 1027 | case '7': |
1007 | case '0': | 1028 | case '8': |
1008 | case '1': | 1029 | case '9': |
1009 | case '2': | 1030 | if ((digit_optind != 0) && (digit_optind != this_option_optind) ) |
1010 | case '3': | 1031 | printf ("digits occur in two different argv-elements.\n"); |
1011 | case '4': | 1032 | digit_optind = this_option_optind; |
1012 | case '5': | 1033 | printf ("option %c\n", c); |
1013 | case '6': | 1034 | break; |
1014 | case '7': | 1035 | |
1015 | case '8': | 1036 | case 'a': |
1016 | case '9': | 1037 | printf ("option a\n"); |
1017 | if (digit_optind != 0 && digit_optind != this_option_optind) | 1038 | break; |
1018 | printf ("digits occur in two different argv-elements.\n"); | 1039 | |
1019 | digit_optind = this_option_optind; | 1040 | case 'b': |
1020 | printf ("option %c\n", c); | 1041 | printf ("option b\n"); |
1021 | break; | 1042 | break; |
1022 | 1043 | ||
1023 | case 'a': | 1044 | case 'c': |
1024 | printf ("option a\n"); | 1045 | printf ("option c with value `%s'\n", optarg); |
1025 | break; | 1046 | break; |
1026 | 1047 | ||
1027 | case 'b': | 1048 | case '?': |
1028 | printf ("option b\n"); | 1049 | break; |
1029 | break; | 1050 | |
1030 | 1051 | default: | |
1031 | case 'c': | 1052 | printf ("?? getopt returned character code 0%o ??\n", c); |
1032 | printf ("option c with value `%s'\n", optarg); | ||
1033 | break; | ||
1034 | |||
1035 | case '?': | ||
1036 | break; | ||
1037 | |||
1038 | default: | ||
1039 | printf ("?? getopt returned character code 0%o ??\n", c); | ||
1040 | } | ||
1041 | } | 1053 | } |
1054 | } | ||
1042 | 1055 | ||
1043 | if (optind < argc) | 1056 | if (optind < argc) |
1044 | { | 1057 | { |
1045 | printf ("non-option ARGV-elements: "); | 1058 | printf ("non-option ARGV-elements: "); |
1046 | while (optind < argc) | 1059 | while (optind < argc) |
1047 | printf ("%s ", argv[optind++]); | 1060 | printf ("%s ", argv[optind++]); |
1048 | printf ("\n"); | 1061 | printf ("\n"); |
1049 | } | 1062 | } |
1050 | 1063 | ||
1051 | exit (0); | 1064 | exit (0); |
1052 | } | 1065 | } |