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