Home | History | Annotate | Line # | Download | only in sh
main.c revision 1.17
      1  1.17  cgd /*	$NetBSD: main.c,v 1.17 1995/03/21 09:09:26 cgd 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.1  cgd #ifndef lint
     40   1.9  jtc static char copyright[] =
     41   1.9  jtc "@(#) Copyright (c) 1991, 1993\n\
     42   1.9  jtc 	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.17  cgd static char sccsid[] = "@(#)main.c	8.1 (Berkeley) 5/31/93";
     48  1.17  cgd #else
     49  1.17  cgd static char rcsid[] = "$NetBSD: main.c,v 1.17 1995/03/21 09:09:26 cgd 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.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.1  cgd #include "eval.h"
     66   1.1  cgd #include "jobs.h"
     67   1.1  cgd #include "input.h"
     68   1.1  cgd #include "trap.h"
     69   1.1  cgd #include "var.h"
     70   1.1  cgd #include "memalloc.h"
     71   1.1  cgd #include "error.h"
     72   1.1  cgd #include "init.h"
     73   1.1  cgd #include "mystring.h"
     74  1.11  jtc #include "exec.h"
     75  1.15  cgd #include "extern.h"
     76   1.1  cgd 
     77   1.1  cgd #define PROFILE 0
     78   1.1  cgd 
     79   1.1  cgd int rootpid;
     80   1.1  cgd int rootshell;
     81   1.1  cgd STATIC union node *curcmd;
     82   1.1  cgd STATIC union node *prevcmd;
     83   1.1  cgd extern int errno;
     84   1.1  cgd #if PROFILE
     85   1.1  cgd short profile_buf[16384];
     86   1.1  cgd extern int etext();
     87   1.1  cgd #endif
     88   1.1  cgd 
     89   1.1  cgd #ifdef __STDC__
     90   1.1  cgd STATIC void read_profile(char *);
     91   1.1  cgd char *getenv(char *);
     92   1.1  cgd #else
     93   1.1  cgd STATIC void read_profile();
     94   1.1  cgd char *getenv();
     95   1.1  cgd #endif
     96   1.1  cgd 
     97   1.1  cgd 
     98   1.1  cgd /*
     99   1.1  cgd  * Main routine.  We initialize things, parse the arguments, execute
    100   1.1  cgd  * profiles if we're a login shell, and then call cmdloop to execute
    101   1.1  cgd  * commands.  The setjmp call sets up the location to jump to when an
    102   1.1  cgd  * exception occurs.  When an exception occurs the variable "state"
    103   1.1  cgd  * is used to figure out how far we had gotten.
    104   1.1  cgd  */
    105   1.1  cgd 
    106  1.15  cgd int
    107  1.15  cgd main(argc, argv)
    108  1.15  cgd 	int argc;
    109  1.15  cgd 	char **argv;
    110  1.15  cgd {
    111   1.1  cgd 	struct jmploc jmploc;
    112   1.1  cgd 	struct stackmark smark;
    113   1.1  cgd 	volatile int state;
    114   1.1  cgd 	char *shinit;
    115   1.1  cgd 
    116   1.1  cgd #if PROFILE
    117   1.1  cgd 	monitor(4, etext, profile_buf, sizeof profile_buf, 50);
    118   1.1  cgd #endif
    119   1.1  cgd 	state = 0;
    120   1.1  cgd 	if (setjmp(jmploc.loc)) {
    121   1.1  cgd 		/*
    122   1.1  cgd 		 * When a shell procedure is executed, we raise the
    123   1.1  cgd 		 * exception EXSHELLPROC to clean up before executing
    124   1.1  cgd 		 * the shell procedure.
    125   1.1  cgd 		 */
    126   1.1  cgd 		if (exception == 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.1  cgd 		} else if (state == 0 || iflag == 0 || ! rootshell)
    132   1.1  cgd 			exitshell(2);
    133   1.1  cgd 		reset();
    134   1.9  jtc 		if (exception == EXINT
    135   1.1  cgd #if ATTY
    136   1.9  jtc 		 && (! attyset() || equal(termval(), "emacs"))
    137   1.1  cgd #endif
    138   1.9  jtc 		 ) {
    139   1.1  cgd 			out2c('\n');
    140   1.1  cgd 			flushout(&errout);
    141   1.1  cgd 		}
    142   1.1  cgd 		popstackmark(&smark);
    143   1.1  cgd 		FORCEINTON;				/* enable interrupts */
    144   1.1  cgd 		if (state == 1)
    145   1.1  cgd 			goto state1;
    146   1.1  cgd 		else if (state == 2)
    147   1.1  cgd 			goto state2;
    148   1.9  jtc 		else if (state == 3)
    149   1.9  jtc 			goto state3;
    150   1.1  cgd 		else
    151   1.9  jtc 			goto state4;
    152   1.1  cgd 	}
    153   1.1  cgd 	handler = &jmploc;
    154   1.1  cgd #ifdef DEBUG
    155   1.1  cgd 	opentrace();
    156   1.1  cgd 	trputs("Shell args:  ");  trargs(argv);
    157   1.1  cgd #endif
    158   1.1  cgd 	rootpid = getpid();
    159   1.1  cgd 	rootshell = 1;
    160   1.1  cgd 	init();
    161   1.1  cgd 	setstackmark(&smark);
    162   1.1  cgd 	procargs(argc, argv);
    163   1.1  cgd 	if (argv[0] && argv[0][0] == '-') {
    164   1.1  cgd 		state = 1;
    165   1.1  cgd 		read_profile("/etc/profile");
    166   1.1  cgd state1:
    167   1.1  cgd 		state = 2;
    168   1.1  cgd 		read_profile(".profile");
    169   1.9  jtc 	}
    170   1.1  cgd state2:
    171   1.1  cgd 	state = 3;
    172  1.14  jtc 	if (getuid() == geteuid() && getgid() == getegid()) {
    173  1.14  jtc 		if ((shinit = lookupvar("ENV")) != NULL && *shinit != '\0') {
    174  1.14  jtc 			state = 3;
    175  1.14  jtc 			read_profile(shinit);
    176  1.14  jtc 		}
    177   1.9  jtc 	}
    178   1.9  jtc state3:
    179   1.9  jtc 	state = 4;
    180   1.1  cgd 	if (minusc) {
    181   1.1  cgd 		evalstring(minusc);
    182   1.1  cgd 	}
    183   1.1  cgd 	if (sflag || minusc == NULL) {
    184   1.9  jtc state4:	/* XXX ??? - why isn't this before the "if" statement */
    185   1.1  cgd 		cmdloop(1);
    186   1.1  cgd 	}
    187   1.1  cgd #if PROFILE
    188   1.1  cgd 	monitor(0);
    189   1.1  cgd #endif
    190   1.1  cgd 	exitshell(exitstatus);
    191   1.1  cgd }
    192   1.1  cgd 
    193   1.1  cgd 
    194   1.1  cgd /*
    195   1.1  cgd  * Read and execute commands.  "Top" is nonzero for the top level command
    196   1.1  cgd  * loop; it turns on prompting if the shell is interactive.
    197   1.1  cgd  */
    198   1.1  cgd 
    199   1.1  cgd void
    200  1.15  cgd cmdloop(top)
    201  1.15  cgd 	int top;
    202  1.15  cgd {
    203   1.1  cgd 	union node *n;
    204   1.1  cgd 	struct stackmark smark;
    205   1.1  cgd 	int inter;
    206   1.9  jtc 	int numeof = 0;
    207   1.1  cgd 
    208   1.1  cgd 	TRACE(("cmdloop(%d) called\n", top));
    209   1.1  cgd 	setstackmark(&smark);
    210   1.1  cgd 	for (;;) {
    211   1.1  cgd 		if (pendingsigs)
    212   1.1  cgd 			dotrap();
    213   1.1  cgd 		inter = 0;
    214   1.1  cgd 		if (iflag && top) {
    215   1.1  cgd 			inter++;
    216   1.1  cgd 			showjobs(1);
    217   1.1  cgd 			chkmail(0);
    218   1.1  cgd 			flushout(&output);
    219   1.1  cgd 		}
    220   1.1  cgd 		n = parsecmd(inter);
    221   1.9  jtc 		/* showtree(n); DEBUG */
    222   1.1  cgd 		if (n == NEOF) {
    223   1.9  jtc 			if (!top || numeof >= 50)
    224   1.1  cgd 				break;
    225   1.9  jtc 			if (!stoppedjobs()) {
    226   1.9  jtc 				if (!Iflag)
    227   1.9  jtc 					break;
    228   1.9  jtc 				out2str("\nUse \"exit\" to leave shell.\n");
    229   1.9  jtc 			}
    230   1.1  cgd 			numeof++;
    231   1.1  cgd 		} else if (n != NULL && nflag == 0) {
    232   1.9  jtc 			job_warning = (job_warning == 2) ? 1 : 0;
    233   1.9  jtc 			numeof = 0;
    234   1.1  cgd 			evaltree(n, 0);
    235   1.1  cgd 		}
    236   1.1  cgd 		popstackmark(&smark);
    237   1.1  cgd 	}
    238   1.1  cgd 	popstackmark(&smark);		/* unnecessary */
    239   1.1  cgd }
    240   1.1  cgd 
    241   1.1  cgd 
    242   1.1  cgd 
    243   1.1  cgd /*
    244   1.1  cgd  * Read /etc/profile or .profile.  Return on error.
    245   1.1  cgd  */
    246   1.1  cgd 
    247   1.1  cgd STATIC void
    248   1.1  cgd read_profile(name)
    249   1.1  cgd 	char *name;
    250   1.1  cgd 	{
    251   1.1  cgd 	int fd;
    252   1.1  cgd 
    253   1.1  cgd 	INTOFF;
    254   1.1  cgd 	if ((fd = open(name, O_RDONLY)) >= 0)
    255   1.1  cgd 		setinputfd(fd, 1);
    256   1.1  cgd 	INTON;
    257   1.1  cgd 	if (fd < 0)
    258   1.1  cgd 		return;
    259   1.1  cgd 	cmdloop(0);
    260   1.1  cgd 	popfile();
    261   1.1  cgd }
    262   1.1  cgd 
    263   1.1  cgd 
    264   1.1  cgd 
    265   1.1  cgd /*
    266   1.1  cgd  * Read a file containing shell functions.
    267   1.1  cgd  */
    268   1.1  cgd 
    269   1.1  cgd void
    270   1.1  cgd readcmdfile(name)
    271   1.1  cgd 	char *name;
    272  1.16  cgd {
    273   1.1  cgd 	int fd;
    274   1.1  cgd 
    275   1.1  cgd 	INTOFF;
    276   1.1  cgd 	if ((fd = open(name, O_RDONLY)) >= 0)
    277   1.1  cgd 		setinputfd(fd, 1);
    278   1.1  cgd 	else
    279   1.1  cgd 		error("Can't open %s", name);
    280   1.1  cgd 	INTON;
    281   1.1  cgd 	cmdloop(0);
    282   1.1  cgd 	popfile();
    283   1.1  cgd }
    284   1.1  cgd 
    285   1.1  cgd 
    286   1.1  cgd 
    287   1.1  cgd /*
    288   1.1  cgd  * Take commands from a file.  To be compatable we should do a path
    289  1.10  jtc  * search for the file, which is necessary to find sub-commands.
    290   1.1  cgd  */
    291   1.1  cgd 
    292  1.10  jtc 
    293  1.10  jtc static char *
    294  1.10  jtc find_dot_file(basename) char *basename; {
    295  1.10  jtc 	static char localname[FILENAME_MAX+1];
    296  1.10  jtc 	char *fullname;
    297  1.10  jtc 	char *path = pathval();
    298  1.10  jtc 	struct stat statb;
    299  1.10  jtc 
    300  1.10  jtc 	/* don't try this for absolute or relative paths */
    301  1.10  jtc 	if( strchr(basename, '/'))
    302  1.10  jtc 		return basename;
    303  1.10  jtc 
    304  1.10  jtc 	while ((fullname = padvance(&path, basename)) != NULL) {
    305  1.10  jtc 		strcpy(localname, fullname);
    306  1.10  jtc 		stunalloc(fullname);
    307  1.10  jtc 		if ((stat(fullname, &statb) == 0) && S_ISREG(statb.st_mode))
    308  1.10  jtc 			return localname;
    309  1.10  jtc 	}
    310  1.10  jtc 	return basename;
    311  1.10  jtc }
    312  1.10  jtc 
    313  1.15  cgd int
    314  1.15  cgd dotcmd(argc, argv)
    315  1.15  cgd 	int argc;
    316  1.15  cgd 	char **argv;
    317  1.15  cgd {
    318   1.1  cgd 	exitstatus = 0;
    319   1.1  cgd 	if (argc >= 2) {		/* That's what SVR2 does */
    320  1.10  jtc 		char *fullname = find_dot_file(argv[1]);
    321  1.10  jtc 		setinputfile(fullname, 1);
    322  1.10  jtc 		commandname = fullname;
    323   1.1  cgd 		cmdloop(0);
    324   1.1  cgd 		popfile();
    325   1.1  cgd 	}
    326   1.1  cgd 	return exitstatus;
    327   1.1  cgd }
    328   1.1  cgd 
    329   1.1  cgd 
    330  1.15  cgd int
    331  1.15  cgd exitcmd(argc, argv)
    332  1.15  cgd 	int argc;
    333  1.15  cgd 	char **argv;
    334  1.15  cgd {
    335   1.9  jtc 	if (stoppedjobs())
    336   1.9  jtc 		return;
    337   1.1  cgd 	if (argc > 1)
    338   1.1  cgd 		exitstatus = number(argv[1]);
    339   1.1  cgd 	exitshell(exitstatus);
    340   1.1  cgd }
    341   1.1  cgd 
    342   1.1  cgd 
    343   1.1  cgd #ifdef notdef
    344   1.1  cgd /*
    345   1.1  cgd  * Should never be called.
    346   1.1  cgd  */
    347   1.1  cgd 
    348   1.1  cgd void
    349   1.1  cgd exit(exitstatus) {
    350   1.1  cgd 	_exit(exitstatus);
    351   1.1  cgd }
    352   1.1  cgd #endif
    353