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 * 4. Neither the name of the University nor the names of its contributors
14 * may be used to endorse or promote products derived from this software
15 * without specific prior written permission.
17 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 static char sccsid[] = "@(#)lex.c 8.2 (Berkeley) 4/20/95";
35 #include <sys/cdefs.h>
36 __FBSDID("$FreeBSD$");
44 * Mail -- a mail program
46 * Lexical processing of commands.
49 static const char *prompt = "& ";
51 extern const struct cmd cmdtab[];
52 extern const char *version;
55 * Set up editing on the given file name.
56 * If the first character of name is %, we are considered to be
57 * editing the file, otherwise we are reading our mail which has
58 * signficance for mbox and so forth.
60 * If the -e option is being passed to mail, this function has a
61 * tri-state return code: -1 on error, 0 on no mail, 1 if there is
70 char isedit = *name != '%' || getuserid(myname) != getuid();
71 char *who = name[1] ? name + 1 : myname;
72 char tempname[PATHSIZE];
75 checkmode = value("checkmode") != NULL;
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);
151 if ((checkmode || !edit) && msgCount == 0) {
154 fprintf(stderr, "No mail for %s\n", who);
159 return (checkmode ? 1 : 0);
163 * Incorporate any new mail that has arrived since we first
164 * started reading mail.
170 int omsgCount = msgCount;
173 ibuf = Fopen(mailname, "r");
177 newsize = fsize(ibuf);
179 return (-1); /* mail box is now empty??? */
180 if (newsize < mailsize)
181 return (-1); /* mail box has shrunk??? */
182 if (newsize == mailsize)
183 return (0); /* no new mail */
184 setptr(ibuf, mailsize);
186 mailsize = ftello(ibuf);
189 return (msgCount - omsgCount);
193 static int reset_on_stop; /* do a reset() if stopped */
196 * Interpret user commands one by one. If standard input is not a tty,
203 char linebuf[LINESIZE];
206 if (signal(SIGINT, SIG_IGN) != SIG_IGN)
207 (void)signal(SIGINT, intr);
208 if (signal(SIGHUP, SIG_IGN) != SIG_IGN)
209 (void)signal(SIGHUP, hangup);
210 (void)signal(SIGTSTP, stop);
211 (void)signal(SIGTTOU, stop);
212 (void)signal(SIGTTIN, stop);
217 * Print the prompt, if needed. Clear out
218 * string space, and flush the output.
220 if (!sourcing && value("interactive") != NULL) {
221 if ((value("autoinc") != NULL) && (incfile() > 0))
222 printf("New mail has arrived.\n");
224 printf("%s", prompt);
226 (void)fflush(stdout);
229 * Read a line of commands from the current input
230 * and handle end of file specially.
234 if (readline(input, &linebuf[n], LINESIZE - n) < 0) {
239 if ((n = strlen(linebuf)) == 0)
242 if (linebuf[n] != '\\')
255 if (value("interactive") != NULL &&
256 value("ignoreeof") != NULL &&
258 printf("Use \"quit\" to quit.\n");
264 if (execute(linebuf, 0))
270 * Execute a single command.
271 * Command functions return 0 for success, 1 for error, and -1
272 * for abort. A 1 or -1 aborts a load or source. A -1 aborts
273 * the interactive command loop.
274 * Contxt is non-zero if called while composing mail.
277 execute(char linebuf[], int contxt)
280 char *arglist[MAXARGC];
281 const struct cmd *com;
287 * Strip the white space away from the beginning
288 * of the command, then scan out a word, which
289 * consists of anything except digits and white space.
291 * Handle ! escapes differently to get the correct
292 * lexical conventions.
295 for (cp = linebuf; isspace((unsigned char)*cp); cp++)
299 printf("Can't \"!\" while sourcing\n");
306 while (*cp != '\0' && strchr(" \t0123456789$^.:/-+*'\"", *cp) == NULL)
311 * Look up the command; if not found, bitch.
312 * Normally, a blank command would map to the
313 * first command in the table; while sourcing,
314 * however, we ignore blank lines to eliminate
318 if (sourcing && *word == '\0')
322 printf("Unknown command: \"%s\"\n", word);
327 * See if we should execute the command -- if a conditional
328 * we always execute it, otherwise, check the state of cond.
331 if ((com->c_argtype & F) == 0)
332 if ((cond == CRCV && !rcvmode) || (cond == CSEND && rcvmode))
336 * Process the arguments to the command, depending
337 * on the type he expects. Default to an error.
338 * If we are sourcing an interactive command, it's
342 if (!rcvmode && (com->c_argtype & M) == 0) {
343 printf("May not execute \"%s\" while sending\n",
347 if (sourcing && com->c_argtype & I) {
348 printf("May not execute \"%s\" while sourcing\n",
352 if (readonly && com->c_argtype & W) {
353 printf("May not execute \"%s\" -- message file is read only\n",
357 if (contxt && com->c_argtype & R) {
358 printf("Cannot recursively invoke \"%s\"\n", com->c_name);
361 switch (com->c_argtype & ~(F|P|I|M|T|W|R)) {
364 * A message list defaulting to nearest forward
368 printf("Illegal use of \"message list\"\n");
371 if ((c = getmsglist(cp, msgvec, com->c_msgflag)) < 0)
374 *msgvec = first(com->c_msgflag, com->c_msgmask);
378 printf("No applicable messages\n");
381 e = (*com->c_func)(msgvec);
386 * A message list with no defaults, but no error
390 printf("Illegal use of \"message list\"\n");
393 if (getmsglist(cp, msgvec, com->c_msgflag) < 0)
395 e = (*com->c_func)(msgvec);
400 * Just the straight string, with
401 * leading blanks removed.
403 while (isspace((unsigned char)*cp))
405 e = (*com->c_func)(cp);
410 * A vector of strings, in shell style.
412 if ((c = getrawlist(cp, arglist,
413 sizeof(arglist) / sizeof(*arglist))) < 0)
415 if (c < com->c_minargs) {
416 printf("%s requires at least %d arg(s)\n",
417 com->c_name, com->c_minargs);
420 if (c > com->c_maxargs) {
421 printf("%s takes no more than %d arg(s)\n",
422 com->c_name, com->c_maxargs);
425 e = (*com->c_func)(arglist);
430 * Just the constant zero, for exiting,
433 e = (*com->c_func)(0);
437 errx(1, "Unknown argtype");
442 * Exit the current source file on
456 if (value("autoprint") != NULL && com->c_argtype & P)
457 if ((dot->m_flag & MDELETED) == 0) {
458 muvec[0] = dot - &message[0] + 1;
462 if (!sourcing && (com->c_argtype & T) == 0)
468 * Set the size of the message vector used to construct argument
469 * lists to message list functions.
477 msgvec = calloc((unsigned)(sz + 1), sizeof(*msgvec));
481 * Find the correct command in the command table corresponding
482 * 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.
512 isprefix(const char *as1, const char *as2)
521 return (*--s1 == '\0');
525 * The following gets called on receipt of an interrupt. This is
526 * to abort printout of a command, mainly.
527 * Dispatching here when command() is inactive crashes rcv.
528 * Close all open files except 0, 1, 2, and the temporary.
529 * Also, unstack all source files.
532 static int inithdr; /* am printing startup headers */
551 fprintf(stderr, "Interrupt\n");
556 * When we wake up after ^Z, reprint the prompt.
561 sig_t old_action = signal(s, SIG_DFL);
564 (void)sigemptyset(&nset);
565 (void)sigaddset(&nset, s);
566 (void)sigprocmask(SIG_UNBLOCK, &nset, NULL);
568 (void)sigprocmask(SIG_BLOCK, &nset, NULL);
569 (void)signal(s, old_action);
577 * Branch here on hangup signal and simulate "exit".
580 hangup(int s __unused)
588 * Announce the presence of the current Mail version,
589 * give the message count, and print a header listing.
596 mdot = newfileinfo(0);
599 dot = &message[mdot - 1];
600 if (msgCount > 0 && value("noheader") == NULL) {
608 * Announce information about the file we are editing.
609 * Return a likely place to set dot.
612 newfileinfo(int omsgCount)
615 int u, n, mdot, d, s;
616 char fname[PATHSIZE+1], zname[PATHSIZE+1], *ename;
618 for (mp = &message[omsgCount]; mp < &message[msgCount]; mp++)
619 if (mp->m_flag & MNEW)
621 if (mp >= &message[msgCount])
622 for (mp = &message[omsgCount]; mp < &message[msgCount]; mp++)
623 if ((mp->m_flag & MREAD) == 0)
625 if (mp < &message[msgCount])
626 mdot = mp - &message[0] + 1;
628 mdot = omsgCount + 1;
630 for (mp = &message[0], n = 0, u = 0; mp < &message[msgCount]; mp++) {
631 if (mp->m_flag & MNEW)
633 if ((mp->m_flag & MREAD) == 0)
635 if (mp->m_flag & MDELETED)
637 if (mp->m_flag & MSAVED)
641 if (getfold(fname, sizeof(fname) - 1) >= 0) {
643 if (strncmp(fname, mailname, strlen(fname)) == 0) {
644 (void)snprintf(zname, sizeof(zname), "+%s",
645 mailname + strlen(fname));
649 printf("\"%s\": ", ename);
653 printf("%d messages", msgCount);
655 printf(" %d new", n);
657 printf(" %d unread", u);
659 printf(" %d deleted", d);
661 printf(" %d saved", s);
663 printf(" [Read only]");
669 * Print the current version number.
673 pversion(int e __unused)
676 printf("Version %s\n", version);
681 * Load a file of user definitions.
688 if ((in = Fopen(name, "r")) == NULL)