getopt_long.c revision 126518
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 126518 2004-03-03 03:05:21Z 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); 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) 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; 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 228 optind++; 229 230 if ((has_equal = strchr(current_argv, '=')) != NULL) { 231 /* argument found (--option=arg) */ 232 current_argv_len = has_equal - current_argv; 233 has_equal++; 234 } else 235 current_argv_len = strlen(current_argv); 236 237 for (i = 0; long_options[i].name; i++) { 238 /* find matching long option */ 239 if (strncmp(current_argv, long_options[i].name, 240 current_argv_len)) 241 continue; 242 243 if (strlen(long_options[i].name) == current_argv_len) { 244 /* exact match */ 245 match = i; 246 break; 247 } 248 /* 249 * If this is a known short option, don't allow 250 * a partial match of a single character. 251 */ 252 if (short_too && current_argv_len == 1) 253 continue; 254 255 if (match == -1) /* partial match */ 256 match = i; 257 else { 258 /* ambiguous abbreviation */ 259 if (PRINT_ERROR) 260 warnx(ambig, 261#ifdef GNU_COMPATIBLE 262 current_dash, 263#endif 264 (int)current_argv_len, 265 current_argv); 266 optopt = 0; 267 return (BADCH); 268 } 269 } 270 if (match != -1) { /* option found */ 271 if (long_options[match].has_arg == no_argument 272 && has_equal) { 273 if (PRINT_ERROR) 274 warnx(noarg, 275#ifdef GNU_COMPATIBLE 276 current_dash, 277#endif 278 (int)current_argv_len, 279 current_argv); 280 /* 281 * XXX: GNU sets optopt to val regardless of flag 282 */ 283 if (long_options[match].flag == NULL) 284 optopt = long_options[match].val; 285 else 286 optopt = 0; 287 return (BADARG); 288 } 289 if (long_options[match].has_arg == required_argument || 290 long_options[match].has_arg == optional_argument) { 291 if (has_equal) 292 optarg = has_equal; 293 else if (long_options[match].has_arg == 294 required_argument) { 295 /* 296 * optional argument doesn't use next nargv 297 */ 298 optarg = nargv[optind++]; 299 } 300 } 301 if ((long_options[match].has_arg == required_argument) 302 && (optarg == NULL)) { 303 /* 304 * Missing argument; leading ':' indicates no error 305 * should be generated. 306 */ 307 if (PRINT_ERROR) 308 warnx(recargstring, 309#ifdef GNU_COMPATIBLE 310 current_dash, 311#endif 312 current_argv); 313 /* 314 * XXX: GNU sets optopt to val regardless of flag 315 */ 316 if (long_options[match].flag == NULL) 317 optopt = long_options[match].val; 318 else 319 optopt = 0; 320 --optind; 321 return (BADARG); 322 } 323 } else { /* unknown option */ 324 if (short_too) { 325 --optind; 326 return (-1); 327 } 328 if (PRINT_ERROR) 329 warnx(illoptstring, 330#ifdef GNU_COMPATIBLE 331 current_dash, 332#endif 333 current_argv); 334 optopt = 0; 335 return (BADCH); 336 } 337 if (idx) 338 *idx = match; 339 if (long_options[match].flag) { 340 *long_options[match].flag = long_options[match].val; 341 return (0); 342 } else 343 return (long_options[match].val); 344} 345 346/* 347 * getopt_internal -- 348 * Parse argc/argv argument vector. Called by user level routines. 349 */ 350static int 351getopt_internal(int nargc, char * const *nargv, const char *options, 352 const struct option *long_options, int *idx, int flags) 353{ 354 char *oli; /* option letter list index */ 355 int optchar, short_too; 356 int posixly_correct; 357 358 if (options == NULL) 359 return (-1); 360 361 /* 362 * Disable GNU extensions if POSIXLY_CORRECT is set or options 363 * string begins with a '+'. 364 */ 365 posixly_correct = (getenv("POSIXLY_CORRECT") != NULL); 366#ifdef GNU_COMPATIBLE 367 if (*options == '-') 368 flags |= FLAG_ALLARGS; 369 else if (posixly_correct || *options == '+') 370 flags &= ~FLAG_PERMUTE; 371#else 372 if (posixly_correct || *options == '+') 373 flags &= ~FLAG_PERMUTE; 374 else if (*options == '-') 375 flags |= FLAG_ALLARGS; 376#endif 377 if (*options == '+' || *options == '-') 378 options++; 379 380 /* 381 * XXX Some GNU programs (like cvs) set optind to 0 instead of 382 * XXX using optreset. Work around this braindamage. 383 */ 384 if (optind == 0) 385 optind = optreset = 1; 386 387 optarg = NULL; 388 if (optreset) 389 nonopt_start = nonopt_end = -1; 390start: 391 if (optreset || !*place) { /* update scanning pointer */ 392 optreset = 0; 393 if (optind >= nargc) { /* end of argument vector */ 394 place = EMSG; 395 if (nonopt_end != -1) { 396 /* do permutation, if we have to */ 397 permute_args(nonopt_start, nonopt_end, 398 optind, nargv); 399 optind -= nonopt_end - nonopt_start; 400 } 401 else if (nonopt_start != -1) { 402 /* 403 * If we skipped non-options, set optind 404 * to the first of them. 405 */ 406 optind = nonopt_start; 407 } 408 nonopt_start = nonopt_end = -1; 409 return (-1); 410 } 411 if (*(place = nargv[optind]) != '-' || 412 (place[1] == '\0' && strchr(options, '-') == NULL)) { 413 place = EMSG; /* found non-option */ 414 if (flags & FLAG_ALLARGS) { 415 /* 416 * GNU extension: 417 * return non-option as argument to option 1 418 */ 419 optarg = nargv[optind++]; 420 return (INORDER); 421 } 422 if (!(flags & FLAG_PERMUTE)) { 423 /* 424 * If no permutation wanted, stop parsing 425 * at first non-option. 426 */ 427 return (-1); 428 } 429 /* do permutation */ 430 if (nonopt_start == -1) 431 nonopt_start = optind; 432 else if (nonopt_end != -1) { 433 permute_args(nonopt_start, nonopt_end, 434 optind, nargv); 435 nonopt_start = optind - 436 (nonopt_end - nonopt_start); 437 nonopt_end = -1; 438 } 439 optind++; 440 /* process next argument */ 441 goto start; 442 } 443 if (nonopt_start != -1 && nonopt_end == -1) 444 nonopt_end = optind; 445 446 /* 447 * If we have "-" do nothing, if "--" we are done. 448 */ 449 if (place[1] != '\0' && *++place == '-' && place[1] == '\0') { 450 optind++; 451 place = EMSG; 452 /* 453 * We found an option (--), so if we skipped 454 * non-options, we have to permute. 455 */ 456 if (nonopt_end != -1) { 457 permute_args(nonopt_start, nonopt_end, 458 optind, nargv); 459 optind -= nonopt_end - nonopt_start; 460 } 461 nonopt_start = nonopt_end = -1; 462 return (-1); 463 } 464 } 465 466 /* 467 * Check long options if: 468 * 1) we were passed some 469 * 2) the arg is not just "-" 470 * 3) either the arg starts with -- we are getopt_long_only() 471 */ 472 if (long_options != NULL && place != nargv[optind] && 473 (*place == '-' || (flags & FLAG_LONGONLY))) { 474 short_too = 0; 475#ifdef GNU_COMPATIBLE 476 dash_prefix = D_PREFIX; 477#endif 478 if (*place == '-') { 479 place++; /* --foo long option */ 480#ifdef GNU_COMPATIBLE 481 dash_prefix = DD_PREFIX; 482#endif 483 } else if (*place != ':' && strchr(options, *place) != NULL) 484 short_too = 1; /* could be short option too */ 485 486 optchar = parse_long_options(nargv, options, long_options, 487 idx, short_too); 488 if (optchar != -1) { 489 place = EMSG; 490 return (optchar); 491 } 492 } 493 494 if ((optchar = (int)*place++) == (int)':' || 495 (optchar == (int)'-' && *place != '\0') || 496 (oli = strchr(options, optchar)) == NULL) { 497 /* 498 * If the user specified "-" and '-' isn't listed in 499 * options, return -1 (non-option) as per POSIX. 500 * Otherwise, it is an unknown option character (or ':'). 501 */ 502 if (optchar == (int)'-' && *place == '\0') 503 return (-1); 504 if (!*place) 505 ++optind; 506#ifdef GNU_COMPATIBLE 507 if (PRINT_ERROR) 508 warnx(posixly_correct ? illoptchar : gnuoptchar, 509 optchar); 510#else 511 if (PRINT_ERROR) 512 warnx(illoptchar, optchar); 513#endif 514 optopt = optchar; 515 return (BADCH); 516 } 517 if (long_options != NULL && optchar == 'W' && oli[1] == ';') { 518 /* -W long-option */ 519 if (*place) /* no space */ 520 /* NOTHING */; 521 else if (++optind >= nargc) { /* no arg */ 522 place = EMSG; 523 if (PRINT_ERROR) 524 warnx(recargchar, optchar); 525 optopt = optchar; 526 return (BADARG); 527 } else /* white space */ 528 place = nargv[optind]; 529#ifdef GNU_COMPATIBLE 530 dash_prefix = W_PREFIX; 531#endif 532 optchar = parse_long_options(nargv, options, long_options, 533 idx, 0); 534 place = EMSG; 535 return (optchar); 536 } 537 if (*++oli != ':') { /* doesn't take argument */ 538 if (!*place) 539 ++optind; 540 } else { /* takes (optional) argument */ 541 optarg = NULL; 542 if (*place) /* no white space */ 543 optarg = place; 544 /* XXX: disable test for :: if PC? (GNU doesn't) */ 545 else if (oli[1] != ':') { /* arg not optional */ 546 if (++optind >= nargc) { /* no arg */ 547 place = EMSG; 548 if (PRINT_ERROR) 549 warnx(recargchar, optchar); 550 optopt = optchar; 551 return (BADARG); 552 } else 553 optarg = nargv[optind]; 554 } else if (!(flags & FLAG_PERMUTE)) { 555 /* 556 * If permutation is disabled, we can accept an 557 * optional arg separated by whitespace. 558 */ 559 if (optind + 1 < nargc) 560 optarg = nargv[++optind]; 561 } 562 place = EMSG; 563 ++optind; 564 } 565 /* dump back option letter */ 566 return (optchar); 567} 568 569#ifdef REPLACE_GETOPT 570/* 571 * getopt -- 572 * Parse argc/argv argument vector. 573 * 574 * [eventually this will replace the BSD getopt] 575 */ 576int 577getopt(int nargc, char * const *nargv, const char *options) 578{ 579 580 /* 581 * We don't pass FLAG_PERMUTE to getopt_internal() since 582 * the BSD getopt(3) (unlike GNU) has never done this. 583 * 584 * Furthermore, since many privileged programs call getopt() 585 * before dropping privileges it makes sense to keep things 586 * as simple (and bug-free) as possible. 587 */ 588 return (getopt_internal(nargc, nargv, options, NULL, NULL, 0)); 589} 590#endif /* REPLACE_GETOPT */ 591 592/* 593 * getopt_long -- 594 * Parse argc/argv argument vector. 595 */ 596int 597getopt_long(nargc, nargv, options, long_options, idx) 598 int nargc; 599 char * const *nargv; 600 const char *options; 601 const struct option *long_options; 602 int *idx; 603{ 604 605 return (getopt_internal(nargc, nargv, options, long_options, idx, 606 FLAG_PERMUTE)); 607} 608 609/* 610 * getopt_long_only -- 611 * Parse argc/argv argument vector. 612 */ 613int 614getopt_long_only(nargc, nargv, options, long_options, idx) 615 int nargc; 616 char * const *nargv; 617 const char *options; 618 const struct option *long_options; 619 int *idx; 620{ 621 622 return (getopt_internal(nargc, nargv, options, long_options, idx, 623 FLAG_PERMUTE|FLAG_LONGONLY)); 624} 625