2 * SPDX-License-Identifier: BSD-3-Clause
4 * Copyright (c) 1980, 1993
5 * The Regents of the University of California. All rights reserved.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. Neither the name of the University nor the names of its contributors
16 * may be used to endorse or promote products derived from this software
17 * without specific prior written permission.
19 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
38 * Mail -- a mail program
40 * Lexical processing of commands.
43 static const char *prompt = "& ";
45 extern const struct cmd cmdtab[];
46 extern const char *version;
49 * Set up editing on the given file name.
50 * If the first character of name is %, we are considered to be
51 * editing the file, otherwise we are reading our mail which has
52 * signficance for mbox and so forth.
54 * If the -e option is being passed to mail, this function has a
55 * tri-state return code: -1 on error, 0 on no mail, 1 if there is
64 char isedit = *name != '%' || getuserid(myname) != getuid();
65 char *who = name[1] ? name + 1 : myname;
66 char tempname[PATHSIZE];
69 checkmode = value("checkmode") != NULL;
70 if ((name = expand(name)) == NULL)
73 if ((ibuf = Fopen(name, "r")) == NULL) {
74 if (!isedit && errno == ENOENT)
80 if (fstat(fileno(ibuf), &stb) < 0) {
86 if (S_ISDIR(stb.st_mode) || !S_ISREG(stb.st_mode)) {
88 errno = S_ISDIR(stb.st_mode) ? EISDIR : EINVAL;
94 * Looks like all will be well. We must now relinquish our
95 * hold on the current set of stuff. Must hold signals
96 * while we are reading the new file, else we will ruin
97 * the message[] data structure.
105 * Copy the messages into /tmp
110 if ((i = open(name, 1)) < 0)
120 strlcpy(prevfile, mailname, sizeof(prevfile));
121 if (name != mailname)
122 strlcpy(mailname, name, sizeof(mailname));
123 mailsize = fsize(ibuf);
124 (void)snprintf(tempname, sizeof(tempname),
125 "%s/mail.RxXXXXXXXXXX", tmpdir);
126 if ((fd = mkstemp(tempname)) == -1 || (otf = fdopen(fd, "w")) == NULL)
127 err(1, "%s", tempname);
128 (void)fcntl(fileno(otf), F_SETFD, 1);
129 if ((itf = fopen(tempname, "r")) == NULL)
130 err(1, "%s", tempname);
131 (void)fcntl(fileno(itf), F_SETFD, 1);
136 * New mail may have arrived while we were reading
137 * the mail file, so reset mailsize to be where
138 * we really are in the file...
140 mailsize = ftello(ibuf);
145 if ((checkmode || !edit) && msgCount == 0) {
148 fprintf(stderr, "No mail for %s\n", who);
153 return (checkmode ? 1 : 0);
157 * Incorporate any new mail that has arrived since we first
158 * started reading mail.
164 int omsgCount = msgCount;
167 ibuf = Fopen(mailname, "r");
171 newsize = fsize(ibuf);
173 return (-1); /* mail box is now empty??? */
174 if (newsize < mailsize)
175 return (-1); /* mail box has shrunk??? */
176 if (newsize == mailsize)
177 return (0); /* no new mail */
178 setptr(ibuf, mailsize);
180 mailsize = ftello(ibuf);
183 return (msgCount - omsgCount);
187 static int reset_on_stop; /* do a reset() if stopped */
190 * Interpret user commands one by one. If standard input is not a tty,
197 char linebuf[LINESIZE];
200 if (signal(SIGINT, SIG_IGN) != SIG_IGN)
201 (void)signal(SIGINT, intr);
202 if (signal(SIGHUP, SIG_IGN) != SIG_IGN)
203 (void)signal(SIGHUP, hangup);
204 (void)signal(SIGTSTP, stop);
205 (void)signal(SIGTTOU, stop);
206 (void)signal(SIGTTIN, stop);
211 * Print the prompt, if needed. Clear out
212 * string space, and flush the output.
214 if (!sourcing && value("interactive") != NULL) {
215 if ((value("autoinc") != NULL) && (incfile() > 0))
216 printf("New mail has arrived.\n");
218 printf("%s", prompt);
220 (void)fflush(stdout);
223 * Read a line of commands from the current input
224 * and handle end of file specially.
228 if (readline(input, &linebuf[n], LINESIZE - n) < 0) {
233 if ((n = strlen(linebuf)) == 0)
236 if (linebuf[n] != '\\')
249 if (value("interactive") != NULL &&
250 value("ignoreeof") != NULL &&
252 printf("Use \"quit\" to quit.\n");
258 if (execute(linebuf, 0))
264 * Execute a single command.
265 * Command functions return 0 for success, 1 for error, and -1
266 * for abort. A 1 or -1 aborts a load or source. A -1 aborts
267 * the interactive command loop.
268 * Contxt is non-zero if called while composing mail.
271 execute(char linebuf[], int 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.
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 static 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);
558 (void)sigemptyset(&nset);
559 (void)sigaddset(&nset, s);
560 (void)sigprocmask(SIG_UNBLOCK, &nset, NULL);
562 (void)sigprocmask(SIG_BLOCK, &nset, NULL);
563 (void)signal(s, old_action);
571 * Branch here on hangup signal and simulate "exit".
574 hangup(int s __unused)
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 (void)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.
667 pversion(void *arg __unused)
670 printf("Version %s\n", version);
675 * Load a file of user definitions.
682 if ((in = Fopen(name, "r")) == NULL)