Changeset 3213 for trunk/src/kmk/kmkbuiltin/getopt_r.c
- Timestamp:
- Mar 30, 2018, 11:03:28 PM (7 years ago)
- File:
-
- 1 copied
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/kmk/kmkbuiltin/getopt_r.c
r3212 r3213 1 /* Getopt for GNU. 1 /* Reentrant version of getopt. 2 3 Based on ../getopt*.*: 4 5 Getopt for GNU. 2 6 NOTE: getopt is now part of the C library, so if you don't know what 3 7 "Keep this file name-space clean" means, talk to drepper@gnu.org … … 19 23 20 24 You should have received a copy of the GNU General Public License along with 21 this program. If not, see <http://www.gnu.org/licenses/>. */ 25 this program. If not, see <http://www.gnu.org/licenses/>. 26 27 Modifications: 28 Copyright (c) 2018 knut st. osmundsen <bird-kBuild-spamx@anduin.net> 29 */ 22 30 23 31 /* This tells Alpha OSF/1 not to define a getopt prototype in <stdio.h>. … … 27 35 #endif 28 36 37 #define FAKES_NO_GETOPT_H /* bird */ 29 38 #ifdef HAVE_CONFIG_H 30 39 # include <config.h> … … 41 50 #include <stdio.h> 42 51 52 #if 0 43 53 /* Comment out all this code if we are using the GNU C Library, and are not 44 54 actually compiling the library itself. This code is part of the GNU C … … 56 66 # endif 57 67 #endif 58 59 #ifndef ELIDE_CODE 68 #endif 69 70 #if 1 //ndef ELIDE_CODE 60 71 61 72 … … 96 107 they can distinguish the relative order of options and other arguments. */ 97 108 98 #include "getopt.h" 99 109 #include "getopt_r.h" 110 #include "err.h" 111 #include <assert.h> 112 113 #if 0 /* Moved to state_getopt_r in getopt_r.h. */ 100 114 /* For communication from `getopt' to the caller. 101 115 When `getopt' finds an option that takes an argument, … … 146 160 147 161 int optopt = '?'; 162 #endif /* Moved to state_getopt_r in getopt_r.h. */ 148 163 149 164 /* Describe how to deal with options that follow non-option ARGV-elements. … … 176 191 `--' can cause `getopt' to return -1 with `optind' != ARGC. */ 177 192 178 staticenum193 /*static*/ enum 179 194 { 180 195 REQUIRE_ORDER, PERMUTE, RETURN_IN_ORDER 181 } ordering; 182 196 } /*ordering*/; 197 198 #if 0 /* Moved to state_getopt_r in getopt_r.h. */ 183 199 /* Value of POSIXLY_CORRECT environment variable. */ 184 200 static char *posixly_correct; 185 186 187 #ifdef __GNU_LIBRARY__ 201 #endif 202 203 204 205 #if 1 //def __GNU_LIBRARY__ 188 206 /* We want to avoid inclusion of string.h with non-GNU libraries 189 207 because there are many ways it can cause trouble. … … 191 209 in GCC. */ 192 210 # include <string.h> 193 # define my_index strchr211 //# define my_index strchr 194 212 #else 195 213 … … 200 218 # endif 201 219 202 #if ndef KMK220 #if 0 //def 203 221 /* Avoid depending on library functions or files 204 222 whose names are inconsistent. */ … … 206 224 extern char *getenv (); 207 225 #endif 208 #endif /* !KMK */226 #endif 209 227 210 228 static char * … … 237 255 /* Handle permutation of arguments. */ 238 256 257 #if 0 /* Moved to state_getopt_r in getopt_r.h. */ 239 258 /* Describe the part of ARGV that contains non-options that have 240 259 been skipped. `first_nonopt' is the index in ARGV of the first of them; … … 243 262 static int first_nonopt; 244 263 static int last_nonopt; 245 246 #ifdef _LIBC 264 #endif 265 266 #if 0 //def _LIBC 247 267 /* Bash 2.0 gives us an environment variable containing flags 248 268 indicating ARGV elements that should not be considered arguments. */ … … 280 300 } 281 301 #else /* !_LIBC */ 282 # define SWAP_FLAGS(ch1, ch2) 302 # define SWAP_FLAGS(ch1, ch2) do { } while (0) 283 303 #endif /* _LIBC */ 284 304 … … 292 312 the new indices of the non-options in ARGV after they are moved. */ 293 313 294 #if defined __STDC__ && __STDC__295 static void exchange (char **);296 #endif297 298 314 static void 299 exchange ( char **argv)315 exchange (struct getopt_state_r *gos, char **argv) 300 316 { 301 int bottom = first_nonopt;302 int middle = last_nonopt;303 int top = optind;317 int bottom = gos->first_nonopt; 318 int middle = gos->last_nonopt; 319 int top = gos->optind; 304 320 char *tem; 305 321 … … 309 325 but it consists of two parts that need to be swapped next. */ 310 326 311 #if def _LIBC327 #if 0 //def _LIBC 312 328 /* First make sure the handling of the `__getopt_nonoption_flags' 313 329 string can work normally. Our top argument must be in the range … … 371 387 /* Update records for the slots the non-options now occupy. */ 372 388 373 first_nonopt += (optind -last_nonopt);374 last_nonopt =optind;389 gos->first_nonopt += (gos->optind - gos->last_nonopt); 390 gos->last_nonopt = gos->optind; 375 391 } 376 392 377 /* Initialize the internal data when the first call is made.*/378 379 #if defined __STDC__ && __STDC__ 380 static const char *_getopt_initialize (int, char *const *, const char *); 381 #endif 382 static const char * 383 _getopt_initialize (int argc, char *const *argv, const char *optstring)393 /* Initialize the internal data */ 394 395 void 396 getopt_initialize_r (struct getopt_state_r *gos, int argc, 397 char * const *argv, const char *shortopts, 398 const struct option *long_options, 399 char **envp, struct KMKBUILTINCTX *pCtx) 384 400 { 401 assert (shortopts != NULL); 402 403 /* General initialization. */ 404 gos->optarg = NULL; 405 gos->optind = 1; 406 gos->__getopt_initialized = (void *)(uintptr_t)&exchange; 407 gos->opterr = 1; 408 gos->optopt = '?'; 409 gos->argc = argc; 410 gos->argv = argv; 411 gos->optstring = shortopts; 412 gos->len_optstring = strlen (shortopts); 413 gos->long_options = long_options; 414 gos->pCtx = pCtx; 415 385 416 /* Start processing options with ARGV-element 1 (since ARGV-element 0 386 417 is the program name); the sequence of previously skipped 387 418 non-option ARGV-elements is empty. */ 388 419 389 first_nonopt = last_nonopt = optind; 390 391 nextchar = NULL; 392 393 posixly_correct = getenv ("POSIXLY_CORRECT"); 420 gos->first_nonopt = gos->last_nonopt = gos->optind; 421 422 gos->nextchar = NULL; 423 424 if (!envp) 425 gos->posixly_correct = getenv("POSIXLY_CORRECT"); 426 else 427 { 428 const char *psz; 429 size_t i = 0; 430 gos->posixly_correct = NULL; 431 while ((psz = envp[i]) != NULL) 432 { 433 if ( psz[0] == 'P' 434 && strncmp (psz, "POSIXLY_CORRECT=", sizeof("POSIXLY_CORRECT=") - 1) == 0) 435 { 436 gos->posixly_correct = psz + sizeof("POSIXLY_CORRECT=") - 1; 437 break; 438 } 439 i++; 440 } 441 } 394 442 395 443 /* Determine how to handle the ordering of options and nonoptions. */ 396 444 397 if (optstring[0] == '-') 398 { 399 ordering = RETURN_IN_ORDER; 400 ++optstring; 401 } 402 else if (optstring[0] == '+') 403 { 404 ordering = REQUIRE_ORDER; 405 ++optstring; 406 } 407 else if (posixly_correct != NULL) 408 ordering = REQUIRE_ORDER; 445 if (shortopts[0] == '-') 446 { 447 gos->ordering = RETURN_IN_ORDER; 448 gos->optstring++; 449 gos->len_optstring--; 450 } 451 else if (shortopts[0] == '+') 452 { 453 gos->ordering = REQUIRE_ORDER; 454 gos->optstring++; 455 gos->len_optstring--; 456 } 457 else if (gos->posixly_correct != NULL) 458 gos->ordering = REQUIRE_ORDER; 409 459 else 410 ordering = PERMUTE;411 412 #if def _LIBC460 gos->ordering = PERMUTE; 461 462 #if 0 //def _LIBC 413 463 if (posixly_correct == NULL 414 464 && argc == original_argc && argv == original_argv) … … 440 490 #endif 441 491 442 return optstring;492 //return optstring; 443 493 } 444 494 … … 501 551 502 552 int 503 _getopt_internal (int argc, char *const *argv, const char *optstring,504 const struct option *longopts,int *longind, int long_only)553 _getopt_internal_r (struct getopt_state_r *gos, const struct option *longopts, 554 int *longind, int long_only) 505 555 { 506 optarg = NULL; 507 508 if (optind == 0 || !__getopt_initialized) 509 { 510 if (optind == 0) 511 optind = 1; /* Don't scan ARGV[0], the program name. */ 512 optstring = _getopt_initialize (argc, argv, optstring); 513 __getopt_initialized = 1; 514 } 556 assert (gos->__getopt_initialized == (void *)(uintptr_t)&exchange); 557 gos->optarg = NULL; 558 559 #if 0 /* requires explicit call now */ 560 if (gos->optind == 0 || !gos->__getopt_initialized) 561 { 562 if (gos->optind == 0) 563 gos->optind = 1; /* Don't scan ARGV[0], the program name. */ 564 optstring = _getopt_initialize_r (gos, gos->argc, gos->argv, optstring); 565 gos->__getopt_initialized = 1; 566 } 567 #else 568 assert (gos->__getopt_initialized == (void *)(uintptr_t)&exchange); 569 #endif 515 570 516 571 /* Test whether ARGV[optind] points to a non-option argument. … … 518 573 from the shell indicating it is not an option. The later information 519 574 is only used when the used in the GNU libc. */ 520 #if def _LIBC521 # define NONOPTION_P ( argv[optind][0] != '-' || argv[optind][1] == '\0' \522 || ( optind <nonoption_flags_len \523 && __getopt_nonoption_flags[optind] == '1'))575 #if 0 //def _LIBC 576 # define NONOPTION_P (gos->argv[gos->optind][0] != '-' || gos->argv[gos->optind][1] == '\0' \ 577 || (gos->optind < gos->nonoption_flags_len \ 578 && gos->__getopt_nonoption_flags[gos->optind] == '1')) 524 579 #else 525 # define NONOPTION_P ( argv[optind][0] != '-' || argv[optind][1] == '\0')526 #endif 527 528 if ( nextchar == NULL || *nextchar == '\0')580 # define NONOPTION_P (gos->argv[gos->optind][0] != '-' || gos->argv[gos->optind][1] == '\0') 581 #endif 582 583 if (gos->nextchar == NULL || *gos->nextchar == '\0') 529 584 { 530 585 /* Advance to the next ARGV-element. */ … … 532 587 /* Give FIRST_NONOPT & LAST_NONOPT rational values if OPTIND has been 533 588 moved back by the user (who may also have changed the arguments). */ 534 if ( last_nonopt >optind)535 last_nonopt =optind;536 if ( first_nonopt >optind)537 first_nonopt =optind;538 539 if ( ordering == PERMUTE)589 if (gos->last_nonopt > gos->optind) 590 gos->last_nonopt = gos->optind; 591 if (gos->first_nonopt > gos->optind) 592 gos->first_nonopt = gos->optind; 593 594 if (gos->ordering == PERMUTE) 540 595 { 541 596 /* If we have just processed some options following some non-options, 542 597 exchange them so that the options come first. */ 543 598 544 if ( first_nonopt != last_nonopt && last_nonopt !=optind)545 exchange ( (char **)argv);546 else if ( last_nonopt !=optind)547 first_nonopt =optind;599 if (gos->first_nonopt != gos->last_nonopt && gos->last_nonopt != gos->optind) 600 exchange (gos, (char **) gos->argv); 601 else if (gos->last_nonopt != gos->optind) 602 gos->first_nonopt = gos->optind; 548 603 549 604 /* Skip any additional non-options 550 605 and extend the range of non-options previously skipped. */ 551 606 552 while ( optind <argc && NONOPTION_P)553 optind++;554 last_nonopt =optind;607 while (gos->optind < gos->argc && NONOPTION_P) 608 gos->optind++; 609 gos->last_nonopt = gos->optind; 555 610 } 556 611 … … 560 615 then skip everything else like a non-option. */ 561 616 562 if ( optind != argc && !strcmp (argv[optind], "--"))563 { 564 optind++;565 566 if ( first_nonopt != last_nonopt && last_nonopt !=optind)567 exchange ( (char **)argv);568 else if ( first_nonopt ==last_nonopt)569 first_nonopt =optind;570 last_nonopt =argc;571 572 optind =argc;617 if (gos->optind != gos->argc && !strcmp (gos->argv[gos->optind], "--")) 618 { 619 gos->optind++; 620 621 if (gos->first_nonopt != gos->last_nonopt && gos->last_nonopt != gos->optind) 622 exchange (gos, (char **) gos->argv); 623 else if (gos->first_nonopt == gos->last_nonopt) 624 gos->first_nonopt = gos->optind; 625 gos->last_nonopt = gos->argc; 626 627 gos->optind = gos->argc; 573 628 } 574 629 … … 576 631 and back over any non-options that we skipped and permuted. */ 577 632 578 if ( optind ==argc)633 if (gos->optind == gos->argc) 579 634 { 580 635 /* Set the next-arg-index to point at the non-options 581 636 that we previously skipped, so the caller will digest them. */ 582 if ( first_nonopt !=last_nonopt)583 optind =first_nonopt;637 if (gos->first_nonopt != gos->last_nonopt) 638 gos->optind = gos->first_nonopt; 584 639 return -1; 585 640 } … … 590 645 if (NONOPTION_P) 591 646 { 592 if ( ordering == REQUIRE_ORDER)647 if (gos->ordering == REQUIRE_ORDER) 593 648 return -1; 594 optarg = argv[optind++];649 gos->optarg = gos->argv[gos->optind++]; 595 650 return 1; 596 651 } … … 599 654 Skip the initial punctuation. */ 600 655 601 nextchar = (argv[optind] + 1602 + (longopts != NULL && argv[optind][1] == '-'));656 gos->nextchar = (gos->argv[gos->optind] + 1 657 + (longopts != NULL && gos->argv[gos->optind][1] == '-')); 603 658 } 604 659 … … 619 674 620 675 if (longopts != NULL 621 && (argv[optind][1] == '-' 622 || (long_only && (argv[optind][2] || !my_index (optstring, argv[optind][1]))))) 676 && (gos->argv[gos->optind][1] == '-' 677 || (long_only 678 && ( gos->argv[gos->optind][2] 679 || !memchr (gos->optstring, gos->argv[gos->optind][1], gos->len_optstring) ) 680 ) 681 ) 682 ) 623 683 { 624 684 char *nameend; … … 630 690 int option_index; 631 691 632 for (nameend = nextchar; *nameend && *nameend != '='; nameend++)692 for (nameend = gos->nextchar; *nameend && *nameend != '='; nameend++) 633 693 /* Do nothing. */ ; 634 694 … … 636 696 or abbreviated matches. */ 637 697 for (p = longopts, option_index = 0; p->name; p++, option_index++) 638 if (!strncmp (p->name, nextchar, nameend -nextchar))698 if (!strncmp (p->name, gos->nextchar, nameend - gos->nextchar)) 639 699 { 640 if ((unsigned int) (nameend - nextchar)700 if ((unsigned int) (nameend - gos->nextchar) 641 701 == (unsigned int) strlen (p->name)) 642 702 { … … 660 720 if (ambig && !exact) 661 721 { 662 if ( opterr)663 fprintf (stderr, _("%s: option '%s' is ambiguous\n"),664 argv[0], argv[optind]);665 nextchar += strlen (nextchar);666 optind++;667 optopt = 0;722 if (gos->opterr) 723 errx (gos->pCtx, 2, _("%s: option '%s' is ambiguous"), 724 gos->argv[0], gos->argv[gos->optind]); 725 gos->nextchar += strlen (gos->nextchar); 726 gos->optind++; 727 gos->optopt = 0; 668 728 return '?'; 669 729 } … … 672 732 { 673 733 option_index = indfound; 674 optind++;734 gos->optind++; 675 735 if (*nameend) 676 736 { … … 678 738 allow it to be used on enums. */ 679 739 if (pfound->has_arg) 680 optarg = nameend + 1;740 gos->optarg = nameend + 1; 681 741 else 682 742 { 683 if ( opterr)743 if (gos->opterr) 684 744 { /* bird: disambiguate */ 685 if ( argv[optind - 1][1] == '-')745 if (gos->argv[gos->optind - 1][1] == '-') 686 746 /* --option */ 687 fprintf (stderr,688 689 747 errx (gos->pCtx, 2, 748 _("%s: option '--%s' doesn't allow an argument\n"), 749 gos->argv[0], pfound->name); 690 750 else 691 751 /* +option or -option */ 692 fprintf (stderr,693 694 argv[0], argv[optind - 1][0], pfound->name);752 errx (gos->pCtx, 2, 753 _("%s: option '%c%s' doesn't allow an argument\n"), 754 gos->argv[0], gos->argv[gos->optind - 1][0], pfound->name); 695 755 } 696 756 697 nextchar += strlen (nextchar);698 699 optopt = pfound->val;757 gos->nextchar += strlen (gos->nextchar); 758 759 gos->optopt = pfound->val; 700 760 return '?'; 701 761 } … … 703 763 else if (pfound->has_arg == 1) 704 764 { 705 if ( optind <argc)706 optarg = argv[optind++];765 if (gos->optind < gos->argc) 766 gos->optarg = gos->argv[gos->optind++]; 707 767 else 708 768 { 709 if ( opterr)710 fprintf (stderr,711 712 argv[0], argv[optind - 1]);713 nextchar += strlen (nextchar);714 optopt = pfound->val;715 return optstring[0] == ':' ? ':' : '?';769 if (gos->opterr) 770 errx (gos->pCtx, 2, 771 _("%s: option '%s' requires an argument\n"), 772 gos->argv[0], gos->argv[gos->optind - 1]); 773 gos->nextchar += strlen (gos->nextchar); 774 gos->optopt = pfound->val; 775 return gos->optstring[0] == ':' ? ':' : '?'; 716 776 } 717 777 } 718 nextchar += strlen (nextchar);778 gos->nextchar += strlen (gos->nextchar); 719 779 if (longind != NULL) 720 780 *longind = option_index; … … 731 791 option, then it's an error. 732 792 Otherwise interpret it as a short option. */ 733 if (!long_only || argv[optind][1] == '-'734 || m y_index (optstring, *nextchar) == NULL)735 { 736 if ( opterr)793 if (!long_only || gos->argv[gos->optind][1] == '-' 794 || memchr(gos->optstring, *gos->nextchar, gos->len_optstring) == NULL) 795 { 796 if (gos->opterr) 737 797 { 738 if ( argv[optind][1] == '-')798 if (gos->argv[gos->optind][1] == '-') 739 799 /* --option */ 740 fprintf (stderr, _("%s: unrecognized option '--%s'\n"),741 argv[0],nextchar);800 errx (gos->pCtx, 2, _("%s: unrecognized option '--%s'\n"), 801 gos->argv[0], gos->nextchar); 742 802 else 743 803 /* +option or -option */ 744 fprintf (stderr, _("%s: unrecognized option '%c%s'\n"),745 argv[0], argv[optind][0],nextchar);804 errx (gos->pCtx, 2, _("%s: unrecognized option '%c%s'\n"), 805 gos->argv[0], gos->argv[gos->optind][0], gos->nextchar); 746 806 } 747 nextchar = (char *) "";748 optind++;749 optopt = 0;807 gos->nextchar = (char *) ""; 808 gos->optind++; 809 gos->optopt = 0; 750 810 return '?'; 751 811 } … … 755 815 756 816 { 757 char c = * nextchar++;758 char *temp = my_index (optstring, c);817 char c = *gos->nextchar++; 818 char *temp = (char *)memchr (gos->optstring, c, gos->len_optstring); 759 819 760 820 /* Increment `optind' when we start to process its last character. */ 761 if (* nextchar == '\0')762 ++ optind;821 if (*gos->nextchar == '\0') 822 ++gos->optind; 763 823 764 824 if (temp == NULL || c == ':') 765 825 { 766 if ( opterr)826 if (gos->opterr) 767 827 { 768 if ( posixly_correct)828 if (gos->posixly_correct) 769 829 /* 1003.2 specifies the format of this message. */ 770 fprintf (stderr, _("%s: illegal option -- %c\n"),771 830 errx (gos->pCtx, 2, _("%s: illegal option -- %c\n"), 831 gos->argv[0], c); 772 832 else 773 fprintf (stderr, _("%s: invalid option -- %c\n"),774 833 errx (gos->pCtx, 2, _("%s: invalid option -- %c\n"), 834 gos->argv[0], c); 775 835 } 776 optopt = c;836 gos->optopt = c; 777 837 return '?'; 778 838 } … … 789 849 790 850 /* This is an option that requires an argument. */ 791 if (* nextchar != '\0')851 if (*gos->nextchar != '\0') 792 852 { 793 optarg =nextchar;853 gos->optarg = gos->nextchar; 794 854 /* If we end this ARGV-element by taking the rest as an arg, 795 855 we must advance to the next element now. */ 796 optind++;856 gos->optind++; 797 857 } 798 else if ( optind ==argc)858 else if (gos->optind == gos->argc) 799 859 { 800 if ( opterr)860 if (gos->opterr) 801 861 { 802 862 /* 1003.2 specifies the format of this message. */ 803 fprintf (stderr, _("%s: option requires an argument -- %c\n"),804 863 errx (gos->pCtx, 2, _("%s: option requires an argument -- %c\n"), 864 gos->argv[0], c); 805 865 } 806 optopt = c;807 if ( optstring[0] == ':')866 gos->optopt = c; 867 if (gos->optstring[0] == ':') 808 868 c = ':'; 809 869 else … … 814 874 /* We already incremented `optind' once; 815 875 increment it again when taking next ARGV-elt as argument. */ 816 optarg = argv[optind++];876 gos->optarg = gos->argv[gos->optind++]; 817 877 818 878 /* optarg is now the argument, see if it's in the 819 879 table of longopts. */ 820 880 821 for ( nextchar = nameend =optarg; *nameend && *nameend != '='; nameend++)881 for (gos->nextchar = nameend = gos->optarg; *nameend && *nameend != '='; nameend++) 822 882 /* Do nothing. */ ; 823 883 … … 825 885 or abbreviated matches. */ 826 886 for (p = longopts, option_index = 0; p->name; p++, option_index++) 827 if (!strncmp (p->name, nextchar, nameend -nextchar))887 if (!strncmp (p->name, gos->nextchar, nameend - gos->nextchar)) 828 888 { 829 if ((unsigned int) (nameend - nextchar) == strlen (p->name))889 if ((unsigned int) (nameend - gos->nextchar) == strlen (p->name)) 830 890 { 831 891 /* Exact match found. */ … … 847 907 if (ambig && !exact) 848 908 { 849 if ( opterr)850 fprintf (stderr, _("%s: option '-W %s' is ambiguous\n"),851 argv[0], argv[optind]);852 nextchar += strlen (nextchar);853 optind++;909 if (gos->opterr) 910 errx (gos->pCtx, 2, _("%s: option '-W %s' is ambiguous\n"), 911 gos->argv[0], gos->argv[gos->optind]); 912 gos->nextchar += strlen (gos->nextchar); 913 gos->optind++; 854 914 return '?'; 855 915 } … … 862 922 allow it to be used on enums. */ 863 923 if (pfound->has_arg) 864 optarg = nameend + 1;924 gos->optarg = nameend + 1; 865 925 else 866 926 { 867 if ( opterr)868 fprintf (stderr, _("\869 %s: option '-W %s' doesn't allow an argument\n"),870 871 872 nextchar += strlen (nextchar);927 if (gos->opterr) 928 errx (gos->pCtx, 2, 929 _("%s: option '-W %s' doesn't allow an argument\n"), 930 gos->argv[0], pfound->name); 931 932 gos->nextchar += strlen (gos->nextchar); 873 933 return '?'; 874 934 } … … 876 936 else if (pfound->has_arg == 1) 877 937 { 878 if ( optind <argc)879 optarg = argv[optind++];938 if (gos->optind < gos->argc) 939 gos->optarg = gos->argv[gos->optind++]; 880 940 else 881 941 { 882 if ( opterr)883 fprintf (stderr,884 885 argv[0], argv[optind - 1]);886 nextchar += strlen (nextchar);887 return optstring[0] == ':' ? ':' : '?';942 if (gos->opterr) 943 errx (gos->pCtx, 2, 944 _("%s: option '%s' requires an argument\n"), 945 gos->argv[0], gos->argv[gos->optind - 1]); 946 gos->nextchar += strlen (gos->nextchar); 947 return gos->optstring[0] == ':' ? ':' : '?'; 888 948 } 889 949 } 890 nextchar += strlen (nextchar);950 gos->nextchar += strlen (gos->nextchar); 891 951 if (longind != NULL) 892 952 *longind = option_index; … … 898 958 return pfound->val; 899 959 } 900 nextchar = NULL;960 gos->nextchar = NULL; 901 961 return 'W'; /* Let the application handle it. */ 902 962 } … … 906 966 { 907 967 /* This is an option that accepts an argument optionally. */ 908 if (* nextchar != '\0')968 if (*gos->nextchar != '\0') 909 969 { 910 optarg =nextchar;911 optind++;970 gos->optarg = gos->nextchar; 971 gos->optind++; 912 972 } 913 973 else 914 optarg = NULL;915 nextchar = NULL;974 gos->optarg = NULL; 975 gos->nextchar = NULL; 916 976 } 917 977 else 918 978 { 919 979 /* This is an option that requires an argument. */ 920 if (* nextchar != '\0')980 if (*gos->nextchar != '\0') 921 981 { 922 optarg =nextchar;982 gos->optarg = gos->nextchar; 923 983 /* If we end this ARGV-element by taking the rest as an arg, 924 984 we must advance to the next element now. */ 925 optind++;985 gos->optind++; 926 986 } 927 else if ( optind ==argc)987 else if (gos->optind == gos->argc) 928 988 { 929 if ( opterr)989 if (gos->opterr) 930 990 { 931 991 /* 1003.2 specifies the format of this message. */ 932 fprintf (stderr,933 934 992 errx (gos->pCtx, 2, 993 _("%s: option requires an argument -- %c\n"), 994 gos->argv[0], c); 935 995 } 936 optopt = c;937 if ( optstring[0] == ':')996 gos->optopt = c; 997 if (gos->optstring[0] == ':') 938 998 c = ':'; 939 999 else … … 943 1003 /* We already incremented `optind' once; 944 1004 increment it again when taking next ARGV-elt as argument. */ 945 optarg = argv[optind++];946 nextchar = NULL;1005 gos->optarg = gos->argv[gos->optind++]; 1006 gos->nextchar = NULL; 947 1007 } 948 1008 } … … 952 1012 953 1013 int 954 getopt (int argc, char *const *argv, const char *optstring)1014 getopt_r (struct getopt_state_r *gos) 955 1015 { 956 return _getopt_internal (argc, argv, optstring, 957 (const struct option *) 0, 958 (int *) 0, 959 0); 1016 return _getopt_internal_r (gos, NULL, NULL, 0); 960 1017 } 961 1018 962 #endif 1019 #endif /* #if 1 */ /* Not ELIDE_CODE. */ 963 1020 964 1021 … … 973 1030 int c; 974 1031 int digit_optind = 0; 1032 struct getopt_state_r = gos; 1033 1034 getopt_initialize_r (&gos, argc, argv, "abc:d:0123456789", NULL, NULL, NULL); 975 1035 976 1036 while (1) 977 1037 { 978 int this_option_optind = optind ?optind : 1;979 980 c = getopt (argc, argv, "abc:d:0123456789");1038 int this_option_optind = gos.optind ? gos.optind : 1; 1039 1040 c = getopt_r (&gos); 981 1041 if (c == -1) 982 1042 break; … … 1020 1080 } 1021 1081 1022 if ( optind < argc)1082 if (gos.optind < argc) 1023 1083 { 1024 1084 printf ("non-option ARGV-elements: "); 1025 while ( optind < argc)1026 printf ("%s ", argv[ optind++]);1085 while (gos.optind < argc) 1086 printf ("%s ", argv[gos.optind++]); 1027 1087 printf ("\n"); 1028 1088 }
Note:
See TracChangeset
for help on using the changeset viewer.