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: options.c,v 1.12 1997/02/22 13:58:40 peter Exp $ 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 && *argptr) 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 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 } 290 291 292 /* 293 * Free the list of positional parameters. 294 */ 295 296 void 297 freeparam(param) 298 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(argc, argv) 317 int argc; 318 char **argv; 319 { 320 int n; 321 char **ap1, **ap2; 322 323 n = 1; 324 if (argc > 1) 325 n = number(argv[1]); 326 if (n > shellparam.nparam) 327 error("can't shift that many"); 328 INTOFF; 329 shellparam.nparam -= n; 330 for (ap1 = shellparam.p ; --n >= 0 ; ap1++) { 331 if (shellparam.malloc) 332 ckfree(*ap1); 333 } 334 ap2 = shellparam.p; 335 while ((*ap2++ = *ap1++) != NULL); 336 shellparam.optnext = NULL; 337 INTON; 338 return 0; 339 } 340 341 342 343 /* 344 * The set command builtin. 345 */ 346 347 int 348 setcmd(argc, argv) 349 int argc; 350 char **argv; 351 { 352 if (argc == 1) 353 return showvarscmd(argc, argv); 354 INTOFF; 355 options(0); 356 optschanged(); 357 if (*argptr != NULL) { 358 setparam(argptr); 359 } 360 INTON; 361 return 0; 362 } 363 364 365 void 366 getoptsreset(value) 367 const char *value; 368 { 369 if (number(value) == 1) { 370 shellparam.optnext = NULL; 371 shellparam.reset = 1; 372 } 373 } 374 375 /* 376 * The getopts builtin. Shellparam.optnext points to the next argument 377 * to be processed. Shellparam.optptr points to the next character to 378 * be processed in the current argument. If shellparam.optnext is NULL, 379 * then it's the first time getopts has been called. 380 */ 381 382 int 383 getoptscmd(argc, argv) 384 int argc; 385 char **argv; 386 { 387 char **optbase = NULL; 388 389 if (argc < 3) 390 error("Usage: getopts optstring var [arg]"); 391 else if (argc == 3) 392 optbase = shellparam.p; 393 else 394 optbase = &argv[3]; 395 396 if (shellparam.reset == 1) { 397 shellparam.optnext = optbase; 398 shellparam.optptr = NULL; 399 shellparam.reset = 0; 400 } 401 402 return getopts(argv[1], argv[2], optbase, &shellparam.optnext, 403 &shellparam.optptr); 404 } 405 406 STATIC int 407 getopts(optstr, optvar, optfirst, optnext, optptr) 408 char *optstr; 409 char *optvar; 410 char **optfirst; 411 char ***optnext; 412 char **optptr; 413 { 414 char *p, *q; 415 char c = '?'; 416 int done = 0; 417 int ind = 0; 418 int err = 0; 419 char s[10]; 420 421 if ((p = *optptr) == NULL || *p == '\0') { 422 /* Current word is done, advance */ 423 if (*optnext == NULL) 424 return 1; 425 p = **optnext; 426 if (p == NULL || *p != '-' || *++p == '\0') { 427 atend: 428 ind = *optnext - optfirst + 1; 429 *optnext = NULL; 430 p = NULL; 431 done = 1; 432 goto out; 433 } 434 (*optnext)++; 435 if (p[0] == '-' && p[1] == '\0') /* check for "--" */ 436 goto atend; 437 } 438 439 c = *p++; 440 for (q = optstr; *q != c; ) { 441 if (*q == '\0') { 442 if (optstr[0] == ':') { 443 s[0] = c; 444 s[1] = '\0'; 445 err |= setvarsafe("OPTARG", s, 0); 446 } 447 else { 448 out1fmt("Illegal option -%c\n", c); 449 (void) unsetvar("OPTARG"); 450 } 451 c = '?'; 452 goto bad; 453 } 454 if (*++q == ':') 455 q++; 456 } 457 458 if (*++q == ':') { 459 if (*p == '\0' && (p = **optnext) == NULL) { 460 if (optstr[0] == ':') { 461 s[0] = c; 462 s[1] = '\0'; 463 err |= setvarsafe("OPTARG", s, 0); 464 c = ':'; 465 } 466 else { 467 out1fmt("No arg for -%c option\n", c); 468 (void) unsetvar("OPTARG"); 469 c = '?'; 470 } 471 goto bad; 472 } 473 474 if (p == **optnext) 475 (*optnext)++; 476 setvarsafe("OPTARG", p, 0); 477 p = NULL; 478 } 479 else 480 setvarsafe("OPTARG", "", 0); 481 ind = *optnext - optfirst + 1; 482 goto out; 483 484 bad: 485 ind = 1; 486 *optnext = NULL; 487 p = NULL; 488 out: 489 *optptr = p; 490 fmtstr(s, sizeof(s), "%d", ind); 491 err |= setvarsafe("OPTIND", s, VNOFUNC); 492 s[0] = c; 493 s[1] = '\0'; 494 err |= setvarsafe(optvar, s, 0); 495 if (err) { 496 *optnext = NULL; 497 *optptr = NULL; 498 flushall(); 499 exraise(EXERROR); 500 } 501 return done; 502 } 503 504 /* 505 * XXX - should get rid of. have all builtins use getopt(3). the 506 * library getopt must have the BSD extension static variable "optreset" 507 * otherwise it can't be used within the shell safely. 508 * 509 * Standard option processing (a la getopt) for builtin routines. The 510 * only argument that is passed to nextopt is the option string; the 511 * other arguments are unnecessary. It return the character, or '\0' on 512 * end of input. 513 */ 514 515 int 516 nextopt(optstring) 517 char *optstring; 518 { 519 char *p, *q; 520 char c; 521 522 if ((p = optptr) == NULL || *p == '\0') { 523 p = *argptr; 524 if (p == NULL || *p != '-' || *++p == '\0') 525 return '\0'; 526 argptr++; 527 if (p[0] == '-' && p[1] == '\0') /* check for "--" */ 528 return '\0'; 529 } 530 c = *p++; 531 for (q = optstring ; *q != c ; ) { 532 if (*q == '\0') 533 error("Illegal option -%c", c); 534 if (*++q == ':') 535 q++; 536 } 537 if (*++q == ':') { 538 if (*p == '\0' && (p = *argptr++) == NULL) 539 error("No arg for -%c option", c); 540 optarg = p; 541 p = NULL; 542 } 543 optptr = p; 544 return c; 545 } 546