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 $
35 * $DragonFly: src/usr.bin/mail/lex.c,v 1.6 2004/09/08 03:01:11 joerg Exp $
44 * Mail -- a mail program
46 * Lexical processing of commands.
49 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 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;
77 if ((name = expand(name)) == NULL)
80 if ((ibuf = Fopen(name, "r")) == NULL) {
81 if (!isedit && errno == ENOENT)
87 if (fstat(fileno(ibuf), &stb) < 0) {
93 if (S_ISDIR(stb.st_mode) || !S_ISREG(stb.st_mode)) {
95 errno = S_ISDIR(stb.st_mode) ? EISDIR : EINVAL;
101 * Looks like all will be well. We must now relinquish our
102 * hold on the current set of stuff. Must hold signals
103 * while we are reading the new file, else we will ruin
104 * the message[] data structure.
112 * Copy the messages into /tmp
117 if ((i = open(name, 1)) < 0)
127 strlcpy(prevfile, mailname, sizeof(prevfile));
128 if (name != mailname)
129 strlcpy(mailname, name, sizeof(mailname));
130 mailsize = fsize(ibuf);
131 snprintf(tempname, sizeof(tempname), "%s/mail.RxXXXXXXXXXX", tmpdir);
132 if ((fd = mkstemp(tempname)) == -1 || (otf = fdopen(fd, "w")) == NULL)
133 err(1, "%s", tempname);
134 fcntl(fileno(otf), F_SETFD, 1);
135 if ((itf = fopen(tempname, "r")) == NULL)
136 err(1, "%s", tempname);
137 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 ((checkmode || !edit) && msgCount == 0) {
153 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 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 signal(SIGINT, intr);
208 if (signal(SIGHUP, SIG_IGN) != SIG_IGN)
209 signal(SIGHUP, hangup);
210 signal(SIGTSTP, stop);
211 signal(SIGTTOU, stop);
212 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);
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.
476 msgvec = calloc((unsigned)(sz + 1), sizeof(*msgvec));
480 * Find the correct command in the command table corresponding
481 * to the passed command "word"
487 const struct cmd *cp;
490 * ignore trailing chars after `#'
492 * lines with beginning `#' are comments
493 * spaces before `#' are ignored in execute()
500 for (cp = &cmdtab[0]; cp->c_name != NULL; cp++)
501 if (isprefix(word, cp->c_name))
507 * Determine if as1 is a valid prefix of as2.
508 * Return true if yep.
511 isprefix(const char *as1, const char *as2)
520 return (*--s1 == '\0');
524 * The following gets called on receipt of an interrupt. This is
525 * to abort printout of a command, mainly.
526 * Dispatching here when command() is inactive crashes rcv.
527 * Close all open files except 0, 1, 2, and the temporary.
528 * Also, unstack all source files.
531 int inithdr; /* am printing startup headers */
550 fprintf(stderr, "Interrupt\n");
555 * When we wake up after ^Z, reprint the prompt.
560 sig_t old_action = signal(s, SIG_DFL);
565 sigprocmask(SIG_UNBLOCK, &nset, NULL);
567 sigprocmask(SIG_BLOCK, &nset, NULL);
568 signal(s, old_action);
576 * Branch here on hangup signal and simulate "exit".
587 * Announce the presence of the current Mail version,
588 * give the message count, and print a header listing.
595 mdot = newfileinfo(0);
598 dot = &message[mdot - 1];
599 if (msgCount > 0 && value("noheader") == NULL) {
607 * Announce information about the file we are editing.
608 * Return a likely place to set dot.
611 newfileinfo(int omsgCount)
614 int u, n, mdot, d, s;
615 char fname[PATHSIZE+1], zname[PATHSIZE+1], *ename;
617 for (mp = &message[omsgCount]; mp < &message[msgCount]; mp++)
618 if (mp->m_flag & MNEW)
620 if (mp >= &message[msgCount])
621 for (mp = &message[omsgCount]; mp < &message[msgCount]; mp++)
622 if ((mp->m_flag & MREAD) == 0)
624 if (mp < &message[msgCount])
625 mdot = mp - &message[0] + 1;
627 mdot = omsgCount + 1;
629 for (mp = &message[0], n = 0, u = 0; mp < &message[msgCount]; mp++) {
630 if (mp->m_flag & MNEW)
632 if ((mp->m_flag & MREAD) == 0)
634 if (mp->m_flag & MDELETED)
636 if (mp->m_flag & MSAVED)
640 if (getfold(fname, sizeof(fname) - 1) >= 0) {
642 if (strncmp(fname, mailname, strlen(fname)) == 0) {
643 snprintf(zname, sizeof(zname), "+%s",
644 mailname + strlen(fname));
648 printf("\"%s\": ", ename);
652 printf("%d messages", msgCount);
654 printf(" %d new", n);
656 printf(" %d unread", u);
658 printf(" %d deleted", d);
660 printf(" %d saved", s);
662 printf(" [Read only]");
668 * Print the current version number.
675 printf("Version %s\n", version);
680 * Load a file of user definitions.
687 if ((in = Fopen(name, "r")) == NULL)