Home | History | Annotate | Line # | Download | only in sh
main.c revision 1.53
      1  1.53     lukem /*	$NetBSD: main.c,v 1.53 2009/01/18 00:30:54 lukem Exp $	*/
      2  1.17       cgd 
      3   1.1       cgd /*-
      4   1.9       jtc  * Copyright (c) 1991, 1993
      5   1.9       jtc  *	The Regents of the University of California.  All rights reserved.
      6   1.1       cgd  *
      7   1.1       cgd  * This code is derived from software contributed to Berkeley by
      8   1.1       cgd  * Kenneth Almquist.
      9   1.1       cgd  *
     10   1.1       cgd  * Redistribution and use in source and binary forms, with or without
     11   1.1       cgd  * modification, are permitted provided that the following conditions
     12   1.1       cgd  * are met:
     13   1.1       cgd  * 1. Redistributions of source code must retain the above copyright
     14   1.1       cgd  *    notice, this list of conditions and the following disclaimer.
     15   1.1       cgd  * 2. Redistributions in binary form must reproduce the above copyright
     16   1.1       cgd  *    notice, this list of conditions and the following disclaimer in the
     17   1.1       cgd  *    documentation and/or other materials provided with the distribution.
     18  1.47       agc  * 3. Neither the name of the University nor the names of its contributors
     19   1.1       cgd  *    may be used to endorse or promote products derived from this software
     20   1.1       cgd  *    without specific prior written permission.
     21   1.1       cgd  *
     22   1.1       cgd  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     23   1.1       cgd  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     24   1.1       cgd  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     25   1.1       cgd  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     26   1.1       cgd  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     27   1.1       cgd  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     28   1.1       cgd  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     29   1.1       cgd  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     30   1.1       cgd  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     31   1.1       cgd  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     32   1.1       cgd  * SUCH DAMAGE.
     33   1.1       cgd  */
     34   1.1       cgd 
     35  1.26  christos #include <sys/cdefs.h>
     36   1.1       cgd #ifndef lint
     37  1.51     lukem __COPYRIGHT("@(#) Copyright (c) 1991, 1993\
     38  1.51     lukem  The Regents of the University of California.  All rights reserved.");
     39   1.1       cgd #endif /* not lint */
     40   1.1       cgd 
     41   1.1       cgd #ifndef lint
     42  1.17       cgd #if 0
     43  1.21  christos static char sccsid[] = "@(#)main.c	8.7 (Berkeley) 7/19/95";
     44  1.17       cgd #else
     45  1.53     lukem __RCSID("$NetBSD: main.c,v 1.53 2009/01/18 00:30:54 lukem Exp $");
     46  1.17       cgd #endif
     47   1.1       cgd #endif /* not lint */
     48   1.1       cgd 
     49  1.36    simonb #include <errno.h>
     50  1.11       jtc #include <stdio.h>
     51   1.1       cgd #include <signal.h>
     52  1.11       jtc #include <sys/stat.h>
     53  1.12       jtc #include <unistd.h>
     54  1.46  christos #include <locale.h>
     55   1.1       cgd #include <fcntl.h>
     56  1.18  christos 
     57  1.18  christos 
     58   1.1       cgd #include "shell.h"
     59   1.1       cgd #include "main.h"
     60   1.1       cgd #include "mail.h"
     61   1.1       cgd #include "options.h"
     62   1.1       cgd #include "output.h"
     63   1.1       cgd #include "parser.h"
     64   1.1       cgd #include "nodes.h"
     65  1.20  christos #include "expand.h"
     66   1.1       cgd #include "eval.h"
     67   1.1       cgd #include "jobs.h"
     68   1.1       cgd #include "input.h"
     69   1.1       cgd #include "trap.h"
     70   1.1       cgd #include "var.h"
     71  1.18  christos #include "show.h"
     72   1.1       cgd #include "memalloc.h"
     73   1.1       cgd #include "error.h"
     74   1.1       cgd #include "init.h"
     75   1.1       cgd #include "mystring.h"
     76  1.11       jtc #include "exec.h"
     77  1.23  christos #include "cd.h"
     78   1.1       cgd 
     79   1.1       cgd #define PROFILE 0
     80   1.1       cgd 
     81   1.1       cgd int rootpid;
     82   1.1       cgd int rootshell;
     83   1.1       cgd #if PROFILE
     84   1.1       cgd short profile_buf[16384];
     85   1.1       cgd extern int etext();
     86   1.1       cgd #endif
     87   1.1       cgd 
     88  1.45  christos STATIC void read_profile(const char *);
     89  1.45  christos STATIC char *find_dot_file(char *);
     90  1.45  christos int main(int, char **);
     91   1.1       cgd 
     92   1.1       cgd /*
     93   1.1       cgd  * Main routine.  We initialize things, parse the arguments, execute
     94   1.1       cgd  * profiles if we're a login shell, and then call cmdloop to execute
     95   1.1       cgd  * commands.  The setjmp call sets up the location to jump to when an
     96   1.1       cgd  * exception occurs.  When an exception occurs the variable "state"
     97   1.1       cgd  * is used to figure out how far we had gotten.
     98   1.1       cgd  */
     99   1.1       cgd 
    100  1.15       cgd int
    101  1.45  christos main(int argc, char **argv)
    102  1.15       cgd {
    103   1.1       cgd 	struct jmploc jmploc;
    104   1.1       cgd 	struct stackmark smark;
    105   1.1       cgd 	volatile int state;
    106   1.1       cgd 	char *shinit;
    107  1.46  christos 
    108  1.46  christos 	setlocale(LC_ALL, "");
    109   1.1       cgd 
    110   1.1       cgd #if PROFILE
    111   1.1       cgd 	monitor(4, etext, profile_buf, sizeof profile_buf, 50);
    112   1.1       cgd #endif
    113   1.1       cgd 	state = 0;
    114   1.1       cgd 	if (setjmp(jmploc.loc)) {
    115   1.1       cgd 		/*
    116   1.1       cgd 		 * When a shell procedure is executed, we raise the
    117   1.1       cgd 		 * exception EXSHELLPROC to clean up before executing
    118   1.1       cgd 		 * the shell procedure.
    119   1.1       cgd 		 */
    120  1.24  christos 		switch (exception) {
    121  1.24  christos 		case EXSHELLPROC:
    122   1.1       cgd 			rootpid = getpid();
    123   1.1       cgd 			rootshell = 1;
    124   1.1       cgd 			minusc = NULL;
    125   1.1       cgd 			state = 3;
    126  1.24  christos 			break;
    127  1.24  christos 
    128  1.24  christos 		case EXEXEC:
    129  1.24  christos 			exitstatus = exerrno;
    130  1.24  christos 			break;
    131  1.24  christos 
    132  1.24  christos 		case EXERROR:
    133  1.24  christos 			exitstatus = 2;
    134  1.24  christos 			break;
    135  1.24  christos 
    136  1.24  christos 		default:
    137  1.24  christos 			break;
    138  1.24  christos 		}
    139  1.24  christos 
    140  1.24  christos 		if (exception != EXSHELLPROC) {
    141  1.45  christos 			if (state == 0 || iflag == 0 || ! rootshell)
    142  1.45  christos 				exitshell(exitstatus);
    143  1.24  christos 		}
    144   1.1       cgd 		reset();
    145   1.9       jtc 		if (exception == EXINT
    146   1.1       cgd #if ATTY
    147   1.9       jtc 		 && (! attyset() || equal(termval(), "emacs"))
    148   1.1       cgd #endif
    149   1.9       jtc 		 ) {
    150   1.1       cgd 			out2c('\n');
    151   1.1       cgd 			flushout(&errout);
    152   1.1       cgd 		}
    153   1.1       cgd 		popstackmark(&smark);
    154   1.1       cgd 		FORCEINTON;				/* enable interrupts */
    155   1.1       cgd 		if (state == 1)
    156   1.1       cgd 			goto state1;
    157   1.1       cgd 		else if (state == 2)
    158   1.1       cgd 			goto state2;
    159   1.9       jtc 		else if (state == 3)
    160   1.9       jtc 			goto state3;
    161   1.1       cgd 		else
    162   1.9       jtc 			goto state4;
    163   1.1       cgd 	}
    164   1.1       cgd 	handler = &jmploc;
    165   1.1       cgd #ifdef DEBUG
    166  1.48      jmmv #if DEBUG == 2
    167  1.48      jmmv 	debug = 1;
    168  1.48      jmmv #endif
    169   1.1       cgd 	opentrace();
    170   1.1       cgd 	trputs("Shell args:  ");  trargs(argv);
    171   1.1       cgd #endif
    172   1.1       cgd 	rootpid = getpid();
    173   1.1       cgd 	rootshell = 1;
    174   1.1       cgd 	init();
    175  1.50  christos 	initpwd();
    176   1.1       cgd 	setstackmark(&smark);
    177   1.1       cgd 	procargs(argc, argv);
    178   1.1       cgd 	if (argv[0] && argv[0][0] == '-') {
    179   1.1       cgd 		state = 1;
    180   1.1       cgd 		read_profile("/etc/profile");
    181   1.1       cgd state1:
    182   1.1       cgd 		state = 2;
    183   1.1       cgd 		read_profile(".profile");
    184  1.24  christos 	}
    185   1.1       cgd state2:
    186   1.1       cgd 	state = 3;
    187  1.14       jtc 	if (getuid() == geteuid() && getgid() == getegid()) {
    188  1.14       jtc 		if ((shinit = lookupvar("ENV")) != NULL && *shinit != '\0') {
    189  1.14       jtc 			state = 3;
    190  1.14       jtc 			read_profile(shinit);
    191  1.14       jtc 		}
    192   1.9       jtc 	}
    193   1.9       jtc state3:
    194   1.9       jtc 	state = 4;
    195  1.33  christos 	if (sflag == 0 || minusc) {
    196  1.32  christos 		static int sigs[] =  {
    197  1.32  christos 		    SIGINT, SIGQUIT, SIGHUP,
    198  1.31  christos #ifdef SIGTSTP
    199  1.32  christos 		    SIGTSTP,
    200  1.31  christos #endif
    201  1.32  christos 		    SIGPIPE
    202  1.31  christos 		};
    203  1.32  christos #define SIGSSIZE (sizeof(sigs)/sizeof(sigs[0]))
    204  1.53     lukem 		size_t i;
    205  1.31  christos 
    206  1.31  christos 		for (i = 0; i < SIGSSIZE; i++)
    207  1.42  christos 		    setsignal(sigs[i], 0);
    208  1.33  christos 	}
    209  1.33  christos 
    210  1.33  christos 	if (minusc)
    211  1.35  christos 		evalstring(minusc, 0);
    212  1.33  christos 
    213   1.1       cgd 	if (sflag || minusc == NULL) {
    214   1.9       jtc state4:	/* XXX ??? - why isn't this before the "if" statement */
    215   1.1       cgd 		cmdloop(1);
    216   1.1       cgd 	}
    217   1.1       cgd #if PROFILE
    218   1.1       cgd 	monitor(0);
    219   1.1       cgd #endif
    220   1.1       cgd 	exitshell(exitstatus);
    221  1.28   mycroft 	/* NOTREACHED */
    222   1.1       cgd }
    223   1.1       cgd 
    224   1.1       cgd 
    225   1.1       cgd /*
    226   1.1       cgd  * Read and execute commands.  "Top" is nonzero for the top level command
    227   1.1       cgd  * loop; it turns on prompting if the shell is interactive.
    228   1.1       cgd  */
    229   1.1       cgd 
    230   1.1       cgd void
    231  1.45  christos cmdloop(int top)
    232  1.15       cgd {
    233   1.1       cgd 	union node *n;
    234   1.1       cgd 	struct stackmark smark;
    235   1.1       cgd 	int inter;
    236   1.9       jtc 	int numeof = 0;
    237   1.1       cgd 
    238   1.1       cgd 	TRACE(("cmdloop(%d) called\n", top));
    239   1.1       cgd 	setstackmark(&smark);
    240   1.1       cgd 	for (;;) {
    241   1.1       cgd 		if (pendingsigs)
    242   1.1       cgd 			dotrap();
    243   1.1       cgd 		inter = 0;
    244  1.49  christos 		if (iflag == 1 && top) {
    245  1.45  christos 			inter = 1;
    246  1.45  christos 			showjobs(out2, SHOW_CHANGED);
    247   1.1       cgd 			chkmail(0);
    248  1.45  christos 			flushout(&errout);
    249   1.1       cgd 		}
    250   1.1       cgd 		n = parsecmd(inter);
    251   1.9       jtc 		/* showtree(n); DEBUG */
    252   1.1       cgd 		if (n == NEOF) {
    253   1.9       jtc 			if (!top || numeof >= 50)
    254   1.1       cgd 				break;
    255   1.9       jtc 			if (!stoppedjobs()) {
    256   1.9       jtc 				if (!Iflag)
    257   1.9       jtc 					break;
    258   1.9       jtc 				out2str("\nUse \"exit\" to leave shell.\n");
    259   1.9       jtc 			}
    260   1.1       cgd 			numeof++;
    261   1.1       cgd 		} else if (n != NULL && nflag == 0) {
    262   1.9       jtc 			job_warning = (job_warning == 2) ? 1 : 0;
    263   1.9       jtc 			numeof = 0;
    264  1.44  christos 			evaltree(n, 0);
    265   1.1       cgd 		}
    266   1.1       cgd 		popstackmark(&smark);
    267  1.39  christos 		setstackmark(&smark);
    268  1.22  christos 		if (evalskip == SKIPFILE) {
    269  1.22  christos 			evalskip = 0;
    270  1.22  christos 			break;
    271  1.22  christos 		}
    272   1.1       cgd 	}
    273  1.39  christos 	popstackmark(&smark);
    274   1.1       cgd }
    275   1.1       cgd 
    276   1.1       cgd 
    277   1.1       cgd 
    278   1.1       cgd /*
    279   1.1       cgd  * Read /etc/profile or .profile.  Return on error.
    280   1.1       cgd  */
    281   1.1       cgd 
    282   1.1       cgd STATIC void
    283  1.45  christos read_profile(const char *name)
    284  1.30       cjs {
    285   1.1       cgd 	int fd;
    286  1.30       cjs 	int xflag_set = 0;
    287  1.30       cjs 	int vflag_set = 0;
    288   1.1       cgd 
    289   1.1       cgd 	INTOFF;
    290   1.1       cgd 	if ((fd = open(name, O_RDONLY)) >= 0)
    291   1.1       cgd 		setinputfd(fd, 1);
    292   1.1       cgd 	INTON;
    293   1.1       cgd 	if (fd < 0)
    294   1.1       cgd 		return;
    295  1.30       cjs 	/* -q turns off -x and -v just when executing init files */
    296  1.30       cjs 	if (qflag)  {
    297  1.30       cjs 	    if (xflag)
    298  1.30       cjs 		    xflag = 0, xflag_set = 1;
    299  1.30       cjs 	    if (vflag)
    300  1.30       cjs 		    vflag = 0, vflag_set = 1;
    301  1.30       cjs 	}
    302   1.1       cgd 	cmdloop(0);
    303  1.30       cjs 	if (qflag)  {
    304  1.30       cjs 	    if (xflag_set)
    305  1.30       cjs 		    xflag = 1;
    306  1.30       cjs 	    if (vflag_set)
    307  1.30       cjs 		    vflag = 1;
    308  1.30       cjs 	}
    309   1.1       cgd 	popfile();
    310   1.1       cgd }
    311   1.1       cgd 
    312   1.1       cgd 
    313   1.1       cgd 
    314   1.1       cgd /*
    315   1.1       cgd  * Read a file containing shell functions.
    316   1.1       cgd  */
    317   1.1       cgd 
    318   1.1       cgd void
    319  1.45  christos readcmdfile(char *name)
    320  1.16       cgd {
    321   1.1       cgd 	int fd;
    322   1.1       cgd 
    323   1.1       cgd 	INTOFF;
    324   1.1       cgd 	if ((fd = open(name, O_RDONLY)) >= 0)
    325   1.1       cgd 		setinputfd(fd, 1);
    326   1.1       cgd 	else
    327   1.1       cgd 		error("Can't open %s", name);
    328   1.1       cgd 	INTON;
    329   1.1       cgd 	cmdloop(0);
    330   1.1       cgd 	popfile();
    331   1.1       cgd }
    332   1.1       cgd 
    333   1.1       cgd 
    334   1.1       cgd 
    335   1.1       cgd /*
    336  1.41       wiz  * Take commands from a file.  To be compatible we should do a path
    337  1.10       jtc  * search for the file, which is necessary to find sub-commands.
    338   1.1       cgd  */
    339   1.1       cgd 
    340  1.10       jtc 
    341  1.18  christos STATIC char *
    342  1.45  christos find_dot_file(char *basename)
    343  1.18  christos {
    344  1.10       jtc 	char *fullname;
    345  1.34  christos 	const char *path = pathval();
    346  1.10       jtc 	struct stat statb;
    347  1.10       jtc 
    348  1.10       jtc 	/* don't try this for absolute or relative paths */
    349  1.29     itohy 	if (strchr(basename, '/'))
    350  1.10       jtc 		return basename;
    351  1.10       jtc 
    352  1.10       jtc 	while ((fullname = padvance(&path, basename)) != NULL) {
    353  1.29     itohy 		if ((stat(fullname, &statb) == 0) && S_ISREG(statb.st_mode)) {
    354  1.29     itohy 			/*
    355  1.29     itohy 			 * Don't bother freeing here, since it will
    356  1.29     itohy 			 * be freed by the caller.
    357  1.29     itohy 			 */
    358  1.29     itohy 			return fullname;
    359  1.29     itohy 		}
    360  1.10       jtc 		stunalloc(fullname);
    361  1.10       jtc 	}
    362  1.29     itohy 
    363  1.29     itohy 	/* not found in the PATH */
    364  1.29     itohy 	error("%s: not found", basename);
    365  1.29     itohy 	/* NOTREACHED */
    366  1.10       jtc }
    367  1.10       jtc 
    368  1.15       cgd int
    369  1.45  christos dotcmd(int argc, char **argv)
    370  1.15       cgd {
    371   1.1       cgd 	exitstatus = 0;
    372  1.20  christos 
    373   1.1       cgd 	if (argc >= 2) {		/* That's what SVR2 does */
    374  1.29     itohy 		char *fullname;
    375  1.29     itohy 		struct stackmark smark;
    376  1.20  christos 
    377  1.29     itohy 		setstackmark(&smark);
    378  1.29     itohy 		fullname = find_dot_file(argv[1]);
    379  1.10       jtc 		setinputfile(fullname, 1);
    380  1.10       jtc 		commandname = fullname;
    381   1.1       cgd 		cmdloop(0);
    382   1.1       cgd 		popfile();
    383  1.29     itohy 		popstackmark(&smark);
    384   1.1       cgd 	}
    385   1.1       cgd 	return exitstatus;
    386   1.1       cgd }
    387   1.1       cgd 
    388   1.1       cgd 
    389  1.15       cgd int
    390  1.45  christos exitcmd(int argc, char **argv)
    391  1.15       cgd {
    392   1.9       jtc 	if (stoppedjobs())
    393  1.18  christos 		return 0;
    394   1.1       cgd 	if (argc > 1)
    395   1.1       cgd 		exitstatus = number(argv[1]);
    396   1.1       cgd 	exitshell(exitstatus);
    397  1.28   mycroft 	/* NOTREACHED */
    398   1.1       cgd }
    399