Changeset 300 for pjproject/trunk/pjsip/src/pjsua-lib/getopt.c
- Timestamp:
- Mar 6, 2006 3:14:59 PM (18 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
pjproject/trunk/pjsip/src/pjsua-lib/getopt.c
r238 r300 19 19 20 20 /* 21 * getopt entry points21 * pj_getopt entry points 22 22 * 23 23 * modified by Mike Borella <mike_borella@mw.3com.com> … … 33 33 int _getopt_internal (int argc, char *const *argv, 34 34 const char *shortopts, 35 const struct option *longopts, int *longind,35 const struct pj_getopt_option *longopts, int *longind, 36 36 int long_only); 37 37 38 /* getopt_long and getopt_long_only entry points for GNUgetopt.38 /* pj_getopt_long and pj_getopt_long_only entry points for GNU pj_getopt. 39 39 Copyright (C) 1987,88,89,90,91,92,93,94,96,97 Free Software Foundation, Inc. 40 40 This file is part of the GNU C Library. … … 68 68 69 69 int 70 getopt_long (int argc, char *const *argv, const char *options,71 const struct option *long_options, int *opt_index)70 pj_getopt_long (int argc, char *const *argv, const char *options, 71 const struct pj_getopt_option *long_options, int *opt_index) 72 72 { 73 73 return _getopt_internal (argc, argv, options, long_options, opt_index, 0); 74 74 } 75 75 76 /* Like getopt_long, but '-' as well as '--' can indicate a long option.76 /* Like pj_getopt_long, but '-' as well as '--' can indicate a long option. 77 77 If an option that starts with '-' (not '--') doesn't match a long option, 78 78 but does match a short option, it is parsed as a short option … … 80 80 81 81 int 82 getopt (int argc, char * const * argv, const char * optstring)82 pj_getopt (int argc, char * const * argv, const char * optstring) 83 83 { 84 84 return _getopt_internal (argc, argv, optstring, 85 (const struct option *) 0,85 (const struct pj_getopt_option *) 0, 86 86 (int *) 0, 87 87 0); … … 91 91 #define _(msgid) (msgid) 92 92 93 /* This version of ` getopt' appears to the caller like standard Unix `getopt'93 /* This version of `pj_getopt' appears to the caller like standard Unix `pj_getopt' 94 94 but it behaves differently for the user, since it allows the user 95 95 to intersperse the options with the other arguments. 96 96 97 As ` getopt' works, it permutes the elements of ARGV so that,97 As `pj_getopt' works, it permutes the elements of ARGV so that, 98 98 when it is done, all the options precede everything else. Thus 99 99 all application programs are extended to handle flexible argument order. … … 105 105 they can distinguish the relative order of options and other arguments. */ 106 106 107 /* For communication from ` getopt' to the caller.108 When ` getopt' finds an option that takes an argument,107 /* For communication from `pj_getopt' to the caller. 108 When `pj_getopt' finds an option that takes an argument, 109 109 the argument value is returned here. 110 110 Also, when `ordering' is RETURN_IN_ORDER, 111 111 each non-option ARGV-element is returned here. */ 112 112 113 char * optarg = NULL;113 char *pj_optarg = NULL; 114 114 115 115 /* Index in ARGV of the next element to be scanned. 116 116 This is used for communication to and from the caller 117 and for communication between successive calls to ` getopt'.118 119 On entry to ` getopt', zero means this is the first call; initialize.120 121 When ` getopt' returns -1, this is the index of the first of the117 and for communication between successive calls to `pj_getopt'. 118 119 On entry to `pj_getopt', zero means this is the first call; initialize. 120 121 When `pj_getopt' returns -1, this is the index of the first of the 122 122 non-option elements that the caller should itself scan. 123 123 124 Otherwise, ` optind' communicates from one call to the next124 Otherwise, `pj_optind' communicates from one call to the next 125 125 how much of ARGV has been scanned so far. */ 126 126 127 127 /* 1003.2 says this must be 1 before any call. */ 128 int optind = 1;129 130 /* Formerly, initialization of getopt depended onoptind==0, which131 causes problems with re-calling getopt as programs generally don't128 int pj_optind = 1; 129 130 /* Formerly, initialization of pj_getopt depended on pj_optind==0, which 131 causes problems with re-calling pj_getopt as programs generally don't 132 132 know that. */ 133 133 … … 145 145 /* Set to an option character which was unrecognized. 146 146 This must be initialized on some systems to avoid linking in the 147 system's own getopt implementation. */148 149 int optopt = '?';147 system's own pj_getopt implementation. */ 148 149 int pj_optopt = '?'; 150 150 151 151 /* Describe how to deal with options that follow non-option ARGV-elements. … … 176 176 The special argument `--' forces an end of option-scanning regardless 177 177 of the value of `ordering'. In the case of RETURN_IN_ORDER, only 178 `--' can cause ` getopt' to return -1 with `optind' != ARGC. */178 `--' can cause `pj_getopt' to return -1 with `pj_optind' != ARGC. */ 179 179 180 180 static enum … … 213 213 One subsequence is elements [first_nonopt,last_nonopt) 214 214 which contains all the non-options that have been skipped so far. 215 The other is elements [last_nonopt, optind), which contains all215 The other is elements [last_nonopt,pj_optind), which contains all 216 216 the options processed since those non-options were skipped. 217 217 … … 224 224 int bottom = first_nonopt; 225 225 int middle = last_nonopt; 226 int top = optind;226 int top = pj_optind; 227 227 char *tem; 228 228 … … 272 272 /* Update records for the slots the non-options now occupy. */ 273 273 274 first_nonopt += ( optind - last_nonopt);275 last_nonopt = optind;274 first_nonopt += (pj_optind - last_nonopt); 275 last_nonopt = pj_optind; 276 276 } 277 277 … … 288 288 non-option ARGV-elements is empty. */ 289 289 290 first_nonopt = last_nonopt = optind;290 first_nonopt = last_nonopt = pj_optind; 291 291 292 292 nextchar = NULL; … … 320 320 If an element of ARGV starts with '-', and is not exactly "-" or "--", 321 321 then it is an option element. The characters of this element 322 (aside from the initial '-') are option characters. If ` getopt'322 (aside from the initial '-') are option characters. If `pj_getopt' 323 323 is called repeatedly, it returns successively each of the option characters 324 324 from each of the option elements. 325 325 326 If ` getopt' finds another option character, it returns that character,327 updating ` optind' and `nextchar' so that the next call to `getopt' can326 If `pj_getopt' finds another option character, it returns that character, 327 updating `pj_optind' and `nextchar' so that the next call to `pj_getopt' can 328 328 resume the scan with the following option character or ARGV-element. 329 329 330 If there are no more option characters, ` getopt' returns -1.331 Then ` optind' is the index in ARGV of the first ARGV-element330 If there are no more option characters, `pj_getopt' returns -1. 331 Then `pj_optind' is the index in ARGV of the first ARGV-element 332 332 that is not an option. (The ARGV-elements have been permuted 333 333 so that those that are not options now come last.) … … 335 335 OPTSTRING is a string containing the legitimate option characters. 336 336 If an option character is seen that is not listed in OPTSTRING, 337 return '?' after printing an error message. If you set ` opterr' to337 return '?' after printing an error message. If you set `pj_opterr' to 338 338 zero, the error message is suppressed but we still return '?'. 339 339 340 340 If a char in OPTSTRING is followed by a colon, that means it wants an arg, 341 341 so the following text in the same ARGV-element, or the text of the following 342 ARGV-element, is returned in ` optarg'. Two colons mean an option that342 ARGV-element, is returned in `pj_optarg'. Two colons mean an option that 343 343 wants an optional arg; if there is text in the current ARGV-element, 344 it is returned in ` optarg', otherwise `optarg' is set to zero.344 it is returned in `pj_optarg', otherwise `pj_optarg' is set to zero. 345 345 346 346 If OPTSTRING starts with `-' or `+', it requests different methods of … … 353 353 argument, it follows the option name in the same ARGV-element, separated 354 354 from the option name by a `=', or else the in next ARGV-element. 355 When ` getopt' finds a long-named option, it returns 0 if that option's355 When `pj_getopt' finds a long-named option, it returns 0 if that option's 356 356 `flag' field is nonzero, the value of the option's `val' field 357 357 if the `flag' field is zero. … … 361 361 with other systems. 362 362 363 LONGOPTS is a vector of `struct option' terminated by an363 LONGOPTS is a vector of `struct pj_getopt_option' terminated by an 364 364 element containing a name which is zero. 365 365 … … 373 373 static int 374 374 _getopt_internal (int argc, char *const *argv, const char *optstring, 375 const struct option *longopts, int *longind,375 const struct pj_getopt_option *longopts, int *longind, 376 376 int long_only) 377 377 { 378 optarg = NULL;379 380 if ( optind == 0 || !__getopt_initialized)378 pj_optarg = NULL; 379 380 if (pj_optind == 0 || !__getopt_initialized) 381 381 { 382 if ( optind == 0)383 optind = 1; /* Don't scan ARGV[0], the program name. */382 if (pj_optind == 0) 383 pj_optind = 1; /* Don't scan ARGV[0], the program name. */ 384 384 optstring = _getopt_initialize (argc, argv, optstring); 385 385 __getopt_initialized = 1; 386 386 } 387 387 388 /* Test whether ARGV[ optind] points to a non-option argument.388 /* Test whether ARGV[pj_optind] points to a non-option argument. 389 389 Either it does not have option syntax, or there is an environment flag 390 390 from the shell indicating it is not an option. The later information 391 391 is only used when the used in the GNU libc. */ 392 #define NONOPTION_P (argv[ optind][0] != '-' || argv[optind][1] == '\0')392 #define NONOPTION_P (argv[pj_optind][0] != '-' || argv[pj_optind][1] == '\0') 393 393 394 394 if (nextchar == NULL || *nextchar == '\0') … … 398 398 /* Give FIRST_NONOPT & LAST_NONOPT rational values if OPTIND has been 399 399 moved back by the user (who may also have changed the arguments). */ 400 if (last_nonopt > optind)401 last_nonopt = optind;402 if (first_nonopt > optind)403 first_nonopt = optind;400 if (last_nonopt > pj_optind) 401 last_nonopt = pj_optind; 402 if (first_nonopt > pj_optind) 403 first_nonopt = pj_optind; 404 404 405 405 if (ordering == PERMUTE) … … 408 408 exchange them so that the options come first. */ 409 409 410 if (first_nonopt != last_nonopt && last_nonopt != optind)410 if (first_nonopt != last_nonopt && last_nonopt != pj_optind) 411 411 exchange ((char **) argv); 412 else if (last_nonopt != optind)413 first_nonopt = optind;412 else if (last_nonopt != pj_optind) 413 first_nonopt = pj_optind; 414 414 415 415 /* Skip any additional non-options 416 416 and extend the range of non-options previously skipped. */ 417 417 418 while ( optind < argc && NONOPTION_P)419 optind++;420 last_nonopt = optind;418 while (pj_optind < argc && NONOPTION_P) 419 pj_optind++; 420 last_nonopt = pj_optind; 421 421 } 422 422 … … 426 426 then skip everything else like a non-option. */ 427 427 428 if ( optind != argc && !pj_ansi_strcmp(argv[optind], "--"))429 { 430 optind++;431 432 if (first_nonopt != last_nonopt && last_nonopt != optind)428 if (pj_optind != argc && !pj_ansi_strcmp(argv[pj_optind], "--")) 429 { 430 pj_optind++; 431 432 if (first_nonopt != last_nonopt && last_nonopt != pj_optind) 433 433 exchange ((char **) argv); 434 434 else if (first_nonopt == last_nonopt) 435 first_nonopt = optind;435 first_nonopt = pj_optind; 436 436 last_nonopt = argc; 437 437 438 optind = argc;438 pj_optind = argc; 439 439 } 440 440 … … 442 442 and back over any non-options that we skipped and permuted. */ 443 443 444 if ( optind == argc)444 if (pj_optind == argc) 445 445 { 446 446 /* Set the next-arg-index to point at the non-options 447 447 that we previously skipped, so the caller will digest them. */ 448 448 if (first_nonopt != last_nonopt) 449 optind = first_nonopt;449 pj_optind = first_nonopt; 450 450 return -1; 451 451 } … … 458 458 if (ordering == REQUIRE_ORDER) 459 459 return -1; 460 optarg = argv[optind++];460 pj_optarg = argv[pj_optind++]; 461 461 return 1; 462 462 } … … 465 465 Skip the initial punctuation. */ 466 466 467 nextchar = (argv[ optind] + 1468 + (longopts != NULL && argv[ optind][1] == '-'));467 nextchar = (argv[pj_optind] + 1 468 + (longopts != NULL && argv[pj_optind][1] == '-')); 469 469 } 470 470 … … 485 485 486 486 if (longopts != NULL 487 && (argv[ optind][1] == '-'488 || (long_only && (argv[ optind][2] || !my_index (optstring, argv[optind][1])))))487 && (argv[pj_optind][1] == '-' 488 || (long_only && (argv[pj_optind][2] || !my_index (optstring, argv[pj_optind][1]))))) 489 489 { 490 490 char *nameend; 491 const struct option *p;492 const struct option *pfound = NULL;491 const struct pj_getopt_option *p; 492 const struct pj_getopt_option *pfound = NULL; 493 493 int exact = 0; 494 494 int ambig = 0; … … 527 527 { 528 528 nextchar += strlen (nextchar); 529 optind++;530 optopt = 0;529 pj_optind++; 530 pj_optopt = 0; 531 531 return '?'; 532 532 } … … 535 535 { 536 536 option_index = indfound; 537 optind++;537 pj_optind++; 538 538 if (*nameend) 539 539 { … … 541 541 allow it to be used on enums. */ 542 542 if (pfound->has_arg) 543 optarg = nameend + 1;543 pj_optarg = nameend + 1; 544 544 else 545 545 { 546 546 nextchar += strlen (nextchar); 547 547 548 optopt = pfound->val;548 pj_optopt = pfound->val; 549 549 return '?'; 550 550 } … … 552 552 else if (pfound->has_arg == 1) 553 553 { 554 if ( optind < argc)555 optarg = argv[optind++];554 if (pj_optind < argc) 555 pj_optarg = argv[pj_optind++]; 556 556 else 557 557 { 558 558 nextchar += strlen (nextchar); 559 optopt = pfound->val;559 pj_optopt = pfound->val; 560 560 return optstring[0] == ':' ? ':' : '?'; 561 561 } … … 572 572 } 573 573 574 /* Can't find it as a long option. If this is not getopt_long_only,574 /* Can't find it as a long option. If this is not pj_getopt_long_only, 575 575 or the option starts with '--' or is not a valid short 576 576 option, then it's an error. 577 577 Otherwise interpret it as a short option. */ 578 if (!long_only || argv[ optind][1] == '-'578 if (!long_only || argv[pj_optind][1] == '-' 579 579 || my_index (optstring, *nextchar) == NULL) 580 580 { 581 581 nextchar = (char *) ""; 582 optind++;583 optopt = 0;582 pj_optind++; 583 pj_optopt = 0; 584 584 return '?'; 585 585 } … … 592 592 char *temp = my_index (optstring, c); 593 593 594 /* Increment ` optind' when we start to process its last character. */594 /* Increment `pj_optind' when we start to process its last character. */ 595 595 if (*nextchar == '\0') 596 ++ optind;596 ++pj_optind; 597 597 598 598 if (temp == NULL || c == ':') 599 599 { 600 optopt = c;600 pj_optopt = c; 601 601 return '?'; 602 602 } … … 605 605 { 606 606 char *nameend; 607 const struct option *p;608 const struct option *pfound = NULL;607 const struct pj_getopt_option *p; 608 const struct pj_getopt_option *pfound = NULL; 609 609 int exact = 0; 610 610 int ambig = 0; … … 615 615 if (*nextchar != '\0') 616 616 { 617 optarg = nextchar;617 pj_optarg = nextchar; 618 618 /* If we end this ARGV-element by taking the rest as an arg, 619 619 we must advance to the next element now. */ 620 optind++;620 pj_optind++; 621 621 } 622 else if ( optind == argc)622 else if (pj_optind == argc) 623 623 { 624 optopt = c;624 pj_optopt = c; 625 625 if (optstring[0] == ':') 626 626 c = ':'; … … 630 630 } 631 631 else 632 /* We already incremented ` optind' once;632 /* We already incremented `pj_optind' once; 633 633 increment it again when taking next ARGV-elt as argument. */ 634 optarg = argv[optind++];635 636 /* optarg is now the argument, see if it's in the634 pj_optarg = argv[pj_optind++]; 635 636 /* pj_optarg is now the argument, see if it's in the 637 637 table of longopts. */ 638 638 639 for (nextchar = nameend = optarg; *nameend && *nameend != '='; nameend++)639 for (nextchar = nameend = pj_optarg; *nameend && *nameend != '='; nameend++) 640 640 /* Do nothing. */ ; 641 641 … … 666 666 { 667 667 nextchar += strlen (nextchar); 668 optind++;668 pj_optind++; 669 669 return '?'; 670 670 } … … 677 677 allow it to be used on enums. */ 678 678 if (pfound->has_arg) 679 optarg = nameend + 1;679 pj_optarg = nameend + 1; 680 680 else 681 681 { … … 686 686 else if (pfound->has_arg == 1) 687 687 { 688 if ( optind < argc)689 optarg = argv[optind++];688 if (pj_optind < argc) 689 pj_optarg = argv[pj_optind++]; 690 690 else 691 691 { … … 714 714 if (*nextchar != '\0') 715 715 { 716 optarg = nextchar;717 optind++;716 pj_optarg = nextchar; 717 pj_optind++; 718 718 } 719 719 else 720 optarg = NULL;720 pj_optarg = NULL; 721 721 nextchar = NULL; 722 722 } … … 726 726 if (*nextchar != '\0') 727 727 { 728 optarg = nextchar;728 pj_optarg = nextchar; 729 729 /* If we end this ARGV-element by taking the rest as an arg, 730 730 we must advance to the next element now. */ 731 optind++;731 pj_optind++; 732 732 } 733 else if ( optind == argc)733 else if (pj_optind == argc) 734 734 { 735 optopt = c;735 pj_optopt = c; 736 736 if (optstring[0] == ':') 737 737 c = ':'; … … 740 740 } 741 741 else 742 /* We already incremented ` optind' once;742 /* We already incremented `pj_optind' once; 743 743 increment it again when taking next ARGV-elt as argument. */ 744 optarg = argv[optind++];744 pj_optarg = argv[pj_optind++]; 745 745 nextchar = NULL; 746 746 }
Note: See TracChangeset
for help on using the changeset viewer.