1 /* $NetBSD: options.c,v 1.60 2024/09/21 20:48:50 kre 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.60 2024/09/21 20:48:50 kre 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 "builtins.h" 53 #include "nodes.h" /* for other header files */ 54 #include "eval.h" 55 #include "jobs.h" 56 #include "input.h" 57 #include "output.h" 58 #include "trap.h" 59 #include "var.h" 60 #include "memalloc.h" 61 #include "error.h" 62 #include "mystring.h" 63 #include "syntax.h" 64 #ifndef SMALL 65 #include "myhistedit.h" 66 #endif 67 #include "show.h" 68 69 char *arg0; /* value of $0 */ 70 struct shparam shellparam; /* current positional parameters */ 71 char **argptr; /* argument list for builtin commands */ 72 char *optionarg; /* set by nextopt (like getopt) */ 73 char *optptr; /* used by nextopt */ 74 75 char *minusc; /* argument to -c option */ 76 77 78 STATIC void options(int); 79 STATIC void minus_o(char *, int); 80 STATIC void setoption(int, int); 81 STATIC int getopts(char *, char *, char **, char ***, char **); 82 83 84 /* 85 * Process the shell command line arguments. 86 */ 87 88 void 89 procargs(int argc, char **argv) 90 { 91 size_t i; 92 int psx; 93 94 argptr = argv; 95 if (argc > 0) 96 argptr++; 97 98 psx = posix; /* save what we set it to earlier */ 99 /* 100 * option values are mostly boolean 0:off 1:on 101 * we use 2 (just in this routine) to mean "unknown yet" 102 */ 103 for (i = 0; i < NOPTS; i++) 104 optlist[i].val = 2; 105 posix = psx; /* restore before processing -o ... */ 106 107 options(1); 108 109 if (*argptr == NULL && minusc == NULL) 110 sflag = 1; 111 if (iflag == 2 && sflag == 1 && isatty(0) && isatty(2)) 112 iflag = 1; 113 if (iflag == 1 && sflag == 2) 114 iflag = 2; 115 if (mflag == 2) 116 mflag = iflag; 117 #ifndef DO_SHAREDVFORK 118 if (usefork == 2) 119 usefork = 1; 120 #endif 121 #if DEBUG >= 2 122 if (debug == 2) 123 debug = 1; 124 #endif 125 arg0 = argv[0]; 126 if (loginsh == 2 && arg0 != NULL && arg0[0] == '-') 127 loginsh = 1; 128 129 /* 130 * Any options not dealt with as special cases just above, 131 * and which were not set on the command line, are set to 132 * their expected default values (mostly "off") 133 * 134 * then as each option is initialised, save its setting now 135 * as its "default" value for future use ("set -o default"). 136 */ 137 for (i = 0; i < NOPTS; i++) { 138 if (optlist[i].val == 2) 139 optlist[i].val = optlist[i].dflt; 140 optlist[i].dflt = optlist[i].val; 141 } 142 143 if (sflag == 0 && minusc == NULL) { 144 commandname = argv[0]; 145 arg0 = *argptr++; 146 setinputfile(arg0, 0); 147 commandname = arg0; 148 } 149 /* POSIX 1003.2: first arg after -c cmd is $0, remainder $1... */ 150 if (minusc != NULL) { 151 if (argptr == NULL || *argptr == NULL) 152 error("Bad -c option"); 153 minusc = *argptr++; 154 if (*argptr != 0) 155 arg0 = *argptr++; 156 } 157 158 shellparam.p = argptr; 159 shellparam.reset = 1; 160 /* assert(shellparam.malloc == 0 && shellparam.nparam == 0); */ 161 while (*argptr) { 162 shellparam.nparam++; 163 argptr++; 164 } 165 optschanged(); 166 } 167 168 169 void 170 optschanged(void) 171 { 172 setinteractive(iflag); 173 #ifndef SMALL 174 histedit(); 175 #endif 176 setjobctl(mflag); 177 178 if (privileged && !pflag) { 179 setuid(getuid()); 180 setgid(getgid()); 181 privileged = 0; 182 setvarsafe("PSc", (getuid() == 0 ? "#" : "$"), 0); 183 } 184 } 185 186 /* 187 * Process shell options. The global variable argptr contains a pointer 188 * to the argument list; we advance it past the options. 189 */ 190 191 STATIC void 192 options(int cmdline) 193 { 194 static char empty[] = ""; 195 char *p; 196 int val; 197 int c; 198 199 if (cmdline) 200 minusc = NULL; 201 while ((p = *argptr) != NULL) { 202 argptr++; 203 if ((c = *p++) == '-') { 204 val = 1; 205 if (p[0] == '\0' || (p[0] == '-' && p[1] == '\0')) { 206 if (!cmdline) { 207 /* "-" means turn off -x and -v */ 208 if (p[0] == '\0') 209 xflag = vflag = 0; 210 /* "--" means reset params */ 211 else if (*argptr == NULL) 212 setparam(argptr); 213 } 214 break; /* "-" or "--" terminates options */ 215 } 216 } else if (c == '+') { 217 val = 0; 218 } else { 219 argptr--; 220 break; 221 } 222 while ((c = *p++) != '\0') { 223 if (val == 1 && c == 'c' && cmdline) { 224 /* command is after shell args*/ 225 minusc = empty; 226 } else if (c == 'o') { 227 if (*p != '\0') 228 minus_o(p, val + (cmdline ? val : 0)); 229 else if (*argptr) 230 minus_o(*argptr++, 231 val + (cmdline ? val : 0)); 232 else if (!cmdline) 233 minus_o(NULL, val); 234 else 235 error("arg for %co missing", "+-"[val]); 236 break; 237 #ifdef DEBUG 238 } else if (c == 'D') { 239 if (*p) { 240 set_debug(p, val); 241 break; 242 } else if (*argptr) 243 set_debug(*argptr++, val); 244 else 245 set_debug("*$", val); 246 #endif 247 } else { 248 setoption(c, val); 249 } 250 } 251 } 252 } 253 254 static void 255 set_opt_val(size_t i, int val) 256 { 257 size_t j; 258 int flag; 259 260 if (val && (flag = optlist[i].opt_set)) { 261 /* some options (eg vi/emacs) are mutually exclusive */ 262 for (j = 0; j < NOPTS; j++) 263 if (optlist[j].opt_set == flag) 264 optlist[j].val = 0; 265 } 266 #ifndef SMALL 267 if (i == _SH_OPT_Xflag) 268 xtracefdsetup(val); 269 #endif 270 optlist[i].val = val; 271 #ifdef DEBUG 272 if (&optlist[i].val == &debug) 273 opentrace(); /* different "trace" than the -x one... */ 274 #endif 275 } 276 277 STATIC void 278 minus_o(char *name, int val) 279 { 280 size_t i; 281 const char *sep = ": "; 282 283 if (name == NULL) { 284 if (val) { 285 out1str("Current option settings"); 286 for (i = 0; i < NOPTS; i++) { 287 if (optlist[i].name == NULL) { 288 out1fmt("%s%c%c", sep, 289 "+-"[optlist[i].val], 290 optlist[i].letter); 291 sep = ", "; 292 } 293 } 294 out1c('\n'); 295 for (i = 0; i < NOPTS; i++) { 296 if (optlist[i].name) 297 out1fmt("%-19s %s\n", optlist[i].name, 298 optlist[i].val ? "on" : "off"); 299 } 300 } else { 301 out1str("set -o default"); 302 for (i = 0; i < NOPTS; i++) { 303 if (optlist[i].val == optlist[i].dflt) 304 continue; 305 if (optlist[i].name) 306 out1fmt(" %co %s", 307 "+-"[optlist[i].val], optlist[i].name); 308 else 309 out1fmt(" %c%c", "+-"[optlist[i].val], 310 optlist[i].letter); 311 } 312 out1c('\n'); 313 } 314 } else { 315 if (val == 1 && equal(name, "default")) { /* special case */ 316 for (i = 0; i < NOPTS; i++) 317 set_opt_val(i, optlist[i].dflt); 318 return; 319 } 320 if (val) 321 val = 1; 322 for (i = 0; i < NOPTS; i++) 323 if (optlist[i].name && equal(name, optlist[i].name)) { 324 set_opt_val(i, val); 325 #ifndef SMALL 326 if (i == _SH_OPT_Xflag) 327 set_opt_val(_SH_OPT_xflag, val); 328 #endif 329 return; 330 } 331 error("Unknown option %co %s", "+-"[val], name); 332 } 333 } 334 335 336 STATIC void 337 setoption(int flag, int val) 338 { 339 size_t i; 340 341 for (i = 0; i < NOPTS; i++) 342 if (optlist[i].letter == flag) { 343 set_opt_val(i, val); 344 #ifndef SMALL 345 if (i == _SH_OPT_Xflag) 346 set_opt_val(_SH_OPT_xflag, val); 347 #endif 348 return; 349 } 350 error("Unknown option %c%c", "+-"[val], flag); 351 /* NOTREACHED */ 352 } 353 354 355 356 #ifdef mkinit 357 INCLUDE "options.h" 358 359 SHELLPROC { 360 int i; 361 362 for (i = 0; optlist[i].name; i++) 363 optlist[i].val = 0; 364 optschanged(); 365 366 } 367 #endif 368 369 370 /* 371 * Set the shell parameters. 372 */ 373 374 void 375 setparam(char **argv) 376 { 377 char **newparam; 378 char **ap; 379 int nparam; 380 381 for (nparam = 0 ; argv[nparam] ; nparam++) 382 continue; 383 ap = newparam = ckmalloc((nparam + 1) * sizeof *ap); 384 while (*argv) { 385 *ap++ = savestr(*argv++); 386 } 387 *ap = NULL; 388 freeparam(&shellparam); 389 shellparam.malloc = 1; 390 shellparam.nparam = nparam; 391 shellparam.p = newparam; 392 shellparam.optnext = NULL; 393 } 394 395 396 /* 397 * Free the list of positional parameters. 398 */ 399 400 void 401 freeparam(volatile struct shparam *param) 402 { 403 char **ap; 404 405 if (param->malloc) { 406 for (ap = param->p ; *ap ; ap++) 407 ckfree(*ap); 408 ckfree(param->p); 409 } 410 } 411 412 413 414 /* 415 * The shift builtin command. 416 */ 417 418 #ifndef TINY 419 /* first the rotate variant */ 420 static inline int 421 rotatecmd(int argc, char **argv) 422 { 423 int n; 424 char **ap1, **ap2, **ss; 425 426 (void) nextopt(NULL); /* ignore '--' as leading option */ 427 428 /* 429 * half this is just in case it ever becomes 430 * a separate named command, while it remains 431 * puerly an inline inside shift, the compiler 432 * should optimise most of it to nothingness 433 */ 434 if (argptr[0] && argptr[1]) 435 error("Usage: rotate [n]"); 436 n = 1; 437 if (*argptr) { 438 if (**argptr == '-') 439 n = number(*argptr + 1); 440 else /* anti-clockwise n == clockwise $# - n */ 441 n = shellparam.nparam - number(*argptr); 442 } 443 444 if (n == 0 || n == shellparam.nparam) /* nothing to do */ 445 return 0; 446 447 if (n < 0 || n > shellparam.nparam) 448 error("can't rotate that many"); 449 450 ap2 = ss = (char **)stalloc(n * sizeof(char *)); 451 INTOFF; 452 for (ap1 = shellparam.p + shellparam.nparam - n; 453 ap1 < shellparam.p + shellparam.nparam; ) 454 *ap2++ = *ap1++; 455 for (ap2 = shellparam.p + shellparam.nparam, ap1 = ap2 - n; 456 ap1 > shellparam.p; ) 457 *--ap2 = *--ap1; 458 for (ap1 = ss + n; ap1 > ss; ) 459 *--ap2 = *--ap1; 460 shellparam.optnext = NULL; 461 INTON; 462 stunalloc(ss); 463 464 return 0; 465 } 466 #endif 467 468 int 469 shiftcmd(int argc, char **argv) 470 { 471 int n; 472 char **ap1, **ap2; 473 474 (void) nextopt(NULL); /* ignore '--' as leading option */ 475 476 if (argptr[0] && argptr[1]) 477 error("Usage: shift [n]"); 478 479 #ifndef TINY 480 if (*argptr && **argptr == '-') { 481 argptr = argv + 1; /* reinit nextopt() */ 482 optptr = NULL; 483 return rotatecmd(argc, argv); 484 } 485 #endif 486 487 n = 1; 488 if (*argptr) 489 n = number(*argptr); 490 if (n > shellparam.nparam) 491 error("can't shift that many"); 492 INTOFF; 493 shellparam.nparam -= n; 494 for (ap1 = shellparam.p ; --n >= 0 ; ap1++) { 495 if (shellparam.malloc) 496 ckfree(*ap1); 497 } 498 ap2 = shellparam.p; 499 while ((*ap2++ = *ap1++) != NULL) 500 continue; 501 shellparam.optnext = NULL; 502 INTON; 503 return 0; 504 } 505 506 507 508 /* 509 * The set command builtin. 510 */ 511 512 int 513 setcmd(int argc, char **argv) 514 { 515 if (argc == 1) 516 return showvars(0, 0, 1, 0); 517 INTOFF; 518 options(0); 519 optschanged(); 520 if (*argptr != NULL) { 521 setparam(argptr); 522 } 523 INTON; 524 return 0; 525 } 526 527 528 void 529 getoptsreset(char *value, int flags __unused) 530 { 531 /* 532 * This is just to detect the case where OPTIND=1 533 * is executed. Any other string assigned to OPTIND 534 * is OK, but is not a reset. No errors, so cannot use number() 535 */ 536 if (is_digit(*value) && strtol(value, NULL, 10) == 1) { 537 shellparam.optnext = NULL; 538 shellparam.reset = 1; 539 } 540 } 541 542 /* 543 * The getopts builtin. Shellparam.optnext points to the next argument 544 * to be processed. Shellparam.optptr points to the next character to 545 * be processed in the current argument. If shellparam.optnext is NULL, 546 * then it's the first time getopts has been called. 547 */ 548 549 int 550 getoptscmd(int argc, char **argv) 551 { 552 char **optbase; 553 554 if (argc < 3) 555 error("usage: getopts optstring var [arg]"); 556 else if (argc == 3) 557 optbase = shellparam.p; 558 else 559 optbase = &argv[3]; 560 561 if (shellparam.reset == 1) { 562 shellparam.optnext = optbase; 563 shellparam.optptr = NULL; 564 shellparam.reset = 0; 565 } 566 567 return getopts(argv[1], argv[2], optbase, &shellparam.optnext, 568 &shellparam.optptr); 569 } 570 571 STATIC int 572 getopts(char *optstr, char *optvar, char **optfirst, char ***optnext, char **optpptr) 573 { 574 char *p, *q; 575 char c = '?'; 576 int done = 0; 577 int ind = 0; 578 int err = 0; 579 char s[12]; 580 581 if ((p = *optpptr) == NULL || *p == '\0') { 582 /* Current word is done, advance */ 583 if (*optnext == NULL) 584 return 1; 585 p = **optnext; 586 if (p == NULL || *p != '-' || *++p == '\0') { 587 atend: 588 ind = *optnext - optfirst + 1; 589 *optnext = NULL; 590 p = NULL; 591 done = 1; 592 goto out; 593 } 594 (*optnext)++; 595 if (p[0] == '-' && p[1] == '\0') /* check for "--" */ 596 goto atend; 597 } 598 599 c = *p++; 600 for (q = optstr; *q != c; ) { 601 if (*q == '\0') { 602 if (optstr[0] == ':') { 603 s[0] = c; 604 s[1] = '\0'; 605 err |= setvarsafe("OPTARG", s, 0); 606 } else { 607 outfmt(&errout, "Unknown option -%c\n", c); 608 (void) unsetvar("OPTARG", 0); 609 } 610 c = '?'; 611 goto bad; 612 } 613 if (*++q == ':') 614 q++; 615 } 616 617 if (*++q == ':') { 618 if (*p == '\0' && (p = **optnext) == NULL) { 619 if (optstr[0] == ':') { 620 s[0] = c; 621 s[1] = '\0'; 622 err |= setvarsafe("OPTARG", s, 0); 623 c = ':'; 624 } else { 625 outfmt(&errout, "No arg for -%c option\n", c); 626 (void) unsetvar("OPTARG", 0); 627 c = '?'; 628 } 629 goto bad; 630 } 631 632 if (p == **optnext) 633 (*optnext)++; 634 err |= setvarsafe("OPTARG", p, 0); 635 p = NULL; 636 } else 637 err |= setvarsafe("OPTARG", "", 0); 638 ind = *optnext - optfirst + 1; 639 goto out; 640 641 bad: 642 ind = 1; 643 *optnext = NULL; 644 p = NULL; 645 out: 646 *optpptr = p; 647 fmtstr(s, sizeof(s), "%d", ind); 648 err |= setvarsafe("OPTIND", s, VNOFUNC); 649 s[0] = c; 650 s[1] = '\0'; 651 err |= setvarsafe(optvar, s, 0); 652 if (err) { 653 *optnext = NULL; 654 *optpptr = NULL; 655 flushall(); 656 exraise(EXERROR); 657 } 658 return done; 659 } 660 661 /* 662 * XXX - should get rid of. have all builtins use getopt(3). the 663 * library getopt must have the BSD extension static variable "optreset" 664 * otherwise it can't be used within the shell safely. 665 * 666 * Standard option processing (a la getopt) for builtin routines. The 667 * only argument that is passed to nextopt is the option string; the 668 * other arguments are unnecessary. It return the character, or '\0' on 669 * end of input. If optstring is NULL, then there are no options, and 670 * args are allowed to begin with '-', but a single leading "--" will be 671 * discarded. This is for some POSIX special builtins that require 672 * -- processing, have no args, and we never did opt processing before 673 * and need to retain backwards compat. 674 */ 675 676 int 677 nextopt(const char *optstring) 678 { 679 char *p; 680 const char *q; 681 char c; 682 683 if ((p = optptr) == NULL || *p == '\0') { 684 p = *argptr; 685 if (p == NULL || *p != '-' || *++p == '\0') 686 return '\0'; 687 argptr++; 688 if (p[0] == '-' && p[1] == '\0') /* check for "--" */ 689 return '\0'; 690 if (optstring == NULL) /* not processing the "option" */ 691 argptr--; /* so make it be an arg again */ 692 } 693 if (optstring == NULL) 694 return '\0'; 695 c = *p++; 696 for (q = optstring ; *q != c ; ) { 697 if (*q == '\0') 698 error("Unknown option -%c", c); 699 if (*++q == ':') 700 q++; 701 } 702 if (*++q == ':') { 703 if (*p == '\0' && (p = *argptr++) == NULL) 704 error("No arg for -%c option", c); 705 optionarg = p; 706 p = NULL; 707 } 708 optptr = p; 709 return c; 710 } 711