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
33 * @(#)lex.c 8.2 (Berkeley) 4/20/95
34 * $FreeBSD: src/usr.bin/mail/lex.c,v 1.5.6.5 2003/01/06 05:46:03 mikeh Exp $
43 * Mail -- a mail program
45 * Lexical processing of commands.
48 const char *prompt = "& ";
50 extern const struct cmd cmdtab[];
51 extern const char *version;
54 * Set up editing on the given file name.
55 * If the first character of name is %, we are considered to be
56 * editing the file, otherwise we are reading our mail which has
57 * signficance for mbox and so forth.
59 * If the -e option is passed to mail, this function has a
60 * tri-state return code: -1 on error, 0 on no mail, 1 if there is
69 char isedit = *name != '%' || getuserid(myname) != getuid();
70 char *who = name[1] ? name + 1 : myname;
71 char tempname[PATHSIZE];
74 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 snprintf(tempname, sizeof(tempname), "%s/mail.RxXXXXXXXXXX", tmpdir);
131 if ((fd = mkstemp(tempname)) == -1 || (otf = fdopen(fd, "w")) == NULL)
132 err(1, "%s", tempname);
133 fcntl(fileno(otf), F_SETFD, 1);
134 if ((itf = fopen(tempname, "r")) == NULL)
135 err(1, "%s", tempname);
136 fcntl(fileno(itf), F_SETFD, 1);
141 * New mail may have arrived while we were reading
142 * the mail file, so reset mailsize to be where
143 * we really are in the file...
145 mailsize = ftello(ibuf);
149 if ((checkmode || !edit) && msgCount == 0) {
152 fprintf(stderr, "No mail for %s\n", who);
158 return (checkmode ? 1 : 0);
162 * Incorporate any new mail that has arrived since we first
163 * started reading mail.
169 int omsgCount = msgCount;
172 ibuf = Fopen(mailname, "r");
176 newsize = fsize(ibuf);
178 return (-1); /* mail box is now empty??? */
179 if (newsize < mailsize)
180 return (-1); /* mail box has shrunk??? */
181 if (newsize == mailsize)
182 return (0); /* no new mail */
183 setptr(ibuf, mailsize);
185 mailsize = ftello(ibuf);
188 return (msgCount - omsgCount);
192 int reset_on_stop; /* do a reset() if stopped */
195 * Interpret user commands one by one. If standard input is not a tty,
202 char linebuf[LINESIZE];
205 if (signal(SIGINT, SIG_IGN) != SIG_IGN)
206 signal(SIGINT, intr);
207 if (signal(SIGHUP, SIG_IGN) != SIG_IGN)
208 signal(SIGHUP, hangup);
209 signal(SIGTSTP, stop);
210 signal(SIGTTOU, stop);
211 signal(SIGTTIN, stop);
216 * Print the prompt, if needed. Clear out
217 * string space, and flush the output.
219 if (!sourcing && value("interactive") != NULL) {
220 if ((value("autoinc") != NULL) && (incfile() > 0))
221 printf("New mail has arrived.\n");
223 printf("%s", prompt);
228 * Read a line of commands from the current input
229 * and handle end of file specially.
233 if (readline(input, &linebuf[n], LINESIZE - n) < 0) {
238 if ((n = strlen(linebuf)) == 0)
241 if (linebuf[n] != '\\')
254 if (value("interactive") != NULL &&
255 value("ignoreeof") != NULL &&
257 printf("Use \"quit\" to quit.\n");
263 if (execute(linebuf, 0))
269 * Execute a single command.
270 * Command functions return 0 for success, 1 for error, and -1
271 * for abort. A 1 or -1 aborts a load or source. A -1 aborts
272 * the interactive command loop.
273 * Contxt is non-zero if called while composing mail.
276 execute(char *linebuf, int contxt)
279 char *arglist[MAXARGC];
280 const struct cmd *com;
286 * Strip the white space away from the beginning
287 * of the command, then scan out a word, which
288 * consists of anything except digits and white space.
290 * Handle ! escapes differently to get the correct
291 * lexical conventions.
294 for (cp = linebuf; isspace((unsigned char)*cp); cp++)
298 printf("Can't \"!\" while sourcing\n");
305 while (*cp != '\0' && strchr(" \t0123456789$^.:/-+*'\"", *cp) == NULL)
310 * Look up the command; if not found, bitch.
311 * Normally, a blank command would map to the
312 * first command in the table; while sourcing,
313 * however, we ignore blank lines to eliminate
317 if (sourcing && *word == '\0')
321 printf("Unknown command: \"%s\"\n", word);
326 * See if we should execute the command -- if a conditional
327 * we always execute it, otherwise, check the state of cond.
330 if ((com->c_argtype & F) == 0)
331 if ((cond == CRCV && !rcvmode) || (cond == CSEND && rcvmode))
335 * Process the arguments to the command, depending
336 * on the type he expects. Default to an error.
337 * If we are sourcing an interactive command, it's
341 if (!rcvmode && (com->c_argtype & M) == 0) {
342 printf("May not execute \"%s\" while sending\n",
346 if (sourcing && com->c_argtype & I) {
347 printf("May not execute \"%s\" while sourcing\n",
351 if (readonly && com->c_argtype & W) {
352 printf("May not execute \"%s\" -- message file is read only\n",
356 if (contxt && com->c_argtype & R) {
357 printf("Cannot recursively invoke \"%s\"\n", com->c_name);
360 switch (com->c_argtype & ~(F|P|I|M|T|W|R)) {
363 * A message list defaulting to nearest forward
366 if (msgvec == NULL) {
367 printf("Illegal use of \"message list\"\n");
370 if ((c = getmsglist(cp, msgvec, com->c_msgflag)) < 0)
373 *msgvec = first(com->c_msgflag, com->c_msgmask);
377 printf("No applicable messages\n");
380 e = (*com->c_func)(msgvec);
385 * A message list with no defaults, but no error
388 if (msgvec == NULL) {
389 printf("Illegal use of \"message list\"\n");
392 if (getmsglist(cp, msgvec, com->c_msgflag) < 0)
394 e = (*com->c_func)(msgvec);
399 * Just the straight string, with
400 * leading blanks removed.
402 while (isspace((unsigned char)*cp))
404 e = (*com->c_func)(cp);
409 * A vector of strings, in shell style.
411 if ((c = getrawlist(cp, arglist,
412 sizeof(arglist) / sizeof(*arglist))) < 0)
414 if (c < com->c_minargs) {
415 printf("%s requires at least %d arg(s)\n",
416 com->c_name, com->c_minargs);
419 if (c > com->c_maxargs) {
420 printf("%s takes no more than %d arg(s)\n",
421 com->c_name, com->c_maxargs);
424 e = (*com->c_func)(arglist);
429 * Just the constant zero, for exiting,
432 e = (*com->c_func)(0);
436 errx(1, "Unknown argtype");
441 * Exit the current source file on
455 if (value("autoprint") != NULL && com->c_argtype & P)
456 if ((dot->m_flag & MDELETED) == 0) {
457 muvec[0] = dot - &message[0] + 1;
461 if (!sourcing && (com->c_argtype & T) == 0)
467 * Set the size of the message vector used to construct argument
468 * lists to message list functions.
475 msgvec = calloc((unsigned)(sz + 1), sizeof(*msgvec));
479 * Find the correct command in the command table corresponding
480 * to the passed command "word"
486 const struct cmd *cp;
489 * ignore trailing chars after `#'
491 * lines with beginning `#' are comments
492 * spaces before `#' are ignored in execute()
499 for (cp = &cmdtab[0]; cp->c_name != NULL; cp++)
500 if (isprefix(word, cp->c_name))
506 * Determine if as1 is a valid prefix of as2.
507 * Return true if yep.
510 isprefix(const char *as1, const char *as2)
519 return (*--s1 == '\0');
523 * The following gets called on receipt of an interrupt. This is
524 * to abort printout of a command, mainly.
525 * Dispatching here when command() is inactive crashes rcv.
526 * Close all open files except 0, 1, 2, and the temporary.
527 * Also, unstack all source files.
530 int inithdr; /* am printing startup headers */
549 fprintf(stderr, "Interrupt\n");
554 * When we wake up after ^Z, reprint the prompt.
559 sig_t old_action = signal(s, SIG_DFL);
564 sigprocmask(SIG_UNBLOCK, &nset, NULL);
566 sigprocmask(SIG_BLOCK, &nset, NULL);
567 signal(s, old_action);
575 * Branch here on hangup signal and simulate "exit".
586 * Announce the presence of the current Mail version,
587 * give the message count, and print a header listing.
594 mdot = newfileinfo(0);
597 dot = &message[mdot - 1];
598 if (msgCount > 0 && value("noheader") == NULL) {
606 * Announce information about the file we are editing.
607 * Return a likely place to set dot.
610 newfileinfo(int omsgCount)
613 int u, n, mdot, d, s;
614 char fname[PATHSIZE+1], zname[PATHSIZE+1], *ename;
616 for (mp = &message[omsgCount]; mp < &message[msgCount]; mp++)
617 if (mp->m_flag & MNEW)
619 if (mp >= &message[msgCount])
620 for (mp = &message[omsgCount]; mp < &message[msgCount]; mp++)
621 if ((mp->m_flag & MREAD) == 0)
623 if (mp < &message[msgCount])
624 mdot = mp - &message[0] + 1;
626 mdot = omsgCount + 1;
628 for (mp = &message[0], n = 0, u = 0; mp < &message[msgCount]; mp++) {
629 if (mp->m_flag & MNEW)
631 if ((mp->m_flag & MREAD) == 0)
633 if (mp->m_flag & MDELETED)
635 if (mp->m_flag & MSAVED)
639 if (getfold(fname, sizeof(fname) - 1) >= 0) {
641 if (strncmp(fname, mailname, strlen(fname)) == 0) {
642 snprintf(zname, sizeof(zname), "+%s",
643 mailname + strlen(fname));
647 printf("\"%s\": ", ename);
651 printf("%d messages", msgCount);
653 printf(" %d new", n);
655 printf(" %d unread", u);
657 printf(" %d deleted", d);
659 printf(" %d saved", s);
661 printf(" [Read only]");
667 * Print the current version number.
674 printf("Version %s\n", version);
679 * Load a file of user definitions.
686 if ((in = Fopen(name, "r")) == NULL)