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