comparison mupdf-source/thirdparty/zint/getopt/getopt.c @ 2:b50eed0cc0ef upstream

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