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