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