NetBSD/bin/sh/main.c

412 lines
8.5 KiB
C
Raw Normal View History

/* $NetBSD: main.c,v 1.32 1999/02/05 21:21:27 christos Exp $ */
1995-03-21 12:01:59 +03:00
1993-03-21 12:45:37 +03:00
/*-
1994-05-11 21:09:42 +04:00
* Copyright (c) 1991, 1993
* The Regents of the University of California. All rights reserved.
1993-03-21 12:45:37 +03:00
*
* This code is derived from software contributed to Berkeley by
* Kenneth Almquist.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. All advertising materials mentioning features or use of this software
* must display the following acknowledgement:
* This product includes software developed by the University of
* California, Berkeley and its contributors.
* 4. Neither the name of the University nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*/
1997-07-05 01:01:48 +04:00
#include <sys/cdefs.h>
1993-03-21 12:45:37 +03:00
#ifndef lint
1997-07-05 01:01:48 +04:00
__COPYRIGHT("@(#) Copyright (c) 1991, 1993\n\
The Regents of the University of California. All rights reserved.\n");
1993-03-21 12:45:37 +03:00
#endif /* not lint */
#ifndef lint
1995-03-21 12:01:59 +03:00
#if 0
static char sccsid[] = "@(#)main.c 8.7 (Berkeley) 7/19/95";
1995-03-21 12:01:59 +03:00
#else
__RCSID("$NetBSD: main.c,v 1.32 1999/02/05 21:21:27 christos Exp $");
1995-03-21 12:01:59 +03:00
#endif
1993-03-21 12:45:37 +03:00
#endif /* not lint */
1994-05-11 21:54:32 +04:00
#include <stdio.h>
1993-03-21 12:45:37 +03:00
#include <signal.h>
1994-05-11 21:54:32 +04:00
#include <sys/stat.h>
#include <unistd.h>
1993-03-21 12:45:37 +03:00
#include <fcntl.h>
1993-03-21 12:45:37 +03:00
#include "shell.h"
#include "main.h"
#include "mail.h"
#include "options.h"
#include "output.h"
#include "parser.h"
#include "nodes.h"
#include "expand.h"
1993-03-21 12:45:37 +03:00
#include "eval.h"
#include "jobs.h"
#include "input.h"
#include "trap.h"
#include "var.h"
#include "show.h"
1993-03-21 12:45:37 +03:00
#include "memalloc.h"
#include "error.h"
#include "init.h"
#include "mystring.h"
1994-05-11 21:54:32 +04:00
#include "exec.h"
#include "cd.h"
1993-03-21 12:45:37 +03:00
#define PROFILE 0
int rootpid;
int rootshell;
STATIC union node *curcmd;
STATIC union node *prevcmd;
extern int errno;
#if PROFILE
short profile_buf[16384];
extern int etext();
#endif
STATIC void read_profile __P((char *));
STATIC char *find_dot_file __P((char *));
1997-07-05 01:01:48 +04:00
int main __P((int, char **));
1993-03-21 12:45:37 +03:00
/*
* Main routine. We initialize things, parse the arguments, execute
* profiles if we're a login shell, and then call cmdloop to execute
* commands. The setjmp call sets up the location to jump to when an
* exception occurs. When an exception occurs the variable "state"
* is used to figure out how far we had gotten.
*/
int
main(argc, argv)
int argc;
char **argv;
{
1993-03-21 12:45:37 +03:00
struct jmploc jmploc;
struct stackmark smark;
volatile int state;
char *shinit;
#if PROFILE
monitor(4, etext, profile_buf, sizeof profile_buf, 50);
#endif
state = 0;
if (setjmp(jmploc.loc)) {
/*
* When a shell procedure is executed, we raise the
* exception EXSHELLPROC to clean up before executing
* the shell procedure.
*/
switch (exception) {
case EXSHELLPROC:
1993-03-21 12:45:37 +03:00
rootpid = getpid();
rootshell = 1;
minusc = NULL;
state = 3;
break;
case EXEXEC:
exitstatus = exerrno;
break;
case EXERROR:
exitstatus = 2;
break;
default:
break;
}
if (exception != EXSHELLPROC) {
if (state == 0 || iflag == 0 || ! rootshell)
exitshell(exitstatus);
}
1993-03-21 12:45:37 +03:00
reset();
if (exception == EXINT
1994-05-11 21:09:42 +04:00
#if ATTY
&& (! attyset() || equal(termval(), "emacs"))
1993-03-21 12:45:37 +03:00
#endif
1994-05-11 21:09:42 +04:00
) {
1993-03-21 12:45:37 +03:00
out2c('\n');
flushout(&errout);
}
popstackmark(&smark);
FORCEINTON; /* enable interrupts */
if (state == 1)
goto state1;
else if (state == 2)
goto state2;
1994-05-11 21:09:42 +04:00
else if (state == 3)
1993-03-21 12:45:37 +03:00
goto state3;
1994-05-11 21:09:42 +04:00
else
goto state4;
1993-03-21 12:45:37 +03:00
}
handler = &jmploc;
#ifdef DEBUG
opentrace();
trputs("Shell args: "); trargs(argv);
#endif
rootpid = getpid();
rootshell = 1;
init();
setstackmark(&smark);
procargs(argc, argv);
if (argv[0] && argv[0][0] == '-') {
state = 1;
read_profile("/etc/profile");
state1:
state = 2;
read_profile(".profile");
}
1993-03-21 12:45:37 +03:00
state2:
state = 3;
if (getuid() == geteuid() && getgid() == getegid()) {
if ((shinit = lookupvar("ENV")) != NULL && *shinit != '\0') {
state = 3;
read_profile(shinit);
}
1994-05-11 21:09:42 +04:00
}
state3:
state = 4;
1993-03-21 12:45:37 +03:00
if (minusc) {
static int sigs[] = {
SIGINT, SIGQUIT, SIGHUP,
#ifdef SIGTSTP
SIGTSTP,
#endif
SIGPIPE
};
#define SIGSSIZE (sizeof(sigs)/sizeof(sigs[0]))
int i;
for (i = 0; i < SIGSSIZE; i++)
setsignal(sigs[i]);
1993-03-21 12:45:37 +03:00
evalstring(minusc);
}
if (sflag || minusc == NULL) {
1994-05-11 21:09:42 +04:00
state4: /* XXX ??? - why isn't this before the "if" statement */
1993-03-21 12:45:37 +03:00
cmdloop(1);
}
#if PROFILE
monitor(0);
#endif
exitshell(exitstatus);
/* NOTREACHED */
1993-03-21 12:45:37 +03:00
}
/*
* Read and execute commands. "Top" is nonzero for the top level command
* loop; it turns on prompting if the shell is interactive.
*/
void
cmdloop(top)
int top;
{
1993-03-21 12:45:37 +03:00
union node *n;
struct stackmark smark;
int inter;
1994-05-11 21:09:42 +04:00
int numeof = 0;
1993-03-21 12:45:37 +03:00
TRACE(("cmdloop(%d) called\n", top));
setstackmark(&smark);
for (;;) {
if (pendingsigs)
dotrap();
inter = 0;
if (iflag && top) {
inter++;
showjobs(1);
chkmail(0);
flushout(&output);
}
n = parsecmd(inter);
1994-05-11 21:09:42 +04:00
/* showtree(n); DEBUG */
1993-03-21 12:45:37 +03:00
if (n == NEOF) {
1994-05-11 21:09:42 +04:00
if (!top || numeof >= 50)
1993-03-21 12:45:37 +03:00
break;
1994-05-11 21:09:42 +04:00
if (!stoppedjobs()) {
if (!Iflag)
break;
out2str("\nUse \"exit\" to leave shell.\n");
}
1993-03-21 12:45:37 +03:00
numeof++;
} else if (n != NULL && nflag == 0) {
1994-05-11 21:09:42 +04:00
job_warning = (job_warning == 2) ? 1 : 0;
numeof = 0;
1993-03-21 12:45:37 +03:00
evaltree(n, 0);
}
popstackmark(&smark);
if (evalskip == SKIPFILE) {
evalskip = 0;
break;
}
1993-03-21 12:45:37 +03:00
}
popstackmark(&smark); /* unnecessary */
}
/*
* Read /etc/profile or .profile. Return on error.
*/
STATIC void
read_profile(name)
char *name;
{
1993-03-21 12:45:37 +03:00
int fd;
int xflag_set = 0;
int vflag_set = 0;
1993-03-21 12:45:37 +03:00
INTOFF;
if ((fd = open(name, O_RDONLY)) >= 0)
setinputfd(fd, 1);
INTON;
if (fd < 0)
return;
/* -q turns off -x and -v just when executing init files */
if (qflag) {
if (xflag)
xflag = 0, xflag_set = 1;
if (vflag)
vflag = 0, vflag_set = 1;
}
1993-03-21 12:45:37 +03:00
cmdloop(0);
if (qflag) {
if (xflag_set)
xflag = 1;
if (vflag_set)
vflag = 1;
}
1993-03-21 12:45:37 +03:00
popfile();
}
/*
* Read a file containing shell functions.
*/
void
readcmdfile(name)
char *name;
{
1993-03-21 12:45:37 +03:00
int fd;
INTOFF;
if ((fd = open(name, O_RDONLY)) >= 0)
setinputfd(fd, 1);
else
error("Can't open %s", name);
INTON;
cmdloop(0);
popfile();
}
/*
* Take commands from a file. To be compatable we should do a path
* search for the file, which is necessary to find sub-commands.
1993-03-21 12:45:37 +03:00
*/
STATIC char *
find_dot_file(basename)
char *basename;
{
char *fullname;
char *path = pathval();
struct stat statb;
/* don't try this for absolute or relative paths */
if (strchr(basename, '/'))
return basename;
while ((fullname = padvance(&path, basename)) != NULL) {
if ((stat(fullname, &statb) == 0) && S_ISREG(statb.st_mode)) {
/*
* Don't bother freeing here, since it will
* be freed by the caller.
*/
return fullname;
}
stunalloc(fullname);
}
/* not found in the PATH */
error("%s: not found", basename);
/* NOTREACHED */
}
int
dotcmd(argc, argv)
int argc;
char **argv;
{
struct strlist *sp;
1993-03-21 12:45:37 +03:00
exitstatus = 0;
for (sp = cmdenviron; sp ; sp = sp->next)
setvareq(savestr(sp->text), VSTRFIXED|VTEXTFIXED);
1993-03-21 12:45:37 +03:00
if (argc >= 2) { /* That's what SVR2 does */
char *fullname;
struct stackmark smark;
setstackmark(&smark);
fullname = find_dot_file(argv[1]);
setinputfile(fullname, 1);
commandname = fullname;
1993-03-21 12:45:37 +03:00
cmdloop(0);
popfile();
popstackmark(&smark);
1993-03-21 12:45:37 +03:00
}
return exitstatus;
}
int
exitcmd(argc, argv)
int argc;
char **argv;
{
extern int oexitstatus;
1994-05-11 21:09:42 +04:00
if (stoppedjobs())
return 0;
1993-03-21 12:45:37 +03:00
if (argc > 1)
exitstatus = number(argv[1]);
else
exitstatus = oexitstatus;
1993-03-21 12:45:37 +03:00
exitshell(exitstatus);
/* NOTREACHED */
1993-03-21 12:45:37 +03:00
}