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