#include "sh.h"
RCSID("$Id: sh.sem.c,v 1.1.1.2 2001/06/28 23:10:52 bbraun Exp $")
#include "tc.h"
#include "tw.h"
#ifdef WINNT_NATIVE
#include "nt.const.h"
#endif
#ifdef CLOSE_ON_EXEC
# ifndef SUNOS4
# ifndef CLEX_DUPS
# define CLEX_DUPS
# endif
# endif
#endif
#if defined(__sparc__) || defined(sparc)
# if !defined(MACH) && SYSVREL == 0 && !defined(Lynx) && !defined(BSD4_4) && !defined(linux)
# include <vfork.h>
# endif
#endif
#ifdef VFORK
static sigret_t vffree __P((int));
#endif
static Char *splicepipe __P((struct command *, Char *));
static void doio __P((struct command *, int *, int *));
static void chkclob __P((char *));
void
execute(t, wanttty, pipein, pipeout)
register struct command *t;
int wanttty;
int *pipein, *pipeout;
{
#ifdef VFORK
extern bool use_fork;
#endif
bool forked = 0;
struct biltins *bifunc;
int pid = 0;
int pv[2];
#ifdef BSDSIGS
static sigmask_t csigmask;
#endif
#ifdef VFORK
static int onosigchld = 0;
#endif
static int nosigchld = 0;
(void) &wanttty;
(void) &forked;
(void) &bifunc;
if (t == 0)
return;
#ifdef WINNT_NATIVE
{
if ((varval(STRNTslowexec) == STRNULL) &&
!t->t_dcdr && !t->t_dcar && !t->t_dflg && !didfds &&
(intty || intact) && (t->t_dtyp == NODE_COMMAND) &&
!isbfunc(t)) {
if ((t->t_dcom[0][0] & (QUOTE | TRIM)) == QUOTE)
(void) Strcpy(t->t_dcom[0], t->t_dcom[0] + 1);
Dfix(t);
if (nt_try_fast_exec(t) == 0)
return;
}
}
#endif
if (implicit_cd && (intty || intact) && t->t_dcom && t->t_dcom[0] &&
t->t_dcom[0][0] && (blklen(t->t_dcom) == 1) && !noexec) {
Char sCName[MAXPATHLEN];
Char *pCN;
struct stat stbuf;
char *pathname;
dollar(sCName, t->t_dcom[0]);
pCN = sCName;
if (pCN[0] == '~') {
Char sCPath[MAXPATHLEN];
Char *pCP = sCPath;
++pCN;
while (*pCN && *pCN != '/')
*pCP++ = *pCN++;
*pCP = 0;
if (sCPath[0])
gethdir(sCPath);
else
(void) Strcpy(sCPath, varval(STRhome));
catn(sCPath, pCN, MAXPATHLEN);
(void) Strcpy(sCName, sCPath);
}
pathname = short2str(sCName);
if ((stat(pathname, &stbuf) != -1 && S_ISDIR(stbuf.st_mode))
#ifdef WINNT_NATIVE
|| (pathname[0] && pathname[1] == ':' && pathname[2] == '\0')
#endif
) {
Char *vCD[2];
Char **ot_dcom = t->t_dcom;
vCD[0] = Strsave(STRcd);
vCD[1] = NULL;
t->t_dcom = blkspl(vCD, ot_dcom);
if (implicit_cd > 1) {
blkpr(t->t_dcom);
xputchar( '\n' );
}
xfree((ptr_t) ot_dcom);
}
}
if (t->t_dflg & F_AMPERSAND)
wanttty = 0;
switch (t->t_dtyp) {
case NODE_COMMAND:
if ((t->t_dcom[0][0] & (QUOTE | TRIM)) == QUOTE)
(void) Strcpy(t->t_dcom[0], t->t_dcom[0] + 1);
if ((t->t_dflg & F_REPEAT) == 0)
Dfix(t);
if (t->t_dcom[0] == 0) {
return;
}
case NODE_PAREN:
#ifdef BACKPIPE
if (t->t_dflg & F_PIPEIN)
mypipe(pipein);
#else
if (t->t_dflg & F_PIPEOUT)
mypipe(pipeout);
#endif
if (t->t_dflg & F_READ) {
(void) close(0);
heredoc(t->t_dlef);
if (noexec)
(void) close(0);
}
set(STRstatus, Strsave(STR0), VAR_READWRITE);
while (t->t_dtyp == NODE_COMMAND)
if (eq(t->t_dcom[0], STRnice)) {
if (t->t_dcom[1]) {
if (strchr("+-", t->t_dcom[1][0])) {
if (t->t_dcom[2]) {
setname("nice");
t->t_nice =
getn(t->t_dcom[1]);
lshift(t->t_dcom, 2);
t->t_dflg |= F_NICE;
}
else
break;
}
else {
t->t_nice = 4;
lshift(t->t_dcom, 1);
t->t_dflg |= F_NICE;
}
}
else
break;
}
else if (eq(t->t_dcom[0], STRnohup)) {
if (t->t_dcom[1]) {
t->t_dflg |= F_NOHUP;
lshift(t->t_dcom, 1);
}
else
break;
}
else if (eq(t->t_dcom[0], STRhup)) {
if (t->t_dcom[1]) {
t->t_dflg |= F_HUP;
lshift(t->t_dcom, 1);
}
else
break;
}
else if (eq(t->t_dcom[0], STRtime)) {
if (t->t_dcom[1]) {
t->t_dflg |= F_TIME;
lshift(t->t_dcom, 1);
}
else
break;
}
#ifdef F_VER
else if (eq(t->t_dcom[0], STRver))
if (t->t_dcom[1] && t->t_dcom[2]) {
setname("ver");
t->t_systype = getv(t->t_dcom[1]);
lshift(t->t_dcom, 2);
t->t_dflg |= F_VER;
}
else
break;
#endif
else
break;
if (t->t_dtyp == NODE_COMMAND) {
bifunc = isbfunc(t);
if (noexec && bifunc) {
if (bifunc->bfunct != (bfunc_t)dobreak &&
bifunc->bfunct != (bfunc_t)docontin &&
bifunc->bfunct != (bfunc_t)doelse &&
bifunc->bfunct != (bfunc_t)doend &&
bifunc->bfunct != (bfunc_t)doforeach&&
bifunc->bfunct != (bfunc_t)dogoto &&
bifunc->bfunct != (bfunc_t)doif &&
bifunc->bfunct != (bfunc_t)dorepeat &&
bifunc->bfunct != (bfunc_t)doswbrk &&
bifunc->bfunct != (bfunc_t)doswitch &&
bifunc->bfunct != (bfunc_t)dowhile &&
bifunc->bfunct != (bfunc_t)dozip)
break;
}
}
else {
bifunc = NULL;
if (noexec)
break;
}
#ifdef BACKPIPE
if (t->t_dflg & F_PIPEIN)
t->t_dflg &= ~(F_NOFORK);
#endif
if (bifunc && (bifunc->bfunct == (bfunc_t)dochngd ||
bifunc->bfunct == (bfunc_t)dopushd ||
bifunc->bfunct == (bfunc_t)dopopd))
t->t_dflg &= ~(F_NICE);
if (((t->t_dflg & F_TIME) || ((t->t_dflg & F_NOFORK) == 0 &&
(!bifunc || t->t_dflg &
(F_PIPEOUT | F_AMPERSAND | F_NICE | F_NOHUP | F_HUP)))) ||
(bifunc && (t->t_dflg & F_PIPEIN) != 0 &&
bifunc->bfunct == (bfunc_t)doeval)) {
#ifdef VFORK
if (t->t_dtyp == NODE_PAREN ||
t->t_dflg & (F_REPEAT | F_AMPERSAND) || bifunc)
#endif
{
forked++;
if (wanttty >= 0 && !nosigchld) {
#ifdef BSDSIGS
csigmask = sigblock(sigmask(SIGCHLD));
#else
(void) sighold(SIGCHLD);
#endif
nosigchld = 1;
}
pid = pfork(t, wanttty);
if (pid == 0 && nosigchld) {
#ifdef BSDSIGS
(void) sigsetmask(csigmask);
#else
(void) sigrelse(SIGCHLD);
#endif
nosigchld = 0;
}
else if (pid != 0 && (t->t_dflg & F_AMPERSAND))
backpid = pid;
}
#ifdef VFORK
else {
int ochild, osetintr, ohaderr, odidfds;
int oSHIN, oSHOUT, oSHDIAG, oOLDSTD, otpgrp;
int oisoutatty, oisdiagatty;
# ifndef CLOSE_ON_EXEC
int odidcch;
# endif
# ifdef BSDSIGS
sigmask_t omask, ocsigmask;
# endif
# ifdef SAVESIGVEC
sigvec_t savesv[NSIGSAVED];
sigmask_t savesm;
# endif
if (wanttty >= 0 && !nosigchld && !noexec) {
# ifdef BSDSIGS
csigmask = sigblock(sigmask(SIGCHLD));
# else
(void) sighold(SIGCHLD);
# endif
nosigchld = 1;
}
# ifdef BSDSIGS
omask = sigblock(sigmask(SIGCHLD)|sigmask(SIGINT));
# else
(void) sighold(SIGCHLD);
(void) sighold(SIGINT);
# endif
ochild = child;
osetintr = setintr;
ohaderr = haderr;
odidfds = didfds;
# ifndef CLOSE_ON_EXEC
odidcch = didcch;
# endif
oSHIN = SHIN;
oSHOUT = SHOUT;
oSHDIAG = SHDIAG;
oOLDSTD = OLDSTD;
otpgrp = tpgrp;
oisoutatty = isoutatty;
oisdiagatty = isdiagatty;
# ifdef BSDSIGS
ocsigmask = csigmask;
# endif
onosigchld = nosigchld;
Vsav = Vdp = 0;
Vexpath = 0;
Vt = 0;
# ifdef SAVESIGVEC
savesm = savesigvec(savesv);
# endif
if (use_fork)
pid = fork();
else
pid = vfork();
if (pid < 0) {
# ifdef BSDSIGS
# ifdef SAVESIGVEC
restoresigvec(savesv, savesm);
# endif
(void) sigsetmask(omask);
# else
(void) sigrelse(SIGCHLD);
(void) sigrelse(SIGINT);
# endif
stderror(ERR_NOPROC);
}
forked++;
if (pid) {
# ifdef SAVESIGVEC
restoresigvec(savesv, savesm);
# endif
child = ochild;
setintr = osetintr;
haderr = ohaderr;
didfds = odidfds;
SHIN = oSHIN;
# ifndef CLOSE_ON_EXEC
didcch = odidcch;
# endif
SHOUT = oSHOUT;
SHDIAG = oSHDIAG;
OLDSTD = oOLDSTD;
tpgrp = otpgrp;
isoutatty = oisoutatty;
isdiagatty = oisdiagatty;
# ifdef BSDSIGS
csigmask = ocsigmask;
# endif
nosigchld = onosigchld;
xfree((ptr_t) Vsav);
Vsav = 0;
xfree((ptr_t) Vdp);
Vdp = 0;
xfree((ptr_t) Vexpath);
Vexpath = 0;
blkfree((Char **) Vt);
Vt = 0;
palloc(pid, t);
# ifdef BSDSIGS
(void) sigsetmask(omask);
# else
(void) sigrelse(SIGCHLD);
(void) sigrelse(SIGINT);
# endif
}
else {
int pgrp;
bool ignint = 0;
if (nosigchld) {
# ifdef BSDSIGS
(void) sigsetmask(csigmask);
# else
(void) sigrelse(SIGCHLD);
# endif
nosigchld = 0;
}
if (setintr)
ignint = (tpgrp == -1 && (t->t_dflg & F_NOINTERRUPT))
|| (gointr && eq(gointr, STRminus));
pgrp = pcurrjob ? pcurrjob->p_jobid : getpid();
child++;
if (setintr) {
setintr = 0;
if (ignint) {
(void) signal(SIGINT, SIG_IGN);
(void) signal(SIGQUIT, SIG_IGN);
}
else {
(void) signal(SIGINT, vffree);
(void) signal(SIGQUIT, SIG_DFL);
}
# ifdef BSDJOBS
if (wanttty >= 0) {
(void) signal(SIGTSTP, SIG_DFL);
(void) signal(SIGTTIN, SIG_DFL);
(void) signal(SIGTTOU, SIG_DFL);
}
# endif
(void) signal(SIGTERM, parterm);
}
else if (tpgrp == -1 &&
(t->t_dflg & F_NOINTERRUPT)) {
(void) signal(SIGINT, SIG_IGN);
(void) signal(SIGQUIT, SIG_IGN);
}
pgetty(wanttty, pgrp);
if (t->t_dflg & F_NOHUP)
(void) signal(SIGHUP, SIG_IGN);
if (t->t_dflg & F_HUP)
(void) signal(SIGHUP, SIG_DFL);
if (t->t_dflg & F_NICE) {
int nval = SIGN_EXTEND_CHAR(t->t_nice);
# ifdef BSDNICE
(void) setpriority(PRIO_PROCESS, 0, nval);
# else
(void) nice(nval);
# endif
}
# ifdef F_VER
if (t->t_dflg & F_VER) {
tsetenv(STRSYSTYPE, t->t_systype ? STRbsd43 : STRsys53);
dohash(NULL, NULL);
}
# endif
}
}
#endif
}
if (pid != 0) {
#ifdef BACKPIPE
if (didfds == 0 && t->t_dflg & F_PIPEOUT) {
(void) close(pipeout[0]);
(void) close(pipeout[1]);
}
if ((t->t_dflg & F_PIPEIN) != 0)
break;
#else
if (didfds == 0 && t->t_dflg & F_PIPEIN) {
(void) close(pipein[0]);
(void) close(pipein[1]);
}
if ((t->t_dflg & F_PIPEOUT) != 0)
break;
#endif
if (nosigchld) {
#ifdef BSDSIGS
(void) sigsetmask(csigmask);
#else
(void) sigrelse(SIGCHLD);
#endif
nosigchld = 0;
}
if ((t->t_dflg & F_AMPERSAND) == 0)
pwait();
break;
}
doio(t, pipein, pipeout);
#ifdef BACKPIPE
if (t->t_dflg & F_PIPEIN) {
(void) close(pipein[0]);
(void) close(pipein[1]);
}
#else
if (t->t_dflg & F_PIPEOUT) {
(void) close(pipeout[0]);
(void) close(pipeout[1]);
}
#endif
if (bifunc) {
func(t, bifunc);
if (forked)
exitstat();
break;
}
if (t->t_dtyp != NODE_PAREN) {
doexec(t);
}
OLDSTD = dcopy(0, FOLDSTD);
SHOUT = dcopy(1, FSHOUT);
isoutatty = isatty(SHOUT);
SHDIAG = dcopy(2, FSHDIAG);
isdiagatty = isatty(SHDIAG);
(void) close(SHIN);
SHIN = -1;
#ifndef CLOSE_ON_EXEC
didcch = 0;
#endif
didfds = 0;
wanttty = -1;
t->t_dspr->t_dflg |= t->t_dflg & F_NOINTERRUPT;
execute(t->t_dspr, wanttty, NULL, NULL);
exitstat();
case NODE_PIPE:
#ifdef BACKPIPE
t->t_dcdr->t_dflg |= F_PIPEIN | (t->t_dflg &
(F_PIPEOUT | F_AMPERSAND | F_NOFORK | F_NOINTERRUPT));
execute(t->t_dcdr, wanttty, pv, pipeout);
t->t_dcar->t_dflg |= F_PIPEOUT |
(t->t_dflg & (F_PIPEIN | F_AMPERSAND | F_STDERR | F_NOINTERRUPT));
execute(t->t_dcar, wanttty, pipein, pv);
#else
t->t_dcar->t_dflg |= F_PIPEOUT |
(t->t_dflg & (F_PIPEIN | F_AMPERSAND | F_STDERR | F_NOINTERRUPT));
execute(t->t_dcar, wanttty, pipein, pv);
t->t_dcdr->t_dflg |= F_PIPEIN | (t->t_dflg &
(F_PIPEOUT | F_AMPERSAND | F_NOFORK | F_NOINTERRUPT));
execute(t->t_dcdr, wanttty, pv, pipeout);
#endif
break;
case NODE_LIST:
if (t->t_dcar) {
t->t_dcar->t_dflg |= t->t_dflg & F_NOINTERRUPT;
execute(t->t_dcar, wanttty, NULL, NULL);
if (t->t_dcar->t_dflg & F_AMPERSAND && t->t_dcdr &&
(t->t_dcdr->t_dflg & F_AMPERSAND) == 0)
pendjob();
}
if (t->t_dcdr) {
t->t_dcdr->t_dflg |= t->t_dflg &
(F_NOFORK | F_NOINTERRUPT);
execute(t->t_dcdr, wanttty, NULL, NULL);
}
break;
case NODE_OR:
case NODE_AND:
if (t->t_dcar) {
t->t_dcar->t_dflg |= t->t_dflg & F_NOINTERRUPT;
execute(t->t_dcar, wanttty, NULL, NULL);
if ((getn(varval(STRstatus)) == 0) !=
(t->t_dtyp == NODE_AND)) {
return;
}
}
if (t->t_dcdr) {
t->t_dcdr->t_dflg |= t->t_dflg &
(F_NOFORK | F_NOINTERRUPT);
execute(t->t_dcdr, wanttty, NULL, NULL);
}
break;
default:
break;
}
if (didfds && !(t->t_dflg & F_REPEAT))
donefds();
}
#ifdef VFORK
static sigret_t
vffree(snum)
int snum;
{
register Char **v;
USE(snum);
if ((v = gargv) != 0) {
gargv = 0;
xfree((ptr_t) v);
}
if ((v = pargv) != 0) {
pargv = 0;
xfree((ptr_t) v);
}
_exit(1);
#ifndef SIGVOID
return(0);
#endif
}
#endif
static Char *
splicepipe(t, cp)
register struct command *t;
Char *cp;
{
Char *blk[2];
if (adrof(STRnoambiguous)) {
Char **pv;
blk[0] = Dfix1(cp);
blk[1] = NULL;
gflag = 0, tglob(blk);
if (gflag) {
pv = globall(blk);
if (pv == NULL) {
setname(short2str(blk[0]));
xfree((ptr_t) blk[0]);
stderror(ERR_NAME | ERR_NOMATCH);
}
gargv = NULL;
if (pv[1] != NULL) {
Char **av = blkspl(t->t_dcom, &pv[1]);
xfree((ptr_t) t->t_dcom);
t->t_dcom = av;
}
xfree((ptr_t) blk[0]);
blk[0] = pv[0];
xfree((ptr_t) pv);
}
}
else {
Char buf[BUFSIZE];
(void) Strcpy(buf, blk[1] = Dfix1(cp));
xfree((ptr_t) blk[1]);
blk[0] = globone(buf, G_ERROR);
}
return(blk[0]);
}
static void
doio(t, pipein, pipeout)
register struct command *t;
int *pipein, *pipeout;
{
register int fd;
register Char *cp;
register unsigned long flags = t->t_dflg;
if (didfds || (flags & F_REPEAT))
return;
if ((flags & F_READ) == 0) {
if (t->t_dlef) {
char tmp[MAXPATHLEN+1];
(void) dcopy(SHIN, 0);
(void) dcopy(SHOUT, 1);
(void) dcopy(SHDIAG, 2);
cp = splicepipe(t, t->t_dlef);
(void) strncpy(tmp, short2str(cp), MAXPATHLEN);
tmp[MAXPATHLEN] = '\0';
xfree((ptr_t) cp);
if ((fd = open(tmp, O_RDONLY)) < 0)
stderror(ERR_SYSTEM, tmp, strerror(errno));
#ifdef O_LARGEFILE
(void) fcntl(fd, O_LARGEFILE, 0);
#endif
(void) dmove(fd, 0);
}
else if (flags & F_PIPEIN) {
(void) close(0);
(void) dup(pipein[0]);
(void) close(pipein[0]);
(void) close(pipein[1]);
}
else if ((flags & F_NOINTERRUPT) && tpgrp == -1) {
(void) close(0);
(void) open(_PATH_DEVNULL, O_RDONLY);
}
else {
(void) close(0);
(void) dup(OLDSTD);
#if defined(CLOSE_ON_EXEC) && defined(CLEX_DUPS)
(void) close_on_exec(0, 0);
#endif
}
}
if (t->t_drit) {
char tmp[MAXPATHLEN+1];
cp = splicepipe(t, t->t_drit);
(void) strncpy(tmp, short2str(cp), MAXPATHLEN);
tmp[MAXPATHLEN] = '\0';
xfree((ptr_t) cp);
(void) dcopy(SHOUT, 1);
(void) dcopy(SHDIAG, 2);
if ((flags & F_APPEND) != 0) {
#ifdef O_APPEND
fd = open(tmp, O_WRONLY | O_APPEND);
#else
fd = open(tmp, O_WRONLY);
(void) lseek(fd, (off_t) 0, L_XTND);
#endif
}
else
fd = 0;
if ((flags & F_APPEND) == 0 || fd == -1) {
if (!(flags & F_OVERWRITE) && adrof(STRnoclobber)) {
if (flags & F_APPEND)
stderror(ERR_SYSTEM, tmp, strerror(errno));
chkclob(tmp);
}
if ((fd = creat(tmp, 0666)) < 0)
stderror(ERR_SYSTEM, tmp, strerror(errno));
#ifdef O_LARGEFILE
(void) fcntl(fd, O_LARGEFILE, 0);
#endif
}
(void) dmove(fd, 1);
is1atty = isatty(1);
}
else if (flags & F_PIPEOUT) {
(void) close(1);
(void) dup(pipeout[1]);
is1atty = 0;
}
else {
(void) close(1);
(void) dup(SHOUT);
is1atty = isoutatty;
# if defined(CLOSE_ON_EXEC) && defined(CLEX_DUPS)
(void) close_on_exec(1, 0);
# endif
}
(void) close(2);
if (flags & F_STDERR) {
(void) dup(1);
is2atty = is1atty;
}
else {
(void) dup(SHDIAG);
is2atty = isdiagatty;
# if defined(CLOSE_ON_EXEC) && defined(CLEX_DUPS)
(void) close_on_exec(2, 0);
# endif
}
didfds = 1;
}
void
mypipe(pv)
register int *pv;
{
if (pipe(pv) < 0)
goto oops;
pv[0] = dmove(pv[0], -1);
pv[1] = dmove(pv[1], -1);
if (pv[0] >= 0 && pv[1] >= 0)
return;
oops:
stderror(ERR_PIPE);
}
static void
chkclob(cp)
register char *cp;
{
struct stat stb;
if (stat(cp, &stb) < 0)
return;
if (S_ISCHR(stb.st_mode))
return;
stderror(ERR_EXISTS, cp);
}