Vendor import of tcsh 6.15.00
[dragonfly.git] / contrib / tcsh-6 / sh.sem.c
blob0ae1502734802b161451e853087a0fa167c615a6
1 /* $Header: /p/tcsh/cvsroot/tcsh/sh.sem.c,v 3.78 2006/10/14 17:23:39 christos Exp $ */
2 /*
3 * sh.sem.c: I/O redirections and job forking. A touchy issue!
4 * Most stuff with builtins is incorrect
5 */
6 /*-
7 * Copyright (c) 1980, 1991 The Regents of the University of California.
8 * All rights reserved.
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 * 3. Neither the name of the University nor the names of its contributors
19 * may be used to endorse or promote products derived from this software
20 * without specific prior written permission.
22 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
23 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 * SUCH DAMAGE.
34 #include "sh.h"
36 RCSID("$tcsh: sh.sem.c,v 3.78 2006/10/14 17:23:39 christos Exp $")
38 #include "tc.h"
39 #include "tw.h"
40 #ifdef WINNT_NATIVE
41 #include "nt.const.h"
42 #endif /*WINNT_NATIVE*/
44 #ifdef CLOSE_ON_EXEC
45 # ifndef SUNOS4
46 # ifndef CLEX_DUPS
47 # define CLEX_DUPS
48 # endif /* CLEX_DUPS */
49 # endif /* !SUNOS4 */
50 #endif /* CLOSE_ON_EXEC */
52 #if defined(__sparc__) || defined(sparc)
53 # if !defined(MACH) && SYSVREL == 0 && !defined(Lynx) && !defined(BSD4_4) && !defined(linux) && !defined(__GNU__) && !defined(__GLIBC__)
54 # include <vfork.h>
55 # endif /* !MACH && SYSVREL == 0 && !Lynx && !BSD4_4 && !glibc */
56 #endif /* __sparc__ || sparc */
58 #ifdef VFORK
59 static void vffree (int);
60 #endif
61 static Char *splicepipe (struct command *, Char *);
62 static void doio (struct command *, int *, int *);
63 static void chkclob (const char *);
66 * C shell
70 * For SVR4, there are problems with pipelines having the first process as
71 * the group leader. The problem occurs when the first process exits before
72 * the others have a chance to setpgid(). This is because in SVR4 you can't
73 * have a zombie as a group leader. The solution I have used is to reverse
74 * the order in which pipelines are started, making the last process the
75 * group leader. (Note I am not using 'pipeline' in the generic sense -- I
76 * mean processes connected by '|'.) I don't know yet if this causes other
77 * problems.
79 * All the changes for this are in execute(), and are enclosed in
80 * '#ifdef BACKPIPE'
82 * David Dawes (dawes@physics.su.oz.au) Oct 1991
85 /*VARARGS 1*/
86 void
87 execute(struct command *t, volatile int wanttty, int *pipein, int *pipeout,
88 int do_glob)
90 int forked = 0;
91 const struct biltins * volatile bifunc;
92 pid_t pid = 0;
93 int pv[2];
94 sigset_t set;
95 static sigset_t csigset;
96 #ifdef VFORK
97 static int onosigchld = 0;
98 #endif /* VFORK */
99 static int nosigchld = 0;
101 (void) &wanttty;
102 (void) &forked;
103 (void) &bifunc;
105 if (t == 0)
106 return;
108 #ifdef WINNT_NATIVE
110 if ((varval(STRNTslowexec) == STRNULL) &&
111 !t->t_dcdr && !t->t_dcar && !t->t_dflg && !didfds &&
112 (intty || intact) && (t->t_dtyp == NODE_COMMAND) &&
113 !isbfunc(t)) {
114 if ((t->t_dcom[0][0] & (QUOTE | TRIM)) == QUOTE)
115 (void) Strcpy(t->t_dcom[0], t->t_dcom[0] + 1);
116 Dfix(t);
117 if (nt_try_fast_exec(t) == 0)
118 return;
121 #endif /* WINNT_NATIVE */
124 * Ed hutchins@sgi.com & Dominic dbg@sgi.com
125 * Sat Feb 25 03:13:11 PST 1995
126 * try implicit cd if we have a 1 word command
128 if (implicit_cd && (intty || intact) && t->t_dcom && t->t_dcom[0] &&
129 t->t_dcom[0][0] && (blklen(t->t_dcom) == 1) && !noexec) {
130 Char *sCName;
131 struct stat stbuf;
132 char *pathname;
134 sCName = dollar(t->t_dcom[0]);
135 if (sCName != NULL && sCName[0] == '~') {
136 struct Strbuf buf = Strbuf_INIT;
137 const Char *name_end;
139 for (name_end = sCName + 1; *name_end != '\0' && *name_end != '/';
140 name_end++)
141 continue;
142 if (name_end != sCName + 1) {
143 Char *name, *home;
145 name = Strnsave(sCName + 1, name_end - (sCName + 1));
146 home = gethdir(name);
147 if (home != NULL) {
148 Strbuf_append(&buf, home);
149 xfree(home);
150 } else
151 Strbuf_append(&buf, name);
152 xfree(name);
153 } else
154 Strbuf_append(&buf, varval(STRhome));
155 Strbuf_append(&buf, name_end);
156 xfree(sCName);
157 sCName = Strbuf_finish(&buf);
160 pathname = short2str(sCName);
161 xfree(sCName);
162 /* if this is a dir, tack a "cd" on as the first arg */
163 if (pathname != NULL &&
164 ((stat(pathname, &stbuf) != -1 && S_ISDIR(stbuf.st_mode))
165 #ifdef WINNT_NATIVE
166 || (pathname[0] && pathname[1] == ':' && pathname[2] == '\0')
167 #endif /* WINNT_NATIVE */
168 )) {
169 Char *vCD[2];
170 Char **ot_dcom = t->t_dcom;
172 vCD[0] = Strsave(STRcd);
173 vCD[1] = NULL;
174 t->t_dcom = blkspl(vCD, ot_dcom);
175 xfree(ot_dcom);
176 if (implicit_cd > 1) {
177 blkpr(t->t_dcom);
178 xputchar( '\n' );
184 * From: Michael Schroeder <mlschroe@immd4.informatik.uni-erlangen.de>
185 * Don't check for wantty > 0...
187 if (t->t_dflg & F_AMPERSAND)
188 wanttty = 0;
189 switch (t->t_dtyp) {
191 case NODE_COMMAND:
192 if ((t->t_dcom[0][0] & (QUOTE | TRIM)) == QUOTE)
193 memmove(t->t_dcom[0], t->t_dcom[0] + 1,
194 (Strlen(t->t_dcom[0] + 1) + 1) * sizeof (*t->t_dcom[0]));
195 if ((t->t_dflg & F_REPEAT) == 0)
196 Dfix(t); /* $ " ' \ */
197 if (t->t_dcom[0] == 0) {
198 return;
200 /*FALLTHROUGH*/
202 case NODE_PAREN:
203 #ifdef BACKPIPE
204 if (t->t_dflg & F_PIPEIN)
205 mypipe(pipein);
206 #else /* !BACKPIPE */
207 if (t->t_dflg & F_PIPEOUT)
208 mypipe(pipeout);
209 #endif /* BACKPIPE */
211 * Must do << early so parent will know where input pointer should be.
212 * If noexec then this is all we do.
214 if (t->t_dflg & F_READ) {
215 xclose(0);
216 heredoc(t->t_dlef);
217 if (noexec)
218 xclose(0);
221 setcopy(STRstatus, STR0, VAR_READWRITE);
224 * This mess is the necessary kludge to handle the prefix builtins:
225 * nice, nohup, time. These commands can also be used by themselves,
226 * and this is not handled here. This will also work when loops are
227 * parsed.
229 while (t->t_dtyp == NODE_COMMAND)
230 if (eq(t->t_dcom[0], STRnice)) {
231 if (t->t_dcom[1]) {
232 if (strchr("+-", t->t_dcom[1][0])) {
233 if (t->t_dcom[2]) {
234 setname("nice");
235 t->t_nice =
236 getn(t->t_dcom[1]);
237 lshift(t->t_dcom, 2);
238 t->t_dflg |= F_NICE;
240 else
241 break;
243 else {
244 t->t_nice = 4;
245 lshift(t->t_dcom, 1);
246 t->t_dflg |= F_NICE;
249 else
250 break;
252 else if (eq(t->t_dcom[0], STRnohup)) {
253 if (t->t_dcom[1]) {
254 t->t_dflg |= F_NOHUP;
255 lshift(t->t_dcom, 1);
257 else
258 break;
260 else if (eq(t->t_dcom[0], STRhup)) {
261 if (t->t_dcom[1]) {
262 t->t_dflg |= F_HUP;
263 lshift(t->t_dcom, 1);
265 else
266 break;
268 else if (eq(t->t_dcom[0], STRtime)) {
269 if (t->t_dcom[1]) {
270 t->t_dflg |= F_TIME;
271 lshift(t->t_dcom, 1);
273 else
274 break;
276 #ifdef F_VER
277 else if (eq(t->t_dcom[0], STRver))
278 if (t->t_dcom[1] && t->t_dcom[2]) {
279 setname("ver");
280 t->t_systype = getv(t->t_dcom[1]);
281 lshift(t->t_dcom, 2);
282 t->t_dflg |= F_VER;
284 else
285 break;
286 #endif /* F_VER */
287 else
288 break;
290 /* is it a command */
291 if (t->t_dtyp == NODE_COMMAND) {
293 * Check if we have a builtin function and remember which one.
295 bifunc = isbfunc(t);
296 if (noexec) {
298 * Continue for builtins that are part of the scripting language
300 if (bifunc == NULL)
301 break;
302 if (bifunc->bfunct != (bfunc_t)dobreak &&
303 bifunc->bfunct != (bfunc_t)docontin &&
304 bifunc->bfunct != (bfunc_t)doelse &&
305 bifunc->bfunct != (bfunc_t)doend &&
306 bifunc->bfunct != (bfunc_t)doforeach&&
307 bifunc->bfunct != (bfunc_t)dogoto &&
308 bifunc->bfunct != (bfunc_t)doif &&
309 bifunc->bfunct != (bfunc_t)dorepeat &&
310 bifunc->bfunct != (bfunc_t)doswbrk &&
311 bifunc->bfunct != (bfunc_t)doswitch &&
312 bifunc->bfunct != (bfunc_t)dowhile &&
313 bifunc->bfunct != (bfunc_t)dozip)
314 break;
317 else { /* not a command */
318 bifunc = NULL;
319 if (noexec)
320 break;
324 * GrP Executing a command - run jobcmd hook
325 * Don't run for builtins
326 * Don't run if we're not in a tty
327 * Don't run if we're not really executing
329 if (t->t_dtyp == NODE_COMMAND && !bifunc && !noexec && intty) {
330 Char *cmd = unparse(t);
332 cleanup_push(cmd, xfree);
333 job_cmd(cmd);
334 cleanup_until(cmd);
338 * We fork only if we are timed, or are not the end of a parenthesized
339 * list and not a simple builtin function. Simple meaning one that is
340 * not pipedout, niced, nohupped, or &'d. It would be nice(?) to not
341 * fork in some of these cases.
344 * Prevent forking cd, pushd, popd, chdir cause this will cause the
345 * shell not to change dir!
347 #ifdef BACKPIPE
349 * Can't have NOFORK for the tail of a pipe - because it is not the
350 * last command spawned (even if it is at the end of a parenthesised
351 * list).
353 if (t->t_dflg & F_PIPEIN)
354 t->t_dflg &= ~(F_NOFORK);
355 #endif /* BACKPIPE */
356 if (bifunc && (bifunc->bfunct == (bfunc_t)dochngd ||
357 bifunc->bfunct == (bfunc_t)dopushd ||
358 bifunc->bfunct == (bfunc_t)dopopd))
359 t->t_dflg &= ~(F_NICE);
360 if (((t->t_dflg & F_TIME) || ((t->t_dflg & F_NOFORK) == 0 &&
361 (!bifunc || t->t_dflg &
362 (F_PIPEOUT | F_AMPERSAND | F_NICE | F_NOHUP | F_HUP)))) ||
364 * We have to fork for eval too.
366 (bifunc && (t->t_dflg & F_PIPEIN) != 0 &&
367 bifunc->bfunct == (bfunc_t)doeval)) {
368 #ifdef VFORK
369 if (t->t_dtyp == NODE_PAREN ||
370 t->t_dflg & (F_REPEAT | F_AMPERSAND) || bifunc)
371 #endif /* VFORK */
373 forked++;
375 * We need to block SIGCHLD here, so that if the process does
376 * not die before we can set the process group
378 if (wanttty >= 0 && !nosigchld) {
379 sigemptyset(&set);
380 sigaddset(&set, SIGCHLD);
381 (void)sigprocmask(SIG_BLOCK, &set, &csigset);
383 nosigchld = 1;
386 pid = pfork(t, wanttty);
387 if (pid == 0 && nosigchld) {
388 sigprocmask(SIG_SETMASK, &csigset, NULL);
389 nosigchld = 0;
391 else if (pid != 0 && (t->t_dflg & F_AMPERSAND))
392 backpid = pid;
395 #ifdef VFORK
396 else {
397 int ochild, osetintr, ohaderr, odidfds;
398 int oSHIN, oSHOUT, oSHDIAG, oOLDSTD, otpgrp;
399 int oisoutatty, oisdiagatty;
400 sigset_t oset, ocsigset;
401 # ifndef CLOSE_ON_EXEC
402 int odidcch;
403 # endif /* !CLOSE_ON_EXEC */
406 * Prepare for the vfork by saving everything that the child
407 * corrupts before it exec's. Note that in some signal
408 * implementations which keep the signal info in user space
409 * (e.g. Sun's) it will also be necessary to save and restore
410 * the current sigvec's for the signals the child touches
411 * before it exec's.
415 * Sooooo true... If this is a Sun, save the sigvec's. (Skip
416 * Gilbrech - 11/22/87)
418 # ifdef SAVESIGVEC
419 struct sigaction savesv[NSIGSAVED];
420 sigset_t savesm;
422 # endif /* SAVESIGVEC */
423 if (wanttty >= 0 && !nosigchld && !noexec) {
424 sigemptyset(&set);
425 sigaddset(&set, SIGCHLD);
426 (void)sigprocmask(SIG_BLOCK, &set, &csigset);
427 nosigchld = 1;
429 sigemptyset(&set);
430 sigaddset(&set, SIGCHLD);
431 sigaddset(&set, SIGINT);
432 (void)sigprocmask(SIG_BLOCK, &set, &oset);
433 ochild = child;
434 osetintr = setintr;
435 ohaderr = haderr;
436 odidfds = didfds;
437 # ifndef CLOSE_ON_EXEC
438 odidcch = didcch;
439 # endif /* !CLOSE_ON_EXEC */
440 oSHIN = SHIN;
441 oSHOUT = SHOUT;
442 oSHDIAG = SHDIAG;
443 oOLDSTD = OLDSTD;
444 otpgrp = tpgrp;
445 oisoutatty = isoutatty;
446 oisdiagatty = isdiagatty;
447 ocsigset = csigset;
448 onosigchld = nosigchld;
449 Vsav = Vdp = 0;
450 Vexpath = 0;
451 Vt = 0;
452 # ifdef SAVESIGVEC
453 savesigvec(savesv, savesm);
454 # endif /* SAVESIGVEC */
455 if (use_fork)
456 pid = fork();
457 else
458 pid = vfork();
460 if (pid < 0) {
461 # ifdef SAVESIGVEC
462 restoresigvec(savesv, savesm);
463 # endif /* SAVESIGVEC */
464 sigprocmask(SIG_SETMASK, &oset, NULL);
465 stderror(ERR_NOPROC);
467 forked++;
468 if (pid) { /* parent */
469 # ifdef SAVESIGVEC
470 restoresigvec(savesv, savesm);
471 # endif /* SAVESIGVEC */
472 child = ochild;
473 setintr = osetintr;
474 haderr = ohaderr;
475 didfds = odidfds;
476 SHIN = oSHIN;
477 # ifndef CLOSE_ON_EXEC
478 didcch = odidcch;
479 # endif /* !CLOSE_ON_EXEC */
480 SHOUT = oSHOUT;
481 SHDIAG = oSHDIAG;
482 OLDSTD = oOLDSTD;
483 tpgrp = otpgrp;
484 isoutatty = oisoutatty;
485 isdiagatty = oisdiagatty;
486 csigset = ocsigset;
487 nosigchld = onosigchld;
489 xfree(Vsav);
490 Vsav = 0;
491 xfree(Vdp);
492 Vdp = 0;
493 xfree(Vexpath);
494 Vexpath = 0;
495 blk_cleanup(Vt);
496 Vt = 0;
497 /* this is from pfork() */
498 palloc(pid, t);
499 sigprocmask(SIG_SETMASK, &oset, NULL);
501 else { /* child */
502 /* this is from pfork() */
503 pid_t pgrp;
504 int ignint = 0;
505 if (nosigchld) {
506 sigprocmask(SIG_SETMASK, &csigset, NULL);
507 nosigchld = 0;
510 if (setintr)
511 ignint = (tpgrp == -1 && (t->t_dflg & F_NOINTERRUPT))
512 || (gointr && eq(gointr, STRminus));
513 pgrp = pcurrjob ? pcurrjob->p_jobid : getpid();
514 child++;
515 if (setintr) {
516 setintr = 0;
518 * casts made right for SunOS 4.0 by Douglas C. Schmidt
519 * <schmidt%sunshine.ics.uci.edu@ROME.ICS.UCI.EDU>
520 * (thanks! -- PWP)
522 * ignint ifs cleaned by Johan Widen <mcvax!osiris.sics.se!jw@uunet.UU.NET>
523 * (thanks again)
525 if (ignint) {
526 (void) signal(SIGINT, SIG_IGN);
527 (void) signal(SIGQUIT, SIG_IGN);
529 else {
530 (void) signal(SIGINT, vffree);
531 (void) signal(SIGQUIT, SIG_DFL);
533 # ifdef BSDJOBS
534 if (wanttty >= 0) {
535 (void) signal(SIGTSTP, SIG_DFL);
536 (void) signal(SIGTTIN, SIG_DFL);
537 (void) signal(SIGTTOU, SIG_DFL);
539 # endif /* BSDJOBS */
541 sigaction(SIGTERM, &parterm, NULL);
543 else if (tpgrp == -1 &&
544 (t->t_dflg & F_NOINTERRUPT)) {
545 (void) signal(SIGINT, SIG_IGN);
546 (void) signal(SIGQUIT, SIG_IGN);
549 pgetty(wanttty, pgrp);
551 if (t->t_dflg & F_NOHUP)
552 (void) signal(SIGHUP, SIG_IGN);
553 if (t->t_dflg & F_HUP)
554 (void) signal(SIGHUP, SIG_DFL);
555 if (t->t_dflg & F_NICE) {
556 int nval = SIGN_EXTEND_CHAR(t->t_nice);
557 # ifdef HAVE_SETPRIORITY
558 if (setpriority(PRIO_PROCESS, 0, nval) == -1 && errno)
559 stderror(ERR_SYSTEM, "setpriority",
560 strerror(errno));
561 # else /* !HAVE_SETPRIORITY */
562 (void) nice(nval);
563 # endif /* HAVE_SETPRIORITY */
565 # ifdef F_VER
566 if (t->t_dflg & F_VER) {
567 tsetenv(STRSYSTYPE, t->t_systype ? STRbsd43 : STRsys53);
568 dohash(NULL, NULL);
570 # endif /* F_VER */
574 #endif /* VFORK */
576 if (pid != 0) {
578 * It would be better if we could wait for the whole job when we
579 * knew the last process had been started. Pwait, in fact, does
580 * wait for the whole job anyway, but this test doesn't really
581 * express our intentions.
583 #ifdef BACKPIPE
584 if (didfds == 0 && t->t_dflg & F_PIPEOUT) {
585 xclose(pipeout[0]);
586 xclose(pipeout[1]);
588 if ((t->t_dflg & F_PIPEIN) != 0)
589 break;
590 #else /* !BACKPIPE */
591 if (didfds == 0 && t->t_dflg & F_PIPEIN) {
592 xclose(pipein[0]);
593 xclose(pipein[1]);
595 if ((t->t_dflg & F_PIPEOUT) != 0)
596 break;
597 #endif /* BACKPIPE */
599 if (nosigchld) {
600 sigprocmask(SIG_SETMASK, &csigset, NULL);
601 nosigchld = 0;
603 if ((t->t_dflg & F_AMPERSAND) == 0)
604 pwait();
605 break;
608 doio(t, pipein, pipeout);
609 #ifdef BACKPIPE
610 if (t->t_dflg & F_PIPEIN) {
611 xclose(pipein[0]);
612 xclose(pipein[1]);
614 #else /* !BACKPIPE */
615 if (t->t_dflg & F_PIPEOUT) {
616 xclose(pipeout[0]);
617 xclose(pipeout[1]);
619 #endif /* BACKPIPE */
621 * Perform a builtin function. If we are not forked, arrange for
622 * possible stopping
624 if (bifunc) {
625 func(t, bifunc);
626 if (forked)
627 exitstat();
628 else {
629 if (adrof(STRprintexitvalue)) {
630 int rv = getn(varval(STRstatus));
631 if (rv != 0)
632 xprintf(CGETS(17, 2, "Exit %d\n"), rv);
635 break;
637 if (t->t_dtyp != NODE_PAREN) {
638 doexec(t, do_glob);
639 /* NOTREACHED */
642 * For () commands must put new 0,1,2 in FSH* and recurse
644 (void)close_on_exec(OLDSTD = dcopy(0, FOLDSTD), 1);
645 (void)close_on_exec(SHOUT = dcopy(1, FSHOUT), 1);
646 isoutatty = isatty(SHOUT);
647 (void)close_on_exec(SHDIAG = dcopy(2, FSHDIAG), 1);
648 isdiagatty = isatty(SHDIAG);
649 xclose(SHIN);
650 SHIN = -1;
651 #ifndef CLOSE_ON_EXEC
652 didcch = 0;
653 #else
654 (void) close_on_exec(FSHOUT, 1);
655 (void) close_on_exec(FSHDIAG, 1);
656 (void) close_on_exec(FOLDSTD, 1);
657 #endif /* !CLOSE_ON_EXEC */
658 didfds = 0;
659 wanttty = -1;
660 t->t_dspr->t_dflg |= t->t_dflg & F_NOINTERRUPT;
661 execute(t->t_dspr, wanttty, NULL, NULL, do_glob);
662 exitstat();
664 case NODE_PIPE:
665 #ifdef BACKPIPE
666 t->t_dcdr->t_dflg |= F_PIPEIN | (t->t_dflg &
667 (F_PIPEOUT | F_AMPERSAND | F_NOFORK | F_NOINTERRUPT));
668 execute(t->t_dcdr, wanttty, pv, pipeout, do_glob);
669 t->t_dcar->t_dflg |= F_PIPEOUT |
670 (t->t_dflg & (F_PIPEIN | F_AMPERSAND | F_STDERR | F_NOINTERRUPT));
671 execute(t->t_dcar, wanttty, pipein, pv, do_glob);
672 #else /* !BACKPIPE */
673 t->t_dcar->t_dflg |= F_PIPEOUT |
674 (t->t_dflg & (F_PIPEIN | F_AMPERSAND | F_STDERR | F_NOINTERRUPT));
675 execute(t->t_dcar, wanttty, pipein, pv, do_glob);
676 t->t_dcdr->t_dflg |= F_PIPEIN | (t->t_dflg &
677 (F_PIPEOUT | F_AMPERSAND | F_NOFORK | F_NOINTERRUPT));
678 execute(t->t_dcdr, wanttty, pv, pipeout, do_glob);
679 #endif /* BACKPIPE */
680 break;
682 case NODE_LIST:
683 if (t->t_dcar) {
684 t->t_dcar->t_dflg |= t->t_dflg & F_NOINTERRUPT;
685 execute(t->t_dcar, wanttty, NULL, NULL, do_glob);
687 * In strange case of A&B make a new job after A
689 if (t->t_dcar->t_dflg & F_AMPERSAND && t->t_dcdr &&
690 (t->t_dcdr->t_dflg & F_AMPERSAND) == 0)
691 pendjob();
693 if (t->t_dcdr) {
694 t->t_dcdr->t_dflg |= t->t_dflg &
695 (F_NOFORK | F_NOINTERRUPT);
696 execute(t->t_dcdr, wanttty, NULL, NULL, do_glob);
698 break;
700 case NODE_OR:
701 case NODE_AND:
702 if (t->t_dcar) {
703 t->t_dcar->t_dflg |= t->t_dflg & F_NOINTERRUPT;
704 execute(t->t_dcar, wanttty, NULL, NULL, do_glob);
705 if ((getn(varval(STRstatus)) == 0) !=
706 (t->t_dtyp == NODE_AND)) {
707 return;
710 if (t->t_dcdr) {
711 t->t_dcdr->t_dflg |= t->t_dflg &
712 (F_NOFORK | F_NOINTERRUPT);
713 execute(t->t_dcdr, wanttty, NULL, NULL, do_glob);
715 break;
717 default:
718 break;
721 * Fall through for all breaks from switch
723 * If there will be no more executions of this command, flush all file
724 * descriptors. Places that turn on the F_REPEAT bit are responsible for
725 * doing donefds after the last re-execution
727 if (didfds && !(t->t_dflg & F_REPEAT))
728 donefds();
731 #ifdef VFORK
732 static void
733 /*ARGSUSED*/
734 vffree(int snum)
736 USE(snum);
738 _exit(1);
740 #endif /* VFORK */
743 * Expand and glob the words after an i/o redirection.
744 * If more than one word is generated, then update the command vector.
746 * This is done differently in all the shells:
747 * 1. in the bourne shell and ksh globbing is not performed
748 * 2. Bash/csh say ambiguous
749 * 3. zsh does i/o to/from all the files
750 * 4. itcsh concatenates the words.
752 * I don't know what is best to do. I think that Ambiguous is better
753 * than restructuring the command vector, because the user can get
754 * unexpected results. In any case, the command vector restructuring
755 * code is present and the user can choose it by setting noambiguous
757 static Char *
758 splicepipe(struct command *t, Char *cp)
760 Char *blk[2];
762 if (adrof(STRnoambiguous)) {
763 Char **pv;
764 int gflag;
766 blk[0] = Dfix1(cp); /* expand $ */
767 blk[1] = NULL;
769 gflag = tglob(blk);
770 if (gflag) {
771 pv = globall(blk, gflag);
772 if (pv == NULL) {
773 setname(short2str(blk[0]));
774 xfree(blk[0]);
775 stderror(ERR_NAME | ERR_NOMATCH);
777 if (pv[1] != NULL) { /* we need to fix the command vector */
778 Char **av = blkspl(t->t_dcom, &pv[1]);
779 xfree(t->t_dcom);
780 t->t_dcom = av;
782 xfree(blk[0]);
783 blk[0] = pv[0];
784 xfree(pv);
787 else {
788 Char *buf;
790 buf = Dfix1(cp);
791 cleanup_push(buf, xfree);
792 blk[0] = globone(buf, G_ERROR);
793 cleanup_until(buf);
795 return(blk[0]);
799 * Perform io redirection.
800 * We may or maynot be forked here.
802 static void
803 doio(struct command *t, int *pipein, int *pipeout)
805 int fd;
806 Char *cp;
807 unsigned long flags = t->t_dflg;
809 if (didfds || (flags & F_REPEAT))
810 return;
811 if ((flags & F_READ) == 0) {/* F_READ already done */
812 if (t->t_dlef) {
813 char *tmp;
816 * so < /dev/std{in,out,err} work
818 (void) dcopy(SHIN, 0);
819 (void) dcopy(SHOUT, 1);
820 (void) dcopy(SHDIAG, 2);
821 cp = splicepipe(t, t->t_dlef);
822 tmp = strsave(short2str(cp));
823 xfree(cp);
824 cleanup_push(tmp, xfree);
825 if ((fd = xopen(tmp, O_RDONLY|O_LARGEFILE)) < 0)
826 stderror(ERR_SYSTEM, tmp, strerror(errno));
827 cleanup_until(tmp);
828 /* allow input files larger than 2Gb */
829 #ifndef WINNT_NATIVE
830 (void) fcntl(fd, F_SETFL, fcntl(fd, F_GETFL) | O_LARGEFILE);
831 #endif /*!WINNT_NATIVE*/
832 (void) dmove(fd, 0);
834 else if (flags & F_PIPEIN) {
835 xclose(0);
836 (void) dup(pipein[0]);
837 xclose(pipein[0]);
838 xclose(pipein[1]);
840 else if ((flags & F_NOINTERRUPT) && tpgrp == -1) {
841 xclose(0);
842 (void) xopen(_PATH_DEVNULL, O_RDONLY|O_LARGEFILE);
844 else {
845 xclose(0);
846 (void) dup(OLDSTD);
847 #if defined(CLOSE_ON_EXEC) && defined(CLEX_DUPS)
849 * PWP: Unlike Bezerkeley 4.3, FIONCLEX for Pyramid is preserved
850 * across dup()s, so we have to UNSET it here or else we get a
851 * command with NO stdin, stdout, or stderr at all (a bad thing
852 * indeed)
854 (void) close_on_exec(0, 0);
855 #endif /* CLOSE_ON_EXEC && CLEX_DUPS */
858 if (t->t_drit) {
859 char *tmp;
861 cp = splicepipe(t, t->t_drit);
862 tmp = strsave(short2str(cp));
863 xfree(cp);
864 cleanup_push(tmp, xfree);
866 * so > /dev/std{out,err} work
868 (void) dcopy(SHOUT, 1);
869 (void) dcopy(SHDIAG, 2);
870 if ((flags & F_APPEND) != 0) {
871 #ifdef O_APPEND
872 fd = xopen(tmp, O_WRONLY|O_APPEND|O_LARGEFILE);
873 #else /* !O_APPEND */
874 fd = xopen(tmp, O_WRONLY|O_LARGEFILE);
875 (void) lseek(fd, (off_t) 0, L_XTND);
876 #endif /* O_APPEND */
878 else
879 fd = 0;
880 if ((flags & F_APPEND) == 0 || fd == -1) {
881 if (!(flags & F_OVERWRITE) && adrof(STRnoclobber)) {
882 if (flags & F_APPEND)
883 stderror(ERR_SYSTEM, tmp, strerror(errno));
884 chkclob(tmp);
886 if ((fd = xcreat(tmp, 0666)) < 0)
887 stderror(ERR_SYSTEM, tmp, strerror(errno));
888 /* allow input files larger than 2Gb */
889 #ifndef WINNT_NATIVE
890 (void) fcntl(fd, F_SETFL, fcntl(fd, F_GETFL) | O_LARGEFILE);
891 #endif /*!WINNT_NATIVE*/
893 cleanup_until(tmp);
894 (void) dmove(fd, 1);
895 is1atty = isatty(1);
897 else if (flags & F_PIPEOUT) {
898 xclose(1);
899 (void) dup(pipeout[1]);
900 is1atty = 0;
902 else {
903 xclose(1);
904 (void) dup(SHOUT);
905 is1atty = isoutatty;
906 # if defined(CLOSE_ON_EXEC) && defined(CLEX_DUPS)
907 (void) close_on_exec(1, 0);
908 # endif /* CLOSE_ON_EXEC && CLEX_DUPS */
911 xclose(2);
912 if (flags & F_STDERR) {
913 (void) dup(1);
914 is2atty = is1atty;
916 else {
917 (void) dup(SHDIAG);
918 is2atty = isdiagatty;
919 # if defined(CLOSE_ON_EXEC) && defined(CLEX_DUPS)
920 (void) close_on_exec(2, 0);
921 # endif /* CLOSE_ON_EXEC && CLEX_DUPS */
923 didfds = 1;
926 void
927 mypipe(int *pv)
930 if (pipe(pv) < 0)
931 goto oops;
932 (void)close_on_exec(pv[0] = dmove(pv[0], -1), 1);
933 (void)close_on_exec(pv[1] = dmove(pv[1], -1), 1);
934 if (pv[0] >= 0 && pv[1] >= 0)
935 return;
936 if (pv[0] >= 0)
937 xclose(pv[0]);
938 if (pv[1] >= 0)
939 xclose(pv[1]);
940 oops:
941 stderror(ERR_PIPE);
944 static void
945 chkclob(const char *cp)
947 struct stat stb;
949 if (stat(cp, &stb) < 0)
950 return;
951 if (S_ISCHR(stb.st_mode))
952 return;
953 stderror(ERR_EXISTS, cp);