Annotation of src/usr.bin/mail/lex.c, Revision 1.9
1.9 ! millert 1: /* $OpenBSD: lex.c,v 1.8 1997/07/14 00:24:27 millert Exp $ */
1.6 millert 2: /* $NetBSD: lex.c,v 1.10 1997/05/17 19:55:13 pk Exp $ */
1.2 niklas 3:
1.1 deraadt 4: /*
5: * Copyright (c) 1980, 1993
6: * The Regents of the University of California. All rights reserved.
7: *
8: * Redistribution and use in source and binary forms, with or without
9: * modification, are permitted provided that the following conditions
10: * are met:
11: * 1. Redistributions of source code must retain the above copyright
12: * notice, this list of conditions and the following disclaimer.
13: * 2. Redistributions in binary form must reproduce the above copyright
14: * notice, this list of conditions and the following disclaimer in the
15: * documentation and/or other materials provided with the distribution.
16: * 3. All advertising materials mentioning features or use of this software
17: * must display the following acknowledgement:
18: * This product includes software developed by the University of
19: * California, Berkeley and its contributors.
20: * 4. Neither the name of the University nor the names of its contributors
21: * may be used to endorse or promote products derived from this software
22: * without specific prior written permission.
23: *
24: * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
25: * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26: * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27: * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
28: * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29: * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30: * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31: * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32: * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33: * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34: * SUCH DAMAGE.
35: */
36:
37: #ifndef lint
1.3 deraadt 38: #if 0
1.6 millert 39: static char sccsid[] = "@(#)lex.c 8.2 (Berkeley) 4/20/95";
1.3 deraadt 40: #else
1.9 ! millert 41: static char rcsid[] = "$OpenBSD: lex.c,v 1.8 1997/07/14 00:24:27 millert Exp $";
1.3 deraadt 42: #endif
1.1 deraadt 43: #endif /* not lint */
44:
45: #include "rcv.h"
46: #include <errno.h>
47: #include <fcntl.h>
48: #include "extern.h"
49:
50: /*
51: * Mail -- a mail program
52: *
53: * Lexical processing of commands.
54: */
55:
56: char *prompt = "& ";
57:
58: /*
59: * Set up editing on the given file name.
60: * If the first character of name is %, we are considered to be
61: * editing the file, otherwise we are reading our mail which has
62: * signficance for mbox and so forth.
63: */
64: int
65: setfile(name)
66: char *name;
67: {
68: FILE *ibuf;
69: int i;
70: struct stat stb;
71: char isedit = *name != '%';
72: char *who = name[1] ? name + 1 : myname;
73: static int shudclob;
74: extern char *tempMesg;
75:
1.8 millert 76: if ((name = expand(name)) == NULL)
1.6 millert 77: return(-1);
1.1 deraadt 78:
79: if ((ibuf = Fopen(name, "r")) == NULL) {
80: if (!isedit && errno == ENOENT)
81: goto nomail;
1.6 millert 82: warn(name);
1.1 deraadt 83: return(-1);
84: }
85:
86: if (fstat(fileno(ibuf), &stb) < 0) {
1.6 millert 87: warn("fstat");
88: (void)Fclose(ibuf);
89: return(-1);
1.1 deraadt 90: }
91:
92: switch (stb.st_mode & S_IFMT) {
93: case S_IFDIR:
1.6 millert 94: (void)Fclose(ibuf);
1.1 deraadt 95: errno = EISDIR;
1.6 millert 96: warn(name);
97: return(-1);
1.1 deraadt 98:
99: case S_IFREG:
100: break;
101:
102: default:
1.6 millert 103: (void)Fclose(ibuf);
1.1 deraadt 104: errno = EINVAL;
1.6 millert 105: warn(name);
106: return(-1);
1.1 deraadt 107: }
108:
109: /*
110: * Looks like all will be well. We must now relinquish our
111: * hold on the current set of stuff. Must hold signals
112: * while we are reading the new file, else we will ruin
113: * the message[] data structure.
114: */
115:
116: holdsigs();
117: if (shudclob)
118: quit();
119:
120: /*
121: * Copy the messages into /tmp
122: * and set pointers.
123: */
124:
125: readonly = 0;
126: if ((i = open(name, 1)) < 0)
127: readonly++;
128: else
1.6 millert 129: (void)close(i);
1.1 deraadt 130: if (shudclob) {
1.6 millert 131: (void)fclose(itf);
132: (void)fclose(otf);
1.1 deraadt 133: }
134: shudclob = 1;
135: edit = isedit;
136: strcpy(prevfile, mailname);
137: if (name != mailname)
138: strcpy(mailname, name);
139: mailsize = fsize(ibuf);
1.6 millert 140: if ((otf = fopen(tempMesg, "w")) == NULL)
141: err(1, tempMesg);
1.7 millert 142: (void)fcntl(fileno(otf), F_SETFD, 1);
1.6 millert 143: if ((itf = fopen(tempMesg, "r")) == NULL)
144: err(1, tempMesg);
1.7 millert 145: (void)fcntl(fileno(itf), F_SETFD, 1);
1.1 deraadt 146: rm(tempMesg);
1.6 millert 147: setptr(ibuf, 0);
1.1 deraadt 148: setmsize(msgCount);
1.6 millert 149: /*
150: * New mail may have arrived while we were reading
151: * the mail file, so reset mailsize to be where
152: * we really are in the file...
153: */
154: mailsize = ftell(ibuf);
155: (void)Fclose(ibuf);
1.1 deraadt 156: relsesigs();
157: sawcom = 0;
158: if (!edit && msgCount == 0) {
159: nomail:
160: fprintf(stderr, "No mail for %s\n", who);
1.6 millert 161: return(-1);
1.1 deraadt 162: }
163: return(0);
164: }
165:
1.6 millert 166: /*
167: * Incorporate any new mail that has arrived since we first
168: * started reading mail.
169: */
170: int
171: incfile()
172: {
173: int newsize;
174: int omsgCount = msgCount;
175: FILE *ibuf;
176:
177: ibuf = Fopen(mailname, "r");
178: if (ibuf == NULL)
179: return(-1);
180: holdsigs();
181: newsize = fsize(ibuf);
1.9 ! millert 182: /* make sure mail box has grown and is non-empty */
! 183: if (newsize == 0 || newsize <= mailsize) {
! 184: relsesigs();
! 185: return(newsize == mailsize ? 0 : -1);
! 186: }
1.6 millert 187: setptr(ibuf, mailsize);
188: setmsize(msgCount);
189: mailsize = ftell(ibuf);
190: (void)Fclose(ibuf);
191: relsesigs();
192: return(msgCount - omsgCount);
193: }
194:
195:
1.1 deraadt 196: int *msgvec;
197: int reset_on_stop; /* do a reset() if stopped */
198:
199: /*
200: * Interpret user commands one by one. If standard input is not a tty,
201: * print no prompt.
202: */
203: void
204: commands()
205: {
206: int eofloop = 0;
207: register int n;
208: char linebuf[LINESIZE];
1.3 deraadt 209: #if __GNUC__
1.8 millert 210: /* Avoid siglongjmp clobbering */
1.7 millert 211: (void)&eofloop;
1.3 deraadt 212: #endif
1.1 deraadt 213:
214: if (!sourcing) {
215: if (signal(SIGINT, SIG_IGN) != SIG_IGN)
216: signal(SIGINT, intr);
217: if (signal(SIGHUP, SIG_IGN) != SIG_IGN)
218: signal(SIGHUP, hangup);
219: signal(SIGTSTP, stop);
220: signal(SIGTTOU, stop);
221: signal(SIGTTIN, stop);
222: }
223: setexit();
224: for (;;) {
225: /*
226: * Print the prompt, if needed. Clear out
227: * string space, and flush the output.
228: */
1.8 millert 229: if (!sourcing && value("interactive") != NULL) {
230: if ((value("autoinc") != NULL) && (incfile() > 0))
1.6 millert 231: puts("New mail has arrived.");
1.1 deraadt 232: reset_on_stop = 1;
233: printf(prompt);
234: }
235: fflush(stdout);
236: sreset();
237: /*
238: * Read a line of commands from the current input
239: * and handle end of file specially.
240: */
241: n = 0;
242: for (;;) {
243: if (readline(input, &linebuf[n], LINESIZE - n) < 0) {
244: if (n == 0)
245: n = -1;
246: break;
247: }
248: if ((n = strlen(linebuf)) == 0)
249: break;
250: n--;
251: if (linebuf[n] != '\\')
252: break;
253: linebuf[n++] = ' ';
254: }
255: reset_on_stop = 0;
256: if (n < 0) {
257: /* eof */
258: if (loading)
259: break;
260: if (sourcing) {
261: unstack();
262: continue;
263: }
1.8 millert 264: if (value("interactive") != NULL &&
265: value("ignoreeof") != NULL &&
1.1 deraadt 266: ++eofloop < 25) {
1.6 millert 267: puts("Use \"quit\" to quit.");
1.1 deraadt 268: continue;
269: }
270: break;
271: }
272: eofloop = 0;
273: if (execute(linebuf, 0))
274: break;
275: }
276: }
277:
278: /*
279: * Execute a single command.
280: * Command functions return 0 for success, 1 for error, and -1
281: * for abort. A 1 or -1 aborts a load or source. A -1 aborts
282: * the interactive command loop.
283: * Contxt is non-zero if called while composing mail.
284: */
285: int
286: execute(linebuf, contxt)
287: char linebuf[];
288: int contxt;
289: {
290: char word[LINESIZE];
291: char *arglist[MAXARGC];
1.3 deraadt 292: const struct cmd *com = NULL;
1.1 deraadt 293: register char *cp, *cp2;
294: register int c;
295: int muvec[2];
296: int e = 1;
297:
298: /*
299: * Strip the white space away from the beginning
300: * of the command, then scan out a word, which
301: * consists of anything except digits and white space.
302: *
303: * Handle ! escapes differently to get the correct
304: * lexical conventions.
305: */
306:
307: for (cp = linebuf; isspace(*cp); cp++)
308: ;
309: if (*cp == '!') {
310: if (sourcing) {
1.6 millert 311: puts("Can't \"!\" while sourcing");
1.1 deraadt 312: goto out;
313: }
314: shell(cp+1);
315: return(0);
316: }
317: cp2 = word;
1.8 millert 318: while (*cp && strchr(" \t0123456789$^.:/-+*'\"", *cp) == NULL)
1.1 deraadt 319: *cp2++ = *cp++;
320: *cp2 = '\0';
321:
322: /*
323: * Look up the command; if not found, bitch.
324: * Normally, a blank command would map to the
325: * first command in the table; while sourcing,
326: * however, we ignore blank lines to eliminate
327: * confusion.
328: */
329:
330: if (sourcing && *word == '\0')
331: return(0);
332: com = lex(word);
333: if (com == NONE) {
334: printf("Unknown command: \"%s\"\n", word);
335: goto out;
336: }
337:
338: /*
339: * See if we should execute the command -- if a conditional
340: * we always execute it, otherwise, check the state of cond.
341: */
342:
343: if ((com->c_argtype & F) == 0)
1.3 deraadt 344: if ((cond == CRCV && !rcvmode) || (cond == CSEND && rcvmode))
1.1 deraadt 345: return(0);
346:
347: /*
348: * Process the arguments to the command, depending
349: * on the type he expects. Default to an error.
350: * If we are sourcing an interactive command, it's
351: * an error.
352: */
353:
354: if (!rcvmode && (com->c_argtype & M) == 0) {
355: printf("May not execute \"%s\" while sending\n",
356: com->c_name);
357: goto out;
358: }
359: if (sourcing && com->c_argtype & I) {
360: printf("May not execute \"%s\" while sourcing\n",
361: com->c_name);
362: goto out;
363: }
364: if (readonly && com->c_argtype & W) {
365: printf("May not execute \"%s\" -- message file is read only\n",
366: com->c_name);
367: goto out;
368: }
369: if (contxt && com->c_argtype & R) {
370: printf("Cannot recursively invoke \"%s\"\n", com->c_name);
371: goto out;
372: }
373: switch (com->c_argtype & ~(F|P|I|M|T|W|R)) {
374: case MSGLIST:
375: /*
376: * A message list defaulting to nearest forward
377: * legal message.
378: */
379: if (msgvec == 0) {
1.6 millert 380: puts("Illegal use of \"message list\"");
1.1 deraadt 381: break;
382: }
383: if ((c = getmsglist(cp, msgvec, com->c_msgflag)) < 0)
384: break;
385: if (c == 0) {
386: *msgvec = first(com->c_msgflag,
387: com->c_msgmask);
388: msgvec[1] = NULL;
389: }
390: if (*msgvec == NULL) {
1.6 millert 391: puts("No applicable messages");
1.1 deraadt 392: break;
393: }
394: e = (*com->c_func)(msgvec);
395: break;
396:
397: case NDMLIST:
398: /*
399: * A message list with no defaults, but no error
400: * if none exist.
401: */
402: if (msgvec == 0) {
1.6 millert 403: puts("Illegal use of \"message list\"");
1.1 deraadt 404: break;
405: }
406: if (getmsglist(cp, msgvec, com->c_msgflag) < 0)
407: break;
408: e = (*com->c_func)(msgvec);
409: break;
410:
411: case STRLIST:
412: /*
413: * Just the straight string, with
414: * leading blanks removed.
415: */
416: while (isspace(*cp))
417: cp++;
418: e = (*com->c_func)(cp);
419: break;
420:
421: case RAWLIST:
422: /*
423: * A vector of strings, in shell style.
424: */
425: if ((c = getrawlist(cp, arglist,
1.6 millert 426: sizeof(arglist) / sizeof(*arglist))) < 0)
1.1 deraadt 427: break;
428: if (c < com->c_minargs) {
429: printf("%s requires at least %d arg(s)\n",
430: com->c_name, com->c_minargs);
431: break;
432: }
433: if (c > com->c_maxargs) {
434: printf("%s takes no more than %d arg(s)\n",
435: com->c_name, com->c_maxargs);
436: break;
437: }
438: e = (*com->c_func)(arglist);
439: break;
440:
441: case NOLIST:
442: /*
443: * Just the constant zero, for exiting,
444: * eg.
445: */
446: e = (*com->c_func)(0);
447: break;
448:
449: default:
450: panic("Unknown argtype");
451: }
452:
453: out:
454: /*
455: * Exit the current source file on
456: * error.
457: */
458: if (e) {
459: if (e < 0)
1.6 millert 460: return(1);
1.1 deraadt 461: if (loading)
1.6 millert 462: return(1);
1.1 deraadt 463: if (sourcing)
464: unstack();
1.6 millert 465: return(0);
1.1 deraadt 466: }
1.3 deraadt 467: if (com == NULL)
468: return(0);
1.8 millert 469: if (value("autoprint") != NULL && com->c_argtype & P)
1.1 deraadt 470: if ((dot->m_flag & MDELETED) == 0) {
471: muvec[0] = dot - &message[0] + 1;
472: muvec[1] = 0;
473: type(muvec);
474: }
475: if (!sourcing && (com->c_argtype & T) == 0)
476: sawcom = 1;
477: return(0);
478: }
479:
480: /*
481: * Set the size of the message vector used to construct argument
482: * lists to message list functions.
483: */
484: void
485: setmsize(sz)
486: int sz;
487: {
488:
489: if (msgvec != 0)
1.7 millert 490: (void)free(msgvec);
491: msgvec = (int *)calloc(sz + 1, sizeof(*msgvec));
1.1 deraadt 492: }
493:
494: /*
495: * Find the correct command in the command table corresponding
496: * to the passed command "word"
497: */
498:
1.2 niklas 499: const struct cmd *
1.1 deraadt 500: lex(word)
501: char word[];
502: {
1.2 niklas 503: extern const struct cmd cmdtab[];
504: register const struct cmd *cp;
1.1 deraadt 505:
1.8 millert 506: for (cp = &cmdtab[0]; cp->c_name != NULL; cp++)
1.1 deraadt 507: if (isprefix(word, cp->c_name))
508: return(cp);
509: return(NONE);
510: }
511:
512: /*
513: * Determine if as1 is a valid prefix of as2.
514: * Return true if yep.
515: */
516: int
517: isprefix(as1, as2)
518: char *as1, *as2;
519: {
520: register char *s1, *s2;
521:
522: s1 = as1;
523: s2 = as2;
524: while (*s1++ == *s2)
525: if (*s2++ == '\0')
526: return(1);
527: return(*--s1 == '\0');
528: }
529:
530: /*
531: * The following gets called on receipt of an interrupt. This is
532: * to abort printout of a command, mainly.
533: * Dispatching here when command() is inactive crashes rcv.
534: * Close all open files except 0, 1, 2, and the temporary.
535: * Also, unstack all source files.
536: */
537:
538: int inithdr; /* am printing startup headers */
539:
540: /*ARGSUSED*/
541: void
542: intr(s)
543: int s;
544: {
545:
546: noreset = 0;
547: if (!inithdr)
548: sawcom++;
549: inithdr = 0;
550: while (sourcing)
551: unstack();
552:
553: close_all_files();
554:
555: if (image >= 0) {
1.6 millert 556: (void)close(image);
1.1 deraadt 557: image = -1;
558: }
1.6 millert 559: fputs("Interrupt\n", stderr);
1.1 deraadt 560: reset(0);
561: }
562:
563: /*
564: * When we wake up after ^Z, reprint the prompt.
565: */
566: void
567: stop(s)
568: int s;
569: {
570: sig_t old_action = signal(s, SIG_DFL);
1.3 deraadt 571: sigset_t nset;
1.1 deraadt 572:
1.3 deraadt 573: sigemptyset(&nset);
574: sigaddset(&nset, s);
575: sigprocmask(SIG_UNBLOCK, &nset, NULL);
1.1 deraadt 576: kill(0, s);
1.3 deraadt 577: sigprocmask(SIG_BLOCK, &nset, NULL);
1.1 deraadt 578: signal(s, old_action);
579: if (reset_on_stop) {
580: reset_on_stop = 0;
581: reset(0);
582: }
583: }
584:
585: /*
586: * Branch here on hangup signal and simulate "exit".
587: */
588: /*ARGSUSED*/
589: void
590: hangup(s)
591: int s;
592: {
593:
594: /* nothing to do? */
595: exit(1);
596: }
597:
598: /*
599: * Announce the presence of the current Mail version,
600: * give the message count, and print a header listing.
601: */
602: void
603: announce()
604: {
605: int vec[2], mdot;
606:
1.6 millert 607: mdot = newfileinfo(0);
1.1 deraadt 608: vec[0] = mdot;
609: vec[1] = 0;
610: dot = &message[mdot - 1];
1.8 millert 611: if (msgCount > 0 && value("noheader") == NULL) {
1.1 deraadt 612: inithdr++;
613: headers(vec);
614: inithdr = 0;
615: }
616: }
617:
618: /*
619: * Announce information about the file we are editing.
620: * Return a likely place to set dot.
621: */
622: int
1.6 millert 623: newfileinfo(omsgCount)
624: int omsgCount;
1.1 deraadt 625: {
626: register struct message *mp;
627: register int u, n, mdot, d, s;
1.6 millert 628: char fname[PATHSIZE], zname[PATHSIZE], *ename;
1.1 deraadt 629:
1.6 millert 630: for (mp = &message[omsgCount]; mp < &message[msgCount]; mp++)
1.1 deraadt 631: if (mp->m_flag & MNEW)
632: break;
633: if (mp >= &message[msgCount])
1.6 millert 634: for (mp = &message[omsgCount]; mp < &message[msgCount]; mp++)
1.1 deraadt 635: if ((mp->m_flag & MREAD) == 0)
636: break;
637: if (mp < &message[msgCount])
638: mdot = mp - &message[0] + 1;
639: else
1.6 millert 640: mdot = omsgCount + 1;
1.1 deraadt 641: s = d = 0;
642: for (mp = &message[0], n = 0, u = 0; mp < &message[msgCount]; mp++) {
643: if (mp->m_flag & MNEW)
644: n++;
645: if ((mp->m_flag & MREAD) == 0)
646: u++;
647: if (mp->m_flag & MDELETED)
648: d++;
649: if (mp->m_flag & MSAVED)
650: s++;
651: }
652: ename = mailname;
1.6 millert 653: if (getfold(fname, sizeof(fname)) >= 0) {
654: strncat(fname, "/", sizeof(fname) - strlen(fname) - 1);
1.1 deraadt 655: if (strncmp(fname, mailname, strlen(fname)) == 0) {
1.6 millert 656: snprintf(zname, sizeof(zname), "+%s",
657: mailname + strlen(fname));
1.1 deraadt 658: ename = zname;
659: }
660: }
661: printf("\"%s\": ", ename);
662: if (msgCount == 1)
1.6 millert 663: fputs("1 message", stdout);
1.1 deraadt 664: else
665: printf("%d messages", msgCount);
666: if (n > 0)
667: printf(" %d new", n);
668: if (u-n > 0)
669: printf(" %d unread", u);
670: if (d > 0)
671: printf(" %d deleted", d);
672: if (s > 0)
673: printf(" %d saved", s);
674: if (readonly)
1.6 millert 675: fputs(" [Read only]", stdout);
676: putchar('\n');
1.1 deraadt 677: return(mdot);
678: }
679:
680: /*
681: * Print the current version number.
682: */
683:
684: /*ARGSUSED*/
685: int
1.3 deraadt 686: pversion(v)
687: void *v;
1.1 deraadt 688: {
689: extern char *version;
690:
691: printf("Version %s\n", version);
692: return(0);
693: }
694:
695: /*
696: * Load a file of user definitions.
697: */
698: void
699: load(name)
700: char *name;
701: {
702: register FILE *in, *oldin;
703:
704: if ((in = Fopen(name, "r")) == NULL)
705: return;
706: oldin = input;
707: input = in;
708: loading = 1;
709: sourcing = 1;
710: commands();
711: loading = 0;
712: sourcing = 0;
713: input = oldin;
1.6 millert 714: (void)Fclose(in);
1.1 deraadt 715: }