Home | History | Annotate | Line # | Download | only in mail
list.c revision 1.12
      1 /*	$NetBSD: list.c,v 1.12 2002/03/04 03:07:26 wiz Exp $	*/
      2 
      3 /*
      4  * Copyright (c) 1980, 1993
      5  *	The Regents of the University of California.  All rights reserved.
      6  *
      7  * Redistribution and use in source and binary forms, with or without
      8  * modification, are permitted provided that the following conditions
      9  * are met:
     10  * 1. Redistributions of source code must retain the above copyright
     11  *    notice, this list of conditions and the following disclaimer.
     12  * 2. Redistributions in binary form must reproduce the above copyright
     13  *    notice, this list of conditions and the following disclaimer in the
     14  *    documentation and/or other materials provided with the distribution.
     15  * 3. All advertising materials mentioning features or use of this software
     16  *    must display the following acknowledgement:
     17  *	This product includes software developed by the University of
     18  *	California, Berkeley and its contributors.
     19  * 4. Neither the name of the University nor the names of its contributors
     20  *    may be used to endorse or promote products derived from this software
     21  *    without specific prior written permission.
     22  *
     23  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     24  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     25  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     26  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     27  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     28  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     29  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     30  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     31  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     32  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     33  * SUCH DAMAGE.
     34  */
     35 
     36 #include <sys/cdefs.h>
     37 #ifndef lint
     38 #if 0
     39 static char sccsid[] = "@(#)list.c	8.4 (Berkeley) 5/1/95";
     40 #else
     41 __RCSID("$NetBSD: list.c,v 1.12 2002/03/04 03:07:26 wiz Exp $");
     42 #endif
     43 #endif /* not lint */
     44 
     45 #include "rcv.h"
     46 #include "extern.h"
     47 
     48 int	matchto(char *, int);
     49 
     50 /*
     51  * Mail -- a mail program
     52  *
     53  * Message list handling.
     54  */
     55 
     56 /*
     57  * Convert the user string of message numbers and
     58  * store the numbers into vector.
     59  *
     60  * Returns the count of messages picked up or -1 on error.
     61  */
     62 int
     63 getmsglist(char *buf, int *vector, int flags)
     64 {
     65 	int *ip;
     66 	struct message *mp;
     67 
     68 	if (msgCount == 0) {
     69 		*vector = 0;
     70 		return 0;
     71 	}
     72 	if (markall(buf, flags) < 0)
     73 		return(-1);
     74 	ip = vector;
     75 	for (mp = &message[0]; mp < &message[msgCount]; mp++)
     76 		if (mp->m_flag & MMARK)
     77 			*ip++ = mp - &message[0] + 1;
     78 	*ip = 0;
     79 	return(ip - vector);
     80 }
     81 
     82 /*
     83  * Mark all messages that the user wanted from the command
     84  * line in the message structure.  Return 0 on success, -1
     85  * on error.
     86  */
     87 
     88 /*
     89  * Bit values for colon modifiers.
     90  */
     91 
     92 #define	CMNEW		01		/* New messages */
     93 #define	CMOLD		02		/* Old messages */
     94 #define	CMUNREAD	04		/* Unread messages */
     95 #define	CMDELETED	010		/* Deleted messages */
     96 #define	CMREAD		020		/* Read messages */
     97 
     98 /*
     99  * The following table describes the letters which can follow
    100  * the colon and gives the corresponding modifier bit.
    101  */
    102 
    103 struct coltab {
    104 	char	co_char;		/* What to find past : */
    105 	int	co_bit;			/* Associated modifier bit */
    106 	int	co_mask;		/* m_status bits to mask */
    107 	int	co_equal;		/* ... must equal this */
    108 } coltab[] = {
    109 	{ 'n',		CMNEW,		MNEW,		MNEW },
    110 	{ 'o',		CMOLD,		MNEW,		0 },
    111 	{ 'u',		CMUNREAD,	MREAD,		0 },
    112 	{ 'd',		CMDELETED,	MDELETED,	MDELETED },
    113 	{ 'r',		CMREAD,		MREAD,		MREAD },
    114 	{ 0,		0,		0,		0 }
    115 };
    116 
    117 static	int	lastcolmod;
    118 
    119 int
    120 markall(char buf[], 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; 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; 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(int col)
    351 {
    352 	struct coltab *colp;
    353 
    354 	if (col == 0)
    355 		return(lastcolmod);
    356 	for (colp = &coltab[0]; colp->co_char; colp++)
    357 		if (colp->co_char == col)
    358 			return(colp->co_bit);
    359 	return(0);
    360 }
    361 
    362 /*
    363  * Check the passed message number for legality and proper flags.
    364  * If f is MDELETED, then either kind will do.  Otherwise, the message
    365  * has to be undeleted.
    366  */
    367 int
    368 check(int mesg, int f)
    369 {
    370 	struct message *mp;
    371 
    372 	if (mesg < 1 || mesg > msgCount) {
    373 		printf("%d: Invalid message number\n", mesg);
    374 		return(-1);
    375 	}
    376 	mp = &message[mesg-1];
    377 	if (f != MDELETED && (mp->m_flag & MDELETED) != 0) {
    378 		printf("%d: Inappropriate message\n", mesg);
    379 		return(-1);
    380 	}
    381 	return(0);
    382 }
    383 
    384 /*
    385  * Scan out the list of string arguments, shell style
    386  * for a RAWLIST.
    387  */
    388 int
    389 getrawlist(char line[], char **argv, int argc)
    390 {
    391 	char c, *cp, *cp2, quotec;
    392 	int argn;
    393 	char linebuf[BUFSIZ];
    394 
    395 	argn = 0;
    396 	cp = line;
    397 	for (;;) {
    398 		for (; *cp == ' ' || *cp == '\t'; cp++)
    399 			;
    400 		if (*cp == '\0')
    401 			break;
    402 		if (argn >= argc - 1) {
    403 			printf(
    404 			"Too many elements in the list; excess discarded.\n");
    405 			break;
    406 		}
    407 		cp2 = linebuf;
    408 		quotec = '\0';
    409 		while ((c = *cp) != '\0') {
    410 			cp++;
    411 			if (quotec != '\0') {
    412 				if (c == quotec)
    413 					quotec = '\0';
    414 				else if (c == '\\')
    415 					switch (c = *cp++) {
    416 					case '\0':
    417 						*cp2++ = '\\';
    418 						cp--;
    419 						break;
    420 					case '0': case '1': case '2': case '3':
    421 					case '4': case '5': case '6': case '7':
    422 						c -= '0';
    423 						if (*cp >= '0' && *cp <= '7')
    424 							c = c * 8 + *cp++ - '0';
    425 						if (*cp >= '0' && *cp <= '7')
    426 							c = c * 8 + *cp++ - '0';
    427 						*cp2++ = c;
    428 						break;
    429 					case 'b':
    430 						*cp2++ = '\b';
    431 						break;
    432 					case 'f':
    433 						*cp2++ = '\f';
    434 						break;
    435 					case 'n':
    436 						*cp2++ = '\n';
    437 						break;
    438 					case 'r':
    439 						*cp2++ = '\r';
    440 						break;
    441 					case 't':
    442 						*cp2++ = '\t';
    443 						break;
    444 					case 'v':
    445 						*cp2++ = '\v';
    446 						break;
    447 					default:
    448 						*cp2++ = c;
    449 					}
    450 				else if (c == '^') {
    451 					c = *cp++;
    452 					if (c == '?')
    453 						*cp2++ = '\177';
    454 					/* null doesn't show up anyway */
    455 					else if ((c >= 'A' && c <= '_') ||
    456 						 (c >= 'a' && c <= 'z'))
    457 						*cp2++ = c & 037;
    458 					else {
    459 						*cp2++ = '^';
    460 						cp--;
    461 					}
    462 				} else
    463 					*cp2++ = c;
    464 			} else if (c == '"' || c == '\'')
    465 				quotec = c;
    466 			else if (c == ' ' || c == '\t')
    467 				break;
    468 			else
    469 				*cp2++ = c;
    470 		}
    471 		*cp2 = '\0';
    472 		argv[argn++] = savestr(linebuf);
    473 	}
    474 	argv[argn] = NULL;
    475 	return argn;
    476 }
    477 
    478 /*
    479  * scan out a single lexical item and return its token number,
    480  * updating the string pointer passed **p.  Also, store the value
    481  * of the number or string scanned in lexnumber or lexstring as
    482  * appropriate.  In any event, store the scanned `thing' in lexstring.
    483  */
    484 
    485 struct lex {
    486 	char	l_char;
    487 	char	l_token;
    488 } singles[] = {
    489 	{ '$',	TDOLLAR },
    490 	{ '.',	TDOT },
    491 	{ '^',	TUP },
    492 	{ '*',	TSTAR },
    493 	{ '-',	TDASH },
    494 	{ '+',	TPLUS },
    495 	{ '(',	TOPEN },
    496 	{ ')',	TCLOSE },
    497 	{ 0,	0 }
    498 };
    499 
    500 int
    501 scan(char **sp)
    502 {
    503 	char *cp, *cp2;
    504 	int c;
    505 	struct lex *lp;
    506 	int quotec;
    507 
    508 	if (regretp >= 0) {
    509 		strcpy(lexstring, string_stack[regretp]);
    510 		lexnumber = numberstack[regretp];
    511 		return(regretstack[regretp--]);
    512 	}
    513 	cp = *sp;
    514 	cp2 = lexstring;
    515 	c = *cp++;
    516 
    517 	/*
    518 	 * strip away leading white space.
    519 	 */
    520 
    521 	while (c == ' ' || c == '\t')
    522 		c = *cp++;
    523 
    524 	/*
    525 	 * If no characters remain, we are at end of line,
    526 	 * so report that.
    527 	 */
    528 
    529 	if (c == '\0') {
    530 		*sp = --cp;
    531 		return(TEOL);
    532 	}
    533 
    534 	/*
    535 	 * If the leading character is a digit, scan
    536 	 * the number and convert it on the fly.
    537 	 * Return TNUMBER when done.
    538 	 */
    539 
    540 	if (isdigit(c)) {
    541 		lexnumber = 0;
    542 		while (isdigit(c)) {
    543 			lexnumber = lexnumber*10 + c - '0';
    544 			*cp2++ = c;
    545 			c = *cp++;
    546 		}
    547 		*cp2 = '\0';
    548 		*sp = --cp;
    549 		return(TNUMBER);
    550 	}
    551 
    552 	/*
    553 	 * Check for single character tokens; return such
    554 	 * if found.
    555 	 */
    556 
    557 	for (lp = &singles[0]; lp->l_char != 0; lp++)
    558 		if (c == lp->l_char) {
    559 			lexstring[0] = c;
    560 			lexstring[1] = '\0';
    561 			*sp = cp;
    562 			return(lp->l_token);
    563 		}
    564 
    565 	/*
    566 	 * We've got a string!  Copy all the characters
    567 	 * of the string into lexstring, until we see
    568 	 * a null, space, or tab.
    569 	 * If the lead character is a " or ', save it
    570 	 * and scan until you get another.
    571 	 */
    572 
    573 	quotec = 0;
    574 	if (c == '\'' || c == '"') {
    575 		quotec = c;
    576 		c = *cp++;
    577 	}
    578 	while (c != '\0') {
    579 		if (c == quotec) {
    580 			cp++;
    581 			break;
    582 		}
    583 		if (quotec == 0 && (c == ' ' || c == '\t'))
    584 			break;
    585 		if (cp2 - lexstring < STRINGLEN-1)
    586 			*cp2++ = c;
    587 		c = *cp++;
    588 	}
    589 	if (quotec && c == 0) {
    590 		fprintf(stderr, "Missing %c\n", quotec);
    591 		return TERROR;
    592 	}
    593 	*sp = --cp;
    594 	*cp2 = '\0';
    595 	return(TSTRING);
    596 }
    597 
    598 /*
    599  * Unscan the named token by pushing it onto the regret stack.
    600  */
    601 void
    602 regret(int token)
    603 {
    604 	if (++regretp >= REGDEP)
    605 		errx(1, "Too many regrets");
    606 	regretstack[regretp] = token;
    607 	lexstring[STRINGLEN-1] = '\0';
    608 	string_stack[regretp] = savestr(lexstring);
    609 	numberstack[regretp] = lexnumber;
    610 }
    611 
    612 /*
    613  * Reset all the scanner global variables.
    614  */
    615 void
    616 scaninit(void)
    617 {
    618 	regretp = -1;
    619 }
    620 
    621 /*
    622  * Find the first message whose flags & m == f  and return
    623  * its message number.
    624  */
    625 int
    626 first(int f, int m)
    627 {
    628 	struct message *mp;
    629 
    630 	if (msgCount == 0)
    631 		return 0;
    632 	f &= MDELETED;
    633 	m &= MDELETED;
    634 	for (mp = dot; mp < &message[msgCount]; mp++)
    635 		if ((mp->m_flag & m) == f)
    636 			return mp - message + 1;
    637 	for (mp = dot-1; mp >= &message[0]; mp--)
    638 		if ((mp->m_flag & m) == f)
    639 			return mp - message + 1;
    640 	return 0;
    641 }
    642 
    643 /*
    644  * See if the passed name sent the passed message number.  Return true
    645  * if so.
    646  */
    647 int
    648 matchsender(char *str, int mesg)
    649 {
    650 	char *cp, *cp2, *backup;
    651 
    652 	if (!*str)	/* null string matches nothing instead of everything */
    653 		return 0;
    654 	backup = cp2 = nameof(&message[mesg - 1], 0);
    655 	cp = str;
    656 	while (*cp2) {
    657 		if (*cp == 0)
    658 			return(1);
    659 		if (upcase(*cp++) != upcase(*cp2++)) {
    660 			cp2 = ++backup;
    661 			cp = str;
    662 		}
    663 	}
    664 	return(*cp == 0);
    665 }
    666 
    667 /*
    668  * See if the passed name received the passed message number.  Return true
    669  * if so.
    670  */
    671 
    672 static char *to_fields[] = { "to", "cc", "bcc", 0 };
    673 
    674 int
    675 matchto(char *str, int mesg)
    676 {
    677 	struct message *mp;
    678 	char *cp, *cp2, *backup, **to;
    679 
    680 	str++;
    681 
    682 	if (*str == 0)	/* null string matches nothing instead of everything */
    683 		return(0);
    684 
    685 	mp = &message[mesg-1];
    686 
    687 	for (to = to_fields; *to; to++) {
    688 		cp = str;
    689 		cp2 = hfield(*to, mp);
    690 		if (cp2 != NULL) {
    691 			backup = cp2;
    692 			while (*cp2) {
    693 				if (*cp == 0)
    694 					return(1);
    695 				if (upcase(*cp++) != upcase(*cp2++)) {
    696 					cp2 = ++backup;
    697 					cp = str;
    698 				}
    699 			}
    700 			if (*cp == 0)
    701 				return(1);
    702 		}
    703 	}
    704 	return(0);
    705 }
    706 
    707 /*
    708  * See if the given string matches inside the subject field of the
    709  * given message.  For the purpose of the scan, we ignore case differences.
    710  * If it does, return true.  The string search argument is assumed to
    711  * have the form "/search-string."  If it is of the form "/," we use the
    712  * previous search string.
    713  */
    714 
    715 char lastscan[STRINGLEN];
    716 int
    717 matchsubj(char *str, int mesg)
    718 {
    719 	struct message *mp;
    720 	char *cp, *cp2, *backup;
    721 
    722 	str++;
    723 	if (*str == '\0')
    724 		str = lastscan;
    725 	else {
    726 		strncpy(lastscan, str, STRINGLEN - 1);
    727 		lastscan[STRINGLEN - 1] = '\0' ;
    728 	}
    729 	mp = &message[mesg-1];
    730 
    731 	/*
    732 	 * Now look, ignoring case, for the word in the string.
    733 	 */
    734 
    735 	if (value("searchheaders") && (cp = strchr(str, ':'))) {
    736 		/* Check for special case "/To:" */
    737 		if (upcase(str[0]) == 'T' && upcase(str[1]) == 'O' &&
    738 		    str[2] == ':')
    739 			return(matchto(cp, mesg));
    740 		*cp++ = '\0';
    741 		cp2 = hfield(*str ? str : "subject", mp);
    742 		cp[-1] = ':';
    743 		str = cp;
    744 	} else {
    745 		cp = str;
    746 		cp2 = hfield("subject", mp);
    747 	}
    748 	if (cp2 == NULL)
    749 		return(0);
    750 	backup = cp2;
    751 	while (*cp2) {
    752 		if (*cp == 0)
    753 			return(1);
    754 		if (upcase(*cp++) != upcase(*cp2++)) {
    755 			cp2 = ++backup;
    756 			cp = str;
    757 		}
    758 	}
    759 	return(*cp == 0);
    760 }
    761 
    762 /*
    763  * Mark the named message by setting its mark bit.
    764  */
    765 void
    766 mark(int mesg)
    767 {
    768 	int i;
    769 
    770 	i = mesg;
    771 	if (i < 1 || i > msgCount)
    772 		errx(1, "Bad message number to mark");
    773 	message[i-1].m_flag |= MMARK;
    774 }
    775 
    776 /*
    777  * Unmark the named message.
    778  */
    779 void
    780 unmark(int mesg)
    781 {
    782 	int i;
    783 
    784 	i = mesg;
    785 	if (i < 1 || i > msgCount)
    786 		errx(1, "Bad message number to unmark");
    787 	message[i-1].m_flag &= ~MMARK;
    788 }
    789 
    790 /*
    791  * Return the message number corresponding to the passed meta character.
    792  */
    793 int
    794 metamess(int meta, int f)
    795 {
    796 	int c, m;
    797 	struct message *mp;
    798 
    799 	c = meta;
    800 	switch (c) {
    801 	case '^':
    802 		/*
    803 		 * First 'good' message left.
    804 		 */
    805 		for (mp = &message[0]; mp < &message[msgCount]; mp++)
    806 			if ((mp->m_flag & MDELETED) == f)
    807 				return(mp - &message[0] + 1);
    808 		printf("No applicable messages\n");
    809 		return(-1);
    810 
    811 	case '$':
    812 		/*
    813 		 * Last 'good message left.
    814 		 */
    815 		for (mp = &message[msgCount-1]; mp >= &message[0]; mp--)
    816 			if ((mp->m_flag & MDELETED) == f)
    817 				return(mp - &message[0] + 1);
    818 		printf("No applicable messages\n");
    819 		return(-1);
    820 
    821 	case '.':
    822 		/*
    823 		 * Current message.
    824 		 */
    825 		m = dot - &message[0] + 1;
    826 		if ((dot->m_flag & MDELETED) != f) {
    827 			printf("%d: Inappropriate message\n", m);
    828 			return(-1);
    829 		}
    830 		return(m);
    831 
    832 	default:
    833 		printf("Unknown metachar (%c)\n", c);
    834 		return(-1);
    835 	}
    836 }
    837