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