2 * Copyright (c) 1980, 1993
3 * The Regents of the University of California. All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 3. All advertising materials mentioning features or use of this software
14 * must display the following acknowledgement:
15 * This product includes software developed by the University of
16 * California, Berkeley and its contributors.
17 * 4. Neither the name of the University nor the names of its contributors
18 * may be used to endorse or promote products derived from this software
19 * without specific prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
36 static char sccsid[] = "@(#)lex.c 8.2 (Berkeley) 4/20/95";
39 #include <sys/cdefs.h>
40 __FBSDID("$FreeBSD: src/usr.bin/mail/lex.c,v 1.5.6.5 2003/01/06 05:46:03 mikeh Exp $");
48 * Mail -- a mail program
50 * Lexical processing of commands.
53 const char *prompt = "& ";
55 extern const struct cmd cmdtab[];
56 extern const char *version;
59 * Set up editing on the given file name.
60 * If the first character of name is %, we are considered to be
61 * editing the file, otherwise we are reading our mail which has
62 * signficance for mbox and so forth.
71 char isedit = *name != '%' || getuserid(myname) != getuid();
72 char *who = name[1] ? name + 1 : myname;
73 char tempname[PATHSIZE];
76 if ((name = expand(name)) == NULL)
79 if ((ibuf = Fopen(name, "r")) == NULL) {
80 if (!isedit && errno == ENOENT)
86 if (fstat(fileno(ibuf), &stb) < 0) {
92 if (S_ISDIR(stb.st_mode) || !S_ISREG(stb.st_mode)) {
94 errno = S_ISDIR(stb.st_mode) ? EISDIR : EINVAL;
100 * Looks like all will be well. We must now relinquish our
101 * hold on the current set of stuff. Must hold signals
102 * while we are reading the new file, else we will ruin
103 * the message[] data structure.
111 * Copy the messages into /tmp
116 if ((i = open(name, 1)) < 0)
126 strlcpy(prevfile, mailname, sizeof(prevfile));
127 if (name != mailname)
128 strlcpy(mailname, name, sizeof(mailname));
129 mailsize = fsize(ibuf);
130 (void)snprintf(tempname, sizeof(tempname),
131 "%s/mail.RxXXXXXXXXXX", tmpdir);
132 if ((fd = mkstemp(tempname)) == -1 || (otf = fdopen(fd, "w")) == NULL)
133 err(1, "%s", tempname);
134 (void)fcntl(fileno(otf), F_SETFD, 1);
135 if ((itf = fopen(tempname, "r")) == NULL)
136 err(1, "%s", tempname);
137 (void)fcntl(fileno(itf), F_SETFD, 1);
142 * New mail may have arrived while we were reading
143 * the mail file, so reset mailsize to be where
144 * we really are in the file...
146 mailsize = ftello(ibuf);
150 if (!edit && msgCount == 0) {
152 fprintf(stderr, "No mail for %s\n", who);
159 * Incorporate any new mail that has arrived since we first
160 * started reading mail.
166 int omsgCount = msgCount;
169 ibuf = Fopen(mailname, "r");
173 newsize = fsize(ibuf);
175 return (-1); /* mail box is now empty??? */
176 if (newsize < mailsize)
177 return (-1); /* mail box has shrunk??? */
178 if (newsize == mailsize)
179 return (0); /* no new mail */
180 setptr(ibuf, mailsize);
182 mailsize = ftello(ibuf);
185 return (msgCount - omsgCount);
189 int reset_on_stop; /* do a reset() if stopped */
192 * Interpret user commands one by one. If standard input is not a tty,
199 char linebuf[LINESIZE];
202 if (signal(SIGINT, SIG_IGN) != SIG_IGN)
203 (void)signal(SIGINT, intr);
204 if (signal(SIGHUP, SIG_IGN) != SIG_IGN)
205 (void)signal(SIGHUP, hangup);
206 (void)signal(SIGTSTP, stop);
207 (void)signal(SIGTTOU, stop);
208 (void)signal(SIGTTIN, stop);
213 * Print the prompt, if needed. Clear out
214 * string space, and flush the output.
216 if (!sourcing && value("interactive") != NULL) {
217 if ((value("autoinc") != NULL) && (incfile() > 0))
218 printf("New mail has arrived.\n");
220 printf("%s", prompt);
222 (void)fflush(stdout);
225 * Read a line of commands from the current input
226 * and handle end of file specially.
230 if (readline(input, &linebuf[n], LINESIZE - n) < 0) {
235 if ((n = strlen(linebuf)) == 0)
238 if (linebuf[n] != '\\')
251 if (value("interactive") != NULL &&
252 value("ignoreeof") != NULL &&
254 printf("Use \"quit\" to quit.\n");
260 if (execute(linebuf, 0))
266 * Execute a single command.
267 * Command functions return 0 for success, 1 for error, and -1
268 * for abort. A 1 or -1 aborts a load or source. A -1 aborts
269 * the interactive command loop.
270 * Contxt is non-zero if called while composing mail.
273 execute(linebuf, contxt)
278 char *arglist[MAXARGC];
279 const struct cmd *com;
285 * Strip the white space away from the beginning
286 * of the command, then scan out a word, which
287 * consists of anything except digits and white space.
289 * Handle ! escapes differently to get the correct
290 * lexical conventions.
293 for (cp = linebuf; isspace((unsigned char)*cp); cp++)
297 printf("Can't \"!\" while sourcing\n");
304 while (*cp != '\0' && strchr(" \t0123456789$^.:/-+*'\"", *cp) == NULL)
309 * Look up the command; if not found, bitch.
310 * Normally, a blank command would map to the
311 * first command in the table; while sourcing,
312 * however, we ignore blank lines to eliminate
316 if (sourcing && *word == '\0')
320 printf("Unknown command: \"%s\"\n", word);
325 * See if we should execute the command -- if a conditional
326 * we always execute it, otherwise, check the state of cond.
329 if ((com->c_argtype & F) == 0)
330 if ((cond == CRCV && !rcvmode) || (cond == CSEND && rcvmode))
334 * Process the arguments to the command, depending
335 * on the type he expects. Default to an error.
336 * If we are sourcing an interactive command, it's
340 if (!rcvmode && (com->c_argtype & M) == 0) {
341 printf("May not execute \"%s\" while sending\n",
345 if (sourcing && com->c_argtype & I) {
346 printf("May not execute \"%s\" while sourcing\n",
350 if (readonly && com->c_argtype & W) {
351 printf("May not execute \"%s\" -- message file is read only\n",
355 if (contxt && com->c_argtype & R) {
356 printf("Cannot recursively invoke \"%s\"\n", com->c_name);
359 switch (com->c_argtype & ~(F|P|I|M|T|W|R)) {
362 * A message list defaulting to nearest forward
366 printf("Illegal use of \"message list\"\n");
369 if ((c = getmsglist(cp, msgvec, com->c_msgflag)) < 0)
372 *msgvec = first(com->c_msgflag, com->c_msgmask);
376 printf("No applicable messages\n");
379 e = (*com->c_func)(msgvec);
384 * A message list with no defaults, but no error
388 printf("Illegal use of \"message list\"\n");
391 if (getmsglist(cp, msgvec, com->c_msgflag) < 0)
393 e = (*com->c_func)(msgvec);
398 * Just the straight string, with
399 * leading blanks removed.
401 while (isspace((unsigned char)*cp))
403 e = (*com->c_func)(cp);
408 * A vector of strings, in shell style.
410 if ((c = getrawlist(cp, arglist,
411 sizeof(arglist) / sizeof(*arglist))) < 0)
413 if (c < com->c_minargs) {
414 printf("%s requires at least %d arg(s)\n",
415 com->c_name, com->c_minargs);
418 if (c > com->c_maxargs) {
419 printf("%s takes no more than %d arg(s)\n",
420 com->c_name, com->c_maxargs);
423 e = (*com->c_func)(arglist);
428 * Just the constant zero, for exiting,
431 e = (*com->c_func)(0);
435 errx(1, "Unknown argtype");
440 * Exit the current source file on
454 if (value("autoprint") != NULL && com->c_argtype & P)
455 if ((dot->m_flag & MDELETED) == 0) {
456 muvec[0] = dot - &message[0] + 1;
460 if (!sourcing && (com->c_argtype & T) == 0)
466 * Set the size of the message vector used to construct argument
467 * lists to message list functions.
476 msgvec = calloc((unsigned)(sz + 1), sizeof(*msgvec));
480 * Find the correct command in the command table corresponding
481 * to the passed command "word"
488 const struct cmd *cp;
491 * ignore trailing chars after `#'
493 * lines with beginning `#' are comments
494 * spaces before `#' are ignored in execute()
501 for (cp = &cmdtab[0]; cp->c_name != NULL; cp++)
502 if (isprefix(word, cp->c_name))
508 * Determine if as1 is a valid prefix of as2.
509 * Return true if yep.
513 const char *as1, *as2;
522 return (*--s1 == '\0');
526 * The following gets called on receipt of an interrupt. This is
527 * to abort printout of a command, mainly.
528 * Dispatching here when command() is inactive crashes rcv.
529 * Close all open files except 0, 1, 2, and the temporary.
530 * Also, unstack all source files.
533 int inithdr; /* am printing startup headers */
554 fprintf(stderr, "Interrupt\n");
559 * When we wake up after ^Z, reprint the prompt.
565 sig_t old_action = signal(s, SIG_DFL);
568 (void)sigemptyset(&nset);
569 (void)sigaddset(&nset, s);
570 (void)sigprocmask(SIG_UNBLOCK, &nset, NULL);
572 (void)sigprocmask(SIG_BLOCK, &nset, NULL);
573 (void)signal(s, old_action);
581 * Branch here on hangup signal and simulate "exit".
594 * Announce the presence of the current Mail version,
595 * give the message count, and print a header listing.
602 mdot = newfileinfo(0);
605 dot = &message[mdot - 1];
606 if (msgCount > 0 && value("noheader") == NULL) {
614 * Announce information about the file we are editing.
615 * Return a likely place to set dot.
618 newfileinfo(omsgCount)
622 int u, n, mdot, d, s;
623 char fname[PATHSIZE+1], zname[PATHSIZE+1], *ename;
625 for (mp = &message[omsgCount]; mp < &message[msgCount]; mp++)
626 if (mp->m_flag & MNEW)
628 if (mp >= &message[msgCount])
629 for (mp = &message[omsgCount]; mp < &message[msgCount]; mp++)
630 if ((mp->m_flag & MREAD) == 0)
632 if (mp < &message[msgCount])
633 mdot = mp - &message[0] + 1;
635 mdot = omsgCount + 1;
637 for (mp = &message[0], n = 0, u = 0; mp < &message[msgCount]; mp++) {
638 if (mp->m_flag & MNEW)
640 if ((mp->m_flag & MREAD) == 0)
642 if (mp->m_flag & MDELETED)
644 if (mp->m_flag & MSAVED)
648 if (getfold(fname, sizeof(fname) - 1) >= 0) {
650 if (strncmp(fname, mailname, strlen(fname)) == 0) {
651 (void)snprintf(zname, sizeof(zname), "+%s",
652 mailname + strlen(fname));
656 printf("\"%s\": ", ename);
660 printf("%d messages", msgCount);
662 printf(" %d new", n);
664 printf(" %d unread", u);
666 printf(" %d deleted", d);
668 printf(" %d saved", s);
670 printf(" [Read only]");
676 * Print the current version number.
685 printf("Version %s\n", version);
690 * Load a file of user definitions.
698 if ((in = Fopen(name, "r")) == NULL)