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