nexmon – Blame information for rev 1

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