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