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 * 4. Neither the name of the University nor the names of its contributors 17 * may be used to endorse or promote products derived from this software 18 * without specific prior written permission. 19 * 20 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 23 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 24 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 26 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 30 * SUCH DAMAGE. 31 */ 32 33 #ifndef lint 34 #if 0 35 static char sccsid[] = "@(#)options.c 8.2 (Berkeley) 5/4/95"; 36 #endif 37 #endif /* not lint */ 38 #include <sys/cdefs.h> 39 __FBSDID("$FreeBSD$"); 40 41 #include <signal.h> 42 #include <unistd.h> 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 NO_HISTORY 60 #include "myhistedit.h" 61 #endif 62 63 char *arg0; /* value of $0 */ 64 struct shparam shellparam; /* current positional parameters */ 65 char **argptr; /* argument list for builtin commands */ 66 char *shoptarg; /* set by nextopt (like getopt) */ 67 char *nextopt_optptr; /* used by nextopt */ 68 69 char *minusc; /* argument to -c option */ 70 71 72 static void options(int); 73 static void minus_o(char *, int); 74 static void setoption(int, int); 75 static int getopts(char *, char *, char **, char ***, char **); 76 77 78 /* 79 * Process the shell command line arguments. 80 */ 81 82 void 83 procargs(int argc, char **argv) 84 { 85 int i; 86 87 argptr = argv; 88 if (argc > 0) 89 argptr++; 90 for (i = 0; i < NOPTS; i++) 91 optlist[i].val = 2; 92 privileged = (getuid() != geteuid() || getgid() != getegid()); 93 options(1); 94 if (*argptr == NULL && minusc == NULL) 95 sflag = 1; 96 if (iflag != 0 && sflag == 1 && isatty(0) && isatty(1)) { 97 iflag = 1; 98 if (Eflag == 2) 99 Eflag = 1; 100 } 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 arg0 = argv[0]; 107 if (sflag == 0 && minusc == NULL) { 108 commandname = arg0 = *argptr++; 109 setinputfile(commandname, 0); 110 } 111 /* POSIX 1003.2: first arg after -c cmd is $0, remainder $1... */ 112 if (argptr && minusc && *argptr) 113 arg0 = *argptr++; 114 115 shellparam.p = argptr; 116 shellparam.reset = 1; 117 /* assert(shellparam.malloc == 0 && shellparam.nparam == 0); */ 118 while (*argptr) { 119 shellparam.nparam++; 120 argptr++; 121 } 122 optschanged(); 123 } 124 125 126 void 127 optschanged(void) 128 { 129 setinteractive(iflag); 130 #ifndef NO_HISTORY 131 histedit(); 132 #endif 133 setjobctl(mflag); 134 } 135 136 /* 137 * Process shell options. The global variable argptr contains a pointer 138 * to the argument list; we advance it past the options. 139 */ 140 141 static void 142 options(int cmdline) 143 { 144 char *kp, *p; 145 int val; 146 int c; 147 148 if (cmdline) 149 minusc = NULL; 150 while ((p = *argptr) != NULL) { 151 argptr++; 152 if ((c = *p++) == '-') { 153 val = 1; 154 /* A "-" or "--" terminates options */ 155 if (p[0] == '\0') 156 goto end_options1; 157 if (p[0] == '-' && p[1] == '\0') 158 goto end_options2; 159 /** 160 * For the benefit of `#!' lines in shell scripts, 161 * treat a string of '-- *#.*' the same as '--'. 162 * This is needed so that a script starting with: 163 * #!/bin/sh -- # -*- perl -*- 164 * will continue to work after a change is made to 165 * kern/imgact_shell.c to NOT token-ize the options 166 * specified on a '#!' line. A bit of a kludge, 167 * but that trick is recommended in documentation 168 * for some scripting languages, and we might as 169 * well continue to support it. 170 */ 171 if (p[0] == '-') { 172 kp = p + 1; 173 while (*kp == ' ' || *kp == '\t') 174 kp++; 175 if (*kp == '#' || *kp == '\0') 176 goto end_options2; 177 } 178 } else if (c == '+') { 179 val = 0; 180 } else { 181 argptr--; 182 break; 183 } 184 while ((c = *p++) != '\0') { 185 if (c == 'c' && cmdline) { 186 char *q; 187 #ifdef NOHACK /* removing this code allows sh -ce 'foo' for compat */ 188 if (*p == '\0') 189 #endif 190 q = *argptr++; 191 if (q == NULL || minusc != NULL) 192 error("Bad -c option"); 193 minusc = q; 194 #ifdef NOHACK 195 break; 196 #endif 197 } else if (c == 'o') { 198 minus_o(*argptr, val); 199 if (*argptr) 200 argptr++; 201 } else { 202 if (c == 'p' && !val && privileged) { 203 (void) setuid(getuid()); 204 (void) setgid(getgid()); 205 } 206 setoption(c, val); 207 } 208 } 209 } 210 return; 211 212 /* When processing `set', a single "-" means turn off -x and -v */ 213 end_options1: 214 if (!cmdline) { 215 xflag = vflag = 0; 216 return; 217 } 218 219 /* 220 * When processing `set', a "--" means the remaining arguments 221 * replace the positional parameters in the active shell. If 222 * there are no remaining options, then all the positional 223 * parameters are cleared (equivalent to doing ``shift $#''). 224 */ 225 end_options2: 226 if (!cmdline) { 227 if (*argptr == NULL) 228 setparam(argptr); 229 return; 230 } 231 232 /* 233 * At this point we are processing options given to 'sh' on a command 234 * line. If an end-of-options marker ("-" or "--") is followed by an 235 * arg of "#", then skip over all remaining arguments. Some scripting 236 * languages (e.g.: perl) document that /bin/sh will implement this 237 * behavior, and they recommend that users take advantage of it to 238 * solve certain issues that can come up when writing a perl script. 239 * Yes, this feature is in /bin/sh to help users write perl scripts. 240 */ 241 p = *argptr; 242 if (p != NULL && p[0] == '#' && p[1] == '\0') { 243 while (*argptr != NULL) 244 argptr++; 245 /* We need to keep the final argument */ 246 argptr--; 247 } 248 } 249 250 static void 251 minus_o(char *name, int val) 252 { 253 int i; 254 255 if (name == NULL) { 256 if (val) { 257 /* "Pretty" output. */ 258 out1str("Current option settings\n"); 259 for (i = 0; i < NOPTS; i++) 260 out1fmt("%-16s%s\n", optlist[i].name, 261 optlist[i].val ? "on" : "off"); 262 } else { 263 /* Output suitable for re-input to shell. */ 264 for (i = 0; i < NOPTS; i++) 265 out1fmt("%s %co %s%s", 266 i % 6 == 0 ? "set" : "", 267 optlist[i].val ? '-' : '+', 268 optlist[i].name, 269 i % 6 == 5 || i == NOPTS - 1 ? "\n" : ""); 270 } 271 } else { 272 for (i = 0; i < NOPTS; i++) 273 if (equal(name, optlist[i].name)) { 274 if (!val && privileged && equal(name, "privileged")) { 275 (void) setuid(getuid()); 276 (void) setgid(getgid()); 277 } 278 setoption(optlist[i].letter, val); 279 return; 280 } 281 error("Illegal option -o %s", name); 282 } 283 } 284 285 286 static void 287 setoption(int flag, int val) 288 { 289 int i; 290 291 for (i = 0; i < NOPTS; i++) 292 if (optlist[i].letter == flag) { 293 optlist[i].val = val; 294 if (val) { 295 /* #%$ hack for ksh semantics */ 296 if (flag == 'V') 297 Eflag = 0; 298 else if (flag == 'E') 299 Vflag = 0; 300 } 301 return; 302 } 303 error("Illegal option -%c", flag); 304 } 305 306 307 /* 308 * Set the shell parameters. 309 */ 310 311 void 312 setparam(char **argv) 313 { 314 char **newparam; 315 char **ap; 316 int nparam; 317 318 for (nparam = 0 ; argv[nparam] ; nparam++); 319 ap = newparam = ckmalloc((nparam + 1) * sizeof *ap); 320 while (*argv) { 321 *ap++ = savestr(*argv++); 322 } 323 *ap = NULL; 324 freeparam(&shellparam); 325 shellparam.malloc = 1; 326 shellparam.nparam = nparam; 327 shellparam.p = newparam; 328 shellparam.reset = 1; 329 shellparam.optnext = NULL; 330 } 331 332 333 /* 334 * Free the list of positional parameters. 335 */ 336 337 void 338 freeparam(struct shparam *param) 339 { 340 char **ap; 341 342 if (param->malloc) { 343 for (ap = param->p ; *ap ; ap++) 344 ckfree(*ap); 345 ckfree(param->p); 346 } 347 } 348 349 350 351 /* 352 * The shift builtin command. 353 */ 354 355 int 356 shiftcmd(int argc, char **argv) 357 { 358 int n; 359 char **ap1, **ap2; 360 361 n = 1; 362 if (argc > 1) 363 n = number(argv[1]); 364 if (n > shellparam.nparam) 365 return 1; 366 INTOFF; 367 shellparam.nparam -= n; 368 for (ap1 = shellparam.p ; --n >= 0 ; ap1++) { 369 if (shellparam.malloc) 370 ckfree(*ap1); 371 } 372 ap2 = shellparam.p; 373 while ((*ap2++ = *ap1++) != NULL); 374 shellparam.reset = 1; 375 INTON; 376 return 0; 377 } 378 379 380 381 /* 382 * The set command builtin. 383 */ 384 385 int 386 setcmd(int argc, char **argv) 387 { 388 if (argc == 1) 389 return showvarscmd(argc, argv); 390 INTOFF; 391 options(0); 392 optschanged(); 393 if (*argptr != NULL) { 394 setparam(argptr); 395 } 396 INTON; 397 return 0; 398 } 399 400 401 void 402 getoptsreset(const char *value) 403 { 404 if (number(value) == 1) { 405 shellparam.reset = 1; 406 } 407 } 408 409 /* 410 * The getopts builtin. Shellparam.optnext points to the next argument 411 * to be processed. Shellparam.optptr points to the next character to 412 * be processed in the current argument. If shellparam.optnext is NULL, 413 * then it's the first time getopts has been called. 414 */ 415 416 int 417 getoptscmd(int argc, char **argv) 418 { 419 char **optbase = NULL; 420 421 if (argc < 3) 422 error("usage: getopts optstring var [arg]"); 423 else if (argc == 3) 424 optbase = shellparam.p; 425 else 426 optbase = &argv[3]; 427 428 if (shellparam.reset == 1) { 429 shellparam.optnext = optbase; 430 shellparam.optptr = NULL; 431 shellparam.reset = 0; 432 } 433 434 return getopts(argv[1], argv[2], optbase, &shellparam.optnext, 435 &shellparam.optptr); 436 } 437 438 static int 439 getopts(char *optstr, char *optvar, char **optfirst, char ***optnext, 440 char **optptr) 441 { 442 char *p, *q; 443 char c = '?'; 444 int done = 0; 445 int ind = 0; 446 int err = 0; 447 char s[10]; 448 449 if ((p = *optptr) == NULL || *p == '\0') { 450 /* Current word is done, advance */ 451 if (*optnext == NULL) 452 return 1; 453 p = **optnext; 454 if (p == NULL || *p != '-' || *++p == '\0') { 455 atend: 456 ind = *optnext - optfirst + 1; 457 *optnext = NULL; 458 p = NULL; 459 done = 1; 460 goto out; 461 } 462 (*optnext)++; 463 if (p[0] == '-' && p[1] == '\0') /* check for "--" */ 464 goto atend; 465 } 466 467 c = *p++; 468 for (q = optstr; *q != c; ) { 469 if (*q == '\0') { 470 if (optstr[0] == ':') { 471 s[0] = c; 472 s[1] = '\0'; 473 err |= setvarsafe("OPTARG", s, 0); 474 } 475 else { 476 out1fmt("Illegal option -%c\n", c); 477 (void) unsetvar("OPTARG"); 478 } 479 c = '?'; 480 goto bad; 481 } 482 if (*++q == ':') 483 q++; 484 } 485 486 if (*++q == ':') { 487 if (*p == '\0' && (p = **optnext) == NULL) { 488 if (optstr[0] == ':') { 489 s[0] = c; 490 s[1] = '\0'; 491 err |= setvarsafe("OPTARG", s, 0); 492 c = ':'; 493 } 494 else { 495 out1fmt("No arg for -%c option\n", c); 496 (void) unsetvar("OPTARG"); 497 c = '?'; 498 } 499 goto bad; 500 } 501 502 if (p == **optnext) 503 (*optnext)++; 504 setvarsafe("OPTARG", p, 0); 505 p = NULL; 506 } 507 else 508 setvarsafe("OPTARG", "", 0); 509 ind = *optnext - optfirst + 1; 510 goto out; 511 512 bad: 513 ind = 1; 514 *optnext = NULL; 515 p = NULL; 516 out: 517 *optptr = p; 518 fmtstr(s, sizeof(s), "%d", ind); 519 err |= setvarsafe("OPTIND", s, VNOFUNC); 520 s[0] = c; 521 s[1] = '\0'; 522 err |= setvarsafe(optvar, s, 0); 523 if (err) { 524 *optnext = NULL; 525 *optptr = NULL; 526 flushall(); 527 exraise(EXERROR); 528 } 529 return done; 530 } 531 532 /* 533 * XXX - should get rid of. have all builtins use getopt(3). the 534 * library getopt must have the BSD extension static variable "optreset" 535 * otherwise it can't be used within the shell safely. 536 * 537 * Standard option processing (a la getopt) for builtin routines. The 538 * only argument that is passed to nextopt is the option string; the 539 * other arguments are unnecessary. It return the character, or '\0' on 540 * end of input. 541 */ 542 543 int 544 nextopt(const char *optstring) 545 { 546 char *p; 547 const char *q; 548 char c; 549 550 if ((p = nextopt_optptr) == NULL || *p == '\0') { 551 p = *argptr; 552 if (p == NULL || *p != '-' || *++p == '\0') 553 return '\0'; 554 argptr++; 555 if (p[0] == '-' && p[1] == '\0') /* check for "--" */ 556 return '\0'; 557 } 558 c = *p++; 559 for (q = optstring ; *q != c ; ) { 560 if (*q == '\0') 561 error("Illegal option -%c", c); 562 if (*++q == ':') 563 q++; 564 } 565 if (*++q == ':') { 566 if (*p == '\0' && (p = *argptr++) == NULL) 567 error("No arg for -%c option", c); 568 shoptarg = p; 569 p = NULL; 570 } 571 nextopt_optptr = p; 572 return c; 573 } 574