main.c revision 248980
1240116Smarcel/*-
2240116Smarcel * Copyright (c) 1991, 1993
3240116Smarcel *	The Regents of the University of California.  All rights reserved.
4240116Smarcel *
5240116Smarcel * This code is derived from software contributed to Berkeley by
6240116Smarcel * Kenneth Almquist.
7240116Smarcel *
8240116Smarcel * Redistribution and use in source and binary forms, with or without
9240116Smarcel * modification, are permitted provided that the following conditions
10240116Smarcel * are met:
11240116Smarcel * 1. Redistributions of source code must retain the above copyright
12240116Smarcel *    notice, this list of conditions and the following disclaimer.
13240116Smarcel * 2. Redistributions in binary form must reproduce the above copyright
14240116Smarcel *    notice, this list of conditions and the following disclaimer in the
15240116Smarcel *    documentation and/or other materials provided with the distribution.
16240116Smarcel * 4. Neither the name of the University nor the names of its contributors
17240116Smarcel *    may be used to endorse or promote products derived from this software
18240116Smarcel *    without specific prior written permission.
19240116Smarcel *
20240116Smarcel * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
21240116Smarcel * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22240116Smarcel * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23240116Smarcel * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
24240116Smarcel * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25240116Smarcel * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26240116Smarcel * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27240116Smarcel * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28240116Smarcel * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29240116Smarcel * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30240116Smarcel * SUCH DAMAGE.
31240116Smarcel */
32240116Smarcel
33240116Smarcel#ifndef lint
34240116Smarcelstatic char const copyright[] =
35240116Smarcel"@(#) Copyright (c) 1991, 1993\n\
36240116Smarcel	The Regents of the University of California.  All rights reserved.\n";
37240116Smarcel#endif /* not lint */
38240116Smarcel
39240116Smarcel#ifndef lint
40240116Smarcel#if 0
41240116Smarcelstatic char sccsid[] = "@(#)main.c	8.6 (Berkeley) 5/28/95";
42240116Smarcel#endif
43240116Smarcel#endif /* not lint */
44240116Smarcel#include <sys/cdefs.h>
45240116Smarcel__FBSDID("$FreeBSD: head/bin/sh/main.c 248980 2013-04-01 17:18:22Z jilles $");
46240116Smarcel
47240116Smarcel#include <stdio.h>
48240116Smarcel#include <signal.h>
49240116Smarcel#include <sys/stat.h>
50240116Smarcel#include <unistd.h>
51240116Smarcel#include <fcntl.h>
52240116Smarcel#include <locale.h>
53240116Smarcel#include <errno.h>
54240116Smarcel
55240116Smarcel#include "shell.h"
56240116Smarcel#include "main.h"
57240116Smarcel#include "mail.h"
58240116Smarcel#include "options.h"
59240116Smarcel#include "output.h"
60240116Smarcel#include "parser.h"
61240116Smarcel#include "nodes.h"
62240116Smarcel#include "expand.h"
63240116Smarcel#include "eval.h"
64240116Smarcel#include "jobs.h"
65240116Smarcel#include "input.h"
66240116Smarcel#include "trap.h"
67240116Smarcel#include "var.h"
68240116Smarcel#include "show.h"
69240116Smarcel#include "memalloc.h"
70240116Smarcel#include "error.h"
71240116Smarcel#include "init.h"
72240116Smarcel#include "mystring.h"
73240116Smarcel#include "exec.h"
74240116Smarcel#include "cd.h"
75240116Smarcel#include "builtins.h"
76240116Smarcel
77240116Smarcelint rootpid;
78240116Smarcelint rootshell;
79240116Smarcelstruct jmploc main_handler;
80240116Smarcelint localeisutf8, initial_localeisutf8;
81240116Smarcel
82240116Smarcelstatic void cmdloop(int);
83240116Smarcelstatic void read_profile(const char *);
84240116Smarcelstatic char *find_dot_file(char *);
85240116Smarcel
86240116Smarcel/*
87240116Smarcel * Main routine.  We initialize things, parse the arguments, execute
88240116Smarcel * profiles if we're a login shell, and then call cmdloop to execute
89240116Smarcel * commands.  The setjmp call sets up the location to jump to when an
90240116Smarcel * exception occurs.  When an exception occurs the variable "state"
91240116Smarcel * is used to figure out how far we had gotten.
92240116Smarcel */
93240116Smarcel
94240116Smarcelint
95240116Smarcelmain(int argc, char *argv[])
96240116Smarcel{
97240116Smarcel	struct stackmark smark, smark2;
98240116Smarcel	volatile int state;
99240116Smarcel	char *shinit;
100240116Smarcel
101240116Smarcel	(void) setlocale(LC_ALL, "");
102240116Smarcel	initcharset();
103240116Smarcel	state = 0;
104240116Smarcel	if (setjmp(main_handler.loc)) {
105240116Smarcel		switch (exception) {
106240116Smarcel		case EXEXEC:
107240116Smarcel			exitstatus = exerrno;
108240116Smarcel			break;
109240116Smarcel
110240116Smarcel		case EXERROR:
111240116Smarcel			exitstatus = 2;
112240116Smarcel			break;
113240116Smarcel
114240116Smarcel		default:
115240116Smarcel			break;
116240116Smarcel		}
117240116Smarcel
118240116Smarcel		if (state == 0 || iflag == 0 || ! rootshell ||
119240116Smarcel		    exception == EXEXIT)
120240116Smarcel			exitshell(exitstatus);
121240116Smarcel		reset();
122240116Smarcel		if (exception == EXINT)
123240116Smarcel			out2fmt_flush("\n");
124240116Smarcel		popstackmark(&smark);
125240116Smarcel		FORCEINTON;				/* enable interrupts */
126240116Smarcel		if (state == 1)
127240116Smarcel			goto state1;
128240116Smarcel		else if (state == 2)
129240116Smarcel			goto state2;
130240116Smarcel		else if (state == 3)
131240116Smarcel			goto state3;
132240116Smarcel		else
133240116Smarcel			goto state4;
134240116Smarcel	}
135240116Smarcel	handler = &main_handler;
136240116Smarcel#ifdef DEBUG
137240116Smarcel	opentrace();
138240116Smarcel	trputs("Shell args:  ");  trargs(argv);
139240116Smarcel#endif
140240116Smarcel	rootpid = getpid();
141240116Smarcel	rootshell = 1;
142240116Smarcel	initvar();
143240116Smarcel	setstackmark(&smark);
144240116Smarcel	setstackmark(&smark2);
145240116Smarcel	procargs(argc, argv);
146240116Smarcel	pwd_init(iflag);
147240116Smarcel	if (iflag)
148240116Smarcel		chkmail(1);
149240116Smarcel	if (argv[0] && argv[0][0] == '-') {
150240116Smarcel		state = 1;
151240116Smarcel		read_profile("/etc/profile");
152240116Smarcelstate1:
153240116Smarcel		state = 2;
154240116Smarcel		if (privileged == 0)
155240116Smarcel			read_profile("${HOME-}/.profile");
156240116Smarcel		else
157240116Smarcel			read_profile("/etc/suid_profile");
158240116Smarcel	}
159240116Smarcelstate2:
160240116Smarcel	state = 3;
161240116Smarcel	if (!privileged && iflag) {
162240116Smarcel		if ((shinit = lookupvar("ENV")) != NULL && *shinit != '\0') {
163240116Smarcel			state = 3;
164240116Smarcel			read_profile(shinit);
165240116Smarcel		}
166240116Smarcel	}
167240116Smarcelstate3:
168240116Smarcel	state = 4;
169240116Smarcel	popstackmark(&smark2);
170240116Smarcel	if (minusc) {
171240116Smarcel		evalstring(minusc, sflag ? 0 : EV_EXIT);
172240116Smarcel	}
173240116Smarcel	if (sflag || minusc == NULL) {
174240116Smarcelstate4:	/* XXX ??? - why isn't this before the "if" statement */
175240116Smarcel		cmdloop(1);
176240116Smarcel	}
177240116Smarcel	exitshell(exitstatus);
178240116Smarcel	/*NOTREACHED*/
179240116Smarcel	return 0;
180240116Smarcel}
181240116Smarcel
182240116Smarcel
183240116Smarcel/*
184240116Smarcel * Read and execute commands.  "Top" is nonzero for the top level command
185240116Smarcel * loop; it turns on prompting if the shell is interactive.
186240116Smarcel */
187240116Smarcel
188240116Smarcelstatic void
189240116Smarcelcmdloop(int top)
190240116Smarcel{
191240116Smarcel	union node *n;
192240116Smarcel	struct stackmark smark;
193240116Smarcel	int inter;
194240116Smarcel	int numeof = 0;
195240116Smarcel
196240116Smarcel	TRACE(("cmdloop(%d) called\n", top));
197240116Smarcel	setstackmark(&smark);
198240116Smarcel	for (;;) {
199240116Smarcel		if (pendingsig)
200240116Smarcel			dotrap();
201240116Smarcel		inter = 0;
202240116Smarcel		if (iflag && top) {
203240116Smarcel			inter++;
204240116Smarcel			showjobs(1, SHOWJOBS_DEFAULT);
205240116Smarcel			chkmail(0);
206240116Smarcel			flushout(&output);
207240116Smarcel		}
208240116Smarcel		n = parsecmd(inter);
209240116Smarcel		/* showtree(n); DEBUG */
210240116Smarcel		if (n == NEOF) {
211240116Smarcel			if (!top || numeof >= 50)
212240116Smarcel				break;
213240116Smarcel			if (!stoppedjobs()) {
214240116Smarcel				if (!Iflag)
215240116Smarcel					break;
216240116Smarcel				out2fmt_flush("\nUse \"exit\" to leave shell.\n");
217240116Smarcel			}
218240116Smarcel			numeof++;
219240116Smarcel		} else if (n != NULL && nflag == 0) {
220240116Smarcel			job_warning = (job_warning == 2) ? 1 : 0;
221240116Smarcel			numeof = 0;
222240116Smarcel			evaltree(n, 0);
223240116Smarcel		}
224240116Smarcel		popstackmark(&smark);
225240116Smarcel		setstackmark(&smark);
226240116Smarcel		if (evalskip != 0) {
227240116Smarcel			if (evalskip == SKIPFILE)
228240116Smarcel				evalskip = 0;
229240116Smarcel			break;
230240116Smarcel		}
231240116Smarcel	}
232	popstackmark(&smark);
233}
234
235
236
237/*
238 * Read /etc/profile or .profile.  Return on error.
239 */
240
241static void
242read_profile(const char *name)
243{
244	int fd;
245	const char *expandedname;
246
247	expandedname = expandstr(name);
248	if (expandedname == NULL)
249		return;
250	INTOFF;
251	if ((fd = open(expandedname, O_RDONLY)) >= 0)
252		setinputfd(fd, 1);
253	INTON;
254	if (fd < 0)
255		return;
256	cmdloop(0);
257	popfile();
258}
259
260
261
262/*
263 * Read a file containing shell functions.
264 */
265
266void
267readcmdfile(const char *name)
268{
269	setinputfile(name, 1);
270	cmdloop(0);
271	popfile();
272}
273
274
275
276/*
277 * Take commands from a file.  To be compatible we should do a path
278 * search for the file, which is necessary to find sub-commands.
279 */
280
281
282static char *
283find_dot_file(char *basename)
284{
285	char *fullname;
286	const char *path = pathval();
287	struct stat statb;
288
289	/* don't try this for absolute or relative paths */
290	if( strchr(basename, '/'))
291		return basename;
292
293	while ((fullname = padvance(&path, basename)) != NULL) {
294		if ((stat(fullname, &statb) == 0) && S_ISREG(statb.st_mode)) {
295			/*
296			 * Don't bother freeing here, since it will
297			 * be freed by the caller.
298			 */
299			return fullname;
300		}
301		stunalloc(fullname);
302	}
303	return basename;
304}
305
306int
307dotcmd(int argc, char **argv)
308{
309	char *filename, *fullname;
310
311	if (argc < 2)
312		error("missing filename");
313
314	exitstatus = 0;
315
316	/*
317	 * Because we have historically not supported any options,
318	 * only treat "--" specially.
319	 */
320	filename = argc > 2 && strcmp(argv[1], "--") == 0 ? argv[2] : argv[1];
321
322	fullname = find_dot_file(filename);
323	setinputfile(fullname, 1);
324	commandname = fullname;
325	cmdloop(0);
326	popfile();
327	return exitstatus;
328}
329
330
331int
332exitcmd(int argc, char **argv)
333{
334	if (stoppedjobs())
335		return 0;
336	if (argc > 1)
337		exitshell(number(argv[1]));
338	else
339		exitshell_savedstatus();
340}
341