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