Home | History | Annotate | Line # | Download | only in sh
main.c revision 1.11
      1 /*-
      2  * Copyright (c) 1991, 1993
      3  *	The Regents of the University of California.  All rights reserved.
      4  *
      5  * This code is derived from software contributed to Berkeley by
      6  * Kenneth Almquist.
      7  *
      8  * Redistribution and use in source and binary forms, with or without
      9  * modification, are permitted provided that the following conditions
     10  * are met:
     11  * 1. Redistributions of source code must retain the above copyright
     12  *    notice, this list of conditions and the following disclaimer.
     13  * 2. Redistributions in binary form must reproduce the above copyright
     14  *    notice, this list of conditions and the following disclaimer in the
     15  *    documentation and/or other materials provided with the distribution.
     16  * 3. All advertising materials mentioning features or use of this software
     17  *    must display the following acknowledgement:
     18  *	This product includes software developed by the University of
     19  *	California, Berkeley and its contributors.
     20  * 4. Neither the name of the University nor the names of its contributors
     21  *    may be used to endorse or promote products derived from this software
     22  *    without specific prior written permission.
     23  *
     24  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     25  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     26  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     27  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     28  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     29  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     30  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     31  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     32  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     33  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     34  * SUCH DAMAGE.
     35  */
     36 
     37 #ifndef lint
     38 static char copyright[] =
     39 "@(#) Copyright (c) 1991, 1993\n\
     40 	The Regents of the University of California.  All rights reserved.\n";
     41 #endif /* not lint */
     42 
     43 #ifndef lint
     44 static char sccsid[] = "@(#)main.c	8.1 (Berkeley) 5/31/93";
     45 #endif /* not lint */
     46 
     47 #include <stdio.h>
     48 #include <signal.h>
     49 #include <sys/stat.h>
     50 #include <fcntl.h>
     51 #include "shell.h"
     52 #include "main.h"
     53 #include "mail.h"
     54 #include "options.h"
     55 #include "output.h"
     56 #include "parser.h"
     57 #include "nodes.h"
     58 #include "eval.h"
     59 #include "jobs.h"
     60 #include "input.h"
     61 #include "trap.h"
     62 #include "var.h"
     63 #include "memalloc.h"
     64 #include "error.h"
     65 #include "init.h"
     66 #include "mystring.h"
     67 #include "exec.h"
     68 
     69 #define PROFILE 0
     70 
     71 int rootpid;
     72 int rootshell;
     73 STATIC union node *curcmd;
     74 STATIC union node *prevcmd;
     75 extern int errno;
     76 #if PROFILE
     77 short profile_buf[16384];
     78 extern int etext();
     79 #endif
     80 
     81 #ifdef __STDC__
     82 STATIC void read_profile(char *);
     83 char *getenv(char *);
     84 #else
     85 STATIC void read_profile();
     86 char *getenv();
     87 #endif
     88 
     89 
     90 /*
     91  * Main routine.  We initialize things, parse the arguments, execute
     92  * profiles if we're a login shell, and then call cmdloop to execute
     93  * commands.  The setjmp call sets up the location to jump to when an
     94  * exception occurs.  When an exception occurs the variable "state"
     95  * is used to figure out how far we had gotten.
     96  */
     97 
     98 main(argc, argv)  char **argv; {
     99 	struct jmploc jmploc;
    100 	struct stackmark smark;
    101 	volatile int state;
    102 	char *shinit;
    103 
    104 #if PROFILE
    105 	monitor(4, etext, profile_buf, sizeof profile_buf, 50);
    106 #endif
    107 	state = 0;
    108 	if (setjmp(jmploc.loc)) {
    109 		/*
    110 		 * When a shell procedure is executed, we raise the
    111 		 * exception EXSHELLPROC to clean up before executing
    112 		 * the shell procedure.
    113 		 */
    114 		if (exception == EXSHELLPROC) {
    115 			rootpid = getpid();
    116 			rootshell = 1;
    117 			minusc = NULL;
    118 			state = 3;
    119 		} else if (state == 0 || iflag == 0 || ! rootshell)
    120 			exitshell(2);
    121 		reset();
    122 		if (exception == EXINT
    123 #if ATTY
    124 		 && (! attyset() || equal(termval(), "emacs"))
    125 #endif
    126 		 ) {
    127 			out2c('\n');
    128 			flushout(&errout);
    129 		}
    130 		popstackmark(&smark);
    131 		FORCEINTON;				/* enable interrupts */
    132 		if (state == 1)
    133 			goto state1;
    134 		else if (state == 2)
    135 			goto state2;
    136 		else if (state == 3)
    137 			goto state3;
    138 		else
    139 			goto state4;
    140 	}
    141 	handler = &jmploc;
    142 #ifdef DEBUG
    143 	opentrace();
    144 	trputs("Shell args:  ");  trargs(argv);
    145 #endif
    146 	rootpid = getpid();
    147 	rootshell = 1;
    148 	init();
    149 	setstackmark(&smark);
    150 	procargs(argc, argv);
    151 	if (argv[0] && argv[0][0] == '-') {
    152 		state = 1;
    153 		read_profile("/etc/profile");
    154 state1:
    155 		state = 2;
    156 		read_profile(".profile");
    157 	}
    158 state2:
    159 	state = 3;
    160 	if ((shinit = lookupvar("ENV")) != NULL &&
    161 	     *shinit != '\0') {
    162 		state = 3;
    163 		read_profile(shinit);
    164 	}
    165 state3:
    166 	state = 4;
    167 	if (minusc) {
    168 		evalstring(minusc);
    169 	}
    170 	if (sflag || minusc == NULL) {
    171 state4:	/* XXX ??? - why isn't this before the "if" statement */
    172 		cmdloop(1);
    173 	}
    174 #if PROFILE
    175 	monitor(0);
    176 #endif
    177 	exitshell(exitstatus);
    178 }
    179 
    180 
    181 /*
    182  * Read and execute commands.  "Top" is nonzero for the top level command
    183  * loop; it turns on prompting if the shell is interactive.
    184  */
    185 
    186 void
    187 cmdloop(top) {
    188 	union node *n;
    189 	struct stackmark smark;
    190 	int inter;
    191 	int numeof = 0;
    192 
    193 	TRACE(("cmdloop(%d) called\n", top));
    194 	setstackmark(&smark);
    195 	for (;;) {
    196 		if (pendingsigs)
    197 			dotrap();
    198 		inter = 0;
    199 		if (iflag && top) {
    200 			inter++;
    201 			showjobs(1);
    202 			chkmail(0);
    203 			flushout(&output);
    204 		}
    205 		n = parsecmd(inter);
    206 		/* showtree(n); DEBUG */
    207 		if (n == NEOF) {
    208 			if (!top || numeof >= 50)
    209 				break;
    210 			if (!stoppedjobs()) {
    211 				if (!Iflag)
    212 					break;
    213 				out2str("\nUse \"exit\" to leave shell.\n");
    214 			}
    215 			numeof++;
    216 		} else if (n != NULL && nflag == 0) {
    217 			job_warning = (job_warning == 2) ? 1 : 0;
    218 			numeof = 0;
    219 			evaltree(n, 0);
    220 		}
    221 		popstackmark(&smark);
    222 	}
    223 	popstackmark(&smark);		/* unnecessary */
    224 }
    225 
    226 
    227 
    228 /*
    229  * Read /etc/profile or .profile.  Return on error.
    230  */
    231 
    232 STATIC void
    233 read_profile(name)
    234 	char *name;
    235 	{
    236 	int fd;
    237 
    238 	INTOFF;
    239 	if ((fd = open(name, O_RDONLY)) >= 0)
    240 		setinputfd(fd, 1);
    241 	INTON;
    242 	if (fd < 0)
    243 		return;
    244 	cmdloop(0);
    245 	popfile();
    246 }
    247 
    248 
    249 
    250 /*
    251  * Read a file containing shell functions.
    252  */
    253 
    254 void
    255 readcmdfile(name)
    256 	char *name;
    257 	{
    258 	int fd;
    259 
    260 	INTOFF;
    261 	if ((fd = open(name, O_RDONLY)) >= 0)
    262 		setinputfd(fd, 1);
    263 	else
    264 		error("Can't open %s", name);
    265 	INTON;
    266 	cmdloop(0);
    267 	popfile();
    268 }
    269 
    270 
    271 
    272 /*
    273  * Take commands from a file.  To be compatable we should do a path
    274  * search for the file, which is necessary to find sub-commands.
    275  */
    276 
    277 
    278 static char *
    279 find_dot_file(basename) char *basename; {
    280 	static char localname[FILENAME_MAX+1];
    281 	char *fullname;
    282 	char *path = pathval();
    283 	struct stat statb;
    284 
    285 	/* don't try this for absolute or relative paths */
    286 	if( strchr(basename, '/'))
    287 		return basename;
    288 
    289 	while ((fullname = padvance(&path, basename)) != NULL) {
    290 		strcpy(localname, fullname);
    291 		stunalloc(fullname);
    292 		if ((stat(fullname, &statb) == 0) && S_ISREG(statb.st_mode))
    293 			return localname;
    294 	}
    295 	return basename;
    296 }
    297 
    298 dotcmd(argc, argv)  char **argv; {
    299 	exitstatus = 0;
    300 	if (argc >= 2) {		/* That's what SVR2 does */
    301 		char *fullname = find_dot_file(argv[1]);
    302 		setinputfile(fullname, 1);
    303 		commandname = fullname;
    304 		cmdloop(0);
    305 		popfile();
    306 	}
    307 	return exitstatus;
    308 }
    309 
    310 
    311 exitcmd(argc, argv)  char **argv; {
    312 	if (stoppedjobs())
    313 		return;
    314 	if (argc > 1)
    315 		exitstatus = number(argv[1]);
    316 	exitshell(exitstatus);
    317 }
    318 
    319 
    320 #ifdef notdef
    321 /*
    322  * Should never be called.
    323  */
    324 
    325 void
    326 exit(exitstatus) {
    327 	_exit(exitstatus);
    328 }
    329 #endif
    330