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