comparison lib/getopt.c @ 637:1df6981c6f31

use latest from gettext-0.10.23
author Jim Meyering <jim@meyering.net>
date Thu, 11 Jul 1996 03:57:04 +0000
parents 39f6e38131a6
children acf83993b98e
comparison
equal deleted inserted replaced
636:ddd029a89661 637:1df6981c6f31
1 /* Getopt for GNU. 1 /* Getopt for GNU.
2 NOTE: getopt is now part of the C library, so if you don't know what 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 3 "Keep this file name-space clean" means, talk to roland@gnu.ai.mit.edu
4 before changing it! 4 before changing it!
5 5
6 Copyright (C) 1987, 88, 89, 90, 91, 92, 93, 94, 95 6 Copyright (C) 1987, 88, 89, 90, 91, 92, 93, 94, 95, 1996
7 Free Software Foundation, Inc. 7 Free Software Foundation, Inc.
8 8
9 This program is free software; you can redistribute it and/or modify it 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 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 11 Free Software Foundation; either version 2, or (at your option) any
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details. 17 GNU General Public License for more details.
18 18
19 You should have received a copy of the GNU General Public License 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 20 along with this program; if not, write to the Free Software
21 Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. */ 21 Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
22 22
23 /* This tells Alpha OSF/1 not to define a getopt prototype in <stdio.h>. 23 /* This tells Alpha OSF/1 not to define a getopt prototype in <stdio.h>.
24 Ditto for AIX 3.2 and <stdlib.h>. */ 24 Ditto for AIX 3.2 and <stdlib.h>. */
25 #ifndef _NO_PROTO 25 #ifndef _NO_PROTO
26 #define _NO_PROTO 26 #define _NO_PROTO
51 #if defined (_LIBC) || !defined (__GNU_LIBRARY__) 51 #if defined (_LIBC) || !defined (__GNU_LIBRARY__)
52 52
53 53
54 /* This needs to come after some library #include 54 /* This needs to come after some library #include
55 to get __GNU_LIBRARY__ defined. */ 55 to get __GNU_LIBRARY__ defined. */
56 #ifdef __GNU_LIBRARY__ 56 #ifdef __GNU_LIBRARY__
57 /* Don't include stdlib.h for non-GNU C libraries because some of them 57 /* Don't include stdlib.h for non-GNU C libraries because some of them
58 contain conflicting prototypes for getopt. */ 58 contain conflicting prototypes for getopt. */
59 #include <stdlib.h> 59 #include <stdlib.h>
60 #if defined (_LIBC) || defined (HAVE_UNISTD_H)
61 #include <unistd.h>
62 #endif
60 #endif /* GNU C library. */ 63 #endif /* GNU C library. */
64
65 #ifdef VMS
66 #include <unixlib.h>
67 #if HAVE_STRING_H - 0
68 #include <string.h>
69 #endif
70 #endif
71
72 #ifdef WIN32
73 /* It's not Unix, really. See? Capital letters. */
74 #include <windows.h>
75 #define getpid() GetCurrentProcessId()
76 #endif
61 77
62 #ifndef _ 78 #ifndef _
63 /* This is for other GNU distributions with internationalized messages. 79 /* This is for other GNU distributions with internationalized messages.
64 When compiling libc, the _ macro is predefined. */ 80 When compiling libc, the _ macro is predefined. */
65 #ifdef HAVE_LIBINTL_H 81 #ifdef HAVE_LIBINTL_H
164 } ordering; 180 } ordering;
165 181
166 /* Value of POSIXLY_CORRECT environment variable. */ 182 /* Value of POSIXLY_CORRECT environment variable. */
167 static char *posixly_correct; 183 static char *posixly_correct;
168 184
169 #ifdef __GNU_LIBRARY__ 185 #ifdef __GNU_LIBRARY__
170 /* We want to avoid inclusion of string.h with non-GNU libraries 186 /* We want to avoid inclusion of string.h with non-GNU libraries
171 because there are many ways it can cause trouble. 187 because there are many ways it can cause trouble.
172 On some systems, it contains special magic macros that don't work 188 On some systems, it contains special magic macros that don't work
173 in GCC. */ 189 in GCC. */
174 #include <string.h> 190 #include <string.h>
215 `last_nonopt' is the index after the last of them. */ 231 `last_nonopt' is the index after the last of them. */
216 232
217 static int first_nonopt; 233 static int first_nonopt;
218 static int last_nonopt; 234 static int last_nonopt;
219 235
236 /* Bash 2.0 gives us an environment variable containing flags
237 indicating ARGV elements that should not be considered arguments. */
238
239 static const char *nonoption_flags;
240 static int nonoption_flags_len;
241
220 /* Exchange two adjacent subsequences of ARGV. 242 /* Exchange two adjacent subsequences of ARGV.
221 One subsequence is elements [first_nonopt,last_nonopt) 243 One subsequence is elements [first_nonopt,last_nonopt)
222 which contains all the non-options that have been skipped so far. 244 which contains all the non-options that have been skipped so far.
223 The other is elements [last_nonopt,optind), which contains all 245 The other is elements [last_nonopt,optind), which contains all
224 the options processed since those non-options were skipped. 246 the options processed since those non-options were skipped.
225 247
226 `first_nonopt' and `last_nonopt' are relocated so that they describe 248 `first_nonopt' and `last_nonopt' are relocated so that they describe
227 the new indices of the non-options in ARGV after they are moved. */ 249 the new indices of the non-options in ARGV after they are moved. */
250
251 #if defined (__STDC__) && __STDC__
252 static void exchange (char **);
253 #endif
228 254
229 static void 255 static void
230 exchange (argv) 256 exchange (argv)
231 char **argv; 257 char **argv;
232 { 258 {
282 last_nonopt = optind; 308 last_nonopt = optind;
283 } 309 }
284 310
285 /* Initialize the internal data when the first call is made. */ 311 /* Initialize the internal data when the first call is made. */
286 312
313 #if defined (__STDC__) && __STDC__
314 static const char *_getopt_initialize (const char *);
315 #endif
287 static const char * 316 static const char *
288 _getopt_initialize (optstring) 317 _getopt_initialize (optstring)
289 const char *optstring; 318 const char *optstring;
290 { 319 {
291 /* Start processing options with ARGV-element 1 (since ARGV-element 0 320 /* Start processing options with ARGV-element 1 (since ARGV-element 0
312 } 341 }
313 else if (posixly_correct != NULL) 342 else if (posixly_correct != NULL)
314 ordering = REQUIRE_ORDER; 343 ordering = REQUIRE_ORDER;
315 else 344 else
316 ordering = PERMUTE; 345 ordering = PERMUTE;
346
347 if (posixly_correct == NULL)
348 {
349 /* Bash 2.0 puts a special variable in the environment for each
350 command it runs, specifying which ARGV elements are the results of
351 file name wildcard expansion and therefore should not be
352 considered as options. */
353 char var[100];
354 sprintf (var, "_%d_GNU_nonoption_argv_flags_", getpid ());
355 nonoption_flags = getenv (var);
356 if (nonoption_flags == NULL)
357 nonoption_flags_len = 0;
358 else
359 nonoption_flags_len = strlen (nonoption_flags);
360 }
317 361
318 return optstring; 362 return optstring;
319 } 363 }
320 364
321 /* Scan elements of ARGV (whose length is ARGC) for option characters 365 /* Scan elements of ARGV (whose length is ARGC) for option characters
389 { 433 {
390 optstring = _getopt_initialize (optstring); 434 optstring = _getopt_initialize (optstring);
391 optind = 1; /* Don't scan ARGV[0], the program name. */ 435 optind = 1; /* Don't scan ARGV[0], the program name. */
392 } 436 }
393 437
438 /* Test whether ARGV[optind] points to a non-option argument.
439 Either it does not have option syntax, or there is an environment flag
440 from the shell indicating it is not an option. */
441 #define NONOPTION_P (argv[optind][0] != '-' || argv[optind][1] == '\0' \
442 || (optind < nonoption_flags_len \
443 && nonoption_flags[optind] == '1'))
444
394 if (nextchar == NULL || *nextchar == '\0') 445 if (nextchar == NULL || *nextchar == '\0')
395 { 446 {
396 /* Advance to the next ARGV-element. */ 447 /* Advance to the next ARGV-element. */
448
449 /* Give FIRST_NONOPT & LAST_NONOPT rational values if OPTIND has been
450 moved back by the user (who may also have changed the arguments). */
451 if (last_nonopt > optind)
452 last_nonopt = optind;
453 if (first_nonopt > optind)
454 first_nonopt = optind;
397 455
398 if (ordering == PERMUTE) 456 if (ordering == PERMUTE)
399 { 457 {
400 /* If we have just processed some options following some non-options, 458 /* If we have just processed some options following some non-options,
401 exchange them so that the options come first. */ 459 exchange them so that the options come first. */
406 first_nonopt = optind; 464 first_nonopt = optind;
407 465
408 /* Skip any additional non-options 466 /* Skip any additional non-options
409 and extend the range of non-options previously skipped. */ 467 and extend the range of non-options previously skipped. */
410 468
411 while (optind < argc 469 while (optind < argc && NONOPTION_P)
412 && (argv[optind][0] != '-' || argv[optind][1] == '\0'))
413 optind++; 470 optind++;
414 last_nonopt = optind; 471 last_nonopt = optind;
415 } 472 }
416 473
417 /* The special ARGV-element `--' means premature end of options. 474 /* The special ARGV-element `--' means premature end of options.
445 } 502 }
446 503
447 /* If we have come to a non-option and did not permute it, 504 /* If we have come to a non-option and did not permute it,
448 either stop the scan or describe it to the caller and pass it by. */ 505 either stop the scan or describe it to the caller and pass it by. */
449 506
450 if ((argv[optind][0] != '-' || argv[optind][1] == '\0')) 507 if (NONOPTION_P)
451 { 508 {
452 if (ordering == REQUIRE_ORDER) 509 if (ordering == REQUIRE_ORDER)
453 return EOF; 510 return EOF;
454 optarg = argv[optind++]; 511 optarg = argv[optind++];
455 return 1; 512 return 1;
477 534
478 This distinction seems to be the most useful approach. */ 535 This distinction seems to be the most useful approach. */
479 536
480 if (longopts != NULL 537 if (longopts != NULL
481 && (argv[optind][1] == '-' 538 && (argv[optind][1] == '-'
482 || (long_only && (argv[optind][2] 539 || (long_only && (argv[optind][2] || !my_index (optstring, argv[optind][1])))))
483 || !my_index (optstring, argv[optind][1])))))
484 { 540 {
485 char *nameend; 541 char *nameend;
486 const struct option *p; 542 const struct option *p;
487 const struct option *pfound = NULL; 543 const struct option *pfound = NULL;
488 int exact = 0; 544 int exact = 0;
491 int option_index; 547 int option_index;
492 548
493 for (nameend = nextchar; *nameend && *nameend != '='; nameend++) 549 for (nameend = nextchar; *nameend && *nameend != '='; nameend++)
494 /* Do nothing. */ ; 550 /* Do nothing. */ ;
495 551
496 #ifdef lint 552 #ifdef lint /* Suppress `used before initialized' warning. */
497 indfound = 0; /* Avoid spurious compiler warning. */ 553 indfound = 0;
498 #endif 554 #endif
499 555
500 /* Test all long options for either exact match 556 /* Test all long options for either exact match
501 or abbreviated matches. */ 557 or abbreviated matches. */
502 for (p = longopts, option_index = 0; p->name; p++, option_index++) 558 for (p = longopts, option_index = 0; p->name; p++, option_index++)
526 if (opterr) 582 if (opterr)
527 fprintf (stderr, _("%s: option `%s' is ambiguous\n"), 583 fprintf (stderr, _("%s: option `%s' is ambiguous\n"),
528 argv[0], argv[optind]); 584 argv[0], argv[optind]);
529 nextchar += strlen (nextchar); 585 nextchar += strlen (nextchar);
530 optind++; 586 optind++;
587 optopt = 0;
531 return '?'; 588 return '?';
532 } 589 }
533 590
534 if (pfound != NULL) 591 if (pfound != NULL)
535 { 592 {
542 if (pfound->has_arg) 599 if (pfound->has_arg)
543 optarg = nameend + 1; 600 optarg = nameend + 1;
544 else 601 else
545 { 602 {
546 if (opterr) 603 if (opterr)
547 if (argv[optind - 1][1] == '-') 604 if (argv[optind - 1][1] == '-')
548 /* --option */ 605 /* --option */
549 fprintf (stderr, 606 fprintf (stderr,
550 _("%s: option `--%s' doesn't allow an argument\n"), 607 _("%s: option `--%s' doesn't allow an argument\n"),
551 argv[0], pfound->name); 608 argv[0], pfound->name);
552 else 609 else
553 /* +option or -option */ 610 /* +option or -option */
554 fprintf (stderr, 611 fprintf (stderr,
555 _("%s: option `%c%s' doesn't allow an argument\n"), 612 _("%s: option `%c%s' doesn't allow an argument\n"),
556 argv[0], argv[optind - 1][0], pfound->name); 613 argv[0], argv[optind - 1][0], pfound->name);
557 614
558 nextchar += strlen (nextchar); 615 nextchar += strlen (nextchar);
616
617 optopt = pfound->val;
559 return '?'; 618 return '?';
560 } 619 }
561 } 620 }
562 else if (pfound->has_arg == 1) 621 else if (pfound->has_arg == 1)
563 { 622 {
565 optarg = argv[optind++]; 624 optarg = argv[optind++];
566 else 625 else
567 { 626 {
568 if (opterr) 627 if (opterr)
569 fprintf (stderr, 628 fprintf (stderr,
570 _("%s: option `%s' requires an argument\n"), 629 _("%s: option `%s' requires an argument\n"),
571 argv[0], argv[optind - 1]); 630 argv[0], argv[optind - 1]);
572 nextchar += strlen (nextchar); 631 nextchar += strlen (nextchar);
632 optopt = pfound->val;
573 return optstring[0] == ':' ? ':' : '?'; 633 return optstring[0] == ':' ? ':' : '?';
574 } 634 }
575 } 635 }
576 nextchar += strlen (nextchar); 636 nextchar += strlen (nextchar);
577 if (longind != NULL) 637 if (longind != NULL)
602 fprintf (stderr, _("%s: unrecognized option `%c%s'\n"), 662 fprintf (stderr, _("%s: unrecognized option `%c%s'\n"),
603 argv[0], argv[optind][0], nextchar); 663 argv[0], argv[optind][0], nextchar);
604 } 664 }
605 nextchar = (char *) ""; 665 nextchar = (char *) "";
606 optind++; 666 optind++;
667 optopt = 0;
607 return '?'; 668 return '?';
608 } 669 }
609 } 670 }
610 671
611 /* Look at and handle the next short option-character. */ 672 /* Look at and handle the next short option-character. */
661 { 722 {
662 if (opterr) 723 if (opterr)
663 { 724 {
664 /* 1003.2 specifies the format of this message. */ 725 /* 1003.2 specifies the format of this message. */
665 fprintf (stderr, 726 fprintf (stderr,
666 _("%s: option requires an argument -- %c\n"), 727 _("%s: option requires an argument -- %c\n"),
667 argv[0], c); 728 argv[0], c);
668 } 729 }
669 optopt = c; 730 optopt = c;
670 if (optstring[0] == ':') 731 if (optstring[0] == ':')
671 c = ':'; 732 c = ':';
672 else 733 else