| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
#define HAVE_STRING_H 1 |
| 28 |
|
| 29 |
#ifndef _NO_PROTO |
| 30 |
# define _NO_PROTO |
| 31 |
#endif |
| 32 |
|
| 33 |
#ifdef HAVE_CONFIG_H |
| 34 |
# include <config.h> |
| 35 |
#endif |
| 36 |
|
| 37 |
#if !defined __STDC__ || !__STDC__ |
| 38 |
|
| 39 |
|
| 40 |
# ifndef const |
| 41 |
# define const |
| 42 |
# endif |
| 43 |
#endif |
| 44 |
|
| 45 |
#include <stdio.h> |
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
#define GETOPT_INTERFACE_VERSION 2 |
| 56 |
#if !defined _LIBC && defined __GLIBC__ && __GLIBC__ >= 2 |
| 57 |
# include <gnu-versions.h> |
| 58 |
# if _GNU_GETOPT_INTERFACE_VERSION == GETOPT_INTERFACE_VERSION |
| 59 |
# define ELIDE_CODE |
| 60 |
# endif |
| 61 |
#endif |
| 62 |
|
| 63 |
#ifndef ELIDE_CODE |
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
#ifdef __GNU_LIBRARY__ |
| 69 |
|
| 70 |
|
| 71 |
# include <stdlib.h> |
| 72 |
# include <unistd.h> |
| 73 |
#endif |
| 74 |
|
| 75 |
#ifdef VMS |
| 76 |
# include <unixlib.h> |
| 77 |
# if HAVE_STRING_H - 0 |
| 78 |
# include <string.h> |
| 79 |
# endif |
| 80 |
#endif |
| 81 |
|
| 82 |
#ifndef _ |
| 83 |
|
| 84 |
# if (HAVE_LIBINTL_H && ENABLE_NLS) || defined _LIBC |
| 85 |
# include <libintl.h> |
| 86 |
# ifndef _ |
| 87 |
# define _(msgid) gettext (msgid) |
| 88 |
# endif |
| 89 |
# else |
| 90 |
# define _(msgid) (msgid) |
| 91 |
# endif |
| 92 |
# if defined _LIBC && defined USE_IN_LIBIO |
| 93 |
# include <wchar.h> |
| 94 |
# endif |
| 95 |
#endif |
| 96 |
|
| 97 |
#ifndef attribute_hidden |
| 98 |
# define attribute_hidden |
| 99 |
#endif |
| 100 |
|
| 101 |
|
| 102 |
|
| 103 |
|
| 104 |
|
| 105 |
|
| 106 |
|
| 107 |
|
| 108 |
|
| 109 |
|
| 110 |
|
| 111 |
|
| 112 |
|
| 113 |
|
| 114 |
|
| 115 |
#include "getopt.h" |
| 116 |
|
| 117 |
|
| 118 |
|
| 119 |
|
| 120 |
|
| 121 |
|
| 122 |
|
| 123 |
char *optarg; |
| 124 |
|
| 125 |
|
| 126 |
|
| 127 |
|
| 128 |
|
| 129 |
|
| 130 |
|
| 131 |
|
| 132 |
|
| 133 |
|
| 134 |
|
| 135 |
|
| 136 |
|
| 137 |
|
| 138 |
int optind = 1; |
| 139 |
|
| 140 |
|
| 141 |
|
| 142 |
|
| 143 |
|
| 144 |
int __getopt_initialized attribute_hidden; |
| 145 |
|
| 146 |
|
| 147 |
|
| 148 |
|
| 149 |
|
| 150 |
|
| 151 |
|
| 152 |
|
| 153 |
static char *nextchar; |
| 154 |
|
| 155 |
|
| 156 |
|
| 157 |
|
| 158 |
int opterr = 1; |
| 159 |
|
| 160 |
|
| 161 |
|
| 162 |
|
| 163 |
|
| 164 |
int optopt = '?'; |
| 165 |
|
| 166 |
|
| 167 |
|
| 168 |
|
| 169 |
|
| 170 |
|
| 171 |
|
| 172 |
|
| 173 |
|
| 174 |
|
| 175 |
|
| 176 |
|
| 177 |
|
| 178 |
|
| 179 |
|
| 180 |
|
| 181 |
|
| 182 |
|
| 183 |
|
| 184 |
|
| 185 |
|
| 186 |
|
| 187 |
|
| 188 |
|
| 189 |
|
| 190 |
|
| 191 |
|
| 192 |
|
| 193 |
|
| 194 |
|
| 195 |
static enum |
| 196 |
{ |
| 197 |
REQUIRE_ORDER, PERMUTE, RETURN_IN_ORDER |
| 198 |
} ordering; |
| 199 |
|
| 200 |
|
| 201 |
static char *posixly_correct; |
| 202 |
|
| 203 |
#ifdef __GNU_LIBRARY__ |
| 204 |
|
| 205 |
|
| 206 |
|
| 207 |
|
| 208 |
# include <string.h> |
| 209 |
# define my_index strchr |
| 210 |
#else |
| 211 |
|
| 212 |
# if HAVE_STRING_H |
| 213 |
# include <string.h> |
| 214 |
# else |
| 215 |
# include <strings.h> |
| 216 |
# endif |
| 217 |
|
| 218 |
|
| 219 |
|
| 220 |
|
| 221 |
#ifndef getenv |
| 222 |
extern char *getenv (); |
| 223 |
#endif |
| 224 |
|
| 225 |
static char * |
| 226 |
my_index (str, chr) |
| 227 |
const char *str; |
| 228 |
int chr; |
| 229 |
{ |
| 230 |
while (*str) |
| 231 |
{ |
| 232 |
if (*str == chr) |
| 233 |
return (char *) str; |
| 234 |
str++; |
| 235 |
} |
| 236 |
return 0; |
| 237 |
} |
| 238 |
|
| 239 |
|
| 240 |
|
| 241 |
#ifdef __GNUC__ |
| 242 |
|
| 243 |
|
| 244 |
# if (!defined __STDC__ || !__STDC__) && !defined strlen |
| 245 |
|
| 246 |
|
| 247 |
extern int strlen (const char *); |
| 248 |
# endif |
| 249 |
#endif |
| 250 |
|
| 251 |
#endif |
| 252 |
|
| 253 |
|
| 254 |
|
| 255 |
|
| 256 |
|
| 257 |
|
| 258 |
|
| 259 |
static int first_nonopt; |
| 260 |
static int last_nonopt; |
| 261 |
|
| 262 |
#ifdef _LIBC |
| 263 |
|
| 264 |
|
| 265 |
|
| 266 |
extern int __libc_argc; |
| 267 |
extern char **__libc_argv; |
| 268 |
|
| 269 |
|
| 270 |
|
| 271 |
|
| 272 |
# ifdef USE_NONOPTION_FLAGS |
| 273 |
|
| 274 |
extern char *__getopt_nonoption_flags; |
| 275 |
|
| 276 |
static int nonoption_flags_max_len; |
| 277 |
static int nonoption_flags_len; |
| 278 |
# endif |
| 279 |
|
| 280 |
# ifdef USE_NONOPTION_FLAGS |
| 281 |
# define SWAP_FLAGS(ch1, ch2) \ |
| 282 |
if (nonoption_flags_len > 0) \ |
| 283 |
{ \ |
| 284 |
char __tmp = __getopt_nonoption_flags[ch1]; \ |
| 285 |
__getopt_nonoption_flags[ch1] = __getopt_nonoption_flags[ch2]; \ |
| 286 |
__getopt_nonoption_flags[ch2] = __tmp; \ |
| 287 |
} |
| 288 |
# else |
| 289 |
# define SWAP_FLAGS(ch1, ch2) |
| 290 |
# endif |
| 291 |
#else |
| 292 |
# define SWAP_FLAGS(ch1, ch2) |
| 293 |
#endif |
| 294 |
|
| 295 |
|
| 296 |
|
| 297 |
|
| 298 |
|
| 299 |
|
| 300 |
|
| 301 |
|
| 302 |
|
| 303 |
|
| 304 |
#if defined __STDC__ && __STDC__ |
| 305 |
static void exchange (char **); |
| 306 |
#endif |
| 307 |
|
| 308 |
static void |
| 309 |
exchange (argv) |
| 310 |
char **argv; |
| 311 |
{ |
| 312 |
int bottom = first_nonopt; |
| 313 |
int middle = last_nonopt; |
| 314 |
int top = optind; |
| 315 |
char *tem; |
| 316 |
|
| 317 |
|
| 318 |
|
| 319 |
|
| 320 |
|
| 321 |
|
| 322 |
#if defined _LIBC && defined USE_NONOPTION_FLAGS |
| 323 |
|
| 324 |
|
| 325 |
|
| 326 |
if (nonoption_flags_len > 0 && top >= nonoption_flags_max_len) |
| 327 |
{ |
| 328 |
|
| 329 |
|
| 330 |
char *new_str = malloc (top + 1); |
| 331 |
if (new_str == NULL) |
| 332 |
nonoption_flags_len = nonoption_flags_max_len = 0; |
| 333 |
else |
| 334 |
{ |
| 335 |
memset (__mempcpy (new_str, __getopt_nonoption_flags, |
| 336 |
nonoption_flags_max_len), |
| 337 |
'\0', top + 1 - nonoption_flags_max_len); |
| 338 |
nonoption_flags_max_len = top + 1; |
| 339 |
__getopt_nonoption_flags = new_str; |
| 340 |
} |
| 341 |
} |
| 342 |
#endif |
| 343 |
|
| 344 |
while (top > middle && middle > bottom) |
| 345 |
{ |
| 346 |
if (top - middle > middle - bottom) |
| 347 |
{ |
| 348 |
|
| 349 |
int len = middle - bottom; |
| 350 |
register int i; |
| 351 |
|
| 352 |
|
| 353 |
for (i = 0; i < len; i++) |
| 354 |
{ |
| 355 |
tem = argv[bottom + i]; |
| 356 |
argv[bottom + i] = argv[top - (middle - bottom) + i]; |
| 357 |
argv[top - (middle - bottom) + i] = tem; |
| 358 |
SWAP_FLAGS (bottom + i, top - (middle - bottom) + i); |
| 359 |
} |
| 360 |
|
| 361 |
top -= len; |
| 362 |
} |
| 363 |
else |
| 364 |
{ |
| 365 |
|
| 366 |
int len = top - middle; |
| 367 |
register int i; |
| 368 |
|
| 369 |
|
| 370 |
for (i = 0; i < len; i++) |
| 371 |
{ |
| 372 |
tem = argv[bottom + i]; |
| 373 |
argv[bottom + i] = argv[middle + i]; |
| 374 |
argv[middle + i] = tem; |
| 375 |
SWAP_FLAGS (bottom + i, middle + i); |
| 376 |
} |
| 377 |
|
| 378 |
bottom += len; |
| 379 |
} |
| 380 |
} |
| 381 |
|
| 382 |
|
| 383 |
|
| 384 |
first_nonopt += (optind - last_nonopt); |
| 385 |
last_nonopt = optind; |
| 386 |
} |
| 387 |
|
| 388 |
|
| 389 |
|
| 390 |
#if defined __STDC__ && __STDC__ |
| 391 |
static const char *_getopt_initialize (int, char *const *, const char *); |
| 392 |
#endif |
| 393 |
static const char * |
| 394 |
_getopt_initialize (argc, argv, optstring) |
| 395 |
int argc; |
| 396 |
char *const *argv; |
| 397 |
const char *optstring; |
| 398 |
{ |
| 399 |
|
| 400 |
|
| 401 |
|
| 402 |
|
| 403 |
first_nonopt = last_nonopt = optind; |
| 404 |
|
| 405 |
nextchar = NULL; |
| 406 |
|
| 407 |
posixly_correct = getenv ("POSIXLY_CORRECT"); |
| 408 |
|
| 409 |
|
| 410 |
|
| 411 |
if (optstring[0] == '-') |
| 412 |
{ |
| 413 |
ordering = RETURN_IN_ORDER; |
| 414 |
++optstring; |
| 415 |
} |
| 416 |
else if (optstring[0] == '+') |
| 417 |
{ |
| 418 |
ordering = REQUIRE_ORDER; |
| 419 |
++optstring; |
| 420 |
} |
| 421 |
else if (posixly_correct != NULL) |
| 422 |
ordering = REQUIRE_ORDER; |
| 423 |
else |
| 424 |
ordering = PERMUTE; |
| 425 |
|
| 426 |
#if defined _LIBC && defined USE_NONOPTION_FLAGS |
| 427 |
if (posixly_correct == NULL |
| 428 |
&& argc == __libc_argc && argv == __libc_argv) |
| 429 |
{ |
| 430 |
if (nonoption_flags_max_len == 0) |
| 431 |
{ |
| 432 |
if (__getopt_nonoption_flags == NULL |
| 433 |
|| __getopt_nonoption_flags[0] == '\0') |
| 434 |
nonoption_flags_max_len = -1; |
| 435 |
else |
| 436 |
{ |
| 437 |
const char *orig_str = __getopt_nonoption_flags; |
| 438 |
int len = nonoption_flags_max_len = strlen (orig_str); |
| 439 |
if (nonoption_flags_max_len < argc) |
| 440 |
nonoption_flags_max_len = argc; |
| 441 |
__getopt_nonoption_flags = |
| 442 |
(char *) malloc (nonoption_flags_max_len); |
| 443 |
if (__getopt_nonoption_flags == NULL) |
| 444 |
nonoption_flags_max_len = -1; |
| 445 |
else |
| 446 |
memset (__mempcpy (__getopt_nonoption_flags, orig_str, len), |
| 447 |
'\0', nonoption_flags_max_len - len); |
| 448 |
} |
| 449 |
} |
| 450 |
nonoption_flags_len = nonoption_flags_max_len; |
| 451 |
} |
| 452 |
else |
| 453 |
nonoption_flags_len = 0; |
| 454 |
#endif |
| 455 |
|
| 456 |
return optstring; |
| 457 |
} |
| 458 |
|
| 459 |
|
| 460 |
|
| 461 |
|
| 462 |
|
| 463 |
|
| 464 |
|
| 465 |
|
| 466 |
|
| 467 |
|
| 468 |
|
| 469 |
|
| 470 |
|
| 471 |
|
| 472 |
|
| 473 |
|
| 474 |
|
| 475 |
|
| 476 |
|
| 477 |
|
| 478 |
|
| 479 |
|
| 480 |
|
| 481 |
|
| 482 |
|
| 483 |
|
| 484 |
|
| 485 |
|
| 486 |
|
| 487 |
|
| 488 |
|
| 489 |
|
| 490 |
|
| 491 |
|
| 492 |
|
| 493 |
|
| 494 |
|
| 495 |
|
| 496 |
|
| 497 |
|
| 498 |
|
| 499 |
|
| 500 |
|
| 501 |
|
| 502 |
|
| 503 |
|
| 504 |
|
| 505 |
|
| 506 |
|
| 507 |
|
| 508 |
|
| 509 |
|
| 510 |
|
| 511 |
|
| 512 |
|
| 513 |
|
| 514 |
|
| 515 |
int |
| 516 |
_getopt_internal (argc, argv, optstring, longopts, longind, long_only) |
| 517 |
int argc; |
| 518 |
char *const *argv; |
| 519 |
const char *optstring; |
| 520 |
const struct option *longopts; |
| 521 |
int *longind; |
| 522 |
int long_only; |
| 523 |
{ |
| 524 |
int print_errors = opterr; |
| 525 |
if (optstring[0] == ':') |
| 526 |
print_errors = 0; |
| 527 |
|
| 528 |
if (argc < 1) |
| 529 |
return -1; |
| 530 |
|
| 531 |
optarg = NULL; |
| 532 |
|
| 533 |
if (optind == 0 || !__getopt_initialized) |
| 534 |
{ |
| 535 |
if (optind == 0) |
| 536 |
optind = 1; |
| 537 |
optstring = _getopt_initialize (argc, argv, optstring); |
| 538 |
__getopt_initialized = 1; |
| 539 |
} |
| 540 |
|
| 541 |
|
| 542 |
|
| 543 |
|
| 544 |
|
| 545 |
#if defined _LIBC && defined USE_NONOPTION_FLAGS |
| 546 |
# define NONOPTION_P (argv[optind][0] != '-' || argv[optind][1] == '\0' \ |
| 547 |
|| (optind < nonoption_flags_len \ |
| 548 |
&& __getopt_nonoption_flags[optind] == '1')) |
| 549 |
#else |
| 550 |
# define NONOPTION_P (argv[optind][0] != '-' || argv[optind][1] == '\0') |
| 551 |
#endif |
| 552 |
|
| 553 |
if (nextchar == NULL || *nextchar == '\0') |
| 554 |
{ |
| 555 |
|
| 556 |
|
| 557 |
|
| 558 |
|
| 559 |
if (last_nonopt > optind) |
| 560 |
last_nonopt = optind; |
| 561 |
if (first_nonopt > optind) |
| 562 |
first_nonopt = optind; |
| 563 |
|
| 564 |
if (ordering == PERMUTE) |
| 565 |
{ |
| 566 |
|
| 567 |
|
| 568 |
|
| 569 |
if (first_nonopt != last_nonopt && last_nonopt != optind) |
| 570 |
exchange ((char **) argv); |
| 571 |
else if (last_nonopt != optind) |
| 572 |
first_nonopt = optind; |
| 573 |
|
| 574 |
|
| 575 |
|
| 576 |
|
| 577 |
while (optind < argc && NONOPTION_P) |
| 578 |
optind++; |
| 579 |
last_nonopt = optind; |
| 580 |
} |
| 581 |
|
| 582 |
|
| 583 |
|
| 584 |
|
| 585 |
|
| 586 |
|
| 587 |
if (optind != argc && !strcmp (argv[optind], "--")) |
| 588 |
{ |
| 589 |
optind++; |
| 590 |
|
| 591 |
if (first_nonopt != last_nonopt && last_nonopt != optind) |
| 592 |
exchange ((char **) argv); |
| 593 |
else if (first_nonopt == last_nonopt) |
| 594 |
first_nonopt = optind; |
| 595 |
last_nonopt = argc; |
| 596 |
|
| 597 |
optind = argc; |
| 598 |
} |
| 599 |
|
| 600 |
|
| 601 |
|
| 602 |
|
| 603 |
if (optind == argc) |
| 604 |
{ |
| 605 |
|
| 606 |
|
| 607 |
if (first_nonopt != last_nonopt) |
| 608 |
optind = first_nonopt; |
| 609 |
return -1; |
| 610 |
} |
| 611 |
|
| 612 |
|
| 613 |
|
| 614 |
|
| 615 |
if (NONOPTION_P) |
| 616 |
{ |
| 617 |
if (ordering == REQUIRE_ORDER) |
| 618 |
return -1; |
| 619 |
optarg = argv[optind++]; |
| 620 |
return 1; |
| 621 |
} |
| 622 |
|
| 623 |
|
| 624 |
|
| 625 |
|
| 626 |
nextchar = (argv[optind] + 1 |
| 627 |
+ (longopts != NULL && argv[optind][1] == '-')); |
| 628 |
} |
| 629 |
|
| 630 |
|
| 631 |
|
| 632 |
|
| 633 |
|
| 634 |
|
| 635 |
|
| 636 |
|
| 637 |
|
| 638 |
|
| 639 |
|
| 640 |
|
| 641 |
|
| 642 |
|
| 643 |
|
| 644 |
|
| 645 |
if (longopts != NULL |
| 646 |
&& (argv[optind][1] == '-' |
| 647 |
|| (long_only && (argv[optind][2] || !my_index (optstring, argv[optind][1]))))) |
| 648 |
{ |
| 649 |
char *nameend; |
| 650 |
const struct option *p; |
| 651 |
const struct option *pfound = NULL; |
| 652 |
int exact = 0; |
| 653 |
int ambig = 0; |
| 654 |
int indfound = -1; |
| 655 |
int option_index; |
| 656 |
|
| 657 |
for (nameend = nextchar; *nameend && *nameend != '='; nameend++) |
| 658 |
; |
| 659 |
|
| 660 |
|
| 661 |
|
| 662 |
for (p = longopts, option_index = 0; p->name; p++, option_index++) |
| 663 |
if (!strncmp (p->name, nextchar, nameend - nextchar)) |
| 664 |
{ |
| 665 |
if ((unsigned int) (nameend - nextchar) |
| 666 |
== (unsigned int) strlen (p->name)) |
| 667 |
{ |
| 668 |
|
| 669 |
pfound = p; |
| 670 |
indfound = option_index; |
| 671 |
exact = 1; |
| 672 |
break; |
| 673 |
} |
| 674 |
else if (pfound == NULL) |
| 675 |
{ |
| 676 |
|
| 677 |
pfound = p; |
| 678 |
indfound = option_index; |
| 679 |
} |
| 680 |
else if (long_only |
| 681 |
|| pfound->has_arg != p->has_arg |
| 682 |
|| pfound->flag != p->flag |
| 683 |
|| pfound->val != p->val) |
| 684 |
|
| 685 |
ambig = 1; |
| 686 |
} |
| 687 |
|
| 688 |
if (ambig && !exact) |
| 689 |
{ |
| 690 |
if (print_errors) |
| 691 |
{ |
| 692 |
#if defined _LIBC && defined USE_IN_LIBIO |
| 693 |
char *buf; |
| 694 |
|
| 695 |
if (__asprintf (&buf, _("%s: option `%s' is ambiguous\n"), |
| 696 |
argv[0], argv[optind]) >= 0) |
| 697 |
{ |
| 698 |
|
| 699 |
if (_IO_fwide (stderr, 0) > 0) |
| 700 |
__fwprintf (stderr, L"%s", buf); |
| 701 |
else |
| 702 |
fputs (buf, stderr); |
| 703 |
|
| 704 |
free (buf); |
| 705 |
} |
| 706 |
#else |
| 707 |
fprintf (stderr, _("%s: option `%s' is ambiguous\n"), |
| 708 |
argv[0], argv[optind]); |
| 709 |
#endif |
| 710 |
} |
| 711 |
nextchar += strlen (nextchar); |
| 712 |
optind++; |
| 713 |
optopt = 0; |
| 714 |
return '?'; |
| 715 |
} |
| 716 |
|
| 717 |
if (pfound != NULL) |
| 718 |
{ |
| 719 |
option_index = indfound; |
| 720 |
optind++; |
| 721 |
if (*nameend) |
| 722 |
{ |
| 723 |
|
| 724 |
|
| 725 |
if (pfound->has_arg) |
| 726 |
optarg = nameend + 1; |
| 727 |
else |
| 728 |
{ |
| 729 |
if (print_errors) |
| 730 |
{ |
| 731 |
#if defined _LIBC && defined USE_IN_LIBIO |
| 732 |
char *buf; |
| 733 |
int n; |
| 734 |
#endif |
| 735 |
|
| 736 |
if (argv[optind - 1][1] == '-') |
| 737 |
{ |
| 738 |
|
| 739 |
#if defined _LIBC && defined USE_IN_LIBIO |
| 740 |
n = __asprintf (&buf, _("\ |
| 741 |
%s: option `--%s' doesn't allow an argument\n"), |
| 742 |
argv[0], pfound->name); |
| 743 |
#else |
| 744 |
fprintf (stderr, _("\ |
| 745 |
%s: option `--%s' doesn't allow an argument\n"), |
| 746 |
argv[0], pfound->name); |
| 747 |
#endif |
| 748 |
} |
| 749 |
else |
| 750 |
{ |
| 751 |
|
| 752 |
#if defined _LIBC && defined USE_IN_LIBIO |
| 753 |
n = __asprintf (&buf, _("\ |
| 754 |
%s: option `%c%s' doesn't allow an argument\n"), |
| 755 |
argv[0], argv[optind - 1][0], |
| 756 |
pfound->name); |
| 757 |
#else |
| 758 |
fprintf (stderr, _("\ |
| 759 |
%s: option `%c%s' doesn't allow an argument\n"), |
| 760 |
argv[0], argv[optind - 1][0], pfound->name); |
| 761 |
#endif |
| 762 |
} |
| 763 |
|
| 764 |
#if defined _LIBC && defined USE_IN_LIBIO |
| 765 |
if (n >= 0) |
| 766 |
{ |
| 767 |
if (_IO_fwide (stderr, 0) > 0) |
| 768 |
__fwprintf (stderr, L"%s", buf); |
| 769 |
else |
| 770 |
fputs (buf, stderr); |
| 771 |
|
| 772 |
free (buf); |
| 773 |
} |
| 774 |
#endif |
| 775 |
} |
| 776 |
|
| 777 |
nextchar += strlen (nextchar); |
| 778 |
|
| 779 |
optopt = pfound->val; |
| 780 |
return '?'; |
| 781 |
} |
| 782 |
} |
| 783 |
else if (pfound->has_arg == 1) |
| 784 |
{ |
| 785 |
if (optind < argc) |
| 786 |
optarg = argv[optind++]; |
| 787 |
else |
| 788 |
{ |
| 789 |
if (print_errors) |
| 790 |
{ |
| 791 |
#if defined _LIBC && defined USE_IN_LIBIO |
| 792 |
char *buf; |
| 793 |
|
| 794 |
if (__asprintf (&buf, _("\ |
| 795 |
%s: option `%s' requires an argument\n"), |
| 796 |
argv[0], argv[optind - 1]) >= 0) |
| 797 |
{ |
| 798 |
if (_IO_fwide (stderr, 0) > 0) |
| 799 |
__fwprintf (stderr, L"%s", buf); |
| 800 |
else |
| 801 |
fputs (buf, stderr); |
| 802 |
|
| 803 |
free (buf); |
| 804 |
} |
| 805 |
#else |
| 806 |
fprintf (stderr, |
| 807 |
_("%s: option `%s' requires an argument\n"), |
| 808 |
argv[0], argv[optind - 1]); |
| 809 |
#endif |
| 810 |
} |
| 811 |
nextchar += strlen (nextchar); |
| 812 |
optopt = pfound->val; |
| 813 |
return optstring[0] == ':' ? ':' : '?'; |
| 814 |
} |
| 815 |
} |
| 816 |
nextchar += strlen (nextchar); |
| 817 |
if (longind != NULL) |
| 818 |
*longind = option_index; |
| 819 |
if (pfound->flag) |
| 820 |
{ |
| 821 |
*(pfound->flag) = pfound->val; |
| 822 |
return 0; |
| 823 |
} |
| 824 |
return pfound->val; |
| 825 |
} |
| 826 |
|
| 827 |
|
| 828 |
|
| 829 |
|
| 830 |
|
| 831 |
if (!long_only || argv[optind][1] == '-' |
| 832 |
|| my_index (optstring, *nextchar) == NULL) |
| 833 |
{ |
| 834 |
if (print_errors) |
| 835 |
{ |
| 836 |
#if defined _LIBC && defined USE_IN_LIBIO |
| 837 |
char *buf; |
| 838 |
int n; |
| 839 |
#endif |
| 840 |
|
| 841 |
if (argv[optind][1] == '-') |
| 842 |
{ |
| 843 |
|
| 844 |
#if defined _LIBC && defined USE_IN_LIBIO |
| 845 |
n = __asprintf (&buf, _("%s: unrecognized option `--%s'\n"), |
| 846 |
argv[0], nextchar); |
| 847 |
#else |
| 848 |
fprintf (stderr, _("%s: unrecognized option `--%s'\n"), |
| 849 |
argv[0], nextchar); |
| 850 |
#endif |
| 851 |
} |
| 852 |
else |
| 853 |
{ |
| 854 |
|
| 855 |
#if defined _LIBC && defined USE_IN_LIBIO |
| 856 |
n = __asprintf (&buf, _("%s: unrecognized option `%c%s'\n"), |
| 857 |
argv[0], argv[optind][0], nextchar); |
| 858 |
#else |
| 859 |
fprintf (stderr, _("%s: unrecognized option `%c%s'\n"), |
| 860 |
argv[0], argv[optind][0], nextchar); |
| 861 |
#endif |
| 862 |
} |
| 863 |
|
| 864 |
#if defined _LIBC && defined USE_IN_LIBIO |
| 865 |
if (n >= 0) |
| 866 |
{ |
| 867 |
if (_IO_fwide (stderr, 0) > 0) |
| 868 |
__fwprintf (stderr, L"%s", buf); |
| 869 |
else |
| 870 |
fputs (buf, stderr); |
| 871 |
|
| 872 |
free (buf); |
| 873 |
} |
| 874 |
#endif |
| 875 |
} |
| 876 |
nextchar = (char *) ""; |
| 877 |
optind++; |
| 878 |
optopt = 0; |
| 879 |
return '?'; |
| 880 |
} |
| 881 |
} |
| 882 |
|
| 883 |
|
| 884 |
|
| 885 |
{ |
| 886 |
char c = *nextchar++; |
| 887 |
char *temp = my_index (optstring, c); |
| 888 |
|
| 889 |
|
| 890 |
if (*nextchar == '\0') |
| 891 |
++optind; |
| 892 |
|
| 893 |
if (temp == NULL || c == ':') |
| 894 |
{ |
| 895 |
if (print_errors) |
| 896 |
{ |
| 897 |
#if defined _LIBC && defined USE_IN_LIBIO |
| 898 |
char *buf; |
| 899 |
int n; |
| 900 |
#endif |
| 901 |
|
| 902 |
if (posixly_correct) |
| 903 |
{ |
| 904 |
|
| 905 |
#if defined _LIBC && defined USE_IN_LIBIO |
| 906 |
n = __asprintf (&buf, _("%s: illegal option -- %c\n"), |
| 907 |
argv[0], c); |
| 908 |
#else |
| 909 |
fprintf (stderr, _("%s: illegal option -- %c\n"), argv[0], c); |
| 910 |
#endif |
| 911 |
} |
| 912 |
else |
| 913 |
{ |
| 914 |
#if defined _LIBC && defined USE_IN_LIBIO |
| 915 |
n = __asprintf (&buf, _("%s: invalid option -- %c\n"), |
| 916 |
argv[0], c); |
| 917 |
#else |
| 918 |
fprintf (stderr, _("%s: invalid option -- %c\n"), argv[0], c); |
| 919 |
#endif |
| 920 |
} |
| 921 |
|
| 922 |
#if defined _LIBC && defined USE_IN_LIBIO |
| 923 |
if (n >= 0) |
| 924 |
{ |
| 925 |
if (_IO_fwide (stderr, 0) > 0) |
| 926 |
__fwprintf (stderr, L"%s", buf); |
| 927 |
else |
| 928 |
fputs (buf, stderr); |
| 929 |
|
| 930 |
free (buf); |
| 931 |
} |
| 932 |
#endif |
| 933 |
} |
| 934 |
optopt = c; |
| 935 |
return '?'; |
| 936 |
} |
| 937 |
|
| 938 |
if (temp[0] == 'W' && temp[1] == ';') |
| 939 |
{ |
| 940 |
char *nameend; |
| 941 |
const struct option *p; |
| 942 |
const struct option *pfound = NULL; |
| 943 |
int exact = 0; |
| 944 |
int ambig = 0; |
| 945 |
int indfound = 0; |
| 946 |
int option_index; |
| 947 |
|
| 948 |
|
| 949 |
if (*nextchar != '\0') |
| 950 |
{ |
| 951 |
optarg = nextchar; |
| 952 |
|
| 953 |
|
| 954 |
optind++; |
| 955 |
} |
| 956 |
else if (optind == argc) |
| 957 |
{ |
| 958 |
if (print_errors) |
| 959 |
{ |
| 960 |
|
| 961 |
#if defined _LIBC && defined USE_IN_LIBIO |
| 962 |
char *buf; |
| 963 |
|
| 964 |
if (__asprintf (&buf, |
| 965 |
_("%s: option requires an argument -- %c\n"), |
| 966 |
argv[0], c) >= 0) |
| 967 |
{ |
| 968 |
if (_IO_fwide (stderr, 0) > 0) |
| 969 |
__fwprintf (stderr, L"%s", buf); |
| 970 |
else |
| 971 |
fputs (buf, stderr); |
| 972 |
|
| 973 |
free (buf); |
| 974 |
} |
| 975 |
#else |
| 976 |
fprintf (stderr, _("%s: option requires an argument -- %c\n"), |
| 977 |
argv[0], c); |
| 978 |
#endif |
| 979 |
} |
| 980 |
optopt = c; |
| 981 |
if (optstring[0] == ':') |
| 982 |
c = ':'; |
| 983 |
else |
| 984 |
c = '?'; |
| 985 |
return c; |
| 986 |
} |
| 987 |
else |
| 988 |
|
| 989 |
|
| 990 |
optarg = argv[optind++]; |
| 991 |
|
| 992 |
|
| 993 |
|
| 994 |
|
| 995 |
for (nextchar = nameend = optarg; *nameend && *nameend != '='; nameend++) |
| 996 |
; |
| 997 |
|
| 998 |
|
| 999 |
|
| 1000 |
for (p = longopts, option_index = 0; p->name; p++, option_index++) |
| 1001 |
if (!strncmp (p->name, nextchar, nameend - nextchar)) |
| 1002 |
{ |
| 1003 |
if ((unsigned int) (nameend - nextchar) == strlen (p->name)) |
| 1004 |
{ |
| 1005 |
|
| 1006 |
pfound = p; |
| 1007 |
indfound = option_index; |
| 1008 |
exact = 1; |
| 1009 |
break; |
| 1010 |
} |
| 1011 |
else if (pfound == NULL) |
| 1012 |
{ |
| 1013 |
|
| 1014 |
pfound = p; |
| 1015 |
indfound = option_index; |
| 1016 |
} |
| 1017 |
else |
| 1018 |
|
| 1019 |
ambig = 1; |
| 1020 |
} |
| 1021 |
if (ambig && !exact) |
| 1022 |
{ |
| 1023 |
if (print_errors) |
| 1024 |
{ |
| 1025 |
#if defined _LIBC && defined USE_IN_LIBIO |
| 1026 |
char *buf; |
| 1027 |
|
| 1028 |
if (__asprintf (&buf, _("%s: option `-W %s' is ambiguous\n"), |
| 1029 |
argv[0], argv[optind]) >= 0) |
| 1030 |
{ |
| 1031 |
if (_IO_fwide (stderr, 0) > 0) |
| 1032 |
__fwprintf (stderr, L"%s", buf); |
| 1033 |
else |
| 1034 |
fputs (buf, stderr); |
| 1035 |
|
| 1036 |
free (buf); |
| 1037 |
} |
| 1038 |
#else |
| 1039 |
fprintf (stderr, _("%s: option `-W %s' is ambiguous\n"), |
| 1040 |
argv[0], argv[optind]); |
| 1041 |
#endif |
| 1042 |
} |
| 1043 |
nextchar += strlen (nextchar); |
| 1044 |
optind++; |
| 1045 |
return '?'; |
| 1046 |
} |
| 1047 |
if (pfound != NULL) |
| 1048 |
{ |
| 1049 |
option_index = indfound; |
| 1050 |
if (*nameend) |
| 1051 |
{ |
| 1052 |
|
| 1053 |
|
| 1054 |
if (pfound->has_arg) |
| 1055 |
optarg = nameend + 1; |
| 1056 |
else |
| 1057 |
{ |
| 1058 |
if (print_errors) |
| 1059 |
{ |
| 1060 |
#if defined _LIBC && defined USE_IN_LIBIO |
| 1061 |
char *buf; |
| 1062 |
|
| 1063 |
if (__asprintf (&buf, _("\ |
| 1064 |
%s: option `-W %s' doesn't allow an argument\n"), |
| 1065 |
argv[0], pfound->name) >= 0) |
| 1066 |
{ |
| 1067 |
if (_IO_fwide (stderr, 0) > 0) |
| 1068 |
__fwprintf (stderr, L"%s", buf); |
| 1069 |
else |
| 1070 |
fputs (buf, stderr); |
| 1071 |
|
| 1072 |
free (buf); |
| 1073 |
} |
| 1074 |
#else |
| 1075 |
fprintf (stderr, _("\ |
| 1076 |
%s: option `-W %s' doesn't allow an argument\n"), |
| 1077 |
argv[0], pfound->name); |
| 1078 |
#endif |
| 1079 |
} |
| 1080 |
|
| 1081 |
nextchar += strlen (nextchar); |
| 1082 |
return '?'; |
| 1083 |
} |
| 1084 |
} |
| 1085 |
else if (pfound->has_arg == 1) |
| 1086 |
{ |
| 1087 |
if (optind < argc) |
| 1088 |
optarg = argv[optind++]; |
| 1089 |
else |
| 1090 |
{ |
| 1091 |
if (print_errors) |
| 1092 |
{ |
| 1093 |
#if defined _LIBC && defined USE_IN_LIBIO |
| 1094 |
char *buf; |
| 1095 |
|
| 1096 |
if (__asprintf (&buf, _("\ |
| 1097 |
%s: option `%s' requires an argument\n"), |
| 1098 |
argv[0], argv[optind - 1]) >= 0) |
| 1099 |
{ |
| 1100 |
if (_IO_fwide (stderr, 0) > 0) |
| 1101 |
__fwprintf (stderr, L"%s", buf); |
| 1102 |
else |
| 1103 |
fputs (buf, stderr); |
| 1104 |
|
| 1105 |
free (buf); |
| 1106 |
} |
| 1107 |
#else |
| 1108 |
fprintf (stderr, |
| 1109 |
_("%s: option `%s' requires an argument\n"), |
| 1110 |
argv[0], argv[optind - 1]); |
| 1111 |
#endif |
| 1112 |
} |
| 1113 |
nextchar += strlen (nextchar); |
| 1114 |
return optstring[0] == ':' ? ':' : '?'; |
| 1115 |
} |
| 1116 |
} |
| 1117 |
nextchar += strlen (nextchar); |
| 1118 |
if (longind != NULL) |
| 1119 |
*longind = option_index; |
| 1120 |
if (pfound->flag) |
| 1121 |
{ |
| 1122 |
*(pfound->flag) = pfound->val; |
| 1123 |
return 0; |
| 1124 |
} |
| 1125 |
return pfound->val; |
| 1126 |
} |
| 1127 |
nextchar = NULL; |
| 1128 |
return 'W'; |
| 1129 |
} |
| 1130 |
if (temp[1] == ':') |
| 1131 |
{ |
| 1132 |
if (temp[2] == ':') |
| 1133 |
{ |
| 1134 |
|
| 1135 |
if (*nextchar != '\0') |
| 1136 |
{ |
| 1137 |
optarg = nextchar; |
| 1138 |
optind++; |
| 1139 |
} |
| 1140 |
else |
| 1141 |
optarg = NULL; |
| 1142 |
nextchar = NULL; |
| 1143 |
} |
| 1144 |
else |
| 1145 |
{ |
| 1146 |
|
| 1147 |
if (*nextchar != '\0') |
| 1148 |
{ |
| 1149 |
optarg = nextchar; |
| 1150 |
|
| 1151 |
|
| 1152 |
optind++; |
| 1153 |
} |
| 1154 |
else if (optind == argc) |
| 1155 |
{ |
| 1156 |
if (print_errors) |
| 1157 |
{ |
| 1158 |
|
| 1159 |
#if defined _LIBC && defined USE_IN_LIBIO |
| 1160 |
char *buf; |
| 1161 |
|
| 1162 |
if (__asprintf (&buf, _("\ |
| 1163 |
%s: option requires an argument -- %c\n"), |
| 1164 |
argv[0], c) >= 0) |
| 1165 |
{ |
| 1166 |
if (_IO_fwide (stderr, 0) > 0) |
| 1167 |
__fwprintf (stderr, L"%s", buf); |
| 1168 |
else |
| 1169 |
fputs (buf, stderr); |
| 1170 |
|
| 1171 |
free (buf); |
| 1172 |
} |
| 1173 |
#else |
| 1174 |
fprintf (stderr, |
| 1175 |
_("%s: option requires an argument -- %c\n"), |
| 1176 |
argv[0], c); |
| 1177 |
#endif |
| 1178 |
} |
| 1179 |
optopt = c; |
| 1180 |
if (optstring[0] == ':') |
| 1181 |
c = ':'; |
| 1182 |
else |
| 1183 |
c = '?'; |
| 1184 |
} |
| 1185 |
else |
| 1186 |
|
| 1187 |
|
| 1188 |
optarg = argv[optind++]; |
| 1189 |
nextchar = NULL; |
| 1190 |
} |
| 1191 |
} |
| 1192 |
return c; |
| 1193 |
} |
| 1194 |
} |
| 1195 |
|
| 1196 |
int |
| 1197 |
getopt (argc, argv, optstring) |
| 1198 |
int argc; |
| 1199 |
char *const *argv; |
| 1200 |
const char *optstring; |
| 1201 |
{ |
| 1202 |
return _getopt_internal (argc, argv, optstring, |
| 1203 |
(const struct option *) 0, |
| 1204 |
(int *) 0, |
| 1205 |
0); |
| 1206 |
} |
| 1207 |
|
| 1208 |
#endif |
| 1209 |
|
| 1210 |
#ifdef TEST |
| 1211 |
|
| 1212 |
|
| 1213 |
|
| 1214 |
|
| 1215 |
int |
| 1216 |
main (argc, argv) |
| 1217 |
int argc; |
| 1218 |
char **argv; |
| 1219 |
{ |
| 1220 |
int c; |
| 1221 |
int digit_optind = 0; |
| 1222 |
|
| 1223 |
while (1) |
| 1224 |
{ |
| 1225 |
int this_option_optind = optind ? optind : 1; |
| 1226 |
|
| 1227 |
c = getopt (argc, argv, "abc:d:0123456789"); |
| 1228 |
if (c == -1) |
| 1229 |
break; |
| 1230 |
|
| 1231 |
switch (c) |
| 1232 |
{ |
| 1233 |
case '0': |
| 1234 |
case '1': |
| 1235 |
case '2': |
| 1236 |
case '3': |
| 1237 |
case '4': |
| 1238 |
case '5': |
| 1239 |
case '6': |
| 1240 |
case '7': |
| 1241 |
case '8': |
| 1242 |
case '9': |
| 1243 |
if (digit_optind != 0 && digit_optind != this_option_optind) |
| 1244 |
printf ("digits occur in two different argv-elements.\n"); |
| 1245 |
digit_optind = this_option_optind; |
| 1246 |
printf ("option %c\n", c); |
| 1247 |
break; |
| 1248 |
|
| 1249 |
case 'a': |
| 1250 |
printf ("option a\n"); |
| 1251 |
break; |
| 1252 |
|
| 1253 |
case 'b': |
| 1254 |
printf ("option b\n"); |
| 1255 |
break; |
| 1256 |
|
| 1257 |
case 'c': |
| 1258 |
printf ("option c with value `%s'\n", optarg); |
| 1259 |
break; |
| 1260 |
|
| 1261 |
case '?': |
| 1262 |
break; |
| 1263 |
|
| 1264 |
default: |
| 1265 |
printf ("?? getopt returned character code 0%o ??\n", c); |
| 1266 |
} |
| 1267 |
} |
| 1268 |
|
| 1269 |
if (optind < argc) |
| 1270 |
{ |
| 1271 |
printf ("non-option ARGV-elements: "); |
| 1272 |
while (optind < argc) |
| 1273 |
printf ("%s ", argv[optind++]); |
| 1274 |
printf ("\n"); |
| 1275 |
} |
| 1276 |
|
| 1277 |
exit (0); |
| 1278 |
} |
| 1279 |
|
| 1280 |
#endif |
| 1281 |
|