getopt_long.c revision 127733
1/* $OpenBSD: getopt_long.c,v 1.16 2004/02/04 18:17:25 millert Exp $ */ 2/* $NetBSD: getopt_long.c,v 1.15 2002/01/31 22:43:40 tv Exp $ */ 3 4/* 5 * Copyright (c) 2002 Todd C. Miller <Todd.Miller@courtesan.com> 6 * 7 * Permission to use, copy, modify, and distribute this software for any 8 * purpose with or without fee is hereby granted, provided that the above 9 * copyright notice and this permission notice appear in all copies. 10 * 11 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES 12 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF 13 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR 14 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES 15 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN 16 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF 17 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. 18 * 19 * Sponsored in part by the Defense Advanced Research Projects 20 * Agency (DARPA) and Air Force Research Laboratory, Air Force 21 * Materiel Command, USAF, under agreement number F39502-99-1-0512. 22 */ 23/*- 24 * Copyright (c) 2000 The NetBSD Foundation, Inc. 25 * All rights reserved. 26 * 27 * This code is derived from software contributed to The NetBSD Foundation 28 * by Dieter Baron and Thomas Klausner. 29 * 30 * Redistribution and use in source and binary forms, with or without 31 * modification, are permitted provided that the following conditions 32 * are met: 33 * 1. Redistributions of source code must retain the above copyright 34 * notice, this list of conditions and the following disclaimer. 35 * 2. Redistributions in binary form must reproduce the above copyright 36 * notice, this list of conditions and the following disclaimer in the 37 * documentation and/or other materials provided with the distribution. 38 * 3. All advertising materials mentioning features or use of this software 39 * must display the following acknowledgement: 40 * This product includes software developed by the NetBSD 41 * Foundation, Inc. and its contributors. 42 * 4. Neither the name of The NetBSD Foundation nor the names of its 43 * contributors may be used to endorse or promote products derived 44 * from this software without specific prior written permission. 45 * 46 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS 47 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 48 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 49 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 50 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 51 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 52 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 53 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 54 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 55 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 56 * POSSIBILITY OF SUCH DAMAGE. 57 */ 58 59#if 0 60#if defined(LIBC_SCCS) && !defined(lint) 61static char *rcsid = "$OpenBSD: getopt_long.c,v 1.16 2004/02/04 18:17:25 millert Exp $"; 62#endif /* LIBC_SCCS and not lint */ 63#endif 64#include <sys/cdefs.h> 65__FBSDID("$FreeBSD: head/lib/libc/stdlib/getopt_long.c 127733 2004-04-01 22:09:07Z ache $"); 66 67#include <err.h> 68#include <errno.h> 69#include <getopt.h> 70#include <stdlib.h> 71#include <string.h> 72 73#define GNU_COMPATIBLE /* Be more compatible, configure's use us! */ 74 75#ifndef GNU_COMPATIBLE 76#define REPLACE_GETOPT /* use this getopt as the system getopt(3) */ 77#endif 78 79#ifdef REPLACE_GETOPT 80int opterr = 1; /* if error message should be printed */ 81int optind = 1; /* index into parent argv vector */ 82int optopt = '?'; /* character checked for validity */ 83int optreset; /* reset getopt */ 84char *optarg; /* argument associated with option */ 85#endif 86 87#define PRINT_ERROR ((opterr) && (*options != ':')) 88 89#define FLAG_PERMUTE 0x01 /* permute non-options to the end of argv */ 90#define FLAG_ALLARGS 0x02 /* treat non-options as args to option "-1" */ 91#define FLAG_LONGONLY 0x04 /* operate as getopt_long_only */ 92 93/* return values */ 94#define BADCH (int)'?' 95#define BADARG ((*options == ':') ? (int)':' : (int)'?') 96#define INORDER (int)1 97 98#define EMSG "" 99 100#ifdef GNU_COMPATIBLE 101#define NO_PREFIX (-1) 102#define D_PREFIX 0 103#define DD_PREFIX 1 104#define W_PREFIX 2 105#endif 106 107static int getopt_internal(int, char * const *, const char *, 108 const struct option *, int *, int); 109static int parse_long_options(char * const *, const char *, 110 const struct option *, int *, int, int); 111static int gcd(int, int); 112static void permute_args(int, int, int, char * const *); 113 114static char *place = EMSG; /* option letter processing */ 115 116/* XXX: set optreset to 1 rather than these two */ 117static int nonopt_start = -1; /* first non option argument (for permute) */ 118static int nonopt_end = -1; /* first option after non options (for permute) */ 119 120/* Error messages */ 121static const char recargchar[] = "option requires an argument -- %c"; 122static const char illoptchar[] = "illegal option -- %c"; /* From P1003.2 */ 123#ifdef GNU_COMPATIBLE 124static int dash_prefix = NO_PREFIX; 125static const char gnuoptchar[] = "invalid option -- %c"; 126 127static const char recargstring[] = "option `%s%s' requires an argument"; 128static const char ambig[] = "option `%s%.*s' is ambiguous"; 129static const char noarg[] = "option `%s%.*s' doesn't allow an argument"; 130static const char illoptstring[] = "unrecognized option `%s%s'"; 131#else 132static const char recargstring[] = "option requires an argument -- %s"; 133static const char ambig[] = "ambiguous option -- %.*s"; 134static const char noarg[] = "option doesn't take an argument -- %.*s"; 135static const char illoptstring[] = "unknown option -- %s"; 136#endif 137 138/* 139 * Compute the greatest common divisor of a and b. 140 */ 141static int 142gcd(int a, int b) 143{ 144 int c; 145 146 c = a % b; 147 while (c != 0) { 148 a = b; 149 b = c; 150 c = a % b; 151 } 152 153 return (b); 154} 155 156/* 157 * Exchange the block from nonopt_start to nonopt_end with the block 158 * from nonopt_end to opt_end (keeping the same order of arguments 159 * in each block). 160 */ 161static void 162permute_args(int panonopt_start, int panonopt_end, int opt_end, 163 char * const *nargv) 164{ 165 int cstart, cyclelen, i, j, ncycle, nnonopts, nopts, pos; 166 char *swap; 167 168 /* 169 * compute lengths of blocks and number and size of cycles 170 */ 171 nnonopts = panonopt_end - panonopt_start; 172 nopts = opt_end - panonopt_end; 173 ncycle = gcd(nnonopts, nopts); 174 cyclelen = (opt_end - panonopt_start) / ncycle; 175 176 for (i = 0; i < ncycle; i++) { 177 cstart = panonopt_end+i; 178 pos = cstart; 179 for (j = 0; j < cyclelen; j++) { 180 if (pos >= panonopt_end) 181 pos -= nnonopts; 182 else 183 pos += nopts; 184 swap = nargv[pos]; 185 /* LINTED const cast */ 186 ((char **) nargv)[pos] = nargv[cstart]; 187 /* LINTED const cast */ 188 ((char **)nargv)[cstart] = swap; 189 } 190 } 191} 192 193/* 194 * parse_long_options -- 195 * Parse long options in argc/argv argument vector. 196 * Returns -1 if short_too is set and the option does not match long_options. 197 */ 198static int 199parse_long_options(char * const *nargv, const char *options, 200 const struct option *long_options, int *idx, int short_too, int flags) 201{ 202 char *current_argv, *has_equal; 203#ifdef GNU_COMPATIBLE 204 char *current_dash; 205#endif 206 size_t current_argv_len; 207 int i, match, exact_match, second_partial_match; 208 209 current_argv = place; 210#ifdef GNU_COMPATIBLE 211 switch (dash_prefix) { 212 case D_PREFIX: 213 current_dash = "-"; 214 break; 215 case DD_PREFIX: 216 current_dash = "--"; 217 break; 218 case W_PREFIX: 219 current_dash = "-W "; 220 break; 221 default: 222 current_dash = ""; 223 break; 224 } 225#endif 226 match = -1; 227 exact_match = 0; 228 second_partial_match = 0; 229 230 optind++; 231 232 if ((has_equal = strchr(current_argv, '=')) != NULL) { 233 /* argument found (--option=arg) */ 234 current_argv_len = has_equal - current_argv; 235 has_equal++; 236 } else 237 current_argv_len = strlen(current_argv); 238 239 for (i = 0; long_options[i].name; i++) { 240 /* find matching long option */ 241 if (strncmp(current_argv, long_options[i].name, 242 current_argv_len)) 243 continue; 244 245 if (strlen(long_options[i].name) == current_argv_len) { 246 /* exact match */ 247 match = i; 248 exact_match = 1; 249 break; 250 } 251 /* 252 * If this is a known short option, don't allow 253 * a partial match of a single character. 254 */ 255 if (short_too && 256 (!(flags & FLAG_LONGONLY) || current_argv_len == 1)) 257 continue; 258 259 if (match == -1) /* first partial match */ 260 match = i; 261 else if ((flags & FLAG_LONGONLY) || 262 long_options[i].has_arg != 263 long_options[match].has_arg || 264 long_options[i].flag != long_options[match].flag || 265 long_options[i].val != long_options[match].val) 266 second_partial_match = 1; 267 } 268 if (!exact_match && second_partial_match) { 269 /* ambiguous abbreviation */ 270 if (PRINT_ERROR) 271 warnx(ambig, 272#ifdef GNU_COMPATIBLE 273 current_dash, 274#endif 275 (int)current_argv_len, 276 current_argv); 277 optopt = 0; 278 return (BADCH); 279 } 280 if (match != -1) { /* option found */ 281 if (long_options[match].has_arg == no_argument 282 && has_equal) { 283 if (PRINT_ERROR) 284 warnx(noarg, 285#ifdef GNU_COMPATIBLE 286 current_dash, 287#endif 288 (int)current_argv_len, 289 current_argv); 290 /* 291 * XXX: GNU sets optopt to val regardless of flag 292 */ 293 if (long_options[match].flag == NULL) 294 optopt = long_options[match].val; 295 else 296 optopt = 0; 297#ifdef GNU_COMPATIBLE 298 return (BADCH); 299#else 300 return (BADARG); 301#endif 302 } 303 if (long_options[match].has_arg == required_argument || 304 long_options[match].has_arg == optional_argument) { 305 if (has_equal) 306 optarg = has_equal; 307 else if (long_options[match].has_arg == 308 required_argument) { 309 /* 310 * optional argument doesn't use next nargv 311 */ 312 optarg = nargv[optind++]; 313 } 314 } 315 if ((long_options[match].has_arg == required_argument) 316 && (optarg == NULL)) { 317 /* 318 * Missing argument; leading ':' indicates no error 319 * should be generated. 320 */ 321 if (PRINT_ERROR) 322 warnx(recargstring, 323#ifdef GNU_COMPATIBLE 324 current_dash, 325#endif 326 current_argv); 327 /* 328 * XXX: GNU sets optopt to val regardless of flag 329 */ 330 if (long_options[match].flag == NULL) 331 optopt = long_options[match].val; 332 else 333 optopt = 0; 334 --optind; 335 return (BADARG); 336 } 337 } else { /* unknown option */ 338 if (short_too) { 339 --optind; 340 return (-1); 341 } 342 if (PRINT_ERROR) 343 warnx(illoptstring, 344#ifdef GNU_COMPATIBLE 345 current_dash, 346#endif 347 current_argv); 348 optopt = 0; 349 return (BADCH); 350 } 351 if (idx) 352 *idx = match; 353 if (long_options[match].flag) { 354 *long_options[match].flag = long_options[match].val; 355 return (0); 356 } else 357 return (long_options[match].val); 358} 359 360/* 361 * getopt_internal -- 362 * Parse argc/argv argument vector. Called by user level routines. 363 */ 364static int 365getopt_internal(int nargc, char * const *nargv, const char *options, 366 const struct option *long_options, int *idx, int flags) 367{ 368 char *oli; /* option letter list index */ 369 int optchar, short_too; 370 int posixly_correct; 371 372 if (options == NULL) 373 return (-1); 374 375 /* 376 * Disable GNU extensions if POSIXLY_CORRECT is set or options 377 * string begins with a '+'. 378 */ 379 posixly_correct = (getenv("POSIXLY_CORRECT") != NULL); 380#ifdef GNU_COMPATIBLE 381 if (*options == '-') 382 flags |= FLAG_ALLARGS; 383 else if (posixly_correct || *options == '+') 384 flags &= ~FLAG_PERMUTE; 385#else 386 if (posixly_correct || *options == '+') 387 flags &= ~FLAG_PERMUTE; 388 else if (*options == '-') 389 flags |= FLAG_ALLARGS; 390#endif 391 if (*options == '+' || *options == '-') 392 options++; 393 394 /* 395 * XXX Some GNU programs (like cvs) set optind to 0 instead of 396 * XXX using optreset. Work around this braindamage. 397 */ 398 if (optind == 0) 399 optind = optreset = 1; 400 401 optarg = NULL; 402 if (optreset) 403 nonopt_start = nonopt_end = -1; 404start: 405 if (optreset || !*place) { /* update scanning pointer */ 406 optreset = 0; 407 if (optind >= nargc) { /* end of argument vector */ 408 place = EMSG; 409 if (nonopt_end != -1) { 410 /* do permutation, if we have to */ 411 permute_args(nonopt_start, nonopt_end, 412 optind, nargv); 413 optind -= nonopt_end - nonopt_start; 414 } 415 else if (nonopt_start != -1) { 416 /* 417 * If we skipped non-options, set optind 418 * to the first of them. 419 */ 420 optind = nonopt_start; 421 } 422 nonopt_start = nonopt_end = -1; 423 return (-1); 424 } 425 if (*(place = nargv[optind]) != '-' || 426#ifdef GNU_COMPATIBLE 427 place[1] == '\0') { 428#else 429 (place[1] == '\0' && strchr(options, '-') == NULL)) { 430#endif 431 place = EMSG; /* found non-option */ 432 if (flags & FLAG_ALLARGS) { 433 /* 434 * GNU extension: 435 * return non-option as argument to option 1 436 */ 437 optarg = nargv[optind++]; 438 return (INORDER); 439 } 440 if (!(flags & FLAG_PERMUTE)) { 441 /* 442 * If no permutation wanted, stop parsing 443 * at first non-option. 444 */ 445 return (-1); 446 } 447 /* do permutation */ 448 if (nonopt_start == -1) 449 nonopt_start = optind; 450 else if (nonopt_end != -1) { 451 permute_args(nonopt_start, nonopt_end, 452 optind, nargv); 453 nonopt_start = optind - 454 (nonopt_end - nonopt_start); 455 nonopt_end = -1; 456 } 457 optind++; 458 /* process next argument */ 459 goto start; 460 } 461 if (nonopt_start != -1 && nonopt_end == -1) 462 nonopt_end = optind; 463 464 /* 465 * If we have "-" do nothing, if "--" we are done. 466 */ 467 if (place[1] != '\0' && *++place == '-' && place[1] == '\0') { 468 optind++; 469 place = EMSG; 470 /* 471 * We found an option (--), so if we skipped 472 * non-options, we have to permute. 473 */ 474 if (nonopt_end != -1) { 475 permute_args(nonopt_start, nonopt_end, 476 optind, nargv); 477 optind -= nonopt_end - nonopt_start; 478 } 479 nonopt_start = nonopt_end = -1; 480 return (-1); 481 } 482 } 483 484 /* 485 * Check long options if: 486 * 1) we were passed some 487 * 2) the arg is not just "-" 488 * 3) either the arg starts with -- we are getopt_long_only() 489 */ 490 if (long_options != NULL && place != nargv[optind] && 491 (*place == '-' || (flags & FLAG_LONGONLY))) { 492 short_too = 0; 493#ifdef GNU_COMPATIBLE 494 dash_prefix = D_PREFIX; 495#endif 496 if (*place == '-') { 497 place++; /* --foo long option */ 498#ifdef GNU_COMPATIBLE 499 dash_prefix = DD_PREFIX; 500#endif 501 } else if (*place != ':' && strchr(options, *place) != NULL) 502 short_too = 1; /* could be short option too */ 503 504 optchar = parse_long_options(nargv, options, long_options, 505 idx, short_too, flags); 506 if (optchar != -1) { 507 place = EMSG; 508 return (optchar); 509 } 510 } 511 512 if ((optchar = (int)*place++) == (int)':' || 513 (optchar == (int)'-' && *place != '\0') || 514 (oli = strchr(options, optchar)) == NULL) { 515 /* 516 * If the user specified "-" and '-' isn't listed in 517 * options, return -1 (non-option) as per POSIX. 518 * Otherwise, it is an unknown option character (or ':'). 519 */ 520 if (optchar == (int)'-' && *place == '\0') 521 return (-1); 522 if (!*place) 523 ++optind; 524#ifdef GNU_COMPATIBLE 525 if (PRINT_ERROR) 526 warnx(posixly_correct ? illoptchar : gnuoptchar, 527 optchar); 528#else 529 if (PRINT_ERROR) 530 warnx(illoptchar, optchar); 531#endif 532 optopt = optchar; 533 return (BADCH); 534 } 535 if (long_options != NULL && optchar == 'W' && oli[1] == ';') { 536 /* -W long-option */ 537 if (*place) /* no space */ 538 /* NOTHING */; 539 else if (++optind >= nargc) { /* no arg */ 540 place = EMSG; 541 if (PRINT_ERROR) 542 warnx(recargchar, optchar); 543 optopt = optchar; 544 return (BADARG); 545 } else /* white space */ 546 place = nargv[optind]; 547#ifdef GNU_COMPATIBLE 548 dash_prefix = W_PREFIX; 549#endif 550 optchar = parse_long_options(nargv, options, long_options, 551 idx, 0, flags); 552 place = EMSG; 553 return (optchar); 554 } 555 if (*++oli != ':') { /* doesn't take argument */ 556 if (!*place) 557 ++optind; 558 } else { /* takes (optional) argument */ 559 optarg = NULL; 560 if (*place) /* no white space */ 561 optarg = place; 562 /* XXX: disable test for :: if PC? (GNU doesn't) */ 563 else if (oli[1] != ':') { /* arg not optional */ 564 if (++optind >= nargc) { /* no arg */ 565 place = EMSG; 566 if (PRINT_ERROR) 567 warnx(recargchar, optchar); 568 optopt = optchar; 569 return (BADARG); 570 } else 571 optarg = nargv[optind]; 572 } else if (!(flags & FLAG_PERMUTE)) { 573 /* 574 * If permutation is disabled, we can accept an 575 * optional arg separated by whitespace. 576 */ 577 if (optind + 1 < nargc) 578 optarg = nargv[++optind]; 579 } 580 place = EMSG; 581 ++optind; 582 } 583 /* dump back option letter */ 584 return (optchar); 585} 586 587#ifdef REPLACE_GETOPT 588/* 589 * getopt -- 590 * Parse argc/argv argument vector. 591 * 592 * [eventually this will replace the BSD getopt] 593 */ 594int 595getopt(int nargc, char * const *nargv, const char *options) 596{ 597 598 /* 599 * We don't pass FLAG_PERMUTE to getopt_internal() since 600 * the BSD getopt(3) (unlike GNU) has never done this. 601 * 602 * Furthermore, since many privileged programs call getopt() 603 * before dropping privileges it makes sense to keep things 604 * as simple (and bug-free) as possible. 605 */ 606 return (getopt_internal(nargc, nargv, options, NULL, NULL, 0)); 607} 608#endif /* REPLACE_GETOPT */ 609 610/* 611 * getopt_long -- 612 * Parse argc/argv argument vector. 613 */ 614int 615getopt_long(nargc, nargv, options, long_options, idx) 616 int nargc; 617 char * const *nargv; 618 const char *options; 619 const struct option *long_options; 620 int *idx; 621{ 622 623 return (getopt_internal(nargc, nargv, options, long_options, idx, 624 FLAG_PERMUTE)); 625} 626 627/* 628 * getopt_long_only -- 629 * Parse argc/argv argument vector. 630 */ 631int 632getopt_long_only(nargc, nargv, options, long_options, idx) 633 int nargc; 634 char * const *nargv; 635 const char *options; 636 const struct option *long_options; 637 int *idx; 638{ 639 640 return (getopt_internal(nargc, nargv, options, long_options, idx, 641 FLAG_PERMUTE|FLAG_LONGONLY)); 642} 643