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