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