#include <locale.h>
#include "sh.h"
#include <fcntl.h>
#include <sys/filio.h>
#include "sh.tconst.h"
#include <pwd.h>
#include <stdlib.h>
#ifdef TRACE
#include <stdio.h>
#endif
#define CSH_FD_SET(n, p) ((*((p) + ((n)/NFDBITS))) |= (1 << ((n) % NFDBITS)))
#define CSH_FD_CLR(n, p) ((*((p) + ((n)/NFDBITS))) &= ~(1 << ((n) % NFDBITS)))
#define CSH_FD_ISSET(n, p) ((*((p) + ((n)/NFDBITS))) & (1 << ((n) % NFDBITS)))
#define CSH_FD_ZERO(p, n) memset((void *)(p), 0, (n))
tchar *pathlist[] = { S_usrbin , S_DOT , 0 };
tchar *dumphist[] = { S_history , S_h , 0, 0 };
tchar *loadhist[] = { S_source , S_h ,
S_NDOThistory , 0 };
tchar HIST = '!';
tchar HISTSUB = '^';
int nofile;
bool reenter;
bool nverbose;
bool nexececho;
bool quitit;
bool fast;
bool batch;
bool prompt = 1;
bool enterhist = 0;
static time_t chktim;
char *err_msg;
extern gid_t getegid(), getgid();
extern uid_t geteuid(), getuid();
extern tchar **strblktotsblk();
extern void hupforegnd(void);
void interactive_hup(void);
void interactive_login_hup(void);
void importpath(tchar *);
void srccat(tchar *, tchar *);
void srccat_inlogin(tchar *, tchar *);
void srcunit(int, bool, bool);
void rechist(void);
void goodbye(void);
void pintr1(bool);
void process(bool);
void dosource(tchar **);
void mailchk(void);
void printprompt(void);
void sigwaiting(void);
void siglwp(void);
void initdesc(int, char *[]);
void initdesc_x(int, char *[], int);
void closem(void);
void unsetfd(int);
void phup(void);
#ifdef TRACE
FILE *trace;
#define TRACEFILE "/tmp/trace.XXXXXX"
void
trace_init(void)
{
char name[128];
char *p;
strcpy(name, TRACEFILE);
p = mktemp(name);
trace = fopen(p, "w");
}
void
tprintf(fmt, a, b, c, d, e, f, g, h, i, j)
char *fmt;
{
if (trace) {
fprintf(trace, fmt, a, b, c, d, e, f, g, h, i, j);
fflush(trace);
}
}
#endif
int
main(int c, char **av)
{
tchar **v, *cp, *r;
int f;
struct sigvec osv;
struct sigaction sa;
tchar s_prompt[MAXHOSTNAMELEN+3];
char *c_max_var_len;
int c_max_var_len_size;
bool intact = 0;
haderr = 0;
setexit();
if (haderr) {
printf("csh startup error, csh exiting...\n");
flush();
exitstat();
}
(void) setlocale(LC_ALL, "");
#if !defined(TEXT_DOMAIN)
#define TEXT_DOMAIN "SYS_TEST"
#endif
(void) textdomain(TEXT_DOMAIN);
v = strblktotsblk(av, c);
paraml.next = paraml.prev = ¶ml;
settimes();
if (eq(v[0], S_aout ))
quitit = 1;
uid = getuid();
loginsh = **v == '-';
if (loginsh)
(void) time(&chktim);
initdesc(c, av);
c_max_var_len_size = snprintf(NULL, 0, "%ld", MAX_VAR_LEN);
c_max_var_len = (char *)xalloc(c_max_var_len_size + 1);
(void) snprintf(c_max_var_len, (c_max_var_len_size + 1),
"%ld", MAX_VAR_LEN);
set(S_SUNW_VARLEN, strtots(NOSTR, c_max_var_len));
xfree(c_max_var_len);
setNS(S_noglob);
set(S_status , S_0 );
dinit(cp = getenvs_("HOME"));
if (cp == NOSTR)
fast++;
else {
if (strlen_(cp) >= BUFSIZ - 10) {
cp = NOSTR;
fast++;
printf("%s\n", gettext("Pathname too long"));
set(S_home , savestr(cp));
local_setenv(S_HOME, savestr(cp));
}
set(S_home , savestr(cp));
}
if ((cp = getenvs_("USER")) != NOSTR)
set(S_user , savestr(cp));
else {
struct passwd *pw;
pw = getpwuid(getuid());
if (pw != NULL) {
set(S_user, strtots((tchar *)0, pw->pw_name));
local_setenv(S_USER, strtots((tchar *)0, pw->pw_name));
} else if (loginsh) {
printf("Warning: USER environment variable could not be set.\n");
}
}
if ((cp = getenvs_("TERM")) != NOSTR)
set(S_term , savestr(cp));
if ((cp = getenvs_("PATH")) == NOSTR)
set1(S_path , saveblk(pathlist), &shvhed);
else
importpath(cp);
set(S_shell , S_SHELLPATH);
doldol = putn(getpid());
unsetv(S_noglob);
(void) sigvec(SIGINT, (struct sigvec *)0, &osv);
parintr = osv.sv_handler;
(void) sigvec(SIGTERM, (struct sigvec *)0, &osv);
parterm = osv.sv_handler;
_signal(SIGLWP, siglwp);
_signal(SIGWAITING, sigwaiting);
if (loginsh) {
(void) signal(SIGHUP, phup);
(void) signal(SIGXCPU, phup);
(void) signal(SIGXFSZ, phup);
}
c--, v++;
while (c > 0 && (cp = v[0])[0] == '-' && *++cp != '\0' && !batch) {
do switch (*cp++) {
case 'b':
batch++;
break;
case 'c':
if (c == 1)
exit(0);
c--, v++;
arginp = v[0];
prompt = 0;
nofile++;
cflg++;
break;
case 'e':
exiterr++;
break;
case 'f':
fast++;
break;
case 'i':
intact++;
nofile++;
break;
case 'n':
noexec++;
break;
case 'q':
quitit = 1;
break;
case 's':
nofile++;
break;
case 't':
onelflg = 2;
prompt = 0;
nofile++;
break;
#ifdef TRACE
case 'T':
trace_init();
break;
#endif
case 'v':
nverbose = 1;
break;
case 'x':
nexececho = 1;
break;
case 'V':
setNS(S_verbose );
break;
case 'X':
setNS(S_echo );
break;
} while (*cp);
v++, c--;
}
if (quitit)
(void) signal(SIGQUIT, SIG_DFL);
if (!batch && (uid != geteuid() || getgid() != getegid())) {
errno = EACCES;
child++;
Perror(S_csh );
}
if (nofile == 0 && c > 0) {
nofile = open_(v[0], 0);
if (nofile < 0) {
child++;
Perror(v[0]);
}
file = v[0];
SHIN = dmove(nofile, FSHIN);
(void) fcntl(SHIN, F_SETFD, 1);
prompt = 0;
c--, v++;
}
intty = intact || isatty(SHIN);
if (loginsh || intact || intty && isatty(SHOUT))
setintr = 1;
#ifdef TELL
settell();
#endif
setq(S_argv , copyblk(v), &shvhed);
if (prompt) {
gethostname_(s_prompt, MAXHOSTNAMELEN);
strcat_(s_prompt,
uid == 0 ? S_SHARPSP : S_PERSENTSP );
set(S_prompt , s_prompt);
}
shpgrp = getpgid(0);
opgrp = tpgrp = -1;
if (setintr) {
**av = '-';
if (!quitit)
(void) signal(SIGQUIT, SIG_IGN);
(void) signal(SIGINT, pintr);
(void) sigblock(sigmask(SIGINT));
(void) signal(SIGTERM, SIG_IGN);
if (loginsh) {
(void) signal(SIGHUP, interactive_login_hup);
} else {
(void) signal(SIGHUP, interactive_hup);
}
if (quitit == 0 && arginp == 0) {
(void) signal(SIGTSTP, SIG_IGN);
(void) signal(SIGTTIN, SIG_IGN);
(void) signal(SIGTTOU, SIG_IGN);
if (isatty(FSHDIAG))
f = FSHDIAG;
else if (isatty(FSHOUT))
f = FSHOUT;
else if (isatty(OLDSTD))
f = OLDSTD;
else
f = -1;
retry:
if (ioctl(f, TIOCGPGRP, (char *)&tpgrp) == 0 &&
tpgrp != -1) {
if (tpgrp != shpgrp) {
void (*old)() = (void (*)())
signal(SIGTTIN, SIG_DFL);
(void) kill(0, SIGTTIN);
(void) signal(SIGTTIN, old);
goto retry;
}
opgrp = shpgrp;
shpgrp = getpid();
tpgrp = shpgrp;
(void) setpgid(0, shpgrp);
(void) ioctl(f, TIOCSPGRP, (char *)&shpgrp);
(void) fcntl(dcopy(f, FSHTTY), F_SETFD, 1);
} else {
printf("Warning: no access to tty; thus no job control in this shell...\n");
tpgrp = -1;
}
}
}
if (setintr == 0 && parintr == SIG_DFL)
setintr++;
sigemptyset(&sa.sa_mask);
sa.sa_handler = pchild;
sa.sa_flags = SA_RESTART;
(void) sigaction(SIGCHLD, &sa, (struct sigaction *)NULL);
setexit();
haderr = 0;
if (!fast && reenter == 0) {
reenter++;
if (loginsh) {
tchar tmp_etc[4+1];
tchar tmp_login[7+1];
strtots(tmp_etc, "/etc");
strtots(tmp_login, "/.login");
srccat_inlogin(tmp_etc, tmp_login);
}
srccat(value(S_home ),
S_SLADOTcshrc );
if (!fast && !arginp && !onelflg && !havhash)
dohash(xhash);
dosource(loadhist);
if (loginsh) {
srccat_inlogin(value(S_home ),
S_SLADOTlogin );
}
if (!fast && !arginp && !onelflg && !havhash2)
dohash(xhash2);
}
if (nverbose)
setNS(S_verbose );
if (nexececho)
setNS(S_echo );
process(setintr);
if (loginsh) {
printf("logout\n");
(void) close(SHIN);
child++;
goodbye();
}
rechist();
exitstat();
}
void
untty(void)
{
if (tpgrp > 0) {
(void) setpgid(0, opgrp);
(void) ioctl(FSHTTY, TIOCSPGRP, (char *)&opgrp);
}
}
void
importpath(tchar *cp)
{
int i = 0;
tchar *dp;
tchar **pv;
int c;
static tchar dot[2] = {'.', 0};
for (dp = cp; *dp; dp++)
if (*dp == ':')
i++;
pv = (tchar **)xcalloc((unsigned)(i + 2), sizeof (tchar **));
dp = cp;
i = 0;
if (*dp)
for (;;) {
if ((c = *dp) == ':' || c == 0) {
*dp = 0;
pv[i++] = savestr(*cp ? cp : dot);
if (c) {
cp = dp + 1;
*dp = ':';
} else
break;
}
dp++;
}
pv[i] = 0;
set1(S_path , pv, &shvhed);
}
void
srccat(tchar *cp, tchar *dp)
{
tchar *ep = strspl(cp, dp);
int unit = dmove(open_(ep, 0), -1);
(void) fcntl(unit, F_SETFD, 1);
xfree(ep);
#ifdef INGRES
srcunit(unit, 0, 0);
#else
srcunit(unit, 1, 0);
#endif
}
void
srccat_inlogin(tchar *cp, tchar *dp)
{
tchar *ep = strspl(cp, dp);
int unit = dmove(open_(ep, 0), -1);
(void) fcntl(unit, F_SETFD, 1);
xfree(ep);
srcunit(unit, 0, 0);
}
void
srcunit(int unit, bool onlyown, bool hflg)
{
int oSHIN = -1, oldintty = intty;
struct whyle *oldwhyl = whyles;
tchar *ogointr = gointr, *oarginp = arginp;
tchar *oevalp = evalp, **oevalvec = evalvec;
int oonelflg = onelflg;
bool oenterhist = enterhist;
tchar OHIST = HIST;
#ifdef TELL
bool otell = cantell;
#endif
struct Bin saveB;
jmp_buf oldexit;
int reenter, omask;
if (unit < 0)
return;
if (didfds)
donefds();
if (onlyown) {
struct stat stb;
if (fstat(unit, &stb) < 0 ||
(stb.st_uid != uid && stb.st_gid != getgid())) {
(void) close(unit);
unsetfd(unit);
return;
}
}
getexit(oldexit);
reenter = 0;
if (setintr)
omask = sigblock(sigmask(SIGINT));
setexit();
reenter++;
if (reenter == 1) {
copy((char *)&saveB, (char *)&B, sizeof (saveB));
fbuf = (tchar **) 0;
fseekp = feobp = fblocks = 0;
oSHIN = SHIN, SHIN = unit, arginp = 0, onelflg = 0;
intty = isatty(SHIN), whyles = 0, gointr = 0;
evalvec = 0; evalp = 0;
enterhist = hflg;
if (enterhist)
HIST = '\0';
if (setintr)
(void) sigsetmask(omask);
#ifdef TELL
settell();
#endif
process(0);
}
if (setintr)
(void) sigsetmask(omask);
if (oSHIN >= 0) {
int i;
for (i = 0; i < fblocks; i++)
xfree(fbuf[i]);
xfree((char *)fbuf);
copy((char *)&B, (char *)&saveB, sizeof (B));
(void) close(SHIN), SHIN = oSHIN;
unsetfd(SHIN);
arginp = oarginp, onelflg = oonelflg;
evalp = oevalp, evalvec = oevalvec;
intty = oldintty, whyles = oldwhyl, gointr = ogointr;
if (enterhist)
HIST = OHIST;
enterhist = oenterhist;
#ifdef TELL
cantell = otell;
#endif
}
resexit(oldexit);
if (reenter >= 2)
error(NULL);
}
void
rechist(void)
{
tchar buf[BUFSIZ];
int fp, ftmp, oldidfds;
if (!fast) {
if (value(S_savehist )[0] == '\0')
return;
(void) strcpy_(buf, value(S_home ));
(void) strcat_(buf, S_SLADOThistory );
fp = creat_(buf, 0666);
if (fp == -1)
return;
oldidfds = didfds;
didfds = 0;
ftmp = SHOUT;
SHOUT = fp;
(void) strcpy_(buf, value(S_savehist ));
dumphist[2] = buf;
dohist(dumphist);
(void) close(fp);
unsetfd(fp);
SHOUT = ftmp;
didfds = oldidfds;
}
}
void
goodbye(void)
{
if (loginsh) {
(void) signal(SIGQUIT, SIG_IGN);
(void) signal(SIGINT, SIG_IGN);
(void) signal(SIGTERM, SIG_IGN);
setintr = 0;
if (adrof(S_home ))
srccat(value(S_home ),
S_SLADOTlogout );
}
rechist();
exitstat();
}
void
exitstat(void)
{
#ifdef PROF
monitor(0);
#endif
child++;
untty();
exit(getn(value(S_status )));
}
void
phup(void)
{
rechist();
exit(1);
}
void
interactive_hup(void)
{
hupforegnd();
exit(1);
}
void
interactive_login_hup(void)
{
rechist();
hupforegnd();
exit(1);
}
tchar *jobargv[2] = { S_jobs , 0 };
void
pintr(void)
{
pintr1(1);
}
void
pintr1(bool wantnl)
{
tchar **v;
int omask;
omask = sigblock(0);
if (setintr) {
(void) sigsetmask(omask & ~sigmask(SIGINT));
if (pjobs) {
pjobs = 0;
printf("\n");
dojobs(jobargv);
bferr("Interrupted");
}
}
(void) sigsetmask(omask & ~sigmask(SIGCHLD));
draino();
if (gointr) {
search(ZGOTO, 0, gointr);
if (v = pargv)
pargv = 0, blkfree(v);
if (v = gargv)
gargv = 0, blkfree(v);
reset();
} else if (intty && wantnl)
printf("\n");
error(NULL);
}
void
process(bool catch)
{
jmp_buf osetexit;
struct command *t;
getexit(osetexit);
for (;;) {
pendjob();
freelex(¶ml);
paraml.next = paraml.prev = ¶ml;
paraml.word = S_ ;
t = 0;
setexit();
justpr = enterhist;
if (setintr)
(void) sigsetmask(sigblock(0) & ~sigmask(SIGINT));
freelex(¶ml), freesyn(t), t = 0;
if (haderr) {
if (!catch) {
doneinp = 0;
resexit(osetexit);
reset();
}
haderr = 0;
closem();
continue;
}
if (doneinp) {
doneinp = 0;
break;
}
if (chkstop)
chkstop--;
if (neednote)
pnote();
if (intty && prompt && evalvec == 0) {
mailchk();
if (fseekp == feobp)
printprompt();
}
err_msg = NULL;
if (lex(¶ml) && intty ||
adrof(S_verbose )) {
haderr = 1;
prlex(¶ml);
haderr = 0;
}
if (setintr)
(void) sigblock(sigmask(SIGINT));
if (enterhist || catch && intty && !whyles)
savehist(¶ml);
if (!enterhist && err_msg)
error("%s", gettext(err_msg));
if (justpr)
reset();
alias(¶ml);
t = syntax(paraml.next, ¶ml, 0);
if (err_msg)
error("%s", gettext(err_msg));
{
int omask;
omask = sigblock(sigmask(SIGCHLD));
execute(t, tpgrp);
(void) sigsetmask(omask &~ sigmask(SIGCHLD));
}
if (err_msg)
error("%s", gettext(err_msg));
freelex(¶ml), freesyn(t);
}
resexit(osetexit);
}
void
dosource(tchar **t)
{
tchar *f;
int u;
bool hflg = 0;
tchar buf[BUFSIZ];
t++;
if (*t && eq(*t, S_h )) {
if (*++t == NOSTR)
bferr("Too few arguments.");
hflg++;
}
(void) strcpy_(buf, *t);
f = globone(buf);
u = dmove(open_(f, 0), -1);
xfree(f);
freelex(¶ml);
if (u < 0 && !hflg)
Perror(f);
(void) fcntl(u, F_SETFD, 1);
srcunit(u, 0, hflg);
}
void
mailchk(void)
{
struct varent *v;
tchar **vp;
time_t t;
int intvl, cnt;
struct stat stb;
bool new;
v = adrof(S_mail );
if (v == 0)
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++) {
if (stat_(*vp, &stb) < 0)
continue;
new = stb.st_mtime > time0.tv_sec;
if (stb.st_size == 0 || stb.st_atime >= stb.st_mtime ||
(stb.st_atime <= chktim && stb.st_mtime <= chktim) ||
loginsh && !new)
continue;
if (cnt == 1)
printf("You have %smail.\n", new ? "new " : "");
else
printf("%s in %t.\n", new ? "New mail" : "Mail", *vp);
}
chktim = t;
}
int
gethdir(tchar *home)
{
char home_str[BUFSIZ];
tchar home_ts[BUFSIZ];
struct passwd *pp ;
pp = getpwnam(tstostr(home_str, home));
if (pp == 0)
return (1);
(void) strcpy_(home, strtots(home_ts, pp->pw_dir));
return (0);
}
#if 0
void
#ifdef PROF
done(int i)
#else
exit(int i)
#endif
{
untty();
_exit(i);
}
#endif
void
printprompt(void)
{
tchar *cp;
if (!whyles) {
for (cp = value(S_prompt ); *cp; cp++)
if (*cp == HIST)
printf("%d", eventno + 1);
else {
if (*cp == '\\' && cp[1] == HIST)
cp++;
Putchar(*cp | QUOTE);
}
} else
printf("? ");
flush();
}
tchar **
strblktotsblk(char **v, int num)
{
tchar **newv =
(tchar **)xcalloc((unsigned)(num+ 1), sizeof (tchar **));
tchar **onewv = newv;
while (*v && num--)
*newv++ = strtots(NOSTR, *v++);
*newv = 0;
return (onewv);
}
void
sigwaiting(void)
{
_signal(SIGWAITING, sigwaiting);
}
void
siglwp(void)
{
_signal(SIGLWP, siglwp);
}
static int *fdinuse = NULL;
static int nbytesused = 0;
static int max_fd = 0;
static int my_pid;
static int NoFile = NOFILE;
void
initdesc(int argc, char *argv[])
{
initdesc_x(argc, argv, 0);
}
void
reinitdesc(int argc, char *argv[])
{
initdesc_x(argc, argv, 1);
}
static int
close_except(void *cd, int fd)
{
int script_fd = *(int *)cd;
if (fd >= 3 && fd < NoFile && fd != script_fd)
(void) close(fd);
return (0);
}
static int
close_inuse(void *cd, int fd)
{
int script_fd = *(int *)cd;
if (fd >= 3 && fd < NoFile && fd != script_fd &&
CSH_FD_ISSET(fd, fdinuse)) {
(void) close(fd);
unsetfd(fd);
}
return (0);
}
void
initdesc_x(int argc, char *argv[], int is_reinit)
{
int script_fd = -1;
struct stat buf;
struct rlimit rlp;
my_pid = getpid();
if (getrlimit(RLIMIT_NOFILE, &rlp) == 0)
NoFile = rlp.rlim_cur;
if (argc >= 3)
if (sscanf(argv[2], "/dev/fd/%d", &script_fd) != 1)
script_fd = -1;
else
fcntl(script_fd, F_SETFD, 1);
if (fdinuse == NULL) {
nbytesused = sizeof (int) *
howmany(NoFile, sizeof (int) * NBBY);
fdinuse = (int *)xalloc(nbytesused);
}
if (!is_reinit)
(void) fdwalk(close_except, &script_fd);
else
(void) fdwalk(close_inuse, &script_fd);
didfds = 0;
if (fstat(0, &buf) < 0)
open("/dev/null", 0);
(void) fcntl(SHIN = dcopy(0, FSHIN), F_SETFD, 1);
(void) fcntl(SHOUT = dcopy(1, FSHOUT), F_SETFD, 1);
(void) fcntl(SHDIAG = dcopy(2, FSHDIAG), F_SETFD, 1);
(void) fcntl(OLDSTD = dcopy(SHIN, FOLDSTD), F_SETFD, 1);
close(0); close(1); close(2);
open("/dev/null", 0);
dup(0);
dup(0);
if (!is_reinit)
CSH_FD_ZERO(fdinuse, nbytesused);
}
void
closem(void)
{
int f;
for (f = 3; f <= max_fd; f++) {
if (CSH_FD_ISSET(f, fdinuse) &&
f != SHIN && f != SHOUT && f != SHDIAG &&
f != OLDSTD && f != FSHTTY)
close(f);
}
CSH_FD_ZERO(fdinuse, nbytesused);
max_fd = 0;
}
void
new_process(void)
{
my_pid = getpid();
}
void
setfd(int fd)
{
if (my_pid != getpid())
return;
if (fd >= NoFile || fd < 0)
return;
if (fd > max_fd)
max_fd = fd;
CSH_FD_SET(fd, fdinuse);
}
void
unsetfd(int fd)
{
int i;
if (my_pid != getpid())
return;
if (fd >= NoFile || fd < 0)
return;
CSH_FD_CLR(fd, fdinuse);
if (fd == max_fd) {
for (i = max_fd-1; i >= 3; i--)
if (CSH_FD_ISSET(i, fdinuse)) {
max_fd = i;
return;
}
max_fd = 0;
}
}