kernel/{hammer2,tmpfs}: Fix ENOTDIR/EISDIR logic.
[dragonfly.git] / usr.bin / mail / lex.c
blob1a34532f64f73d5187359892bc7d4347195fc831
1 /*
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
7 * are met:
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
27 * SUCH DAMAGE.
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 $
33 #include "rcv.h"
34 #include <errno.h>
35 #include <fcntl.h>
36 #include "extern.h"
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
57 * mail.
59 int
60 setfile(char *name)
62 FILE *ibuf;
63 int checkmode, i, fd;
64 struct stat stb;
65 char isedit = *name != '%' || getuserid(myname) != getuid();
66 char *who = name[1] ? name + 1 : myname;
67 char tempname[PATHSIZE];
68 static int shudclob;
70 checkmode = value("checkmode") != NULL;
72 if ((name = expand(name)) == NULL)
73 return (-1);
75 if ((ibuf = Fopen(name, "r")) == NULL) {
76 if (!isedit && errno == ENOENT)
77 goto nomail;
78 warn("%s", name);
79 return (-1);
82 if (fstat(fileno(ibuf), &stb) < 0) {
83 warn("fstat");
84 Fclose(ibuf);
85 return (-1);
88 if (S_ISDIR(stb.st_mode) || !S_ISREG(stb.st_mode)) {
89 Fclose(ibuf);
90 errno = S_ISDIR(stb.st_mode) ? EISDIR : EINVAL;
91 warn("%s", name);
92 return (-1);
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.
102 holdsigs();
103 if (shudclob)
104 quit();
107 * Copy the messages into /tmp
108 * and set pointers.
111 readonly = 0;
112 if ((i = open(name, 1)) < 0)
113 readonly++;
114 else
115 close(i);
116 if (shudclob) {
117 fclose(itf);
118 fclose(otf);
120 shudclob = 1;
121 edit = isedit;
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);
133 rm(tempname);
134 setptr(ibuf, 0);
135 setmsize(msgCount);
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);
142 Fclose(ibuf);
143 relsesigs();
144 sawcom = 0;
145 if ((checkmode || !edit) && msgCount == 0) {
146 nomail:
147 if (!checkmode) {
148 fprintf(stderr, "No mail for %s\n", who);
149 return (-1);
150 } else {
151 return (0);
154 return (checkmode ? 1 : 0);
158 * Incorporate any new mail that has arrived since we first
159 * started reading mail.
162 incfile(void)
164 off_t newsize;
165 int omsgCount = msgCount;
166 FILE *ibuf;
168 ibuf = Fopen(mailname, "r");
169 if (ibuf == NULL)
170 return (-1);
171 holdsigs();
172 newsize = fsize(ibuf);
173 if (newsize == 0)
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);
180 setmsize(msgCount);
181 mailsize = ftello(ibuf);
182 Fclose(ibuf);
183 relsesigs();
184 return (msgCount - omsgCount);
187 int *msgvec;
188 int reset_on_stop; /* do a reset() if stopped */
191 * Interpret user commands one by one. If standard input is not a tty,
192 * print no prompt.
194 void
195 commands(void)
197 int n, eofloop = 0;
198 char linebuf[LINESIZE];
200 if (!sourcing) {
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);
209 setexit();
210 for (;;) {
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");
218 reset_on_stop = 1;
219 printf("%s", prompt);
221 fflush(stdout);
222 sreset();
224 * Read a line of commands from the current input
225 * and handle end of file specially.
227 n = 0;
228 for (;;) {
229 if (readline(input, &linebuf[n], LINESIZE - n) < 0) {
230 if (n == 0)
231 n = -1;
232 break;
234 if ((n = strlen(linebuf)) == 0)
235 break;
236 n--;
237 if (linebuf[n] != '\\')
238 break;
239 linebuf[n++] = ' ';
241 reset_on_stop = 0;
242 if (n < 0) {
243 /* eof */
244 if (loading)
245 break;
246 if (sourcing) {
247 unstack();
248 continue;
250 if (value("interactive") != NULL &&
251 value("ignoreeof") != NULL &&
252 ++eofloop < 25) {
253 printf("Use \"quit\" to quit.\n");
254 continue;
256 break;
258 eofloop = 0;
259 if (execute(linebuf, 0))
260 break;
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)
274 char word[LINESIZE];
275 char *arglist[MAXARGC];
276 const struct cmd *com;
277 char *cp, *cp2;
278 int c, muvec[2];
279 int e = 1;
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++)
292 if (*cp == '!') {
293 if (sourcing) {
294 printf("Can't \"!\" while sourcing\n");
295 goto out;
297 shell(cp+1);
298 return (0);
300 cp2 = word;
301 while (*cp != '\0' && strchr(" \t0123456789$^.:/-+*'\"", *cp) == NULL)
302 *cp2++ = *cp++;
303 *cp2 = '\0';
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
310 * confusion.
313 if (sourcing && *word == '\0')
314 return (0);
315 com = lex(word);
316 if (com == NULL) {
317 printf("Unknown command: \"%s\"\n", word);
318 goto out;
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))
328 return (0);
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
334 * an error.
337 if (!rcvmode && (com->c_argtype & M) == 0) {
338 printf("May not execute \"%s\" while sending\n",
339 com->c_name);
340 goto out;
342 if (sourcing && com->c_argtype & I) {
343 printf("May not execute \"%s\" while sourcing\n",
344 com->c_name);
345 goto out;
347 if (readonly && com->c_argtype & W) {
348 printf("May not execute \"%s\" -- message file is read only\n",
349 com->c_name);
350 goto out;
352 if (contxt && com->c_argtype & R) {
353 printf("Cannot recursively invoke \"%s\"\n", com->c_name);
354 goto out;
356 switch (com->c_argtype & ~(F|P|I|M|T|W|R)) {
357 case MSGLIST:
359 * A message list defaulting to nearest forward
360 * legal message.
362 if (msgvec == NULL) {
363 printf("Illegal use of \"message list\"\n");
364 break;
366 if ((c = getmsglist(cp, msgvec, com->c_msgflag)) < 0)
367 break;
368 if (c == 0) {
369 *msgvec = first(com->c_msgflag, com->c_msgmask);
370 msgvec[1] = 0;
372 if (*msgvec == 0) {
373 printf("No applicable messages\n");
374 break;
376 e = (*com->c_func)(msgvec);
377 break;
379 case NDMLIST:
381 * A message list with no defaults, but no error
382 * if none exist.
384 if (msgvec == NULL) {
385 printf("Illegal use of \"message list\"\n");
386 break;
388 if (getmsglist(cp, msgvec, com->c_msgflag) < 0)
389 break;
390 e = (*com->c_func)(msgvec);
391 break;
393 case STRLIST:
395 * Just the straight string, with
396 * leading blanks removed.
398 while (isspace((unsigned char)*cp))
399 cp++;
400 e = (*com->c_func)(cp);
401 break;
403 case RAWLIST:
405 * A vector of strings, in shell style.
407 if ((c = getrawlist(cp, arglist,
408 sizeof(arglist) / sizeof(*arglist))) < 0)
409 break;
410 if (c < com->c_minargs) {
411 printf("%s requires at least %d arg(s)\n",
412 com->c_name, com->c_minargs);
413 break;
415 if (c > com->c_maxargs) {
416 printf("%s takes no more than %d arg(s)\n",
417 com->c_name, com->c_maxargs);
418 break;
420 e = (*com->c_func)(arglist);
421 break;
423 case NOLIST:
425 * Just the constant zero, for exiting,
426 * eg.
428 e = (*com->c_func)(0);
429 break;
431 default:
432 errx(1, "Unknown argtype");
435 out:
437 * Exit the current source file on
438 * error.
440 if (e) {
441 if (e < 0)
442 return (1);
443 if (loading)
444 return (1);
445 if (sourcing)
446 unstack();
447 return (0);
449 if (com == NULL)
450 return (0);
451 if (value("autoprint") != NULL && com->c_argtype & P)
452 if ((dot->m_flag & MDELETED) == 0) {
453 muvec[0] = dot - &message[0] + 1;
454 muvec[1] = 0;
455 type(muvec);
457 if (!sourcing && (com->c_argtype & T) == 0)
458 sawcom = 1;
459 return (0);
463 * Set the size of the message vector used to construct argument
464 * lists to message list functions.
466 void
467 setmsize(int sz)
469 if (msgvec != NULL)
470 free(msgvec);
471 msgvec = calloc((unsigned)(sz + 1), sizeof(*msgvec));
475 * Find the correct command in the command table corresponding
476 * to the passed command "word"
479 const struct cmd *
480 lex(char *word)
482 const struct cmd *cp;
485 * ignore trailing chars after `#'
487 * lines with beginning `#' are comments
488 * spaces before `#' are ignored in execute()
491 if (*word == '#')
492 *(word+1) = '\0';
495 for (cp = &cmdtab[0]; cp->c_name != NULL; cp++)
496 if (isprefix(word, cp->c_name))
497 return (cp);
498 return (NULL);
502 * Determine if as1 is a valid prefix of as2.
503 * Return true if yep.
506 isprefix(const char *as1, const char *as2)
508 const char *s1, *s2;
510 s1 = as1;
511 s2 = as2;
512 while (*s1++ == *s2)
513 if (*s2++ == '\0')
514 return (1);
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 */
528 /*ARGSUSED*/
529 void
530 intr(int s)
532 noreset = 0;
533 if (!inithdr)
534 sawcom++;
535 inithdr = 0;
536 while (sourcing)
537 unstack();
539 close_all_files();
541 if (image >= 0) {
542 close(image);
543 image = -1;
545 fprintf(stderr, "Interrupt\n");
546 reset(0);
550 * When we wake up after ^Z, reprint the prompt.
552 void
553 stop(int s)
555 sig_t old_action = signal(s, SIG_DFL);
556 sigset_t nset;
558 sigemptyset(&nset);
559 sigaddset(&nset, s);
560 sigprocmask(SIG_UNBLOCK, &nset, NULL);
561 kill(0, s);
562 sigprocmask(SIG_BLOCK, &nset, NULL);
563 signal(s, old_action);
564 if (reset_on_stop) {
565 reset_on_stop = 0;
566 reset(0);
571 * Branch here on hangup signal and simulate "exit".
573 /*ARGSUSED*/
574 void
575 hangup(int s)
577 /* nothing to do? */
578 exit(1);
582 * Announce the presence of the current Mail version,
583 * give the message count, and print a header listing.
585 void
586 announce(void)
588 int vec[2], mdot;
590 mdot = newfileinfo(0);
591 vec[0] = mdot;
592 vec[1] = 0;
593 dot = &message[mdot - 1];
594 if (msgCount > 0 && value("noheader") == NULL) {
595 inithdr++;
596 headers(vec);
597 inithdr = 0;
602 * Announce information about the file we are editing.
603 * Return a likely place to set dot.
606 newfileinfo(int omsgCount)
608 struct message *mp;
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)
614 break;
615 if (mp >= &message[msgCount])
616 for (mp = &message[omsgCount]; mp < &message[msgCount]; mp++)
617 if ((mp->m_flag & MREAD) == 0)
618 break;
619 if (mp < &message[msgCount])
620 mdot = mp - &message[0] + 1;
621 else
622 mdot = omsgCount + 1;
623 s = d = 0;
624 for (mp = &message[0], n = 0, u = 0; mp < &message[msgCount]; mp++) {
625 if (mp->m_flag & MNEW)
626 n++;
627 if ((mp->m_flag & MREAD) == 0)
628 u++;
629 if (mp->m_flag & MDELETED)
630 d++;
631 if (mp->m_flag & MSAVED)
632 s++;
634 ename = mailname;
635 if (getfold(fname, sizeof(fname) - 1) >= 0) {
636 strcat(fname, "/");
637 if (strncmp(fname, mailname, strlen(fname)) == 0) {
638 snprintf(zname, sizeof(zname), "+%s",
639 mailname + strlen(fname));
640 ename = zname;
643 printf("\"%s\": ", ename);
644 if (msgCount == 1)
645 printf("1 message");
646 else
647 printf("%d messages", msgCount);
648 if (n > 0)
649 printf(" %d new", n);
650 if (u-n > 0)
651 printf(" %d unread", u);
652 if (d > 0)
653 printf(" %d deleted", d);
654 if (s > 0)
655 printf(" %d saved", s);
656 if (readonly)
657 printf(" [Read only]");
658 printf("\n");
659 return (mdot);
663 * Print the current version number.
666 /*ARGSUSED*/
668 pversion(int e)
670 printf("Version %s\n", version);
671 return (0);
675 * Load a file of user definitions.
677 void
678 load(char *name)
680 FILE *in, *oldin;
682 if ((in = Fopen(name, "r")) == NULL)
683 return;
684 oldin = input;
685 input = in;
686 loading = 1;
687 sourcing = 1;
688 commands();
689 loading = 0;
690 sourcing = 0;
691 input = oldin;
692 Fclose(in);