Home | History | Annotate | Line # | Download | only in sh
main.c revision 1.19
      1 /*	$NetBSD: main.c,v 1.19 1995/05/19 15:08:58 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 #ifndef lint
     40 static char copyright[] =
     41 "@(#) 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.5 (Berkeley) 5/19/95";
     48 #else
     49 static char rcsid[] = "$Header: /tank/opengrok/rsync2/NetBSD/src/bin/sh/main.c,v 1.19 1995/05/19 15:08:58 christos Exp $";
     50 #endif
     51 #endif /* not lint */
     52 
     53 #include <stdio.h>
     54 #include <signal.h>
     55 #include <sys/stat.h>
     56 #include <unistd.h>
     57 #include <fcntl.h>
     58 
     59 
     60 #include "shell.h"
     61 #include "main.h"
     62 #include "mail.h"
     63 #include "options.h"
     64 #include "output.h"
     65 #include "parser.h"
     66 #include "nodes.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 
     79 #define PROFILE 0
     80 
     81 int rootpid;
     82 int rootshell;
     83 STATIC union node *curcmd;
     84 STATIC union node *prevcmd;
     85 extern int errno;
     86 #if PROFILE
     87 short profile_buf[16384];
     88 extern int etext();
     89 #endif
     90 
     91 STATIC void read_profile __P((char *));
     92 STATIC char *find_dot_file __P((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(argc, argv)
    104 	int argc;
    105 	char **argv;
    106 {
    107 	struct jmploc jmploc;
    108 	struct stackmark smark;
    109 	volatile int state;
    110 	char *shinit;
    111 
    112 #if PROFILE
    113 	monitor(4, etext, profile_buf, sizeof profile_buf, 50);
    114 #endif
    115 	state = 0;
    116 	if (setjmp(jmploc.loc)) {
    117 		/*
    118 		 * When a shell procedure is executed, we raise the
    119 		 * exception EXSHELLPROC to clean up before executing
    120 		 * the shell procedure.
    121 		 */
    122 		if (exception == EXERROR)
    123 			exitstatus = 2;
    124 		if (exception == EXSHELLPROC) {
    125 			rootpid = getpid();
    126 			rootshell = 1;
    127 			minusc = NULL;
    128 			state = 3;
    129 		} else if (state == 0 || iflag == 0 || ! rootshell)
    130 			exitshell(2);
    131 		reset();
    132 		if (exception == EXINT
    133 #if ATTY
    134 		 && (! attyset() || equal(termval(), "emacs"))
    135 #endif
    136 		 ) {
    137 			out2c('\n');
    138 			flushout(&errout);
    139 		}
    140 		popstackmark(&smark);
    141 		FORCEINTON;				/* enable interrupts */
    142 		if (state == 1)
    143 			goto state1;
    144 		else if (state == 2)
    145 			goto state2;
    146 		else if (state == 3)
    147 			goto state3;
    148 		else
    149 			goto state4;
    150 	}
    151 	handler = &jmploc;
    152 #ifdef DEBUG
    153 	opentrace();
    154 	trputs("Shell args:  ");  trargs(argv);
    155 #endif
    156 	rootpid = getpid();
    157 	rootshell = 1;
    158 	init();
    159 	setstackmark(&smark);
    160 	procargs(argc, argv);
    161 	if (argv[0] && argv[0][0] == '-') {
    162 		state = 1;
    163 		read_profile("/etc/profile");
    164 state1:
    165 		state = 2;
    166 		read_profile(".profile");
    167 	}
    168 state2:
    169 	state = 3;
    170 	if (getuid() == geteuid() && getgid() == getegid()) {
    171 		if ((shinit = lookupvar("ENV")) != NULL && *shinit != '\0') {
    172 			state = 3;
    173 			read_profile(shinit);
    174 		}
    175 	}
    176 state3:
    177 	state = 4;
    178 	if (minusc) {
    179 		evalstring(minusc);
    180 	}
    181 	if (sflag || minusc == NULL) {
    182 state4:	/* XXX ??? - why isn't this before the "if" statement */
    183 		cmdloop(1);
    184 	}
    185 #if PROFILE
    186 	monitor(0);
    187 #endif
    188 	exitshell(exitstatus);
    189 	/*NOTREACHED*/
    190 	return 0;
    191 }
    192 
    193 
    194 /*
    195  * Read and execute commands.  "Top" is nonzero for the top level command
    196  * loop; it turns on prompting if the shell is interactive.
    197  */
    198 
    199 void
    200 cmdloop(top)
    201 	int top;
    202 {
    203 	union node *n;
    204 	struct stackmark smark;
    205 	int inter;
    206 	int numeof = 0;
    207 
    208 	TRACE(("cmdloop(%d) called\n", top));
    209 	setstackmark(&smark);
    210 	for (;;) {
    211 		if (pendingsigs)
    212 			dotrap();
    213 		inter = 0;
    214 		if (iflag && top) {
    215 			inter++;
    216 			showjobs(1);
    217 			chkmail(0);
    218 			flushout(&output);
    219 		}
    220 		n = parsecmd(inter);
    221 		/* showtree(n); DEBUG */
    222 		if (n == NEOF) {
    223 			if (!top || numeof >= 50)
    224 				break;
    225 			if (!stoppedjobs()) {
    226 				if (!Iflag)
    227 					break;
    228 				out2str("\nUse \"exit\" to leave shell.\n");
    229 			}
    230 			numeof++;
    231 		} else if (n != NULL && nflag == 0) {
    232 			job_warning = (job_warning == 2) ? 1 : 0;
    233 			numeof = 0;
    234 			evaltree(n, 0);
    235 		}
    236 		popstackmark(&smark);
    237 	}
    238 	popstackmark(&smark);		/* unnecessary */
    239 }
    240 
    241 
    242 
    243 /*
    244  * Read /etc/profile or .profile.  Return on error.
    245  */
    246 
    247 STATIC void
    248 read_profile(name)
    249 	char *name;
    250 	{
    251 	int fd;
    252 
    253 	INTOFF;
    254 	if ((fd = open(name, O_RDONLY)) >= 0)
    255 		setinputfd(fd, 1);
    256 	INTON;
    257 	if (fd < 0)
    258 		return;
    259 	cmdloop(0);
    260 	popfile();
    261 }
    262 
    263 
    264 
    265 /*
    266  * Read a file containing shell functions.
    267  */
    268 
    269 void
    270 readcmdfile(name)
    271 	char *name;
    272 {
    273 	int fd;
    274 
    275 	INTOFF;
    276 	if ((fd = open(name, O_RDONLY)) >= 0)
    277 		setinputfd(fd, 1);
    278 	else
    279 		error("Can't open %s", name);
    280 	INTON;
    281 	cmdloop(0);
    282 	popfile();
    283 }
    284 
    285 
    286 
    287 /*
    288  * Take commands from a file.  To be compatable we should do a path
    289  * search for the file, which is necessary to find sub-commands.
    290  */
    291 
    292 
    293 STATIC char *
    294 find_dot_file(basename)
    295 	char *basename;
    296 {
    297 	static char localname[FILENAME_MAX+1];
    298 	char *fullname;
    299 	char *path = pathval();
    300 	struct stat statb;
    301 
    302 	/* don't try this for absolute or relative paths */
    303 	if( strchr(basename, '/'))
    304 		return basename;
    305 
    306 	while ((fullname = padvance(&path, basename)) != NULL) {
    307 		strcpy(localname, fullname);
    308 		stunalloc(fullname);
    309 		if ((stat(fullname, &statb) == 0) && S_ISREG(statb.st_mode))
    310 			return localname;
    311 	}
    312 	return basename;
    313 }
    314 
    315 int
    316 dotcmd(argc, argv)
    317 	int argc;
    318 	char **argv;
    319 {
    320 	exitstatus = 0;
    321 	if (argc >= 2) {		/* That's what SVR2 does */
    322 		char *fullname = find_dot_file(argv[1]);
    323 		setinputfile(fullname, 1);
    324 		commandname = fullname;
    325 		cmdloop(0);
    326 		popfile();
    327 	}
    328 	return exitstatus;
    329 }
    330 
    331 
    332 int
    333 exitcmd(argc, argv)
    334 	int argc;
    335 	char **argv;
    336 {
    337 	if (stoppedjobs())
    338 		return 0;
    339 	if (argc > 1)
    340 		exitstatus = number(argv[1]);
    341 	exitshell(exitstatus);
    342 	/*NOTREACHED*/
    343 	return 0;
    344 }
    345 
    346 
    347 #ifdef notdef
    348 /*
    349  * Should never be called.
    350  */
    351 
    352 void
    353 exit(exitstatus) {
    354 	_exit(exitstatus);
    355 }
    356 #endif
    357