1994-05-26 06:18:55 +00:00
|
|
|
/*-
|
|
|
|
* Copyright (c) 1991, 1993
|
|
|
|
* The Regents of the University of California. All rights reserved.
|
|
|
|
*
|
|
|
|
* 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.
|
|
|
|
* 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.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifndef lint
|
1996-12-14 06:20:03 +00:00
|
|
|
static char const copyright[] =
|
1994-05-26 06:18:55 +00:00
|
|
|
"@(#) Copyright (c) 1991, 1993\n\
|
|
|
|
The Regents of the University of California. All rights reserved.\n";
|
|
|
|
#endif /* not lint */
|
|
|
|
|
|
|
|
#ifndef lint
|
1998-05-18 06:44:24 +00:00
|
|
|
#if 0
|
|
|
|
static char sccsid[] = "@(#)main.c 8.6 (Berkeley) 5/28/95";
|
|
|
|
#endif
|
1994-05-26 06:18:55 +00:00
|
|
|
#endif /* not lint */
|
2002-06-30 05:15:05 +00:00
|
|
|
#include <sys/cdefs.h>
|
|
|
|
__FBSDID("$FreeBSD$");
|
1994-05-26 06:18:55 +00:00
|
|
|
|
1996-09-01 10:22:36 +00:00
|
|
|
#include <stdio.h>
|
1994-05-26 06:18:55 +00:00
|
|
|
#include <signal.h>
|
1996-09-01 10:22:36 +00:00
|
|
|
#include <sys/stat.h>
|
|
|
|
#include <unistd.h>
|
1994-05-26 06:18:55 +00:00
|
|
|
#include <fcntl.h>
|
1996-08-11 22:51:00 +00:00
|
|
|
#include <locale.h>
|
2000-04-14 06:03:39 +00:00
|
|
|
#include <errno.h>
|
1996-09-01 10:22:36 +00:00
|
|
|
|
1994-05-26 06:18:55 +00:00
|
|
|
#include "shell.h"
|
|
|
|
#include "main.h"
|
|
|
|
#include "mail.h"
|
|
|
|
#include "options.h"
|
|
|
|
#include "output.h"
|
|
|
|
#include "parser.h"
|
|
|
|
#include "nodes.h"
|
1996-09-01 10:22:36 +00:00
|
|
|
#include "expand.h"
|
1994-05-26 06:18:55 +00:00
|
|
|
#include "eval.h"
|
|
|
|
#include "jobs.h"
|
|
|
|
#include "input.h"
|
|
|
|
#include "trap.h"
|
|
|
|
#include "var.h"
|
1996-09-01 10:22:36 +00:00
|
|
|
#include "show.h"
|
1994-05-26 06:18:55 +00:00
|
|
|
#include "memalloc.h"
|
|
|
|
#include "error.h"
|
|
|
|
#include "init.h"
|
|
|
|
#include "mystring.h"
|
1996-09-01 10:22:36 +00:00
|
|
|
#include "exec.h"
|
1996-12-14 06:20:03 +00:00
|
|
|
#include "cd.h"
|
1994-05-26 06:18:55 +00:00
|
|
|
|
|
|
|
int rootpid;
|
|
|
|
int rootshell;
|
2009-12-25 20:21:35 +00:00
|
|
|
struct jmploc main_handler;
|
2011-05-08 17:40:10 +00:00
|
|
|
int localeisutf8, initial_localeisutf8;
|
1994-05-26 06:18:55 +00:00
|
|
|
|
2011-06-10 22:42:00 +00:00
|
|
|
static void read_profile(char *);
|
2010-10-13 22:18:03 +00:00
|
|
|
static char *find_dot_file(char *);
|
1994-05-26 06:18:55 +00: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.
|
|
|
|
*/
|
|
|
|
|
1996-09-01 10:22:36 +00:00
|
|
|
int
|
2002-02-02 06:50:57 +00:00
|
|
|
main(int argc, char *argv[])
|
1996-09-01 10:22:36 +00:00
|
|
|
{
|
2011-06-10 22:42:00 +00:00
|
|
|
struct stackmark smark, smark2;
|
1994-05-26 06:18:55 +00:00
|
|
|
volatile int state;
|
|
|
|
char *shinit;
|
|
|
|
|
1996-08-11 22:51:00 +00:00
|
|
|
(void) setlocale(LC_ALL, "");
|
2011-05-08 17:40:10 +00:00
|
|
|
initcharset();
|
1994-05-26 06:18:55 +00:00
|
|
|
state = 0;
|
2009-12-25 20:21:35 +00:00
|
|
|
if (setjmp(main_handler.loc)) {
|
1996-12-14 06:20:03 +00:00
|
|
|
switch (exception) {
|
|
|
|
case EXEXEC:
|
|
|
|
exitstatus = exerrno;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case EXERROR:
|
|
|
|
exitstatus = 2;
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2011-04-23 22:28:56 +00:00
|
|
|
if (state == 0 || iflag == 0 || ! rootshell ||
|
|
|
|
exception == EXEXIT)
|
2011-02-04 22:47:55 +00:00
|
|
|
exitshell(exitstatus);
|
1994-05-26 06:18:55 +00:00
|
|
|
reset();
|
2010-11-20 14:14:52 +00:00
|
|
|
if (exception == EXINT)
|
|
|
|
out2fmt_flush("\n");
|
1994-05-26 06:18:55 +00:00
|
|
|
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;
|
|
|
|
}
|
2009-12-25 20:21:35 +00:00
|
|
|
handler = &main_handler;
|
1994-05-26 06:18:55 +00:00
|
|
|
#ifdef DEBUG
|
|
|
|
opentrace();
|
|
|
|
trputs("Shell args: "); trargs(argv);
|
|
|
|
#endif
|
|
|
|
rootpid = getpid();
|
|
|
|
rootshell = 1;
|
|
|
|
init();
|
|
|
|
setstackmark(&smark);
|
2011-06-10 22:42:00 +00:00
|
|
|
setstackmark(&smark2);
|
1994-05-26 06:18:55 +00:00
|
|
|
procargs(argc, argv);
|
2010-04-17 14:35:46 +00:00
|
|
|
pwd_init(iflag);
|
2010-02-06 22:57:24 +00:00
|
|
|
if (iflag)
|
|
|
|
chkmail(1);
|
1994-05-26 06:18:55 +00:00
|
|
|
if (argv[0] && argv[0][0] == '-') {
|
|
|
|
state = 1;
|
|
|
|
read_profile("/etc/profile");
|
|
|
|
state1:
|
|
|
|
state = 2;
|
1996-10-29 03:12:51 +00:00
|
|
|
if (privileged == 0)
|
|
|
|
read_profile(".profile");
|
|
|
|
else
|
|
|
|
read_profile("/etc/suid_profile");
|
1995-05-30 00:07:29 +00:00
|
|
|
}
|
1994-05-26 06:18:55 +00:00
|
|
|
state2:
|
|
|
|
state = 3;
|
1997-05-05 03:45:17 +00:00
|
|
|
if (!privileged && iflag) {
|
1996-09-01 10:22:36 +00:00
|
|
|
if ((shinit = lookupvar("ENV")) != NULL && *shinit != '\0') {
|
|
|
|
state = 3;
|
|
|
|
read_profile(shinit);
|
|
|
|
}
|
1994-05-26 06:18:55 +00:00
|
|
|
}
|
|
|
|
state3:
|
|
|
|
state = 4;
|
2011-06-10 22:42:00 +00:00
|
|
|
popstackmark(&smark2);
|
1994-05-26 06:18:55 +00:00
|
|
|
if (minusc) {
|
2009-06-13 21:17:45 +00:00
|
|
|
evalstring(minusc, sflag ? 0 : EV_EXIT);
|
1994-05-26 06:18:55 +00:00
|
|
|
}
|
|
|
|
if (sflag || minusc == NULL) {
|
|
|
|
state4: /* XXX ??? - why isn't this before the "if" statement */
|
|
|
|
cmdloop(1);
|
|
|
|
}
|
|
|
|
exitshell(exitstatus);
|
1996-09-01 10:22:36 +00:00
|
|
|
/*NOTREACHED*/
|
|
|
|
return 0;
|
1994-05-26 06:18:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Read and execute commands. "Top" is nonzero for the top level command
|
|
|
|
* loop; it turns on prompting if the shell is interactive.
|
|
|
|
*/
|
|
|
|
|
|
|
|
void
|
2002-02-02 06:50:57 +00:00
|
|
|
cmdloop(int top)
|
1996-09-01 10:22:36 +00:00
|
|
|
{
|
1994-05-26 06:18:55 +00:00
|
|
|
union node *n;
|
|
|
|
struct stackmark smark;
|
|
|
|
int inter;
|
|
|
|
int numeof = 0;
|
|
|
|
|
|
|
|
TRACE(("cmdloop(%d) called\n", top));
|
|
|
|
setstackmark(&smark);
|
|
|
|
for (;;) {
|
|
|
|
if (pendingsigs)
|
|
|
|
dotrap();
|
|
|
|
inter = 0;
|
|
|
|
if (iflag && top) {
|
|
|
|
inter++;
|
2006-10-07 16:51:16 +00:00
|
|
|
showjobs(1, SHOWJOBS_DEFAULT);
|
1994-05-26 06:18:55 +00:00
|
|
|
chkmail(0);
|
|
|
|
flushout(&output);
|
|
|
|
}
|
|
|
|
n = parsecmd(inter);
|
|
|
|
/* showtree(n); DEBUG */
|
|
|
|
if (n == NEOF) {
|
|
|
|
if (!top || numeof >= 50)
|
|
|
|
break;
|
|
|
|
if (!stoppedjobs()) {
|
|
|
|
if (!Iflag)
|
|
|
|
break;
|
2009-11-21 14:28:32 +00:00
|
|
|
out2fmt_flush("\nUse \"exit\" to leave shell.\n");
|
1994-05-26 06:18:55 +00:00
|
|
|
}
|
|
|
|
numeof++;
|
|
|
|
} else if (n != NULL && nflag == 0) {
|
|
|
|
job_warning = (job_warning == 2) ? 1 : 0;
|
|
|
|
numeof = 0;
|
|
|
|
evaltree(n, 0);
|
|
|
|
}
|
|
|
|
popstackmark(&smark);
|
2000-08-16 10:39:43 +00:00
|
|
|
setstackmark(&smark);
|
2010-08-15 21:06:53 +00:00
|
|
|
if (evalskip != 0) {
|
|
|
|
if (evalskip == SKIPFILE)
|
|
|
|
evalskip = 0;
|
1996-12-14 06:20:03 +00:00
|
|
|
break;
|
|
|
|
}
|
1994-05-26 06:18:55 +00:00
|
|
|
}
|
2000-08-16 10:39:43 +00:00
|
|
|
popstackmark(&smark);
|
1994-05-26 06:18:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Read /etc/profile or .profile. Return on error.
|
|
|
|
*/
|
|
|
|
|
2010-10-13 22:18:03 +00:00
|
|
|
static void
|
2011-06-10 22:42:00 +00:00
|
|
|
read_profile(char *name)
|
2002-02-02 06:50:57 +00:00
|
|
|
{
|
1994-05-26 06:18:55 +00:00
|
|
|
int fd;
|
2011-06-10 22:42:00 +00:00
|
|
|
const char *expandedname;
|
1994-05-26 06:18:55 +00:00
|
|
|
|
2011-06-10 22:42:00 +00:00
|
|
|
expandedname = expandstr(name);
|
|
|
|
if (expandedname == NULL)
|
|
|
|
return;
|
1994-05-26 06:18:55 +00:00
|
|
|
INTOFF;
|
2011-06-10 22:42:00 +00:00
|
|
|
if ((fd = open(expandedname, O_RDONLY)) >= 0)
|
1994-05-26 06:18:55 +00:00
|
|
|
setinputfd(fd, 1);
|
|
|
|
INTON;
|
|
|
|
if (fd < 0)
|
|
|
|
return;
|
|
|
|
cmdloop(0);
|
|
|
|
popfile();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Read a file containing shell functions.
|
|
|
|
*/
|
|
|
|
|
|
|
|
void
|
2009-12-24 18:41:14 +00:00
|
|
|
readcmdfile(const char *name)
|
1996-09-01 10:22:36 +00:00
|
|
|
{
|
1994-05-26 06:18:55 +00:00
|
|
|
int fd;
|
|
|
|
|
|
|
|
INTOFF;
|
|
|
|
if ((fd = open(name, O_RDONLY)) >= 0)
|
|
|
|
setinputfd(fd, 1);
|
|
|
|
else
|
2011-06-04 15:05:52 +00:00
|
|
|
error("cannot open %s: %s", name, strerror(errno));
|
1994-05-26 06:18:55 +00:00
|
|
|
INTON;
|
|
|
|
cmdloop(0);
|
|
|
|
popfile();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
1999-05-08 10:22:15 +00:00
|
|
|
* Take commands from a file. To be compatible we should do a path
|
1996-09-01 10:22:36 +00:00
|
|
|
* search for the file, which is necessary to find sub-commands.
|
1994-05-26 06:18:55 +00:00
|
|
|
*/
|
|
|
|
|
1996-09-01 10:22:36 +00:00
|
|
|
|
2010-10-13 22:18:03 +00:00
|
|
|
static char *
|
2002-02-02 06:50:57 +00:00
|
|
|
find_dot_file(char *basename)
|
1996-09-01 10:22:36 +00:00
|
|
|
{
|
|
|
|
char *fullname;
|
2009-12-24 18:41:14 +00:00
|
|
|
const char *path = pathval();
|
1996-09-01 10:22:36 +00:00
|
|
|
struct stat statb;
|
|
|
|
|
|
|
|
/* don't try this for absolute or relative paths */
|
|
|
|
if( strchr(basename, '/'))
|
|
|
|
return basename;
|
|
|
|
|
|
|
|
while ((fullname = padvance(&path, basename)) != NULL) {
|
2011-05-22 12:12:28 +00: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;
|
|
|
|
}
|
1996-09-01 10:22:36 +00:00
|
|
|
stunalloc(fullname);
|
|
|
|
}
|
|
|
|
return basename;
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
2002-02-02 06:50:57 +00:00
|
|
|
dotcmd(int argc, char **argv)
|
1996-09-01 10:22:36 +00:00
|
|
|
{
|
2010-05-28 22:40:24 +00:00
|
|
|
char *filename, *fullname;
|
2006-04-02 18:51:32 +00:00
|
|
|
|
|
|
|
if (argc < 2)
|
|
|
|
error("missing filename");
|
|
|
|
|
1994-05-26 06:18:55 +00:00
|
|
|
exitstatus = 0;
|
1996-09-01 10:22:36 +00:00
|
|
|
|
2010-05-28 22:40:24 +00:00
|
|
|
/*
|
|
|
|
* Because we have historically not supported any options,
|
|
|
|
* only treat "--" specially.
|
|
|
|
*/
|
|
|
|
filename = argc > 2 && strcmp(argv[1], "--") == 0 ? argv[2] : argv[1];
|
|
|
|
|
|
|
|
fullname = find_dot_file(filename);
|
2006-04-02 18:51:32 +00:00
|
|
|
setinputfile(fullname, 1);
|
|
|
|
commandname = fullname;
|
|
|
|
cmdloop(0);
|
|
|
|
popfile();
|
1994-05-26 06:18:55 +00:00
|
|
|
return exitstatus;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
1996-09-01 10:22:36 +00:00
|
|
|
int
|
2002-02-02 06:50:57 +00:00
|
|
|
exitcmd(int argc, char **argv)
|
1996-09-01 10:22:36 +00:00
|
|
|
{
|
1994-05-26 06:18:55 +00:00
|
|
|
if (stoppedjobs())
|
1996-09-01 10:22:36 +00:00
|
|
|
return 0;
|
1996-12-14 06:20:03 +00:00
|
|
|
if (argc > 1)
|
2011-01-08 23:08:13 +00:00
|
|
|
exitshell(number(argv[1]));
|
1996-12-14 06:20:03 +00:00
|
|
|
else
|
2011-01-08 23:08:13 +00:00
|
|
|
exitshell_savedstatus();
|
1994-05-26 06:18:55 +00:00
|
|
|
}
|