2014-08-03 23:10:41 +02:00
|
|
|
/* $NetBSD: main.c,v 1.57 2011/06/18 21:18:46 christos Exp $ */
|
|
|
|
|
2005-04-21 16:53:53 +02:00
|
|
|
/*-
|
2006-05-23 14:59:34 +02:00
|
|
|
* Copyright (c) 1991, 1993
|
|
|
|
* The Regents of the University of California. All rights reserved.
|
2005-04-21 16:53:53 +02: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.
|
2014-08-03 23:10:41 +02:00
|
|
|
* 3. Neither the name of the University nor the names of its contributors
|
2005-04-21 16:53:53 +02:00
|
|
|
* 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.
|
|
|
|
*/
|
|
|
|
|
2014-08-03 23:10:41 +02:00
|
|
|
#include <sys/cdefs.h>
|
2005-04-21 16:53:53 +02:00
|
|
|
#ifndef lint
|
2014-08-03 23:10:41 +02:00
|
|
|
__COPYRIGHT("@(#) Copyright (c) 1991, 1993\
|
|
|
|
The Regents of the University of California. All rights reserved.");
|
2005-04-21 16:53:53 +02:00
|
|
|
#endif /* not lint */
|
|
|
|
|
|
|
|
#ifndef lint
|
2006-05-23 14:59:34 +02:00
|
|
|
#if 0
|
2014-08-03 23:10:41 +02:00
|
|
|
static char sccsid[] = "@(#)main.c 8.7 (Berkeley) 7/19/95";
|
|
|
|
#else
|
|
|
|
__RCSID("$NetBSD: main.c,v 1.57 2011/06/18 21:18:46 christos Exp $");
|
2006-05-23 14:59:34 +02:00
|
|
|
#endif
|
2005-04-21 16:53:53 +02:00
|
|
|
#endif /* not lint */
|
|
|
|
|
2014-08-03 23:10:41 +02:00
|
|
|
#include <errno.h>
|
2006-05-23 14:59:34 +02:00
|
|
|
#include <stdio.h>
|
2005-04-21 16:53:53 +02:00
|
|
|
#include <signal.h>
|
2006-05-23 14:59:34 +02:00
|
|
|
#include <sys/stat.h>
|
|
|
|
#include <unistd.h>
|
2014-08-03 23:10:41 +02:00
|
|
|
#include <stdlib.h>
|
2006-05-23 14:59:34 +02:00
|
|
|
#include <locale.h>
|
2014-08-03 23:10:41 +02:00
|
|
|
#include <fcntl.h>
|
|
|
|
|
2006-05-23 14:59:34 +02:00
|
|
|
|
2005-04-21 16:53:53 +02:00
|
|
|
#include "shell.h"
|
|
|
|
#include "main.h"
|
|
|
|
#include "mail.h"
|
|
|
|
#include "options.h"
|
2014-08-03 23:10:41 +02:00
|
|
|
#include "builtins.h"
|
2005-04-21 16:53:53 +02:00
|
|
|
#include "output.h"
|
|
|
|
#include "parser.h"
|
|
|
|
#include "nodes.h"
|
2006-05-23 14:59:34 +02:00
|
|
|
#include "expand.h"
|
2005-04-21 16:53:53 +02:00
|
|
|
#include "eval.h"
|
|
|
|
#include "jobs.h"
|
|
|
|
#include "input.h"
|
|
|
|
#include "trap.h"
|
|
|
|
#include "var.h"
|
2006-05-23 14:59:34 +02:00
|
|
|
#include "show.h"
|
2005-04-21 16:53:53 +02:00
|
|
|
#include "memalloc.h"
|
|
|
|
#include "error.h"
|
|
|
|
#include "init.h"
|
|
|
|
#include "mystring.h"
|
2006-05-23 14:59:34 +02:00
|
|
|
#include "exec.h"
|
|
|
|
#include "cd.h"
|
2014-08-03 23:10:41 +02:00
|
|
|
|
|
|
|
#define PROFILE 0
|
2005-04-21 16:53:53 +02:00
|
|
|
|
|
|
|
int rootpid;
|
|
|
|
int rootshell;
|
2014-08-03 23:10:41 +02:00
|
|
|
int posix;
|
|
|
|
#if PROFILE
|
|
|
|
short profile_buf[16384];
|
|
|
|
extern int etext();
|
|
|
|
#endif
|
2005-04-21 16:53:53 +02:00
|
|
|
|
2014-08-03 23:10:41 +02:00
|
|
|
STATIC void read_profile(const char *);
|
2006-05-23 14:59:34 +02:00
|
|
|
STATIC char *find_dot_file(char *);
|
2014-08-03 23:10:41 +02:00
|
|
|
int main(int, char **);
|
2005-04-21 16:53:53 +02: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.
|
|
|
|
*/
|
|
|
|
|
2006-05-23 14:59:34 +02:00
|
|
|
int
|
2014-08-03 23:10:41 +02:00
|
|
|
main(int argc, char **argv)
|
2006-05-23 14:59:34 +02:00
|
|
|
{
|
2005-04-21 16:53:53 +02:00
|
|
|
struct jmploc jmploc;
|
|
|
|
struct stackmark smark;
|
|
|
|
volatile int state;
|
2006-05-23 14:59:34 +02:00
|
|
|
char *shinit;
|
2005-04-21 16:53:53 +02:00
|
|
|
|
2014-08-03 23:10:41 +02:00
|
|
|
setlocale(LC_ALL, "");
|
|
|
|
|
|
|
|
posix = getenv("POSIXLY_CORRECT") != NULL;
|
|
|
|
#if PROFILE
|
|
|
|
monitor(4, etext, profile_buf, sizeof profile_buf, 50);
|
|
|
|
#endif
|
2005-04-21 16:53:53 +02:00
|
|
|
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.
|
|
|
|
*/
|
2006-05-23 14:59:34 +02:00
|
|
|
switch (exception) {
|
|
|
|
case EXSHELLPROC:
|
2005-04-21 16:53:53 +02:00
|
|
|
rootpid = getpid();
|
|
|
|
rootshell = 1;
|
|
|
|
minusc = NULL;
|
|
|
|
state = 3;
|
2006-05-23 14:59:34 +02:00
|
|
|
break;
|
|
|
|
|
|
|
|
case EXEXEC:
|
|
|
|
exitstatus = exerrno;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case EXERROR:
|
|
|
|
exitstatus = 2;
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (exception != EXSHELLPROC) {
|
2014-08-03 23:10:41 +02:00
|
|
|
if (state == 0 || iflag == 0 || ! rootshell)
|
|
|
|
exitshell(exitstatus);
|
2006-05-23 14:59:34 +02:00
|
|
|
}
|
2005-04-21 16:53:53 +02:00
|
|
|
reset();
|
2014-08-03 23:10:41 +02:00
|
|
|
if (exception == EXINT
|
|
|
|
#if ATTY
|
|
|
|
&& (! attyset() || equal(termval(), "emacs"))
|
|
|
|
#endif
|
|
|
|
) {
|
2005-04-21 16:53:53 +02:00
|
|
|
out2c('\n');
|
|
|
|
flushout(&errout);
|
|
|
|
}
|
|
|
|
popstackmark(&smark);
|
|
|
|
FORCEINTON; /* enable interrupts */
|
|
|
|
if (state == 1)
|
|
|
|
goto state1;
|
|
|
|
else if (state == 2)
|
|
|
|
goto state2;
|
|
|
|
else if (state == 3)
|
|
|
|
goto state3;
|
|
|
|
else
|
|
|
|
goto state4;
|
|
|
|
}
|
|
|
|
handler = &jmploc;
|
2014-08-03 23:10:41 +02:00
|
|
|
#ifdef DEBUG
|
|
|
|
#if DEBUG == 2
|
|
|
|
debug = 1;
|
|
|
|
#endif
|
2005-04-21 16:53:53 +02:00
|
|
|
opentrace();
|
|
|
|
trputs("Shell args: "); trargs(argv);
|
|
|
|
#endif
|
|
|
|
rootpid = getpid();
|
|
|
|
rootshell = 1;
|
|
|
|
init();
|
2014-08-03 23:10:41 +02:00
|
|
|
initpwd();
|
2005-04-21 16:53:53 +02:00
|
|
|
setstackmark(&smark);
|
|
|
|
procargs(argc, argv);
|
|
|
|
if (argv[0] && argv[0][0] == '-') {
|
|
|
|
state = 1;
|
|
|
|
read_profile("/etc/profile");
|
|
|
|
state1:
|
|
|
|
state = 2;
|
2014-08-03 23:10:41 +02:00
|
|
|
read_profile(".profile");
|
2005-04-21 16:53:53 +02:00
|
|
|
}
|
|
|
|
state2:
|
|
|
|
state = 3;
|
2014-08-03 23:10:41 +02:00
|
|
|
if ((iflag || !posix) &&
|
|
|
|
getuid() == geteuid() && getgid() == getegid()) {
|
2006-05-23 14:59:34 +02:00
|
|
|
if ((shinit = lookupvar("ENV")) != NULL && *shinit != '\0') {
|
|
|
|
state = 3;
|
|
|
|
read_profile(shinit);
|
2005-04-21 16:53:53 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
state3:
|
|
|
|
state = 4;
|
2014-08-03 23:10:41 +02:00
|
|
|
if (sflag == 0 || minusc) {
|
|
|
|
static int sigs[] = {
|
|
|
|
SIGINT, SIGQUIT, SIGHUP,
|
|
|
|
#ifdef SIGTSTP
|
|
|
|
SIGTSTP,
|
|
|
|
#endif
|
|
|
|
SIGPIPE
|
|
|
|
};
|
|
|
|
#define SIGSSIZE (sizeof(sigs)/sizeof(sigs[0]))
|
|
|
|
size_t i;
|
|
|
|
|
|
|
|
for (i = 0; i < SIGSSIZE; i++)
|
|
|
|
setsignal(sigs[i], 0);
|
2005-04-21 16:53:53 +02:00
|
|
|
}
|
2014-08-03 23:10:41 +02:00
|
|
|
|
|
|
|
if (minusc)
|
|
|
|
evalstring(minusc, 0);
|
|
|
|
|
2005-04-21 16:53:53 +02:00
|
|
|
if (sflag || minusc == NULL) {
|
2006-05-23 14:59:34 +02:00
|
|
|
state4: /* XXX ??? - why isn't this before the "if" statement */
|
2005-04-21 16:53:53 +02:00
|
|
|
cmdloop(1);
|
|
|
|
}
|
2014-08-03 23:10:41 +02:00
|
|
|
#if PROFILE
|
|
|
|
monitor(0);
|
|
|
|
#endif
|
2005-04-21 16:53:53 +02:00
|
|
|
exitshell(exitstatus);
|
2014-08-03 23:10:41 +02:00
|
|
|
/* NOTREACHED */
|
2005-04-21 16:53:53 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Read and execute commands. "Top" is nonzero for the top level command
|
|
|
|
* loop; it turns on prompting if the shell is interactive.
|
|
|
|
*/
|
|
|
|
|
|
|
|
void
|
2006-05-23 14:59:34 +02:00
|
|
|
cmdloop(int top)
|
|
|
|
{
|
2005-04-21 16:53:53 +02:00
|
|
|
union node *n;
|
|
|
|
struct stackmark smark;
|
|
|
|
int inter;
|
2006-05-23 14:59:34 +02:00
|
|
|
int numeof = 0;
|
2005-04-21 16:53:53 +02:00
|
|
|
|
|
|
|
TRACE(("cmdloop(%d) called\n", top));
|
|
|
|
setstackmark(&smark);
|
|
|
|
for (;;) {
|
|
|
|
if (pendingsigs)
|
|
|
|
dotrap();
|
|
|
|
inter = 0;
|
2014-08-03 23:10:41 +02:00
|
|
|
if (iflag == 1 && top) {
|
|
|
|
inter = 1;
|
|
|
|
showjobs(out2, SHOW_CHANGED);
|
2005-04-21 16:53:53 +02:00
|
|
|
chkmail(0);
|
2014-08-03 23:10:41 +02:00
|
|
|
flushout(&errout);
|
2005-04-21 16:53:53 +02:00
|
|
|
}
|
|
|
|
n = parsecmd(inter);
|
2006-05-23 14:59:34 +02:00
|
|
|
/* showtree(n); DEBUG */
|
2005-04-21 16:53:53 +02:00
|
|
|
if (n == NEOF) {
|
2006-05-23 14:59:34 +02:00
|
|
|
if (!top || numeof >= 50)
|
2005-04-21 16:53:53 +02:00
|
|
|
break;
|
2006-05-23 14:59:34 +02:00
|
|
|
if (!stoppedjobs()) {
|
|
|
|
if (!Iflag)
|
|
|
|
break;
|
|
|
|
out2str("\nUse \"exit\" to leave shell.\n");
|
|
|
|
}
|
2005-04-21 16:53:53 +02:00
|
|
|
numeof++;
|
|
|
|
} else if (n != NULL && nflag == 0) {
|
2006-05-23 14:59:34 +02:00
|
|
|
job_warning = (job_warning == 2) ? 1 : 0;
|
|
|
|
numeof = 0;
|
2005-04-21 16:53:53 +02:00
|
|
|
evaltree(n, 0);
|
|
|
|
}
|
|
|
|
popstackmark(&smark);
|
2006-05-23 14:59:34 +02:00
|
|
|
setstackmark(&smark);
|
|
|
|
if (evalskip == SKIPFILE) {
|
|
|
|
evalskip = 0;
|
|
|
|
break;
|
|
|
|
}
|
2005-04-21 16:53:53 +02:00
|
|
|
}
|
2006-05-23 14:59:34 +02:00
|
|
|
popstackmark(&smark);
|
2005-04-21 16:53:53 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Read /etc/profile or .profile. Return on error.
|
|
|
|
*/
|
|
|
|
|
|
|
|
STATIC void
|
2014-08-03 23:10:41 +02:00
|
|
|
read_profile(const char *name)
|
2006-05-23 14:59:34 +02:00
|
|
|
{
|
2005-04-21 16:53:53 +02:00
|
|
|
int fd;
|
2014-08-03 23:10:41 +02:00
|
|
|
int xflag_set = 0;
|
|
|
|
int vflag_set = 0;
|
2005-04-21 16:53:53 +02:00
|
|
|
|
|
|
|
INTOFF;
|
|
|
|
if ((fd = open(name, O_RDONLY)) >= 0)
|
|
|
|
setinputfd(fd, 1);
|
|
|
|
INTON;
|
|
|
|
if (fd < 0)
|
|
|
|
return;
|
2014-08-03 23:10:41 +02:00
|
|
|
/* -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;
|
|
|
|
}
|
2005-04-21 16:53:53 +02:00
|
|
|
cmdloop(0);
|
2014-08-03 23:10:41 +02:00
|
|
|
if (qflag) {
|
|
|
|
if (xflag_set)
|
|
|
|
xflag = 1;
|
|
|
|
if (vflag_set)
|
|
|
|
vflag = 1;
|
|
|
|
}
|
2005-04-21 16:53:53 +02:00
|
|
|
popfile();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Read a file containing shell functions.
|
|
|
|
*/
|
|
|
|
|
|
|
|
void
|
2006-05-23 14:59:34 +02:00
|
|
|
readcmdfile(char *name)
|
|
|
|
{
|
2005-04-21 16:53:53 +02:00
|
|
|
int fd;
|
|
|
|
|
|
|
|
INTOFF;
|
|
|
|
if ((fd = open(name, O_RDONLY)) >= 0)
|
|
|
|
setinputfd(fd, 1);
|
|
|
|
else
|
2014-08-03 23:10:41 +02:00
|
|
|
error("Can't open %s", name);
|
2005-04-21 16:53:53 +02:00
|
|
|
INTON;
|
|
|
|
cmdloop(0);
|
|
|
|
popfile();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2006-05-23 14:59:34 +02:00
|
|
|
|
2005-04-21 16:53:53 +02:00
|
|
|
/*
|
2006-05-23 14:59:34 +02:00
|
|
|
* Take commands from a file. To be compatible we should do a path
|
|
|
|
* search for the file, which is necessary to find sub-commands.
|
2005-04-21 16:53:53 +02:00
|
|
|
*/
|
|
|
|
|
2006-05-23 14:59:34 +02:00
|
|
|
|
|
|
|
STATIC char *
|
|
|
|
find_dot_file(char *basename)
|
|
|
|
{
|
|
|
|
char *fullname;
|
2014-08-03 23:10:41 +02:00
|
|
|
const char *path = pathval();
|
2006-05-23 14:59:34 +02:00
|
|
|
struct stat statb;
|
|
|
|
|
|
|
|
/* don't try this for absolute or relative paths */
|
2014-08-03 23:10:41 +02:00
|
|
|
if (strchr(basename, '/'))
|
2006-05-23 14:59:34 +02:00
|
|
|
return basename;
|
|
|
|
|
|
|
|
while ((fullname = padvance(&path, basename)) != NULL) {
|
2014-08-03 23:10:41 +02:00
|
|
|
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;
|
|
|
|
}
|
2006-05-23 14:59:34 +02:00
|
|
|
stunalloc(fullname);
|
|
|
|
}
|
2014-08-03 23:10:41 +02:00
|
|
|
|
|
|
|
/* not found in the PATH */
|
|
|
|
error("%s: not found", basename);
|
|
|
|
/* NOTREACHED */
|
2006-05-23 14:59:34 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
dotcmd(int argc, char **argv)
|
|
|
|
{
|
2005-04-21 16:53:53 +02:00
|
|
|
exitstatus = 0;
|
2006-05-23 14:59:34 +02:00
|
|
|
|
2005-04-21 16:53:53 +02:00
|
|
|
if (argc >= 2) { /* That's what SVR2 does */
|
2014-08-03 23:10:41 +02:00
|
|
|
char *fullname;
|
|
|
|
struct stackmark smark;
|
2006-05-23 14:59:34 +02:00
|
|
|
|
2014-08-03 23:10:41 +02:00
|
|
|
setstackmark(&smark);
|
|
|
|
fullname = find_dot_file(argv[1]);
|
2006-05-23 14:59:34 +02:00
|
|
|
setinputfile(fullname, 1);
|
|
|
|
commandname = fullname;
|
2005-04-21 16:53:53 +02:00
|
|
|
cmdloop(0);
|
|
|
|
popfile();
|
2014-08-03 23:10:41 +02:00
|
|
|
popstackmark(&smark);
|
2005-04-21 16:53:53 +02:00
|
|
|
}
|
|
|
|
return exitstatus;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2006-05-23 14:59:34 +02:00
|
|
|
int
|
|
|
|
exitcmd(int argc, char **argv)
|
|
|
|
{
|
|
|
|
if (stoppedjobs())
|
|
|
|
return 0;
|
2005-04-21 16:53:53 +02:00
|
|
|
if (argc > 1)
|
|
|
|
exitstatus = number(argv[1]);
|
|
|
|
exitshell(exitstatus);
|
2014-08-03 23:10:41 +02:00
|
|
|
/* NOTREACHED */
|
2005-04-21 16:53:53 +02:00
|
|
|
}
|