xref: /dragonfly/bin/sh/main.c (revision 7bc7e232)
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  * @(#) Copyright (c) 1991, 1993 The Regents of the University of California.  All rights reserved.
37  * @(#)main.c	8.6 (Berkeley) 5/28/95
38  * $FreeBSD: src/bin/sh/main.c,v 1.29 2006/10/07 16:51:16 stefanf Exp $
39  * $DragonFly: src/bin/sh/main.c,v 1.8 2007/01/13 23:36:14 pavalos Exp $
40  */
41 
42 #include <stdio.h>
43 #include <signal.h>
44 #include <sys/stat.h>
45 #include <unistd.h>
46 #include <fcntl.h>
47 #include <locale.h>
48 #include <errno.h>
49 
50 #include "shell.h"
51 #include "main.h"
52 #include "mail.h"
53 #include "options.h"
54 #include "output.h"
55 #include "parser.h"
56 #include "nodes.h"
57 #include "expand.h"
58 #include "eval.h"
59 #include "jobs.h"
60 #include "input.h"
61 #include "trap.h"
62 #include "var.h"
63 #include "show.h"
64 #include "memalloc.h"
65 #include "error.h"
66 #include "init.h"
67 #include "mystring.h"
68 #include "exec.h"
69 #include "cd.h"
70 
71 int rootpid;
72 int rootshell;
73 
74 STATIC void read_profile(const char *);
75 STATIC const char *find_dot_file(const char *);
76 
77 extern int oexitstatus;
78 
79 /*
80  * Main routine.  We initialize things, parse the arguments, execute
81  * profiles if we're a login shell, and then call cmdloop to execute
82  * commands.  The setjmp call sets up the location to jump to when an
83  * exception occurs.  When an exception occurs the variable "state"
84  * is used to figure out how far we had gotten.
85  */
86 
87 int
88 main(int argc, char *argv[])
89 {
90 	struct jmploc jmploc;
91 	struct stackmark smark;
92 	volatile int state;
93 	char *shinit;
94 
95 	setlocale(LC_ALL, "");
96 	state = 0;
97 	if (setjmp(jmploc.loc)) {
98 		/*
99 		 * When a shell procedure is executed, we raise the
100 		 * exception EXSHELLPROC to clean up before executing
101 		 * the shell procedure.
102 		 */
103 		switch (exception) {
104 		case EXSHELLPROC:
105 			rootpid = getpid();
106 			rootshell = 1;
107 			minusc = NULL;
108 			state = 3;
109 			break;
110 
111 		case EXEXEC:
112 			exitstatus = exerrno;
113 			break;
114 
115 		case EXERROR:
116 			exitstatus = 2;
117 			break;
118 
119 		default:
120 			break;
121 		}
122 
123 		if (exception != EXSHELLPROC) {
124 		    if (state == 0 || iflag == 0 || ! rootshell)
125 			    exitshell(exitstatus);
126 		}
127 		reset();
128 		if (exception == EXINT) {
129 			out2c('\n');
130 			flushout(&errout);
131 		}
132 		popstackmark(&smark);
133 		FORCEINTON;				/* enable interrupts */
134 		if (state == 1)
135 			goto state1;
136 		else if (state == 2)
137 			goto state2;
138 		else if (state == 3)
139 			goto state3;
140 		else
141 			goto state4;
142 	}
143 	handler = &jmploc;
144 #ifdef DEBUG
145 	opentrace();
146 	trputs("Shell args:  ");  trargs(argv);
147 #endif
148 	rootpid = getpid();
149 	rootshell = 1;
150 	init();
151 	setstackmark(&smark);
152 	procargs(argc, argv);
153 	if (getpwd() == NULL && iflag)
154 		out2str("sh: cannot determine working directory\n");
155 	if (getpwd() != NULL)
156 		setvar ("PWD", getpwd(), VEXPORT);
157 	if (argv[0] && argv[0][0] == '-') {
158 		state = 1;
159 		read_profile("/etc/profile");
160 state1:
161 		state = 2;
162 		if (privileged == 0)
163 			read_profile(".profile");
164 		else
165 			read_profile("/etc/suid_profile");
166 	}
167 state2:
168 	state = 3;
169 	if (!privileged && iflag) {
170 		if ((shinit = lookupvar("ENV")) != NULL && *shinit != '\0') {
171 			state = 3;
172 			read_profile(shinit);
173 		}
174 	}
175 state3:
176 	state = 4;
177 	if (minusc) {
178 		evalstring(minusc);
179 	}
180 	if (sflag || minusc == NULL) {
181 state4:	/* XXX ??? - why isn't this before the "if" statement */
182 		cmdloop(1);
183 	}
184 	exitshell(exitstatus);
185 	/*NOTREACHED*/
186 	return 0;
187 }
188 
189 
190 /*
191  * Read and execute commands.  "Top" is nonzero for the top level command
192  * loop; it turns on prompting if the shell is interactive.
193  */
194 
195 void
196 cmdloop(int top)
197 {
198 	union node *n;
199 	struct stackmark smark;
200 	int inter;
201 	int numeof = 0;
202 
203 	TRACE(("cmdloop(%d) called\n", top));
204 	setstackmark(&smark);
205 	for (;;) {
206 		if (pendingsigs)
207 			dotrap();
208 		inter = 0;
209 		if (iflag && top) {
210 			inter++;
211 			showjobs(1, SHOWJOBS_DEFAULT);
212 			chkmail(0);
213 			flushout(&output);
214 		}
215 		n = parsecmd(inter);
216 		/* showtree(n); DEBUG */
217 		if (n == NEOF) {
218 			if (!top || numeof >= 50)
219 				break;
220 			if (!stoppedjobs()) {
221 				if (!Iflag)
222 					break;
223 				out2str("\nUse \"exit\" to leave shell.\n");
224 			}
225 			numeof++;
226 		} else if (n != NULL && nflag == 0) {
227 			job_warning = (job_warning == 2) ? 1 : 0;
228 			numeof = 0;
229 			evaltree(n, 0);
230 		}
231 		popstackmark(&smark);
232 		setstackmark(&smark);
233 		if (evalskip == SKIPFILE) {
234 			evalskip = 0;
235 			break;
236 		}
237 	}
238 	popstackmark(&smark);
239 }
240 
241 
242 
243 /*
244  * Read /etc/profile or .profile.  Return on error.
245  */
246 
247 STATIC void
248 read_profile(const 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 
268 void
269 readcmdfile(char *name)
270 {
271 	int fd;
272 
273 	INTOFF;
274 	if ((fd = open(name, O_RDONLY)) >= 0)
275 		setinputfd(fd, 1);
276 	else
277 		error("Can't open %s: %s", name, strerror(errno));
278 	INTON;
279 	cmdloop(0);
280 	popfile();
281 }
282 
283 
284 
285 /*
286  * Take commands from a file.  To be compatible we should do a path
287  * search for the file, which is necessary to find sub-commands.
288  */
289 
290 
291 STATIC const char *
292 find_dot_file(const char *basename)
293 {
294 	static char localname[FILENAME_MAX+1];
295 	char *fullname;
296 	const char *path = pathval();
297 	struct stat statb;
298 
299 	/* don't try this for absolute or relative paths */
300 	if( strchr(basename, '/'))
301 		return basename;
302 
303 	while ((fullname = padvance(&path, basename)) != NULL) {
304 		strcpy(localname, fullname);
305 		stunalloc(fullname);
306 		if ((stat(fullname, &statb) == 0) && S_ISREG(statb.st_mode))
307 			return localname;
308 	}
309 	return basename;
310 }
311 
312 int
313 dotcmd(int argc, char **argv)
314 {
315 	struct strlist *sp;
316 	const char *fullname;
317 
318 	if (argc < 2)
319 		error("missing filename");
320 
321 	exitstatus = 0;
322 
323 	for (sp = cmdenviron; sp ; sp = sp->next)
324 		setvareq(savestr(sp->text), VSTRFIXED|VTEXTFIXED);
325 
326 	fullname = find_dot_file(argv[1]);
327 	setinputfile(fullname, 1);
328 	commandname = fullname;
329 	cmdloop(0);
330 	popfile();
331 	return exitstatus;
332 }
333 
334 
335 int
336 exitcmd(int argc, char **argv)
337 {
338 	if (stoppedjobs())
339 		return 0;
340 	if (argc > 1)
341 		exitstatus = number(argv[1]);
342 	else
343 		exitstatus = oexitstatus;
344 	exitshell(exitstatus);
345 	/*NOTREACHED*/
346 	return 0;
347 }
348