11556Srgrimes/*-
21556Srgrimes * Copyright (c) 1991, 1993
31556Srgrimes *	The Regents of the University of California.  All rights reserved.
41556Srgrimes *
51556Srgrimes * This code is derived from software contributed to Berkeley by
61556Srgrimes * Kenneth Almquist.
71556Srgrimes *
81556Srgrimes * Redistribution and use in source and binary forms, with or without
91556Srgrimes * modification, are permitted provided that the following conditions
101556Srgrimes * are met:
111556Srgrimes * 1. Redistributions of source code must retain the above copyright
121556Srgrimes *    notice, this list of conditions and the following disclaimer.
131556Srgrimes * 2. Redistributions in binary form must reproduce the above copyright
141556Srgrimes *    notice, this list of conditions and the following disclaimer in the
151556Srgrimes *    documentation and/or other materials provided with the distribution.
161556Srgrimes * 4. Neither the name of the University nor the names of its contributors
171556Srgrimes *    may be used to endorse or promote products derived from this software
181556Srgrimes *    without specific prior written permission.
191556Srgrimes *
201556Srgrimes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
211556Srgrimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
221556Srgrimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
231556Srgrimes * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
241556Srgrimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
251556Srgrimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
261556Srgrimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
271556Srgrimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
281556Srgrimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
291556Srgrimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
301556Srgrimes * SUCH DAMAGE.
311556Srgrimes */
321556Srgrimes
331556Srgrimes#ifndef lint
3420425Sstevestatic char const copyright[] =
351556Srgrimes"@(#) Copyright (c) 1991, 1993\n\
361556Srgrimes	The Regents of the University of California.  All rights reserved.\n";
371556Srgrimes#endif /* not lint */
381556Srgrimes
391556Srgrimes#ifndef lint
4036150Scharnier#if 0
4136150Scharnierstatic char sccsid[] = "@(#)main.c	8.6 (Berkeley) 5/28/95";
4236150Scharnier#endif
431556Srgrimes#endif /* not lint */
4499110Sobrien#include <sys/cdefs.h>
4599110Sobrien__FBSDID("$FreeBSD$");
461556Srgrimes
4717987Speter#include <stdio.h>
481556Srgrimes#include <signal.h>
4917987Speter#include <sys/stat.h>
5017987Speter#include <unistd.h>
511556Srgrimes#include <fcntl.h>
5217525Sache#include <locale.h>
5359214Simp#include <errno.h>
5417525Sache
551556Srgrimes#include "shell.h"
561556Srgrimes#include "main.h"
571556Srgrimes#include "mail.h"
581556Srgrimes#include "options.h"
591556Srgrimes#include "output.h"
601556Srgrimes#include "parser.h"
611556Srgrimes#include "nodes.h"
6217987Speter#include "expand.h"
631556Srgrimes#include "eval.h"
641556Srgrimes#include "jobs.h"
651556Srgrimes#include "input.h"
661556Srgrimes#include "trap.h"
671556Srgrimes#include "var.h"
6817987Speter#include "show.h"
691556Srgrimes#include "memalloc.h"
701556Srgrimes#include "error.h"
711556Srgrimes#include "init.h"
721556Srgrimes#include "mystring.h"
7317987Speter#include "exec.h"
7420425Ssteve#include "cd.h"
75223060Sjilles#include "builtins.h"
761556Srgrimes
771556Srgrimesint rootpid;
781556Srgrimesint rootshell;
79200998Sjillesstruct jmploc main_handler;
80221669Sjillesint localeisutf8, initial_localeisutf8;
811556Srgrimes
82231790Sjillesstatic void cmdloop(int);
83222957Sjillesstatic void read_profile(char *);
84213811Sobrienstatic char *find_dot_file(char *);
851556Srgrimes
861556Srgrimes/*
871556Srgrimes * Main routine.  We initialize things, parse the arguments, execute
881556Srgrimes * profiles if we're a login shell, and then call cmdloop to execute
891556Srgrimes * commands.  The setjmp call sets up the location to jump to when an
901556Srgrimes * exception occurs.  When an exception occurs the variable "state"
911556Srgrimes * is used to figure out how far we had gotten.
921556Srgrimes */
931556Srgrimes
9417987Speterint
9590111Simpmain(int argc, char *argv[])
9617987Speter{
97222957Sjilles	struct stackmark smark, smark2;
981556Srgrimes	volatile int state;
991556Srgrimes	char *shinit;
1001556Srgrimes
10117525Sache	(void) setlocale(LC_ALL, "");
102221669Sjilles	initcharset();
1031556Srgrimes	state = 0;
104200998Sjilles	if (setjmp(main_handler.loc)) {
10520425Ssteve		switch (exception) {
10620425Ssteve		case EXEXEC:
10720425Ssteve			exitstatus = exerrno;
10820425Ssteve			break;
10920425Ssteve
11020425Ssteve		case EXERROR:
11120425Ssteve			exitstatus = 2;
11220425Ssteve			break;
11320425Ssteve
11420425Ssteve		default:
11520425Ssteve			break;
11620425Ssteve		}
11720425Ssteve
118220978Sjilles		if (state == 0 || iflag == 0 || ! rootshell ||
119220978Sjilles		    exception == EXEXIT)
120218306Sjilles			exitshell(exitstatus);
1211556Srgrimes		reset();
122215567Sjilles		if (exception == EXINT)
123215567Sjilles			out2fmt_flush("\n");
1241556Srgrimes		popstackmark(&smark);
1251556Srgrimes		FORCEINTON;				/* enable interrupts */
1261556Srgrimes		if (state == 1)
1271556Srgrimes			goto state1;
1281556Srgrimes		else if (state == 2)
1291556Srgrimes			goto state2;
1301556Srgrimes		else if (state == 3)
1311556Srgrimes			goto state3;
1321556Srgrimes		else
1331556Srgrimes			goto state4;
1341556Srgrimes	}
135200998Sjilles	handler = &main_handler;
1361556Srgrimes#ifdef DEBUG
1371556Srgrimes	opentrace();
1381556Srgrimes	trputs("Shell args:  ");  trargs(argv);
1391556Srgrimes#endif
1401556Srgrimes	rootpid = getpid();
1411556Srgrimes	rootshell = 1;
1421556Srgrimes	init();
1431556Srgrimes	setstackmark(&smark);
144222957Sjilles	setstackmark(&smark2);
1451556Srgrimes	procargs(argc, argv);
146206759Sjilles	pwd_init(iflag);
147203576Sjilles	if (iflag)
148203576Sjilles		chkmail(1);
1491556Srgrimes	if (argv[0] && argv[0][0] == '-') {
1501556Srgrimes		state = 1;
1511556Srgrimes		read_profile("/etc/profile");
1521556Srgrimesstate1:
1531556Srgrimes		state = 2;
15419240Ssteve		if (privileged == 0)
155223005Sjilles			read_profile("${HOME-}/.profile");
15619240Ssteve		else
15719240Ssteve			read_profile("/etc/suid_profile");
1588855Srgrimes	}
1591556Srgrimesstate2:
1601556Srgrimes	state = 3;
16125471Ssteve	if (!privileged && iflag) {
16217987Speter		if ((shinit = lookupvar("ENV")) != NULL && *shinit != '\0') {
16317987Speter			state = 3;
16417987Speter			read_profile(shinit);
16517987Speter		}
1661556Srgrimes	}
1671556Srgrimesstate3:
1681556Srgrimes	state = 4;
169222957Sjilles	popstackmark(&smark2);
1701556Srgrimes	if (minusc) {
171194128Sjilles		evalstring(minusc, sflag ? 0 : EV_EXIT);
1721556Srgrimes	}
1731556Srgrimes	if (sflag || minusc == NULL) {
1741556Srgrimesstate4:	/* XXX ??? - why isn't this before the "if" statement */
1751556Srgrimes		cmdloop(1);
1761556Srgrimes	}
1771556Srgrimes	exitshell(exitstatus);
17817987Speter	/*NOTREACHED*/
17917987Speter	return 0;
1801556Srgrimes}
1811556Srgrimes
1821556Srgrimes
1831556Srgrimes/*
1841556Srgrimes * Read and execute commands.  "Top" is nonzero for the top level command
1851556Srgrimes * loop; it turns on prompting if the shell is interactive.
1861556Srgrimes */
1871556Srgrimes
188231790Sjillesstatic void
18990111Simpcmdloop(int top)
19017987Speter{
1911556Srgrimes	union node *n;
1921556Srgrimes	struct stackmark smark;
1931556Srgrimes	int inter;
1941556Srgrimes	int numeof = 0;
1951556Srgrimes
1961556Srgrimes	TRACE(("cmdloop(%d) called\n", top));
1971556Srgrimes	setstackmark(&smark);
1981556Srgrimes	for (;;) {
1991556Srgrimes		if (pendingsigs)
2001556Srgrimes			dotrap();
2011556Srgrimes		inter = 0;
2021556Srgrimes		if (iflag && top) {
2031556Srgrimes			inter++;
204163085Sstefanf			showjobs(1, SHOWJOBS_DEFAULT);
2051556Srgrimes			chkmail(0);
2061556Srgrimes			flushout(&output);
2071556Srgrimes		}
2081556Srgrimes		n = parsecmd(inter);
2091556Srgrimes		/* showtree(n); DEBUG */
2101556Srgrimes		if (n == NEOF) {
2111556Srgrimes			if (!top || numeof >= 50)
2121556Srgrimes				break;
2131556Srgrimes			if (!stoppedjobs()) {
2141556Srgrimes				if (!Iflag)
2151556Srgrimes					break;
216199629Sjilles				out2fmt_flush("\nUse \"exit\" to leave shell.\n");
2171556Srgrimes			}
2181556Srgrimes			numeof++;
2191556Srgrimes		} else if (n != NULL && nflag == 0) {
2201556Srgrimes			job_warning = (job_warning == 2) ? 1 : 0;
2211556Srgrimes			numeof = 0;
2221556Srgrimes			evaltree(n, 0);
2231556Srgrimes		}
2241556Srgrimes		popstackmark(&smark);
22564702Scracauer		setstackmark(&smark);
226211349Sjilles		if (evalskip != 0) {
227211349Sjilles			if (evalskip == SKIPFILE)
228211349Sjilles				evalskip = 0;
22920425Ssteve			break;
23020425Ssteve		}
2311556Srgrimes	}
23264702Scracauer	popstackmark(&smark);
2331556Srgrimes}
2341556Srgrimes
2351556Srgrimes
2361556Srgrimes
2371556Srgrimes/*
2381556Srgrimes * Read /etc/profile or .profile.  Return on error.
2391556Srgrimes */
2401556Srgrimes
241213811Sobrienstatic void
242222957Sjillesread_profile(char *name)
24390111Simp{
2441556Srgrimes	int fd;
245222957Sjilles	const char *expandedname;
2461556Srgrimes
247222957Sjilles	expandedname = expandstr(name);
248222957Sjilles	if (expandedname == NULL)
249222957Sjilles		return;
2501556Srgrimes	INTOFF;
251222957Sjilles	if ((fd = open(expandedname, O_RDONLY)) >= 0)
2521556Srgrimes		setinputfd(fd, 1);
2531556Srgrimes	INTON;
2541556Srgrimes	if (fd < 0)
2551556Srgrimes		return;
2561556Srgrimes	cmdloop(0);
2571556Srgrimes	popfile();
2581556Srgrimes}
2591556Srgrimes
2601556Srgrimes
2611556Srgrimes
2621556Srgrimes/*
2631556Srgrimes * Read a file containing shell functions.
2641556Srgrimes */
2651556Srgrimes
2661556Srgrimesvoid
267200956Sjillesreadcmdfile(const char *name)
26817987Speter{
269241030Sjilles	setinputfile(name, 1);
2701556Srgrimes	cmdloop(0);
2711556Srgrimes	popfile();
2721556Srgrimes}
2731556Srgrimes
2741556Srgrimes
2751556Srgrimes
2761556Srgrimes/*
27746684Skris * Take commands from a file.  To be compatible we should do a path
27817987Speter * search for the file, which is necessary to find sub-commands.
2791556Srgrimes */
2801556Srgrimes
28117987Speter
282213811Sobrienstatic char *
28390111Simpfind_dot_file(char *basename)
28417987Speter{
28517987Speter	char *fullname;
286200956Sjilles	const char *path = pathval();
28717987Speter	struct stat statb;
28817987Speter
28917987Speter	/* don't try this for absolute or relative paths */
29017987Speter	if( strchr(basename, '/'))
29117987Speter		return basename;
29217987Speter
29317987Speter	while ((fullname = padvance(&path, basename)) != NULL) {
294222173Sjilles		if ((stat(fullname, &statb) == 0) && S_ISREG(statb.st_mode)) {
295222173Sjilles			/*
296222173Sjilles			 * Don't bother freeing here, since it will
297222173Sjilles			 * be freed by the caller.
298222173Sjilles			 */
299222173Sjilles			return fullname;
300222173Sjilles		}
30117987Speter		stunalloc(fullname);
30217987Speter	}
30317987Speter	return basename;
30417987Speter}
30517987Speter
30617987Speterint
30790111Simpdotcmd(int argc, char **argv)
30817987Speter{
309208630Sjilles	char *filename, *fullname;
310157414Sstefanf
311157414Sstefanf	if (argc < 2)
312157414Sstefanf		error("missing filename");
313157414Sstefanf
3141556Srgrimes	exitstatus = 0;
31517987Speter
316208630Sjilles	/*
317208630Sjilles	 * Because we have historically not supported any options,
318208630Sjilles	 * only treat "--" specially.
319208630Sjilles	 */
320208630Sjilles	filename = argc > 2 && strcmp(argv[1], "--") == 0 ? argv[2] : argv[1];
321208630Sjilles
322208630Sjilles	fullname = find_dot_file(filename);
323157414Sstefanf	setinputfile(fullname, 1);
324157414Sstefanf	commandname = fullname;
325157414Sstefanf	cmdloop(0);
326157414Sstefanf	popfile();
3271556Srgrimes	return exitstatus;
3281556Srgrimes}
3291556Srgrimes
3301556Srgrimes
33117987Speterint
33290111Simpexitcmd(int argc, char **argv)
33317987Speter{
3341556Srgrimes	if (stoppedjobs())
33517987Speter		return 0;
33620425Ssteve	if (argc > 1)
337217175Sjilles		exitshell(number(argv[1]));
33820425Ssteve	else
339217175Sjilles		exitshell_savedstatus();
3401556Srgrimes}
341