main.c revision 206759
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 206759 2010-04-17 14:35:46Z 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;
791556Srgrimes
80201053SjillesSTATIC void read_profile(const char *);
8190111SimpSTATIC char *find_dot_file(char *);
821556Srgrimes
831556Srgrimes/*
841556Srgrimes * Main routine.  We initialize things, parse the arguments, execute
851556Srgrimes * profiles if we're a login shell, and then call cmdloop to execute
861556Srgrimes * commands.  The setjmp call sets up the location to jump to when an
871556Srgrimes * exception occurs.  When an exception occurs the variable "state"
881556Srgrimes * is used to figure out how far we had gotten.
891556Srgrimes */
901556Srgrimes
9117987Speterint
9290111Simpmain(int argc, char *argv[])
9317987Speter{
941556Srgrimes	struct stackmark smark;
951556Srgrimes	volatile int state;
961556Srgrimes	char *shinit;
971556Srgrimes
9817525Sache	(void) setlocale(LC_ALL, "");
991556Srgrimes	state = 0;
100200998Sjilles	if (setjmp(main_handler.loc)) {
1011556Srgrimes		/*
1021556Srgrimes		 * When a shell procedure is executed, we raise the
1031556Srgrimes		 * exception EXSHELLPROC to clean up before executing
1041556Srgrimes		 * the shell procedure.
1051556Srgrimes		 */
10620425Ssteve		switch (exception) {
10720425Ssteve		case EXSHELLPROC:
1081556Srgrimes			rootpid = getpid();
1091556Srgrimes			rootshell = 1;
1101556Srgrimes			minusc = NULL;
1111556Srgrimes			state = 3;
11220425Ssteve			break;
11320425Ssteve
11420425Ssteve		case EXEXEC:
11520425Ssteve			exitstatus = exerrno;
11620425Ssteve			break;
11720425Ssteve
11820425Ssteve		case EXERROR:
11920425Ssteve			exitstatus = 2;
12020425Ssteve			break;
12120425Ssteve
12220425Ssteve		default:
12320425Ssteve			break;
12420425Ssteve		}
12520425Ssteve
12620425Ssteve		if (exception != EXSHELLPROC) {
12720425Ssteve		    if (state == 0 || iflag == 0 || ! rootshell)
12820425Ssteve			    exitshell(exitstatus);
12920425Ssteve		}
1301556Srgrimes		reset();
131104255Stjr		if (exception == EXINT) {
1321556Srgrimes			out2c('\n');
1331556Srgrimes			flushout(&errout);
1341556Srgrimes		}
1351556Srgrimes		popstackmark(&smark);
1361556Srgrimes		FORCEINTON;				/* enable interrupts */
1371556Srgrimes		if (state == 1)
1381556Srgrimes			goto state1;
1391556Srgrimes		else if (state == 2)
1401556Srgrimes			goto state2;
1411556Srgrimes		else if (state == 3)
1421556Srgrimes			goto state3;
1431556Srgrimes		else
1441556Srgrimes			goto state4;
1451556Srgrimes	}
146200998Sjilles	handler = &main_handler;
1471556Srgrimes#ifdef DEBUG
1481556Srgrimes	opentrace();
1491556Srgrimes	trputs("Shell args:  ");  trargs(argv);
1501556Srgrimes#endif
1511556Srgrimes	rootpid = getpid();
1521556Srgrimes	rootshell = 1;
1531556Srgrimes	init();
1541556Srgrimes	setstackmark(&smark);
1551556Srgrimes	procargs(argc, argv);
156206759Sjilles	pwd_init(iflag);
157203576Sjilles	if (iflag)
158203576Sjilles		chkmail(1);
1591556Srgrimes	if (argv[0] && argv[0][0] == '-') {
1601556Srgrimes		state = 1;
1611556Srgrimes		read_profile("/etc/profile");
1621556Srgrimesstate1:
1631556Srgrimes		state = 2;
16419240Ssteve		if (privileged == 0)
16519240Ssteve			read_profile(".profile");
16619240Ssteve		else
16719240Ssteve			read_profile("/etc/suid_profile");
1688855Srgrimes	}
1691556Srgrimesstate2:
1701556Srgrimes	state = 3;
17125471Ssteve	if (!privileged && iflag) {
17217987Speter		if ((shinit = lookupvar("ENV")) != NULL && *shinit != '\0') {
17317987Speter			state = 3;
17417987Speter			read_profile(shinit);
17517987Speter		}
1761556Srgrimes	}
1771556Srgrimesstate3:
1781556Srgrimes	state = 4;
1791556Srgrimes	if (minusc) {
180194128Sjilles		evalstring(minusc, sflag ? 0 : EV_EXIT);
1811556Srgrimes	}
1821556Srgrimes	if (sflag || minusc == NULL) {
1831556Srgrimesstate4:	/* XXX ??? - why isn't this before the "if" statement */
1841556Srgrimes		cmdloop(1);
1851556Srgrimes	}
1861556Srgrimes	exitshell(exitstatus);
18717987Speter	/*NOTREACHED*/
18817987Speter	return 0;
1891556Srgrimes}
1901556Srgrimes
1911556Srgrimes
1921556Srgrimes/*
1931556Srgrimes * Read and execute commands.  "Top" is nonzero for the top level command
1941556Srgrimes * loop; it turns on prompting if the shell is interactive.
1951556Srgrimes */
1961556Srgrimes
1971556Srgrimesvoid
19890111Simpcmdloop(int top)
19917987Speter{
2001556Srgrimes	union node *n;
2011556Srgrimes	struct stackmark smark;
2021556Srgrimes	int inter;
2031556Srgrimes	int numeof = 0;
2041556Srgrimes
2051556Srgrimes	TRACE(("cmdloop(%d) called\n", top));
2061556Srgrimes	setstackmark(&smark);
2071556Srgrimes	for (;;) {
2081556Srgrimes		if (pendingsigs)
2091556Srgrimes			dotrap();
2101556Srgrimes		inter = 0;
2111556Srgrimes		if (iflag && top) {
2121556Srgrimes			inter++;
213163085Sstefanf			showjobs(1, SHOWJOBS_DEFAULT);
2141556Srgrimes			chkmail(0);
2151556Srgrimes			flushout(&output);
2161556Srgrimes		}
2171556Srgrimes		n = parsecmd(inter);
2181556Srgrimes		/* showtree(n); DEBUG */
2191556Srgrimes		if (n == NEOF) {
2201556Srgrimes			if (!top || numeof >= 50)
2211556Srgrimes				break;
2221556Srgrimes			if (!stoppedjobs()) {
2231556Srgrimes				if (!Iflag)
2241556Srgrimes					break;
225199629Sjilles				out2fmt_flush("\nUse \"exit\" to leave shell.\n");
2261556Srgrimes			}
2271556Srgrimes			numeof++;
2281556Srgrimes		} else if (n != NULL && nflag == 0) {
2291556Srgrimes			job_warning = (job_warning == 2) ? 1 : 0;
2301556Srgrimes			numeof = 0;
2311556Srgrimes			evaltree(n, 0);
2321556Srgrimes		}
2331556Srgrimes		popstackmark(&smark);
23464702Scracauer		setstackmark(&smark);
23520425Ssteve		if (evalskip == SKIPFILE) {
23620425Ssteve			evalskip = 0;
23720425Ssteve			break;
23820425Ssteve		}
2391556Srgrimes	}
24064702Scracauer	popstackmark(&smark);
2411556Srgrimes}
2421556Srgrimes
2431556Srgrimes
2441556Srgrimes
2451556Srgrimes/*
2461556Srgrimes * Read /etc/profile or .profile.  Return on error.
2471556Srgrimes */
2481556Srgrimes
2491556SrgrimesSTATIC void
250201053Sjillesread_profile(const char *name)
25190111Simp{
2521556Srgrimes	int fd;
2531556Srgrimes
2541556Srgrimes	INTOFF;
2551556Srgrimes	if ((fd = open(name, O_RDONLY)) >= 0)
2561556Srgrimes		setinputfd(fd, 1);
2571556Srgrimes	INTON;
2581556Srgrimes	if (fd < 0)
2591556Srgrimes		return;
2601556Srgrimes	cmdloop(0);
2611556Srgrimes	popfile();
2621556Srgrimes}
2631556Srgrimes
2641556Srgrimes
2651556Srgrimes
2661556Srgrimes/*
2671556Srgrimes * Read a file containing shell functions.
2681556Srgrimes */
2691556Srgrimes
2701556Srgrimesvoid
271200956Sjillesreadcmdfile(const char *name)
27217987Speter{
2731556Srgrimes	int fd;
2741556Srgrimes
2751556Srgrimes	INTOFF;
2761556Srgrimes	if ((fd = open(name, O_RDONLY)) >= 0)
2771556Srgrimes		setinputfd(fd, 1);
2781556Srgrimes	else
27953891Scracauer		error("Can't open %s: %s", name, strerror(errno));
2801556Srgrimes	INTON;
2811556Srgrimes	cmdloop(0);
2821556Srgrimes	popfile();
2831556Srgrimes}
2841556Srgrimes
2851556Srgrimes
2861556Srgrimes
2871556Srgrimes/*
28846684Skris * Take commands from a file.  To be compatible we should do a path
28917987Speter * search for the file, which is necessary to find sub-commands.
2901556Srgrimes */
2911556Srgrimes
29217987Speter
29317987SpeterSTATIC char *
29490111Simpfind_dot_file(char *basename)
29517987Speter{
29617987Speter	static char localname[FILENAME_MAX+1];
29717987Speter	char *fullname;
298200956Sjilles	const char *path = pathval();
29917987Speter	struct stat statb;
30017987Speter
30117987Speter	/* don't try this for absolute or relative paths */
30217987Speter	if( strchr(basename, '/'))
30317987Speter		return basename;
30417987Speter
30517987Speter	while ((fullname = padvance(&path, basename)) != NULL) {
30617987Speter		strcpy(localname, fullname);
30717987Speter		stunalloc(fullname);
30817987Speter		if ((stat(fullname, &statb) == 0) && S_ISREG(statb.st_mode))
30917987Speter			return localname;
31017987Speter	}
31117987Speter	return basename;
31217987Speter}
31317987Speter
31417987Speterint
31590111Simpdotcmd(int argc, char **argv)
31617987Speter{
317157414Sstefanf	char *fullname;
318157414Sstefanf
319157414Sstefanf	if (argc < 2)
320157414Sstefanf		error("missing filename");
321157414Sstefanf
3221556Srgrimes	exitstatus = 0;
32317987Speter
324157414Sstefanf	fullname = find_dot_file(argv[1]);
325157414Sstefanf	setinputfile(fullname, 1);
326157414Sstefanf	commandname = fullname;
327157414Sstefanf	cmdloop(0);
328157414Sstefanf	popfile();
3291556Srgrimes	return exitstatus;
3301556Srgrimes}
3311556Srgrimes
3321556Srgrimes
33317987Speterint
33490111Simpexitcmd(int argc, char **argv)
33517987Speter{
3361556Srgrimes	if (stoppedjobs())
33717987Speter		return 0;
33820425Ssteve	if (argc > 1)
33920425Ssteve		exitstatus = number(argv[1]);
34020425Ssteve	else
34120425Ssteve		exitstatus = oexitstatus;
34218254Sbde	exitshell(exitstatus);
34317987Speter	/*NOTREACHED*/
34417987Speter	return 0;
3451556Srgrimes}
346