xref: /onnv-gate/usr/src/cmd/mailx/list.c (revision 3268:b143e3d8063d)
10Sstevel@tonic-gate /*
20Sstevel@tonic-gate  * CDDL HEADER START
30Sstevel@tonic-gate  *
40Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
52931Sas145665  * Common Development and Distribution License (the "License").
62931Sas145665  * You may not use this file except in compliance with the License.
70Sstevel@tonic-gate  *
80Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
90Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
100Sstevel@tonic-gate  * See the License for the specific language governing permissions
110Sstevel@tonic-gate  * and limitations under the License.
120Sstevel@tonic-gate  *
130Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
140Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
150Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
160Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
170Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
180Sstevel@tonic-gate  *
190Sstevel@tonic-gate  * CDDL HEADER END
200Sstevel@tonic-gate  */
210Sstevel@tonic-gate 
220Sstevel@tonic-gate /*
232931Sas145665  * Copyright 2006 Sun Microsystems, Inc.  All rights reserved.
242931Sas145665  * Use is subject to license terms.
250Sstevel@tonic-gate  */
260Sstevel@tonic-gate 
272931Sas145665 /* Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T */
282931Sas145665 /* All Rights Reserved   */
292931Sas145665 
300Sstevel@tonic-gate /*
310Sstevel@tonic-gate  * University Copyright- Copyright (c) 1982, 1986, 1988
320Sstevel@tonic-gate  * The Regents of the University of California
330Sstevel@tonic-gate  * All Rights Reserved
340Sstevel@tonic-gate  *
350Sstevel@tonic-gate  * University Acknowledgment- Portions of this document are derived from
360Sstevel@tonic-gate  * software developed by the University of California, Berkeley, and its
370Sstevel@tonic-gate  * contributors.
380Sstevel@tonic-gate  */
390Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
400Sstevel@tonic-gate 
410Sstevel@tonic-gate #include "rcv.h"
420Sstevel@tonic-gate #include <locale.h>
43*3268Sas145665 #include <stdlib.h>
44*3268Sas145665 #include <string.h>
450Sstevel@tonic-gate 
460Sstevel@tonic-gate /*
470Sstevel@tonic-gate  * mailx -- a modified version of a University of California at Berkeley
480Sstevel@tonic-gate  *	mail program
490Sstevel@tonic-gate  *
500Sstevel@tonic-gate  * Message list handling.
510Sstevel@tonic-gate  */
520Sstevel@tonic-gate 
530Sstevel@tonic-gate static int	check(int mesg, int f);
540Sstevel@tonic-gate static int	evalcol(int col);
55*3268Sas145665 static int	isinteger(char *buf);
560Sstevel@tonic-gate static void	mark(int mesg);
570Sstevel@tonic-gate static int	markall(char buf[], int f);
580Sstevel@tonic-gate static int	matchsubj(char *str, int mesg);
590Sstevel@tonic-gate static int	metamess(int meta, int f);
600Sstevel@tonic-gate static void	regret(int token);
610Sstevel@tonic-gate static int	scan(char **sp);
620Sstevel@tonic-gate static void	scaninit(void);
630Sstevel@tonic-gate static int	sender(char *str, int mesg);
640Sstevel@tonic-gate static void	unmark(int mesg);
650Sstevel@tonic-gate 
660Sstevel@tonic-gate /*
672931Sas145665  * Process message operand list.
682931Sas145665  * Convert the user string of message numbers and
692931Sas145665  * store the numbers into vector.
702931Sas145665  *
712931Sas145665  * Returns the count of messages picked up or -1 on error.
722931Sas145665  */
732931Sas145665 int
getmessage(char * buf,int * vector,int flags)742931Sas145665 getmessage(char *buf, int *vector, int flags)
752931Sas145665 {
762931Sas145665 	register int *ip;
772931Sas145665 	register struct message *mp;
78*3268Sas145665 	int firstmsg = -1;
79*3268Sas145665 	char delims[] = "\t- ";
80*3268Sas145665 	char *result  = NULL;
812931Sas145665 
822931Sas145665 	if (markall(buf, flags) < 0)
832931Sas145665 		return (-1);
842931Sas145665 	ip = vector;
852931Sas145665 
862931Sas145665 	/*
872931Sas145665 	 * Check for first message number and make sure it is
882931Sas145665 	 * at the beginning of the vector.
892931Sas145665 	 */
90*3268Sas145665 	result = strtok(buf, delims);
91*3268Sas145665 	if (result != NULL && isinteger(result)) {
92*3268Sas145665 		firstmsg = atoi(result);
932931Sas145665 		*ip++ = firstmsg;
942931Sas145665 	}
952931Sas145665 
962931Sas145665 	/*
972931Sas145665 	 * Add marked messages to vector and skip first
982931Sas145665 	 * message number because it is already at the
992931Sas145665 	 * beginning of the vector
1002931Sas145665 	 */
1012931Sas145665 	for (mp = &message[0]; mp < &message[msgCount]; mp++) {
1022931Sas145665 		if (firstmsg == mp - &message[0] + 1)
1032931Sas145665 			continue;
1042931Sas145665 		if (mp->m_flag & MMARK)
1052931Sas145665 			*ip++ = mp - &message[0] + 1;
1062931Sas145665 	}
1072931Sas145665 	*ip = NULL;
1082931Sas145665 	return (ip - vector);
1092931Sas145665 }
1102931Sas145665 
1112931Sas145665 /*
112*3268Sas145665  * Check to see if string is an integer
113*3268Sas145665  *
114*3268Sas145665  * Returns 1 if is an integer and 0 if it is not
115*3268Sas145665  */
116*3268Sas145665 static int
isinteger(char * buf)117*3268Sas145665 isinteger(char *buf)
118*3268Sas145665 {
119*3268Sas145665 	int i, result = 1;
120*3268Sas145665 
121*3268Sas145665 	/* check for empty string */
122*3268Sas145665 	if (strcmp(buf, "") == 0) {
123*3268Sas145665 		result = 0;
124*3268Sas145665 		return (result);
125*3268Sas145665 	}
126*3268Sas145665 
127*3268Sas145665 	i = 0;
128*3268Sas145665 	while (buf[i] != '\0') {
129*3268Sas145665 		if (!isdigit(buf[i])) {
130*3268Sas145665 			result = 0;
131*3268Sas145665 			break;
132*3268Sas145665 		}
133*3268Sas145665 		i++;
134*3268Sas145665 	}
135*3268Sas145665 	return (result);
136*3268Sas145665 }
137*3268Sas145665 
138*3268Sas145665 /*
1392931Sas145665  * Process msglist operand list.
1400Sstevel@tonic-gate  * Convert the user string of message numbers and
1410Sstevel@tonic-gate  * store the numbers into vector.
1420Sstevel@tonic-gate  *
1430Sstevel@tonic-gate  * Returns the count of messages picked up or -1 on error.
1440Sstevel@tonic-gate  */
1450Sstevel@tonic-gate 
1462931Sas145665 int
getmsglist(char * buf,int * vector,int flags)1470Sstevel@tonic-gate getmsglist(char *buf, int *vector, int flags)
1480Sstevel@tonic-gate {
1490Sstevel@tonic-gate 	register int *ip;
1500Sstevel@tonic-gate 	register struct message *mp;
1510Sstevel@tonic-gate 
1520Sstevel@tonic-gate 	if (markall(buf, flags) < 0)
1532931Sas145665 		return (-1);
1540Sstevel@tonic-gate 	ip = vector;
1550Sstevel@tonic-gate 	for (mp = &message[0]; mp < &message[msgCount]; mp++)
1560Sstevel@tonic-gate 		if (mp->m_flag & MMARK)
1570Sstevel@tonic-gate 			*ip++ = mp - &message[0] + 1;
1580Sstevel@tonic-gate 	*ip = NULL;
1592931Sas145665 	return (ip - vector);
1600Sstevel@tonic-gate }
1610Sstevel@tonic-gate 
1622931Sas145665 
1630Sstevel@tonic-gate /*
1640Sstevel@tonic-gate  * Mark all messages that the user wanted from the command
1650Sstevel@tonic-gate  * line in the message structure.  Return 0 on success, -1
1660Sstevel@tonic-gate  * on error.
1670Sstevel@tonic-gate  */
1680Sstevel@tonic-gate 
1690Sstevel@tonic-gate /*
1700Sstevel@tonic-gate  * Bit values for colon modifiers.
1710Sstevel@tonic-gate  */
1720Sstevel@tonic-gate 
1730Sstevel@tonic-gate #define	CMNEW		01		/* New messages */
1740Sstevel@tonic-gate #define	CMOLD		02		/* Old messages */
1750Sstevel@tonic-gate #define	CMUNREAD	04		/* Unread messages */
1760Sstevel@tonic-gate #define	CMDELETED	010		/* Deleted messages */
1770Sstevel@tonic-gate #define	CMREAD		020		/* Read messages */
1780Sstevel@tonic-gate 
1790Sstevel@tonic-gate /*
1800Sstevel@tonic-gate  * The following table describes the letters which can follow
1810Sstevel@tonic-gate  * the colon and gives the corresponding modifier bit.
1820Sstevel@tonic-gate  */
1830Sstevel@tonic-gate 
1840Sstevel@tonic-gate static struct coltab {
1850Sstevel@tonic-gate 	char	co_char;		/* What to find past : */
1860Sstevel@tonic-gate 	int	co_bit;			/* Associated modifier bit */
1870Sstevel@tonic-gate 	int	co_mask;		/* m_status bits to mask */
1880Sstevel@tonic-gate 	int	co_equal;		/* ... must equal this */
1890Sstevel@tonic-gate } coltab[] = {
1900Sstevel@tonic-gate 	'n',		CMNEW,		MNEW,		MNEW,
1910Sstevel@tonic-gate 	'o',		CMOLD,		MNEW,		0,
1920Sstevel@tonic-gate 	'u',		CMUNREAD,	MREAD,		0,
1930Sstevel@tonic-gate 	'd',		CMDELETED,	MDELETED,	MDELETED,
1940Sstevel@tonic-gate 	'r',		CMREAD,		MREAD,		MREAD,
1950Sstevel@tonic-gate 	0,		0,		0,		0
1960Sstevel@tonic-gate };
1970Sstevel@tonic-gate 
1980Sstevel@tonic-gate static	int	lastcolmod;
1990Sstevel@tonic-gate 
2002931Sas145665 static int
markall(char buf[],int f)2010Sstevel@tonic-gate markall(char buf[], int f)
2020Sstevel@tonic-gate {
2030Sstevel@tonic-gate 	register char **np;
2040Sstevel@tonic-gate 	register int i;
2050Sstevel@tonic-gate 	register struct message *mp;
2060Sstevel@tonic-gate 	char *namelist[NMLSIZE], *bufp;
2070Sstevel@tonic-gate 	int tok, beg, mc, star, other, colmod, colresult;
2080Sstevel@tonic-gate 
2090Sstevel@tonic-gate 	colmod = 0;
2100Sstevel@tonic-gate 	for (i = 1; i <= msgCount; i++)
2110Sstevel@tonic-gate 		unmark(i);
2120Sstevel@tonic-gate 	bufp = buf;
2130Sstevel@tonic-gate 	mc = 0;
2140Sstevel@tonic-gate 	np = &namelist[0];
2150Sstevel@tonic-gate 	scaninit();
2160Sstevel@tonic-gate 	tok = scan(&bufp);
2170Sstevel@tonic-gate 	star = 0;
2180Sstevel@tonic-gate 	other = 0;
2190Sstevel@tonic-gate 	beg = 0;
2200Sstevel@tonic-gate 	while (tok != TEOL) {
2210Sstevel@tonic-gate 		switch (tok) {
2220Sstevel@tonic-gate 		case TNUMBER:
2230Sstevel@tonic-gate number:
2240Sstevel@tonic-gate 			if (star) {
2250Sstevel@tonic-gate 				printf(gettext("No numbers mixed with *\n"));
2262931Sas145665 				return (-1);
2270Sstevel@tonic-gate 			}
2280Sstevel@tonic-gate 			mc++;
2290Sstevel@tonic-gate 			other++;
2300Sstevel@tonic-gate 			if (beg != 0) {
2310Sstevel@tonic-gate 				if (check(lexnumber, f))
2322931Sas145665 					return (-1);
2330Sstevel@tonic-gate 				for (i = beg; i <= lexnumber; i++)
2340Sstevel@tonic-gate 					if ((message[i-1].m_flag&MDELETED) == f)
2350Sstevel@tonic-gate 						mark(i);
2360Sstevel@tonic-gate 				beg = 0;
2370Sstevel@tonic-gate 				break;
2380Sstevel@tonic-gate 			}
2390Sstevel@tonic-gate 			beg = lexnumber;
2400Sstevel@tonic-gate 			if (check(beg, f))
2412931Sas145665 				return (-1);
2420Sstevel@tonic-gate 			tok = scan(&bufp);
2430Sstevel@tonic-gate 			if (tok != TDASH) {
2440Sstevel@tonic-gate 				regret(tok);
2450Sstevel@tonic-gate 				mark(beg);
2460Sstevel@tonic-gate 				beg = 0;
2470Sstevel@tonic-gate 			}
2480Sstevel@tonic-gate 			break;
2490Sstevel@tonic-gate 
2500Sstevel@tonic-gate 		case TSTRING:
2510Sstevel@tonic-gate 			if (beg != 0) {
2520Sstevel@tonic-gate 				printf(gettext(
2530Sstevel@tonic-gate 				    "Non-numeric second argument\n"));
2542931Sas145665 				return (-1);
2550Sstevel@tonic-gate 			}
2560Sstevel@tonic-gate 			other++;
2570Sstevel@tonic-gate 			if (lexstring[0] == ':') {
2580Sstevel@tonic-gate 				colresult = evalcol(lexstring[1]);
2590Sstevel@tonic-gate 				if (colresult == 0) {
2600Sstevel@tonic-gate 					printf(gettext(
2610Sstevel@tonic-gate 					    "Unknown colon modifier \"%s\"\n"),
2620Sstevel@tonic-gate 					    lexstring);
2632931Sas145665 					return (-1);
2640Sstevel@tonic-gate 				}
2650Sstevel@tonic-gate 				colmod |= colresult;
2660Sstevel@tonic-gate 			}
2670Sstevel@tonic-gate 			else
2680Sstevel@tonic-gate 				*np++ = savestr(lexstring);
2690Sstevel@tonic-gate 			break;
2700Sstevel@tonic-gate 
2710Sstevel@tonic-gate 		case TDASH:
2720Sstevel@tonic-gate 		case TPLUS:
2730Sstevel@tonic-gate 		case TDOLLAR:
2740Sstevel@tonic-gate 		case TUP:
2750Sstevel@tonic-gate 		case TDOT:
2760Sstevel@tonic-gate 			lexnumber = metamess(lexstring[0], f);
2770Sstevel@tonic-gate 			if (lexnumber == -1)
2782931Sas145665 				return (-1);
2790Sstevel@tonic-gate 			goto number;
2800Sstevel@tonic-gate 
2810Sstevel@tonic-gate 		case TSTAR:
2820Sstevel@tonic-gate 			if (other) {
2830Sstevel@tonic-gate 				printf(gettext(
2840Sstevel@tonic-gate 				    "Can't mix \"*\" with anything\n"));
2852931Sas145665 				return (-1);
2860Sstevel@tonic-gate 			}
2870Sstevel@tonic-gate 			star++;
2880Sstevel@tonic-gate 			break;
2890Sstevel@tonic-gate 		}
2900Sstevel@tonic-gate 		tok = scan(&bufp);
2910Sstevel@tonic-gate 	}
2920Sstevel@tonic-gate 	lastcolmod = colmod;
2930Sstevel@tonic-gate 	*np = NOSTR;
2940Sstevel@tonic-gate 	mc = 0;
2950Sstevel@tonic-gate 	if (star) {
2960Sstevel@tonic-gate 		for (i = 0; i < msgCount; i++)
2970Sstevel@tonic-gate 			if ((message[i].m_flag & MDELETED) == f) {
2980Sstevel@tonic-gate 				mark(i+1);
2990Sstevel@tonic-gate 				mc++;
3000Sstevel@tonic-gate 			}
3010Sstevel@tonic-gate 		if (mc == 0) {
3020Sstevel@tonic-gate 			printf(gettext("No applicable messages\n"));
3032931Sas145665 			return (-1);
3040Sstevel@tonic-gate 		}
3052931Sas145665 		return (0);
3060Sstevel@tonic-gate 	}
3070Sstevel@tonic-gate 
3080Sstevel@tonic-gate 	/*
3090Sstevel@tonic-gate 	 * If no numbers were given, mark all of the messages,
3100Sstevel@tonic-gate 	 * so that we can unmark any whose sender was not selected
3110Sstevel@tonic-gate 	 * if any user names were given.
3120Sstevel@tonic-gate 	 */
3130Sstevel@tonic-gate 
3140Sstevel@tonic-gate 	if ((np > namelist || colmod != 0) && mc == 0)
3150Sstevel@tonic-gate 		for (i = 1; i <= msgCount; i++)
3160Sstevel@tonic-gate 			if ((message[i-1].m_flag & MDELETED) == f)
3170Sstevel@tonic-gate 				mark(i);
3180Sstevel@tonic-gate 
3190Sstevel@tonic-gate 	/*
3200Sstevel@tonic-gate 	 * If any names were given, go through and eliminate any
3210Sstevel@tonic-gate 	 * messages whose senders were not requested.
3220Sstevel@tonic-gate 	 */
3230Sstevel@tonic-gate 
3240Sstevel@tonic-gate 	if (np > namelist) {
3250Sstevel@tonic-gate 		for (i = 1; i <= msgCount; i++) {
3260Sstevel@tonic-gate 			for (mc = 0, np = &namelist[0]; *np != NOSTR; np++)
3270Sstevel@tonic-gate 				if (**np == '/') {
3280Sstevel@tonic-gate 					if (matchsubj(*np, i)) {
3290Sstevel@tonic-gate 						mc++;
3300Sstevel@tonic-gate 						break;
3310Sstevel@tonic-gate 					}
3322931Sas145665 				} else {
3330Sstevel@tonic-gate 					if (sender(*np, i)) {
3340Sstevel@tonic-gate 						mc++;
3350Sstevel@tonic-gate 						break;
3360Sstevel@tonic-gate 					}
3370Sstevel@tonic-gate 				}
3380Sstevel@tonic-gate 			if (mc == 0)
3390Sstevel@tonic-gate 				unmark(i);
3400Sstevel@tonic-gate 		}
3410Sstevel@tonic-gate 
3420Sstevel@tonic-gate 		/*
3430Sstevel@tonic-gate 		 * Make sure we got some decent messages.
3440Sstevel@tonic-gate 		 */
3450Sstevel@tonic-gate 
3460Sstevel@tonic-gate 		mc = 0;
3470Sstevel@tonic-gate 		for (i = 1; i <= msgCount; i++)
3480Sstevel@tonic-gate 			if (message[i-1].m_flag & MMARK) {
3490Sstevel@tonic-gate 				mc++;
3500Sstevel@tonic-gate 				break;
3510Sstevel@tonic-gate 			}
3520Sstevel@tonic-gate 		if (mc == 0) {
3530Sstevel@tonic-gate 			printf(gettext("No applicable messages from {%s"),
3540Sstevel@tonic-gate namelist[0]);
3550Sstevel@tonic-gate 			for (np = &namelist[1]; *np != NOSTR; np++)
3560Sstevel@tonic-gate 				printf(", %s", *np);
3570Sstevel@tonic-gate 			printf("}\n");
3582931Sas145665 			return (-1);
3590Sstevel@tonic-gate 		}
3600Sstevel@tonic-gate 	}
3610Sstevel@tonic-gate 
3620Sstevel@tonic-gate 	/*
3630Sstevel@tonic-gate 	 * If any colon modifiers were given, go through and
3640Sstevel@tonic-gate 	 * unmark any messages which do not satisfy the modifiers.
3650Sstevel@tonic-gate 	 */
3660Sstevel@tonic-gate 
3670Sstevel@tonic-gate 	if (colmod != 0) {
3680Sstevel@tonic-gate 		for (i = 1; i <= msgCount; i++) {
3690Sstevel@tonic-gate 			register struct coltab *colp;
3700Sstevel@tonic-gate 
3710Sstevel@tonic-gate 			mp = &message[i - 1];
3720Sstevel@tonic-gate 			for (colp = &coltab[0]; colp->co_char; colp++)
3730Sstevel@tonic-gate 				if (colp->co_bit & colmod)
3740Sstevel@tonic-gate 					if ((mp->m_flag & colp->co_mask)
3750Sstevel@tonic-gate 					    != colp->co_equal)
3760Sstevel@tonic-gate 						unmark(i);
3770Sstevel@tonic-gate 
3780Sstevel@tonic-gate 		}
3790Sstevel@tonic-gate 		for (mp = &message[0]; mp < &message[msgCount]; mp++)
3800Sstevel@tonic-gate 			if (mp->m_flag & MMARK)
3810Sstevel@tonic-gate 				break;
3820Sstevel@tonic-gate 		if (mp >= &message[msgCount]) {
3830Sstevel@tonic-gate 			register struct coltab *colp;
3840Sstevel@tonic-gate 
3850Sstevel@tonic-gate 			printf(gettext("No messages satisfy"));
3860Sstevel@tonic-gate 			for (colp = &coltab[0]; colp->co_char; colp++)
3870Sstevel@tonic-gate 				if (colp->co_bit & colmod)
3880Sstevel@tonic-gate 					printf(" :%c", colp->co_char);
3890Sstevel@tonic-gate 			printf("\n");
3902931Sas145665 			return (-1);
3910Sstevel@tonic-gate 		}
3920Sstevel@tonic-gate 	}
3932931Sas145665 	return (0);
3940Sstevel@tonic-gate }
3950Sstevel@tonic-gate 
3960Sstevel@tonic-gate /*
3970Sstevel@tonic-gate  * Turn the character after a colon modifier into a bit
3980Sstevel@tonic-gate  * value.
3990Sstevel@tonic-gate  */
4002931Sas145665 static int
evalcol(int col)4010Sstevel@tonic-gate evalcol(int col)
4020Sstevel@tonic-gate {
4030Sstevel@tonic-gate 	register struct coltab *colp;
4040Sstevel@tonic-gate 
4050Sstevel@tonic-gate 	if (col == 0)
4062931Sas145665 		return (lastcolmod);
4070Sstevel@tonic-gate 	for (colp = &coltab[0]; colp->co_char; colp++)
4080Sstevel@tonic-gate 		if (colp->co_char == col)
4092931Sas145665 			return (colp->co_bit);
4102931Sas145665 	return (0);
4110Sstevel@tonic-gate }
4120Sstevel@tonic-gate 
4130Sstevel@tonic-gate /*
4140Sstevel@tonic-gate  * Check the passed message number for legality and proper flags.
4150Sstevel@tonic-gate  */
4162931Sas145665 static int
check(int mesg,int f)4170Sstevel@tonic-gate check(int mesg, int f)
4180Sstevel@tonic-gate {
4190Sstevel@tonic-gate 	register struct message *mp;
4200Sstevel@tonic-gate 
4210Sstevel@tonic-gate 	if (mesg < 1 || mesg > msgCount) {
4220Sstevel@tonic-gate 		printf(gettext("%d: Invalid message number\n"), mesg);
4232931Sas145665 		return (-1);
4240Sstevel@tonic-gate 	}
4250Sstevel@tonic-gate 	mp = &message[mesg-1];
4260Sstevel@tonic-gate 	if ((mp->m_flag & MDELETED) != f) {
4270Sstevel@tonic-gate 		printf(gettext("%d: Inappropriate message\n"), mesg);
4282931Sas145665 		return (-1);
4290Sstevel@tonic-gate 	}
4302931Sas145665 	return (0);
4310Sstevel@tonic-gate }
4320Sstevel@tonic-gate 
4330Sstevel@tonic-gate /*
4340Sstevel@tonic-gate  * Scan out the list of string arguments, shell style
4350Sstevel@tonic-gate  * for a RAWLIST.
4360Sstevel@tonic-gate  */
4370Sstevel@tonic-gate 
4382931Sas145665 int
getrawlist(char line[],char ** argv,int argc)4390Sstevel@tonic-gate getrawlist(char line[], char **argv, int argc)
4400Sstevel@tonic-gate {
4410Sstevel@tonic-gate 	register char **ap, *cp, *cp2;
4420Sstevel@tonic-gate 	char linebuf[LINESIZE], quotec;
4430Sstevel@tonic-gate 	register char **last;
4440Sstevel@tonic-gate 
4450Sstevel@tonic-gate 	ap = argv;
4460Sstevel@tonic-gate 	cp = line;
4470Sstevel@tonic-gate 	last = argv + argc - 1;
4480Sstevel@tonic-gate 	while (*cp != '\0') {
4490Sstevel@tonic-gate 		while (any(*cp, " \t"))
4500Sstevel@tonic-gate 			cp++;
4510Sstevel@tonic-gate 		cp2 = linebuf;
4520Sstevel@tonic-gate 		quotec = 0;
4530Sstevel@tonic-gate 		while (*cp != '\0') {
4540Sstevel@tonic-gate 			if (quotec) {
4550Sstevel@tonic-gate 				if (*cp == quotec) {
4562931Sas145665 					quotec = 0;
4570Sstevel@tonic-gate 					cp++;
4580Sstevel@tonic-gate 				} else
4590Sstevel@tonic-gate 					*cp2++ = *cp++;
4600Sstevel@tonic-gate 			} else {
4610Sstevel@tonic-gate 				if (*cp == '\\') {
4620Sstevel@tonic-gate 					if (*(cp+1) != '\0') {
4630Sstevel@tonic-gate 						*cp2++ = *++cp;
4640Sstevel@tonic-gate 						cp++;
4650Sstevel@tonic-gate 					} else {
4660Sstevel@tonic-gate 						printf(gettext(
4672931Sas145665 						    "Trailing \\; ignoring\n"));
4680Sstevel@tonic-gate 						break;
4690Sstevel@tonic-gate 					}
4700Sstevel@tonic-gate 				}
4710Sstevel@tonic-gate 				if (any(*cp, " \t"))
4720Sstevel@tonic-gate 					break;
4730Sstevel@tonic-gate 				if (any(*cp, "'\""))
4740Sstevel@tonic-gate 					quotec = *cp++;
4750Sstevel@tonic-gate 				else
4760Sstevel@tonic-gate 					*cp2++ = *cp++;
4770Sstevel@tonic-gate 			}
4780Sstevel@tonic-gate 		}
4790Sstevel@tonic-gate 		*cp2 = '\0';
4800Sstevel@tonic-gate 		if (cp2 == linebuf)
4810Sstevel@tonic-gate 			break;
4820Sstevel@tonic-gate 		if (ap >= last) {
4832931Sas145665 			printf(gettext("Too many elements in the list;"
4842931Sas145665 			    " excess discarded\n"));
4850Sstevel@tonic-gate 			break;
4860Sstevel@tonic-gate 		}
4870Sstevel@tonic-gate 		*ap++ = savestr(linebuf);
4880Sstevel@tonic-gate 	}
4890Sstevel@tonic-gate 	*ap = NOSTR;
4902931Sas145665 	return (ap-argv);
4910Sstevel@tonic-gate }
4920Sstevel@tonic-gate 
4930Sstevel@tonic-gate /*
4940Sstevel@tonic-gate  * scan out a single lexical item and return its token number,
4950Sstevel@tonic-gate  * updating the string pointer passed **p.  Also, store the value
4960Sstevel@tonic-gate  * of the number or string scanned in lexnumber or lexstring as
4970Sstevel@tonic-gate  * appropriate.  In any event, store the scanned `thing' in lexstring.
4980Sstevel@tonic-gate  */
4990Sstevel@tonic-gate 
5000Sstevel@tonic-gate static struct lex {
5010Sstevel@tonic-gate 	char	l_char;
5020Sstevel@tonic-gate 	char	l_token;
5030Sstevel@tonic-gate } singles[] = {
5040Sstevel@tonic-gate 	'$',	TDOLLAR,
5050Sstevel@tonic-gate 	'.',	TDOT,
5060Sstevel@tonic-gate 	'^',	TUP,
5070Sstevel@tonic-gate 	'*',	TSTAR,
5080Sstevel@tonic-gate 	'-',	TDASH,
5090Sstevel@tonic-gate 	'+',	TPLUS,
5100Sstevel@tonic-gate 	'(',	TOPEN,
5110Sstevel@tonic-gate 	')',	TCLOSE,
5120Sstevel@tonic-gate 	0,	0
5130Sstevel@tonic-gate };
5140Sstevel@tonic-gate 
5152931Sas145665 static int
scan(char ** sp)5160Sstevel@tonic-gate scan(char **sp)
5170Sstevel@tonic-gate {
5180Sstevel@tonic-gate 	register char *cp, *cp2;
5190Sstevel@tonic-gate 	register char c;
5200Sstevel@tonic-gate 	register struct lex *lp;
5210Sstevel@tonic-gate 	int quotec;
5220Sstevel@tonic-gate 
5230Sstevel@tonic-gate 	if (regretp >= 0) {
5240Sstevel@tonic-gate 		copy(stringstack[regretp], lexstring);
5250Sstevel@tonic-gate 		lexnumber = numberstack[regretp];
5262931Sas145665 		return (regretstack[regretp--]);
5270Sstevel@tonic-gate 	}
5280Sstevel@tonic-gate 	cp = *sp;
5290Sstevel@tonic-gate 	cp2 = lexstring;
5300Sstevel@tonic-gate 	c = *cp++;
5310Sstevel@tonic-gate 
5320Sstevel@tonic-gate 	/*
5330Sstevel@tonic-gate 	 * strip away leading white space.
5340Sstevel@tonic-gate 	 */
5350Sstevel@tonic-gate 
5360Sstevel@tonic-gate 	while (any(c, " \t"))
5370Sstevel@tonic-gate 		c = *cp++;
5380Sstevel@tonic-gate 
5390Sstevel@tonic-gate 	/*
5400Sstevel@tonic-gate 	 * If no characters remain, we are at end of line,
5410Sstevel@tonic-gate 	 * so report that.
5420Sstevel@tonic-gate 	 */
5430Sstevel@tonic-gate 
5440Sstevel@tonic-gate 	if (c == '\0') {
5450Sstevel@tonic-gate 		*sp = --cp;
5462931Sas145665 		return (TEOL);
5470Sstevel@tonic-gate 	}
5480Sstevel@tonic-gate 
5490Sstevel@tonic-gate 	/*
5500Sstevel@tonic-gate 	 * If the leading character is a digit, scan
5510Sstevel@tonic-gate 	 * the number and convert it on the fly.
5520Sstevel@tonic-gate 	 * Return TNUMBER when done.
5530Sstevel@tonic-gate 	 */
5540Sstevel@tonic-gate 
5550Sstevel@tonic-gate 	if (isdigit(c)) {
5560Sstevel@tonic-gate 		lexnumber = 0;
5570Sstevel@tonic-gate 		while (isdigit(c)) {
5580Sstevel@tonic-gate 			lexnumber = lexnumber*10 + c - '0';
5590Sstevel@tonic-gate 			*cp2++ = c;
5600Sstevel@tonic-gate 			c = *cp++;
5610Sstevel@tonic-gate 		}
5620Sstevel@tonic-gate 		*cp2 = '\0';
5630Sstevel@tonic-gate 		*sp = --cp;
5642931Sas145665 		return (TNUMBER);
5650Sstevel@tonic-gate 	}
5660Sstevel@tonic-gate 
5670Sstevel@tonic-gate 	/*
5680Sstevel@tonic-gate 	 * Check for single character tokens; return such
5690Sstevel@tonic-gate 	 * if found.
5700Sstevel@tonic-gate 	 */
5710Sstevel@tonic-gate 
5720Sstevel@tonic-gate 	for (lp = &singles[0]; lp->l_char != 0; lp++)
5730Sstevel@tonic-gate 		if (c == lp->l_char) {
5740Sstevel@tonic-gate 			lexstring[0] = c;
5750Sstevel@tonic-gate 			lexstring[1] = '\0';
5760Sstevel@tonic-gate 			*sp = cp;
5772931Sas145665 			return (lp->l_token);
5780Sstevel@tonic-gate 		}
5790Sstevel@tonic-gate 
5800Sstevel@tonic-gate 	/*
5810Sstevel@tonic-gate 	 * We've got a string!  Copy all the characters
5820Sstevel@tonic-gate 	 * of the string into lexstring, until we see
5830Sstevel@tonic-gate 	 * a null, space, or tab.
5840Sstevel@tonic-gate 	 * If the lead character is a " or ', save it
5850Sstevel@tonic-gate 	 * and scan until you get another.
5860Sstevel@tonic-gate 	 */
5870Sstevel@tonic-gate 
5880Sstevel@tonic-gate 	quotec = 0;
5890Sstevel@tonic-gate 	if (any(c, "'\"")) {
5900Sstevel@tonic-gate 		quotec = c;
5910Sstevel@tonic-gate 		c = *cp++;
5920Sstevel@tonic-gate 	}
5930Sstevel@tonic-gate 	while (c != '\0') {
5940Sstevel@tonic-gate 		if (quotec == 0 && c == '\\') {
5950Sstevel@tonic-gate 			if (*cp != '\0') {
5960Sstevel@tonic-gate 				c = *cp++;
5970Sstevel@tonic-gate 			} else {
5980Sstevel@tonic-gate 				fprintf(stderr, gettext("Trailing \\; "
5990Sstevel@tonic-gate 				    "ignoring\n"));
6000Sstevel@tonic-gate 			}
6010Sstevel@tonic-gate 		}
6020Sstevel@tonic-gate 		if (c == quotec) {
6030Sstevel@tonic-gate 			cp++;
6040Sstevel@tonic-gate 			break;
6050Sstevel@tonic-gate 		}
6060Sstevel@tonic-gate 		if (quotec == 0 && any(c, " \t"))
6070Sstevel@tonic-gate 			break;
6080Sstevel@tonic-gate 		if (cp2 - lexstring < STRINGLEN-1)
6090Sstevel@tonic-gate 			*cp2++ = c;
6100Sstevel@tonic-gate 		c = *cp++;
6110Sstevel@tonic-gate 	}
6120Sstevel@tonic-gate 	if (quotec && c == 0)
6130Sstevel@tonic-gate 		fprintf(stderr, gettext("Missing %c\n"), quotec);
6140Sstevel@tonic-gate 	*sp = --cp;
6150Sstevel@tonic-gate 	*cp2 = '\0';
6162931Sas145665 	return (TSTRING);
6170Sstevel@tonic-gate }
6180Sstevel@tonic-gate 
6190Sstevel@tonic-gate /*
6200Sstevel@tonic-gate  * Unscan the named token by pushing it onto the regret stack.
6210Sstevel@tonic-gate  */
6220Sstevel@tonic-gate 
6232931Sas145665 static void
regret(int token)6240Sstevel@tonic-gate regret(int token)
6250Sstevel@tonic-gate {
6260Sstevel@tonic-gate 	if (++regretp >= REGDEP)
6270Sstevel@tonic-gate 		panic("Too many regrets");
6280Sstevel@tonic-gate 	regretstack[regretp] = token;
6290Sstevel@tonic-gate 	lexstring[STRINGLEN-1] = '\0';
6300Sstevel@tonic-gate 	stringstack[regretp] = savestr(lexstring);
6310Sstevel@tonic-gate 	numberstack[regretp] = lexnumber;
6320Sstevel@tonic-gate }
6330Sstevel@tonic-gate 
6340Sstevel@tonic-gate /*
6350Sstevel@tonic-gate  * Reset all the scanner global variables.
6360Sstevel@tonic-gate  */
6370Sstevel@tonic-gate 
6382931Sas145665 static void
scaninit(void)6390Sstevel@tonic-gate scaninit(void)
6400Sstevel@tonic-gate {
6410Sstevel@tonic-gate 	regretp = -1;
6420Sstevel@tonic-gate }
6430Sstevel@tonic-gate 
6440Sstevel@tonic-gate /*
6450Sstevel@tonic-gate  * Find the first message whose flags & m == f  and return
6460Sstevel@tonic-gate  * its message number.
6470Sstevel@tonic-gate  */
6480Sstevel@tonic-gate 
6492931Sas145665 int
first(int f,int m)6500Sstevel@tonic-gate first(int f, int m)
6510Sstevel@tonic-gate {
6520Sstevel@tonic-gate 	register int mesg;
6530Sstevel@tonic-gate 	register struct message *mp;
6540Sstevel@tonic-gate 
6550Sstevel@tonic-gate 	mesg = dot - &message[0] + 1;
6560Sstevel@tonic-gate 	f &= MDELETED;
6570Sstevel@tonic-gate 	m &= MDELETED;
6580Sstevel@tonic-gate 	for (mp = dot; mp < &message[msgCount]; mp++) {
6590Sstevel@tonic-gate 		if ((mp->m_flag & m) == f)
6602931Sas145665 			return (mesg);
6610Sstevel@tonic-gate 		mesg++;
6620Sstevel@tonic-gate 	}
6630Sstevel@tonic-gate 	mesg = dot - &message[0];
6640Sstevel@tonic-gate 	for (mp = dot-1; mp >= &message[0]; mp--) {
6650Sstevel@tonic-gate 		if ((mp->m_flag & m) == f)
6662931Sas145665 			return (mesg);
6670Sstevel@tonic-gate 		mesg--;
6680Sstevel@tonic-gate 	}
6692931Sas145665 	return (NULL);
6700Sstevel@tonic-gate }
6710Sstevel@tonic-gate 
6720Sstevel@tonic-gate /*
6730Sstevel@tonic-gate  * See if the passed name sent the passed message number.  Return true
6740Sstevel@tonic-gate  * if so.
6750Sstevel@tonic-gate  */
6762931Sas145665 static int
sender(char * str,int mesg)6770Sstevel@tonic-gate sender(char *str, int mesg)
6780Sstevel@tonic-gate {
6790Sstevel@tonic-gate 	return (samebody(str, skin(nameof(&message[mesg-1])), TRUE));
6800Sstevel@tonic-gate }
6810Sstevel@tonic-gate 
6820Sstevel@tonic-gate /*
6830Sstevel@tonic-gate  * See if the given string matches inside the subject field of the
6840Sstevel@tonic-gate  * given message.  For the purpose of the scan, we ignore case differences.
6850Sstevel@tonic-gate  * If it does, return true.  The string search argument is assumed to
6860Sstevel@tonic-gate  * have the form "/search-string."  If it is of the form "/," we use the
6870Sstevel@tonic-gate  * previous search string.
6880Sstevel@tonic-gate  */
6890Sstevel@tonic-gate 
6900Sstevel@tonic-gate static char lastscan[128];
6910Sstevel@tonic-gate 
6922931Sas145665 static int
matchsubj(char * str,int mesg)6930Sstevel@tonic-gate matchsubj(char *str, int mesg)
6940Sstevel@tonic-gate {
6950Sstevel@tonic-gate 	register struct message *mp;
6960Sstevel@tonic-gate 	register char *cp, *cp2, *backup;
6970Sstevel@tonic-gate 
6980Sstevel@tonic-gate 	str++;
6990Sstevel@tonic-gate 	if (strlen(str) == 0)
7000Sstevel@tonic-gate 		str = lastscan;
7010Sstevel@tonic-gate 	else
7020Sstevel@tonic-gate 		nstrcpy(lastscan, sizeof (lastscan), str);
7030Sstevel@tonic-gate 	mp = &message[mesg-1];
7040Sstevel@tonic-gate 
7050Sstevel@tonic-gate 	/*
7060Sstevel@tonic-gate 	 * Now look, ignoring case, for the word in the string.
7070Sstevel@tonic-gate 	 */
7080Sstevel@tonic-gate 
7090Sstevel@tonic-gate 	cp = str;
7100Sstevel@tonic-gate 	cp2 = hfield("subject", mp, addone);
7110Sstevel@tonic-gate 	if (cp2 == NOSTR)
7122931Sas145665 		return (0);
7130Sstevel@tonic-gate 	backup = cp2;
7140Sstevel@tonic-gate 	while (*cp2) {
7150Sstevel@tonic-gate 		if (*cp == 0)
7162931Sas145665 			return (1);
7170Sstevel@tonic-gate 		if (toupper(*cp++) != toupper(*cp2++)) {
7180Sstevel@tonic-gate 			cp2 = ++backup;
7190Sstevel@tonic-gate 			cp = str;
7200Sstevel@tonic-gate 		}
7210Sstevel@tonic-gate 	}
7222931Sas145665 	return (*cp == 0);
7230Sstevel@tonic-gate }
7240Sstevel@tonic-gate 
7250Sstevel@tonic-gate /*
7260Sstevel@tonic-gate  * Mark the named message by setting its mark bit.
7270Sstevel@tonic-gate  */
7280Sstevel@tonic-gate 
7292931Sas145665 static void
mark(int mesg)7300Sstevel@tonic-gate mark(int mesg)
7310Sstevel@tonic-gate {
7320Sstevel@tonic-gate 	register int i;
7330Sstevel@tonic-gate 
7340Sstevel@tonic-gate 	i = mesg;
7350Sstevel@tonic-gate 	if (i < 1 || i > msgCount)
7360Sstevel@tonic-gate 		panic("Bad message number to mark");
7370Sstevel@tonic-gate 	message[i-1].m_flag |= MMARK;
7380Sstevel@tonic-gate }
7390Sstevel@tonic-gate 
7400Sstevel@tonic-gate /*
7410Sstevel@tonic-gate  * Unmark the named message.
7420Sstevel@tonic-gate  */
7430Sstevel@tonic-gate 
7442931Sas145665 static void
unmark(int mesg)7450Sstevel@tonic-gate unmark(int mesg)
7460Sstevel@tonic-gate {
7470Sstevel@tonic-gate 	register int i;
7480Sstevel@tonic-gate 
7490Sstevel@tonic-gate 	i = mesg;
7500Sstevel@tonic-gate 	if (i < 1 || i > msgCount)
7510Sstevel@tonic-gate 		panic("Bad message number to unmark");
7520Sstevel@tonic-gate 	message[i-1].m_flag &= ~MMARK;
7530Sstevel@tonic-gate }
7540Sstevel@tonic-gate 
7550Sstevel@tonic-gate /*
7560Sstevel@tonic-gate  * Return the message number corresponding to the passed meta character.
7570Sstevel@tonic-gate  */
7582931Sas145665 static int
metamess(int meta,int f)7590Sstevel@tonic-gate metamess(int meta, int f)
7600Sstevel@tonic-gate {
7610Sstevel@tonic-gate 	register int c, m;
7620Sstevel@tonic-gate 	register struct message *mp;
7630Sstevel@tonic-gate 
7640Sstevel@tonic-gate 	c = meta;
7650Sstevel@tonic-gate 	switch (c) {
7660Sstevel@tonic-gate 	case '^':
7670Sstevel@tonic-gate 		/*
7680Sstevel@tonic-gate 		 * First 'good' message left.
7690Sstevel@tonic-gate 		 */
7700Sstevel@tonic-gate 		for (mp = &message[0]; mp < &message[msgCount]; mp++)
7710Sstevel@tonic-gate 			if ((mp->m_flag & MDELETED) == f)
7722931Sas145665 				return (mp - &message[0] + 1);
7730Sstevel@tonic-gate 		printf(gettext("No applicable messages\n"));
7742931Sas145665 		return (-1);
7750Sstevel@tonic-gate 
7760Sstevel@tonic-gate 	case '+':
7770Sstevel@tonic-gate 		/*
7780Sstevel@tonic-gate 		 * Next 'good' message left.
7790Sstevel@tonic-gate 		 */
7800Sstevel@tonic-gate 		for (mp = dot + 1; mp < &message[msgCount]; mp++)
7810Sstevel@tonic-gate 			if ((mp->m_flag & MDELETED) == f)
7822931Sas145665 				return (mp - &message[0] + 1);
7830Sstevel@tonic-gate 		printf(gettext("Referencing beyond last message\n"));
7842931Sas145665 		return (-1);
7850Sstevel@tonic-gate 
7860Sstevel@tonic-gate 	case '-':
7870Sstevel@tonic-gate 		/*
7880Sstevel@tonic-gate 		 * Previous 'good' message.
7890Sstevel@tonic-gate 		 */
7900Sstevel@tonic-gate 		for (mp = dot - 1; mp >= &message[0]; mp--)
7910Sstevel@tonic-gate 			if ((mp->m_flag & MDELETED) == f)
7922931Sas145665 				return (mp - &message[0] + 1);
7930Sstevel@tonic-gate 		printf(gettext("Referencing before first message\n"));
7942931Sas145665 		return (-1);
7950Sstevel@tonic-gate 
7960Sstevel@tonic-gate 	case '$':
7970Sstevel@tonic-gate 		/*
7980Sstevel@tonic-gate 		 * Last 'good message left.
7990Sstevel@tonic-gate 		 */
8000Sstevel@tonic-gate 		for (mp = &message[msgCount-1]; mp >= &message[0]; mp--)
8010Sstevel@tonic-gate 			if ((mp->m_flag & MDELETED) == f)
8022931Sas145665 				return (mp - &message[0] + 1);
8030Sstevel@tonic-gate 		printf(gettext("No applicable messages\n"));
8042931Sas145665 		return (-1);
8050Sstevel@tonic-gate 
8060Sstevel@tonic-gate 	case '.':
8072931Sas145665 		/*
8080Sstevel@tonic-gate 		 * Current message.
8090Sstevel@tonic-gate 		 */
8100Sstevel@tonic-gate 		m = dot - &message[0] + 1;
8110Sstevel@tonic-gate 		if ((dot->m_flag & MDELETED) != f) {
8120Sstevel@tonic-gate 			printf(gettext("%d: Inappropriate message\n"), m);
8132931Sas145665 			return (-1);
8140Sstevel@tonic-gate 		}
8152931Sas145665 		return (m);
8160Sstevel@tonic-gate 
8170Sstevel@tonic-gate 	default:
8180Sstevel@tonic-gate 		printf(gettext("Unknown metachar (%c)\n"), c);
8192931Sas145665 		return (-1);
8200Sstevel@tonic-gate 	}
8210Sstevel@tonic-gate }
822