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