~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to gnulib/getopt.c

  • Committer: Monty Taylor
  • Date: 2009-02-05 21:07:57 UTC
  • mto: This revision was merged to the branch mainline in revision 840.
  • Revision ID: mordred@inaugust.com-20090205210757-6487lf69y3mndcds
Fixed warnings badness in csv_alter_table test.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* Getopt for GNU.
 
2
   NOTE: getopt is now part of the C library, so if you don't know what
 
3
   "Keep this file name-space clean" means, talk to drepper@gnu.org
 
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,2008
 
6
        Free Software Foundation, Inc.
 
7
   This file is part of the GNU C Library.
 
8
 
 
9
   This program is free software: you can redistribute it and/or modify
 
10
   it under the terms of the GNU Lesser General Public License as published by
 
11
   the Free Software Foundation; either version 3 of the License, or
 
12
   (at your option) any later version.
 
13
 
 
14
   This program is distributed in the hope that it will be useful,
 
15
   but WITHOUT ANY WARRANTY; without even the implied warranty of
 
16
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
17
   GNU Lesser General Public License for more details.
 
18
 
 
19
   You should have received a copy of the GNU Lesser General Public License
 
20
   along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
 
21
 
 
22
#ifndef _LIBC
 
23
# include <config.h>
 
24
#endif
 
25
 
 
26
#include "getopt.h"
 
27
 
 
28
#include <stdio.h>
 
29
#include <stdlib.h>
 
30
#include <string.h>
 
31
#include <unistd.h>
 
32
 
 
33
#ifdef _LIBC
 
34
# include <libintl.h>
 
35
#else
 
36
# include "drizzled/gettext.h"
 
37
#endif
 
38
 
 
39
#if defined _LIBC && defined USE_IN_LIBIO
 
40
# include <wchar.h>
 
41
#endif
 
42
 
 
43
#ifndef attribute_hidden
 
44
# define attribute_hidden
 
45
#endif
 
46
 
 
47
/* Unlike standard Unix `getopt', functions like `getopt_long'
 
48
   let the user intersperse the options with the other arguments.
 
49
 
 
50
   As `getopt_long' works, it permutes the elements of ARGV so that,
 
51
   when it is done, all the options precede everything else.  Thus
 
52
   all application programs are extended to handle flexible argument order.
 
53
 
 
54
   Using `getopt' or setting the environment variable POSIXLY_CORRECT
 
55
   disables permutation.
 
56
   Then the application's behavior is completely standard.
 
57
 
 
58
   GNU application programs can use a third alternative mode in which
 
59
   they can distinguish the relative order of options and other arguments.  */
 
60
 
 
61
#include "getopt_int.h"
 
62
 
 
63
/* For communication from `getopt' to the caller.
 
64
   When `getopt' finds an option that takes an argument,
 
65
   the argument value is returned here.
 
66
   Also, when `ordering' is RETURN_IN_ORDER,
 
67
   each non-option ARGV-element is returned here.  */
 
68
 
 
69
char *optarg;
 
70
 
 
71
/* Index in ARGV of the next element to be scanned.
 
72
   This is used for communication to and from the caller
 
73
   and for communication between successive calls to `getopt'.
 
74
 
 
75
   On entry to `getopt', zero means this is the first call; initialize.
 
76
 
 
77
   When `getopt' returns -1, this is the index of the first of the
 
78
   non-option elements that the caller should itself scan.
 
79
 
 
80
   Otherwise, `optind' communicates from one call to the next
 
81
   how much of ARGV has been scanned so far.  */
 
82
 
 
83
/* 1003.2 says this must be 1 before any call.  */
 
84
int optind = 1;
 
85
 
 
86
/* Callers store zero here to inhibit the error message
 
87
   for unrecognized options.  */
 
88
 
 
89
int opterr = 1;
 
90
 
 
91
/* Set to an option character which was unrecognized.
 
92
   This must be initialized on some systems to avoid linking in the
 
93
   system's own getopt implementation.  */
 
94
 
 
95
int optopt = '?';
 
96
 
 
97
/* Keep a global copy of all internal members of getopt_data.  */
 
98
 
 
99
static struct _getopt_data getopt_data;
 
100
 
 
101
 
 
102
#if defined HAVE_DECL_GETENV && !HAVE_DECL_GETENV
 
103
extern char *getenv ();
 
104
#endif
 
105
 
 
106
#ifdef _LIBC
 
107
/* Stored original parameters.
 
108
   XXX This is no good solution.  We should rather copy the args so
 
109
   that we can compare them later.  But we must not use malloc(3).  */
 
110
extern int __libc_argc;
 
111
extern char **__libc_argv;
 
112
 
 
113
/* Bash 2.0 gives us an environment variable containing flags
 
114
   indicating ARGV elements that should not be considered arguments.  */
 
115
 
 
116
# ifdef USE_NONOPTION_FLAGS
 
117
/* Defined in getopt_init.c  */
 
118
extern char *__getopt_nonoption_flags;
 
119
# endif
 
120
 
 
121
# ifdef USE_NONOPTION_FLAGS
 
122
#  define SWAP_FLAGS(ch1, ch2) \
 
123
  if (d->__nonoption_flags_len > 0)                                           \
 
124
    {                                                                         \
 
125
      char __tmp = __getopt_nonoption_flags[ch1];                             \
 
126
      __getopt_nonoption_flags[ch1] = __getopt_nonoption_flags[ch2];          \
 
127
      __getopt_nonoption_flags[ch2] = __tmp;                                  \
 
128
    }
 
129
# else
 
130
#  define SWAP_FLAGS(ch1, ch2)
 
131
# endif
 
132
#else   /* !_LIBC */
 
133
# define SWAP_FLAGS(ch1, ch2)
 
134
#endif  /* _LIBC */
 
135
 
 
136
/* Exchange two adjacent subsequences of ARGV.
 
137
   One subsequence is elements [first_nonopt,last_nonopt)
 
138
   which contains all the non-options that have been skipped so far.
 
139
   The other is elements [last_nonopt,optind), which contains all
 
140
   the options processed since those non-options were skipped.
 
141
 
 
142
   `first_nonopt' and `last_nonopt' are relocated so that they describe
 
143
   the new indices of the non-options in ARGV after they are moved.  */
 
144
 
 
145
static void
 
146
exchange (char **argv, struct _getopt_data *d)
 
147
{
 
148
  int bottom = d->__first_nonopt;
 
149
  int middle = d->__last_nonopt;
 
150
  int top = d->optind;
 
151
  char *tem;
 
152
 
 
153
  /* Exchange the shorter segment with the far end of the longer segment.
 
154
     That puts the shorter segment into the right place.
 
155
     It leaves the longer segment in the right place overall,
 
156
     but it consists of two parts that need to be swapped next.  */
 
157
 
 
158
#if defined _LIBC && defined USE_NONOPTION_FLAGS
 
159
  /* First make sure the handling of the `__getopt_nonoption_flags'
 
160
     string can work normally.  Our top argument must be in the range
 
161
     of the string.  */
 
162
  if (d->__nonoption_flags_len > 0 && top >= d->__nonoption_flags_max_len)
 
163
    {
 
164
      /* We must extend the array.  The user plays games with us and
 
165
         presents new arguments.  */
 
166
      char *new_str = malloc (top + 1);
 
167
      if (new_str == NULL)
 
168
        d->__nonoption_flags_len = d->__nonoption_flags_max_len = 0;
 
169
      else
 
170
        {
 
171
          memset (__mempcpy (new_str, __getopt_nonoption_flags,
 
172
                             d->__nonoption_flags_max_len),
 
173
                  '\0', top + 1 - d->__nonoption_flags_max_len);
 
174
          d->__nonoption_flags_max_len = top + 1;
 
175
          __getopt_nonoption_flags = new_str;
 
176
        }
 
177
    }
 
178
#endif
 
179
 
 
180
  while (top > middle && middle > bottom)
 
181
    {
 
182
      if (top - middle > middle - bottom)
 
183
        {
 
184
          /* Bottom segment is the short one.  */
 
185
          int len = middle - bottom;
 
186
          register int i;
 
187
 
 
188
          /* Swap it with the top part of the top segment.  */
 
189
          for (i = 0; i < len; i++)
 
190
            {
 
191
              tem = argv[bottom + i];
 
192
              argv[bottom + i] = argv[top - (middle - bottom) + i];
 
193
              argv[top - (middle - bottom) + i] = tem;
 
194
              SWAP_FLAGS (bottom + i, top - (middle - bottom) + i);
 
195
            }
 
196
          /* Exclude the moved bottom segment from further swapping.  */
 
197
          top -= len;
 
198
        }
 
199
      else
 
200
        {
 
201
          /* Top segment is the short one.  */
 
202
          int len = top - middle;
 
203
          register int i;
 
204
 
 
205
          /* Swap it with the bottom part of the bottom segment.  */
 
206
          for (i = 0; i < len; i++)
 
207
            {
 
208
              tem = argv[bottom + i];
 
209
              argv[bottom + i] = argv[middle + i];
 
210
              argv[middle + i] = tem;
 
211
              SWAP_FLAGS (bottom + i, middle + i);
 
212
            }
 
213
          /* Exclude the moved top segment from further swapping.  */
 
214
          bottom += len;
 
215
        }
 
216
    }
 
217
 
 
218
  /* Update records for the slots the non-options now occupy.  */
 
219
 
 
220
  d->__first_nonopt += (d->optind - d->__last_nonopt);
 
221
  d->__last_nonopt = d->optind;
 
222
}
 
223
 
 
224
/* Initialize the internal data when the first call is made.  */
 
225
 
 
226
static const char *
 
227
_getopt_initialize (int argc, char **argv, const char *optstring,
 
228
                    int posixly_correct, struct _getopt_data *d)
 
229
{
 
230
  /* Start processing options with ARGV-element 1 (since ARGV-element 0
 
231
     is the program name); the sequence of previously skipped
 
232
     non-option ARGV-elements is empty.  */
 
233
 
 
234
  d->__first_nonopt = d->__last_nonopt = d->optind;
 
235
 
 
236
  d->__nextchar = NULL;
 
237
 
 
238
  d->__posixly_correct = posixly_correct || !!getenv ("POSIXLY_CORRECT");
 
239
 
 
240
  /* Determine how to handle the ordering of options and nonoptions.  */
 
241
 
 
242
  if (optstring[0] == '-')
 
243
    {
 
244
      d->__ordering = RETURN_IN_ORDER;
 
245
      ++optstring;
 
246
    }
 
247
  else if (optstring[0] == '+')
 
248
    {
 
249
      d->__ordering = REQUIRE_ORDER;
 
250
      ++optstring;
 
251
    }
 
252
  else if (d->__posixly_correct)
 
253
    d->__ordering = REQUIRE_ORDER;
 
254
  else
 
255
    d->__ordering = PERMUTE;
 
256
 
 
257
#if defined _LIBC && defined USE_NONOPTION_FLAGS
 
258
  if (!d->__posixly_correct
 
259
      && argc == __libc_argc && argv == __libc_argv)
 
260
    {
 
261
      if (d->__nonoption_flags_max_len == 0)
 
262
        {
 
263
          if (__getopt_nonoption_flags == NULL
 
264
              || __getopt_nonoption_flags[0] == '\0')
 
265
            d->__nonoption_flags_max_len = -1;
 
266
          else
 
267
            {
 
268
              const char *orig_str = __getopt_nonoption_flags;
 
269
              int len = d->__nonoption_flags_max_len = strlen (orig_str);
 
270
              if (d->__nonoption_flags_max_len < argc)
 
271
                d->__nonoption_flags_max_len = argc;
 
272
              __getopt_nonoption_flags =
 
273
                (char *) malloc (d->__nonoption_flags_max_len);
 
274
              if (__getopt_nonoption_flags == NULL)
 
275
                d->__nonoption_flags_max_len = -1;
 
276
              else
 
277
                memset (__mempcpy (__getopt_nonoption_flags, orig_str, len),
 
278
                        '\0', d->__nonoption_flags_max_len - len);
 
279
            }
 
280
        }
 
281
      d->__nonoption_flags_len = d->__nonoption_flags_max_len;
 
282
    }
 
283
  else
 
284
    d->__nonoption_flags_len = 0;
 
285
#else 
 
286
  (void)argc;
 
287
  (void)argv;
 
288
#endif
 
289
 
 
290
  return optstring;
 
291
}
 
292
 
 
293
/* Scan elements of ARGV (whose length is ARGC) for option characters
 
294
   given in OPTSTRING.
 
295
 
 
296
   If an element of ARGV starts with '-', and is not exactly "-" or "--",
 
297
   then it is an option element.  The characters of this element
 
298
   (aside from the initial '-') are option characters.  If `getopt'
 
299
   is called repeatedly, it returns successively each of the option characters
 
300
   from each of the option elements.
 
301
 
 
302
   If `getopt' finds another option character, it returns that character,
 
303
   updating `optind' and `nextchar' so that the next call to `getopt' can
 
304
   resume the scan with the following option character or ARGV-element.
 
305
 
 
306
   If there are no more option characters, `getopt' returns -1.
 
307
   Then `optind' is the index in ARGV of the first ARGV-element
 
308
   that is not an option.  (The ARGV-elements have been permuted
 
309
   so that those that are not options now come last.)
 
310
 
 
311
   OPTSTRING is a string containing the legitimate option characters.
 
312
   If an option character is seen that is not listed in OPTSTRING,
 
313
   return '?' after printing an error message.  If you set `opterr' to
 
314
   zero, the error message is suppressed but we still return '?'.
 
315
 
 
316
   If a char in OPTSTRING is followed by a colon, that means it wants an arg,
 
317
   so the following text in the same ARGV-element, or the text of the following
 
318
   ARGV-element, is returned in `optarg'.  Two colons mean an option that
 
319
   wants an optional arg; if there is text in the current ARGV-element,
 
320
   it is returned in `optarg', otherwise `optarg' is set to zero.
 
321
 
 
322
   If OPTSTRING starts with `-' or `+', it requests different methods of
 
323
   handling the non-option ARGV-elements.
 
324
   See the comments about RETURN_IN_ORDER and REQUIRE_ORDER, above.
 
325
 
 
326
   Long-named options begin with `--' instead of `-'.
 
327
   Their names may be abbreviated as long as the abbreviation is unique
 
328
   or is an exact match for some defined option.  If they have an
 
329
   argument, it follows the option name in the same ARGV-element, separated
 
330
   from the option name by a `=', or else the in next ARGV-element.
 
331
   When `getopt' finds a long-named option, it returns 0 if that option's
 
332
   `flag' field is nonzero, the value of the option's `val' field
 
333
   if the `flag' field is zero.
 
334
 
 
335
   LONGOPTS is a vector of `struct option' terminated by an
 
336
   element containing a name which is zero.
 
337
 
 
338
   LONGIND returns the index in LONGOPT of the long-named option found.
 
339
   It is only valid when a long-named option has been found by the most
 
340
   recent call.
 
341
 
 
342
   If LONG_ONLY is nonzero, '-' as well as '--' can introduce
 
343
   long-named options.
 
344
 
 
345
   If POSIXLY_CORRECT is nonzero, behave as if the POSIXLY_CORRECT
 
346
   environment variable were set.  */
 
347
 
 
348
int
 
349
_getopt_internal_r (int argc, char **argv, const char *optstring,
 
350
                    const struct option *longopts, int *longind,
 
351
                    int long_only, int posixly_correct, struct _getopt_data *d)
 
352
{
 
353
  int print_errors = d->opterr;
 
354
  if (optstring[0] == ':')
 
355
    print_errors = 0;
 
356
 
 
357
  if (argc < 1)
 
358
    return -1;
 
359
 
 
360
  d->optarg = NULL;
 
361
 
 
362
  if (d->optind == 0 || !d->__initialized)
 
363
    {
 
364
      if (d->optind == 0)
 
365
        d->optind = 1;  /* Don't scan ARGV[0], the program name.  */
 
366
      optstring = _getopt_initialize (argc, argv, optstring,
 
367
                                      posixly_correct, d);
 
368
      d->__initialized = 1;
 
369
    }
 
370
 
 
371
  /* Test whether ARGV[optind] points to a non-option argument.
 
372
     Either it does not have option syntax, or there is an environment flag
 
373
     from the shell indicating it is not an option.  The later information
 
374
     is only used when the used in the GNU libc.  */
 
375
#if defined _LIBC && defined USE_NONOPTION_FLAGS
 
376
# define NONOPTION_P (argv[d->optind][0] != '-' || argv[d->optind][1] == '\0' \
 
377
                      || (d->optind < d->__nonoption_flags_len                \
 
378
                          && __getopt_nonoption_flags[d->optind] == '1'))
 
379
#else
 
380
# define NONOPTION_P (argv[d->optind][0] != '-' || argv[d->optind][1] == '\0')
 
381
#endif
 
382
 
 
383
  if (d->__nextchar == NULL || *d->__nextchar == '\0')
 
384
    {
 
385
      /* Advance to the next ARGV-element.  */
 
386
 
 
387
      /* Give FIRST_NONOPT & LAST_NONOPT rational values if OPTIND has been
 
388
         moved back by the user (who may also have changed the arguments).  */
 
389
      if (d->__last_nonopt > d->optind)
 
390
        d->__last_nonopt = d->optind;
 
391
      if (d->__first_nonopt > d->optind)
 
392
        d->__first_nonopt = d->optind;
 
393
 
 
394
      if (d->__ordering == PERMUTE)
 
395
        {
 
396
          /* If we have just processed some options following some non-options,
 
397
             exchange them so that the options come first.  */
 
398
 
 
399
          if (d->__first_nonopt != d->__last_nonopt
 
400
              && d->__last_nonopt != d->optind)
 
401
            exchange ((char **) argv, d);
 
402
          else if (d->__last_nonopt != d->optind)
 
403
            d->__first_nonopt = d->optind;
 
404
 
 
405
          /* Skip any additional non-options
 
406
             and extend the range of non-options previously skipped.  */
 
407
 
 
408
          while (d->optind < argc && NONOPTION_P)
 
409
            d->optind++;
 
410
          d->__last_nonopt = d->optind;
 
411
        }
 
412
 
 
413
      /* The special ARGV-element `--' means premature end of options.
 
414
         Skip it like a null option,
 
415
         then exchange with previous non-options as if it were an option,
 
416
         then skip everything else like a non-option.  */
 
417
 
 
418
      if (d->optind != argc && !strcmp (argv[d->optind], "--"))
 
419
        {
 
420
          d->optind++;
 
421
 
 
422
          if (d->__first_nonopt != d->__last_nonopt
 
423
              && d->__last_nonopt != d->optind)
 
424
            exchange ((char **) argv, d);
 
425
          else if (d->__first_nonopt == d->__last_nonopt)
 
426
            d->__first_nonopt = d->optind;
 
427
          d->__last_nonopt = argc;
 
428
 
 
429
          d->optind = argc;
 
430
        }
 
431
 
 
432
      /* If we have done all the ARGV-elements, stop the scan
 
433
         and back over any non-options that we skipped and permuted.  */
 
434
 
 
435
      if (d->optind == argc)
 
436
        {
 
437
          /* Set the next-arg-index to point at the non-options
 
438
             that we previously skipped, so the caller will digest them.  */
 
439
          if (d->__first_nonopt != d->__last_nonopt)
 
440
            d->optind = d->__first_nonopt;
 
441
          return -1;
 
442
        }
 
443
 
 
444
      /* If we have come to a non-option and did not permute it,
 
445
         either stop the scan or describe it to the caller and pass it by.  */
 
446
 
 
447
      if (NONOPTION_P)
 
448
        {
 
449
          if (d->__ordering == REQUIRE_ORDER)
 
450
            return -1;
 
451
          d->optarg = argv[d->optind++];
 
452
          return 1;
 
453
        }
 
454
 
 
455
      /* We have found another option-ARGV-element.
 
456
         Skip the initial punctuation.  */
 
457
 
 
458
      d->__nextchar = (argv[d->optind] + 1
 
459
                  + (longopts != NULL && argv[d->optind][1] == '-'));
 
460
    }
 
461
 
 
462
  /* Decode the current option-ARGV-element.  */
 
463
 
 
464
  /* Check whether the ARGV-element is a long option.
 
465
 
 
466
     If long_only and the ARGV-element has the form "-f", where f is
 
467
     a valid short option, don't consider it an abbreviated form of
 
468
     a long option that starts with f.  Otherwise there would be no
 
469
     way to give the -f short option.
 
470
 
 
471
     On the other hand, if there's a long option "fubar" and
 
472
     the ARGV-element is "-fu", do consider that an abbreviation of
 
473
     the long option, just like "--fu", and not "-f" with arg "u".
 
474
 
 
475
     This distinction seems to be the most useful approach.  */
 
476
 
 
477
  if (longopts != NULL
 
478
      && (argv[d->optind][1] == '-'
 
479
          || (long_only && (argv[d->optind][2]
 
480
                            || !strchr (optstring, argv[d->optind][1])))))
 
481
    {
 
482
      char *nameend;
 
483
      const struct option *p;
 
484
      const struct option *pfound = NULL;
 
485
      int exact = 0;
 
486
      int ambig = 0;
 
487
      int indfound = -1;
 
488
      int option_index;
 
489
 
 
490
      for (nameend = d->__nextchar; *nameend && *nameend != '='; nameend++)
 
491
        /* Do nothing.  */ ;
 
492
 
 
493
      /* Test all long options for either exact match
 
494
         or abbreviated matches.  */
 
495
      for (p = longopts, option_index = 0; p->name; p++, option_index++)
 
496
        if (!strncmp (p->name, d->__nextchar, nameend - d->__nextchar))
 
497
          {
 
498
            if ((unsigned int) (nameend - d->__nextchar)
 
499
                == (unsigned int) strlen (p->name))
 
500
              {
 
501
                /* Exact match found.  */
 
502
                pfound = p;
 
503
                indfound = option_index;
 
504
                exact = 1;
 
505
                break;
 
506
              }
 
507
            else if (pfound == NULL)
 
508
              {
 
509
                /* First nonexact match found.  */
 
510
                pfound = p;
 
511
                indfound = option_index;
 
512
              }
 
513
            else if (long_only
 
514
                     || pfound->has_arg != p->has_arg
 
515
                     || pfound->flag != p->flag
 
516
                     || pfound->val != p->val)
 
517
              /* Second or later nonexact match found.  */
 
518
              ambig = 1;
 
519
          }
 
520
 
 
521
      if (ambig && !exact)
 
522
        {
 
523
          if (print_errors)
 
524
            {
 
525
#if defined _LIBC && defined USE_IN_LIBIO
 
526
              char *buf;
 
527
 
 
528
              if (__asprintf (&buf, _("%s: option `%s' is ambiguous\n"),
 
529
                              argv[0], argv[d->optind]) >= 0)
 
530
                {
 
531
                  _IO_flockfile (stderr);
 
532
 
 
533
                  int old_flags2 = ((_IO_FILE *) stderr)->_flags2;
 
534
                  ((_IO_FILE *) stderr)->_flags2 |= _IO_FLAGS2_NOTCANCEL;
 
535
 
 
536
                  __fxprintf (NULL, "%s", buf);
 
537
 
 
538
                  ((_IO_FILE *) stderr)->_flags2 = old_flags2;
 
539
                  _IO_funlockfile (stderr);
 
540
 
 
541
                  free (buf);
 
542
                }
 
543
#else
 
544
              fprintf (stderr, _("%s: option `%s' is ambiguous\n"),
 
545
                       argv[0], argv[d->optind]);
 
546
#endif
 
547
            }
 
548
          d->__nextchar += strlen (d->__nextchar);
 
549
          d->optind++;
 
550
          d->optopt = 0;
 
551
          return '?';
 
552
        }
 
553
 
 
554
      if (pfound != NULL)
 
555
        {
 
556
          option_index = indfound;
 
557
          d->optind++;
 
558
          if (*nameend)
 
559
            {
 
560
              /* Don't test has_arg with >, because some C compilers don't
 
561
                 allow it to be used on enums.  */
 
562
              if (pfound->has_arg)
 
563
                d->optarg = nameend + 1;
 
564
              else
 
565
                {
 
566
                  if (print_errors)
 
567
                    {
 
568
#if defined _LIBC && defined USE_IN_LIBIO
 
569
                      char *buf;
 
570
                      int n;
 
571
#endif
 
572
 
 
573
                      if (argv[d->optind - 1][1] == '-')
 
574
                        {
 
575
                          /* --option */
 
576
#if defined _LIBC && defined USE_IN_LIBIO
 
577
                          n = __asprintf (&buf, _("\
 
578
%s: option `--%s' doesn't allow an argument\n"),
 
579
                                          argv[0], pfound->name);
 
580
#else
 
581
                          fprintf (stderr, _("\
 
582
%s: option `--%s' doesn't allow an argument\n"),
 
583
                                   argv[0], pfound->name);
 
584
#endif
 
585
                        }
 
586
                      else
 
587
                        {
 
588
                          /* +option or -option */
 
589
#if defined _LIBC && defined USE_IN_LIBIO
 
590
                          n = __asprintf (&buf, _("\
 
591
%s: option `%c%s' doesn't allow an argument\n"),
 
592
                                          argv[0], argv[d->optind - 1][0],
 
593
                                          pfound->name);
 
594
#else
 
595
                          fprintf (stderr, _("\
 
596
%s: option `%c%s' doesn't allow an argument\n"),
 
597
                                   argv[0], argv[d->optind - 1][0],
 
598
                                   pfound->name);
 
599
#endif
 
600
                        }
 
601
 
 
602
#if defined _LIBC && defined USE_IN_LIBIO
 
603
                      if (n >= 0)
 
604
                        {
 
605
                          _IO_flockfile (stderr);
 
606
 
 
607
                          int old_flags2 = ((_IO_FILE *) stderr)->_flags2;
 
608
                          ((_IO_FILE *) stderr)->_flags2
 
609
                            |= _IO_FLAGS2_NOTCANCEL;
 
610
 
 
611
                          __fxprintf (NULL, "%s", buf);
 
612
 
 
613
                          ((_IO_FILE *) stderr)->_flags2 = old_flags2;
 
614
                          _IO_funlockfile (stderr);
 
615
 
 
616
                          free (buf);
 
617
                        }
 
618
#endif
 
619
                    }
 
620
 
 
621
                  d->__nextchar += strlen (d->__nextchar);
 
622
 
 
623
                  d->optopt = pfound->val;
 
624
                  return '?';
 
625
                }
 
626
            }
 
627
          else if (pfound->has_arg == 1)
 
628
            {
 
629
              if (d->optind < argc)
 
630
                d->optarg = argv[d->optind++];
 
631
              else
 
632
                {
 
633
                  if (print_errors)
 
634
                    {
 
635
#if defined _LIBC && defined USE_IN_LIBIO
 
636
                      char *buf;
 
637
 
 
638
                      if (__asprintf (&buf, _("\
 
639
%s: option `%s' requires an argument\n"),
 
640
                                      argv[0], argv[d->optind - 1]) >= 0)
 
641
                        {
 
642
                          _IO_flockfile (stderr);
 
643
 
 
644
                          int old_flags2 = ((_IO_FILE *) stderr)->_flags2;
 
645
                          ((_IO_FILE *) stderr)->_flags2
 
646
                            |= _IO_FLAGS2_NOTCANCEL;
 
647
 
 
648
                          __fxprintf (NULL, "%s", buf);
 
649
 
 
650
                          ((_IO_FILE *) stderr)->_flags2 = old_flags2;
 
651
                          _IO_funlockfile (stderr);
 
652
 
 
653
                          free (buf);
 
654
                        }
 
655
#else
 
656
                      fprintf (stderr,
 
657
                               _("%s: option `%s' requires an argument\n"),
 
658
                               argv[0], argv[d->optind - 1]);
 
659
#endif
 
660
                    }
 
661
                  d->__nextchar += strlen (d->__nextchar);
 
662
                  d->optopt = pfound->val;
 
663
                  return optstring[0] == ':' ? ':' : '?';
 
664
                }
 
665
            }
 
666
          d->__nextchar += strlen (d->__nextchar);
 
667
          if (longind != NULL)
 
668
            *longind = option_index;
 
669
          if (pfound->flag)
 
670
            {
 
671
              *(pfound->flag) = pfound->val;
 
672
              return 0;
 
673
            }
 
674
          return pfound->val;
 
675
        }
 
676
 
 
677
      /* Can't find it as a long option.  If this is not getopt_long_only,
 
678
         or the option starts with '--' or is not a valid short
 
679
         option, then it's an error.
 
680
         Otherwise interpret it as a short option.  */
 
681
      if (!long_only || argv[d->optind][1] == '-'
 
682
          || strchr (optstring, *d->__nextchar) == NULL)
 
683
        {
 
684
          if (print_errors)
 
685
            {
 
686
#if defined _LIBC && defined USE_IN_LIBIO
 
687
              char *buf;
 
688
              int n;
 
689
#endif
 
690
 
 
691
              if (argv[d->optind][1] == '-')
 
692
                {
 
693
                  /* --option */
 
694
#if defined _LIBC && defined USE_IN_LIBIO
 
695
                  n = __asprintf (&buf, _("%s: unrecognized option `--%s'\n"),
 
696
                                  argv[0], d->__nextchar);
 
697
#else
 
698
                  fprintf (stderr, _("%s: unrecognized option `--%s'\n"),
 
699
                           argv[0], d->__nextchar);
 
700
#endif
 
701
                }
 
702
              else
 
703
                {
 
704
                  /* +option or -option */
 
705
#if defined _LIBC && defined USE_IN_LIBIO
 
706
                  n = __asprintf (&buf, _("%s: unrecognized option `%c%s'\n"),
 
707
                                  argv[0], argv[d->optind][0], d->__nextchar);
 
708
#else
 
709
                  fprintf (stderr, _("%s: unrecognized option `%c%s'\n"),
 
710
                           argv[0], argv[d->optind][0], d->__nextchar);
 
711
#endif
 
712
                }
 
713
 
 
714
#if defined _LIBC && defined USE_IN_LIBIO
 
715
              if (n >= 0)
 
716
                {
 
717
                  _IO_flockfile (stderr);
 
718
 
 
719
                  int old_flags2 = ((_IO_FILE *) stderr)->_flags2;
 
720
                  ((_IO_FILE *) stderr)->_flags2 |= _IO_FLAGS2_NOTCANCEL;
 
721
 
 
722
                  __fxprintf (NULL, "%s", buf);
 
723
 
 
724
                  ((_IO_FILE *) stderr)->_flags2 = old_flags2;
 
725
                  _IO_funlockfile (stderr);
 
726
 
 
727
                  free (buf);
 
728
                }
 
729
#endif
 
730
            }
 
731
          d->__nextchar = (char *) "";
 
732
          d->optind++;
 
733
          d->optopt = 0;
 
734
          return '?';
 
735
        }
 
736
    }
 
737
 
 
738
  /* Look at and handle the next short option-character.  */
 
739
 
 
740
  {
 
741
    char c = *d->__nextchar++;
 
742
    char *temp = strchr (optstring, c);
 
743
 
 
744
    /* Increment `optind' when we start to process its last character.  */
 
745
    if (*d->__nextchar == '\0')
 
746
      ++d->optind;
 
747
 
 
748
    if (temp == NULL || c == ':')
 
749
      {
 
750
        if (print_errors)
 
751
          {
 
752
#if defined _LIBC && defined USE_IN_LIBIO
 
753
              char *buf;
 
754
              int n;
 
755
#endif
 
756
 
 
757
            if (d->__posixly_correct)
 
758
              {
 
759
                /* 1003.2 specifies the format of this message.  */
 
760
#if defined _LIBC && defined USE_IN_LIBIO
 
761
                n = __asprintf (&buf, _("%s: illegal option -- %c\n"),
 
762
                                argv[0], c);
 
763
#else
 
764
                fprintf (stderr, _("%s: illegal option -- %c\n"), argv[0], c);
 
765
#endif
 
766
              }
 
767
            else
 
768
              {
 
769
#if defined _LIBC && defined USE_IN_LIBIO
 
770
                n = __asprintf (&buf, _("%s: invalid option -- %c\n"),
 
771
                                argv[0], c);
 
772
#else
 
773
                fprintf (stderr, _("%s: invalid option -- %c\n"), argv[0], c);
 
774
#endif
 
775
              }
 
776
 
 
777
#if defined _LIBC && defined USE_IN_LIBIO
 
778
            if (n >= 0)
 
779
              {
 
780
                _IO_flockfile (stderr);
 
781
 
 
782
                int old_flags2 = ((_IO_FILE *) stderr)->_flags2;
 
783
                ((_IO_FILE *) stderr)->_flags2 |= _IO_FLAGS2_NOTCANCEL;
 
784
 
 
785
                __fxprintf (NULL, "%s", buf);
 
786
 
 
787
                ((_IO_FILE *) stderr)->_flags2 = old_flags2;
 
788
                _IO_funlockfile (stderr);
 
789
 
 
790
                free (buf);
 
791
              }
 
792
#endif
 
793
          }
 
794
        d->optopt = c;
 
795
        return '?';
 
796
      }
 
797
    /* Convenience. Treat POSIX -W foo same as long option --foo */
 
798
    if (temp[0] == 'W' && temp[1] == ';')
 
799
      {
 
800
        char *nameend;
 
801
        const struct option *p;
 
802
        const struct option *pfound = NULL;
 
803
        int exact = 0;
 
804
        int ambig = 0;
 
805
        int indfound = 0;
 
806
        int option_index;
 
807
 
 
808
        /* This is an option that requires an argument.  */
 
809
        if (*d->__nextchar != '\0')
 
810
          {
 
811
            d->optarg = d->__nextchar;
 
812
            /* If we end this ARGV-element by taking the rest as an arg,
 
813
               we must advance to the next element now.  */
 
814
            d->optind++;
 
815
          }
 
816
        else if (d->optind == argc)
 
817
          {
 
818
            if (print_errors)
 
819
              {
 
820
                /* 1003.2 specifies the format of this message.  */
 
821
#if defined _LIBC && defined USE_IN_LIBIO
 
822
                char *buf;
 
823
 
 
824
                if (__asprintf (&buf,
 
825
                                _("%s: option requires an argument -- %c\n"),
 
826
                                argv[0], c) >= 0)
 
827
                  {
 
828
                    _IO_flockfile (stderr);
 
829
 
 
830
                    int old_flags2 = ((_IO_FILE *) stderr)->_flags2;
 
831
                    ((_IO_FILE *) stderr)->_flags2 |= _IO_FLAGS2_NOTCANCEL;
 
832
 
 
833
                    __fxprintf (NULL, "%s", buf);
 
834
 
 
835
                    ((_IO_FILE *) stderr)->_flags2 = old_flags2;
 
836
                    _IO_funlockfile (stderr);
 
837
 
 
838
                    free (buf);
 
839
                  }
 
840
#else
 
841
                fprintf (stderr, _("%s: option requires an argument -- %c\n"),
 
842
                         argv[0], c);
 
843
#endif
 
844
              }
 
845
            d->optopt = c;
 
846
            if (optstring[0] == ':')
 
847
              c = ':';
 
848
            else
 
849
              c = '?';
 
850
            return c;
 
851
          }
 
852
        else
 
853
          /* We already incremented `d->optind' once;
 
854
             increment it again when taking next ARGV-elt as argument.  */
 
855
          d->optarg = argv[d->optind++];
 
856
 
 
857
        /* optarg is now the argument, see if it's in the
 
858
           table of longopts.  */
 
859
 
 
860
        for (d->__nextchar = nameend = d->optarg; *nameend && *nameend != '=';
 
861
             nameend++)
 
862
          /* Do nothing.  */ ;
 
863
 
 
864
        /* Test all long options for either exact match
 
865
           or abbreviated matches.  */
 
866
        for (p = longopts, option_index = 0; p->name; p++, option_index++)
 
867
          if (!strncmp (p->name, d->__nextchar, nameend - d->__nextchar))
 
868
            {
 
869
              if ((unsigned int) (nameend - d->__nextchar) == strlen (p->name))
 
870
                {
 
871
                  /* Exact match found.  */
 
872
                  pfound = p;
 
873
                  indfound = option_index;
 
874
                  exact = 1;
 
875
                  break;
 
876
                }
 
877
              else if (pfound == NULL)
 
878
                {
 
879
                  /* First nonexact match found.  */
 
880
                  pfound = p;
 
881
                  indfound = option_index;
 
882
                }
 
883
              else
 
884
                /* Second or later nonexact match found.  */
 
885
                ambig = 1;
 
886
            }
 
887
        if (ambig && !exact)
 
888
          {
 
889
            if (print_errors)
 
890
              {
 
891
#if defined _LIBC && defined USE_IN_LIBIO
 
892
                char *buf;
 
893
 
 
894
                if (__asprintf (&buf, _("%s: option `-W %s' is ambiguous\n"),
 
895
                                argv[0], argv[d->optind]) >= 0)
 
896
                  {
 
897
                    _IO_flockfile (stderr);
 
898
 
 
899
                    int old_flags2 = ((_IO_FILE *) stderr)->_flags2;
 
900
                    ((_IO_FILE *) stderr)->_flags2 |= _IO_FLAGS2_NOTCANCEL;
 
901
 
 
902
                    __fxprintf (NULL, "%s", buf);
 
903
 
 
904
                    ((_IO_FILE *) stderr)->_flags2 = old_flags2;
 
905
                    _IO_funlockfile (stderr);
 
906
 
 
907
                    free (buf);
 
908
                  }
 
909
#else
 
910
                fprintf (stderr, _("%s: option `-W %s' is ambiguous\n"),
 
911
                         argv[0], argv[d->optind]);
 
912
#endif
 
913
              }
 
914
            d->__nextchar += strlen (d->__nextchar);
 
915
            d->optind++;
 
916
            return '?';
 
917
          }
 
918
        if (pfound != NULL)
 
919
          {
 
920
            option_index = indfound;
 
921
            if (*nameend)
 
922
              {
 
923
                /* Don't test has_arg with >, because some C compilers don't
 
924
                   allow it to be used on enums.  */
 
925
                if (pfound->has_arg)
 
926
                  d->optarg = nameend + 1;
 
927
                else
 
928
                  {
 
929
                    if (print_errors)
 
930
                      {
 
931
#if defined _LIBC && defined USE_IN_LIBIO
 
932
                        char *buf;
 
933
 
 
934
                        if (__asprintf (&buf, _("\
 
935
%s: option `-W %s' doesn't allow an argument\n"),
 
936
                                        argv[0], pfound->name) >= 0)
 
937
                          {
 
938
                            _IO_flockfile (stderr);
 
939
 
 
940
                            int old_flags2 = ((_IO_FILE *) stderr)->_flags2;
 
941
                            ((_IO_FILE *) stderr)->_flags2
 
942
                              |= _IO_FLAGS2_NOTCANCEL;
 
943
 
 
944
                            __fxprintf (NULL, "%s", buf);
 
945
 
 
946
                            ((_IO_FILE *) stderr)->_flags2 = old_flags2;
 
947
                            _IO_funlockfile (stderr);
 
948
 
 
949
                            free (buf);
 
950
                          }
 
951
#else
 
952
                        fprintf (stderr, _("\
 
953
%s: option `-W %s' doesn't allow an argument\n"),
 
954
                                 argv[0], pfound->name);
 
955
#endif
 
956
                      }
 
957
 
 
958
                    d->__nextchar += strlen (d->__nextchar);
 
959
                    return '?';
 
960
                  }
 
961
              }
 
962
            else if (pfound->has_arg == 1)
 
963
              {
 
964
                if (d->optind < argc)
 
965
                  d->optarg = argv[d->optind++];
 
966
                else
 
967
                  {
 
968
                    if (print_errors)
 
969
                      {
 
970
#if defined _LIBC && defined USE_IN_LIBIO
 
971
                        char *buf;
 
972
 
 
973
                        if (__asprintf (&buf, _("\
 
974
%s: option `%s' requires an argument\n"),
 
975
                                        argv[0], argv[d->optind - 1]) >= 0)
 
976
                          {
 
977
                            _IO_flockfile (stderr);
 
978
 
 
979
                            int old_flags2 = ((_IO_FILE *) stderr)->_flags2;
 
980
                            ((_IO_FILE *) stderr)->_flags2
 
981
                              |= _IO_FLAGS2_NOTCANCEL;
 
982
 
 
983
                            __fxprintf (NULL, "%s", buf);
 
984
 
 
985
                            ((_IO_FILE *) stderr)->_flags2 = old_flags2;
 
986
                            _IO_funlockfile (stderr);
 
987
 
 
988
                            free (buf);
 
989
                          }
 
990
#else
 
991
                        fprintf (stderr,
 
992
                                 _("%s: option `%s' requires an argument\n"),
 
993
                                 argv[0], argv[d->optind - 1]);
 
994
#endif
 
995
                      }
 
996
                    d->__nextchar += strlen (d->__nextchar);
 
997
                    return optstring[0] == ':' ? ':' : '?';
 
998
                  }
 
999
              }
 
1000
            d->__nextchar += strlen (d->__nextchar);
 
1001
            if (longind != NULL)
 
1002
              *longind = option_index;
 
1003
            if (pfound->flag)
 
1004
              {
 
1005
                *(pfound->flag) = pfound->val;
 
1006
                return 0;
 
1007
              }
 
1008
            return pfound->val;
 
1009
          }
 
1010
          d->__nextchar = NULL;
 
1011
          return 'W';   /* Let the application handle it.   */
 
1012
      }
 
1013
    if (temp[1] == ':')
 
1014
      {
 
1015
        if (temp[2] == ':')
 
1016
          {
 
1017
            /* This is an option that accepts an argument optionally.  */
 
1018
            if (*d->__nextchar != '\0')
 
1019
              {
 
1020
                d->optarg = d->__nextchar;
 
1021
                d->optind++;
 
1022
              }
 
1023
            else
 
1024
              d->optarg = NULL;
 
1025
            d->__nextchar = NULL;
 
1026
          }
 
1027
        else
 
1028
          {
 
1029
            /* This is an option that requires an argument.  */
 
1030
            if (*d->__nextchar != '\0')
 
1031
              {
 
1032
                d->optarg = d->__nextchar;
 
1033
                /* If we end this ARGV-element by taking the rest as an arg,
 
1034
                   we must advance to the next element now.  */
 
1035
                d->optind++;
 
1036
              }
 
1037
            else if (d->optind == argc)
 
1038
              {
 
1039
                if (print_errors)
 
1040
                  {
 
1041
                    /* 1003.2 specifies the format of this message.  */
 
1042
#if defined _LIBC && defined USE_IN_LIBIO
 
1043
                    char *buf;
 
1044
 
 
1045
                    if (__asprintf (&buf, _("\
 
1046
%s: option requires an argument -- %c\n"),
 
1047
                                    argv[0], c) >= 0)
 
1048
                      {
 
1049
                        _IO_flockfile (stderr);
 
1050
 
 
1051
                        int old_flags2 = ((_IO_FILE *) stderr)->_flags2;
 
1052
                        ((_IO_FILE *) stderr)->_flags2 |= _IO_FLAGS2_NOTCANCEL;
 
1053
 
 
1054
                        __fxprintf (NULL, "%s", buf);
 
1055
 
 
1056
                        ((_IO_FILE *) stderr)->_flags2 = old_flags2;
 
1057
                        _IO_funlockfile (stderr);
 
1058
 
 
1059
                        free (buf);
 
1060
                      }
 
1061
#else
 
1062
                    fprintf (stderr,
 
1063
                             _("%s: option requires an argument -- %c\n"),
 
1064
                             argv[0], c);
 
1065
#endif
 
1066
                  }
 
1067
                d->optopt = c;
 
1068
                if (optstring[0] == ':')
 
1069
                  c = ':';
 
1070
                else
 
1071
                  c = '?';
 
1072
              }
 
1073
            else
 
1074
              /* We already incremented `optind' once;
 
1075
                 increment it again when taking next ARGV-elt as argument.  */
 
1076
              d->optarg = argv[d->optind++];
 
1077
            d->__nextchar = NULL;
 
1078
          }
 
1079
      }
 
1080
    return c;
 
1081
  }
 
1082
}
 
1083
 
 
1084
int
 
1085
_getopt_internal (int argc, char **argv, const char *optstring,
 
1086
                  const struct option *longopts, int *longind,
 
1087
                  int long_only, int posixly_correct)
 
1088
{
 
1089
  int result;
 
1090
 
 
1091
  getopt_data.optind = optind;
 
1092
  getopt_data.opterr = opterr;
 
1093
 
 
1094
  result = _getopt_internal_r (argc, argv, optstring, longopts, longind,
 
1095
                               long_only, posixly_correct, &getopt_data);
 
1096
 
 
1097
  optind = getopt_data.optind;
 
1098
  optarg = getopt_data.optarg;
 
1099
  optopt = getopt_data.optopt;
 
1100
 
 
1101
  return result;
 
1102
}
 
1103
 
 
1104
/* glibc gets a LSB-compliant getopt.
 
1105
   Standalone applications get a POSIX-compliant getopt.  */
 
1106
#ifdef _LIBC
 
1107
enum { POSIXLY_CORRECT = 0 };
 
1108
#else
 
1109
enum { POSIXLY_CORRECT = 1 };
 
1110
#endif
 
1111
 
 
1112
int
 
1113
getopt (int argc, char *const *argv, const char *optstring)
 
1114
{
 
1115
  return _getopt_internal (argc, (char **) argv, optstring, NULL, NULL, 0,
 
1116
                           POSIXLY_CORRECT);
 
1117
}
 
1118
 
 
1119
 
 
1120
#ifdef TEST
 
1121
 
 
1122
/* Compile with -DTEST to make an executable for use in testing
 
1123
   the above definition of `getopt'.  */
 
1124
 
 
1125
int
 
1126
main (int argc, char **argv)
 
1127
{
 
1128
  int c;
 
1129
  int digit_optind = 0;
 
1130
 
 
1131
  while (1)
 
1132
    {
 
1133
      int this_option_optind = optind ? optind : 1;
 
1134
 
 
1135
      c = getopt (argc, argv, "abc:d:0123456789");
 
1136
      if (c == -1)
 
1137
        break;
 
1138
 
 
1139
      switch (c)
 
1140
        {
 
1141
        case '0':
 
1142
        case '1':
 
1143
        case '2':
 
1144
        case '3':
 
1145
        case '4':
 
1146
        case '5':
 
1147
        case '6':
 
1148
        case '7':
 
1149
        case '8':
 
1150
        case '9':
 
1151
          if (digit_optind != 0 && digit_optind != this_option_optind)
 
1152
            printf ("digits occur in two different argv-elements.\n");
 
1153
          digit_optind = this_option_optind;
 
1154
          printf ("option %c\n", c);
 
1155
          break;
 
1156
 
 
1157
        case 'a':
 
1158
          printf ("option a\n");
 
1159
          break;
 
1160
 
 
1161
        case 'b':
 
1162
          printf ("option b\n");
 
1163
          break;
 
1164
 
 
1165
        case 'c':
 
1166
          printf ("option c with value `%s'\n", optarg);
 
1167
          break;
 
1168
 
 
1169
        case '?':
 
1170
          break;
 
1171
 
 
1172
        default:
 
1173
          printf ("?? getopt returned character code 0%o ??\n", c);
 
1174
        }
 
1175
    }
 
1176
 
 
1177
  if (optind < argc)
 
1178
    {
 
1179
      printf ("non-option ARGV-elements: ");
 
1180
      while (optind < argc)
 
1181
        printf ("%s ", argv[optind++]);
 
1182
      printf ("\n");
 
1183
    }
 
1184
 
 
1185
  exit (0);
 
1186
}
 
1187
 
 
1188
#endif /* TEST */