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