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