2 |
NOTE: getopt is now part of the C library, so if you don't know what |
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 |
"Keep this file name-space clean" means, talk to drepper@gnu.org |
4 |
before changing it! |
before changing it! |
5 |
Copyright (C) 1987,88,89,90,91,92,93,94,95,96,98,99,2000,2001,2002 |
Copyright (C) 1987,88,89,90,91,92,93,94,95,96,98,99,2000,2001,2002,2003,2004 |
6 |
Free Software Foundation, Inc. |
Free Software Foundation, Inc. |
7 |
This file is part of the GNU C Library. |
This file is part of the GNU C Library. |
8 |
|
|
18 |
|
|
19 |
You should have received a copy of the GNU Lesser General Public |
You should have received a copy of the GNU Lesser General Public |
20 |
License along with the GNU C Library; if not, write to the Free |
License along with the GNU C Library; if not, write to the Free |
21 |
Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA |
Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA |
22 |
02110-1301 USA. */ |
02111-1307 USA. */ |
23 |
|
|
24 |
/* This tells Alpha OSF/1 not to define a getopt prototype in <stdio.h>. |
/* This tells Alpha OSF/1 not to define a getopt prototype in <stdio.h>. |
25 |
Ditto for AIX 3.2 and <stdlib.h>. */ |
Ditto for AIX 3.2 and <stdlib.h>. */ |
31 |
# include <config.h> |
# include <config.h> |
32 |
#endif |
#endif |
33 |
|
|
|
#if !defined __STDC__ || !__STDC__ |
|
|
/* This is a separate conditional since some stdc systems |
|
|
reject `defined (const)'. */ |
|
|
# ifndef const |
|
|
# define const |
|
|
# endif |
|
|
#endif |
|
|
|
|
34 |
#include <stdio.h> |
#include <stdio.h> |
35 |
|
|
36 |
/* Comment out all this code if we are using the GNU C Library, and are not |
/* Comment out all this code if we are using the GNU C Library, and are not |
61 |
# include <unistd.h> |
# include <unistd.h> |
62 |
#endif /* GNU C library. */ |
#endif /* GNU C library. */ |
63 |
|
|
64 |
|
#include <string.h> |
65 |
|
|
66 |
#ifdef VMS |
#ifdef VMS |
67 |
# include <unixlib.h> |
# include <unixlib.h> |
|
# if HAVE_STRING_H - 0 |
|
|
# include <string.h> |
|
|
# endif |
|
68 |
#endif |
#endif |
69 |
|
|
70 |
#ifndef _ |
#ifdef _LIBC |
71 |
/* This is for other GNU distributions with internationalized messages. */ |
# include <libintl.h> |
72 |
# if (HAVE_LIBINTL_H && ENABLE_NLS) || defined _LIBC |
#else |
73 |
# include <libintl.h> |
# include "gettext.h" |
74 |
# ifndef _ |
# define _(msgid) gettext (msgid) |
75 |
# define _(msgid) gettext (msgid) |
#endif |
76 |
# endif |
|
77 |
# else |
#if defined _LIBC && defined USE_IN_LIBIO |
78 |
# define _(msgid) (msgid) |
# include <wchar.h> |
|
# endif |
|
|
# if defined _LIBC && defined USE_IN_LIBIO |
|
|
# include <wchar.h> |
|
|
# endif |
|
79 |
#endif |
#endif |
80 |
|
|
81 |
#ifndef attribute_hidden |
#ifndef attribute_hidden |
97 |
they can distinguish the relative order of options and other arguments. */ |
they can distinguish the relative order of options and other arguments. */ |
98 |
|
|
99 |
#include "getopt.h" |
#include "getopt.h" |
100 |
|
#include "getopt_int.h" |
101 |
|
|
102 |
/* For communication from `getopt' to the caller. |
/* For communication from `getopt' to the caller. |
103 |
When `getopt' finds an option that takes an argument, |
When `getopt' finds an option that takes an argument, |
122 |
/* 1003.2 says this must be 1 before any call. */ |
/* 1003.2 says this must be 1 before any call. */ |
123 |
int optind = 1; |
int optind = 1; |
124 |
|
|
|
/* Formerly, initialization of getopt depended on optind==0, which |
|
|
causes problems with re-calling getopt as programs generally don't |
|
|
know that. */ |
|
|
|
|
|
int __getopt_initialized attribute_hidden; |
|
|
|
|
|
/* The next char to be scanned in the option-element |
|
|
in which the last option character we returned was found. |
|
|
This allows us to pick up the scan where we left off. |
|
|
|
|
|
If this is zero, or a null string, it means resume the scan |
|
|
by advancing to the next ARGV-element. */ |
|
|
|
|
|
static char *nextchar; |
|
|
|
|
125 |
/* Callers store zero here to inhibit the error message |
/* Callers store zero here to inhibit the error message |
126 |
for unrecognized options. */ |
for unrecognized options. */ |
127 |
|
|
133 |
|
|
134 |
int optopt = '?'; |
int optopt = '?'; |
135 |
|
|
136 |
/* Describe how to deal with options that follow non-option ARGV-elements. |
/* Keep a global copy of all internal members of getopt_data. */ |
|
|
|
|
If the caller did not specify anything, |
|
|
the default is REQUIRE_ORDER if the environment variable |
|
|
POSIXLY_CORRECT is defined, PERMUTE otherwise. |
|
|
|
|
|
REQUIRE_ORDER means don't recognize them as options; |
|
|
stop option processing when the first non-option is seen. |
|
|
This is what Unix does. |
|
|
This mode of operation is selected by either setting the environment |
|
|
variable POSIXLY_CORRECT, or using `+' as the first character |
|
|
of the list of option characters. |
|
|
|
|
|
PERMUTE is the default. We permute the contents of ARGV as we scan, |
|
|
so that eventually all the non-options are at the end. This allows options |
|
|
to be given in any order, even with programs that were not written to |
|
|
expect this. |
|
|
|
|
|
RETURN_IN_ORDER is an option available to programs that were written |
|
|
to expect options and other ARGV-elements in any order and that care about |
|
|
the ordering of the two. We describe each non-option ARGV-element |
|
|
as if it were the argument of an option with character code 1. |
|
|
Using `-' as the first character of the list of option characters |
|
|
selects this mode of operation. |
|
|
|
|
|
The special argument `--' forces an end of option-scanning regardless |
|
|
of the value of `ordering'. In the case of RETURN_IN_ORDER, only |
|
|
`--' can cause `getopt' to return -1 with `optind' != ARGC. */ |
|
137 |
|
|
138 |
static enum |
static struct _getopt_data getopt_data; |
|
{ |
|
|
REQUIRE_ORDER, PERMUTE, RETURN_IN_ORDER |
|
|
} ordering; |
|
139 |
|
|
|
/* Value of POSIXLY_CORRECT environment variable. */ |
|
|
static char *posixly_correct; |
|
140 |
|
|
141 |
#ifdef __GNU_LIBRARY__ |
#ifndef __GNU_LIBRARY__ |
|
/* We want to avoid inclusion of string.h with non-GNU libraries |
|
|
because there are many ways it can cause trouble. |
|
|
On some systems, it contains special magic macros that don't work |
|
|
in GCC. */ |
|
|
# include <string.h> |
|
|
# define my_index strchr |
|
|
#else |
|
|
|
|
|
# if HAVE_STRING_H |
|
|
# include <string.h> |
|
|
# else |
|
|
# include <strings.h> |
|
|
# endif |
|
142 |
|
|
143 |
/* Avoid depending on library functions or files |
/* Avoid depending on library functions or files |
144 |
whose names are inconsistent. */ |
whose names are inconsistent. */ |
147 |
extern char *getenv (); |
extern char *getenv (); |
148 |
#endif |
#endif |
149 |
|
|
|
static char * |
|
|
my_index (str, chr) |
|
|
const char *str; |
|
|
int chr; |
|
|
{ |
|
|
while (*str) |
|
|
{ |
|
|
if (*str == chr) |
|
|
return (char *) str; |
|
|
str++; |
|
|
} |
|
|
return 0; |
|
|
} |
|
|
|
|
|
/* If using GCC, we can safely declare strlen this way. |
|
|
If not using GCC, it is ok not to declare it. */ |
|
|
#ifdef __GNUC__ |
|
|
/* Note that Motorola Delta 68k R3V7 comes with GCC but not stddef.h. |
|
|
That was relevant to code that was here before. */ |
|
|
# if (!defined __STDC__ || !__STDC__) && !defined strlen |
|
|
/* gcc with -traditional declares the built-in strlen to return int, |
|
|
and has done so at least since version 2.4.5. -- rms. */ |
|
|
extern int strlen (const char *); |
|
|
# endif /* not __STDC__ */ |
|
|
#endif /* __GNUC__ */ |
|
|
|
|
150 |
#endif /* not __GNU_LIBRARY__ */ |
#endif /* not __GNU_LIBRARY__ */ |
151 |
|
|
|
/* Handle permutation of arguments. */ |
|
|
|
|
|
/* Describe the part of ARGV that contains non-options that have |
|
|
been skipped. `first_nonopt' is the index in ARGV of the first of them; |
|
|
`last_nonopt' is the index after the last of them. */ |
|
|
|
|
|
static int first_nonopt; |
|
|
static int last_nonopt; |
|
|
|
|
152 |
#ifdef _LIBC |
#ifdef _LIBC |
153 |
/* Stored original parameters. |
/* Stored original parameters. |
154 |
XXX This is no good solution. We should rather copy the args so |
XXX This is no good solution. We should rather copy the args so |
162 |
# ifdef USE_NONOPTION_FLAGS |
# ifdef USE_NONOPTION_FLAGS |
163 |
/* Defined in getopt_init.c */ |
/* Defined in getopt_init.c */ |
164 |
extern char *__getopt_nonoption_flags; |
extern char *__getopt_nonoption_flags; |
|
|
|
|
static int nonoption_flags_max_len; |
|
|
static int nonoption_flags_len; |
|
165 |
# endif |
# endif |
166 |
|
|
167 |
# ifdef USE_NONOPTION_FLAGS |
# ifdef USE_NONOPTION_FLAGS |
168 |
# define SWAP_FLAGS(ch1, ch2) \ |
# define SWAP_FLAGS(ch1, ch2) \ |
169 |
if (nonoption_flags_len > 0) \ |
if (d->__nonoption_flags_len > 0) \ |
170 |
{ \ |
{ \ |
171 |
char __tmp = __getopt_nonoption_flags[ch1]; \ |
char __tmp = __getopt_nonoption_flags[ch1]; \ |
172 |
__getopt_nonoption_flags[ch1] = __getopt_nonoption_flags[ch2]; \ |
__getopt_nonoption_flags[ch1] = __getopt_nonoption_flags[ch2]; \ |
188 |
`first_nonopt' and `last_nonopt' are relocated so that they describe |
`first_nonopt' and `last_nonopt' are relocated so that they describe |
189 |
the new indices of the non-options in ARGV after they are moved. */ |
the new indices of the non-options in ARGV after they are moved. */ |
190 |
|
|
|
#if defined __STDC__ && __STDC__ |
|
|
static void exchange (char **); |
|
|
#endif |
|
|
|
|
191 |
static void |
static void |
192 |
exchange (argv) |
exchange (char **argv, struct _getopt_data *d) |
|
char **argv; |
|
193 |
{ |
{ |
194 |
int bottom = first_nonopt; |
int bottom = d->__first_nonopt; |
195 |
int middle = last_nonopt; |
int middle = d->__last_nonopt; |
196 |
int top = optind; |
int top = d->optind; |
197 |
char *tem; |
char *tem; |
198 |
|
|
199 |
/* Exchange the shorter segment with the far end of the longer segment. |
/* Exchange the shorter segment with the far end of the longer segment. |
205 |
/* First make sure the handling of the `__getopt_nonoption_flags' |
/* First make sure the handling of the `__getopt_nonoption_flags' |
206 |
string can work normally. Our top argument must be in the range |
string can work normally. Our top argument must be in the range |
207 |
of the string. */ |
of the string. */ |
208 |
if (nonoption_flags_len > 0 && top >= nonoption_flags_max_len) |
if (d->__nonoption_flags_len > 0 && top >= d->__nonoption_flags_max_len) |
209 |
{ |
{ |
210 |
/* We must extend the array. The user plays games with us and |
/* We must extend the array. The user plays games with us and |
211 |
presents new arguments. */ |
presents new arguments. */ |
212 |
char *new_str = malloc (top + 1); |
char *new_str = malloc (top + 1); |
213 |
if (new_str == NULL) |
if (new_str == NULL) |
214 |
nonoption_flags_len = nonoption_flags_max_len = 0; |
d->__nonoption_flags_len = d->__nonoption_flags_max_len = 0; |
215 |
else |
else |
216 |
{ |
{ |
217 |
memset (__mempcpy (new_str, __getopt_nonoption_flags, |
memset (__mempcpy (new_str, __getopt_nonoption_flags, |
218 |
nonoption_flags_max_len), |
d->__nonoption_flags_max_len), |
219 |
'\0', top + 1 - nonoption_flags_max_len); |
'\0', top + 1 - d->__nonoption_flags_max_len); |
220 |
nonoption_flags_max_len = top + 1; |
d->__nonoption_flags_max_len = top + 1; |
221 |
__getopt_nonoption_flags = new_str; |
__getopt_nonoption_flags = new_str; |
222 |
} |
} |
223 |
} |
} |
263 |
|
|
264 |
/* Update records for the slots the non-options now occupy. */ |
/* Update records for the slots the non-options now occupy. */ |
265 |
|
|
266 |
first_nonopt += (optind - last_nonopt); |
d->__first_nonopt += (d->optind - d->__last_nonopt); |
267 |
last_nonopt = optind; |
d->__last_nonopt = d->optind; |
268 |
} |
} |
269 |
|
|
270 |
/* Initialize the internal data when the first call is made. */ |
/* Initialize the internal data when the first call is made. */ |
271 |
|
|
|
#if defined __STDC__ && __STDC__ |
|
|
static const char *_getopt_initialize (int, char *const *, const char *); |
|
|
#endif |
|
272 |
static const char * |
static const char * |
273 |
_getopt_initialize (argc, argv, optstring) |
_getopt_initialize (int argc, char *const *argv, const char *optstring, |
274 |
int argc; |
struct _getopt_data *d) |
|
char *const *argv; |
|
|
const char *optstring; |
|
275 |
{ |
{ |
276 |
/* Start processing options with ARGV-element 1 (since ARGV-element 0 |
/* Start processing options with ARGV-element 1 (since ARGV-element 0 |
277 |
is the program name); the sequence of previously skipped |
is the program name); the sequence of previously skipped |
278 |
non-option ARGV-elements is empty. */ |
non-option ARGV-elements is empty. */ |
279 |
|
|
280 |
first_nonopt = last_nonopt = optind; |
d->__first_nonopt = d->__last_nonopt = d->optind; |
281 |
|
|
282 |
nextchar = NULL; |
d->__nextchar = NULL; |
283 |
|
|
284 |
posixly_correct = getenv ("POSIXLY_CORRECT"); |
d->__posixly_correct = !!getenv ("POSIXLY_CORRECT"); |
285 |
|
|
286 |
/* Determine how to handle the ordering of options and nonoptions. */ |
/* Determine how to handle the ordering of options and nonoptions. */ |
287 |
|
|
288 |
if (optstring[0] == '-') |
if (optstring[0] == '-') |
289 |
{ |
{ |
290 |
ordering = RETURN_IN_ORDER; |
d->__ordering = RETURN_IN_ORDER; |
291 |
++optstring; |
++optstring; |
292 |
} |
} |
293 |
else if (optstring[0] == '+') |
else if (optstring[0] == '+') |
294 |
{ |
{ |
295 |
ordering = REQUIRE_ORDER; |
d->__ordering = REQUIRE_ORDER; |
296 |
++optstring; |
++optstring; |
297 |
} |
} |
298 |
else if (posixly_correct != NULL) |
else if (d->__posixly_correct) |
299 |
ordering = REQUIRE_ORDER; |
d->__ordering = REQUIRE_ORDER; |
300 |
else |
else |
301 |
ordering = PERMUTE; |
d->__ordering = PERMUTE; |
302 |
|
|
303 |
#if defined _LIBC && defined USE_NONOPTION_FLAGS |
#if defined _LIBC && defined USE_NONOPTION_FLAGS |
304 |
if (posixly_correct == NULL |
if (!d->__posixly_correct |
305 |
&& argc == __libc_argc && argv == __libc_argv) |
&& argc == __libc_argc && argv == __libc_argv) |
306 |
{ |
{ |
307 |
if (nonoption_flags_max_len == 0) |
if (d->__nonoption_flags_max_len == 0) |
308 |
{ |
{ |
309 |
if (__getopt_nonoption_flags == NULL |
if (__getopt_nonoption_flags == NULL |
310 |
|| __getopt_nonoption_flags[0] == '\0') |
|| __getopt_nonoption_flags[0] == '\0') |
311 |
nonoption_flags_max_len = -1; |
d->__nonoption_flags_max_len = -1; |
312 |
else |
else |
313 |
{ |
{ |
314 |
const char *orig_str = __getopt_nonoption_flags; |
const char *orig_str = __getopt_nonoption_flags; |
315 |
int len = nonoption_flags_max_len = strlen (orig_str); |
int len = d->__nonoption_flags_max_len = strlen (orig_str); |
316 |
if (nonoption_flags_max_len < argc) |
if (d->__nonoption_flags_max_len < argc) |
317 |
nonoption_flags_max_len = argc; |
d->__nonoption_flags_max_len = argc; |
318 |
__getopt_nonoption_flags = |
__getopt_nonoption_flags = |
319 |
(char *) malloc (nonoption_flags_max_len); |
(char *) malloc (d->__nonoption_flags_max_len); |
320 |
if (__getopt_nonoption_flags == NULL) |
if (__getopt_nonoption_flags == NULL) |
321 |
nonoption_flags_max_len = -1; |
d->__nonoption_flags_max_len = -1; |
322 |
else |
else |
323 |
memset (__mempcpy (__getopt_nonoption_flags, orig_str, len), |
memset (__mempcpy (__getopt_nonoption_flags, orig_str, len), |
324 |
'\0', nonoption_flags_max_len - len); |
'\0', d->__nonoption_flags_max_len - len); |
325 |
} |
} |
326 |
} |
} |
327 |
nonoption_flags_len = nonoption_flags_max_len; |
d->__nonoption_flags_len = d->__nonoption_flags_max_len; |
328 |
} |
} |
329 |
else |
else |
330 |
nonoption_flags_len = 0; |
d->__nonoption_flags_len = 0; |
331 |
#endif |
#endif |
332 |
|
|
333 |
return optstring; |
return optstring; |
390 |
long-named options. */ |
long-named options. */ |
391 |
|
|
392 |
int |
int |
393 |
_getopt_internal (argc, argv, optstring, longopts, longind, long_only) |
_getopt_internal_r (int argc, char *const *argv, const char *optstring, |
394 |
int argc; |
const struct option *longopts, int *longind, |
395 |
char *const *argv; |
int long_only, struct _getopt_data *d) |
|
const char *optstring; |
|
|
const struct option *longopts; |
|
|
int *longind; |
|
|
int long_only; |
|
396 |
{ |
{ |
397 |
int print_errors = opterr; |
int print_errors = d->opterr; |
398 |
if (optstring[0] == ':') |
if (optstring[0] == ':') |
399 |
print_errors = 0; |
print_errors = 0; |
400 |
|
|
401 |
if (argc < 1) |
if (argc < 1) |
402 |
return -1; |
return -1; |
403 |
|
|
404 |
optarg = NULL; |
d->optarg = NULL; |
405 |
|
|
406 |
if (optind == 0 || !__getopt_initialized) |
if (d->optind == 0 || !d->__initialized) |
407 |
{ |
{ |
408 |
if (optind == 0) |
if (d->optind == 0) |
409 |
optind = 1; /* Don't scan ARGV[0], the program name. */ |
d->optind = 1; /* Don't scan ARGV[0], the program name. */ |
410 |
optstring = _getopt_initialize (argc, argv, optstring); |
optstring = _getopt_initialize (argc, argv, optstring, d); |
411 |
__getopt_initialized = 1; |
d->__initialized = 1; |
412 |
} |
} |
413 |
|
|
414 |
/* Test whether ARGV[optind] points to a non-option argument. |
/* Test whether ARGV[optind] points to a non-option argument. |
416 |
from the shell indicating it is not an option. The later information |
from the shell indicating it is not an option. The later information |
417 |
is only used when the used in the GNU libc. */ |
is only used when the used in the GNU libc. */ |
418 |
#if defined _LIBC && defined USE_NONOPTION_FLAGS |
#if defined _LIBC && defined USE_NONOPTION_FLAGS |
419 |
# define NONOPTION_P (argv[optind][0] != '-' || argv[optind][1] == '\0' \ |
# define NONOPTION_P (argv[d->optind][0] != '-' || argv[d->optind][1] == '\0' \ |
420 |
|| (optind < nonoption_flags_len \ |
|| (d->optind < d->__nonoption_flags_len \ |
421 |
&& __getopt_nonoption_flags[optind] == '1')) |
&& __getopt_nonoption_flags[d->optind] == '1')) |
422 |
#else |
#else |
423 |
# define NONOPTION_P (argv[optind][0] != '-' || argv[optind][1] == '\0') |
# define NONOPTION_P (argv[d->optind][0] != '-' || argv[d->optind][1] == '\0') |
424 |
#endif |
#endif |
425 |
|
|
426 |
if (nextchar == NULL || *nextchar == '\0') |
if (d->__nextchar == NULL || *d->__nextchar == '\0') |
427 |
{ |
{ |
428 |
/* Advance to the next ARGV-element. */ |
/* Advance to the next ARGV-element. */ |
429 |
|
|
430 |
/* Give FIRST_NONOPT & LAST_NONOPT rational values if OPTIND has been |
/* Give FIRST_NONOPT & LAST_NONOPT rational values if OPTIND has been |
431 |
moved back by the user (who may also have changed the arguments). */ |
moved back by the user (who may also have changed the arguments). */ |
432 |
if (last_nonopt > optind) |
if (d->__last_nonopt > d->optind) |
433 |
last_nonopt = optind; |
d->__last_nonopt = d->optind; |
434 |
if (first_nonopt > optind) |
if (d->__first_nonopt > d->optind) |
435 |
first_nonopt = optind; |
d->__first_nonopt = d->optind; |
436 |
|
|
437 |
if (ordering == PERMUTE) |
if (d->__ordering == PERMUTE) |
438 |
{ |
{ |
439 |
/* If we have just processed some options following some non-options, |
/* If we have just processed some options following some non-options, |
440 |
exchange them so that the options come first. */ |
exchange them so that the options come first. */ |
441 |
|
|
442 |
if (first_nonopt != last_nonopt && last_nonopt != optind) |
if (d->__first_nonopt != d->__last_nonopt |
443 |
exchange ((char **) argv); |
&& d->__last_nonopt != d->optind) |
444 |
else if (last_nonopt != optind) |
exchange ((char **) argv, d); |
445 |
first_nonopt = optind; |
else if (d->__last_nonopt != d->optind) |
446 |
|
d->__first_nonopt = d->optind; |
447 |
|
|
448 |
/* Skip any additional non-options |
/* Skip any additional non-options |
449 |
and extend the range of non-options previously skipped. */ |
and extend the range of non-options previously skipped. */ |
450 |
|
|
451 |
while (optind < argc && NONOPTION_P) |
while (d->optind < argc && NONOPTION_P) |
452 |
optind++; |
d->optind++; |
453 |
last_nonopt = optind; |
d->__last_nonopt = d->optind; |
454 |
} |
} |
455 |
|
|
456 |
/* The special ARGV-element `--' means premature end of options. |
/* The special ARGV-element `--' means premature end of options. |
458 |
then exchange with previous non-options as if it were an option, |
then exchange with previous non-options as if it were an option, |
459 |
then skip everything else like a non-option. */ |
then skip everything else like a non-option. */ |
460 |
|
|
461 |
if (optind != argc && !strcmp (argv[optind], "--")) |
if (d->optind != argc && !strcmp (argv[d->optind], "--")) |
462 |
{ |
{ |
463 |
optind++; |
d->optind++; |
464 |
|
|
465 |
if (first_nonopt != last_nonopt && last_nonopt != optind) |
if (d->__first_nonopt != d->__last_nonopt |
466 |
exchange ((char **) argv); |
&& d->__last_nonopt != d->optind) |
467 |
else if (first_nonopt == last_nonopt) |
exchange ((char **) argv, d); |
468 |
first_nonopt = optind; |
else if (d->__first_nonopt == d->__last_nonopt) |
469 |
last_nonopt = argc; |
d->__first_nonopt = d->optind; |
470 |
|
d->__last_nonopt = argc; |
471 |
|
|
472 |
optind = argc; |
d->optind = argc; |
473 |
} |
} |
474 |
|
|
475 |
/* If we have done all the ARGV-elements, stop the scan |
/* If we have done all the ARGV-elements, stop the scan |
476 |
and back over any non-options that we skipped and permuted. */ |
and back over any non-options that we skipped and permuted. */ |
477 |
|
|
478 |
if (optind == argc) |
if (d->optind == argc) |
479 |
{ |
{ |
480 |
/* Set the next-arg-index to point at the non-options |
/* Set the next-arg-index to point at the non-options |
481 |
that we previously skipped, so the caller will digest them. */ |
that we previously skipped, so the caller will digest them. */ |
482 |
if (first_nonopt != last_nonopt) |
if (d->__first_nonopt != d->__last_nonopt) |
483 |
optind = first_nonopt; |
d->optind = d->__first_nonopt; |
484 |
return -1; |
return -1; |
485 |
} |
} |
486 |
|
|
489 |
|
|
490 |
if (NONOPTION_P) |
if (NONOPTION_P) |
491 |
{ |
{ |
492 |
if (ordering == REQUIRE_ORDER) |
if (d->__ordering == REQUIRE_ORDER) |
493 |
return -1; |
return -1; |
494 |
optarg = argv[optind++]; |
d->optarg = argv[d->optind++]; |
495 |
return 1; |
return 1; |
496 |
} |
} |
497 |
|
|
498 |
/* We have found another option-ARGV-element. |
/* We have found another option-ARGV-element. |
499 |
Skip the initial punctuation. */ |
Skip the initial punctuation. */ |
500 |
|
|
501 |
nextchar = (argv[optind] + 1 |
d->__nextchar = (argv[d->optind] + 1 |
502 |
+ (longopts != NULL && argv[optind][1] == '-')); |
+ (longopts != NULL && argv[d->optind][1] == '-')); |
503 |
} |
} |
504 |
|
|
505 |
/* Decode the current option-ARGV-element. */ |
/* Decode the current option-ARGV-element. */ |
518 |
This distinction seems to be the most useful approach. */ |
This distinction seems to be the most useful approach. */ |
519 |
|
|
520 |
if (longopts != NULL |
if (longopts != NULL |
521 |
&& (argv[optind][1] == '-' |
&& (argv[d->optind][1] == '-' |
522 |
|| (long_only && (argv[optind][2] || !my_index (optstring, argv[optind][1]))))) |
|| (long_only && (argv[d->optind][2] |
523 |
|
|| !strchr (optstring, argv[d->optind][1]))))) |
524 |
{ |
{ |
525 |
char *nameend; |
char *nameend; |
526 |
const struct option *p; |
const struct option *p; |
530 |
int indfound = -1; |
int indfound = -1; |
531 |
int option_index; |
int option_index; |
532 |
|
|
533 |
for (nameend = nextchar; *nameend && *nameend != '='; nameend++) |
for (nameend = d->__nextchar; *nameend && *nameend != '='; nameend++) |
534 |
/* Do nothing. */ ; |
/* Do nothing. */ ; |
535 |
|
|
536 |
/* Test all long options for either exact match |
/* Test all long options for either exact match |
537 |
or abbreviated matches. */ |
or abbreviated matches. */ |
538 |
for (p = longopts, option_index = 0; p->name; p++, option_index++) |
for (p = longopts, option_index = 0; p->name; p++, option_index++) |
539 |
if (!strncmp (p->name, nextchar, nameend - nextchar)) |
if (!strncmp (p->name, d->__nextchar, nameend - d->__nextchar)) |
540 |
{ |
{ |
541 |
if ((unsigned int) (nameend - nextchar) |
if ((unsigned int) (nameend - d->__nextchar) |
542 |
== (unsigned int) strlen (p->name)) |
== (unsigned int) strlen (p->name)) |
543 |
{ |
{ |
544 |
/* Exact match found. */ |
/* Exact match found. */ |
569 |
char *buf; |
char *buf; |
570 |
|
|
571 |
if (__asprintf (&buf, _("%s: option `%s' is ambiguous\n"), |
if (__asprintf (&buf, _("%s: option `%s' is ambiguous\n"), |
572 |
argv[0], argv[optind]) >= 0) |
argv[0], argv[d->optind]) >= 0) |
573 |
{ |
{ |
574 |
|
_IO_flockfile (stderr); |
575 |
|
|
576 |
|
int old_flags2 = ((_IO_FILE *) stderr)->_flags2; |
577 |
|
((_IO_FILE *) stderr)->_flags2 |= _IO_FLAGS2_NOTCANCEL; |
578 |
|
|
579 |
if (_IO_fwide (stderr, 0) > 0) |
if (_IO_fwide (stderr, 0) > 0) |
580 |
__fwprintf (stderr, L"%s", buf); |
__fwprintf (stderr, L"%s", buf); |
581 |
else |
else |
582 |
fputs (buf, stderr); |
fputs (buf, stderr); |
583 |
|
|
584 |
|
((_IO_FILE *) stderr)->_flags2 = old_flags2; |
585 |
|
_IO_funlockfile (stderr); |
586 |
|
|
587 |
free (buf); |
free (buf); |
588 |
} |
} |
589 |
#else |
#else |
590 |
fprintf (stderr, _("%s: option `%s' is ambiguous\n"), |
fprintf (stderr, _("%s: option `%s' is ambiguous\n"), |
591 |
argv[0], argv[optind]); |
argv[0], argv[d->optind]); |
592 |
#endif |
#endif |
593 |
} |
} |
594 |
nextchar += strlen (nextchar); |
d->__nextchar += strlen (d->__nextchar); |
595 |
optind++; |
d->optind++; |
596 |
optopt = 0; |
d->optopt = 0; |
597 |
return '?'; |
return '?'; |
598 |
} |
} |
599 |
|
|
600 |
if (pfound != NULL) |
if (pfound != NULL) |
601 |
{ |
{ |
602 |
option_index = indfound; |
option_index = indfound; |
603 |
optind++; |
d->optind++; |
604 |
if (*nameend) |
if (*nameend) |
605 |
{ |
{ |
606 |
/* Don't test has_arg with >, because some C compilers don't |
/* Don't test has_arg with >, because some C compilers don't |
607 |
allow it to be used on enums. */ |
allow it to be used on enums. */ |
608 |
if (pfound->has_arg) |
if (pfound->has_arg) |
609 |
optarg = nameend + 1; |
d->optarg = nameend + 1; |
610 |
else |
else |
611 |
{ |
{ |
612 |
if (print_errors) |
if (print_errors) |
616 |
int n; |
int n; |
617 |
#endif |
#endif |
618 |
|
|
619 |
if (argv[optind - 1][1] == '-') |
if (argv[d->optind - 1][1] == '-') |
620 |
{ |
{ |
621 |
/* --option */ |
/* --option */ |
622 |
#if defined _LIBC && defined USE_IN_LIBIO |
#if defined _LIBC && defined USE_IN_LIBIO |
635 |
#if defined _LIBC && defined USE_IN_LIBIO |
#if defined _LIBC && defined USE_IN_LIBIO |
636 |
n = __asprintf (&buf, _("\ |
n = __asprintf (&buf, _("\ |
637 |
%s: option `%c%s' doesn't allow an argument\n"), |
%s: option `%c%s' doesn't allow an argument\n"), |
638 |
argv[0], argv[optind - 1][0], |
argv[0], argv[d->optind - 1][0], |
639 |
pfound->name); |
pfound->name); |
640 |
#else |
#else |
641 |
fprintf (stderr, _("\ |
fprintf (stderr, _("\ |
642 |
%s: option `%c%s' doesn't allow an argument\n"), |
%s: option `%c%s' doesn't allow an argument\n"), |
643 |
argv[0], argv[optind - 1][0], pfound->name); |
argv[0], argv[d->optind - 1][0], |
644 |
|
pfound->name); |
645 |
#endif |
#endif |
646 |
} |
} |
647 |
|
|
648 |
#if defined _LIBC && defined USE_IN_LIBIO |
#if defined _LIBC && defined USE_IN_LIBIO |
649 |
if (n >= 0) |
if (n >= 0) |
650 |
{ |
{ |
651 |
|
_IO_flockfile (stderr); |
652 |
|
|
653 |
|
int old_flags2 = ((_IO_FILE *) stderr)->_flags2; |
654 |
|
((_IO_FILE *) stderr)->_flags2 |
655 |
|
|= _IO_FLAGS2_NOTCANCEL; |
656 |
|
|
657 |
if (_IO_fwide (stderr, 0) > 0) |
if (_IO_fwide (stderr, 0) > 0) |
658 |
__fwprintf (stderr, L"%s", buf); |
__fwprintf (stderr, L"%s", buf); |
659 |
else |
else |
660 |
fputs (buf, stderr); |
fputs (buf, stderr); |
661 |
|
|
662 |
|
((_IO_FILE *) stderr)->_flags2 = old_flags2; |
663 |
|
_IO_funlockfile (stderr); |
664 |
|
|
665 |
free (buf); |
free (buf); |
666 |
} |
} |
667 |
#endif |
#endif |
668 |
} |
} |
669 |
|
|
670 |
nextchar += strlen (nextchar); |
d->__nextchar += strlen (d->__nextchar); |
671 |
|
|
672 |
optopt = pfound->val; |
d->optopt = pfound->val; |
673 |
return '?'; |
return '?'; |
674 |
} |
} |
675 |
} |
} |
676 |
else if (pfound->has_arg == 1) |
else if (pfound->has_arg == 1) |
677 |
{ |
{ |
678 |
if (optind < argc) |
if (d->optind < argc) |
679 |
optarg = argv[optind++]; |
d->optarg = argv[d->optind++]; |
680 |
else |
else |
681 |
{ |
{ |
682 |
if (print_errors) |
if (print_errors) |
686 |
|
|
687 |
if (__asprintf (&buf, _("\ |
if (__asprintf (&buf, _("\ |
688 |
%s: option `%s' requires an argument\n"), |
%s: option `%s' requires an argument\n"), |
689 |
argv[0], argv[optind - 1]) >= 0) |
argv[0], argv[d->optind - 1]) >= 0) |
690 |
{ |
{ |
691 |
|
_IO_flockfile (stderr); |
692 |
|
|
693 |
|
int old_flags2 = ((_IO_FILE *) stderr)->_flags2; |
694 |
|
((_IO_FILE *) stderr)->_flags2 |
695 |
|
|= _IO_FLAGS2_NOTCANCEL; |
696 |
|
|
697 |
if (_IO_fwide (stderr, 0) > 0) |
if (_IO_fwide (stderr, 0) > 0) |
698 |
__fwprintf (stderr, L"%s", buf); |
__fwprintf (stderr, L"%s", buf); |
699 |
else |
else |
700 |
fputs (buf, stderr); |
fputs (buf, stderr); |
701 |
|
|
702 |
|
((_IO_FILE *) stderr)->_flags2 = old_flags2; |
703 |
|
_IO_funlockfile (stderr); |
704 |
|
|
705 |
free (buf); |
free (buf); |
706 |
} |
} |
707 |
#else |
#else |
708 |
fprintf (stderr, |
fprintf (stderr, |
709 |
_("%s: option `%s' requires an argument\n"), |
_("%s: option `%s' requires an argument\n"), |
710 |
argv[0], argv[optind - 1]); |
argv[0], argv[d->optind - 1]); |
711 |
#endif |
#endif |
712 |
} |
} |
713 |
nextchar += strlen (nextchar); |
d->__nextchar += strlen (d->__nextchar); |
714 |
optopt = pfound->val; |
d->optopt = pfound->val; |
715 |
return optstring[0] == ':' ? ':' : '?'; |
return optstring[0] == ':' ? ':' : '?'; |
716 |
} |
} |
717 |
} |
} |
718 |
nextchar += strlen (nextchar); |
d->__nextchar += strlen (d->__nextchar); |
719 |
if (longind != NULL) |
if (longind != NULL) |
720 |
*longind = option_index; |
*longind = option_index; |
721 |
if (pfound->flag) |
if (pfound->flag) |
730 |
or the option starts with '--' or is not a valid short |
or the option starts with '--' or is not a valid short |
731 |
option, then it's an error. |
option, then it's an error. |
732 |
Otherwise interpret it as a short option. */ |
Otherwise interpret it as a short option. */ |
733 |
if (!long_only || argv[optind][1] == '-' |
if (!long_only || argv[d->optind][1] == '-' |
734 |
|| my_index (optstring, *nextchar) == NULL) |
|| strchr (optstring, *d->__nextchar) == NULL) |
735 |
{ |
{ |
736 |
if (print_errors) |
if (print_errors) |
737 |
{ |
{ |
740 |
int n; |
int n; |
741 |
#endif |
#endif |
742 |
|
|
743 |
if (argv[optind][1] == '-') |
if (argv[d->optind][1] == '-') |
744 |
{ |
{ |
745 |
/* --option */ |
/* --option */ |
746 |
#if defined _LIBC && defined USE_IN_LIBIO |
#if defined _LIBC && defined USE_IN_LIBIO |
747 |
n = __asprintf (&buf, _("%s: unrecognized option `--%s'\n"), |
n = __asprintf (&buf, _("%s: unrecognized option `--%s'\n"), |
748 |
argv[0], nextchar); |
argv[0], d->__nextchar); |
749 |
#else |
#else |
750 |
fprintf (stderr, _("%s: unrecognized option `--%s'\n"), |
fprintf (stderr, _("%s: unrecognized option `--%s'\n"), |
751 |
argv[0], nextchar); |
argv[0], d->__nextchar); |
752 |
#endif |
#endif |
753 |
} |
} |
754 |
else |
else |
756 |
/* +option or -option */ |
/* +option or -option */ |
757 |
#if defined _LIBC && defined USE_IN_LIBIO |
#if defined _LIBC && defined USE_IN_LIBIO |
758 |
n = __asprintf (&buf, _("%s: unrecognized option `%c%s'\n"), |
n = __asprintf (&buf, _("%s: unrecognized option `%c%s'\n"), |
759 |
argv[0], argv[optind][0], nextchar); |
argv[0], argv[d->optind][0], d->__nextchar); |
760 |
#else |
#else |
761 |
fprintf (stderr, _("%s: unrecognized option `%c%s'\n"), |
fprintf (stderr, _("%s: unrecognized option `%c%s'\n"), |
762 |
argv[0], argv[optind][0], nextchar); |
argv[0], argv[d->optind][0], d->__nextchar); |
763 |
#endif |
#endif |
764 |
} |
} |
765 |
|
|
766 |
#if defined _LIBC && defined USE_IN_LIBIO |
#if defined _LIBC && defined USE_IN_LIBIO |
767 |
if (n >= 0) |
if (n >= 0) |
768 |
{ |
{ |
769 |
|
_IO_flockfile (stderr); |
770 |
|
|
771 |
|
int old_flags2 = ((_IO_FILE *) stderr)->_flags2; |
772 |
|
((_IO_FILE *) stderr)->_flags2 |= _IO_FLAGS2_NOTCANCEL; |
773 |
|
|
774 |
if (_IO_fwide (stderr, 0) > 0) |
if (_IO_fwide (stderr, 0) > 0) |
775 |
__fwprintf (stderr, L"%s", buf); |
__fwprintf (stderr, L"%s", buf); |
776 |
else |
else |
777 |
fputs (buf, stderr); |
fputs (buf, stderr); |
778 |
|
|
779 |
|
((_IO_FILE *) stderr)->_flags2 = old_flags2; |
780 |
|
_IO_funlockfile (stderr); |
781 |
|
|
782 |
free (buf); |
free (buf); |
783 |
} |
} |
784 |
#endif |
#endif |
785 |
} |
} |
786 |
nextchar = (char *) ""; |
d->__nextchar = (char *) ""; |
787 |
optind++; |
d->optind++; |
788 |
optopt = 0; |
d->optopt = 0; |
789 |
return '?'; |
return '?'; |
790 |
} |
} |
791 |
} |
} |
793 |
/* Look at and handle the next short option-character. */ |
/* Look at and handle the next short option-character. */ |
794 |
|
|
795 |
{ |
{ |
796 |
char c = *nextchar++; |
char c = *d->__nextchar++; |
797 |
char *temp = my_index (optstring, c); |
char *temp = strchr (optstring, c); |
798 |
|
|
799 |
/* Increment `optind' when we start to process its last character. */ |
/* Increment `optind' when we start to process its last character. */ |
800 |
if (*nextchar == '\0') |
if (*d->__nextchar == '\0') |
801 |
++optind; |
++d->optind; |
802 |
|
|
803 |
if (temp == NULL || c == ':') |
if (temp == NULL || c == ':') |
804 |
{ |
{ |
809 |
int n; |
int n; |
810 |
#endif |
#endif |
811 |
|
|
812 |
if (posixly_correct) |
if (d->__posixly_correct) |
813 |
{ |
{ |
814 |
/* 1003.2 specifies the format of this message. */ |
/* 1003.2 specifies the format of this message. */ |
815 |
#if defined _LIBC && defined USE_IN_LIBIO |
#if defined _LIBC && defined USE_IN_LIBIO |
832 |
#if defined _LIBC && defined USE_IN_LIBIO |
#if defined _LIBC && defined USE_IN_LIBIO |
833 |
if (n >= 0) |
if (n >= 0) |
834 |
{ |
{ |
835 |
|
_IO_flockfile (stderr); |
836 |
|
|
837 |
|
int old_flags2 = ((_IO_FILE *) stderr)->_flags2; |
838 |
|
((_IO_FILE *) stderr)->_flags2 |= _IO_FLAGS2_NOTCANCEL; |
839 |
|
|
840 |
if (_IO_fwide (stderr, 0) > 0) |
if (_IO_fwide (stderr, 0) > 0) |
841 |
__fwprintf (stderr, L"%s", buf); |
__fwprintf (stderr, L"%s", buf); |
842 |
else |
else |
843 |
fputs (buf, stderr); |
fputs (buf, stderr); |
844 |
|
|
845 |
|
((_IO_FILE *) stderr)->_flags2 = old_flags2; |
846 |
|
_IO_funlockfile (stderr); |
847 |
|
|
848 |
free (buf); |
free (buf); |
849 |
} |
} |
850 |
#endif |
#endif |
851 |
} |
} |
852 |
optopt = c; |
d->optopt = c; |
853 |
return '?'; |
return '?'; |
854 |
} |
} |
855 |
/* Convenience. Treat POSIX -W foo same as long option --foo */ |
/* Convenience. Treat POSIX -W foo same as long option --foo */ |
864 |
int option_index; |
int option_index; |
865 |
|
|
866 |
/* This is an option that requires an argument. */ |
/* This is an option that requires an argument. */ |
867 |
if (*nextchar != '\0') |
if (*d->__nextchar != '\0') |
868 |
{ |
{ |
869 |
optarg = nextchar; |
d->optarg = d->__nextchar; |
870 |
/* If we end this ARGV-element by taking the rest as an arg, |
/* If we end this ARGV-element by taking the rest as an arg, |
871 |
we must advance to the next element now. */ |
we must advance to the next element now. */ |
872 |
optind++; |
d->optind++; |
873 |
} |
} |
874 |
else if (optind == argc) |
else if (d->optind == argc) |
875 |
{ |
{ |
876 |
if (print_errors) |
if (print_errors) |
877 |
{ |
{ |
883 |
_("%s: option requires an argument -- %c\n"), |
_("%s: option requires an argument -- %c\n"), |
884 |
argv[0], c) >= 0) |
argv[0], c) >= 0) |
885 |
{ |
{ |
886 |
|
_IO_flockfile (stderr); |
887 |
|
|
888 |
|
int old_flags2 = ((_IO_FILE *) stderr)->_flags2; |
889 |
|
((_IO_FILE *) stderr)->_flags2 |= _IO_FLAGS2_NOTCANCEL; |
890 |
|
|
891 |
if (_IO_fwide (stderr, 0) > 0) |
if (_IO_fwide (stderr, 0) > 0) |
892 |
__fwprintf (stderr, L"%s", buf); |
__fwprintf (stderr, L"%s", buf); |
893 |
else |
else |
894 |
fputs (buf, stderr); |
fputs (buf, stderr); |
895 |
|
|
896 |
|
((_IO_FILE *) stderr)->_flags2 = old_flags2; |
897 |
|
_IO_funlockfile (stderr); |
898 |
|
|
899 |
free (buf); |
free (buf); |
900 |
} |
} |
901 |
#else |
#else |
903 |
argv[0], c); |
argv[0], c); |
904 |
#endif |
#endif |
905 |
} |
} |
906 |
optopt = c; |
d->optopt = c; |
907 |
if (optstring[0] == ':') |
if (optstring[0] == ':') |
908 |
c = ':'; |
c = ':'; |
909 |
else |
else |
911 |
return c; |
return c; |
912 |
} |
} |
913 |
else |
else |
914 |
/* We already incremented `optind' once; |
/* We already incremented `d->optind' once; |
915 |
increment it again when taking next ARGV-elt as argument. */ |
increment it again when taking next ARGV-elt as argument. */ |
916 |
optarg = argv[optind++]; |
d->optarg = argv[d->optind++]; |
917 |
|
|
918 |
/* optarg is now the argument, see if it's in the |
/* optarg is now the argument, see if it's in the |
919 |
table of longopts. */ |
table of longopts. */ |
920 |
|
|
921 |
for (nextchar = nameend = optarg; *nameend && *nameend != '='; nameend++) |
for (d->__nextchar = nameend = d->optarg; *nameend && *nameend != '='; |
922 |
|
nameend++) |
923 |
/* Do nothing. */ ; |
/* Do nothing. */ ; |
924 |
|
|
925 |
/* Test all long options for either exact match |
/* Test all long options for either exact match |
926 |
or abbreviated matches. */ |
or abbreviated matches. */ |
927 |
for (p = longopts, option_index = 0; p->name; p++, option_index++) |
for (p = longopts, option_index = 0; p->name; p++, option_index++) |
928 |
if (!strncmp (p->name, nextchar, nameend - nextchar)) |
if (!strncmp (p->name, d->__nextchar, nameend - d->__nextchar)) |
929 |
{ |
{ |
930 |
if ((unsigned int) (nameend - nextchar) == strlen (p->name)) |
if ((unsigned int) (nameend - d->__nextchar) == strlen (p->name)) |
931 |
{ |
{ |
932 |
/* Exact match found. */ |
/* Exact match found. */ |
933 |
pfound = p; |
pfound = p; |
953 |
char *buf; |
char *buf; |
954 |
|
|
955 |
if (__asprintf (&buf, _("%s: option `-W %s' is ambiguous\n"), |
if (__asprintf (&buf, _("%s: option `-W %s' is ambiguous\n"), |
956 |
argv[0], argv[optind]) >= 0) |
argv[0], argv[d->optind]) >= 0) |
957 |
{ |
{ |
958 |
|
_IO_flockfile (stderr); |
959 |
|
|
960 |
|
int old_flags2 = ((_IO_FILE *) stderr)->_flags2; |
961 |
|
((_IO_FILE *) stderr)->_flags2 |= _IO_FLAGS2_NOTCANCEL; |
962 |
|
|
963 |
if (_IO_fwide (stderr, 0) > 0) |
if (_IO_fwide (stderr, 0) > 0) |
964 |
__fwprintf (stderr, L"%s", buf); |
__fwprintf (stderr, L"%s", buf); |
965 |
else |
else |
966 |
fputs (buf, stderr); |
fputs (buf, stderr); |
967 |
|
|
968 |
|
((_IO_FILE *) stderr)->_flags2 = old_flags2; |
969 |
|
_IO_funlockfile (stderr); |
970 |
|
|
971 |
free (buf); |
free (buf); |
972 |
} |
} |
973 |
#else |
#else |
974 |
fprintf (stderr, _("%s: option `-W %s' is ambiguous\n"), |
fprintf (stderr, _("%s: option `-W %s' is ambiguous\n"), |
975 |
argv[0], argv[optind]); |
argv[0], argv[d->optind]); |
976 |
#endif |
#endif |
977 |
} |
} |
978 |
nextchar += strlen (nextchar); |
d->__nextchar += strlen (d->__nextchar); |
979 |
optind++; |
d->optind++; |
980 |
return '?'; |
return '?'; |
981 |
} |
} |
982 |
if (pfound != NULL) |
if (pfound != NULL) |
987 |
/* Don't test has_arg with >, because some C compilers don't |
/* Don't test has_arg with >, because some C compilers don't |
988 |
allow it to be used on enums. */ |
allow it to be used on enums. */ |
989 |
if (pfound->has_arg) |
if (pfound->has_arg) |
990 |
optarg = nameend + 1; |
d->optarg = nameend + 1; |
991 |
else |
else |
992 |
{ |
{ |
993 |
if (print_errors) |
if (print_errors) |
999 |
%s: option `-W %s' doesn't allow an argument\n"), |
%s: option `-W %s' doesn't allow an argument\n"), |
1000 |
argv[0], pfound->name) >= 0) |
argv[0], pfound->name) >= 0) |
1001 |
{ |
{ |
1002 |
|
_IO_flockfile (stderr); |
1003 |
|
|
1004 |
|
int old_flags2 = ((_IO_FILE *) stderr)->_flags2; |
1005 |
|
((_IO_FILE *) stderr)->_flags2 |
1006 |
|
|= _IO_FLAGS2_NOTCANCEL; |
1007 |
|
|
1008 |
if (_IO_fwide (stderr, 0) > 0) |
if (_IO_fwide (stderr, 0) > 0) |
1009 |
__fwprintf (stderr, L"%s", buf); |
__fwprintf (stderr, L"%s", buf); |
1010 |
else |
else |
1011 |
fputs (buf, stderr); |
fputs (buf, stderr); |
1012 |
|
|
1013 |
|
((_IO_FILE *) stderr)->_flags2 = old_flags2; |
1014 |
|
_IO_funlockfile (stderr); |
1015 |
|
|
1016 |
free (buf); |
free (buf); |
1017 |
} |
} |
1018 |
#else |
#else |
1022 |
#endif |
#endif |
1023 |
} |
} |
1024 |
|
|
1025 |
nextchar += strlen (nextchar); |
d->__nextchar += strlen (d->__nextchar); |
1026 |
return '?'; |
return '?'; |
1027 |
} |
} |
1028 |
} |
} |
1029 |
else if (pfound->has_arg == 1) |
else if (pfound->has_arg == 1) |
1030 |
{ |
{ |
1031 |
if (optind < argc) |
if (d->optind < argc) |
1032 |
optarg = argv[optind++]; |
d->optarg = argv[d->optind++]; |
1033 |
else |
else |
1034 |
{ |
{ |
1035 |
if (print_errors) |
if (print_errors) |
1039 |
|
|
1040 |
if (__asprintf (&buf, _("\ |
if (__asprintf (&buf, _("\ |
1041 |
%s: option `%s' requires an argument\n"), |
%s: option `%s' requires an argument\n"), |
1042 |
argv[0], argv[optind - 1]) >= 0) |
argv[0], argv[d->optind - 1]) >= 0) |
1043 |
{ |
{ |
1044 |
|
_IO_flockfile (stderr); |
1045 |
|
|
1046 |
|
int old_flags2 = ((_IO_FILE *) stderr)->_flags2; |
1047 |
|
((_IO_FILE *) stderr)->_flags2 |
1048 |
|
|= _IO_FLAGS2_NOTCANCEL; |
1049 |
|
|
1050 |
if (_IO_fwide (stderr, 0) > 0) |
if (_IO_fwide (stderr, 0) > 0) |
1051 |
__fwprintf (stderr, L"%s", buf); |
__fwprintf (stderr, L"%s", buf); |
1052 |
else |
else |
1053 |
fputs (buf, stderr); |
fputs (buf, stderr); |
1054 |
|
|
1055 |
|
((_IO_FILE *) stderr)->_flags2 = old_flags2; |
1056 |
|
_IO_funlockfile (stderr); |
1057 |
|
|
1058 |
free (buf); |
free (buf); |
1059 |
} |
} |
1060 |
#else |
#else |
1061 |
fprintf (stderr, |
fprintf (stderr, |
1062 |
_("%s: option `%s' requires an argument\n"), |
_("%s: option `%s' requires an argument\n"), |
1063 |
argv[0], argv[optind - 1]); |
argv[0], argv[d->optind - 1]); |
1064 |
#endif |
#endif |
1065 |
} |
} |
1066 |
nextchar += strlen (nextchar); |
d->__nextchar += strlen (d->__nextchar); |
1067 |
return optstring[0] == ':' ? ':' : '?'; |
return optstring[0] == ':' ? ':' : '?'; |
1068 |
} |
} |
1069 |
} |
} |
1070 |
nextchar += strlen (nextchar); |
d->__nextchar += strlen (d->__nextchar); |
1071 |
if (longind != NULL) |
if (longind != NULL) |
1072 |
*longind = option_index; |
*longind = option_index; |
1073 |
if (pfound->flag) |
if (pfound->flag) |
1077 |
} |
} |
1078 |
return pfound->val; |
return pfound->val; |
1079 |
} |
} |
1080 |
nextchar = NULL; |
d->__nextchar = NULL; |
1081 |
return 'W'; /* Let the application handle it. */ |
return 'W'; /* Let the application handle it. */ |
1082 |
} |
} |
1083 |
if (temp[1] == ':') |
if (temp[1] == ':') |
1085 |
if (temp[2] == ':') |
if (temp[2] == ':') |
1086 |
{ |
{ |
1087 |
/* This is an option that accepts an argument optionally. */ |
/* This is an option that accepts an argument optionally. */ |
1088 |
if (*nextchar != '\0') |
if (*d->__nextchar != '\0') |
1089 |
{ |
{ |
1090 |
optarg = nextchar; |
d->optarg = d->__nextchar; |
1091 |
optind++; |
d->optind++; |
1092 |
} |
} |
1093 |
else |
else |
1094 |
optarg = NULL; |
d->optarg = NULL; |
1095 |
nextchar = NULL; |
d->__nextchar = NULL; |
1096 |
} |
} |
1097 |
else |
else |
1098 |
{ |
{ |
1099 |
/* This is an option that requires an argument. */ |
/* This is an option that requires an argument. */ |
1100 |
if (*nextchar != '\0') |
if (*d->__nextchar != '\0') |
1101 |
{ |
{ |
1102 |
optarg = nextchar; |
d->optarg = d->__nextchar; |
1103 |
/* If we end this ARGV-element by taking the rest as an arg, |
/* If we end this ARGV-element by taking the rest as an arg, |
1104 |
we must advance to the next element now. */ |
we must advance to the next element now. */ |
1105 |
optind++; |
d->optind++; |
1106 |
} |
} |
1107 |
else if (optind == argc) |
else if (d->optind == argc) |
1108 |
{ |
{ |
1109 |
if (print_errors) |
if (print_errors) |
1110 |
{ |
{ |
1116 |
%s: option requires an argument -- %c\n"), |
%s: option requires an argument -- %c\n"), |
1117 |
argv[0], c) >= 0) |
argv[0], c) >= 0) |
1118 |
{ |
{ |
1119 |
|
_IO_flockfile (stderr); |
1120 |
|
|
1121 |
|
int old_flags2 = ((_IO_FILE *) stderr)->_flags2; |
1122 |
|
((_IO_FILE *) stderr)->_flags2 |= _IO_FLAGS2_NOTCANCEL; |
1123 |
|
|
1124 |
if (_IO_fwide (stderr, 0) > 0) |
if (_IO_fwide (stderr, 0) > 0) |
1125 |
__fwprintf (stderr, L"%s", buf); |
__fwprintf (stderr, L"%s", buf); |
1126 |
else |
else |
1127 |
fputs (buf, stderr); |
fputs (buf, stderr); |
1128 |
|
|
1129 |
|
((_IO_FILE *) stderr)->_flags2 = old_flags2; |
1130 |
|
_IO_funlockfile (stderr); |
1131 |
|
|
1132 |
free (buf); |
free (buf); |
1133 |
} |
} |
1134 |
#else |
#else |
1137 |
argv[0], c); |
argv[0], c); |
1138 |
#endif |
#endif |
1139 |
} |
} |
1140 |
optopt = c; |
d->optopt = c; |
1141 |
if (optstring[0] == ':') |
if (optstring[0] == ':') |
1142 |
c = ':'; |
c = ':'; |
1143 |
else |
else |
1146 |
else |
else |
1147 |
/* We already incremented `optind' once; |
/* We already incremented `optind' once; |
1148 |
increment it again when taking next ARGV-elt as argument. */ |
increment it again when taking next ARGV-elt as argument. */ |
1149 |
optarg = argv[optind++]; |
d->optarg = argv[d->optind++]; |
1150 |
nextchar = NULL; |
d->__nextchar = NULL; |
1151 |
} |
} |
1152 |
} |
} |
1153 |
return c; |
return c; |
1155 |
} |
} |
1156 |
|
|
1157 |
int |
int |
1158 |
getopt (argc, argv, optstring) |
_getopt_internal (int argc, char *const *argv, const char *optstring, |
1159 |
int argc; |
const struct option *longopts, int *longind, int long_only) |
1160 |
char *const *argv; |
{ |
1161 |
const char *optstring; |
int result; |
1162 |
|
|
1163 |
|
getopt_data.optind = optind; |
1164 |
|
getopt_data.opterr = opterr; |
1165 |
|
|
1166 |
|
result = _getopt_internal_r (argc, argv, optstring, longopts, |
1167 |
|
longind, long_only, &getopt_data); |
1168 |
|
|
1169 |
|
optind = getopt_data.optind; |
1170 |
|
optarg = getopt_data.optarg; |
1171 |
|
optopt = getopt_data.optopt; |
1172 |
|
|
1173 |
|
return result; |
1174 |
|
} |
1175 |
|
|
1176 |
|
int |
1177 |
|
getopt (int argc, char *const *argv, const char *optstring) |
1178 |
{ |
{ |
1179 |
return _getopt_internal (argc, argv, optstring, |
return _getopt_internal (argc, argv, optstring, |
1180 |
(const struct option *) 0, |
(const struct option *) 0, |
1190 |
the above definition of `getopt'. */ |
the above definition of `getopt'. */ |
1191 |
|
|
1192 |
int |
int |
1193 |
main (argc, argv) |
main (int argc, char **argv) |
|
int argc; |
|
|
char **argv; |
|
1194 |
{ |
{ |
1195 |
int c; |
int c; |
1196 |
int digit_optind = 0; |
int digit_optind = 0; |