Home | History | Annotate | Line # | Download | only in ksh
main.c revision 1.3
      1  1.3  christos /*	$NetBSD: main.c,v 1.3 1997/07/20 17:42:08 christos Exp $	*/
      2  1.2       tls 
      3  1.1       jtc /*
      4  1.1       jtc  * startup, main loop, enviroments and error handling
      5  1.1       jtc  */
      6  1.1       jtc 
      7  1.1       jtc #define	EXTERN				/* define EXTERNs in sh.h */
      8  1.1       jtc 
      9  1.1       jtc #include "sh.h"
     10  1.1       jtc #include "ksh_stat.h"
     11  1.1       jtc #include "ksh_time.h"
     12  1.1       jtc 
     13  1.1       jtc extern char **environ;
     14  1.1       jtc 
     15  1.1       jtc /*
     16  1.1       jtc  * global data
     17  1.1       jtc  */
     18  1.1       jtc 
     19  1.2       tls static void	reclaim ARGS((void));
     20  1.2       tls static void	remove_temps ARGS((struct temp *tp));
     21  1.2       tls static int	is_restricted ARGS((char *name));
     22  1.1       jtc 
     23  1.1       jtc /*
     24  1.1       jtc  * shell initialization
     25  1.1       jtc  */
     26  1.1       jtc 
     27  1.2       tls static const char	initifs [] = "IFS= \t\n"; /* must be R/W */
     28  1.1       jtc 
     29  1.2       tls static const	char   initsubs [] =
     30  1.1       jtc   "${PS2=> } ${PS3=#? } ${PS4=+ }";
     31  1.1       jtc 
     32  1.1       jtc static const char version_param[] =
     33  1.1       jtc #ifdef KSH
     34  1.1       jtc 	"KSH_VERSION"
     35  1.1       jtc #else /* KSH */
     36  1.1       jtc 	"SH_VERSION"
     37  1.1       jtc #endif /* KSH */
     38  1.1       jtc 	;
     39  1.1       jtc 
     40  1.2       tls static const char *const initcoms [] = {
     41  1.1       jtc 	"typeset", "-x", "SHELL", "PATH", "HOME", NULL,
     42  1.1       jtc 	"typeset", "-r", version_param, NULL,
     43  1.1       jtc 	"typeset", "-ri", "PPID", NULL,
     44  1.1       jtc 	"typeset", "-i", "OPTIND=1",
     45  1.1       jtc #ifdef KSH
     46  1.1       jtc 	    "MAILCHECK=600", "RANDOM", "SECONDS=0", "TMOUT=0",
     47  1.1       jtc #endif /* KSH */
     48  1.1       jtc 	    NULL,
     49  1.1       jtc 	"alias",
     50  1.1       jtc 	 /* Standard ksh aliases */
     51  1.1       jtc 	  "hash=alias -t",	/* not "alias -t --": hash -r needs to work */
     52  1.1       jtc 	  "type=whence -v",
     53  1.1       jtc #ifdef JOBS
     54  1.1       jtc 	  "stop=kill -STOP",
     55  1.1       jtc 	  "suspend=kill -STOP $$",
     56  1.1       jtc #endif
     57  1.1       jtc #ifdef KSH
     58  1.1       jtc 	  "autoload=typeset -fu",
     59  1.1       jtc 	  "functions=typeset -f",
     60  1.2       tls # ifdef HISTORY
     61  1.1       jtc 	  "history=fc -l",
     62  1.2       tls # endif /* HISTORY */
     63  1.1       jtc 	  "integer=typeset -i",
     64  1.1       jtc 	  "nohup=nohup ",
     65  1.1       jtc 	  "local=typeset",
     66  1.1       jtc 	  "r=fc -e -",
     67  1.1       jtc #endif /* KSH */
     68  1.1       jtc #ifdef KSH
     69  1.1       jtc 	 /* Aliases that are builtin commands in at&t */
     70  1.1       jtc 	  "login=exec login",
     71  1.1       jtc 	  "newgrp=exec newgrp",
     72  1.1       jtc #endif /* KSH */
     73  1.1       jtc 	  NULL,
     74  1.1       jtc 	/* this is what at&t ksh seems to track, with the addition of emacs */
     75  1.1       jtc 	"alias", "-tU",
     76  1.1       jtc 	  "cat", "cc", "chmod", "cp", "date", "ed", "emacs", "grep", "ls",
     77  1.1       jtc 	  "mail", "make", "mv", "pr", "rm", "sed", "sh", "vi", "who",
     78  1.1       jtc 	  NULL,
     79  1.1       jtc #ifdef EXTRA_INITCOMS
     80  1.1       jtc 	EXTRA_INITCOMS, NULL,
     81  1.1       jtc #endif /* EXTRA_INITCOMS */
     82  1.1       jtc 	NULL
     83  1.1       jtc };
     84  1.3  christos 
     85  1.3  christos int main __P((int, char **));
     86  1.1       jtc 
     87  1.1       jtc int
     88  1.1       jtc main(argc, argv)
     89  1.1       jtc 	int argc;
     90  1.1       jtc 	register char **argv;
     91  1.1       jtc {
     92  1.1       jtc 	register int i;
     93  1.1       jtc 	int argi;
     94  1.1       jtc 	Source *s;
     95  1.1       jtc 	struct block *l;
     96  1.1       jtc 	int restricted;
     97  1.1       jtc 	char **wp;
     98  1.1       jtc 	struct env env;
     99  1.1       jtc 	int euid;
    100  1.1       jtc 
    101  1.1       jtc #ifdef MEM_DEBUG
    102  1.1       jtc 	chmem_push("+c", 1);
    103  1.1       jtc 	/*chmem_push("+cd", 1);*/
    104  1.1       jtc #endif
    105  1.1       jtc 
    106  1.1       jtc #ifdef OS2
    107  1.1       jtc 	setmode (0, O_BINARY);
    108  1.1       jtc 	setmode (1, O_TEXT);
    109  1.1       jtc #endif
    110  1.1       jtc 
    111  1.1       jtc 	/* make sure argv[] is sane */
    112  1.1       jtc 	if (!*argv) {
    113  1.1       jtc 		static const char	*empty_argv[] = {
    114  1.1       jtc 					    "pdksh", (char *) 0
    115  1.1       jtc 					};
    116  1.1       jtc 
    117  1.1       jtc 		argv = (char **) empty_argv;
    118  1.1       jtc 		argc = 1;
    119  1.1       jtc 	}
    120  1.1       jtc 	kshname = *argv;
    121  1.1       jtc 
    122  1.1       jtc 	ainit(&aperm);		/* initialize permanent Area */
    123  1.1       jtc 
    124  1.1       jtc 	/* set up base enviroment */
    125  1.2       tls 	memset(&env, 0, sizeof(env));
    126  1.1       jtc 	env.type = E_NONE;
    127  1.1       jtc 	ainit(&env.area);
    128  1.1       jtc 	e = &env;
    129  1.1       jtc 	newblock();		/* set up global l->vars and l->funs */
    130  1.1       jtc 
    131  1.1       jtc 	/* Do this first so output routines (eg, errorf, shellf) can work */
    132  1.1       jtc 	initio();
    133  1.1       jtc 
    134  1.1       jtc 	initvar();
    135  1.1       jtc 
    136  1.1       jtc 	initctypes();
    137  1.1       jtc 
    138  1.1       jtc 	inittraps();
    139  1.1       jtc 
    140  1.1       jtc #ifdef KSH
    141  1.1       jtc 	coproc_init();
    142  1.1       jtc #endif /* KSH */
    143  1.1       jtc 
    144  1.1       jtc 	/* set up variable and command dictionaries */
    145  1.1       jtc 	tinit(&taliases, APERM, 0);
    146  1.1       jtc 	tinit(&aliases, APERM, 0);
    147  1.1       jtc 	tinit(&homedirs, APERM, 0);
    148  1.1       jtc 
    149  1.1       jtc 	/* define shell keywords */
    150  1.1       jtc 	initkeywords();
    151  1.1       jtc 
    152  1.1       jtc 	/* define built-in commands */
    153  1.1       jtc 	tinit(&builtins, APERM, 64); /* must be 2^n (currently 40 builtins) */
    154  1.1       jtc 	for (i = 0; shbuiltins[i].name != NULL; i++)
    155  1.1       jtc 		builtin(shbuiltins[i].name, shbuiltins[i].func);
    156  1.1       jtc 	for (i = 0; kshbuiltins[i].name != NULL; i++)
    157  1.1       jtc 		builtin(kshbuiltins[i].name, kshbuiltins[i].func);
    158  1.1       jtc 
    159  1.1       jtc 	init_histvec();
    160  1.1       jtc 
    161  1.1       jtc 	def_path = DEFAULT__PATH;
    162  1.1       jtc #if defined(HAVE_CONFSTR) && defined(_CS_PATH)
    163  1.1       jtc 	{
    164  1.1       jtc 		size_t len = confstr(_CS_PATH, (char *) 0, 0);
    165  1.1       jtc 		char *new;
    166  1.1       jtc 
    167  1.1       jtc 		if (len > 0) {
    168  1.1       jtc 			confstr(_CS_PATH, new = alloc(len + 1, APERM), len + 1);
    169  1.1       jtc 			def_path = new;
    170  1.1       jtc 		}
    171  1.1       jtc 	}
    172  1.1       jtc #endif /* HAVE_CONFSTR && _CS_PATH */
    173  1.1       jtc 	path = def_path;
    174  1.1       jtc 
    175  1.1       jtc 
    176  1.1       jtc 	/* Turn on nohup by default for how - will change to off
    177  1.1       jtc 	 * by default once people are aware of its existance
    178  1.1       jtc 	 * (at&t ksh does not have a nohup option - it always sends
    179  1.1       jtc 	 * the hup).
    180  1.1       jtc 	 */
    181  1.1       jtc 	Flag(FNOHUP) = 1;
    182  1.1       jtc 
    183  1.1       jtc 	/* Turn on brace expansion by default.  At&t ksh's that have
    184  1.1       jtc 	 * alternation always have it on.  BUT, posix doesn't have
    185  1.1       jtc 	 * brace expansion, so set this before setting up FPOSIX
    186  1.1       jtc 	 * (change_flag() clears FBRACEEXPAND when FPOSIX is set).
    187  1.1       jtc 	 */
    188  1.1       jtc #ifdef BRACE_EXPAND
    189  1.1       jtc 	Flag(FBRACEEXPAND) = 1;
    190  1.1       jtc #endif /* BRACE_EXPAND */
    191  1.1       jtc 
    192  1.1       jtc 	/* set posix flag just before environment so that it will have
    193  1.1       jtc 	 * exactly the same effect as the POSIXLY_CORRECT environment
    194  1.1       jtc 	 * variable.  If this needs to be done sooner to ensure correct posix
    195  1.1       jtc 	 * operation, an initial scan of the environment will also have
    196  1.1       jtc 	 * done sooner.
    197  1.1       jtc 	 */
    198  1.1       jtc #ifdef POSIXLY_CORRECT
    199  1.1       jtc 	change_flag(FPOSIX, OF_SPECIAL, 1);
    200  1.1       jtc #endif /* POSIXLY_CORRECT */
    201  1.1       jtc 
    202  1.1       jtc 	/* import enviroment */
    203  1.1       jtc 	if (environ != NULL)
    204  1.1       jtc 		for (wp = environ; *wp != NULL; wp++)
    205  1.1       jtc 			typeset(*wp, IMPORT|EXPORT, 0, 0, 0);
    206  1.1       jtc 
    207  1.1       jtc 	kshpid = procpid = getpid();
    208  1.1       jtc 	typeset(initifs, 0, 0, 0, 0);	/* for security */
    209  1.1       jtc 
    210  1.1       jtc 	/* assign default shell variable values */
    211  1.1       jtc 	substitute(initsubs, 0);
    212  1.1       jtc 
    213  1.1       jtc 	/* Figure out the current working directory and set $PWD */
    214  1.1       jtc 	{
    215  1.1       jtc 		struct stat s_pwd, s_dot;
    216  1.1       jtc 		struct tbl *pwd_v = global("PWD");
    217  1.1       jtc 		char *pwd = str_val(pwd_v);
    218  1.1       jtc 		char *pwdx = pwd;
    219  1.1       jtc 
    220  1.1       jtc 		/* Try to use existing $PWD if it is valid */
    221  1.1       jtc 		if (!ISABSPATH(pwd)
    222  1.1       jtc 		    || stat(pwd, &s_pwd) < 0 || stat(".", &s_dot) < 0
    223  1.1       jtc 		    || s_pwd.st_dev != s_dot.st_dev
    224  1.1       jtc 		    || s_pwd.st_ino != s_dot.st_ino)
    225  1.1       jtc 			pwdx = (char *) 0;
    226  1.1       jtc 		set_current_wd(pwdx);
    227  1.1       jtc 		if (current_wd[0])
    228  1.1       jtc 			simplify_path(current_wd);
    229  1.1       jtc 		/* Only set pwd if we know where we are or if it had a
    230  1.1       jtc 		 * bogus value
    231  1.1       jtc 		 */
    232  1.1       jtc 		if (current_wd[0] || pwd != null)
    233  1.1       jtc 			setstr(pwd_v, current_wd);
    234  1.1       jtc 	}
    235  1.1       jtc 	setint(global("PPID"), (long) getppid());
    236  1.1       jtc #ifdef KSH
    237  1.1       jtc 	setint(global("RANDOM"), (long) time((time_t *)0));
    238  1.1       jtc #endif /* KSH */
    239  1.1       jtc 	setstr(global(version_param), ksh_version);
    240  1.1       jtc 
    241  1.1       jtc 	/* execute initialization statements */
    242  1.1       jtc 	for (wp = (char**) initcoms; *wp != NULL; wp++) {
    243  1.1       jtc 		shcomexec(wp);
    244  1.1       jtc 		for (; *wp != NULL; wp++)
    245  1.1       jtc 			;
    246  1.1       jtc 	}
    247  1.1       jtc 
    248  1.1       jtc 	euid = geteuid();
    249  1.1       jtc 	safe_prompt = euid ? "$ " : "# ";
    250  1.1       jtc 	{
    251  1.1       jtc 		struct tbl *vp = global("PS1");
    252  1.1       jtc 
    253  1.1       jtc 		/* Set PS1 if it isn't set, or we are root and prompt doesn't
    254  1.1       jtc 		 * contain a #.
    255  1.1       jtc 		 */
    256  1.1       jtc 		if (!(vp->flag & ISSET) || (!euid && !strchr(str_val(vp), '#')))
    257  1.1       jtc 			setstr(vp, safe_prompt);
    258  1.1       jtc 	}
    259  1.1       jtc 
    260  1.1       jtc 	/* Set this before parsing arguments */
    261  1.1       jtc 	Flag(FPRIVILEGED) = getuid() != euid || getgid() != getegid();
    262  1.1       jtc 
    263  1.1       jtc 	/* this to note if monitor is set on command line (see below) */
    264  1.1       jtc 	Flag(FMONITOR) = 127;
    265  1.1       jtc 	argi = parse_args(argv, OF_CMDLINE, (int *) 0);
    266  1.1       jtc 	if (argi < 0)
    267  1.1       jtc 		exit(1);
    268  1.1       jtc 
    269  1.1       jtc 	if (Flag(FCOMMAND)) {
    270  1.1       jtc 		s = pushs(SSTRING, ATEMP);
    271  1.1       jtc 		if (!(s->start = s->str = argv[argi++]))
    272  1.1       jtc 			errorf("-c requires an argument");
    273  1.1       jtc 		if (argv[argi])
    274  1.1       jtc 			kshname = argv[argi++];
    275  1.1       jtc 	} else if (argi < argc && !Flag(FSTDIN)) {
    276  1.1       jtc 		s = pushs(SFILE, ATEMP);
    277  1.1       jtc #ifdef OS2
    278  1.1       jtc 		/* a bug in os2 extproc shell processing doesn't
    279  1.1       jtc 		 * pass full pathnames so we have to search for it.
    280  1.1       jtc 		 * This changes the behavior of 'ksh arg' to search
    281  1.1       jtc 		 * the users search path but it can't be helped.
    282  1.1       jtc 		 */
    283  1.1       jtc 		s->file = search(argv[argi++], path, R_OK, (int *) 0);
    284  1.1       jtc 		if (!s->file || !*s->file)
    285  1.1       jtc 		        s->file = argv[argi - 1];
    286  1.1       jtc #else
    287  1.1       jtc 		s->file = argv[argi++];
    288  1.1       jtc #endif /* OS2 */
    289  1.1       jtc 		s->u.shf = shf_open(s->file, O_RDONLY, 0, SHF_MAPHI|SHF_CLEXEC);
    290  1.1       jtc 		if (s->u.shf == NULL) {
    291  1.1       jtc 			exstat = 127; /* POSIX */
    292  1.1       jtc 			errorf("%s: %s", s->file, strerror(errno));
    293  1.1       jtc 		}
    294  1.1       jtc 		kshname = s->file;
    295  1.1       jtc 	} else {
    296  1.1       jtc 		Flag(FSTDIN) = 1;
    297  1.1       jtc 		s = pushs(SSTDIN, ATEMP);
    298  1.1       jtc 		s->file = "<stdin>";
    299  1.1       jtc 		s->u.shf = shf_fdopen(0, SHF_RD | can_seek(0),
    300  1.1       jtc 				      (struct shf *) 0);
    301  1.1       jtc 		if (isatty(0) && isatty(2)) {
    302  1.1       jtc 			Flag(FTALKING) = 1;
    303  1.1       jtc 			/* The following only if isatty(0) */
    304  1.1       jtc 			s->flags |= SF_TTY;
    305  1.1       jtc 			s->u.shf->flags |= SHF_INTERRUPT;
    306  1.1       jtc 			s->file = (char *) 0;
    307  1.1       jtc 		}
    308  1.1       jtc 	}
    309  1.1       jtc 
    310  1.1       jtc 	/* This bizarreness is mandated by POSIX */
    311  1.1       jtc 	{
    312  1.1       jtc 		struct stat s_stdin;
    313  1.1       jtc 
    314  1.1       jtc 		if (fstat(0, &s_stdin) >= 0 && S_ISCHR(s_stdin.st_mode))
    315  1.1       jtc 			reset_nonblock(0);
    316  1.1       jtc 	}
    317  1.1       jtc 
    318  1.1       jtc 	/* initialize job control */
    319  1.1       jtc 	i = Flag(FMONITOR) != 127;
    320  1.1       jtc 	Flag(FMONITOR) = 0;
    321  1.1       jtc 	j_init(i);
    322  1.1       jtc #ifdef EDIT
    323  1.1       jtc 	/* Do this after j_init(), as tty_fd is not initialized 'til then */
    324  1.1       jtc 	if (Flag(FTALKING))
    325  1.1       jtc 		x_init();
    326  1.1       jtc #endif
    327  1.1       jtc 
    328  1.1       jtc 	l = e->loc;
    329  1.1       jtc 	l->argv = &argv[argi - 1];
    330  1.1       jtc 	l->argc = argc - argi;
    331  1.1       jtc 	l->argv[0] = (char *) kshname;
    332  1.1       jtc 	getopts_reset(1);
    333  1.1       jtc 
    334  1.1       jtc 	/* Disable during .profile/ENV reading */
    335  1.1       jtc 	restricted = Flag(FRESTRICTED);
    336  1.1       jtc 	Flag(FRESTRICTED) = 0;
    337  1.1       jtc 
    338  1.1       jtc 	/* Do this before profile/$ENV so that if it causes problems in them,
    339  1.1       jtc 	 * user will know why things broke.
    340  1.1       jtc 	 */
    341  1.1       jtc 	if (!current_wd[0] && Flag(FTALKING))
    342  1.1       jtc 		warningf(FALSE, "Cannot determine current working directory");
    343  1.1       jtc 
    344  1.1       jtc 	if (Flag(FLOGIN)) {
    345  1.1       jtc #ifdef OS2
    346  1.1       jtc 		char *profile;
    347  1.1       jtc 
    348  1.1       jtc 		/* Try to find a profile - first see if $INIT has a value,
    349  1.1       jtc 		 * then try /etc/profile.ksh, then c:/usr/etc/profile.ksh.
    350  1.1       jtc 		 */
    351  1.1       jtc 		if (!Flag(FPRIVILEGED)
    352  1.1       jtc 		    && strcmp(profile = substitute("$INIT/profile.ksh", 0),
    353  1.1       jtc 			      "/profile.ksh"))
    354  1.1       jtc 			include(profile, 0, (char **) 0, 1);
    355  1.1       jtc 		else if (include("/etc/profile.ksh", 0, (char **) 0, 1) < 0)
    356  1.1       jtc 			include("c:/usr/etc/profile.ksh", 0, (char **) 0, 1);
    357  1.1       jtc 		if (!Flag(FPRIVILEGED))
    358  1.1       jtc 			include(substitute("$HOME/profile.ksh", 0), 0,
    359  1.1       jtc 				(char **) 0, 1);
    360  1.1       jtc #else /* OS2 */
    361  1.2       tls 		include(KSH_SYSTEM_PROFILE, 0, (char **) 0, 1);
    362  1.1       jtc 		if (!Flag(FPRIVILEGED))
    363  1.1       jtc 			include(substitute("$HOME/.profile", 0), 0,
    364  1.1       jtc 				(char **) 0, 1);
    365  1.1       jtc #endif /* OS2 */
    366  1.1       jtc 	}
    367  1.1       jtc 
    368  1.1       jtc 	if (Flag(FPRIVILEGED))
    369  1.1       jtc 		include("/etc/suid_profile", 0, (char **) 0, 1);
    370  1.1       jtc 	else {
    371  1.1       jtc 		char *env_file;
    372  1.1       jtc 
    373  1.1       jtc #ifndef KSH
    374  1.1       jtc 		if (!Flag(FPOSIX))
    375  1.1       jtc 			env_file = null;
    376  1.1       jtc 		else
    377  1.1       jtc #endif /* !KSH */
    378  1.1       jtc 			/* include $ENV */
    379  1.1       jtc 			env_file = str_val(global("ENV"));
    380  1.1       jtc 
    381  1.1       jtc #ifdef DEFAULT_ENV
    382  1.1       jtc 		/* If env isn't set, include default environment */
    383  1.1       jtc 		if (env_file == null)
    384  1.1       jtc 			env_file = DEFAULT_ENV;
    385  1.1       jtc #endif /* DEFAULT_ENV */
    386  1.1       jtc 		env_file = substitute(env_file, DOTILDE);
    387  1.1       jtc 		if (*env_file != '\0')
    388  1.1       jtc 			include(env_file, 0, (char **) 0, 1);
    389  1.1       jtc #ifdef OS2
    390  1.1       jtc 		else if (Flag(FTALKING))
    391  1.1       jtc 			include(substitute("$HOME/kshrc.ksh", 0), 0,
    392  1.1       jtc 				(char **) 0, 1);
    393  1.1       jtc #endif /* OS2 */
    394  1.1       jtc 	}
    395  1.1       jtc 
    396  1.1       jtc 	if (is_restricted(argv[0]) || is_restricted(str_val(global("SHELL"))))
    397  1.1       jtc 		restricted = 1;
    398  1.1       jtc 	if (restricted) {
    399  1.1       jtc 		static const char *const restr_com[] = {
    400  1.1       jtc 						"typeset", "-r", "PATH",
    401  1.1       jtc 						    "ENV", "SHELL",
    402  1.1       jtc 						(char *) 0
    403  1.1       jtc 					    };
    404  1.1       jtc 		shcomexec((char **) restr_com);
    405  1.1       jtc 		/* After typeset command... */
    406  1.1       jtc 		Flag(FRESTRICTED) = 1;
    407  1.1       jtc 	}
    408  1.1       jtc 
    409  1.1       jtc 	if (Flag(FTALKING)) {
    410  1.1       jtc 		hist_init(s);
    411  1.1       jtc #ifdef KSH
    412  1.1       jtc 		alarm_init();
    413  1.1       jtc #endif /* KSH */
    414  1.1       jtc 	} else
    415  1.1       jtc 		Flag(FTRACKALL) = 1;	/* set after ENV */
    416  1.1       jtc 
    417  1.1       jtc 	shell(s, TRUE);	/* doesn't return */
    418  1.1       jtc 	return 0;
    419  1.1       jtc }
    420  1.1       jtc 
    421  1.1       jtc int
    422  1.1       jtc include(name, argc, argv, intr_ok)
    423  1.1       jtc 	const char *name;
    424  1.1       jtc 	int argc;
    425  1.1       jtc 	char **argv;
    426  1.1       jtc 	int intr_ok;
    427  1.1       jtc {
    428  1.1       jtc 	register Source *volatile s = NULL;
    429  1.1       jtc 	Source *volatile sold;
    430  1.1       jtc 	struct shf *shf;
    431  1.1       jtc 	char **volatile old_argv;
    432  1.1       jtc 	volatile int old_argc;
    433  1.1       jtc 	int i;
    434  1.1       jtc 
    435  1.1       jtc 	shf = shf_open(name, O_RDONLY, 0, SHF_MAPHI|SHF_CLEXEC);
    436  1.1       jtc 	if (shf == NULL)
    437  1.1       jtc 		return -1;
    438  1.1       jtc 
    439  1.1       jtc 	if (argv) {
    440  1.1       jtc 		old_argv = e->loc->argv;
    441  1.1       jtc 		old_argc = e->loc->argc;
    442  1.1       jtc 	} else {
    443  1.1       jtc 		old_argv = (char **) 0;
    444  1.1       jtc 		old_argc = 0;
    445  1.1       jtc 	}
    446  1.1       jtc 	sold = source;
    447  1.1       jtc 	newenv(E_INCL);
    448  1.1       jtc 	i = ksh_sigsetjmp(e->jbuf, 0);
    449  1.1       jtc 	if (i) {
    450  1.1       jtc 		quitenv();
    451  1.1       jtc 		source = sold;
    452  1.1       jtc 		if (s)
    453  1.1       jtc 			shf_close(s->u.shf);
    454  1.1       jtc 		if (old_argv) {
    455  1.1       jtc 			e->loc->argv = old_argv;
    456  1.1       jtc 			e->loc->argc = old_argc;
    457  1.1       jtc 		}
    458  1.1       jtc 		switch (i) {
    459  1.1       jtc 		  case LRETURN:
    460  1.1       jtc 		  case LERROR:
    461  1.1       jtc 			return exstat & 0xff; /* see below */
    462  1.1       jtc 		  case LINTR:
    463  1.1       jtc 			/* intr_ok is set if we are including .profile or $ENV.
    464  1.1       jtc 			 * If user ^C's out, we don't want to kill the shell...
    465  1.1       jtc 			 */
    466  1.1       jtc 			if (intr_ok && (exstat - 128) != SIGTERM)
    467  1.1       jtc 				return 1;
    468  1.1       jtc 			/* fall through... */
    469  1.1       jtc 		  case LEXIT:
    470  1.1       jtc 		  case LLEAVE:
    471  1.1       jtc 		  case LSHELL:
    472  1.1       jtc 			unwind(i);
    473  1.1       jtc 			/*NOREACHED*/
    474  1.1       jtc 		  default:
    475  1.1       jtc 			internal_errorf(1, "include: %d", i);
    476  1.1       jtc 			/*NOREACHED*/
    477  1.1       jtc 		}
    478  1.1       jtc 	}
    479  1.1       jtc 	if (argv) {
    480  1.1       jtc 		e->loc->argv = argv;
    481  1.1       jtc 		e->loc->argc = argc;
    482  1.1       jtc 	}
    483  1.1       jtc 	s = pushs(SFILE, ATEMP);
    484  1.1       jtc 	s->u.shf = shf;
    485  1.1       jtc 	s->file = str_save(name, ATEMP);
    486  1.1       jtc 	i = shell(s, FALSE);
    487  1.1       jtc 	quitenv();
    488  1.1       jtc 	source = sold;
    489  1.1       jtc 	shf_close(s->u.shf);
    490  1.1       jtc 	if (old_argv) {
    491  1.1       jtc 		e->loc->argv = old_argv;
    492  1.1       jtc 		e->loc->argc = old_argc;
    493  1.1       jtc 	}
    494  1.1       jtc 	return i & 0xff;	/* & 0xff to ensure value not -1 */
    495  1.1       jtc }
    496  1.1       jtc 
    497  1.1       jtc int
    498  1.1       jtc command(comm)
    499  1.1       jtc 	const char *comm;
    500  1.1       jtc {
    501  1.1       jtc 	register Source *s;
    502  1.1       jtc 
    503  1.1       jtc 	s = pushs(SSTRING, ATEMP);
    504  1.1       jtc 	s->start = s->str = comm;
    505  1.1       jtc 	return shell(s, FALSE);
    506  1.1       jtc }
    507  1.1       jtc 
    508  1.1       jtc /*
    509  1.1       jtc  * run the commands from the input source, returning status.
    510  1.1       jtc  */
    511  1.1       jtc int
    512  1.1       jtc shell(s, toplevel)
    513  1.1       jtc 	Source *volatile s;		/* input source */
    514  1.1       jtc 	int volatile toplevel;
    515  1.1       jtc {
    516  1.1       jtc 	struct op *t;
    517  1.1       jtc 	volatile int wastty = s->flags & SF_TTY;
    518  1.1       jtc 	volatile int attempts = 13;
    519  1.1       jtc 	volatile int interactive = Flag(FTALKING) && toplevel;
    520  1.1       jtc 	int i;
    521  1.1       jtc 
    522  1.1       jtc 	newenv(E_PARSE);
    523  1.1       jtc 	if (interactive)
    524  1.1       jtc 		really_exit = 0;
    525  1.1       jtc 	i = ksh_sigsetjmp(e->jbuf, 0);
    526  1.1       jtc 	if (i) {
    527  1.1       jtc 		s->start = s->str = null;
    528  1.1       jtc 		switch (i) {
    529  1.1       jtc 		  case LINTR: /* we get here if SIGINT not caught or ignored */
    530  1.1       jtc 		  case LERROR:
    531  1.1       jtc 		  case LSHELL:
    532  1.1       jtc 			if (interactive) {
    533  1.1       jtc 				if (i == LINTR)
    534  1.1       jtc 					shellf(newline);
    535  1.1       jtc 				/* Reset any eof that was read as part of a
    536  1.1       jtc 				 * multiline command.
    537  1.1       jtc 				 */
    538  1.1       jtc 				if (Flag(FIGNOREEOF) && s->type == SEOF
    539  1.1       jtc 				    && wastty)
    540  1.1       jtc 					s->type = SSTDIN;
    541  1.1       jtc 				/* Used by exit command to get back to
    542  1.1       jtc 				 * top level shell.  Kind of strange since
    543  1.1       jtc 				 * interactive is set if we are reading from
    544  1.1       jtc 				 * a tty, but to have stopped jobs, one only
    545  1.1       jtc 				 * needs FMONITOR set (not FTALKING/SF_TTY)...
    546  1.1       jtc 				 */
    547  1.1       jtc 				break;
    548  1.1       jtc 			}
    549  1.1       jtc 			/* fall through... */
    550  1.1       jtc 		  case LEXIT:
    551  1.1       jtc 		  case LLEAVE:
    552  1.1       jtc 		  case LRETURN:
    553  1.1       jtc 			quitenv();
    554  1.1       jtc 			unwind(i);	/* keep on going */
    555  1.1       jtc 			/*NOREACHED*/
    556  1.1       jtc 		  default:
    557  1.1       jtc 			quitenv();
    558  1.1       jtc 			internal_errorf(1, "shell: %d", i);
    559  1.1       jtc 			/*NOREACHED*/
    560  1.1       jtc 		}
    561  1.1       jtc 	}
    562  1.1       jtc 
    563  1.1       jtc 	while (1) {
    564  1.1       jtc 		if (trap)
    565  1.1       jtc 			runtraps(0);
    566  1.1       jtc 
    567  1.1       jtc 		if (s->next == NULL)
    568  1.1       jtc 			if (Flag(FVERBOSE))
    569  1.1       jtc 				s->flags |= SF_ECHO;
    570  1.1       jtc 			else
    571  1.1       jtc 				s->flags &= ~SF_ECHO;
    572  1.1       jtc 
    573  1.1       jtc 		if (interactive) {
    574  1.1       jtc 			j_notify();
    575  1.1       jtc #ifdef KSH
    576  1.1       jtc 			mcheck();
    577  1.1       jtc #endif /* KSH */
    578  1.1       jtc 			set_prompt(PS1, s);
    579  1.1       jtc 		}
    580  1.1       jtc 
    581  1.1       jtc 		t = compile(s);
    582  1.1       jtc 		if (t != NULL && t->type == TEOF) {
    583  1.1       jtc 			if (wastty && Flag(FIGNOREEOF) && --attempts > 0) {
    584  1.1       jtc 				shellf("Use `exit' to leave ksh\n");
    585  1.1       jtc 				s->type = SSTDIN;
    586  1.1       jtc 			} else if (wastty && !really_exit
    587  1.1       jtc 				   && j_stopped_running())
    588  1.1       jtc 			{
    589  1.1       jtc 				really_exit = 1;
    590  1.1       jtc 				s->type = SSTDIN;
    591  1.1       jtc 			} else {
    592  1.1       jtc 				/* this for POSIX, which says EXIT traps
    593  1.1       jtc 				 * shall be taken in the environment
    594  1.1       jtc 				 * immediately after the last command
    595  1.1       jtc 				 * executed.
    596  1.1       jtc 				 */
    597  1.1       jtc 				if (toplevel)
    598  1.1       jtc 					unwind(LEXIT);
    599  1.1       jtc 				break;
    600  1.1       jtc 			}
    601  1.1       jtc 		}
    602  1.1       jtc 
    603  1.1       jtc 		if (t && (!Flag(FNOEXEC) || (s->flags & SF_TTY)))
    604  1.1       jtc 			exstat = execute(t, 0);
    605  1.1       jtc 
    606  1.1       jtc 		if (t != NULL && t->type != TEOF && interactive && really_exit)
    607  1.1       jtc 			really_exit = 0;
    608  1.1       jtc 
    609  1.1       jtc 		reclaim();
    610  1.1       jtc 	}
    611  1.1       jtc 	quitenv();
    612  1.1       jtc 	return exstat;
    613  1.1       jtc }
    614  1.1       jtc 
    615  1.1       jtc /* return to closest error handler or shell(), exit if none found */
    616  1.1       jtc void
    617  1.1       jtc unwind(i)
    618  1.1       jtc 	int i;
    619  1.1       jtc {
    620  1.1       jtc 	/* ordering for EXIT vs ERR is a bit odd (this is what at&t ksh does) */
    621  1.1       jtc 	if (i == LEXIT || (Flag(FERREXIT) && (i == LERROR || i == LINTR)
    622  1.1       jtc 			   && sigtraps[SIGEXIT_].trap))
    623  1.1       jtc 	{
    624  1.1       jtc 		runtrap(&sigtraps[SIGEXIT_]);
    625  1.1       jtc 		i = LLEAVE;
    626  1.1       jtc 	} else if (Flag(FERREXIT) && (i == LERROR || i == LINTR)) {
    627  1.1       jtc 		runtrap(&sigtraps[SIGERR_]);
    628  1.1       jtc 		i = LLEAVE;
    629  1.1       jtc 	}
    630  1.1       jtc 	while (1) {
    631  1.1       jtc 		switch (e->type) {
    632  1.1       jtc 		  case E_PARSE:
    633  1.1       jtc 		  case E_FUNC:
    634  1.1       jtc 		  case E_INCL:
    635  1.1       jtc 		  case E_LOOP:
    636  1.1       jtc 		  case E_ERRH:
    637  1.1       jtc 			ksh_siglongjmp(e->jbuf, i);
    638  1.1       jtc 			/*NOTREACHED*/
    639  1.1       jtc 
    640  1.1       jtc 		  case E_NONE: 	/* bottom of the stack */
    641  1.1       jtc 		  {
    642  1.1       jtc 			if (Flag(FTALKING))
    643  1.1       jtc 				hist_finish();
    644  1.1       jtc 			j_exit();
    645  1.1       jtc 			remove_temps(func_heredocs);
    646  1.1       jtc 			if (i == LINTR) {
    647  1.1       jtc 				int sig = exstat - 128;
    648  1.1       jtc 
    649  1.1       jtc 				/* ham up our death a bit (at&t ksh
    650  1.1       jtc 				 * only seems to do this for SIGTERM)
    651  1.1       jtc 				 * Don't do it for SIGQUIT, since we'd
    652  1.1       jtc 				 * dump a core..
    653  1.1       jtc 				 */
    654  1.1       jtc 				if (sig == SIGINT || sig == SIGTERM) {
    655  1.1       jtc 					setsig(&sigtraps[sig], SIG_DFL,
    656  1.1       jtc 						SS_RESTORE_CURR|SS_FORCE);
    657  1.1       jtc 					kill(0, sig);
    658  1.1       jtc 				}
    659  1.1       jtc 			}
    660  1.1       jtc 			exit(exstat);
    661  1.1       jtc 			/* NOTREACHED */
    662  1.1       jtc 		  }
    663  1.1       jtc 
    664  1.1       jtc 		  default:
    665  1.1       jtc 			quitenv();
    666  1.1       jtc 		}
    667  1.1       jtc 	}
    668  1.1       jtc }
    669  1.1       jtc 
    670  1.1       jtc void
    671  1.1       jtc newenv(type)
    672  1.1       jtc 	int type;
    673  1.1       jtc {
    674  1.1       jtc 	register struct env *ep;
    675  1.1       jtc 
    676  1.1       jtc 	ep = (struct env *) alloc(sizeof(*ep), ATEMP);
    677  1.1       jtc 	ep->type = type;
    678  1.1       jtc 	ep->flags = 0;
    679  1.1       jtc 	ainit(&ep->area);
    680  1.1       jtc 	ep->loc = e->loc;
    681  1.1       jtc 	ep->savefd = NULL;
    682  1.1       jtc 	ep->oenv = e;
    683  1.1       jtc 	ep->temps = NULL;
    684  1.1       jtc 	e = ep;
    685  1.1       jtc }
    686  1.1       jtc 
    687  1.1       jtc void
    688  1.1       jtc quitenv()
    689  1.1       jtc {
    690  1.1       jtc 	register struct env *ep = e;
    691  1.1       jtc 	register int fd;
    692  1.1       jtc 
    693  1.1       jtc 	if (ep->oenv == NULL) /* cleanup_parents_env() was called */
    694  1.1       jtc 		exit(exstat);	/* exit child */
    695  1.1       jtc 	if (ep->oenv->loc != ep->loc)
    696  1.1       jtc 		popblock();
    697  1.1       jtc 	if (ep->savefd != NULL) {
    698  1.1       jtc 		for (fd = 0; fd < NUFILE; fd++)
    699  1.1       jtc 			/* if ep->savefd[fd] < 0, means fd was closed */
    700  1.1       jtc 			if (ep->savefd[fd])
    701  1.1       jtc 				restfd(fd, ep->savefd[fd]);
    702  1.1       jtc 		if (ep->savefd[2]) /* Clear any write errors */
    703  1.1       jtc 			shf_reopen(2, SHF_WR, shl_out);
    704  1.1       jtc 	}
    705  1.1       jtc 	reclaim();
    706  1.1       jtc 	e = e->oenv;
    707  1.1       jtc 	afree(ep, ATEMP);
    708  1.1       jtc }
    709  1.1       jtc 
    710  1.1       jtc /* Called after a fork to cleanup stuff left over from parents environment */
    711  1.1       jtc void
    712  1.1       jtc cleanup_parents_env()
    713  1.1       jtc {
    714  1.1       jtc 	struct env *ep;
    715  1.1       jtc 	int fd;
    716  1.1       jtc 
    717  1.1       jtc 	/* Don't clean up temporary files - parent will probably need them.
    718  1.1       jtc 	 * Also, can't easily reclaim memory since variables, etc. could be
    719  1.1       jtc 	 * anywyere.
    720  1.1       jtc 	 */
    721  1.1       jtc 
    722  1.1       jtc 	/* close all file descriptors hiding in savefd */
    723  1.1       jtc 	for (ep = e; ep; ep = ep->oenv) {
    724  1.1       jtc 		if (ep->savefd)
    725  1.1       jtc 			for (fd = 0; fd < NUFILE; fd++)
    726  1.1       jtc 				if (ep->savefd[fd] > 0)
    727  1.1       jtc 					close(ep->savefd[fd]);
    728  1.1       jtc 	}
    729  1.1       jtc 	e->oenv = (struct env *) 0;
    730  1.1       jtc }
    731  1.1       jtc 
    732  1.2       tls /* Called just before an execve cleanup stuff temporary files */
    733  1.2       tls void
    734  1.2       tls cleanup_proc_env()
    735  1.2       tls {
    736  1.2       tls 	struct env *ep;
    737  1.2       tls 
    738  1.2       tls 	for (ep = e; ep; ep = ep->oenv)
    739  1.2       tls 		remove_temps(ep->temps);
    740  1.2       tls 	remove_temps(func_heredocs);
    741  1.2       tls }
    742  1.2       tls 
    743  1.1       jtc /* remove temp files and free ATEMP Area */
    744  1.1       jtc static void
    745  1.1       jtc reclaim()
    746  1.1       jtc {
    747  1.1       jtc 	remove_temps(e->temps);
    748  1.1       jtc 	e->temps = NULL;
    749  1.1       jtc 	afreeall(&e->area);
    750  1.1       jtc }
    751  1.1       jtc 
    752  1.1       jtc static void
    753  1.1       jtc remove_temps(tp)
    754  1.1       jtc 	struct temp *tp;
    755  1.1       jtc {
    756  1.1       jtc #ifdef OS2
    757  1.2       tls 	static struct temp *delayed_remove;
    758  1.2       tls 	struct temp *t, **tprev;
    759  1.1       jtc 
    760  1.2       tls 	if (delayed_remove) {
    761  1.2       tls 		for (tprev = &delayed_remove, t = delayed_remove; t; t = *tprev)
    762  1.2       tls 			/* No need to check t->pid here... */
    763  1.2       tls 			if (unlink(t->name) >= 0 || errno == ENOENT) {
    764  1.2       tls 				*tprev = t->next;
    765  1.2       tls 				afree(t, APERM);
    766  1.2       tls 			} else
    767  1.2       tls 				tprev = &t->next;
    768  1.2       tls 	}
    769  1.1       jtc #endif /* OS2 */
    770  1.1       jtc 
    771  1.1       jtc 	for (; tp != NULL; tp = tp->next)
    772  1.2       tls 		if (tp->pid == procpid) {
    773  1.1       jtc #ifdef OS2
    774  1.2       tls 			/* OS/2 (and dos) do not allow files that are currently
    775  1.2       tls 			 * open to be removed, so we cache it away for future
    776  1.2       tls 			 * removal.
    777  1.2       tls 			 * XXX should only do this if errno
    778  1.2       tls 			 *     is Efile-still-open-can't-remove
    779  1.2       tls 			 *     (but I don't know what that is...)
    780  1.2       tls 			 */
    781  1.2       tls 			if (unlink(tp->name) < 0 && errno != ENOENT) {
    782  1.2       tls 				t = (struct temp *) alloc(
    783  1.2       tls 				    sizeof(struct temp) + strlen(tp->name) + 1,
    784  1.2       tls 				    APERM);
    785  1.2       tls 				memset(t, 0, sizeof(struct temp));
    786  1.2       tls 				strcpy(t->name, tp->name);
    787  1.2       tls 				t->next = delayed_remove;
    788  1.2       tls 				delayed_remove = t;
    789  1.2       tls 			}
    790  1.1       jtc #else /* OS2 */
    791  1.1       jtc 			unlink(tp->name);
    792  1.1       jtc #endif /* OS2 */
    793  1.2       tls 		}
    794  1.1       jtc }
    795  1.1       jtc 
    796  1.1       jtc /* Returns true if name refers to a restricted shell */
    797  1.1       jtc static int
    798  1.1       jtc is_restricted(name)
    799  1.1       jtc 	char *name;
    800  1.1       jtc {
    801  1.1       jtc 	char *p;
    802  1.1       jtc 
    803  1.1       jtc 	if ((p = ksh_strrchr_dirsep(name)))
    804  1.1       jtc 		name = p;
    805  1.1       jtc 	/* accepts rsh, rksh, rpdksh, pdrksh, etc. */
    806  1.1       jtc 	return (p = strchr(name, 'r')) && strstr(p, "sh");
    807  1.1       jtc }
    808  1.1       jtc 
    809  1.1       jtc void
    810  1.1       jtc aerror(ap, msg)
    811  1.1       jtc 	Area *ap;
    812  1.1       jtc 	const char *msg;
    813  1.1       jtc {
    814  1.1       jtc 	internal_errorf(1, "alloc: %s", msg);
    815  1.1       jtc 	errorf(null); /* this is never executed - keeps gcc quiet */
    816  1.1       jtc 	/*NOTREACHED*/
    817  1.1       jtc }
    818