~ubuntu-branches/ubuntu/maverick/wget/maverick

« back to all changes in this revision

Viewing changes to src/getopt.c

  • Committer: Bazaar Package Importer
  • Author(s): Michael Vogt
  • Date: 2008-05-27 11:49:54 UTC
  • mfrom: (1.1.3 upstream)
  • Revision ID: james.westby@ubuntu.com-20080527114954-ame070pjhqtofeaf
Tags: 1.11.2-1ubuntu1
* Merge from debian unstable, remaining changes:
  - Add wget-udeb to ship wget.gnu as alternative to busybox wget
    implementation.

Show diffs side-by-side

added added

removed removed

Lines of Context:
3
3
   C Library.  Bugs can be reported to bug-glibc@gnu.org.
4
4
 
5
5
   Copyright (C) 1987, 88, 89, 90, 91, 92, 93, 94, 95, 96, 97, 98, 99
6
 
        Free Software Foundation, Inc.
 
6
        Free Software Foundation, Inc.
7
7
 
8
8
   This program is free software; you can redistribute it and/or modify it
9
9
   under the terms of the GNU General Public License as published by the
10
 
   Free Software Foundation; either version 2, or (at your option) any
 
10
   Free Software Foundation; either version 3, or (at your option) any
11
11
   later version.
12
12
 
13
13
   This program is distributed in the hope that it will be useful,
16
16
   GNU General Public License for more details.
17
17
 
18
18
   You should have received a copy of the GNU General Public License
19
 
   along with this program; if not, write to the Free Software Foundation,
20
 
   Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.  */
 
19
   along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
21
20
 
22
21
/* This tells Alpha OSF/1 not to define a getopt prototype in <stdio.h>.
23
22
   Ditto for AIX 3.2 and <stdlib.h>.  */
60
59
 
61
60
/* This needs to come after some library #include
62
61
   to get __GNU_LIBRARY__ defined.  */
63
 
#ifdef  __GNU_LIBRARY__
 
62
#ifdef  __GNU_LIBRARY__
64
63
/* Don't include stdlib.h for non-GNU C libraries because some of them
65
64
   contain conflicting prototypes for getopt.  */
66
65
# include <stdlib.h>
67
66
# include <unistd.h>
68
 
#endif  /* GNU C library.  */
 
67
#endif  /* GNU C library.  */
69
68
 
70
69
#ifdef VMS
71
70
# include <unixlib.h>
79
78
   When compiling libc, the _ macro is predefined.  */
80
79
# ifdef HAVE_LIBINTL_H
81
80
#  include <libintl.h>
82
 
#  define _(msgid)      gettext (msgid)
 
81
#  define _(msgid)      gettext (msgid)
83
82
# else
84
 
#  define _(msgid)      (msgid)
 
83
#  define _(msgid)      (msgid)
85
84
# endif
86
85
#endif
87
86
 
187
186
/* Value of POSIXLY_CORRECT environment variable.  */
188
187
static char *posixly_correct;
189
188
 
190
 
#ifdef  __GNU_LIBRARY__
 
189
#ifdef  __GNU_LIBRARY__
191
190
/* We want to avoid inclusion of string.h with non-GNU libraries
192
191
   because there are many ways it can cause trouble.
193
192
   On some systems, it contains special magic macros that don't work
194
193
   in GCC.  */
195
194
# include <string.h>
196
 
# define my_index       strchr
 
195
# define my_index       strchr
197
196
#else
198
197
 
199
 
# if HAVE_STRING_H
200
 
#  include <string.h>
201
 
# else
202
 
#  include <strings.h>
203
 
# endif
 
198
#include <string.h>
204
199
 
205
200
/* Avoid depending on library functions or files
206
201
   whose names are inconsistent.  */
217
212
  while (*str)
218
213
    {
219
214
      if (*str == chr)
220
 
        return (char *) str;
 
215
        return (char *) str;
221
216
      str++;
222
217
    }
223
218
  return 0;
276
271
# endif /* text_set_element */
277
272
 
278
273
# define SWAP_FLAGS(ch1, ch2) \
279
 
  if (nonoption_flags_len > 0)                                                \
280
 
    {                                                                         \
281
 
      char __tmp = __getopt_nonoption_flags[ch1];                             \
282
 
      __getopt_nonoption_flags[ch1] = __getopt_nonoption_flags[ch2];          \
283
 
      __getopt_nonoption_flags[ch2] = __tmp;                                  \
 
274
  if (nonoption_flags_len > 0)                                                \
 
275
    {                                                                         \
 
276
      char __tmp = __getopt_nonoption_flags[ch1];                             \
 
277
      __getopt_nonoption_flags[ch1] = __getopt_nonoption_flags[ch2];          \
 
278
      __getopt_nonoption_flags[ch2] = __tmp;                                  \
284
279
    }
285
 
#else   /* !_LIBC */
 
280
#else   /* !_LIBC */
286
281
# define SWAP_FLAGS(ch1, ch2)
287
 
#endif  /* _LIBC */
 
282
#endif  /* _LIBC */
288
283
 
289
284
/* Exchange two adjacent subsequences of ARGV.
290
285
   One subsequence is elements [first_nonopt,last_nonopt)
320
315
  if (nonoption_flags_len > 0 && top >= nonoption_flags_max_len)
321
316
    {
322
317
      /* We must extend the array.  The user plays games with us and
323
 
         presents new arguments.  */
 
318
         presents new arguments.  */
324
319
      char *new_str = malloc (top + 1);
325
320
      if (new_str == NULL)
326
 
        nonoption_flags_len = nonoption_flags_max_len = 0;
 
321
        nonoption_flags_len = nonoption_flags_max_len = 0;
327
322
      else
328
 
        {
329
 
          memset (__mempcpy (new_str, __getopt_nonoption_flags,
330
 
                             nonoption_flags_max_len),
331
 
                  '\0', top + 1 - nonoption_flags_max_len);
332
 
          nonoption_flags_max_len = top + 1;
333
 
          __getopt_nonoption_flags = new_str;
334
 
        }
 
323
        {
 
324
          memset (__mempcpy (new_str, __getopt_nonoption_flags,
 
325
                             nonoption_flags_max_len),
 
326
                  '\0', top + 1 - nonoption_flags_max_len);
 
327
          nonoption_flags_max_len = top + 1;
 
328
          __getopt_nonoption_flags = new_str;
 
329
        }
335
330
    }
336
331
#endif
337
332
 
338
333
  while (top > middle && middle > bottom)
339
334
    {
340
335
      if (top - middle > middle - bottom)
341
 
        {
342
 
          /* Bottom segment is the short one.  */
343
 
          int len = middle - bottom;
344
 
          register int i;
 
336
        {
 
337
          /* Bottom segment is the short one.  */
 
338
          int len = middle - bottom;
 
339
          register int i;
345
340
 
346
 
          /* Swap it with the top part of the top segment.  */
347
 
          for (i = 0; i < len; i++)
348
 
            {
349
 
              tem = argv[bottom + i];
350
 
              argv[bottom + i] = argv[top - (middle - bottom) + i];
351
 
              argv[top - (middle - bottom) + i] = tem;
352
 
              SWAP_FLAGS (bottom + i, top - (middle - bottom) + i);
353
 
            }
354
 
          /* Exclude the moved bottom segment from further swapping.  */
355
 
          top -= len;
356
 
        }
 
341
          /* Swap it with the top part of the top segment.  */
 
342
          for (i = 0; i < len; i++)
 
343
            {
 
344
              tem = argv[bottom + i];
 
345
              argv[bottom + i] = argv[top - (middle - bottom) + i];
 
346
              argv[top - (middle - bottom) + i] = tem;
 
347
              SWAP_FLAGS (bottom + i, top - (middle - bottom) + i);
 
348
            }
 
349
          /* Exclude the moved bottom segment from further swapping.  */
 
350
          top -= len;
 
351
        }
357
352
      else
358
 
        {
359
 
          /* Top segment is the short one.  */
360
 
          int len = top - middle;
361
 
          register int i;
 
353
        {
 
354
          /* Top segment is the short one.  */
 
355
          int len = top - middle;
 
356
          register int i;
362
357
 
363
 
          /* Swap it with the bottom part of the bottom segment.  */
364
 
          for (i = 0; i < len; i++)
365
 
            {
366
 
              tem = argv[bottom + i];
367
 
              argv[bottom + i] = argv[middle + i];
368
 
              argv[middle + i] = tem;
369
 
              SWAP_FLAGS (bottom + i, middle + i);
370
 
            }
371
 
          /* Exclude the moved top segment from further swapping.  */
372
 
          bottom += len;
373
 
        }
 
358
          /* Swap it with the bottom part of the bottom segment.  */
 
359
          for (i = 0; i < len; i++)
 
360
            {
 
361
              tem = argv[bottom + i];
 
362
              argv[bottom + i] = argv[middle + i];
 
363
              argv[middle + i] = tem;
 
364
              SWAP_FLAGS (bottom + i, middle + i);
 
365
            }
 
366
          /* Exclude the moved top segment from further swapping.  */
 
367
          bottom += len;
 
368
        }
374
369
    }
375
370
 
376
371
  /* Update records for the slots the non-options now occupy.  */
422
417
      && argc == original_argc && argv == original_argv)
423
418
    {
424
419
      if (nonoption_flags_max_len == 0)
425
 
        {
426
 
          if (__getopt_nonoption_flags == NULL
427
 
              || __getopt_nonoption_flags[0] == '\0')
428
 
            nonoption_flags_max_len = -1;
429
 
          else
430
 
            {
431
 
              const char *orig_str = __getopt_nonoption_flags;
432
 
              int len = nonoption_flags_max_len = strlen (orig_str);
433
 
              if (nonoption_flags_max_len < argc)
434
 
                nonoption_flags_max_len = argc;
435
 
              __getopt_nonoption_flags =
436
 
                (char *) malloc (nonoption_flags_max_len);
437
 
              if (__getopt_nonoption_flags == NULL)
438
 
                nonoption_flags_max_len = -1;
439
 
              else
440
 
                memset (__mempcpy (__getopt_nonoption_flags, orig_str, len),
441
 
                        '\0', nonoption_flags_max_len - len);
442
 
            }
443
 
        }
 
420
        {
 
421
          if (__getopt_nonoption_flags == NULL
 
422
              || __getopt_nonoption_flags[0] == '\0')
 
423
            nonoption_flags_max_len = -1;
 
424
          else
 
425
            {
 
426
              const char *orig_str = __getopt_nonoption_flags;
 
427
              int len = nonoption_flags_max_len = strlen (orig_str);
 
428
              if (nonoption_flags_max_len < argc)
 
429
                nonoption_flags_max_len = argc;
 
430
              __getopt_nonoption_flags =
 
431
                (char *) malloc (nonoption_flags_max_len);
 
432
              if (__getopt_nonoption_flags == NULL)
 
433
                nonoption_flags_max_len = -1;
 
434
              else
 
435
                memset (__mempcpy (__getopt_nonoption_flags, orig_str, len),
 
436
                        '\0', nonoption_flags_max_len - len);
 
437
            }
 
438
        }
444
439
      nonoption_flags_len = nonoption_flags_max_len;
445
440
    }
446
441
  else
520
515
  if (optind == 0 || !__getopt_initialized)
521
516
    {
522
517
      if (optind == 0)
523
 
        optind = 1;     /* Don't scan ARGV[0], the program name.  */
 
518
        optind = 1;     /* Don't scan ARGV[0], the program name.  */
524
519
      optstring = _getopt_initialize (argc, argv, optstring);
525
520
      __getopt_initialized = 1;
526
521
    }
530
525
     from the shell indicating it is not an option.  The later information
531
526
     is only used when the used in the GNU libc.  */
532
527
#ifdef _LIBC
533
 
# define NONOPTION_P (argv[optind][0] != '-' || argv[optind][1] == '\0'       \
534
 
                      || (optind < nonoption_flags_len                        \
535
 
                          && __getopt_nonoption_flags[optind] == '1'))
 
528
# define NONOPTION_P (argv[optind][0] != '-' || argv[optind][1] == '\0'       \
 
529
                      || (optind < nonoption_flags_len                        \
 
530
                          && __getopt_nonoption_flags[optind] == '1'))
536
531
#else
537
532
# define NONOPTION_P (argv[optind][0] != '-' || argv[optind][1] == '\0')
538
533
#endif
542
537
      /* Advance to the next ARGV-element.  */
543
538
 
544
539
      /* Give FIRST_NONOPT & LAST_NONOPT rational values if OPTIND has been
545
 
         moved back by the user (who may also have changed the arguments).  */
 
540
         moved back by the user (who may also have changed the arguments).  */
546
541
      if (last_nonopt > optind)
547
 
        last_nonopt = optind;
 
542
        last_nonopt = optind;
548
543
      if (first_nonopt > optind)
549
 
        first_nonopt = optind;
 
544
        first_nonopt = optind;
550
545
 
551
546
      if (ordering == PERMUTE)
552
 
        {
553
 
          /* If we have just processed some options following some non-options,
554
 
             exchange them so that the options come first.  */
555
 
 
556
 
          if (first_nonopt != last_nonopt && last_nonopt != optind)
557
 
            exchange ((char **) argv);
558
 
          else if (last_nonopt != optind)
559
 
            first_nonopt = optind;
560
 
 
561
 
          /* Skip any additional non-options
562
 
             and extend the range of non-options previously skipped.  */
563
 
 
564
 
          while (optind < argc && NONOPTION_P)
565
 
            optind++;
566
 
          last_nonopt = optind;
567
 
        }
 
547
        {
 
548
          /* If we have just processed some options following some non-options,
 
549
             exchange them so that the options come first.  */
 
550
 
 
551
          if (first_nonopt != last_nonopt && last_nonopt != optind)
 
552
            exchange ((char **) argv);
 
553
          else if (last_nonopt != optind)
 
554
            first_nonopt = optind;
 
555
 
 
556
          /* Skip any additional non-options
 
557
             and extend the range of non-options previously skipped.  */
 
558
 
 
559
          while (optind < argc && NONOPTION_P)
 
560
            optind++;
 
561
          last_nonopt = optind;
 
562
        }
568
563
 
569
564
      /* The special ARGV-element `--' means premature end of options.
570
 
         Skip it like a null option,
571
 
         then exchange with previous non-options as if it were an option,
572
 
         then skip everything else like a non-option.  */
 
565
         Skip it like a null option,
 
566
         then exchange with previous non-options as if it were an option,
 
567
         then skip everything else like a non-option.  */
573
568
 
574
569
      if (optind != argc && !strcmp (argv[optind], "--"))
575
 
        {
576
 
          optind++;
577
 
 
578
 
          if (first_nonopt != last_nonopt && last_nonopt != optind)
579
 
            exchange ((char **) argv);
580
 
          else if (first_nonopt == last_nonopt)
581
 
            first_nonopt = optind;
582
 
          last_nonopt = argc;
583
 
 
584
 
          optind = argc;
585
 
        }
 
570
        {
 
571
          optind++;
 
572
 
 
573
          if (first_nonopt != last_nonopt && last_nonopt != optind)
 
574
            exchange ((char **) argv);
 
575
          else if (first_nonopt == last_nonopt)
 
576
            first_nonopt = optind;
 
577
          last_nonopt = argc;
 
578
 
 
579
          optind = argc;
 
580
        }
586
581
 
587
582
      /* If we have done all the ARGV-elements, stop the scan
588
 
         and back over any non-options that we skipped and permuted.  */
 
583
         and back over any non-options that we skipped and permuted.  */
589
584
 
590
585
      if (optind == argc)
591
 
        {
592
 
          /* Set the next-arg-index to point at the non-options
593
 
             that we previously skipped, so the caller will digest them.  */
594
 
          if (first_nonopt != last_nonopt)
595
 
            optind = first_nonopt;
596
 
          return -1;
597
 
        }
 
586
        {
 
587
          /* Set the next-arg-index to point at the non-options
 
588
             that we previously skipped, so the caller will digest them.  */
 
589
          if (first_nonopt != last_nonopt)
 
590
            optind = first_nonopt;
 
591
          return -1;
 
592
        }
598
593
 
599
594
      /* If we have come to a non-option and did not permute it,
600
 
         either stop the scan or describe it to the caller and pass it by.  */
 
595
         either stop the scan or describe it to the caller and pass it by.  */
601
596
 
602
597
      if (NONOPTION_P)
603
 
        {
604
 
          if (ordering == REQUIRE_ORDER)
605
 
            return -1;
606
 
          optarg = argv[optind++];
607
 
          return 1;
608
 
        }
 
598
        {
 
599
          if (ordering == REQUIRE_ORDER)
 
600
            return -1;
 
601
          optarg = argv[optind++];
 
602
          return 1;
 
603
        }
609
604
 
610
605
      /* We have found another option-ARGV-element.
611
 
         Skip the initial punctuation.  */
 
606
         Skip the initial punctuation.  */
612
607
 
613
608
      nextchar = (argv[optind] + 1
614
 
                  + (longopts != NULL && argv[optind][1] == '-'));
 
609
                  + (longopts != NULL && argv[optind][1] == '-'));
615
610
    }
616
611
 
617
612
  /* Decode the current option-ARGV-element.  */
631
626
 
632
627
  if (longopts != NULL
633
628
      && (argv[optind][1] == '-'
634
 
          || (long_only && (argv[optind][2] || !my_index (optstring, argv[optind][1])))))
 
629
          || (long_only && (argv[optind][2] || !my_index (optstring, argv[optind][1])))))
635
630
    {
636
631
      char *nameend;
637
632
      const struct option *p;
642
637
      int option_index;
643
638
 
644
639
      for (nameend = nextchar; *nameend && *nameend != '='; nameend++)
645
 
        /* Do nothing.  */ ;
 
640
        /* Do nothing.  */ ;
646
641
 
647
642
      /* Test all long options for either exact match
648
 
         or abbreviated matches.  */
 
643
         or abbreviated matches.  */
649
644
      for (p = longopts, option_index = 0; p->name; p++, option_index++)
650
 
        if (!strncmp (p->name, nextchar, nameend - nextchar))
651
 
          {
652
 
            if ((unsigned int) (nameend - nextchar)
653
 
                == (unsigned int) strlen (p->name))
654
 
              {
655
 
                /* Exact match found.  */
656
 
                pfound = p;
657
 
                indfound = option_index;
658
 
                exact = 1;
659
 
                break;
660
 
              }
661
 
            else if (pfound == NULL)
662
 
              {
663
 
                /* First nonexact match found.  */
664
 
                pfound = p;
665
 
                indfound = option_index;
666
 
              }
667
 
            else
668
 
              /* Second or later nonexact match found.  */
669
 
              ambig = 1;
670
 
          }
 
645
        if (!strncmp (p->name, nextchar, nameend - nextchar))
 
646
          {
 
647
            if ((unsigned int) (nameend - nextchar)
 
648
                == (unsigned int) strlen (p->name))
 
649
              {
 
650
                /* Exact match found.  */
 
651
                pfound = p;
 
652
                indfound = option_index;
 
653
                exact = 1;
 
654
                break;
 
655
              }
 
656
            else if (pfound == NULL)
 
657
              {
 
658
                /* First nonexact match found.  */
 
659
                pfound = p;
 
660
                indfound = option_index;
 
661
              }
 
662
            else
 
663
              /* Second or later nonexact match found.  */
 
664
              ambig = 1;
 
665
          }
671
666
 
672
667
      if (ambig && !exact)
673
 
        {
674
 
          if (opterr)
675
 
            fprintf (stderr, _("%s: option `%s' is ambiguous\n"),
676
 
                     argv[0], argv[optind]);
677
 
          nextchar += strlen (nextchar);
678
 
          optind++;
679
 
          optopt = 0;
680
 
          return '?';
681
 
        }
 
668
        {
 
669
          if (opterr)
 
670
            fprintf (stderr, _("%s: option `%s' is ambiguous\n"),
 
671
                     argv[0], argv[optind]);
 
672
          nextchar += strlen (nextchar);
 
673
          optind++;
 
674
          optopt = 0;
 
675
          return '?';
 
676
        }
682
677
 
683
678
      if (pfound != NULL)
684
 
        {
685
 
          option_index = indfound;
686
 
          optind++;
687
 
          if (*nameend)
688
 
            {
689
 
              /* Don't test has_arg with >, because some C compilers don't
690
 
                 allow it to be used on enums.  */
691
 
              if (pfound->has_arg)
692
 
                optarg = nameend + 1;
693
 
              else
694
 
                {
695
 
                  if (opterr)
696
 
                    {
697
 
                      if (argv[optind - 1][1] == '-')
698
 
                        /* --option */
699
 
                        fprintf (stderr,
700
 
                                 _("%s: option `--%s' doesn't allow an argument\n"),
701
 
                                 argv[0], pfound->name);
702
 
                      else
703
 
                        /* +option or -option */
704
 
                        fprintf (stderr,
705
 
                                 _("%s: option `%c%s' doesn't allow an argument\n"),
706
 
                                 argv[0], argv[optind - 1][0], pfound->name);
707
 
                    }
708
 
 
709
 
                  nextchar += strlen (nextchar);
710
 
 
711
 
                  optopt = pfound->val;
712
 
                  return '?';
713
 
                }
714
 
            }
715
 
          else if (pfound->has_arg == 1)
716
 
            {
717
 
              if (optind < argc)
718
 
                optarg = argv[optind++];
719
 
              else
720
 
                {
721
 
                  if (opterr)
722
 
                    fprintf (stderr,
723
 
                           _("%s: option `%s' requires an argument\n"),
724
 
                           argv[0], argv[optind - 1]);
725
 
                  nextchar += strlen (nextchar);
726
 
                  optopt = pfound->val;
727
 
                  return optstring[0] == ':' ? ':' : '?';
728
 
                }
729
 
            }
730
 
          nextchar += strlen (nextchar);
731
 
          if (longind != NULL)
732
 
            *longind = option_index;
733
 
          if (pfound->flag)
734
 
            {
735
 
              *(pfound->flag) = pfound->val;
736
 
              return 0;
737
 
            }
738
 
          return pfound->val;
739
 
        }
 
679
        {
 
680
          option_index = indfound;
 
681
          optind++;
 
682
          if (*nameend)
 
683
            {
 
684
              /* Don't test has_arg with >, because some C compilers don't
 
685
                 allow it to be used on enums.  */
 
686
              if (pfound->has_arg)
 
687
                optarg = nameend + 1;
 
688
              else
 
689
                {
 
690
                  if (opterr)
 
691
                    {
 
692
                      if (argv[optind - 1][1] == '-')
 
693
                        /* --option */
 
694
                        fprintf (stderr,
 
695
                                 _("%s: option `--%s' doesn't allow an argument\n"),
 
696
                                 argv[0], pfound->name);
 
697
                      else
 
698
                        /* +option or -option */
 
699
                        fprintf (stderr,
 
700
                                 _("%s: option `%c%s' doesn't allow an argument\n"),
 
701
                                 argv[0], argv[optind - 1][0], pfound->name);
 
702
                    }
 
703
 
 
704
                  nextchar += strlen (nextchar);
 
705
 
 
706
                  optopt = pfound->val;
 
707
                  return '?';
 
708
                }
 
709
            }
 
710
          else if (pfound->has_arg == 1)
 
711
            {
 
712
              if (optind < argc)
 
713
                optarg = argv[optind++];
 
714
              else
 
715
                {
 
716
                  if (opterr)
 
717
                    fprintf (stderr,
 
718
                           _("%s: option `%s' requires an argument\n"),
 
719
                           argv[0], argv[optind - 1]);
 
720
                  nextchar += strlen (nextchar);
 
721
                  optopt = pfound->val;
 
722
                  return optstring[0] == ':' ? ':' : '?';
 
723
                }
 
724
            }
 
725
          nextchar += strlen (nextchar);
 
726
          if (longind != NULL)
 
727
            *longind = option_index;
 
728
          if (pfound->flag)
 
729
            {
 
730
              *(pfound->flag) = pfound->val;
 
731
              return 0;
 
732
            }
 
733
          return pfound->val;
 
734
        }
740
735
 
741
736
      /* Can't find it as a long option.  If this is not getopt_long_only,
742
 
         or the option starts with '--' or is not a valid short
743
 
         option, then it's an error.
744
 
         Otherwise interpret it as a short option.  */
 
737
         or the option starts with '--' or is not a valid short
 
738
         option, then it's an error.
 
739
         Otherwise interpret it as a short option.  */
745
740
      if (!long_only || argv[optind][1] == '-'
746
 
          || my_index (optstring, *nextchar) == NULL)
747
 
        {
748
 
          if (opterr)
749
 
            {
750
 
              if (argv[optind][1] == '-')
751
 
                /* --option */
752
 
                fprintf (stderr, _("%s: unrecognized option `--%s'\n"),
753
 
                         argv[0], nextchar);
754
 
              else
755
 
                /* +option or -option */
756
 
                fprintf (stderr, _("%s: unrecognized option `%c%s'\n"),
757
 
                         argv[0], argv[optind][0], nextchar);
758
 
            }
759
 
          nextchar = (char *) "";
760
 
          optind++;
761
 
          optopt = 0;
762
 
          return '?';
763
 
        }
 
741
          || my_index (optstring, *nextchar) == NULL)
 
742
        {
 
743
          if (opterr)
 
744
            {
 
745
              if (argv[optind][1] == '-')
 
746
                /* --option */
 
747
                fprintf (stderr, _("%s: unrecognized option `--%s'\n"),
 
748
                         argv[0], nextchar);
 
749
              else
 
750
                /* +option or -option */
 
751
                fprintf (stderr, _("%s: unrecognized option `%c%s'\n"),
 
752
                         argv[0], argv[optind][0], nextchar);
 
753
            }
 
754
          nextchar = (char *) "";
 
755
          optind++;
 
756
          optopt = 0;
 
757
          return '?';
 
758
        }
764
759
    }
765
760
 
766
761
  /* Look at and handle the next short option-character.  */
775
770
 
776
771
    if (temp == NULL || c == ':')
777
772
      {
778
 
        if (opterr)
779
 
          {
780
 
            if (posixly_correct)
781
 
              /* 1003.2 specifies the format of this message.  */
782
 
              fprintf (stderr, _("%s: illegal option -- %c\n"),
783
 
                       argv[0], c);
784
 
            else
785
 
              fprintf (stderr, _("%s: invalid option -- %c\n"),
786
 
                       argv[0], c);
787
 
          }
788
 
        optopt = c;
789
 
        return '?';
 
773
        if (opterr)
 
774
          {
 
775
            if (posixly_correct)
 
776
              /* 1003.2 specifies the format of this message.  */
 
777
              fprintf (stderr, _("%s: illegal option -- %c\n"),
 
778
                       argv[0], c);
 
779
            else
 
780
              fprintf (stderr, _("%s: invalid option -- %c\n"),
 
781
                       argv[0], c);
 
782
          }
 
783
        optopt = c;
 
784
        return '?';
790
785
      }
791
786
    /* Convenience. Treat POSIX -W foo same as long option --foo */
792
787
    if (temp[0] == 'W' && temp[1] == ';')
793
788
      {
794
 
        char *nameend;
795
 
        const struct option *p;
796
 
        const struct option *pfound = NULL;
797
 
        int exact = 0;
798
 
        int ambig = 0;
799
 
        int indfound = 0;
800
 
        int option_index;
801
 
 
802
 
        /* This is an option that requires an argument.  */
803
 
        if (*nextchar != '\0')
804
 
          {
805
 
            optarg = nextchar;
806
 
            /* If we end this ARGV-element by taking the rest as an arg,
807
 
               we must advance to the next element now.  */
808
 
            optind++;
809
 
          }
810
 
        else if (optind == argc)
811
 
          {
812
 
            if (opterr)
813
 
              {
814
 
                /* 1003.2 specifies the format of this message.  */
815
 
                fprintf (stderr, _("%s: option requires an argument -- %c\n"),
816
 
                         argv[0], c);
817
 
              }
818
 
            optopt = c;
819
 
            if (optstring[0] == ':')
820
 
              c = ':';
821
 
            else
822
 
              c = '?';
823
 
            return c;
824
 
          }
825
 
        else
826
 
          /* We already incremented `optind' once;
827
 
             increment it again when taking next ARGV-elt as argument.  */
828
 
          optarg = argv[optind++];
829
 
 
830
 
        /* optarg is now the argument, see if it's in the
831
 
           table of longopts.  */
832
 
 
833
 
        for (nextchar = nameend = optarg; *nameend && *nameend != '='; nameend++)
834
 
          /* Do nothing.  */ ;
835
 
 
836
 
        /* Test all long options for either exact match
837
 
           or abbreviated matches.  */
838
 
        for (p = longopts, option_index = 0; p->name; p++, option_index++)
839
 
          if (!strncmp (p->name, nextchar, nameend - nextchar))
840
 
            {
841
 
              if ((unsigned int) (nameend - nextchar) == strlen (p->name))
842
 
                {
843
 
                  /* Exact match found.  */
844
 
                  pfound = p;
845
 
                  indfound = option_index;
846
 
                  exact = 1;
847
 
                  break;
848
 
                }
849
 
              else if (pfound == NULL)
850
 
                {
851
 
                  /* First nonexact match found.  */
852
 
                  pfound = p;
853
 
                  indfound = option_index;
854
 
                }
855
 
              else
856
 
                /* Second or later nonexact match found.  */
857
 
                ambig = 1;
858
 
            }
859
 
        if (ambig && !exact)
860
 
          {
861
 
            if (opterr)
862
 
              fprintf (stderr, _("%s: option `-W %s' is ambiguous\n"),
863
 
                       argv[0], argv[optind]);
864
 
            nextchar += strlen (nextchar);
865
 
            optind++;
866
 
            return '?';
867
 
          }
868
 
        if (pfound != NULL)
869
 
          {
870
 
            option_index = indfound;
871
 
            if (*nameend)
872
 
              {
873
 
                /* Don't test has_arg with >, because some C compilers don't
874
 
                   allow it to be used on enums.  */
875
 
                if (pfound->has_arg)
876
 
                  optarg = nameend + 1;
877
 
                else
878
 
                  {
879
 
                    if (opterr)
880
 
                      fprintf (stderr, _("\
 
789
        char *nameend;
 
790
        const struct option *p;
 
791
        const struct option *pfound = NULL;
 
792
        int exact = 0;
 
793
        int ambig = 0;
 
794
        int indfound = 0;
 
795
        int option_index;
 
796
 
 
797
        /* This is an option that requires an argument.  */
 
798
        if (*nextchar != '\0')
 
799
          {
 
800
            optarg = nextchar;
 
801
            /* If we end this ARGV-element by taking the rest as an arg,
 
802
               we must advance to the next element now.  */
 
803
            optind++;
 
804
          }
 
805
        else if (optind == argc)
 
806
          {
 
807
            if (opterr)
 
808
              {
 
809
                /* 1003.2 specifies the format of this message.  */
 
810
                fprintf (stderr, _("%s: option requires an argument -- %c\n"),
 
811
                         argv[0], c);
 
812
              }
 
813
            optopt = c;
 
814
            if (optstring[0] == ':')
 
815
              c = ':';
 
816
            else
 
817
              c = '?';
 
818
            return c;
 
819
          }
 
820
        else
 
821
          /* We already incremented `optind' once;
 
822
             increment it again when taking next ARGV-elt as argument.  */
 
823
          optarg = argv[optind++];
 
824
 
 
825
        /* optarg is now the argument, see if it's in the
 
826
           table of longopts.  */
 
827
 
 
828
        for (nextchar = nameend = optarg; *nameend && *nameend != '='; nameend++)
 
829
          /* Do nothing.  */ ;
 
830
 
 
831
        /* Test all long options for either exact match
 
832
           or abbreviated matches.  */
 
833
        for (p = longopts, option_index = 0; p->name; p++, option_index++)
 
834
          if (!strncmp (p->name, nextchar, nameend - nextchar))
 
835
            {
 
836
              if ((unsigned int) (nameend - nextchar) == strlen (p->name))
 
837
                {
 
838
                  /* Exact match found.  */
 
839
                  pfound = p;
 
840
                  indfound = option_index;
 
841
                  exact = 1;
 
842
                  break;
 
843
                }
 
844
              else if (pfound == NULL)
 
845
                {
 
846
                  /* First nonexact match found.  */
 
847
                  pfound = p;
 
848
                  indfound = option_index;
 
849
                }
 
850
              else
 
851
                /* Second or later nonexact match found.  */
 
852
                ambig = 1;
 
853
            }
 
854
        if (ambig && !exact)
 
855
          {
 
856
            if (opterr)
 
857
              fprintf (stderr, _("%s: option `-W %s' is ambiguous\n"),
 
858
                       argv[0], argv[optind]);
 
859
            nextchar += strlen (nextchar);
 
860
            optind++;
 
861
            return '?';
 
862
          }
 
863
        if (pfound != NULL)
 
864
          {
 
865
            option_index = indfound;
 
866
            if (*nameend)
 
867
              {
 
868
                /* Don't test has_arg with >, because some C compilers don't
 
869
                   allow it to be used on enums.  */
 
870
                if (pfound->has_arg)
 
871
                  optarg = nameend + 1;
 
872
                else
 
873
                  {
 
874
                    if (opterr)
 
875
                      fprintf (stderr, _("\
881
876
%s: option `-W %s' doesn't allow an argument\n"),
882
 
                               argv[0], pfound->name);
 
877
                               argv[0], pfound->name);
883
878
 
884
 
                    nextchar += strlen (nextchar);
885
 
                    return '?';
886
 
                  }
887
 
              }
888
 
            else if (pfound->has_arg == 1)
889
 
              {
890
 
                if (optind < argc)
891
 
                  optarg = argv[optind++];
892
 
                else
893
 
                  {
894
 
                    if (opterr)
895
 
                      fprintf (stderr,
896
 
                               _("%s: option `%s' requires an argument\n"),
897
 
                               argv[0], argv[optind - 1]);
898
 
                    nextchar += strlen (nextchar);
899
 
                    return optstring[0] == ':' ? ':' : '?';
900
 
                  }
901
 
              }
902
 
            nextchar += strlen (nextchar);
903
 
            if (longind != NULL)
904
 
              *longind = option_index;
905
 
            if (pfound->flag)
906
 
              {
907
 
                *(pfound->flag) = pfound->val;
908
 
                return 0;
909
 
              }
910
 
            return pfound->val;
911
 
          }
912
 
          nextchar = NULL;
913
 
          return 'W';   /* Let the application handle it.   */
 
879
                    nextchar += strlen (nextchar);
 
880
                    return '?';
 
881
                  }
 
882
              }
 
883
            else if (pfound->has_arg == 1)
 
884
              {
 
885
                if (optind < argc)
 
886
                  optarg = argv[optind++];
 
887
                else
 
888
                  {
 
889
                    if (opterr)
 
890
                      fprintf (stderr,
 
891
                               _("%s: option `%s' requires an argument\n"),
 
892
                               argv[0], argv[optind - 1]);
 
893
                    nextchar += strlen (nextchar);
 
894
                    return optstring[0] == ':' ? ':' : '?';
 
895
                  }
 
896
              }
 
897
            nextchar += strlen (nextchar);
 
898
            if (longind != NULL)
 
899
              *longind = option_index;
 
900
            if (pfound->flag)
 
901
              {
 
902
                *(pfound->flag) = pfound->val;
 
903
                return 0;
 
904
              }
 
905
            return pfound->val;
 
906
          }
 
907
          nextchar = NULL;
 
908
          return 'W';   /* Let the application handle it.   */
914
909
      }
915
910
    if (temp[1] == ':')
916
911
      {
917
 
        if (temp[2] == ':')
918
 
          {
919
 
            /* This is an option that accepts an argument optionally.  */
920
 
            if (*nextchar != '\0')
921
 
              {
922
 
                optarg = nextchar;
923
 
                optind++;
924
 
              }
925
 
            else
926
 
              optarg = NULL;
927
 
            nextchar = NULL;
928
 
          }
929
 
        else
930
 
          {
931
 
            /* This is an option that requires an argument.  */
932
 
            if (*nextchar != '\0')
933
 
              {
934
 
                optarg = nextchar;
935
 
                /* If we end this ARGV-element by taking the rest as an arg,
936
 
                   we must advance to the next element now.  */
937
 
                optind++;
938
 
              }
939
 
            else if (optind == argc)
940
 
              {
941
 
                if (opterr)
942
 
                  {
943
 
                    /* 1003.2 specifies the format of this message.  */
944
 
                    fprintf (stderr,
945
 
                           _("%s: option requires an argument -- %c\n"),
946
 
                           argv[0], c);
947
 
                  }
948
 
                optopt = c;
949
 
                if (optstring[0] == ':')
950
 
                  c = ':';
951
 
                else
952
 
                  c = '?';
953
 
              }
954
 
            else
955
 
              /* We already incremented `optind' once;
956
 
                 increment it again when taking next ARGV-elt as argument.  */
957
 
              optarg = argv[optind++];
958
 
            nextchar = NULL;
959
 
          }
 
912
        if (temp[2] == ':')
 
913
          {
 
914
            /* This is an option that accepts an argument optionally.  */
 
915
            if (*nextchar != '\0')
 
916
              {
 
917
                optarg = nextchar;
 
918
                optind++;
 
919
              }
 
920
            else
 
921
              optarg = NULL;
 
922
            nextchar = NULL;
 
923
          }
 
924
        else
 
925
          {
 
926
            /* This is an option that requires an argument.  */
 
927
            if (*nextchar != '\0')
 
928
              {
 
929
                optarg = nextchar;
 
930
                /* If we end this ARGV-element by taking the rest as an arg,
 
931
                   we must advance to the next element now.  */
 
932
                optind++;
 
933
              }
 
934
            else if (optind == argc)
 
935
              {
 
936
                if (opterr)
 
937
                  {
 
938
                    /* 1003.2 specifies the format of this message.  */
 
939
                    fprintf (stderr,
 
940
                           _("%s: option requires an argument -- %c\n"),
 
941
                           argv[0], c);
 
942
                  }
 
943
                optopt = c;
 
944
                if (optstring[0] == ':')
 
945
                  c = ':';
 
946
                else
 
947
                  c = '?';
 
948
              }
 
949
            else
 
950
              /* We already incremented `optind' once;
 
951
                 increment it again when taking next ARGV-elt as argument.  */
 
952
              optarg = argv[optind++];
 
953
            nextchar = NULL;
 
954
          }
960
955
      }
961
956
    return c;
962
957
  }
969
964
     const char *optstring;
970
965
{
971
966
  return _getopt_internal (argc, argv, optstring,
972
 
                           (const struct option *) 0,
973
 
                           (int *) 0,
974
 
                           0);
 
967
                           (const struct option *) 0,
 
968
                           (int *) 0,
 
969
                           0);
975
970
}
976
971
 
977
972
int
1001
996
  return _getopt_internal (argc, argv, options, long_options, opt_index, 1);
1002
997
}
1003
998
 
1004
 
#endif  /* Not ELIDE_CODE.  */
 
999
#endif  /* Not ELIDE_CODE.  */
1005
1000
 
1006
1001
#ifdef TEST
1007
1002
 
1022
1017
 
1023
1018
      c = getopt (argc, argv, "abc:d:0123456789");
1024
1019
      if (c == -1)
1025
 
        break;
 
1020
        break;
1026
1021
 
1027
1022
      switch (c)
1028
 
        {
1029
 
        case '0':
1030
 
        case '1':
1031
 
        case '2':
1032
 
        case '3':
1033
 
        case '4':
1034
 
        case '5':
1035
 
        case '6':
1036
 
        case '7':
1037
 
        case '8':
1038
 
        case '9':
1039
 
          if (digit_optind != 0 && digit_optind != this_option_optind)
1040
 
            printf ("digits occur in two different argv-elements.\n");
1041
 
          digit_optind = this_option_optind;
1042
 
          printf ("option %c\n", c);
1043
 
          break;
1044
 
 
1045
 
        case 'a':
1046
 
          printf ("option a\n");
1047
 
          break;
1048
 
 
1049
 
        case 'b':
1050
 
          printf ("option b\n");
1051
 
          break;
1052
 
 
1053
 
        case 'c':
1054
 
          printf ("option c with value `%s'\n", optarg);
1055
 
          break;
1056
 
 
1057
 
        case '?':
1058
 
          break;
1059
 
 
1060
 
        default:
1061
 
          printf ("?? getopt returned character code 0%o ??\n", c);
1062
 
        }
 
1023
        {
 
1024
        case '0':
 
1025
        case '1':
 
1026
        case '2':
 
1027
        case '3':
 
1028
        case '4':
 
1029
        case '5':
 
1030
        case '6':
 
1031
        case '7':
 
1032
        case '8':
 
1033
        case '9':
 
1034
          if (digit_optind != 0 && digit_optind != this_option_optind)
 
1035
            printf ("digits occur in two different argv-elements.\n");
 
1036
          digit_optind = this_option_optind;
 
1037
          printf ("option %c\n", c);
 
1038
          break;
 
1039
 
 
1040
        case 'a':
 
1041
          printf ("option a\n");
 
1042
          break;
 
1043
 
 
1044
        case 'b':
 
1045
          printf ("option b\n");
 
1046
          break;
 
1047
 
 
1048
        case 'c':
 
1049
          printf ("option c with value `%s'\n", optarg);
 
1050
          break;
 
1051
 
 
1052
        case '?':
 
1053
          break;
 
1054
 
 
1055
        default:
 
1056
          printf ("?? getopt returned character code 0%o ??\n", c);
 
1057
        }
1063
1058
    }
1064
1059
 
1065
1060
  if (optind < argc)
1066
1061
    {
1067
1062
      printf ("non-option ARGV-elements: ");
1068
1063
      while (optind < argc)
1069
 
        printf ("%s ", argv[optind++]);
 
1064
        printf ("%s ", argv[optind++]);
1070
1065
      printf ("\n");
1071
1066
    }
1072
1067