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