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