Home | History | Annotate | Line # | Download | only in mail
support.c revision 1.24
      1  1.24  christos /*	$NetBSD: support.c,v 1.24 2012/04/29 23:50:22 christos Exp $	*/
      2   1.1        tv 
      3   1.1        tv /*
      4   1.1        tv  * Copyright (c) 1980, 1993
      5   1.1        tv  *	The Regents of the University of California.  All rights reserved.
      6   1.1        tv  *
      7   1.1        tv  * Redistribution and use in source and binary forms, with or without
      8   1.1        tv  * modification, are permitted provided that the following conditions
      9   1.1        tv  * are met:
     10   1.1        tv  * 1. Redistributions of source code must retain the above copyright
     11   1.1        tv  *    notice, this list of conditions and the following disclaimer.
     12   1.1        tv  * 2. Redistributions in binary form must reproduce the above copyright
     13   1.1        tv  *    notice, this list of conditions and the following disclaimer in the
     14   1.1        tv  *    documentation and/or other materials provided with the distribution.
     15  1.10       agc  * 3. Neither the name of the University nor the names of its contributors
     16   1.1        tv  *    may be used to endorse or promote products derived from this software
     17   1.1        tv  *    without specific prior written permission.
     18   1.1        tv  *
     19   1.1        tv  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     20   1.1        tv  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     21   1.1        tv  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     22   1.1        tv  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     23   1.1        tv  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     24   1.1        tv  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     25   1.1        tv  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     26   1.1        tv  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     27   1.1        tv  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     28   1.1        tv  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     29   1.1        tv  * SUCH DAMAGE.
     30   1.1        tv  */
     31   1.1        tv 
     32   1.1        tv #include <sys/cdefs.h>
     33   1.1        tv #ifndef lint
     34   1.1        tv #if 0
     35   1.1        tv static char sccsid[] = "@(#)aux.c	8.1 (Berkeley) 6/6/93";
     36   1.1        tv #else
     37  1.24  christos __RCSID("$NetBSD: support.c,v 1.24 2012/04/29 23:50:22 christos Exp $");
     38   1.1        tv #endif
     39   1.1        tv #endif /* not lint */
     40   1.1        tv 
     41  1.17  christos #include <stdarg.h>
     42  1.17  christos #include <util.h>
     43  1.17  christos 
     44   1.1        tv #include "rcv.h"
     45   1.1        tv #include "extern.h"
     46  1.15  christos #include "mime.h"
     47  1.17  christos #include "thread.h"
     48   1.1        tv 
     49   1.1        tv /*
     50   1.1        tv  * Mail -- a mail program
     51   1.1        tv  *
     52   1.1        tv  * Auxiliary functions.
     53   1.1        tv  */
     54  1.17  christos 
     55  1.17  christos 
     56   1.1        tv /*
     57   1.1        tv  * Return a pointer to a dynamic copy of the argument.
     58   1.1        tv  */
     59  1.17  christos PUBLIC char *
     60   1.2       wiz savestr(const char *str)
     61   1.1        tv {
     62   1.1        tv 	char *new;
     63  1.14  christos 	size_t size = strlen(str) + 1;
     64   1.1        tv 
     65   1.4       wiz 	if ((new = salloc(size)) != NULL)
     66  1.14  christos 		(void)memmove(new, str, size);
     67   1.1        tv 	return new;
     68   1.1        tv }
     69   1.1        tv 
     70   1.1        tv /*
     71   1.1        tv  * Make a copy of new argument incorporating old one.
     72   1.1        tv  */
     73   1.1        tv static char *
     74   1.2       wiz save2str(char *str, char *old)
     75   1.1        tv {
     76   1.1        tv 	char *new;
     77  1.14  christos 	size_t newsize = strlen(str) + 1;
     78  1.14  christos 	size_t oldsize = old ? strlen(old) + 1 : 0;
     79   1.1        tv 
     80   1.4       wiz 	if ((new = salloc(newsize + oldsize)) != NULL) {
     81   1.1        tv 		if (oldsize) {
     82  1.14  christos 			(void)memmove(new, old, oldsize);
     83   1.1        tv 			new[oldsize - 1] = ' ';
     84   1.1        tv 		}
     85  1.14  christos 		(void)memmove(new + oldsize, str, newsize);
     86   1.1        tv 	}
     87   1.1        tv 	return new;
     88   1.1        tv }
     89   1.1        tv 
     90   1.1        tv /*
     91  1.17  christos  * Like asprintf(), but with result salloc-ed rather than malloc-ed.
     92  1.17  christos  */
     93  1.17  christos PUBLIC int
     94  1.17  christos sasprintf(char **ret, const char *format, ...)
     95  1.17  christos {
     96  1.17  christos 	int rval;
     97  1.17  christos 	char *p;
     98  1.17  christos 	va_list args;
     99  1.17  christos 
    100  1.17  christos 	va_start(args, format);
    101  1.17  christos 	rval = evasprintf(&p, format, args);
    102  1.17  christos 	*ret = savestr(p);
    103  1.17  christos 	free(p);
    104  1.17  christos 	va_end(args);
    105  1.17  christos 	return rval;
    106  1.17  christos }
    107  1.17  christos 
    108  1.17  christos struct set_m_flag_args_s {
    109  1.17  christos 	int and_bits;
    110  1.17  christos 	int xor_bits;
    111  1.17  christos };
    112  1.17  christos static int
    113  1.17  christos set_m_flag_core(struct message *mp, void *v)
    114  1.17  christos {
    115  1.17  christos 	struct set_m_flag_args_s *args;
    116  1.17  christos 	args = v;
    117  1.17  christos 	mp->m_flag &= args->and_bits;
    118  1.17  christos 	mp->m_flag ^= args->xor_bits;
    119  1.17  christos 	return 0;
    120  1.17  christos }
    121  1.17  christos 
    122  1.17  christos PUBLIC struct message *
    123  1.17  christos set_m_flag(int msgnum, int and_bits, int xor_bits)
    124  1.17  christos {
    125  1.17  christos 	struct message *mp;
    126  1.17  christos 	struct set_m_flag_args_s args;
    127  1.17  christos 	mp = get_message(msgnum);
    128  1.17  christos 	args.and_bits = and_bits;
    129  1.17  christos 	args.xor_bits = xor_bits;
    130  1.17  christos 	(void)thread_recursion(mp, set_m_flag_core, &args);
    131  1.17  christos 	return mp;
    132  1.17  christos }
    133  1.17  christos 
    134  1.17  christos /*
    135   1.1        tv  * Touch the named message by setting its MTOUCH flag.
    136   1.1        tv  * Touched messages have the effect of not being sent
    137   1.1        tv  * back to the system mailbox on exit.
    138   1.1        tv  */
    139  1.17  christos PUBLIC void
    140   1.2       wiz touch(struct message *mp)
    141   1.1        tv {
    142   1.1        tv 
    143   1.1        tv 	mp->m_flag |= MTOUCH;
    144   1.1        tv 	if ((mp->m_flag & MREAD) == 0)
    145   1.1        tv 		mp->m_flag |= MREAD|MSTATUS;
    146   1.1        tv }
    147   1.1        tv 
    148   1.1        tv /*
    149   1.1        tv  * Test to see if the passed file name is a directory.
    150   1.1        tv  * Return true if it is.
    151   1.1        tv  */
    152  1.17  christos PUBLIC int
    153  1.13  christos isdir(const char name[])
    154   1.1        tv {
    155   1.1        tv 	struct stat sbuf;
    156   1.1        tv 
    157   1.1        tv 	if (stat(name, &sbuf) < 0)
    158  1.17  christos 		return 0;
    159  1.17  christos 	return S_ISDIR(sbuf.st_mode);
    160   1.1        tv }
    161   1.1        tv 
    162   1.1        tv /*
    163   1.1        tv  * Count the number of arguments in the given string raw list.
    164   1.1        tv  */
    165  1.17  christos PUBLIC int
    166   1.2       wiz argcount(char **argv)
    167   1.1        tv {
    168   1.1        tv 	char **ap;
    169   1.1        tv 
    170  1.19  christos 	for (ap = argv; *ap++ != NULL; /*EMPTY*/)
    171  1.17  christos 		continue;
    172  1.22  christos 	return (int)(ap - argv - 1);
    173   1.1        tv }
    174   1.1        tv 
    175   1.1        tv /*
    176  1.17  christos  * Check whether the passed line is a header line of
    177  1.21  christos  * the desired breed.  Return the field body, or NULL.
    178   1.1        tv  */
    179  1.17  christos static char*
    180  1.17  christos ishfield(const char linebuf[], char *colon, const char field[])
    181   1.1        tv {
    182  1.17  christos 	char *cp = colon;
    183  1.15  christos 
    184  1.17  christos 	*cp = 0;
    185  1.17  christos 	if (strcasecmp(linebuf, field) != 0) {
    186  1.17  christos 		*cp = ':';
    187  1.21  christos 		return NULL;
    188   1.1        tv 	}
    189  1.17  christos 	*cp = ':';
    190  1.19  christos 	for (cp++; is_WSP(*cp); cp++)
    191  1.17  christos 		continue;
    192  1.17  christos 	return cp;
    193   1.1        tv }
    194   1.1        tv 
    195   1.1        tv /*
    196   1.1        tv  * Return the next header field found in the given message.
    197   1.1        tv  * Return >= 0 if something found, < 0 elsewise.
    198   1.1        tv  * "colon" is set to point to the colon in the header.
    199   1.1        tv  * Must deal with \ continuations & other such fraud.
    200  1.17  christos  *
    201  1.17  christos  * WARNING - do not call this outside hfield() or decoding will not
    202  1.17  christos  *           get done.
    203   1.1        tv  */
    204  1.15  christos static int
    205   1.2       wiz gethfield(FILE *f, char linebuf[], int rem, char **colon)
    206   1.1        tv {
    207   1.1        tv 	char line2[LINESIZE];
    208   1.1        tv 	char *cp, *cp2;
    209   1.1        tv 	int c;
    210   1.1        tv 
    211   1.1        tv 	for (;;) {
    212   1.1        tv 		if (--rem < 0)
    213   1.1        tv 			return -1;
    214  1.22  christos 		if ((c = readline(f, linebuf, LINESIZE, 0)) <= 0)
    215   1.1        tv 			return -1;
    216  1.19  christos 		for (cp = linebuf;
    217  1.19  christos 		     isprint((unsigned char)*cp) && *cp != ' ' && *cp != ':';
    218   1.1        tv 		     cp++)
    219  1.17  christos 			continue;
    220   1.1        tv 		if (*cp != ':' || cp == linebuf)
    221   1.1        tv 			continue;
    222   1.1        tv 		/*
    223   1.1        tv 		 * I guess we got a headline.
    224   1.1        tv 		 * Handle wraparounding
    225   1.1        tv 		 */
    226   1.1        tv 		*colon = cp;
    227   1.1        tv 		cp = linebuf + c;
    228   1.1        tv 		for (;;) {
    229  1.19  christos 			while (--cp >= linebuf && is_WSP(*cp))
    230  1.17  christos 				continue;
    231   1.1        tv 			cp++;
    232   1.1        tv 			if (rem <= 0)
    233   1.1        tv 				break;
    234  1.14  christos 			(void)ungetc(c = getc(f), f);
    235  1.19  christos 			if (!is_WSP(c))
    236   1.1        tv 				break;
    237  1.22  christos 			if ((c = readline(f, line2, LINESIZE, 0)) < 0)
    238   1.1        tv 				break;
    239   1.1        tv 			rem--;
    240  1.19  christos 			cp2 = skip_WSP(line2);
    241  1.22  christos 			c -= (int)(cp2 - line2);
    242   1.1        tv 			if (cp + c >= linebuf + LINESIZE - 2)
    243   1.1        tv 				break;
    244   1.1        tv 			*cp++ = ' ';
    245  1.14  christos 			(void)memmove(cp, cp2, (size_t)c);
    246   1.1        tv 			cp += c;
    247   1.1        tv 		}
    248   1.1        tv 		*cp = 0;
    249   1.1        tv 		return rem;
    250   1.1        tv 	}
    251   1.1        tv 	/* NOTREACHED */
    252   1.1        tv }
    253   1.1        tv 
    254   1.1        tv /*
    255  1.17  christos  * Return the desired header line from the passed message
    256  1.17  christos  * pointer (or NULL if the desired header field is not available).
    257   1.1        tv  */
    258  1.17  christos PUBLIC char *
    259  1.17  christos hfield(const char field[], const struct message *mp)
    260  1.17  christos {
    261  1.17  christos 	FILE *ibuf;
    262  1.17  christos 	char linebuf[LINESIZE];
    263  1.17  christos 	int lc;
    264  1.17  christos 	char *headerfield;
    265  1.17  christos 	char *colon, *oldhfield = NULL;
    266  1.17  christos #ifdef MIME_SUPPORT
    267  1.17  christos 	int decode;
    268   1.1        tv 
    269  1.17  christos 	decode = value(ENAME_MIME_DECODE_MSG) &&
    270  1.17  christos 	    value(ENAME_MIME_DECODE_HDR);
    271  1.17  christos #endif
    272   1.1        tv 
    273  1.17  christos 	ibuf = setinput(mp);
    274  1.22  christos 	if ((lc = (int)(mp->m_lines - 1)) < 0)
    275  1.17  christos 		return NULL;
    276  1.22  christos 	if (readline(ibuf, linebuf, LINESIZE, 0) < 0)
    277  1.17  christos 		return NULL;
    278  1.17  christos 	while (lc > 0) {
    279  1.17  christos 		if ((lc = gethfield(ibuf, linebuf, lc, &colon)) < 0)
    280  1.17  christos 			return oldhfield;
    281  1.17  christos #ifdef MIME_SUPPORT
    282  1.17  christos 		if ((headerfield = ishfield(linebuf, colon, field)) != NULL) {
    283  1.17  christos 			char linebuf2[LINESIZE];
    284  1.21  christos 			if (decode)
    285  1.21  christos 				headerfield = mime_decode_hfield(linebuf2,
    286  1.21  christos 				    sizeof(linebuf2), linebuf, headerfield);
    287  1.17  christos 			oldhfield = save2str(headerfield, oldhfield);
    288  1.17  christos 		}
    289  1.17  christos #else
    290  1.17  christos 		if ((headerfield = ishfield(linebuf, colon, field)) != NULL)
    291  1.17  christos 			oldhfield = save2str(headerfield, oldhfield);
    292  1.17  christos #endif
    293   1.1        tv 	}
    294  1.17  christos 	return oldhfield;
    295   1.1        tv }
    296   1.1        tv 
    297   1.1        tv /*
    298   1.1        tv  * Copy a string, lowercasing it as we go.
    299   1.1        tv  */
    300  1.17  christos PUBLIC void
    301  1.13  christos istrcpy(char *dest, const char *src)
    302   1.1        tv {
    303   1.1        tv 
    304   1.1        tv 	do {
    305  1.12       dsl 		*dest++ = tolower((unsigned char)*src);
    306   1.1        tv 	} while (*src++ != 0);
    307   1.1        tv }
    308   1.1        tv 
    309   1.1        tv /*
    310   1.1        tv  * The following code deals with input stacking to do source
    311   1.1        tv  * commands.  All but the current file pointer are saved on
    312   1.1        tv  * the stack.
    313   1.1        tv  */
    314   1.1        tv 
    315   1.1        tv static	int	ssp;			/* Top of file stack */
    316  1.17  christos static struct sstack {
    317   1.1        tv 	FILE	*s_file;		/* File we were in. */
    318   1.1        tv 	int	s_cond;			/* Saved state of conditionals */
    319  1.17  christos #ifdef NEW_CONDITIONAL
    320  1.17  christos 	struct cond_stack_s *s_cond_stack; /* Saved conditional stack */
    321  1.17  christos #endif
    322   1.1        tv 	int	s_loading;		/* Loading .mailrc, etc. */
    323   1.1        tv } sstack[NOFILE];
    324   1.1        tv 
    325   1.1        tv /*
    326   1.1        tv  * Pushdown current input file and switch to a new one.
    327   1.1        tv  * Set the global flag "sourcing" so that others will realize
    328   1.1        tv  * that they are no longer reading from a tty (in all probability).
    329   1.1        tv  */
    330  1.17  christos PUBLIC int
    331   1.2       wiz source(void *v)
    332   1.1        tv {
    333   1.1        tv 	char **arglist = v;
    334   1.1        tv 	FILE *fi;
    335  1.13  christos 	const char *cp;
    336   1.1        tv 
    337   1.4       wiz 	if ((cp = expand(*arglist)) == NULL)
    338  1.17  christos 		return 1;
    339  1.24  christos 	if ((fi = Fopen(cp, "re")) == NULL) {
    340   1.9       wiz 		warn("%s", cp);
    341  1.17  christos 		return 1;
    342   1.1        tv 	}
    343   1.1        tv 	if (ssp >= NOFILE - 1) {
    344  1.14  christos 		(void)printf("Too much \"sourcing\" going on.\n");
    345  1.14  christos 		(void)Fclose(fi);
    346  1.17  christos 		return 1;
    347   1.1        tv 	}
    348   1.1        tv 	sstack[ssp].s_file = input;
    349   1.1        tv 	sstack[ssp].s_cond = cond;
    350  1.17  christos #ifdef NEW_CONDITIONAL
    351  1.17  christos 	sstack[ssp].s_cond_stack = cond_stack;
    352  1.17  christos #endif
    353   1.1        tv 	sstack[ssp].s_loading = loading;
    354   1.1        tv 	ssp++;
    355   1.1        tv 	loading = 0;
    356  1.17  christos 	cond = CNONE;
    357   1.1        tv 	input = fi;
    358   1.1        tv 	sourcing++;
    359  1.17  christos 	return 0;
    360   1.1        tv }
    361   1.1        tv 
    362   1.1        tv /*
    363   1.1        tv  * Pop the current input back to the previous level.
    364   1.1        tv  * Update the "sourcing" flag as appropriate.
    365   1.1        tv  */
    366  1.17  christos PUBLIC int
    367   1.2       wiz unstack(void)
    368   1.1        tv {
    369   1.1        tv 	if (ssp <= 0) {
    370  1.14  christos 		(void)printf("\"Source\" stack over-pop.\n");
    371   1.1        tv 		sourcing = 0;
    372  1.17  christos 		return 1;
    373   1.1        tv 	}
    374  1.14  christos 	(void)Fclose(input);
    375  1.17  christos 	if (cond != CNONE || cond_stack != NULL)
    376  1.14  christos 		(void)printf("Unmatched \"if\"\n");
    377   1.1        tv 	ssp--;
    378  1.17  christos 	input = sstack[ssp].s_file;
    379   1.1        tv 	cond = sstack[ssp].s_cond;
    380  1.17  christos #ifdef NEW_CONDITIONAL
    381  1.17  christos 	cond_stack = sstack[ssp].s_cond_stack;
    382  1.17  christos #endif
    383   1.1        tv 	loading = sstack[ssp].s_loading;
    384   1.1        tv 	if (ssp == 0)
    385   1.1        tv 		sourcing = loading;
    386  1.17  christos 	return 0;
    387   1.1        tv }
    388   1.1        tv 
    389   1.1        tv /*
    390   1.1        tv  * Touch the indicated file.
    391   1.1        tv  * This is nifty for the shell.
    392   1.1        tv  */
    393  1.17  christos PUBLIC void
    394   1.2       wiz alter(char *name)
    395   1.1        tv {
    396   1.1        tv 	struct stat sb;
    397   1.1        tv 	struct timeval tv[2];
    398   1.1        tv 
    399   1.1        tv 	if (stat(name, &sb))
    400   1.1        tv 		return;
    401   1.8       wiz 	(void)gettimeofday(&tv[0], NULL);
    402   1.1        tv 	tv[0].tv_sec++;
    403   1.1        tv 	TIMESPEC_TO_TIMEVAL(&tv[1], &sb.st_mtimespec);
    404   1.8       wiz 	(void)utimes(name, tv);
    405   1.1        tv }
    406   1.1        tv 
    407   1.1        tv /*
    408   1.1        tv  * Examine the passed line buffer and
    409   1.1        tv  * return true if it is all blanks and tabs.
    410   1.1        tv  */
    411  1.17  christos PUBLIC int
    412   1.2       wiz blankline(char linebuf[])
    413   1.1        tv {
    414   1.1        tv 	char *cp;
    415   1.1        tv 
    416   1.1        tv 	for (cp = linebuf; *cp; cp++)
    417  1.19  christos 		if (!is_WSP(*cp))
    418  1.17  christos 			return 0;
    419  1.17  christos 	return 1;
    420   1.1        tv }
    421   1.1        tv 
    422   1.1        tv /*
    423   1.1        tv  * Start of a "comment".
    424   1.1        tv  * Ignore it.
    425   1.1        tv  */
    426  1.17  christos static char *
    427   1.2       wiz skip_comment(char *cp)
    428   1.1        tv {
    429   1.1        tv 	int nesting = 1;
    430   1.1        tv 
    431  1.17  christos 	for (/*EMPTY*/; nesting > 0 && *cp; cp++) {
    432   1.1        tv 		switch (*cp) {
    433   1.1        tv 		case '\\':
    434   1.1        tv 			if (cp[1])
    435   1.1        tv 				cp++;
    436   1.1        tv 			break;
    437   1.1        tv 		case '(':
    438   1.1        tv 			nesting++;
    439   1.1        tv 			break;
    440   1.1        tv 		case ')':
    441   1.1        tv 			nesting--;
    442   1.1        tv 			break;
    443   1.1        tv 		}
    444   1.1        tv 	}
    445   1.1        tv 	return cp;
    446   1.1        tv }
    447   1.1        tv 
    448   1.1        tv /*
    449   1.1        tv  * Skin an arpa net address according to the RFC 822 interpretation
    450   1.1        tv  * of "host-phrase."
    451   1.1        tv  */
    452  1.17  christos PUBLIC char *
    453   1.2       wiz skin(char *name)
    454   1.1        tv {
    455   1.1        tv 	int c;
    456   1.1        tv 	char *cp, *cp2;
    457   1.1        tv 	char *bufend;
    458   1.1        tv 	int gotlt, lastsp;
    459  1.17  christos 	char nbuf[LINESIZE];
    460   1.1        tv 
    461   1.4       wiz 	if (name == NULL)
    462  1.17  christos 		return NULL;
    463   1.4       wiz 	if (strchr(name, '(') == NULL && strchr(name, '<') == NULL
    464   1.4       wiz 	    && strchr(name, ' ') == NULL)
    465  1.17  christos 		return name;
    466   1.1        tv 	gotlt = 0;
    467   1.1        tv 	lastsp = 0;
    468   1.1        tv 	bufend = nbuf;
    469  1.19  christos 	for (cp = name, cp2 = bufend; (c = *cp++) != '\0'; /*EMPTY*/) {
    470   1.1        tv 		switch (c) {
    471   1.1        tv 		case '(':
    472   1.1        tv 			cp = skip_comment(cp);
    473   1.1        tv 			lastsp = 0;
    474   1.1        tv 			break;
    475   1.1        tv 
    476   1.1        tv 		case '"':
    477   1.1        tv 			/*
    478   1.1        tv 			 * Start of a "quoted-string".
    479   1.1        tv 			 * Copy it in its entirety.
    480   1.1        tv 			 */
    481   1.1        tv 			while ((c = *cp) != '\0') {
    482   1.1        tv 				cp++;
    483   1.1        tv 				if (c == '"')
    484   1.1        tv 					break;
    485   1.1        tv 				if (c != '\\')
    486   1.1        tv 					*cp2++ = c;
    487   1.1        tv 				else if ((c = *cp) != '\0') {
    488   1.1        tv 					*cp2++ = c;
    489   1.1        tv 					cp++;
    490   1.1        tv 				}
    491   1.1        tv 			}
    492   1.1        tv 			lastsp = 0;
    493   1.1        tv 			break;
    494   1.1        tv 
    495   1.1        tv 		case ' ':
    496   1.1        tv 			if (cp[0] == 'a' && cp[1] == 't' && cp[2] == ' ')
    497   1.1        tv 				cp += 3, *cp2++ = '@';
    498   1.1        tv 			else
    499   1.1        tv 			if (cp[0] == '@' && cp[1] == ' ')
    500   1.1        tv 				cp += 2, *cp2++ = '@';
    501   1.1        tv 			else
    502   1.1        tv 				lastsp = 1;
    503   1.1        tv 			break;
    504   1.1        tv 
    505   1.1        tv 		case '<':
    506   1.1        tv 			cp2 = bufend;
    507   1.1        tv 			gotlt++;
    508   1.1        tv 			lastsp = 0;
    509   1.1        tv 			break;
    510   1.1        tv 
    511   1.1        tv 		case '>':
    512   1.1        tv 			if (gotlt) {
    513   1.1        tv 				gotlt = 0;
    514   1.1        tv 				while ((c = *cp) && c != ',') {
    515   1.1        tv 					cp++;
    516   1.1        tv 					if (c == '(')
    517   1.1        tv 						cp = skip_comment(cp);
    518   1.1        tv 					else if (c == '"')
    519   1.1        tv 						while ((c = *cp) != '\0') {
    520   1.1        tv 							cp++;
    521   1.1        tv 							if (c == '"')
    522   1.1        tv 								break;
    523   1.1        tv 							if (c == '\\' && *cp)
    524   1.1        tv 								cp++;
    525   1.1        tv 						}
    526   1.1        tv 				}
    527   1.1        tv 				lastsp = 0;
    528   1.1        tv 				break;
    529   1.1        tv 			}
    530  1.14  christos 			/* FALLTHROUGH */
    531   1.1        tv 
    532   1.1        tv 		default:
    533   1.1        tv 			if (lastsp) {
    534   1.1        tv 				lastsp = 0;
    535   1.1        tv 				*cp2++ = ' ';
    536   1.1        tv 			}
    537   1.1        tv 			*cp2++ = c;
    538   1.1        tv 			if (c == ',' && !gotlt) {
    539   1.1        tv 				*cp2++ = ' ';
    540  1.17  christos 				for (/*EMPTY*/; *cp == ' '; cp++)
    541  1.17  christos 					continue;
    542   1.1        tv 				lastsp = 0;
    543   1.1        tv 				bufend = cp2;
    544   1.1        tv 			}
    545   1.1        tv 		}
    546   1.1        tv 	}
    547   1.1        tv 	*cp2 = 0;
    548   1.1        tv 
    549  1.17  christos 	return savestr(nbuf);
    550   1.1        tv }
    551   1.1        tv 
    552   1.1        tv /*
    553   1.1        tv  * Fetch the sender's name from the passed message.
    554   1.1        tv  * Reptype can be
    555   1.1        tv  *	0 -- get sender's name for display purposes
    556   1.1        tv  *	1 -- get sender's name for reply
    557   1.1        tv  *	2 -- get sender's name for Reply
    558   1.1        tv  */
    559  1.17  christos static char *
    560   1.2       wiz name1(struct message *mp, int reptype)
    561   1.1        tv {
    562   1.1        tv 	char namebuf[LINESIZE];
    563   1.1        tv 	char linebuf[LINESIZE];
    564   1.1        tv 	char *cp, *cp2;
    565   1.1        tv 	FILE *ibuf;
    566   1.3       wiz 	int firstrun = 1;
    567   1.1        tv 
    568   1.4       wiz 	if ((cp = hfield("from", mp)) != NULL)
    569   1.1        tv 		return cp;
    570   1.4       wiz 	if (reptype == 0 && (cp = hfield("sender", mp)) != NULL)
    571   1.1        tv 		return cp;
    572   1.1        tv 	ibuf = setinput(mp);
    573   1.1        tv 	namebuf[0] = '\0';
    574  1.22  christos 	if (readline(ibuf, linebuf, LINESIZE, 0) < 0)
    575  1.17  christos 		return savestr(namebuf);
    576  1.17  christos  newname:
    577   1.1        tv 	for (cp = linebuf; *cp && *cp != ' '; cp++)
    578  1.17  christos 		continue;
    579  1.19  christos 	cp = skip_WSP(cp);
    580   1.1        tv 	for (cp2 = &namebuf[strlen(namebuf)];
    581  1.19  christos 	     *cp && !is_WSP(*cp) && cp2 < namebuf + LINESIZE - 1;
    582  1.19  christos 	     /*EMPTY*/)
    583   1.1        tv 		*cp2++ = *cp++;
    584   1.1        tv 	*cp2 = '\0';
    585  1.22  christos 	if (readline(ibuf, linebuf, LINESIZE, 0) < 0)
    586  1.17  christos 		return savestr(namebuf);
    587   1.1        tv 	if ((cp = strchr(linebuf, 'F')) == NULL)
    588  1.17  christos 		return savestr(namebuf);
    589   1.1        tv 	if (strncmp(cp, "From", 4) != 0)
    590  1.17  christos 		return savestr(namebuf);
    591   1.1        tv 	while ((cp = strchr(cp, 'r')) != NULL) {
    592   1.1        tv 		if (strncmp(cp, "remote", 6) == 0) {
    593   1.1        tv 			if ((cp = strchr(cp, 'f')) == NULL)
    594   1.1        tv 				break;
    595   1.1        tv 			if (strncmp(cp, "from", 4) != 0)
    596   1.1        tv 				break;
    597   1.1        tv 			if ((cp = strchr(cp, ' ')) == NULL)
    598   1.1        tv 				break;
    599   1.1        tv 			cp++;
    600   1.3       wiz 			if (firstrun) {
    601   1.1        tv 				cp2 = namebuf;
    602   1.3       wiz 				firstrun = 0;
    603   1.1        tv 			} else
    604   1.1        tv 				cp2 = strrchr(namebuf, '!') + 1;
    605   1.1        tv 			while (*cp && cp2 < namebuf + LINESIZE - 1)
    606   1.1        tv 				*cp2++ = *cp++;
    607   1.1        tv 			if (cp2 < namebuf + LINESIZE - 1)
    608   1.1        tv 				*cp2++ = '!';
    609   1.1        tv 			*cp2 = '\0';
    610   1.1        tv 			if (cp2 < namebuf + LINESIZE - 1)
    611   1.1        tv 				goto newname;
    612   1.1        tv 			else
    613   1.1        tv 				break;
    614   1.1        tv 		}
    615   1.1        tv 		cp++;
    616   1.1        tv 	}
    617  1.17  christos 	return savestr(namebuf);
    618   1.1        tv }
    619   1.1        tv 
    620   1.1        tv /*
    621  1.15  christos  * Count the occurrences of c in str
    622   1.1        tv  */
    623  1.17  christos static int
    624   1.2       wiz charcount(char *str, int c)
    625   1.1        tv {
    626   1.1        tv 	char *cp;
    627   1.1        tv 	int i;
    628   1.1        tv 
    629   1.1        tv 	for (i = 0, cp = str; *cp; cp++)
    630   1.1        tv 		if (*cp == c)
    631   1.1        tv 			i++;
    632  1.17  christos 	return i;
    633   1.1        tv }
    634   1.1        tv 
    635   1.1        tv /*
    636  1.17  christos  * Get sender's name from this message.  If the message has
    637  1.17  christos  * a bunch of arpanet stuff in it, we may have to skin the name
    638  1.17  christos  * before returning it.
    639   1.1        tv  */
    640  1.17  christos PUBLIC char *
    641  1.17  christos nameof(struct message *mp, int reptype)
    642   1.1        tv {
    643  1.17  christos 	char *cp, *cp2;
    644   1.1        tv 
    645  1.17  christos 	cp = skin(name1(mp, reptype));
    646  1.17  christos 	if (reptype != 0 || charcount(cp, '!') < 2)
    647  1.17  christos 		return cp;
    648  1.17  christos 	cp2 = strrchr(cp, '!');
    649  1.17  christos 	cp2--;
    650  1.17  christos 	while (cp2 > cp && *cp2 != '!')
    651  1.17  christos 		cp2--;
    652  1.17  christos 	if (*cp2 == '!')
    653  1.17  christos 		return cp2 + 1;
    654  1.17  christos 	return cp;
    655   1.1        tv }
    656   1.1        tv 
    657   1.1        tv /*
    658   1.1        tv  * Copy s1 to s2, return pointer to null in s2.
    659   1.1        tv  */
    660  1.17  christos PUBLIC char *
    661   1.2       wiz copy(char *s1, char *s2)
    662   1.1        tv {
    663   1.1        tv 
    664   1.1        tv 	while ((*s2++ = *s1++) != '\0')
    665  1.17  christos 		continue;
    666   1.1        tv 	return s2 - 1;
    667   1.1        tv }
    668   1.1        tv 
    669   1.1        tv /*
    670  1.17  christos  * The core routine to check the ignore table for a field.
    671  1.17  christos  * Note: realfield should be lower-cased!
    672  1.17  christos  */
    673  1.17  christos PUBLIC int
    674  1.17  christos member(char *realfield, struct ignoretab *table)
    675  1.17  christos {
    676  1.17  christos 	struct ignore *igp;
    677  1.17  christos 
    678  1.17  christos 	for (igp = table->i_head[hash(realfield)]; igp != 0; igp = igp->i_link)
    679  1.17  christos 		if (*igp->i_field == *realfield &&
    680  1.17  christos 		    equal(igp->i_field, realfield))
    681  1.17  christos 			return 1;
    682  1.17  christos 	return 0;
    683  1.17  christos }
    684  1.17  christos 
    685  1.17  christos /*
    686   1.1        tv  * See if the given header field is supposed to be ignored.
    687   1.1        tv  */
    688  1.17  christos PUBLIC int
    689  1.13  christos isign(const char *field, struct ignoretab ignoretabs[2])
    690   1.1        tv {
    691   1.1        tv 	char realfld[LINESIZE];
    692   1.1        tv 
    693   1.3       wiz 	if (ignoretabs == ignoreall)
    694   1.1        tv 		return 1;
    695   1.1        tv 	/*
    696   1.1        tv 	 * Lower-case the string, so that "Status" and "status"
    697   1.1        tv 	 * will hash to the same place.
    698   1.1        tv 	 */
    699   1.1        tv 	istrcpy(realfld, field);
    700   1.3       wiz 	if (ignoretabs[1].i_count > 0)
    701  1.17  christos 		return !member(realfld, ignoretabs + 1);
    702   1.1        tv 	else
    703  1.17  christos 		return member(realfld, ignoretabs);
    704   1.1        tv }
    705  1.18  christos 
    706  1.20  christos PUBLIC void
    707  1.20  christos add_ignore(const char *name, struct ignoretab *tab)
    708  1.20  christos {
    709  1.20  christos 	char field[LINESIZE];
    710  1.20  christos 	int h;
    711  1.20  christos 	struct ignore *igp;
    712  1.20  christos 
    713  1.20  christos 	istrcpy(field, name);
    714  1.20  christos 	if (member(field, tab))
    715  1.20  christos 		return;
    716  1.20  christos 	h = hash(field);
    717  1.20  christos 	igp = ecalloc(1, sizeof(struct ignore));
    718  1.20  christos 	igp->i_field = ecalloc(strlen(field) + 1, sizeof(char));
    719  1.20  christos 	(void)strcpy(igp->i_field, field);
    720  1.20  christos 	igp->i_link = tab->i_head[h];
    721  1.20  christos 	tab->i_head[h] = igp;
    722  1.20  christos 	tab->i_count++;
    723  1.20  christos }
    724  1.20  christos 
    725  1.18  christos /*
    726  1.18  christos  * Write a file to stdout, skipping comment lines.
    727  1.18  christos  */
    728  1.18  christos PUBLIC void
    729  1.18  christos cathelp(const char *fname)
    730  1.18  christos {
    731  1.18  christos 	char *line;
    732  1.18  christos 	FILE *f;
    733  1.18  christos 	size_t len;
    734  1.18  christos 
    735  1.24  christos 	if ((f = Fopen(fname, "re")) == NULL) {
    736  1.23     joerg 		warn("%s", fname);
    737  1.18  christos 		return;
    738  1.18  christos 	}
    739  1.18  christos 	while ((line = fgetln(f, &len)) != NULL) {
    740  1.18  christos 		if (*line == '#')
    741  1.18  christos 			continue;
    742  1.18  christos 		if (fwrite(line, 1, len, stdout) != len)
    743  1.18  christos 			break;
    744  1.18  christos 	}
    745  1.18  christos 	(void)Fclose(f);
    746  1.18  christos 	return;
    747  1.18  christos }
    748