#define EXTERN
#include "sh.h"
#ifndef lint
char copyright[] =
"@(#) Copyright (c) 1991 The Regents of the University of California.\n\
All rights reserved.\n";
#endif
RCSID("$tcsh: sh.c,v 3.145 2009/06/25 21:15:37 christos Exp $")
#include "tc.h"
#include "ed.h"
#include "tw.h"
extern int MapsAreInited;
extern int NLSMapsAreInited;
jmp_buf_t reslab INIT_ZERO_STRUCT;
static const char tcshstr[] = "tcsh";
struct sigaction parintr;
struct sigaction parterm;
#ifdef TESLA
int do_logout = 0;
#endif
int use_fork = 0;
static Char INVCHAR;
Char *INVPTR = &INVCHAR;
Char **INVPPTR = &INVPTR;
static int fast = 0;
static int mflag = 0;
static int prompt = 1;
int enterhist = 0;
int tellwhat = 0;
time_t t_period;
Char *ffile = NULL;
int dolzero = 0;
int insource = 0;
int exitset = 0;
static time_t chktim;
char *progname;
int tcsh;
struct saved_state {
int insource;
int OLDSTD;
int SHIN;
int SHOUT;
int SHDIAG;
int intty;
struct whyle *whyles;
Char *gointr;
Char *arginp;
Char *evalp;
Char **evalvec;
Char *alvecp;
Char **alvec;
int onelflg;
int enterhist;
Char **argv;
Char **av;
Char HIST;
int cantell;
struct Bin B;
int justpr;
};
static int srccat (Char *, Char *);
#ifndef WINNT_NATIVE
static int srcfile (const char *, int, int, Char **);
#else
int srcfile (const char *, int, int, Char **);
#endif
static void srcunit (int, int, int, Char **);
static void mailchk (void);
#ifndef _PATH_DEFPATH
static Char **defaultpath (void);
#endif
static void record (void);
static void st_save (struct saved_state *, int, int,
Char **, Char **);
static void st_restore (void *);
int main (int, char **);
int
main(int argc, char **argv)
{
int batch = 0;
volatile int nexececho = 0;
int nofile = 0;
volatile int nverbose = 0;
volatile int rdirs = 0;
int quitit = 0;
Char *cp;
#ifdef AUTOLOGOUT
Char *cp2;
#endif
char *tcp, *ttyn;
int f, reenter;
char **tempv;
int osetintr;
struct sigaction oparintr;
#ifdef WINNT_NATIVE
nt_init();
#endif
#if defined(NLS_CATALOGS) && defined(LC_MESSAGES)
(void) setlocale(LC_MESSAGES, "");
#endif
#ifdef NLS
# ifdef LC_CTYPE
(void) setlocale(LC_CTYPE, "");
# endif
#endif
nlsinit();
#ifdef MALLOC_TRACE
mal_setstatsfile(fdopen(dmove(xopen("/tmp/tcsh.trace",
O_WRONLY|O_CREAT|O_LARGEFILE, 0666), 25), "w"));
mal_trace(1);
#endif
#if !(defined(BSDTIMES) || defined(_SEQUENT_)) && defined(POSIX)
# ifdef _SC_CLK_TCK
clk_tck = (clock_t) sysconf(_SC_CLK_TCK);
# else
# ifdef CLK_TCK
clk_tck = CLK_TCK;
# else
clk_tck = HZ;
# endif
# endif
#endif
settimes();
#ifdef TESLA
do_logout = 0;
#endif
{
do
if ((f = xopen(_PATH_DEVNULL, O_RDONLY|O_LARGEFILE)) == -1 &&
(f = xopen("/", O_RDONLY|O_LARGEFILE)) == -1)
exit(1);
while (f < 3);
xclose(f);
}
osinit();
{
char *t;
t = strrchr(argv[0], '/');
#ifdef WINNT_NATIVE
{
char *s = strrchr(argv[0], '\\');
if (s)
t = s;
}
#endif
t = t ? t + 1 : argv[0];
if (*t == '-') t++;
progname = strsave((t && *t) ? t : tcshstr);
tcsh = strcmp(progname, tcshstr) == 0;
}
#ifdef _PATH_BSHELL
STR_BSHELL = SAVE(_PATH_BSHELL);
#endif
#ifdef _PATH_TCSHELL
STR_SHELLPATH = SAVE(_PATH_TCSHELL);
#else
# ifdef _PATH_CSHELL
STR_SHELLPATH = SAVE(_PATH_CSHELL);
# endif
#endif
STR_environ = blk2short(environ);
environ = short2blk(STR_environ);
STR_WORD_CHARS = SAVE(WORD_CHARS);
HIST = '!';
HISTSUB = '^';
PRCH = '>';
PRCHROOT = '#';
word_chars = STR_WORD_CHARS;
bslash_quote = 0;
setcopy(STRhistory, str2short("100"), VAR_READWRITE);
tempv = argv;
ffile = SAVE(tempv[0]);
dolzero = 0;
if (eq(ffile, STRaout))
quitit = 1;
uid = getuid();
gid = getgid();
euid = geteuid();
egid = getegid();
loginsh = (**tempv == '-') || (argc == 2 &&
tempv[1][0] == '-' && tempv[1][1] == 'l' &&
tempv[1][2] == '\0');
#ifdef _VMS_POSIX
if (!loginsh) {
loginsh = (argc == 1 && getppid() == 1);
**tempv = '-';
}
#endif
if (loginsh && **tempv != '-') {
char *argv0;
tempv[1][0] = '\0';
tempv[1][1] = '\0';
tempv[1] = NULL;
argv0 = strspl("-", *tempv);
*tempv = argv0;
argc--;
}
if (loginsh) {
(void) time(&chktim);
setNS(STRloginsh);
}
AsciiOnly = 1;
NoNLSRebind = getenv("NOREBIND") != NULL;
#ifdef NLS
# ifdef SETLOCALEBUG
dont_free = 1;
# endif
(void) setlocale(LC_ALL, "");
# ifdef LC_COLLATE
(void) setlocale(LC_COLLATE, "");
# endif
# ifdef SETLOCALEBUG
dont_free = 0;
# endif
# ifdef STRCOLLBUG
fix_strcoll_bug();
# endif
{
int k;
for (k = 0200; k <= 0377 && !Isprint(CTL_ESC(k)); k++)
continue;
AsciiOnly = MB_CUR_MAX == 1 && k > 0377;
}
#else
AsciiOnly = getenv("LANG") == NULL && getenv("LC_CTYPE") == NULL;
#endif
if (MapsAreInited && !NLSMapsAreInited)
ed_InitNLSMaps();
ResetArrowKeys();
(void) time(&t_period);
#ifndef HAVENOUTMP
initwatch();
#endif
#if defined(alliant)
if (loginsh && isatty(SHIN)) {
ttyn = ttyname(SHIN);
xclose(SHIN);
SHIN = xopen(ttyn, O_RDWR|O_LARGEFILE);
shpgrp = getpid();
(void) ioctl (SHIN, TIOCSPGRP, (ioctl_t) &shpgrp);
(void) setpgid(0, shpgrp);
}
#endif
initdesc();
if ((ttyn = ttyname(SHIN)) != NULL) {
if (strncmp(ttyn, "/dev/", 5) == 0)
setv(STRtty, cp = SAVE(ttyn + 5), VAR_READWRITE);
else
setv(STRtty, cp = SAVE(ttyn), VAR_READWRITE);
}
else
setv(STRtty, cp = SAVE(""), VAR_READWRITE);
#ifdef AUTOLOGOUT
# ifdef convex
if (uid == 0)
setcopy(STRautologout, STRrootdefautologout, VAR_READWRITE);
else
if (loginsh)
setcopy(STRautologout, STR0, VAR_READWRITE);
# else
if (loginsh || (uid == 0)) {
if (*cp) {
if ((cp2 = Strrchr(cp, (Char) '/')) != NULL) {
cp2 = cp2 + 1;
}
else
cp2 = cp;
if (!(((Strncmp(cp2, STRtty, 3) == 0) && Isalpha(cp2[3])) ||
Strstr(cp, STRptssl) != NULL)) {
if (getenv("DISPLAY") == NULL) {
setcopy(STRautologout, STRdefautologout, VAR_READWRITE);
}
}
}
}
# endif
#endif
sigset_interrupting(SIGALRM, queue_alrmcatch);
setcopy(STRstatus, STR0, VAR_READWRITE);
getmachine();
#if ECHO_STYLE != BSD_ECHO
if (tcsh) {
# if ECHO_STYLE == NONE_ECHO
setcopy(STRecho_style, STRnone, VAR_READWRITE);
# endif
# if ECHO_STYLE == SYSV_ECHO
setcopy(STRecho_style, STRsysv, VAR_READWRITE);
# endif
# if ECHO_STYLE == BOTH_ECHO
setcopy(STRecho_style, STRboth, VAR_READWRITE);
# endif
} else
#endif
setcopy(STRecho_style, STRbsd, VAR_READWRITE);
shlvl(1);
if ((tcp = getenv("HOME")) != NULL)
cp = quote(SAVE(tcp));
else
cp = NULL;
if (cp == NULL)
fast = 1;
else
setv(STRhome, cp, VAR_READWRITE);
dinit(cp);
{
char *cln, *cus, *cgr;
struct passwd *pw;
struct group *gr;
#ifdef apollo
int oid = getoid();
setv(STRoid, Itoa(oid, 0, 0), VAR_READWRITE);
#endif
setv(STRuid, Itoa(uid, 0, 0), VAR_READWRITE);
setv(STRgid, Itoa(gid, 0, 0), VAR_READWRITE);
cln = getenv("LOGNAME");
cus = getenv("USER");
if (cus != NULL)
setv(STRuser, quote(SAVE(cus)), VAR_READWRITE);
else if (cln != NULL)
setv(STRuser, quote(SAVE(cln)), VAR_READWRITE);
else if ((pw = xgetpwuid(uid)) == NULL)
setcopy(STRuser, str2short("unknown"), VAR_READWRITE);
else
setcopy(STRuser, str2short(pw->pw_name), VAR_READWRITE);
if (cln == NULL)
tsetenv(STRLOGNAME, varval(STRuser));
if (cus == NULL)
tsetenv(STRKUSER, varval(STRuser));
cgr = getenv("GROUP");
if (cgr != NULL)
setv(STRgroup, quote(SAVE(cgr)), VAR_READWRITE);
else if ((gr = xgetgrgid(gid)) == NULL)
setcopy(STRgroup, str2short("unknown"), VAR_READWRITE);
else
setcopy(STRgroup, str2short(gr->gr_name), VAR_READWRITE);
if (cgr == NULL)
tsetenv(STRKGROUP, varval(STRgroup));
}
{
char cbuff[MAXHOSTNAMELEN];
if (gethostname(cbuff, sizeof(cbuff)) >= 0) {
cbuff[sizeof(cbuff) - 1] = '\0';
tsetenv(STRHOST, str2short(cbuff));
}
else
tsetenv(STRHOST, str2short("unknown"));
}
#ifdef REMOTEHOST
remotehost();
#endif
#ifdef apollo
if ((tcp = getenv("SYSTYPE")) == NULL)
tcp = "bsd4.3";
tsetenv(STRSYSTYPE, quote(str2short(tcp)));
#endif
if ((tcp = getenv("TERM")) != NULL) {
setv(STRterm, quote(SAVE(tcp)), VAR_READWRITE);
noediting = strcmp(tcp, "unknown") == 0 || strcmp(tcp, "dumb") == 0 ||
strcmp(tcp, "network") == 0;
editing = strcmp(tcp, "emacs") != 0 && strcmp(tcp, "wm") != 0 &&
!noediting;
}
else {
noediting = 0;
editing = ((tcp = getenv("EMACS")) == NULL || strcmp(tcp, "t") != 0);
}
if (editing)
setNS(STRedit);
setNS(STRaddsuffix);
setNS(STRcsubstnonl);
setcopy(STRkillring, str2short("30"), VAR_READWRITE);
if ((tcp = getenv("PATH")) == NULL)
#ifdef _PATH_DEFPATH
importpath(str2short(_PATH_DEFPATH));
#else
setq(STRpath, defaultpath(), &shvhed, VAR_READWRITE);
#endif
else
importpath(str2short(tcp));
{
size_t sh_len = 0;
if ((tcp = getenv("SHELL")) != NULL) {
sh_len = strlen(tcp);
if ((sh_len >= 5 && strcmp(tcp + (sh_len - 5), "/tcsh") == 0) ||
(!tcsh && sh_len >= 4 && strcmp(tcp + (sh_len - 4), "/csh") == 0))
setv(STRshell, quote(SAVE(tcp)), VAR_READWRITE);
else
sh_len = 0;
}
if (sh_len == 0)
setcopy(STRshell, STR_SHELLPATH, VAR_READWRITE);
}
#ifdef _OSD_POSIX
if ((tcp = getenv("PROGRAM_ENVIRONMENT")) == NULL)
tcp = "SHELL";
tsetenv(STRPROGRAM_ENVIRONMENT, quote(str2short(tcp)));
#endif
#ifdef COLOR_LS_F
if ((tcp = getenv("LS_COLORS")) != NULL)
parseLS_COLORS(str2short(tcp));
#endif
doldol = putn((int) getpid());
#ifdef WINNT_NATIVE
{
char *tmp;
Char *tmp2;
if ((tmp = getenv("TMP")) != NULL) {
tmp = xasprintf("%s/%s", tmp, "sh");
tmp2 = SAVE(tmp);
xfree(tmp);
}
else {
tmp2 = SAVE("");
}
shtemp = Strspl(tmp2, doldol);
xfree(tmp2);
}
#else
shtemp = Strspl(STRtmpsh, doldol);
#endif
sigaction(SIGINT, NULL, &parintr);
sigaction(SIGTERM, NULL, &parterm);
#ifdef TCF
(void) signal(SIGMIGRATE, SIG_DFL);
#endif
#if defined(DSPMBYTE)
#if defined(NLS) && defined(LC_CTYPE)
if (((tcp = setlocale(LC_CTYPE, NULL)) != NULL || (tcp = getenv("LANG")) != NULL) && !adrof(CHECK_MBYTEVAR)) {
#else
if ((tcp = getenv("LANG")) != NULL && !adrof(CHECK_MBYTEVAR)) {
#endif
autoset_dspmbyte(str2short(tcp));
}
#if defined(WINNT_NATIVE)
else if (!adrof(CHECK_MBYTEVAR))
nt_autoset_dspmbyte();
#endif
#endif
fix_version();
if (argc > 1 && strcmp(argv[1], "--version") == 0) {
xprintf("%S\n", varval(STRversion));
xexit(0);
}
if (argc > 1 && strcmp(argv[1], "--help") == 0) {
xprintf("%S\n\n", varval(STRversion));
xprintf("%s", CGETS(11, 8, HELP_STRING));
xexit(0);
}
argc--, tempv++;
while (argc > 0 && (tcp = tempv[0])[0] == '-' &&
*++tcp != '\0' && !batch) {
do
switch (*tcp++) {
case 0:
prompt = 0;
setintr = 1;
nofile = 1;
break;
case 'b':
batch = 1;
break;
case 'c':
if (argc == 1)
xexit(0);
argc--, tempv++;
#ifdef M_XENIX
if (!strcmp(argv[0], "sh"))
{
char *p;
for (p = tempv[0]; *p; ++p)
*p &= ASCII;
}
#endif
arginp = SAVE(tempv[0]);
if (arginp != NULL)
setv(STRcommand, quote(Strsave(arginp)), VAR_READWRITE);
{
int count;
cp = Strend(arginp);
count = 0;
while (cp > arginp && *--cp == '\\')
++count;
if ((count & 1) != 0) {
exiterr = 1;
stderror(ERR_ARGC);
}
}
prompt = 0;
nofile = 1;
break;
case 'd':
rdirs = 1;
break;
#ifdef apollo
case 'D':
{
Char *dp;
cp = str2short(tcp);
if (dp = Strchr(cp, '=')) {
*dp++ = '\0';
tsetenv(cp, dp);
}
else
tsetenv(cp, STRNULL);
}
*tcp = '\0';
break;
#endif
case 'e':
exiterr = 1;
break;
case 'f':
fast = 1;
break;
case 'i':
intact = 1;
nofile = 1;
break;
case 'm':
mflag = 1;
break;
case 'n':
noexec = 1;
break;
case 'q':
quitit = 1;
break;
case 's':
nofile = 1;
break;
case 't':
onelflg = 2;
prompt = 0;
nofile = 1;
break;
case 'v':
nverbose = 1;
break;
case 'x':
nexececho = 1;
break;
case 'V':
setNS(STRverbose);
break;
case 'X':
setNS(STRecho);
break;
case 'F':
use_fork = 1;
break;
case ' ':
case '\t':
break;
default:
exiterr = 1;
stderror(ERR_TCSHUSAGE, tcp-1, progname);
break;
} while (*tcp);
tempv++, argc--;
}
if (quitit)
(void) signal(SIGQUIT, SIG_DFL);
if (nofile == 0 && argc > 0) {
nofile = xopen(tempv[0], O_RDONLY|O_LARGEFILE);
if (nofile < 0) {
child = 1;
stderror(ERR_SYSTEM, tempv[0], strerror(errno));
}
xfree(ffile);
dolzero = 1;
ffile = SAVE(tempv[0]);
if ((SHIN = dmove(nofile, FSHIN)) < 0)
switch(nofile) {
case 0:
SHIN = FSHIN;
break;
case 1:
SHIN = FSHOUT;
break;
case 2:
SHIN = FSHDIAG;
break;
default:
stderror(ERR_SYSTEM, tempv[0], strerror(errno));
break;
}
(void) close_on_exec(SHIN, 1);
prompt = 0;
tempv++;
}
closem();
if (!(intty = isatty(SHIN))) {
if (adrof(STRedit))
unsetv(STRedit);
editing = 0;
}
intty |= intact;
#ifndef convex
if (intty || (intact && isatty(SHOUT))) {
if (!batch && (uid != euid || gid != egid)) {
errno = EACCES;
child = 1;
stderror(ERR_SYSTEM, progname, strerror(errno));
}
}
#endif
isoutatty = isatty(SHOUT);
isdiagatty = isatty(SHDIAG);
if (loginsh || intact || (intty && isatty(SHOUT)))
setintr = 1;
settell();
setq(STRargv, blk2short(tempv), &shvhed, VAR_READWRITE);
if (prompt) {
if (tcsh)
setcopy(STRprompt, STRdeftcshprompt, VAR_READWRITE);
else
setcopy(STRprompt, STRdefcshprompt, VAR_READWRITE);
setcopy(STRprompt2, STRmquestion, VAR_READWRITE);
setcopy(STRprompt3, STRKCORRECT, VAR_READWRITE);
}
shpgrp = mygetpgrp();
opgrp = tpgrp = -1;
if (setintr) {
struct sigaction osig;
**argv = '-';
if (!quitit)
(void) signal(SIGQUIT, SIG_IGN);
pintr_disabled = 1;
sigset_interrupting(SIGINT, queue_pintr);
(void) signal(SIGTERM, SIG_IGN);
sigaction(SIGHUP, NULL, &osig);
if (loginsh || osig.sa_handler != SIG_IGN)
sigset_interrupting(SIGHUP, queue_phup);
#ifdef SIGXCPU
sigaction(SIGXCPU, NULL, &osig);
if (loginsh || osig.sa_handler != SIG_IGN)
sigset_interrupting(SIGXCPU, queue_phup);
#endif
#ifdef SIGXFSZ
sigaction(SIGXFSZ, NULL, &osig);
if (loginsh || osig.sa_handler != SIG_IGN)
sigset_interrupting(SIGXFSZ, queue_phup);
#endif
if (quitit == 0 && arginp == 0) {
#ifdef SIGTSTP
(void) signal(SIGTSTP, SIG_IGN);
#endif
#ifdef SIGTTIN
(void) signal(SIGTTIN, SIG_IGN);
#endif
#ifdef SIGTTOU
(void) signal(SIGTTOU, SIG_IGN);
#endif
if (isatty(FSHDIAG))
f = FSHDIAG;
else if (isatty(FSHOUT))
f = FSHOUT;
else if (isatty(OLDSTD))
f = OLDSTD;
else
f = -1;
#ifdef NeXT
if (shpgrp == 0) {
shpgrp = getpid();
(void) setpgid(0, shpgrp);
(void) tcsetpgrp(f, shpgrp);
}
#endif
#ifdef BSDJOBS
if (grabpgrp(f, shpgrp) != -1) {
if (setdisc(f) != -1) {
opgrp = shpgrp;
shpgrp = getpid();
tpgrp = shpgrp;
if (tcsetpgrp(f, shpgrp) == -1) {
if (setpgid(0, shpgrp) == -1) {
xprintf("setpgid:");
goto notty;
}
if (tcsetpgrp(f, shpgrp) == -1) {
xprintf("tcsetpgrp:");
goto notty;
}
}
else if (shpgrp != mygetpgrp()) {
if(setpgid(0, shpgrp) == -1) {
xprintf("setpgid:");
goto notty;
}
}
#ifdef IRIS4D
else
(void) setpgid(0, shpgrp);
#endif
(void) close_on_exec(dcopy(f, FSHTTY), 1);
}
else
tpgrp = -1;
}
if (tpgrp == -1) {
notty:
xprintf(CGETS(11, 1, "Warning: no access to tty (%s).\n"),
strerror(errno));
xprintf("%s",
CGETS(11, 2, "Thus no job control in this shell.\n"));
if (adrof(STRedit))
unsetv(STRedit);
editing = 0;
}
#else
tpgrp = -1;
#endif
}
}
if (setintr == 0 && parintr.sa_handler == SIG_DFL)
setintr = 1;
sigset_interrupting(SIGCHLD, queue_pchild);
if (intty && !arginp)
(void) ed_Setup(editing);
osetintr = setintr;
oparintr = parintr;
(void)cleanup_push_mark();
if (setexit() != 0)
reenter = 1;
else
reenter = 0;
exitset++;
haderr = 0;
if (!fast && reenter == 0) {
{
pintr_disabled++;
cleanup_push(&pintr_disabled, disabled_cleanup);
setintr = 0;
parintr.sa_handler = SIG_IGN;
#ifdef LOGINFIRST
#ifdef _PATH_DOTLOGIN
if (loginsh)
(void) srcfile(_PATH_DOTLOGIN, 0, 0, NULL);
#endif
#endif
#ifdef _PATH_DOTCSHRC
(void) srcfile(_PATH_DOTCSHRC, 0, 0, NULL);
#endif
if (!arginp && !onelflg && !havhash)
dohash(NULL,NULL);
#ifndef LOGINFIRST
#ifdef _PATH_DOTLOGIN
if (loginsh)
(void) srcfile(_PATH_DOTLOGIN, 0, 0, NULL);
#endif
#endif
cleanup_until(&pintr_disabled);
setintr = osetintr;
parintr = oparintr;
}
#ifdef LOGINFIRST
if (loginsh)
(void) srccat(varval(STRhome), STRsldotlogin);
#endif
if (!srccat(varval(STRhome), STRsldottcshrc))
(void) srccat(varval(STRhome), STRsldotcshrc);
if (!arginp && !onelflg && !havhash)
dohash(NULL,NULL);
loadhist(NULL, 0);
#ifndef LOGINFIRST
if (loginsh)
(void) srccat(varval(STRhome), STRsldotlogin);
#endif
if (loginsh || rdirs)
loaddirs(NULL);
}
setintr = osetintr;
parintr = oparintr;
exitset--;
if (intty && !arginp) {
ed_Init();
#ifdef SIG_WINDOW
check_window_size(1);
#endif
}
if (nverbose)
setNS(STRverbose);
if (nexececho)
setNS(STRecho);
process(setintr);
if (intty) {
if (loginsh) {
xprintf("logout\n");
xclose(SHIN);
child = 1;
#ifdef TESLA
do_logout = 1;
#endif
goodbye(NULL, NULL);
}
else {
xprintf("exit\n");
}
}
record();
exitstat();
return (0);
}
void
untty(void)
{
#ifdef BSDJOBS
if (tpgrp > 0 && opgrp != shpgrp) {
(void) setpgid(0, opgrp);
(void) tcsetpgrp(FSHTTY, opgrp);
(void) resetdisc(FSHTTY);
}
#endif
}
void
importpath(Char *cp)
{
size_t i = 0;
Char *dp;
Char **pv;
int c;
for (dp = cp; *dp; dp++)
if (*dp == PATHSEP)
i++;
pv = xcalloc(i + 2, sizeof(Char *));
dp = cp;
i = 0;
if (*dp)
for (;;) {
if ((c = *dp) == PATHSEP || c == 0) {
*dp = 0;
pv[i++] = Strsave(*cp ? cp : STRdot);
if (c) {
cp = dp + 1;
*dp = PATHSEP;
}
else
break;
}
#ifdef WINNT_NATIVE
else if (*dp == '\\')
*dp = '/';
#endif
dp++;
}
pv[i] = 0;
cleanup_push(pv, blk_cleanup);
setq(STRpath, pv, &shvhed, VAR_READWRITE);
cleanup_ignore(pv);
cleanup_until(pv);
}
static int
srccat(Char *cp, Char *dp)
{
if (cp[0] == '/' && cp[1] == '\0')
return srcfile(short2str(dp), (mflag ? 0 : 1), 0, NULL);
else {
Char *ep;
char *ptr;
int rv;
#ifdef WINNT_NATIVE
ep = Strend(cp);
if (ep != cp && ep[-1] == '/' && dp[0] == '/')
dp++;
#endif
ep = Strspl(cp, dp);
cleanup_push(ep, xfree);
ptr = short2str(ep);
rv = srcfile(ptr, (mflag ? 0 : 1), 0, NULL);
cleanup_until(ep);
return rv;
}
}
#ifndef WINNT_NATIVE
static int
#else
int
#endif
srcfile(const char *f, int onlyown, int flag, Char **av)
{
int unit;
if ((unit = xopen(f, O_RDONLY|O_LARGEFILE)) == -1)
return 0;
cleanup_push(&unit, open_cleanup);
unit = dmove(unit, -1);
cleanup_ignore(&unit);
cleanup_until(&unit);
(void) close_on_exec(unit, 1);
srcunit(unit, onlyown, flag, av);
return 1;
}
static void
st_save(struct saved_state *st, int unit, int hflg, Char **al, Char **av)
{
st->insource = insource;
st->SHIN = SHIN;
#define NEED_SAVE_FD(d1,d2) \
(fstat(d1, &s1) != -1 && fstat(d2, &s2) != -1 \
&& (s1.st_ino != s2.st_ino || s1.st_dev != s2.st_dev) )
st->OLDSTD = st->SHOUT = st->SHDIAG = -1;
if (didfds) {
struct stat s1, s2;
if (NEED_SAVE_FD(0,OLDSTD)) {
st->OLDSTD = OLDSTD;
OLDSTD = dmove(0, -1);
(void)close_on_exec(OLDSTD, 1);
}
if (NEED_SAVE_FD(1,SHOUT)) {
st->SHOUT = SHOUT;
SHOUT = dmove(1, -1);
(void)close_on_exec(SHOUT, 1);
}
if (NEED_SAVE_FD(2,SHDIAG)) {
st->SHDIAG = SHDIAG;
SHDIAG = dmove(2, -1);
(void)close_on_exec(SHDIAG, 1);
}
donefds();
}
st->intty = intty;
st->whyles = whyles;
st->gointr = gointr;
st->arginp = arginp;
st->evalp = evalp;
st->evalvec = evalvec;
st->alvecp = alvecp;
st->alvec = alvec;
st->onelflg = onelflg;
st->enterhist = enterhist;
st->justpr = justpr;
if (hflg)
st->HIST = HIST;
else
st->HIST = '\0';
st->cantell = cantell;
cpybin(st->B, B);
if (av != NULL && *av != NULL) {
struct varent *vp;
if ((vp = adrof(STRargv)) != NULL && vp->vec != NULL)
st->argv = saveblk(vp->vec);
else
st->argv = NULL;
setq(STRargv, saveblk(av), &shvhed, VAR_READWRITE);
}
else
st->argv = NULL;
st->av = av;
SHIN = unit;
{
fbuf = NULL;
fseekp = feobp = fblocks = 0;
settell();
}
arginp = 0;
onelflg = 0;
intty = isatty(SHIN);
whyles = 0;
gointr = 0;
evalvec = 0;
evalp = 0;
alvec = al;
alvecp = 0;
enterhist = hflg;
if (enterhist)
HIST = '\0';
insource = 1;
}
static void
st_restore(void *xst)
{
struct saved_state *st;
st = xst;
if (st->SHIN == -1)
return;
{
int i;
Char** nfbuf = fbuf;
int nfblocks = fblocks;
fblocks = 0;
fbuf = NULL;
for (i = 0; i < nfblocks; i++)
xfree(nfbuf[i]);
xfree(nfbuf);
}
cpybin(B, st->B);
xclose(SHIN);
insource = st->insource;
SHIN = st->SHIN;
if (st->OLDSTD != -1)
xclose(OLDSTD), OLDSTD = st->OLDSTD;
if (st->SHOUT != -1)
xclose(SHOUT), SHOUT = st->SHOUT;
if (st->SHDIAG != -1)
xclose(SHDIAG), SHDIAG = st->SHDIAG;
arginp = st->arginp;
onelflg = st->onelflg;
evalp = st->evalp;
evalvec = st->evalvec;
alvecp = st->alvecp;
alvec = st->alvec;
intty = st->intty;
whyles = st->whyles;
gointr = st->gointr;
if (st->HIST != '\0')
HIST = st->HIST;
enterhist = st->enterhist;
cantell = st->cantell;
justpr = st->justpr;
if (st->argv != NULL)
setq(STRargv, st->argv, &shvhed, VAR_READWRITE);
else if (st->av != NULL && *st->av != NULL && adrof(STRargv) != NULL)
unsetv(STRargv);
}
static void
srcunit(int unit, int onlyown, int hflg, Char **av)
{
struct saved_state st;
st.SHIN = -1;
if (unit < 0)
return;
if (onlyown) {
struct stat stb;
if (fstat(unit, &stb) < 0) {
xclose(unit);
return;
}
}
cleanup_push(&st, st_restore);
if (setintr) {
pintr_disabled++;
cleanup_push(&pintr_disabled, disabled_cleanup);
}
st_save(&st, unit, hflg, NULL, av);
if (setintr) {
cleanup_until(&pintr_disabled);
pintr_disabled++;
cleanup_push(&pintr_disabled, disabled_cleanup);
}
process(0);
cleanup_until(&st);
}
void
goodbye(Char **v, struct command *c)
{
USE(v);
USE(c);
record();
if (loginsh) {
size_t omark;
sigset_t set;
sigemptyset(&set);
signal(SIGQUIT, SIG_IGN);
sigaddset(&set, SIGQUIT);
sigprocmask(SIG_UNBLOCK, &set, NULL);
signal(SIGINT, SIG_IGN);
sigaddset(&set, SIGINT);
signal(SIGTERM, SIG_IGN);
sigaddset(&set, SIGTERM);
signal(SIGHUP, SIG_IGN);
sigaddset(&set, SIGHUP);
sigprocmask(SIG_UNBLOCK, &set, NULL);
phup_disabled = 1;
setintr = 0;
omark = cleanup_push_mark();
if (setexit() == 0) {
if (!(adrof(STRlogout)))
setcopy(STRlogout, STRnormal, VAR_READWRITE);
#ifdef _PATH_DOTLOGOUT
(void) srcfile(_PATH_DOTLOGOUT, 0, 0, NULL);
#endif
if (adrof(STRhome))
(void) srccat(varval(STRhome), STRsldtlogout);
#ifdef TESLA
do_logout = 1;
#endif
}
cleanup_pop_mark(omark);
}
exitstat();
}
void
exitstat(void)
{
#ifdef PROF
monitor(0);
#endif
child = 1;
xexit(getn(varval(STRstatus)));
}
void
phup(void)
{
if (loginsh) {
setcopy(STRlogout, STRhangup, VAR_READWRITE);
#ifdef _PATH_DOTLOGOUT
(void) srcfile(_PATH_DOTLOGOUT, 0, 0, NULL);
#endif
if (adrof(STRhome))
(void) srccat(varval(STRhome), STRsldtlogout);
}
record();
#ifdef POSIXJOBS
{
struct process *pp, *np;
for (pp = proclist.p_next; pp; pp = pp->p_next) {
np = pp;
do
if (((np->p_flags & PFOREGND) != 0) && np->p_jobid != shpgrp) {
np->p_flags &= ~PHUP;
if (killpg(np->p_jobid, SIGHUP) != -1) {
#ifdef SIGCONT
(void) killpg(np->p_jobid, SIGCONT);
#endif
break;
}
}
while ((np = np->p_friends) != pp);
}
}
#endif
xexit(SIGHUP);
}
static Char *jobargv[2] = {STRjobs, 0};
int just_signaled;
void
pintr(void)
{
just_signaled = 1;
pintr1(1);
}
void
pintr1(int wantnl)
{
if (setintr) {
if (pjobs) {
pjobs = 0;
xputchar('\n');
dojobs(jobargv, NULL);
stderror(ERR_NAME | ERR_INTR);
}
}
{
if (InsideCompletion)
stderror(ERR_SILENT);
}
{
(void) Cookedmode();
GettingInput = 0;
}
drainoline();
#ifdef HAVE_GETPWENT
(void) endpwent();
#endif
if (gointr) {
gotolab(gointr);
reset();
}
else if (intty && wantnl) {
if (editing) {
PastBottom();
ClearLines();
ClearDisp();
}
else {
(void) putraw('\r');
(void) putraw('\n');
}
}
stderror(ERR_SILENT);
}
void
process(int catch)
{
jmp_buf_t osetexit;
size_t omark;
volatile int didexitset = 0;
getexit(osetexit);
omark = cleanup_push_mark();
for (;;) {
struct command *t;
int hadhist, old_pintr_disabled;
(void)setexit();
if (didexitset == 0) {
exitset++;
didexitset++;
}
pendjob();
justpr = enterhist;
if (haderr) {
if (!catch) {
doneinp = 0;
cleanup_pop_mark(omark);
resexit(osetexit);
reset();
}
haderr = 0;
closem();
continue;
}
if (doneinp) {
doneinp = 0;
break;
}
if (chkstop)
chkstop--;
if (neednote)
pnote();
if (intty && prompt && evalvec == 0) {
just_signaled = 0;
mailchk();
#ifndef HAVENOUTMP
watch_login(0);
#endif
sched_run();
period_cmd();
precmd();
if (fseekp == feobp && aret == TCSH_F_SEEK)
printprompt(0, NULL);
flush();
setalarm(1);
}
if (seterr) {
xfree(seterr);
seterr = NULL;
}
if (setintr)
pintr_push_enable(&old_pintr_disabled);
hadhist = lex(¶ml);
if (setintr)
cleanup_until(&old_pintr_disabled);
cleanup_push(¶ml, lex_cleanup);
if ((hadhist && !seterr && intty && !tellwhat && !Expand && !whyles) ||
adrof(STRverbose)) {
int odidfds = didfds;
haderr = 1;
didfds = 0;
prlex(¶ml);
flush();
haderr = 0;
didfds = odidfds;
}
(void) alarm(0);
alrmcatch_disabled = 1;
if (enterhist || (catch && intty && !whyles && !tellwhat && !arun))
savehist(¶ml, enterhist > 1);
if (Expand && seterr)
Expand = 0;
if (!enterhist && seterr)
stderror(ERR_OLD);
if (justpr)
goto cmd_done;
if (tellwhat) {
(void) tellmewhat(¶ml, NULL);
goto cmd_done;
}
alias(¶ml);
#ifdef BSDJOBS
if (prompt)
continue_jobs(¶ml);
#endif
if (prompt)
rmstar(¶ml);
t = syntax(paraml.next, ¶ml, 0);
cleanup_push(t, syntax_cleanup);
if (seterr)
stderror(ERR_OLD);
postcmd();
execute(t, (tpgrp > 0 ? tpgrp : -1), NULL, NULL, TRUE);
#ifdef SIG_WINDOW
if (windowchg || (catch && intty && !whyles && !tellwhat)) {
(void) check_window_size(0);
}
#endif
setcopy(STR_, InputBuf, VAR_READWRITE | VAR_NOGLOB);
cmd_done:
cleanup_until(¶ml);
}
cleanup_pop_mark(omark);
resexit(osetexit);
exitset--;
}
void
dosource(Char **t, struct command *c)
{
Char *f;
int hflg = 0;
char *file;
USE(c);
t++;
if (*t && eq(*t, STRmh)) {
if (*++t == NULL)
stderror(ERR_NAME | ERR_HFLAG);
hflg++;
}
else if (*t && eq(*t, STRmm)) {
if (*++t == NULL)
stderror(ERR_NAME | ERR_MFLAG);
hflg = 2;
}
f = globone(*t++, G_ERROR);
file = strsave(short2str(f));
cleanup_push(file, xfree);
xfree(f);
t = glob_all_or_error(t);
if ((!srcfile(file, 0, hflg, t)) && (!hflg) && (!bequiet))
stderror(ERR_SYSTEM, file, strerror(errno));
cleanup_until(file);
}
static void
mailchk(void)
{
struct varent *v;
Char **vp;
time_t t;
int intvl, cnt;
struct stat stb;
int new;
v = adrof(STRmail);
if (v == NULL || v->vec == NULL)
return;
(void) time(&t);
vp = v->vec;
cnt = blklen(vp);
intvl = (cnt && number(*vp)) ? (--cnt, getn(*vp++)) : MAILINTVL;
if (intvl < 1)
intvl = 1;
if (chktim + intvl > t)
return;
for (; *vp; vp++) {
char *filename = short2str(*vp);
char *mboxdir = filename;
if (stat(filename, &stb) < 0)
continue;
#if defined(BSDTIMES) || defined(_SEQUENT_)
new = stb.st_mtime > time0.tv_sec;
#else
new = stb.st_mtime > seconds0;
#endif
if (S_ISDIR(stb.st_mode)) {
DIR *mailbox;
int mailcount = 0;
char *tempfilename;
struct stat stc;
tempfilename = xasprintf("%s/new", filename);
if (stat(tempfilename, &stc) != -1 && S_ISDIR(stc.st_mode)) {
stb = stc;
#if defined(BSDTIMES) || defined(_SEQUENT_)
new = stb.st_mtime > time0.tv_sec;
#else
new = stb.st_mtime > seconds0;
#endif
mboxdir = tempfilename;
}
if (stb.st_mtime <= chktim + 1 || (loginsh && !new)) {
xfree(tempfilename);
continue;
}
mailbox = opendir(mboxdir);
xfree(tempfilename);
if (mailbox == NULL)
continue;
if (!readdir(mailbox) || !readdir(mailbox))
continue;
while (readdir(mailbox))
mailcount++;
if (mailcount == 0)
continue;
if (cnt == 1)
xprintf(CGETS(11, 3, "You have %d mail messages.\n"),
mailcount);
else
xprintf(CGETS(11, 4, "You have %d mail messages in %s.\n"),
mailcount, filename);
}
else {
char *type;
if (stb.st_size == 0 || stb.st_atime > stb.st_mtime ||
(stb.st_atime <= chktim && stb.st_mtime <= chktim) ||
(loginsh && !new))
continue;
type = strsave(new ? CGETS(11, 6, "new ") : "");
cleanup_push(type, xfree);
if (cnt == 1)
xprintf(CGETS(11, 5, "You have %smail.\n"), type);
else
xprintf(CGETS(11, 7, "You have %smail in %s.\n"), type, filename);
cleanup_until(type);
}
}
chktim = t;
}
Char *
gethdir(const Char *home)
{
Char *h;
if (*home == '\0') {
if ((h = varval(STRhome)) != STRNULL)
return Strsave(h);
else
return NULL;
}
if ((h = gettilde(home)) == NULL)
return NULL;
else
return Strsave(h);
}
void
initdesc(void)
{
#ifdef NLS_BUGS
#ifdef NLS_CATALOGS
nlsclose();
#endif
#endif
didfds = 0;
(void) close_on_exec(SHIN = dcopy(0, FSHIN), 1);
(void) close_on_exec(SHOUT = dcopy(1, FSHOUT), 1);
(void) close_on_exec(SHDIAG = dcopy(2, FSHDIAG), 1);
(void) close_on_exec(OLDSTD = dcopy(SHIN, FOLDSTD), 1);
#ifndef CLOSE_ON_EXEC
didcch = 0;
#endif
isdiagatty = isatty(SHDIAG);
isoutatty = isatty(SHOUT);
#ifdef NLS_BUGS
#ifdef NLS_CATALOGS
nlsinit();
#endif
#endif
}
void
#ifdef PROF
done(int i)
#else
xexit(int i)
#endif
{
#ifdef TESLA
if (loginsh && do_logout) {
#ifdef TIOCCDTR
(void) sleep(2);
(void) ioctl(FSHTTY, TIOCCDTR, NULL);
(void) sleep(1);
(void) ioctl(FSHTTY, TIOCSDTR, NULL);
#endif
}
#endif
{
struct process *pp, *np;
for (pp = proclist.p_next; pp; pp = pp->p_next) {
np = pp;
do
if ((np->p_flags & PHUP) && np->p_jobid != shpgrp) {
if (killpg(np->p_jobid, SIGHUP) != -1) {
#ifdef SIGCONT
(void) killpg(np->p_jobid, SIGCONT);
#endif
break;
}
}
while ((np = np->p_friends) != pp);
}
}
untty();
#ifdef NLS_CATALOGS
if (child == 0)
nlsclose();
#endif
#ifdef WINNT_NATIVE
nt_cleanup();
#endif
_exit(i);
}
#ifndef _PATH_DEFPATH
static Char **
defaultpath(void)
{
char *ptr;
Char **blk, **blkp;
struct stat stb;
blkp = blk = xmalloc(sizeof(Char *) * 10);
#ifndef NODOT
# ifndef DOTLAST
*blkp++ = Strsave(STRdot);
# endif
#endif
#define DIRAPPEND(a) \
if (stat(ptr = a, &stb) == 0 && S_ISDIR(stb.st_mode)) \
*blkp++ = SAVE(ptr)
#ifdef _PATH_LOCAL
DIRAPPEND(_PATH_LOCAL);
#endif
#ifdef _PATH_USRUCB
DIRAPPEND(_PATH_USRUCB);
#endif
#ifdef _PATH_USRBSD
DIRAPPEND(_PATH_USRBSD);
#endif
#ifdef _PATH_BIN
DIRAPPEND(_PATH_BIN);
#endif
#ifdef _PATH_USRBIN
DIRAPPEND(_PATH_USRBIN);
#endif
#undef DIRAPPEND
#ifndef NODOT
# ifdef DOTLAST
*blkp++ = Strsave(STRdot);
# endif
#endif
*blkp = NULL;
return (blk);
}
#endif
static void
record(void)
{
if (!fast) {
recdirs(NULL, adrof(STRsavedirs) != NULL);
rechist(NULL, adrof(STRsavehist) != NULL);
}
}
int
grabpgrp(int fd, pid_t desired)
{
struct sigaction old;
pid_t pgrp;
size_t i;
for (i = 0; i < 100; i++) {
if ((pgrp = tcgetpgrp(fd)) == -1)
return -1;
if (pgrp == desired)
return 0;
(void)sigaction(SIGTTIN, NULL, &old);
(void)signal(SIGTTIN, SIG_DFL);
(void)kill(0, SIGTTIN);
(void)sigaction(SIGTTIN, &old, NULL);
}
errno = EPERM;
return -1;
}