main.c revision 64702
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 64702 2000-08-16 10:39:43Z cracauer $";
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		setstackmark(&smark);
257		if (evalskip == SKIPFILE) {
258			evalskip = 0;
259			break;
260		}
261	}
262	popstackmark(&smark);
263}
264
265
266
267/*
268 * Read /etc/profile or .profile.  Return on error.
269 */
270
271STATIC void
272read_profile(name)
273	char *name;
274	{
275	int fd;
276
277	INTOFF;
278	if ((fd = open(name, O_RDONLY)) >= 0)
279		setinputfd(fd, 1);
280	INTON;
281	if (fd < 0)
282		return;
283	cmdloop(0);
284	popfile();
285}
286
287
288
289/*
290 * Read a file containing shell functions.
291 */
292
293void
294readcmdfile(name)
295	char *name;
296{
297	int fd;
298
299	INTOFF;
300	if ((fd = open(name, O_RDONLY)) >= 0)
301		setinputfd(fd, 1);
302	else
303		error("Can't open %s: %s", name, strerror(errno));
304	INTON;
305	cmdloop(0);
306	popfile();
307}
308
309
310
311/*
312 * Take commands from a file.  To be compatible we should do a path
313 * search for the file, which is necessary to find sub-commands.
314 */
315
316
317STATIC char *
318find_dot_file(basename)
319	char *basename;
320{
321	static char localname[FILENAME_MAX+1];
322	char *fullname;
323	char *path = pathval();
324	struct stat statb;
325
326	/* don't try this for absolute or relative paths */
327	if( strchr(basename, '/'))
328		return basename;
329
330	while ((fullname = padvance(&path, basename)) != NULL) {
331		strcpy(localname, fullname);
332		stunalloc(fullname);
333		if ((stat(fullname, &statb) == 0) && S_ISREG(statb.st_mode))
334			return localname;
335	}
336	return basename;
337}
338
339int
340dotcmd(argc, argv)
341	int argc;
342	char **argv;
343{
344	struct strlist *sp;
345	exitstatus = 0;
346
347	for (sp = cmdenviron; sp ; sp = sp->next)
348		setvareq(savestr(sp->text), VSTRFIXED|VTEXTFIXED);
349
350	if (argc >= 2) {		/* That's what SVR2 does */
351		char *fullname = find_dot_file(argv[1]);
352
353		setinputfile(fullname, 1);
354		commandname = fullname;
355		cmdloop(0);
356		popfile();
357	}
358	return exitstatus;
359}
360
361
362int
363exitcmd(argc, argv)
364	int argc;
365	char **argv;
366{
367	extern int oexitstatus;
368
369	if (stoppedjobs())
370		return 0;
371	if (argc > 1)
372		exitstatus = number(argv[1]);
373	else
374		exitstatus = oexitstatus;
375	exitshell(exitstatus);
376	/*NOTREACHED*/
377	return 0;
378}
379
380
381#ifdef notdef
382/*
383 * Should never be called.
384 */
385
386void
387exit(exitstatus) {
388	_exit(exitstatus);
389}
390#endif
391