main.c revision 59214
1/*-
2 * Copyright (c) 1991, 1993
3 *	The Regents of the University of California.  All rights reserved.
4 *
5 * This code is derived from software contributed to Berkeley by
6 * Kenneth Almquist.
7 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
11 * 1. Redistributions of source code must retain the above copyright
12 *    notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 *    notice, this list of conditions and the following disclaimer in the
15 *    documentation and/or other materials provided with the distribution.
16 * 3. All advertising materials mentioning features or use of this software
17 *    must display the following acknowledgement:
18 *	This product includes software developed by the University of
19 *	California, Berkeley and its contributors.
20 * 4. Neither the name of the University nor the names of its contributors
21 *    may be used to endorse or promote products derived from this software
22 *    without specific prior written permission.
23 *
24 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
25 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27 * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
28 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34 * SUCH DAMAGE.
35 */
36
37#ifndef lint
38static char const copyright[] =
39"@(#) Copyright (c) 1991, 1993\n\
40	The Regents of the University of California.  All rights reserved.\n";
41#endif /* not lint */
42
43#ifndef lint
44#if 0
45static char sccsid[] = "@(#)main.c	8.6 (Berkeley) 5/28/95";
46#endif
47static const char rcsid[] =
48  "$FreeBSD: head/bin/sh/main.c 59214 2000-04-14 06:03:39Z imp $";
49#endif /* not lint */
50
51#include <stdio.h>
52#include <signal.h>
53#include <sys/stat.h>
54#include <unistd.h>
55#include <fcntl.h>
56#include <locale.h>
57#include <errno.h>
58
59#include "shell.h"
60#include "main.h"
61#include "mail.h"
62#include "options.h"
63#include "output.h"
64#include "parser.h"
65#include "nodes.h"
66#include "expand.h"
67#include "eval.h"
68#include "jobs.h"
69#include "input.h"
70#include "trap.h"
71#include "var.h"
72#include "show.h"
73#include "memalloc.h"
74#include "error.h"
75#include "init.h"
76#include "mystring.h"
77#include "exec.h"
78#include "cd.h"
79
80#define PROFILE 0
81
82int rootpid;
83int rootshell;
84#if PROFILE
85short profile_buf[16384];
86extern int etext();
87#endif
88
89STATIC void read_profile __P((char *));
90STATIC char *find_dot_file __P((char *));
91
92/*
93 * Main routine.  We initialize things, parse the arguments, execute
94 * profiles if we're a login shell, and then call cmdloop to execute
95 * commands.  The setjmp call sets up the location to jump to when an
96 * exception occurs.  When an exception occurs the variable "state"
97 * is used to figure out how far we had gotten.
98 */
99
100int
101main(argc, argv)
102	int argc;
103	char **argv;
104{
105	struct jmploc jmploc;
106	struct stackmark smark;
107	volatile int state;
108	char *shinit;
109
110#if PROFILE
111	monitor(4, etext, profile_buf, sizeof profile_buf, 50);
112#endif
113	(void) setlocale(LC_ALL, "");
114	state = 0;
115	if (setjmp(jmploc.loc)) {
116		/*
117		 * When a shell procedure is executed, we raise the
118		 * exception EXSHELLPROC to clean up before executing
119		 * the shell procedure.
120		 */
121		switch (exception) {
122		case EXSHELLPROC:
123			rootpid = getpid();
124			rootshell = 1;
125			minusc = NULL;
126			state = 3;
127			break;
128
129		case EXEXEC:
130			exitstatus = exerrno;
131			break;
132
133		case EXERROR:
134			exitstatus = 2;
135			break;
136
137		default:
138			break;
139		}
140
141		if (exception != EXSHELLPROC) {
142		    if (state == 0 || iflag == 0 || ! rootshell)
143			    exitshell(exitstatus);
144		}
145		reset();
146		if (exception == EXINT
147#if ATTY
148		 && (! attyset() || equal(termval(), "emacs"))
149#endif
150		 ) {
151			out2c('\n');
152			flushout(&errout);
153		}
154		popstackmark(&smark);
155		FORCEINTON;				/* enable interrupts */
156		if (state == 1)
157			goto state1;
158		else if (state == 2)
159			goto state2;
160		else if (state == 3)
161			goto state3;
162		else
163			goto state4;
164	}
165	handler = &jmploc;
166#ifdef DEBUG
167	opentrace();
168	trputs("Shell args:  ");  trargs(argv);
169#endif
170	rootpid = getpid();
171	rootshell = 1;
172	init();
173	setstackmark(&smark);
174	procargs(argc, argv);
175	if (getpwd() == NULL && iflag)
176		out2str("sh: cannot determine working directory\n");
177	if (argv[0] && argv[0][0] == '-') {
178		state = 1;
179		read_profile("/etc/profile");
180state1:
181		state = 2;
182		if (privileged == 0)
183			read_profile(".profile");
184		else
185			read_profile("/etc/suid_profile");
186	}
187state2:
188	state = 3;
189	if (!privileged && iflag) {
190		if ((shinit = lookupvar("ENV")) != NULL && *shinit != '\0') {
191			state = 3;
192			read_profile(shinit);
193		}
194	}
195state3:
196	state = 4;
197	if (minusc) {
198		evalstring(minusc);
199	}
200	if (sflag || minusc == NULL) {
201state4:	/* XXX ??? - why isn't this before the "if" statement */
202		cmdloop(1);
203	}
204#if PROFILE
205	monitor(0);
206#endif
207	exitshell(exitstatus);
208	/*NOTREACHED*/
209	return 0;
210}
211
212
213/*
214 * Read and execute commands.  "Top" is nonzero for the top level command
215 * loop; it turns on prompting if the shell is interactive.
216 */
217
218void
219cmdloop(top)
220	int top;
221{
222	union node *n;
223	struct stackmark smark;
224	int inter;
225	int numeof = 0;
226
227	TRACE(("cmdloop(%d) called\n", top));
228	setstackmark(&smark);
229	for (;;) {
230		if (pendingsigs)
231			dotrap();
232		inter = 0;
233		if (iflag && top) {
234			inter++;
235			showjobs(1);
236			chkmail(0);
237			flushout(&output);
238		}
239		n = parsecmd(inter);
240		/* showtree(n); DEBUG */
241		if (n == NEOF) {
242			if (!top || numeof >= 50)
243				break;
244			if (!stoppedjobs()) {
245				if (!Iflag)
246					break;
247				out2str("\nUse \"exit\" to leave shell.\n");
248			}
249			numeof++;
250		} else if (n != NULL && nflag == 0) {
251			job_warning = (job_warning == 2) ? 1 : 0;
252			numeof = 0;
253			evaltree(n, 0);
254		}
255		popstackmark(&smark);
256		if (evalskip == SKIPFILE) {
257			evalskip = 0;
258			break;
259		}
260	}
261	popstackmark(&smark);		/* unnecessary */
262}
263
264
265
266/*
267 * Read /etc/profile or .profile.  Return on error.
268 */
269
270STATIC void
271read_profile(name)
272	char *name;
273	{
274	int fd;
275
276	INTOFF;
277	if ((fd = open(name, O_RDONLY)) >= 0)
278		setinputfd(fd, 1);
279	INTON;
280	if (fd < 0)
281		return;
282	cmdloop(0);
283	popfile();
284}
285
286
287
288/*
289 * Read a file containing shell functions.
290 */
291
292void
293readcmdfile(name)
294	char *name;
295{
296	int fd;
297
298	INTOFF;
299	if ((fd = open(name, O_RDONLY)) >= 0)
300		setinputfd(fd, 1);
301	else
302		error("Can't open %s: %s", name, strerror(errno));
303	INTON;
304	cmdloop(0);
305	popfile();
306}
307
308
309
310/*
311 * Take commands from a file.  To be compatible we should do a path
312 * search for the file, which is necessary to find sub-commands.
313 */
314
315
316STATIC char *
317find_dot_file(basename)
318	char *basename;
319{
320	static char localname[FILENAME_MAX+1];
321	char *fullname;
322	char *path = pathval();
323	struct stat statb;
324
325	/* don't try this for absolute or relative paths */
326	if( strchr(basename, '/'))
327		return basename;
328
329	while ((fullname = padvance(&path, basename)) != NULL) {
330		strcpy(localname, fullname);
331		stunalloc(fullname);
332		if ((stat(fullname, &statb) == 0) && S_ISREG(statb.st_mode))
333			return localname;
334	}
335	return basename;
336}
337
338int
339dotcmd(argc, argv)
340	int argc;
341	char **argv;
342{
343	struct strlist *sp;
344	exitstatus = 0;
345
346	for (sp = cmdenviron; sp ; sp = sp->next)
347		setvareq(savestr(sp->text), VSTRFIXED|VTEXTFIXED);
348
349	if (argc >= 2) {		/* That's what SVR2 does */
350		char *fullname = find_dot_file(argv[1]);
351
352		setinputfile(fullname, 1);
353		commandname = fullname;
354		cmdloop(0);
355		popfile();
356	}
357	return exitstatus;
358}
359
360
361int
362exitcmd(argc, argv)
363	int argc;
364	char **argv;
365{
366	extern int oexitstatus;
367
368	if (stoppedjobs())
369		return 0;
370	if (argc > 1)
371		exitstatus = number(argv[1]);
372	else
373		exitstatus = oexitstatus;
374	exitshell(exitstatus);
375	/*NOTREACHED*/
376	return 0;
377}
378
379
380#ifdef notdef
381/*
382 * Should never be called.
383 */
384
385void
386exit(exitstatus) {
387	_exit(exitstatus);
388}
389#endif
390