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