#include "rcv.h"
#include <locale.h>
#ifdef SIGCONT
static void contin(int);
#endif
static int isprefix(char *as1, char *as2);
static const struct cmd *lex(char word[]);
static int Passeren(void);
static void setmsize(int sz);
int
setfile(char *name, int isedit)
{
FILE *ibuf;
int i;
static int shudclob;
static char efile[PATHSIZE];
char fortest[128];
struct stat stbuf;
int exrc = 1;
int rc = -1;
int fd = -1;
if (!isedit && issysmbox)
lockmail();
if (stat(name, &stbuf) < 0 && errno == EOVERFLOW) {
fprintf(stderr, gettext("mailbox %s is too large to"
" accept incoming mail\n"), name);
goto doret;
}
if ((ibuf = fopen(name, "r")) == NULL) {
extern int errno;
int sverrno = errno;
int filethere = (access(name, 0) == 0);
errno = sverrno;
if (exitflg)
goto doexit;
if (isedit || filethere)
perror(name);
else if (!Hflag) {
char *f = strrchr(name, '/');
if (f == NOSTR)
fprintf(stderr, gettext("No mail.\n"));
else
fprintf(stderr, gettext("No mail for %s\n"),
f+1);
}
goto doret;
}
fstat(fileno(ibuf), &stbuf);
if (stbuf.st_size == 0L || (stbuf.st_mode&S_IFMT) != S_IFREG) {
if (exitflg)
goto doexit;
if (isedit)
if (stbuf.st_size == 0L)
fprintf(stderr, gettext("%s: empty file\n"),
name);
else
fprintf(stderr,
gettext("%s: not a regular file\n"), name);
else if (!Hflag) {
if (strrchr(name, '/') == NOSTR)
fprintf(stderr, gettext("No mail.\n"));
else
fprintf(stderr, gettext("No mail for %s\n"),
strrchr(name, '/') + 1);
}
fclose(ibuf);
goto doret;
}
if (fgets(fortest, sizeof (fortest), ibuf) == NULL) {
perror(gettext("mailx: Unable to read from mail file"));
goto doexit;
}
fseek(ibuf, (long)(BUFSIZ+1), 0);
fseek(ibuf, 0L, 0);
if (strncmp(fortest, "Forward to ", 11) == 0) {
if (exitflg)
goto doexit;
fprintf(stderr, gettext("Your mail is being forwarded to %s"),
fortest+11);
fclose(ibuf);
goto doret;
}
if (exitflg) {
exrc = 0;
goto doexit;
}
holdsigs();
if (shudclob) {
if (edit)
edstop(strcmp(editfile, name) == 0);
else {
quit(strcmp(editfile, name) == 0);
if (issysmbox)
Verhogen();
}
fflush(stdout);
fclose(itf);
fclose(otf);
free(message);
space = 0;
}
readonly = 0;
if (!isedit && issysmbox && !Hflag)
readonly = Passeren() == -1;
lock(ibuf, "r", 1);
fstat(fileno(ibuf), &stbuf);
utimep->actime = stbuf.st_atime;
utimep->modtime = stbuf.st_mtime;
if (!readonly)
if ((i = open(name, O_WRONLY)) < 0)
readonly++;
else
close(i);
shudclob = 1;
edit = isedit;
nstrcpy(efile, PATHSIZE, name);
editfile = efile;
#ifdef notdef
if (name != mailname)
nstrcpy(mailname, PATHSIZE, name);
#endif
mailsize = fsize(ibuf);
if ((fd = open(tempMesg, O_RDWR|O_CREAT|O_EXCL, 0600)) < 0 ||
(otf = fdopen(fd, "w")) == NULL) {
perror(tempMesg);
if (!edit && issysmbox)
Verhogen();
goto doexit;
}
if ((itf = fopen(tempMesg, "r")) == NULL) {
perror(tempMesg);
if (!edit && issysmbox)
Verhogen();
goto doexit;
}
removefile(tempMesg);
setptr(ibuf);
setmsize(msgCount);
fclose(ibuf);
relsesigs();
sawcom = 0;
rc = 0;
doret:
if (!isedit && issysmbox)
unlockmail();
return (rc);
doexit:
if (!isedit && issysmbox)
unlockmail();
exit(exrc ? exrc : rpterr);
}
static char semfn[128];
static FILE *semfp = NULL;
static int
Passeren(void)
{
char *home;
if ((home = getenv("HOME")) == NULL)
return (0);
snprintf(semfn, sizeof (semfn), "%s%s", home, "/.Maillock");
if ((semfp = fopen(semfn, "w")) == NULL) {
fprintf(stderr,
gettext("WARNING: Can't open mail lock file (%s).\n"), semfn);
fprintf(stderr,
gettext("\t Assuming you are not already reading mail.\n"));
return (0);
}
if (lock(semfp, "w", 0) < 0) {
if (errno == ENOLCK) {
fprintf(stderr,
gettext("WARNING: Unable to acquire mail lock, no record locks available.\n"));
fprintf(stderr,
gettext("\t Assuming you are not already reading mail.\n"));
return (0);
}
fprintf(stderr,
gettext("WARNING: You are already reading mail.\n"));
fprintf(stderr,
gettext("\t This instance of mail is read only.\n"));
fclose(semfp);
semfp = NULL;
return (-1);
}
return (0);
}
void
Verhogen(void)
{
if (semfp != NULL) {
unlink(semfn);
fclose(semfp);
}
}
static int *msgvec;
static int shudprompt;
void
commands(void)
{
int eofloop;
int n;
char linebuf[LINESIZE];
char line[LINESIZE];
struct stat minfo;
FILE *ibuf;
#ifdef SIGCONT
sigset(SIGCONT, SIG_DFL);
#endif
if (rcvmode && !sourcing) {
if (sigset(SIGINT, SIG_IGN) != SIG_IGN)
sigset(SIGINT, stop);
if (sigset(SIGHUP, SIG_IGN) != SIG_IGN)
sigset(SIGHUP, hangup);
}
for (;;) {
setjmp(srbuf);
if (!rcvmode && !sourcing)
return;
eofloop = 0;
top:
if ((shudprompt = (intty && !sourcing)) != 0) {
if (prompt == NOSTR) {
if ((int)value("bsdcompat"))
prompt = "& ";
else
prompt = "";
}
#ifdef SIGCONT
sigset(SIGCONT, contin);
#endif
if (intty && value("autoinc") &&
stat(editfile, &minfo) >= 0 &&
minfo.st_size > mailsize) {
int OmsgCount, i;
OmsgCount = msgCount;
fseek(otf, 0L, 2);
holdsigs();
if (!edit && issysmbox)
lockmail();
if ((ibuf = fopen(editfile, "r")) == NULL) {
fprintf(stderr,
gettext("Can't reopen %s\n"),
editfile);
if (!edit && issysmbox)
unlockmail();
exit(1);
}
if (edit || !issysmbox)
lock(ibuf, "r", 1);
fseek(ibuf, mailsize, 0);
mailsize = fsize(ibuf);
setptr(ibuf);
setmsize(msgCount);
fclose(ibuf);
if (!edit && issysmbox)
unlockmail();
if (msgCount-OmsgCount > 0) {
printf(gettext(
"New mail has arrived.\n"));
if (msgCount - OmsgCount == 1)
printf(gettext(
"Loaded 1 new message\n"));
else
printf(gettext(
"Loaded %d new messages\n"),
msgCount-OmsgCount);
if (value("header") != NOSTR)
for (i = OmsgCount+1;
i <= msgCount; i++) {
printhead(i);
sreset();
}
}
relsesigs();
}
printf("%s", prompt);
}
flush();
sreset();
n = 0;
linebuf[0] = '\0';
for (;;) {
if (readline(input, line) <= 0) {
if (n != 0)
break;
if (loading)
return;
if (sourcing) {
unstack();
goto more;
}
if (value("ignoreeof") != NOSTR && shudprompt) {
if (++eofloop < 25) {
printf(gettext(
"Use \"quit\" to quit.\n"));
goto top;
}
}
return;
}
if ((n = strlen(line)) == 0)
break;
n--;
if (line[n] != '\\')
break;
line[n++] = ' ';
if (n > LINESIZE - (int)strlen(linebuf) - 1)
break;
strcat(linebuf, line);
}
n = LINESIZE - strlen(linebuf) - 1;
if ((int)strlen(line) > n) {
printf(gettext(
"Line plus continuation line too long:\n\t%s\n\nplus\n\t%s\n"),
linebuf, line);
if (loading)
return;
if (sourcing) {
unstack();
goto more;
}
return;
}
strncat(linebuf, line, n);
#ifdef SIGCONT
sigset(SIGCONT, SIG_DFL);
#endif
if (execute(linebuf, 0))
return;
more:;
}
}
int
execute(char linebuf[], int contxt)
{
char word[LINESIZE];
char *arglist[MAXARGC];
const struct cmd *com;
char *cp, *cp2;
int c, e;
int muvec[2];
cp = linebuf;
while (any(*cp, " \t"))
cp++;
cp2 = word;
if (any(*cp, "!|#"))
*cp2++ = *cp++;
else
while (*cp && !any(*cp, " \t0123456789$^.:/-+*'\""))
*cp2++ = *cp++;
*cp2 = '\0';
if (sourcing && equal(word, ""))
return (0);
com = lex(word);
if (com == NONE) {
fprintf(stderr, gettext("Unknown command: \"%s\"\n"), word);
if (loading) {
cond = CANY;
return (1);
}
if (sourcing) {
cond = CANY;
unstack();
}
return (0);
}
if ((com->c_argtype & F) == 0)
if (cond == CRCV && !rcvmode || cond == CSEND && rcvmode ||
cond == CTTY && !intty || cond == CNOTTY && intty)
return (0);
if ((uintptr_t)com->c_func == (uintptr_t)edstop) {
if (sourcing) {
if (loading)
return (1);
unstack();
return (0);
}
return (1);
}
if (!rcvmode && (com->c_argtype & M) == 0) {
fprintf(stderr,
gettext("May not execute \"%s\" while sending\n"),
com->c_name);
if (loading)
return (1);
if (sourcing)
unstack();
return (0);
}
if (sourcing && com->c_argtype & I) {
fprintf(stderr,
gettext("May not execute \"%s\" while sourcing\n"),
com->c_name);
rpterr = 1;
if (loading)
return (1);
unstack();
return (0);
}
if (readonly && com->c_argtype & W) {
fprintf(stderr, gettext(
"May not execute \"%s\" -- message file is read only\n"),
com->c_name);
if (loading)
return (1);
if (sourcing)
unstack();
return (0);
}
if (contxt && com->c_argtype & R) {
fprintf(stderr, gettext("Cannot recursively invoke \"%s\"\n"),
com->c_name);
return (0);
}
e = 1;
switch (com->c_argtype & ~(F|P|I|M|T|W|R)) {
case MSGLIST:
if (msgvec == 0) {
fprintf(stderr,
gettext("Illegal use of \"message list\"\n"));
return (-1);
}
if ((c = getmsglist(cp, msgvec, com->c_msgflag)) < 0)
break;
if (c == 0)
if (msgCount == 0)
*msgvec = 0;
else {
*msgvec = first(com->c_msgflag,
com->c_msgmask);
msgvec[1] = 0;
}
if (*msgvec == 0) {
fprintf(stderr, gettext("No applicable messages\n"));
break;
}
e = (*com->c_func)(msgvec);
break;
case NDMLIST:
if (msgvec == 0) {
fprintf(stderr,
gettext("Illegal use of \"message operand\"\n"));
return (-1);
}
if (getmessage(cp, msgvec, com->c_msgflag) < 0)
break;
e = (*com->c_func)(msgvec);
break;
case STRLIST:
while (any(*cp, " \t"))
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) {
fprintf(stderr,
gettext("%s requires at least %d arg(s)\n"),
com->c_name, com->c_minargs);
break;
}
if (c > com->c_maxargs) {
fprintf(stderr,
gettext("%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:
panic("Unknown argtype");
}
if (e && loading)
return (1);
if (e && sourcing)
unstack();
if ((uintptr_t)com->c_func == (uintptr_t)edstop)
return (1);
if (value("autoprint") != NOSTR && 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);
}
#ifdef SIGCONT
static void
contin(int s __unused)
{
if (shudprompt)
printf("%s", prompt);
fflush(stdout);
}
#endif
void
hangup(int s __unused)
{
holdsigs();
#ifdef OLD_BSD_SIGS
sigignore(SIGHUP);
#endif
if (edit) {
if (setjmp(srbuf))
exit(rpterr);
edstop(0);
} else {
if (issysmbox)
Verhogen();
if (value("exit") != NOSTR)
exit(1);
else
quit(0);
}
exit(rpterr);
}
static void
setmsize(int sz)
{
if (msgvec != (int *)0)
free(msgvec);
if (sz < 1)
sz = 1;
if ((msgvec = (int *)
calloc((unsigned)(sz + 1), sizeof (*msgvec))) == NULL)
panic("Failed to allocate memory for message vector");
}
static const struct cmd *
lex(char word[])
{
const struct cmd *cp;
for (cp = &cmdtab[0]; cp->c_name != NOSTR; cp++)
if (isprefix(word, cp->c_name))
return (cp);
return (NONE);
}
static int
isprefix(char *as1, char *as2)
{
char *s1, *s2;
s1 = as1;
s2 = as2;
while (*s1++ == *s2)
if (*s2++ == '\0')
return (1);
return (*--s1 == '\0');
}
static int inithdr;
void
stop(int s)
{
NODE *head;
noreset = 0;
if (!inithdr)
sawcom++;
inithdr = 0;
while (sourcing)
unstack();
(void) getuserid((char *)0);
for (head = fplist; head != (NODE *)NULL; head = head->next) {
if (head->fp == stdin || head->fp == stdout)
continue;
if (head->fp == itf || head->fp == otf)
continue;
if (head->fp == stderr)
continue;
if (head->fp == semfp)
continue;
if (head->fp == pipef) {
npclose(pipef);
pipef = NULL;
continue;
}
fclose(head->fp);
}
if (image >= 0) {
close(image);
image = -1;
}
if (s) {
fprintf(stderr, gettext("Interrupt\n"));
fflush(stderr);
#ifdef OLD_BSD_SIGS
sigrelse(s);
#endif
}
longjmp(srbuf, 1);
}
#define GREETING "%s Type ? for help.\n"
void
announce(void)
{
int vec[2], mdot;
extern const char *const version;
if (!Hflag && value("quiet") == NOSTR)
printf(gettext(GREETING), version);
mdot = newfileinfo(1);
vec[0] = mdot;
vec[1] = 0;
dot = &message[mdot - 1];
if (msgCount > 0 && !noheader) {
inithdr++;
headers(vec);
inithdr = 0;
}
}
int
newfileinfo(int start)
{
struct message *mp;
int u, n, mdot, d, s;
char fname[BUFSIZ], zname[BUFSIZ], *ename;
if (Hflag)
return (1);
for (mp = &message[start - 1]; mp < &message[msgCount]; mp++)
if ((mp->m_flag & (MNEW|MREAD)) == MNEW)
break;
if (mp >= &message[msgCount])
for (mp = &message[start - 1]; mp < &message[msgCount]; mp++)
if ((mp->m_flag & MREAD) == 0)
break;
if (mp < &message[msgCount])
mdot = mp - &message[0] + 1;
else
mdot = 1;
n = u = d = s = 0;
for (mp = &message[start - 1]; 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 = origname;
if (getfold(fname) >= 0) {
nstrcat(fname, sizeof (fname), "/");
if (strncmp(fname, editfile, strlen(fname)) == 0) {
snprintf(zname, sizeof (zname),
"+%s", editfile + strlen(fname));
ename = zname;
}
}
printf("\"%s\": ", ename);
if (msgCount == 1)
printf(gettext("1 message"));
else
printf(gettext("%d messages"), msgCount);
if (n > 0)
printf(gettext(" %d new"), n);
if (u-n > 0)
printf(gettext(" %d unread"), u);
if (d > 0)
printf(gettext(" %d deleted"), d);
if (s > 0)
printf(gettext(" %d saved"), s);
if (readonly)
printf(gettext(" [Read only]"));
printf("\n");
return (mdot);
}
int
pversion(char *s __unused)
{
printf("%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;
fclose(in);
}
int
inc(void)
{
FILE *ibuf;
int mdot;
struct stat stbuf;
int firstnewmsg = msgCount + 1;
if (edit) {
fprintf(stderr, gettext("Not in system mailbox\n"));
return (-1);
}
if (((ibuf = fopen(mailname, "r")) == NULL) ||
(fstat(fileno(ibuf), &stbuf) < 0) || stbuf.st_size == 0L ||
stbuf.st_size == mailsize || (stbuf.st_mode&S_IFMT) != S_IFREG) {
if (strrchr(mailname, '/') == NOSTR)
fprintf(stderr, gettext("No new mail.\n"));
else
fprintf(stderr, gettext("No new mail for %s\n"),
strrchr(mailname, '/')+1);
if (ibuf != NULL)
fclose(ibuf);
return (-1);
}
fseek(otf, 0L, 2);
holdsigs();
if (issysmbox)
lockmail();
fseek(ibuf, mailsize, 0);
mailsize = fsize(ibuf);
setptr(ibuf);
setmsize(msgCount);
fclose(ibuf);
if (issysmbox)
unlockmail();
relsesigs();
mdot = newfileinfo(firstnewmsg);
dot = &message[mdot - 1];
sawcom = 0;
return (0);
}