main.c revision 222173
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: head/bin/sh/main.c 222173 2011-05-22 12:12:28Z jilles $");
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"
751556Srgrimes
761556Srgrimesint rootpid;
771556Srgrimesint rootshell;
78200998Sjillesstruct jmploc main_handler;
79221669Sjillesint localeisutf8, initial_localeisutf8;
801556Srgrimes
81213811Sobrienstatic void read_profile(const char *);
82213811Sobrienstatic char *find_dot_file(char *);
831556Srgrimes
841556Srgrimes/*
851556Srgrimes * Main routine.  We initialize things, parse the arguments, execute
861556Srgrimes * profiles if we're a login shell, and then call cmdloop to execute
871556Srgrimes * commands.  The setjmp call sets up the location to jump to when an
881556Srgrimes * exception occurs.  When an exception occurs the variable "state"
891556Srgrimes * is used to figure out how far we had gotten.
901556Srgrimes */
911556Srgrimes
9217987Speterint
9390111Simpmain(int argc, char *argv[])
9417987Speter{
951556Srgrimes	struct stackmark smark;
961556Srgrimes	volatile int state;
971556Srgrimes	char *shinit;
981556Srgrimes
9917525Sache	(void) setlocale(LC_ALL, "");
100221669Sjilles	initcharset();
1011556Srgrimes	state = 0;
102200998Sjilles	if (setjmp(main_handler.loc)) {
10320425Ssteve		switch (exception) {
10420425Ssteve		case EXEXEC:
10520425Ssteve			exitstatus = exerrno;
10620425Ssteve			break;
10720425Ssteve
10820425Ssteve		case EXERROR:
10920425Ssteve			exitstatus = 2;
11020425Ssteve			break;
11120425Ssteve
11220425Ssteve		default:
11320425Ssteve			break;
11420425Ssteve		}
11520425Ssteve
116220978Sjilles		if (state == 0 || iflag == 0 || ! rootshell ||
117220978Sjilles		    exception == EXEXIT)
118218306Sjilles			exitshell(exitstatus);
1191556Srgrimes		reset();
120215567Sjilles		if (exception == EXINT)
121215567Sjilles			out2fmt_flush("\n");
1221556Srgrimes		popstackmark(&smark);
1231556Srgrimes		FORCEINTON;				/* enable interrupts */
1241556Srgrimes		if (state == 1)
1251556Srgrimes			goto state1;
1261556Srgrimes		else if (state == 2)
1271556Srgrimes			goto state2;
1281556Srgrimes		else if (state == 3)
1291556Srgrimes			goto state3;
1301556Srgrimes		else
1311556Srgrimes			goto state4;
1321556Srgrimes	}
133200998Sjilles	handler = &main_handler;
1341556Srgrimes#ifdef DEBUG
1351556Srgrimes	opentrace();
1361556Srgrimes	trputs("Shell args:  ");  trargs(argv);
1371556Srgrimes#endif
1381556Srgrimes	rootpid = getpid();
1391556Srgrimes	rootshell = 1;
1401556Srgrimes	init();
1411556Srgrimes	setstackmark(&smark);
1421556Srgrimes	procargs(argc, argv);
143206759Sjilles	pwd_init(iflag);
144203576Sjilles	if (iflag)
145203576Sjilles		chkmail(1);
1461556Srgrimes	if (argv[0] && argv[0][0] == '-') {
1471556Srgrimes		state = 1;
1481556Srgrimes		read_profile("/etc/profile");
1491556Srgrimesstate1:
1501556Srgrimes		state = 2;
15119240Ssteve		if (privileged == 0)
15219240Ssteve			read_profile(".profile");
15319240Ssteve		else
15419240Ssteve			read_profile("/etc/suid_profile");
1558855Srgrimes	}
1561556Srgrimesstate2:
1571556Srgrimes	state = 3;
15825471Ssteve	if (!privileged && iflag) {
15917987Speter		if ((shinit = lookupvar("ENV")) != NULL && *shinit != '\0') {
16017987Speter			state = 3;
16117987Speter			read_profile(shinit);
16217987Speter		}
1631556Srgrimes	}
1641556Srgrimesstate3:
1651556Srgrimes	state = 4;
1661556Srgrimes	if (minusc) {
167194128Sjilles		evalstring(minusc, sflag ? 0 : EV_EXIT);
1681556Srgrimes	}
1691556Srgrimes	if (sflag || minusc == NULL) {
1701556Srgrimesstate4:	/* XXX ??? - why isn't this before the "if" statement */
1711556Srgrimes		cmdloop(1);
1721556Srgrimes	}
1731556Srgrimes	exitshell(exitstatus);
17417987Speter	/*NOTREACHED*/
17517987Speter	return 0;
1761556Srgrimes}
1771556Srgrimes
1781556Srgrimes
1791556Srgrimes/*
1801556Srgrimes * Read and execute commands.  "Top" is nonzero for the top level command
1811556Srgrimes * loop; it turns on prompting if the shell is interactive.
1821556Srgrimes */
1831556Srgrimes
1841556Srgrimesvoid
18590111Simpcmdloop(int top)
18617987Speter{
1871556Srgrimes	union node *n;
1881556Srgrimes	struct stackmark smark;
1891556Srgrimes	int inter;
1901556Srgrimes	int numeof = 0;
1911556Srgrimes
1921556Srgrimes	TRACE(("cmdloop(%d) called\n", top));
1931556Srgrimes	setstackmark(&smark);
1941556Srgrimes	for (;;) {
1951556Srgrimes		if (pendingsigs)
1961556Srgrimes			dotrap();
1971556Srgrimes		inter = 0;
1981556Srgrimes		if (iflag && top) {
1991556Srgrimes			inter++;
200163085Sstefanf			showjobs(1, SHOWJOBS_DEFAULT);
2011556Srgrimes			chkmail(0);
2021556Srgrimes			flushout(&output);
2031556Srgrimes		}
2041556Srgrimes		n = parsecmd(inter);
2051556Srgrimes		/* showtree(n); DEBUG */
2061556Srgrimes		if (n == NEOF) {
2071556Srgrimes			if (!top || numeof >= 50)
2081556Srgrimes				break;
2091556Srgrimes			if (!stoppedjobs()) {
2101556Srgrimes				if (!Iflag)
2111556Srgrimes					break;
212199629Sjilles				out2fmt_flush("\nUse \"exit\" to leave shell.\n");
2131556Srgrimes			}
2141556Srgrimes			numeof++;
2151556Srgrimes		} else if (n != NULL && nflag == 0) {
2161556Srgrimes			job_warning = (job_warning == 2) ? 1 : 0;
2171556Srgrimes			numeof = 0;
2181556Srgrimes			evaltree(n, 0);
2191556Srgrimes		}
2201556Srgrimes		popstackmark(&smark);
22164702Scracauer		setstackmark(&smark);
222211349Sjilles		if (evalskip != 0) {
223211349Sjilles			if (evalskip == SKIPFILE)
224211349Sjilles				evalskip = 0;
22520425Ssteve			break;
22620425Ssteve		}
2271556Srgrimes	}
22864702Scracauer	popstackmark(&smark);
2291556Srgrimes}
2301556Srgrimes
2311556Srgrimes
2321556Srgrimes
2331556Srgrimes/*
2341556Srgrimes * Read /etc/profile or .profile.  Return on error.
2351556Srgrimes */
2361556Srgrimes
237213811Sobrienstatic void
238201053Sjillesread_profile(const char *name)
23990111Simp{
2401556Srgrimes	int fd;
2411556Srgrimes
2421556Srgrimes	INTOFF;
2431556Srgrimes	if ((fd = open(name, O_RDONLY)) >= 0)
2441556Srgrimes		setinputfd(fd, 1);
2451556Srgrimes	INTON;
2461556Srgrimes	if (fd < 0)
2471556Srgrimes		return;
2481556Srgrimes	cmdloop(0);
2491556Srgrimes	popfile();
2501556Srgrimes}
2511556Srgrimes
2521556Srgrimes
2531556Srgrimes
2541556Srgrimes/*
2551556Srgrimes * Read a file containing shell functions.
2561556Srgrimes */
2571556Srgrimes
2581556Srgrimesvoid
259200956Sjillesreadcmdfile(const char *name)
26017987Speter{
2611556Srgrimes	int fd;
2621556Srgrimes
2631556Srgrimes	INTOFF;
2641556Srgrimes	if ((fd = open(name, O_RDONLY)) >= 0)
2651556Srgrimes		setinputfd(fd, 1);
2661556Srgrimes	else
26753891Scracauer		error("Can't open %s: %s", name, strerror(errno));
2681556Srgrimes	INTON;
2691556Srgrimes	cmdloop(0);
2701556Srgrimes	popfile();
2711556Srgrimes}
2721556Srgrimes
2731556Srgrimes
2741556Srgrimes
2751556Srgrimes/*
27646684Skris * Take commands from a file.  To be compatible we should do a path
27717987Speter * search for the file, which is necessary to find sub-commands.
2781556Srgrimes */
2791556Srgrimes
28017987Speter
281213811Sobrienstatic char *
28290111Simpfind_dot_file(char *basename)
28317987Speter{
28417987Speter	char *fullname;
285200956Sjilles	const char *path = pathval();
28617987Speter	struct stat statb;
28717987Speter
28817987Speter	/* don't try this for absolute or relative paths */
28917987Speter	if( strchr(basename, '/'))
29017987Speter		return basename;
29117987Speter
29217987Speter	while ((fullname = padvance(&path, basename)) != NULL) {
293222173Sjilles		if ((stat(fullname, &statb) == 0) && S_ISREG(statb.st_mode)) {
294222173Sjilles			/*
295222173Sjilles			 * Don't bother freeing here, since it will
296222173Sjilles			 * be freed by the caller.
297222173Sjilles			 */
298222173Sjilles			return fullname;
299222173Sjilles		}
30017987Speter		stunalloc(fullname);
30117987Speter	}
30217987Speter	return basename;
30317987Speter}
30417987Speter
30517987Speterint
30690111Simpdotcmd(int argc, char **argv)
30717987Speter{
308208630Sjilles	char *filename, *fullname;
309157414Sstefanf
310157414Sstefanf	if (argc < 2)
311157414Sstefanf		error("missing filename");
312157414Sstefanf
3131556Srgrimes	exitstatus = 0;
31417987Speter
315208630Sjilles	/*
316208630Sjilles	 * Because we have historically not supported any options,
317208630Sjilles	 * only treat "--" specially.
318208630Sjilles	 */
319208630Sjilles	filename = argc > 2 && strcmp(argv[1], "--") == 0 ? argv[2] : argv[1];
320208630Sjilles
321208630Sjilles	fullname = find_dot_file(filename);
322157414Sstefanf	setinputfile(fullname, 1);
323157414Sstefanf	commandname = fullname;
324157414Sstefanf	cmdloop(0);
325157414Sstefanf	popfile();
3261556Srgrimes	return exitstatus;
3271556Srgrimes}
3281556Srgrimes
3291556Srgrimes
33017987Speterint
33190111Simpexitcmd(int argc, char **argv)
33217987Speter{
3331556Srgrimes	if (stoppedjobs())
33417987Speter		return 0;
33520425Ssteve	if (argc > 1)
336217175Sjilles		exitshell(number(argv[1]));
33720425Ssteve	else
338217175Sjilles		exitshell_savedstatus();
3391556Srgrimes}
340