1994-05-27 12:33:43 +00:00
|
|
|
/*
|
|
|
|
* Copyright (c) 1980, 1993
|
|
|
|
* The Regents of the University of California. All rights reserved.
|
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions
|
|
|
|
* are met:
|
|
|
|
* 1. Redistributions of source code must retain the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer.
|
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer in the
|
|
|
|
* documentation and/or other materials provided with the distribution.
|
2017-02-28 23:42:47 +00:00
|
|
|
* 3. Neither the name of the University nor the names of its contributors
|
1994-05-27 12:33:43 +00:00
|
|
|
* may be used to endorse or promote products derived from this software
|
|
|
|
* without specific prior written permission.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
|
|
|
|
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
|
|
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
|
|
|
* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
|
|
|
|
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
|
|
|
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
|
|
|
|
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
|
|
|
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
|
|
|
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
|
|
|
|
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
|
|
|
* SUCH DAMAGE.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifndef lint
|
2001-03-25 04:57:05 +00:00
|
|
|
#if 0
|
2001-12-18 20:52:09 +00:00
|
|
|
static char sccsid[] = "@(#)lex.c 8.2 (Berkeley) 4/20/95";
|
2001-03-25 04:57:05 +00:00
|
|
|
#endif
|
1994-05-27 12:33:43 +00:00
|
|
|
#endif /* not lint */
|
2002-06-30 05:25:07 +00:00
|
|
|
#include <sys/cdefs.h>
|
|
|
|
__FBSDID("$FreeBSD$");
|
1994-05-27 12:33:43 +00:00
|
|
|
|
|
|
|
#include "rcv.h"
|
|
|
|
#include <errno.h>
|
|
|
|
#include <fcntl.h>
|
|
|
|
#include "extern.h"
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Mail -- a mail program
|
|
|
|
*
|
|
|
|
* Lexical processing of commands.
|
|
|
|
*/
|
|
|
|
|
2007-11-08 11:13:03 +00:00
|
|
|
static const char *prompt = "& ";
|
2001-05-27 20:26:22 +00:00
|
|
|
|
|
|
|
extern const struct cmd cmdtab[];
|
|
|
|
extern const char *version;
|
1994-05-27 12:33:43 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Set up editing on the given file name.
|
|
|
|
* If the first character of name is %, we are considered to be
|
|
|
|
* editing the file, otherwise we are reading our mail which has
|
|
|
|
* signficance for mbox and so forth.
|
2004-02-29 20:44:44 +00:00
|
|
|
*
|
|
|
|
* If the -e option is being passed to mail, this function has a
|
|
|
|
* tri-state return code: -1 on error, 0 on no mail, 1 if there is
|
|
|
|
* mail.
|
1994-05-27 12:33:43 +00:00
|
|
|
*/
|
|
|
|
int
|
2010-12-19 16:25:23 +00:00
|
|
|
setfile(char *name)
|
1994-05-27 12:33:43 +00:00
|
|
|
{
|
|
|
|
FILE *ibuf;
|
2004-02-29 20:44:44 +00:00
|
|
|
int checkmode, i, fd;
|
1994-05-27 12:33:43 +00:00
|
|
|
struct stat stb;
|
2001-12-18 20:52:09 +00:00
|
|
|
char isedit = *name != '%' || getuserid(myname) != getuid();
|
1994-05-27 12:33:43 +00:00
|
|
|
char *who = name[1] ? name + 1 : myname;
|
2001-03-25 04:57:05 +00:00
|
|
|
char tempname[PATHSIZE];
|
1994-05-27 12:33:43 +00:00
|
|
|
static int shudclob;
|
|
|
|
|
2004-03-06 13:27:59 +00:00
|
|
|
checkmode = value("checkmode") != NULL;
|
2001-05-27 20:26:22 +00:00
|
|
|
if ((name = expand(name)) == NULL)
|
|
|
|
return (-1);
|
1994-05-27 12:33:43 +00:00
|
|
|
|
|
|
|
if ((ibuf = Fopen(name, "r")) == NULL) {
|
|
|
|
if (!isedit && errno == ENOENT)
|
|
|
|
goto nomail;
|
2001-03-25 04:57:05 +00:00
|
|
|
warn("%s", name);
|
2001-05-27 20:26:22 +00:00
|
|
|
return (-1);
|
1994-05-27 12:33:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (fstat(fileno(ibuf), &stb) < 0) {
|
2001-03-25 04:57:05 +00:00
|
|
|
warn("fstat");
|
2001-05-27 20:26:22 +00:00
|
|
|
(void)Fclose(ibuf);
|
1994-05-27 12:33:43 +00:00
|
|
|
return (-1);
|
|
|
|
}
|
|
|
|
|
2001-03-25 04:57:05 +00:00
|
|
|
if (S_ISDIR(stb.st_mode) || !S_ISREG(stb.st_mode)) {
|
2001-05-27 20:26:22 +00:00
|
|
|
(void)Fclose(ibuf);
|
2001-03-25 04:57:05 +00:00
|
|
|
errno = S_ISDIR(stb.st_mode) ? EISDIR : EINVAL;
|
|
|
|
warn("%s", name);
|
1994-05-27 12:33:43 +00:00
|
|
|
return (-1);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Looks like all will be well. We must now relinquish our
|
|
|
|
* hold on the current set of stuff. Must hold signals
|
|
|
|
* while we are reading the new file, else we will ruin
|
|
|
|
* the message[] data structure.
|
|
|
|
*/
|
|
|
|
|
|
|
|
holdsigs();
|
|
|
|
if (shudclob)
|
|
|
|
quit();
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Copy the messages into /tmp
|
|
|
|
* and set pointers.
|
|
|
|
*/
|
|
|
|
|
|
|
|
readonly = 0;
|
|
|
|
if ((i = open(name, 1)) < 0)
|
|
|
|
readonly++;
|
|
|
|
else
|
2001-05-27 20:26:22 +00:00
|
|
|
(void)close(i);
|
1994-05-27 12:33:43 +00:00
|
|
|
if (shudclob) {
|
2001-05-27 20:26:22 +00:00
|
|
|
(void)fclose(itf);
|
|
|
|
(void)fclose(otf);
|
1994-05-27 12:33:43 +00:00
|
|
|
}
|
|
|
|
shudclob = 1;
|
|
|
|
edit = isedit;
|
2001-03-25 04:57:05 +00:00
|
|
|
strlcpy(prevfile, mailname, sizeof(prevfile));
|
1994-05-27 12:33:43 +00:00
|
|
|
if (name != mailname)
|
2001-03-25 04:57:05 +00:00
|
|
|
strlcpy(mailname, name, sizeof(mailname));
|
1994-05-27 12:33:43 +00:00
|
|
|
mailsize = fsize(ibuf);
|
2001-05-27 20:26:22 +00:00
|
|
|
(void)snprintf(tempname, sizeof(tempname),
|
|
|
|
"%s/mail.RxXXXXXXXXXX", tmpdir);
|
2001-03-25 04:57:05 +00:00
|
|
|
if ((fd = mkstemp(tempname)) == -1 || (otf = fdopen(fd, "w")) == NULL)
|
|
|
|
err(1, "%s", tempname);
|
2001-05-27 20:26:22 +00:00
|
|
|
(void)fcntl(fileno(otf), F_SETFD, 1);
|
2001-03-25 04:57:05 +00:00
|
|
|
if ((itf = fopen(tempname, "r")) == NULL)
|
|
|
|
err(1, "%s", tempname);
|
2001-05-27 20:26:22 +00:00
|
|
|
(void)fcntl(fileno(itf), F_SETFD, 1);
|
|
|
|
(void)rm(tempname);
|
2001-12-18 20:52:09 +00:00
|
|
|
setptr(ibuf, 0);
|
1994-05-27 12:33:43 +00:00
|
|
|
setmsize(msgCount);
|
2001-12-18 20:52:09 +00:00
|
|
|
/*
|
|
|
|
* New mail may have arrived while we were reading
|
|
|
|
* the mail file, so reset mailsize to be where
|
|
|
|
* we really are in the file...
|
|
|
|
*/
|
2001-12-19 21:50:22 +00:00
|
|
|
mailsize = ftello(ibuf);
|
2001-05-27 20:26:22 +00:00
|
|
|
(void)Fclose(ibuf);
|
1994-05-27 12:33:43 +00:00
|
|
|
relsesigs();
|
|
|
|
sawcom = 0;
|
2004-02-29 20:44:44 +00:00
|
|
|
|
|
|
|
if ((checkmode || !edit) && msgCount == 0) {
|
1994-05-27 12:33:43 +00:00
|
|
|
nomail:
|
2004-02-29 20:44:44 +00:00
|
|
|
if (!checkmode) {
|
|
|
|
fprintf(stderr, "No mail for %s\n", who);
|
|
|
|
return (-1);
|
|
|
|
} else
|
|
|
|
return (0);
|
1994-05-27 12:33:43 +00:00
|
|
|
}
|
2004-02-29 20:44:44 +00:00
|
|
|
return (checkmode ? 1 : 0);
|
1994-05-27 12:33:43 +00:00
|
|
|
}
|
|
|
|
|
2001-12-18 20:52:09 +00:00
|
|
|
/*
|
|
|
|
* Incorporate any new mail that has arrived since we first
|
|
|
|
* started reading mail.
|
|
|
|
*/
|
|
|
|
int
|
2010-12-19 16:25:23 +00:00
|
|
|
incfile(void)
|
2001-12-18 20:52:09 +00:00
|
|
|
{
|
2001-12-19 21:50:22 +00:00
|
|
|
off_t newsize;
|
2001-12-18 20:52:09 +00:00
|
|
|
int omsgCount = msgCount;
|
|
|
|
FILE *ibuf;
|
|
|
|
|
|
|
|
ibuf = Fopen(mailname, "r");
|
|
|
|
if (ibuf == NULL)
|
|
|
|
return (-1);
|
|
|
|
holdsigs();
|
|
|
|
newsize = fsize(ibuf);
|
|
|
|
if (newsize == 0)
|
|
|
|
return (-1); /* mail box is now empty??? */
|
|
|
|
if (newsize < mailsize)
|
|
|
|
return (-1); /* mail box has shrunk??? */
|
|
|
|
if (newsize == mailsize)
|
|
|
|
return (0); /* no new mail */
|
|
|
|
setptr(ibuf, mailsize);
|
|
|
|
setmsize(msgCount);
|
2001-12-19 21:50:22 +00:00
|
|
|
mailsize = ftello(ibuf);
|
2001-12-18 20:52:09 +00:00
|
|
|
(void)Fclose(ibuf);
|
|
|
|
relsesigs();
|
|
|
|
return (msgCount - omsgCount);
|
|
|
|
}
|
|
|
|
|
2007-11-08 11:13:03 +00:00
|
|
|
static int *msgvec;
|
|
|
|
static int reset_on_stop; /* do a reset() if stopped */
|
1994-05-27 12:33:43 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Interpret user commands one by one. If standard input is not a tty,
|
|
|
|
* print no prompt.
|
|
|
|
*/
|
|
|
|
void
|
2010-12-19 16:25:23 +00:00
|
|
|
commands(void)
|
1994-05-27 12:33:43 +00:00
|
|
|
{
|
2001-05-27 20:26:22 +00:00
|
|
|
int n, eofloop = 0;
|
1994-05-27 12:33:43 +00:00
|
|
|
char linebuf[LINESIZE];
|
|
|
|
|
|
|
|
if (!sourcing) {
|
|
|
|
if (signal(SIGINT, SIG_IGN) != SIG_IGN)
|
2001-05-27 20:26:22 +00:00
|
|
|
(void)signal(SIGINT, intr);
|
1994-05-27 12:33:43 +00:00
|
|
|
if (signal(SIGHUP, SIG_IGN) != SIG_IGN)
|
2001-05-27 20:26:22 +00:00
|
|
|
(void)signal(SIGHUP, hangup);
|
|
|
|
(void)signal(SIGTSTP, stop);
|
|
|
|
(void)signal(SIGTTOU, stop);
|
|
|
|
(void)signal(SIGTTIN, stop);
|
1994-05-27 12:33:43 +00:00
|
|
|
}
|
|
|
|
setexit();
|
|
|
|
for (;;) {
|
|
|
|
/*
|
|
|
|
* Print the prompt, if needed. Clear out
|
|
|
|
* string space, and flush the output.
|
|
|
|
*/
|
2001-05-27 20:26:22 +00:00
|
|
|
if (!sourcing && value("interactive") != NULL) {
|
2001-12-18 20:52:09 +00:00
|
|
|
if ((value("autoinc") != NULL) && (incfile() > 0))
|
|
|
|
printf("New mail has arrived.\n");
|
1994-05-27 12:33:43 +00:00
|
|
|
reset_on_stop = 1;
|
2000-11-27 06:55:38 +00:00
|
|
|
printf("%s", prompt);
|
1994-05-27 12:33:43 +00:00
|
|
|
}
|
2001-05-27 20:26:22 +00:00
|
|
|
(void)fflush(stdout);
|
1994-05-27 12:33:43 +00:00
|
|
|
sreset();
|
|
|
|
/*
|
|
|
|
* Read a line of commands from the current input
|
|
|
|
* and handle end of file specially.
|
|
|
|
*/
|
|
|
|
n = 0;
|
|
|
|
for (;;) {
|
|
|
|
if (readline(input, &linebuf[n], LINESIZE - 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) {
|
|
|
|
/* eof */
|
|
|
|
if (loading)
|
|
|
|
break;
|
|
|
|
if (sourcing) {
|
|
|
|
unstack();
|
|
|
|
continue;
|
|
|
|
}
|
2001-05-27 20:26:22 +00:00
|
|
|
if (value("interactive") != NULL &&
|
|
|
|
value("ignoreeof") != NULL &&
|
1994-05-27 12:33:43 +00:00
|
|
|
++eofloop < 25) {
|
|
|
|
printf("Use \"quit\" to quit.\n");
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
eofloop = 0;
|
|
|
|
if (execute(linebuf, 0))
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Execute a single command.
|
|
|
|
* Command functions return 0 for success, 1 for error, and -1
|
|
|
|
* for abort. A 1 or -1 aborts a load or source. A -1 aborts
|
|
|
|
* the interactive command loop.
|
|
|
|
* Contxt is non-zero if called while composing mail.
|
|
|
|
*/
|
|
|
|
int
|
2010-12-19 16:25:23 +00:00
|
|
|
execute(char linebuf[], int contxt)
|
1994-05-27 12:33:43 +00:00
|
|
|
{
|
|
|
|
char word[LINESIZE];
|
|
|
|
char *arglist[MAXARGC];
|
2001-05-27 20:26:22 +00:00
|
|
|
const struct cmd *com;
|
|
|
|
char *cp, *cp2;
|
|
|
|
int c, muvec[2];
|
1994-05-27 12:33:43 +00:00
|
|
|
int e = 1;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Strip the white space away from the beginning
|
|
|
|
* of the command, then scan out a word, which
|
|
|
|
* consists of anything except digits and white space.
|
|
|
|
*
|
|
|
|
* Handle ! escapes differently to get the correct
|
|
|
|
* lexical conventions.
|
|
|
|
*/
|
|
|
|
|
2001-12-19 21:50:22 +00:00
|
|
|
for (cp = linebuf; isspace((unsigned char)*cp); cp++)
|
1994-05-27 12:33:43 +00:00
|
|
|
;
|
|
|
|
if (*cp == '!') {
|
|
|
|
if (sourcing) {
|
|
|
|
printf("Can't \"!\" while sourcing\n");
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
shell(cp+1);
|
2001-05-27 20:26:22 +00:00
|
|
|
return (0);
|
1994-05-27 12:33:43 +00:00
|
|
|
}
|
|
|
|
cp2 = word;
|
2001-05-27 20:26:22 +00:00
|
|
|
while (*cp != '\0' && strchr(" \t0123456789$^.:/-+*'\"", *cp) == NULL)
|
1994-05-27 12:33:43 +00:00
|
|
|
*cp2++ = *cp++;
|
|
|
|
*cp2 = '\0';
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Look up the command; if not found, bitch.
|
|
|
|
* Normally, a blank command would map to the
|
|
|
|
* first command in the table; while sourcing,
|
|
|
|
* however, we ignore blank lines to eliminate
|
|
|
|
* confusion.
|
|
|
|
*/
|
|
|
|
|
|
|
|
if (sourcing && *word == '\0')
|
2001-05-27 20:26:22 +00:00
|
|
|
return (0);
|
1994-05-27 12:33:43 +00:00
|
|
|
com = lex(word);
|
2001-05-27 20:26:22 +00:00
|
|
|
if (com == NULL) {
|
1994-05-27 12:33:43 +00:00
|
|
|
printf("Unknown command: \"%s\"\n", word);
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* See if we should execute the command -- if a conditional
|
|
|
|
* we always execute it, otherwise, check the state of cond.
|
|
|
|
*/
|
|
|
|
|
|
|
|
if ((com->c_argtype & F) == 0)
|
2001-05-27 20:26:22 +00:00
|
|
|
if ((cond == CRCV && !rcvmode) || (cond == CSEND && rcvmode))
|
|
|
|
return (0);
|
1994-05-27 12:33:43 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Process the arguments to the command, depending
|
|
|
|
* on the type he expects. Default to an error.
|
|
|
|
* If we are sourcing an interactive command, it's
|
|
|
|
* an error.
|
|
|
|
*/
|
|
|
|
|
|
|
|
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:
|
|
|
|
/*
|
|
|
|
* A message list defaulting to nearest forward
|
|
|
|
* legal message.
|
|
|
|
*/
|
|
|
|
if (msgvec == 0) {
|
|
|
|
printf("Illegal use of \"message list\"\n");
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if ((c = getmsglist(cp, msgvec, com->c_msgflag)) < 0)
|
|
|
|
break;
|
|
|
|
if (c == 0) {
|
2001-05-27 20:26:22 +00:00
|
|
|
*msgvec = first(com->c_msgflag, com->c_msgmask);
|
1997-09-18 14:08:40 +00:00
|
|
|
msgvec[1] = 0;
|
1994-05-27 12:33:43 +00:00
|
|
|
}
|
1997-09-18 14:08:40 +00:00
|
|
|
if (*msgvec == 0) {
|
1994-05-27 12:33:43 +00:00
|
|
|
printf("No applicable messages\n");
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
e = (*com->c_func)(msgvec);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case NDMLIST:
|
|
|
|
/*
|
|
|
|
* A message list with no defaults, but no error
|
|
|
|
* if none exist.
|
|
|
|
*/
|
|
|
|
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:
|
|
|
|
/*
|
|
|
|
* Just the straight string, with
|
|
|
|
* leading blanks removed.
|
|
|
|
*/
|
2001-12-19 21:50:22 +00:00
|
|
|
while (isspace((unsigned char)*cp))
|
1994-05-27 12:33:43 +00:00
|
|
|
cp++;
|
|
|
|
e = (*com->c_func)(cp);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case RAWLIST:
|
|
|
|
/*
|
|
|
|
* A vector of strings, in shell style.
|
|
|
|
*/
|
|
|
|
if ((c = getrawlist(cp, arglist,
|
2001-05-27 20:26:22 +00:00
|
|
|
sizeof(arglist) / sizeof(*arglist))) < 0)
|
1994-05-27 12:33:43 +00:00
|
|
|
break;
|
|
|
|
if (c < com->c_minargs) {
|
|
|
|
printf("%s requires at least %d arg(s)\n",
|
2001-05-27 20:26:22 +00:00
|
|
|
com->c_name, com->c_minargs);
|
1994-05-27 12:33:43 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (c > com->c_maxargs) {
|
|
|
|
printf("%s takes no more than %d arg(s)\n",
|
2001-05-27 20:26:22 +00:00
|
|
|
com->c_name, com->c_maxargs);
|
1994-05-27 12:33:43 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
e = (*com->c_func)(arglist);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case NOLIST:
|
|
|
|
/*
|
|
|
|
* Just the constant zero, for exiting,
|
|
|
|
* eg.
|
|
|
|
*/
|
|
|
|
e = (*com->c_func)(0);
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
2001-03-25 04:57:05 +00:00
|
|
|
errx(1, "Unknown argtype");
|
1994-05-27 12:33:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
out:
|
|
|
|
/*
|
|
|
|
* Exit the current source file on
|
|
|
|
* error.
|
|
|
|
*/
|
|
|
|
if (e) {
|
|
|
|
if (e < 0)
|
2001-05-27 20:26:22 +00:00
|
|
|
return (1);
|
1994-05-27 12:33:43 +00:00
|
|
|
if (loading)
|
2001-05-27 20:26:22 +00:00
|
|
|
return (1);
|
1994-05-27 12:33:43 +00:00
|
|
|
if (sourcing)
|
|
|
|
unstack();
|
2001-05-27 20:26:22 +00:00
|
|
|
return (0);
|
1994-05-27 12:33:43 +00:00
|
|
|
}
|
2001-12-18 20:52:09 +00:00
|
|
|
if (com == NULL)
|
|
|
|
return (0);
|
2001-05-27 20:26:22 +00:00
|
|
|
if (value("autoprint") != NULL && com->c_argtype & P)
|
1994-05-27 12:33:43 +00:00
|
|
|
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;
|
2001-05-27 20:26:22 +00:00
|
|
|
return (0);
|
1994-05-27 12:33:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Set the size of the message vector used to construct argument
|
|
|
|
* lists to message list functions.
|
|
|
|
*/
|
|
|
|
void
|
2010-12-19 16:25:23 +00:00
|
|
|
setmsize(int sz)
|
1994-05-27 12:33:43 +00:00
|
|
|
{
|
|
|
|
|
2001-12-18 20:52:09 +00:00
|
|
|
if (msgvec != NULL)
|
2001-05-27 20:26:22 +00:00
|
|
|
(void)free(msgvec);
|
|
|
|
msgvec = calloc((unsigned)(sz + 1), sizeof(*msgvec));
|
1994-05-27 12:33:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Find the correct command in the command table corresponding
|
|
|
|
* to the passed command "word"
|
|
|
|
*/
|
|
|
|
|
2011-12-13 13:32:56 +00:00
|
|
|
const struct cmd *
|
2010-12-19 16:25:23 +00:00
|
|
|
lex(char word[])
|
1994-05-27 12:33:43 +00:00
|
|
|
{
|
2001-05-27 20:26:22 +00:00
|
|
|
const struct cmd *cp;
|
1994-05-27 12:33:43 +00:00
|
|
|
|
1995-08-15 19:40:26 +00:00
|
|
|
/*
|
2001-05-27 20:26:22 +00:00
|
|
|
* ignore trailing chars after `#'
|
1995-08-15 19:40:26 +00:00
|
|
|
*
|
|
|
|
* lines with beginning `#' are comments
|
2001-03-25 04:57:05 +00:00
|
|
|
* spaces before `#' are ignored in execute()
|
1995-08-15 19:40:26 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
if (*word == '#')
|
|
|
|
*(word+1) = '\0';
|
|
|
|
|
|
|
|
|
2001-05-27 20:26:22 +00:00
|
|
|
for (cp = &cmdtab[0]; cp->c_name != NULL; cp++)
|
1994-05-27 12:33:43 +00:00
|
|
|
if (isprefix(word, cp->c_name))
|
2001-05-27 20:26:22 +00:00
|
|
|
return (cp);
|
|
|
|
return (NULL);
|
1994-05-27 12:33:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Determine if as1 is a valid prefix of as2.
|
|
|
|
* Return true if yep.
|
|
|
|
*/
|
|
|
|
int
|
2010-12-19 16:25:23 +00:00
|
|
|
isprefix(const char *as1, const char *as2)
|
1994-05-27 12:33:43 +00:00
|
|
|
{
|
2001-05-27 20:26:22 +00:00
|
|
|
const char *s1, *s2;
|
1994-05-27 12:33:43 +00:00
|
|
|
|
|
|
|
s1 = as1;
|
|
|
|
s2 = as2;
|
|
|
|
while (*s1++ == *s2)
|
|
|
|
if (*s2++ == '\0')
|
2001-05-27 20:26:22 +00:00
|
|
|
return (1);
|
|
|
|
return (*--s1 == '\0');
|
1994-05-27 12:33:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* The following gets called on receipt of an interrupt. This is
|
|
|
|
* to abort printout of a command, mainly.
|
|
|
|
* Dispatching here when command() is inactive crashes rcv.
|
|
|
|
* Close all open files except 0, 1, 2, and the temporary.
|
|
|
|
* Also, unstack all source files.
|
|
|
|
*/
|
|
|
|
|
2007-11-08 11:13:03 +00:00
|
|
|
static int inithdr; /* am printing startup headers */
|
1994-05-27 12:33:43 +00:00
|
|
|
|
|
|
|
void
|
2010-12-19 16:25:23 +00:00
|
|
|
intr(int s __unused)
|
1994-05-27 12:33:43 +00:00
|
|
|
{
|
|
|
|
|
|
|
|
noreset = 0;
|
|
|
|
if (!inithdr)
|
|
|
|
sawcom++;
|
|
|
|
inithdr = 0;
|
|
|
|
while (sourcing)
|
|
|
|
unstack();
|
|
|
|
|
|
|
|
close_all_files();
|
|
|
|
|
|
|
|
if (image >= 0) {
|
2001-05-27 20:26:22 +00:00
|
|
|
(void)close(image);
|
1994-05-27 12:33:43 +00:00
|
|
|
image = -1;
|
|
|
|
}
|
|
|
|
fprintf(stderr, "Interrupt\n");
|
|
|
|
reset(0);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* When we wake up after ^Z, reprint the prompt.
|
|
|
|
*/
|
|
|
|
void
|
2010-12-19 16:25:23 +00:00
|
|
|
stop(int s)
|
1994-05-27 12:33:43 +00:00
|
|
|
{
|
|
|
|
sig_t old_action = signal(s, SIG_DFL);
|
2001-12-18 20:52:09 +00:00
|
|
|
sigset_t nset;
|
1994-05-27 12:33:43 +00:00
|
|
|
|
2001-12-18 20:52:09 +00:00
|
|
|
(void)sigemptyset(&nset);
|
|
|
|
(void)sigaddset(&nset, s);
|
|
|
|
(void)sigprocmask(SIG_UNBLOCK, &nset, NULL);
|
2001-05-27 20:26:22 +00:00
|
|
|
(void)kill(0, s);
|
2001-12-18 20:52:09 +00:00
|
|
|
(void)sigprocmask(SIG_BLOCK, &nset, NULL);
|
2001-05-27 20:26:22 +00:00
|
|
|
(void)signal(s, old_action);
|
1994-05-27 12:33:43 +00:00
|
|
|
if (reset_on_stop) {
|
|
|
|
reset_on_stop = 0;
|
|
|
|
reset(0);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Branch here on hangup signal and simulate "exit".
|
|
|
|
*/
|
|
|
|
void
|
2010-12-19 16:25:23 +00:00
|
|
|
hangup(int s __unused)
|
1994-05-27 12:33:43 +00:00
|
|
|
{
|
|
|
|
|
|
|
|
/* nothing to do? */
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Announce the presence of the current Mail version,
|
|
|
|
* give the message count, and print a header listing.
|
|
|
|
*/
|
|
|
|
void
|
2010-12-19 16:25:23 +00:00
|
|
|
announce(void)
|
1994-05-27 12:33:43 +00:00
|
|
|
{
|
|
|
|
int vec[2], mdot;
|
|
|
|
|
2001-12-18 20:52:09 +00:00
|
|
|
mdot = newfileinfo(0);
|
1994-05-27 12:33:43 +00:00
|
|
|
vec[0] = mdot;
|
|
|
|
vec[1] = 0;
|
|
|
|
dot = &message[mdot - 1];
|
2001-05-27 20:26:22 +00:00
|
|
|
if (msgCount > 0 && value("noheader") == NULL) {
|
1994-05-27 12:33:43 +00:00
|
|
|
inithdr++;
|
|
|
|
headers(vec);
|
|
|
|
inithdr = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Announce information about the file we are editing.
|
|
|
|
* Return a likely place to set dot.
|
|
|
|
*/
|
|
|
|
int
|
2010-12-19 16:25:23 +00:00
|
|
|
newfileinfo(int omsgCount)
|
1994-05-27 12:33:43 +00:00
|
|
|
{
|
2001-05-27 20:26:22 +00:00
|
|
|
struct message *mp;
|
|
|
|
int u, n, mdot, d, s;
|
2001-03-25 04:57:05 +00:00
|
|
|
char fname[PATHSIZE+1], zname[PATHSIZE+1], *ename;
|
1994-05-27 12:33:43 +00:00
|
|
|
|
2001-12-18 20:52:09 +00:00
|
|
|
for (mp = &message[omsgCount]; mp < &message[msgCount]; mp++)
|
1994-05-27 12:33:43 +00:00
|
|
|
if (mp->m_flag & MNEW)
|
|
|
|
break;
|
|
|
|
if (mp >= &message[msgCount])
|
2001-12-18 20:52:09 +00:00
|
|
|
for (mp = &message[omsgCount]; mp < &message[msgCount]; mp++)
|
1994-05-27 12:33:43 +00:00
|
|
|
if ((mp->m_flag & MREAD) == 0)
|
|
|
|
break;
|
|
|
|
if (mp < &message[msgCount])
|
|
|
|
mdot = mp - &message[0] + 1;
|
|
|
|
else
|
2001-12-18 20:52:09 +00:00
|
|
|
mdot = omsgCount + 1;
|
1994-05-27 12:33:43 +00:00
|
|
|
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;
|
2001-03-25 04:57:05 +00:00
|
|
|
if (getfold(fname, sizeof(fname) - 1) >= 0) {
|
1994-05-27 12:33:43 +00:00
|
|
|
strcat(fname, "/");
|
|
|
|
if (strncmp(fname, mailname, strlen(fname)) == 0) {
|
2001-05-27 20:26:22 +00:00
|
|
|
(void)snprintf(zname, sizeof(zname), "+%s",
|
|
|
|
mailname + strlen(fname));
|
1994-05-27 12:33:43 +00:00
|
|
|
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");
|
2001-05-27 20:26:22 +00:00
|
|
|
return (mdot);
|
1994-05-27 12:33:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Print the current version number.
|
|
|
|
*/
|
|
|
|
|
|
|
|
int
|
2010-12-19 16:25:23 +00:00
|
|
|
pversion(int e __unused)
|
1994-05-27 12:33:43 +00:00
|
|
|
{
|
|
|
|
|
|
|
|
printf("Version %s\n", version);
|
2001-05-27 20:26:22 +00:00
|
|
|
return (0);
|
1994-05-27 12:33:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Load a file of user definitions.
|
|
|
|
*/
|
|
|
|
void
|
2010-12-19 16:25:23 +00:00
|
|
|
load(char *name)
|
1994-05-27 12:33:43 +00:00
|
|
|
{
|
2001-05-27 20:26:22 +00:00
|
|
|
FILE *in, *oldin;
|
1994-05-27 12:33:43 +00:00
|
|
|
|
|
|
|
if ((in = Fopen(name, "r")) == NULL)
|
|
|
|
return;
|
|
|
|
oldin = input;
|
|
|
|
input = in;
|
|
|
|
loading = 1;
|
|
|
|
sourcing = 1;
|
|
|
|
commands();
|
|
|
|
loading = 0;
|
|
|
|
sourcing = 0;
|
|
|
|
input = oldin;
|
2001-05-27 20:26:22 +00:00
|
|
|
(void)Fclose(in);
|
1994-05-27 12:33:43 +00:00
|
|
|
}
|