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.5 2004/09/07 21:31:45 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 (void)snprintf(tempname, sizeof(tempname),
132 "%s/mail.RxXXXXXXXXXX", tmpdir);
133 if ((fd = mkstemp(tempname)) == -1 || (otf = fdopen(fd, "w")) == NULL)
134 err(1, "%s", tempname);
135 (void)fcntl(fileno(otf), F_SETFD, 1);
136 if ((itf = fopen(tempname, "r")) == NULL)
137 err(1, "%s", tempname);
138 (void)fcntl(fileno(itf), F_SETFD, 1);
143 * New mail may have arrived while we were reading
144 * the mail file, so reset mailsize to be where
145 * we really are in the file...
147 mailsize = ftello(ibuf);
151 if ((checkmode || !edit) && msgCount == 0) {
154 fprintf(stderr, "No mail for %s\n", who);
160 return (checkmode ? 1 : 0);
164 * Incorporate any new mail that has arrived since we first
165 * started reading mail.
171 int omsgCount = msgCount;
174 ibuf = Fopen(mailname, "r");
178 newsize = fsize(ibuf);
180 return (-1); /* mail box is now empty??? */
181 if (newsize < mailsize)
182 return (-1); /* mail box has shrunk??? */
183 if (newsize == mailsize)
184 return (0); /* no new mail */
185 setptr(ibuf, mailsize);
187 mailsize = ftello(ibuf);
190 return (msgCount - omsgCount);
194 int reset_on_stop; /* do a reset() if stopped */
197 * Interpret user commands one by one. If standard input is not a tty,
204 char linebuf[LINESIZE];
207 if (signal(SIGINT, SIG_IGN) != SIG_IGN)
208 (void)signal(SIGINT, intr);
209 if (signal(SIGHUP, SIG_IGN) != SIG_IGN)
210 (void)signal(SIGHUP, hangup);
211 (void)signal(SIGTSTP, stop);
212 (void)signal(SIGTTOU, stop);
213 (void)signal(SIGTTIN, stop);
218 * Print the prompt, if needed. Clear out
219 * string space, and flush the output.
221 if (!sourcing && value("interactive") != NULL) {
222 if ((value("autoinc") != NULL) && (incfile() > 0))
223 printf("New mail has arrived.\n");
225 printf("%s", prompt);
227 (void)fflush(stdout);
230 * Read a line of commands from the current input
231 * and handle end of file specially.
235 if (readline(input, &linebuf[n], LINESIZE - n) < 0) {
240 if ((n = strlen(linebuf)) == 0)
243 if (linebuf[n] != '\\')
256 if (value("interactive") != NULL &&
257 value("ignoreeof") != NULL &&
259 printf("Use \"quit\" to quit.\n");
265 if (execute(linebuf, 0))
271 * Execute a single command.
272 * Command functions return 0 for success, 1 for error, and -1
273 * for abort. A 1 or -1 aborts a load or source. A -1 aborts
274 * the interactive command loop.
275 * Contxt is non-zero if called while composing mail.
278 execute(char *linebuf, int contxt)
281 char *arglist[MAXARGC];
282 const struct cmd *com;
288 * Strip the white space away from the beginning
289 * of the command, then scan out a word, which
290 * consists of anything except digits and white space.
292 * Handle ! escapes differently to get the correct
293 * lexical conventions.
296 for (cp = linebuf; isspace((unsigned char)*cp); cp++)
300 printf("Can't \"!\" while sourcing\n");
307 while (*cp != '\0' && strchr(" \t0123456789$^.:/-+*'\"", *cp) == NULL)
312 * Look up the command; if not found, bitch.
313 * Normally, a blank command would map to the
314 * first command in the table; while sourcing,
315 * however, we ignore blank lines to eliminate
319 if (sourcing && *word == '\0')
323 printf("Unknown command: \"%s\"\n", word);
328 * See if we should execute the command -- if a conditional
329 * we always execute it, otherwise, check the state of cond.
332 if ((com->c_argtype & F) == 0)
333 if ((cond == CRCV && !rcvmode) || (cond == CSEND && rcvmode))
337 * Process the arguments to the command, depending
338 * on the type he expects. Default to an error.
339 * If we are sourcing an interactive command, it's
343 if (!rcvmode && (com->c_argtype & M) == 0) {
344 printf("May not execute \"%s\" while sending\n",
348 if (sourcing && com->c_argtype & I) {
349 printf("May not execute \"%s\" while sourcing\n",
353 if (readonly && com->c_argtype & W) {
354 printf("May not execute \"%s\" -- message file is read only\n",
358 if (contxt && com->c_argtype & R) {
359 printf("Cannot recursively invoke \"%s\"\n", com->c_name);
362 switch (com->c_argtype & ~(F|P|I|M|T|W|R)) {
365 * A message list defaulting to nearest forward
369 printf("Illegal use of \"message list\"\n");
372 if ((c = getmsglist(cp, msgvec, com->c_msgflag)) < 0)
375 *msgvec = first(com->c_msgflag, com->c_msgmask);
379 printf("No applicable messages\n");
382 e = (*com->c_func)(msgvec);
387 * A message list with no defaults, but no error
391 printf("Illegal use of \"message list\"\n");
394 if (getmsglist(cp, msgvec, com->c_msgflag) < 0)
396 e = (*com->c_func)(msgvec);
401 * Just the straight string, with
402 * leading blanks removed.
404 while (isspace((unsigned char)*cp))
406 e = (*com->c_func)(cp);
411 * A vector of strings, in shell style.
413 if ((c = getrawlist(cp, arglist,
414 sizeof(arglist) / sizeof(*arglist))) < 0)
416 if (c < com->c_minargs) {
417 printf("%s requires at least %d arg(s)\n",
418 com->c_name, com->c_minargs);
421 if (c > com->c_maxargs) {
422 printf("%s takes no more than %d arg(s)\n",
423 com->c_name, com->c_maxargs);
426 e = (*com->c_func)(arglist);
431 * Just the constant zero, for exiting,
434 e = (*com->c_func)(0);
438 errx(1, "Unknown argtype");
443 * Exit the current source file on
457 if (value("autoprint") != NULL && com->c_argtype & P)
458 if ((dot->m_flag & MDELETED) == 0) {
459 muvec[0] = dot - &message[0] + 1;
463 if (!sourcing && (com->c_argtype & T) == 0)
469 * Set the size of the message vector used to construct argument
470 * lists to message list functions.
478 msgvec = calloc((unsigned)(sz + 1), sizeof(*msgvec));
482 * Find the correct command in the command table corresponding
483 * to the passed command "word"
489 const struct cmd *cp;
492 * ignore trailing chars after `#'
494 * lines with beginning `#' are comments
495 * spaces before `#' are ignored in execute()
502 for (cp = &cmdtab[0]; cp->c_name != NULL; cp++)
503 if (isprefix(word, cp->c_name))
509 * Determine if as1 is a valid prefix of as2.
510 * Return true if yep.
513 isprefix(const char *as1, const char *as2)
522 return (*--s1 == '\0');
526 * The following gets called on receipt of an interrupt. This is
527 * to abort printout of a command, mainly.
528 * Dispatching here when command() is inactive crashes rcv.
529 * Close all open files except 0, 1, 2, and the temporary.
530 * Also, unstack all source files.
533 int inithdr; /* am printing startup headers */
553 fprintf(stderr, "Interrupt\n");
558 * When we wake up after ^Z, reprint the prompt.
563 sig_t old_action = signal(s, SIG_DFL);
566 (void)sigemptyset(&nset);
567 (void)sigaddset(&nset, s);
568 (void)sigprocmask(SIG_UNBLOCK, &nset, NULL);
570 (void)sigprocmask(SIG_BLOCK, &nset, NULL);
571 (void)signal(s, old_action);
579 * Branch here on hangup signal and simulate "exit".
591 * Announce the presence of the current Mail version,
592 * give the message count, and print a header listing.
599 mdot = newfileinfo(0);
602 dot = &message[mdot - 1];
603 if (msgCount > 0 && value("noheader") == NULL) {
611 * Announce information about the file we are editing.
612 * Return a likely place to set dot.
615 newfileinfo(int 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.
680 printf("Version %s\n", version);
685 * Load a file of user definitions.
692 if ((in = Fopen(name, "r")) == NULL)