/***********************************************************************
* *
* This software is part of the ast package *
* Copyright (c) 1982-2012 AT&T Intellectual Property *
* and is licensed under the *
* Eclipse Public License, Version 1.0 *
* by AT&T Intellectual Property *
* *
* A copy of the License is available at *
* (with md5 checksum b35adb5213ca9657e911e9befb180842) *
* *
* Information and Software Systems Research *
* AT&T Research *
* Florham Park NJ *
* *
* David Korn <dgk@research.att.com> *
* *
***********************************************************************/
#pragma prototyped
/*
* Create and manage subshells avoiding forks when possible
*
* David Korn
* AT&T Labs
*
*/
#include "defs.h"
#include <ls.h>
#include "io.h"
#include "fault.h"
#include "shnodes.h"
#include "shlex.h"
#include "jobs.h"
#include "variables.h"
#include "path.h"
#ifndef PIPE_BUF
#endif
#ifndef O_SEARCH
# ifdef O_PATH
# else
# define O_SEARCH 0
# endif
#endif
/*
* Note that the following structure must be the same
* size as the Dtlink_t structure
*/
struct Link
{
};
/*
* The following structure is used for command substitution and (...)
*/
static struct subshell
{
#if (ERROR_VERSION >= 20030214L)
#else
#endif
char jobcontrol;
char monitor;
unsigned char fdstatus;
int coutpipe;
int cpipe;
int nofork;
int subdup;
char subshare;
char comsub;
char pwdclose;
#if SHOPT_COSHELL
void *coshell;
#endif /* SHOPT_COSHELL */
} *subshell_data;
static int subenv;
/*
* This routine will turn the sftmp() file into a real /tmp file or pipe
* if the /tmp file create fails
*/
{
{
register int fd;
/* save file descriptor 1 if open */
{
close(1);
}
/* popping a discipline forces a /tmp file create */
{
/* unable to create the /tmp file so use a pipe */
fds[2] = 0;
/* write the data to the pipe */
}
else
{
if(fd==1)
else
{
}
}
}
}
/*
* This routine creates a temp file if necessary and creates a subshell.
* The parent routine longjmps back to sh_subshell()
* The child continues possibly with its standard output replaced by temp file
*/
void sh_subfork(void)
{
if(trap)
/* see whether inside $(...) */
{
/* this is the parent part of the fork */
if(trap)
}
else
{
/* this is the child part of the fork */
/* setting subpid to 1 causes subshell to exit when reached */
subshell_data = 0;
SH_SUBSHELLNOD->nvalue.s = 0;
}
}
{
{
{
return(1);
}
}
return(0);
}
/*
* This routine will make a copy of the given node in the
* layer created by the most recent subshell_fork if the
* node hasn't already been copied
*/
{
int save;
/* don't bother with this */
return(np);
/* don't bother to save if in newer scope */
return(np);
{
return(np);
}
{
return(np);
}
/* first two pointers use linkage from np */
{
{
break;
}
}
return(np);
}
/*
* restore the variables
*/
{
{
continue;
flags = 0;
flags |= NV_MINIMAL;
if(nv_isarray(mp))
if(nv_isarray(np))
{
goto skip;
}
if(!(flags&NV_MINIMAL))
if(!nofree)
{
}
else
{
}
skip:
{
}
}
}
/*
* return pointer to alias tree
* create new one if in a subshell and one doesn't exist and create is non-zero
*/
{
return(sh.alias_tree);
{
}
}
/*
* return pointer to function tree
* create new one if in a subshell and one doesn't exist and create is non-zero
*/
{
{
}
}
{
int flag;
{
flag=0;
{
}
else
}
}
{
register int old=0;
if(sp)
{
}
return(old);
}
{
while(sp)
{
{
return;
}
}
}
/*
* Run command tree <t> in a virtual sub-shell
* If comsub is not null, then output will be placed in temp file (or buffer)
* If comsub is not null, the return value will be a stream consisting of
* output of command <t>. Otherwise, NULL will be returned.
*/
{
char *savsig;
int argcnt;
{
subenv = 0;
}
subshell_data = sp;
#if SHOPT_COSHELL
#endif /* SHOPT_COSHELL */
/* make sure initialization has occurred */
{
}
if(comsub)
else
if(comsub)
{
#ifdef _lib_fchdir
{
{
break;
}
}
{
if(n>=0)
{
if(n<10)
{
close(n);
}
{
}
}
}
#endif
/* save trap table */
{
nsig += sizeof(char*);
/* this nonsense needed for $(trap) */
}
sh_sigreset(0);
}
if(jmpval==0)
{
if(comsub)
{
/* disable job control */
job.jobcontrol=0;
/* save sfstdout and status */
/* use sftmp() file for standard output */
{
}
}
{
}
{
}
}
{
/* trap on EXIT not handled by child */
}
{
if(jmpval==SH_JMPSCRIPT)
}
nv_restore(sp);
if(comsub)
{
/* re-enable job control */
{
/* sftmp() file has been returned into pipe */
}
else
{
/* move tmp file to iop and restore sfstdout */
if(!iop)
{
/* maybe locked try again */
}
{
if(fd<0)
{
}
}
}
/* check if standard output was preserved */
{
close(1);
duped++;
}
}
{
int n;
{
}
{
}
sh_sigreset(1);
if(n>0)
if(nsig)
{
}
{
/* restore PWDNOD */
{
{
}
else
}
}
{
}
else
{
}
}
#if SHOPT_COSHELL
#endif /* SHOPT_COSHELL */
{
else
{
}
}
if(nsig>0)
{
}
if(duped)
{
}
return(iop);
}