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