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