source: trunk/texinfo/lib/getopt.c@ 2787

Last change on this file since 2787 was 2617, checked in by bird, 19 years ago

GNU Texinfo 4.8

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