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