1 /* $NetBSD: getopt_long.c,v 1.1.1.3 2014/06/13 01:48:21 christos Exp $ */ 2 /* NetBSD: getopt_long.c,v 1.21.4.1 2008/01/09 01:34:14 matt Exp */ 3 4 /*- 5 * Copyright (c) 2000 The NetBSD Foundation, Inc. 6 * All rights reserved. 7 * 8 * This code is derived from software contributed to The NetBSD Foundation 9 * by Dieter Baron and Thomas Klausner. 10 * 11 * Redistribution and use in source and binary forms, with or without 12 * modification, are permitted provided that the following conditions 13 * are met: 14 * 1. Redistributions of source code must retain the above copyright 15 * notice, this list of conditions and the following disclaimer. 16 * 2. Redistributions in binary form must reproduce the above copyright 17 * notice, this list of conditions and the following disclaimer in the 18 * documentation and/or other materials provided with the distribution. 19 * 20 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS 21 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 22 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 23 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 24 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 25 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 26 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 27 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 28 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 29 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 30 * POSSIBILITY OF SUCH DAMAGE. 31 */ 32 33 #include "file.h" 34 35 #ifndef lint 36 #if 0 37 FILE_RCSID("@(#)$File: getopt_long.c,v 1.6 2009/02/13 18:48:05 christos Exp $") 38 #else 39 __RCSID("$NetBSD: getopt_long.c,v 1.1.1.3 2014/06/13 01:48:21 christos Exp $"); 40 #endif 41 #endif /* lint */ 42 43 #include <assert.h> 44 #ifdef HAVE_ERR_H 45 #include <err.h> 46 #else 47 #define warnx printf 48 #endif 49 #include <errno.h> 50 #if defined(HAVE_GETOPT_H) && defined(HAVE_STRUCT_OPTION) 51 #include <getopt.h> 52 #else 53 #include "mygetopt.h" 54 #endif 55 #include <stdlib.h> 56 #include <string.h> 57 58 #define REPLACE_GETOPT 59 60 #ifndef _DIAGASSERT 61 #define _DIAGASSERT assert 62 #endif 63 64 #ifdef REPLACE_GETOPT 65 #ifdef __weak_alias 66 __weak_alias(getopt,_getopt) 67 #endif 68 int opterr = 1; /* if error message should be printed */ 69 int optind = 1; /* index into parent argv vector */ 70 int optopt = '?'; /* character checked for validity */ 71 int optreset; /* reset getopt */ 72 char *optarg; /* argument associated with option */ 73 #elif HAVE_NBTOOL_CONFIG_H && !HAVE_DECL_OPTRESET 74 static int optreset; 75 #endif 76 77 #ifdef __weak_alias 78 __weak_alias(getopt_long,_getopt_long) 79 #endif 80 81 #define IGNORE_FIRST (*options == '-' || *options == '+') 82 #define PRINT_ERROR ((opterr) && ((*options != ':') \ 83 || (IGNORE_FIRST && options[1] != ':'))) 84 #define IS_POSIXLY_CORRECT (getenv("POSIXLY_CORRECT") != NULL) 85 #define PERMUTE (!IS_POSIXLY_CORRECT && !IGNORE_FIRST) 86 /* XXX: GNU ignores PC if *options == '-' */ 87 #define IN_ORDER (!IS_POSIXLY_CORRECT && *options == '-') 88 89 /* return values */ 90 #define BADCH (int)'?' 91 #define BADARG ((IGNORE_FIRST && options[1] == ':') \ 92 || (*options == ':') ? (int)':' : (int)'?') 93 #define INORDER (int)1 94 95 #define EMSG "" 96 97 static int getopt_internal(int, char **, const char *); 98 static int gcd(int, int); 99 static void permute_args(int, int, int, char **); 100 101 static const char *place = EMSG; /* option letter processing */ 102 103 /* XXX: set optreset to 1 rather than these two */ 104 static int nonopt_start = -1; /* first non option argument (for permute) */ 105 static int nonopt_end = -1; /* first option after non options (for permute) */ 106 107 /* Error messages */ 108 static const char recargchar[] = "option requires an argument -- %c"; 109 static const char recargstring[] = "option requires an argument -- %s"; 110 static const char ambig[] = "ambiguous option -- %.*s"; 111 static const char noarg[] = "option doesn't take an argument -- %.*s"; 112 static const char illoptchar[] = "unknown option -- %c"; 113 static const char illoptstring[] = "unknown option -- %s"; 114 115 116 /* 117 * Compute the greatest common divisor of a and b. 118 */ 119 static int 120 gcd(a, b) 121 int a; 122 int b; 123 { 124 int c; 125 126 c = a % b; 127 while (c != 0) { 128 a = b; 129 b = c; 130 c = a % b; 131 } 132 133 return b; 134 } 135 136 /* 137 * Exchange the block from nonopt_start to nonopt_end with the block 138 * from nonopt_end to opt_end (keeping the same order of arguments 139 * in each block). 140 */ 141 static void 142 permute_args(panonopt_start, panonopt_end, opt_end, nargv) 143 int panonopt_start; 144 int panonopt_end; 145 int opt_end; 146 char **nargv; 147 { 148 int cstart, cyclelen, i, j, ncycle, nnonopts, nopts, pos; 149 char *swap; 150 151 _DIAGASSERT(nargv != NULL); 152 153 /* 154 * compute lengths of blocks and number and size of cycles 155 */ 156 nnonopts = panonopt_end - panonopt_start; 157 nopts = opt_end - panonopt_end; 158 ncycle = gcd(nnonopts, nopts); 159 cyclelen = (opt_end - panonopt_start) / ncycle; 160 161 for (i = 0; i < ncycle; i++) { 162 cstart = panonopt_end+i; 163 pos = cstart; 164 for (j = 0; j < cyclelen; j++) { 165 if (pos >= panonopt_end) 166 pos -= nnonopts; 167 else 168 pos += nopts; 169 swap = nargv[pos]; 170 nargv[pos] = nargv[cstart]; 171 nargv[cstart] = swap; 172 } 173 } 174 } 175 176 /* 177 * getopt_internal -- 178 * Parse argc/argv argument vector. Called by user level routines. 179 * Returns -2 if -- is found (can be long option or end of options marker). 180 */ 181 static int 182 getopt_internal(nargc, nargv, options) 183 int nargc; 184 char **nargv; 185 const char *options; 186 { 187 char *oli; /* option letter list index */ 188 int optchar; 189 190 _DIAGASSERT(nargv != NULL); 191 _DIAGASSERT(options != NULL); 192 193 optarg = NULL; 194 195 /* 196 * XXX Some programs (like rsyncd) expect to be able to 197 * XXX re-initialize optind to 0 and have getopt_long(3) 198 * XXX properly function again. Work around this braindamage. 199 */ 200 if (optind == 0) 201 optind = 1; 202 203 if (optreset) 204 nonopt_start = nonopt_end = -1; 205 start: 206 if (optreset || !*place) { /* update scanning pointer */ 207 optreset = 0; 208 if (optind >= nargc) { /* end of argument vector */ 209 place = EMSG; 210 if (nonopt_end != -1) { 211 /* do permutation, if we have to */ 212 permute_args(nonopt_start, nonopt_end, 213 optind, nargv); 214 optind -= nonopt_end - nonopt_start; 215 } 216 else if (nonopt_start != -1) { 217 /* 218 * If we skipped non-options, set optind 219 * to the first of them. 220 */ 221 optind = nonopt_start; 222 } 223 nonopt_start = nonopt_end = -1; 224 return -1; 225 } 226 if ((*(place = nargv[optind]) != '-') 227 || (place[1] == '\0')) { /* found non-option */ 228 place = EMSG; 229 if (IN_ORDER) { 230 /* 231 * GNU extension: 232 * return non-option as argument to option 1 233 */ 234 optarg = nargv[optind++]; 235 return INORDER; 236 } 237 if (!PERMUTE) { 238 /* 239 * if no permutation wanted, stop parsing 240 * at first non-option 241 */ 242 return -1; 243 } 244 /* do permutation */ 245 if (nonopt_start == -1) 246 nonopt_start = optind; 247 else if (nonopt_end != -1) { 248 permute_args(nonopt_start, nonopt_end, 249 optind, nargv); 250 nonopt_start = optind - 251 (nonopt_end - nonopt_start); 252 nonopt_end = -1; 253 } 254 optind++; 255 /* process next argument */ 256 goto start; 257 } 258 if (nonopt_start != -1 && nonopt_end == -1) 259 nonopt_end = optind; 260 if (place[1] && *++place == '-') { /* found "--" */ 261 place++; 262 return -2; 263 } 264 } 265 if ((optchar = (int)*place++) == (int)':' || 266 (oli = strchr(options + (IGNORE_FIRST ? 1 : 0), optchar)) == NULL) { 267 /* option letter unknown or ':' */ 268 if (!*place) 269 ++optind; 270 if (PRINT_ERROR) 271 warnx(illoptchar, optchar); 272 optopt = optchar; 273 return BADCH; 274 } 275 if (optchar == 'W' && oli[1] == ';') { /* -W long-option */ 276 /* XXX: what if no long options provided (called by getopt)? */ 277 if (*place) 278 return -2; 279 280 if (++optind >= nargc) { /* no arg */ 281 place = EMSG; 282 if (PRINT_ERROR) 283 warnx(recargchar, optchar); 284 optopt = optchar; 285 return BADARG; 286 } else /* white space */ 287 place = nargv[optind]; 288 /* 289 * Handle -W arg the same as --arg (which causes getopt to 290 * stop parsing). 291 */ 292 return -2; 293 } 294 if (*++oli != ':') { /* doesn't take argument */ 295 if (!*place) 296 ++optind; 297 } else { /* takes (optional) argument */ 298 optarg = NULL; 299 if (*place) /* no white space */ 300 optarg = (char *)place; 301 /* XXX: disable test for :: if PC? (GNU doesn't) */ 302 else if (oli[1] != ':') { /* arg not optional */ 303 if (++optind >= nargc) { /* no arg */ 304 place = EMSG; 305 if (PRINT_ERROR) 306 warnx(recargchar, optchar); 307 optopt = optchar; 308 return BADARG; 309 } else 310 optarg = nargv[optind]; 311 } 312 place = EMSG; 313 ++optind; 314 } 315 /* dump back option letter */ 316 return optchar; 317 } 318 319 #ifdef REPLACE_GETOPT 320 /* 321 * getopt -- 322 * Parse argc/argv argument vector. 323 * 324 * [eventually this will replace the real getopt] 325 */ 326 int 327 getopt(nargc, nargv, options) 328 int nargc; 329 char * const *nargv; 330 const char *options; 331 { 332 int retval; 333 334 _DIAGASSERT(nargv != NULL); 335 _DIAGASSERT(options != NULL); 336 337 retval = getopt_internal(nargc, (char **)nargv, options); 338 if (retval == -2) { 339 ++optind; 340 /* 341 * We found an option (--), so if we skipped non-options, 342 * we have to permute. 343 */ 344 if (nonopt_end != -1) { 345 permute_args(nonopt_start, nonopt_end, optind, 346 (char **)nargv); 347 optind -= nonopt_end - nonopt_start; 348 } 349 nonopt_start = nonopt_end = -1; 350 retval = -1; 351 } 352 return retval; 353 } 354 #endif 355 356 /* 357 * getopt_long -- 358 * Parse argc/argv argument vector. 359 */ 360 int 361 getopt_long(nargc, nargv, options, long_options, idx) 362 int nargc; 363 char * const *nargv; 364 const char *options; 365 const struct option *long_options; 366 int *idx; 367 { 368 int retval; 369 370 #define IDENTICAL_INTERPRETATION(_x, _y) \ 371 (long_options[(_x)].has_arg == long_options[(_y)].has_arg && \ 372 long_options[(_x)].flag == long_options[(_y)].flag && \ 373 long_options[(_x)].val == long_options[(_y)].val) 374 375 _DIAGASSERT(nargv != NULL); 376 _DIAGASSERT(options != NULL); 377 _DIAGASSERT(long_options != NULL); 378 /* idx may be NULL */ 379 380 retval = getopt_internal(nargc, (char **)nargv, options); 381 if (retval == -2) { 382 char *current_argv, *has_equal; 383 size_t current_argv_len; 384 int i, ambiguous, match; 385 386 current_argv = (char *)place; 387 match = -1; 388 ambiguous = 0; 389 390 optind++; 391 place = EMSG; 392 393 if (*current_argv == '\0') { /* found "--" */ 394 /* 395 * We found an option (--), so if we skipped 396 * non-options, we have to permute. 397 */ 398 if (nonopt_end != -1) { 399 permute_args(nonopt_start, nonopt_end, 400 optind, (char **)nargv); 401 optind -= nonopt_end - nonopt_start; 402 } 403 nonopt_start = nonopt_end = -1; 404 return -1; 405 } 406 if ((has_equal = strchr(current_argv, '=')) != NULL) { 407 /* argument found (--option=arg) */ 408 current_argv_len = has_equal - current_argv; 409 has_equal++; 410 } else 411 current_argv_len = strlen(current_argv); 412 413 for (i = 0; long_options[i].name; i++) { 414 /* find matching long option */ 415 if (strncmp(current_argv, long_options[i].name, 416 current_argv_len)) 417 continue; 418 419 if (strlen(long_options[i].name) == 420 (unsigned)current_argv_len) { 421 /* exact match */ 422 match = i; 423 ambiguous = 0; 424 break; 425 } 426 if (match == -1) /* partial match */ 427 match = i; 428 else if (!IDENTICAL_INTERPRETATION(i, match)) 429 ambiguous = 1; 430 } 431 if (ambiguous) { 432 /* ambiguous abbreviation */ 433 if (PRINT_ERROR) 434 warnx(ambig, (int)current_argv_len, 435 current_argv); 436 optopt = 0; 437 return BADCH; 438 } 439 if (match != -1) { /* option found */ 440 if (long_options[match].has_arg == no_argument 441 && has_equal) { 442 if (PRINT_ERROR) 443 warnx(noarg, (int)current_argv_len, 444 current_argv); 445 /* 446 * XXX: GNU sets optopt to val regardless of 447 * flag 448 */ 449 if (long_options[match].flag == NULL) 450 optopt = long_options[match].val; 451 else 452 optopt = 0; 453 return BADARG; 454 } 455 if (long_options[match].has_arg == required_argument || 456 long_options[match].has_arg == optional_argument) { 457 if (has_equal) 458 optarg = has_equal; 459 else if (long_options[match].has_arg == 460 required_argument) { 461 /* 462 * optional argument doesn't use 463 * next nargv 464 */ 465 optarg = nargv[optind++]; 466 } 467 } 468 if ((long_options[match].has_arg == required_argument) 469 && (optarg == NULL)) { 470 /* 471 * Missing argument; leading ':' 472 * indicates no error should be generated 473 */ 474 if (PRINT_ERROR) 475 warnx(recargstring, current_argv); 476 /* 477 * XXX: GNU sets optopt to val regardless 478 * of flag 479 */ 480 if (long_options[match].flag == NULL) 481 optopt = long_options[match].val; 482 else 483 optopt = 0; 484 --optind; 485 return BADARG; 486 } 487 } else { /* unknown option */ 488 if (PRINT_ERROR) 489 warnx(illoptstring, current_argv); 490 optopt = 0; 491 return BADCH; 492 } 493 if (long_options[match].flag) { 494 *long_options[match].flag = long_options[match].val; 495 retval = 0; 496 } else 497 retval = long_options[match].val; 498 if (idx) 499 *idx = match; 500 } 501 return retval; 502 #undef IDENTICAL_INTERPRETATION 503 } 504