| 1 | /*      $NetBSD: options.c,v 1.38 2005/03/20 21:38:17 dsl Exp $ */ | 
|---|
| 2 |  | 
|---|
| 3 | /*- | 
|---|
| 4 | * Copyright (c) 1991, 1993 | 
|---|
| 5 | *      The Regents of the University of California.  All rights reserved. | 
|---|
| 6 | * | 
|---|
| 7 | * This code is derived from software contributed to Berkeley by | 
|---|
| 8 | * Kenneth Almquist. | 
|---|
| 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. Neither the name of the University nor the names of its contributors | 
|---|
| 19 | *    may be used to endorse or promote products derived from this software | 
|---|
| 20 | *    without specific prior written permission. | 
|---|
| 21 | * | 
|---|
| 22 | * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND | 
|---|
| 23 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | 
|---|
| 24 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | 
|---|
| 25 | * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE | 
|---|
| 26 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | 
|---|
| 27 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | 
|---|
| 28 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | 
|---|
| 29 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | 
|---|
| 30 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | 
|---|
| 31 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | 
|---|
| 32 | * SUCH DAMAGE. | 
|---|
| 33 | */ | 
|---|
| 34 |  | 
|---|
| 35 | #include <sys/cdefs.h> | 
|---|
| 36 | #ifndef lint | 
|---|
| 37 | #if 0 | 
|---|
| 38 | static char sccsid[] = "@(#)options.c   8.2 (Berkeley) 5/4/95"; | 
|---|
| 39 | #else | 
|---|
| 40 | __RCSID("$NetBSD: options.c,v 1.38 2005/03/20 21:38:17 dsl Exp $"); | 
|---|
| 41 | #endif | 
|---|
| 42 | #endif /* not lint */ | 
|---|
| 43 |  | 
|---|
| 44 | #include <signal.h> | 
|---|
| 45 | #include <unistd.h> | 
|---|
| 46 | #include <stdlib.h> | 
|---|
| 47 |  | 
|---|
| 48 | #include "shell.h" | 
|---|
| 49 | #define DEFINE_OPTIONS | 
|---|
| 50 | #include "options.h" | 
|---|
| 51 | #undef DEFINE_OPTIONS | 
|---|
| 52 | #include "nodes.h"      /* for other header files */ | 
|---|
| 53 | #include "eval.h" | 
|---|
| 54 | #include "jobs.h" | 
|---|
| 55 | #include "input.h" | 
|---|
| 56 | #include "output.h" | 
|---|
| 57 | #include "trap.h" | 
|---|
| 58 | #include "var.h" | 
|---|
| 59 | #include "memalloc.h" | 
|---|
| 60 | #include "error.h" | 
|---|
| 61 | #include "mystring.h" | 
|---|
| 62 | #ifndef SMALL | 
|---|
| 63 | #include "myhistedit.h" | 
|---|
| 64 | #endif | 
|---|
| 65 | #include "show.h" | 
|---|
| 66 |  | 
|---|
| 67 | char *arg0;                     /* value of $0 */ | 
|---|
| 68 | struct shparam shellparam;      /* current positional parameters */ | 
|---|
| 69 | char **argptr;                  /* argument list for builtin commands */ | 
|---|
| 70 | char *optionarg;                /* set by nextopt (like getopt) */ | 
|---|
| 71 | char *optptr;                   /* used by nextopt */ | 
|---|
| 72 |  | 
|---|
| 73 | char *minusc;                   /* argument to -c option */ | 
|---|
| 74 |  | 
|---|
| 75 |  | 
|---|
| 76 | STATIC void options(int); | 
|---|
| 77 | STATIC void minus_o(char *, int); | 
|---|
| 78 | STATIC void setoption(int, int); | 
|---|
| 79 | STATIC int getopts(char *, char *, char **, char ***, char **); | 
|---|
| 80 |  | 
|---|
| 81 |  | 
|---|
| 82 | /* | 
|---|
| 83 | * Process the shell command line arguments. | 
|---|
| 84 | */ | 
|---|
| 85 |  | 
|---|
| 86 | void | 
|---|
| 87 | procargs(int argc, char **argv) | 
|---|
| 88 | { | 
|---|
| 89 | int i; | 
|---|
| 90 |  | 
|---|
| 91 | argptr = argv; | 
|---|
| 92 | if (argc > 0) | 
|---|
| 93 | argptr++; | 
|---|
| 94 | for (i = 0; i < NOPTS; i++) | 
|---|
| 95 | optlist[i].val = 2; | 
|---|
| 96 | options(1); | 
|---|
| 97 | if (*argptr == NULL && minusc == NULL) | 
|---|
| 98 | sflag = 1; | 
|---|
| 99 | if (iflag == 2 && sflag == 1 && isatty(0) && isatty(1)) | 
|---|
| 100 | iflag = 1; | 
|---|
| 101 | if (mflag == 2) | 
|---|
| 102 | mflag = iflag; | 
|---|
| 103 | for (i = 0; i < NOPTS; i++) | 
|---|
| 104 | if (optlist[i].val == 2) | 
|---|
| 105 | optlist[i].val = 0; | 
|---|
| 106 | #if DEBUG == 2 | 
|---|
| 107 | debug = 1; | 
|---|
| 108 | #endif | 
|---|
| 109 | arg0 = argv[0]; | 
|---|
| 110 | if (sflag == 0 && minusc == NULL) { | 
|---|
| 111 | commandname = argv[0]; | 
|---|
| 112 | arg0 = *argptr++; | 
|---|
| 113 | setinputfile(arg0, 0); | 
|---|
| 114 | commandname = arg0; | 
|---|
| 115 | } | 
|---|
| 116 | /* POSIX 1003.2: first arg after -c cmd is $0, remainder $1... */ | 
|---|
| 117 | if (minusc != NULL) { | 
|---|
| 118 | if (argptr == NULL || *argptr == NULL) | 
|---|
| 119 | error("Bad -c option"); | 
|---|
| 120 | minusc = *argptr++; | 
|---|
| 121 | if (*argptr != 0) | 
|---|
| 122 | arg0 = *argptr++; | 
|---|
| 123 | } | 
|---|
| 124 |  | 
|---|
| 125 | shellparam.p = argptr; | 
|---|
| 126 | shellparam.reset = 1; | 
|---|
| 127 | /* assert(shellparam.malloc == 0 && shellparam.nparam == 0); */ | 
|---|
| 128 | while (*argptr) { | 
|---|
| 129 | shellparam.nparam++; | 
|---|
| 130 | argptr++; | 
|---|
| 131 | } | 
|---|
| 132 | optschanged(); | 
|---|
| 133 | } | 
|---|
| 134 |  | 
|---|
| 135 |  | 
|---|
| 136 | void | 
|---|
| 137 | optschanged(void) | 
|---|
| 138 | { | 
|---|
| 139 | setinteractive(iflag); | 
|---|
| 140 | #ifndef SMALL | 
|---|
| 141 | histedit(); | 
|---|
| 142 | #endif | 
|---|
| 143 | setjobctl(mflag); | 
|---|
| 144 | } | 
|---|
| 145 |  | 
|---|
| 146 | /* | 
|---|
| 147 | * Process shell options.  The global variable argptr contains a pointer | 
|---|
| 148 | * to the argument list; we advance it past the options. | 
|---|
| 149 | */ | 
|---|
| 150 |  | 
|---|
| 151 | STATIC void | 
|---|
| 152 | options(int cmdline) | 
|---|
| 153 | { | 
|---|
| 154 | static char empty[] = ""; | 
|---|
| 155 | char *p; | 
|---|
| 156 | int val; | 
|---|
| 157 | int c; | 
|---|
| 158 |  | 
|---|
| 159 | if (cmdline) | 
|---|
| 160 | minusc = NULL; | 
|---|
| 161 | while ((p = *argptr) != NULL) { | 
|---|
| 162 | argptr++; | 
|---|
| 163 | if ((c = *p++) == '-') { | 
|---|
| 164 | val = 1; | 
|---|
| 165 | if (p[0] == '\0' || (p[0] == '-' && p[1] == '\0')) { | 
|---|
| 166 | if (!cmdline) { | 
|---|
| 167 | /* "-" means turn off -x and -v */ | 
|---|
| 168 | if (p[0] == '\0') | 
|---|
| 169 | xflag = vflag = 0; | 
|---|
| 170 | /* "--" means reset params */ | 
|---|
| 171 | else if (*argptr == NULL) | 
|---|
| 172 | setparam(argptr); | 
|---|
| 173 | } | 
|---|
| 174 | break;    /* "-" or  "--" terminates options */ | 
|---|
| 175 | } | 
|---|
| 176 | } else if (c == '+') { | 
|---|
| 177 | val = 0; | 
|---|
| 178 | } else { | 
|---|
| 179 | argptr--; | 
|---|
| 180 | break; | 
|---|
| 181 | } | 
|---|
| 182 | while ((c = *p++) != '\0') { | 
|---|
| 183 | if (c == 'c' && cmdline) { | 
|---|
| 184 | /* command is after shell args*/ | 
|---|
| 185 | minusc = empty; | 
|---|
| 186 | } else if (c == 'o') { | 
|---|
| 187 | minus_o(*argptr, val); | 
|---|
| 188 | if (*argptr) | 
|---|
| 189 | argptr++; | 
|---|
| 190 | } else { | 
|---|
| 191 | setoption(c, val); | 
|---|
| 192 | } | 
|---|
| 193 | } | 
|---|
| 194 | } | 
|---|
| 195 | } | 
|---|
| 196 |  | 
|---|
| 197 | static void | 
|---|
| 198 | set_opt_val(int i, int val) | 
|---|
| 199 | { | 
|---|
| 200 | int j; | 
|---|
| 201 | int flag; | 
|---|
| 202 |  | 
|---|
| 203 | if (val && (flag = optlist[i].opt_set)) { | 
|---|
| 204 | /* some options (eg vi/emacs) are mutually exclusive */ | 
|---|
| 205 | for (j = 0; j < NOPTS; j++) | 
|---|
| 206 | if (optlist[j].opt_set == flag) | 
|---|
| 207 | optlist[j].val = 0; | 
|---|
| 208 | } | 
|---|
| 209 | optlist[i].val = val; | 
|---|
| 210 | #ifdef DEBUG | 
|---|
| 211 | if (&optlist[i].val == &debug) | 
|---|
| 212 | opentrace(); | 
|---|
| 213 | #endif | 
|---|
| 214 | } | 
|---|
| 215 |  | 
|---|
| 216 | STATIC void | 
|---|
| 217 | minus_o(char *name, int val) | 
|---|
| 218 | { | 
|---|
| 219 | int i; | 
|---|
| 220 |  | 
|---|
| 221 | if (name == NULL) { | 
|---|
| 222 | out1str("Current option settings\n"); | 
|---|
| 223 | for (i = 0; i < NOPTS; i++) | 
|---|
| 224 | out1fmt("%-16s%s\n", optlist[i].name, | 
|---|
| 225 | optlist[i].val ? "on" : "off"); | 
|---|
| 226 | } else { | 
|---|
| 227 | for (i = 0; i < NOPTS; i++) | 
|---|
| 228 | if (equal(name, optlist[i].name)) { | 
|---|
| 229 | set_opt_val(i, val); | 
|---|
| 230 | return; | 
|---|
| 231 | } | 
|---|
| 232 | error("Illegal option -o %s", name); | 
|---|
| 233 | } | 
|---|
| 234 | } | 
|---|
| 235 |  | 
|---|
| 236 |  | 
|---|
| 237 | STATIC void | 
|---|
| 238 | setoption(int flag, int val) | 
|---|
| 239 | { | 
|---|
| 240 | int i; | 
|---|
| 241 |  | 
|---|
| 242 | for (i = 0; i < NOPTS; i++) | 
|---|
| 243 | if (optlist[i].letter == flag) { | 
|---|
| 244 | set_opt_val( i, val ); | 
|---|
| 245 | return; | 
|---|
| 246 | } | 
|---|
| 247 | error("Illegal option -%c", flag); | 
|---|
| 248 | /* NOTREACHED */ | 
|---|
| 249 | } | 
|---|
| 250 |  | 
|---|
| 251 |  | 
|---|
| 252 |  | 
|---|
| 253 | #ifdef mkinit | 
|---|
| 254 | INCLUDE "options.h" | 
|---|
| 255 |  | 
|---|
| 256 | SHELLPROC { | 
|---|
| 257 | int i; | 
|---|
| 258 |  | 
|---|
| 259 | for (i = 0; optlist[i].name; i++) | 
|---|
| 260 | optlist[i].val = 0; | 
|---|
| 261 | optschanged(); | 
|---|
| 262 |  | 
|---|
| 263 | } | 
|---|
| 264 | #endif | 
|---|
| 265 |  | 
|---|
| 266 |  | 
|---|
| 267 | /* | 
|---|
| 268 | * Set the shell parameters. | 
|---|
| 269 | */ | 
|---|
| 270 |  | 
|---|
| 271 | void | 
|---|
| 272 | setparam(char **argv) | 
|---|
| 273 | { | 
|---|
| 274 | char **newparam; | 
|---|
| 275 | char **ap; | 
|---|
| 276 | int nparam; | 
|---|
| 277 |  | 
|---|
| 278 | for (nparam = 0 ; argv[nparam] ; nparam++) | 
|---|
| 279 | continue; | 
|---|
| 280 | ap = newparam = ckmalloc((nparam + 1) * sizeof *ap); | 
|---|
| 281 | while (*argv) { | 
|---|
| 282 | *ap++ = savestr(*argv++); | 
|---|
| 283 | } | 
|---|
| 284 | *ap = NULL; | 
|---|
| 285 | freeparam(&shellparam); | 
|---|
| 286 | shellparam.malloc = 1; | 
|---|
| 287 | shellparam.nparam = nparam; | 
|---|
| 288 | shellparam.p = newparam; | 
|---|
| 289 | shellparam.optnext = NULL; | 
|---|
| 290 | } | 
|---|
| 291 |  | 
|---|
| 292 |  | 
|---|
| 293 | /* | 
|---|
| 294 | * Free the list of positional parameters. | 
|---|
| 295 | */ | 
|---|
| 296 |  | 
|---|
| 297 | void | 
|---|
| 298 | freeparam(volatile struct shparam *param) | 
|---|
| 299 | { | 
|---|
| 300 | char **ap; | 
|---|
| 301 |  | 
|---|
| 302 | if (param->malloc) { | 
|---|
| 303 | for (ap = param->p ; *ap ; ap++) | 
|---|
| 304 | ckfree(*ap); | 
|---|
| 305 | ckfree(param->p); | 
|---|
| 306 | } | 
|---|
| 307 | } | 
|---|
| 308 |  | 
|---|
| 309 |  | 
|---|
| 310 |  | 
|---|
| 311 | /* | 
|---|
| 312 | * The shift builtin command. | 
|---|
| 313 | */ | 
|---|
| 314 |  | 
|---|
| 315 | int | 
|---|
| 316 | shiftcmd(int argc, char **argv) | 
|---|
| 317 | { | 
|---|
| 318 | int n; | 
|---|
| 319 | char **ap1, **ap2; | 
|---|
| 320 |  | 
|---|
| 321 | n = 1; | 
|---|
| 322 | if (argc > 1) | 
|---|
| 323 | n = number(argv[1]); | 
|---|
| 324 | if (n > shellparam.nparam) | 
|---|
| 325 | error("can't shift that many"); | 
|---|
| 326 | INTOFF; | 
|---|
| 327 | shellparam.nparam -= n; | 
|---|
| 328 | for (ap1 = shellparam.p ; --n >= 0 ; ap1++) { | 
|---|
| 329 | if (shellparam.malloc) | 
|---|
| 330 | ckfree(*ap1); | 
|---|
| 331 | } | 
|---|
| 332 | ap2 = shellparam.p; | 
|---|
| 333 | while ((*ap2++ = *ap1++) != NULL); | 
|---|
| 334 | shellparam.optnext = NULL; | 
|---|
| 335 | INTON; | 
|---|
| 336 | return 0; | 
|---|
| 337 | } | 
|---|
| 338 |  | 
|---|
| 339 |  | 
|---|
| 340 |  | 
|---|
| 341 | /* | 
|---|
| 342 | * The set command builtin. | 
|---|
| 343 | */ | 
|---|
| 344 |  | 
|---|
| 345 | int | 
|---|
| 346 | setcmd(int argc, char **argv) | 
|---|
| 347 | { | 
|---|
| 348 | if (argc == 1) | 
|---|
| 349 | return showvars(0, 0, 1); | 
|---|
| 350 | INTOFF; | 
|---|
| 351 | options(0); | 
|---|
| 352 | optschanged(); | 
|---|
| 353 | if (*argptr != NULL) { | 
|---|
| 354 | setparam(argptr); | 
|---|
| 355 | } | 
|---|
| 356 | INTON; | 
|---|
| 357 | return 0; | 
|---|
| 358 | } | 
|---|
| 359 |  | 
|---|
| 360 |  | 
|---|
| 361 | void | 
|---|
| 362 | getoptsreset(value) | 
|---|
| 363 | const char *value; | 
|---|
| 364 | { | 
|---|
| 365 | if (number(value) == 1) { | 
|---|
| 366 | shellparam.optnext = NULL; | 
|---|
| 367 | shellparam.reset = 1; | 
|---|
| 368 | } | 
|---|
| 369 | } | 
|---|
| 370 |  | 
|---|
| 371 | /* | 
|---|
| 372 | * The getopts builtin.  Shellparam.optnext points to the next argument | 
|---|
| 373 | * to be processed.  Shellparam.optptr points to the next character to | 
|---|
| 374 | * be processed in the current argument.  If shellparam.optnext is NULL, | 
|---|
| 375 | * then it's the first time getopts has been called. | 
|---|
| 376 | */ | 
|---|
| 377 |  | 
|---|
| 378 | int | 
|---|
| 379 | getoptscmd(int argc, char **argv) | 
|---|
| 380 | { | 
|---|
| 381 | char **optbase; | 
|---|
| 382 |  | 
|---|
| 383 | if (argc < 3) | 
|---|
| 384 | error("usage: getopts optstring var [arg]"); | 
|---|
| 385 | else if (argc == 3) | 
|---|
| 386 | optbase = shellparam.p; | 
|---|
| 387 | else | 
|---|
| 388 | optbase = &argv[3]; | 
|---|
| 389 |  | 
|---|
| 390 | if (shellparam.reset == 1) { | 
|---|
| 391 | shellparam.optnext = optbase; | 
|---|
| 392 | shellparam.optptr = NULL; | 
|---|
| 393 | shellparam.reset = 0; | 
|---|
| 394 | } | 
|---|
| 395 |  | 
|---|
| 396 | return getopts(argv[1], argv[2], optbase, &shellparam.optnext, | 
|---|
| 397 | &shellparam.optptr); | 
|---|
| 398 | } | 
|---|
| 399 |  | 
|---|
| 400 | STATIC int | 
|---|
| 401 | getopts(char *optstr, char *optvar, char **optfirst, char ***optnext, char **optpptr) | 
|---|
| 402 | { | 
|---|
| 403 | char *p, *q; | 
|---|
| 404 | char c = '?'; | 
|---|
| 405 | int done = 0; | 
|---|
| 406 | int ind = 0; | 
|---|
| 407 | int err = 0; | 
|---|
| 408 | char s[12]; | 
|---|
| 409 |  | 
|---|
| 410 | if ((p = *optpptr) == NULL || *p == '\0') { | 
|---|
| 411 | /* Current word is done, advance */ | 
|---|
| 412 | if (*optnext == NULL) | 
|---|
| 413 | return 1; | 
|---|
| 414 | p = **optnext; | 
|---|
| 415 | if (p == NULL || *p != '-' || *++p == '\0') { | 
|---|
| 416 | atend: | 
|---|
| 417 | ind = *optnext - optfirst + 1; | 
|---|
| 418 | *optnext = NULL; | 
|---|
| 419 | p = NULL; | 
|---|
| 420 | done = 1; | 
|---|
| 421 | goto out; | 
|---|
| 422 | } | 
|---|
| 423 | (*optnext)++; | 
|---|
| 424 | if (p[0] == '-' && p[1] == '\0')        /* check for "--" */ | 
|---|
| 425 | goto atend; | 
|---|
| 426 | } | 
|---|
| 427 |  | 
|---|
| 428 | c = *p++; | 
|---|
| 429 | for (q = optstr; *q != c; ) { | 
|---|
| 430 | if (*q == '\0') { | 
|---|
| 431 | if (optstr[0] == ':') { | 
|---|
| 432 | s[0] = c; | 
|---|
| 433 | s[1] = '\0'; | 
|---|
| 434 | err |= setvarsafe("OPTARG", s, 0); | 
|---|
| 435 | } else { | 
|---|
| 436 | outfmt(&errout, "Illegal option -%c\n", c); | 
|---|
| 437 | (void) unsetvar("OPTARG", 0); | 
|---|
| 438 | } | 
|---|
| 439 | c = '?'; | 
|---|
| 440 | goto bad; | 
|---|
| 441 | } | 
|---|
| 442 | if (*++q == ':') | 
|---|
| 443 | q++; | 
|---|
| 444 | } | 
|---|
| 445 |  | 
|---|
| 446 | if (*++q == ':') { | 
|---|
| 447 | if (*p == '\0' && (p = **optnext) == NULL) { | 
|---|
| 448 | if (optstr[0] == ':') { | 
|---|
| 449 | s[0] = c; | 
|---|
| 450 | s[1] = '\0'; | 
|---|
| 451 | err |= setvarsafe("OPTARG", s, 0); | 
|---|
| 452 | c = ':'; | 
|---|
| 453 | } else { | 
|---|
| 454 | outfmt(&errout, "No arg for -%c option\n", c); | 
|---|
| 455 | (void) unsetvar("OPTARG", 0); | 
|---|
| 456 | c = '?'; | 
|---|
| 457 | } | 
|---|
| 458 | goto bad; | 
|---|
| 459 | } | 
|---|
| 460 |  | 
|---|
| 461 | if (p == **optnext) | 
|---|
| 462 | (*optnext)++; | 
|---|
| 463 | err |= setvarsafe("OPTARG", p, 0); | 
|---|
| 464 | p = NULL; | 
|---|
| 465 | } else | 
|---|
| 466 | err |= setvarsafe("OPTARG", "", 0); | 
|---|
| 467 | ind = *optnext - optfirst + 1; | 
|---|
| 468 | goto out; | 
|---|
| 469 |  | 
|---|
| 470 | bad: | 
|---|
| 471 | ind = 1; | 
|---|
| 472 | *optnext = NULL; | 
|---|
| 473 | p = NULL; | 
|---|
| 474 | out: | 
|---|
| 475 | *optpptr = p; | 
|---|
| 476 | fmtstr(s, sizeof(s), "%d", ind); | 
|---|
| 477 | err |= setvarsafe("OPTIND", s, VNOFUNC); | 
|---|
| 478 | s[0] = c; | 
|---|
| 479 | s[1] = '\0'; | 
|---|
| 480 | err |= setvarsafe(optvar, s, 0); | 
|---|
| 481 | if (err) { | 
|---|
| 482 | *optnext = NULL; | 
|---|
| 483 | *optpptr = NULL; | 
|---|
| 484 | output_flushall(); | 
|---|
| 485 | exraise(EXERROR); | 
|---|
| 486 | } | 
|---|
| 487 | return done; | 
|---|
| 488 | } | 
|---|
| 489 |  | 
|---|
| 490 | /* | 
|---|
| 491 | * XXX - should get rid of.  have all builtins use getopt(3).  the | 
|---|
| 492 | * library getopt must have the BSD extension static variable "optreset" | 
|---|
| 493 | * otherwise it can't be used within the shell safely. | 
|---|
| 494 | * | 
|---|
| 495 | * Standard option processing (a la getopt) for builtin routines.  The | 
|---|
| 496 | * only argument that is passed to nextopt is the option string; the | 
|---|
| 497 | * other arguments are unnecessary.  It return the character, or '\0' on | 
|---|
| 498 | * end of input. | 
|---|
| 499 | */ | 
|---|
| 500 |  | 
|---|
| 501 | int | 
|---|
| 502 | nextopt(const char *optstring) | 
|---|
| 503 | { | 
|---|
| 504 | char *p; | 
|---|
| 505 | const char *q; | 
|---|
| 506 | char c; | 
|---|
| 507 |  | 
|---|
| 508 | if ((p = optptr) == NULL || *p == '\0') { | 
|---|
| 509 | p = *argptr; | 
|---|
| 510 | if (p == NULL || *p != '-' || *++p == '\0') | 
|---|
| 511 | return '\0'; | 
|---|
| 512 | argptr++; | 
|---|
| 513 | if (p[0] == '-' && p[1] == '\0')        /* check for "--" */ | 
|---|
| 514 | return '\0'; | 
|---|
| 515 | } | 
|---|
| 516 | c = *p++; | 
|---|
| 517 | for (q = optstring ; *q != c ; ) { | 
|---|
| 518 | if (*q == '\0') | 
|---|
| 519 | error("Illegal option -%c", c); | 
|---|
| 520 | if (*++q == ':') | 
|---|
| 521 | q++; | 
|---|
| 522 | } | 
|---|
| 523 | if (*++q == ':') { | 
|---|
| 524 | if (*p == '\0' && (p = *argptr++) == NULL) | 
|---|
| 525 | error("No arg for -%c option", c); | 
|---|
| 526 | optionarg = p; | 
|---|
| 527 | p = NULL; | 
|---|
| 528 | } | 
|---|
| 529 | optptr = p; | 
|---|
| 530 | return c; | 
|---|
| 531 | } | 
|---|