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