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