use new signal calls instead of compatibility routines.
[unix-history] / usr / src / bin / csh / csh.c
CommitLineData
d33af40e 1static char *sccsid = "@(#)csh.c 4.18 %G%";
04d3b78c
BJ
2
3#include "sh.h"
4#include <sys/ioctl.h>
5/*
6 * C Shell
7 *
8 * Bill Joy, UC Berkeley, California, USA
9 * October 1978, May 1980
10 *
11 * Jim Kulp, IIASA, Laxenburg, Austria
12 * April 1980
13 */
14
15char *pathlist[] = { ".", "/usr/ucb", "/bin", "/usr/bin", 0 };
6ea8dfee 16char *dumphist[] = { "history", "-h", 0, 0 };
d7929fa7 17char *loadhist[] = { "source", "-h", "~/.history", 0 };
04d3b78c
BJ
18char HIST = '!';
19char HISTSUB = '^';
20bool nofile;
21bool reenter;
22bool nverbose;
23bool nexececho;
24bool quitit;
25bool fast;
26bool prompt = 1;
63af20f7 27bool enterhist = 0;
04d3b78c
BJ
28
29main(c, av)
30 int c;
31 char **av;
32{
33 register char **v, *cp;
34 register int f;
d33af40e 35 struct sigvec osv;
04d3b78c
BJ
36
37 settimes(); /* Immed. estab. timing base */
38 v = av;
39 if (eq(v[0], "a.out")) /* A.out's are quittable */
40 quitit = 1;
41 uid = getuid();
d33af40e 42 loginsh = **v == '-' && c == 1;
04d3b78c
BJ
43 if (loginsh)
44 time(&chktim);
45
46 /*
47 * Move the descriptors to safe places.
48 * The variable didfds is 0 while we have only FSH* to work with.
49 * When didfds is true, we have 0,1,2 and prefer to use these.
50 */
51 initdesc();
52
53 /*
54 * Initialize the shell variables.
55 * ARGV and PROMPT are initialized later.
56 * STATUS is also munged in several places.
57 * CHILD is munged when forking/waiting
58 */
59
60 set("status", "0");
61 dinit(cp = getenv("HOME")); /* dinit thinks that HOME == cwd in a
62 * login shell */
63 if (cp == NOSTR)
64 fast++; /* No home -> can't read scripts */
65 else
66 set("home", savestr(cp));
67 /*
68 * Grab other useful things from the environment.
69 * Should we grab everything??
70 */
71 if ((cp = getenv("USER")) != NOSTR)
72 set("user", savestr(cp));
73 if ((cp = getenv("TERM")) != NOSTR)
74 set("term", savestr(cp));
75 /*
76 * Re-initialize path if set in environment
77 */
78 if ((cp = getenv("PATH")) == NOSTR)
79 set1("path", saveblk(pathlist), &shvhed);
80 else {
81 register unsigned i = 0;
82 register char *dp;
83 register char **pv;
84
85 for (dp = cp; *dp; dp++)
86 if (*dp == ':')
87 i++;
88 pv = (char **)calloc(i+2, sizeof (char **));
89 for (dp = cp, i = 0; ;)
90 if (*dp == ':') {
91 *dp = 0;
92 pv[i++] = savestr(*cp ? cp : ".");
93 *dp++ = ':';
94 cp = dp;
95 } else if (*dp++ == 0) {
96 pv[i++] = savestr(*cp ? cp : ".");
97 break;
98 }
99 pv[i] = 0;
100 set1("path", pv, &shvhed);
101 }
102 set("shell", SHELLPATH);
103
104 doldol = putn(getpid()); /* For $$ */
105 shtemp = strspl("/tmp/sh", doldol); /* For << */
106
107 /*
108 * Record the interrupt states from the parent process.
109 * If the parent is non-interruptible our hand must be forced
110 * or we (and our children) won't be either.
111 * Our children inherit termination from our parent.
112 * We catch it only if we are the login shell.
113 */
d33af40e
RC
114 sigvec(SIGINT, 0, &osv); /* parents interruptibility */
115 parintr = osv.sv_handler;
116 sigvec(SIGTERM, 0, &osv); /* parents terminability */
117 parterm = osv.sv_handler;
f66d6be5 118 if (loginsh) {
d7929fa7 119 signal(SIGHUP, phup); /* exit processing on HUP */
f66d6be5
KM
120 signal(SIGXCPU, phup); /* ...and on XCPU */
121 signal(SIGXFSZ, phup); /* ...and on XFSZ */
122 }
04d3b78c
BJ
123
124 /*
125 * Process the arguments.
126 *
127 * Note that processing of -v/-x is actually delayed till after
128 * script processing.
129 *
130 * We set the first character of our name to be '-' if we are
131 * a shell running interruptible commands. Many programs which
132 * examine ps'es use this to filter such shells out.
133 */
134 c--, v++;
135 while (c > 0 && (cp = v[0])[0] == '-') {
d33af40e 136 cp++;
04d3b78c
BJ
137 do switch (*cp++) {
138
04d3b78c
BJ
139 case 'c': /* -c Command input from arg */
140 if (c == 1)
141 exit(0);
142 c--, v++;
143 arginp = v[0];
144 prompt = 0;
145 nofile++;
146 break;
147
148 case 'e': /* -e Exit on any error */
149 exiterr++;
150 break;
151
152 case 'f': /* -f Fast start */
153 fast++;
154 break;
155
156 case 'i': /* -i Interactive, even if !intty */
157 intact++;
158 nofile++;
159 break;
160
161 case 'n': /* -n Don't execute */
162 noexec++;
163 break;
164
165 case 'q': /* -q (Undoc'd) ... die on quit */
166 quitit = 1;
167 break;
168
169 case 's': /* -s Read from std input */
170 nofile++;
171 break;
172
173 case 't': /* -t Read one line from input */
174 onelflg = 2;
175 prompt = 0;
176 nofile++;
177 break;
178
179 case 'v': /* -v Echo hist expanded input */
180 nverbose = 1; /* ... later */
181 break;
182
183 case 'x': /* -x Echo just before execution */
184 nexececho = 1; /* ... later */
185 break;
186
187 case 'V': /* -V Echo hist expanded input */
188 setNS("verbose"); /* NOW! */
189 break;
190
191 case 'X': /* -X Echo just before execution */
192 setNS("echo"); /* NOW! */
193 break;
194
195 } while (*cp);
196 v++, c--;
197 }
198
199 if (quitit) /* With all due haste, for debugging */
200 signal(SIGQUIT, SIG_DFL);
201
202 /*
203 * Unless prevented by -, -c, -i, -s, or -t, if there
204 * are remaining arguments the first of them is the name
205 * of a shell file from which to read commands.
206 */
207 if (nofile == 0 && c > 0) {
208 nofile = open(v[0], 0);
209 if (nofile < 0) {
210 child++; /* So this ... */
211 Perror(v[0]); /* ... doesn't return */
212 }
213 file = v[0];
214 SHIN = dmove(nofile, FSHIN); /* Replace FSHIN */
215 prompt = 0;
216 c--, v++;
217 }
218 /*
219 * Consider input a tty if it really is or we are interactive.
220 */
221 intty = intact || isatty(SHIN);
222 /*
223 * Decide whether we should play with signals or not.
224 * If we are explicitly told (via -i, or -) or we are a login
225 * shell (arg0 starts with -) or the input and output are both
226 * the ttys("csh", or "csh</dev/ttyx>/dev/ttyx")
227 * Note that in only the login shell is it likely that parent
228 * may have set signals to be ignored
229 */
3b1a0a1e 230 if (loginsh || intact || intty && isatty(SHOUT))
04d3b78c
BJ
231 setintr = 1;
232#ifdef TELL
233 settell();
234#endif
235 /*
236 * Save the remaining arguments in argv.
237 */
238 setq("argv", v, &shvhed);
239
240 /*
241 * Set up the prompt.
242 */
243 if (prompt)
244 set("prompt", uid == 0 ? "# " : "% ");
245
246 /*
247 * If we are an interactive shell, then start fiddling
248 * with the signals; this is a tricky game.
249 */
250 shpgrp = getpgrp(0);
251 opgrp = tpgrp = -1;
252 oldisc = -1;
253 if (setintr) {
254 **av = '-';
255 if (!quitit) /* Wary! */
256 signal(SIGQUIT, SIG_IGN);
d33af40e
RC
257 signal(SIGINT, pintr);
258 sigblock(sigmask(SIGINT));
04d3b78c
BJ
259 signal(SIGTERM, SIG_IGN);
260 if (quitit == 0 && arginp == 0) {
261 signal(SIGTSTP, SIG_IGN);
262 signal(SIGTTIN, SIG_IGN);
263 signal(SIGTTOU, SIG_IGN);
264 /*
265 * Wait till in foreground, in case someone
266 * stupidly runs
267 * csh &
268 * dont want to try to grab away the tty.
269 */
270 if (isatty(FSHDIAG))
271 f = FSHDIAG;
272 else if (isatty(FSHOUT))
273 f = FSHOUT;
274 else if (isatty(OLDSTD))
275 f = OLDSTD;
276 else
277 f = -1;
278retry:
279 if (ioctl(f, TIOCGPGRP, &tpgrp) == 0 && tpgrp != -1) {
280 int ldisc;
281 if (tpgrp != shpgrp) {
d33af40e 282 int (*old)() = signal(SIGTTIN, SIG_DFL);
04d3b78c 283 kill(0, SIGTTIN);
d33af40e 284 signal(SIGTTIN, old);
04d3b78c
BJ
285 goto retry;
286 }
287 if (ioctl(f, TIOCGETD, &oldisc) != 0)
288 goto notty;
289 if (oldisc != NTTYDISC) {
d7929fa7
KM
290#ifdef DEBUG
291 printf("Switching to new tty driver...\n");
292#endif DEBUG
04d3b78c
BJ
293 ldisc = NTTYDISC;
294 ioctl(f, TIOCSETD, &ldisc);
295 } else
296 oldisc = -1;
297 opgrp = shpgrp;
298 shpgrp = getpid();
299 tpgrp = shpgrp;
300 ioctl(f, TIOCSPGRP, &shpgrp);
301 setpgrp(0, shpgrp);
302 dcopy(f, FSHTTY);
303 ioctl(FSHTTY, FIOCLEX, 0);
304 } else {
305notty:
306 printf("Warning: no access to tty; thus no job control in this shell...\n");
307 tpgrp = -1;
308 }
309 }
310 }
3b1a0a1e
KM
311 if (setintr == 0 && parintr == SIG_DFL)
312 setintr++;
d33af40e 313 signal(SIGCHLD, pchild); /* while signals not ready */
04d3b78c
BJ
314
315 /*
316 * Set an exit here in case of an interrupt or error reading
317 * the shell start-up scripts.
318 */
319 setexit();
320 haderr = 0; /* In case second time through */
321 if (!fast && reenter == 0) {
322 reenter++;
323 /* Will have value("home") here because set fast if don't */
324 srccat(value("home"), "/.cshrc");
325 if (!fast && !arginp && !onelflg)
326 dohash();
d7929fa7 327 dosource(loadhist);
04d3b78c 328 if (loginsh) {
04d3b78c
BJ
329 srccat(value("home"), "/.login");
330 }
331 }
332
333 /*
334 * Now are ready for the -v and -x flags
335 */
336 if (nverbose)
337 setNS("verbose");
338 if (nexececho)
339 setNS("echo");
340
341 /*
342 * All the rest of the world is inside this call.
343 * The argument to process indicates whether it should
344 * catch "error unwinds". Thus if we are a interactive shell
345 * our call here will never return by being blown past on an error.
346 */
347 process(setintr);
348
349 /*
350 * Mop-up.
351 */
352 if (loginsh) {
353 printf("logout\n");
354 close(SHIN);
355 child++;
356 goodbye();
357 }
d7929fa7 358 rechist();
04d3b78c
BJ
359 exitstat();
360}
361
362untty()
363{
364
365 if (tpgrp > 0) {
366 setpgrp(0, opgrp);
367 ioctl(FSHTTY, TIOCSPGRP, &opgrp);
368 if (oldisc != -1 && oldisc != NTTYDISC) {
d7929fa7 369#ifdef DEBUG
04d3b78c 370 printf("\nReverting to old tty driver...\n");
d7929fa7 371#endif DEBUG
04d3b78c
BJ
372 ioctl(FSHTTY, TIOCSETD, &oldisc);
373 }
374 }
375}
376
377importpath(cp)
ec1a81af 378 char *cp;
04d3b78c
BJ
379{
380 register int i = 0;
381 register char *dp;
382 register char **pv;
383 int c;
384 static char dot[2] = {'.', 0};
385
386 for (dp = cp; *dp; dp++)
387 if (*dp == ':')
388 i++;
389 /*
390 * i+2 where i is the number of colons in the path.
391 * There are i+1 directories in the path plus we need
392 * room for a zero terminator.
393 */
394 pv = (char **) calloc(i+2, sizeof (char **));
395 dp = cp;
396 i = 0;
397 if (*dp)
398 for (;;) {
399 if ((c = *dp) == ':' || c == 0) {
400 *dp = 0;
401 pv[i++] = savestr(*cp ? cp : dot);
402 if (c) {
403 cp = dp + 1;
404 *dp = ':';
405 } else
406 break;
407 }
408 dp++;
409 }
410 pv[i] = 0;
411 set1("path", pv, &shvhed);
412}
413
414/*
415 * Source to the file which is the catenation of the argument names.
416 */
417srccat(cp, dp)
418 char *cp, *dp;
419{
420 register char *ep = strspl(cp, dp);
421 register int unit = dmove(open(ep, 0), -1);
422
423 /* ioctl(unit, FIOCLEX, NULL); */
424 xfree(ep);
425#ifdef INGRES
d7929fa7 426 srcunit(unit, 0, 0);
04d3b78c 427#else
d7929fa7 428 srcunit(unit, 1, 0);
04d3b78c
BJ
429#endif
430}
431
432/*
433 * Source to a unit. If onlyown it must be our file or our group or
434 * we don't chance it. This occurs on ".cshrc"s and the like.
435 */
d7929fa7 436srcunit(unit, onlyown, hflg)
04d3b78c
BJ
437 register int unit;
438 bool onlyown;
d7929fa7 439 bool hflg;
04d3b78c
BJ
440{
441 /* We have to push down a lot of state here */
442 /* All this could go into a structure */
443 int oSHIN = -1, oldintty = intty;
444 struct whyle *oldwhyl = whyles;
445 char *ogointr = gointr, *oarginp = arginp;
446 char *oevalp = evalp, **oevalvec = evalvec;
447 int oonelflg = onelflg;
d7929fa7
KM
448 bool oenterhist = enterhist;
449 char OHIST = HIST;
04d3b78c
BJ
450#ifdef TELL
451 bool otell = cantell;
452#endif
453 struct Bin saveB;
454
455 /* The (few) real local variables */
456 jmp_buf oldexit;
d33af40e 457 int reenter, omask;
04d3b78c
BJ
458
459 if (unit < 0)
460 return;
461 if (didfds)
462 donefds();
463 if (onlyown) {
464 struct stat stb;
465
ec1a81af
SL
466 if (fstat(unit, &stb) < 0 ||
467 (stb.st_uid != uid && stb.st_gid != getgid())) {
04d3b78c
BJ
468 close(unit);
469 return;
470 }
471 }
472
473 /*
474 * There is a critical section here while we are pushing down the
475 * input stream since we have stuff in different structures.
476 * If we weren't careful an interrupt could corrupt SHIN's Bin
477 * structure and kill the shell.
478 *
479 * We could avoid the critical region by grouping all the stuff
480 * in a single structure and pointing at it to move it all at
481 * once. This is less efficient globally on many variable references
482 * however.
483 */
484 getexit(oldexit);
485 reenter = 0;
486 if (setintr)
d33af40e 487 omask = sigblock(sigmask(SIGINT));
04d3b78c
BJ
488 setexit();
489 reenter++;
490 if (reenter == 1) {
491 /* Setup the new values of the state stuff saved above */
492 copy((char *)&saveB, (char *)&B, sizeof saveB);
493 fbuf = (char **) 0;
494 fseekp = feobp = fblocks = 0;
495 oSHIN = SHIN, SHIN = unit, arginp = 0, onelflg = 0;
496 intty = isatty(SHIN), whyles = 0, gointr = 0;
497 evalvec = 0; evalp = 0;
d7929fa7
KM
498 enterhist = hflg;
499 if (enterhist)
500 HIST = '\0';
04d3b78c
BJ
501 /*
502 * Now if we are allowing commands to be interrupted,
503 * we let ourselves be interrupted.
504 */
505 if (setintr)
d33af40e 506 sigsetmask(omask);
04d3b78c
BJ
507#ifdef TELL
508 settell();
509#endif
510 process(0); /* 0 -> blow away on errors */
511 }
512 if (setintr)
d33af40e 513 sigsetmask(omask);
04d3b78c
BJ
514 if (oSHIN >= 0) {
515 register int i;
516
517 /* We made it to the new state... free up its storage */
518 /* This code could get run twice but xfree doesn't care */
519 for (i = 0; i < fblocks; i++)
520 xfree(fbuf[i]);
521 xfree((char *)fbuf);
522
523 /* Reset input arena */
524 copy((char *)&B, (char *)&saveB, sizeof B);
525
526 close(SHIN), SHIN = oSHIN;
527 arginp = oarginp, onelflg = oonelflg;
528 evalp = oevalp, evalvec = oevalvec;
529 intty = oldintty, whyles = oldwhyl, gointr = ogointr;
020d9a25
KM
530 if (enterhist)
531 HIST = OHIST;
d7929fa7 532 enterhist = oenterhist;
04d3b78c
BJ
533#ifdef TELL
534 cantell = otell;
535#endif
536 }
537
538 resexit(oldexit);
539 /*
540 * If process reset() (effectively an unwind) then
541 * we must also unwind.
542 */
543 if (reenter >= 2)
544 error(NOSTR);
545}
546
d7929fa7 547rechist()
04d3b78c 548{
63af20f7
KM
549 char buf[BUFSIZ];
550 int fp, ftmp, oldidfds;
04d3b78c 551
d7929fa7 552 if (!fast) {
6ea8dfee
KM
553 if (value("savehist")[0] == '\0')
554 return;
63af20f7
KM
555 strcpy(buf, value("home"));
556 strcat(buf, "/.history");
14f52162 557 fp = creat(buf, 0666);
6ea8dfee
KM
558 if (fp == -1)
559 return;
560 oldidfds = didfds;
561 didfds = 0;
562 ftmp = SHOUT;
563 SHOUT = fp;
564 strcpy(buf, value("savehist"));
565 dumphist[2] = buf;
566 dohist(dumphist);
567 close(fp);
568 SHOUT = ftmp;
569 didfds = oldidfds;
d7929fa7
KM
570 }
571}
572
573goodbye()
574{
575 if (loginsh) {
04d3b78c 576 signal(SIGQUIT, SIG_IGN);
d33af40e 577 signal(SIGINT, SIG_IGN);
04d3b78c
BJ
578 signal(SIGTERM, SIG_IGN);
579 setintr = 0; /* No interrupts after "logout" */
580 if (adrof("home"))
581 srccat(value("home"), "/.logout");
582 }
d7929fa7 583 rechist();
04d3b78c
BJ
584 exitstat();
585}
586
587exitstat()
588{
589
590 /*
591 * Note that if STATUS is corrupted (i.e. getn bombs)
592 * then error will exit directly because we poke child here.
593 * Otherwise we might continue unwarrantedly (sic).
594 */
595 child++;
596 exit(getn(value("status")));
597}
598
d7929fa7
KM
599/*
600 * in the event of a HUP we want to save the history
601 */
602phup()
603{
604 rechist();
605 exit(1);
606}
607
04d3b78c
BJ
608char *jobargv[2] = { "jobs", 0 };
609/*
610 * Catch an interrupt, e.g. during lexical input.
611 * If we are an interactive shell, we reset the interrupt catch
612 * immediately. In any case we drain the shell output,
613 * and finally go through the normal error mechanism, which
614 * gets a chance to make the shell go away.
615 */
616pintr()
401149be
BJ
617{
618 pintr1(1);
619}
620
621pintr1(wantnl)
622 bool wantnl;
04d3b78c
BJ
623{
624 register char **v;
d33af40e 625 int omask;
04d3b78c 626
d33af40e 627 omask = sigblock(0);
04d3b78c 628 if (setintr) {
d33af40e 629 sigsetmask(omask & ~sigmask(SIGINT));
04d3b78c
BJ
630 if (pjobs) {
631 pjobs = 0;
632 printf("\n");
633 dojobs(jobargv);
634 bferr("Interrupted");
635 }
636 }
d33af40e 637 sigsetmask(omask & ~sigmask(SIGCHLD));
04d3b78c
BJ
638 draino();
639
640 /*
641 * If we have an active "onintr" then we search for the label.
642 * Note that if one does "onintr -" then we shan't be interruptible
643 * so we needn't worry about that here.
644 */
645 if (gointr) {
646 search(ZGOTO, 0, gointr);
647 timflg = 0;
648 if (v = pargv)
649 pargv = 0, blkfree(v);
650 if (v = gargv)
651 gargv = 0, blkfree(v);
652 reset();
401149be 653 } else if (intty && wantnl)
04d3b78c
BJ
654 printf("\n"); /* Some like this, others don't */
655 error(NOSTR);
656}
657
658/*
659 * Process is the main driving routine for the shell.
660 * It runs all command processing, except for those within { ... }
661 * in expressions (which is run by a routine evalav in sh.exp.c which
662 * is a stripped down process), and `...` evaluation which is run
663 * also by a subset of this code in sh.glob.c in the routine backeval.
664 *
665 * The code here is a little strange because part of it is interruptible
666 * and hence freeing of structures appears to occur when none is necessary
667 * if this is ignored.
668 *
669 * Note that if catch is not set then we will unwind on any error.
670 * If an end-of-file occurs, we return.
671 */
672process(catch)
673 bool catch;
674{
675 register char *cp;
676 jmp_buf osetexit;
677 struct command *t;
678
679 getexit(osetexit);
680 for (;;) {
681 pendjob();
682 paraml.next = paraml.prev = &paraml;
683 paraml.word = "";
684 t = 0;
685 setexit();
63af20f7 686 justpr = enterhist; /* execute if not entering history */
04d3b78c
BJ
687
688 /*
689 * Interruptible during interactive reads
690 */
691 if (setintr)
d33af40e 692 sigsetmask(sigblock(0) & ~sigmask(SIGINT));
04d3b78c
BJ
693
694 /*
695 * For the sake of reset()
696 */
697 freelex(&paraml), freesyn(t), t = 0;
698
699 if (haderr) {
700 if (!catch) {
701 /* unwind */
702 doneinp = 0;
703 resexit(osetexit);
704 reset();
705 }
706 haderr = 0;
707 /*
708 * Every error is eventually caught here or
709 * the shell dies. It is at this
710 * point that we clean up any left-over open
711 * files, by closing all but a fixed number
712 * of pre-defined files. Thus routines don't
713 * have to worry about leaving files open due
714 * to deeper errors... they will get closed here.
715 */
716 closem();
717 continue;
718 }
719 if (doneinp) {
720 doneinp = 0;
721 break;
722 }
723 if (chkstop)
724 chkstop--;
725 if (neednote)
726 pnote();
c9166193 727 if (intty && prompt && evalvec == 0) {
04d3b78c
BJ
728 mailchk();
729 /*
730 * If we are at the end of the input buffer
731 * then we are going to read fresh stuff.
732 * Otherwise, we are rereading input and don't
733 * need or want to prompt.
734 */
735 if (fseekp == feobp)
544d4ed6 736 printprompt();
04d3b78c
BJ
737 flush();
738 }
739 err = 0;
740
741 /*
742 * Echo not only on VERBOSE, but also with history expansion.
743 * If there is a lexical error then we forego history echo.
744 */
d7929fa7
KM
745 if (lex(&paraml) && !err && intty ||
746 adrof("verbose")) {
04d3b78c
BJ
747 haderr = 1;
748 prlex(&paraml);
749 haderr = 0;
750 }
751
752 /*
753 * The parser may lose space if interrupted.
754 */
755 if (setintr)
d33af40e 756 sigblock(sigmask(SIGINT));
04d3b78c
BJ
757
758 /*
63af20f7
KM
759 * Save input text on the history list if
760 * reading in old history, or it
04d3b78c
BJ
761 * is from the terminal at the top level and not
762 * in a loop.
763 */
63af20f7 764 if (enterhist || catch && intty && !whyles)
04d3b78c
BJ
765 savehist(&paraml);
766
767 /*
d7929fa7
KM
768 * Print lexical error messages, except when sourcing
769 * history lists.
04d3b78c 770 */
d7929fa7 771 if (!enterhist && err)
04d3b78c
BJ
772 error(err);
773
774 /*
775 * If had a history command :p modifier then
776 * this is as far as we should go
777 */
778 if (justpr)
779 reset();
780
781 alias(&paraml);
782
783 /*
784 * Parse the words of the input into a parse tree.
785 */
786 t = syntax(paraml.next, &paraml, 0);
787 if (err)
788 error(err);
789
790 /*
791 * Execute the parse tree
792 */
793 execute(t, tpgrp);
794
795 /*
796 * Made it!
797 */
798 freelex(&paraml), freesyn(t);
799 }
800 resexit(osetexit);
801}
802
803dosource(t)
804 register char **t;
805{
806 register char *f;
807 register int u;
d7929fa7
KM
808 bool hflg = 0;
809 char buf[BUFSIZ];
04d3b78c
BJ
810
811 t++;
d7929fa7
KM
812 if (*t && eq(*t, "-h")) {
813 t++;
814 hflg++;
815 }
816 strcpy(buf, *t);
817 f = globone(buf);
04d3b78c
BJ
818 u = dmove(open(f, 0), -1);
819 xfree(f);
d7929fa7 820 if (u < 0 && !hflg)
04d3b78c 821 Perror(f);
d7929fa7 822 srcunit(u, 0, hflg);
04d3b78c
BJ
823}
824
825/*
826 * Check for mail.
827 * If we are a login shell, then we don't want to tell
828 * about any mail file unless its been modified
829 * after the time we started.
830 * This prevents us from telling the user things he already
831 * knows, since the login program insists on saying
832 * "You have mail."
833 */
834mailchk()
835{
836 register struct varent *v;
837 register char **vp;
838 time_t t;
839 int intvl, cnt;
840 struct stat stb;
841 bool new;
842
843 v = adrof("mail");
844 if (v == 0)
845 return;
846 time(&t);
847 vp = v->vec;
848 cnt = blklen(vp);
849 intvl = (cnt && number(*vp)) ? (--cnt, getn(*vp++)) : MAILINTVL;
850 if (intvl < 1)
851 intvl = 1;
852 if (chktim + intvl > t)
853 return;
854 for (; *vp; vp++) {
855 if (stat(*vp, &stb) < 0)
856 continue;
ae3d9709 857 new = stb.st_mtime > time0.tv_sec;
04d3b78c
BJ
858 if (stb.st_size == 0 || stb.st_atime > stb.st_mtime ||
859 (stb.st_atime < chktim && stb.st_mtime < chktim) ||
860 loginsh && !new)
861 continue;
862 if (cnt == 1)
863 printf("You have %smail.\n", new ? "new " : "");
864 else
865 printf("%s in %s.\n", new ? "New mail" : "Mail", *vp);
866 }
867 chktim = t;
868}
869
870#include <pwd.h>
871/*
872 * Extract a home directory from the password file
873 * The argument points to a buffer where the name of the
874 * user whose home directory is sought is currently.
875 * We write the home directory of the user back there.
876 */
877gethdir(home)
878 char *home;
879{
880 register struct passwd *pp = getpwnam(home);
881
882 if (pp == 0)
883 return (1);
884 strcpy(home, pp->pw_dir);
885 return (0);
886}
887
888/*
889 * Move the initial descriptors to their eventual
890 * resting places, closin all other units.
891 */
892initdesc()
893{
894
895 didcch = 0; /* Havent closed for child */
896 didfds = 0; /* 0, 1, 2 aren't set up */
897 SHIN = dcopy(0, FSHIN);
898 SHOUT = dcopy(1, FSHOUT);
899 SHDIAG = dcopy(2, FSHDIAG);
900 OLDSTD = dcopy(SHIN, FOLDSTD);
901 closem();
902}
903
e31125a2
SL
904#ifdef PROF
905done(i)
906#else
04d3b78c 907exit(i)
e31125a2 908#endif
04d3b78c
BJ
909 int i;
910{
911
912 untty();
913#ifdef PROF
e31125a2 914 monitor(0);
04d3b78c
BJ
915#else
916 _exit(i);
917#endif
918}
19d39288 919
544d4ed6 920printprompt()
19d39288 921{
544d4ed6
SL
922 register char *cp;
923
e31125a2 924 if (!whyles) {
544d4ed6
SL
925 for (cp = value("prompt"); *cp; cp++)
926 if (*cp == HIST)
927 printf("%d", eventno + 1);
928 else {
929 if (*cp == '\\' && cp[1] == HIST)
930 cp++;
931 putchar(*cp | QUOTE);
932 }
933 } else
934 /*
935 * Prompt for forward reading loop
936 * body content.
937 */
938 printf("? ");
939 flush();
19d39288 940}