Home | History | Annotate | Line # | Download | only in sh
main.c revision 1.56
      1 /*	$NetBSD: main.c,v 1.56 2010/02/21 09:54:57 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.56 2010/02/21 09:54:57 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 "output.h"
     64 #include "parser.h"
     65 #include "nodes.h"
     66 #include "expand.h"
     67 #include "eval.h"
     68 #include "jobs.h"
     69 #include "input.h"
     70 #include "trap.h"
     71 #include "var.h"
     72 #include "show.h"
     73 #include "memalloc.h"
     74 #include "error.h"
     75 #include "init.h"
     76 #include "mystring.h"
     77 #include "exec.h"
     78 #include "cd.h"
     79 
     80 #define PROFILE 0
     81 
     82 int rootpid;
     83 int rootshell;
     84 int posix;
     85 #if PROFILE
     86 short profile_buf[16384];
     87 extern int etext();
     88 #endif
     89 
     90 STATIC void read_profile(const char *);
     91 STATIC char *find_dot_file(char *);
     92 int main(int, char **);
     93 
     94 /*
     95  * Main routine.  We initialize things, parse the arguments, execute
     96  * profiles if we're a login shell, and then call cmdloop to execute
     97  * commands.  The setjmp call sets up the location to jump to when an
     98  * exception occurs.  When an exception occurs the variable "state"
     99  * is used to figure out how far we had gotten.
    100  */
    101 
    102 int
    103 main(int argc, char **argv)
    104 {
    105 	struct jmploc jmploc;
    106 	struct stackmark smark;
    107 	volatile int state;
    108 	char *shinit;
    109 
    110 	setlocale(LC_ALL, "");
    111 
    112 	posix = getenv("POSIXLY_CORRECT") != NULL;
    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 #if DEBUG == 2
    170 	debug = 1;
    171 #endif
    172 	opentrace();
    173 	trputs("Shell args:  ");  trargs(argv);
    174 #endif
    175 	rootpid = getpid();
    176 	rootshell = 1;
    177 	init();
    178 	initpwd();
    179 	setstackmark(&smark);
    180 	procargs(argc, argv);
    181 	if (argv[0] && argv[0][0] == '-') {
    182 		state = 1;
    183 		read_profile("/etc/profile");
    184 state1:
    185 		state = 2;
    186 		read_profile(".profile");
    187 	}
    188 state2:
    189 	state = 3;
    190 	if ((iflag || !posix) &&
    191 	    getuid() == geteuid() && getgid() == getegid()) {
    192 		if ((shinit = lookupvar("ENV")) != NULL && *shinit != '\0') {
    193 			state = 3;
    194 			read_profile(shinit);
    195 		}
    196 	}
    197 state3:
    198 	state = 4;
    199 	if (sflag == 0 || minusc) {
    200 		static int sigs[] =  {
    201 		    SIGINT, SIGQUIT, SIGHUP,
    202 #ifdef SIGTSTP
    203 		    SIGTSTP,
    204 #endif
    205 		    SIGPIPE
    206 		};
    207 #define SIGSSIZE (sizeof(sigs)/sizeof(sigs[0]))
    208 		size_t i;
    209 
    210 		for (i = 0; i < SIGSSIZE; i++)
    211 		    setsignal(sigs[i], 0);
    212 	}
    213 
    214 	if (minusc)
    215 		evalstring(minusc, 0);
    216 
    217 	if (sflag || minusc == NULL) {
    218 state4:	/* XXX ??? - why isn't this before the "if" statement */
    219 		cmdloop(1);
    220 	}
    221 #if PROFILE
    222 	monitor(0);
    223 #endif
    224 	exitshell(exitstatus);
    225 	/* NOTREACHED */
    226 }
    227 
    228 
    229 /*
    230  * Read and execute commands.  "Top" is nonzero for the top level command
    231  * loop; it turns on prompting if the shell is interactive.
    232  */
    233 
    234 void
    235 cmdloop(int top)
    236 {
    237 	union node *n;
    238 	struct stackmark smark;
    239 	int inter;
    240 	int numeof = 0;
    241 
    242 	TRACE(("cmdloop(%d) called\n", top));
    243 	setstackmark(&smark);
    244 	for (;;) {
    245 		if (pendingsigs)
    246 			dotrap();
    247 		inter = 0;
    248 		if (iflag == 1 && top) {
    249 			inter = 1;
    250 			showjobs(out2, SHOW_CHANGED);
    251 			chkmail(0);
    252 			flushout(&errout);
    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 			evaltree(n, 0);
    269 		}
    270 		popstackmark(&smark);
    271 		setstackmark(&smark);
    272 		if (evalskip == SKIPFILE) {
    273 			evalskip = 0;
    274 			break;
    275 		}
    276 	}
    277 	popstackmark(&smark);
    278 }
    279 
    280 
    281 
    282 /*
    283  * Read /etc/profile or .profile.  Return on error.
    284  */
    285 
    286 STATIC void
    287 read_profile(const char *name)
    288 {
    289 	int fd;
    290 	int xflag_set = 0;
    291 	int vflag_set = 0;
    292 
    293 	INTOFF;
    294 	if ((fd = open(name, O_RDONLY)) >= 0)
    295 		setinputfd(fd, 1);
    296 	INTON;
    297 	if (fd < 0)
    298 		return;
    299 	/* -q turns off -x and -v just when executing init files */
    300 	if (qflag)  {
    301 	    if (xflag)
    302 		    xflag = 0, xflag_set = 1;
    303 	    if (vflag)
    304 		    vflag = 0, vflag_set = 1;
    305 	}
    306 	cmdloop(0);
    307 	if (qflag)  {
    308 	    if (xflag_set)
    309 		    xflag = 1;
    310 	    if (vflag_set)
    311 		    vflag = 1;
    312 	}
    313 	popfile();
    314 }
    315 
    316 
    317 
    318 /*
    319  * Read a file containing shell functions.
    320  */
    321 
    322 void
    323 readcmdfile(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 compatible 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(char *basename)
    347 {
    348 	char *fullname;
    349 	const char *path = pathval();
    350 	struct stat statb;
    351 
    352 	/* don't try this for absolute or relative paths */
    353 	if (strchr(basename, '/'))
    354 		return basename;
    355 
    356 	while ((fullname = padvance(&path, basename)) != NULL) {
    357 		if ((stat(fullname, &statb) == 0) && S_ISREG(statb.st_mode)) {
    358 			/*
    359 			 * Don't bother freeing here, since it will
    360 			 * be freed by the caller.
    361 			 */
    362 			return fullname;
    363 		}
    364 		stunalloc(fullname);
    365 	}
    366 
    367 	/* not found in the PATH */
    368 	error("%s: not found", basename);
    369 	/* NOTREACHED */
    370 }
    371 
    372 int
    373 dotcmd(int argc, char **argv)
    374 {
    375 	exitstatus = 0;
    376 
    377 	if (argc >= 2) {		/* That's what SVR2 does */
    378 		char *fullname;
    379 		struct stackmark smark;
    380 
    381 		setstackmark(&smark);
    382 		fullname = find_dot_file(argv[1]);
    383 		setinputfile(fullname, 1);
    384 		commandname = fullname;
    385 		cmdloop(0);
    386 		popfile();
    387 		popstackmark(&smark);
    388 	}
    389 	return exitstatus;
    390 }
    391 
    392 
    393 int
    394 exitcmd(int argc, char **argv)
    395 {
    396 	if (stoppedjobs())
    397 		return 0;
    398 	if (argc > 1)
    399 		exitstatus = number(argv[1]);
    400 	exitshell(exitstatus);
    401 	/* NOTREACHED */
    402 }
    403