Home | History | Annotate | Line # | Download | only in sh
main.c revision 1.43
      1 /*	$NetBSD: main.c,v 1.43 2002/09/27 22:56:24 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.43 2002/09/27 22:56:24 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 extern int oexitstatus;
     98 /*
     99  * Main routine.  We initialize things, parse the arguments, execute
    100  * profiles if we're a login shell, and then call cmdloop to execute
    101  * commands.  The setjmp call sets up the location to jump to when an
    102  * exception occurs.  When an exception occurs the variable "state"
    103  * is used to figure out how far we had gotten.
    104  */
    105 
    106 int
    107 main(argc, argv)
    108 	int argc;
    109 	char **argv;
    110 {
    111 	struct jmploc jmploc;
    112 	struct stackmark smark;
    113 	volatile int state;
    114 	char *shinit;
    115 
    116 #if PROFILE
    117 	monitor(4, etext, profile_buf, sizeof profile_buf, 50);
    118 #endif
    119 	state = 0;
    120 	if (setjmp(jmploc.loc)) {
    121 		/*
    122 		 * When a shell procedure is executed, we raise the
    123 		 * exception EXSHELLPROC to clean up before executing
    124 		 * the shell procedure.
    125 		 */
    126 		switch (exception) {
    127 		case EXSHELLPROC:
    128 			rootpid = getpid();
    129 			rootshell = 1;
    130 			minusc = NULL;
    131 			state = 3;
    132 			break;
    133 
    134 		case EXEXEC:
    135 			exitstatus = exerrno;
    136 			break;
    137 
    138 		case EXERROR:
    139 			exitstatus = 2;
    140 			break;
    141 
    142 		default:
    143 			break;
    144 		}
    145 
    146 		if (exception != EXSHELLPROC) {
    147 		    if (state == 0 || iflag == 0 || ! rootshell)
    148 			    exitshell(exitstatus);
    149 		}
    150 		reset();
    151 		if (exception == EXINT
    152 #if ATTY
    153 		 && (! attyset() || equal(termval(), "emacs"))
    154 #endif
    155 		 ) {
    156 			out2c('\n');
    157 			flushout(&errout);
    158 		}
    159 		popstackmark(&smark);
    160 		FORCEINTON;				/* enable interrupts */
    161 		if (state == 1)
    162 			goto state1;
    163 		else if (state == 2)
    164 			goto state2;
    165 		else if (state == 3)
    166 			goto state3;
    167 		else
    168 			goto state4;
    169 	}
    170 	handler = &jmploc;
    171 #ifdef DEBUG
    172 	opentrace();
    173 	trputs("Shell args:  ");  trargs(argv);
    174 #endif
    175 	rootpid = getpid();
    176 	rootshell = 1;
    177 	init();
    178 	setstackmark(&smark);
    179 	procargs(argc, argv);
    180 	if (argv[0] && argv[0][0] == '-') {
    181 		state = 1;
    182 		read_profile("/etc/profile");
    183 state1:
    184 		state = 2;
    185 		read_profile(".profile");
    186 	}
    187 state2:
    188 	state = 3;
    189 	if (getuid() == geteuid() && getgid() == getegid()) {
    190 		if ((shinit = lookupvar("ENV")) != NULL && *shinit != '\0') {
    191 			state = 3;
    192 			read_profile(shinit);
    193 		}
    194 	}
    195 state3:
    196 	state = 4;
    197 	if (sflag == 0 || minusc) {
    198 		static int sigs[] =  {
    199 		    SIGINT, SIGQUIT, SIGHUP,
    200 #ifdef SIGTSTP
    201 		    SIGTSTP,
    202 #endif
    203 		    SIGPIPE
    204 		};
    205 #define SIGSSIZE (sizeof(sigs)/sizeof(sigs[0]))
    206 		int i;
    207 
    208 		for (i = 0; i < SIGSSIZE; i++)
    209 		    setsignal(sigs[i], 0);
    210 	}
    211 
    212 	if (minusc)
    213 		evalstring(minusc, 0);
    214 
    215 	if (sflag || minusc == NULL) {
    216 state4:	/* XXX ??? - why isn't this before the "if" statement */
    217 		cmdloop(1);
    218 	}
    219 #if PROFILE
    220 	monitor(0);
    221 #endif
    222 	exitshell(exitstatus);
    223 	/* NOTREACHED */
    224 }
    225 
    226 
    227 /*
    228  * Read and execute commands.  "Top" is nonzero for the top level command
    229  * loop; it turns on prompting if the shell is interactive.
    230  */
    231 
    232 void
    233 cmdloop(top)
    234 	int top;
    235 {
    236 	union node *n;
    237 	struct stackmark smark;
    238 	int inter;
    239 	int numeof = 0;
    240 	int isroot;
    241 
    242 	TRACE(("cmdloop(%d) called\n", top));
    243 	setstackmark(&smark);
    244 	for (;;) {
    245 		if (pendingsigs)
    246 			dotrap();
    247 		inter = 0;
    248 		if (iflag && top) {
    249 			inter++;
    250 			showjobs(1);
    251 			chkmail(0);
    252 			flushout(&output);
    253 		}
    254 		n = parsecmd(inter);
    255 		/* showtree(n); DEBUG */
    256 		if (n == NEOF) {
    257 			if (!top || numeof >= 50)
    258 				break;
    259 			if (!stoppedjobs()) {
    260 				if (!Iflag)
    261 					break;
    262 				out2str("\nUse \"exit\" to leave shell.\n");
    263 			}
    264 			numeof++;
    265 		} else if (n != NULL && nflag == 0) {
    266 			job_warning = (job_warning == 2) ? 1 : 0;
    267 			numeof = 0;
    268 			isroot = rootshell;
    269 			evaltree(n, 0, &isroot);
    270 		}
    271 		popstackmark(&smark);
    272 		setstackmark(&smark);
    273 		if (evalskip == SKIPFILE) {
    274 			evalskip = 0;
    275 			break;
    276 		}
    277 	}
    278 	popstackmark(&smark);
    279 }
    280 
    281 
    282 
    283 /*
    284  * Read /etc/profile or .profile.  Return on error.
    285  */
    286 
    287 STATIC void
    288 read_profile(name)
    289 	const char *name;
    290 {
    291 	int fd;
    292 	int xflag_set = 0;
    293 	int vflag_set = 0;
    294 
    295 	INTOFF;
    296 	if ((fd = open(name, O_RDONLY)) >= 0)
    297 		setinputfd(fd, 1);
    298 	INTON;
    299 	if (fd < 0)
    300 		return;
    301 	/* -q turns off -x and -v just when executing init files */
    302 	if (qflag)  {
    303 	    if (xflag)
    304 		    xflag = 0, xflag_set = 1;
    305 	    if (vflag)
    306 		    vflag = 0, vflag_set = 1;
    307 	}
    308 	cmdloop(0);
    309 	if (qflag)  {
    310 	    if (xflag_set)
    311 		    xflag = 1;
    312 	    if (vflag_set)
    313 		    vflag = 1;
    314 	}
    315 	popfile();
    316 }
    317 
    318 
    319 
    320 /*
    321  * Read a file containing shell functions.
    322  */
    323 
    324 void
    325 readcmdfile(name)
    326 	char *name;
    327 {
    328 	int fd;
    329 
    330 	INTOFF;
    331 	if ((fd = open(name, O_RDONLY)) >= 0)
    332 		setinputfd(fd, 1);
    333 	else
    334 		error("Can't open %s", name);
    335 	INTON;
    336 	cmdloop(0);
    337 	popfile();
    338 }
    339 
    340 
    341 
    342 /*
    343  * Take commands from a file.  To be compatible we should do a path
    344  * search for the file, which is necessary to find sub-commands.
    345  */
    346 
    347 
    348 STATIC char *
    349 find_dot_file(basename)
    350 	char *basename;
    351 {
    352 	char *fullname;
    353 	const char *path = pathval();
    354 	struct stat statb;
    355 
    356 	/* don't try this for absolute or relative paths */
    357 	if (strchr(basename, '/'))
    358 		return basename;
    359 
    360 	while ((fullname = padvance(&path, basename)) != NULL) {
    361 		if ((stat(fullname, &statb) == 0) && S_ISREG(statb.st_mode)) {
    362 			/*
    363 			 * Don't bother freeing here, since it will
    364 			 * be freed by the caller.
    365 			 */
    366 			return fullname;
    367 		}
    368 		stunalloc(fullname);
    369 	}
    370 
    371 	/* not found in the PATH */
    372 	error("%s: not found", basename);
    373 	/* NOTREACHED */
    374 }
    375 
    376 int
    377 dotcmd(argc, argv)
    378 	int argc;
    379 	char **argv;
    380 {
    381 	struct strlist *sp;
    382 	exitstatus = 0;
    383 
    384 	for (sp = cmdenviron; sp ; sp = sp->next)
    385 		setvareq(savestr(sp->text), VSTRFIXED|VTEXTFIXED);
    386 
    387 	if (argc >= 2) {		/* That's what SVR2 does */
    388 		char *fullname;
    389 		struct stackmark smark;
    390 
    391 		setstackmark(&smark);
    392 		fullname = find_dot_file(argv[1]);
    393 		setinputfile(fullname, 1);
    394 		commandname = fullname;
    395 		cmdloop(0);
    396 		popfile();
    397 		popstackmark(&smark);
    398 	}
    399 	return exitstatus;
    400 }
    401 
    402 
    403 int
    404 exitcmd(argc, argv)
    405 	int argc;
    406 	char **argv;
    407 {
    408 	if (stoppedjobs())
    409 		return 0;
    410 	if (argc > 1)
    411 		exitstatus = number(argv[1]);
    412 	else
    413 		exitstatus = oexitstatus;
    414 	exitshell(exitstatus);
    415 	/* NOTREACHED */
    416 }
    417