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