xref: /freebsd/bin/sh/main.c (revision 5ebc7e6281887681c3a348a5a4c902e262ccd656)
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.2 1994/09/24 02:57:48 davidg Exp $
37  */
38 
39 #ifndef lint
40 static 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
46 static char sccsid[] = "@(#)main.c	8.1 (Berkeley) 5/31/93";
47 #endif /* not lint */
48 
49 #include <signal.h>
50 #include <fcntl.h>
51 #include "shell.h"
52 #include "main.h"
53 #include "mail.h"
54 #include "options.h"
55 #include "output.h"
56 #include "parser.h"
57 #include "nodes.h"
58 #include "eval.h"
59 #include "jobs.h"
60 #include "input.h"
61 #include "trap.h"
62 #include "var.h"
63 #include "memalloc.h"
64 #include "error.h"
65 #include "init.h"
66 #include "mystring.h"
67 
68 #define PROFILE 0
69 
70 int rootpid;
71 int rootshell;
72 STATIC union node *curcmd;
73 STATIC union node *prevcmd;
74 extern int errno;
75 #if PROFILE
76 short profile_buf[16384];
77 extern int etext();
78 #endif
79 
80 #ifdef __STDC__
81 STATIC void read_profile(char *);
82 char *getenv(char *);
83 #else
84 STATIC void read_profile();
85 char *getenv();
86 #endif
87 
88 
89 /*
90  * Main routine.  We initialize things, parse the arguments, execute
91  * profiles if we're a login shell, and then call cmdloop to execute
92  * commands.  The setjmp call sets up the location to jump to when an
93  * exception occurs.  When an exception occurs the variable "state"
94  * is used to figure out how far we had gotten.
95  */
96 
97 main(argc, argv)  char **argv; {
98 	struct jmploc jmploc;
99 	struct stackmark smark;
100 	volatile int state;
101 	char *shinit;
102 
103 #if PROFILE
104 	monitor(4, etext, profile_buf, sizeof profile_buf, 50);
105 #endif
106 	state = 0;
107 	if (setjmp(jmploc.loc)) {
108 		/*
109 		 * When a shell procedure is executed, we raise the
110 		 * exception EXSHELLPROC to clean up before executing
111 		 * the shell procedure.
112 		 */
113 		if (exception == EXSHELLPROC) {
114 			rootpid = getpid();
115 			rootshell = 1;
116 			minusc = NULL;
117 			state = 3;
118 		} else if (state == 0 || iflag == 0 || ! rootshell)
119 			exitshell(2);
120 		reset();
121 		if (exception == EXINT
122 #if ATTY
123 		 && (! attyset() || equal(termval(), "emacs"))
124 #endif
125 		 ) {
126 			out2c('\n');
127 			flushout(&errout);
128 		}
129 		popstackmark(&smark);
130 		FORCEINTON;				/* enable interrupts */
131 		if (state == 1)
132 			goto state1;
133 		else if (state == 2)
134 			goto state2;
135 		else if (state == 3)
136 			goto state3;
137 		else
138 			goto state4;
139 	}
140 	handler = &jmploc;
141 #ifdef DEBUG
142 	opentrace();
143 	trputs("Shell args:  ");  trargs(argv);
144 #endif
145 	rootpid = getpid();
146 	rootshell = 1;
147 	init();
148 	setstackmark(&smark);
149 	procargs(argc, argv);
150 	if (argv[0] && argv[0][0] == '-') {
151 		state = 1;
152 		read_profile("/etc/profile");
153 state1:
154 		state = 2;
155 		read_profile(".profile");
156 	}
157 state2:
158 	state = 3;
159 	if ((shinit = lookupvar("ENV")) != NULL &&
160 	     *shinit != '\0') {
161 		state = 3;
162 		read_profile(shinit);
163 	}
164 state3:
165 	state = 4;
166 	if (minusc) {
167 		evalstring(minusc);
168 	}
169 	if (sflag || minusc == NULL) {
170 state4:	/* XXX ??? - why isn't this before the "if" statement */
171 		cmdloop(1);
172 	}
173 #if PROFILE
174 	monitor(0);
175 #endif
176 	exitshell(exitstatus);
177 }
178 
179 
180 /*
181  * Read and execute commands.  "Top" is nonzero for the top level command
182  * loop; it turns on prompting if the shell is interactive.
183  */
184 
185 void
186 cmdloop(top) {
187 	union node *n;
188 	struct stackmark smark;
189 	int inter;
190 	int numeof = 0;
191 
192 	TRACE(("cmdloop(%d) called\n", top));
193 	setstackmark(&smark);
194 	for (;;) {
195 		if (pendingsigs)
196 			dotrap();
197 		inter = 0;
198 		if (iflag && top) {
199 			inter++;
200 			showjobs(1);
201 			chkmail(0);
202 			flushout(&output);
203 		}
204 		n = parsecmd(inter);
205 		/* showtree(n); DEBUG */
206 		if (n == NEOF) {
207 			if (!top || numeof >= 50)
208 				break;
209 			if (!stoppedjobs()) {
210 				if (!Iflag)
211 					break;
212 				out2str("\nUse \"exit\" to leave shell.\n");
213 			}
214 			numeof++;
215 		} else if (n != NULL && nflag == 0) {
216 			job_warning = (job_warning == 2) ? 1 : 0;
217 			numeof = 0;
218 			evaltree(n, 0);
219 		}
220 		popstackmark(&smark);
221 	}
222 	popstackmark(&smark);		/* unnecessary */
223 }
224 
225 
226 
227 /*
228  * Read /etc/profile or .profile.  Return on error.
229  */
230 
231 STATIC void
232 read_profile(name)
233 	char *name;
234 	{
235 	int fd;
236 
237 	INTOFF;
238 	if ((fd = open(name, O_RDONLY)) >= 0)
239 		setinputfd(fd, 1);
240 	INTON;
241 	if (fd < 0)
242 		return;
243 	cmdloop(0);
244 	popfile();
245 }
246 
247 
248 
249 /*
250  * Read a file containing shell functions.
251  */
252 
253 void
254 readcmdfile(name)
255 	char *name;
256 	{
257 	int fd;
258 
259 	INTOFF;
260 	if ((fd = open(name, O_RDONLY)) >= 0)
261 		setinputfd(fd, 1);
262 	else
263 		error("Can't open %s", name);
264 	INTON;
265 	cmdloop(0);
266 	popfile();
267 }
268 
269 
270 
271 /*
272  * Take commands from a file.  To be compatable we should do a path
273  * search for the file, but a path search doesn't make any sense.
274  */
275 
276 dotcmd(argc, argv)  char **argv; {
277 	exitstatus = 0;
278 	if (argc >= 2) {		/* That's what SVR2 does */
279 		setinputfile(argv[1], 1);
280 		commandname = argv[1];
281 		cmdloop(0);
282 		popfile();
283 	}
284 	return exitstatus;
285 }
286 
287 
288 exitcmd(argc, argv)  char **argv; {
289 	if (stoppedjobs())
290 		return;
291 	if (argc > 1)
292 		exitstatus = number(argv[1]);
293 	exitshell(exitstatus);
294 }
295 
296 
297 #ifdef notdef
298 /*
299  * Should never be called.
300  */
301 
302 void
303 exit(exitstatus) {
304 	_exit(exitstatus);
305 }
306 #endif
307