#include <sys/cdefs.h>
#ifndef lint
#if 0
static char sccsid[] = "@(#)lex.c 8.2 (Berkeley) 4/20/95";
#else
__RCSID("$NetBSD: lex.c,v 1.47 2024/08/17 19:18:10 andvar Exp $");
#endif
#endif
#include <assert.h>
#include <util.h>
#include "rcv.h"
#include "extern.h"
#ifdef USE_EDITLINE
#include "complete.h"
#endif
#include "format.h"
#include "sig.h"
#include "thread.h"
static const char *prompt = DEFAULT_PROMPT;
static int *msgvec;
static int inithdr;
static jmp_buf jmpbuf;
static int reset_on_stop;
#ifdef DEBUG_FILE_LEAK
struct glue {
struct glue *next;
int niobs;
FILE *iobs;
};
extern struct glue __sglue;
static int open_fd_cnt;
static int open_fp_cnt;
static int
file_count(void)
{
struct glue *gp;
FILE *fp;
int n;
int cnt;
cnt = 0;
for (gp = &__sglue; gp; gp = gp->next) {
for (fp = gp->iobs, n = gp->niobs; --n >= 0; fp++)
if (fp->_flags)
cnt++;
}
return cnt;
}
static int
fds_count(void)
{
int maxfd;
int cnt;
int fd;
maxfd = fcntl(0, F_MAXFD);
if (maxfd == -1) {
warn("fcntl");
return -1;
}
cnt = 0;
for (fd = 0; fd <= maxfd; fd++) {
struct stat sb;
if (fstat(fd, &sb) != -1)
cnt++;
else if (errno != EBADF
#ifdef BROKEN_CLONE_STAT
&& errno != EOPNOTSUPP
#endif
)
warn("fstat(%d): errno=%d", fd, errno);
}
return cnt;
}
static void
file_leak_init(void)
{
open_fd_cnt = fds_count();
open_fp_cnt = file_count();
}
static void
file_leak_check(void)
{
if (open_fp_cnt != file_count() ||
open_fd_cnt != fds_count()) {
(void)printf("FILE LEAK WARNING: "
"fp-count: %d (%d) "
"fd-count: %d (%d) max-fd: %d\n",
file_count(), open_fp_cnt,
fds_count(), open_fd_cnt,
fcntl(0, F_MAXFD));
}
}
#endif
static void
update_mailname(const char *name)
{
char tbuf[PATHSIZE];
size_t l;
if (name != NULL && realpath(name, mailname) == NULL) {
warn("Can't canonicalize `%s'", name);
return;
}
if (getfold(tbuf, sizeof(tbuf)) >= 0) {
l = strlen(tbuf);
if (l < sizeof(tbuf) - 1)
tbuf[l++] = '/';
if (strncmp(tbuf, mailname, l) == 0) {
char const *sep = "", *cp = mailname + l;
l = strlen(cp);
if (l >= sizeof(displayname)) {
cp += l;
cp -= sizeof(displayname) - 5;
sep = "...";
}
(void)snprintf(displayname, sizeof(displayname),
"+%s%.*s", sep,
(int)(sizeof(displayname) - 1 - strlen(sep)), cp);
return;
}
}
l = strlen(mailname);
if (l < sizeof(displayname))
strcpy(displayname, mailname);
else {
l -= sizeof(displayname) - 4 - sizeof(displayname) / 3;
(void)snprintf(displayname, sizeof(displayname), "%.*s...%s",
(int)sizeof(displayname) / 3, mailname, mailname + l);
}
}
static void
setmsize(int sz)
{
if (msgvec != 0)
free(msgvec);
msgvec = ecalloc((size_t) (sz + 1), sizeof(*msgvec));
}
PUBLIC int
setfile(const char *name)
{
FILE *ibuf;
int i, fd;
struct stat stb;
char isedit = *name != '%' || getuserid(myname) != (int)getuid();
const char *who = name[1] ? name + 1 : myname;
static int shudclob;
char tempname[PATHSIZE];
if ((name = expand(name)) == NULL)
return -1;
if ((ibuf = Fopen(name, "ref")) == NULL) {
if (!isedit && errno == ENOENT)
goto nomail;
warn("Can't open `%s'", name);
return -1;
}
if (fstat(fileno(ibuf), &stb) < 0) {
warn("fstat");
(void)Fclose(ibuf);
return -1;
}
switch (stb.st_mode & S_IFMT) {
case S_IFDIR:
(void)Fclose(ibuf);
errno = EISDIR;
warn("%s", name);
return -1;
case S_IFREG:
break;
default:
(void)Fclose(ibuf);
errno = EINVAL;
warn("%s", name);
return -1;
}
sig_check();
sig_hold();
if (shudclob)
quit(jmpbuf);
readonly = 0;
if ((i = open(name, O_WRONLY)) < 0)
readonly++;
else
(void)close(i);
if (shudclob) {
(void)fclose(itf);
(void)fclose(otf);
}
shudclob = 1;
edit = isedit;
(void)strcpy(prevfile, mailname);
update_mailname(name != mailname ? name : NULL);
mailsize = fsize(ibuf);
(void)snprintf(tempname, sizeof(tempname),
"%s/mail.RxXXXXXXXXXX", tmpdir);
if ((fd = mkstemp(tempname)) == -1 ||
(otf = fdopen(fd, "wef")) == NULL)
err(EXIT_FAILURE, "Can't create tmp file `%s'", tempname);
if ((itf = fopen(tempname, "ref")) == NULL)
err(EXIT_FAILURE, "Can't create tmp file `%s'", tempname);
(void)rm(tempname);
setptr(ibuf, (off_t)0);
setmsize(get_abs_msgCount());
mailsize = ftell(ibuf);
(void)Fclose(ibuf);
sig_release();
sig_check();
sawcom = 0;
if (!edit && get_abs_msgCount() == 0) {
nomail:
(void)fprintf(stderr, "No mail for %s\n", who);
return -1;
}
return 0;
}
PUBLIC int
incfile(void)
{
off_t newsize;
int omsgCount;
FILE *ibuf;
int rval;
omsgCount = get_abs_msgCount();
ibuf = Fopen(mailname, "ref");
if (ibuf == NULL)
return -1;
sig_check();
sig_hold();
newsize = fsize(ibuf);
if (newsize == 0 ||
newsize < mailsize) {
rval = -1;
goto done;
}
if (newsize == mailsize) {
rval = 0;
goto done;
}
setptr(ibuf, mailsize);
setmsize(get_abs_msgCount());
mailsize = ftell(ibuf);
rval = get_abs_msgCount() - omsgCount;
done:
(void)Fclose(ibuf);
sig_release();
sig_check();
return rval;
}
static char *
comment_char(char *line)
{
char *p;
char quotec;
quotec = '\0';
for (p = line; *p; p++) {
if (quotec != '\0') {
if (*p == quotec)
quotec = '\0';
}
else if (*p == '"' || *p == '\'')
quotec = *p;
else if (*p == COMMENT_CHAR)
return p;
}
return NULL;
}
static jmp_buf pipestop;
__dead static void
lex_brokpipe(int signo)
{
longjmp(pipestop, signo);
}
PUBLIC char *
shellpr(char *cp)
{
int quotec;
int level;
if (cp == NULL || value(ENAME_ENABLE_PIPES) == NULL)
return NULL;
level = 0;
quotec = 0;
for (; *cp != '\0'; cp++) {
if (quotec) {
if (*cp == quotec)
quotec = 0;
if (*cp == '\\' &&
(cp[1] == quotec || cp[1] == '\\'))
cp++;
}
else {
switch (*cp) {
case '|':
case '>':
if (level == 0)
return cp;
break;
case '(':
level++;
break;
case ')':
level--;
break;
case '"':
case '\'':
quotec = *cp;
break;
default:
break;
}
}
}
return NULL;
}
static int
do_paging(const char *cmd, int c_pipe)
{
char *cp, *p;
if (value(ENAME_PAGER_OFF) != NULL)
return 0;
if (c_pipe & C_PIPE_PAGER)
return 1;
if (c_pipe & C_PIPE_CRT && value(ENAME_CRT) != NULL)
return 1;
if ((cp = value(ENAME_PAGE_ALSO)) == NULL)
return 0;
if ((p = strcasestr(cp, cmd)) == NULL)
return 0;
if (p != cp && p[-1] != ',' && !is_WSP(p[-1]))
return 0;
p += strlen(cmd);
return (*p == '\0' || *p == ',' || is_WSP(*p));
}
static FILE *fp_stop = NULL;
static int oldfd1 = -1;
static sig_t old_sigpipe;
static int
setup_piping(const char *cmd, char *cmdline, int c_pipe)
{
FILE *fout;
FILE *last_file;
char *cp;
sig_check();
last_file = last_registered_file(0);
fout = NULL;
if ((cp = shellpr(cmdline)) != NULL) {
char c;
c = *cp;
*cp = '\0';
cp++;
if (c == '|') {
if ((fout = Popen(cp, "we")) == NULL) {
warn("Popen: %s", cp);
return -1;
}
}
else {
const char *mode;
assert(c == '>');
mode = *cp == '>' ? "ae" : "we";
if (*cp == '>')
cp++;
cp = skip_WSP(cp);
if ((fout = Fopen(cp, mode)) == NULL) {
warn("Fopen: %s", cp);
return -1;
}
}
}
else if (do_paging(cmd, c_pipe)) {
const char *pager;
pager = value(ENAME_PAGER);
if (pager == NULL || *pager == '\0')
pager = _PATH_MORE;
if ((fout = Popen(pager, "we")) == NULL) {
warn("Popen: %s", pager);
return -1;
}
}
if (fout) {
old_sigpipe = sig_signal(SIGPIPE, lex_brokpipe);
(void)fflush(stdout);
if ((oldfd1 = dup(STDOUT_FILENO)) == -1)
err(EXIT_FAILURE, "dup failed");
if (dup2(fileno(fout), STDOUT_FILENO) == -1)
err(EXIT_FAILURE, "dup2 failed");
fp_stop = last_file;
}
return 0;
}
static void
close_piping(void)
{
sigset_t oset;
struct sigaction osa;
if (oldfd1 != -1) {
(void)fflush(stdout);
if (fileno(stdout) != oldfd1 &&
dup2(oldfd1, STDOUT_FILENO) == -1)
err(EXIT_FAILURE, "dup2 failed");
(void)sig_ignore(SIGPIPE, &osa, &oset);
close_top_files(fp_stop);
fp_stop = NULL;
(void)close(oldfd1);
oldfd1 = -1;
(void)sig_signal(SIGPIPE, old_sigpipe);
(void)sig_restore(SIGPIPE, &osa, &oset);
}
sig_check();
}
static int
isprefix(char *as1, const char *as2)
{
char *s1;
const char *s2;
s1 = as1;
s2 = as2;
while (*s1++ == *s2)
if (*s2++ == '\0')
return 1;
return *--s1 == '\0';
}
PUBLIC const struct cmd *
lex(char word[])
{
const struct cmd *cp;
for (cp = &cmdtab[0]; cp->c_name != NULL; cp++)
if (isprefix(word, cp->c_name))
return cp;
return NULL;
}
PUBLIC char *
get_cmdname(char *buf)
{
char *cp;
char *cmd;
size_t len;
for (cp = buf; *cp; cp++)
if (strchr(" \t0123456789$^.:/-+*'\">|", *cp) != NULL)
break;
if (cp == buf && *cp == '|')
cp++;
len = cp - buf + 1;
cmd = salloc(len);
(void)strlcpy(cmd, buf, len);
return cmd;
}
PUBLIC int
execute(char linebuf[], enum execute_contxt_e contxt)
{
char *word;
char *arglist[MAXARGC];
const struct cmd * volatile com = NULL;
char *volatile cp;
int retval;
int c;
volatile int e = 1;
cp = skip_space(linebuf);
if (*cp == '!') {
if (sourcing) {
(void)printf("Can't \"!\" while sourcing\n");
goto out;
}
(void)shell(cp + 1);
return 0;
}
word = get_cmdname(cp);
cp += strlen(word);
if (sourcing && *word == '\0')
return 0;
com = lex(word);
if (com == NULL) {
(void)printf("Unknown command: \"%s\"\n", word);
goto out;
}
if ((com->c_argtype & F) == 0 && (cond & CSKIP))
return 0;
if (mailmode == mm_sending && (com->c_argtype & M) == 0) {
(void)printf("May not execute \"%s\" while sending\n",
com->c_name);
goto out;
}
if (sourcing && com->c_argtype & I) {
(void)printf("May not execute \"%s\" while sourcing\n",
com->c_name);
goto out;
}
if (readonly && com->c_argtype & W) {
(void)printf("May not execute \"%s\" -- message file is read only\n",
com->c_name);
goto out;
}
if (contxt == ec_composing && com->c_argtype & R) {
(void)printf("Cannot recursively invoke \"%s\"\n", com->c_name);
goto out;
}
if (!sourcing && com->c_pipe && value(ENAME_INTERACTIVE) != NULL) {
sig_check();
if (setjmp(pipestop))
goto out;
if (setup_piping(com->c_name, cp, com->c_pipe) == -1)
goto out;
}
switch (com->c_argtype & ARGTYPE_MASK) {
case MSGLIST:
if (msgvec == 0) {
(void)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) {
(void)printf("No applicable messages\n");
break;
}
e = (*com->c_func)(msgvec);
break;
case NDMLIST:
if (msgvec == 0) {
(void)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:
cp = skip_space(cp);
e = (*com->c_func)(cp);
break;
case RAWLIST:
if ((c = getrawlist(cp, arglist, (int)__arraycount(arglist))) < 0)
break;
if (c < com->c_minargs) {
(void)printf("%s requires at least %d arg(s)\n",
com->c_name, com->c_minargs);
break;
}
if (c > com->c_maxargs) {
(void)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(EXIT_FAILURE, "Unknown argtype");
}
out:
close_piping();
retval = 0;
if (e) {
if (e < 0)
retval = 1;
else if (loading)
retval = 1;
else if (sourcing)
(void)unstack();
}
else if (com != NULL) {
if (contxt != ec_autoprint && com->c_argtype & P &&
value(ENAME_AUTOPRINT) != NULL &&
(dot->m_flag & MDELETED) == 0)
(void)execute(__UNCONST("print ."), ec_autoprint);
if (!sourcing && (com->c_argtype & T) == 0)
sawcom = 1;
}
sig_check();
return retval;
}
__dead static void
lex_intr(int signo)
{
noreset = 0;
if (!inithdr)
sawcom++;
inithdr = 0;
while (sourcing)
(void)unstack();
close_piping();
close_all_files();
if (image >= 0) {
(void)close(image);
image = -1;
}
(void)fprintf(stderr, "Interrupt\n");
longjmp(jmpbuf, signo);
}
__dead static void
lex_hangup(int s __unused)
{
exit(EXIT_FAILURE);
}
static void
lex_stop(int signo)
{
if (reset_on_stop) {
reset_on_stop = 0;
longjmp(jmpbuf, signo);
}
}
PUBLIC void
commands(void)
{
int n;
char linebuf[LINESIZE];
int eofloop;
#ifdef DEBUG_FILE_LEAK
file_leak_init();
#endif
if (!sourcing) {
sig_check();
sig_hold();
(void)sig_signal(SIGINT, lex_intr);
(void)sig_signal(SIGHUP, lex_hangup);
(void)sig_signal(SIGTSTP, lex_stop);
(void)sig_signal(SIGTTOU, lex_stop);
(void)sig_signal(SIGTTIN, lex_stop);
sig_release();
}
(void)setjmp(jmpbuf);
eofloop = 0;
for (;;) {
sig_check();
(void)fflush(stdout);
sreset();
if (!sourcing && value(ENAME_INTERACTIVE) != NULL) {
if ((prompt = value(ENAME_PROMPT)) == NULL)
prompt = DEFAULT_PROMPT;
prompt = smsgprintf(prompt, dot);
if ((value(ENAME_AUTOINC) != NULL) && (incfile() > 0))
(void)printf("New mail has arrived.\n");
#ifndef USE_EDITLINE
reset_on_stop = 1;
(void)printf("%s", prompt);
#endif
}
#ifdef DEBUG_FILE_LEAK
file_leak_check();
#endif
n = 0;
for (;;) {
sig_check();
#ifdef USE_EDITLINE
if (!sourcing) {
char *line;
line = my_gets(&elm.command, prompt, NULL);
if (line == NULL) {
if (n == 0)
n = -1;
break;
}
(void)strlcpy(linebuf, line, sizeof(linebuf));
}
else {
if (readline(input, &linebuf[n], LINESIZE - n, 0) < 0) {
if (n == 0)
n = -1;
break;
}
}
#else
if (readline(input, &linebuf[n], LINESIZE - n, reset_on_stop) < 0) {
if (n == 0)
n = -1;
break;
}
#endif
if (!sourcing)
setscreensize();
if (sourcing) {
char *ptr;
if ((ptr = comment_char(linebuf)) != NULL)
*ptr = '\0';
}
if ((n = (int)strlen(linebuf)) == 0)
break;
n--;
if (linebuf[n] != '\\')
break;
linebuf[n++] = ' ';
}
#ifndef USE_EDITLINE
sig_check();
reset_on_stop = 0;
#endif
if (n < 0) {
char *p;
if (loading)
break;
if (sourcing) {
(void)unstack();
continue;
}
if (value(ENAME_INTERACTIVE) != NULL &&
(p = value(ENAME_IGNOREEOF)) != NULL &&
++eofloop < (*p == '\0' ? 25 : atoi(p))) {
(void)printf("Use \"quit\" to quit.\n");
continue;
}
break;
}
eofloop = 0;
if (execute(linebuf, ec_normal))
break;
}
}
PUBLIC int
newfileinfo(int omsgCount)
{
struct message *mp;
int d, n, s, t, u, mdot;
for (mp = get_abs_message(omsgCount + 1); mp;
mp = next_abs_message(mp))
if (mp->m_flag & MNEW)
break;
if (mp == NULL)
for (mp = get_abs_message(omsgCount + 1); mp;
mp = next_abs_message(mp))
if ((mp->m_flag & MREAD) == 0)
break;
if (mp != NULL)
mdot = get_msgnum(mp);
else
mdot = omsgCount + 1;
#ifdef THREAD_SUPPORT
if (mp != NULL && get_message(1) != NULL && get_message(mdot) != mp)
mdot = 0;
#endif
d = n = s = t = u = 0;
for (mp = get_abs_message(1); mp; mp = next_abs_message(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++;
if (mp->m_flag & MTAGGED)
t++;
}
update_mailname(NULL);
(void)printf("\"%s\": ", displayname);
{
int cnt = get_abs_msgCount();
(void)printf("%d message%s", cnt, cnt == 1 ? "" : "s");
}
if (n > 0)
(void)printf(" %d new", n);
if (u-n > 0)
(void)printf(" %d unread", u);
if (t > 0)
(void)printf(" %d tagged", t);
if (d > 0)
(void)printf(" %d deleted", d);
if (s > 0)
(void)printf(" %d saved", s);
if (readonly)
(void)printf(" [Read only]");
(void)printf("\n");
return mdot;
}
PUBLIC void
announce(void)
{
int vec[2], mdot;
mdot = newfileinfo(0);
vec[0] = mdot;
vec[1] = 0;
if ((dot = get_message(mdot)) == NULL)
dot = get_abs_message(1);
if (get_abs_msgCount() > 0 && value(ENAME_NOHEADER) == NULL) {
inithdr++;
(void)headers(vec);
inithdr = 0;
}
}
PUBLIC int
pversion(void *v __unused)
{
(void)printf("Version %s\n", version);
return 0;
}
PUBLIC void
load(const char *name)
{
FILE *in, *oldin;
if ((in = Fopen(name, "ref")) == NULL)
return;
oldin = input;
input = in;
loading = 1;
sourcing = 1;
commands();
loading = 0;
sourcing = 0;
input = oldin;
(void)Fclose(in);
}