xref: /illumos-gate/usr/src/cmd/mailx/list.c (revision 46d64c14)
17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate  * CDDL HEADER START
37c478bd9Sstevel@tonic-gate  *
47c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5eb2b0a61Sas  * Common Development and Distribution License (the "License").
6eb2b0a61Sas  * You may not use this file except in compliance with the License.
77c478bd9Sstevel@tonic-gate  *
87c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
97c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
107c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
117c478bd9Sstevel@tonic-gate  * and limitations under the License.
127c478bd9Sstevel@tonic-gate  *
137c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
147c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
157c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
167c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
177c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
187c478bd9Sstevel@tonic-gate  *
197c478bd9Sstevel@tonic-gate  * CDDL HEADER END
207c478bd9Sstevel@tonic-gate  */
217c478bd9Sstevel@tonic-gate 
227c478bd9Sstevel@tonic-gate /*
23eb2b0a61Sas  * Copyright 2006 Sun Microsystems, Inc.  All rights reserved.
24eb2b0a61Sas  * Use is subject to license terms.
257c478bd9Sstevel@tonic-gate  */
267c478bd9Sstevel@tonic-gate 
27eb2b0a61Sas /* Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T */
28eb2b0a61Sas /* All Rights Reserved   */
29eb2b0a61Sas 
307c478bd9Sstevel@tonic-gate /*
317c478bd9Sstevel@tonic-gate  * University Copyright- Copyright (c) 1982, 1986, 1988
327c478bd9Sstevel@tonic-gate  * The Regents of the University of California
337c478bd9Sstevel@tonic-gate  * All Rights Reserved
347c478bd9Sstevel@tonic-gate  *
357c478bd9Sstevel@tonic-gate  * University Acknowledgment- Portions of this document are derived from
367c478bd9Sstevel@tonic-gate  * software developed by the University of California, Berkeley, and its
377c478bd9Sstevel@tonic-gate  * contributors.
387c478bd9Sstevel@tonic-gate  */
397c478bd9Sstevel@tonic-gate 
407c478bd9Sstevel@tonic-gate #include "rcv.h"
417c478bd9Sstevel@tonic-gate #include <locale.h>
42411fa6a8Sas #include <stdlib.h>
43411fa6a8Sas #include <string.h>
447c478bd9Sstevel@tonic-gate 
457c478bd9Sstevel@tonic-gate /*
467c478bd9Sstevel@tonic-gate  * mailx -- a modified version of a University of California at Berkeley
477c478bd9Sstevel@tonic-gate  *	mail program
487c478bd9Sstevel@tonic-gate  *
497c478bd9Sstevel@tonic-gate  * Message list handling.
507c478bd9Sstevel@tonic-gate  */
517c478bd9Sstevel@tonic-gate 
527c478bd9Sstevel@tonic-gate static int	check(int mesg, int f);
537c478bd9Sstevel@tonic-gate static int	evalcol(int col);
54411fa6a8Sas static int	isinteger(char *buf);
557c478bd9Sstevel@tonic-gate static void	mark(int mesg);
567c478bd9Sstevel@tonic-gate static int	markall(char buf[], int f);
577c478bd9Sstevel@tonic-gate static int	matchsubj(char *str, int mesg);
587c478bd9Sstevel@tonic-gate static int	metamess(int meta, int f);
597c478bd9Sstevel@tonic-gate static void	regret(int token);
607c478bd9Sstevel@tonic-gate static int	scan(char **sp);
617c478bd9Sstevel@tonic-gate static void	scaninit(void);
627c478bd9Sstevel@tonic-gate static int	sender(char *str, int mesg);
637c478bd9Sstevel@tonic-gate static void	unmark(int mesg);
647c478bd9Sstevel@tonic-gate 
657c478bd9Sstevel@tonic-gate /*
66eb2b0a61Sas  * Process message operand list.
67eb2b0a61Sas  * Convert the user string of message numbers and
68eb2b0a61Sas  * store the numbers into vector.
69eb2b0a61Sas  *
70eb2b0a61Sas  * Returns the count of messages picked up or -1 on error.
71eb2b0a61Sas  */
72eb2b0a61Sas int
getmessage(char * buf,int * vector,int flags)73eb2b0a61Sas getmessage(char *buf, int *vector, int flags)
74eb2b0a61Sas {
75*46d64c14SToomas Soome 	int *ip;
76*46d64c14SToomas Soome 	struct message *mp;
77411fa6a8Sas 	int firstmsg = -1;
78411fa6a8Sas 	char delims[] = "\t- ";
79411fa6a8Sas 	char *result  = NULL;
80eb2b0a61Sas 
81eb2b0a61Sas 	if (markall(buf, flags) < 0)
82eb2b0a61Sas 		return (-1);
83eb2b0a61Sas 	ip = vector;
84eb2b0a61Sas 
85eb2b0a61Sas 	/*
86eb2b0a61Sas 	 * Check for first message number and make sure it is
87eb2b0a61Sas 	 * at the beginning of the vector.
88eb2b0a61Sas 	 */
89411fa6a8Sas 	result = strtok(buf, delims);
90411fa6a8Sas 	if (result != NULL && isinteger(result)) {
91411fa6a8Sas 		firstmsg = atoi(result);
92eb2b0a61Sas 		*ip++ = firstmsg;
93eb2b0a61Sas 	}
94eb2b0a61Sas 
95eb2b0a61Sas 	/*
96eb2b0a61Sas 	 * Add marked messages to vector and skip first
97eb2b0a61Sas 	 * message number because it is already at the
98eb2b0a61Sas 	 * beginning of the vector
99eb2b0a61Sas 	 */
100eb2b0a61Sas 	for (mp = &message[0]; mp < &message[msgCount]; mp++) {
101eb2b0a61Sas 		if (firstmsg == mp - &message[0] + 1)
102eb2b0a61Sas 			continue;
103eb2b0a61Sas 		if (mp->m_flag & MMARK)
104eb2b0a61Sas 			*ip++ = mp - &message[0] + 1;
105eb2b0a61Sas 	}
106*46d64c14SToomas Soome 	*ip = 0;
107eb2b0a61Sas 	return (ip - vector);
108eb2b0a61Sas }
109eb2b0a61Sas 
110411fa6a8Sas /*
111411fa6a8Sas  * Check to see if string is an integer
112411fa6a8Sas  *
113411fa6a8Sas  * Returns 1 if is an integer and 0 if it is not
114411fa6a8Sas  */
115411fa6a8Sas static int
isinteger(char * buf)116411fa6a8Sas isinteger(char *buf)
117411fa6a8Sas {
118411fa6a8Sas 	int i, result = 1;
119411fa6a8Sas 
120411fa6a8Sas 	/* check for empty string */
121411fa6a8Sas 	if (strcmp(buf, "") == 0) {
122411fa6a8Sas 		result = 0;
123411fa6a8Sas 		return (result);
124411fa6a8Sas 	}
125411fa6a8Sas 
126411fa6a8Sas 	i = 0;
127411fa6a8Sas 	while (buf[i] != '\0') {
128411fa6a8Sas 		if (!isdigit(buf[i])) {
129411fa6a8Sas 			result = 0;
130411fa6a8Sas 			break;
131411fa6a8Sas 		}
132411fa6a8Sas 		i++;
133411fa6a8Sas 	}
134411fa6a8Sas 	return (result);
135411fa6a8Sas }
136411fa6a8Sas 
137eb2b0a61Sas /*
138eb2b0a61Sas  * Process msglist operand list.
1397c478bd9Sstevel@tonic-gate  * Convert the user string of message numbers and
1407c478bd9Sstevel@tonic-gate  * store the numbers into vector.
1417c478bd9Sstevel@tonic-gate  *
1427c478bd9Sstevel@tonic-gate  * Returns the count of messages picked up or -1 on error.
1437c478bd9Sstevel@tonic-gate  */
1447c478bd9Sstevel@tonic-gate 
145eb2b0a61Sas int
getmsglist(char * buf,int * vector,int flags)1467c478bd9Sstevel@tonic-gate getmsglist(char *buf, int *vector, int flags)
1477c478bd9Sstevel@tonic-gate {
148*46d64c14SToomas Soome 	int *ip;
149*46d64c14SToomas Soome 	struct message *mp;
1507c478bd9Sstevel@tonic-gate 
1517c478bd9Sstevel@tonic-gate 	if (markall(buf, flags) < 0)
152eb2b0a61Sas 		return (-1);
1537c478bd9Sstevel@tonic-gate 	ip = vector;
1547c478bd9Sstevel@tonic-gate 	for (mp = &message[0]; mp < &message[msgCount]; mp++)
1557c478bd9Sstevel@tonic-gate 		if (mp->m_flag & MMARK)
1567c478bd9Sstevel@tonic-gate 			*ip++ = mp - &message[0] + 1;
157*46d64c14SToomas Soome 	*ip = 0;
158eb2b0a61Sas 	return (ip - vector);
1597c478bd9Sstevel@tonic-gate }
1607c478bd9Sstevel@tonic-gate 
161eb2b0a61Sas 
1627c478bd9Sstevel@tonic-gate /*
1637c478bd9Sstevel@tonic-gate  * Mark all messages that the user wanted from the command
1647c478bd9Sstevel@tonic-gate  * line in the message structure.  Return 0 on success, -1
1657c478bd9Sstevel@tonic-gate  * on error.
1667c478bd9Sstevel@tonic-gate  */
1677c478bd9Sstevel@tonic-gate 
1687c478bd9Sstevel@tonic-gate /*
1697c478bd9Sstevel@tonic-gate  * Bit values for colon modifiers.
1707c478bd9Sstevel@tonic-gate  */
1717c478bd9Sstevel@tonic-gate 
1727c478bd9Sstevel@tonic-gate #define	CMNEW		01		/* New messages */
1737c478bd9Sstevel@tonic-gate #define	CMOLD		02		/* Old messages */
1747c478bd9Sstevel@tonic-gate #define	CMUNREAD	04		/* Unread messages */
1757c478bd9Sstevel@tonic-gate #define	CMDELETED	010		/* Deleted messages */
1767c478bd9Sstevel@tonic-gate #define	CMREAD		020		/* Read messages */
1777c478bd9Sstevel@tonic-gate 
1787c478bd9Sstevel@tonic-gate /*
1797c478bd9Sstevel@tonic-gate  * The following table describes the letters which can follow
1807c478bd9Sstevel@tonic-gate  * the colon and gives the corresponding modifier bit.
1817c478bd9Sstevel@tonic-gate  */
1827c478bd9Sstevel@tonic-gate 
1837c478bd9Sstevel@tonic-gate static struct coltab {
1847c478bd9Sstevel@tonic-gate 	char	co_char;		/* What to find past : */
1857c478bd9Sstevel@tonic-gate 	int	co_bit;			/* Associated modifier bit */
1867c478bd9Sstevel@tonic-gate 	int	co_mask;		/* m_status bits to mask */
1877c478bd9Sstevel@tonic-gate 	int	co_equal;		/* ... must equal this */
1887c478bd9Sstevel@tonic-gate } coltab[] = {
1897c478bd9Sstevel@tonic-gate 	'n',		CMNEW,		MNEW,		MNEW,
1907c478bd9Sstevel@tonic-gate 	'o',		CMOLD,		MNEW,		0,
1917c478bd9Sstevel@tonic-gate 	'u',		CMUNREAD,	MREAD,		0,
1927c478bd9Sstevel@tonic-gate 	'd',		CMDELETED,	MDELETED,	MDELETED,
1937c478bd9Sstevel@tonic-gate 	'r',		CMREAD,		MREAD,		MREAD,
1947c478bd9Sstevel@tonic-gate 	0,		0,		0,		0
1957c478bd9Sstevel@tonic-gate };
1967c478bd9Sstevel@tonic-gate 
1977c478bd9Sstevel@tonic-gate static	int	lastcolmod;
1987c478bd9Sstevel@tonic-gate 
199eb2b0a61Sas static int
markall(char buf[],int f)2007c478bd9Sstevel@tonic-gate markall(char buf[], int f)
2017c478bd9Sstevel@tonic-gate {
202*46d64c14SToomas Soome 	char **np;
203*46d64c14SToomas Soome 	int i;
204*46d64c14SToomas Soome 	struct message *mp;
2057c478bd9Sstevel@tonic-gate 	char *namelist[NMLSIZE], *bufp;
2067c478bd9Sstevel@tonic-gate 	int tok, beg, mc, star, other, colmod, colresult;
2077c478bd9Sstevel@tonic-gate 
2087c478bd9Sstevel@tonic-gate 	colmod = 0;
2097c478bd9Sstevel@tonic-gate 	for (i = 1; i <= msgCount; i++)
2107c478bd9Sstevel@tonic-gate 		unmark(i);
2117c478bd9Sstevel@tonic-gate 	bufp = buf;
2127c478bd9Sstevel@tonic-gate 	mc = 0;
2137c478bd9Sstevel@tonic-gate 	np = &namelist[0];
2147c478bd9Sstevel@tonic-gate 	scaninit();
2157c478bd9Sstevel@tonic-gate 	tok = scan(&bufp);
2167c478bd9Sstevel@tonic-gate 	star = 0;
2177c478bd9Sstevel@tonic-gate 	other = 0;
2187c478bd9Sstevel@tonic-gate 	beg = 0;
2197c478bd9Sstevel@tonic-gate 	while (tok != TEOL) {
2207c478bd9Sstevel@tonic-gate 		switch (tok) {
2217c478bd9Sstevel@tonic-gate 		case TNUMBER:
2227c478bd9Sstevel@tonic-gate number:
2237c478bd9Sstevel@tonic-gate 			if (star) {
2247c478bd9Sstevel@tonic-gate 				printf(gettext("No numbers mixed with *\n"));
225eb2b0a61Sas 				return (-1);
2267c478bd9Sstevel@tonic-gate 			}
2277c478bd9Sstevel@tonic-gate 			mc++;
2287c478bd9Sstevel@tonic-gate 			other++;
2297c478bd9Sstevel@tonic-gate 			if (beg != 0) {
2307c478bd9Sstevel@tonic-gate 				if (check(lexnumber, f))
231eb2b0a61Sas 					return (-1);
2327c478bd9Sstevel@tonic-gate 				for (i = beg; i <= lexnumber; i++)
2337c478bd9Sstevel@tonic-gate 					if ((message[i-1].m_flag&MDELETED) == f)
2347c478bd9Sstevel@tonic-gate 						mark(i);
2357c478bd9Sstevel@tonic-gate 				beg = 0;
2367c478bd9Sstevel@tonic-gate 				break;
2377c478bd9Sstevel@tonic-gate 			}
2387c478bd9Sstevel@tonic-gate 			beg = lexnumber;
2397c478bd9Sstevel@tonic-gate 			if (check(beg, f))
240eb2b0a61Sas 				return (-1);
2417c478bd9Sstevel@tonic-gate 			tok = scan(&bufp);
2427c478bd9Sstevel@tonic-gate 			if (tok != TDASH) {
2437c478bd9Sstevel@tonic-gate 				regret(tok);
2447c478bd9Sstevel@tonic-gate 				mark(beg);
2457c478bd9Sstevel@tonic-gate 				beg = 0;
2467c478bd9Sstevel@tonic-gate 			}
2477c478bd9Sstevel@tonic-gate 			break;
2487c478bd9Sstevel@tonic-gate 
2497c478bd9Sstevel@tonic-gate 		case TSTRING:
2507c478bd9Sstevel@tonic-gate 			if (beg != 0) {
2517c478bd9Sstevel@tonic-gate 				printf(gettext(
2527c478bd9Sstevel@tonic-gate 				    "Non-numeric second argument\n"));
253eb2b0a61Sas 				return (-1);
2547c478bd9Sstevel@tonic-gate 			}
2557c478bd9Sstevel@tonic-gate 			other++;
2567c478bd9Sstevel@tonic-gate 			if (lexstring[0] == ':') {
2577c478bd9Sstevel@tonic-gate 				colresult = evalcol(lexstring[1]);
2587c478bd9Sstevel@tonic-gate 				if (colresult == 0) {
2597c478bd9Sstevel@tonic-gate 					printf(gettext(
2607c478bd9Sstevel@tonic-gate 					    "Unknown colon modifier \"%s\"\n"),
2617c478bd9Sstevel@tonic-gate 					    lexstring);
262eb2b0a61Sas 					return (-1);
2637c478bd9Sstevel@tonic-gate 				}
2647c478bd9Sstevel@tonic-gate 				colmod |= colresult;
2657c478bd9Sstevel@tonic-gate 			}
2667c478bd9Sstevel@tonic-gate 			else
2677c478bd9Sstevel@tonic-gate 				*np++ = savestr(lexstring);
2687c478bd9Sstevel@tonic-gate 			break;
2697c478bd9Sstevel@tonic-gate 
2707c478bd9Sstevel@tonic-gate 		case TDASH:
2717c478bd9Sstevel@tonic-gate 		case TPLUS:
2727c478bd9Sstevel@tonic-gate 		case TDOLLAR:
2737c478bd9Sstevel@tonic-gate 		case TUP:
2747c478bd9Sstevel@tonic-gate 		case TDOT:
2757c478bd9Sstevel@tonic-gate 			lexnumber = metamess(lexstring[0], f);
2767c478bd9Sstevel@tonic-gate 			if (lexnumber == -1)
277eb2b0a61Sas 				return (-1);
2787c478bd9Sstevel@tonic-gate 			goto number;
2797c478bd9Sstevel@tonic-gate 
2807c478bd9Sstevel@tonic-gate 		case TSTAR:
2817c478bd9Sstevel@tonic-gate 			if (other) {
2827c478bd9Sstevel@tonic-gate 				printf(gettext(
2837c478bd9Sstevel@tonic-gate 				    "Can't mix \"*\" with anything\n"));
284eb2b0a61Sas 				return (-1);
2857c478bd9Sstevel@tonic-gate 			}
2867c478bd9Sstevel@tonic-gate 			star++;
2877c478bd9Sstevel@tonic-gate 			break;
2887c478bd9Sstevel@tonic-gate 		}
2897c478bd9Sstevel@tonic-gate 		tok = scan(&bufp);
2907c478bd9Sstevel@tonic-gate 	}
2917c478bd9Sstevel@tonic-gate 	lastcolmod = colmod;
2927c478bd9Sstevel@tonic-gate 	*np = NOSTR;
2937c478bd9Sstevel@tonic-gate 	mc = 0;
2947c478bd9Sstevel@tonic-gate 	if (star) {
2957c478bd9Sstevel@tonic-gate 		for (i = 0; i < msgCount; i++)
2967c478bd9Sstevel@tonic-gate 			if ((message[i].m_flag & MDELETED) == f) {
2977c478bd9Sstevel@tonic-gate 				mark(i+1);
2987c478bd9Sstevel@tonic-gate 				mc++;
2997c478bd9Sstevel@tonic-gate 			}
3007c478bd9Sstevel@tonic-gate 		if (mc == 0) {
3017c478bd9Sstevel@tonic-gate 			printf(gettext("No applicable messages\n"));
302eb2b0a61Sas 			return (-1);
3037c478bd9Sstevel@tonic-gate 		}
304eb2b0a61Sas 		return (0);
3057c478bd9Sstevel@tonic-gate 	}
3067c478bd9Sstevel@tonic-gate 
3077c478bd9Sstevel@tonic-gate 	/*
3087c478bd9Sstevel@tonic-gate 	 * If no numbers were given, mark all of the messages,
3097c478bd9Sstevel@tonic-gate 	 * so that we can unmark any whose sender was not selected
3107c478bd9Sstevel@tonic-gate 	 * if any user names were given.
3117c478bd9Sstevel@tonic-gate 	 */
3127c478bd9Sstevel@tonic-gate 
3137c478bd9Sstevel@tonic-gate 	if ((np > namelist || colmod != 0) && mc == 0)
3147c478bd9Sstevel@tonic-gate 		for (i = 1; i <= msgCount; i++)
3157c478bd9Sstevel@tonic-gate 			if ((message[i-1].m_flag & MDELETED) == f)
3167c478bd9Sstevel@tonic-gate 				mark(i);
3177c478bd9Sstevel@tonic-gate 
3187c478bd9Sstevel@tonic-gate 	/*
3197c478bd9Sstevel@tonic-gate 	 * If any names were given, go through and eliminate any
3207c478bd9Sstevel@tonic-gate 	 * messages whose senders were not requested.
3217c478bd9Sstevel@tonic-gate 	 */
3227c478bd9Sstevel@tonic-gate 
3237c478bd9Sstevel@tonic-gate 	if (np > namelist) {
3247c478bd9Sstevel@tonic-gate 		for (i = 1; i <= msgCount; i++) {
3257c478bd9Sstevel@tonic-gate 			for (mc = 0, np = &namelist[0]; *np != NOSTR; np++)
3267c478bd9Sstevel@tonic-gate 				if (**np == '/') {
3277c478bd9Sstevel@tonic-gate 					if (matchsubj(*np, i)) {
3287c478bd9Sstevel@tonic-gate 						mc++;
3297c478bd9Sstevel@tonic-gate 						break;
3307c478bd9Sstevel@tonic-gate 					}
331eb2b0a61Sas 				} else {
3327c478bd9Sstevel@tonic-gate 					if (sender(*np, i)) {
3337c478bd9Sstevel@tonic-gate 						mc++;
3347c478bd9Sstevel@tonic-gate 						break;
3357c478bd9Sstevel@tonic-gate 					}
3367c478bd9Sstevel@tonic-gate 				}
3377c478bd9Sstevel@tonic-gate 			if (mc == 0)
3387c478bd9Sstevel@tonic-gate 				unmark(i);
3397c478bd9Sstevel@tonic-gate 		}
3407c478bd9Sstevel@tonic-gate 
3417c478bd9Sstevel@tonic-gate 		/*
3427c478bd9Sstevel@tonic-gate 		 * Make sure we got some decent messages.
3437c478bd9Sstevel@tonic-gate 		 */
3447c478bd9Sstevel@tonic-gate 
3457c478bd9Sstevel@tonic-gate 		mc = 0;
3467c478bd9Sstevel@tonic-gate 		for (i = 1; i <= msgCount; i++)
3477c478bd9Sstevel@tonic-gate 			if (message[i-1].m_flag & MMARK) {
3487c478bd9Sstevel@tonic-gate 				mc++;
3497c478bd9Sstevel@tonic-gate 				break;
3507c478bd9Sstevel@tonic-gate 			}
3517c478bd9Sstevel@tonic-gate 		if (mc == 0) {
3527c478bd9Sstevel@tonic-gate 			printf(gettext("No applicable messages from {%s"),
3537c478bd9Sstevel@tonic-gate namelist[0]);
3547c478bd9Sstevel@tonic-gate 			for (np = &namelist[1]; *np != NOSTR; np++)
3557c478bd9Sstevel@tonic-gate 				printf(", %s", *np);
3567c478bd9Sstevel@tonic-gate 			printf("}\n");
357eb2b0a61Sas 			return (-1);
3587c478bd9Sstevel@tonic-gate 		}
3597c478bd9Sstevel@tonic-gate 	}
3607c478bd9Sstevel@tonic-gate 
3617c478bd9Sstevel@tonic-gate 	/*
3627c478bd9Sstevel@tonic-gate 	 * If any colon modifiers were given, go through and
3637c478bd9Sstevel@tonic-gate 	 * unmark any messages which do not satisfy the modifiers.
3647c478bd9Sstevel@tonic-gate 	 */
3657c478bd9Sstevel@tonic-gate 
3667c478bd9Sstevel@tonic-gate 	if (colmod != 0) {
3677c478bd9Sstevel@tonic-gate 		for (i = 1; i <= msgCount; i++) {
368*46d64c14SToomas Soome 			struct coltab *colp;
3697c478bd9Sstevel@tonic-gate 
3707c478bd9Sstevel@tonic-gate 			mp = &message[i - 1];
3717c478bd9Sstevel@tonic-gate 			for (colp = &coltab[0]; colp->co_char; colp++)
3727c478bd9Sstevel@tonic-gate 				if (colp->co_bit & colmod)
3737c478bd9Sstevel@tonic-gate 					if ((mp->m_flag & colp->co_mask)
3747c478bd9Sstevel@tonic-gate 					    != colp->co_equal)
3757c478bd9Sstevel@tonic-gate 						unmark(i);
3767c478bd9Sstevel@tonic-gate 
3777c478bd9Sstevel@tonic-gate 		}
3787c478bd9Sstevel@tonic-gate 		for (mp = &message[0]; mp < &message[msgCount]; mp++)
3797c478bd9Sstevel@tonic-gate 			if (mp->m_flag & MMARK)
3807c478bd9Sstevel@tonic-gate 				break;
3817c478bd9Sstevel@tonic-gate 		if (mp >= &message[msgCount]) {
382*46d64c14SToomas Soome 			struct coltab *colp;
3837c478bd9Sstevel@tonic-gate 
3847c478bd9Sstevel@tonic-gate 			printf(gettext("No messages satisfy"));
3857c478bd9Sstevel@tonic-gate 			for (colp = &coltab[0]; colp->co_char; colp++)
3867c478bd9Sstevel@tonic-gate 				if (colp->co_bit & colmod)
3877c478bd9Sstevel@tonic-gate 					printf(" :%c", colp->co_char);
3887c478bd9Sstevel@tonic-gate 			printf("\n");
389eb2b0a61Sas 			return (-1);
3907c478bd9Sstevel@tonic-gate 		}
3917c478bd9Sstevel@tonic-gate 	}
392eb2b0a61Sas 	return (0);
3937c478bd9Sstevel@tonic-gate }
3947c478bd9Sstevel@tonic-gate 
3957c478bd9Sstevel@tonic-gate /*
3967c478bd9Sstevel@tonic-gate  * Turn the character after a colon modifier into a bit
3977c478bd9Sstevel@tonic-gate  * value.
3987c478bd9Sstevel@tonic-gate  */
399eb2b0a61Sas static int
evalcol(int col)4007c478bd9Sstevel@tonic-gate evalcol(int col)
4017c478bd9Sstevel@tonic-gate {
402*46d64c14SToomas Soome 	struct coltab *colp;
4037c478bd9Sstevel@tonic-gate 
4047c478bd9Sstevel@tonic-gate 	if (col == 0)
405eb2b0a61Sas 		return (lastcolmod);
4067c478bd9Sstevel@tonic-gate 	for (colp = &coltab[0]; colp->co_char; colp++)
4077c478bd9Sstevel@tonic-gate 		if (colp->co_char == col)
408eb2b0a61Sas 			return (colp->co_bit);
409eb2b0a61Sas 	return (0);
4107c478bd9Sstevel@tonic-gate }
4117c478bd9Sstevel@tonic-gate 
4127c478bd9Sstevel@tonic-gate /*
4137c478bd9Sstevel@tonic-gate  * Check the passed message number for legality and proper flags.
4147c478bd9Sstevel@tonic-gate  */
415eb2b0a61Sas static int
check(int mesg,int f)4167c478bd9Sstevel@tonic-gate check(int mesg, int f)
4177c478bd9Sstevel@tonic-gate {
418*46d64c14SToomas Soome 	struct message *mp;
4197c478bd9Sstevel@tonic-gate 
4207c478bd9Sstevel@tonic-gate 	if (mesg < 1 || mesg > msgCount) {
4217c478bd9Sstevel@tonic-gate 		printf(gettext("%d: Invalid message number\n"), mesg);
422eb2b0a61Sas 		return (-1);
4237c478bd9Sstevel@tonic-gate 	}
4247c478bd9Sstevel@tonic-gate 	mp = &message[mesg-1];
4257c478bd9Sstevel@tonic-gate 	if ((mp->m_flag & MDELETED) != f) {
4267c478bd9Sstevel@tonic-gate 		printf(gettext("%d: Inappropriate message\n"), mesg);
427eb2b0a61Sas 		return (-1);
4287c478bd9Sstevel@tonic-gate 	}
429eb2b0a61Sas 	return (0);
4307c478bd9Sstevel@tonic-gate }
4317c478bd9Sstevel@tonic-gate 
4327c478bd9Sstevel@tonic-gate /*
4337c478bd9Sstevel@tonic-gate  * Scan out the list of string arguments, shell style
4347c478bd9Sstevel@tonic-gate  * for a RAWLIST.
4357c478bd9Sstevel@tonic-gate  */
4367c478bd9Sstevel@tonic-gate 
437eb2b0a61Sas int
getrawlist(char line[],char ** argv,int argc)4387c478bd9Sstevel@tonic-gate getrawlist(char line[], char **argv, int argc)
4397c478bd9Sstevel@tonic-gate {
440*46d64c14SToomas Soome 	char **ap, *cp, *cp2;
4417c478bd9Sstevel@tonic-gate 	char linebuf[LINESIZE], quotec;
442*46d64c14SToomas Soome 	char **last;
4437c478bd9Sstevel@tonic-gate 
4447c478bd9Sstevel@tonic-gate 	ap = argv;
4457c478bd9Sstevel@tonic-gate 	cp = line;
4467c478bd9Sstevel@tonic-gate 	last = argv + argc - 1;
4477c478bd9Sstevel@tonic-gate 	while (*cp != '\0') {
4487c478bd9Sstevel@tonic-gate 		while (any(*cp, " \t"))
4497c478bd9Sstevel@tonic-gate 			cp++;
4507c478bd9Sstevel@tonic-gate 		cp2 = linebuf;
4517c478bd9Sstevel@tonic-gate 		quotec = 0;
4527c478bd9Sstevel@tonic-gate 		while (*cp != '\0') {
4537c478bd9Sstevel@tonic-gate 			if (quotec) {
4547c478bd9Sstevel@tonic-gate 				if (*cp == quotec) {
455eb2b0a61Sas 					quotec = 0;
4567c478bd9Sstevel@tonic-gate 					cp++;
4577c478bd9Sstevel@tonic-gate 				} else
4587c478bd9Sstevel@tonic-gate 					*cp2++ = *cp++;
4597c478bd9Sstevel@tonic-gate 			} else {
4607c478bd9Sstevel@tonic-gate 				if (*cp == '\\') {
4617c478bd9Sstevel@tonic-gate 					if (*(cp+1) != '\0') {
4627c478bd9Sstevel@tonic-gate 						*cp2++ = *++cp;
4637c478bd9Sstevel@tonic-gate 						cp++;
4647c478bd9Sstevel@tonic-gate 					} else {
4657c478bd9Sstevel@tonic-gate 						printf(gettext(
466eb2b0a61Sas 						    "Trailing \\; ignoring\n"));
4677c478bd9Sstevel@tonic-gate 						break;
4687c478bd9Sstevel@tonic-gate 					}
4697c478bd9Sstevel@tonic-gate 				}
4707c478bd9Sstevel@tonic-gate 				if (any(*cp, " \t"))
4717c478bd9Sstevel@tonic-gate 					break;
4727c478bd9Sstevel@tonic-gate 				if (any(*cp, "'\""))
4737c478bd9Sstevel@tonic-gate 					quotec = *cp++;
4747c478bd9Sstevel@tonic-gate 				else
4757c478bd9Sstevel@tonic-gate 					*cp2++ = *cp++;
4767c478bd9Sstevel@tonic-gate 			}
4777c478bd9Sstevel@tonic-gate 		}
4787c478bd9Sstevel@tonic-gate 		*cp2 = '\0';
4797c478bd9Sstevel@tonic-gate 		if (cp2 == linebuf)
4807c478bd9Sstevel@tonic-gate 			break;
4817c478bd9Sstevel@tonic-gate 		if (ap >= last) {
482eb2b0a61Sas 			printf(gettext("Too many elements in the list;"
483eb2b0a61Sas 			    " excess discarded\n"));
4847c478bd9Sstevel@tonic-gate 			break;
4857c478bd9Sstevel@tonic-gate 		}
4867c478bd9Sstevel@tonic-gate 		*ap++ = savestr(linebuf);
4877c478bd9Sstevel@tonic-gate 	}
4887c478bd9Sstevel@tonic-gate 	*ap = NOSTR;
489eb2b0a61Sas 	return (ap-argv);
4907c478bd9Sstevel@tonic-gate }
4917c478bd9Sstevel@tonic-gate 
4927c478bd9Sstevel@tonic-gate /*
4937c478bd9Sstevel@tonic-gate  * scan out a single lexical item and return its token number,
4947c478bd9Sstevel@tonic-gate  * updating the string pointer passed **p.  Also, store the value
4957c478bd9Sstevel@tonic-gate  * of the number or string scanned in lexnumber or lexstring as
4967c478bd9Sstevel@tonic-gate  * appropriate.  In any event, store the scanned `thing' in lexstring.
4977c478bd9Sstevel@tonic-gate  */
4987c478bd9Sstevel@tonic-gate 
4997c478bd9Sstevel@tonic-gate static struct lex {
5007c478bd9Sstevel@tonic-gate 	char	l_char;
5017c478bd9Sstevel@tonic-gate 	char	l_token;
5027c478bd9Sstevel@tonic-gate } singles[] = {
5037c478bd9Sstevel@tonic-gate 	'$',	TDOLLAR,
5047c478bd9Sstevel@tonic-gate 	'.',	TDOT,
5057c478bd9Sstevel@tonic-gate 	'^',	TUP,
5067c478bd9Sstevel@tonic-gate 	'*',	TSTAR,
5077c478bd9Sstevel@tonic-gate 	'-',	TDASH,
5087c478bd9Sstevel@tonic-gate 	'+',	TPLUS,
5097c478bd9Sstevel@tonic-gate 	'(',	TOPEN,
5107c478bd9Sstevel@tonic-gate 	')',	TCLOSE,
5117c478bd9Sstevel@tonic-gate 	0,	0
5127c478bd9Sstevel@tonic-gate };
5137c478bd9Sstevel@tonic-gate 
514eb2b0a61Sas static int
scan(char ** sp)5157c478bd9Sstevel@tonic-gate scan(char **sp)
5167c478bd9Sstevel@tonic-gate {
517*46d64c14SToomas Soome 	char *cp, *cp2;
518*46d64c14SToomas Soome 	char c;
519*46d64c14SToomas Soome 	struct lex *lp;
5207c478bd9Sstevel@tonic-gate 	int quotec;
5217c478bd9Sstevel@tonic-gate 
5227c478bd9Sstevel@tonic-gate 	if (regretp >= 0) {
5237c478bd9Sstevel@tonic-gate 		copy(stringstack[regretp], lexstring);
5247c478bd9Sstevel@tonic-gate 		lexnumber = numberstack[regretp];
525eb2b0a61Sas 		return (regretstack[regretp--]);
5267c478bd9Sstevel@tonic-gate 	}
5277c478bd9Sstevel@tonic-gate 	cp = *sp;
5287c478bd9Sstevel@tonic-gate 	cp2 = lexstring;
5297c478bd9Sstevel@tonic-gate 	c = *cp++;
5307c478bd9Sstevel@tonic-gate 
5317c478bd9Sstevel@tonic-gate 	/*
5327c478bd9Sstevel@tonic-gate 	 * strip away leading white space.
5337c478bd9Sstevel@tonic-gate 	 */
5347c478bd9Sstevel@tonic-gate 
5357c478bd9Sstevel@tonic-gate 	while (any(c, " \t"))
5367c478bd9Sstevel@tonic-gate 		c = *cp++;
5377c478bd9Sstevel@tonic-gate 
5387c478bd9Sstevel@tonic-gate 	/*
5397c478bd9Sstevel@tonic-gate 	 * If no characters remain, we are at end of line,
5407c478bd9Sstevel@tonic-gate 	 * so report that.
5417c478bd9Sstevel@tonic-gate 	 */
5427c478bd9Sstevel@tonic-gate 
5437c478bd9Sstevel@tonic-gate 	if (c == '\0') {
5447c478bd9Sstevel@tonic-gate 		*sp = --cp;
545eb2b0a61Sas 		return (TEOL);
5467c478bd9Sstevel@tonic-gate 	}
5477c478bd9Sstevel@tonic-gate 
5487c478bd9Sstevel@tonic-gate 	/*
5497c478bd9Sstevel@tonic-gate 	 * If the leading character is a digit, scan
5507c478bd9Sstevel@tonic-gate 	 * the number and convert it on the fly.
5517c478bd9Sstevel@tonic-gate 	 * Return TNUMBER when done.
5527c478bd9Sstevel@tonic-gate 	 */
5537c478bd9Sstevel@tonic-gate 
5547c478bd9Sstevel@tonic-gate 	if (isdigit(c)) {
5557c478bd9Sstevel@tonic-gate 		lexnumber = 0;
5567c478bd9Sstevel@tonic-gate 		while (isdigit(c)) {
5577c478bd9Sstevel@tonic-gate 			lexnumber = lexnumber*10 + c - '0';
5587c478bd9Sstevel@tonic-gate 			*cp2++ = c;
5597c478bd9Sstevel@tonic-gate 			c = *cp++;
5607c478bd9Sstevel@tonic-gate 		}
5617c478bd9Sstevel@tonic-gate 		*cp2 = '\0';
5627c478bd9Sstevel@tonic-gate 		*sp = --cp;
563eb2b0a61Sas 		return (TNUMBER);
5647c478bd9Sstevel@tonic-gate 	}
5657c478bd9Sstevel@tonic-gate 
5667c478bd9Sstevel@tonic-gate 	/*
5677c478bd9Sstevel@tonic-gate 	 * Check for single character tokens; return such
5687c478bd9Sstevel@tonic-gate 	 * if found.
5697c478bd9Sstevel@tonic-gate 	 */
5707c478bd9Sstevel@tonic-gate 
5717c478bd9Sstevel@tonic-gate 	for (lp = &singles[0]; lp->l_char != 0; lp++)
5727c478bd9Sstevel@tonic-gate 		if (c == lp->l_char) {
5737c478bd9Sstevel@tonic-gate 			lexstring[0] = c;
5747c478bd9Sstevel@tonic-gate 			lexstring[1] = '\0';
5757c478bd9Sstevel@tonic-gate 			*sp = cp;
576eb2b0a61Sas 			return (lp->l_token);
5777c478bd9Sstevel@tonic-gate 		}
5787c478bd9Sstevel@tonic-gate 
5797c478bd9Sstevel@tonic-gate 	/*
5807c478bd9Sstevel@tonic-gate 	 * We've got a string!  Copy all the characters
5817c478bd9Sstevel@tonic-gate 	 * of the string into lexstring, until we see
5827c478bd9Sstevel@tonic-gate 	 * a null, space, or tab.
5837c478bd9Sstevel@tonic-gate 	 * If the lead character is a " or ', save it
5847c478bd9Sstevel@tonic-gate 	 * and scan until you get another.
5857c478bd9Sstevel@tonic-gate 	 */
5867c478bd9Sstevel@tonic-gate 
5877c478bd9Sstevel@tonic-gate 	quotec = 0;
5887c478bd9Sstevel@tonic-gate 	if (any(c, "'\"")) {
5897c478bd9Sstevel@tonic-gate 		quotec = c;
5907c478bd9Sstevel@tonic-gate 		c = *cp++;
5917c478bd9Sstevel@tonic-gate 	}
5927c478bd9Sstevel@tonic-gate 	while (c != '\0') {
5937c478bd9Sstevel@tonic-gate 		if (quotec == 0 && c == '\\') {
5947c478bd9Sstevel@tonic-gate 			if (*cp != '\0') {
5957c478bd9Sstevel@tonic-gate 				c = *cp++;
5967c478bd9Sstevel@tonic-gate 			} else {
5977c478bd9Sstevel@tonic-gate 				fprintf(stderr, gettext("Trailing \\; "
5987c478bd9Sstevel@tonic-gate 				    "ignoring\n"));
5997c478bd9Sstevel@tonic-gate 			}
6007c478bd9Sstevel@tonic-gate 		}
6017c478bd9Sstevel@tonic-gate 		if (c == quotec) {
6027c478bd9Sstevel@tonic-gate 			cp++;
6037c478bd9Sstevel@tonic-gate 			break;
6047c478bd9Sstevel@tonic-gate 		}
6057c478bd9Sstevel@tonic-gate 		if (quotec == 0 && any(c, " \t"))
6067c478bd9Sstevel@tonic-gate 			break;
6077c478bd9Sstevel@tonic-gate 		if (cp2 - lexstring < STRINGLEN-1)
6087c478bd9Sstevel@tonic-gate 			*cp2++ = c;
6097c478bd9Sstevel@tonic-gate 		c = *cp++;
6107c478bd9Sstevel@tonic-gate 	}
6117c478bd9Sstevel@tonic-gate 	if (quotec && c == 0)
6127c478bd9Sstevel@tonic-gate 		fprintf(stderr, gettext("Missing %c\n"), quotec);
6137c478bd9Sstevel@tonic-gate 	*sp = --cp;
6147c478bd9Sstevel@tonic-gate 	*cp2 = '\0';
615eb2b0a61Sas 	return (TSTRING);
6167c478bd9Sstevel@tonic-gate }
6177c478bd9Sstevel@tonic-gate 
6187c478bd9Sstevel@tonic-gate /*
6197c478bd9Sstevel@tonic-gate  * Unscan the named token by pushing it onto the regret stack.
6207c478bd9Sstevel@tonic-gate  */
6217c478bd9Sstevel@tonic-gate 
622eb2b0a61Sas static void
regret(int token)6237c478bd9Sstevel@tonic-gate regret(int token)
6247c478bd9Sstevel@tonic-gate {
6257c478bd9Sstevel@tonic-gate 	if (++regretp >= REGDEP)
6267c478bd9Sstevel@tonic-gate 		panic("Too many regrets");
6277c478bd9Sstevel@tonic-gate 	regretstack[regretp] = token;
6287c478bd9Sstevel@tonic-gate 	lexstring[STRINGLEN-1] = '\0';
6297c478bd9Sstevel@tonic-gate 	stringstack[regretp] = savestr(lexstring);
6307c478bd9Sstevel@tonic-gate 	numberstack[regretp] = lexnumber;
6317c478bd9Sstevel@tonic-gate }
6327c478bd9Sstevel@tonic-gate 
6337c478bd9Sstevel@tonic-gate /*
6347c478bd9Sstevel@tonic-gate  * Reset all the scanner global variables.
6357c478bd9Sstevel@tonic-gate  */
6367c478bd9Sstevel@tonic-gate 
637eb2b0a61Sas static void
scaninit(void)6387c478bd9Sstevel@tonic-gate scaninit(void)
6397c478bd9Sstevel@tonic-gate {
6407c478bd9Sstevel@tonic-gate 	regretp = -1;
6417c478bd9Sstevel@tonic-gate }
6427c478bd9Sstevel@tonic-gate 
6437c478bd9Sstevel@tonic-gate /*
6447c478bd9Sstevel@tonic-gate  * Find the first message whose flags & m == f  and return
6457c478bd9Sstevel@tonic-gate  * its message number.
6467c478bd9Sstevel@tonic-gate  */
6477c478bd9Sstevel@tonic-gate 
648eb2b0a61Sas int
first(int f,int m)6497c478bd9Sstevel@tonic-gate first(int f, int m)
6507c478bd9Sstevel@tonic-gate {
651*46d64c14SToomas Soome 	int mesg;
652*46d64c14SToomas Soome 	struct message *mp;
6537c478bd9Sstevel@tonic-gate 
6547c478bd9Sstevel@tonic-gate 	mesg = dot - &message[0] + 1;
6557c478bd9Sstevel@tonic-gate 	f &= MDELETED;
6567c478bd9Sstevel@tonic-gate 	m &= MDELETED;
6577c478bd9Sstevel@tonic-gate 	for (mp = dot; mp < &message[msgCount]; mp++) {
6587c478bd9Sstevel@tonic-gate 		if ((mp->m_flag & m) == f)
659eb2b0a61Sas 			return (mesg);
6607c478bd9Sstevel@tonic-gate 		mesg++;
6617c478bd9Sstevel@tonic-gate 	}
6627c478bd9Sstevel@tonic-gate 	mesg = dot - &message[0];
6637c478bd9Sstevel@tonic-gate 	for (mp = dot-1; mp >= &message[0]; mp--) {
6647c478bd9Sstevel@tonic-gate 		if ((mp->m_flag & m) == f)
665eb2b0a61Sas 			return (mesg);
6667c478bd9Sstevel@tonic-gate 		mesg--;
6677c478bd9Sstevel@tonic-gate 	}
668*46d64c14SToomas Soome 	return (0);
6697c478bd9Sstevel@tonic-gate }
6707c478bd9Sstevel@tonic-gate 
6717c478bd9Sstevel@tonic-gate /*
6727c478bd9Sstevel@tonic-gate  * See if the passed name sent the passed message number.  Return true
6737c478bd9Sstevel@tonic-gate  * if so.
6747c478bd9Sstevel@tonic-gate  */
675eb2b0a61Sas static int
sender(char * str,int mesg)6767c478bd9Sstevel@tonic-gate sender(char *str, int mesg)
6777c478bd9Sstevel@tonic-gate {
6787c478bd9Sstevel@tonic-gate 	return (samebody(str, skin(nameof(&message[mesg-1])), TRUE));
6797c478bd9Sstevel@tonic-gate }
6807c478bd9Sstevel@tonic-gate 
6817c478bd9Sstevel@tonic-gate /*
6827c478bd9Sstevel@tonic-gate  * See if the given string matches inside the subject field of the
6837c478bd9Sstevel@tonic-gate  * given message.  For the purpose of the scan, we ignore case differences.
6847c478bd9Sstevel@tonic-gate  * If it does, return true.  The string search argument is assumed to
6857c478bd9Sstevel@tonic-gate  * have the form "/search-string."  If it is of the form "/," we use the
6867c478bd9Sstevel@tonic-gate  * previous search string.
6877c478bd9Sstevel@tonic-gate  */
6887c478bd9Sstevel@tonic-gate 
6897c478bd9Sstevel@tonic-gate static char lastscan[128];
6907c478bd9Sstevel@tonic-gate 
691eb2b0a61Sas static int
matchsubj(char * str,int mesg)6927c478bd9Sstevel@tonic-gate matchsubj(char *str, int mesg)
6937c478bd9Sstevel@tonic-gate {
694*46d64c14SToomas Soome 	struct message *mp;
695*46d64c14SToomas Soome 	char *cp, *cp2, *backup;
6967c478bd9Sstevel@tonic-gate 
6977c478bd9Sstevel@tonic-gate 	str++;
6987c478bd9Sstevel@tonic-gate 	if (strlen(str) == 0)
6997c478bd9Sstevel@tonic-gate 		str = lastscan;
7007c478bd9Sstevel@tonic-gate 	else
7017c478bd9Sstevel@tonic-gate 		nstrcpy(lastscan, sizeof (lastscan), str);
7027c478bd9Sstevel@tonic-gate 	mp = &message[mesg-1];
7037c478bd9Sstevel@tonic-gate 
7047c478bd9Sstevel@tonic-gate 	/*
7057c478bd9Sstevel@tonic-gate 	 * Now look, ignoring case, for the word in the string.
7067c478bd9Sstevel@tonic-gate 	 */
7077c478bd9Sstevel@tonic-gate 
7087c478bd9Sstevel@tonic-gate 	cp = str;
7097c478bd9Sstevel@tonic-gate 	cp2 = hfield("subject", mp, addone);
7107c478bd9Sstevel@tonic-gate 	if (cp2 == NOSTR)
711eb2b0a61Sas 		return (0);
7127c478bd9Sstevel@tonic-gate 	backup = cp2;
7137c478bd9Sstevel@tonic-gate 	while (*cp2) {
7147c478bd9Sstevel@tonic-gate 		if (*cp == 0)
715eb2b0a61Sas 			return (1);
7167c478bd9Sstevel@tonic-gate 		if (toupper(*cp++) != toupper(*cp2++)) {
7177c478bd9Sstevel@tonic-gate 			cp2 = ++backup;
7187c478bd9Sstevel@tonic-gate 			cp = str;
7197c478bd9Sstevel@tonic-gate 		}
7207c478bd9Sstevel@tonic-gate 	}
721eb2b0a61Sas 	return (*cp == 0);
7227c478bd9Sstevel@tonic-gate }
7237c478bd9Sstevel@tonic-gate 
7247c478bd9Sstevel@tonic-gate /*
7257c478bd9Sstevel@tonic-gate  * Mark the named message by setting its mark bit.
7267c478bd9Sstevel@tonic-gate  */
7277c478bd9Sstevel@tonic-gate 
728eb2b0a61Sas static void
mark(int mesg)7297c478bd9Sstevel@tonic-gate mark(int mesg)
7307c478bd9Sstevel@tonic-gate {
731*46d64c14SToomas Soome 	int i;
7327c478bd9Sstevel@tonic-gate 
7337c478bd9Sstevel@tonic-gate 	i = mesg;
7347c478bd9Sstevel@tonic-gate 	if (i < 1 || i > msgCount)
7357c478bd9Sstevel@tonic-gate 		panic("Bad message number to mark");
7367c478bd9Sstevel@tonic-gate 	message[i-1].m_flag |= MMARK;
7377c478bd9Sstevel@tonic-gate }
7387c478bd9Sstevel@tonic-gate 
7397c478bd9Sstevel@tonic-gate /*
7407c478bd9Sstevel@tonic-gate  * Unmark the named message.
7417c478bd9Sstevel@tonic-gate  */
7427c478bd9Sstevel@tonic-gate 
743eb2b0a61Sas static void
unmark(int mesg)7447c478bd9Sstevel@tonic-gate unmark(int mesg)
7457c478bd9Sstevel@tonic-gate {
746*46d64c14SToomas Soome 	int i;
7477c478bd9Sstevel@tonic-gate 
7487c478bd9Sstevel@tonic-gate 	i = mesg;
7497c478bd9Sstevel@tonic-gate 	if (i < 1 || i > msgCount)
7507c478bd9Sstevel@tonic-gate 		panic("Bad message number to unmark");
7517c478bd9Sstevel@tonic-gate 	message[i-1].m_flag &= ~MMARK;
7527c478bd9Sstevel@tonic-gate }
7537c478bd9Sstevel@tonic-gate 
7547c478bd9Sstevel@tonic-gate /*
7557c478bd9Sstevel@tonic-gate  * Return the message number corresponding to the passed meta character.
7567c478bd9Sstevel@tonic-gate  */
757eb2b0a61Sas static int
metamess(int meta,int f)7587c478bd9Sstevel@tonic-gate metamess(int meta, int f)
7597c478bd9Sstevel@tonic-gate {
760*46d64c14SToomas Soome 	int c, m;
761*46d64c14SToomas Soome 	struct message *mp;
7627c478bd9Sstevel@tonic-gate 
7637c478bd9Sstevel@tonic-gate 	c = meta;
7647c478bd9Sstevel@tonic-gate 	switch (c) {
7657c478bd9Sstevel@tonic-gate 	case '^':
7667c478bd9Sstevel@tonic-gate 		/*
7677c478bd9Sstevel@tonic-gate 		 * First 'good' message left.
7687c478bd9Sstevel@tonic-gate 		 */
7697c478bd9Sstevel@tonic-gate 		for (mp = &message[0]; mp < &message[msgCount]; mp++)
7707c478bd9Sstevel@tonic-gate 			if ((mp->m_flag & MDELETED) == f)
771eb2b0a61Sas 				return (mp - &message[0] + 1);
7727c478bd9Sstevel@tonic-gate 		printf(gettext("No applicable messages\n"));
773eb2b0a61Sas 		return (-1);
7747c478bd9Sstevel@tonic-gate 
7757c478bd9Sstevel@tonic-gate 	case '+':
7767c478bd9Sstevel@tonic-gate 		/*
7777c478bd9Sstevel@tonic-gate 		 * Next 'good' message left.
7787c478bd9Sstevel@tonic-gate 		 */
7797c478bd9Sstevel@tonic-gate 		for (mp = dot + 1; mp < &message[msgCount]; mp++)
7807c478bd9Sstevel@tonic-gate 			if ((mp->m_flag & MDELETED) == f)
781eb2b0a61Sas 				return (mp - &message[0] + 1);
7827c478bd9Sstevel@tonic-gate 		printf(gettext("Referencing beyond last message\n"));
783eb2b0a61Sas 		return (-1);
7847c478bd9Sstevel@tonic-gate 
7857c478bd9Sstevel@tonic-gate 	case '-':
7867c478bd9Sstevel@tonic-gate 		/*
7877c478bd9Sstevel@tonic-gate 		 * Previous 'good' message.
7887c478bd9Sstevel@tonic-gate 		 */
7897c478bd9Sstevel@tonic-gate 		for (mp = dot - 1; mp >= &message[0]; mp--)
7907c478bd9Sstevel@tonic-gate 			if ((mp->m_flag & MDELETED) == f)
791eb2b0a61Sas 				return (mp - &message[0] + 1);
7927c478bd9Sstevel@tonic-gate 		printf(gettext("Referencing before first message\n"));
793eb2b0a61Sas 		return (-1);
7947c478bd9Sstevel@tonic-gate 
7957c478bd9Sstevel@tonic-gate 	case '$':
7967c478bd9Sstevel@tonic-gate 		/*
7977c478bd9Sstevel@tonic-gate 		 * Last 'good message left.
7987c478bd9Sstevel@tonic-gate 		 */
7997c478bd9Sstevel@tonic-gate 		for (mp = &message[msgCount-1]; mp >= &message[0]; mp--)
8007c478bd9Sstevel@tonic-gate 			if ((mp->m_flag & MDELETED) == f)
801eb2b0a61Sas 				return (mp - &message[0] + 1);
8027c478bd9Sstevel@tonic-gate 		printf(gettext("No applicable messages\n"));
803eb2b0a61Sas 		return (-1);
8047c478bd9Sstevel@tonic-gate 
8057c478bd9Sstevel@tonic-gate 	case '.':
806eb2b0a61Sas 		/*
8077c478bd9Sstevel@tonic-gate 		 * Current message.
8087c478bd9Sstevel@tonic-gate 		 */
8097c478bd9Sstevel@tonic-gate 		m = dot - &message[0] + 1;
8107c478bd9Sstevel@tonic-gate 		if ((dot->m_flag & MDELETED) != f) {
8117c478bd9Sstevel@tonic-gate 			printf(gettext("%d: Inappropriate message\n"), m);
812eb2b0a61Sas 			return (-1);
8137c478bd9Sstevel@tonic-gate 		}
814eb2b0a61Sas 		return (m);
8157c478bd9Sstevel@tonic-gate 
8167c478bd9Sstevel@tonic-gate 	default:
8177c478bd9Sstevel@tonic-gate 		printf(gettext("Unknown metachar (%c)\n"), c);
818eb2b0a61Sas 		return (-1);
8197c478bd9Sstevel@tonic-gate 	}
8207c478bd9Sstevel@tonic-gate }
821