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