122976Smiriam /*
234921Sbostic  * Copyright (c) 1983 Eric P. Allman
333730Sbostic  * Copyright (c) 1988 Regents of the University of California.
433730Sbostic  * All rights reserved.
533730Sbostic  *
642828Sbostic  * %sccs.include.redist.c%
733730Sbostic  */
822976Smiriam 
922976Smiriam #ifndef lint
10*59089Seric static char sccsid[] = "@(#)parseaddr.c	6.40 (Berkeley) 04/14/93";
1133730Sbostic #endif /* not lint */
1222976Smiriam 
1356678Seric # include "sendmail.h"
14297Seric 
15297Seric /*
169888Seric **  PARSEADDR -- Parse an address
17297Seric **
18297Seric **	Parses an address and breaks it up into three parts: a
19297Seric **	net to transmit the message on, the host to transmit it
20297Seric **	to, and a user on that host.  These are loaded into an
212973Seric **	ADDRESS header with the values squirreled away if necessary.
22297Seric **	The "user" part may not be a real user; the process may
23297Seric **	just reoccur on that machine.  For example, on a machine
24297Seric **	with an arpanet connection, the address
25297Seric **		csvax.bill@berkeley
26297Seric **	will break up to a "user" of 'csvax.bill' and a host
27297Seric **	of 'berkeley' -- to be transmitted over the arpanet.
28297Seric **
29297Seric **	Parameters:
30297Seric **		addr -- the address to parse.
31297Seric **		a -- a pointer to the address descriptor buffer.
32297Seric **			If NULL, a header will be created.
33297Seric **		copyf -- determines what shall be copied:
34297Seric **			-1 -- don't copy anything.  The printname
35297Seric **				(q_paddr) is just addr, and the
36297Seric **				user & host are allocated internally
37297Seric **				to parse.
38297Seric **			0 -- copy out the parsed user & host, but
39297Seric **				don't copy the printname.
40297Seric **			+1 -- copy everything.
4111445Seric **		delim -- the character to terminate the address, passed
4211445Seric **			to prescan.
4358333Seric **		delimptr -- if non-NULL, set to the location of the
4458333Seric **			delim character that was found.
4556678Seric **		e -- the envelope that will contain this address.
46297Seric **
47297Seric **	Returns:
48297Seric **		A pointer to the address descriptor header (`a' if
49297Seric **			`a' is non-NULL).
50297Seric **		NULL on error.
51297Seric **
52297Seric **	Side Effects:
53297Seric **		none
54297Seric */
55297Seric 
569374Seric /* following delimiters are inherent to the internal algorithms */
5758050Seric # define DELIMCHARS	"\201()<>,;\\\"\r\n"	/* word delimiters */
582091Seric 
592973Seric ADDRESS *
6058333Seric parseaddr(addr, a, copyf, delim, delimptr, e)
61297Seric 	char *addr;
622973Seric 	register ADDRESS *a;
63297Seric 	int copyf;
6411445Seric 	char delim;
6558333Seric 	char **delimptr;
6656678Seric 	register ENVELOPE *e;
67297Seric {
683149Seric 	register char **pvp;
6958333Seric 	auto char *delimptrbuf;
7059084Seric 	bool queueup;
7116914Seric 	char pvpbuf[PSBUFSIZE];
7256678Seric 	extern char **prescan();
7356678Seric 	extern ADDRESS *buildaddr();
7457388Seric 	extern bool invalidaddr();
75297Seric 
76297Seric 	/*
77297Seric 	**  Initialize and prescan address.
78297Seric 	*/
79297Seric 
8056678Seric 	e->e_to = addr;
817675Seric 	if (tTd(20, 1))
829888Seric 		printf("\n--parseaddr(%s)\n", addr);
833188Seric 
8457388Seric 	if (invalidaddr(addr))
8557388Seric 	{
8657388Seric 		if (tTd(20, 1))
8757388Seric 			printf("parseaddr-->bad address\n");
8857388Seric 		return NULL;
8957388Seric 	}
9057388Seric 
9158333Seric 	if (delimptr == NULL)
9258333Seric 		delimptr = &delimptrbuf;
9358333Seric 
9458333Seric 	pvp = prescan(addr, delim, pvpbuf, delimptr);
953149Seric 	if (pvp == NULL)
9656729Seric 	{
9756729Seric 		if (tTd(20, 1))
9856729Seric 			printf("parseaddr-->NULL\n");
99297Seric 		return (NULL);
10056729Seric 	}
101297Seric 
102297Seric 	/*
1033149Seric 	**  Apply rewriting rules.
1047889Seric 	**	Ruleset 0 does basic parsing.  It must resolve.
105297Seric 	*/
106297Seric 
10759084Seric 	queueup = FALSE;
10859084Seric 	if (rewrite(pvp, 3, e) == EX_TEMPFAIL)
10959084Seric 		queueup = TRUE;
11059084Seric 	if (rewrite(pvp, 0, e) == EX_TEMPFAIL)
11159084Seric 		queueup = TRUE;
112297Seric 
1133149Seric 	/*
1143149Seric 	**  See if we resolved to a real mailer.
1153149Seric 	*/
116297Seric 
11759040Seric 	if (pvp[0] == NULL || (pvp[0][0] & 0377) != CANONNET)
1183149Seric 	{
1193149Seric 		setstat(EX_USAGE);
12058151Seric 		syserr("554 cannot resolve name");
1213149Seric 		return (NULL);
122297Seric 	}
123297Seric 
124297Seric 	/*
1253149Seric 	**  Build canonical address from pvp.
126297Seric 	*/
127297Seric 
12858966Seric 	a = buildaddr(pvp, a, e);
1294279Seric 	if (a == NULL)
1304279Seric 		return (NULL);
131297Seric 
132297Seric 	/*
1333149Seric 	**  Make local copies of the host & user and then
1343149Seric 	**  transport them out.
135297Seric 	*/
136297Seric 
13758333Seric 	allocaddr(a, copyf, addr, *delimptr);
13856678Seric 
13956678Seric 	/*
14059084Seric 	**  If there was a parsing failure, mark it for queueing.
14159084Seric 	*/
14259084Seric 
14359084Seric 	if (queueup)
14459084Seric 		a->q_flags |= QQUEUEUP;
14559084Seric 
14659084Seric 	/*
14756678Seric 	**  Compute return value.
14856678Seric 	*/
14956678Seric 
15056678Seric 	if (tTd(20, 1))
1518078Seric 	{
15256678Seric 		printf("parseaddr-->");
15356678Seric 		printaddr(a, FALSE);
15456678Seric 	}
15556678Seric 
15656678Seric 	return (a);
15756678Seric }
15856678Seric /*
15957388Seric **  INVALIDADDR -- check for address containing meta-characters
16057388Seric **
16157388Seric **	Parameters:
16257388Seric **		addr -- the address to check.
16357388Seric **
16457388Seric **	Returns:
16557388Seric **		TRUE -- if the address has any "wierd" characters
16657388Seric **		FALSE -- otherwise.
16757388Seric */
16857388Seric 
16957388Seric bool
17057388Seric invalidaddr(addr)
17157388Seric 	register char *addr;
17257388Seric {
17357388Seric 	for (; *addr != '\0'; addr++)
17457388Seric 	{
17558050Seric 		if ((*addr & 0340) != 0200)
17657388Seric 			continue;
17757388Seric 		setstat(EX_USAGE);
17858151Seric 		usrerr("553 Address contained invalid control characters");
17957388Seric 		return TRUE;
18057388Seric 	}
18157388Seric 	return FALSE;
18257388Seric }
18357388Seric /*
18456678Seric **  ALLOCADDR -- do local allocations of address on demand.
18556678Seric **
18656678Seric **	Also lowercases the host name if requested.
18756678Seric **
18856678Seric **	Parameters:
18956678Seric **		a -- the address to reallocate.
19056678Seric **		copyf -- the copy flag (see parseaddr for description).
19156678Seric **		paddr -- the printname of the address.
19258333Seric **		delimptr -- a pointer to the address delimiter.  Must be set.
19356678Seric **
19456678Seric **	Returns:
19556678Seric **		none.
19656678Seric **
19756678Seric **	Side Effects:
19856678Seric **		Copies portions of a into local buffers as requested.
19956678Seric */
20056678Seric 
20158333Seric allocaddr(a, copyf, paddr, delimptr)
20256678Seric 	register ADDRESS *a;
20356678Seric 	int copyf;
20456678Seric 	char *paddr;
20558333Seric 	char *delimptr;
20656678Seric {
20756678Seric 	register MAILER *m = a->q_mailer;
20856678Seric 
20958673Seric 	if (tTd(24, 4))
21058673Seric 		printf("allocaddr(copyf=%d, paddr=%s)\n", copyf, paddr);
21158673Seric 
21256678Seric 	if (copyf > 0 && paddr != NULL)
21356678Seric 	{
21458333Seric 		char savec = *delimptr;
2158078Seric 
21658333Seric 		*delimptr = '\0';
21756678Seric 		a->q_paddr = newstr(paddr);
21858333Seric 		*delimptr = savec;
2198078Seric 	}
220297Seric 	else
22156678Seric 		a->q_paddr = paddr;
22224944Seric 
22324944Seric 	if (a->q_user == NULL)
22424944Seric 		a->q_user = "";
22524944Seric 	if (a->q_host == NULL)
22624944Seric 		a->q_host = "";
22724944Seric 
2283149Seric 	if (copyf >= 0)
229297Seric 	{
23024944Seric 		a->q_host = newstr(a->q_host);
2313149Seric 		if (a->q_user != a->q_paddr)
2323149Seric 			a->q_user = newstr(a->q_user);
233297Seric 	}
234297Seric 
23556678Seric 	if (a->q_paddr == NULL)
23656678Seric 		a->q_paddr = a->q_user;
237297Seric }
238297Seric /*
239297Seric **  PRESCAN -- Prescan name and make it canonical
240297Seric **
2419374Seric **	Scans a name and turns it into a set of tokens.  This process
2429374Seric **	deletes blanks and comments (in parentheses).
243297Seric **
244297Seric **	This routine knows about quoted strings and angle brackets.
245297Seric **
246297Seric **	There are certain subtleties to this routine.  The one that
247297Seric **	comes to mind now is that backslashes on the ends of names
248297Seric **	are silently stripped off; this is intentional.  The problem
249297Seric **	is that some versions of sndmsg (like at LBL) set the kill
250297Seric **	character to something other than @ when reading addresses;
251297Seric **	so people type "csvax.eric\@berkeley" -- which screws up the
252297Seric **	berknet mailer.
253297Seric **
254297Seric **	Parameters:
255297Seric **		addr -- the name to chomp.
256297Seric **		delim -- the delimiter for the address, normally
257297Seric **			'\0' or ','; \0 is accepted in any case.
25815284Seric **			If '\t' then we are reading the .cf file.
25916914Seric **		pvpbuf -- place to put the saved text -- note that
26016914Seric **			the pointers are static.
26158333Seric **		delimptr -- if non-NULL, set to the location of the
26258333Seric **			terminating delimiter.
263297Seric **
264297Seric **	Returns:
2653149Seric **		A pointer to a vector of tokens.
266297Seric **		NULL on error.
267297Seric */
268297Seric 
2698078Seric /* states and character types */
2708078Seric # define OPR		0	/* operator */
2718078Seric # define ATM		1	/* atom */
2728078Seric # define QST		2	/* in quoted string */
2738078Seric # define SPC		3	/* chewing up spaces */
2748078Seric # define ONE		4	/* pick up one character */
2753149Seric 
2768078Seric # define NSTATES	5	/* number of states */
2778078Seric # define TYPE		017	/* mask to select state type */
2788078Seric 
2798078Seric /* meta bits for table */
2808078Seric # define M		020	/* meta character; don't pass through */
2818078Seric # define B		040	/* cause a break */
2828078Seric # define MB		M|B	/* meta-break */
2838078Seric 
2848078Seric static short StateTab[NSTATES][NSTATES] =
2858078Seric {
2868087Seric    /*	oldst	chtype>	OPR	ATM	QST	SPC	ONE	*/
2879051Seric 	/*OPR*/		OPR|B,	ATM|B,	QST|B,	SPC|MB,	ONE|B,
2889051Seric 	/*ATM*/		OPR|B,	ATM,	QST|B,	SPC|MB,	ONE|B,
2899051Seric 	/*QST*/		QST,	QST,	OPR,	QST,	QST,
2908078Seric 	/*SPC*/		OPR,	ATM,	QST,	SPC|M,	ONE,
2918078Seric 	/*ONE*/		OPR,	OPR,	OPR,	OPR,	OPR,
2928078Seric };
2938078Seric 
2948078Seric # define NOCHAR		-1	/* signal nothing in lookahead token */
2958078Seric 
2963149Seric char **
29758333Seric prescan(addr, delim, pvpbuf, delimptr)
298297Seric 	char *addr;
299297Seric 	char delim;
30016914Seric 	char pvpbuf[];
30158333Seric 	char **delimptr;
302297Seric {
303297Seric 	register char *p;
3048078Seric 	register char *q;
3059346Seric 	register int c;
3063149Seric 	char **avp;
307297Seric 	bool bslashmode;
308297Seric 	int cmntcnt;
3098423Seric 	int anglecnt;
3103149Seric 	char *tok;
3118078Seric 	int state;
3128078Seric 	int newstate;
3138078Seric 	static char *av[MAXATOM+1];
31456678Seric 	extern int errno;
315297Seric 
31615253Seric 	/* make sure error messages don't have garbage on them */
31715253Seric 	errno = 0;
31815253Seric 
31916914Seric 	q = pvpbuf;
3203149Seric 	bslashmode = FALSE;
3217800Seric 	cmntcnt = 0;
3228423Seric 	anglecnt = 0;
3233149Seric 	avp = av;
32456678Seric 	state = ATM;
3258078Seric 	c = NOCHAR;
3268078Seric 	p = addr;
32756764Seric 	if (tTd(22, 11))
328297Seric 	{
3298078Seric 		printf("prescan: ");
3308078Seric 		xputs(p);
33123109Seric 		(void) putchar('\n');
3328078Seric 	}
3338078Seric 
3348078Seric 	do
3358078Seric 	{
3363149Seric 		/* read a token */
3373149Seric 		tok = q;
3388078Seric 		for (;;)
339297Seric 		{
3408078Seric 			/* store away any old lookahead character */
3418078Seric 			if (c != NOCHAR)
3428078Seric 			{
34315284Seric 				/* see if there is room */
34416914Seric 				if (q >= &pvpbuf[PSBUFSIZE - 5])
3458078Seric 				{
34658151Seric 					usrerr("553 Address too long");
34758333Seric 					if (delimptr != NULL)
34858333Seric 						*delimptr = p;
3498078Seric 					return (NULL);
3508078Seric 				}
35115284Seric 
35215284Seric 				/* squirrel it away */
3538078Seric 				*q++ = c;
3548078Seric 			}
3558078Seric 
3568078Seric 			/* read a new input character */
3578078Seric 			c = *p++;
35857631Seric 			if (c == '\0')
35956764Seric 			{
36056764Seric 				/* diagnose and patch up bad syntax */
36156764Seric 				if (state == QST)
36256764Seric 				{
36358151Seric 					usrerr("553 Unbalanced '\"'");
36456764Seric 					c = '"';
36556764Seric 				}
36656764Seric 				else if (cmntcnt > 0)
36756764Seric 				{
36858151Seric 					usrerr("553 Unbalanced '('");
36956764Seric 					c = ')';
37056764Seric 				}
37156764Seric 				else if (anglecnt > 0)
37256764Seric 				{
37356764Seric 					c = '>';
37458151Seric 					usrerr("553 Unbalanced '<'");
37556764Seric 				}
37656764Seric 				else
37756764Seric 					break;
37815284Seric 
37956764Seric 				p--;
38056764Seric 			}
38157631Seric 			else if (c == delim && anglecnt <= 0 &&
38257631Seric 					cmntcnt <= 0 && state != QST)
38357631Seric 				break;
38456764Seric 
3858078Seric 			if (tTd(22, 101))
3868078Seric 				printf("c=%c, s=%d; ", c, state);
3878078Seric 
3883149Seric 			/* chew up special characters */
3893149Seric 			*q = '\0';
3903149Seric 			if (bslashmode)
3913149Seric 			{
39224944Seric 				/* kludge \! for naive users */
39358061Seric 				if (cmntcnt > 0)
39458061Seric 					c = NOCHAR;
39558061Seric 				else if (c != '!')
39656678Seric 					*q++ = '\\';
3973149Seric 				bslashmode = FALSE;
39856678Seric 				continue;
3993149Seric 			}
40056678Seric 
40156678Seric 			if (c == '\\')
4023149Seric 			{
4033149Seric 				bslashmode = TRUE;
4048078Seric 				c = NOCHAR;
40556678Seric 				continue;
4063149Seric 			}
40756678Seric 			else if (state == QST)
4088514Seric 			{
4098514Seric 				/* do nothing, just avoid next clauses */
4108514Seric 			}
4118078Seric 			else if (c == '(')
4124100Seric 			{
4138078Seric 				cmntcnt++;
4148078Seric 				c = NOCHAR;
4154100Seric 			}
4168078Seric 			else if (c == ')')
4173149Seric 			{
4188078Seric 				if (cmntcnt <= 0)
4193149Seric 				{
42058151Seric 					usrerr("553 Unbalanced ')'");
42158333Seric 					if (delimptr != NULL)
42258333Seric 						*delimptr = p;
4238078Seric 					return (NULL);
4243149Seric 				}
4258078Seric 				else
4268078Seric 					cmntcnt--;
4278078Seric 			}
4288078Seric 			else if (cmntcnt > 0)
4298078Seric 				c = NOCHAR;
4308423Seric 			else if (c == '<')
4318423Seric 				anglecnt++;
4328423Seric 			else if (c == '>')
4338423Seric 			{
4348423Seric 				if (anglecnt <= 0)
4358423Seric 				{
43658151Seric 					usrerr("553 Unbalanced '>'");
43758333Seric 					if (delimptr != NULL)
43858333Seric 						*delimptr = p;
4398423Seric 					return (NULL);
4408423Seric 				}
4418423Seric 				anglecnt--;
4428423Seric 			}
44358050Seric 			else if (delim == ' ' && isascii(c) && isspace(c))
44411423Seric 				c = ' ';
4453149Seric 
4468078Seric 			if (c == NOCHAR)
4478078Seric 				continue;
4483149Seric 
4498078Seric 			/* see if this is end of input */
45011405Seric 			if (c == delim && anglecnt <= 0 && state != QST)
4513149Seric 				break;
4523149Seric 
4538078Seric 			newstate = StateTab[state][toktype(c)];
4548078Seric 			if (tTd(22, 101))
4558078Seric 				printf("ns=%02o\n", newstate);
4568078Seric 			state = newstate & TYPE;
4578078Seric 			if (bitset(M, newstate))
4588078Seric 				c = NOCHAR;
4598078Seric 			if (bitset(B, newstate))
4604228Seric 				break;
461297Seric 		}
4623149Seric 
4633149Seric 		/* new token */
4648078Seric 		if (tok != q)
4651378Seric 		{
4668078Seric 			*q++ = '\0';
4678078Seric 			if (tTd(22, 36))
468297Seric 			{
4698078Seric 				printf("tok=");
4708078Seric 				xputs(tok);
47123109Seric 				(void) putchar('\n');
472297Seric 			}
4738078Seric 			if (avp >= &av[MAXATOM])
474297Seric 			{
47558151Seric 				syserr("553 prescan: too many tokens");
47658333Seric 				if (delimptr != NULL)
47758333Seric 					*delimptr = p;
4788078Seric 				return (NULL);
479297Seric 			}
4808078Seric 			*avp++ = tok;
481297Seric 		}
4828423Seric 	} while (c != '\0' && (c != delim || anglecnt > 0));
4833149Seric 	*avp = NULL;
48458333Seric 	p--;
48558333Seric 	if (delimptr != NULL)
48658333Seric 		*delimptr = p;
48756764Seric 	if (tTd(22, 12))
48856764Seric 	{
48956764Seric 		printf("prescan==>");
49056764Seric 		printav(av);
49156764Seric 	}
49258546Seric 	if (av[0] == NULL)
49358546Seric 		return (NULL);
49458403Seric 	return (av);
4953149Seric }
4963149Seric /*
4973149Seric **  TOKTYPE -- return token type
4983149Seric **
4993149Seric **	Parameters:
5003149Seric **		c -- the character in question.
5013149Seric **
5023149Seric **	Returns:
5033149Seric **		Its type.
5043149Seric **
5053149Seric **	Side Effects:
5063149Seric **		none.
5073149Seric */
508297Seric 
5093149Seric toktype(c)
51058050Seric 	register int c;
5113149Seric {
5123380Seric 	static char buf[50];
5133382Seric 	static bool firstime = TRUE;
5143380Seric 
5153382Seric 	if (firstime)
5163380Seric 	{
5173382Seric 		firstime = FALSE;
51858050Seric 		expand("\201o", buf, &buf[sizeof buf - 1], CurEnv);
5197005Seric 		(void) strcat(buf, DELIMCHARS);
5203380Seric 	}
52158050Seric 	c &= 0377;
52256327Seric 	if (c == MATCHCLASS || c == MATCHREPL || c == MATCHNCLASS)
5238078Seric 		return (ONE);
52459027Seric 	if (c == MACRODEXPAND)
52559027Seric 		return (ONE);
5268078Seric 	if (c == '"')
5278078Seric 		return (QST);
52858050Seric 	if ((c & 0340) == 0200)
52958050Seric 		return (OPR);
5304100Seric 	if (!isascii(c))
5318078Seric 		return (ATM);
5328078Seric 	if (isspace(c) || c == ')')
5338078Seric 		return (SPC);
53458050Seric 	if (strchr(buf, c) != NULL)
5358078Seric 		return (OPR);
5368078Seric 	return (ATM);
5373149Seric }
5383149Seric /*
5393149Seric **  REWRITE -- apply rewrite rules to token vector.
5403149Seric **
5414476Seric **	This routine is an ordered production system.  Each rewrite
5424476Seric **	rule has a LHS (called the pattern) and a RHS (called the
5434476Seric **	rewrite); 'rwr' points the the current rewrite rule.
5444476Seric **
5454476Seric **	For each rewrite rule, 'avp' points the address vector we
5464476Seric **	are trying to match against, and 'pvp' points to the pattern.
5478058Seric **	If pvp points to a special match value (MATCHZANY, MATCHANY,
5489585Seric **	MATCHONE, MATCHCLASS, MATCHNCLASS) then the address in avp
5499585Seric **	matched is saved away in the match vector (pointed to by 'mvp').
5504476Seric **
5514476Seric **	When a match between avp & pvp does not match, we try to
5529585Seric **	back out.  If we back up over MATCHONE, MATCHCLASS, or MATCHNCLASS
5534476Seric **	we must also back out the match in mvp.  If we reach a
5548058Seric **	MATCHANY or MATCHZANY we just extend the match and start
5558058Seric **	over again.
5564476Seric **
5574476Seric **	When we finally match, we rewrite the address vector
5584476Seric **	and try over again.
5594476Seric **
5603149Seric **	Parameters:
5613149Seric **		pvp -- pointer to token vector.
56259027Seric **		ruleset -- the ruleset to use for rewriting.
56359027Seric **		e -- the current envelope.
5643149Seric **
5653149Seric **	Returns:
56659084Seric **		A status code.  If EX_TEMPFAIL, higher level code should
56759084Seric **			attempt recovery.
5683149Seric **
5693149Seric **	Side Effects:
5703149Seric **		pvp is modified.
5713149Seric */
5722091Seric 
5733149Seric struct match
5743149Seric {
5754468Seric 	char	**first;	/* first token matched */
5764468Seric 	char	**last;		/* last token matched */
57758825Seric 	char	**pattern;	/* pointer to pattern */
5783149Seric };
5793149Seric 
5804468Seric # define MAXMATCH	9	/* max params per rewrite */
5813149Seric 
5823149Seric 
58359084Seric int
58459027Seric rewrite(pvp, ruleset, e)
5853149Seric 	char **pvp;
5864070Seric 	int ruleset;
58759027Seric 	register ENVELOPE *e;
5883149Seric {
5893149Seric 	register char *ap;		/* address pointer */
5903149Seric 	register char *rp;		/* rewrite pointer */
5913149Seric 	register char **avp;		/* address vector pointer */
5923149Seric 	register char **rvp;		/* rewrite vector pointer */
5938058Seric 	register struct match *mlp;	/* cur ptr into mlist */
5948058Seric 	register struct rewrite *rwr;	/* pointer to current rewrite rule */
59558866Seric 	int ruleno;			/* current rule number */
59659084Seric 	int rstat = EX_OK;		/* return status */
59756678Seric 	struct match mlist[MAXMATCH];	/* stores match on LHS */
5983149Seric 	char *npvp[MAXATOM+1];		/* temporary space for rebuild */
59959027Seric 	extern char *macvalue();
6003149Seric 
6019279Seric 	if (OpMode == MD_TEST || tTd(21, 2))
6023149Seric 	{
6038959Seric 		printf("rewrite: ruleset %2d   input:", ruleset);
60456678Seric 		printav(pvp);
6053149Seric 	}
60656678Seric 	if (ruleset < 0 || ruleset >= MAXRWSETS)
60756326Seric 	{
60858151Seric 		syserr("554 rewrite: illegal ruleset number %d", ruleset);
60959084Seric 		return EX_CONFIG;
61056326Seric 	}
61156678Seric 	if (pvp == NULL)
61259084Seric 		return EX_USAGE;
61356326Seric 
6143149Seric 	/*
61556678Seric 	**  Run through the list of rewrite rules, applying
61656678Seric 	**	any that match.
6173149Seric 	*/
6183149Seric 
61958866Seric 	ruleno = 1;
6204070Seric 	for (rwr = RewriteRules[ruleset]; rwr != NULL; )
6213149Seric 	{
62256678Seric 		int loopcount = 0;
62356678Seric 
6247675Seric 		if (tTd(21, 12))
625297Seric 		{
6268069Seric 			printf("-----trying rule:");
62756678Seric 			printav(rwr->r_lhs);
6283149Seric 		}
6293149Seric 
6303149Seric 		/* try to match on this rule */
6314468Seric 		mlp = mlist;
6328058Seric 		rvp = rwr->r_lhs;
6338058Seric 		avp = pvp;
63458866Seric 		if (++loopcount > 100)
6353149Seric 		{
63658866Seric 			syserr("554 Infinite loop in ruleset %d, rule %d",
63758866Seric 				ruleset, ruleno);
63858866Seric 			if (tTd(21, 1))
63952637Seric 			{
64056678Seric 				printf("workspace: ");
64156678Seric 				printav(pvp);
64252637Seric 			}
64358866Seric 			break;
64458866Seric 		}
64558866Seric 
64658866Seric 		while ((ap = *avp) != NULL || *rvp != NULL)
64758866Seric 		{
6483149Seric 			rp = *rvp;
6498058Seric 			if (tTd(21, 35))
6508058Seric 			{
65158825Seric 				printf("ADVANCE rp=");
65257531Seric 				xputs(rp);
65357532Seric 				printf(", ap=");
6548058Seric 				xputs(ap);
6558069Seric 				printf("\n");
6568058Seric 			}
65756678Seric 			if (rp == NULL)
65856326Seric 			{
6593149Seric 				/* end-of-pattern before end-of-address */
6608058Seric 				goto backup;
66156678Seric 			}
66258173Seric 			if (ap == NULL && (*rp & 0377) != MATCHZANY &&
66358827Seric 			    (*rp & 0377) != MATCHZERO)
66456326Seric 			{
66558825Seric 				/* end-of-input with patterns left */
66658825Seric 				goto backup;
667297Seric 			}
66856326Seric 
66958050Seric 			switch (*rp & 0377)
6708058Seric 			{
67156678Seric 				register STAB *s;
67258814Seric 				char buf[MAXLINE];
67356326Seric 
67456678Seric 			  case MATCHCLASS:
67558825Seric 				/* match any phrase in a class */
67658825Seric 				mlp->pattern = rvp;
67758814Seric 				mlp->first = avp;
67858814Seric 	extendclass:
67958825Seric 				ap = *avp;
68058825Seric 				if (ap == NULL)
68158814Seric 					goto backup;
68258814Seric 				mlp->last = avp++;
68358814Seric 				cataddr(mlp->first, mlp->last, buf, sizeof buf, '\0');
68458814Seric 				s = stab(buf, ST_CLASS, ST_FIND);
68556678Seric 				if (s == NULL || !bitnset(rp[1], s->s_class))
68656326Seric 				{
68758825Seric 					if (tTd(21, 36))
68858825Seric 					{
68958825Seric 						printf("EXTEND  rp=");
69058825Seric 						xputs(rp);
69158825Seric 						printf(", ap=");
69258825Seric 						xputs(ap);
69358825Seric 						printf("\n");
69458825Seric 					}
69558825Seric 					goto extendclass;
69656326Seric 				}
69758825Seric 				if (tTd(21, 36))
69858825Seric 					printf("CLMATCH\n");
69958814Seric 				mlp++;
70058814Seric 				break;
7014060Seric 
70258825Seric 			  case MATCHNCLASS:
70358825Seric 				/* match any token not in a class */
70458825Seric 				s = stab(ap, ST_CLASS, ST_FIND);
70558825Seric 				if (s != NULL && bitnset(rp[1], s->s_class))
70658825Seric 					goto backup;
70758825Seric 
70858825Seric 				/* fall through */
70958825Seric 
71056678Seric 			  case MATCHONE:
71156678Seric 			  case MATCHANY:
71256678Seric 				/* match exactly one token */
71358825Seric 				mlp->pattern = rvp;
71456678Seric 				mlp->first = avp;
71556678Seric 				mlp->last = avp++;
7168058Seric 				mlp++;
71756678Seric 				break;
7188058Seric 
71956678Seric 			  case MATCHZANY:
72056678Seric 				/* match zero or more tokens */
72158825Seric 				mlp->pattern = rvp;
72256678Seric 				mlp->first = avp;
72356678Seric 				mlp->last = avp - 1;
72456678Seric 				mlp++;
72556678Seric 				break;
72656326Seric 
72758827Seric 			  case MATCHZERO:
72858173Seric 				/* match zero tokens */
72958173Seric 				break;
73058173Seric 
73159027Seric 			  case MACRODEXPAND:
73259027Seric 				/*
73359027Seric 				**  Match against run-time macro.
73459027Seric 				**  This algorithm is broken for the
73559027Seric 				**  general case (no recursive macros,
73659027Seric 				**  improper tokenization) but should
73759027Seric 				**  work for the usual cases.
73859027Seric 				*/
73959027Seric 
74059027Seric 				ap = macvalue(rp[1], e);
74159027Seric 				mlp->first = avp;
74259027Seric 				if (tTd(21, 2))
74359027Seric 					printf("rewrite: LHS $&%c => \"%s\"\n",
74459027Seric 						rp[1],
74559027Seric 						ap == NULL ? "(NULL)" : ap);
74659027Seric 
74759027Seric 				if (ap == NULL)
74859027Seric 					break;
74959027Seric 				while (*ap != NULL)
75059027Seric 				{
75159027Seric 					if (*avp == NULL ||
75259027Seric 					    strncasecmp(ap, *avp, strlen(*avp)) != 0)
75359027Seric 					{
75459027Seric 						/* no match */
75559027Seric 						avp = mlp->first;
75659027Seric 						goto backup;
75759027Seric 					}
75859027Seric 					ap += strlen(*avp++);
75959027Seric 				}
76059027Seric 
76159027Seric 				/* match */
76259027Seric 				break;
76359027Seric 
76456678Seric 			  default:
76556678Seric 				/* must have exact match */
76656678Seric 				if (strcasecmp(rp, ap))
7678058Seric 					goto backup;
7684468Seric 				avp++;
76956678Seric 				break;
7703149Seric 			}
7713149Seric 
77256678Seric 			/* successful match on this token */
7733149Seric 			rvp++;
7743149Seric 			continue;
7753149Seric 
77658825Seric 	  backup:
77756678Seric 			/* match failed -- back up */
77858825Seric 			while (--mlp >= mlist)
7793149Seric 			{
78058825Seric 				rvp = mlp->pattern;
78156678Seric 				rp = *rvp;
78258825Seric 				avp = mlp->last + 1;
78358825Seric 				ap = *avp;
78458825Seric 
78558825Seric 				if (tTd(21, 36))
78658825Seric 				{
78758825Seric 					printf("BACKUP  rp=");
78858825Seric 					xputs(rp);
78958825Seric 					printf(", ap=");
79058825Seric 					xputs(ap);
79158825Seric 					printf("\n");
79258825Seric 				}
79358825Seric 
79458825Seric 				if (ap == NULL)
79558825Seric 				{
79658825Seric 					/* run off the end -- back up again */
79758825Seric 					continue;
79858825Seric 				}
79958050Seric 				if ((*rp & 0377) == MATCHANY ||
80058050Seric 				    (*rp & 0377) == MATCHZANY)
8014468Seric 				{
80256678Seric 					/* extend binding and continue */
80358825Seric 					mlp->last = avp++;
80456678Seric 					rvp++;
80558825Seric 					mlp++;
80656678Seric 					break;
8074468Seric 				}
80858825Seric 				if ((*rp & 0377) == MATCHCLASS)
80956678Seric 				{
81058814Seric 					/* extend binding and try again */
81158825Seric 					mlp->last = avp++;
81258814Seric 					goto extendclass;
81358814Seric 				}
8143149Seric 			}
8153149Seric 
81658825Seric 			if (mlp < mlist)
81756678Seric 			{
81856678Seric 				/* total failure to match */
81956326Seric 				break;
8203149Seric 			}
821297Seric 		}
8223149Seric 
8233149Seric 		/*
82456678Seric 		**  See if we successfully matched
8253149Seric 		*/
8263149Seric 
82758827Seric 		if (mlp < mlist || *rvp != NULL)
8283149Seric 		{
8299374Seric 			if (tTd(21, 10))
8309374Seric 				printf("----- rule fails\n");
8319374Seric 			rwr = rwr->r_next;
83258866Seric 			ruleno++;
8339374Seric 			continue;
8349374Seric 		}
8353149Seric 
8369374Seric 		rvp = rwr->r_rhs;
8379374Seric 		if (tTd(21, 12))
8389374Seric 		{
8399374Seric 			printf("-----rule matches:");
84056678Seric 			printav(rvp);
8419374Seric 		}
8429374Seric 
8439374Seric 		rp = *rvp;
84458050Seric 		if ((*rp & 0377) == CANONUSER)
8459374Seric 		{
8469374Seric 			rvp++;
8479374Seric 			rwr = rwr->r_next;
84858866Seric 			ruleno++;
8499374Seric 		}
85058050Seric 		else if ((*rp & 0377) == CANONHOST)
8519374Seric 		{
8529374Seric 			rvp++;
8539374Seric 			rwr = NULL;
8549374Seric 		}
85558050Seric 		else if ((*rp & 0377) == CANONNET)
8569374Seric 			rwr = NULL;
8579374Seric 
8589374Seric 		/* substitute */
8599374Seric 		for (avp = npvp; *rvp != NULL; rvp++)
8609374Seric 		{
8619374Seric 			register struct match *m;
8629374Seric 			register char **pp;
8639374Seric 
8648058Seric 			rp = *rvp;
86558050Seric 			if ((*rp & 0377) == MATCHREPL)
8668058Seric 			{
86716914Seric 				/* substitute from LHS */
86816914Seric 				m = &mlist[rp[1] - '1'];
86956678Seric 				if (m < mlist || m >= mlp)
8709374Seric 				{
87158151Seric 					syserr("554 rewrite: ruleset %d: replacement $%c out of bounds",
87256326Seric 						ruleset, rp[1]);
87359084Seric 					return EX_CONFIG;
8749374Seric 				}
87516914Seric 				if (tTd(21, 15))
87616914Seric 				{
87716914Seric 					printf("$%c:", rp[1]);
87816914Seric 					pp = m->first;
87956678Seric 					while (pp <= m->last)
88016914Seric 					{
88116914Seric 						printf(" %x=\"", *pp);
88216914Seric 						(void) fflush(stdout);
88316914Seric 						printf("%s\"", *pp++);
88416914Seric 					}
88516914Seric 					printf("\n");
88616914Seric 				}
8879374Seric 				pp = m->first;
88856678Seric 				while (pp <= m->last)
8893149Seric 				{
89016914Seric 					if (avp >= &npvp[MAXATOM])
89156678Seric 					{
89258151Seric 						syserr("554 rewrite: expansion too long");
89359084Seric 						return EX_DATAERR;
89456678Seric 					}
89516914Seric 					*avp++ = *pp++;
8963149Seric 				}
8973149Seric 			}
89816914Seric 			else
8998226Seric 			{
90016914Seric 				/* vanilla replacement */
9019374Seric 				if (avp >= &npvp[MAXATOM])
90216889Seric 				{
90356678Seric 	toolong:
90458151Seric 					syserr("554 rewrite: expansion too long");
90559084Seric 					return EX_DATAERR;
90616889Seric 				}
90759027Seric 				if ((*rp & 0377) != MACRODEXPAND)
90859027Seric 					*avp++ = rp;
90959027Seric 				else
91059027Seric 				{
91159027Seric 					*avp = macvalue(rp[1], e);
91259027Seric 					if (tTd(21, 2))
91359027Seric 						printf("rewrite: RHS $&%c => \"%s\"\n",
91459027Seric 							rp[1],
91559027Seric 							*avp == NULL ? "(NULL)" : *avp);
91659027Seric 					if (*avp != NULL)
91759027Seric 						avp++;
91859027Seric 				}
9198226Seric 			}
9209374Seric 		}
9219374Seric 		*avp++ = NULL;
92216914Seric 
92316914Seric 		/*
92456678Seric 		**  Check for any hostname/keyword lookups.
92516914Seric 		*/
92616914Seric 
92716914Seric 		for (rvp = npvp; *rvp != NULL; rvp++)
92816914Seric 		{
92956678Seric 			char **hbrvp;
93016914Seric 			char **xpvp;
93116914Seric 			int trsize;
93217473Seric 			char *olddelimchar;
93356678Seric 			char *replac;
93456678Seric 			int endtoken;
93556678Seric 			STAB *map;
93656678Seric 			char *mapname;
93756678Seric 			char **key_rvp;
93856678Seric 			char **arg_rvp;
93956678Seric 			char **default_rvp;
94056678Seric 			char buf[MAXNAME + 1];
94116914Seric 			char *pvpb1[MAXATOM + 1];
94256823Seric 			char *argvect[10];
94317174Seric 			char pvpbuf[PSBUFSIZE];
94416914Seric 
94558050Seric 			if ((**rvp & 0377) != HOSTBEGIN &&
94658050Seric 			    (**rvp & 0377) != LOOKUPBEGIN)
94716914Seric 				continue;
94816914Seric 
94916914Seric 			/*
95056678Seric 			**  Got a hostname/keyword lookup.
95116914Seric 			**
95216914Seric 			**	This could be optimized fairly easily.
95316914Seric 			*/
95416914Seric 
95516914Seric 			hbrvp = rvp;
95658050Seric 			if ((**rvp & 0377) == HOSTBEGIN)
95756327Seric 			{
95856678Seric 				endtoken = HOSTEND;
95956678Seric 				mapname = "host";
96056327Seric 			}
96156326Seric 			else
96256327Seric 			{
96356678Seric 				endtoken = LOOKUPEND;
96456678Seric 				mapname = *++rvp;
96556327Seric 			}
96656678Seric 			map = stab(mapname, ST_MAP, ST_FIND);
96756678Seric 			if (map == NULL)
96858151Seric 				syserr("554 rewrite: map %s not found", mapname);
96956678Seric 
97056678Seric 			/* extract the match part */
97156678Seric 			key_rvp = ++rvp;
97256823Seric 			default_rvp = NULL;
97356823Seric 			arg_rvp = argvect;
97456823Seric 			xpvp = NULL;
97556823Seric 			replac = pvpbuf;
97658050Seric 			while (*rvp != NULL && (**rvp & 0377) != endtoken)
97753654Seric 			{
97858050Seric 				int nodetype = **rvp & 0377;
97956823Seric 
98056823Seric 				if (nodetype != CANONHOST && nodetype != CANONUSER)
98156678Seric 				{
98256823Seric 					rvp++;
98356823Seric 					continue;
98456823Seric 				}
98556823Seric 
98656823Seric 				*rvp++ = NULL;
98756823Seric 
98856823Seric 				if (xpvp != NULL)
98956823Seric 				{
99058814Seric 					cataddr(xpvp, NULL, replac,
99158082Seric 						&pvpbuf[sizeof pvpbuf] - replac,
99258082Seric 						'\0');
99356823Seric 					*++arg_rvp = replac;
99456823Seric 					replac += strlen(replac) + 1;
99556823Seric 					xpvp = NULL;
99656823Seric 				}
99756823Seric 				switch (nodetype)
99856823Seric 				{
99956678Seric 				  case CANONHOST:
100056823Seric 					xpvp = rvp;
100156678Seric 					break;
100256678Seric 
100356678Seric 				  case CANONUSER:
100456678Seric 					default_rvp = rvp;
100556678Seric 					break;
100656678Seric 				}
100753654Seric 			}
100816914Seric 			if (*rvp != NULL)
100916914Seric 				*rvp++ = NULL;
101056823Seric 			if (xpvp != NULL)
101156823Seric 			{
101258814Seric 				cataddr(xpvp, NULL, replac,
101358082Seric 					&pvpbuf[sizeof pvpbuf] - replac,
101458082Seric 					'\0');
101556823Seric 				*++arg_rvp = replac;
101656823Seric 			}
101756823Seric 			*++arg_rvp = NULL;
101816914Seric 
101916914Seric 			/* save the remainder of the input string */
102016914Seric 			trsize = (int) (avp - rvp + 1) * sizeof *rvp;
102116914Seric 			bcopy((char *) rvp, (char *) pvpb1, trsize);
102216914Seric 
102356678Seric 			/* look it up */
102458814Seric 			cataddr(key_rvp, NULL, buf, sizeof buf, '\0');
102556823Seric 			argvect[0] = buf;
102656678Seric 			if (map != NULL && bitset(MF_VALID, map->s_map.map_flags))
102756836Seric 			{
102856836Seric 				int bsize = sizeof buf - 1;
102959084Seric 				auto int stat = EX_OK;
103056836Seric 
103156836Seric 				if (map->s_map.map_app != NULL)
103256836Seric 					bsize -= strlen(map->s_map.map_app);
103358796Seric 				if (tTd(60, 1))
103458796Seric 					printf("map_lookup(%s, %s) => ",
103558796Seric 						mapname, buf);
103656836Seric 				replac = (*map->s_map.map_class->map_lookup)(&map->s_map,
103759084Seric 						buf, sizeof buf - 1, argvect,
103859084Seric 						&stat);
103956836Seric 				if (replac != NULL && map->s_map.map_app != NULL)
104056836Seric 					strcat(replac, map->s_map.map_app);
104158796Seric 				if (tTd(60, 1))
104259084Seric 					printf("%s (%d)\n",
104359084Seric 						replac ? replac : "NOT FOUND",
104459084Seric 						stat);
104559084Seric 
104659084Seric 				/* should recover if stat == EX_TEMPFAIL */
104759084Seric 				if (stat == EX_TEMPFAIL)
104859084Seric 					rstat = stat;
104956836Seric 			}
105053654Seric 			else
105156678Seric 				replac = NULL;
105256678Seric 
105356678Seric 			/* if no replacement, use default */
105456823Seric 			if (replac == NULL && default_rvp != NULL)
105556823Seric 			{
105656823Seric 				char buf2[sizeof buf];
105756823Seric 
105856823Seric 				/* rewrite the default with % translations */
105958814Seric 				cataddr(default_rvp, NULL, buf2, sizeof buf2, '\0');
106056823Seric 				map_rewrite(buf2, sizeof buf2, buf, sizeof buf,
106156823Seric 					argvect);
106256823Seric 				replac = buf;
106356823Seric 			}
106456823Seric 
106556678Seric 			if (replac == NULL)
106651317Seric 			{
106756823Seric 				xpvp = key_rvp;
106853654Seric 			}
106956678Seric 			else
107053654Seric 			{
107156678Seric 				/* scan the new replacement */
107258333Seric 				xpvp = prescan(replac, '\0', pvpbuf, NULL);
107353654Seric 				if (xpvp == NULL)
107451317Seric 				{
107558403Seric 					/* prescan already printed error */
107659084Seric 					return EX_DATAERR;
107756678Seric 				}
107851317Seric 			}
107951317Seric 
108016914Seric 			/* append it to the token list */
108156678Seric 			for (avp = hbrvp; *xpvp != NULL; xpvp++)
108256678Seric 			{
108317174Seric 				*avp++ = newstr(*xpvp);
108416920Seric 				if (avp >= &npvp[MAXATOM])
108516914Seric 					goto toolong;
108617174Seric 			}
108716914Seric 
108816914Seric 			/* restore the old trailing information */
108956678Seric 			for (xpvp = pvpb1; (*avp++ = *xpvp++) != NULL; )
109016920Seric 				if (avp >= &npvp[MAXATOM])
109116914Seric 					goto toolong;
109217174Seric 
109356678Seric 			break;
109416914Seric 		}
109516914Seric 
109616914Seric 		/*
109716914Seric 		**  Check for subroutine calls.
109816914Seric 		*/
109916914Seric 
110058050Seric 		if (*npvp != NULL && (**npvp & 0377) == CALLSUBR)
110156678Seric 		{
110259084Seric 			int stat;
110359084Seric 
110456678Seric 			bcopy((char *) &npvp[2], (char *) pvp,
110556678Seric 				(int) (avp - npvp - 2) * sizeof *avp);
110656678Seric 			if (tTd(21, 3))
110756678Seric 				printf("-----callsubr %s\n", npvp[1]);
110859084Seric 			stat = rewrite(pvp, atoi(npvp[1]), e);
110959084Seric 			if (rstat == EX_OK || stat == EX_TEMPFAIL)
111059084Seric 				rstat = stat;
111156678Seric 		}
111256678Seric 		else
111356678Seric 		{
111417348Seric 			bcopy((char *) npvp, (char *) pvp,
111516900Seric 				(int) (avp - npvp) * sizeof *avp);
111656678Seric 		}
11179374Seric 		if (tTd(21, 4))
11189374Seric 		{
11199374Seric 			printf("rewritten as:");
112056678Seric 			printav(pvp);
11219374Seric 		}
1122297Seric 	}
11238069Seric 
11249279Seric 	if (OpMode == MD_TEST || tTd(21, 2))
11258069Seric 	{
11268959Seric 		printf("rewrite: ruleset %2d returns:", ruleset);
112756678Seric 		printav(pvp);
11288069Seric 	}
112959084Seric 
113059084Seric 	return rstat;
11313149Seric }
11323149Seric /*
11333149Seric **  BUILDADDR -- build address from token vector.
11343149Seric **
11353149Seric **	Parameters:
11363149Seric **		tv -- token vector.
11373149Seric **		a -- pointer to address descriptor to fill.
11383149Seric **			If NULL, one will be allocated.
113958966Seric **		e -- the current envelope.
11403149Seric **
11413149Seric **	Returns:
11424279Seric **		NULL if there was an error.
11434279Seric **		'a' otherwise.
11443149Seric **
11453149Seric **	Side Effects:
11463149Seric **		fills in 'a'
11473149Seric */
11483149Seric 
114957249Seric struct errcodes
115057249Seric {
115157249Seric 	char	*ec_name;		/* name of error code */
115257249Seric 	int	ec_code;		/* numeric code */
115357249Seric } ErrorCodes[] =
115457249Seric {
115557249Seric 	"usage",	EX_USAGE,
115657249Seric 	"nouser",	EX_NOUSER,
115757249Seric 	"nohost",	EX_NOHOST,
115857249Seric 	"unavailable",	EX_UNAVAILABLE,
115957249Seric 	"software",	EX_SOFTWARE,
116057249Seric 	"tempfail",	EX_TEMPFAIL,
116157249Seric 	"protocol",	EX_PROTOCOL,
116257249Seric #ifdef EX_CONFIG
116357249Seric 	"config",	EX_CONFIG,
116457249Seric #endif
116557249Seric 	NULL,		EX_UNAVAILABLE,
116657249Seric };
116757249Seric 
116856678Seric ADDRESS *
116958966Seric buildaddr(tv, a, e)
11703149Seric 	register char **tv;
11713149Seric 	register ADDRESS *a;
117258966Seric 	register ENVELOPE *e;
11733149Seric {
11743149Seric 	struct mailer **mp;
11753149Seric 	register struct mailer *m;
117658008Seric 	char *bp;
117758008Seric 	int spaceleft;
117857402Seric 	static char buf[MAXNAME];
11793149Seric 
11803149Seric 	if (a == NULL)
11813149Seric 		a = (ADDRESS *) xalloc(sizeof *a);
118216889Seric 	bzero((char *) a, sizeof *a);
11833149Seric 
11843149Seric 	/* figure out what net/mailer to use */
118558050Seric 	if ((**tv & 0377) != CANONNET)
11864279Seric 	{
118758151Seric 		syserr("554 buildaddr: no net");
11884279Seric 		return (NULL);
11894279Seric 	}
11903149Seric 	tv++;
119158680Seric 	if (strcasecmp(*tv, "error") == 0)
11924279Seric 	{
119358050Seric 		if ((**++tv & 0377) == CANONHOST)
119410183Seric 		{
119557249Seric 			register struct errcodes *ep;
119657249Seric 
119758050Seric 			if (isascii(**++tv) && isdigit(**tv))
119857249Seric 			{
119957249Seric 				setstat(atoi(*tv));
120057249Seric 			}
120157249Seric 			else
120257249Seric 			{
120357249Seric 				for (ep = ErrorCodes; ep->ec_name != NULL; ep++)
120457249Seric 					if (strcasecmp(ep->ec_name, *tv) == 0)
120557249Seric 						break;
120657249Seric 				setstat(ep->ec_code);
120757249Seric 			}
120810183Seric 			tv++;
120910183Seric 		}
121058050Seric 		if ((**tv & 0377) != CANONUSER)
121158151Seric 			syserr("554 buildaddr: error: no user");
121258814Seric 		cataddr(++tv, NULL, buf, sizeof buf, ' ');
121358082Seric 		stripquotes(buf);
12144279Seric 		usrerr(buf);
121558966Seric 		if (e->e_message == NULL)
121658966Seric 			e->e_message = newstr(buf);
12174279Seric 		return (NULL);
12184279Seric 	}
121957402Seric 
12204598Seric 	for (mp = Mailer; (m = *mp++) != NULL; )
12213149Seric 	{
122258680Seric 		if (strcasecmp(m->m_name, *tv) == 0)
12233149Seric 			break;
12243149Seric 	}
12253149Seric 	if (m == NULL)
12264279Seric 	{
122758151Seric 		syserr("554 buildaddr: unknown mailer %s", *tv);
12284279Seric 		return (NULL);
12294279Seric 	}
12304598Seric 	a->q_mailer = m;
12313149Seric 
12323149Seric 	/* figure out what host (if any) */
123356678Seric 	tv++;
123458509Seric 	if ((**tv & 0377) == CANONHOST)
12353149Seric 	{
123658008Seric 		bp = buf;
123758008Seric 		spaceleft = sizeof buf - 1;
123858050Seric 		while (*++tv != NULL && (**tv & 0377) != CANONUSER)
123958008Seric 		{
124058008Seric 			int i = strlen(*tv);
124158008Seric 
124258008Seric 			if (i > spaceleft)
124358008Seric 			{
124458008Seric 				/* out of space for this address */
124558008Seric 				if (spaceleft >= 0)
124658151Seric 					syserr("554 buildaddr: host too long (%.40s...)",
124758008Seric 						buf);
124858008Seric 				i = spaceleft;
124958008Seric 				spaceleft = 0;
125058008Seric 			}
125158008Seric 			if (i <= 0)
125258008Seric 				continue;
125358008Seric 			bcopy(*tv, bp, i);
125458008Seric 			bp += i;
125558008Seric 			spaceleft -= i;
125658008Seric 		}
125758010Seric 		*bp = '\0';
12585704Seric 		a->q_host = newstr(buf);
12593149Seric 	}
126057249Seric 	else
126158509Seric 	{
126258509Seric 		if (!bitnset(M_LOCALMAILER, m->m_flags))
126358509Seric 		{
126458509Seric 			syserr("554 buildaddr: no host");
126558509Seric 			return (NULL);
126658509Seric 		}
126757249Seric 		a->q_host = NULL;
126858509Seric 	}
12693149Seric 
12703149Seric 	/* figure out the user */
127158050Seric 	if (*tv == NULL || (**tv & 0377) != CANONUSER)
12724279Seric 	{
127358151Seric 		syserr("554 buildaddr: no user");
12744279Seric 		return (NULL);
12754279Seric 	}
127657402Seric 	tv++;
127751317Seric 
127857402Seric 	/* do special mapping for local mailer */
127957402Seric 	if (m == LocalMailer && *tv != NULL)
128057402Seric 	{
128157454Seric 		register char *p = *tv;
128257454Seric 
128357454Seric 		if (*p == '"')
128457454Seric 			p++;
128557454Seric 		if (*p == '|')
128657402Seric 			a->q_mailer = m = ProgMailer;
128757454Seric 		else if (*p == '/')
128857402Seric 			a->q_mailer = m = FileMailer;
128957454Seric 		else if (*p == ':')
129057402Seric 		{
129157402Seric 			/* may be :include: */
129258814Seric 			cataddr(tv, NULL, buf, sizeof buf, '\0');
129358008Seric 			stripquotes(buf);
129458008Seric 			if (strncasecmp(buf, ":include:", 9) == 0)
129558008Seric 			{
129658008Seric 				/* if :include:, don't need further rewriting */
129757402Seric 				a->q_mailer = m = InclMailer;
129858008Seric 				a->q_user = &buf[9];
129958008Seric 				return (a);
130058008Seric 			}
130157402Seric 		}
130257402Seric 	}
130357402Seric 
130458008Seric 	if (m == LocalMailer && *tv != NULL && strcmp(*tv, "@") == 0)
130558008Seric 	{
130658008Seric 		tv++;
130758008Seric 		a->q_flags |= QNOTREMOTE;
130858008Seric 	}
130958008Seric 
131058673Seric 	/* do cleanup of final address */
131159084Seric 	(void) rewrite(tv, 4, e);
131219040Seric 
131319040Seric 	/* save the result for the command line/RCPT argument */
131458814Seric 	cataddr(tv, NULL, buf, sizeof buf, '\0');
13153149Seric 	a->q_user = buf;
13163149Seric 
131758670Seric 	/*
131858670Seric 	**  Do mapping to lower case as requested by mailer
131958670Seric 	*/
132058670Seric 
132158670Seric 	if (a->q_host != NULL && !bitnset(M_HST_UPPER, m->m_flags))
132258670Seric 		makelower(a->q_host);
132358670Seric 	if (!bitnset(M_USR_UPPER, m->m_flags))
132458670Seric 		makelower(a->q_user);
132558670Seric 
13263149Seric 	return (a);
13273149Seric }
13283188Seric /*
13294228Seric **  CATADDR -- concatenate pieces of addresses (putting in <LWSP> subs)
13304228Seric **
13314228Seric **	Parameters:
13324228Seric **		pvp -- parameter vector to rebuild.
133358814Seric **		evp -- last parameter to include.  Can be NULL to
133458814Seric **			use entire pvp.
13354228Seric **		buf -- buffer to build the string into.
13364228Seric **		sz -- size of buf.
133758082Seric **		spacesub -- the space separator character; if null,
133858082Seric **			use SpaceSub.
13394228Seric **
13404228Seric **	Returns:
13414228Seric **		none.
13424228Seric **
13434228Seric **	Side Effects:
13444228Seric **		Destroys buf.
13454228Seric */
13464228Seric 
134758814Seric cataddr(pvp, evp, buf, sz, spacesub)
13484228Seric 	char **pvp;
134958814Seric 	char **evp;
13504228Seric 	char *buf;
13514228Seric 	register int sz;
135258082Seric 	char spacesub;
13534228Seric {
13544228Seric 	bool oatomtok = FALSE;
135556678Seric 	bool natomtok = FALSE;
13564228Seric 	register int i;
13574228Seric 	register char *p;
13584228Seric 
135958082Seric 	if (spacesub == '\0')
136058082Seric 		spacesub = SpaceSub;
136158082Seric 
13628423Seric 	if (pvp == NULL)
13638423Seric 	{
136423109Seric 		(void) strcpy(buf, "");
13658423Seric 		return;
13668423Seric 	}
13674228Seric 	p = buf;
136811156Seric 	sz -= 2;
13694228Seric 	while (*pvp != NULL && (i = strlen(*pvp)) < sz)
13704228Seric 	{
13718078Seric 		natomtok = (toktype(**pvp) == ATM);
13724228Seric 		if (oatomtok && natomtok)
137358082Seric 			*p++ = spacesub;
13744228Seric 		(void) strcpy(p, *pvp);
13754228Seric 		oatomtok = natomtok;
13764228Seric 		p += i;
137711156Seric 		sz -= i + 1;
137858814Seric 		if (pvp++ == evp)
137958814Seric 			break;
13804228Seric 	}
13814228Seric 	*p = '\0';
13824228Seric }
13834228Seric /*
13843188Seric **  SAMEADDR -- Determine if two addresses are the same
13853188Seric **
13863188Seric **	This is not just a straight comparison -- if the mailer doesn't
13873188Seric **	care about the host we just ignore it, etc.
13883188Seric **
13893188Seric **	Parameters:
13903188Seric **		a, b -- pointers to the internal forms to compare.
13913188Seric **
13923188Seric **	Returns:
13933188Seric **		TRUE -- they represent the same mailbox.
13943188Seric **		FALSE -- they don't.
13953188Seric **
13963188Seric **	Side Effects:
13973188Seric **		none.
13983188Seric */
13993188Seric 
14003188Seric bool
14019374Seric sameaddr(a, b)
14023188Seric 	register ADDRESS *a;
14033188Seric 	register ADDRESS *b;
14043188Seric {
14053188Seric 	/* if they don't have the same mailer, forget it */
14063188Seric 	if (a->q_mailer != b->q_mailer)
14073188Seric 		return (FALSE);
14083188Seric 
14093188Seric 	/* if the user isn't the same, we can drop out */
141056678Seric 	if (strcmp(a->q_user, b->q_user) != 0)
14113188Seric 		return (FALSE);
14123188Seric 
141358438Seric 	/* if we have good uids for both but the differ, these are different */
141458438Seric 	if (bitset(QGOODUID, a->q_flags & b->q_flags) && a->q_uid != b->q_uid)
141558438Seric 		return (FALSE);
141658438Seric 
141758509Seric 	/* otherwise compare hosts (but be careful for NULL ptrs) */
141858509Seric 	if (a->q_host == b->q_host)
141958509Seric 	{
142058509Seric 		/* probably both null pointers */
14213188Seric 		return (TRUE);
142258509Seric 	}
14233188Seric 	if (a->q_host == NULL || b->q_host == NULL)
142458509Seric 	{
142558509Seric 		/* only one is a null pointer */
14263188Seric 		return (FALSE);
142758509Seric 	}
142856678Seric 	if (strcmp(a->q_host, b->q_host) != 0)
14293188Seric 		return (FALSE);
14303188Seric 
14313188Seric 	return (TRUE);
14323188Seric }
14333234Seric /*
14343234Seric **  PRINTADDR -- print address (for debugging)
14353234Seric **
14363234Seric **	Parameters:
14373234Seric **		a -- the address to print
14383234Seric **		follow -- follow the q_next chain.
14393234Seric **
14403234Seric **	Returns:
14413234Seric **		none.
14423234Seric **
14433234Seric **	Side Effects:
14443234Seric **		none.
14453234Seric */
14463234Seric 
14473234Seric printaddr(a, follow)
14483234Seric 	register ADDRESS *a;
14493234Seric 	bool follow;
14503234Seric {
14515001Seric 	bool first = TRUE;
145257731Seric 	register MAILER *m;
145357731Seric 	MAILER pseudomailer;
14545001Seric 
14553234Seric 	while (a != NULL)
14563234Seric 	{
14575001Seric 		first = FALSE;
14584443Seric 		printf("%x=", a);
14594085Seric 		(void) fflush(stdout);
146057731Seric 
146157731Seric 		/* find the mailer -- carefully */
146257731Seric 		m = a->q_mailer;
146357731Seric 		if (m == NULL)
146457731Seric 		{
146557731Seric 			m = &pseudomailer;
146657731Seric 			m->m_mno = -1;
146757731Seric 			m->m_name = "NULL";
146857731Seric 		}
146957731Seric 
147058680Seric 		printf("%s:\n\tmailer %d (%s), host `%s', user `%s', ruser `%s'\n",
147157731Seric 		       a->q_paddr, m->m_mno, m->m_name,
147240973Sbostic 		       a->q_host, a->q_user, a->q_ruser? a->q_ruser: "<null>");
14738181Seric 		printf("\tnext=%x, flags=%o, alias %x\n", a->q_next, a->q_flags,
14748181Seric 		       a->q_alias);
14758181Seric 		printf("\thome=\"%s\", fullname=\"%s\"\n", a->q_home,
14768181Seric 		       a->q_fullname);
14774996Seric 
14783234Seric 		if (!follow)
14793234Seric 			return;
14804996Seric 		a = a->q_next;
14813234Seric 	}
14825001Seric 	if (first)
14834443Seric 		printf("[NULL]\n");
14843234Seric }
14854317Seric 
14867682Seric /*
14877682Seric **  REMOTENAME -- return the name relative to the current mailer
14887682Seric **
14897682Seric **	Parameters:
14907682Seric **		name -- the name to translate.
14918069Seric **		m -- the mailer that we want to do rewriting relative
14928069Seric **			to.
14938069Seric **		senderaddress -- if set, uses the sender rewriting rules
14948069Seric **			rather than the recipient rewriting rules.
149558020Seric **		header -- set if this address is in the header, rather
149658020Seric **			than an envelope header.
149710310Seric **		canonical -- if set, strip out any comment information,
149810310Seric **			etc.
149958509Seric **		adddomain -- if set, OK to do domain extension.
150058020Seric **		e -- the current envelope.
15017682Seric **
15027682Seric **	Returns:
15037682Seric **		the text string representing this address relative to
15047682Seric **			the receiving mailer.
15057682Seric **
15067682Seric **	Side Effects:
15077682Seric **		none.
15087682Seric **
15097682Seric **	Warnings:
15107682Seric **		The text string returned is tucked away locally;
15117682Seric **			copy it if you intend to save it.
15127682Seric */
15137682Seric 
15147682Seric char *
151558509Seric remotename(name, m, senderaddress, header, canonical, adddomain, e)
15167682Seric 	char *name;
151756678Seric 	struct mailer *m;
15188069Seric 	bool senderaddress;
151958020Seric 	bool header;
152010310Seric 	bool canonical;
152158509Seric 	bool adddomain;
152256678Seric 	register ENVELOPE *e;
15237682Seric {
15248069Seric 	register char **pvp;
15258069Seric 	char *fancy;
152656678Seric 	extern char *macvalue();
152756678Seric 	char *oldg = macvalue('g', e);
152858020Seric 	int rwset;
15297682Seric 	static char buf[MAXNAME];
15307682Seric 	char lbuf[MAXNAME];
153116914Seric 	char pvpbuf[PSBUFSIZE];
153256678Seric 	extern char **prescan();
153356678Seric 	extern char *crackaddr();
15347682Seric 
15357755Seric 	if (tTd(12, 1))
15367755Seric 		printf("remotename(%s)\n", name);
15377755Seric 
153810177Seric 	/* don't do anything if we are tagging it as special */
153958020Seric 	if (senderaddress)
154058020Seric 		rwset = header ? m->m_sh_rwset : m->m_se_rwset;
154158020Seric 	else
154258020Seric 		rwset = header ? m->m_rh_rwset : m->m_re_rwset;
154358020Seric 	if (rwset < 0)
154410177Seric 		return (name);
154510177Seric 
15467682Seric 	/*
15478181Seric 	**  Do a heuristic crack of this name to extract any comment info.
15488181Seric 	**	This will leave the name as a comment and a $g macro.
15497889Seric 	*/
15507889Seric 
155158036Seric 	if (canonical || bitnset(M_NOCOMMENT, m->m_flags))
155258050Seric 		fancy = "\201g";
155310310Seric 	else
155410310Seric 		fancy = crackaddr(name);
15557889Seric 
15568181Seric 	/*
15578181Seric 	**  Turn the name into canonical form.
15588181Seric 	**	Normally this will be RFC 822 style, i.e., "user@domain".
15598181Seric 	**	If this only resolves to "user", and the "C" flag is
15608181Seric 	**	specified in the sending mailer, then the sender's
15618181Seric 	**	domain will be appended.
15628181Seric 	*/
15638181Seric 
156458333Seric 	pvp = prescan(name, '\0', pvpbuf, NULL);
15657889Seric 	if (pvp == NULL)
15667889Seric 		return (name);
156759084Seric 	(void) rewrite(pvp, 3, e);
156858509Seric 	if (adddomain && e->e_fromdomain != NULL)
15698181Seric 	{
15708181Seric 		/* append from domain to this address */
15718181Seric 		register char **pxp = pvp;
15728181Seric 
15739594Seric 		/* see if there is an "@domain" in the current name */
15748181Seric 		while (*pxp != NULL && strcmp(*pxp, "@") != 0)
15758181Seric 			pxp++;
15768181Seric 		if (*pxp == NULL)
15778181Seric 		{
15789594Seric 			/* no.... append the "@domain" from the sender */
157956678Seric 			register char **qxq = e->e_fromdomain;
15808181Seric 
15819594Seric 			while ((*pxp++ = *qxq++) != NULL)
15829594Seric 				continue;
158359084Seric 			(void) rewrite(pvp, 3, e);
15848181Seric 		}
15858181Seric 	}
15868181Seric 
15878181Seric 	/*
15888959Seric 	**  Do more specific rewriting.
158956678Seric 	**	Rewrite using ruleset 1 or 2 depending on whether this is
159056678Seric 	**		a sender address or not.
15918181Seric 	**	Then run it through any receiving-mailer-specific rulesets.
15928181Seric 	*/
15938181Seric 
15948069Seric 	if (senderaddress)
159559084Seric 		(void) rewrite(pvp, 1, e);
15968069Seric 	else
159759084Seric 		(void) rewrite(pvp, 2, e);
159858020Seric 	if (rwset > 0)
159959084Seric 		(void) rewrite(pvp, rwset, e);
16007682Seric 
16018181Seric 	/*
16028959Seric 	**  Do any final sanitation the address may require.
16038959Seric 	**	This will normally be used to turn internal forms
16048959Seric 	**	(e.g., user@host.LOCAL) into external form.  This
16058959Seric 	**	may be used as a default to the above rules.
16068959Seric 	*/
16078959Seric 
160859084Seric 	(void) rewrite(pvp, 4, e);
16098959Seric 
16108959Seric 	/*
16118181Seric 	**  Now restore the comment information we had at the beginning.
16128181Seric 	*/
16138181Seric 
161458825Seric 	cataddr(pvp, NULL, lbuf, sizeof lbuf, '\0');
161556678Seric 	define('g', lbuf, e);
161656678Seric 	expand(fancy, buf, &buf[sizeof buf - 1], e);
161756678Seric 	define('g', oldg, e);
16187682Seric 
16197682Seric 	if (tTd(12, 1))
16207755Seric 		printf("remotename => `%s'\n", buf);
16217682Seric 	return (buf);
16227682Seric }
162351317Seric /*
162456678Seric **  MAPLOCALUSER -- run local username through ruleset 5 for final redirection
162551317Seric **
162651317Seric **	Parameters:
162756678Seric **		a -- the address to map (but just the user name part).
162856678Seric **		sendq -- the sendq in which to install any replacement
162956678Seric **			addresses.
163051317Seric **
163151317Seric **	Returns:
163251317Seric **		none.
163351317Seric */
163451317Seric 
163556678Seric maplocaluser(a, sendq, e)
163656678Seric 	register ADDRESS *a;
163756678Seric 	ADDRESS **sendq;
163856678Seric 	ENVELOPE *e;
163951317Seric {
164056678Seric 	register char **pvp;
164156678Seric 	register ADDRESS *a1 = NULL;
164258333Seric 	auto char *delimptr;
164356678Seric 	char pvpbuf[PSBUFSIZE];
164451317Seric 
164556678Seric 	if (tTd(29, 1))
164656678Seric 	{
164756678Seric 		printf("maplocaluser: ");
164856678Seric 		printaddr(a, FALSE);
164956678Seric 	}
165058333Seric 	pvp = prescan(a->q_user, '\0', pvpbuf, &delimptr);
165156678Seric 	if (pvp == NULL)
165256678Seric 		return;
165351317Seric 
165459084Seric 	(void) rewrite(pvp, 5, e);
165558050Seric 	if (pvp[0] == NULL || (pvp[0][0] & 0377) != CANONNET)
165656678Seric 		return;
165751317Seric 
165856678Seric 	/* if non-null, mailer destination specified -- has it changed? */
165958966Seric 	a1 = buildaddr(pvp, NULL, e);
166056678Seric 	if (a1 == NULL || sameaddr(a, a1))
166156678Seric 		return;
166251317Seric 
166356678Seric 	/* mark old address as dead; insert new address */
166456678Seric 	a->q_flags |= QDONTSEND;
166557731Seric 	if (tTd(29, 5))
166657731Seric 	{
166757731Seric 		printf("maplocaluser: QDONTSEND ");
166857731Seric 		printaddr(a, FALSE);
166957731Seric 	}
167056678Seric 	a1->q_alias = a;
167158333Seric 	allocaddr(a1, 1, NULL, delimptr);
167256678Seric 	(void) recipient(a1, sendq, e);
167351317Seric }
167458802Seric /*
167558802Seric **  DEQUOTE_INIT -- initialize dequote map
167658802Seric **
167758802Seric **	This is a no-op.
167858802Seric **
167958802Seric **	Parameters:
168058802Seric **		map -- the internal map structure.
168158802Seric **		mapname -- the name of the mapl.
168258802Seric **		args -- arguments.
168358802Seric **
168458802Seric **	Returns:
168558802Seric **		TRUE.
168658802Seric */
168758802Seric 
168858802Seric bool
168958802Seric dequote_init(map, mapname, args)
169058802Seric 	MAP *map;
169158802Seric 	char *mapname;
169258802Seric 	char *args;
169358802Seric {
169458805Seric 	register char *p = args;
169558805Seric 
169658805Seric 	for (;;)
169758805Seric 	{
169858805Seric 		while (isascii(*p) && isspace(*p))
169958805Seric 			p++;
170058805Seric 		if (*p != '-')
170158805Seric 			break;
170258805Seric 		switch (*++p)
170358805Seric 		{
170458805Seric 		  case 'a':
170558805Seric 			map->map_app = ++p;
170658805Seric 			break;
170758805Seric 		}
170858805Seric 		while (*p != '\0' && !(isascii(*p) && isspace(*p)))
170958805Seric 			p++;
171058805Seric 		if (*p != '\0')
171158805Seric 			*p = '\0';
171258805Seric 	}
171358805Seric 	if (map->map_app != NULL)
171458805Seric 		map->map_app = newstr(map->map_app);
171558805Seric 
171658802Seric 	return TRUE;
171758802Seric }
171858802Seric /*
171958802Seric **  DEQUOTE_MAP -- unquote an address
172058802Seric **
172158802Seric **	Parameters:
172258802Seric **		map -- the internal map structure (ignored).
172358802Seric **		buf -- the buffer to dequote.
172458802Seric **		bufsiz -- the size of that buffer.
172558802Seric **		av -- arguments (ignored).
172659084Seric **		statp -- pointer to status out-parameter.
172758802Seric **
172858802Seric **	Returns:
172958802Seric **		NULL -- if there were no quotes, or if the resulting
173058802Seric **			unquoted buffer would not be acceptable to prescan.
173158802Seric **		else -- The dequoted buffer.
173258802Seric */
173358802Seric 
173458802Seric char *
173559084Seric dequote_map(map, buf, bufsiz, av, statp)
173658802Seric 	MAP *map;
173758802Seric 	char buf[];
173858802Seric 	int bufsiz;
173958802Seric 	char **av;
174059084Seric 	int *statp;
174158802Seric {
174258802Seric 	register char *p;
174358802Seric 	register char *q;
174458802Seric 	register char c;
174558802Seric 	int anglecnt;
174658802Seric 	int cmntcnt;
174758802Seric 	int quotecnt;
1748*59089Seric 	int spacecnt;
174958802Seric 	bool quotemode;
175058802Seric 	bool bslashmode;
175158802Seric 
175258802Seric 	anglecnt = 0;
175358802Seric 	cmntcnt = 0;
175458802Seric 	quotecnt = 0;
1755*59089Seric 	spacecnt = 0;
175658802Seric 	quotemode = FALSE;
175758802Seric 	bslashmode = FALSE;
175858802Seric 
175958802Seric 	for (p = q = buf; (c = *p++) != '\0'; )
176058802Seric 	{
176158802Seric 		if (bslashmode)
176258802Seric 		{
176358802Seric 			bslashmode = FALSE;
176458802Seric 			*q++ = c;
176558802Seric 			continue;
176658802Seric 		}
176758802Seric 
176858802Seric 		switch (c)
176958802Seric 		{
177058802Seric 		  case '\\':
177158802Seric 			bslashmode = TRUE;
177258802Seric 			break;
177358802Seric 
177458802Seric 		  case '(':
177558802Seric 			cmntcnt++;
177658802Seric 			break;
177758802Seric 
177858802Seric 		  case ')':
177958802Seric 			if (cmntcnt-- <= 0)
178058802Seric 				return NULL;
178158802Seric 			break;
1782*59089Seric 
1783*59089Seric 		  case ' ':
1784*59089Seric 			spacecnt++;
1785*59089Seric 			break;
178658802Seric 		}
178758802Seric 
178858802Seric 		if (cmntcnt > 0)
178958802Seric 		{
179058802Seric 			*q++ = c;
179158802Seric 			continue;
179258802Seric 		}
179358802Seric 
179458802Seric 		switch (c)
179558802Seric 		{
179658802Seric 		  case '"':
179758802Seric 			quotemode = !quotemode;
179858802Seric 			quotecnt++;
179958802Seric 			continue;
180058802Seric 
180158802Seric 		  case '<':
180258802Seric 			anglecnt++;
180358802Seric 			break;
180458802Seric 
180558802Seric 		  case '>':
180658802Seric 			if (anglecnt-- <= 0)
180758802Seric 				return NULL;
180858802Seric 			break;
180958802Seric 		}
181058802Seric 		*q++ = c;
181158802Seric 	}
181258802Seric 
181358802Seric 	if (anglecnt != 0 || cmntcnt != 0 || bslashmode ||
1814*59089Seric 	    quotemode || quotecnt <= 0 || spacecnt != 0)
181558802Seric 		return NULL;
181658802Seric 	*q++ = '\0';
181758802Seric 	return buf;
181858802Seric }
1819