~ubuntu-branches/ubuntu/utopic/gettext/utopic

« back to all changes in this revision

Viewing changes to gettext-tools/gnulib-lib/getopt.c

  • Committer: Colin Watson
  • Date: 2010-08-01 21:36:08 UTC
  • mfrom: (2.1.10 sid)
  • Revision ID: cjwatson@canonical.com-20100801213608-yy7vkm8lpatep3ci
merge from Debian 0.18.1.1-1

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
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 part of the C library, so if you don't know what
3
3
   "Keep this file name-space clean" means, talk to drepper@gnu.org
4
4
   before changing it!
5
 
   Copyright (C) 1987,88,89,90,91,92,93,94,95,96,98,99,2000,2001,2002,2003,2004,2006
6
 
        Free Software Foundation, Inc.
 
5
   Copyright (C) 1987-1996, 1998-2004, 2006, 2008-2010 Free Software
 
6
   Foundation, Inc.
7
7
   This file is part of the GNU C Library.
8
8
 
9
9
   This program is free software: you can redistribute it and/or modify
30
30
#include <string.h>
31
31
#include <unistd.h>
32
32
 
33
 
#ifdef __VMS
34
 
# include <unixlib.h>
35
 
#endif
36
 
 
37
33
#ifdef _LIBC
38
34
# include <libintl.h>
39
35
#else
45
41
# include <wchar.h>
46
42
#endif
47
43
 
48
 
#ifndef attribute_hidden
49
 
# define attribute_hidden
50
 
#endif
51
 
 
52
 
/* Unlike standard Unix `getopt', functions like `getopt_long'
53
 
   let the user intersperse the options with the other arguments.
 
44
/* This version of `getopt' appears to the caller like standard Unix `getopt'
 
45
   but it behaves differently for the user, since it allows the user
 
46
   to intersperse the options with the other arguments.
54
47
 
55
48
   As `getopt_long' works, it permutes the elements of ARGV so that,
56
49
   when it is done, all the options precede everything else.  Thus
58
51
 
59
52
   Using `getopt' or setting the environment variable POSIXLY_CORRECT
60
53
   disables permutation.
61
 
   Then the application's behavior is completely standard.
 
54
   Then the behavior is completely standard.
62
55
 
63
56
   GNU application programs can use a third alternative mode in which
64
57
   they can distinguish the relative order of options and other arguments.  */
125
118
 
126
119
# ifdef USE_NONOPTION_FLAGS
127
120
#  define SWAP_FLAGS(ch1, ch2) \
128
 
  if (d->__nonoption_flags_len > 0)                                           \
129
 
    {                                                                         \
130
 
      char __tmp = __getopt_nonoption_flags[ch1];                             \
131
 
      __getopt_nonoption_flags[ch1] = __getopt_nonoption_flags[ch2];          \
132
 
      __getopt_nonoption_flags[ch2] = __tmp;                                  \
 
121
  if (d->__nonoption_flags_len > 0)                                           \
 
122
    {                                                                         \
 
123
      char __tmp = __getopt_nonoption_flags[ch1];                             \
 
124
      __getopt_nonoption_flags[ch1] = __getopt_nonoption_flags[ch2];          \
 
125
      __getopt_nonoption_flags[ch2] = __tmp;                                  \
133
126
    }
134
127
# else
135
128
#  define SWAP_FLAGS(ch1, ch2)
136
129
# endif
137
 
#else   /* !_LIBC */
 
130
#else   /* !_LIBC */
138
131
# define SWAP_FLAGS(ch1, ch2)
139
 
#endif  /* _LIBC */
 
132
#endif  /* _LIBC */
140
133
 
141
134
/* Exchange two adjacent subsequences of ARGV.
142
135
   One subsequence is elements [first_nonopt,last_nonopt)
167
160
  if (d->__nonoption_flags_len > 0 && top >= d->__nonoption_flags_max_len)
168
161
    {
169
162
      /* We must extend the array.  The user plays games with us and
170
 
         presents new arguments.  */
 
163
         presents new arguments.  */
171
164
      char *new_str = malloc (top + 1);
172
165
      if (new_str == NULL)
173
 
        d->__nonoption_flags_len = d->__nonoption_flags_max_len = 0;
 
166
        d->__nonoption_flags_len = d->__nonoption_flags_max_len = 0;
174
167
      else
175
 
        {
176
 
          memset (__mempcpy (new_str, __getopt_nonoption_flags,
177
 
                             d->__nonoption_flags_max_len),
178
 
                  '\0', top + 1 - d->__nonoption_flags_max_len);
179
 
          d->__nonoption_flags_max_len = top + 1;
180
 
          __getopt_nonoption_flags = new_str;
181
 
        }
 
168
        {
 
169
          memset (__mempcpy (new_str, __getopt_nonoption_flags,
 
170
                             d->__nonoption_flags_max_len),
 
171
                  '\0', top + 1 - d->__nonoption_flags_max_len);
 
172
          d->__nonoption_flags_max_len = top + 1;
 
173
          __getopt_nonoption_flags = new_str;
 
174
        }
182
175
    }
183
176
#endif
184
177
 
185
178
  while (top > middle && middle > bottom)
186
179
    {
187
180
      if (top - middle > middle - bottom)
188
 
        {
189
 
          /* Bottom segment is the short one.  */
190
 
          int len = middle - bottom;
191
 
          register int i;
 
181
        {
 
182
          /* Bottom segment is the short one.  */
 
183
          int len = middle - bottom;
 
184
          register int i;
192
185
 
193
 
          /* Swap it with the top part of the top segment.  */
194
 
          for (i = 0; i < len; i++)
195
 
            {
196
 
              tem = argv[bottom + i];
197
 
              argv[bottom + i] = argv[top - (middle - bottom) + i];
198
 
              argv[top - (middle - bottom) + i] = tem;
199
 
              SWAP_FLAGS (bottom + i, top - (middle - bottom) + i);
200
 
            }
201
 
          /* Exclude the moved bottom segment from further swapping.  */
202
 
          top -= len;
203
 
        }
 
186
          /* Swap it with the top part of the top segment.  */
 
187
          for (i = 0; i < len; i++)
 
188
            {
 
189
              tem = argv[bottom + i];
 
190
              argv[bottom + i] = argv[top - (middle - bottom) + i];
 
191
              argv[top - (middle - bottom) + i] = tem;
 
192
              SWAP_FLAGS (bottom + i, top - (middle - bottom) + i);
 
193
            }
 
194
          /* Exclude the moved bottom segment from further swapping.  */
 
195
          top -= len;
 
196
        }
204
197
      else
205
 
        {
206
 
          /* Top segment is the short one.  */
207
 
          int len = top - middle;
208
 
          register int i;
 
198
        {
 
199
          /* Top segment is the short one.  */
 
200
          int len = top - middle;
 
201
          register int i;
209
202
 
210
 
          /* Swap it with the bottom part of the bottom segment.  */
211
 
          for (i = 0; i < len; i++)
212
 
            {
213
 
              tem = argv[bottom + i];
214
 
              argv[bottom + i] = argv[middle + i];
215
 
              argv[middle + i] = tem;
216
 
              SWAP_FLAGS (bottom + i, middle + i);
217
 
            }
218
 
          /* Exclude the moved top segment from further swapping.  */
219
 
          bottom += len;
220
 
        }
 
203
          /* Swap it with the bottom part of the bottom segment.  */
 
204
          for (i = 0; i < len; i++)
 
205
            {
 
206
              tem = argv[bottom + i];
 
207
              argv[bottom + i] = argv[middle + i];
 
208
              argv[middle + i] = tem;
 
209
              SWAP_FLAGS (bottom + i, middle + i);
 
210
            }
 
211
          /* Exclude the moved top segment from further swapping.  */
 
212
          bottom += len;
 
213
        }
221
214
    }
222
215
 
223
216
  /* Update records for the slots the non-options now occupy.  */
229
222
/* Initialize the internal data when the first call is made.  */
230
223
 
231
224
static const char *
232
 
_getopt_initialize (int argc, char **argv, const char *optstring,
233
 
                    int posixly_correct, struct _getopt_data *d)
 
225
_getopt_initialize (int argc _GL_UNUSED,
 
226
                    char **argv _GL_UNUSED, const char *optstring,
 
227
                    struct _getopt_data *d, int posixly_correct)
234
228
{
235
229
  /* Start processing options with ARGV-element 1 (since ARGV-element 0
236
230
     is the program name); the sequence of previously skipped
264
258
      && argc == __libc_argc && argv == __libc_argv)
265
259
    {
266
260
      if (d->__nonoption_flags_max_len == 0)
267
 
        {
268
 
          if (__getopt_nonoption_flags == NULL
269
 
              || __getopt_nonoption_flags[0] == '\0')
270
 
            d->__nonoption_flags_max_len = -1;
271
 
          else
272
 
            {
273
 
              const char *orig_str = __getopt_nonoption_flags;
274
 
              int len = d->__nonoption_flags_max_len = strlen (orig_str);
275
 
              if (d->__nonoption_flags_max_len < argc)
276
 
                d->__nonoption_flags_max_len = argc;
277
 
              __getopt_nonoption_flags =
278
 
                (char *) malloc (d->__nonoption_flags_max_len);
279
 
              if (__getopt_nonoption_flags == NULL)
280
 
                d->__nonoption_flags_max_len = -1;
281
 
              else
282
 
                memset (__mempcpy (__getopt_nonoption_flags, orig_str, len),
283
 
                        '\0', d->__nonoption_flags_max_len - len);
284
 
            }
285
 
        }
 
261
        {
 
262
          if (__getopt_nonoption_flags == NULL
 
263
              || __getopt_nonoption_flags[0] == '\0')
 
264
            d->__nonoption_flags_max_len = -1;
 
265
          else
 
266
            {
 
267
              const char *orig_str = __getopt_nonoption_flags;
 
268
              int len = d->__nonoption_flags_max_len = strlen (orig_str);
 
269
              if (d->__nonoption_flags_max_len < argc)
 
270
                d->__nonoption_flags_max_len = argc;
 
271
              __getopt_nonoption_flags =
 
272
                (char *) malloc (d->__nonoption_flags_max_len);
 
273
              if (__getopt_nonoption_flags == NULL)
 
274
                d->__nonoption_flags_max_len = -1;
 
275
              else
 
276
                memset (__mempcpy (__getopt_nonoption_flags, orig_str, len),
 
277
                        '\0', d->__nonoption_flags_max_len - len);
 
278
            }
 
279
        }
286
280
      d->__nonoption_flags_len = d->__nonoption_flags_max_len;
287
281
    }
288
282
  else
334
328
   `flag' field is nonzero, the value of the option's `val' field
335
329
   if the `flag' field is zero.
336
330
 
 
331
   The elements of ARGV aren't really const, because we permute them.
 
332
   But we pretend they're const in the prototype to be compatible
 
333
   with other systems.
 
334
 
337
335
   LONGOPTS is a vector of `struct option' terminated by an
338
336
   element containing a name which is zero.
339
337
 
342
340
   recent call.
343
341
 
344
342
   If LONG_ONLY is nonzero, '-' as well as '--' can introduce
345
 
   long-named options.
346
 
 
347
 
   If POSIXLY_CORRECT is nonzero, behave as if the POSIXLY_CORRECT
348
 
   environment variable were set.  */
 
343
   long-named options.  */
349
344
 
350
345
int
351
346
_getopt_internal_r (int argc, char **argv, const char *optstring,
352
 
                    const struct option *longopts, int *longind,
353
 
                    int long_only, int posixly_correct, struct _getopt_data *d)
 
347
                    const struct option *longopts, int *longind,
 
348
                    int long_only, struct _getopt_data *d, int posixly_correct)
354
349
{
355
350
  int print_errors = d->opterr;
356
 
  if (optstring[0] == ':')
357
 
    print_errors = 0;
358
351
 
359
352
  if (argc < 1)
360
353
    return -1;
364
357
  if (d->optind == 0 || !d->__initialized)
365
358
    {
366
359
      if (d->optind == 0)
367
 
        d->optind = 1;  /* Don't scan ARGV[0], the program name.  */
368
 
      optstring = _getopt_initialize (argc, argv, optstring,
369
 
                                      posixly_correct, d);
 
360
        d->optind = 1;  /* Don't scan ARGV[0], the program name.  */
 
361
      optstring = _getopt_initialize (argc, argv, optstring, d,
 
362
                                      posixly_correct);
370
363
      d->__initialized = 1;
371
364
    }
 
365
  else if (optstring[0] == '-' || optstring[0] == '+')
 
366
    optstring++;
 
367
  if (optstring[0] == ':')
 
368
    print_errors = 0;
372
369
 
373
370
  /* Test whether ARGV[optind] points to a non-option argument.
374
371
     Either it does not have option syntax, or there is an environment flag
376
373
     is only used when the used in the GNU libc.  */
377
374
#if defined _LIBC && defined USE_NONOPTION_FLAGS
378
375
# define NONOPTION_P (argv[d->optind][0] != '-' || argv[d->optind][1] == '\0' \
379
 
                      || (d->optind < d->__nonoption_flags_len                \
380
 
                          && __getopt_nonoption_flags[d->optind] == '1'))
 
376
                      || (d->optind < d->__nonoption_flags_len                \
 
377
                          && __getopt_nonoption_flags[d->optind] == '1'))
381
378
#else
382
379
# define NONOPTION_P (argv[d->optind][0] != '-' || argv[d->optind][1] == '\0')
383
380
#endif
387
384
      /* Advance to the next ARGV-element.  */
388
385
 
389
386
      /* Give FIRST_NONOPT & LAST_NONOPT rational values if OPTIND has been
390
 
         moved back by the user (who may also have changed the arguments).  */
 
387
         moved back by the user (who may also have changed the arguments).  */
391
388
      if (d->__last_nonopt > d->optind)
392
 
        d->__last_nonopt = d->optind;
 
389
        d->__last_nonopt = d->optind;
393
390
      if (d->__first_nonopt > d->optind)
394
 
        d->__first_nonopt = d->optind;
 
391
        d->__first_nonopt = d->optind;
395
392
 
396
393
      if (d->__ordering == PERMUTE)
397
 
        {
398
 
          /* If we have just processed some options following some non-options,
399
 
             exchange them so that the options come first.  */
400
 
 
401
 
          if (d->__first_nonopt != d->__last_nonopt
402
 
              && d->__last_nonopt != d->optind)
403
 
            exchange ((char **) argv, d);
404
 
          else if (d->__last_nonopt != d->optind)
405
 
            d->__first_nonopt = d->optind;
406
 
 
407
 
          /* Skip any additional non-options
408
 
             and extend the range of non-options previously skipped.  */
409
 
 
410
 
          while (d->optind < argc && NONOPTION_P)
411
 
            d->optind++;
412
 
          d->__last_nonopt = d->optind;
413
 
        }
 
394
        {
 
395
          /* If we have just processed some options following some non-options,
 
396
             exchange them so that the options come first.  */
 
397
 
 
398
          if (d->__first_nonopt != d->__last_nonopt
 
399
              && d->__last_nonopt != d->optind)
 
400
            exchange ((char **) argv, d);
 
401
          else if (d->__last_nonopt != d->optind)
 
402
            d->__first_nonopt = d->optind;
 
403
 
 
404
          /* Skip any additional non-options
 
405
             and extend the range of non-options previously skipped.  */
 
406
 
 
407
          while (d->optind < argc && NONOPTION_P)
 
408
            d->optind++;
 
409
          d->__last_nonopt = d->optind;
 
410
        }
414
411
 
415
412
      /* The special ARGV-element `--' means premature end of options.
416
 
         Skip it like a null option,
417
 
         then exchange with previous non-options as if it were an option,
418
 
         then skip everything else like a non-option.  */
 
413
         Skip it like a null option,
 
414
         then exchange with previous non-options as if it were an option,
 
415
         then skip everything else like a non-option.  */
419
416
 
420
417
      if (d->optind != argc && !strcmp (argv[d->optind], "--"))
421
 
        {
422
 
          d->optind++;
423
 
 
424
 
          if (d->__first_nonopt != d->__last_nonopt
425
 
              && d->__last_nonopt != d->optind)
426
 
            exchange ((char **) argv, d);
427
 
          else if (d->__first_nonopt == d->__last_nonopt)
428
 
            d->__first_nonopt = d->optind;
429
 
          d->__last_nonopt = argc;
430
 
 
431
 
          d->optind = argc;
432
 
        }
 
418
        {
 
419
          d->optind++;
 
420
 
 
421
          if (d->__first_nonopt != d->__last_nonopt
 
422
              && d->__last_nonopt != d->optind)
 
423
            exchange ((char **) argv, d);
 
424
          else if (d->__first_nonopt == d->__last_nonopt)
 
425
            d->__first_nonopt = d->optind;
 
426
          d->__last_nonopt = argc;
 
427
 
 
428
          d->optind = argc;
 
429
        }
433
430
 
434
431
      /* If we have done all the ARGV-elements, stop the scan
435
 
         and back over any non-options that we skipped and permuted.  */
 
432
         and back over any non-options that we skipped and permuted.  */
436
433
 
437
434
      if (d->optind == argc)
438
 
        {
439
 
          /* Set the next-arg-index to point at the non-options
440
 
             that we previously skipped, so the caller will digest them.  */
441
 
          if (d->__first_nonopt != d->__last_nonopt)
442
 
            d->optind = d->__first_nonopt;
443
 
          return -1;
444
 
        }
 
435
        {
 
436
          /* Set the next-arg-index to point at the non-options
 
437
             that we previously skipped, so the caller will digest them.  */
 
438
          if (d->__first_nonopt != d->__last_nonopt)
 
439
            d->optind = d->__first_nonopt;
 
440
          return -1;
 
441
        }
445
442
 
446
443
      /* If we have come to a non-option and did not permute it,
447
 
         either stop the scan or describe it to the caller and pass it by.  */
 
444
         either stop the scan or describe it to the caller and pass it by.  */
448
445
 
449
446
      if (NONOPTION_P)
450
 
        {
451
 
          if (d->__ordering == REQUIRE_ORDER)
452
 
            return -1;
453
 
          d->optarg = argv[d->optind++];
454
 
          return 1;
455
 
        }
 
447
        {
 
448
          if (d->__ordering == REQUIRE_ORDER)
 
449
            return -1;
 
450
          d->optarg = argv[d->optind++];
 
451
          return 1;
 
452
        }
456
453
 
457
454
      /* We have found another option-ARGV-element.
458
 
         Skip the initial punctuation.  */
 
455
         Skip the initial punctuation.  */
459
456
 
460
457
      d->__nextchar = (argv[d->optind] + 1
461
 
                  + (longopts != NULL && argv[d->optind][1] == '-'));
 
458
                  + (longopts != NULL && argv[d->optind][1] == '-'));
462
459
    }
463
460
 
464
461
  /* Decode the current option-ARGV-element.  */
478
475
 
479
476
  if (longopts != NULL
480
477
      && (argv[d->optind][1] == '-'
481
 
          || (long_only && (argv[d->optind][2]
482
 
                            || !strchr (optstring, argv[d->optind][1])))))
 
478
          || (long_only && (argv[d->optind][2]
 
479
                            || !strchr (optstring, argv[d->optind][1])))))
483
480
    {
484
481
      char *nameend;
485
482
      const struct option *p;
490
487
      int option_index;
491
488
 
492
489
      for (nameend = d->__nextchar; *nameend && *nameend != '='; nameend++)
493
 
        /* Do nothing.  */ ;
 
490
        /* Do nothing.  */ ;
494
491
 
495
492
      /* Test all long options for either exact match
496
 
         or abbreviated matches.  */
 
493
         or abbreviated matches.  */
497
494
      for (p = longopts, option_index = 0; p->name; p++, option_index++)
498
 
        if (!strncmp (p->name, d->__nextchar, nameend - d->__nextchar))
499
 
          {
500
 
            if ((unsigned int) (nameend - d->__nextchar)
501
 
                == (unsigned int) strlen (p->name))
502
 
              {
503
 
                /* Exact match found.  */
504
 
                pfound = p;
505
 
                indfound = option_index;
506
 
                exact = 1;
507
 
                break;
508
 
              }
509
 
            else if (pfound == NULL)
510
 
              {
511
 
                /* First nonexact match found.  */
512
 
                pfound = p;
513
 
                indfound = option_index;
514
 
              }
515
 
            else if (long_only
516
 
                     || pfound->has_arg != p->has_arg
517
 
                     || pfound->flag != p->flag
518
 
                     || pfound->val != p->val)
519
 
              /* Second or later nonexact match found.  */
520
 
              ambig = 1;
521
 
          }
 
495
        if (!strncmp (p->name, d->__nextchar, nameend - d->__nextchar))
 
496
          {
 
497
            if ((unsigned int) (nameend - d->__nextchar)
 
498
                == (unsigned int) strlen (p->name))
 
499
              {
 
500
                /* Exact match found.  */
 
501
                pfound = p;
 
502
                indfound = option_index;
 
503
                exact = 1;
 
504
                break;
 
505
              }
 
506
            else if (pfound == NULL)
 
507
              {
 
508
                /* First nonexact match found.  */
 
509
                pfound = p;
 
510
                indfound = option_index;
 
511
              }
 
512
            else if (long_only
 
513
                     || pfound->has_arg != p->has_arg
 
514
                     || pfound->flag != p->flag
 
515
                     || pfound->val != p->val)
 
516
              /* Second or later nonexact match found.  */
 
517
              ambig = 1;
 
518
          }
522
519
 
523
520
      if (ambig && !exact)
524
 
        {
525
 
          if (print_errors)
526
 
            {
 
521
        {
 
522
          if (print_errors)
 
523
            {
527
524
#if defined _LIBC && defined USE_IN_LIBIO
528
 
              char *buf;
529
 
 
530
 
              if (__asprintf (&buf, _("%s: option `%s' is ambiguous\n"),
531
 
                              argv[0], argv[d->optind]) >= 0)
532
 
                {
533
 
                  _IO_flockfile (stderr);
534
 
 
535
 
                  int old_flags2 = ((_IO_FILE *) stderr)->_flags2;
536
 
                  ((_IO_FILE *) stderr)->_flags2 |= _IO_FLAGS2_NOTCANCEL;
537
 
 
538
 
                  __fxprintf (NULL, "%s", buf);
539
 
 
540
 
                  ((_IO_FILE *) stderr)->_flags2 = old_flags2;
541
 
                  _IO_funlockfile (stderr);
542
 
 
543
 
                  free (buf);
544
 
                }
 
525
              char *buf;
 
526
 
 
527
              if (__asprintf (&buf, _("%s: option '%s' is ambiguous\n"),
 
528
                              argv[0], argv[d->optind]) >= 0)
 
529
                {
 
530
                  _IO_flockfile (stderr);
 
531
 
 
532
                  int old_flags2 = ((_IO_FILE *) stderr)->_flags2;
 
533
                  ((_IO_FILE *) stderr)->_flags2 |= _IO_FLAGS2_NOTCANCEL;
 
534
 
 
535
                  __fxprintf (NULL, "%s", buf);
 
536
 
 
537
                  ((_IO_FILE *) stderr)->_flags2 = old_flags2;
 
538
                  _IO_funlockfile (stderr);
 
539
 
 
540
                  free (buf);
 
541
                }
545
542
#else
546
 
              fprintf (stderr, _("%s: option `%s' is ambiguous\n"),
547
 
                       argv[0], argv[d->optind]);
 
543
              fprintf (stderr, _("%s: option '%s' is ambiguous\n"),
 
544
                       argv[0], argv[d->optind]);
548
545
#endif
549
 
            }
550
 
          d->__nextchar += strlen (d->__nextchar);
551
 
          d->optind++;
552
 
          d->optopt = 0;
553
 
          return '?';
554
 
        }
 
546
            }
 
547
          d->__nextchar += strlen (d->__nextchar);
 
548
          d->optind++;
 
549
          d->optopt = 0;
 
550
          return '?';
 
551
        }
555
552
 
556
553
      if (pfound != NULL)
557
 
        {
558
 
          option_index = indfound;
559
 
          d->optind++;
560
 
          if (*nameend)
561
 
            {
562
 
              /* Don't test has_arg with >, because some C compilers don't
563
 
                 allow it to be used on enums.  */
564
 
              if (pfound->has_arg)
565
 
                d->optarg = nameend + 1;
566
 
              else
567
 
                {
568
 
                  if (print_errors)
569
 
                    {
570
 
#if defined _LIBC && defined USE_IN_LIBIO
571
 
                      char *buf;
572
 
                      int n;
573
 
#endif
574
 
 
575
 
                      if (argv[d->optind - 1][1] == '-')
576
 
                        {
577
 
                          /* --option */
578
 
#if defined _LIBC && defined USE_IN_LIBIO
579
 
                          n = __asprintf (&buf, _("\
580
 
%s: option `--%s' doesn't allow an argument\n"),
581
 
                                          argv[0], pfound->name);
582
 
#else
583
 
                          fprintf (stderr, _("\
584
 
%s: option `--%s' doesn't allow an argument\n"),
585
 
                                   argv[0], pfound->name);
586
 
#endif
587
 
                        }
588
 
                      else
589
 
                        {
590
 
                          /* +option or -option */
591
 
#if defined _LIBC && defined USE_IN_LIBIO
592
 
                          n = __asprintf (&buf, _("\
593
 
%s: option `%c%s' doesn't allow an argument\n"),
594
 
                                          argv[0], argv[d->optind - 1][0],
595
 
                                          pfound->name);
596
 
#else
597
 
                          fprintf (stderr, _("\
598
 
%s: option `%c%s' doesn't allow an argument\n"),
599
 
                                   argv[0], argv[d->optind - 1][0],
600
 
                                   pfound->name);
601
 
#endif
602
 
                        }
603
 
 
604
 
#if defined _LIBC && defined USE_IN_LIBIO
605
 
                      if (n >= 0)
606
 
                        {
607
 
                          _IO_flockfile (stderr);
608
 
 
609
 
                          int old_flags2 = ((_IO_FILE *) stderr)->_flags2;
610
 
                          ((_IO_FILE *) stderr)->_flags2
611
 
                            |= _IO_FLAGS2_NOTCANCEL;
612
 
 
613
 
                          __fxprintf (NULL, "%s", buf);
614
 
 
615
 
                          ((_IO_FILE *) stderr)->_flags2 = old_flags2;
616
 
                          _IO_funlockfile (stderr);
617
 
 
618
 
                          free (buf);
619
 
                        }
620
 
#endif
621
 
                    }
622
 
 
623
 
                  d->__nextchar += strlen (d->__nextchar);
624
 
 
625
 
                  d->optopt = pfound->val;
626
 
                  return '?';
627
 
                }
628
 
            }
629
 
          else if (pfound->has_arg == 1)
630
 
            {
631
 
              if (d->optind < argc)
632
 
                d->optarg = argv[d->optind++];
633
 
              else
634
 
                {
635
 
                  if (print_errors)
636
 
                    {
637
 
#if defined _LIBC && defined USE_IN_LIBIO
638
 
                      char *buf;
639
 
 
640
 
                      if (__asprintf (&buf, _("\
641
 
%s: option `%s' requires an argument\n"),
642
 
                                      argv[0], argv[d->optind - 1]) >= 0)
643
 
                        {
644
 
                          _IO_flockfile (stderr);
645
 
 
646
 
                          int old_flags2 = ((_IO_FILE *) stderr)->_flags2;
647
 
                          ((_IO_FILE *) stderr)->_flags2
648
 
                            |= _IO_FLAGS2_NOTCANCEL;
649
 
 
650
 
                          __fxprintf (NULL, "%s", buf);
651
 
 
652
 
                          ((_IO_FILE *) stderr)->_flags2 = old_flags2;
653
 
                          _IO_funlockfile (stderr);
654
 
 
655
 
                          free (buf);
656
 
                        }
657
 
#else
658
 
                      fprintf (stderr,
659
 
                               _("%s: option `%s' requires an argument\n"),
660
 
                               argv[0], argv[d->optind - 1]);
661
 
#endif
662
 
                    }
663
 
                  d->__nextchar += strlen (d->__nextchar);
664
 
                  d->optopt = pfound->val;
665
 
                  return optstring[0] == ':' ? ':' : '?';
666
 
                }
667
 
            }
668
 
          d->__nextchar += strlen (d->__nextchar);
669
 
          if (longind != NULL)
670
 
            *longind = option_index;
671
 
          if (pfound->flag)
672
 
            {
673
 
              *(pfound->flag) = pfound->val;
674
 
              return 0;
675
 
            }
676
 
          return pfound->val;
677
 
        }
 
554
        {
 
555
          option_index = indfound;
 
556
          d->optind++;
 
557
          if (*nameend)
 
558
            {
 
559
              /* Don't test has_arg with >, because some C compilers don't
 
560
                 allow it to be used on enums.  */
 
561
              if (pfound->has_arg)
 
562
                d->optarg = nameend + 1;
 
563
              else
 
564
                {
 
565
                  if (print_errors)
 
566
                    {
 
567
#if defined _LIBC && defined USE_IN_LIBIO
 
568
                      char *buf;
 
569
                      int n;
 
570
#endif
 
571
 
 
572
                      if (argv[d->optind - 1][1] == '-')
 
573
                        {
 
574
                          /* --option */
 
575
#if defined _LIBC && defined USE_IN_LIBIO
 
576
                          n = __asprintf (&buf, _("\
 
577
%s: option '--%s' doesn't allow an argument\n"),
 
578
                                          argv[0], pfound->name);
 
579
#else
 
580
                          fprintf (stderr, _("\
 
581
%s: option '--%s' doesn't allow an argument\n"),
 
582
                                   argv[0], pfound->name);
 
583
#endif
 
584
                        }
 
585
                      else
 
586
                        {
 
587
                          /* +option or -option */
 
588
#if defined _LIBC && defined USE_IN_LIBIO
 
589
                          n = __asprintf (&buf, _("\
 
590
%s: option '%c%s' doesn't allow an argument\n"),
 
591
                                          argv[0], argv[d->optind - 1][0],
 
592
                                          pfound->name);
 
593
#else
 
594
                          fprintf (stderr, _("\
 
595
%s: option '%c%s' doesn't allow an argument\n"),
 
596
                                   argv[0], argv[d->optind - 1][0],
 
597
                                   pfound->name);
 
598
#endif
 
599
                        }
 
600
 
 
601
#if defined _LIBC && defined USE_IN_LIBIO
 
602
                      if (n >= 0)
 
603
                        {
 
604
                          _IO_flockfile (stderr);
 
605
 
 
606
                          int old_flags2 = ((_IO_FILE *) stderr)->_flags2;
 
607
                          ((_IO_FILE *) stderr)->_flags2
 
608
                            |= _IO_FLAGS2_NOTCANCEL;
 
609
 
 
610
                          __fxprintf (NULL, "%s", buf);
 
611
 
 
612
                          ((_IO_FILE *) stderr)->_flags2 = old_flags2;
 
613
                          _IO_funlockfile (stderr);
 
614
 
 
615
                          free (buf);
 
616
                        }
 
617
#endif
 
618
                    }
 
619
 
 
620
                  d->__nextchar += strlen (d->__nextchar);
 
621
 
 
622
                  d->optopt = pfound->val;
 
623
                  return '?';
 
624
                }
 
625
            }
 
626
          else if (pfound->has_arg == 1)
 
627
            {
 
628
              if (d->optind < argc)
 
629
                d->optarg = argv[d->optind++];
 
630
              else
 
631
                {
 
632
                  if (print_errors)
 
633
                    {
 
634
#if defined _LIBC && defined USE_IN_LIBIO
 
635
                      char *buf;
 
636
 
 
637
                      if (__asprintf (&buf, _("\
 
638
%s: option '--%s' requires an argument\n"),
 
639
                                      argv[0], pfound->name) >= 0)
 
640
                        {
 
641
                          _IO_flockfile (stderr);
 
642
 
 
643
                          int old_flags2 = ((_IO_FILE *) stderr)->_flags2;
 
644
                          ((_IO_FILE *) stderr)->_flags2
 
645
                            |= _IO_FLAGS2_NOTCANCEL;
 
646
 
 
647
                          __fxprintf (NULL, "%s", buf);
 
648
 
 
649
                          ((_IO_FILE *) stderr)->_flags2 = old_flags2;
 
650
                          _IO_funlockfile (stderr);
 
651
 
 
652
                          free (buf);
 
653
                        }
 
654
#else
 
655
                      fprintf (stderr,
 
656
                               _("%s: option '--%s' requires an argument\n"),
 
657
                               argv[0], pfound->name);
 
658
#endif
 
659
                    }
 
660
                  d->__nextchar += strlen (d->__nextchar);
 
661
                  d->optopt = pfound->val;
 
662
                  return optstring[0] == ':' ? ':' : '?';
 
663
                }
 
664
            }
 
665
          d->__nextchar += strlen (d->__nextchar);
 
666
          if (longind != NULL)
 
667
            *longind = option_index;
 
668
          if (pfound->flag)
 
669
            {
 
670
              *(pfound->flag) = pfound->val;
 
671
              return 0;
 
672
            }
 
673
          return pfound->val;
 
674
        }
678
675
 
679
676
      /* Can't find it as a long option.  If this is not getopt_long_only,
680
 
         or the option starts with '--' or is not a valid short
681
 
         option, then it's an error.
682
 
         Otherwise interpret it as a short option.  */
 
677
         or the option starts with '--' or is not a valid short
 
678
         option, then it's an error.
 
679
         Otherwise interpret it as a short option.  */
683
680
      if (!long_only || argv[d->optind][1] == '-'
684
 
          || strchr (optstring, *d->__nextchar) == NULL)
685
 
        {
686
 
          if (print_errors)
687
 
            {
688
 
#if defined _LIBC && defined USE_IN_LIBIO
689
 
              char *buf;
690
 
              int n;
691
 
#endif
692
 
 
693
 
              if (argv[d->optind][1] == '-')
694
 
                {
695
 
                  /* --option */
696
 
#if defined _LIBC && defined USE_IN_LIBIO
697
 
                  n = __asprintf (&buf, _("%s: unrecognized option `--%s'\n"),
698
 
                                  argv[0], d->__nextchar);
699
 
#else
700
 
                  fprintf (stderr, _("%s: unrecognized option `--%s'\n"),
701
 
                           argv[0], d->__nextchar);
702
 
#endif
703
 
                }
704
 
              else
705
 
                {
706
 
                  /* +option or -option */
707
 
#if defined _LIBC && defined USE_IN_LIBIO
708
 
                  n = __asprintf (&buf, _("%s: unrecognized option `%c%s'\n"),
709
 
                                  argv[0], argv[d->optind][0], d->__nextchar);
710
 
#else
711
 
                  fprintf (stderr, _("%s: unrecognized option `%c%s'\n"),
712
 
                           argv[0], argv[d->optind][0], d->__nextchar);
713
 
#endif
714
 
                }
715
 
 
716
 
#if defined _LIBC && defined USE_IN_LIBIO
717
 
              if (n >= 0)
718
 
                {
719
 
                  _IO_flockfile (stderr);
720
 
 
721
 
                  int old_flags2 = ((_IO_FILE *) stderr)->_flags2;
722
 
                  ((_IO_FILE *) stderr)->_flags2 |= _IO_FLAGS2_NOTCANCEL;
723
 
 
724
 
                  __fxprintf (NULL, "%s", buf);
725
 
 
726
 
                  ((_IO_FILE *) stderr)->_flags2 = old_flags2;
727
 
                  _IO_funlockfile (stderr);
728
 
 
729
 
                  free (buf);
730
 
                }
731
 
#endif
732
 
            }
733
 
          d->__nextchar = (char *) "";
734
 
          d->optind++;
735
 
          d->optopt = 0;
736
 
          return '?';
737
 
        }
 
681
          || strchr (optstring, *d->__nextchar) == NULL)
 
682
        {
 
683
          if (print_errors)
 
684
            {
 
685
#if defined _LIBC && defined USE_IN_LIBIO
 
686
              char *buf;
 
687
              int n;
 
688
#endif
 
689
 
 
690
              if (argv[d->optind][1] == '-')
 
691
                {
 
692
                  /* --option */
 
693
#if defined _LIBC && defined USE_IN_LIBIO
 
694
                  n = __asprintf (&buf, _("%s: unrecognized option '--%s'\n"),
 
695
                                  argv[0], d->__nextchar);
 
696
#else
 
697
                  fprintf (stderr, _("%s: unrecognized option '--%s'\n"),
 
698
                           argv[0], d->__nextchar);
 
699
#endif
 
700
                }
 
701
              else
 
702
                {
 
703
                  /* +option or -option */
 
704
#if defined _LIBC && defined USE_IN_LIBIO
 
705
                  n = __asprintf (&buf, _("%s: unrecognized option '%c%s'\n"),
 
706
                                  argv[0], argv[d->optind][0], d->__nextchar);
 
707
#else
 
708
                  fprintf (stderr, _("%s: unrecognized option '%c%s'\n"),
 
709
                           argv[0], argv[d->optind][0], d->__nextchar);
 
710
#endif
 
711
                }
 
712
 
 
713
#if defined _LIBC && defined USE_IN_LIBIO
 
714
              if (n >= 0)
 
715
                {
 
716
                  _IO_flockfile (stderr);
 
717
 
 
718
                  int old_flags2 = ((_IO_FILE *) stderr)->_flags2;
 
719
                  ((_IO_FILE *) stderr)->_flags2 |= _IO_FLAGS2_NOTCANCEL;
 
720
 
 
721
                  __fxprintf (NULL, "%s", buf);
 
722
 
 
723
                  ((_IO_FILE *) stderr)->_flags2 = old_flags2;
 
724
                  _IO_funlockfile (stderr);
 
725
 
 
726
                  free (buf);
 
727
                }
 
728
#endif
 
729
            }
 
730
          d->__nextchar = (char *) "";
 
731
          d->optind++;
 
732
          d->optopt = 0;
 
733
          return '?';
 
734
        }
738
735
    }
739
736
 
740
737
  /* Look at and handle the next short option-character.  */
741
738
 
742
739
  {
743
740
    char c = *d->__nextchar++;
744
 
    char *temp = strchr (optstring, c);
 
741
    const char *temp = strchr (optstring, c);
745
742
 
746
743
    /* Increment `optind' when we start to process its last character.  */
747
744
    if (*d->__nextchar == '\0')
748
745
      ++d->optind;
749
746
 
750
 
    if (temp == NULL || c == ':')
 
747
    if (temp == NULL || c == ':' || c == ';')
751
748
      {
752
 
        if (print_errors)
753
 
          {
754
 
#if defined _LIBC && defined USE_IN_LIBIO
755
 
              char *buf;
756
 
              int n;
757
 
#endif
758
 
 
759
 
            if (d->__posixly_correct)
760
 
              {
761
 
                /* 1003.2 specifies the format of this message.  */
762
 
#if defined _LIBC && defined USE_IN_LIBIO
763
 
                n = __asprintf (&buf, _("%s: illegal option -- %c\n"),
764
 
                                argv[0], c);
765
 
#else
766
 
                fprintf (stderr, _("%s: illegal option -- %c\n"), argv[0], c);
767
 
#endif
768
 
              }
769
 
            else
770
 
              {
771
 
#if defined _LIBC && defined USE_IN_LIBIO
772
 
                n = __asprintf (&buf, _("%s: invalid option -- %c\n"),
773
 
                                argv[0], c);
774
 
#else
775
 
                fprintf (stderr, _("%s: invalid option -- %c\n"), argv[0], c);
776
 
#endif
777
 
              }
778
 
 
779
 
#if defined _LIBC && defined USE_IN_LIBIO
780
 
            if (n >= 0)
781
 
              {
782
 
                _IO_flockfile (stderr);
783
 
 
784
 
                int old_flags2 = ((_IO_FILE *) stderr)->_flags2;
785
 
                ((_IO_FILE *) stderr)->_flags2 |= _IO_FLAGS2_NOTCANCEL;
786
 
 
787
 
                __fxprintf (NULL, "%s", buf);
788
 
 
789
 
                ((_IO_FILE *) stderr)->_flags2 = old_flags2;
790
 
                _IO_funlockfile (stderr);
791
 
 
792
 
                free (buf);
793
 
              }
794
 
#endif
795
 
          }
796
 
        d->optopt = c;
797
 
        return '?';
 
749
        if (print_errors)
 
750
          {
 
751
#if defined _LIBC && defined USE_IN_LIBIO
 
752
              char *buf;
 
753
              int n;
 
754
#endif
 
755
 
 
756
#if defined _LIBC && defined USE_IN_LIBIO
 
757
              n = __asprintf (&buf, _("%s: invalid option -- '%c'\n"),
 
758
                              argv[0], c);
 
759
#else
 
760
              fprintf (stderr, _("%s: invalid option -- '%c'\n"), argv[0], c);
 
761
#endif
 
762
 
 
763
#if defined _LIBC && defined USE_IN_LIBIO
 
764
            if (n >= 0)
 
765
              {
 
766
                _IO_flockfile (stderr);
 
767
 
 
768
                int old_flags2 = ((_IO_FILE *) stderr)->_flags2;
 
769
                ((_IO_FILE *) stderr)->_flags2 |= _IO_FLAGS2_NOTCANCEL;
 
770
 
 
771
                __fxprintf (NULL, "%s", buf);
 
772
 
 
773
                ((_IO_FILE *) stderr)->_flags2 = old_flags2;
 
774
                _IO_funlockfile (stderr);
 
775
 
 
776
                free (buf);
 
777
              }
 
778
#endif
 
779
          }
 
780
        d->optopt = c;
 
781
        return '?';
798
782
      }
799
783
    /* Convenience. Treat POSIX -W foo same as long option --foo */
800
784
    if (temp[0] == 'W' && temp[1] == ';')
801
785
      {
802
 
        char *nameend;
803
 
        const struct option *p;
804
 
        const struct option *pfound = NULL;
805
 
        int exact = 0;
806
 
        int ambig = 0;
807
 
        int indfound = 0;
808
 
        int option_index;
809
 
 
810
 
        /* This is an option that requires an argument.  */
811
 
        if (*d->__nextchar != '\0')
812
 
          {
813
 
            d->optarg = d->__nextchar;
814
 
            /* If we end this ARGV-element by taking the rest as an arg,
815
 
               we must advance to the next element now.  */
816
 
            d->optind++;
817
 
          }
818
 
        else if (d->optind == argc)
819
 
          {
820
 
            if (print_errors)
821
 
              {
822
 
                /* 1003.2 specifies the format of this message.  */
823
 
#if defined _LIBC && defined USE_IN_LIBIO
824
 
                char *buf;
825
 
 
826
 
                if (__asprintf (&buf,
827
 
                                _("%s: option requires an argument -- %c\n"),
828
 
                                argv[0], c) >= 0)
829
 
                  {
830
 
                    _IO_flockfile (stderr);
831
 
 
832
 
                    int old_flags2 = ((_IO_FILE *) stderr)->_flags2;
833
 
                    ((_IO_FILE *) stderr)->_flags2 |= _IO_FLAGS2_NOTCANCEL;
834
 
 
835
 
                    __fxprintf (NULL, "%s", buf);
836
 
 
837
 
                    ((_IO_FILE *) stderr)->_flags2 = old_flags2;
838
 
                    _IO_funlockfile (stderr);
839
 
 
840
 
                    free (buf);
841
 
                  }
842
 
#else
843
 
                fprintf (stderr, _("%s: option requires an argument -- %c\n"),
844
 
                         argv[0], c);
845
 
#endif
846
 
              }
847
 
            d->optopt = c;
848
 
            if (optstring[0] == ':')
849
 
              c = ':';
850
 
            else
851
 
              c = '?';
852
 
            return c;
853
 
          }
854
 
        else
855
 
          /* We already incremented `d->optind' once;
856
 
             increment it again when taking next ARGV-elt as argument.  */
857
 
          d->optarg = argv[d->optind++];
858
 
 
859
 
        /* optarg is now the argument, see if it's in the
860
 
           table of longopts.  */
861
 
 
862
 
        for (d->__nextchar = nameend = d->optarg; *nameend && *nameend != '=';
863
 
             nameend++)
864
 
          /* Do nothing.  */ ;
865
 
 
866
 
        /* Test all long options for either exact match
867
 
           or abbreviated matches.  */
868
 
        for (p = longopts, option_index = 0; p->name; p++, option_index++)
869
 
          if (!strncmp (p->name, d->__nextchar, nameend - d->__nextchar))
870
 
            {
871
 
              if ((unsigned int) (nameend - d->__nextchar) == strlen (p->name))
872
 
                {
873
 
                  /* Exact match found.  */
874
 
                  pfound = p;
875
 
                  indfound = option_index;
876
 
                  exact = 1;
877
 
                  break;
878
 
                }
879
 
              else if (pfound == NULL)
880
 
                {
881
 
                  /* First nonexact match found.  */
882
 
                  pfound = p;
883
 
                  indfound = option_index;
884
 
                }
885
 
              else
886
 
                /* Second or later nonexact match found.  */
887
 
                ambig = 1;
888
 
            }
889
 
        if (ambig && !exact)
890
 
          {
891
 
            if (print_errors)
892
 
              {
893
 
#if defined _LIBC && defined USE_IN_LIBIO
894
 
                char *buf;
895
 
 
896
 
                if (__asprintf (&buf, _("%s: option `-W %s' is ambiguous\n"),
897
 
                                argv[0], argv[d->optind]) >= 0)
898
 
                  {
899
 
                    _IO_flockfile (stderr);
900
 
 
901
 
                    int old_flags2 = ((_IO_FILE *) stderr)->_flags2;
902
 
                    ((_IO_FILE *) stderr)->_flags2 |= _IO_FLAGS2_NOTCANCEL;
903
 
 
904
 
                    __fxprintf (NULL, "%s", buf);
905
 
 
906
 
                    ((_IO_FILE *) stderr)->_flags2 = old_flags2;
907
 
                    _IO_funlockfile (stderr);
908
 
 
909
 
                    free (buf);
910
 
                  }
911
 
#else
912
 
                fprintf (stderr, _("%s: option `-W %s' is ambiguous\n"),
913
 
                         argv[0], argv[d->optind]);
914
 
#endif
915
 
              }
916
 
            d->__nextchar += strlen (d->__nextchar);
917
 
            d->optind++;
918
 
            return '?';
919
 
          }
920
 
        if (pfound != NULL)
921
 
          {
922
 
            option_index = indfound;
923
 
            if (*nameend)
924
 
              {
925
 
                /* Don't test has_arg with >, because some C compilers don't
926
 
                   allow it to be used on enums.  */
927
 
                if (pfound->has_arg)
928
 
                  d->optarg = nameend + 1;
929
 
                else
930
 
                  {
931
 
                    if (print_errors)
932
 
                      {
933
 
#if defined _LIBC && defined USE_IN_LIBIO
934
 
                        char *buf;
935
 
 
936
 
                        if (__asprintf (&buf, _("\
937
 
%s: option `-W %s' doesn't allow an argument\n"),
938
 
                                        argv[0], pfound->name) >= 0)
939
 
                          {
940
 
                            _IO_flockfile (stderr);
941
 
 
942
 
                            int old_flags2 = ((_IO_FILE *) stderr)->_flags2;
943
 
                            ((_IO_FILE *) stderr)->_flags2
944
 
                              |= _IO_FLAGS2_NOTCANCEL;
945
 
 
946
 
                            __fxprintf (NULL, "%s", buf);
947
 
 
948
 
                            ((_IO_FILE *) stderr)->_flags2 = old_flags2;
949
 
                            _IO_funlockfile (stderr);
950
 
 
951
 
                            free (buf);
952
 
                          }
953
 
#else
954
 
                        fprintf (stderr, _("\
955
 
%s: option `-W %s' doesn't allow an argument\n"),
956
 
                                 argv[0], pfound->name);
957
 
#endif
958
 
                      }
959
 
 
960
 
                    d->__nextchar += strlen (d->__nextchar);
961
 
                    return '?';
962
 
                  }
963
 
              }
964
 
            else if (pfound->has_arg == 1)
965
 
              {
966
 
                if (d->optind < argc)
967
 
                  d->optarg = argv[d->optind++];
968
 
                else
969
 
                  {
970
 
                    if (print_errors)
971
 
                      {
972
 
#if defined _LIBC && defined USE_IN_LIBIO
973
 
                        char *buf;
974
 
 
975
 
                        if (__asprintf (&buf, _("\
976
 
%s: option `%s' requires an argument\n"),
977
 
                                        argv[0], argv[d->optind - 1]) >= 0)
978
 
                          {
979
 
                            _IO_flockfile (stderr);
980
 
 
981
 
                            int old_flags2 = ((_IO_FILE *) stderr)->_flags2;
982
 
                            ((_IO_FILE *) stderr)->_flags2
983
 
                              |= _IO_FLAGS2_NOTCANCEL;
984
 
 
985
 
                            __fxprintf (NULL, "%s", buf);
986
 
 
987
 
                            ((_IO_FILE *) stderr)->_flags2 = old_flags2;
988
 
                            _IO_funlockfile (stderr);
989
 
 
990
 
                            free (buf);
991
 
                          }
992
 
#else
993
 
                        fprintf (stderr,
994
 
                                 _("%s: option `%s' requires an argument\n"),
995
 
                                 argv[0], argv[d->optind - 1]);
996
 
#endif
997
 
                      }
998
 
                    d->__nextchar += strlen (d->__nextchar);
999
 
                    return optstring[0] == ':' ? ':' : '?';
1000
 
                  }
1001
 
              }
1002
 
            d->__nextchar += strlen (d->__nextchar);
1003
 
            if (longind != NULL)
1004
 
              *longind = option_index;
1005
 
            if (pfound->flag)
1006
 
              {
1007
 
                *(pfound->flag) = pfound->val;
1008
 
                return 0;
1009
 
              }
1010
 
            return pfound->val;
1011
 
          }
1012
 
          d->__nextchar = NULL;
1013
 
          return 'W';   /* Let the application handle it.   */
 
786
        char *nameend;
 
787
        const struct option *p;
 
788
        const struct option *pfound = NULL;
 
789
        int exact = 0;
 
790
        int ambig = 0;
 
791
        int indfound = 0;
 
792
        int option_index;
 
793
 
 
794
        /* This is an option that requires an argument.  */
 
795
        if (*d->__nextchar != '\0')
 
796
          {
 
797
            d->optarg = d->__nextchar;
 
798
            /* If we end this ARGV-element by taking the rest as an arg,
 
799
               we must advance to the next element now.  */
 
800
            d->optind++;
 
801
          }
 
802
        else if (d->optind == argc)
 
803
          {
 
804
            if (print_errors)
 
805
              {
 
806
#if defined _LIBC && defined USE_IN_LIBIO
 
807
                char *buf;
 
808
 
 
809
                if (__asprintf (&buf,
 
810
                                _("%s: option requires an argument -- '%c'\n"),
 
811
                                argv[0], c) >= 0)
 
812
                  {
 
813
                    _IO_flockfile (stderr);
 
814
 
 
815
                    int old_flags2 = ((_IO_FILE *) stderr)->_flags2;
 
816
                    ((_IO_FILE *) stderr)->_flags2 |= _IO_FLAGS2_NOTCANCEL;
 
817
 
 
818
                    __fxprintf (NULL, "%s", buf);
 
819
 
 
820
                    ((_IO_FILE *) stderr)->_flags2 = old_flags2;
 
821
                    _IO_funlockfile (stderr);
 
822
 
 
823
                    free (buf);
 
824
                  }
 
825
#else
 
826
                fprintf (stderr,
 
827
                         _("%s: option requires an argument -- '%c'\n"),
 
828
                         argv[0], c);
 
829
#endif
 
830
              }
 
831
            d->optopt = c;
 
832
            if (optstring[0] == ':')
 
833
              c = ':';
 
834
            else
 
835
              c = '?';
 
836
            return c;
 
837
          }
 
838
        else
 
839
          /* We already incremented `d->optind' once;
 
840
             increment it again when taking next ARGV-elt as argument.  */
 
841
          d->optarg = argv[d->optind++];
 
842
 
 
843
        /* optarg is now the argument, see if it's in the
 
844
           table of longopts.  */
 
845
 
 
846
        for (d->__nextchar = nameend = d->optarg; *nameend && *nameend != '=';
 
847
             nameend++)
 
848
          /* Do nothing.  */ ;
 
849
 
 
850
        /* Test all long options for either exact match
 
851
           or abbreviated matches.  */
 
852
        for (p = longopts, option_index = 0; p->name; p++, option_index++)
 
853
          if (!strncmp (p->name, d->__nextchar, nameend - d->__nextchar))
 
854
            {
 
855
              if ((unsigned int) (nameend - d->__nextchar) == strlen (p->name))
 
856
                {
 
857
                  /* Exact match found.  */
 
858
                  pfound = p;
 
859
                  indfound = option_index;
 
860
                  exact = 1;
 
861
                  break;
 
862
                }
 
863
              else if (pfound == NULL)
 
864
                {
 
865
                  /* First nonexact match found.  */
 
866
                  pfound = p;
 
867
                  indfound = option_index;
 
868
                }
 
869
              else if (long_only
 
870
                       || pfound->has_arg != p->has_arg
 
871
                       || pfound->flag != p->flag
 
872
                       || pfound->val != p->val)
 
873
                /* Second or later nonexact match found.  */
 
874
                ambig = 1;
 
875
            }
 
876
        if (ambig && !exact)
 
877
          {
 
878
            if (print_errors)
 
879
              {
 
880
#if defined _LIBC && defined USE_IN_LIBIO
 
881
                char *buf;
 
882
 
 
883
                if (__asprintf (&buf, _("%s: option '-W %s' is ambiguous\n"),
 
884
                                argv[0], d->optarg) >= 0)
 
885
                  {
 
886
                    _IO_flockfile (stderr);
 
887
 
 
888
                    int old_flags2 = ((_IO_FILE *) stderr)->_flags2;
 
889
                    ((_IO_FILE *) stderr)->_flags2 |= _IO_FLAGS2_NOTCANCEL;
 
890
 
 
891
                    __fxprintf (NULL, "%s", buf);
 
892
 
 
893
                    ((_IO_FILE *) stderr)->_flags2 = old_flags2;
 
894
                    _IO_funlockfile (stderr);
 
895
 
 
896
                    free (buf);
 
897
                  }
 
898
#else
 
899
                fprintf (stderr, _("%s: option '-W %s' is ambiguous\n"),
 
900
                         argv[0], d->optarg);
 
901
#endif
 
902
              }
 
903
            d->__nextchar += strlen (d->__nextchar);
 
904
            d->optind++;
 
905
            return '?';
 
906
          }
 
907
        if (pfound != NULL)
 
908
          {
 
909
            option_index = indfound;
 
910
            if (*nameend)
 
911
              {
 
912
                /* Don't test has_arg with >, because some C compilers don't
 
913
                   allow it to be used on enums.  */
 
914
                if (pfound->has_arg)
 
915
                  d->optarg = nameend + 1;
 
916
                else
 
917
                  {
 
918
                    if (print_errors)
 
919
                      {
 
920
#if defined _LIBC && defined USE_IN_LIBIO
 
921
                        char *buf;
 
922
 
 
923
                        if (__asprintf (&buf, _("\
 
924
%s: option '-W %s' doesn't allow an argument\n"),
 
925
                                        argv[0], pfound->name) >= 0)
 
926
                          {
 
927
                            _IO_flockfile (stderr);
 
928
 
 
929
                            int old_flags2 = ((_IO_FILE *) stderr)->_flags2;
 
930
                            ((_IO_FILE *) stderr)->_flags2
 
931
                              |= _IO_FLAGS2_NOTCANCEL;
 
932
 
 
933
                            __fxprintf (NULL, "%s", buf);
 
934
 
 
935
                            ((_IO_FILE *) stderr)->_flags2 = old_flags2;
 
936
                            _IO_funlockfile (stderr);
 
937
 
 
938
                            free (buf);
 
939
                          }
 
940
#else
 
941
                        fprintf (stderr, _("\
 
942
%s: option '-W %s' doesn't allow an argument\n"),
 
943
                                 argv[0], pfound->name);
 
944
#endif
 
945
                      }
 
946
 
 
947
                    d->__nextchar += strlen (d->__nextchar);
 
948
                    return '?';
 
949
                  }
 
950
              }
 
951
            else if (pfound->has_arg == 1)
 
952
              {
 
953
                if (d->optind < argc)
 
954
                  d->optarg = argv[d->optind++];
 
955
                else
 
956
                  {
 
957
                    if (print_errors)
 
958
                      {
 
959
#if defined _LIBC && defined USE_IN_LIBIO
 
960
                        char *buf;
 
961
 
 
962
                        if (__asprintf (&buf, _("\
 
963
%s: option '-W %s' requires an argument\n"),
 
964
                                        argv[0], pfound->name) >= 0)
 
965
                          {
 
966
                            _IO_flockfile (stderr);
 
967
 
 
968
                            int old_flags2 = ((_IO_FILE *) stderr)->_flags2;
 
969
                            ((_IO_FILE *) stderr)->_flags2
 
970
                              |= _IO_FLAGS2_NOTCANCEL;
 
971
 
 
972
                            __fxprintf (NULL, "%s", buf);
 
973
 
 
974
                            ((_IO_FILE *) stderr)->_flags2 = old_flags2;
 
975
                            _IO_funlockfile (stderr);
 
976
 
 
977
                            free (buf);
 
978
                          }
 
979
#else
 
980
                        fprintf (stderr, _("\
 
981
%s: option '-W %s' requires an argument\n"),
 
982
                                 argv[0], pfound->name);
 
983
#endif
 
984
                      }
 
985
                    d->__nextchar += strlen (d->__nextchar);
 
986
                    return optstring[0] == ':' ? ':' : '?';
 
987
                  }
 
988
              }
 
989
            else
 
990
              d->optarg = NULL;
 
991
            d->__nextchar += strlen (d->__nextchar);
 
992
            if (longind != NULL)
 
993
              *longind = option_index;
 
994
            if (pfound->flag)
 
995
              {
 
996
                *(pfound->flag) = pfound->val;
 
997
                return 0;
 
998
              }
 
999
            return pfound->val;
 
1000
          }
 
1001
          d->__nextchar = NULL;
 
1002
          return 'W';   /* Let the application handle it.   */
1014
1003
      }
1015
1004
    if (temp[1] == ':')
1016
1005
      {
1017
 
        if (temp[2] == ':')
1018
 
          {
1019
 
            /* This is an option that accepts an argument optionally.  */
1020
 
            if (*d->__nextchar != '\0')
1021
 
              {
1022
 
                d->optarg = d->__nextchar;
1023
 
                d->optind++;
1024
 
              }
1025
 
            else
1026
 
              d->optarg = NULL;
1027
 
            d->__nextchar = NULL;
1028
 
          }
1029
 
        else
1030
 
          {
1031
 
            /* This is an option that requires an argument.  */
1032
 
            if (*d->__nextchar != '\0')
1033
 
              {
1034
 
                d->optarg = d->__nextchar;
1035
 
                /* If we end this ARGV-element by taking the rest as an arg,
1036
 
                   we must advance to the next element now.  */
1037
 
                d->optind++;
1038
 
              }
1039
 
            else if (d->optind == argc)
1040
 
              {
1041
 
                if (print_errors)
1042
 
                  {
1043
 
                    /* 1003.2 specifies the format of this message.  */
 
1006
        if (temp[2] == ':')
 
1007
          {
 
1008
            /* This is an option that accepts an argument optionally.  */
 
1009
            if (*d->__nextchar != '\0')
 
1010
              {
 
1011
                d->optarg = d->__nextchar;
 
1012
                d->optind++;
 
1013
              }
 
1014
            else
 
1015
              d->optarg = NULL;
 
1016
            d->__nextchar = NULL;
 
1017
          }
 
1018
        else
 
1019
          {
 
1020
            /* This is an option that requires an argument.  */
 
1021
            if (*d->__nextchar != '\0')
 
1022
              {
 
1023
                d->optarg = d->__nextchar;
 
1024
                /* If we end this ARGV-element by taking the rest as an arg,
 
1025
                   we must advance to the next element now.  */
 
1026
                d->optind++;
 
1027
              }
 
1028
            else if (d->optind == argc)
 
1029
              {
 
1030
                if (print_errors)
 
1031
                  {
1044
1032
#if defined _LIBC && defined USE_IN_LIBIO
1045
 
                    char *buf;
1046
 
 
1047
 
                    if (__asprintf (&buf, _("\
1048
 
%s: option requires an argument -- %c\n"),
1049
 
                                    argv[0], c) >= 0)
1050
 
                      {
1051
 
                        _IO_flockfile (stderr);
1052
 
 
1053
 
                        int old_flags2 = ((_IO_FILE *) stderr)->_flags2;
1054
 
                        ((_IO_FILE *) stderr)->_flags2 |= _IO_FLAGS2_NOTCANCEL;
1055
 
 
1056
 
                        __fxprintf (NULL, "%s", buf);
1057
 
 
1058
 
                        ((_IO_FILE *) stderr)->_flags2 = old_flags2;
1059
 
                        _IO_funlockfile (stderr);
1060
 
 
1061
 
                        free (buf);
1062
 
                      }
 
1033
                    char *buf;
 
1034
 
 
1035
                    if (__asprintf (&buf, _("\
 
1036
%s: option requires an argument -- '%c'\n"),
 
1037
                                    argv[0], c) >= 0)
 
1038
                      {
 
1039
                        _IO_flockfile (stderr);
 
1040
 
 
1041
                        int old_flags2 = ((_IO_FILE *) stderr)->_flags2;
 
1042
                        ((_IO_FILE *) stderr)->_flags2 |= _IO_FLAGS2_NOTCANCEL;
 
1043
 
 
1044
                        __fxprintf (NULL, "%s", buf);
 
1045
 
 
1046
                        ((_IO_FILE *) stderr)->_flags2 = old_flags2;
 
1047
                        _IO_funlockfile (stderr);
 
1048
 
 
1049
                        free (buf);
 
1050
                      }
1063
1051
#else
1064
 
                    fprintf (stderr,
1065
 
                             _("%s: option requires an argument -- %c\n"),
1066
 
                             argv[0], c);
 
1052
                    fprintf (stderr,
 
1053
                             _("%s: option requires an argument -- '%c'\n"),
 
1054
                             argv[0], c);
1067
1055
#endif
1068
 
                  }
1069
 
                d->optopt = c;
1070
 
                if (optstring[0] == ':')
1071
 
                  c = ':';
1072
 
                else
1073
 
                  c = '?';
1074
 
              }
1075
 
            else
1076
 
              /* We already incremented `optind' once;
1077
 
                 increment it again when taking next ARGV-elt as argument.  */
1078
 
              d->optarg = argv[d->optind++];
1079
 
            d->__nextchar = NULL;
1080
 
          }
 
1056
                  }
 
1057
                d->optopt = c;
 
1058
                if (optstring[0] == ':')
 
1059
                  c = ':';
 
1060
                else
 
1061
                  c = '?';
 
1062
              }
 
1063
            else
 
1064
              /* We already incremented `optind' once;
 
1065
                 increment it again when taking next ARGV-elt as argument.  */
 
1066
              d->optarg = argv[d->optind++];
 
1067
            d->__nextchar = NULL;
 
1068
          }
1081
1069
      }
1082
1070
    return c;
1083
1071
  }
1085
1073
 
1086
1074
int
1087
1075
_getopt_internal (int argc, char **argv, const char *optstring,
1088
 
                  const struct option *longopts, int *longind,
1089
 
                  int long_only, int posixly_correct)
 
1076
                  const struct option *longopts, int *longind, int long_only,
 
1077
                  int posixly_correct)
1090
1078
{
1091
1079
  int result;
1092
1080
 
1093
1081
  getopt_data.optind = optind;
1094
1082
  getopt_data.opterr = opterr;
1095
1083
 
1096
 
  result = _getopt_internal_r (argc, argv, optstring, longopts, longind,
1097
 
                               long_only, posixly_correct, &getopt_data);
 
1084
  result = _getopt_internal_r (argc, argv, optstring, longopts,
 
1085
                               longind, long_only, &getopt_data,
 
1086
                               posixly_correct);
1098
1087
 
1099
1088
  optind = getopt_data.optind;
1100
1089
  optarg = getopt_data.optarg;
1114
1103
int
1115
1104
getopt (int argc, char *const *argv, const char *optstring)
1116
1105
{
1117
 
  return _getopt_internal (argc, (char **) argv, optstring, NULL, NULL, 0,
1118
 
                           POSIXLY_CORRECT);
1119
 
}
 
1106
  return _getopt_internal (argc, (char **) argv, optstring,
 
1107
                           (const struct option *) 0,
 
1108
                           (int *) 0,
 
1109
                           0, POSIXLY_CORRECT);
 
1110
}
 
1111
 
 
1112
#ifdef _LIBC
 
1113
int
 
1114
__posix_getopt (int argc, char *const *argv, const char *optstring)
 
1115
{
 
1116
  return _getopt_internal (argc, argv, optstring,
 
1117
                           (const struct option *) 0,
 
1118
                           (int *) 0,
 
1119
                           0, 1);
 
1120
}
 
1121
#endif
1120
1122
 
1121
1123
 
1122
1124
#ifdef TEST
1136
1138
 
1137
1139
      c = getopt (argc, argv, "abc:d:0123456789");
1138
1140
      if (c == -1)
1139
 
        break;
 
1141
        break;
1140
1142
 
1141
1143
      switch (c)
1142
 
        {
1143
 
        case '0':
1144
 
        case '1':
1145
 
        case '2':
1146
 
        case '3':
1147
 
        case '4':
1148
 
        case '5':
1149
 
        case '6':
1150
 
        case '7':
1151
 
        case '8':
1152
 
        case '9':
1153
 
          if (digit_optind != 0 && digit_optind != this_option_optind)
1154
 
            printf ("digits occur in two different argv-elements.\n");
1155
 
          digit_optind = this_option_optind;
1156
 
          printf ("option %c\n", c);
1157
 
          break;
1158
 
 
1159
 
        case 'a':
1160
 
          printf ("option a\n");
1161
 
          break;
1162
 
 
1163
 
        case 'b':
1164
 
          printf ("option b\n");
1165
 
          break;
1166
 
 
1167
 
        case 'c':
1168
 
          printf ("option c with value `%s'\n", optarg);
1169
 
          break;
1170
 
 
1171
 
        case '?':
1172
 
          break;
1173
 
 
1174
 
        default:
1175
 
          printf ("?? getopt returned character code 0%o ??\n", c);
1176
 
        }
 
1144
        {
 
1145
        case '0':
 
1146
        case '1':
 
1147
        case '2':
 
1148
        case '3':
 
1149
        case '4':
 
1150
        case '5':
 
1151
        case '6':
 
1152
        case '7':
 
1153
        case '8':
 
1154
        case '9':
 
1155
          if (digit_optind != 0 && digit_optind != this_option_optind)
 
1156
            printf ("digits occur in two different argv-elements.\n");
 
1157
          digit_optind = this_option_optind;
 
1158
          printf ("option %c\n", c);
 
1159
          break;
 
1160
 
 
1161
        case 'a':
 
1162
          printf ("option a\n");
 
1163
          break;
 
1164
 
 
1165
        case 'b':
 
1166
          printf ("option b\n");
 
1167
          break;
 
1168
 
 
1169
        case 'c':
 
1170
          printf ("option c with value '%s'\n", optarg);
 
1171
          break;
 
1172
 
 
1173
        case '?':
 
1174
          break;
 
1175
 
 
1176
        default:
 
1177
          printf ("?? getopt returned character code 0%o ??\n", c);
 
1178
        }
1177
1179
    }
1178
1180
 
1179
1181
  if (optind < argc)
1180
1182
    {
1181
1183
      printf ("non-option ARGV-elements: ");
1182
1184
      while (optind < argc)
1183
 
        printf ("%s ", argv[optind++]);
 
1185
        printf ("%s ", argv[optind++]);
1184
1186
      printf ("\n");
1185
1187
    }
1186
1188