| 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 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
#if !(defined(_WIN32) || defined(_WIN64) || defined(__SVR4) || defined(__svr4__)) |
| 60 |
# include <sys/cdefs.h> |
| 61 |
# include <err.h> |
| 62 |
#else |
| 63 |
# include <stdio.h> |
| 64 |
# define warnx printf |
| 65 |
#endif |
| 66 |
|
| 67 |
#include <errno.h> |
| 68 |
#include <getopt.h> |
| 69 |
#include <stdlib.h> |
| 70 |
#include <string.h> |
| 71 |
|
| 72 |
#define GNU_COMPATIBLE |
| 73 |
|
| 74 |
#ifdef REPLACE_GETOPT |
| 75 |
int opterr = 1; |
| 76 |
int optind = 1; |
| 77 |
int optopt = '?'; |
| 78 |
int optreset; |
| 79 |
char *optarg; |
| 80 |
#endif |
| 81 |
|
| 82 |
#define PRINT_ERROR ((opterr) && (*options != ':')) |
| 83 |
|
| 84 |
#define FLAG_PERMUTE 0x01 |
| 85 |
#define FLAG_ALLARGS 0x02 |
| 86 |
#define FLAG_LONGONLY 0x04 |
| 87 |
|
| 88 |
|
| 89 |
#define BADCH (int)'?' |
| 90 |
#define BADARG ((*options == ':') ? (int)':' : (int)'?') |
| 91 |
#define INORDER (int)1 |
| 92 |
|
| 93 |
#define EMSG "" |
| 94 |
|
| 95 |
#ifdef GNU_COMPATIBLE |
| 96 |
#define NO_PREFIX (-1) |
| 97 |
#define D_PREFIX 0 |
| 98 |
#define DD_PREFIX 1 |
| 99 |
#define W_PREFIX 2 |
| 100 |
#endif |
| 101 |
|
| 102 |
static int getopt_internal(int, char * const *, const char *, |
| 103 |
const struct option *, int *, int); |
| 104 |
static int parse_long_options(char * const *, const char *, |
| 105 |
const struct option *, int *, int, int); |
| 106 |
static int gcd(int, int); |
| 107 |
static void permute_args(int, int, int, char * const *); |
| 108 |
|
| 109 |
static char *place = EMSG; |
| 110 |
|
| 111 |
|
| 112 |
static int nonopt_start = -1; |
| 113 |
static int nonopt_end = -1; |
| 114 |
|
| 115 |
|
| 116 |
static const char recargchar[] = "option requires an argument -- %c"; |
| 117 |
static const char illoptchar[] = "illegal option -- %c"; |
| 118 |
#ifdef GNU_COMPATIBLE |
| 119 |
static int dash_prefix = NO_PREFIX; |
| 120 |
static const char gnuoptchar[] = "invalid option -- %c"; |
| 121 |
|
| 122 |
static const char recargstring[] = "option `%s%s' requires an argument"; |
| 123 |
static const char ambig[] = "option `%s%.*s' is ambiguous"; |
| 124 |
static const char noarg[] = "option `%s%.*s' doesn't allow an argument"; |
| 125 |
static const char illoptstring[] = "unrecognized option `%s%s'"; |
| 126 |
#else |
| 127 |
static const char recargstring[] = "option requires an argument -- %s"; |
| 128 |
static const char ambig[] = "ambiguous option -- %.*s"; |
| 129 |
static const char noarg[] = "option doesn't take an argument -- %.*s"; |
| 130 |
static const char illoptstring[] = "unknown option -- %s"; |
| 131 |
#endif |
| 132 |
|
| 133 |
|
| 134 |
|
| 135 |
|
| 136 |
static int |
| 137 |
gcd(int a, int b) |
| 138 |
{ |
| 139 |
int c; |
| 140 |
|
| 141 |
c = a % b; |
| 142 |
while (c != 0) { |
| 143 |
a = b; |
| 144 |
b = c; |
| 145 |
c = a % b; |
| 146 |
} |
| 147 |
|
| 148 |
return (b); |
| 149 |
} |
| 150 |
|
| 151 |
|
| 152 |
|
| 153 |
|
| 154 |
|
| 155 |
|
| 156 |
static void |
| 157 |
permute_args(int panonopt_start, int panonopt_end, int opt_end, |
| 158 |
char * const *nargv) |
| 159 |
{ |
| 160 |
int cstart, cyclelen, i, j, ncycle, nnonopts, nopts, pos; |
| 161 |
char *swap; |
| 162 |
|
| 163 |
|
| 164 |
|
| 165 |
|
| 166 |
nnonopts = panonopt_end - panonopt_start; |
| 167 |
nopts = opt_end - panonopt_end; |
| 168 |
ncycle = gcd(nnonopts, nopts); |
| 169 |
cyclelen = (opt_end - panonopt_start) / ncycle; |
| 170 |
|
| 171 |
for (i = 0; i < ncycle; i++) { |
| 172 |
cstart = panonopt_end+i; |
| 173 |
pos = cstart; |
| 174 |
for (j = 0; j < cyclelen; j++) { |
| 175 |
if (pos >= panonopt_end) |
| 176 |
pos -= nnonopts; |
| 177 |
else |
| 178 |
pos += nopts; |
| 179 |
swap = nargv[pos]; |
| 180 |
|
| 181 |
((char **) nargv)[pos] = nargv[cstart]; |
| 182 |
|
| 183 |
((char **)nargv)[cstart] = swap; |
| 184 |
} |
| 185 |
} |
| 186 |
} |
| 187 |
|
| 188 |
|
| 189 |
|
| 190 |
|
| 191 |
|
| 192 |
|
| 193 |
static int |
| 194 |
parse_long_options(char * const *nargv, const char *options, |
| 195 |
const struct option *long_options, int *idx, int short_too, int flags) |
| 196 |
{ |
| 197 |
char *current_argv, *has_equal; |
| 198 |
#ifdef GNU_COMPATIBLE |
| 199 |
char *current_dash; |
| 200 |
#endif |
| 201 |
size_t current_argv_len; |
| 202 |
int i, match, exact_match, second_partial_match; |
| 203 |
|
| 204 |
current_argv = place; |
| 205 |
#ifdef GNU_COMPATIBLE |
| 206 |
switch (dash_prefix) { |
| 207 |
case D_PREFIX: |
| 208 |
current_dash = "-"; |
| 209 |
break; |
| 210 |
case DD_PREFIX: |
| 211 |
current_dash = "--"; |
| 212 |
break; |
| 213 |
case W_PREFIX: |
| 214 |
current_dash = "-W "; |
| 215 |
break; |
| 216 |
default: |
| 217 |
current_dash = ""; |
| 218 |
break; |
| 219 |
} |
| 220 |
#endif |
| 221 |
match = -1; |
| 222 |
exact_match = 0; |
| 223 |
second_partial_match = 0; |
| 224 |
|
| 225 |
optind++; |
| 226 |
|
| 227 |
if ((has_equal = strchr(current_argv, '=')) != NULL) { |
| 228 |
|
| 229 |
current_argv_len = has_equal - current_argv; |
| 230 |
has_equal++; |
| 231 |
} else |
| 232 |
current_argv_len = strlen(current_argv); |
| 233 |
|
| 234 |
for (i = 0; long_options[i].name; i++) { |
| 235 |
|
| 236 |
if (strncmp(current_argv, long_options[i].name, |
| 237 |
current_argv_len)) |
| 238 |
continue; |
| 239 |
|
| 240 |
if (strlen(long_options[i].name) == current_argv_len) { |
| 241 |
|
| 242 |
match = i; |
| 243 |
exact_match = 1; |
| 244 |
break; |
| 245 |
} |
| 246 |
|
| 247 |
|
| 248 |
|
| 249 |
|
| 250 |
if (short_too && current_argv_len == 1) |
| 251 |
continue; |
| 252 |
|
| 253 |
if (match == -1) |
| 254 |
match = i; |
| 255 |
else if ((flags & FLAG_LONGONLY) || |
| 256 |
long_options[i].has_arg != |
| 257 |
long_options[match].has_arg || |
| 258 |
long_options[i].flag != long_options[match].flag || |
| 259 |
long_options[i].val != long_options[match].val) |
| 260 |
second_partial_match = 1; |
| 261 |
} |
| 262 |
if (!exact_match && second_partial_match) { |
| 263 |
|
| 264 |
if (PRINT_ERROR) |
| 265 |
warnx(ambig, |
| 266 |
#ifdef GNU_COMPATIBLE |
| 267 |
current_dash, |
| 268 |
#endif |
| 269 |
(int)current_argv_len, |
| 270 |
current_argv); |
| 271 |
optopt = 0; |
| 272 |
return (BADCH); |
| 273 |
} |
| 274 |
if (match != -1) { |
| 275 |
if (long_options[match].has_arg == no_argument |
| 276 |
&& has_equal) { |
| 277 |
if (PRINT_ERROR) |
| 278 |
warnx(noarg, |
| 279 |
#ifdef GNU_COMPATIBLE |
| 280 |
current_dash, |
| 281 |
#endif |
| 282 |
(int)current_argv_len, |
| 283 |
current_argv); |
| 284 |
|
| 285 |
|
| 286 |
|
| 287 |
if (long_options[match].flag == NULL) |
| 288 |
optopt = long_options[match].val; |
| 289 |
else |
| 290 |
optopt = 0; |
| 291 |
#ifdef GNU_COMPATIBLE |
| 292 |
return (BADCH); |
| 293 |
#else |
| 294 |
return (BADARG); |
| 295 |
#endif |
| 296 |
} |
| 297 |
if (long_options[match].has_arg == required_argument || |
| 298 |
long_options[match].has_arg == optional_argument) { |
| 299 |
if (has_equal) |
| 300 |
optarg = has_equal; |
| 301 |
else if (long_options[match].has_arg == |
| 302 |
required_argument) { |
| 303 |
|
| 304 |
|
| 305 |
|
| 306 |
optarg = nargv[optind++]; |
| 307 |
} |
| 308 |
} |
| 309 |
if ((long_options[match].has_arg == required_argument) |
| 310 |
&& (optarg == NULL)) { |
| 311 |
|
| 312 |
|
| 313 |
|
| 314 |
|
| 315 |
if (PRINT_ERROR) |
| 316 |
warnx(recargstring, |
| 317 |
#ifdef GNU_COMPATIBLE |
| 318 |
current_dash, |
| 319 |
#endif |
| 320 |
current_argv); |
| 321 |
|
| 322 |
|
| 323 |
|
| 324 |
if (long_options[match].flag == NULL) |
| 325 |
optopt = long_options[match].val; |
| 326 |
else |
| 327 |
optopt = 0; |
| 328 |
--optind; |
| 329 |
return (BADARG); |
| 330 |
} |
| 331 |
} else { |
| 332 |
if (short_too) { |
| 333 |
--optind; |
| 334 |
return (-1); |
| 335 |
} |
| 336 |
if (PRINT_ERROR) |
| 337 |
warnx(illoptstring, |
| 338 |
#ifdef GNU_COMPATIBLE |
| 339 |
current_dash, |
| 340 |
#endif |
| 341 |
current_argv); |
| 342 |
optopt = 0; |
| 343 |
return (BADCH); |
| 344 |
} |
| 345 |
if (idx) |
| 346 |
*idx = match; |
| 347 |
if (long_options[match].flag) { |
| 348 |
*long_options[match].flag = long_options[match].val; |
| 349 |
return (0); |
| 350 |
} else |
| 351 |
return (long_options[match].val); |
| 352 |
} |
| 353 |
|
| 354 |
|
| 355 |
|
| 356 |
|
| 357 |
|
| 358 |
static int |
| 359 |
getopt_internal(int nargc, char * const *nargv, const char *options, |
| 360 |
const struct option *long_options, int *idx, int flags) |
| 361 |
{ |
| 362 |
char *oli; |
| 363 |
int optchar, short_too; |
| 364 |
int posixly_correct; |
| 365 |
|
| 366 |
if (options == NULL) |
| 367 |
return (-1); |
| 368 |
|
| 369 |
|
| 370 |
|
| 371 |
|
| 372 |
|
| 373 |
posixly_correct = (getenv("POSIXLY_CORRECT") != NULL); |
| 374 |
#ifdef GNU_COMPATIBLE |
| 375 |
if (*options == '-') |
| 376 |
flags |= FLAG_ALLARGS; |
| 377 |
else if (posixly_correct || *options == '+') |
| 378 |
flags &= ~FLAG_PERMUTE; |
| 379 |
#else |
| 380 |
if (posixly_correct || *options == '+') |
| 381 |
flags &= ~FLAG_PERMUTE; |
| 382 |
else if (*options == '-') |
| 383 |
flags |= FLAG_ALLARGS; |
| 384 |
#endif |
| 385 |
if (*options == '+' || *options == '-') |
| 386 |
options++; |
| 387 |
|
| 388 |
|
| 389 |
|
| 390 |
|
| 391 |
|
| 392 |
if (optind == 0) |
| 393 |
optind = optreset = 1; |
| 394 |
|
| 395 |
optarg = NULL; |
| 396 |
if (optreset) |
| 397 |
nonopt_start = nonopt_end = -1; |
| 398 |
start: |
| 399 |
if (optreset || !*place) { |
| 400 |
optreset = 0; |
| 401 |
if (optind >= nargc) { |
| 402 |
place = EMSG; |
| 403 |
if (nonopt_end != -1) { |
| 404 |
|
| 405 |
permute_args(nonopt_start, nonopt_end, |
| 406 |
optind, nargv); |
| 407 |
optind -= nonopt_end - nonopt_start; |
| 408 |
} |
| 409 |
else if (nonopt_start != -1) { |
| 410 |
|
| 411 |
|
| 412 |
|
| 413 |
|
| 414 |
optind = nonopt_start; |
| 415 |
} |
| 416 |
nonopt_start = nonopt_end = -1; |
| 417 |
return (-1); |
| 418 |
} |
| 419 |
if (*(place = nargv[optind]) != '-' || |
| 420 |
#ifdef GNU_COMPATIBLE |
| 421 |
place[1] == '\0') { |
| 422 |
#else |
| 423 |
(place[1] == '\0' && strchr(options, '-') == NULL)) { |
| 424 |
#endif |
| 425 |
place = EMSG; |
| 426 |
if (flags & FLAG_ALLARGS) { |
| 427 |
|
| 428 |
|
| 429 |
|
| 430 |
|
| 431 |
optarg = nargv[optind++]; |
| 432 |
return (INORDER); |
| 433 |
} |
| 434 |
if (!(flags & FLAG_PERMUTE)) { |
| 435 |
|
| 436 |
|
| 437 |
|
| 438 |
|
| 439 |
return (-1); |
| 440 |
} |
| 441 |
|
| 442 |
if (nonopt_start == -1) |
| 443 |
nonopt_start = optind; |
| 444 |
else if (nonopt_end != -1) { |
| 445 |
permute_args(nonopt_start, nonopt_end, |
| 446 |
optind, nargv); |
| 447 |
nonopt_start = optind - |
| 448 |
(nonopt_end - nonopt_start); |
| 449 |
nonopt_end = -1; |
| 450 |
} |
| 451 |
optind++; |
| 452 |
|
| 453 |
goto start; |
| 454 |
} |
| 455 |
if (nonopt_start != -1 && nonopt_end == -1) |
| 456 |
nonopt_end = optind; |
| 457 |
|
| 458 |
|
| 459 |
|
| 460 |
|
| 461 |
if (place[1] != '\0' && *++place == '-' && place[1] == '\0') { |
| 462 |
optind++; |
| 463 |
place = EMSG; |
| 464 |
|
| 465 |
|
| 466 |
|
| 467 |
|
| 468 |
if (nonopt_end != -1) { |
| 469 |
permute_args(nonopt_start, nonopt_end, |
| 470 |
optind, nargv); |
| 471 |
optind -= nonopt_end - nonopt_start; |
| 472 |
} |
| 473 |
nonopt_start = nonopt_end = -1; |
| 474 |
return (-1); |
| 475 |
} |
| 476 |
} |
| 477 |
|
| 478 |
|
| 479 |
|
| 480 |
|
| 481 |
|
| 482 |
|
| 483 |
|
| 484 |
if (long_options != NULL && place != nargv[optind] && |
| 485 |
(*place == '-' || (flags & FLAG_LONGONLY))) { |
| 486 |
short_too = 0; |
| 487 |
#ifdef GNU_COMPATIBLE |
| 488 |
dash_prefix = D_PREFIX; |
| 489 |
#endif |
| 490 |
if (*place == '-') { |
| 491 |
place++; |
| 492 |
#ifdef GNU_COMPATIBLE |
| 493 |
dash_prefix = DD_PREFIX; |
| 494 |
#endif |
| 495 |
} else if (*place != ':' && strchr(options, *place) != NULL) |
| 496 |
short_too = 1; |
| 497 |
|
| 498 |
optchar = parse_long_options(nargv, options, long_options, |
| 499 |
idx, short_too, flags); |
| 500 |
if (optchar != -1) { |
| 501 |
place = EMSG; |
| 502 |
return (optchar); |
| 503 |
} |
| 504 |
} |
| 505 |
|
| 506 |
if ((optchar = (int)*place++) == (int)':' || |
| 507 |
(optchar == (int)'-' && *place != '\0') || |
| 508 |
(oli = (char*)strchr(options, optchar)) == NULL) { |
| 509 |
|
| 510 |
|
| 511 |
|
| 512 |
|
| 513 |
|
| 514 |
if (optchar == (int)'-' && *place == '\0') |
| 515 |
return (-1); |
| 516 |
if (!*place) |
| 517 |
++optind; |
| 518 |
#ifdef GNU_COMPATIBLE |
| 519 |
if (PRINT_ERROR) |
| 520 |
warnx(posixly_correct ? illoptchar : gnuoptchar, |
| 521 |
optchar); |
| 522 |
#else |
| 523 |
if (PRINT_ERROR) |
| 524 |
warnx(illoptchar, optchar); |
| 525 |
#endif |
| 526 |
optopt = optchar; |
| 527 |
return (BADCH); |
| 528 |
} |
| 529 |
if (long_options != NULL && optchar == 'W' && oli[1] == ';') { |
| 530 |
|
| 531 |
if (*place) |
| 532 |
; |
| 533 |
else if (++optind >= nargc) { |
| 534 |
place = EMSG; |
| 535 |
if (PRINT_ERROR) |
| 536 |
warnx(recargchar, optchar); |
| 537 |
optopt = optchar; |
| 538 |
return (BADARG); |
| 539 |
} else |
| 540 |
place = nargv[optind]; |
| 541 |
#ifdef GNU_COMPATIBLE |
| 542 |
dash_prefix = W_PREFIX; |
| 543 |
#endif |
| 544 |
optchar = parse_long_options(nargv, options, long_options, |
| 545 |
idx, 0, flags); |
| 546 |
place = EMSG; |
| 547 |
return (optchar); |
| 548 |
} |
| 549 |
if (*++oli != ':') { |
| 550 |
if (!*place) |
| 551 |
++optind; |
| 552 |
} else { |
| 553 |
optarg = NULL; |
| 554 |
if (*place) |
| 555 |
optarg = place; |
| 556 |
else if (oli[1] != ':') { |
| 557 |
if (++optind >= nargc) { |
| 558 |
place = EMSG; |
| 559 |
if (PRINT_ERROR) |
| 560 |
warnx(recargchar, optchar); |
| 561 |
optopt = optchar; |
| 562 |
return (BADARG); |
| 563 |
} else |
| 564 |
optarg = nargv[optind]; |
| 565 |
} |
| 566 |
place = EMSG; |
| 567 |
++optind; |
| 568 |
} |
| 569 |
|
| 570 |
return (optchar); |
| 571 |
} |
| 572 |
|
| 573 |
#ifdef REPLACE_GETOPT |
| 574 |
|
| 575 |
|
| 576 |
|
| 577 |
|
| 578 |
|
| 579 |
|
| 580 |
int |
| 581 |
getopt(int nargc, char * const *nargv, const char *options) |
| 582 |
{ |
| 583 |
|
| 584 |
|
| 585 |
|
| 586 |
|
| 587 |
|
| 588 |
|
| 589 |
|
| 590 |
|
| 591 |
|
| 592 |
return (getopt_internal(nargc, nargv, options, NULL, NULL, 0)); |
| 593 |
} |
| 594 |
#endif |
| 595 |
|
| 596 |
|
| 597 |
|
| 598 |
|
| 599 |
|
| 600 |
int |
| 601 |
getopt_long(int nargc, char * const *nargv, const char *options, |
| 602 |
const struct option *long_options, int *idx) |
| 603 |
{ |
| 604 |
|
| 605 |
return (getopt_internal(nargc, nargv, options, long_options, idx, |
| 606 |
FLAG_PERMUTE)); |
| 607 |
} |
| 608 |
|
| 609 |
|
| 610 |
|
| 611 |
|
| 612 |
|
| 613 |
int |
| 614 |
getopt_long_only(int nargc, char * const *nargv, const char *options, |
| 615 |
const struct option *long_options, int *idx) |
| 616 |
{ |
| 617 |
|
| 618 |
return (getopt_internal(nargc, nargv, options, long_options, idx, |
| 619 |
FLAG_PERMUTE|FLAG_LONGONLY)); |
| 620 |
} |