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.4 2003/10/04 20:36:48 hmp 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.
66 char isedit = *name != '%' || getuserid(myname) != getuid();
67 char *who = name[1] ? name + 1 : myname;
68 char tempname[PATHSIZE];
71 if ((name = expand(name)) == NULL)
74 if ((ibuf = Fopen(name, "r")) == NULL) {
75 if (!isedit && errno == ENOENT)
81 if (fstat(fileno(ibuf), &stb) < 0) {
87 if (S_ISDIR(stb.st_mode) || !S_ISREG(stb.st_mode)) {
89 errno = S_ISDIR(stb.st_mode) ? EISDIR : EINVAL;
95 * Looks like all will be well. We must now relinquish our
96 * hold on the current set of stuff. Must hold signals
97 * while we are reading the new file, else we will ruin
98 * the message[] data structure.
106 * Copy the messages into /tmp
111 if ((i = open(name, 1)) < 0)
121 strlcpy(prevfile, mailname, sizeof(prevfile));
122 if (name != mailname)
123 strlcpy(mailname, name, sizeof(mailname));
124 mailsize = fsize(ibuf);
125 (void)snprintf(tempname, sizeof(tempname),
126 "%s/mail.RxXXXXXXXXXX", tmpdir);
127 if ((fd = mkstemp(tempname)) == -1 || (otf = fdopen(fd, "w")) == NULL)
128 err(1, "%s", tempname);
129 (void)fcntl(fileno(otf), F_SETFD, 1);
130 if ((itf = fopen(tempname, "r")) == NULL)
131 err(1, "%s", tempname);
132 (void)fcntl(fileno(itf), F_SETFD, 1);
137 * New mail may have arrived while we were reading
138 * the mail file, so reset mailsize to be where
139 * we really are in the file...
141 mailsize = ftello(ibuf);
145 if (!edit && msgCount == 0) {
147 fprintf(stderr, "No mail for %s\n", who);
154 * Incorporate any new mail that has arrived since we first
155 * started reading mail.
161 int omsgCount = msgCount;
164 ibuf = Fopen(mailname, "r");
168 newsize = fsize(ibuf);
170 return (-1); /* mail box is now empty??? */
171 if (newsize < mailsize)
172 return (-1); /* mail box has shrunk??? */
173 if (newsize == mailsize)
174 return (0); /* no new mail */
175 setptr(ibuf, mailsize);
177 mailsize = ftello(ibuf);
180 return (msgCount - omsgCount);
184 int reset_on_stop; /* do a reset() if stopped */
187 * Interpret user commands one by one. If standard input is not a tty,
194 char linebuf[LINESIZE];
197 if (signal(SIGINT, SIG_IGN) != SIG_IGN)
198 (void)signal(SIGINT, intr);
199 if (signal(SIGHUP, SIG_IGN) != SIG_IGN)
200 (void)signal(SIGHUP, hangup);
201 (void)signal(SIGTSTP, stop);
202 (void)signal(SIGTTOU, stop);
203 (void)signal(SIGTTIN, stop);
208 * Print the prompt, if needed. Clear out
209 * string space, and flush the output.
211 if (!sourcing && value("interactive") != NULL) {
212 if ((value("autoinc") != NULL) && (incfile() > 0))
213 printf("New mail has arrived.\n");
215 printf("%s", prompt);
217 (void)fflush(stdout);
220 * Read a line of commands from the current input
221 * and handle end of file specially.
225 if (readline(input, &linebuf[n], LINESIZE - n) < 0) {
230 if ((n = strlen(linebuf)) == 0)
233 if (linebuf[n] != '\\')
246 if (value("interactive") != NULL &&
247 value("ignoreeof") != NULL &&
249 printf("Use \"quit\" to quit.\n");
255 if (execute(linebuf, 0))
261 * Execute a single command.
262 * Command functions return 0 for success, 1 for error, and -1
263 * for abort. A 1 or -1 aborts a load or source. A -1 aborts
264 * the interactive command loop.
265 * Contxt is non-zero if called while composing mail.
268 execute(char *linebuf, int contxt)
271 char *arglist[MAXARGC];
272 const struct cmd *com;
278 * Strip the white space away from the beginning
279 * of the command, then scan out a word, which
280 * consists of anything except digits and white space.
282 * Handle ! escapes differently to get the correct
283 * lexical conventions.
286 for (cp = linebuf; isspace((unsigned char)*cp); cp++)
290 printf("Can't \"!\" while sourcing\n");
297 while (*cp != '\0' && strchr(" \t0123456789$^.:/-+*'\"", *cp) == NULL)
302 * Look up the command; if not found, bitch.
303 * Normally, a blank command would map to the
304 * first command in the table; while sourcing,
305 * however, we ignore blank lines to eliminate
309 if (sourcing && *word == '\0')
313 printf("Unknown command: \"%s\"\n", word);
318 * See if we should execute the command -- if a conditional
319 * we always execute it, otherwise, check the state of cond.
322 if ((com->c_argtype & F) == 0)
323 if ((cond == CRCV && !rcvmode) || (cond == CSEND && rcvmode))
327 * Process the arguments to the command, depending
328 * on the type he expects. Default to an error.
329 * If we are sourcing an interactive command, it's
333 if (!rcvmode && (com->c_argtype & M) == 0) {
334 printf("May not execute \"%s\" while sending\n",
338 if (sourcing && com->c_argtype & I) {
339 printf("May not execute \"%s\" while sourcing\n",
343 if (readonly && com->c_argtype & W) {
344 printf("May not execute \"%s\" -- message file is read only\n",
348 if (contxt && com->c_argtype & R) {
349 printf("Cannot recursively invoke \"%s\"\n", com->c_name);
352 switch (com->c_argtype & ~(F|P|I|M|T|W|R)) {
355 * A message list defaulting to nearest forward
359 printf("Illegal use of \"message list\"\n");
362 if ((c = getmsglist(cp, msgvec, com->c_msgflag)) < 0)
365 *msgvec = first(com->c_msgflag, com->c_msgmask);
369 printf("No applicable messages\n");
372 e = (*com->c_func)(msgvec);
377 * A message list with no defaults, but no error
381 printf("Illegal use of \"message list\"\n");
384 if (getmsglist(cp, msgvec, com->c_msgflag) < 0)
386 e = (*com->c_func)(msgvec);
391 * Just the straight string, with
392 * leading blanks removed.
394 while (isspace((unsigned char)*cp))
396 e = (*com->c_func)(cp);
401 * A vector of strings, in shell style.
403 if ((c = getrawlist(cp, arglist,
404 sizeof(arglist) / sizeof(*arglist))) < 0)
406 if (c < com->c_minargs) {
407 printf("%s requires at least %d arg(s)\n",
408 com->c_name, com->c_minargs);
411 if (c > com->c_maxargs) {
412 printf("%s takes no more than %d arg(s)\n",
413 com->c_name, com->c_maxargs);
416 e = (*com->c_func)(arglist);
421 * Just the constant zero, for exiting,
424 e = (*com->c_func)(0);
428 errx(1, "Unknown argtype");
433 * Exit the current source file on
447 if (value("autoprint") != NULL && com->c_argtype & P)
448 if ((dot->m_flag & MDELETED) == 0) {
449 muvec[0] = dot - &message[0] + 1;
453 if (!sourcing && (com->c_argtype & T) == 0)
459 * Set the size of the message vector used to construct argument
460 * lists to message list functions.
468 msgvec = calloc((unsigned)(sz + 1), sizeof(*msgvec));
472 * Find the correct command in the command table corresponding
473 * to the passed command "word"
479 const struct cmd *cp;
482 * ignore trailing chars after `#'
484 * lines with beginning `#' are comments
485 * spaces before `#' are ignored in execute()
492 for (cp = &cmdtab[0]; cp->c_name != NULL; cp++)
493 if (isprefix(word, cp->c_name))
499 * Determine if as1 is a valid prefix of as2.
500 * Return true if yep.
503 isprefix(const char *as1, const char *as2)
512 return (*--s1 == '\0');
516 * The following gets called on receipt of an interrupt. This is
517 * to abort printout of a command, mainly.
518 * Dispatching here when command() is inactive crashes rcv.
519 * Close all open files except 0, 1, 2, and the temporary.
520 * Also, unstack all source files.
523 int inithdr; /* am printing startup headers */
543 fprintf(stderr, "Interrupt\n");
548 * When we wake up after ^Z, reprint the prompt.
553 sig_t old_action = signal(s, SIG_DFL);
556 (void)sigemptyset(&nset);
557 (void)sigaddset(&nset, s);
558 (void)sigprocmask(SIG_UNBLOCK, &nset, NULL);
560 (void)sigprocmask(SIG_BLOCK, &nset, NULL);
561 (void)signal(s, old_action);
569 * Branch here on hangup signal and simulate "exit".
581 * Announce the presence of the current Mail version,
582 * give the message count, and print a header listing.
589 mdot = newfileinfo(0);
592 dot = &message[mdot - 1];
593 if (msgCount > 0 && value("noheader") == NULL) {
601 * Announce information about the file we are editing.
602 * Return a likely place to set dot.
605 newfileinfo(int omsgCount)
608 int u, n, mdot, d, s;
609 char fname[PATHSIZE+1], zname[PATHSIZE+1], *ename;
611 for (mp = &message[omsgCount]; mp < &message[msgCount]; mp++)
612 if (mp->m_flag & MNEW)
614 if (mp >= &message[msgCount])
615 for (mp = &message[omsgCount]; mp < &message[msgCount]; mp++)
616 if ((mp->m_flag & MREAD) == 0)
618 if (mp < &message[msgCount])
619 mdot = mp - &message[0] + 1;
621 mdot = omsgCount + 1;
623 for (mp = &message[0], n = 0, u = 0; mp < &message[msgCount]; mp++) {
624 if (mp->m_flag & MNEW)
626 if ((mp->m_flag & MREAD) == 0)
628 if (mp->m_flag & MDELETED)
630 if (mp->m_flag & MSAVED)
634 if (getfold(fname, sizeof(fname) - 1) >= 0) {
636 if (strncmp(fname, mailname, strlen(fname)) == 0) {
637 (void)snprintf(zname, sizeof(zname), "+%s",
638 mailname + strlen(fname));
642 printf("\"%s\": ", ename);
646 printf("%d messages", msgCount);
648 printf(" %d new", n);
650 printf(" %d unread", u);
652 printf(" %d deleted", d);
654 printf(" %d saved", s);
656 printf(" [Read only]");
662 * Print the current version number.
670 printf("Version %s\n", version);
675 * Load a file of user definitions.
682 if ((in = Fopen(name, "r")) == NULL)