2 * SPDX-License-Identifier: BSD-3-Clause
4 * Copyright (c) 1980, 1993
5 * The Regents of the University of California. All rights reserved.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. Neither the name of the University nor the names of its contributors
16 * may be used to endorse or promote products derived from this software
17 * without specific prior written permission.
19 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34 static char sccsid[] = "@(#)lex.c 8.2 (Berkeley) 4/20/95";
37 #include <sys/cdefs.h>
38 __FBSDID("$FreeBSD$");
46 * Mail -- a mail program
48 * Lexical processing of commands.
51 static const char *prompt = "& ";
53 extern const struct cmd cmdtab[];
54 extern const char *version;
57 * Set up editing on the given file name.
58 * If the first character of name is %, we are considered to be
59 * editing the file, otherwise we are reading our mail which has
60 * signficance for mbox and so forth.
62 * If the -e option is being passed to mail, this function has a
63 * tri-state return code: -1 on error, 0 on no mail, 1 if there is
72 char isedit = *name != '%' || getuserid(myname) != getuid();
73 char *who = name[1] ? name + 1 : myname;
74 char tempname[PATHSIZE];
77 checkmode = value("checkmode") != NULL;
78 if ((name = expand(name)) == NULL)
81 if ((ibuf = Fopen(name, "r")) == NULL) {
82 if (!isedit && errno == ENOENT)
88 if (fstat(fileno(ibuf), &stb) < 0) {
94 if (S_ISDIR(stb.st_mode) || !S_ISREG(stb.st_mode)) {
96 errno = S_ISDIR(stb.st_mode) ? EISDIR : EINVAL;
102 * Looks like all will be well. We must now relinquish our
103 * hold on the current set of stuff. Must hold signals
104 * while we are reading the new file, else we will ruin
105 * the message[] data structure.
113 * Copy the messages into /tmp
118 if ((i = open(name, 1)) < 0)
128 strlcpy(prevfile, mailname, sizeof(prevfile));
129 if (name != mailname)
130 strlcpy(mailname, name, sizeof(mailname));
131 mailsize = fsize(ibuf);
132 (void)snprintf(tempname, sizeof(tempname),
133 "%s/mail.RxXXXXXXXXXX", tmpdir);
134 if ((fd = mkstemp(tempname)) == -1 || (otf = fdopen(fd, "w")) == NULL)
135 err(1, "%s", tempname);
136 (void)fcntl(fileno(otf), F_SETFD, 1);
137 if ((itf = fopen(tempname, "r")) == NULL)
138 err(1, "%s", tempname);
139 (void)fcntl(fileno(itf), F_SETFD, 1);
144 * New mail may have arrived while we were reading
145 * the mail file, so reset mailsize to be where
146 * we really are in the file...
148 mailsize = ftello(ibuf);
153 if ((checkmode || !edit) && msgCount == 0) {
156 fprintf(stderr, "No mail for %s\n", who);
161 return (checkmode ? 1 : 0);
165 * Incorporate any new mail that has arrived since we first
166 * started reading mail.
172 int omsgCount = msgCount;
175 ibuf = Fopen(mailname, "r");
179 newsize = fsize(ibuf);
181 return (-1); /* mail box is now empty??? */
182 if (newsize < mailsize)
183 return (-1); /* mail box has shrunk??? */
184 if (newsize == mailsize)
185 return (0); /* no new mail */
186 setptr(ibuf, mailsize);
188 mailsize = ftello(ibuf);
191 return (msgCount - omsgCount);
195 static int reset_on_stop; /* do a reset() if stopped */
198 * Interpret user commands one by one. If standard input is not a tty,
205 char linebuf[LINESIZE];
208 if (signal(SIGINT, SIG_IGN) != SIG_IGN)
209 (void)signal(SIGINT, intr);
210 if (signal(SIGHUP, SIG_IGN) != SIG_IGN)
211 (void)signal(SIGHUP, hangup);
212 (void)signal(SIGTSTP, stop);
213 (void)signal(SIGTTOU, stop);
214 (void)signal(SIGTTIN, stop);
219 * Print the prompt, if needed. Clear out
220 * string space, and flush the output.
222 if (!sourcing && value("interactive") != NULL) {
223 if ((value("autoinc") != NULL) && (incfile() > 0))
224 printf("New mail has arrived.\n");
226 printf("%s", prompt);
228 (void)fflush(stdout);
231 * Read a line of commands from the current input
232 * and handle end of file specially.
236 if (readline(input, &linebuf[n], LINESIZE - n) < 0) {
241 if ((n = strlen(linebuf)) == 0)
244 if (linebuf[n] != '\\')
257 if (value("interactive") != NULL &&
258 value("ignoreeof") != NULL &&
260 printf("Use \"quit\" to quit.\n");
266 if (execute(linebuf, 0))
272 * Execute a single command.
273 * Command functions return 0 for success, 1 for error, and -1
274 * for abort. A 1 or -1 aborts a load or source. A -1 aborts
275 * the interactive command loop.
276 * Contxt is non-zero if called while composing mail.
279 execute(char linebuf[], int contxt)
282 char *arglist[MAXARGC];
283 const struct cmd *com;
289 * Strip the white space away from the beginning
290 * of the command, then scan out a word, which
291 * consists of anything except digits and white space.
293 * Handle ! escapes differently to get the correct
294 * lexical conventions.
297 for (cp = linebuf; isspace((unsigned char)*cp); cp++)
301 printf("Can't \"!\" while sourcing\n");
308 while (*cp != '\0' && strchr(" \t0123456789$^.:/-+*'\"", *cp) == NULL)
313 * Look up the command; if not found, bitch.
314 * Normally, a blank command would map to the
315 * first command in the table; while sourcing,
316 * however, we ignore blank lines to eliminate
320 if (sourcing && *word == '\0')
324 printf("Unknown command: \"%s\"\n", word);
329 * See if we should execute the command -- if a conditional
330 * we always execute it, otherwise, check the state of cond.
333 if ((com->c_argtype & F) == 0)
334 if ((cond == CRCV && !rcvmode) || (cond == CSEND && rcvmode))
338 * Process the arguments to the command, depending
339 * on the type he expects. Default to an error.
340 * If we are sourcing an interactive command, it's
344 if (!rcvmode && (com->c_argtype & M) == 0) {
345 printf("May not execute \"%s\" while sending\n",
349 if (sourcing && com->c_argtype & I) {
350 printf("May not execute \"%s\" while sourcing\n",
354 if (readonly && com->c_argtype & W) {
355 printf("May not execute \"%s\" -- message file is read only\n",
359 if (contxt && com->c_argtype & R) {
360 printf("Cannot recursively invoke \"%s\"\n", com->c_name);
363 switch (com->c_argtype & ~(F|P|I|M|T|W|R)) {
366 * A message list defaulting to nearest forward
370 printf("Illegal use of \"message list\"\n");
373 if ((c = getmsglist(cp, msgvec, com->c_msgflag)) < 0)
376 *msgvec = first(com->c_msgflag, com->c_msgmask);
380 printf("No applicable messages\n");
383 e = (*com->c_func)(msgvec);
388 * A message list with no defaults, but no error
392 printf("Illegal use of \"message list\"\n");
395 if (getmsglist(cp, msgvec, com->c_msgflag) < 0)
397 e = (*com->c_func)(msgvec);
402 * Just the straight string, with
403 * leading blanks removed.
405 while (isspace((unsigned char)*cp))
407 e = (*com->c_func)(cp);
412 * A vector of strings, in shell style.
414 if ((c = getrawlist(cp, arglist,
415 sizeof(arglist) / sizeof(*arglist))) < 0)
417 if (c < com->c_minargs) {
418 printf("%s requires at least %d arg(s)\n",
419 com->c_name, com->c_minargs);
422 if (c > com->c_maxargs) {
423 printf("%s takes no more than %d arg(s)\n",
424 com->c_name, com->c_maxargs);
427 e = (*com->c_func)(arglist);
432 * Just the constant zero, for exiting,
435 e = (*com->c_func)(0);
439 errx(1, "Unknown argtype");
444 * Exit the current source file on
458 if (value("autoprint") != NULL && com->c_argtype & P)
459 if ((dot->m_flag & MDELETED) == 0) {
460 muvec[0] = dot - &message[0] + 1;
464 if (!sourcing && (com->c_argtype & T) == 0)
470 * Set the size of the message vector used to construct argument
471 * lists to message list functions.
479 msgvec = calloc((unsigned)(sz + 1), sizeof(*msgvec));
483 * Find the correct command in the command table corresponding
484 * to the passed command "word"
490 const struct cmd *cp;
493 * ignore trailing chars after `#'
495 * lines with beginning `#' are comments
496 * spaces before `#' are ignored in execute()
503 for (cp = &cmdtab[0]; cp->c_name != NULL; cp++)
504 if (isprefix(word, cp->c_name))
510 * Determine if as1 is a valid prefix of as2.
511 * Return true if yep.
514 isprefix(const char *as1, const char *as2)
523 return (*--s1 == '\0');
527 * The following gets called on receipt of an interrupt. This is
528 * to abort printout of a command, mainly.
529 * Dispatching here when command() is inactive crashes rcv.
530 * Close all open files except 0, 1, 2, and the temporary.
531 * Also, unstack all source files.
534 static int inithdr; /* am printing startup headers */
553 fprintf(stderr, "Interrupt\n");
558 * When we wake up after ^Z, reprint the prompt.
563 sig_t old_action = signal(s, SIG_DFL);
566 (void)sigemptyset(&nset);
567 (void)sigaddset(&nset, s);
568 (void)sigprocmask(SIG_UNBLOCK, &nset, NULL);
570 (void)sigprocmask(SIG_BLOCK, &nset, NULL);
571 (void)signal(s, old_action);
579 * Branch here on hangup signal and simulate "exit".
582 hangup(int s __unused)
590 * Announce the presence of the current Mail version,
591 * give the message count, and print a header listing.
598 mdot = newfileinfo(0);
601 dot = &message[mdot - 1];
602 if (msgCount > 0 && value("noheader") == NULL) {
610 * Announce information about the file we are editing.
611 * Return a likely place to set dot.
614 newfileinfo(int omsgCount)
617 int u, n, mdot, d, s;
618 char fname[PATHSIZE+1], zname[PATHSIZE+1], *ename;
620 for (mp = &message[omsgCount]; mp < &message[msgCount]; mp++)
621 if (mp->m_flag & MNEW)
623 if (mp >= &message[msgCount])
624 for (mp = &message[omsgCount]; mp < &message[msgCount]; mp++)
625 if ((mp->m_flag & MREAD) == 0)
627 if (mp < &message[msgCount])
628 mdot = mp - &message[0] + 1;
630 mdot = omsgCount + 1;
632 for (mp = &message[0], n = 0, u = 0; mp < &message[msgCount]; mp++) {
633 if (mp->m_flag & MNEW)
635 if ((mp->m_flag & MREAD) == 0)
637 if (mp->m_flag & MDELETED)
639 if (mp->m_flag & MSAVED)
643 if (getfold(fname, sizeof(fname) - 1) >= 0) {
645 if (strncmp(fname, mailname, strlen(fname)) == 0) {
646 (void)snprintf(zname, sizeof(zname), "+%s",
647 mailname + strlen(fname));
651 printf("\"%s\": ", ename);
655 printf("%d messages", msgCount);
657 printf(" %d new", n);
659 printf(" %d unread", u);
661 printf(" %d deleted", d);
663 printf(" %d saved", s);
665 printf(" [Read only]");
671 * Print the current version number.
675 pversion(int e __unused)
678 printf("Version %s\n", version);
683 * Load a file of user definitions.
690 if ((in = Fopen(name, "r")) == NULL)