| 1 |
/* $NetBSD: getopt_long.c,v 1.25 2009/03/20 14:05:54 joerg 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 "mowgli.h" |
| 33 |
|
| 34 |
int mowgli_opterr = 1; /* if error message should be printed */ |
| 35 |
int mowgli_optind = 1; /* index into parent argv vector */ |
| 36 |
int mowgli_optopt = '?';/* character checked for validity */ |
| 37 |
int mowgli_optreset = 0;/* reset getopt */ |
| 38 |
char *mowgli_optarg = NULL; /* argument associated with option */ |
| 39 |
|
| 40 |
/* XXX: suppress const warnings */ |
| 41 |
#define PTR_UNCONSTIFY(a) ((void *)((uintptr_t)((const void *)(a)))) |
| 42 |
|
| 43 |
#define IGNORE_FIRST (*options == '-' || *options == '+') |
| 44 |
#define PRINT_ERROR ((mowgli_opterr) && ((*options != ':') \ |
| 45 |
|| (IGNORE_FIRST && options[1] != ':'))) |
| 46 |
#define IS_POSIXLY_CORRECT (getenv("POSIXLY_CORRECT") != NULL) |
| 47 |
#define PERMUTE (!IS_POSIXLY_CORRECT && !IGNORE_FIRST) |
| 48 |
|
| 49 |
/* XXX: GNU ignores PC if *options == '-' */ |
| 50 |
#define IN_ORDER (!IS_POSIXLY_CORRECT && *options == '-') |
| 51 |
|
| 52 |
/* return values */ |
| 53 |
#define BADCH (int) '?' |
| 54 |
#define BADARG ((IGNORE_FIRST && options[1] == ':') \ |
| 55 |
|| (*options == ':') ? (int) ':' : (int) '?') |
| 56 |
#define INORDER (int) 1 |
| 57 |
|
| 58 |
#define EMSG "" |
| 59 |
|
| 60 |
static inline void MOWGLI_FATTR_PRINTF(1, 2) |
| 61 |
warnx(const char *fmt, ...) |
| 62 |
{ |
| 63 |
va_list ap; |
| 64 |
|
| 65 |
va_start(ap, fmt); |
| 66 |
vfprintf(stderr, fmt, ap); |
| 67 |
fputc('\n', stderr); |
| 68 |
va_end(ap); |
| 69 |
} |
| 70 |
|
| 71 |
static int getopt_internal(int, char **, const char *); |
| 72 |
static int gcd(int, int); |
| 73 |
static void permute_args(int, int, int, char **); |
| 74 |
|
| 75 |
static const char *place = EMSG;/* option letter processing */ |
| 76 |
|
| 77 |
/* XXX: set mowgli_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 |
* Compute the greatest common divisor of a and b. |
| 91 |
*/ |
| 92 |
static int |
| 93 |
gcd(int a, int b) |
| 94 |
{ |
| 95 |
int c; |
| 96 |
|
| 97 |
c = a % b; |
| 98 |
|
| 99 |
while (c != 0) |
| 100 |
{ |
| 101 |
a = b; |
| 102 |
b = c; |
| 103 |
c = a % b; |
| 104 |
} |
| 105 |
|
| 106 |
return b; |
| 107 |
} |
| 108 |
|
| 109 |
/* |
| 110 |
* Exchange the block from nonopt_start to nonopt_end with the block |
| 111 |
* from nonopt_end to opt_end (keeping the same order of arguments |
| 112 |
* in each block). |
| 113 |
*/ |
| 114 |
static void |
| 115 |
permute_args(int panonopt_start, int panonopt_end, int opt_end, char **nargv) |
| 116 |
{ |
| 117 |
int cstart, cyclelen, i, j, ncycle, nnonopts, nopts, pos; |
| 118 |
char *swap; |
| 119 |
|
| 120 |
return_if_fail(nargv != NULL); |
| 121 |
|
| 122 |
/* |
| 123 |
* compute lengths of blocks and number and size of cycles |
| 124 |
*/ |
| 125 |
nnonopts = panonopt_end - panonopt_start; |
| 126 |
nopts = opt_end - panonopt_end; |
| 127 |
ncycle = gcd(nnonopts, nopts); |
| 128 |
cyclelen = (opt_end - panonopt_start) / ncycle; |
| 129 |
|
| 130 |
for (i = 0; i < ncycle; i++) |
| 131 |
{ |
| 132 |
cstart = panonopt_end + i; |
| 133 |
pos = cstart; |
| 134 |
|
| 135 |
for (j = 0; j < cyclelen; j++) |
| 136 |
{ |
| 137 |
if (pos >= panonopt_end) |
| 138 |
pos -= nnonopts; |
| 139 |
else |
| 140 |
pos += nopts; |
| 141 |
|
| 142 |
swap = nargv[pos]; |
| 143 |
nargv[pos] = nargv[cstart]; |
| 144 |
nargv[cstart] = swap; |
| 145 |
} |
| 146 |
} |
| 147 |
} |
| 148 |
|
| 149 |
/* |
| 150 |
* getopt_internal -- |
| 151 |
* Parse argc/argv argument vector. Called by user level routines. |
| 152 |
* Returns -2 if -- is found (can be long option or end of options marker). |
| 153 |
*/ |
| 154 |
static int |
| 155 |
getopt_internal(int nargc, char **nargv, const char *options) |
| 156 |
{ |
| 157 |
char *oli; /* option letter list index */ |
| 158 |
int optchar; |
| 159 |
|
| 160 |
return_val_if_fail(nargv != NULL, -1); |
| 161 |
return_val_if_fail(options != NULL, -1); |
| 162 |
|
| 163 |
mowgli_optarg = NULL; |
| 164 |
|
| 165 |
/* |
| 166 |
* XXX Some programs (like rsyncd) expect to be able to |
| 167 |
* XXX re-initialize mowgli_optind to 0 and have getopt_long(3) |
| 168 |
* XXX properly function again. Work around this braindamage. |
| 169 |
*/ |
| 170 |
if (mowgli_optind == 0) |
| 171 |
mowgli_optind = 1; |
| 172 |
|
| 173 |
if (mowgli_optreset) |
| 174 |
nonopt_start = nonopt_end = -1; |
| 175 |
|
| 176 |
start: |
| 177 |
|
| 178 |
if (mowgli_optreset || !*place) /* update scanning pointer */ |
| 179 |
{ |
| 180 |
mowgli_optreset = 0; |
| 181 |
|
| 182 |
if (mowgli_optind >= nargc) /* end of argument vector */ |
| 183 |
{ |
| 184 |
place = EMSG; |
| 185 |
|
| 186 |
if (nonopt_end != -1) |
| 187 |
{ |
| 188 |
/* do permutation, if we have to */ |
| 189 |
permute_args(nonopt_start, nonopt_end, |
| 190 |
mowgli_optind, nargv); |
| 191 |
mowgli_optind -= nonopt_end - nonopt_start; |
| 192 |
} |
| 193 |
else if (nonopt_start != -1) |
| 194 |
{ |
| 195 |
/* |
| 196 |
* If we skipped non-options, set mowgli_optind |
| 197 |
* to the first of them. |
| 198 |
*/ |
| 199 |
mowgli_optind = nonopt_start; |
| 200 |
} |
| 201 |
|
| 202 |
nonopt_start = nonopt_end = -1; |
| 203 |
return -1; |
| 204 |
} |
| 205 |
|
| 206 |
if ((*(place = nargv[mowgli_optind]) != '-') |
| 207 |
|| (place[1] == '\0')) /* found non-option */ |
| 208 |
{ |
| 209 |
place = EMSG; |
| 210 |
|
| 211 |
if (IN_ORDER) |
| 212 |
{ |
| 213 |
/* |
| 214 |
* GNU extension: |
| 215 |
* return non-option as argument to option 1 |
| 216 |
*/ |
| 217 |
mowgli_optarg = nargv[mowgli_optind++]; |
| 218 |
return INORDER; |
| 219 |
} |
| 220 |
|
| 221 |
if (!PERMUTE) |
| 222 |
/* |
| 223 |
* if no permutation wanted, stop parsing |
| 224 |
* at first non-option |
| 225 |
*/ |
| 226 |
return -1; |
| 227 |
|
| 228 |
/* do permutation */ |
| 229 |
if (nonopt_start == -1) |
| 230 |
{ |
| 231 |
nonopt_start = mowgli_optind; |
| 232 |
} |
| 233 |
else if (nonopt_end != -1) |
| 234 |
{ |
| 235 |
permute_args(nonopt_start, nonopt_end, |
| 236 |
mowgli_optind, nargv); |
| 237 |
nonopt_start = mowgli_optind - |
| 238 |
(nonopt_end - nonopt_start); |
| 239 |
nonopt_end = -1; |
| 240 |
} |
| 241 |
|
| 242 |
mowgli_optind++; |
| 243 |
|
| 244 |
/* process next argument */ |
| 245 |
goto start; |
| 246 |
} |
| 247 |
|
| 248 |
if ((nonopt_start != -1) && (nonopt_end == -1)) |
| 249 |
nonopt_end = mowgli_optind; |
| 250 |
|
| 251 |
if (place[1] && (*++place == '-')) /* found "--" */ |
| 252 |
{ |
| 253 |
place++; |
| 254 |
return -2; |
| 255 |
} |
| 256 |
} |
| 257 |
|
| 258 |
if (((optchar = (int) *place++) == (int) ':') || |
| 259 |
((oli = strchr(options + (IGNORE_FIRST ? 1 : 0), optchar)) == NULL)) |
| 260 |
{ |
| 261 |
/* option letter unknown or ':' */ |
| 262 |
if (!*place) |
| 263 |
++mowgli_optind; |
| 264 |
|
| 265 |
if (PRINT_ERROR) |
| 266 |
warnx(illoptchar, optchar); |
| 267 |
|
| 268 |
mowgli_optopt = optchar; |
| 269 |
return BADCH; |
| 270 |
} |
| 271 |
|
| 272 |
if ((optchar == 'W') && (oli[1] == ';'))/* -W long-option */ |
| 273 |
{ /* XXX: what if no long options provided (called by getopt)? */ |
| 274 |
if (*place) |
| 275 |
return -2; |
| 276 |
|
| 277 |
if (++mowgli_optind >= nargc) /* no arg */ |
| 278 |
{ |
| 279 |
place = EMSG; |
| 280 |
|
| 281 |
if (PRINT_ERROR) |
| 282 |
warnx(recargchar, optchar); |
| 283 |
|
| 284 |
mowgli_optopt = optchar; |
| 285 |
return BADARG; |
| 286 |
} |
| 287 |
else /* white space */ |
| 288 |
{ |
| 289 |
place = nargv[mowgli_optind]; |
| 290 |
} |
| 291 |
|
| 292 |
/* |
| 293 |
* Handle -W arg the same as --arg (which causes getopt to |
| 294 |
* stop parsing). |
| 295 |
*/ |
| 296 |
return -2; |
| 297 |
} |
| 298 |
|
| 299 |
if (*++oli != ':') /* doesn't take argument */ |
| 300 |
{ |
| 301 |
if (!*place) |
| 302 |
++mowgli_optind; |
| 303 |
} |
| 304 |
else /* takes (optional) argument */ |
| 305 |
{ |
| 306 |
mowgli_optarg = NULL; |
| 307 |
|
| 308 |
if (*place) /* no white space */ |
| 309 |
{ |
| 310 |
mowgli_optarg = PTR_UNCONSTIFY(place); |
| 311 |
} |
| 312 |
|
| 313 |
/* XXX: disable test for :: if PC? (GNU doesn't) */ |
| 314 |
else if (oli[1] != ':') /* arg not optional */ |
| 315 |
{ |
| 316 |
if (++mowgli_optind >= nargc) /* no arg */ |
| 317 |
{ |
| 318 |
place = EMSG; |
| 319 |
|
| 320 |
if (PRINT_ERROR) |
| 321 |
warnx(recargchar, optchar); |
| 322 |
|
| 323 |
mowgli_optopt = optchar; |
| 324 |
return BADARG; |
| 325 |
} |
| 326 |
else |
| 327 |
{ |
| 328 |
mowgli_optarg = nargv[mowgli_optind]; |
| 329 |
} |
| 330 |
} |
| 331 |
|
| 332 |
place = EMSG; |
| 333 |
++mowgli_optind; |
| 334 |
} |
| 335 |
|
| 336 |
/* dump back option letter */ |
| 337 |
return optchar; |
| 338 |
} |
| 339 |
|
| 340 |
/* |
| 341 |
* getopt -- |
| 342 |
* Parse argc/argv argument vector. |
| 343 |
* |
| 344 |
* [eventually this will replace the real getopt] |
| 345 |
*/ |
| 346 |
int mowgli_getopt(nargc, nargv, options) |
| 347 |
int nargc; |
| 348 |
|
| 349 |
char *const *nargv; |
| 350 |
const char *options; |
| 351 |
{ |
| 352 |
int retval; |
| 353 |
|
| 354 |
return_val_if_fail(nargv != NULL, -1); |
| 355 |
return_val_if_fail(options != NULL, -1); |
| 356 |
|
| 357 |
retval = getopt_internal(nargc, PTR_UNCONSTIFY(nargv), options); |
| 358 |
|
| 359 |
if (retval == -2) |
| 360 |
{ |
| 361 |
++mowgli_optind; |
| 362 |
|
| 363 |
/* |
| 364 |
* We found an option (--), so if we skipped non-options, |
| 365 |
* we have to permute. |
| 366 |
*/ |
| 367 |
if (nonopt_end != -1) |
| 368 |
{ |
| 369 |
permute_args(nonopt_start, nonopt_end, mowgli_optind, |
| 370 |
PTR_UNCONSTIFY(nargv)); |
| 371 |
mowgli_optind -= nonopt_end - nonopt_start; |
| 372 |
} |
| 373 |
|
| 374 |
nonopt_start = nonopt_end = -1; |
| 375 |
retval = -1; |
| 376 |
} |
| 377 |
|
| 378 |
return retval; |
| 379 |
} |
| 380 |
|
| 381 |
/* |
| 382 |
* getopt_long -- |
| 383 |
* Parse argc/argv argument vector. |
| 384 |
*/ |
| 385 |
int |
| 386 |
mowgli_getopt_long(int nargc, char *const *nargv, const char *options, const mowgli_getopt_option_t *long_options, int *idx) |
| 387 |
{ |
| 388 |
int retval; |
| 389 |
|
| 390 |
#define IDENTICAL_INTERPRETATION(_x, _y) \ |
| 391 |
(long_options[(_x)].has_arg == long_options[(_y)].has_arg && \ |
| 392 |
long_options[(_x)].flag == long_options[(_y)].flag && \ |
| 393 |
long_options[(_x)].val == long_options[(_y)].val) |
| 394 |
|
| 395 |
return_val_if_fail(nargv != NULL, -1); |
| 396 |
return_val_if_fail(options != NULL, -1); |
| 397 |
return_val_if_fail(long_options != NULL, -1); |
| 398 |
|
| 399 |
/* idx may be NULL */ |
| 400 |
|
| 401 |
retval = getopt_internal(nargc, PTR_UNCONSTIFY(nargv), options); |
| 402 |
|
| 403 |
if (retval == -2) |
| 404 |
{ |
| 405 |
char *current_argv, *has_equal; |
| 406 |
size_t current_argv_len; |
| 407 |
int i, ambiguous, match; |
| 408 |
|
| 409 |
current_argv = PTR_UNCONSTIFY(place); |
| 410 |
match = -1; |
| 411 |
ambiguous = 0; |
| 412 |
|
| 413 |
mowgli_optind++; |
| 414 |
place = EMSG; |
| 415 |
|
| 416 |
if (*current_argv == '\0') /* found "--" */ |
| 417 |
{ /* |
| 418 |
* We found an option (--), so if we skipped |
| 419 |
* non-options, we have to permute. |
| 420 |
*/ |
| 421 |
if (nonopt_end != -1) |
| 422 |
{ |
| 423 |
permute_args(nonopt_start, nonopt_end, |
| 424 |
mowgli_optind, PTR_UNCONSTIFY(nargv)); |
| 425 |
mowgli_optind -= nonopt_end - nonopt_start; |
| 426 |
} |
| 427 |
|
| 428 |
nonopt_start = nonopt_end = -1; |
| 429 |
return -1; |
| 430 |
} |
| 431 |
|
| 432 |
if ((has_equal = strchr(current_argv, '=')) != NULL) |
| 433 |
{ |
| 434 |
/* argument found (--option=arg) */ |
| 435 |
current_argv_len = has_equal - current_argv; |
| 436 |
has_equal++; |
| 437 |
} |
| 438 |
else |
| 439 |
{ |
| 440 |
current_argv_len = strlen(current_argv); |
| 441 |
} |
| 442 |
|
| 443 |
for (i = 0; long_options[i].name; i++) |
| 444 |
{ |
| 445 |
/* find matching long option */ |
| 446 |
if (strncmp(current_argv, long_options[i].name, |
| 447 |
current_argv_len)) |
| 448 |
continue; |
| 449 |
|
| 450 |
if (strlen(long_options[i].name) == |
| 451 |
(unsigned) current_argv_len) |
| 452 |
{ |
| 453 |
/* exact match */ |
| 454 |
match = i; |
| 455 |
ambiguous = 0; |
| 456 |
break; |
| 457 |
} |
| 458 |
|
| 459 |
if (match == -1)/* partial match */ |
| 460 |
match = i; |
| 461 |
else if (!IDENTICAL_INTERPRETATION(i, match)) |
| 462 |
ambiguous = 1; |
| 463 |
} |
| 464 |
|
| 465 |
if (ambiguous) |
| 466 |
{ |
| 467 |
/* ambiguous abbreviation */ |
| 468 |
if (PRINT_ERROR) |
| 469 |
warnx(ambig, (int) current_argv_len, |
| 470 |
current_argv); |
| 471 |
|
| 472 |
mowgli_optopt = 0; |
| 473 |
return BADCH; |
| 474 |
} |
| 475 |
|
| 476 |
if (match != -1)/* option found */ |
| 477 |
{ |
| 478 |
if ((long_options[match].has_arg == no_argument) |
| 479 |
&& has_equal) |
| 480 |
{ |
| 481 |
if (PRINT_ERROR) |
| 482 |
warnx(noarg, (int) current_argv_len, |
| 483 |
current_argv); |
| 484 |
|
| 485 |
/* |
| 486 |
* XXX: GNU sets mowgli_optopt to val regardless of |
| 487 |
* flag |
| 488 |
*/ |
| 489 |
if (long_options[match].flag == NULL) |
| 490 |
mowgli_optopt = long_options[match].val; |
| 491 |
else |
| 492 |
mowgli_optopt = 0; |
| 493 |
|
| 494 |
return BADARG; |
| 495 |
} |
| 496 |
|
| 497 |
if ((long_options[match].has_arg == required_argument) || |
| 498 |
(long_options[match].has_arg == optional_argument)) |
| 499 |
{ |
| 500 |
if (has_equal) |
| 501 |
mowgli_optarg = has_equal; |
| 502 |
else if (long_options[match].has_arg == |
| 503 |
required_argument) |
| 504 |
/* |
| 505 |
* optional argument doesn't use |
| 506 |
* next nargv |
| 507 |
*/ |
| 508 |
mowgli_optarg = nargv[mowgli_optind++]; |
| 509 |
} |
| 510 |
|
| 511 |
if ((long_options[match].has_arg == required_argument) |
| 512 |
&& (mowgli_optarg == NULL)) |
| 513 |
{ |
| 514 |
/* |
| 515 |
* Missing argument; leading ':' |
| 516 |
* indicates no error should be generated |
| 517 |
*/ |
| 518 |
if (PRINT_ERROR) |
| 519 |
warnx(recargstring, current_argv); |
| 520 |
|
| 521 |
/* |
| 522 |
* XXX: GNU sets mowgli_optopt to val regardless |
| 523 |
* of flag |
| 524 |
*/ |
| 525 |
if (long_options[match].flag == NULL) |
| 526 |
mowgli_optopt = long_options[match].val; |
| 527 |
else |
| 528 |
mowgli_optopt = 0; |
| 529 |
|
| 530 |
--mowgli_optind; |
| 531 |
return BADARG; |
| 532 |
} |
| 533 |
} |
| 534 |
else /* unknown option */ |
| 535 |
{ |
| 536 |
if (PRINT_ERROR) |
| 537 |
warnx(illoptstring, current_argv); |
| 538 |
|
| 539 |
mowgli_optopt = 0; |
| 540 |
return BADCH; |
| 541 |
} |
| 542 |
|
| 543 |
if (long_options[match].flag) |
| 544 |
{ |
| 545 |
*long_options[match].flag = long_options[match].val; |
| 546 |
retval = 0; |
| 547 |
} |
| 548 |
else |
| 549 |
{ |
| 550 |
retval = long_options[match].val; |
| 551 |
} |
| 552 |
|
| 553 |
if (idx) |
| 554 |
*idx = match; |
| 555 |
} |
| 556 |
|
| 557 |
return retval; |
| 558 |
#undef IDENTICAL_INTERPRETATION |
| 559 |
} |