Home | History | Annotate | Line # | Download | only in sh
main.c revision 1.60
      1 /*	$NetBSD: main.c,v 1.60 2016/02/27 18:34:12 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. Neither the name of the University nor the names of its contributors
     19  *    may be used to endorse or promote products derived from this software
     20  *    without specific prior written permission.
     21  *
     22  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     23  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     24  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     25  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     26  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     27  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     28  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     29  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     30  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     31  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     32  * SUCH DAMAGE.
     33  */
     34 
     35 #include <sys/cdefs.h>
     36 #ifndef lint
     37 __COPYRIGHT("@(#) Copyright (c) 1991, 1993\
     38  The Regents of the University of California.  All rights reserved.");
     39 #endif /* not lint */
     40 
     41 #ifndef lint
     42 #if 0
     43 static char sccsid[] = "@(#)main.c	8.7 (Berkeley) 7/19/95";
     44 #else
     45 __RCSID("$NetBSD: main.c,v 1.60 2016/02/27 18:34:12 christos Exp $");
     46 #endif
     47 #endif /* not lint */
     48 
     49 #include <errno.h>
     50 #include <stdio.h>
     51 #include <signal.h>
     52 #include <sys/stat.h>
     53 #include <unistd.h>
     54 #include <stdlib.h>
     55 #include <locale.h>
     56 #include <fcntl.h>
     57 
     58 
     59 #include "shell.h"
     60 #include "main.h"
     61 #include "mail.h"
     62 #include "options.h"
     63 #include "builtins.h"
     64 #include "output.h"
     65 #include "parser.h"
     66 #include "nodes.h"
     67 #include "expand.h"
     68 #include "eval.h"
     69 #include "jobs.h"
     70 #include "input.h"
     71 #include "trap.h"
     72 #include "var.h"
     73 #include "show.h"
     74 #include "memalloc.h"
     75 #include "error.h"
     76 #include "init.h"
     77 #include "mystring.h"
     78 #include "exec.h"
     79 #include "cd.h"
     80 
     81 #define PROFILE 0
     82 
     83 int rootpid;
     84 int rootshell;
     85 int posix;
     86 #if PROFILE
     87 short profile_buf[16384];
     88 extern int etext();
     89 #endif
     90 
     91 STATIC void read_profile(const char *);
     92 int main(int, 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(int argc, char **argv)
    104 {
    105 	struct jmploc jmploc;
    106 	struct stackmark smark;
    107 	volatile int state;
    108 	char *shinit;
    109 	uid_t uid;
    110 	gid_t gid;
    111 
    112 	uid = getuid();
    113 	gid = getgid();
    114 
    115 	setlocale(LC_ALL, "");
    116 
    117 	posix = getenv("POSIXLY_CORRECT") != NULL;
    118 #if PROFILE
    119 	monitor(4, etext, profile_buf, sizeof profile_buf, 50);
    120 #endif
    121 	state = 0;
    122 	if (setjmp(jmploc.loc)) {
    123 		/*
    124 		 * When a shell procedure is executed, we raise the
    125 		 * exception EXSHELLPROC to clean up before executing
    126 		 * the shell procedure.
    127 		 */
    128 		switch (exception) {
    129 		case EXSHELLPROC:
    130 			rootpid = getpid();
    131 			rootshell = 1;
    132 			minusc = NULL;
    133 			state = 3;
    134 			break;
    135 
    136 		case EXEXEC:
    137 			exitstatus = exerrno;
    138 			break;
    139 
    140 		case EXERROR:
    141 			exitstatus = 2;
    142 			break;
    143 
    144 		default:
    145 			break;
    146 		}
    147 
    148 		if (exception != EXSHELLPROC) {
    149 			if (state == 0 || iflag == 0 || ! rootshell)
    150 				exitshell(exitstatus);
    151 		}
    152 		reset();
    153 		if (exception == EXINT
    154 #if ATTY
    155 		 && (! attyset() || equal(termval(), "emacs"))
    156 #endif
    157 		 ) {
    158 			out2c('\n');
    159 			flushout(&errout);
    160 		}
    161 		popstackmark(&smark);
    162 		FORCEINTON;				/* enable interrupts */
    163 		if (state == 1)
    164 			goto state1;
    165 		else if (state == 2)
    166 			goto state2;
    167 		else if (state == 3)
    168 			goto state3;
    169 		else
    170 			goto state4;
    171 	}
    172 	handler = &jmploc;
    173 #ifdef DEBUG
    174 #if DEBUG == 2
    175 	debug = 1;
    176 #endif
    177 	opentrace();
    178 	trputs("Shell args:  ");  trargs(argv);
    179 #endif
    180 	rootpid = getpid();
    181 	rootshell = 1;
    182 	init();
    183 	initpwd();
    184 	setstackmark(&smark);
    185 	procargs(argc, argv);
    186 
    187 	/*
    188 	 * Limit bogus system(3) or popen(3) calls in setuid binaries,
    189 	 * by requiring the -p flag
    190 	 */
    191 	if (!pflag && (uid != geteuid() || gid != getegid())) {
    192 		setuid(uid);
    193 		setgid(gid);
    194 		/* PS1 might need to be changed accordingly. */
    195 		choose_ps1();
    196 	}
    197 
    198 	if (argv[0] && argv[0][0] == '-') {
    199 		state = 1;
    200 		read_profile("/etc/profile");
    201 state1:
    202 		state = 2;
    203 		read_profile(".profile");
    204 	}
    205 state2:
    206 	state = 3;
    207 	if ((iflag || !posix) &&
    208 	    getuid() == geteuid() && getgid() == getegid()) {
    209 		if ((shinit = lookupvar("ENV")) != NULL && *shinit != '\0') {
    210 			state = 3;
    211 			read_profile(shinit);
    212 		}
    213 	}
    214 state3:
    215 	state = 4;
    216 	if (sflag == 0 || minusc) {
    217 		static int sigs[] =  {
    218 		    SIGINT, SIGQUIT, SIGHUP,
    219 #ifdef SIGTSTP
    220 		    SIGTSTP,
    221 #endif
    222 		    SIGPIPE
    223 		};
    224 #define SIGSSIZE (sizeof(sigs)/sizeof(sigs[0]))
    225 		size_t i;
    226 
    227 		for (i = 0; i < SIGSSIZE; i++)
    228 		    setsignal(sigs[i], 0);
    229 	}
    230 
    231 	if (minusc)
    232 		evalstring(minusc, 0);
    233 
    234 	if (sflag || minusc == NULL) {
    235 state4:	/* XXX ??? - why isn't this before the "if" statement */
    236 		cmdloop(1);
    237 	}
    238 #if PROFILE
    239 	monitor(0);
    240 #endif
    241 	exitshell(exitstatus);
    242 	/* NOTREACHED */
    243 }
    244 
    245 
    246 /*
    247  * Read and execute commands.  "Top" is nonzero for the top level command
    248  * loop; it turns on prompting if the shell is interactive.
    249  */
    250 
    251 void
    252 cmdloop(int top)
    253 {
    254 	union node *n;
    255 	struct stackmark smark;
    256 	int inter;
    257 	int numeof = 0;
    258 	enum skipstate skip;
    259 
    260 	TRACE(("cmdloop(%d) called\n", top));
    261 	setstackmark(&smark);
    262 	for (;;) {
    263 		if (pendingsigs)
    264 			dotrap();
    265 		inter = 0;
    266 		if (iflag == 1 && top) {
    267 			inter = 1;
    268 			showjobs(out2, SHOW_CHANGED);
    269 			chkmail(0);
    270 			flushout(&errout);
    271 		}
    272 		n = parsecmd(inter);
    273 		TRACE(("cmdloop: "); showtree(n));
    274 		/* showtree(n); DEBUG */
    275 		if (n == NEOF) {
    276 			if (!top || numeof >= 50)
    277 				break;
    278 			if (!stoppedjobs()) {
    279 				if (!Iflag)
    280 					break;
    281 				out2str("\nUse \"exit\" to leave shell.\n");
    282 			}
    283 			numeof++;
    284 		} else if (n != NULL && nflag == 0) {
    285 			job_warning = (job_warning == 2) ? 1 : 0;
    286 			numeof = 0;
    287 			evaltree(n, 0);
    288 		}
    289 		popstackmark(&smark);
    290 		setstackmark(&smark);
    291 
    292 		/*
    293 		 * Any SKIP* can occur here!  SKIP(FUNC|BREAK|CONT) occur when
    294 		 * a dotcmd is in a loop or a function body and appropriate
    295 		 * built-ins occurs in file scope in the sourced file.  Values
    296 		 * other than SKIPFILE are reset by the appropriate eval*()
    297 		 * that contained the dotcmd() call.
    298 		 */
    299 		skip = current_skipstate();
    300 		if (skip != SKIPNONE) {
    301 			if (skip == SKIPFILE)
    302 				stop_skipping();
    303 			break;
    304 		}
    305 	}
    306 	popstackmark(&smark);
    307 }
    308 
    309 
    310 
    311 /*
    312  * Read /etc/profile or .profile.  Return on error.
    313  */
    314 
    315 STATIC void
    316 read_profile(const char *name)
    317 {
    318 	int fd;
    319 	int xflag_set = 0;
    320 	int vflag_set = 0;
    321 
    322 	INTOFF;
    323 	if ((fd = open(name, O_RDONLY)) >= 0)
    324 		setinputfd(fd, 1);
    325 	INTON;
    326 	if (fd < 0)
    327 		return;
    328 	/* -q turns off -x and -v just when executing init files */
    329 	if (qflag)  {
    330 	    if (xflag)
    331 		    xflag = 0, xflag_set = 1;
    332 	    if (vflag)
    333 		    vflag = 0, vflag_set = 1;
    334 	}
    335 	cmdloop(0);
    336 	if (qflag)  {
    337 	    if (xflag_set)
    338 		    xflag = 1;
    339 	    if (vflag_set)
    340 		    vflag = 1;
    341 	}
    342 	popfile();
    343 }
    344 
    345 
    346 
    347 /*
    348  * Read a file containing shell functions.
    349  */
    350 
    351 void
    352 readcmdfile(char *name)
    353 {
    354 	int fd;
    355 
    356 	INTOFF;
    357 	if ((fd = open(name, O_RDONLY)) >= 0)
    358 		setinputfd(fd, 1);
    359 	else
    360 		error("Can't open %s", name);
    361 	INTON;
    362 	cmdloop(0);
    363 	popfile();
    364 }
    365 
    366 
    367 
    368 int
    369 exitcmd(int argc, char **argv)
    370 {
    371 	if (stoppedjobs())
    372 		return 0;
    373 	if (argc > 1)
    374 		exitstatus = number(argv[1]);
    375 	exitshell(exitstatus);
    376 	/* NOTREACHED */
    377 }
    378