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 <[email protected]> 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) 61 static 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$"); 66 67 #include <err.h> 68 #include <errno.h> 69 #include <getopt.h> 70 #include <stdlib.h> 71 #include <string.h> 72 73 #ifdef notyet 74 #define REPLACE_GETOPT /* use this getopt as the system getopt(3) */ 75 #endif 76 77 #ifdef REPLACE_GETOPT 78 int opterr = 1; /* if error message should be printed */ 79 int optind = 1; /* index into parent argv vector */ 80 int optopt = '?'; /* character checked for validity */ 81 int optreset; /* reset getopt */ 82 char *optarg; /* argument associated with option */ 83 #endif 84 85 #define PRINT_ERROR ((opterr) && (*options != ':')) 86 87 #define FLAG_PERMUTE 0x01 /* permute non-options to the end of argv */ 88 #define FLAG_ALLARGS 0x02 /* treat non-options as args to option "-1" */ 89 #define FLAG_LONGONLY 0x04 /* operate as getopt_long_only */ 90 91 /* return values */ 92 #define BADCH (int)'?' 93 #define BADARG ((*options == ':') ? (int)':' : (int)'?') 94 #define INORDER (int)1 95 96 #define EMSG "" 97 98 static int getopt_internal(int, char * const *, const char *, 99 const struct option *, int *, int); 100 static int parse_long_options(char * const *, const char *, 101 const struct option *, int *, int); 102 static int gcd(int, int); 103 static void permute_args(int, int, int, char * const *); 104 105 static char *place = EMSG; /* option letter processing */ 106 107 /* XXX: set optreset to 1 rather than these two */ 108 static int nonopt_start = -1; /* first non option argument (for permute) */ 109 static int nonopt_end = -1; /* first option after non options (for permute) */ 110 111 /* Error messages */ 112 static const char recargchar[] = "option requires an argument -- %c"; 113 static const char recargstring[] = "option requires an argument -- %s"; 114 static const char ambig[] = "ambiguous option -- %.*s"; 115 static const char noarg[] = "option doesn't take an argument -- %.*s"; 116 static const char illoptchar[] = "unknown option -- %c"; 117 static const char illoptstring[] = "unknown option -- %s"; 118 119 /* 120 * Compute the greatest common divisor of a and b. 121 */ 122 static int 123 gcd(int a, int b) 124 { 125 int c; 126 127 c = a % b; 128 while (c != 0) { 129 a = b; 130 b = c; 131 c = a % b; 132 } 133 134 return (b); 135 } 136 137 /* 138 * Exchange the block from nonopt_start to nonopt_end with the block 139 * from nonopt_end to opt_end (keeping the same order of arguments 140 * in each block). 141 */ 142 static void 143 permute_args(int panonopt_start, int panonopt_end, int opt_end, 144 char * const *nargv) 145 { 146 int cstart, cyclelen, i, j, ncycle, nnonopts, nopts, pos; 147 char *swap; 148 149 /* 150 * compute lengths of blocks and number and size of cycles 151 */ 152 nnonopts = panonopt_end - panonopt_start; 153 nopts = opt_end - panonopt_end; 154 ncycle = gcd(nnonopts, nopts); 155 cyclelen = (opt_end - panonopt_start) / ncycle; 156 157 for (i = 0; i < ncycle; i++) { 158 cstart = panonopt_end+i; 159 pos = cstart; 160 for (j = 0; j < cyclelen; j++) { 161 if (pos >= panonopt_end) 162 pos -= nnonopts; 163 else 164 pos += nopts; 165 swap = nargv[pos]; 166 /* LINTED const cast */ 167 ((char **) nargv)[pos] = nargv[cstart]; 168 /* LINTED const cast */ 169 ((char **)nargv)[cstart] = swap; 170 } 171 } 172 } 173 174 /* 175 * parse_long_options -- 176 * Parse long options in argc/argv argument vector. 177 * Returns -1 if short_too is set and the option does not match long_options. 178 */ 179 static int 180 parse_long_options(char * const *nargv, const char *options, 181 const struct option *long_options, int *idx, int short_too) 182 { 183 char *current_argv, *has_equal; 184 size_t current_argv_len; 185 int i, match; 186 187 current_argv = place; 188 match = -1; 189 190 optind++; 191 192 if ((has_equal = strchr(current_argv, '=')) != NULL) { 193 /* argument found (--option=arg) */ 194 current_argv_len = has_equal - current_argv; 195 has_equal++; 196 } else 197 current_argv_len = strlen(current_argv); 198 199 for (i = 0; long_options[i].name; i++) { 200 /* find matching long option */ 201 if (strncmp(current_argv, long_options[i].name, 202 current_argv_len)) 203 continue; 204 205 if (strlen(long_options[i].name) == current_argv_len) { 206 /* exact match */ 207 match = i; 208 break; 209 } 210 /* 211 * If this is a known short option, don't allow 212 * a partial match of a single character. 213 */ 214 if (short_too && current_argv_len == 1) 215 continue; 216 217 if (match == -1) /* partial match */ 218 match = i; 219 else { 220 /* ambiguous abbreviation */ 221 if (PRINT_ERROR) 222 warnx(ambig, (int)current_argv_len, 223 current_argv); 224 optopt = 0; 225 return (BADCH); 226 } 227 } 228 if (match != -1) { /* option found */ 229 if (long_options[match].has_arg == no_argument 230 && has_equal) { 231 if (PRINT_ERROR) 232 warnx(noarg, (int)current_argv_len, 233 current_argv); 234 /* 235 * XXX: GNU sets optopt to val regardless of flag 236 */ 237 if (long_options[match].flag == NULL) 238 optopt = long_options[match].val; 239 else 240 optopt = 0; 241 return (BADARG); 242 } 243 if (long_options[match].has_arg == required_argument || 244 long_options[match].has_arg == optional_argument) { 245 if (has_equal) 246 optarg = has_equal; 247 else if (long_options[match].has_arg == 248 required_argument) { 249 /* 250 * optional argument doesn't use next nargv 251 */ 252 optarg = nargv[optind++]; 253 } 254 } 255 if ((long_options[match].has_arg == required_argument) 256 && (optarg == NULL)) { 257 /* 258 * Missing argument; leading ':' indicates no error 259 * should be generated. 260 */ 261 if (PRINT_ERROR) 262 warnx(recargstring, 263 current_argv); 264 /* 265 * XXX: GNU sets optopt to val regardless of flag 266 */ 267 if (long_options[match].flag == NULL) 268 optopt = long_options[match].val; 269 else 270 optopt = 0; 271 --optind; 272 return (BADARG); 273 } 274 } else { /* unknown option */ 275 if (short_too) { 276 --optind; 277 return (-1); 278 } 279 if (PRINT_ERROR) 280 warnx(illoptstring, current_argv); 281 optopt = 0; 282 return (BADCH); 283 } 284 if (idx) 285 *idx = match; 286 if (long_options[match].flag) { 287 *long_options[match].flag = long_options[match].val; 288 return (0); 289 } else 290 return (long_options[match].val); 291 } 292 293 /* 294 * getopt_internal -- 295 * Parse argc/argv argument vector. Called by user level routines. 296 */ 297 static int 298 getopt_internal(int nargc, char * const *nargv, const char *options, 299 const struct option *long_options, int *idx, int flags) 300 { 301 char *oli; /* option letter list index */ 302 int optchar, short_too; 303 static int posixly_correct = -1; 304 305 if (options == NULL) 306 return (-1); 307 308 /* 309 * Disable GNU extensions if POSIXLY_CORRECT is set or options 310 * string begins with a '+'. 311 */ 312 if (posixly_correct == -1) 313 posixly_correct = (getenv("POSIXLY_CORRECT") != NULL); 314 if (posixly_correct || *options == '+') 315 flags &= ~FLAG_PERMUTE; 316 /* 317 * Code "else if (*options == '-')" was here. 318 * Try to be more GNU compatible, configure's use us! 319 */ 320 if (*options == '-') 321 flags |= FLAG_ALLARGS; 322 if (*options == '+' || *options == '-') 323 options++; 324 325 /* 326 * XXX Some GNU programs (like cvs) set optind to 0 instead of 327 * XXX using optreset. Work around this braindamage. 328 */ 329 if (optind == 0) 330 optind = optreset = 1; 331 332 optarg = NULL; 333 if (optreset) 334 nonopt_start = nonopt_end = -1; 335 start: 336 if (optreset || !*place) { /* update scanning pointer */ 337 optreset = 0; 338 if (optind >= nargc) { /* end of argument vector */ 339 place = EMSG; 340 if (nonopt_end != -1) { 341 /* do permutation, if we have to */ 342 permute_args(nonopt_start, nonopt_end, 343 optind, nargv); 344 optind -= nonopt_end - nonopt_start; 345 } 346 else if (nonopt_start != -1) { 347 /* 348 * If we skipped non-options, set optind 349 * to the first of them. 350 */ 351 optind = nonopt_start; 352 } 353 nonopt_start = nonopt_end = -1; 354 return (-1); 355 } 356 if (*(place = nargv[optind]) != '-' || 357 (place[1] == '\0' && strchr(options, '-') == NULL)) { 358 place = EMSG; /* found non-option */ 359 if (flags & FLAG_ALLARGS) { 360 /* 361 * GNU extension: 362 * return non-option as argument to option 1 363 */ 364 optarg = nargv[optind++]; 365 return (INORDER); 366 } 367 if (!(flags & FLAG_PERMUTE)) { 368 /* 369 * If no permutation wanted, stop parsing 370 * at first non-option. 371 */ 372 return (-1); 373 } 374 /* do permutation */ 375 if (nonopt_start == -1) 376 nonopt_start = optind; 377 else if (nonopt_end != -1) { 378 permute_args(nonopt_start, nonopt_end, 379 optind, nargv); 380 nonopt_start = optind - 381 (nonopt_end - nonopt_start); 382 nonopt_end = -1; 383 } 384 optind++; 385 /* process next argument */ 386 goto start; 387 } 388 if (nonopt_start != -1 && nonopt_end == -1) 389 nonopt_end = optind; 390 391 /* 392 * If we have "-" do nothing, if "--" we are done. 393 */ 394 if (place[1] != '\0' && *++place == '-' && place[1] == '\0') { 395 optind++; 396 place = EMSG; 397 /* 398 * We found an option (--), so if we skipped 399 * non-options, we have to permute. 400 */ 401 if (nonopt_end != -1) { 402 permute_args(nonopt_start, nonopt_end, 403 optind, nargv); 404 optind -= nonopt_end - nonopt_start; 405 } 406 nonopt_start = nonopt_end = -1; 407 return (-1); 408 } 409 } 410 411 /* 412 * Check long options if: 413 * 1) we were passed some 414 * 2) the arg is not just "-" 415 * 3) either the arg starts with -- we are getopt_long_only() 416 */ 417 if (long_options != NULL && place != nargv[optind] && 418 (*place == '-' || (flags & FLAG_LONGONLY))) { 419 short_too = 0; 420 if (*place == '-') 421 place++; /* --foo long option */ 422 else if (*place != ':' && strchr(options, *place) != NULL) 423 short_too = 1; /* could be short option too */ 424 425 optchar = parse_long_options(nargv, options, long_options, 426 idx, short_too); 427 if (optchar != -1) { 428 place = EMSG; 429 return (optchar); 430 } 431 } 432 433 if ((optchar = (int)*place++) == (int)':' || 434 (optchar == (int)'-' && *place != '\0') || 435 (oli = strchr(options, optchar)) == NULL) { 436 /* 437 * If the user specified "-" and '-' isn't listed in 438 * options, return -1 (non-option) as per POSIX. 439 * Otherwise, it is an unknown option character (or ':'). 440 */ 441 if (optchar == (int)'-' && *place == '\0') 442 return (-1); 443 if (!*place) 444 ++optind; 445 if (PRINT_ERROR) 446 warnx(illoptchar, optchar); 447 optopt = optchar; 448 return (BADCH); 449 } 450 if (long_options != NULL && optchar == 'W' && oli[1] == ';') { 451 /* -W long-option */ 452 if (*place) /* no space */ 453 /* NOTHING */; 454 else if (++optind >= nargc) { /* no arg */ 455 place = EMSG; 456 if (PRINT_ERROR) 457 warnx(recargchar, optchar); 458 optopt = optchar; 459 return (BADARG); 460 } else /* white space */ 461 place = nargv[optind]; 462 optchar = parse_long_options(nargv, options, long_options, 463 idx, 0); 464 place = EMSG; 465 return (optchar); 466 } 467 if (*++oli != ':') { /* doesn't take argument */ 468 if (!*place) 469 ++optind; 470 } else { /* takes (optional) argument */ 471 optarg = NULL; 472 if (*place) /* no white space */ 473 optarg = place; 474 /* XXX: disable test for :: if PC? (GNU doesn't) */ 475 else if (oli[1] != ':') { /* arg not optional */ 476 if (++optind >= nargc) { /* no arg */ 477 place = EMSG; 478 if (PRINT_ERROR) 479 warnx(recargchar, optchar); 480 optopt = optchar; 481 return (BADARG); 482 } else 483 optarg = nargv[optind]; 484 } else if (!(flags & FLAG_PERMUTE)) { 485 /* 486 * If permutation is disabled, we can accept an 487 * optional arg separated by whitespace. 488 */ 489 if (optind + 1 < nargc) 490 optarg = nargv[++optind]; 491 } 492 place = EMSG; 493 ++optind; 494 } 495 /* dump back option letter */ 496 return (optchar); 497 } 498 499 #ifdef REPLACE_GETOPT 500 /* 501 * getopt -- 502 * Parse argc/argv argument vector. 503 * 504 * [eventually this will replace the BSD getopt] 505 */ 506 int 507 getopt(int nargc, char * const *nargv, const char *options) 508 { 509 510 /* 511 * We don't pass FLAG_PERMUTE to getopt_internal() since 512 * the BSD getopt(3) (unlike GNU) has never done this. 513 * 514 * Furthermore, since many privileged programs call getopt() 515 * before dropping privileges it makes sense to keep things 516 * as simple (and bug-free) as possible. 517 */ 518 return (getopt_internal(nargc, nargv, options, NULL, NULL, 0)); 519 } 520 #endif /* REPLACE_GETOPT */ 521 522 /* 523 * getopt_long -- 524 * Parse argc/argv argument vector. 525 */ 526 int 527 getopt_long(nargc, nargv, options, long_options, idx) 528 int nargc; 529 char * const *nargv; 530 const char *options; 531 const struct option *long_options; 532 int *idx; 533 { 534 535 return (getopt_internal(nargc, nargv, options, long_options, idx, 536 FLAG_PERMUTE)); 537 } 538 539 /* 540 * getopt_long_only -- 541 * Parse argc/argv argument vector. 542 */ 543 int 544 getopt_long_only(nargc, nargv, options, long_options, idx) 545 int nargc; 546 char * const *nargv; 547 const char *options; 548 const struct option *long_options; 549 int *idx; 550 { 551 552 return (getopt_internal(nargc, nargv, options, long_options, idx, 553 FLAG_PERMUTE|FLAG_LONGONLY)); 554 } 555