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