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