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