cb493d8 by Michal Čihař at 2009-08-25 1
/* Note: This file has been created from getopt.c and getopt1.c in order
2
   to have only one file called the same as the function it implements
3
   (getopt_long) by David Necas <yeti@physics.muni.cz>.  During that, some
4
   stuff not needed when building standalone getopt has been removed.  No
5
   other changes have been made.  */
6
/* Getopt for GNU.
7
   NOTE: getopt is now part of the C library, so if you don't know what
8
   "Keep this file name-space clean" means, talk to drepper@gnu.org
9
   before changing it!
10
11
   Copyright (C) 1987, 88, 89, 90, 91, 92, 93, 94, 95, 96, 97, 98, 99, 2000
12
   	Free Software Foundation, Inc.
13
14
   The GNU C Library is free software; you can redistribute it and/or
15
   modify it under the terms of the GNU Library General Public License as
16
   published by the Free Software Foundation; either version 2 of the
17
   License, or (at your option) any later version.
18
19
   The GNU C Library is distributed in the hope that it will be useful,
20
   but WITHOUT ANY WARRANTY; without even the implied warranty of
21
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
22
   Library General Public License for more details.
23
24
   You should have received a copy of the GNU Library General Public
25
   License along with the GNU C Library; see the file COPYING.LIB.  If not,
26
   write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
27
   Boston, MA 02111-1307, USA.  */
28

29
/* This tells Alpha OSF/1 not to define a getopt prototype in <stdio.h>.
30
   Ditto for AIX 3.2 and <stdlib.h>.  */
31
#ifndef _NO_PROTO
32
# define _NO_PROTO
33
#endif
34
35
#ifdef HAVE_CONFIG_H
36
# include "config.h"
37
#endif /* HAVE_CONFIG_H */
38
39
#ifndef NULL
40
#define NULL 0
41
#endif
42
43
#if !defined __STDC__ || !__STDC__
44
/* This is a separate conditional since some stdc systems
45
   reject `defined (const)'.  */
46
# ifndef const
47
#  define const
48
# endif
49
#endif
50
51
#include <stdio.h>
52
53
/* This needs to come after some library #include
54
   to get __GNU_LIBRARY__ defined.  */
55
#ifdef	__GNU_LIBRARY__
56
/* Don't include stdlib.h for non-GNU C libraries because some of them
57
   contain conflicting prototypes for getopt.  */
58
# include <stdlib.h>
59
# include <unistd.h>
60
#endif	/* GNU C library.  */
61
62
#ifdef VMS
63
# include <unixlib.h>
64
# if HAVE_STRING_H - 0
65
#  include <string.h>
66
# endif
67
#endif
68
69
#ifndef _
70
/* This is for other GNU distributions with internationalized messages.  */
71
# if defined HAVE_LIBINTL_H
72
#  include <libintl.h>
73
#  ifndef _
74
#   define _(msgid)	gettext (msgid)
75
#  endif
76
# else
77
#  define _(msgid)	(msgid)
78
# endif
79
#endif
80
81
/* This version of `getopt' appears to the caller like standard Unix `getopt'
82
   but it behaves differently for the user, since it allows the user
83
   to intersperse the options with the other arguments.
84
85
   As `getopt' works, it permutes the elements of ARGV so that,
86
   when it is done, all the options precede everything else.  Thus
87
   all application programs are extended to handle flexible argument order.
88
89
   Setting the environment variable POSIXLY_CORRECT disables permutation.
90
   Then the behavior is completely standard.
91
92
   GNU application programs can use a third alternative mode in which
93
   they can distinguish the relative order of options and other arguments.  */
94
95
#include "getopt.h"
96
97
/* For communication from `getopt' to the caller.
98
   When `getopt' finds an option that takes an argument,
99
   the argument value is returned here.
100
   Also, when `ordering' is RETURN_IN_ORDER,
101
   each non-option ARGV-element is returned here.  */
102
103
char *optarg;
104
105
/* Index in ARGV of the next element to be scanned.
106
   This is used for communication to and from the caller
107
   and for communication between successive calls to `getopt'.
108
109
   On entry to `getopt', zero means this is the first call; initialize.
110
111
   When `getopt' returns -1, this is the index of the first of the
112
   non-option elements that the caller should itself scan.
113
114
   Otherwise, `optind' communicates from one call to the next
115
   how much of ARGV has been scanned so far.  */
116
117
/* 1003.2 says this must be 1 before any call.  */
118
int optind = 1;
119
120
/* Formerly, initialization of getopt depended on optind==0, which
121
   causes problems with re-calling getopt as programs generally don't
122
   know that. */
123
124
int __getopt_initialized;
125
126
/* The next char to be scanned in the option-element
127
   in which the last option character we returned was found.
128
   This allows us to pick up the scan where we left off.
129
130
   If this is zero, or a null string, it means resume the scan
131
   by advancing to the next ARGV-element.  */
132
133
static char *nextchar;
134
135
/* Callers store zero here to inhibit the error message
136
   for unrecognized options.  */
137
138
int opterr = 1;
139
140
/* Set to an option character which was unrecognized.
141
   This must be initialized on some systems to avoid linking in the
142
   system's own getopt implementation.  */
143
144
int optopt = '?';
145
146
/* Describe how to deal with options that follow non-option ARGV-elements.
147
148
   If the caller did not specify anything,
149
   the default is REQUIRE_ORDER if the environment variable
150
   POSIXLY_CORRECT is defined, PERMUTE otherwise.
151
152
   REQUIRE_ORDER means don't recognize them as options;
153
   stop option processing when the first non-option is seen.
154
   This is what Unix does.
155
   This mode of operation is selected by either setting the environment
156
   variable POSIXLY_CORRECT, or using `+' as the first character
157
   of the list of option characters.
158
159
   PERMUTE is the default.  We permute the contents of ARGV as we scan,
160
   so that eventually all the non-options are at the end.  This allows options
161
   to be given in any order, even with programs that were not written to
162
   expect this.
163
164
   RETURN_IN_ORDER is an option available to programs that were written
165
   to expect options and other ARGV-elements in any order and that care about
166
   the ordering of the two.  We describe each non-option ARGV-element
167
   as if it were the argument of an option with character code 1.
168
   Using `-' as the first character of the list of option characters
169
   selects this mode of operation.
170
171
   The special argument `--' forces an end of option-scanning regardless
172
   of the value of `ordering'.  In the case of RETURN_IN_ORDER, only
173
   `--' can cause `getopt' to return -1 with `optind' != ARGC.  */
174
175
static enum
176
{
177
  REQUIRE_ORDER, PERMUTE, RETURN_IN_ORDER
178
} ordering;
179
180
/* Value of POSIXLY_CORRECT environment variable.  */
181
static char *posixly_correct;
182

183
#ifdef	__GNU_LIBRARY__
184
/* We want to avoid inclusion of string.h with non-GNU libraries
185
   because there are many ways it can cause trouble.
186
   On some systems, it contains special magic macros that don't work
187
   in GCC.  */
188
# include <string.h>
189
# define my_index	strchr
190
#else
191
192
# if HAVE_STRING_H
193
#  include <string.h>
194
# else
195
#  include <strings.h>
196
# endif
197
198
/* Avoid depending on library functions or files
199
   whose names are inconsistent.  */
200
201
#ifndef getenv
202
extern char *getenv ();
203
#endif
204
205
static char *
206
my_index (str, chr)
207
     const char *str;
208
     int chr;
209
{
210
  while (*str)
211
    {
212
      if (*str == chr)
213
	return (char *) str;
214
      str++;
215
    }
216
  return 0;
217
}
218
219
/* If using GCC, we can safely declare strlen this way.
220
   If not using GCC, it is ok not to declare it.  */
221
#ifdef __GNUC__
222
/* Note that Motorola Delta 68k R3V7 comes with GCC but not stddef.h.
223
   That was relevant to code that was here before.  */
224
# if (!defined __STDC__ || !__STDC__) && !defined strlen
225
/* gcc with -traditional declares the built-in strlen to return int,
226
   and has done so at least since version 2.4.5. -- rms.  */
227
extern int strlen (const char *);
228
# endif /* not __STDC__ */
229
#endif /* __GNUC__ */
230
231
#endif /* not __GNU_LIBRARY__ */
232

233
/* Handle permutation of arguments.  */
234
235
/* Describe the part of ARGV that contains non-options that have
236
   been skipped.  `first_nonopt' is the index in ARGV of the first of them;
237
   `last_nonopt' is the index after the last of them.  */
238
239
static int first_nonopt;
240
static int last_nonopt;
241
242
#define SWAP_FLAGS(ch1, ch2)
243
244
/* Exchange two adjacent subsequences of ARGV.
245
   One subsequence is elements [first_nonopt,last_nonopt)
246
   which contains all the non-options that have been skipped so far.
247
   The other is elements [last_nonopt,optind), which contains all
248
   the options processed since those non-options were skipped.
249
250
   `first_nonopt' and `last_nonopt' are relocated so that they describe
251
   the new indices of the non-options in ARGV after they are moved.  */
252
253
#if defined __STDC__ && __STDC__
254
static void exchange (char **);
255
#endif
256
257
static void
258
exchange (argv)
259
     char **argv;
260
{
261
  int bottom = first_nonopt;
262
  int middle = last_nonopt;
263
  int top = optind;
264
  char *tem;
265
266
  /* Exchange the shorter segment with the far end of the longer segment.
267
     That puts the shorter segment into the right place.
268
     It leaves the longer segment in the right place overall,
269
     but it consists of two parts that need to be swapped next.  */
270
271
  while (top > middle && middle > bottom)
272
    {
273
      if (top - middle > middle - bottom)
274
	{
275
	  /* Bottom segment is the short one.  */
276
	  int len = middle - bottom;
277
	  register int i;
278
279
	  /* Swap it with the top part of the top segment.  */
280
	  for (i = 0; i < len; i++)
281
	    {
282
	      tem = argv[bottom + i];
283
	      argv[bottom + i] = argv[top - (middle - bottom) + i];
284
	      argv[top - (middle - bottom) + i] = tem;
285
	      SWAP_FLAGS (bottom + i, top - (middle - bottom) + i);
286
	    }
287
	  /* Exclude the moved bottom segment from further swapping.  */
288
	  top -= len;
289
	}
290
      else
291
	{
292
	  /* Top segment is the short one.  */
293
	  int len = top - middle;
294
	  register int i;
295
296
	  /* Swap it with the bottom part of the bottom segment.  */
297
	  for (i = 0; i < len; i++)
298
	    {
299
	      tem = argv[bottom + i];
300
	      argv[bottom + i] = argv[middle + i];
301
	      argv[middle + i] = tem;
302
	      SWAP_FLAGS (bottom + i, middle + i);
303
	    }
304
	  /* Exclude the moved top segment from further swapping.  */
305
	  bottom += len;
306
	}
307
    }
308
309
  /* Update records for the slots the non-options now occupy.  */
310
311
  first_nonopt += (optind - last_nonopt);
312
  last_nonopt = optind;
313
}
314
315
/* Initialize the internal data when the first call is made.  */
316
317
#if defined __STDC__ && __STDC__
318
static const char *_getopt_initialize (int, char *const *, const char *);
319
#endif
320
static const char *
321
_getopt_initialize (argc, argv, optstring)
322
     int argc;
323
     char *const *argv;
324
     const char *optstring;
325
{
326
  /* Start processing options with ARGV-element 1 (since ARGV-element 0
327
     is the program name); the sequence of previously skipped
328
     non-option ARGV-elements is empty.  */
329
330
  first_nonopt = last_nonopt = optind;
331
332
  nextchar = NULL;
333
334
  posixly_correct = getenv ("POSIXLY_CORRECT");
335
336
  /* Determine how to handle the ordering of options and nonoptions.  */
337
338
  if (optstring[0] == '-')
339
    {
340
      ordering = RETURN_IN_ORDER;
341
      ++optstring;
342
    }
343
  else if (optstring[0] == '+')
344
    {
345
      ordering = REQUIRE_ORDER;
346
      ++optstring;
347
    }
348
  else if (posixly_correct != NULL)
349
    ordering = REQUIRE_ORDER;
350
  else
351
    ordering = PERMUTE;
352
353
  return optstring;
354
}
355

356
/* Scan elements of ARGV (whose length is ARGC) for option characters
357
   given in OPTSTRING.
358
359
   If an element of ARGV starts with '-', and is not exactly "-" or "--",
360
   then it is an option element.  The characters of this element
361
   (aside from the initial '-') are option characters.  If `getopt'
362
   is called repeatedly, it returns successively each of the option characters
363
   from each of the option elements.
364
365
   If `getopt' finds another option character, it returns that character,
366
   updating `optind' and `nextchar' so that the next call to `getopt' can
367
   resume the scan with the following option character or ARGV-element.
368
369
   If there are no more option characters, `getopt' returns -1.
370
   Then `optind' is the index in ARGV of the first ARGV-element
371
   that is not an option.  (The ARGV-elements have been permuted
372
   so that those that are not options now come last.)
373
374
   OPTSTRING is a string containing the legitimate option characters.
375
   If an option character is seen that is not listed in OPTSTRING,
376
   return '?' after printing an error message.  If you set `opterr' to
377
   zero, the error message is suppressed but we still return '?'.
378
379
   If a char in OPTSTRING is followed by a colon, that means it wants an arg,
380
   so the following text in the same ARGV-element, or the text of the following
381
   ARGV-element, is returned in `optarg'.  Two colons mean an option that
382
   wants an optional arg; if there is text in the current ARGV-element,
383
   it is returned in `optarg', otherwise `optarg' is set to zero.
384
385
   If OPTSTRING starts with `-' or `+', it requests different methods of
386
   handling the non-option ARGV-elements.
387
   See the comments about RETURN_IN_ORDER and REQUIRE_ORDER, above.
388
389
   Long-named options begin with `--' instead of `-'.
390
   Their names may be abbreviated as long as the abbreviation is unique
391
   or is an exact match for some defined option.  If they have an
392
   argument, it follows the option name in the same ARGV-element, separated
393
   from the option name by a `=', or else the in next ARGV-element.
394
   When `getopt' finds a long-named option, it returns 0 if that option's
395
   `flag' field is nonzero, the value of the option's `val' field
396
   if the `flag' field is zero.
397
398
   The elements of ARGV aren't really const, because we permute them.
399
   But we pretend they're const in the prototype to be compatible
400
   with other systems.
401
402
   LONGOPTS is a vector of `struct option' terminated by an
403
   element containing a name which is zero.
404
405
   LONGIND returns the index in LONGOPT of the long-named option found.
406
   It is only valid when a long-named option has been found by the most
407
   recent call.
408
409
   If LONG_ONLY is nonzero, '-' as well as '--' can introduce
410
   long-named options.  */
411
412
int
413
_getopt_internal (argc, argv, optstring, longopts, longind, long_only)
414
     int argc;
415
     char *const *argv;
416
     const char *optstring;
417
     const struct option *longopts;
418
     int *longind;
419
     int long_only;
420
{
421
  int print_errors = opterr;
422
  if (optstring[0] == ':')
423
    print_errors = 0;
424
425
  if (argc < 1)
426
    return -1;
427
428
  optarg = NULL;
429
430
  if (optind == 0 || !__getopt_initialized)
431
    {
432
      if (optind == 0)
433
	optind = 1;	/* Don't scan ARGV[0], the program name.  */
434
      optstring = _getopt_initialize (argc, argv, optstring);
435
      __getopt_initialized = 1;
436
    }
437
438
  /* Test whether ARGV[optind] points to a non-option argument.
439
     Either it does not have option syntax, or there is an environment flag
440
     from the shell indicating it is not an option.  The later information
441
     is only used when the used in the GNU libc.  */
442
#define NONOPTION_P (argv[optind][0] != '-' || argv[optind][1] == '\0')
443
444
  if (nextchar == NULL || *nextchar == '\0')
445
    {
446
      /* Advance to the next ARGV-element.  */
447
448
      /* Give FIRST_NONOPT & LAST_NONOPT rational values if OPTIND has been
449
	 moved back by the user (who may also have changed the arguments).  */
450
      if (last_nonopt > optind)
451
	last_nonopt = optind;
452
      if (first_nonopt > optind)
453
	first_nonopt = optind;
454
455
      if (ordering == PERMUTE)
456
	{
457
	  /* If we have just processed some options following some non-options,
458
	     exchange them so that the options come first.  */
459
460
	  if (first_nonopt != last_nonopt && last_nonopt != optind)
461
	    exchange ((char **) argv);
462
	  else if (last_nonopt != optind)
463
	    first_nonopt = optind;
464
465
	  /* Skip any additional non-options
466
	     and extend the range of non-options previously skipped.  */
467
468
	  while (optind < argc && NONOPTION_P)
469
	    optind++;
470
	  last_nonopt = optind;
471
	}
472
473
      /* The special ARGV-element `--' means premature end of options.
474
	 Skip it like a null option,
475
	 then exchange with previous non-options as if it were an option,
476
	 then skip everything else like a non-option.  */
477
478
      if (optind != argc && !strcmp (argv[optind], "--"))
479
	{
480
	  optind++;
481
482
	  if (first_nonopt != last_nonopt && last_nonopt != optind)
483
	    exchange ((char **) argv);
484
	  else if (first_nonopt == last_nonopt)
485
	    first_nonopt = optind;
486
	  last_nonopt = argc;
487
488
	  optind = argc;
489
	}
490
491
      /* If we have done all the ARGV-elements, stop the scan
492
	 and back over any non-options that we skipped and permuted.  */
493
494
      if (optind == argc)
495
	{
496
	  /* Set the next-arg-index to point at the non-options
497
	     that we previously skipped, so the caller will digest them.  */
498
	  if (first_nonopt != last_nonopt)
499
	    optind = first_nonopt;
500
	  return -1;
501
	}
502
503
      /* If we have come to a non-option and did not permute it,
504
	 either stop the scan or describe it to the caller and pass it by.  */
505
506
      if (NONOPTION_P)
507
	{
508
	  if (ordering == REQUIRE_ORDER)
509
	    return -1;
510
	  optarg = argv[optind++];
511
	  return 1;
512
	}
513
514
      /* We have found another option-ARGV-element.
515
	 Skip the initial punctuation.  */
516
517
      nextchar = (argv[optind] + 1
518
		  + (longopts != NULL && argv[optind][1] == '-'));
519
    }
520
521
  /* Decode the current option-ARGV-element.  */
522
523
  /* Check whether the ARGV-element is a long option.
524
525
     If long_only and the ARGV-element has the form "-f", where f is
526
     a valid short option, don't consider it an abbreviated form of
527
     a long option that starts with f.  Otherwise there would be no
528
     way to give the -f short option.
529
530
     On the other hand, if there's a long option "fubar" and
531
     the ARGV-element is "-fu", do consider that an abbreviation of
532
     the long option, just like "--fu", and not "-f" with arg "u".
533
534
     This distinction seems to be the most useful approach.  */
535
536
  if (longopts != NULL
537
      && (argv[optind][1] == '-'
538
	  || (long_only && (argv[optind][2] || !my_index (optstring, argv[optind][1])))))
539
    {
540
      char *nameend;
541
      const struct option *p;
542
      const struct option *pfound = NULL;
543
      int exact = 0;
544
      int ambig = 0;
545
      int indfound = -1;
546
      int option_index;
547
548
      for (nameend = nextchar; *nameend && *nameend != '='; nameend++)
549
	/* Do nothing.  */ ;
550
551
      /* Test all long options for either exact match
552
	 or abbreviated matches.  */
553
      for (p = longopts, option_index = 0; p->name; p++, option_index++)
554
	if (!strncmp (p->name, nextchar, nameend - nextchar))
555
	  {
556
	    if ((unsigned int) (nameend - nextchar)
557
		== (unsigned int) strlen (p->name))
558
	      {
559
		/* Exact match found.  */
560
		pfound = p;
561
		indfound = option_index;
562
		exact = 1;
563
		break;
564
	      }
565
	    else if (pfound == NULL)
566
	      {
567
		/* First nonexact match found.  */
568
		pfound = p;
569
		indfound = option_index;
570
	      }
571
	    else if (long_only
572
		     || pfound->has_arg != p->has_arg
573
		     || pfound->flag != p->flag
574
		     || pfound->val != p->val)
575
	      /* Second or later nonexact match found.  */
576
	      ambig = 1;
577
	  }
578
579
      if (ambig && !exact)
580
	{
581
	  if (print_errors)
582
	    fprintf (stderr, _("%s: option `%s' is ambiguous\n"),
583
		     argv[0], argv[optind]);
584
	  nextchar += strlen (nextchar);
585
	  optind++;
586
	  optopt = 0;
587
	  return '?';
588
	}
589
590
      if (pfound != NULL)
591
	{
592
	  option_index = indfound;
593
	  optind++;
594
	  if (*nameend)
595
	    {
596
	      /* Don't test has_arg with >, because some C compilers don't
597
		 allow it to be used on enums.  */
598
	      if (pfound->has_arg)
599
		optarg = nameend + 1;
600
	      else
601
		{
602
		  if (print_errors)
603
		    {
604
		      if (argv[optind - 1][1] == '-')
605
			/* --option */
606
			fprintf (stderr,
607
				 _("%s: option `--%s' doesn't allow an argument\n"),
608
				 argv[0], pfound->name);
609
		      else
610
			/* +option or -option */
611
			fprintf (stderr,
612
				 _("%s: option `%c%s' doesn't allow an argument\n"),
613
				 argv[0], argv[optind - 1][0], pfound->name);
614
		    }
615
616
		  nextchar += strlen (nextchar);
617
618
		  optopt = pfound->val;
619
		  return '?';
620
		}
621
	    }
622
	  else if (pfound->has_arg == 1)
623
	    {
624
	      if (optind < argc)
625
		optarg = argv[optind++];
626
	      else
627
		{
628
		  if (print_errors)
629
		    fprintf (stderr,
630
			   _("%s: option `%s' requires an argument\n"),
631
			   argv[0], argv[optind - 1]);
632
		  nextchar += strlen (nextchar);
633
		  optopt = pfound->val;
634
		  return optstring[0] == ':' ? ':' : '?';
635
		}
636
	    }
637
	  nextchar += strlen (nextchar);
638
	  if (longind != NULL)
639
	    *longind = option_index;
640
	  if (pfound->flag)
641
	    {
642
	      *(pfound->flag) = pfound->val;
643
	      return 0;
644
	    }
645
	  return pfound->val;
646
	}
647
648
      /* Can't find it as a long option.  If this is not getopt_long_only,
649
	 or the option starts with '--' or is not a valid short
650
	 option, then it's an error.
651
	 Otherwise interpret it as a short option.  */
652
      if (!long_only || argv[optind][1] == '-'
653
	  || my_index (optstring, *nextchar) == NULL)
654
	{
655
	  if (print_errors)
656
	    {
657
	      if (argv[optind][1] == '-')
658
		/* --option */
659
		fprintf (stderr, _("%s: unrecognized option `--%s'\n"),
660
			 argv[0], nextchar);
661
	      else
662
		/* +option or -option */
663
		fprintf (stderr, _("%s: unrecognized option `%c%s'\n"),
664
			 argv[0], argv[optind][0], nextchar);
665
	    }
666
	  nextchar = (char *) "";
667
	  optind++;
668
	  optopt = 0;
669
	  return '?';
670
	}
671
    }
672
673
  /* Look at and handle the next short option-character.  */
674
675
  {
676
    char c = *nextchar++;
677
    char *temp = my_index (optstring, c);
678
679
    /* Increment `optind' when we start to process its last character.  */
680
    if (*nextchar == '\0')
681
      ++optind;
682
683
    if (temp == NULL || c == ':')
684
      {
685
	if (print_errors)
686
	  {
687
	    if (posixly_correct)
688
	      /* 1003.2 specifies the format of this message.  */
689
	      fprintf (stderr, _("%s: illegal option -- %c\n"),
690
		       argv[0], c);
691
	    else
692
	      fprintf (stderr, _("%s: invalid option -- %c\n"),
693
		       argv[0], c);
694
	  }
695
	optopt = c;
696
	return '?';
697
      }
698
    /* Convenience. Treat POSIX -W foo same as long option --foo */
699
    if (temp[0] == 'W' && temp[1] == ';')
700
      {
701
	char *nameend;
702
	const struct option *p;
703
	const struct option *pfound = NULL;
704
	int exact = 0;
705
	int ambig = 0;
706
	int indfound = 0;
707
	int option_index;
708
709
	/* This is an option that requires an argument.  */
710
	if (*nextchar != '\0')
711
	  {
712
	    optarg = nextchar;
713
	    /* If we end this ARGV-element by taking the rest as an arg,
714
	       we must advance to the next element now.  */
715
	    optind++;
716
	  }
717
	else if (optind == argc)
718
	  {
719
	    if (print_errors)
720
	      {
721
		/* 1003.2 specifies the format of this message.  */
722
		fprintf (stderr, _("%s: option requires an argument -- %c\n"),
723
			 argv[0], c);
724
	      }
725
	    optopt = c;
726
	    if (optstring[0] == ':')
727
	      c = ':';
728
	    else
729
	      c = '?';
730
	    return c;
731
	  }
732
	else
733
	  /* We already incremented `optind' once;
734
	     increment it again when taking next ARGV-elt as argument.  */
735
	  optarg = argv[optind++];
736
737
	/* optarg is now the argument, see if it's in the
738
	   table of longopts.  */
739
740
	for (nextchar = nameend = optarg; *nameend && *nameend != '='; nameend++)
741
	  /* Do nothing.  */ ;
742
743
	/* Test all long options for either exact match
744
	   or abbreviated matches.  */
745
	for (p = longopts, option_index = 0; p->name; p++, option_index++)
746
	  if (!strncmp (p->name, nextchar, nameend - nextchar))
747
	    {
748
	      if ((unsigned int) (nameend - nextchar) == strlen (p->name))
749
		{
750
		  /* Exact match found.  */
751
		  pfound = p;
752
		  indfound = option_index;
753
		  exact = 1;
754
		  break;
755
		}
756
	      else if (pfound == NULL)
757
		{
758
		  /* First nonexact match found.  */
759
		  pfound = p;
760
		  indfound = option_index;
761
		}
762
	      else
763
		/* Second or later nonexact match found.  */
764
		ambig = 1;
765
	    }
766
	if (ambig && !exact)
767
	  {
768
	    if (print_errors)
769
	      fprintf (stderr, _("%s: option `-W %s' is ambiguous\n"),
770
		       argv[0], argv[optind]);
771
	    nextchar += strlen (nextchar);
772
	    optind++;
773
	    return '?';
774
	  }
775
	if (pfound != NULL)
776
	  {
777
	    option_index = indfound;
778
	    if (*nameend)
779
	      {
780
		/* Don't test has_arg with >, because some C compilers don't
781
		   allow it to be used on enums.  */
782
		if (pfound->has_arg)
783
		  optarg = nameend + 1;
784
		else
785
		  {
786
		    if (print_errors)
787
		      fprintf (stderr, _("\
788
%s: option `-W %s' doesn't allow an argument\n"),
789
			       argv[0], pfound->name);
790
791
		    nextchar += strlen (nextchar);
792
		    return '?';
793
		  }
794
	      }
795
	    else if (pfound->has_arg == 1)
796
	      {
797
		if (optind < argc)
798
		  optarg = argv[optind++];
799
		else
800
		  {
801
		    if (print_errors)
802
		      fprintf (stderr,
803
			       _("%s: option `%s' requires an argument\n"),
804
			       argv[0], argv[optind - 1]);
805
		    nextchar += strlen (nextchar);
806
		    return optstring[0] == ':' ? ':' : '?';
807
		  }
808
	      }
809
	    nextchar += strlen (nextchar);
810
	    if (longind != NULL)
811
	      *longind = option_index;
812
	    if (pfound->flag)
813
	      {
814
		*(pfound->flag) = pfound->val;
815
		return 0;
816
	      }
817
	    return pfound->val;
818
	  }
819
	  nextchar = NULL;
820
	  return 'W';	/* Let the application handle it.   */
821
      }
822
    if (temp[1] == ':')
823
      {
824
	if (temp[2] == ':')
825
	  {
826
	    /* This is an option that accepts an argument optionally.  */
827
	    if (*nextchar != '\0')
828
	      {
829
		optarg = nextchar;
830
		optind++;
831
	      }
832
	    else
833
	      optarg = NULL;
834
	    nextchar = NULL;
835
	  }
836
	else
837
	  {
838
	    /* This is an option that requires an argument.  */
839
	    if (*nextchar != '\0')
840
	      {
841
		optarg = nextchar;
842
		/* If we end this ARGV-element by taking the rest as an arg,
843
		   we must advance to the next element now.  */
844
		optind++;
845
	      }
846
	    else if (optind == argc)
847
	      {
848
		if (print_errors)
849
		  {
850
		    /* 1003.2 specifies the format of this message.  */
851
		    fprintf (stderr,
852
			     _("%s: option requires an argument -- %c\n"),
853
			     argv[0], c);
854
		  }
855
		optopt = c;
856
		if (optstring[0] == ':')
857
		  c = ':';
858
		else
859
		  c = '?';
860
	      }
861
	    else
862
	      /* We already incremented `optind' once;
863
		 increment it again when taking next ARGV-elt as argument.  */
864
	      optarg = argv[optind++];
865
	    nextchar = NULL;
866
	  }
867
      }
868
    return c;
869
  }
870
}
871
872
int
873
getopt (argc, argv, optstring)
874
     int argc;
875
     char *const *argv;
876
     const char *optstring;
877
{
878
  return _getopt_internal (argc, argv, optstring,
879
			   (const struct option *) 0,
880
			   (int *) 0,
881
			   0);
882
}
883
884
int
885
getopt_long (argc, argv, options, long_options, opt_index)
886
     int argc;
887
     char *const *argv;
888
     const char *options;
889
     const struct option *long_options;
890
     int *opt_index;
891
{
892
  return _getopt_internal (argc, argv, options, long_options, opt_index, 0);
893
}
894
895
/* Like getopt_long, but '-' as well as '--' can indicate a long option.
896
   If an option that starts with '-' (not '--') doesn't match a long option,
897
   but does match a short option, it is parsed as a short option
898
   instead.  */
899
900
int
901
getopt_long_only (argc, argv, options, long_options, opt_index)
902
     int argc;
903
     char *const *argv;
904
     const char *options;
905
     const struct option *long_options;
906
     int *opt_index;
907
{
908
  return _getopt_internal (argc, argv, options, long_options, opt_index, 1);
909
}
910
911