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