2 * Copyright (c) 1987, 1993, 1994, 1996
3 * The Regents of the University of California. All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 3. All advertising materials mentioning features or use of this software
14 * must display the following acknowledgement:
15 * This product includes software developed by the University of
16 * California, Berkeley and its contributors.
17 * 4. Neither the name of the University nor the names of its contributors
18 * may be used to endorse or promote products derived from this software
19 * without specific prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
51 #define my_index strchr
54 # ifdef USE_NONOPTION_FLAGS
55 # define SWAP_FLAGS(ch1, ch2) \
56 if (nonoption_flags_len > 0) \
58 char __tmp = __getopt_nonoption_flags[ch1]; \
59 __getopt_nonoption_flags[ch1] = __getopt_nonoption_flags[ch2]; \
60 __getopt_nonoption_flags[ch2] = __tmp; \
63 # define SWAP_FLAGS(ch1, ch2)
66 # define SWAP_FLAGS(ch1, ch2)
69 int __getopt_initialized
;
71 static int first_nonopt
= -1;
72 static int last_nonopt
= -1;
74 static char *nextchar
;
76 static char *posixly_correct
;
89 int bottom
= first_nonopt
;
90 int middle
= last_nonopt
;
94 /* Exchange the shorter segment with the far end of the longer segment.
95 That puts the shorter segment into the right place.
96 It leaves the longer segment in the right place overall,
97 but it consists of two parts that need to be swapped next. */
99 #if defined _LIBC && defined USE_NONOPTION_FLAGS
100 /* First make sure the handling of the `__getopt_nonoption_flags'
101 string can work normally. Our top argument must be in the range
103 if (nonoption_flags_len
> 0 && top
>= nonoption_flags_max_len
)
105 /* We must extend the array. The user plays games with us and
106 presents new arguments. */
107 char *new_str
= malloc (top
+ 1);
109 nonoption_flags_len
= nonoption_flags_max_len
= 0;
112 memset (__mempcpy (new_str
, __getopt_nonoption_flags
,
113 nonoption_flags_max_len
),
114 '\0', top
+ 1 - nonoption_flags_max_len
);
115 nonoption_flags_max_len
= top
+ 1;
116 __getopt_nonoption_flags
= new_str
;
121 while (top
> middle
&& middle
> bottom
)
123 if (top
- middle
> middle
- bottom
)
125 /* Bottom segment is the short one. */
126 int len
= middle
- bottom
;
129 /* Swap it with the top part of the top segment. */
130 for (i
= 0; i
< len
; i
++)
132 tem
= argv
[bottom
+ i
];
133 argv
[bottom
+ i
] = argv
[top
- (middle
- bottom
) + i
];
134 argv
[top
- (middle
- bottom
) + i
] = tem
;
135 SWAP_FLAGS (bottom
+ i
, top
- (middle
- bottom
) + i
);
137 /* Exclude the moved bottom segment from further swapping. */
142 /* Top segment is the short one. */
143 int len
= top
- middle
;
146 /* Swap it with the bottom part of the bottom segment. */
147 for (i
= 0; i
< len
; i
++)
149 tem
= argv
[bottom
+ i
];
150 argv
[bottom
+ i
] = argv
[middle
+ i
];
151 argv
[middle
+ i
] = tem
;
152 SWAP_FLAGS (bottom
+ i
, middle
+ i
);
154 /* Exclude the moved top segment from further swapping. */
159 /* Update records for the slots the non-options now occupy. */
161 first_nonopt
+= (optind
- last_nonopt
);
162 last_nonopt
= optind
;
166 _getopt_initialize (argc
, argv
, optstring
)
169 const char *optstring
;
171 /* Start processing options with ARGV-element 1 (since ARGV-element 0
172 is the program name); the sequence of previously skipped
173 non-option ARGV-elements is empty. */
175 first_nonopt
= last_nonopt
= optind
;
179 posixly_correct
= getenv ("POSIXLY_CORRECT");
181 /* Determine how to handle the ordering of options and nonoptions. */
183 if (optstring
[0] == '-')
185 ordering
= RETURN_IN_ORDER
;
188 else if (optstring
[0] == '+')
190 ordering
= REQUIRE_ORDER
;
193 else if (posixly_correct
!= NULL
)
194 ordering
= REQUIRE_ORDER
;
198 #if defined _LIBC && defined USE_NONOPTION_FLAGS
199 if (posixly_correct
== NULL
200 && argc
== __libc_argc
&& argv
== __libc_argv
)
202 if (nonoption_flags_max_len
== 0)
204 if (__getopt_nonoption_flags
== NULL
205 || __getopt_nonoption_flags
[0] == '\0')
206 nonoption_flags_max_len
= -1;
209 const char *orig_str
= __getopt_nonoption_flags
;
210 int len
= nonoption_flags_max_len
= strlen (orig_str
);
211 if (nonoption_flags_max_len
< argc
)
212 nonoption_flags_max_len
= argc
;
213 __getopt_nonoption_flags
= malloc (nonoption_flags_max_len
);
214 if (__getopt_nonoption_flags
== NULL
)
215 nonoption_flags_max_len
= -1;
217 memset (__mempcpy (__getopt_nonoption_flags
, orig_str
, len
),
218 '\0', nonoption_flags_max_len
- len
);
221 nonoption_flags_len
= nonoption_flags_max_len
;
224 nonoption_flags_len
= 0;
231 getopt_long_only (int argc
, char * const *argv
, const char *options
, const struct option
*long_options
, int *opt_index
)
233 return _getopt_internal (argc
, argv
, options
, long_options
, opt_index
, 1);
237 _getopt_internal (argc
, argv
, optstring
, longopts
, longind
, long_only
)
240 const char *optstring
;
241 const struct option
*longopts
;
245 int print_errors
= opterr
;
246 if (optstring
[0] == ':')
254 if (optind
== 0 || !__getopt_initialized
)
257 optind
= 1; /* Don't scan ARGV[0], the program name. */
258 optstring
= _getopt_initialize (argc
, argv
, optstring
);
259 __getopt_initialized
= 1;
262 /* Test whether ARGV[optind] points to a non-option argument.
263 Either it does not have option syntax, or there is an environment flag
264 from the shell indicating it is not an option. The later information
265 is only used when the used in the GNU libc. */
266 #if defined _LIBC && defined USE_NONOPTION_FLAGS
267 # define NONOPTION_P (argv[optind][0] != '-' || argv[optind][1] == '\0' \
268 || (optind < nonoption_flags_len \
269 && __getopt_nonoption_flags[optind] == '1'))
271 # define NONOPTION_P (argv[optind][0] != '-' || argv[optind][1] == '\0')
274 if (nextchar
== NULL
|| *nextchar
== '\0')
276 /* Advance to the next ARGV-element. */
278 /* Give FIRST_NONOPT & LAST_NONOPT rational values if OPTIND has been
279 moved back by the user (who may also have changed the arguments). */
280 if (last_nonopt
> optind
)
281 last_nonopt
= optind
;
282 if (first_nonopt
> optind
)
283 first_nonopt
= optind
;
285 if (ordering
== PERMUTE
)
287 /* If we have just processed some options following some non-options,
288 exchange them so that the options come first. */
290 if (first_nonopt
!= last_nonopt
&& last_nonopt
!= optind
)
291 exchange ((char **) argv
);
292 else if (last_nonopt
!= optind
)
293 first_nonopt
= optind
;
295 /* Skip any additional non-options
296 and extend the range of non-options previously skipped. */
298 while (optind
< argc
&& NONOPTION_P
)
300 last_nonopt
= optind
;
303 /* The special ARGV-element `--' means premature end of options.
304 Skip it like a null option,
305 then exchange with previous non-options as if it were an option,
306 then skip everything else like a non-option. */
308 if (optind
!= argc
&& !strcmp (argv
[optind
], "--"))
312 if (first_nonopt
!= last_nonopt
&& last_nonopt
!= optind
)
313 exchange ((char **) argv
);
314 else if (first_nonopt
== last_nonopt
)
315 first_nonopt
= optind
;
321 /* If we have done all the ARGV-elements, stop the scan
322 and back over any non-options that we skipped and permuted. */
326 /* Set the next-arg-index to point at the non-options
327 that we previously skipped, so the caller will digest them. */
328 if (first_nonopt
!= last_nonopt
)
329 optind
= first_nonopt
;
333 /* If we have come to a non-option and did not permute it,
334 either stop the scan or describe it to the caller and pass it by. */
338 if (ordering
== REQUIRE_ORDER
)
340 optarg
= argv
[optind
++];
344 /* We have found another option-ARGV-element.
345 Skip the initial punctuation. */
347 nextchar
= (argv
[optind
] + 1
348 + (longopts
!= NULL
&& argv
[optind
][1] == '-'));
351 /* Decode the current option-ARGV-element. */
353 /* Check whether the ARGV-element is a long option.
355 If long_only and the ARGV-element has the form "-f", where f is
356 a valid short option, don't consider it an abbreviated form of
357 a long option that starts with f. Otherwise there would be no
358 way to give the -f short option.
360 On the other hand, if there's a long option "fubar" and
361 the ARGV-element is "-fu", do consider that an abbreviation of
362 the long option, just like "--fu", and not "-f" with arg "u".
364 This distinction seems to be the most useful approach. */
367 && (argv
[optind
][1] == '-'
368 || (long_only
&& (argv
[optind
][2] || !my_index (optstring
, argv
[optind
][1])))))
371 const struct option
*p
;
372 const struct option
*pfound
= NULL
;
378 for (nameend
= nextchar
; *nameend
&& *nameend
!= '='; nameend
++)
381 /* Test all long options for either exact match
382 or abbreviated matches. */
383 for (p
= longopts
, option_index
= 0; p
->name
; p
++, option_index
++)
384 if (!strncmp (p
->name
, nextchar
, nameend
- nextchar
))
386 if ((unsigned int) (nameend
- nextchar
)
387 == (unsigned int) strlen (p
->name
))
389 /* Exact match found. */
391 indfound
= option_index
;
395 else if (pfound
== NULL
)
397 /* First nonexact match found. */
399 indfound
= option_index
;
402 || pfound
->has_arg
!= p
->has_arg
403 || pfound
->flag
!= p
->flag
404 || pfound
->val
!= p
->val
)
405 /* Second or later nonexact match found. */
413 #if defined _LIBC && defined USE_IN_LIBIO
416 if (__asprintf (&buf
, _("%s: option `%s' is ambiguous\n"),
417 argv
[0], argv
[optind
]) >= 0)
420 if (_IO_fwide (stderr
, 0) > 0)
421 __fwprintf (stderr
, L
"%s", buf
);
428 fprintf (stderr
, "%s: option `%s' is ambiguous\n",
429 argv
[0], argv
[optind
]);
432 nextchar
+= strlen (nextchar
);
440 option_index
= indfound
;
444 /* Don't test has_arg with >, because some C compilers don't
445 allow it to be used on enums. */
447 optarg
= nameend
+ 1;
452 #if defined _LIBC && defined USE_IN_LIBIO
457 if (argv
[optind
- 1][1] == '-')
460 #if defined _LIBC && defined USE_IN_LIBIO
461 n
= __asprintf (&buf
, "\
462 %s: option `--%s' doesn't allow an argument\n",
463 argv
[0], pfound
->name
);
466 %s: option `--%s' doesn't allow an argument\n",
467 argv
[0], pfound
->name
);
472 /* +option or -option */
473 #if defined _LIBC && defined USE_IN_LIBIO
474 n
= __asprintf (&buf
, "\
475 %s: option `%c%s' doesn't allow an argument\n",
476 argv
[0], argv
[optind
- 1][0],
480 %s: option `%c%s' doesn't allow an argument\n",
481 argv
[0], argv
[optind
- 1][0], pfound
->name
);
485 #if defined _LIBC && defined USE_IN_LIBIO
488 if (_IO_fwide (stderr
, 0) > 0)
489 __fwprintf (stderr
, L
"%s", buf
);
498 nextchar
+= strlen (nextchar
);
500 optopt
= pfound
->val
;
504 else if (pfound
->has_arg
== 1)
507 optarg
= argv
[optind
++];
512 #if defined _LIBC && defined USE_IN_LIBIO
515 if (__asprintf (&buf
, _("\
516 %s: option `%s' requires an argument\n"),
517 argv
[0], argv
[optind
- 1]) >= 0)
519 if (_IO_fwide (stderr
, 0) > 0)
520 __fwprintf (stderr
, L
"%s", buf
);
528 "%s: option `%s' requires an argument\n",
529 argv
[0], argv
[optind
- 1]);
532 nextchar
+= strlen (nextchar
);
533 optopt
= pfound
->val
;
534 return optstring
[0] == ':' ? ':' : '?';
537 nextchar
+= strlen (nextchar
);
539 *longind
= option_index
;
542 *(pfound
->flag
) = pfound
->val
;
548 /* Can't find it as a long option. If this is not getopt_long_only,
549 or the option starts with '--' or is not a valid short
550 option, then it's an error.
551 Otherwise interpret it as a short option. */
552 if (!long_only
|| argv
[optind
][1] == '-'
553 || my_index (optstring
, *nextchar
) == NULL
)
557 #if defined _LIBC && defined USE_IN_LIBIO
562 if (argv
[optind
][1] == '-')
565 #if defined _LIBC && defined USE_IN_LIBIO
566 n
= __asprintf (&buf
, _("%s: unrecognized option `--%s'\n"),
569 fprintf (stderr
, "%s: unrecognized option `--%s'\n",
575 /* +option or -option */
576 #if defined _LIBC && defined USE_IN_LIBIO
577 n
= __asprintf (&buf
, _("%s: unrecognized option `%c%s'\n"),
578 argv
[0], argv
[optind
][0], nextchar
);
580 fprintf (stderr
, "%s: unrecognized option `%c%s'\n",
581 argv
[0], argv
[optind
][0], nextchar
);
585 #if defined _LIBC && defined USE_IN_LIBIO
588 if (_IO_fwide (stderr
, 0) > 0)
589 __fwprintf (stderr
, L
"%s", buf
);
597 nextchar
= (char *) "";
604 /* Look at and handle the next short option-character. */
607 char c
= *nextchar
++;
608 char *temp
= my_index (optstring
, c
);
610 /* Increment `optind' when we start to process its last character. */
611 if (*nextchar
== '\0')
614 if (temp
== NULL
|| c
== ':')
618 #if defined _LIBC && defined USE_IN_LIBIO
625 /* 1003.2 specifies the format of this message. */
626 #if defined _LIBC && defined USE_IN_LIBIO
627 n
= __asprintf (&buf
, _("%s: illegal option -- %c\n"),
630 fprintf (stderr
, "%s: illegal option -- %c\n", argv
[0], c
);
635 #if defined _LIBC && defined USE_IN_LIBIO
636 n
= __asprintf (&buf
, _("%s: invalid option -- %c\n"),
639 fprintf (stderr
, "%s: invalid option -- %c\n", argv
[0], c
);
643 #if defined _LIBC && defined USE_IN_LIBIO
646 if (_IO_fwide (stderr
, 0) > 0)
647 __fwprintf (stderr
, L
"%s", buf
);
658 /* Convenience. Treat POSIX -W foo same as long option --foo */
659 if (temp
[0] == 'W' && temp
[1] == ';')
662 const struct option
*p
;
663 const struct option
*pfound
= NULL
;
669 /* This is an option that requires an argument. */
670 if (*nextchar
!= '\0')
673 /* If we end this ARGV-element by taking the rest as an arg,
674 we must advance to the next element now. */
677 else if (optind
== argc
)
681 /* 1003.2 specifies the format of this message. */
682 #if defined _LIBC && defined USE_IN_LIBIO
685 if (__asprintf (&buf
,
686 _("%s: option requires an argument -- %c\n"),
689 if (_IO_fwide (stderr
, 0) > 0)
690 __fwprintf (stderr
, L
"%s", buf
);
697 fprintf (stderr
, "%s: option requires an argument -- %c\n",
702 if (optstring
[0] == ':')
709 /* We already incremented `optind' once;
710 increment it again when taking next ARGV-elt as argument. */
711 optarg
= argv
[optind
++];
713 /* optarg is now the argument, see if it's in the
714 table of longopts. */
716 for (nextchar
= nameend
= optarg
; *nameend
&& *nameend
!= '='; nameend
++)
719 /* Test all long options for either exact match
720 or abbreviated matches. */
721 for (p
= longopts
, option_index
= 0; p
->name
; p
++, option_index
++)
722 if (!strncmp (p
->name
, nextchar
, nameend
- nextchar
))
724 if ((unsigned int) (nameend
- nextchar
) == strlen (p
->name
))
726 /* Exact match found. */
728 indfound
= option_index
;
732 else if (pfound
== NULL
)
734 /* First nonexact match found. */
736 indfound
= option_index
;
739 /* Second or later nonexact match found. */
746 #if defined _LIBC && defined USE_IN_LIBIO
749 if (__asprintf (&buf
, _("%s: option `-W %s' is ambiguous\n"),
750 argv
[0], argv
[optind
]) >= 0)
752 if (_IO_fwide (stderr
, 0) > 0)
753 __fwprintf (stderr
, L
"%s", buf
);
760 fprintf (stderr
, "%s: option `-W %s' is ambiguous\n",
761 argv
[0], argv
[optind
]);
764 nextchar
+= strlen (nextchar
);
770 option_index
= indfound
;
773 /* Don't test has_arg with >, because some C compilers don't
774 allow it to be used on enums. */
776 optarg
= nameend
+ 1;
781 #if defined _LIBC && defined USE_IN_LIBIO
784 if (__asprintf (&buf
, _("\
785 %s: option `-W %s' doesn't allow an argument\n"),
786 argv
[0], pfound
->name
) >= 0)
788 if (_IO_fwide (stderr
, 0) > 0)
789 __fwprintf (stderr
, L
"%s", buf
);
797 %s: option `-W %s' doesn't allow an argument\n",
798 argv
[0], pfound
->name
);
802 nextchar
+= strlen (nextchar
);
806 else if (pfound
->has_arg
== 1)
809 optarg
= argv
[optind
++];
814 #if defined _LIBC && defined USE_IN_LIBIO
817 if (__asprintf (&buf
, _("\
818 %s: option `%s' requires an argument\n"),
819 argv
[0], argv
[optind
- 1]) >= 0)
821 if (_IO_fwide (stderr
, 0) > 0)
822 __fwprintf (stderr
, L
"%s", buf
);
830 "%s: option `%s' requires an argument\n",
831 argv
[0], argv
[optind
- 1]);
834 nextchar
+= strlen (nextchar
);
835 return optstring
[0] == ':' ? ':' : '?';
838 nextchar
+= strlen (nextchar
);
840 *longind
= option_index
;
843 *(pfound
->flag
) = pfound
->val
;
849 return 'W'; /* Let the application handle it. */
855 /* This is an option that accepts an argument optionally. */
856 if (*nextchar
!= '\0')
867 /* This is an option that requires an argument. */
868 if (*nextchar
!= '\0')
871 /* If we end this ARGV-element by taking the rest as an arg,
872 we must advance to the next element now. */
875 else if (optind
== argc
)
879 /* 1003.2 specifies the format of this message. */
880 #if defined _LIBC && defined USE_IN_LIBIO
883 if (__asprintf (&buf
, _("\
884 %s: option requires an argument -- %c\n"),
887 if (_IO_fwide (stderr
, 0) > 0)
888 __fwprintf (stderr
, L
"%s", buf
);
896 "%s: option requires an argument -- %c\n",
901 if (optstring
[0] == ':')
907 /* We already incremented `optind' once;
908 increment it again when taking next ARGV-elt as argument. */
909 optarg
= argv
[optind
++];