#include "rcv.h"
#include <errno.h>
#include <fcntl.h>
#include "extern.h"
static const char *prompt = "& ";
extern const struct cmd cmdtab[];
extern const char *version;
int
setfile(char *name)
{
FILE *ibuf;
int checkmode, i, fd;
struct stat stb;
char isedit = *name != '%' || getuserid(myname) != getuid();
char *who = name[1] ? name + 1 : myname;
char tempname[PATHSIZE];
static int shudclob;
checkmode = value("checkmode") != NULL;
if ((name = expand(name)) == NULL)
return (-1);
if ((ibuf = Fopen(name, "r")) == NULL) {
if (!isedit && errno == ENOENT)
goto nomail;
warn("%s", name);
return (-1);
}
if (fstat(fileno(ibuf), &stb) < 0) {
warn("fstat");
(void)Fclose(ibuf);
return (-1);
}
if (S_ISDIR(stb.st_mode) || !S_ISREG(stb.st_mode)) {
(void)Fclose(ibuf);
errno = S_ISDIR(stb.st_mode) ? EISDIR : EINVAL;
warn("%s", name);
return (-1);
}
holdsigs();
if (shudclob)
quit();
readonly = 0;
if ((i = open(name, 1)) < 0)
readonly++;
else
(void)close(i);
if (shudclob) {
(void)fclose(itf);
(void)fclose(otf);
}
shudclob = 1;
edit = isedit;
strlcpy(prevfile, mailname, sizeof(prevfile));
if (name != mailname)
strlcpy(mailname, name, sizeof(mailname));
mailsize = fsize(ibuf);
(void)snprintf(tempname, sizeof(tempname),
"%s/mail.RxXXXXXXXXXX", tmpdir);
if ((fd = mkstemp(tempname)) == -1 || (otf = fdopen(fd, "w")) == NULL)
err(1, "%s", tempname);
(void)fcntl(fileno(otf), F_SETFD, 1);
if ((itf = fopen(tempname, "r")) == NULL)
err(1, "%s", tempname);
(void)fcntl(fileno(itf), F_SETFD, 1);
(void)rm(tempname);
setptr(ibuf, 0);
setmsize(msgCount);
mailsize = ftello(ibuf);
(void)Fclose(ibuf);
relsesigs();
sawcom = 0;
if ((checkmode || !edit) && msgCount == 0) {
nomail:
if (!checkmode) {
fprintf(stderr, "No mail for %s\n", who);
return (-1);
} else
return (0);
}
return (checkmode ? 1 : 0);
}
int
incfile(void)
{
off_t newsize;
int omsgCount = msgCount;
FILE *ibuf;
ibuf = Fopen(mailname, "r");
if (ibuf == NULL)
return (-1);
holdsigs();
newsize = fsize(ibuf);
if (newsize == 0)
return (-1);
if (newsize < mailsize)
return (-1);
if (newsize == mailsize)
return (0);
setptr(ibuf, mailsize);
setmsize(msgCount);
mailsize = ftello(ibuf);
(void)Fclose(ibuf);
relsesigs();
return (msgCount - omsgCount);
}
static int *msgvec;
static int reset_on_stop;
void
commands(void)
{
int n, eofloop = 0;
char linebuf[LINESIZE];
if (!sourcing) {
if (signal(SIGINT, SIG_IGN) != SIG_IGN)
(void)signal(SIGINT, intr);
(void)signal(SIGTSTP, stop);
(void)signal(SIGTTOU, stop);
(void)signal(SIGTTIN, stop);
}
setexit();
for (;;) {
if (!sourcing && value("interactive") != NULL) {
if ((value("autoinc") != NULL) && (incfile() > 0))
printf("New mail has arrived.\n");
reset_on_stop = 1;
printf("%s", prompt);
}
(void)fflush(stdout);
sreset();
n = 0;
for (;;) {
if (readline(input, &linebuf[n],
sizeof(linebuf) - n) < 0) {
if (n == 0)
n = -1;
break;
}
if ((n = strlen(linebuf)) == 0)
break;
n--;
if (linebuf[n] != '\\')
break;
linebuf[n++] = ' ';
}
reset_on_stop = 0;
if (n < 0) {
if (loading)
break;
if (sourcing) {
unstack();
continue;
}
if (value("interactive") != NULL &&
value("ignoreeof") != NULL &&
++eofloop < 25) {
printf("Use \"quit\" to quit.\n");
continue;
}
break;
}
eofloop = 0;
if (execute(linebuf, 0))
break;
}
}
int
execute(char linebuf[], int contxt)
{
char word[LINESIZE];
char *arglist[MAXARGC];
const struct cmd *com;
char *cp, *cp2;
int c, muvec[2];
int e = 1;
for (cp = linebuf; isspace((unsigned char)*cp); cp++)
;
if (*cp == '!') {
if (sourcing) {
printf("Can't \"!\" while sourcing\n");
goto out;
}
shell(cp+1);
return (0);
}
cp2 = word;
while (*cp != '\0' && strchr(" \t0123456789$^.:/-+*'\"", *cp) == NULL)
*cp2++ = *cp++;
*cp2 = '\0';
if (sourcing && *word == '\0')
return (0);
com = lex(word);
if (com == NULL) {
printf("Unknown command: \"%s\"\n", word);
goto out;
}
if ((com->c_argtype & F) == 0)
if ((cond == CRCV && !rcvmode) || (cond == CSEND && rcvmode))
return (0);
if (!rcvmode && (com->c_argtype & M) == 0) {
printf("May not execute \"%s\" while sending\n",
com->c_name);
goto out;
}
if (sourcing && com->c_argtype & I) {
printf("May not execute \"%s\" while sourcing\n",
com->c_name);
goto out;
}
if (readonly && com->c_argtype & W) {
printf("May not execute \"%s\" -- message file is read only\n",
com->c_name);
goto out;
}
if (contxt && com->c_argtype & R) {
printf("Cannot recursively invoke \"%s\"\n", com->c_name);
goto out;
}
switch (com->c_argtype & ~(F|P|I|M|T|W|R)) {
case MSGLIST:
if (msgvec == 0) {
printf("Illegal use of \"message list\"\n");
break;
}
if ((c = getmsglist(cp, msgvec, com->c_msgflag)) < 0)
break;
if (c == 0) {
*msgvec = first(com->c_msgflag, com->c_msgmask);
msgvec[1] = 0;
}
if (*msgvec == 0) {
printf("No applicable messages\n");
break;
}
e = (*com->c_func)(msgvec);
break;
case NDMLIST:
if (msgvec == 0) {
printf("Illegal use of \"message list\"\n");
break;
}
if (getmsglist(cp, msgvec, com->c_msgflag) < 0)
break;
e = (*com->c_func)(msgvec);
break;
case STRLIST:
while (isspace((unsigned char)*cp))
cp++;
e = (*com->c_func)(cp);
break;
case RAWLIST:
if ((c = getrawlist(cp, arglist,
sizeof(arglist) / sizeof(*arglist))) < 0)
break;
if (c < com->c_minargs) {
printf("%s requires at least %d arg(s)\n",
com->c_name, com->c_minargs);
break;
}
if (c > com->c_maxargs) {
printf("%s takes no more than %d arg(s)\n",
com->c_name, com->c_maxargs);
break;
}
e = (*com->c_func)(arglist);
break;
case NOLIST:
e = (*com->c_func)(0);
break;
default:
errx(1, "Unknown argtype");
}
out:
if (e) {
if (e < 0)
return (1);
if (loading)
return (1);
if (sourcing)
unstack();
return (0);
}
if (com == NULL)
return (0);
if (value("autoprint") != NULL && com->c_argtype & P)
if ((dot->m_flag & MDELETED) == 0) {
muvec[0] = dot - &message[0] + 1;
muvec[1] = 0;
type(muvec);
}
if (!sourcing && (com->c_argtype & T) == 0)
sawcom = 1;
return (0);
}
void
setmsize(int sz)
{
if (msgvec != NULL)
(void)free(msgvec);
msgvec = calloc((unsigned)(sz + 1), sizeof(*msgvec));
}
const struct cmd *
lex(char word[])
{
const struct cmd *cp;
if (*word == '#')
*(word+1) = '\0';
for (cp = &cmdtab[0]; cp->c_name != NULL; cp++)
if (isprefix(word, cp->c_name))
return (cp);
return (NULL);
}
int
isprefix(const char *as1, const char *as2)
{
const char *s1, *s2;
s1 = as1;
s2 = as2;
while (*s1++ == *s2)
if (*s2++ == '\0')
return (1);
return (*--s1 == '\0');
}
static int inithdr;
void
intr(int s __unused)
{
noreset = 0;
if (!inithdr)
sawcom++;
inithdr = 0;
while (sourcing)
unstack();
close_all_files();
if (image >= 0) {
(void)close(image);
image = -1;
}
fprintf(stderr, "Interrupt\n");
reset(0);
}
void
stop(int s)
{
sig_t old_action = signal(s, SIG_DFL);
sigset_t nset;
(void)sigemptyset(&nset);
(void)sigaddset(&nset, s);
(void)sigprocmask(SIG_UNBLOCK, &nset, NULL);
(void)kill(0, s);
(void)sigprocmask(SIG_BLOCK, &nset, NULL);
(void)signal(s, old_action);
if (reset_on_stop) {
reset_on_stop = 0;
reset(0);
}
}
void
announce(void)
{
int vec[2], mdot;
mdot = newfileinfo(0);
vec[0] = mdot;
vec[1] = 0;
dot = &message[mdot - 1];
if (msgCount > 0 && value("noheader") == NULL) {
inithdr++;
headers(vec);
inithdr = 0;
}
}
int
newfileinfo(int omsgCount)
{
struct message *mp;
int u, n, mdot, d, s;
char fname[PATHSIZE+1], zname[PATHSIZE+1], *ename;
for (mp = &message[omsgCount]; mp < &message[msgCount]; mp++)
if (mp->m_flag & MNEW)
break;
if (mp >= &message[msgCount])
for (mp = &message[omsgCount]; mp < &message[msgCount]; mp++)
if ((mp->m_flag & MREAD) == 0)
break;
if (mp < &message[msgCount])
mdot = mp - &message[0] + 1;
else
mdot = omsgCount + 1;
s = d = 0;
for (mp = &message[0], n = 0, u = 0; mp < &message[msgCount]; mp++) {
if (mp->m_flag & MNEW)
n++;
if ((mp->m_flag & MREAD) == 0)
u++;
if (mp->m_flag & MDELETED)
d++;
if (mp->m_flag & MSAVED)
s++;
}
ename = mailname;
if (getfold(fname, sizeof(fname) - 1) >= 0) {
strcat(fname, "/");
if (strncmp(fname, mailname, strlen(fname)) == 0) {
(void)snprintf(zname, sizeof(zname), "+%s",
mailname + strlen(fname));
ename = zname;
}
}
printf("\"%s\": ", ename);
if (msgCount == 1)
printf("1 message");
else
printf("%d messages", msgCount);
if (n > 0)
printf(" %d new", n);
if (u-n > 0)
printf(" %d unread", u);
if (d > 0)
printf(" %d deleted", d);
if (s > 0)
printf(" %d saved", s);
if (readonly)
printf(" [Read only]");
printf("\n");
return (mdot);
}
int
pversion(void *arg __unused)
{
printf("Version %s\n", version);
return (0);
}
void
load(char *name)
{
FILE *in, *oldin;
if ((in = Fopen(name, "r")) == NULL)
return;
oldin = input;
input = in;
loading = 1;
sourcing = 1;
commands();
loading = 0;
sourcing = 0;
input = oldin;
(void)Fclose(in);
}