xref: /freebsd/usr.bin/mail/list.c (revision eacee0ff7ec955b32e09515246bd97b6edcd2b0f)
1 /*
2  * Copyright (c) 1980, 1993
3  *	The Regents of the University of California.  All rights reserved.
4  *
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.
32  */
33 
34 #ifndef lint
35 #if 0
36 static char sccsid[] = "@(#)list.c	8.4 (Berkeley) 5/1/95";
37 #endif
38 static const char rcsid[] =
39   "$FreeBSD$";
40 #endif /* not lint */
41 
42 #include "rcv.h"
43 #include <ctype.h>
44 #include "extern.h"
45 
46 /*
47  * Mail -- a mail program
48  *
49  * Message list handling.
50  */
51 
52 /*
53  * Convert the user string of message numbers and
54  * store the numbers into vector.
55  *
56  * Returns the count of messages picked up or -1 on error.
57  */
58 int
59 getmsglist(buf, vector, flags)
60 	char *buf;
61 	int *vector, flags;
62 {
63 	int *ip;
64 	struct message *mp;
65 
66 	if (msgCount == 0) {
67 		*vector = 0;
68 		return (0);
69 	}
70 	if (markall(buf, flags) < 0)
71 		return (-1);
72 	ip = vector;
73 	for (mp = &message[0]; mp < &message[msgCount]; mp++)
74 		if (mp->m_flag & MMARK)
75 			*ip++ = mp - &message[0] + 1;
76 	*ip = 0;
77 	return (ip - vector);
78 }
79 
80 /*
81  * Mark all messages that the user wanted from the command
82  * line in the message structure.  Return 0 on success, -1
83  * on error.
84  */
85 
86 /*
87  * Bit values for colon modifiers.
88  */
89 
90 #define	CMNEW		01		/* New messages */
91 #define	CMOLD		02		/* Old messages */
92 #define	CMUNREAD	04		/* Unread messages */
93 #define	CMDELETED	010		/* Deleted messages */
94 #define	CMREAD		020		/* Read messages */
95 
96 /*
97  * The following table describes the letters which can follow
98  * the colon and gives the corresponding modifier bit.
99  */
100 
101 struct coltab {
102 	char	co_char;		/* What to find past : */
103 	int	co_bit;			/* Associated modifier bit */
104 	int	co_mask;		/* m_status bits to mask */
105 	int	co_equal;		/* ... must equal this */
106 } coltab[] = {
107 	{ 'n',		CMNEW,		MNEW,		MNEW	},
108 	{ 'o',		CMOLD,		MNEW,		0	},
109 	{ 'u',		CMUNREAD,	MREAD,		0	},
110 	{ 'd',		CMDELETED,	MDELETED,	MDELETED},
111 	{ 'r',		CMREAD,		MREAD,		MREAD	},
112 	{ 0,		0,		0,		0	}
113 };
114 
115 static	int	lastcolmod;
116 
117 int
118 markall(buf, f)
119 	char buf[];
120 	int f;
121 {
122 	char **np;
123 	int i;
124 	struct message *mp;
125 	char *namelist[NMLSIZE], *bufp;
126 	int tok, beg, mc, star, other, valdot, colmod, colresult;
127 
128 	valdot = dot - &message[0] + 1;
129 	colmod = 0;
130 	for (i = 1; i <= msgCount; i++)
131 		unmark(i);
132 	bufp = buf;
133 	mc = 0;
134 	np = &namelist[0];
135 	scaninit();
136 	tok = scan(&bufp);
137 	star = 0;
138 	other = 0;
139 	beg = 0;
140 	while (tok != TEOL) {
141 		switch (tok) {
142 		case TNUMBER:
143 number:
144 			if (star) {
145 				printf("No numbers mixed with *\n");
146 				return (-1);
147 			}
148 			mc++;
149 			other++;
150 			if (beg != 0) {
151 				if (check(lexnumber, f))
152 					return (-1);
153 				for (i = beg; i <= lexnumber; i++)
154 					if (f == MDELETED || (message[i - 1].m_flag & MDELETED) == 0)
155 						mark(i);
156 				beg = 0;
157 				break;
158 			}
159 			beg = lexnumber;
160 			if (check(beg, f))
161 				return (-1);
162 			tok = scan(&bufp);
163 			regret(tok);
164 			if (tok != TDASH) {
165 				mark(beg);
166 				beg = 0;
167 			}
168 			break;
169 
170 		case TPLUS:
171 			if (beg != 0) {
172 				printf("Non-numeric second argument\n");
173 				return (-1);
174 			}
175 			i = valdot;
176 			do {
177 				i++;
178 				if (i > msgCount) {
179 					printf("Referencing beyond EOF\n");
180 					return (-1);
181 				}
182 			} while ((message[i - 1].m_flag & MDELETED) != f);
183 			mark(i);
184 			break;
185 
186 		case TDASH:
187 			if (beg == 0) {
188 				i = valdot;
189 				do {
190 					i--;
191 					if (i <= 0) {
192 						printf("Referencing before 1\n");
193 						return (-1);
194 					}
195 				} while ((message[i - 1].m_flag & MDELETED) != f);
196 				mark(i);
197 			}
198 			break;
199 
200 		case TSTRING:
201 			if (beg != 0) {
202 				printf("Non-numeric second argument\n");
203 				return (-1);
204 			}
205 			other++;
206 			if (lexstring[0] == ':') {
207 				colresult = evalcol(lexstring[1]);
208 				if (colresult == 0) {
209 					printf("Unknown colon modifier \"%s\"\n",
210 					    lexstring);
211 					return (-1);
212 				}
213 				colmod |= colresult;
214 			}
215 			else
216 				*np++ = savestr(lexstring);
217 			break;
218 
219 		case TDOLLAR:
220 		case TUP:
221 		case TDOT:
222 			lexnumber = metamess(lexstring[0], f);
223 			if (lexnumber == -1)
224 				return (-1);
225 			goto number;
226 
227 		case TSTAR:
228 			if (other) {
229 				printf("Can't mix \"*\" with anything\n");
230 				return (-1);
231 			}
232 			star++;
233 			break;
234 
235 		case TERROR:
236 			return (-1);
237 		}
238 		tok = scan(&bufp);
239 	}
240 	lastcolmod = colmod;
241 	*np = NULL;
242 	mc = 0;
243 	if (star) {
244 		for (i = 0; i < msgCount; i++)
245 			if ((message[i].m_flag & MDELETED) == f) {
246 				mark(i+1);
247 				mc++;
248 			}
249 		if (mc == 0) {
250 			printf("No applicable messages.\n");
251 			return (-1);
252 		}
253 		return (0);
254 	}
255 
256 	/*
257 	 * If no numbers were given, mark all of the messages,
258 	 * so that we can unmark any whose sender was not selected
259 	 * if any user names were given.
260 	 */
261 
262 	if ((np > namelist || colmod != 0) && mc == 0)
263 		for (i = 1; i <= msgCount; i++)
264 			if ((message[i-1].m_flag & MDELETED) == f)
265 				mark(i);
266 
267 	/*
268 	 * If any names were given, go through and eliminate any
269 	 * messages whose senders were not requested.
270 	 */
271 
272 	if (np > namelist) {
273 		for (i = 1; i <= msgCount; i++) {
274 			for (mc = 0, np = &namelist[0]; *np != NULL; np++)
275 				if (**np == '/') {
276 					if (matchsubj(*np, i)) {
277 						mc++;
278 						break;
279 					}
280 				}
281 				else {
282 					if (matchsender(*np, i)) {
283 						mc++;
284 						break;
285 					}
286 				}
287 			if (mc == 0)
288 				unmark(i);
289 		}
290 
291 		/*
292 		 * Make sure we got some decent messages.
293 		 */
294 
295 		mc = 0;
296 		for (i = 1; i <= msgCount; i++)
297 			if (message[i-1].m_flag & MMARK) {
298 				mc++;
299 				break;
300 			}
301 		if (mc == 0) {
302 			printf("No applicable messages from {%s",
303 				namelist[0]);
304 			for (np = &namelist[1]; *np != NULL; np++)
305 				printf(", %s", *np);
306 			printf("}\n");
307 			return (-1);
308 		}
309 	}
310 
311 	/*
312 	 * If any colon modifiers were given, go through and
313 	 * unmark any messages which do not satisfy the modifiers.
314 	 */
315 
316 	if (colmod != 0) {
317 		for (i = 1; i <= msgCount; i++) {
318 			struct coltab *colp;
319 
320 			mp = &message[i - 1];
321 			for (colp = &coltab[0]; colp->co_char != '\0'; colp++)
322 				if (colp->co_bit & colmod)
323 					if ((mp->m_flag & colp->co_mask)
324 					    != colp->co_equal)
325 						unmark(i);
326 
327 		}
328 		for (mp = &message[0]; mp < &message[msgCount]; mp++)
329 			if (mp->m_flag & MMARK)
330 				break;
331 		if (mp >= &message[msgCount]) {
332 			struct coltab *colp;
333 
334 			printf("No messages satisfy");
335 			for (colp = &coltab[0]; colp->co_char != '\0'; colp++)
336 				if (colp->co_bit & colmod)
337 					printf(" :%c", colp->co_char);
338 			printf("\n");
339 			return (-1);
340 		}
341 	}
342 	return (0);
343 }
344 
345 /*
346  * Turn the character after a colon modifier into a bit
347  * value.
348  */
349 int
350 evalcol(col)
351 	int col;
352 {
353 	struct coltab *colp;
354 
355 	if (col == 0)
356 		return (lastcolmod);
357 	for (colp = &coltab[0]; colp->co_char != '\0'; colp++)
358 		if (colp->co_char == col)
359 			return (colp->co_bit);
360 	return (0);
361 }
362 
363 /*
364  * Check the passed message number for legality and proper flags.
365  * If f is MDELETED, then either kind will do.  Otherwise, the message
366  * has to be undeleted.
367  */
368 int
369 check(mesg, f)
370 	int mesg, f;
371 {
372 	struct message *mp;
373 
374 	if (mesg < 1 || mesg > msgCount) {
375 		printf("%d: Invalid message number\n", mesg);
376 		return (-1);
377 	}
378 	mp = &message[mesg-1];
379 	if (f != MDELETED && (mp->m_flag & MDELETED) != 0) {
380 		printf("%d: Inappropriate message\n", mesg);
381 		return (-1);
382 	}
383 	return (0);
384 }
385 
386 /*
387  * Scan out the list of string arguments, shell style
388  * for a RAWLIST.
389  */
390 int
391 getrawlist(line, argv, argc)
392 	char line[];
393 	char **argv;
394 	int  argc;
395 {
396 	char c, *cp, *cp2, quotec;
397 	int argn;
398 	char *linebuf;
399 	size_t linebufsize = BUFSIZ;
400 
401 	if ((linebuf = malloc(linebufsize)) == NULL)
402 		err(1, "Out of memory");
403 
404 	argn = 0;
405 	cp = line;
406 	for (;;) {
407 		for (; *cp == ' ' || *cp == '\t'; cp++)
408 			;
409 		if (*cp == '\0')
410 			break;
411 		if (argn >= argc - 1) {
412 			printf(
413 			"Too many elements in the list; excess discarded.\n");
414 			break;
415 		}
416 		cp2 = linebuf;
417 		quotec = '\0';
418 		while ((c = *cp) != '\0') {
419 			/* Allocate more space if necessary */
420 			if (cp2 - linebuf == linebufsize - 1) {
421 				linebufsize += BUFSIZ;
422 				if ((linebuf = realloc(linebuf, linebufsize)) == NULL)
423 					err(1, "Out of memory");
424 				cp2 = linebuf + linebufsize - BUFSIZ - 1;
425 			}
426 			cp++;
427 			if (quotec != '\0') {
428 				if (c == quotec)
429 					quotec = '\0';
430 				else if (c == '\\')
431 					switch (c = *cp++) {
432 					case '\0':
433 						*cp2++ = '\\';
434 						cp--;
435 						break;
436 					case '0': case '1': case '2': case '3':
437 					case '4': case '5': case '6': case '7':
438 						c -= '0';
439 						if (*cp >= '0' && *cp <= '7')
440 							c = c * 8 + *cp++ - '0';
441 						if (*cp >= '0' && *cp <= '7')
442 							c = c * 8 + *cp++ - '0';
443 						*cp2++ = c;
444 						break;
445 					case 'b':
446 						*cp2++ = '\b';
447 						break;
448 					case 'f':
449 						*cp2++ = '\f';
450 						break;
451 					case 'n':
452 						*cp2++ = '\n';
453 						break;
454 					case 'r':
455 						*cp2++ = '\r';
456 						break;
457 					case 't':
458 						*cp2++ = '\t';
459 						break;
460 					case 'v':
461 						*cp2++ = '\v';
462 						break;
463 					default:
464 						*cp2++ = c;
465 					}
466 				else if (c == '^') {
467 					c = *cp++;
468 					if (c == '?')
469 						*cp2++ = '\177';
470 					/* null doesn't show up anyway */
471 					else if ((c >= 'A' && c <= '_') ||
472 					    (c >= 'a' && c <= 'z'))
473 						*cp2++ = c & 037;
474 					else {
475 						*cp2++ = '^';
476 						cp--;
477 					}
478 				} else
479 					*cp2++ = c;
480 			} else if (c == '"' || c == '\'')
481 				quotec = c;
482 			else if (c == ' ' || c == '\t')
483 				break;
484 			else
485 				*cp2++ = c;
486 		}
487 		*cp2 = '\0';
488 		argv[argn++] = savestr(linebuf);
489 	}
490 	argv[argn] = NULL;
491 	(void)free(linebuf);
492 	return (argn);
493 }
494 
495 /*
496  * scan out a single lexical item and return its token number,
497  * updating the string pointer passed **p.  Also, store the value
498  * of the number or string scanned in lexnumber or lexstring as
499  * appropriate.  In any event, store the scanned `thing' in lexstring.
500  */
501 
502 struct lex {
503 	char	l_char;
504 	char	l_token;
505 } singles[] = {
506 	{ '$',	TDOLLAR	},
507 	{ '.',	TDOT	},
508 	{ '^',	TUP 	},
509 	{ '*',	TSTAR 	},
510 	{ '-',	TDASH 	},
511 	{ '+',	TPLUS 	},
512 	{ '(',	TOPEN 	},
513 	{ ')',	TCLOSE 	},
514 	{ 0,	0 	}
515 };
516 
517 int
518 scan(sp)
519 	char **sp;
520 {
521 	char *cp, *cp2;
522 	int c;
523 	struct lex *lp;
524 	int quotec;
525 
526 	if (regretp >= 0) {
527 		strcpy(lexstring, string_stack[regretp]);
528 		lexnumber = numberstack[regretp];
529 		return (regretstack[regretp--]);
530 	}
531 	cp = *sp;
532 	cp2 = lexstring;
533 	c = *cp++;
534 
535 	/*
536 	 * strip away leading white space.
537 	 */
538 
539 	while (c == ' ' || c == '\t')
540 		c = *cp++;
541 
542 	/*
543 	 * If no characters remain, we are at end of line,
544 	 * so report that.
545 	 */
546 
547 	if (c == '\0') {
548 		*sp = --cp;
549 		return (TEOL);
550 	}
551 
552 	/*
553 	 * If the leading character is a digit, scan
554 	 * the number and convert it on the fly.
555 	 * Return TNUMBER when done.
556 	 */
557 
558 	if (isdigit((unsigned char)c)) {
559 		lexnumber = 0;
560 		while (isdigit((unsigned char)c)) {
561 			lexnumber = lexnumber*10 + c - '0';
562 			*cp2++ = c;
563 			c = *cp++;
564 		}
565 		*cp2 = '\0';
566 		*sp = --cp;
567 		return (TNUMBER);
568 	}
569 
570 	/*
571 	 * Check for single character tokens; return such
572 	 * if found.
573 	 */
574 
575 	for (lp = &singles[0]; lp->l_char != '\0'; lp++)
576 		if (c == lp->l_char) {
577 			lexstring[0] = c;
578 			lexstring[1] = '\0';
579 			*sp = cp;
580 			return (lp->l_token);
581 		}
582 
583 	/*
584 	 * We've got a string!  Copy all the characters
585 	 * of the string into lexstring, until we see
586 	 * a null, space, or tab.
587 	 * If the lead character is a " or ', save it
588 	 * and scan until you get another.
589 	 */
590 
591 	quotec = 0;
592 	if (c == '\'' || c == '"') {
593 		quotec = c;
594 		c = *cp++;
595 	}
596 	while (c != '\0') {
597 		if (c == quotec) {
598 			cp++;
599 			break;
600 		}
601 		if (quotec == 0 && (c == ' ' || c == '\t'))
602 			break;
603 		if (cp2 - lexstring < STRINGLEN-1)
604 			*cp2++ = c;
605 		c = *cp++;
606 	}
607 	if (quotec && c == '\0') {
608 		fprintf(stderr, "Missing %c\n", quotec);
609 		return (TERROR);
610 	}
611 	*sp = --cp;
612 	*cp2 = '\0';
613 	return (TSTRING);
614 }
615 
616 /*
617  * Unscan the named token by pushing it onto the regret stack.
618  */
619 void
620 regret(token)
621 	int token;
622 {
623 	if (++regretp >= REGDEP)
624 		errx(1, "Too many regrets");
625 	regretstack[regretp] = token;
626 	lexstring[STRINGLEN-1] = '\0';
627 	string_stack[regretp] = savestr(lexstring);
628 	numberstack[regretp] = lexnumber;
629 }
630 
631 /*
632  * Reset all the scanner global variables.
633  */
634 void
635 scaninit()
636 {
637 	regretp = -1;
638 }
639 
640 /*
641  * Find the first message whose flags & m == f  and return
642  * its message number.
643  */
644 int
645 first(f, m)
646 	int f, m;
647 {
648 	struct message *mp;
649 
650 	if (msgCount == 0)
651 		return (0);
652 	f &= MDELETED;
653 	m &= MDELETED;
654 	for (mp = dot; mp < &message[msgCount]; mp++)
655 		if ((mp->m_flag & m) == f)
656 			return (mp - message + 1);
657 	for (mp = dot-1; mp >= &message[0]; mp--)
658 		if ((mp->m_flag & m) == f)
659 			return (mp - message + 1);
660 	return (0);
661 }
662 
663 /*
664  * See if the passed name sent the passed message number.  Return true
665  * if so.
666  */
667 int
668 matchsender(str, mesg)
669 	char *str;
670 	int mesg;
671 {
672 	char *cp, *cp2, *backup;
673 
674 	if (*str == '\0')	/* null string matches nothing instead of everything */
675 		return (0);
676 	backup = cp2 = nameof(&message[mesg - 1], 0);
677 	cp = str;
678 	while (*cp2 != '\0') {
679 		if (*cp == '\0')
680 			return (1);
681 		if (toupper((unsigned char)*cp++) != toupper((unsigned char)*cp2++)) {
682 			cp2 = ++backup;
683 			cp = str;
684 		}
685 	}
686 	return (*cp == '\0');
687 }
688 
689 /*
690  * See if the passed name received the passed message number.  Return true
691  * if so.
692  */
693 
694 static char *to_fields[] = { "to", "cc", "bcc", NULL };
695 
696 int
697 matchto(str, mesg)
698 	char *str;
699 	int mesg;
700 {
701 	struct message *mp;
702 	char *cp, *cp2, *backup, **to;
703 
704 	str++;
705 
706 	/* null string matches nothing instead of everything */
707 	if (*str == '\0')
708 		return (0);
709 
710 	mp = &message[mesg - 1];
711 
712 	for (to = to_fields; *to != NULL; to++) {
713 		cp = str;
714 		cp2 = hfield(*to, mp);
715 		if (cp2 != NULL) {
716 			backup = cp2;
717 			while (*cp2 != '\0') {
718 				if (*cp == '\0')
719 					return (1);
720 				if (toupper((unsigned char)*cp++) != toupper((unsigned char)*cp2++)) {
721 					cp2 = ++backup;
722 					cp = str;
723 				}
724 			}
725 			if (*cp == '\0')
726 				return (1);
727 		}
728 	}
729 	return (0);
730 }
731 
732 /*
733  * See if the given string matches inside the subject field of the
734  * given message.  For the purpose of the scan, we ignore case differences.
735  * If it does, return true.  The string search argument is assumed to
736  * have the form "/search-string."  If it is of the form "/," we use the
737  * previous search string.
738  */
739 
740 char lastscan[STRINGLEN];
741 int
742 matchsubj(str, mesg)
743 	char *str;
744 	int mesg;
745 {
746 	struct message *mp;
747 	char *cp, *cp2, *backup;
748 
749 	str++;
750 	if (*str == '\0')
751 		str = lastscan;
752 	else
753 		strlcpy(lastscan, str, sizeof(lastscan));
754 	mp = &message[mesg-1];
755 
756 	/*
757 	 * Now look, ignoring case, for the word in the string.
758 	 */
759 
760 	if (value("searchheaders") && (cp = strchr(str, ':')) != NULL) {
761 		/* Check for special case "/To:" */
762 		if (strncasecmp(str, "To:", 3) == 0)
763 			return (matchto(cp, mesg));
764 		*cp++ = '\0';
765 		cp2 = hfield(*str != '\0' ? str : "subject", mp);
766 		cp[-1] = ':';
767 		str = cp;
768 	} else {
769 		cp = str;
770 		cp2 = hfield("subject", mp);
771 	}
772 	if (cp2 == NULL)
773 		return (0);
774 	backup = cp2;
775 	while (*cp2 != '\0') {
776 		if (*cp == '\0')
777 			return (1);
778 		if (toupper((unsigned char)*cp++) != toupper((unsigned char)*cp2++)) {
779 			cp2 = ++backup;
780 			cp = str;
781 		}
782 	}
783 	return (*cp == 0);
784 }
785 
786 /*
787  * Mark the named message by setting its mark bit.
788  */
789 void
790 mark(mesg)
791 	int mesg;
792 {
793 	int i;
794 
795 	i = mesg;
796 	if (i < 1 || i > msgCount)
797 		errx(1, "Bad message number to mark");
798 	message[i-1].m_flag |= MMARK;
799 }
800 
801 /*
802  * Unmark the named message.
803  */
804 void
805 unmark(mesg)
806 	int mesg;
807 {
808 	int i;
809 
810 	i = mesg;
811 	if (i < 1 || i > msgCount)
812 		errx(1, "Bad message number to unmark");
813 	message[i-1].m_flag &= ~MMARK;
814 }
815 
816 /*
817  * Return the message number corresponding to the passed meta character.
818  */
819 int
820 metamess(meta, f)
821 	int meta, f;
822 {
823 	int c, m;
824 	struct message *mp;
825 
826 	c = meta;
827 	switch (c) {
828 	case '^':
829 		/*
830 		 * First 'good' message left.
831 		 */
832 		for (mp = &message[0]; mp < &message[msgCount]; mp++)
833 			if ((mp->m_flag & MDELETED) == f)
834 				return (mp - &message[0] + 1);
835 		printf("No applicable messages\n");
836 		return (-1);
837 
838 	case '$':
839 		/*
840 		 * Last 'good message left.
841 		 */
842 		for (mp = &message[msgCount-1]; mp >= &message[0]; mp--)
843 			if ((mp->m_flag & MDELETED) == f)
844 				return (mp - &message[0] + 1);
845 		printf("No applicable messages\n");
846 		return (-1);
847 
848 	case '.':
849 		/*
850 		 * Current message.
851 		 */
852 		m = dot - &message[0] + 1;
853 		if ((dot->m_flag & MDELETED) != f) {
854 			printf("%d: Inappropriate message\n", m);
855 			return (-1);
856 		}
857 		return (m);
858 
859 	default:
860 		printf("Unknown metachar (%c)\n", c);
861 		return (-1);
862 	}
863 }
864