xref: /csrg-svn/bin/sh/main.c (revision 69549)
147125Sbostic /*-
260698Sbostic  * Copyright (c) 1991, 1993
360698Sbostic  *	The Regents of the University of California.  All rights reserved.
447125Sbostic  *
547125Sbostic  * This code is derived from software contributed to Berkeley by
647125Sbostic  * Kenneth Almquist.
747125Sbostic  *
847125Sbostic  * %sccs.include.redist.c%
947125Sbostic  */
1047125Sbostic 
1147125Sbostic #ifndef lint
1260698Sbostic static char copyright[] =
1360698Sbostic "@(#) Copyright (c) 1991, 1993\n\
1460698Sbostic 	The Regents of the University of California.  All rights reserved.\n";
1547125Sbostic #endif /* not lint */
1647125Sbostic 
1747125Sbostic #ifndef lint
18*69549Schristos static char sccsid[] = "@(#)main.c	8.5 (Berkeley) 05/19/95";
1947125Sbostic #endif /* not lint */
2047125Sbostic 
2168926Sbostic #include <stdio.h>
2247125Sbostic #include <signal.h>
2369272Schristos #include <sys/stat.h>
2469272Schristos #include <unistd.h>
2547125Sbostic #include <fcntl.h>
2669272Schristos 
2769272Schristos 
2847125Sbostic #include "shell.h"
2947125Sbostic #include "main.h"
3047125Sbostic #include "mail.h"
3147125Sbostic #include "options.h"
3247125Sbostic #include "output.h"
3347125Sbostic #include "parser.h"
3447125Sbostic #include "nodes.h"
3547125Sbostic #include "eval.h"
3647125Sbostic #include "jobs.h"
3747125Sbostic #include "input.h"
3847125Sbostic #include "trap.h"
3947125Sbostic #include "var.h"
4069272Schristos #include "show.h"
4147125Sbostic #include "memalloc.h"
4247125Sbostic #include "error.h"
4347125Sbostic #include "init.h"
4447125Sbostic #include "mystring.h"
4568926Sbostic #include "exec.h"
4647125Sbostic 
4747125Sbostic #define PROFILE 0
4847125Sbostic 
4947125Sbostic int rootpid;
5047125Sbostic int rootshell;
5147125Sbostic STATIC union node *curcmd;
5247125Sbostic STATIC union node *prevcmd;
5347125Sbostic extern int errno;
5447125Sbostic #if PROFILE
5547125Sbostic short profile_buf[16384];
5647125Sbostic extern int etext();
5747125Sbostic #endif
5847125Sbostic 
5969272Schristos STATIC void read_profile __P((char *));
6069272Schristos STATIC char *find_dot_file __P((char *));
6147125Sbostic 
6247125Sbostic /*
6347125Sbostic  * Main routine.  We initialize things, parse the arguments, execute
6447125Sbostic  * profiles if we're a login shell, and then call cmdloop to execute
6547125Sbostic  * commands.  The setjmp call sets up the location to jump to when an
6647125Sbostic  * exception occurs.  When an exception occurs the variable "state"
6747125Sbostic  * is used to figure out how far we had gotten.
6847125Sbostic  */
6947125Sbostic 
7069272Schristos int
7169272Schristos main(argc, argv)
7269272Schristos 	int argc;
7369272Schristos 	char **argv;
7469272Schristos {
7547125Sbostic 	struct jmploc jmploc;
7647125Sbostic 	struct stackmark smark;
7747125Sbostic 	volatile int state;
7847125Sbostic 	char *shinit;
7947125Sbostic 
8047125Sbostic #if PROFILE
8147125Sbostic 	monitor(4, etext, profile_buf, sizeof profile_buf, 50);
8247125Sbostic #endif
8347125Sbostic 	state = 0;
8447125Sbostic 	if (setjmp(jmploc.loc)) {
8547125Sbostic 		/*
8647125Sbostic 		 * When a shell procedure is executed, we raise the
8747125Sbostic 		 * exception EXSHELLPROC to clean up before executing
8847125Sbostic 		 * the shell procedure.
8947125Sbostic 		 */
90*69549Schristos 		if (exception == EXERROR)
91*69549Schristos 			exitstatus = 2;
9247125Sbostic 		if (exception == EXSHELLPROC) {
9347125Sbostic 			rootpid = getpid();
9447125Sbostic 			rootshell = 1;
9547125Sbostic 			minusc = NULL;
9647125Sbostic 			state = 3;
9747125Sbostic 		} else if (state == 0 || iflag == 0 || ! rootshell)
9847125Sbostic 			exitshell(2);
9947125Sbostic 		reset();
10056563Smarc 		if (exception == EXINT
10147125Sbostic #if ATTY
10256563Smarc 		 && (! attyset() || equal(termval(), "emacs"))
10347125Sbostic #endif
10456563Smarc 		 ) {
10547125Sbostic 			out2c('\n');
10647125Sbostic 			flushout(&errout);
10747125Sbostic 		}
10847125Sbostic 		popstackmark(&smark);
10947125Sbostic 		FORCEINTON;				/* enable interrupts */
11047125Sbostic 		if (state == 1)
11147125Sbostic 			goto state1;
11247125Sbostic 		else if (state == 2)
11347125Sbostic 			goto state2;
11454316Smarc 		else if (state == 3)
11554316Smarc 			goto state3;
11647125Sbostic 		else
11754316Smarc 			goto state4;
11847125Sbostic 	}
11947125Sbostic 	handler = &jmploc;
12047125Sbostic #ifdef DEBUG
12147125Sbostic 	opentrace();
12247125Sbostic 	trputs("Shell args:  ");  trargs(argv);
12347125Sbostic #endif
12447125Sbostic 	rootpid = getpid();
12547125Sbostic 	rootshell = 1;
12647125Sbostic 	init();
12747125Sbostic 	setstackmark(&smark);
12847125Sbostic 	procargs(argc, argv);
12947125Sbostic 	if (argv[0] && argv[0][0] == '-') {
13047125Sbostic 		state = 1;
13147125Sbostic 		read_profile("/etc/profile");
13247125Sbostic state1:
13347125Sbostic 		state = 2;
13447125Sbostic 		read_profile(".profile");
13554316Smarc 	}
13647125Sbostic state2:
13747125Sbostic 	state = 3;
13868931Sbostic 	if (getuid() == geteuid() && getgid() == getegid()) {
13968931Sbostic 		if ((shinit = lookupvar("ENV")) != NULL && *shinit != '\0') {
14068931Sbostic 			state = 3;
14168931Sbostic 			read_profile(shinit);
14268931Sbostic 		}
14354316Smarc 	}
14454316Smarc state3:
14554316Smarc 	state = 4;
14647125Sbostic 	if (minusc) {
14747125Sbostic 		evalstring(minusc);
14847125Sbostic 	}
14947125Sbostic 	if (sflag || minusc == NULL) {
15054316Smarc state4:	/* XXX ??? - why isn't this before the "if" statement */
15147125Sbostic 		cmdloop(1);
15247125Sbostic 	}
15347125Sbostic #if PROFILE
15447125Sbostic 	monitor(0);
15547125Sbostic #endif
15647125Sbostic 	exitshell(exitstatus);
15769272Schristos 	/*NOTREACHED*/
15869272Schristos 	return 0;
15947125Sbostic }
16047125Sbostic 
16147125Sbostic 
16247125Sbostic /*
16347125Sbostic  * Read and execute commands.  "Top" is nonzero for the top level command
16447125Sbostic  * loop; it turns on prompting if the shell is interactive.
16547125Sbostic  */
16647125Sbostic 
16747125Sbostic void
16869272Schristos cmdloop(top)
16969272Schristos 	int top;
17069272Schristos {
17147125Sbostic 	union node *n;
17247125Sbostic 	struct stackmark smark;
17347125Sbostic 	int inter;
17455278Smarc 	int numeof = 0;
17547125Sbostic 
17647125Sbostic 	TRACE(("cmdloop(%d) called\n", top));
17747125Sbostic 	setstackmark(&smark);
17847125Sbostic 	for (;;) {
17947125Sbostic 		if (pendingsigs)
18047125Sbostic 			dotrap();
18147125Sbostic 		inter = 0;
18247125Sbostic 		if (iflag && top) {
18347125Sbostic 			inter++;
18447125Sbostic 			showjobs(1);
18547125Sbostic 			chkmail(0);
18647125Sbostic 			flushout(&output);
18747125Sbostic 		}
18847125Sbostic 		n = parsecmd(inter);
18955277Smarc 		/* showtree(n); DEBUG */
19047125Sbostic 		if (n == NEOF) {
19155277Smarc 			if (!top || numeof >= 50)
19247125Sbostic 				break;
19355277Smarc 			if (!stoppedjobs()) {
19455277Smarc 				if (!Iflag)
19555277Smarc 					break;
19655277Smarc 				out2str("\nUse \"exit\" to leave shell.\n");
19755277Smarc 			}
19847125Sbostic 			numeof++;
19947125Sbostic 		} else if (n != NULL && nflag == 0) {
20055277Smarc 			job_warning = (job_warning == 2) ? 1 : 0;
20155278Smarc 			numeof = 0;
20247125Sbostic 			evaltree(n, 0);
20347125Sbostic 		}
20447125Sbostic 		popstackmark(&smark);
20547125Sbostic 	}
20647125Sbostic 	popstackmark(&smark);		/* unnecessary */
20747125Sbostic }
20847125Sbostic 
20947125Sbostic 
21047125Sbostic 
21147125Sbostic /*
21247125Sbostic  * Read /etc/profile or .profile.  Return on error.
21347125Sbostic  */
21447125Sbostic 
21547125Sbostic STATIC void
21647125Sbostic read_profile(name)
21747125Sbostic 	char *name;
21847125Sbostic 	{
21947125Sbostic 	int fd;
22047125Sbostic 
22147125Sbostic 	INTOFF;
22247125Sbostic 	if ((fd = open(name, O_RDONLY)) >= 0)
22347125Sbostic 		setinputfd(fd, 1);
22447125Sbostic 	INTON;
22547125Sbostic 	if (fd < 0)
22647125Sbostic 		return;
22747125Sbostic 	cmdloop(0);
22847125Sbostic 	popfile();
22947125Sbostic }
23047125Sbostic 
23147125Sbostic 
23247125Sbostic 
23347125Sbostic /*
23447125Sbostic  * Read a file containing shell functions.
23547125Sbostic  */
23647125Sbostic 
23747125Sbostic void
23847125Sbostic readcmdfile(name)
23947125Sbostic 	char *name;
24069272Schristos {
24147125Sbostic 	int fd;
24247125Sbostic 
24347125Sbostic 	INTOFF;
24447125Sbostic 	if ((fd = open(name, O_RDONLY)) >= 0)
24547125Sbostic 		setinputfd(fd, 1);
24647125Sbostic 	else
24747125Sbostic 		error("Can't open %s", name);
24847125Sbostic 	INTON;
24947125Sbostic 	cmdloop(0);
25047125Sbostic 	popfile();
25147125Sbostic }
25247125Sbostic 
25347125Sbostic 
25447125Sbostic 
25547125Sbostic /*
25647125Sbostic  * Take commands from a file.  To be compatable we should do a path
25768926Sbostic  * search for the file, which is necessary to find sub-commands.
25847125Sbostic  */
25947125Sbostic 
26068926Sbostic 
26169272Schristos STATIC char *
26269272Schristos find_dot_file(basename)
26369272Schristos 	char *basename;
26469272Schristos {
26568926Sbostic 	static char localname[FILENAME_MAX+1];
26668926Sbostic 	char *fullname;
26768926Sbostic 	char *path = pathval();
26868926Sbostic 	struct stat statb;
26968926Sbostic 
27068926Sbostic 	/* don't try this for absolute or relative paths */
27168926Sbostic 	if( strchr(basename, '/'))
27268926Sbostic 		return basename;
27368926Sbostic 
27468926Sbostic 	while ((fullname = padvance(&path, basename)) != NULL) {
27568926Sbostic 		strcpy(localname, fullname);
27668926Sbostic 		stunalloc(fullname);
27768926Sbostic 		if ((stat(fullname, &statb) == 0) && S_ISREG(statb.st_mode))
27868926Sbostic 			return localname;
27968926Sbostic 	}
28068926Sbostic 	return basename;
28168926Sbostic }
28268926Sbostic 
28369272Schristos int
28469272Schristos dotcmd(argc, argv)
28569272Schristos 	int argc;
28669272Schristos 	char **argv;
28769272Schristos {
28847125Sbostic 	exitstatus = 0;
28947125Sbostic 	if (argc >= 2) {		/* That's what SVR2 does */
29068926Sbostic 		char *fullname = find_dot_file(argv[1]);
29168926Sbostic 		setinputfile(fullname, 1);
29268926Sbostic 		commandname = fullname;
29347125Sbostic 		cmdloop(0);
29447125Sbostic 		popfile();
29547125Sbostic 	}
29647125Sbostic 	return exitstatus;
29747125Sbostic }
29847125Sbostic 
29947125Sbostic 
30069272Schristos int
30169272Schristos exitcmd(argc, argv)
30269272Schristos 	int argc;
30369272Schristos 	char **argv;
30469272Schristos {
30555277Smarc 	if (stoppedjobs())
30669272Schristos 		return 0;
30747125Sbostic 	if (argc > 1)
30847125Sbostic 		exitstatus = number(argv[1]);
30947125Sbostic 	exitshell(exitstatus);
31069272Schristos 	/*NOTREACHED*/
31169272Schristos 	return 0;
31247125Sbostic }
31347125Sbostic 
31447125Sbostic 
31547125Sbostic #ifdef notdef
31647125Sbostic /*
31747125Sbostic  * Should never be called.
31847125Sbostic  */
31947125Sbostic 
32047125Sbostic void
32147125Sbostic exit(exitstatus) {
32247125Sbostic 	_exit(exitstatus);
32347125Sbostic }
32447125Sbostic #endif
325