main.c revision 18267
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 *	$Id: main.c,v 1.7 1996/09/12 02:23:33 bde Exp $
37 */
38
39#ifndef lint
40static char copyright[] =
41"@(#) Copyright (c) 1991, 1993\n\
42	The Regents of the University of California.  All rights reserved.\n";
43#endif /* not lint */
44
45#ifndef lint
46static char sccsid[] = "@(#)main.c	8.6 (Berkeley) 5/28/95";
47#endif /* not lint */
48
49#include <stdio.h>
50#include <signal.h>
51#include <sys/stat.h>
52#include <unistd.h>
53#include <fcntl.h>
54#include <locale.h>
55
56
57#include "shell.h"
58#include "main.h"
59#include "mail.h"
60#include "options.h"
61#include "output.h"
62#include "parser.h"
63#include "nodes.h"
64#include "expand.h"
65#include "eval.h"
66#include "jobs.h"
67#include "input.h"
68#include "trap.h"
69#include "var.h"
70#include "show.h"
71#include "memalloc.h"
72#include "error.h"
73#include "init.h"
74#include "mystring.h"
75#include "exec.h"
76
77#define PROFILE 0
78
79int rootpid;
80int rootshell;
81STATIC union node *curcmd;
82STATIC union node *prevcmd;
83extern int errno;
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		if (exception == EXERROR)
122			exitstatus = 2;
123		if (exception == EXSHELLPROC) {
124			rootpid = getpid();
125			rootshell = 1;
126			minusc = NULL;
127			state = 3;
128		} else if (state == 0 || iflag == 0 || ! rootshell)
129			exitshell(2);
130		reset();
131		if (exception == EXINT
132#if ATTY
133		 && (! attyset() || equal(termval(), "emacs"))
134#endif
135		 ) {
136			out2c('\n');
137			flushout(&errout);
138		}
139		popstackmark(&smark);
140		FORCEINTON;				/* enable interrupts */
141		if (state == 1)
142			goto state1;
143		else if (state == 2)
144			goto state2;
145		else if (state == 3)
146			goto state3;
147		else
148			goto state4;
149	}
150	handler = &jmploc;
151#ifdef DEBUG
152	opentrace();
153	trputs("Shell args:  ");  trargs(argv);
154#endif
155	rootpid = getpid();
156	rootshell = 1;
157	init();
158	setstackmark(&smark);
159	procargs(argc, argv);
160	if (argv[0] && argv[0][0] == '-') {
161		state = 1;
162		read_profile("/etc/profile");
163state1:
164		state = 2;
165		read_profile(".profile");
166	}
167state2:
168	state = 3;
169	if (getuid() == geteuid() && getgid() == getegid()) {
170		if ((shinit = lookupvar("ENV")) != NULL && *shinit != '\0') {
171			state = 3;
172			read_profile(shinit);
173		}
174	}
175state3:
176	state = 4;
177	if (minusc) {
178		evalstring(minusc);
179	}
180	if (sflag || minusc == NULL) {
181state4:	/* XXX ??? - why isn't this before the "if" statement */
182		cmdloop(1);
183	}
184#if PROFILE
185	monitor(0);
186#endif
187	exitshell(exitstatus);
188	/*NOTREACHED*/
189	return 0;
190}
191
192
193/*
194 * Read and execute commands.  "Top" is nonzero for the top level command
195 * loop; it turns on prompting if the shell is interactive.
196 */
197
198void
199cmdloop(top)
200	int top;
201{
202	union node *n;
203	struct stackmark smark;
204	int inter;
205	int numeof = 0;
206
207	TRACE(("cmdloop(%d) called\n", top));
208	setstackmark(&smark);
209	for (;;) {
210		if (pendingsigs)
211			dotrap();
212		inter = 0;
213		if (iflag && top) {
214			inter++;
215			showjobs(1);
216			chkmail(0);
217			flushout(&output);
218		}
219		n = parsecmd(inter);
220		/* showtree(n); DEBUG */
221		if (n == NEOF) {
222			if (!top || numeof >= 50)
223				break;
224			if (!stoppedjobs()) {
225				if (!Iflag)
226					break;
227				out2str("\nUse \"exit\" to leave shell.\n");
228			}
229			numeof++;
230		} else if (n != NULL && nflag == 0) {
231			job_warning = (job_warning == 2) ? 1 : 0;
232			numeof = 0;
233			evaltree(n, 0);
234		}
235		popstackmark(&smark);
236	}
237	popstackmark(&smark);		/* unnecessary */
238}
239
240
241
242/*
243 * Read /etc/profile or .profile.  Return on error.
244 */
245
246STATIC void
247read_profile(name)
248	char *name;
249	{
250	int fd;
251
252	INTOFF;
253	if ((fd = open(name, O_RDONLY)) >= 0)
254		setinputfd(fd, 1);
255	INTON;
256	if (fd < 0)
257		return;
258	cmdloop(0);
259	popfile();
260}
261
262
263
264/*
265 * Read a file containing shell functions.
266 */
267
268void
269readcmdfile(name)
270	char *name;
271{
272	int fd;
273
274	INTOFF;
275	if ((fd = open(name, O_RDONLY)) >= 0)
276		setinputfd(fd, 1);
277	else
278		error("Can't open %s", name);
279	INTON;
280	cmdloop(0);
281	popfile();
282}
283
284
285
286/*
287 * Take commands from a file.  To be compatable we should do a path
288 * search for the file, which is necessary to find sub-commands.
289 */
290
291
292STATIC char *
293find_dot_file(basename)
294	char *basename;
295{
296	static char localname[FILENAME_MAX+1];
297	char *fullname;
298	char *path = pathval();
299	struct stat statb;
300
301	/* don't try this for absolute or relative paths */
302	if( strchr(basename, '/'))
303		return basename;
304
305	while ((fullname = padvance(&path, basename)) != NULL) {
306		strcpy(localname, fullname);
307		stunalloc(fullname);
308		if ((stat(fullname, &statb) == 0) && S_ISREG(statb.st_mode))
309			return localname;
310	}
311	return basename;
312}
313
314int
315dotcmd(argc, argv)
316	int argc;
317	char **argv;
318{
319	struct strlist *sp;
320	exitstatus = 0;
321
322	for (sp = cmdenviron; sp ; sp = sp->next)
323		setvareq(savestr(sp->text), VSTRFIXED|VTEXTFIXED);
324
325	if (argc >= 2) {		/* That's what SVR2 does */
326		char *fullname = find_dot_file(argv[1]);
327
328		setinputfile(fullname, 1);
329		commandname = fullname;
330		cmdloop(0);
331		popfile();
332	}
333	return exitstatus;
334}
335
336
337int
338exitcmd(argc, argv)
339	int argc;
340	char **argv;
341{
342	extern int oexitstatus;
343
344	if (stoppedjobs())
345		return 0;
346	exitstatus = (argc > 1) ? number(argv[1]) : oexitstatus;
347	exitshell(exitstatus);
348	/*NOTREACHED*/
349	return 0;
350}
351
352
353#ifdef notdef
354/*
355 * Should never be called.
356 */
357
358void
359exit(exitstatus) {
360	_exit(exitstatus);
361}
362#endif
363