1 /*- 2 * Copyright (c) 1980, 1991, 1993 3 * The Regents of the University of California. All rights reserved. 4 * 5 * %sccs.include.redist.c% 6 */ 7 8 #ifndef lint 9 static char copyright[] = 10 "@(#) Copyright (c) 1980, 1991, 1993\n\ 11 The Regents of the University of California. All rights reserved.\n"; 12 #endif /* not lint */ 13 14 #ifndef lint 15 static char sccsid[] = "@(#)csh.c 8.2 (Berkeley) 10/12/93"; 16 #endif /* not lint */ 17 18 #include <sys/types.h> 19 #include <sys/ioctl.h> 20 #include <sys/stat.h> 21 #include <fcntl.h> 22 #include <errno.h> 23 #include <pwd.h> 24 #include <stdlib.h> 25 #include <string.h> 26 #include <locale.h> 27 #include <unistd.h> 28 #include <vis.h> 29 #if __STDC__ 30 # include <stdarg.h> 31 #else 32 # include <varargs.h> 33 #endif 34 35 #include "csh.h" 36 #include "proc.h" 37 #include "extern.h" 38 #include "pathnames.h" 39 40 extern bool MapsAreInited; 41 extern bool NLSMapsAreInited; 42 43 /* 44 * C Shell 45 * 46 * Bill Joy, UC Berkeley, California, USA 47 * October 1978, May 1980 48 * 49 * Jim Kulp, IIASA, Laxenburg, Austria 50 * April 1980 51 * 52 * Christos Zoulas, Cornell University 53 * June, 1991 54 */ 55 56 Char *dumphist[] = {STRhistory, STRmh, 0, 0}; 57 Char *loadhist[] = {STRsource, STRmh, STRtildothist, 0}; 58 59 int nofile = 0; 60 bool reenter = 0; 61 bool nverbose = 0; 62 bool nexececho = 0; 63 bool quitit = 0; 64 bool fast = 0; 65 bool batch = 0; 66 bool mflag = 0; 67 bool prompt = 1; 68 bool enterhist = 0; 69 bool tellwhat = 0; 70 71 extern char **environ; 72 73 static int readf __P((void *, char *, int)); 74 static fpos_t seekf __P((void *, fpos_t, int)); 75 static int writef __P((void *, const char *, int)); 76 static int closef __P((void *)); 77 static int srccat __P((Char *, Char *)); 78 static int srcfile __P((char *, bool, bool)); 79 static void phup __P((int)); 80 static void srcunit __P((int, bool, bool)); 81 static void mailchk __P((void)); 82 static Char **defaultpath __P((void)); 83 84 int 85 main(argc, argv) 86 int argc; 87 char **argv; 88 { 89 register Char *cp; 90 register char *tcp; 91 register int f; 92 register char **tempv; 93 struct sigvec osv; 94 95 cshin = stdin; 96 cshout = stdout; 97 csherr = stderr; 98 99 settimes(); /* Immed. estab. timing base */ 100 101 /* 102 * Initialize non constant strings 103 */ 104 #ifdef _PATH_BSHELL 105 STR_BSHELL = SAVE(_PATH_BSHELL); 106 #endif 107 #ifdef _PATH_CSHELL 108 STR_SHELLPATH = SAVE(_PATH_CSHELL); 109 #endif 110 STR_environ = blk2short(environ); 111 environ = short2blk(STR_environ); /* So that we can free it */ 112 STR_WORD_CHARS = SAVE(WORD_CHARS); 113 114 HIST = '!'; 115 HISTSUB = '^'; 116 word_chars = STR_WORD_CHARS; 117 118 tempv = argv; 119 if (eq(str2short(tempv[0]), STRaout)) /* A.out's are quittable */ 120 quitit = 1; 121 uid = getuid(); 122 gid = getgid(); 123 euid = geteuid(); 124 egid = getegid(); 125 /* 126 * We are a login shell if: 1. we were invoked as -<something> and we had 127 * no arguments 2. or we were invoked only with the -l flag 128 */ 129 loginsh = (**tempv == '-' && argc == 1) || 130 (argc == 2 && tempv[1][0] == '-' && tempv[1][1] == 'l' && 131 tempv[1][2] == '\0'); 132 133 if (loginsh && **tempv != '-') { 134 /* 135 * Mangle the argv space 136 */ 137 tempv[1][0] = '\0'; 138 tempv[1][1] = '\0'; 139 tempv[1] = NULL; 140 for (tcp = *tempv; *tcp++;) 141 continue; 142 for (tcp--; tcp >= *tempv; tcp--) 143 tcp[1] = tcp[0]; 144 *++tcp = '-'; 145 argc--; 146 } 147 if (loginsh) 148 (void) time(&chktim); 149 150 AsciiOnly = 1; 151 #ifdef NLS 152 (void) setlocale(LC_ALL, ""); 153 { 154 int k; 155 156 for (k = 0200; k <= 0377 && !Isprint(k); k++) 157 continue; 158 AsciiOnly = k > 0377; 159 } 160 #else 161 AsciiOnly = getenv("LANG") == NULL && getenv("LC_CTYPE") == NULL; 162 #endif /* NLS */ 163 164 /* 165 * Move the descriptors to safe places. The variable didfds is 0 while we 166 * have only FSH* to work with. When didfds is true, we have 0,1,2 and 167 * prefer to use these. 168 */ 169 initdesc(); 170 /* 171 * XXX: This is to keep programs that use stdio happy. 172 * what we really want is freunopen() .... 173 * Closing cshin cshout and csherr (which are really stdin stdout 174 * and stderr at this point and then reopening them in the same order 175 * gives us again stdin == cshin stdout == cshout and stderr == csherr. 176 * If that was not the case builtins like printf that use stdio 177 * would break. But in any case we could fix that with memcpy and 178 * a bit of pointer manipulation... 179 * Fortunately this is not needed under the current implementation 180 * of stdio. 181 */ 182 (void) fclose(cshin); 183 (void) fclose(cshout); 184 (void) fclose(csherr); 185 if (!(cshin = funopen((void *) &SHIN, readf, writef, seekf, closef))) 186 exit(1); 187 if (!(cshout = funopen((void *) &SHOUT, readf, writef, seekf, closef))) 188 exit(1); 189 if (!(csherr = funopen((void *) &SHERR, readf, writef, seekf, closef))) 190 exit(1); 191 (void) setvbuf(cshin, NULL, _IOLBF, 0); 192 (void) setvbuf(cshout, NULL, _IOLBF, 0); 193 (void) setvbuf(csherr, NULL, _IOLBF, 0); 194 195 /* 196 * Initialize the shell variables. ARGV and PROMPT are initialized later. 197 * STATUS is also munged in several places. CHILD is munged when 198 * forking/waiting 199 */ 200 set(STRstatus, Strsave(STR0)); 201 202 if ((tcp = getenv("HOME")) != NULL) 203 cp = SAVE(tcp); 204 else 205 cp = NULL; 206 207 if (cp == NULL) 208 fast = 1; /* No home -> can't read scripts */ 209 else 210 set(STRhome, cp); 211 dinit(cp); /* dinit thinks that HOME == cwd in a login 212 * shell */ 213 /* 214 * Grab other useful things from the environment. Should we grab 215 * everything?? 216 */ 217 if ((tcp = getenv("LOGNAME")) != NULL || 218 (tcp = getenv("USER")) != NULL) 219 set(STRuser, SAVE(tcp)); 220 if ((tcp = getenv("TERM")) != NULL) 221 set(STRterm, SAVE(tcp)); 222 223 /* 224 * Re-initialize path if set in environment 225 */ 226 if ((tcp = getenv("PATH")) == NULL) 227 set1(STRpath, defaultpath(), &shvhed); 228 else 229 importpath(SAVE(tcp)); 230 231 set(STRshell, Strsave(STR_SHELLPATH)); 232 233 doldol = putn((int) getpid()); /* For $$ */ 234 shtemp = Strspl(STRtmpsh, doldol); /* For << */ 235 236 /* 237 * Record the interrupt states from the parent process. If the parent is 238 * non-interruptible our hand must be forced or we (and our children) won't 239 * be either. Our children inherit termination from our parent. We catch it 240 * only if we are the login shell. 241 */ 242 /* parents interruptibility */ 243 (void) sigvec(SIGINT, NULL, &osv); 244 parintr = (void (*) ()) osv.sv_handler; 245 (void) sigvec(SIGTERM, NULL, &osv); 246 parterm = (void (*) ()) osv.sv_handler; 247 248 if (loginsh) { 249 (void) signal(SIGHUP, phup); /* exit processing on HUP */ 250 (void) signal(SIGXCPU, phup); /* ...and on XCPU */ 251 (void) signal(SIGXFSZ, phup); /* ...and on XFSZ */ 252 } 253 254 /* 255 * Process the arguments. 256 * 257 * Note that processing of -v/-x is actually delayed till after script 258 * processing. 259 * 260 * We set the first character of our name to be '-' if we are a shell 261 * running interruptible commands. Many programs which examine ps'es 262 * use this to filter such shells out. 263 */ 264 argc--, tempv++; 265 while (argc > 0 && (tcp = tempv[0])[0] == '-' && *++tcp != '\0' && !batch) { 266 do 267 switch (*tcp++) { 268 269 case 0: /* - Interruptible, no prompt */ 270 prompt = 0; 271 setintr = 1; 272 nofile = 1; 273 break; 274 275 case 'b': /* -b Next arg is input file */ 276 batch = 1; 277 break; 278 279 case 'c': /* -c Command input from arg */ 280 if (argc == 1) 281 xexit(0); 282 argc--, tempv++; 283 arginp = SAVE(tempv[0]); 284 prompt = 0; 285 nofile = 1; 286 break; 287 288 case 'e': /* -e Exit on any error */ 289 exiterr = 1; 290 break; 291 292 case 'f': /* -f Fast start */ 293 fast = 1; 294 break; 295 296 case 'i': /* -i Interactive, even if !intty */ 297 intact = 1; 298 nofile = 1; 299 break; 300 301 case 'm': /* -m read .cshrc (from su) */ 302 mflag = 1; 303 break; 304 305 case 'n': /* -n Don't execute */ 306 noexec = 1; 307 break; 308 309 case 'q': /* -q (Undoc'd) ... die on quit */ 310 quitit = 1; 311 break; 312 313 case 's': /* -s Read from std input */ 314 nofile = 1; 315 break; 316 317 case 't': /* -t Read one line from input */ 318 onelflg = 2; 319 prompt = 0; 320 nofile = 1; 321 break; 322 323 case 'v': /* -v Echo hist expanded input */ 324 nverbose = 1; /* ... later */ 325 break; 326 327 case 'x': /* -x Echo just before execution */ 328 nexececho = 1; /* ... later */ 329 break; 330 331 case 'V': /* -V Echo hist expanded input */ 332 setNS(STRverbose); /* NOW! */ 333 break; 334 335 case 'X': /* -X Echo just before execution */ 336 setNS(STRecho); /* NOW! */ 337 break; 338 339 } while (*tcp); 340 tempv++, argc--; 341 } 342 343 if (quitit) /* With all due haste, for debugging */ 344 (void) signal(SIGQUIT, SIG_DFL); 345 346 /* 347 * Unless prevented by -, -c, -i, -s, or -t, if there are remaining 348 * arguments the first of them is the name of a shell file from which to 349 * read commands. 350 */ 351 if (nofile == 0 && argc > 0) { 352 nofile = open(tempv[0], O_RDONLY); 353 if (nofile < 0) { 354 child = 1; /* So this doesn't return */ 355 stderror(ERR_SYSTEM, tempv[0], strerror(errno)); 356 } 357 ffile = SAVE(tempv[0]); 358 /* 359 * Replace FSHIN. Handle /dev/std{in,out,err} specially 360 * since once they are closed we cannot open them again. 361 * In that case we use our own saved descriptors 362 */ 363 if ((SHIN = dmove(nofile, FSHIN)) < 0) 364 switch(nofile) { 365 case 0: 366 SHIN = FSHIN; 367 break; 368 case 1: 369 SHIN = FSHOUT; 370 break; 371 case 2: 372 SHIN = FSHERR; 373 break; 374 default: 375 stderror(ERR_SYSTEM, tempv[0], strerror(errno)); 376 break; 377 } 378 (void) ioctl(SHIN, FIOCLEX, NULL); 379 prompt = 0; 380 /* argc not used any more */ tempv++; 381 } 382 383 intty = isatty(SHIN); 384 intty |= intact; 385 if (intty || (intact && isatty(SHOUT))) { 386 if (!batch && (uid != euid || gid != egid)) { 387 errno = EACCES; 388 child = 1; /* So this doesn't return */ 389 stderror(ERR_SYSTEM, "csh", strerror(errno)); 390 } 391 } 392 /* 393 * Decide whether we should play with signals or not. If we are explicitly 394 * told (via -i, or -) or we are a login shell (arg0 starts with -) or the 395 * input and output are both the ttys("csh", or "csh</dev/ttyx>/dev/ttyx") 396 * Note that in only the login shell is it likely that parent may have set 397 * signals to be ignored 398 */ 399 if (loginsh || intact || (intty && isatty(SHOUT))) 400 setintr = 1; 401 settell(); 402 /* 403 * Save the remaining arguments in argv. 404 */ 405 setq(STRargv, blk2short(tempv), &shvhed); 406 407 /* 408 * Set up the prompt. 409 */ 410 if (prompt) { 411 set(STRprompt, Strsave(uid == 0 ? STRsymhash : STRsymcent)); 412 /* that's a meta-questionmark */ 413 set(STRprompt2, Strsave(STRmquestion)); 414 } 415 416 /* 417 * If we are an interactive shell, then start fiddling with the signals; 418 * this is a tricky game. 419 */ 420 shpgrp = getpgrp(); 421 opgrp = tpgrp = -1; 422 if (setintr) { 423 **argv = '-'; 424 if (!quitit) /* Wary! */ 425 (void) signal(SIGQUIT, SIG_IGN); 426 (void) signal(SIGINT, pintr); 427 (void) sigblock(sigmask(SIGINT)); 428 (void) signal(SIGTERM, SIG_IGN); 429 if (quitit == 0 && arginp == 0) { 430 (void) signal(SIGTSTP, SIG_IGN); 431 (void) signal(SIGTTIN, SIG_IGN); 432 (void) signal(SIGTTOU, SIG_IGN); 433 /* 434 * Wait till in foreground, in case someone stupidly runs csh & 435 * dont want to try to grab away the tty. 436 */ 437 if (isatty(FSHERR)) 438 f = FSHERR; 439 else if (isatty(FSHOUT)) 440 f = FSHOUT; 441 else if (isatty(OLDSTD)) 442 f = OLDSTD; 443 else 444 f = -1; 445 retry: 446 if ((tpgrp = tcgetpgrp(f)) != -1) { 447 if (tpgrp != shpgrp) { 448 sig_t old = signal(SIGTTIN, SIG_DFL); 449 (void) kill(0, SIGTTIN); 450 (void) signal(SIGTTIN, old); 451 goto retry; 452 } 453 opgrp = shpgrp; 454 shpgrp = getpid(); 455 tpgrp = shpgrp; 456 /* 457 * Setpgid will fail if we are a session leader and 458 * mypid == mypgrp (POSIX 4.3.3) 459 */ 460 if (opgrp != shpgrp) 461 if (setpgid(0, shpgrp) == -1) 462 goto notty; 463 /* 464 * We do that after we set our process group, to make sure 465 * that the process group belongs to a process in the same 466 * session as the tty (our process and our group) (POSIX 7.2.4) 467 */ 468 if (tcsetpgrp(f, shpgrp) == -1) 469 goto notty; 470 (void) ioctl(dcopy(f, FSHTTY), FIOCLEX, NULL); 471 } 472 if (tpgrp == -1) { 473 notty: 474 (void) fprintf(csherr, "Warning: no access to tty (%s).\n", 475 strerror(errno)); 476 (void) fprintf(csherr, "Thus no job control in this shell.\n"); 477 } 478 } 479 } 480 if ((setintr == 0) && (parintr == SIG_DFL)) 481 setintr = 1; 482 (void) signal(SIGCHLD, pchild); /* while signals not ready */ 483 484 /* 485 * Set an exit here in case of an interrupt or error reading the shell 486 * start-up scripts. 487 */ 488 reenter = setexit(); /* PWP */ 489 haderr = 0; /* In case second time through */ 490 if (!fast && reenter == 0) { 491 /* Will have value(STRhome) here because set fast if don't */ 492 { 493 int osetintr = setintr; 494 sig_t oparintr = parintr; 495 sigset_t omask = sigblock(sigmask(SIGINT)); 496 497 setintr = 0; 498 parintr = SIG_IGN; /* Disable onintr */ 499 #ifdef _PATH_DOTCSHRC 500 (void) srcfile(_PATH_DOTCSHRC, 0, 0); 501 #endif 502 if (!fast && !arginp && !onelflg) 503 dohash(NULL, NULL); 504 #ifdef _PATH_DOTLOGIN 505 if (loginsh) 506 (void) srcfile(_PATH_DOTLOGIN, 0, 0); 507 #endif 508 (void) sigsetmask(omask); 509 setintr = osetintr; 510 parintr = oparintr; 511 } 512 (void) srccat(value(STRhome), STRsldotcshrc); 513 514 if (!fast && !arginp && !onelflg && !havhash) 515 dohash(NULL, NULL); 516 /* 517 * Source history before .login so that it is available in .login 518 */ 519 if ((cp = value(STRhistfile)) != STRNULL) 520 loadhist[2] = cp; 521 dosource(loadhist, NULL); 522 if (loginsh) 523 (void) srccat(value(STRhome), STRsldotlogin); 524 } 525 526 /* 527 * Now are ready for the -v and -x flags 528 */ 529 if (nverbose) 530 setNS(STRverbose); 531 if (nexececho) 532 setNS(STRecho); 533 534 /* 535 * All the rest of the world is inside this call. The argument to process 536 * indicates whether it should catch "error unwinds". Thus if we are a 537 * interactive shell our call here will never return by being blown past on 538 * an error. 539 */ 540 process(setintr); 541 542 /* 543 * Mop-up. 544 */ 545 if (intty) { 546 if (loginsh) { 547 (void) fprintf(cshout, "logout\n"); 548 (void) close(SHIN); 549 child = 1; 550 goodbye(); 551 } 552 else { 553 (void) fprintf(cshout, "exit\n"); 554 } 555 } 556 rechist(); 557 exitstat(); 558 return (0); 559 } 560 561 void 562 untty() 563 { 564 if (tpgrp > 0) { 565 (void) setpgid(0, opgrp); 566 (void) tcsetpgrp(FSHTTY, opgrp); 567 } 568 } 569 570 void 571 importpath(cp) 572 Char *cp; 573 { 574 register int i = 0; 575 register Char *dp; 576 register Char **pv; 577 int c; 578 579 for (dp = cp; *dp; dp++) 580 if (*dp == ':') 581 i++; 582 /* 583 * i+2 where i is the number of colons in the path. There are i+1 584 * directories in the path plus we need room for a zero terminator. 585 */ 586 pv = (Char **) xcalloc((size_t) (i + 2), sizeof(Char **)); 587 dp = cp; 588 i = 0; 589 if (*dp) 590 for (;;) { 591 if ((c = *dp) == ':' || c == 0) { 592 *dp = 0; 593 if ((*cp != '/' || *cp == '\0') && (euid == 0 || uid == 0)) 594 (void) fprintf(csherr, 595 "Warning: imported path contains relative components\n"); 596 pv[i++] = Strsave(*cp ? cp : STRdot); 597 if (c) { 598 cp = dp + 1; 599 *dp = ':'; 600 } 601 else 602 break; 603 } 604 dp++; 605 } 606 pv[i] = 0; 607 set1(STRpath, pv, &shvhed); 608 } 609 610 /* 611 * Source to the file which is the catenation of the argument names. 612 */ 613 static int 614 srccat(cp, dp) 615 Char *cp, *dp; 616 { 617 register Char *ep = Strspl(cp, dp); 618 char *ptr = short2str(ep); 619 620 xfree((ptr_t) ep); 621 return srcfile(ptr, mflag ? 0 : 1, 0); 622 } 623 624 /* 625 * Source to a file putting the file descriptor in a safe place (> 2). 626 */ 627 static int 628 srcfile(f, onlyown, flag) 629 char *f; 630 bool onlyown, flag; 631 { 632 register int unit; 633 634 if ((unit = open(f, O_RDONLY)) == -1) 635 return 0; 636 unit = dmove(unit, -1); 637 638 (void) ioctl(unit, FIOCLEX, NULL); 639 srcunit(unit, onlyown, flag); 640 return 1; 641 } 642 643 /* 644 * Source to a unit. If onlyown it must be our file or our group or 645 * we don't chance it. This occurs on ".cshrc"s and the like. 646 */ 647 int insource; 648 static void 649 srcunit(unit, onlyown, hflg) 650 register int unit; 651 bool onlyown, hflg; 652 { 653 /* We have to push down a lot of state here */ 654 /* All this could go into a structure */ 655 int oSHIN = -1, oldintty = intty, oinsource = insource; 656 struct whyle *oldwhyl = whyles; 657 Char *ogointr = gointr, *oarginp = arginp; 658 Char *oevalp = evalp, **oevalvec = evalvec; 659 int oonelflg = onelflg; 660 bool oenterhist = enterhist; 661 char OHIST = HIST; 662 bool otell = cantell; 663 664 struct Bin saveB; 665 volatile sigset_t omask; 666 jmp_buf oldexit; 667 668 /* The (few) real local variables */ 669 int my_reenter; 670 671 if (unit < 0) 672 return; 673 if (didfds) 674 donefds(); 675 if (onlyown) { 676 struct stat stb; 677 678 if (fstat(unit, &stb) < 0) { 679 (void) close(unit); 680 return; 681 } 682 } 683 684 /* 685 * There is a critical section here while we are pushing down the input 686 * stream since we have stuff in different structures. If we weren't 687 * careful an interrupt could corrupt SHIN's Bin structure and kill the 688 * shell. 689 * 690 * We could avoid the critical region by grouping all the stuff in a single 691 * structure and pointing at it to move it all at once. This is less 692 * efficient globally on many variable references however. 693 */ 694 insource = 1; 695 getexit(oldexit); 696 omask = 0; 697 698 if (setintr) 699 omask = sigblock(sigmask(SIGINT)); 700 /* Setup the new values of the state stuff saved above */ 701 bcopy((char *) &B, (char *) &(saveB), sizeof(B)); 702 fbuf = NULL; 703 fseekp = feobp = fblocks = 0; 704 oSHIN = SHIN, SHIN = unit, arginp = 0, onelflg = 0; 705 intty = isatty(SHIN), whyles = 0, gointr = 0; 706 evalvec = 0; 707 evalp = 0; 708 enterhist = hflg; 709 if (enterhist) 710 HIST = '\0'; 711 712 /* 713 * Now if we are allowing commands to be interrupted, we let ourselves be 714 * interrupted. 715 */ 716 if (setintr) 717 (void) sigsetmask(omask); 718 settell(); 719 720 if ((my_reenter = setexit()) == 0) 721 process(0); /* 0 -> blow away on errors */ 722 723 if (setintr) 724 (void) sigsetmask(omask); 725 if (oSHIN >= 0) { 726 register int i; 727 728 /* We made it to the new state... free up its storage */ 729 /* This code could get run twice but xfree doesn't care */ 730 for (i = 0; i < fblocks; i++) 731 xfree((ptr_t) fbuf[i]); 732 xfree((ptr_t) fbuf); 733 734 /* Reset input arena */ 735 bcopy((char *) &(saveB), (char *) &B, sizeof(B)); 736 737 (void) close(SHIN), SHIN = oSHIN; 738 arginp = oarginp, onelflg = oonelflg; 739 evalp = oevalp, evalvec = oevalvec; 740 intty = oldintty, whyles = oldwhyl, gointr = ogointr; 741 if (enterhist) 742 HIST = OHIST; 743 enterhist = oenterhist; 744 cantell = otell; 745 } 746 747 resexit(oldexit); 748 /* 749 * If process reset() (effectively an unwind) then we must also unwind. 750 */ 751 if (my_reenter) 752 stderror(ERR_SILENT); 753 insource = oinsource; 754 } 755 756 void 757 rechist() 758 { 759 Char buf[BUFSIZ], hbuf[BUFSIZ], *hfile; 760 int fp, ftmp, oldidfds; 761 struct varent *shist; 762 763 if (!fast) { 764 /* 765 * If $savehist is just set, we use the value of $history 766 * else we use the value in $savehist 767 */ 768 if ((shist = adrof(STRsavehist)) != NULL) { 769 if (shist->vec[0][0] != '\0') 770 (void) Strcpy(hbuf, shist->vec[0]); 771 else if ((shist = adrof(STRhistory)) && shist->vec[0][0] != '\0') 772 (void) Strcpy(hbuf, shist->vec[0]); 773 else 774 return; 775 } 776 else 777 return; 778 779 if ((hfile = value(STRhistfile)) == STRNULL) { 780 hfile = Strcpy(buf, value(STRhome)); 781 (void) Strcat(buf, STRsldthist); 782 } 783 784 if ((fp = creat(short2str(hfile), 0600)) == -1) 785 return; 786 787 oldidfds = didfds; 788 didfds = 0; 789 ftmp = SHOUT; 790 SHOUT = fp; 791 dumphist[2] = hbuf; 792 dohist(dumphist, NULL); 793 SHOUT = ftmp; 794 (void) close(fp); 795 didfds = oldidfds; 796 } 797 } 798 799 void 800 goodbye() 801 { 802 rechist(); 803 804 if (loginsh) { 805 (void) signal(SIGQUIT, SIG_IGN); 806 (void) signal(SIGINT, SIG_IGN); 807 (void) signal(SIGTERM, SIG_IGN); 808 setintr = 0; /* No interrupts after "logout" */ 809 if (!(adrof(STRlogout))) 810 set(STRlogout, STRnormal); 811 #ifdef _PATH_DOTLOGOUT 812 (void) srcfile(_PATH_DOTLOGOUT, 0, 0); 813 #endif 814 if (adrof(STRhome)) 815 (void) srccat(value(STRhome), STRsldtlogout); 816 } 817 exitstat(); 818 } 819 820 void 821 exitstat() 822 { 823 Char *s; 824 #ifdef PROF 825 monitor(0); 826 #endif 827 /* 828 * Note that if STATUS is corrupted (i.e. getn bombs) then error will exit 829 * directly because we poke child here. Otherwise we might continue 830 * unwarrantedly (sic). 831 */ 832 child = 1; 833 s = value(STRstatus); 834 xexit(s ? getn(s) : 0); 835 } 836 837 /* 838 * in the event of a HUP we want to save the history 839 */ 840 static void 841 phup(sig) 842 int sig; 843 { 844 rechist(); 845 846 /* 847 * We kill the last foreground process group. It then becomes 848 * responsible to propagate the SIGHUP to its progeny. 849 */ 850 { 851 struct process *pp, *np; 852 853 for (pp = proclist.p_next; pp; pp = pp->p_next) { 854 np = pp; 855 /* 856 * Find if this job is in the foreground. It could be that 857 * the process leader has exited and the foreground flag 858 * is cleared for it. 859 */ 860 do 861 /* 862 * If a process is in the foreground; we try to kill 863 * it's process group. If we succeed, then the 864 * whole job is gone. Otherwise we keep going... 865 * But avoid sending HUP to the shell again. 866 */ 867 if ((np->p_flags & PFOREGND) != 0 && np->p_jobid != shpgrp && 868 killpg(np->p_jobid, SIGHUP) != -1) { 869 /* In case the job was suspended... */ 870 (void) killpg(np->p_jobid, SIGCONT); 871 break; 872 } 873 while ((np = np->p_friends) != pp); 874 } 875 } 876 _exit(sig); 877 } 878 879 Char *jobargv[2] = {STRjobs, 0}; 880 881 /* 882 * Catch an interrupt, e.g. during lexical input. 883 * If we are an interactive shell, we reset the interrupt catch 884 * immediately. In any case we drain the shell output, 885 * and finally go through the normal error mechanism, which 886 * gets a chance to make the shell go away. 887 */ 888 /* ARGSUSED */ 889 void 890 pintr(notused) 891 int notused; 892 { 893 pintr1(1); 894 } 895 896 void 897 pintr1(wantnl) 898 bool wantnl; 899 { 900 Char **v; 901 sigset_t omask; 902 903 omask = sigblock((sigset_t) 0); 904 if (setintr) { 905 (void) sigsetmask(omask & ~sigmask(SIGINT)); 906 if (pjobs) { 907 pjobs = 0; 908 (void) fprintf(cshout, "\n"); 909 dojobs(jobargv, NULL); 910 stderror(ERR_NAME | ERR_INTR); 911 } 912 } 913 (void) sigsetmask(omask & ~sigmask(SIGCHLD)); 914 (void) fpurge(cshout); 915 (void) endpwent(); 916 917 /* 918 * If we have an active "onintr" then we search for the label. Note that if 919 * one does "onintr -" then we shan't be interruptible so we needn't worry 920 * about that here. 921 */ 922 if (gointr) { 923 gotolab(gointr); 924 timflg = 0; 925 if ((v = pargv) != NULL) 926 pargv = 0, blkfree(v); 927 if ((v = gargv) != NULL) 928 gargv = 0, blkfree(v); 929 reset(); 930 } 931 else if (intty && wantnl) { 932 (void) fputc('\r', cshout); 933 (void) fputc('\n', cshout); 934 } 935 stderror(ERR_SILENT); 936 } 937 938 /* 939 * Process is the main driving routine for the shell. 940 * It runs all command processing, except for those within { ... } 941 * in expressions (which is run by a routine evalav in sh.exp.c which 942 * is a stripped down process), and `...` evaluation which is run 943 * also by a subset of this code in sh.glob.c in the routine backeval. 944 * 945 * The code here is a little strange because part of it is interruptible 946 * and hence freeing of structures appears to occur when none is necessary 947 * if this is ignored. 948 * 949 * Note that if catch is not set then we will unwind on any error. 950 * If an end-of-file occurs, we return. 951 */ 952 static struct command *savet = NULL; 953 void 954 process(catch) 955 bool catch; 956 { 957 jmp_buf osetexit; 958 struct command *t = savet; 959 960 savet = NULL; 961 getexit(osetexit); 962 for (;;) { 963 pendjob(); 964 paraml.next = paraml.prev = ¶ml; 965 paraml.word = STRNULL; 966 (void) setexit(); 967 justpr = enterhist; /* execute if not entering history */ 968 969 /* 970 * Interruptible during interactive reads 971 */ 972 if (setintr) 973 (void) sigsetmask(sigblock((sigset_t) 0) & ~sigmask(SIGINT)); 974 975 /* 976 * For the sake of reset() 977 */ 978 freelex(¶ml); 979 if (savet) 980 freesyn(savet), savet = NULL; 981 982 if (haderr) { 983 if (!catch) { 984 /* unwind */ 985 doneinp = 0; 986 resexit(osetexit); 987 savet = t; 988 reset(); 989 } 990 haderr = 0; 991 /* 992 * Every error is eventually caught here or the shell dies. It is 993 * at this point that we clean up any left-over open files, by 994 * closing all but a fixed number of pre-defined files. Thus 995 * routines don't have to worry about leaving files open due to 996 * deeper errors... they will get closed here. 997 */ 998 closem(); 999 continue; 1000 } 1001 if (doneinp) { 1002 doneinp = 0; 1003 break; 1004 } 1005 if (chkstop) 1006 chkstop--; 1007 if (neednote) 1008 pnote(); 1009 if (intty && prompt && evalvec == 0) { 1010 mailchk(); 1011 /* 1012 * If we are at the end of the input buffer then we are going to 1013 * read fresh stuff. Otherwise, we are rereading input and don't 1014 * need or want to prompt. 1015 */ 1016 if (aret == F_SEEK && fseekp == feobp) 1017 printprompt(); 1018 (void) fflush(cshout); 1019 } 1020 if (seterr) { 1021 xfree((ptr_t) seterr); 1022 seterr = NULL; 1023 } 1024 1025 /* 1026 * Echo not only on VERBOSE, but also with history expansion. If there 1027 * is a lexical error then we forego history echo. 1028 */ 1029 if ((lex(¶ml) && !seterr && intty) || adrof(STRverbose)) { 1030 prlex(csherr, ¶ml); 1031 } 1032 1033 /* 1034 * The parser may lose space if interrupted. 1035 */ 1036 if (setintr) 1037 (void) sigblock(sigmask(SIGINT)); 1038 1039 /* 1040 * Save input text on the history list if reading in old history, or it 1041 * is from the terminal at the top level and not in a loop. 1042 * 1043 * PWP: entry of items in the history list while in a while loop is done 1044 * elsewhere... 1045 */ 1046 if (enterhist || (catch && intty && !whyles)) 1047 savehist(¶ml); 1048 1049 /* 1050 * Print lexical error messages, except when sourcing history lists. 1051 */ 1052 if (!enterhist && seterr) 1053 stderror(ERR_OLD); 1054 1055 /* 1056 * If had a history command :p modifier then this is as far as we 1057 * should go 1058 */ 1059 if (justpr) 1060 reset(); 1061 1062 alias(¶ml); 1063 1064 /* 1065 * Parse the words of the input into a parse tree. 1066 */ 1067 savet = syntax(paraml.next, ¶ml, 0); 1068 if (seterr) 1069 stderror(ERR_OLD); 1070 1071 execute(savet, (tpgrp > 0 ? tpgrp : -1), NULL, NULL); 1072 1073 /* 1074 * Made it! 1075 */ 1076 freelex(¶ml); 1077 freesyn((struct command *) savet), savet = NULL; 1078 } 1079 resexit(osetexit); 1080 savet = t; 1081 } 1082 1083 void 1084 /*ARGSUSED*/ 1085 dosource(v, t) 1086 Char **v; 1087 struct command *t; 1088 1089 { 1090 register Char *f; 1091 bool hflg = 0; 1092 Char buf[BUFSIZ]; 1093 1094 v++; 1095 if (*v && eq(*v, STRmh)) { 1096 if (*++v == NULL) 1097 stderror(ERR_NAME | ERR_HFLAG); 1098 hflg++; 1099 } 1100 (void) Strcpy(buf, *v); 1101 f = globone(buf, G_ERROR); 1102 (void) strcpy((char *) buf, short2str(f)); 1103 xfree((ptr_t) f); 1104 if (!srcfile((char *) buf, 0, hflg) && !hflg) 1105 stderror(ERR_SYSTEM, (char *) buf, strerror(errno)); 1106 } 1107 1108 /* 1109 * Check for mail. 1110 * If we are a login shell, then we don't want to tell 1111 * about any mail file unless its been modified 1112 * after the time we started. 1113 * This prevents us from telling the user things he already 1114 * knows, since the login program insists on saying 1115 * "You have mail." 1116 */ 1117 static void 1118 mailchk() 1119 { 1120 register struct varent *v; 1121 register Char **vp; 1122 time_t t; 1123 int intvl, cnt; 1124 struct stat stb; 1125 bool new; 1126 1127 v = adrof(STRmail); 1128 if (v == 0) 1129 return; 1130 (void) time(&t); 1131 vp = v->vec; 1132 cnt = blklen(vp); 1133 intvl = (cnt && number(*vp)) ? (--cnt, getn(*vp++)) : MAILINTVL; 1134 if (intvl < 1) 1135 intvl = 1; 1136 if (chktim + intvl > t) 1137 return; 1138 for (; *vp; vp++) { 1139 if (stat(short2str(*vp), &stb) < 0) 1140 continue; 1141 new = stb.st_mtime > time0.tv_sec; 1142 if (stb.st_size == 0 || stb.st_atime > stb.st_mtime || 1143 (stb.st_atime < chktim && stb.st_mtime < chktim) || 1144 (loginsh && !new)) 1145 continue; 1146 if (cnt == 1) 1147 (void) fprintf(cshout, "You have %smail.\n", new ? "new " : ""); 1148 else 1149 (void) fprintf(cshout, "%s in %s.\n", new ? "New mail" : "Mail", 1150 vis_str(*vp)); 1151 } 1152 chktim = t; 1153 } 1154 1155 /* 1156 * Extract a home directory from the password file 1157 * The argument points to a buffer where the name of the 1158 * user whose home directory is sought is currently. 1159 * We write the home directory of the user back there. 1160 */ 1161 int 1162 gethdir(home) 1163 Char *home; 1164 { 1165 Char *h; 1166 struct passwd *pw; 1167 1168 /* 1169 * Is it us? 1170 */ 1171 if (*home == '\0') { 1172 if ((h = value(STRhome)) != NULL) { 1173 (void) Strcpy(home, h); 1174 return 0; 1175 } 1176 else 1177 return 1; 1178 } 1179 1180 if ((pw = getpwnam(short2str(home))) != NULL) { 1181 (void) Strcpy(home, str2short(pw->pw_dir)); 1182 return 0; 1183 } 1184 else 1185 return 1; 1186 } 1187 1188 /* 1189 * When didfds is set, we do I/O from 0, 1, 2 otherwise from 15, 16, 17 1190 * We also check if the shell has already changed the decriptor to point to 1191 * 0, 1, 2 when didfds is set. 1192 */ 1193 #define DESC(a) (*((int *) (a)) - (didfds && *((int *) a) >= FSHIN ? FSHIN : 0)) 1194 1195 static int 1196 readf(oreo, buf, siz) 1197 void *oreo; 1198 char *buf; 1199 int siz; 1200 { 1201 return read(DESC(oreo), buf, siz); 1202 } 1203 1204 1205 static int 1206 writef(oreo, buf, siz) 1207 void *oreo; 1208 const char *buf; 1209 int siz; 1210 { 1211 return write(DESC(oreo), buf, siz); 1212 } 1213 1214 static fpos_t 1215 seekf(oreo, off, whence) 1216 void *oreo; 1217 fpos_t off; 1218 int whence; 1219 { 1220 return lseek(DESC(oreo), off, whence); 1221 } 1222 1223 1224 static int 1225 closef(oreo) 1226 void *oreo; 1227 { 1228 return close(DESC(oreo)); 1229 } 1230 1231 1232 /* 1233 * Print the visible version of a string. 1234 */ 1235 int 1236 vis_fputc(ch, fp) 1237 int ch; 1238 FILE *fp; 1239 { 1240 char uenc[5]; /* 4 + NULL */ 1241 1242 if (ch & QUOTE) 1243 return fputc(ch & TRIM, fp); 1244 /* 1245 * XXX: When we are in AsciiOnly we want all characters >= 0200 to 1246 * be encoded, but currently there is no way in vis to do that. 1247 */ 1248 (void) vis(uenc, ch & TRIM, VIS_NOSLASH, 0); 1249 return fputs(uenc, fp); 1250 } 1251 1252 /* 1253 * Move the initial descriptors to their eventual 1254 * resting places, closin all other units. 1255 */ 1256 void 1257 initdesc() 1258 { 1259 1260 didfds = 0; /* 0, 1, 2 aren't set up */ 1261 (void) ioctl(SHIN = dcopy(0, FSHIN), FIOCLEX, NULL); 1262 (void) ioctl(SHOUT = dcopy(1, FSHOUT), FIOCLEX, NULL); 1263 (void) ioctl(SHERR = dcopy(2, FSHERR), FIOCLEX, NULL); 1264 (void) ioctl(OLDSTD = dcopy(SHIN, FOLDSTD), FIOCLEX, NULL); 1265 closem(); 1266 } 1267 1268 1269 void 1270 #ifdef PROF 1271 done(i) 1272 #else 1273 xexit(i) 1274 #endif 1275 int i; 1276 { 1277 untty(); 1278 _exit(i); 1279 } 1280 1281 static Char ** 1282 defaultpath() 1283 { 1284 char *ptr; 1285 Char **blk, **blkp; 1286 struct stat stb; 1287 1288 blkp = blk = (Char **) xmalloc((size_t) sizeof(Char *) * 10); 1289 1290 #define DIRAPPEND(a) \ 1291 if (stat(ptr = a, &stb) == 0 && (stb.st_mode & S_IFMT) == S_IFDIR) \ 1292 *blkp++ = SAVE(ptr) 1293 1294 DIRAPPEND(_PATH_BIN); 1295 DIRAPPEND(_PATH_USRBIN); 1296 1297 #undef DIRAPPEND 1298 1299 if (euid != 0 && uid != 0) 1300 *blkp++ = Strsave(STRdot); 1301 *blkp = NULL; 1302 return (blk); 1303 } 1304 1305 void 1306 printprompt() 1307 { 1308 register Char *cp; 1309 1310 if (!whyles) { 1311 for (cp = value(STRprompt); *cp; cp++) 1312 if (*cp == HIST) 1313 (void) fprintf(cshout, "%d", eventno + 1); 1314 else { 1315 if (*cp == '\\' && cp[1] == HIST) 1316 cp++; 1317 (void) vis_fputc(*cp | QUOTE, cshout); 1318 } 1319 } 1320 else 1321 /* 1322 * Prompt for forward reading loop body content. 1323 */ 1324 (void) fprintf(cshout, "? "); 1325 (void) fflush(cshout); 1326 } 1327