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. Neither the name of the University nor the names of its contributors
14 * may be used to endorse or promote products derived from this software
15 * without specific prior written permission.
17 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 * @(#)lex.c 8.2 (Berkeley) 4/20/95
30 * $FreeBSD: src/usr.bin/mail/lex.c,v 1.5.6.5 2003/01/06 05:46:03 mikeh Exp $
39 * Mail -- a mail program
41 * Lexical processing of commands.
44 const char *prompt = "& ";
46 extern const struct cmd cmdtab[];
47 extern const char *version;
50 * Set up editing on the given file name.
51 * If the first character of name is %, we are considered to be
52 * editing the file, otherwise we are reading our mail which has
53 * signficance for mbox and so forth.
55 * If the -e option is passed to mail, this function has a
56 * tri-state return code: -1 on error, 0 on no mail, 1 if there is
65 char isedit = *name != '%' || getuserid(myname) != getuid();
66 char *who = name[1] ? name + 1 : myname;
67 char tempname[PATHSIZE];
70 checkmode = value("checkmode") != NULL;
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 snprintf(tempname, sizeof(tempname), "%s/mail.RxXXXXXXXXXX", tmpdir);
127 if ((fd = mkstemp(tempname)) == -1 || (otf = fdopen(fd, "w")) == NULL)
128 err(1, "%s", tempname);
129 fcntl(fileno(otf), F_SETFD, 1);
130 if ((itf = fopen(tempname, "r")) == NULL)
131 err(1, "%s", tempname);
132 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 ((checkmode || !edit) && msgCount == 0) {
148 fprintf(stderr, "No mail for %s\n", who);
154 return (checkmode ? 1 : 0);
158 * Incorporate any new mail that has arrived since we first
159 * started reading mail.
165 int omsgCount = msgCount;
168 ibuf = Fopen(mailname, "r");
172 newsize = fsize(ibuf);
174 return (-1); /* mail box is now empty??? */
175 if (newsize < mailsize)
176 return (-1); /* mail box has shrunk??? */
177 if (newsize == mailsize)
178 return (0); /* no new mail */
179 setptr(ibuf, mailsize);
181 mailsize = ftello(ibuf);
184 return (msgCount - omsgCount);
188 int reset_on_stop; /* do a reset() if stopped */
191 * Interpret user commands one by one. If standard input is not a tty,
198 char linebuf[LINESIZE];
201 if (signal(SIGINT, SIG_IGN) != SIG_IGN)
202 signal(SIGINT, intr);
203 if (signal(SIGHUP, SIG_IGN) != SIG_IGN)
204 signal(SIGHUP, hangup);
205 signal(SIGTSTP, stop);
206 signal(SIGTTOU, stop);
207 signal(SIGTTIN, stop);
212 * Print the prompt, if needed. Clear out
213 * string space, and flush the output.
215 if (!sourcing && value("interactive") != NULL) {
216 if ((value("autoinc") != NULL) && (incfile() > 0))
217 printf("New mail has arrived.\n");
219 printf("%s", prompt);
224 * Read a line of commands from the current input
225 * and handle end of file specially.
229 if (readline(input, &linebuf[n], LINESIZE - n) < 0) {
234 if ((n = strlen(linebuf)) == 0)
237 if (linebuf[n] != '\\')
250 if (value("interactive") != NULL &&
251 value("ignoreeof") != NULL &&
253 printf("Use \"quit\" to quit.\n");
259 if (execute(linebuf, 0))
265 * Execute a single command.
266 * Command functions return 0 for success, 1 for error, and -1
267 * for abort. A 1 or -1 aborts a load or source. A -1 aborts
268 * the interactive command loop.
269 * Contxt is non-zero if called while composing mail.
272 execute(char *linebuf, int contxt)
275 char *arglist[MAXARGC];
276 const struct cmd *com;
282 * Strip the white space away from the beginning
283 * of the command, then scan out a word, which
284 * consists of anything except digits and white space.
286 * Handle ! escapes differently to get the correct
287 * lexical conventions.
290 for (cp = linebuf; isspace((unsigned char)*cp); cp++)
294 printf("Can't \"!\" while sourcing\n");
301 while (*cp != '\0' && strchr(" \t0123456789$^.:/-+*'\"", *cp) == NULL)
306 * Look up the command; if not found, bitch.
307 * Normally, a blank command would map to the
308 * first command in the table; while sourcing,
309 * however, we ignore blank lines to eliminate
313 if (sourcing && *word == '\0')
317 printf("Unknown command: \"%s\"\n", word);
322 * See if we should execute the command -- if a conditional
323 * we always execute it, otherwise, check the state of cond.
326 if ((com->c_argtype & F) == 0)
327 if ((cond == CRCV && !rcvmode) || (cond == CSEND && rcvmode))
331 * Process the arguments to the command, depending
332 * on the type he expects. Default to an error.
333 * If we are sourcing an interactive command, it's
337 if (!rcvmode && (com->c_argtype & M) == 0) {
338 printf("May not execute \"%s\" while sending\n",
342 if (sourcing && com->c_argtype & I) {
343 printf("May not execute \"%s\" while sourcing\n",
347 if (readonly && com->c_argtype & W) {
348 printf("May not execute \"%s\" -- message file is read only\n",
352 if (contxt && com->c_argtype & R) {
353 printf("Cannot recursively invoke \"%s\"\n", com->c_name);
356 switch (com->c_argtype & ~(F|P|I|M|T|W|R)) {
359 * A message list defaulting to nearest forward
362 if (msgvec == NULL) {
363 printf("Illegal use of \"message list\"\n");
366 if ((c = getmsglist(cp, msgvec, com->c_msgflag)) < 0)
369 *msgvec = first(com->c_msgflag, com->c_msgmask);
373 printf("No applicable messages\n");
376 e = (*com->c_func)(msgvec);
381 * A message list with no defaults, but no error
384 if (msgvec == NULL) {
385 printf("Illegal use of \"message list\"\n");
388 if (getmsglist(cp, msgvec, com->c_msgflag) < 0)
390 e = (*com->c_func)(msgvec);
395 * Just the straight string, with
396 * leading blanks removed.
398 while (isspace((unsigned char)*cp))
400 e = (*com->c_func)(cp);
405 * A vector of strings, in shell style.
407 if ((c = getrawlist(cp, arglist,
408 sizeof(arglist) / sizeof(*arglist))) < 0)
410 if (c < com->c_minargs) {
411 printf("%s requires at least %d arg(s)\n",
412 com->c_name, com->c_minargs);
415 if (c > com->c_maxargs) {
416 printf("%s takes no more than %d arg(s)\n",
417 com->c_name, com->c_maxargs);
420 e = (*com->c_func)(arglist);
425 * Just the constant zero, for exiting,
428 e = (*com->c_func)(0);
432 errx(1, "Unknown argtype");
437 * Exit the current source file on
451 if (value("autoprint") != NULL && com->c_argtype & P)
452 if ((dot->m_flag & MDELETED) == 0) {
453 muvec[0] = dot - &message[0] + 1;
457 if (!sourcing && (com->c_argtype & T) == 0)
463 * Set the size of the message vector used to construct argument
464 * lists to message list functions.
471 msgvec = calloc((unsigned)(sz + 1), sizeof(*msgvec));
475 * Find the correct command in the command table corresponding
476 * to the passed command "word"
482 const struct cmd *cp;
485 * ignore trailing chars after `#'
487 * lines with beginning `#' are comments
488 * spaces before `#' are ignored in execute()
495 for (cp = &cmdtab[0]; cp->c_name != NULL; cp++)
496 if (isprefix(word, cp->c_name))
502 * Determine if as1 is a valid prefix of as2.
503 * Return true if yep.
506 isprefix(const char *as1, const char *as2)
515 return (*--s1 == '\0');
519 * The following gets called on receipt of an interrupt. This is
520 * to abort printout of a command, mainly.
521 * Dispatching here when command() is inactive crashes rcv.
522 * Close all open files except 0, 1, 2, and the temporary.
523 * Also, unstack all source files.
526 int inithdr; /* am printing startup headers */
545 fprintf(stderr, "Interrupt\n");
550 * When we wake up after ^Z, reprint the prompt.
555 sig_t old_action = signal(s, SIG_DFL);
560 sigprocmask(SIG_UNBLOCK, &nset, NULL);
562 sigprocmask(SIG_BLOCK, &nset, NULL);
563 signal(s, old_action);
571 * Branch here on hangup signal and simulate "exit".
582 * Announce the presence of the current Mail version,
583 * give the message count, and print a header listing.
590 mdot = newfileinfo(0);
593 dot = &message[mdot - 1];
594 if (msgCount > 0 && value("noheader") == NULL) {
602 * Announce information about the file we are editing.
603 * Return a likely place to set dot.
606 newfileinfo(int omsgCount)
609 int u, n, mdot, d, s;
610 char fname[PATHSIZE+1], zname[PATHSIZE+1], *ename;
612 for (mp = &message[omsgCount]; mp < &message[msgCount]; mp++)
613 if (mp->m_flag & MNEW)
615 if (mp >= &message[msgCount])
616 for (mp = &message[omsgCount]; mp < &message[msgCount]; mp++)
617 if ((mp->m_flag & MREAD) == 0)
619 if (mp < &message[msgCount])
620 mdot = mp - &message[0] + 1;
622 mdot = omsgCount + 1;
624 for (mp = &message[0], n = 0, u = 0; mp < &message[msgCount]; mp++) {
625 if (mp->m_flag & MNEW)
627 if ((mp->m_flag & MREAD) == 0)
629 if (mp->m_flag & MDELETED)
631 if (mp->m_flag & MSAVED)
635 if (getfold(fname, sizeof(fname) - 1) >= 0) {
637 if (strncmp(fname, mailname, strlen(fname)) == 0) {
638 snprintf(zname, sizeof(zname), "+%s",
639 mailname + strlen(fname));
643 printf("\"%s\": ", ename);
647 printf("%d messages", msgCount);
649 printf(" %d new", n);
651 printf(" %d unread", u);
653 printf(" %d deleted", d);
655 printf(" %d saved", s);
657 printf(" [Read only]");
663 * 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)