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.2 2003/06/17 04:29:28 dillon 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.
67 char isedit = *name != '%' || getuserid(myname) != getuid();
68 char *who = name[1] ? name + 1 : myname;
69 char tempname[PATHSIZE];
72 if ((name = expand(name)) == NULL)
75 if ((ibuf = Fopen(name, "r")) == NULL) {
76 if (!isedit && errno == ENOENT)
82 if (fstat(fileno(ibuf), &stb) < 0) {
88 if (S_ISDIR(stb.st_mode) || !S_ISREG(stb.st_mode)) {
90 errno = S_ISDIR(stb.st_mode) ? EISDIR : EINVAL;
96 * Looks like all will be well. We must now relinquish our
97 * hold on the current set of stuff. Must hold signals
98 * while we are reading the new file, else we will ruin
99 * the message[] data structure.
107 * Copy the messages into /tmp
112 if ((i = open(name, 1)) < 0)
122 strlcpy(prevfile, mailname, sizeof(prevfile));
123 if (name != mailname)
124 strlcpy(mailname, name, sizeof(mailname));
125 mailsize = fsize(ibuf);
126 (void)snprintf(tempname, sizeof(tempname),
127 "%s/mail.RxXXXXXXXXXX", tmpdir);
128 if ((fd = mkstemp(tempname)) == -1 || (otf = fdopen(fd, "w")) == NULL)
129 err(1, "%s", tempname);
130 (void)fcntl(fileno(otf), F_SETFD, 1);
131 if ((itf = fopen(tempname, "r")) == NULL)
132 err(1, "%s", tempname);
133 (void)fcntl(fileno(itf), F_SETFD, 1);
138 * New mail may have arrived while we were reading
139 * the mail file, so reset mailsize to be where
140 * we really are in the file...
142 mailsize = ftello(ibuf);
146 if (!edit && msgCount == 0) {
148 fprintf(stderr, "No mail for %s\n", who);
155 * Incorporate any new mail that has arrived since we first
156 * started reading mail.
162 int omsgCount = msgCount;
165 ibuf = Fopen(mailname, "r");
169 newsize = fsize(ibuf);
171 return (-1); /* mail box is now empty??? */
172 if (newsize < mailsize)
173 return (-1); /* mail box has shrunk??? */
174 if (newsize == mailsize)
175 return (0); /* no new mail */
176 setptr(ibuf, mailsize);
178 mailsize = ftello(ibuf);
181 return (msgCount - omsgCount);
185 int reset_on_stop; /* do a reset() if stopped */
188 * Interpret user commands one by one. If standard input is not a tty,
195 char linebuf[LINESIZE];
198 if (signal(SIGINT, SIG_IGN) != SIG_IGN)
199 (void)signal(SIGINT, intr);
200 if (signal(SIGHUP, SIG_IGN) != SIG_IGN)
201 (void)signal(SIGHUP, hangup);
202 (void)signal(SIGTSTP, stop);
203 (void)signal(SIGTTOU, stop);
204 (void)signal(SIGTTIN, stop);
209 * Print the prompt, if needed. Clear out
210 * string space, and flush the output.
212 if (!sourcing && value("interactive") != NULL) {
213 if ((value("autoinc") != NULL) && (incfile() > 0))
214 printf("New mail has arrived.\n");
216 printf("%s", prompt);
218 (void)fflush(stdout);
221 * Read a line of commands from the current input
222 * and handle end of file specially.
226 if (readline(input, &linebuf[n], LINESIZE - n) < 0) {
231 if ((n = strlen(linebuf)) == 0)
234 if (linebuf[n] != '\\')
247 if (value("interactive") != NULL &&
248 value("ignoreeof") != NULL &&
250 printf("Use \"quit\" to quit.\n");
256 if (execute(linebuf, 0))
262 * Execute a single command.
263 * Command functions return 0 for success, 1 for error, and -1
264 * for abort. A 1 or -1 aborts a load or source. A -1 aborts
265 * the interactive command loop.
266 * Contxt is non-zero if called while composing mail.
269 execute(linebuf, contxt)
274 char *arglist[MAXARGC];
275 const struct cmd *com;
281 * Strip the white space away from the beginning
282 * of the command, then scan out a word, which
283 * consists of anything except digits and white space.
285 * Handle ! escapes differently to get the correct
286 * lexical conventions.
289 for (cp = linebuf; isspace((unsigned char)*cp); cp++)
293 printf("Can't \"!\" while sourcing\n");
300 while (*cp != '\0' && strchr(" \t0123456789$^.:/-+*'\"", *cp) == NULL)
305 * Look up the command; if not found, bitch.
306 * Normally, a blank command would map to the
307 * first command in the table; while sourcing,
308 * however, we ignore blank lines to eliminate
312 if (sourcing && *word == '\0')
316 printf("Unknown command: \"%s\"\n", word);
321 * See if we should execute the command -- if a conditional
322 * we always execute it, otherwise, check the state of cond.
325 if ((com->c_argtype & F) == 0)
326 if ((cond == CRCV && !rcvmode) || (cond == CSEND && rcvmode))
330 * Process the arguments to the command, depending
331 * on the type he expects. Default to an error.
332 * If we are sourcing an interactive command, it's
336 if (!rcvmode && (com->c_argtype & M) == 0) {
337 printf("May not execute \"%s\" while sending\n",
341 if (sourcing && com->c_argtype & I) {
342 printf("May not execute \"%s\" while sourcing\n",
346 if (readonly && com->c_argtype & W) {
347 printf("May not execute \"%s\" -- message file is read only\n",
351 if (contxt && com->c_argtype & R) {
352 printf("Cannot recursively invoke \"%s\"\n", com->c_name);
355 switch (com->c_argtype & ~(F|P|I|M|T|W|R)) {
358 * A message list defaulting to nearest forward
362 printf("Illegal use of \"message list\"\n");
365 if ((c = getmsglist(cp, msgvec, com->c_msgflag)) < 0)
368 *msgvec = first(com->c_msgflag, com->c_msgmask);
372 printf("No applicable messages\n");
375 e = (*com->c_func)(msgvec);
380 * A message list with no defaults, but no error
384 printf("Illegal use of \"message list\"\n");
387 if (getmsglist(cp, msgvec, com->c_msgflag) < 0)
389 e = (*com->c_func)(msgvec);
394 * Just the straight string, with
395 * leading blanks removed.
397 while (isspace((unsigned char)*cp))
399 e = (*com->c_func)(cp);
404 * A vector of strings, in shell style.
406 if ((c = getrawlist(cp, arglist,
407 sizeof(arglist) / sizeof(*arglist))) < 0)
409 if (c < com->c_minargs) {
410 printf("%s requires at least %d arg(s)\n",
411 com->c_name, com->c_minargs);
414 if (c > com->c_maxargs) {
415 printf("%s takes no more than %d arg(s)\n",
416 com->c_name, com->c_maxargs);
419 e = (*com->c_func)(arglist);
424 * Just the constant zero, for exiting,
427 e = (*com->c_func)(0);
431 errx(1, "Unknown argtype");
436 * Exit the current source file on
450 if (value("autoprint") != NULL && com->c_argtype & P)
451 if ((dot->m_flag & MDELETED) == 0) {
452 muvec[0] = dot - &message[0] + 1;
456 if (!sourcing && (com->c_argtype & T) == 0)
462 * Set the size of the message vector used to construct argument
463 * lists to message list functions.
472 msgvec = calloc((unsigned)(sz + 1), sizeof(*msgvec));
476 * Find the correct command in the command table corresponding
477 * to the passed command "word"
484 const struct cmd *cp;
487 * ignore trailing chars after `#'
489 * lines with beginning `#' are comments
490 * spaces before `#' are ignored in execute()
497 for (cp = &cmdtab[0]; cp->c_name != NULL; cp++)
498 if (isprefix(word, cp->c_name))
504 * Determine if as1 is a valid prefix of as2.
505 * Return true if yep.
509 const char *as1, *as2;
518 return (*--s1 == '\0');
522 * The following gets called on receipt of an interrupt. This is
523 * to abort printout of a command, mainly.
524 * Dispatching here when command() is inactive crashes rcv.
525 * Close all open files except 0, 1, 2, and the temporary.
526 * Also, unstack all source files.
529 int inithdr; /* am printing startup headers */
550 fprintf(stderr, "Interrupt\n");
555 * 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".
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(omsgCount)
618 int u, n, mdot, d, s;
619 char fname[PATHSIZE+1], zname[PATHSIZE+1], *ename;
621 for (mp = &message[omsgCount]; mp < &message[msgCount]; mp++)
622 if (mp->m_flag & MNEW)
624 if (mp >= &message[msgCount])
625 for (mp = &message[omsgCount]; mp < &message[msgCount]; mp++)
626 if ((mp->m_flag & MREAD) == 0)
628 if (mp < &message[msgCount])
629 mdot = mp - &message[0] + 1;
631 mdot = omsgCount + 1;
633 for (mp = &message[0], n = 0, u = 0; mp < &message[msgCount]; mp++) {
634 if (mp->m_flag & MNEW)
636 if ((mp->m_flag & MREAD) == 0)
638 if (mp->m_flag & MDELETED)
640 if (mp->m_flag & MSAVED)
644 if (getfold(fname, sizeof(fname) - 1) >= 0) {
646 if (strncmp(fname, mailname, strlen(fname)) == 0) {
647 (void)snprintf(zname, sizeof(zname), "+%s",
648 mailname + strlen(fname));
652 printf("\"%s\": ", ename);
656 printf("%d messages", msgCount);
658 printf(" %d new", n);
660 printf(" %d unread", u);
662 printf(" %d deleted", d);
664 printf(" %d saved", s);
666 printf(" [Read only]");
672 * Print the current version number.
681 printf("Version %s\n", version);
686 * Load a file of user definitions.
694 if ((in = Fopen(name, "r")) == NULL)