Annotation of src/usr.bin/mail/lex.c, Revision 1.14
1.14 ! millert 1: /* $OpenBSD: lex.c,v 1.13 1997/07/30 06:32:40 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.14 ! millert 41: static char rcsid[] = "$OpenBSD: lex.c,v 1.13 1997/07/30 06:32:40 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;
1.11 millert 69: int i, fd;
1.1 deraadt 70: struct stat stb;
71: char isedit = *name != '%';
72: char *who = name[1] ? name + 1 : myname;
1.12 millert 73: char tempname[PATHSIZE];
1.1 deraadt 74: static int shudclob;
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);
1.14 ! millert 137: if (name != mailname) {
! 138: strncpy(mailname, name, sizeof(mailname) - 1);
! 139: mailname[sizeof(mailname) - 1] = '\0';
! 140: }
1.1 deraadt 141: mailsize = fsize(ibuf);
1.11 millert 142: (void)snprintf(tempname, sizeof(tempname),
143: "%s/mail.RxXXXXXXXXXX", tmpdir);
144: if ((fd = mkstemp(tempname)) == -1 ||
145: (otf = fdopen(fd, "w")) == NULL)
146: err(1, tempname);
1.7 millert 147: (void)fcntl(fileno(otf), F_SETFD, 1);
1.11 millert 148: if ((itf = fopen(tempname, "r")) == NULL)
149: err(1, tempname);
1.7 millert 150: (void)fcntl(fileno(itf), F_SETFD, 1);
1.12 millert 151: (void)rm(tempname);
1.6 millert 152: setptr(ibuf, 0);
1.1 deraadt 153: setmsize(msgCount);
1.6 millert 154: /*
155: * New mail may have arrived while we were reading
156: * the mail file, so reset mailsize to be where
157: * we really are in the file...
158: */
159: mailsize = ftell(ibuf);
160: (void)Fclose(ibuf);
1.1 deraadt 161: relsesigs();
162: sawcom = 0;
163: if (!edit && msgCount == 0) {
164: nomail:
165: fprintf(stderr, "No mail for %s\n", who);
1.6 millert 166: return(-1);
1.1 deraadt 167: }
168: return(0);
169: }
170:
1.6 millert 171: /*
172: * Incorporate any new mail that has arrived since we first
173: * started reading mail.
174: */
175: int
176: incfile()
177: {
178: int newsize;
179: int omsgCount = msgCount;
180: FILE *ibuf;
181:
182: ibuf = Fopen(mailname, "r");
183: if (ibuf == NULL)
184: return(-1);
185: holdsigs();
1.10 millert 186: if (!spool_lock())
187: return(-1);
1.6 millert 188: newsize = fsize(ibuf);
1.9 millert 189: /* make sure mail box has grown and is non-empty */
190: if (newsize == 0 || newsize <= mailsize) {
1.10 millert 191: spool_unlock();
1.9 millert 192: relsesigs();
193: return(newsize == mailsize ? 0 : -1);
194: }
1.6 millert 195: setptr(ibuf, mailsize);
196: setmsize(msgCount);
197: mailsize = ftell(ibuf);
198: (void)Fclose(ibuf);
1.10 millert 199: spool_unlock();
1.6 millert 200: relsesigs();
201: return(msgCount - omsgCount);
202: }
203:
204:
1.1 deraadt 205: int *msgvec;
206: int reset_on_stop; /* do a reset() if stopped */
207:
208: /*
209: * Interpret user commands one by one. If standard input is not a tty,
210: * print no prompt.
211: */
212: void
213: commands()
214: {
215: int eofloop = 0;
216: register int n;
217: char linebuf[LINESIZE];
1.3 deraadt 218: #if __GNUC__
1.8 millert 219: /* Avoid siglongjmp clobbering */
1.7 millert 220: (void)&eofloop;
1.3 deraadt 221: #endif
1.1 deraadt 222:
223: if (!sourcing) {
224: if (signal(SIGINT, SIG_IGN) != SIG_IGN)
1.13 millert 225: (void)signal(SIGINT, intr);
1.1 deraadt 226: if (signal(SIGHUP, SIG_IGN) != SIG_IGN)
1.13 millert 227: (void)signal(SIGHUP, hangup);
228: (void)signal(SIGTSTP, stop);
229: (void)signal(SIGTTOU, stop);
230: (void)signal(SIGTTIN, stop);
1.1 deraadt 231: }
232: setexit();
233: for (;;) {
234: /*
235: * Print the prompt, if needed. Clear out
236: * string space, and flush the output.
237: */
1.8 millert 238: if (!sourcing && value("interactive") != NULL) {
239: if ((value("autoinc") != NULL) && (incfile() > 0))
1.6 millert 240: puts("New mail has arrived.");
1.1 deraadt 241: reset_on_stop = 1;
242: printf(prompt);
243: }
244: fflush(stdout);
245: sreset();
246: /*
247: * Read a line of commands from the current input
248: * and handle end of file specially.
249: */
250: n = 0;
251: for (;;) {
252: if (readline(input, &linebuf[n], LINESIZE - n) < 0) {
253: if (n == 0)
254: n = -1;
255: break;
256: }
257: if ((n = strlen(linebuf)) == 0)
258: break;
259: n--;
260: if (linebuf[n] != '\\')
261: break;
262: linebuf[n++] = ' ';
263: }
264: reset_on_stop = 0;
265: if (n < 0) {
266: /* eof */
267: if (loading)
268: break;
269: if (sourcing) {
270: unstack();
271: continue;
272: }
1.8 millert 273: if (value("interactive") != NULL &&
274: value("ignoreeof") != NULL &&
1.1 deraadt 275: ++eofloop < 25) {
1.6 millert 276: puts("Use \"quit\" to quit.");
1.1 deraadt 277: continue;
278: }
279: break;
280: }
281: eofloop = 0;
282: if (execute(linebuf, 0))
283: break;
284: }
285: }
286:
287: /*
288: * Execute a single command.
289: * Command functions return 0 for success, 1 for error, and -1
290: * for abort. A 1 or -1 aborts a load or source. A -1 aborts
291: * the interactive command loop.
292: * Contxt is non-zero if called while composing mail.
293: */
294: int
295: execute(linebuf, contxt)
296: char linebuf[];
297: int contxt;
298: {
299: char word[LINESIZE];
300: char *arglist[MAXARGC];
1.3 deraadt 301: const struct cmd *com = NULL;
1.1 deraadt 302: register char *cp, *cp2;
303: register int c;
304: int muvec[2];
305: int e = 1;
306:
307: /*
308: * Strip the white space away from the beginning
309: * of the command, then scan out a word, which
310: * consists of anything except digits and white space.
311: *
312: * Handle ! escapes differently to get the correct
313: * lexical conventions.
314: */
315:
316: for (cp = linebuf; isspace(*cp); cp++)
317: ;
318: if (*cp == '!') {
319: if (sourcing) {
1.6 millert 320: puts("Can't \"!\" while sourcing");
1.1 deraadt 321: goto out;
322: }
323: shell(cp+1);
324: return(0);
325: }
326: cp2 = word;
1.8 millert 327: while (*cp && strchr(" \t0123456789$^.:/-+*'\"", *cp) == NULL)
1.1 deraadt 328: *cp2++ = *cp++;
329: *cp2 = '\0';
330:
331: /*
332: * Look up the command; if not found, bitch.
333: * Normally, a blank command would map to the
334: * first command in the table; while sourcing,
335: * however, we ignore blank lines to eliminate
336: * confusion.
337: */
338:
339: if (sourcing && *word == '\0')
340: return(0);
341: com = lex(word);
342: if (com == NONE) {
343: printf("Unknown command: \"%s\"\n", word);
344: goto out;
345: }
346:
347: /*
348: * See if we should execute the command -- if a conditional
349: * we always execute it, otherwise, check the state of cond.
350: */
351:
352: if ((com->c_argtype & F) == 0)
1.3 deraadt 353: if ((cond == CRCV && !rcvmode) || (cond == CSEND && rcvmode))
1.1 deraadt 354: return(0);
355:
356: /*
357: * Process the arguments to the command, depending
358: * on the type he expects. Default to an error.
359: * If we are sourcing an interactive command, it's
360: * an error.
361: */
362:
363: if (!rcvmode && (com->c_argtype & M) == 0) {
364: printf("May not execute \"%s\" while sending\n",
365: com->c_name);
366: goto out;
367: }
368: if (sourcing && com->c_argtype & I) {
369: printf("May not execute \"%s\" while sourcing\n",
370: com->c_name);
371: goto out;
372: }
373: if (readonly && com->c_argtype & W) {
374: printf("May not execute \"%s\" -- message file is read only\n",
375: com->c_name);
376: goto out;
377: }
378: if (contxt && com->c_argtype & R) {
379: printf("Cannot recursively invoke \"%s\"\n", com->c_name);
380: goto out;
381: }
382: switch (com->c_argtype & ~(F|P|I|M|T|W|R)) {
383: case MSGLIST:
384: /*
385: * A message list defaulting to nearest forward
386: * legal message.
387: */
388: if (msgvec == 0) {
1.6 millert 389: puts("Illegal use of \"message list\"");
1.1 deraadt 390: break;
391: }
392: if ((c = getmsglist(cp, msgvec, com->c_msgflag)) < 0)
393: break;
394: if (c == 0) {
395: *msgvec = first(com->c_msgflag,
396: com->c_msgmask);
397: msgvec[1] = NULL;
398: }
399: if (*msgvec == NULL) {
1.6 millert 400: puts("No applicable messages");
1.1 deraadt 401: break;
402: }
403: e = (*com->c_func)(msgvec);
404: break;
405:
406: case NDMLIST:
407: /*
408: * A message list with no defaults, but no error
409: * if none exist.
410: */
411: if (msgvec == 0) {
1.6 millert 412: puts("Illegal use of \"message list\"");
1.1 deraadt 413: break;
414: }
415: if (getmsglist(cp, msgvec, com->c_msgflag) < 0)
416: break;
417: e = (*com->c_func)(msgvec);
418: break;
419:
420: case STRLIST:
421: /*
422: * Just the straight string, with
423: * leading blanks removed.
424: */
425: while (isspace(*cp))
426: cp++;
427: e = (*com->c_func)(cp);
428: break;
429:
430: case RAWLIST:
431: /*
432: * A vector of strings, in shell style.
433: */
434: if ((c = getrawlist(cp, arglist,
1.6 millert 435: sizeof(arglist) / sizeof(*arglist))) < 0)
1.1 deraadt 436: break;
437: if (c < com->c_minargs) {
438: printf("%s requires at least %d arg(s)\n",
439: com->c_name, com->c_minargs);
440: break;
441: }
442: if (c > com->c_maxargs) {
443: printf("%s takes no more than %d arg(s)\n",
444: com->c_name, com->c_maxargs);
445: break;
446: }
447: e = (*com->c_func)(arglist);
448: break;
449:
450: case NOLIST:
451: /*
452: * Just the constant zero, for exiting,
453: * eg.
454: */
455: e = (*com->c_func)(0);
456: break;
457:
458: default:
459: panic("Unknown argtype");
460: }
461:
462: out:
463: /*
464: * Exit the current source file on
465: * error.
466: */
467: if (e) {
468: if (e < 0)
1.6 millert 469: return(1);
1.1 deraadt 470: if (loading)
1.6 millert 471: return(1);
1.1 deraadt 472: if (sourcing)
473: unstack();
1.6 millert 474: return(0);
1.1 deraadt 475: }
1.3 deraadt 476: if (com == NULL)
477: return(0);
1.8 millert 478: if (value("autoprint") != NULL && com->c_argtype & P)
1.1 deraadt 479: if ((dot->m_flag & MDELETED) == 0) {
480: muvec[0] = dot - &message[0] + 1;
481: muvec[1] = 0;
482: type(muvec);
483: }
484: if (!sourcing && (com->c_argtype & T) == 0)
485: sawcom = 1;
486: return(0);
487: }
488:
489: /*
490: * Set the size of the message vector used to construct argument
491: * lists to message list functions.
492: */
493: void
494: setmsize(sz)
495: int sz;
496: {
497:
498: if (msgvec != 0)
1.7 millert 499: (void)free(msgvec);
500: msgvec = (int *)calloc(sz + 1, sizeof(*msgvec));
1.1 deraadt 501: }
502:
503: /*
504: * Find the correct command in the command table corresponding
505: * to the passed command "word"
506: */
507:
1.2 niklas 508: const struct cmd *
1.1 deraadt 509: lex(word)
510: char word[];
511: {
1.2 niklas 512: extern const struct cmd cmdtab[];
513: register const struct cmd *cp;
1.1 deraadt 514:
1.8 millert 515: for (cp = &cmdtab[0]; cp->c_name != NULL; cp++)
1.1 deraadt 516: if (isprefix(word, cp->c_name))
517: return(cp);
518: return(NONE);
519: }
520:
521: /*
522: * Determine if as1 is a valid prefix of as2.
523: * Return true if yep.
524: */
525: int
526: isprefix(as1, as2)
527: char *as1, *as2;
528: {
529: register char *s1, *s2;
530:
531: s1 = as1;
532: s2 = as2;
533: while (*s1++ == *s2)
534: if (*s2++ == '\0')
535: return(1);
536: return(*--s1 == '\0');
537: }
538:
539: /*
540: * The following gets called on receipt of an interrupt. This is
541: * to abort printout of a command, mainly.
542: * Dispatching here when command() is inactive crashes rcv.
543: * Close all open files except 0, 1, 2, and the temporary.
544: * Also, unstack all source files.
545: */
546:
547: int inithdr; /* am printing startup headers */
548:
549: /*ARGSUSED*/
550: void
551: intr(s)
552: int s;
553: {
554:
555: noreset = 0;
556: if (!inithdr)
557: sawcom++;
558: inithdr = 0;
559: while (sourcing)
560: unstack();
561:
562: close_all_files();
563:
564: if (image >= 0) {
1.6 millert 565: (void)close(image);
1.1 deraadt 566: image = -1;
567: }
1.6 millert 568: fputs("Interrupt\n", stderr);
1.1 deraadt 569: reset(0);
570: }
571:
572: /*
573: * When we wake up after ^Z, reprint the prompt.
574: */
575: void
576: stop(s)
577: int s;
578: {
579: sig_t old_action = signal(s, SIG_DFL);
1.3 deraadt 580: sigset_t nset;
1.1 deraadt 581:
1.13 millert 582: (void)sigemptyset(&nset);
583: (void)sigaddset(&nset, s);
584: (void)sigprocmask(SIG_UNBLOCK, &nset, NULL);
585: (void)kill(0, s);
586: (void)sigprocmask(SIG_BLOCK, &nset, NULL);
587: (void)signal(s, old_action);
1.1 deraadt 588: if (reset_on_stop) {
589: reset_on_stop = 0;
590: reset(0);
591: }
592: }
593:
594: /*
595: * Branch here on hangup signal and simulate "exit".
596: */
597: /*ARGSUSED*/
598: void
599: hangup(s)
600: int s;
601: {
602:
603: /* nothing to do? */
604: exit(1);
605: }
606:
607: /*
608: * Announce the presence of the current Mail version,
609: * give the message count, and print a header listing.
610: */
611: void
612: announce()
613: {
614: int vec[2], mdot;
615:
1.6 millert 616: mdot = newfileinfo(0);
1.1 deraadt 617: vec[0] = mdot;
618: vec[1] = 0;
619: dot = &message[mdot - 1];
1.8 millert 620: if (msgCount > 0 && value("noheader") == NULL) {
1.1 deraadt 621: inithdr++;
622: headers(vec);
623: inithdr = 0;
624: }
625: }
626:
627: /*
628: * Announce information about the file we are editing.
629: * Return a likely place to set dot.
630: */
631: int
1.6 millert 632: newfileinfo(omsgCount)
633: int omsgCount;
1.1 deraadt 634: {
635: register struct message *mp;
636: register int u, n, mdot, d, s;
1.6 millert 637: char fname[PATHSIZE], zname[PATHSIZE], *ename;
1.1 deraadt 638:
1.6 millert 639: for (mp = &message[omsgCount]; mp < &message[msgCount]; mp++)
1.1 deraadt 640: if (mp->m_flag & MNEW)
641: break;
642: if (mp >= &message[msgCount])
1.6 millert 643: for (mp = &message[omsgCount]; mp < &message[msgCount]; mp++)
1.1 deraadt 644: if ((mp->m_flag & MREAD) == 0)
645: break;
646: if (mp < &message[msgCount])
647: mdot = mp - &message[0] + 1;
648: else
1.6 millert 649: mdot = omsgCount + 1;
1.1 deraadt 650: s = d = 0;
651: for (mp = &message[0], n = 0, u = 0; mp < &message[msgCount]; mp++) {
652: if (mp->m_flag & MNEW)
653: n++;
654: if ((mp->m_flag & MREAD) == 0)
655: u++;
656: if (mp->m_flag & MDELETED)
657: d++;
658: if (mp->m_flag & MSAVED)
659: s++;
660: }
661: ename = mailname;
1.6 millert 662: if (getfold(fname, sizeof(fname)) >= 0) {
663: strncat(fname, "/", sizeof(fname) - strlen(fname) - 1);
1.1 deraadt 664: if (strncmp(fname, mailname, strlen(fname)) == 0) {
1.12 millert 665: (void)snprintf(zname, sizeof(zname), "+%s",
1.6 millert 666: mailname + strlen(fname));
1.1 deraadt 667: ename = zname;
668: }
669: }
670: printf("\"%s\": ", ename);
671: if (msgCount == 1)
1.6 millert 672: fputs("1 message", stdout);
1.1 deraadt 673: else
674: printf("%d messages", msgCount);
675: if (n > 0)
676: printf(" %d new", n);
677: if (u-n > 0)
678: printf(" %d unread", u);
679: if (d > 0)
680: printf(" %d deleted", d);
681: if (s > 0)
682: printf(" %d saved", s);
683: if (readonly)
1.6 millert 684: fputs(" [Read only]", stdout);
685: putchar('\n');
1.1 deraadt 686: return(mdot);
687: }
688:
689: /*
690: * Print the current version number.
691: */
692:
693: /*ARGSUSED*/
694: int
1.3 deraadt 695: pversion(v)
696: void *v;
1.1 deraadt 697: {
698: extern char *version;
699:
700: printf("Version %s\n", version);
701: return(0);
702: }
703:
704: /*
705: * Load a file of user definitions.
706: */
707: void
708: load(name)
709: char *name;
710: {
711: register FILE *in, *oldin;
712:
713: if ((in = Fopen(name, "r")) == NULL)
714: return;
715: oldin = input;
716: input = in;
717: loading = 1;
718: sourcing = 1;
719: commands();
720: loading = 0;
721: sourcing = 0;
722: input = oldin;
1.6 millert 723: (void)Fclose(in);
1.1 deraadt 724: }