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$");
48 * Mail -- a mail program
50 * Lexical processing of commands.
53 static 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.
64 * If the -e option is being passed to mail, this function has a
65 * tri-state return code: -1 on error, 0 on no mail, 1 if there is
75 char isedit = *name != '%' || getuserid(myname) != getuid();
76 char *who = name[1] ? name + 1 : myname;
77 char tempname[PATHSIZE];
80 checkmode = value("checkmode") != NULL;
81 if ((name = expand(name)) == NULL)
84 if ((ibuf = Fopen(name, "r")) == NULL) {
85 if (!isedit && errno == ENOENT)
91 if (fstat(fileno(ibuf), &stb) < 0) {
97 if (S_ISDIR(stb.st_mode) || !S_ISREG(stb.st_mode)) {
99 errno = S_ISDIR(stb.st_mode) ? EISDIR : EINVAL;
105 * Looks like all will be well. We must now relinquish our
106 * hold on the current set of stuff. Must hold signals
107 * while we are reading the new file, else we will ruin
108 * the message[] data structure.
116 * Copy the messages into /tmp
121 if ((i = open(name, 1)) < 0)
131 strlcpy(prevfile, mailname, sizeof(prevfile));
132 if (name != mailname)
133 strlcpy(mailname, name, sizeof(mailname));
134 mailsize = fsize(ibuf);
135 (void)snprintf(tempname, sizeof(tempname),
136 "%s/mail.RxXXXXXXXXXX", tmpdir);
137 if ((fd = mkstemp(tempname)) == -1 || (otf = fdopen(fd, "w")) == NULL)
138 err(1, "%s", tempname);
139 (void)fcntl(fileno(otf), F_SETFD, 1);
140 if ((itf = fopen(tempname, "r")) == NULL)
141 err(1, "%s", tempname);
142 (void)fcntl(fileno(itf), F_SETFD, 1);
147 * New mail may have arrived while we were reading
148 * the mail file, so reset mailsize to be where
149 * we really are in the file...
151 mailsize = ftello(ibuf);
156 if ((checkmode || !edit) && msgCount == 0) {
159 fprintf(stderr, "No mail for %s\n", who);
164 return (checkmode ? 1 : 0);
168 * Incorporate any new mail that has arrived since we first
169 * started reading mail.
175 int omsgCount = msgCount;
178 ibuf = Fopen(mailname, "r");
182 newsize = fsize(ibuf);
184 return (-1); /* mail box is now empty??? */
185 if (newsize < mailsize)
186 return (-1); /* mail box has shrunk??? */
187 if (newsize == mailsize)
188 return (0); /* no new mail */
189 setptr(ibuf, mailsize);
191 mailsize = ftello(ibuf);
194 return (msgCount - omsgCount);
198 static int reset_on_stop; /* do a reset() if stopped */
201 * Interpret user commands one by one. If standard input is not a tty,
208 char linebuf[LINESIZE];
211 if (signal(SIGINT, SIG_IGN) != SIG_IGN)
212 (void)signal(SIGINT, intr);
213 if (signal(SIGHUP, SIG_IGN) != SIG_IGN)
214 (void)signal(SIGHUP, hangup);
215 (void)signal(SIGTSTP, stop);
216 (void)signal(SIGTTOU, stop);
217 (void)signal(SIGTTIN, stop);
222 * Print the prompt, if needed. Clear out
223 * string space, and flush the output.
225 if (!sourcing && value("interactive") != NULL) {
226 if ((value("autoinc") != NULL) && (incfile() > 0))
227 printf("New mail has arrived.\n");
229 printf("%s", prompt);
231 (void)fflush(stdout);
234 * Read a line of commands from the current input
235 * and handle end of file specially.
239 if (readline(input, &linebuf[n], LINESIZE - n) < 0) {
244 if ((n = strlen(linebuf)) == 0)
247 if (linebuf[n] != '\\')
260 if (value("interactive") != NULL &&
261 value("ignoreeof") != NULL &&
263 printf("Use \"quit\" to quit.\n");
269 if (execute(linebuf, 0))
275 * Execute a single command.
276 * Command functions return 0 for success, 1 for error, and -1
277 * for abort. A 1 or -1 aborts a load or source. A -1 aborts
278 * the interactive command loop.
279 * Contxt is non-zero if called while composing mail.
282 execute(linebuf, contxt)
287 char *arglist[MAXARGC];
288 const struct cmd *com;
294 * Strip the white space away from the beginning
295 * of the command, then scan out a word, which
296 * consists of anything except digits and white space.
298 * Handle ! escapes differently to get the correct
299 * lexical conventions.
302 for (cp = linebuf; isspace((unsigned char)*cp); cp++)
306 printf("Can't \"!\" while sourcing\n");
313 while (*cp != '\0' && strchr(" \t0123456789$^.:/-+*'\"", *cp) == NULL)
318 * Look up the command; if not found, bitch.
319 * Normally, a blank command would map to the
320 * first command in the table; while sourcing,
321 * however, we ignore blank lines to eliminate
325 if (sourcing && *word == '\0')
329 printf("Unknown command: \"%s\"\n", word);
334 * See if we should execute the command -- if a conditional
335 * we always execute it, otherwise, check the state of cond.
338 if ((com->c_argtype & F) == 0)
339 if ((cond == CRCV && !rcvmode) || (cond == CSEND && rcvmode))
343 * Process the arguments to the command, depending
344 * on the type he expects. Default to an error.
345 * If we are sourcing an interactive command, it's
349 if (!rcvmode && (com->c_argtype & M) == 0) {
350 printf("May not execute \"%s\" while sending\n",
354 if (sourcing && com->c_argtype & I) {
355 printf("May not execute \"%s\" while sourcing\n",
359 if (readonly && com->c_argtype & W) {
360 printf("May not execute \"%s\" -- message file is read only\n",
364 if (contxt && com->c_argtype & R) {
365 printf("Cannot recursively invoke \"%s\"\n", com->c_name);
368 switch (com->c_argtype & ~(F|P|I|M|T|W|R)) {
371 * A message list defaulting to nearest forward
375 printf("Illegal use of \"message list\"\n");
378 if ((c = getmsglist(cp, msgvec, com->c_msgflag)) < 0)
381 *msgvec = first(com->c_msgflag, com->c_msgmask);
385 printf("No applicable messages\n");
388 e = (*com->c_func)(msgvec);
393 * A message list with no defaults, but no error
397 printf("Illegal use of \"message list\"\n");
400 if (getmsglist(cp, msgvec, com->c_msgflag) < 0)
402 e = (*com->c_func)(msgvec);
407 * Just the straight string, with
408 * leading blanks removed.
410 while (isspace((unsigned char)*cp))
412 e = (*com->c_func)(cp);
417 * A vector of strings, in shell style.
419 if ((c = getrawlist(cp, arglist,
420 sizeof(arglist) / sizeof(*arglist))) < 0)
422 if (c < com->c_minargs) {
423 printf("%s requires at least %d arg(s)\n",
424 com->c_name, com->c_minargs);
427 if (c > com->c_maxargs) {
428 printf("%s takes no more than %d arg(s)\n",
429 com->c_name, com->c_maxargs);
432 e = (*com->c_func)(arglist);
437 * Just the constant zero, for exiting,
440 e = (*com->c_func)(0);
444 errx(1, "Unknown argtype");
449 * Exit the current source file on
463 if (value("autoprint") != NULL && com->c_argtype & P)
464 if ((dot->m_flag & MDELETED) == 0) {
465 muvec[0] = dot - &message[0] + 1;
469 if (!sourcing && (com->c_argtype & T) == 0)
475 * Set the size of the message vector used to construct argument
476 * lists to message list functions.
485 msgvec = calloc((unsigned)(sz + 1), sizeof(*msgvec));
489 * Find the correct command in the command table corresponding
490 * to the passed command "word"
497 const struct cmd *cp;
500 * ignore trailing chars after `#'
502 * lines with beginning `#' are comments
503 * spaces before `#' are ignored in execute()
510 for (cp = &cmdtab[0]; cp->c_name != NULL; cp++)
511 if (isprefix(word, cp->c_name))
517 * Determine if as1 is a valid prefix of as2.
518 * Return true if yep.
522 const char *as1, *as2;
531 return (*--s1 == '\0');
535 * The following gets called on receipt of an interrupt. This is
536 * to abort printout of a command, mainly.
537 * Dispatching here when command() is inactive crashes rcv.
538 * Close all open files except 0, 1, 2, and the temporary.
539 * Also, unstack all source files.
542 static int inithdr; /* am printing startup headers */
563 fprintf(stderr, "Interrupt\n");
568 * When we wake up after ^Z, reprint the prompt.
574 sig_t old_action = signal(s, SIG_DFL);
577 (void)sigemptyset(&nset);
578 (void)sigaddset(&nset, s);
579 (void)sigprocmask(SIG_UNBLOCK, &nset, NULL);
581 (void)sigprocmask(SIG_BLOCK, &nset, NULL);
582 (void)signal(s, old_action);
590 * Branch here on hangup signal and simulate "exit".
603 * Announce the presence of the current Mail version,
604 * give the message count, and print a header listing.
611 mdot = newfileinfo(0);
614 dot = &message[mdot - 1];
615 if (msgCount > 0 && value("noheader") == NULL) {
623 * Announce information about the file we are editing.
624 * Return a likely place to set dot.
627 newfileinfo(omsgCount)
631 int u, n, mdot, d, s;
632 char fname[PATHSIZE+1], zname[PATHSIZE+1], *ename;
634 for (mp = &message[omsgCount]; mp < &message[msgCount]; mp++)
635 if (mp->m_flag & MNEW)
637 if (mp >= &message[msgCount])
638 for (mp = &message[omsgCount]; mp < &message[msgCount]; mp++)
639 if ((mp->m_flag & MREAD) == 0)
641 if (mp < &message[msgCount])
642 mdot = mp - &message[0] + 1;
644 mdot = omsgCount + 1;
646 for (mp = &message[0], n = 0, u = 0; mp < &message[msgCount]; mp++) {
647 if (mp->m_flag & MNEW)
649 if ((mp->m_flag & MREAD) == 0)
651 if (mp->m_flag & MDELETED)
653 if (mp->m_flag & MSAVED)
657 if (getfold(fname, sizeof(fname) - 1) >= 0) {
659 if (strncmp(fname, mailname, strlen(fname)) == 0) {
660 (void)snprintf(zname, sizeof(zname), "+%s",
661 mailname + strlen(fname));
665 printf("\"%s\": ", ename);
669 printf("%d messages", msgCount);
671 printf(" %d new", n);
673 printf(" %d unread", u);
675 printf(" %d deleted", d);
677 printf(" %d saved", s);
679 printf(" [Read only]");
685 * Print the current version number.
694 printf("Version %s\n", version);
699 * Load a file of user definitions.
707 if ((in = Fopen(name, "r")) == NULL)