| 1 | /* $OpenBSD: getopt_long.c,v 1.20 2005/10/25 15:49:37 jmc Exp $ */ |
|---|
| 2 | /* $NetBSD: getopt_long.c,v 1.15 2002/01/31 22:43:40 tv Exp $ */ |
|---|
| 3 | // Adapted for Box Backup by Chris Wilson <chris+boxbackup@qwirx.com> |
|---|
| 4 | |
|---|
| 5 | /* |
|---|
| 6 | * Copyright (c) 2002 Todd C. Miller <Todd.Miller@courtesan.com> |
|---|
| 7 | * |
|---|
| 8 | * Permission to use, copy, modify, and distribute this software for any |
|---|
| 9 | * purpose with or without fee is hereby granted, provided that the above |
|---|
| 10 | * copyright notice and this permission notice appear in all copies. |
|---|
| 11 | * |
|---|
| 12 | * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES |
|---|
| 13 | * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF |
|---|
| 14 | * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR |
|---|
| 15 | * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES |
|---|
| 16 | * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN |
|---|
| 17 | * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF |
|---|
| 18 | * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. |
|---|
| 19 | * |
|---|
| 20 | * Sponsored in part by the Defense Advanced Research Projects |
|---|
| 21 | * Agency (DARPA) and Air Force Research Laboratory, Air Force |
|---|
| 22 | * Materiel Command, USAF, under agreement number F39502-99-1-0512. |
|---|
| 23 | */ |
|---|
| 24 | /*- |
|---|
| 25 | * Copyright (c) 2000 The NetBSD Foundation, Inc. |
|---|
| 26 | * All rights reserved. |
|---|
| 27 | * |
|---|
| 28 | * This code is derived from software contributed to The NetBSD Foundation |
|---|
| 29 | * by Dieter Baron and Thomas Klausner. |
|---|
| 30 | * |
|---|
| 31 | * Redistribution and use in source and binary forms, with or without |
|---|
| 32 | * modification, are permitted provided that the following conditions |
|---|
| 33 | * are met: |
|---|
| 34 | * 1. Redistributions of source code must retain the above copyright |
|---|
| 35 | * notice, this list of conditions and the following disclaimer. |
|---|
| 36 | * 2. Redistributions in binary form must reproduce the above copyright |
|---|
| 37 | * notice, this list of conditions and the following disclaimer in the |
|---|
| 38 | * documentation and/or other materials provided with the distribution. |
|---|
| 39 | * 3. All advertising materials mentioning features or use of this software |
|---|
| 40 | * must display the following acknowledgement: |
|---|
| 41 | * This product includes software developed by the NetBSD |
|---|
| 42 | * Foundation, Inc. and its contributors. |
|---|
| 43 | * 4. Neither the name of The NetBSD Foundation nor the names of its |
|---|
| 44 | * contributors may be used to endorse or promote products derived |
|---|
| 45 | * from this software without specific prior written permission. |
|---|
| 46 | * |
|---|
| 47 | * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS |
|---|
| 48 | * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED |
|---|
| 49 | * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR |
|---|
| 50 | * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS |
|---|
| 51 | * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
|---|
| 52 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
|---|
| 53 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
|---|
| 54 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
|---|
| 55 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
|---|
| 56 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
|---|
| 57 | * POSSIBILITY OF SUCH DAMAGE. |
|---|
| 58 | */ |
|---|
| 59 | |
|---|
| 60 | // #include "Box.h" |
|---|
| 61 | #include "emu.h" |
|---|
| 62 | |
|---|
| 63 | #include <errno.h> |
|---|
| 64 | #include <stdarg.h> |
|---|
| 65 | #include <stdlib.h> |
|---|
| 66 | #include <stdio.h> |
|---|
| 67 | #include <string.h> |
|---|
| 68 | |
|---|
| 69 | #include "getopt.h" |
|---|
| 70 | |
|---|
| 71 | #if defined _MSC_VER || defined __MINGW32__ |
|---|
| 72 | #define REPLACE_GETOPT /* use this getopt as the system getopt(3) */ |
|---|
| 73 | |
|---|
| 74 | #ifdef REPLACE_GETOPT |
|---|
| 75 | int opterr = 1; /* if error message should be printed */ |
|---|
| 76 | int optind = 1; /* index into parent argv vector */ |
|---|
| 77 | int optopt = '?'; /* character checked for validity */ |
|---|
| 78 | int optreset; /* reset getopt */ |
|---|
| 79 | char *optarg; /* argument associated with option */ |
|---|
| 80 | #endif |
|---|
| 81 | |
|---|
| 82 | #define PRINT_ERROR ((opterr) && (*options != ':')) |
|---|
| 83 | |
|---|
| 84 | #define FLAG_PERMUTE 0x01 /* permute non-options to the end of argv */ |
|---|
| 85 | #define FLAG_ALLARGS 0x02 /* treat non-options as args to option "-1" */ |
|---|
| 86 | #define FLAG_LONGONLY 0x04 /* operate as getopt_long_only */ |
|---|
| 87 | |
|---|
| 88 | /* return values */ |
|---|
| 89 | #define BADCH (int)'?' |
|---|
| 90 | #define BADARG ((*options == ':') ? (int)':' : (int)'?') |
|---|
| 91 | #define INORDER (int)1 |
|---|
| 92 | |
|---|
| 93 | #define EMSG "" |
|---|
| 94 | |
|---|
| 95 | static void warnx(const char* fmt, ...) |
|---|
| 96 | { |
|---|
| 97 | va_list ap; |
|---|
| 98 | va_start(ap, fmt); |
|---|
| 99 | vfprintf(stderr, fmt, ap); |
|---|
| 100 | va_end(ap); |
|---|
| 101 | fprintf(stderr, "\n"); |
|---|
| 102 | } |
|---|
| 103 | |
|---|
| 104 | static int getopt_internal(int, char * const *, const char *, |
|---|
| 105 | const struct option *, int *, int); |
|---|
| 106 | static int parse_long_options(char * const *, const char *, |
|---|
| 107 | const struct option *, int *, int); |
|---|
| 108 | static int gcd(int, int); |
|---|
| 109 | static void permute_args(int, int, int, char * const *); |
|---|
| 110 | |
|---|
| 111 | static char *place = EMSG; /* option letter processing */ |
|---|
| 112 | |
|---|
| 113 | /* XXX: set optreset to 1 rather than these two */ |
|---|
| 114 | static int nonopt_start = -1; /* first non option argument (for permute) */ |
|---|
| 115 | static int nonopt_end = -1; /* first option after non options (for permute) */ |
|---|
| 116 | |
|---|
| 117 | /* Error messages */ |
|---|
| 118 | static const char recargchar[] = "option requires an argument -- %c"; |
|---|
| 119 | static const char recargstring[] = "option requires an argument -- %s"; |
|---|
| 120 | static const char ambig[] = "ambiguous option -- %.*s"; |
|---|
| 121 | static const char noarg[] = "option doesn't take an argument -- %.*s"; |
|---|
| 122 | static const char illoptchar[] = "unknown option -- %c"; |
|---|
| 123 | static const char illoptstring[] = "unknown option -- %s"; |
|---|
| 124 | |
|---|
| 125 | /* |
|---|
| 126 | * Compute the greatest common divisor of a and b. |
|---|
| 127 | */ |
|---|
| 128 | static int |
|---|
| 129 | gcd(int a, int b) |
|---|
| 130 | { |
|---|
| 131 | int c; |
|---|
| 132 | |
|---|
| 133 | c = a % b; |
|---|
| 134 | while (c != 0) { |
|---|
| 135 | a = b; |
|---|
| 136 | b = c; |
|---|
| 137 | c = a % b; |
|---|
| 138 | } |
|---|
| 139 | |
|---|
| 140 | return (b); |
|---|
| 141 | } |
|---|
| 142 | |
|---|
| 143 | /* |
|---|
| 144 | * Exchange the block from nonopt_start to nonopt_end with the block |
|---|
| 145 | * from nonopt_end to opt_end (keeping the same order of arguments |
|---|
| 146 | * in each block). |
|---|
| 147 | */ |
|---|
| 148 | static void |
|---|
| 149 | permute_args(int panonopt_start, int panonopt_end, int opt_end, |
|---|
| 150 | char * const *nargv) |
|---|
| 151 | { |
|---|
| 152 | int cstart, cyclelen, i, j, ncycle, nnonopts, nopts, pos; |
|---|
| 153 | char *swap; |
|---|
| 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 | * parse_long_options -- |
|---|
| 182 | * Parse long options in argc/argv argument vector. |
|---|
| 183 | * Returns -1 if short_too is set and the option does not match long_options. |
|---|
| 184 | */ |
|---|
| 185 | static int |
|---|
| 186 | parse_long_options(char * const *nargv, const char *options, |
|---|
| 187 | const struct option *long_options, int *idx, int short_too) |
|---|
| 188 | { |
|---|
| 189 | char *current_argv, *has_equal; |
|---|
| 190 | size_t current_argv_len; |
|---|
| 191 | int i, match; |
|---|
| 192 | |
|---|
| 193 | current_argv = place; |
|---|
| 194 | match = -1; |
|---|
| 195 | |
|---|
| 196 | optind++; |
|---|
| 197 | |
|---|
| 198 | if ((has_equal = strchr(current_argv, '=')) != NULL) { |
|---|
| 199 | /* argument found (--option=arg) */ |
|---|
| 200 | current_argv_len = has_equal - current_argv; |
|---|
| 201 | has_equal++; |
|---|
| 202 | } else |
|---|
| 203 | current_argv_len = strlen(current_argv); |
|---|
| 204 | |
|---|
| 205 | for (i = 0; long_options[i].name; i++) { |
|---|
| 206 | /* find matching long option */ |
|---|
| 207 | if (strncmp(current_argv, long_options[i].name, |
|---|
| 208 | current_argv_len)) |
|---|
| 209 | continue; |
|---|
| 210 | |
|---|
| 211 | if (strlen(long_options[i].name) == current_argv_len) { |
|---|
| 212 | /* exact match */ |
|---|
| 213 | match = i; |
|---|
| 214 | break; |
|---|
| 215 | } |
|---|
| 216 | /* |
|---|
| 217 | * If this is a known short option, don't allow |
|---|
| 218 | * a partial match of a single character. |
|---|
| 219 | */ |
|---|
| 220 | if (short_too && current_argv_len == 1) |
|---|
| 221 | continue; |
|---|
| 222 | |
|---|
| 223 | if (match == -1) /* partial match */ |
|---|
| 224 | match = i; |
|---|
| 225 | else { |
|---|
| 226 | /* ambiguous abbreviation */ |
|---|
| 227 | if (PRINT_ERROR) |
|---|
| 228 | warnx(ambig, (int)current_argv_len, |
|---|
| 229 | current_argv); |
|---|
| 230 | optopt = 0; |
|---|
| 231 | return (BADCH); |
|---|
| 232 | } |
|---|
| 233 | } |
|---|
| 234 | if (match != -1) { /* option found */ |
|---|
| 235 | if (long_options[match].has_arg == no_argument |
|---|
| 236 | && has_equal) { |
|---|
| 237 | if (PRINT_ERROR) |
|---|
| 238 | warnx(noarg, (int)current_argv_len, |
|---|
| 239 | current_argv); |
|---|
| 240 | /* |
|---|
| 241 | * XXX: GNU sets optopt to val regardless of flag |
|---|
| 242 | */ |
|---|
| 243 | if (long_options[match].flag == NULL) |
|---|
| 244 | optopt = long_options[match].val; |
|---|
| 245 | else |
|---|
| 246 | optopt = 0; |
|---|
| 247 | return (BADARG); |
|---|
| 248 | } |
|---|
| 249 | if (long_options[match].has_arg == required_argument || |
|---|
| 250 | long_options[match].has_arg == optional_argument) { |
|---|
| 251 | if (has_equal) |
|---|
| 252 | optarg = has_equal; |
|---|
| 253 | else if (long_options[match].has_arg == |
|---|
| 254 | required_argument) { |
|---|
| 255 | /* |
|---|
| 256 | * optional argument doesn't use next nargv |
|---|
| 257 | */ |
|---|
| 258 | optarg = nargv[optind++]; |
|---|
| 259 | } |
|---|
| 260 | } |
|---|
| 261 | if ((long_options[match].has_arg == required_argument) |
|---|
| 262 | && (optarg == NULL)) { |
|---|
| 263 | /* |
|---|
| 264 | * Missing argument; leading ':' indicates no error |
|---|
| 265 | * should be generated. |
|---|
| 266 | */ |
|---|
| 267 | if (PRINT_ERROR) |
|---|
| 268 | warnx(recargstring, |
|---|
| 269 | current_argv); |
|---|
| 270 | /* |
|---|
| 271 | * XXX: GNU sets optopt to val regardless of flag |
|---|
| 272 | */ |
|---|
| 273 | if (long_options[match].flag == NULL) |
|---|
| 274 | optopt = long_options[match].val; |
|---|
| 275 | else |
|---|
| 276 | optopt = 0; |
|---|
| 277 | --optind; |
|---|
| 278 | return (BADARG); |
|---|
| 279 | } |
|---|
| 280 | } else { /* unknown option */ |
|---|
| 281 | if (short_too) { |
|---|
| 282 | --optind; |
|---|
| 283 | return (-1); |
|---|
| 284 | } |
|---|
| 285 | if (PRINT_ERROR) |
|---|
| 286 | warnx(illoptstring, current_argv); |
|---|
| 287 | optopt = 0; |
|---|
| 288 | return (BADCH); |
|---|
| 289 | } |
|---|
| 290 | if (idx) |
|---|
| 291 | *idx = match; |
|---|
| 292 | if (long_options[match].flag) { |
|---|
| 293 | *long_options[match].flag = long_options[match].val; |
|---|
| 294 | return (0); |
|---|
| 295 | } else |
|---|
| 296 | return (long_options[match].val); |
|---|
| 297 | } |
|---|
| 298 | |
|---|
| 299 | /* |
|---|
| 300 | * getopt_internal -- |
|---|
| 301 | * Parse argc/argv argument vector. Called by user level routines. |
|---|
| 302 | */ |
|---|
| 303 | static int |
|---|
| 304 | getopt_internal(int nargc, char * const *nargv, const char *options, |
|---|
| 305 | const struct option *long_options, int *idx, int flags) |
|---|
| 306 | { |
|---|
| 307 | const char * oli; /* option letter list index */ |
|---|
| 308 | int optchar, short_too; |
|---|
| 309 | static int posixly_correct = -1; |
|---|
| 310 | |
|---|
| 311 | if (options == NULL) |
|---|
| 312 | return (-1); |
|---|
| 313 | |
|---|
| 314 | /* |
|---|
| 315 | * Disable GNU extensions if POSIXLY_CORRECT is set or options |
|---|
| 316 | * string begins with a '+'. |
|---|
| 317 | */ |
|---|
| 318 | if (posixly_correct == -1) |
|---|
| 319 | posixly_correct = (getenv("POSIXLY_CORRECT") != NULL); |
|---|
| 320 | if (posixly_correct || *options == '+') |
|---|
| 321 | flags &= ~FLAG_PERMUTE; |
|---|
| 322 | else if (*options == '-') |
|---|
| 323 | flags |= FLAG_ALLARGS; |
|---|
| 324 | if (*options == '+' || *options == '-') |
|---|
| 325 | options++; |
|---|
| 326 | |
|---|
| 327 | /* |
|---|
| 328 | * XXX Some GNU programs (like cvs) set optind to 0 instead of |
|---|
| 329 | * XXX using optreset. Work around this braindamage. |
|---|
| 330 | */ |
|---|
| 331 | if (optind == 0) |
|---|
| 332 | optind = optreset = 1; |
|---|
| 333 | |
|---|
| 334 | optarg = NULL; |
|---|
| 335 | if (optreset) |
|---|
| 336 | nonopt_start = nonopt_end = -1; |
|---|
| 337 | start: |
|---|
| 338 | if (optreset || !*place) { /* update scanning pointer */ |
|---|
| 339 | optreset = 0; |
|---|
| 340 | if (optind >= nargc) { /* end of argument vector */ |
|---|
| 341 | place = EMSG; |
|---|
| 342 | if (nonopt_end != -1) { |
|---|
| 343 | /* do permutation, if we have to */ |
|---|
| 344 | permute_args(nonopt_start, nonopt_end, |
|---|
| 345 | optind, nargv); |
|---|
| 346 | optind -= nonopt_end - nonopt_start; |
|---|
| 347 | } |
|---|
| 348 | else if (nonopt_start != -1) { |
|---|
| 349 | /* |
|---|
| 350 | * If we skipped non-options, set optind |
|---|
| 351 | * to the first of them. |
|---|
| 352 | */ |
|---|
| 353 | optind = nonopt_start; |
|---|
| 354 | } |
|---|
| 355 | nonopt_start = nonopt_end = -1; |
|---|
| 356 | return (-1); |
|---|
| 357 | } |
|---|
| 358 | if (*(place = nargv[optind]) != '-' || |
|---|
| 359 | (place[1] == '\0' && strchr(options, '-') == NULL)) { |
|---|
| 360 | place = EMSG; /* found non-option */ |
|---|
| 361 | if (flags & FLAG_ALLARGS) { |
|---|
| 362 | /* |
|---|
| 363 | * GNU extension: |
|---|
| 364 | * return non-option as argument to option 1 |
|---|
| 365 | */ |
|---|
| 366 | optarg = nargv[optind++]; |
|---|
| 367 | return (INORDER); |
|---|
| 368 | } |
|---|
| 369 | if (!(flags & FLAG_PERMUTE)) { |
|---|
| 370 | /* |
|---|
| 371 | * If no permutation wanted, stop parsing |
|---|
| 372 | * at first non-option. |
|---|
| 373 | */ |
|---|
| 374 | return (-1); |
|---|
| 375 | } |
|---|
| 376 | /* do permutation */ |
|---|
| 377 | if (nonopt_start == -1) |
|---|
| 378 | nonopt_start = optind; |
|---|
| 379 | else if (nonopt_end != -1) { |
|---|
| 380 | permute_args(nonopt_start, nonopt_end, |
|---|
| 381 | optind, nargv); |
|---|
| 382 | nonopt_start = optind - |
|---|
| 383 | (nonopt_end - nonopt_start); |
|---|
| 384 | nonopt_end = -1; |
|---|
| 385 | } |
|---|
| 386 | optind++; |
|---|
| 387 | /* process next argument */ |
|---|
| 388 | goto start; |
|---|
| 389 | } |
|---|
| 390 | if (nonopt_start != -1 && nonopt_end == -1) |
|---|
| 391 | nonopt_end = optind; |
|---|
| 392 | |
|---|
| 393 | /* |
|---|
| 394 | * If we have "-" do nothing, if "--" we are done. |
|---|
| 395 | */ |
|---|
| 396 | if (place[1] != '\0' && *++place == '-' && place[1] == '\0') { |
|---|
| 397 | optind++; |
|---|
| 398 | place = EMSG; |
|---|
| 399 | /* |
|---|
| 400 | * We found an option (--), so if we skipped |
|---|
| 401 | * non-options, we have to permute. |
|---|
| 402 | */ |
|---|
| 403 | if (nonopt_end != -1) { |
|---|
| 404 | permute_args(nonopt_start, nonopt_end, |
|---|
| 405 | optind, nargv); |
|---|
| 406 | optind -= nonopt_end - nonopt_start; |
|---|
| 407 | } |
|---|
| 408 | nonopt_start = nonopt_end = -1; |
|---|
| 409 | return (-1); |
|---|
| 410 | } |
|---|
| 411 | } |
|---|
| 412 | |
|---|
| 413 | /* |
|---|
| 414 | * Check long options if: |
|---|
| 415 | * 1) we were passed some |
|---|
| 416 | * 2) the arg is not just "-" |
|---|
| 417 | * 3) either the arg starts with -- we are getopt_long_only() |
|---|
| 418 | */ |
|---|
| 419 | if (long_options != NULL && place != nargv[optind] && |
|---|
| 420 | (*place == '-' || (flags & FLAG_LONGONLY))) { |
|---|
| 421 | short_too = 0; |
|---|
| 422 | if (*place == '-') |
|---|
| 423 | place++; /* --foo long option */ |
|---|
| 424 | else if (*place != ':' && strchr(options, *place) != NULL) |
|---|
| 425 | short_too = 1; /* could be short option too */ |
|---|
| 426 | |
|---|
| 427 | optchar = parse_long_options(nargv, options, long_options, |
|---|
| 428 | idx, short_too); |
|---|
| 429 | if (optchar != -1) { |
|---|
| 430 | place = EMSG; |
|---|
| 431 | return (optchar); |
|---|
| 432 | } |
|---|
| 433 | } |
|---|
| 434 | |
|---|
| 435 | if ((optchar = (int)*place++) == (int)':' || |
|---|
| 436 | optchar == (int)'-' && *place != '\0' || |
|---|
| 437 | (oli = strchr(options, optchar)) == NULL) { |
|---|
| 438 | /* |
|---|
| 439 | * If the user specified "-" and '-' isn't listed in |
|---|
| 440 | * options, return -1 (non-option) as per POSIX. |
|---|
| 441 | * Otherwise, it is an unknown option character (or ':'). |
|---|
| 442 | */ |
|---|
| 443 | if (optchar == (int)'-' && *place == '\0') |
|---|
| 444 | return (-1); |
|---|
| 445 | if (!*place) |
|---|
| 446 | ++optind; |
|---|
| 447 | if (PRINT_ERROR) |
|---|
| 448 | warnx(illoptchar, optchar); |
|---|
| 449 | optopt = optchar; |
|---|
| 450 | return (BADCH); |
|---|
| 451 | } |
|---|
| 452 | if (long_options != NULL && optchar == 'W' && oli[1] == ';') { |
|---|
| 453 | /* -W long-option */ |
|---|
| 454 | if (*place) /* no space */ |
|---|
| 455 | /* NOTHING */; |
|---|
| 456 | else if (++optind >= nargc) { /* no arg */ |
|---|
| 457 | place = EMSG; |
|---|
| 458 | if (PRINT_ERROR) |
|---|
| 459 | warnx(recargchar, optchar); |
|---|
| 460 | optopt = optchar; |
|---|
| 461 | return (BADARG); |
|---|
| 462 | } else /* white space */ |
|---|
| 463 | place = nargv[optind]; |
|---|
| 464 | optchar = parse_long_options(nargv, options, long_options, |
|---|
| 465 | idx, 0); |
|---|
| 466 | place = EMSG; |
|---|
| 467 | return (optchar); |
|---|
| 468 | } |
|---|
| 469 | if (*++oli != ':') { /* doesn't take argument */ |
|---|
| 470 | if (!*place) |
|---|
| 471 | ++optind; |
|---|
| 472 | } else { /* takes (optional) argument */ |
|---|
| 473 | optarg = NULL; |
|---|
| 474 | if (*place) /* no white space */ |
|---|
| 475 | optarg = place; |
|---|
| 476 | /* XXX: disable test for :: if PC? (GNU doesn't) */ |
|---|
| 477 | else if (oli[1] != ':') { /* arg not optional */ |
|---|
| 478 | if (++optind >= nargc) { /* no arg */ |
|---|
| 479 | place = EMSG; |
|---|
| 480 | if (PRINT_ERROR) |
|---|
| 481 | warnx(recargchar, optchar); |
|---|
| 482 | optopt = optchar; |
|---|
| 483 | return (BADARG); |
|---|
| 484 | } else |
|---|
| 485 | optarg = nargv[optind]; |
|---|
| 486 | } else if (!(flags & FLAG_PERMUTE)) { |
|---|
| 487 | /* |
|---|
| 488 | * If permutation is disabled, we can accept an |
|---|
| 489 | * optional arg separated by whitespace so long |
|---|
| 490 | * as it does not start with a dash (-). |
|---|
| 491 | */ |
|---|
| 492 | if (optind + 1 < nargc && *nargv[optind + 1] != '-') |
|---|
| 493 | optarg = nargv[++optind]; |
|---|
| 494 | } |
|---|
| 495 | place = EMSG; |
|---|
| 496 | ++optind; |
|---|
| 497 | } |
|---|
| 498 | /* dump back option letter */ |
|---|
| 499 | return (optchar); |
|---|
| 500 | } |
|---|
| 501 | |
|---|
| 502 | #ifdef REPLACE_GETOPT |
|---|
| 503 | /* |
|---|
| 504 | * getopt -- |
|---|
| 505 | * Parse argc/argv argument vector. |
|---|
| 506 | * |
|---|
| 507 | * [eventually this will replace the BSD getopt] |
|---|
| 508 | */ |
|---|
| 509 | int |
|---|
| 510 | getopt(int nargc, char * const *nargv, const char *options) |
|---|
| 511 | { |
|---|
| 512 | |
|---|
| 513 | /* |
|---|
| 514 | * We don't pass FLAG_PERMUTE to getopt_internal() since |
|---|
| 515 | * the BSD getopt(3) (unlike GNU) has never done this. |
|---|
| 516 | * |
|---|
| 517 | * Furthermore, since many privileged programs call getopt() |
|---|
| 518 | * before dropping privileges it makes sense to keep things |
|---|
| 519 | * as simple (and bug-free) as possible. |
|---|
| 520 | */ |
|---|
| 521 | return (getopt_internal(nargc, nargv, options, NULL, NULL, 0)); |
|---|
| 522 | } |
|---|
| 523 | #endif /* REPLACE_GETOPT */ |
|---|
| 524 | |
|---|
| 525 | /* |
|---|
| 526 | * getopt_long -- |
|---|
| 527 | * Parse argc/argv argument vector. |
|---|
| 528 | */ |
|---|
| 529 | int |
|---|
| 530 | getopt_long(int nargc, char * const *nargv, const char *options, |
|---|
| 531 | const struct option *long_options, int *idx) |
|---|
| 532 | { |
|---|
| 533 | |
|---|
| 534 | return (getopt_internal(nargc, nargv, options, long_options, idx, |
|---|
| 535 | FLAG_PERMUTE)); |
|---|
| 536 | } |
|---|
| 537 | |
|---|
| 538 | /* |
|---|
| 539 | * getopt_long_only -- |
|---|
| 540 | * Parse argc/argv argument vector. |
|---|
| 541 | */ |
|---|
| 542 | int |
|---|
| 543 | getopt_long_only(int nargc, char * const *nargv, const char *options, |
|---|
| 544 | const struct option *long_options, int *idx) |
|---|
| 545 | { |
|---|
| 546 | |
|---|
| 547 | return (getopt_internal(nargc, nargv, options, long_options, idx, |
|---|
| 548 | FLAG_PERMUTE|FLAG_LONGONLY)); |
|---|
| 549 | } |
|---|
| 550 | |
|---|
| 551 | #endif // defined _MSC_VER || defined __MINGW32__ |
|---|