122976Smiriam /*
234921Sbostic  * Copyright (c) 1983 Eric P. Allman
363589Sbostic  * Copyright (c) 1988, 1993
463589Sbostic  *	The Regents of the University of California.  All rights reserved.
533730Sbostic  *
642828Sbostic  * %sccs.include.redist.c%
733730Sbostic  */
822976Smiriam 
922976Smiriam #ifndef lint
10*63851Seric static char sccsid[] = "@(#)parseaddr.c	8.6 (Berkeley) 07/17/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 */
5759278Seric # define DELIMCHARS	"()<>,;\r\n"	/* default word delimiters */
582091Seric 
592973Seric ADDRESS *
6058333Seric parseaddr(addr, a, copyf, delim, delimptr, e)
61297Seric 	char *addr;
622973Seric 	register ADDRESS *a;
63297Seric 	int copyf;
6459700Seric 	int 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 ADDRESS *buildaddr();
7357388Seric 	extern bool invalidaddr();
74297Seric 
75297Seric 	/*
76297Seric 	**  Initialize and prescan address.
77297Seric 	*/
78297Seric 
7956678Seric 	e->e_to = addr;
807675Seric 	if (tTd(20, 1))
819888Seric 		printf("\n--parseaddr(%s)\n", addr);
823188Seric 
8357388Seric 	if (invalidaddr(addr))
8457388Seric 	{
8557388Seric 		if (tTd(20, 1))
8657388Seric 			printf("parseaddr-->bad address\n");
8757388Seric 		return NULL;
8857388Seric 	}
8957388Seric 
9058333Seric 	if (delimptr == NULL)
9158333Seric 		delimptr = &delimptrbuf;
9258333Seric 
9358333Seric 	pvp = prescan(addr, delim, pvpbuf, delimptr);
943149Seric 	if (pvp == NULL)
9556729Seric 	{
9656729Seric 		if (tTd(20, 1))
9756729Seric 			printf("parseaddr-->NULL\n");
98297Seric 		return (NULL);
9956729Seric 	}
100297Seric 
101297Seric 	/*
1023149Seric 	**  Apply rewriting rules.
1037889Seric 	**	Ruleset 0 does basic parsing.  It must resolve.
104297Seric 	*/
105297Seric 
10659084Seric 	queueup = FALSE;
10759084Seric 	if (rewrite(pvp, 3, e) == EX_TEMPFAIL)
10859084Seric 		queueup = TRUE;
10959084Seric 	if (rewrite(pvp, 0, e) == EX_TEMPFAIL)
11059084Seric 		queueup = TRUE;
111297Seric 
1123149Seric 	/*
1133149Seric 	**  See if we resolved to a real mailer.
1143149Seric 	*/
115297Seric 
11659040Seric 	if (pvp[0] == NULL || (pvp[0][0] & 0377) != CANONNET)
1173149Seric 	{
1183149Seric 		setstat(EX_USAGE);
11963753Seric 		syserr("554 cannot resolve name %s", addr);
1203149Seric 		return (NULL);
121297Seric 	}
122297Seric 
123297Seric 	/*
1243149Seric 	**  Build canonical address from pvp.
125297Seric 	*/
126297Seric 
12758966Seric 	a = buildaddr(pvp, a, e);
1284279Seric 	if (a == NULL)
1294279Seric 		return (NULL);
130297Seric 
131297Seric 	/*
1323149Seric 	**  Make local copies of the host & user and then
1333149Seric 	**  transport them out.
134297Seric 	*/
135297Seric 
13658333Seric 	allocaddr(a, copyf, addr, *delimptr);
13756678Seric 
13856678Seric 	/*
13959084Seric 	**  If there was a parsing failure, mark it for queueing.
14059084Seric 	*/
14159084Seric 
14259084Seric 	if (queueup)
14359595Seric 	{
14459734Seric 		char *msg = "Transient parse error -- message queued for future delivery";
14559734Seric 
14659595Seric 		if (tTd(20, 1))
14759595Seric 			printf("parseaddr: queuing message\n");
14859734Seric 		message(msg);
14959734Seric 		if (e->e_message == NULL)
15060009Seric 			e->e_message = newstr(msg);
15159084Seric 		a->q_flags |= QQUEUEUP;
15259595Seric 	}
15359084Seric 
15459084Seric 	/*
15556678Seric 	**  Compute return value.
15656678Seric 	*/
15756678Seric 
15856678Seric 	if (tTd(20, 1))
1598078Seric 	{
16056678Seric 		printf("parseaddr-->");
16156678Seric 		printaddr(a, FALSE);
16256678Seric 	}
16356678Seric 
16456678Seric 	return (a);
16556678Seric }
16656678Seric /*
16757388Seric **  INVALIDADDR -- check for address containing meta-characters
16857388Seric **
16957388Seric **	Parameters:
17057388Seric **		addr -- the address to check.
17157388Seric **
17257388Seric **	Returns:
17357388Seric **		TRUE -- if the address has any "wierd" characters
17457388Seric **		FALSE -- otherwise.
17557388Seric */
17657388Seric 
17757388Seric bool
17857388Seric invalidaddr(addr)
17957388Seric 	register char *addr;
18057388Seric {
18157388Seric 	for (; *addr != '\0'; addr++)
18257388Seric 	{
18358050Seric 		if ((*addr & 0340) != 0200)
18457388Seric 			continue;
18557388Seric 		setstat(EX_USAGE);
18658151Seric 		usrerr("553 Address contained invalid control characters");
18757388Seric 		return TRUE;
18857388Seric 	}
18957388Seric 	return FALSE;
19057388Seric }
19157388Seric /*
19256678Seric **  ALLOCADDR -- do local allocations of address on demand.
19356678Seric **
19456678Seric **	Also lowercases the host name if requested.
19556678Seric **
19656678Seric **	Parameters:
19756678Seric **		a -- the address to reallocate.
19856678Seric **		copyf -- the copy flag (see parseaddr for description).
19956678Seric **		paddr -- the printname of the address.
20058333Seric **		delimptr -- a pointer to the address delimiter.  Must be set.
20156678Seric **
20256678Seric **	Returns:
20356678Seric **		none.
20456678Seric **
20556678Seric **	Side Effects:
20656678Seric **		Copies portions of a into local buffers as requested.
20756678Seric */
20856678Seric 
20958333Seric allocaddr(a, copyf, paddr, delimptr)
21056678Seric 	register ADDRESS *a;
21156678Seric 	int copyf;
21256678Seric 	char *paddr;
21358333Seric 	char *delimptr;
21456678Seric {
21558673Seric 	if (tTd(24, 4))
21658673Seric 		printf("allocaddr(copyf=%d, paddr=%s)\n", copyf, paddr);
21758673Seric 
21856678Seric 	if (copyf > 0 && paddr != NULL)
21956678Seric 	{
22058333Seric 		char savec = *delimptr;
2218078Seric 
22259747Seric 		if (savec != '\0')
22359747Seric 			*delimptr = '\0';
22456678Seric 		a->q_paddr = newstr(paddr);
22559747Seric 		if (savec != '\0')
22659747Seric 			*delimptr = savec;
2278078Seric 	}
228297Seric 	else
22956678Seric 		a->q_paddr = paddr;
23024944Seric 
23124944Seric 	if (a->q_user == NULL)
23224944Seric 		a->q_user = "";
23324944Seric 	if (a->q_host == NULL)
23424944Seric 		a->q_host = "";
23524944Seric 
2363149Seric 	if (copyf >= 0)
237297Seric 	{
23824944Seric 		a->q_host = newstr(a->q_host);
2393149Seric 		if (a->q_user != a->q_paddr)
2403149Seric 			a->q_user = newstr(a->q_user);
241297Seric 	}
242297Seric 
24356678Seric 	if (a->q_paddr == NULL)
24456678Seric 		a->q_paddr = a->q_user;
245297Seric }
246297Seric /*
247297Seric **  PRESCAN -- Prescan name and make it canonical
248297Seric **
2499374Seric **	Scans a name and turns it into a set of tokens.  This process
2509374Seric **	deletes blanks and comments (in parentheses).
251297Seric **
252297Seric **	This routine knows about quoted strings and angle brackets.
253297Seric **
254297Seric **	There are certain subtleties to this routine.  The one that
255297Seric **	comes to mind now is that backslashes on the ends of names
256297Seric **	are silently stripped off; this is intentional.  The problem
257297Seric **	is that some versions of sndmsg (like at LBL) set the kill
258297Seric **	character to something other than @ when reading addresses;
259297Seric **	so people type "csvax.eric\@berkeley" -- which screws up the
260297Seric **	berknet mailer.
261297Seric **
262297Seric **	Parameters:
263297Seric **		addr -- the name to chomp.
264297Seric **		delim -- the delimiter for the address, normally
265297Seric **			'\0' or ','; \0 is accepted in any case.
26615284Seric **			If '\t' then we are reading the .cf file.
26716914Seric **		pvpbuf -- place to put the saved text -- note that
26816914Seric **			the pointers are static.
26958333Seric **		delimptr -- if non-NULL, set to the location of the
27058333Seric **			terminating delimiter.
271297Seric **
272297Seric **	Returns:
2733149Seric **		A pointer to a vector of tokens.
274297Seric **		NULL on error.
275297Seric */
276297Seric 
2778078Seric /* states and character types */
2788078Seric # define OPR		0	/* operator */
2798078Seric # define ATM		1	/* atom */
2808078Seric # define QST		2	/* in quoted string */
2818078Seric # define SPC		3	/* chewing up spaces */
2828078Seric # define ONE		4	/* pick up one character */
2833149Seric 
2848078Seric # define NSTATES	5	/* number of states */
2858078Seric # define TYPE		017	/* mask to select state type */
2868078Seric 
2878078Seric /* meta bits for table */
2888078Seric # define M		020	/* meta character; don't pass through */
2898078Seric # define B		040	/* cause a break */
2908078Seric # define MB		M|B	/* meta-break */
2918078Seric 
2928078Seric static short StateTab[NSTATES][NSTATES] =
2938078Seric {
2948087Seric    /*	oldst	chtype>	OPR	ATM	QST	SPC	ONE	*/
2959051Seric 	/*OPR*/		OPR|B,	ATM|B,	QST|B,	SPC|MB,	ONE|B,
2969051Seric 	/*ATM*/		OPR|B,	ATM,	QST|B,	SPC|MB,	ONE|B,
2979051Seric 	/*QST*/		QST,	QST,	OPR,	QST,	QST,
2988078Seric 	/*SPC*/		OPR,	ATM,	QST,	SPC|M,	ONE,
2998078Seric 	/*ONE*/		OPR,	OPR,	OPR,	OPR,	OPR,
3008078Seric };
3018078Seric 
3028078Seric # define NOCHAR		-1	/* signal nothing in lookahead token */
3038078Seric 
3043149Seric char **
30558333Seric prescan(addr, delim, pvpbuf, delimptr)
306297Seric 	char *addr;
307297Seric 	char delim;
30816914Seric 	char pvpbuf[];
30958333Seric 	char **delimptr;
310297Seric {
311297Seric 	register char *p;
3128078Seric 	register char *q;
3139346Seric 	register int c;
3143149Seric 	char **avp;
315297Seric 	bool bslashmode;
316297Seric 	int cmntcnt;
3178423Seric 	int anglecnt;
3183149Seric 	char *tok;
3198078Seric 	int state;
3208078Seric 	int newstate;
32159747Seric 	char *saveto = CurEnv->e_to;
3228078Seric 	static char *av[MAXATOM+1];
32356678Seric 	extern int errno;
324297Seric 
32515253Seric 	/* make sure error messages don't have garbage on them */
32615253Seric 	errno = 0;
32715253Seric 
32816914Seric 	q = pvpbuf;
3293149Seric 	bslashmode = FALSE;
3307800Seric 	cmntcnt = 0;
3318423Seric 	anglecnt = 0;
3323149Seric 	avp = av;
33356678Seric 	state = ATM;
3348078Seric 	c = NOCHAR;
3358078Seric 	p = addr;
33659747Seric 	CurEnv->e_to = p;
33756764Seric 	if (tTd(22, 11))
338297Seric 	{
3398078Seric 		printf("prescan: ");
3408078Seric 		xputs(p);
34123109Seric 		(void) putchar('\n');
3428078Seric 	}
3438078Seric 
3448078Seric 	do
3458078Seric 	{
3463149Seric 		/* read a token */
3473149Seric 		tok = q;
3488078Seric 		for (;;)
349297Seric 		{
3508078Seric 			/* store away any old lookahead character */
35159277Seric 			if (c != NOCHAR && !bslashmode)
3528078Seric 			{
35315284Seric 				/* see if there is room */
35416914Seric 				if (q >= &pvpbuf[PSBUFSIZE - 5])
3558078Seric 				{
35658151Seric 					usrerr("553 Address too long");
35758333Seric 					if (delimptr != NULL)
35858333Seric 						*delimptr = p;
35959747Seric 					CurEnv->e_to = saveto;
3608078Seric 					return (NULL);
3618078Seric 				}
36215284Seric 
36315284Seric 				/* squirrel it away */
3648078Seric 				*q++ = c;
3658078Seric 			}
3668078Seric 
3678078Seric 			/* read a new input character */
3688078Seric 			c = *p++;
36957631Seric 			if (c == '\0')
37056764Seric 			{
37156764Seric 				/* diagnose and patch up bad syntax */
37256764Seric 				if (state == QST)
37356764Seric 				{
374*63851Seric 					usrerr("653 Unbalanced '\"' (fixed)");
37556764Seric 					c = '"';
37656764Seric 				}
37756764Seric 				else if (cmntcnt > 0)
37856764Seric 				{
379*63851Seric 					usrerr("653 Unbalanced '(' (fixed)");
38056764Seric 					c = ')';
38156764Seric 				}
38256764Seric 				else if (anglecnt > 0)
38356764Seric 				{
38456764Seric 					c = '>';
385*63851Seric 					usrerr("653 Unbalanced '<' (fixed)");
38656764Seric 				}
38756764Seric 				else
38856764Seric 					break;
38915284Seric 
39056764Seric 				p--;
39156764Seric 			}
39257631Seric 			else if (c == delim && anglecnt <= 0 &&
39357631Seric 					cmntcnt <= 0 && state != QST)
39457631Seric 				break;
39556764Seric 
3968078Seric 			if (tTd(22, 101))
3978078Seric 				printf("c=%c, s=%d; ", c, state);
3988078Seric 
3993149Seric 			/* chew up special characters */
4003149Seric 			*q = '\0';
4013149Seric 			if (bslashmode)
4023149Seric 			{
40359105Seric 				bslashmode = FALSE;
40459105Seric 
40524944Seric 				/* kludge \! for naive users */
40658061Seric 				if (cmntcnt > 0)
40759105Seric 				{
40858061Seric 					c = NOCHAR;
40959105Seric 					continue;
41059105Seric 				}
41159105Seric 				else if (c != '!' || state == QST)
41259105Seric 				{
41356678Seric 					*q++ = '\\';
41459105Seric 					continue;
41559105Seric 				}
4163149Seric 			}
41756678Seric 
41856678Seric 			if (c == '\\')
4193149Seric 			{
4203149Seric 				bslashmode = TRUE;
4213149Seric 			}
42256678Seric 			else if (state == QST)
4238514Seric 			{
4248514Seric 				/* do nothing, just avoid next clauses */
4258514Seric 			}
4268078Seric 			else if (c == '(')
4274100Seric 			{
4288078Seric 				cmntcnt++;
4298078Seric 				c = NOCHAR;
4304100Seric 			}
4318078Seric 			else if (c == ')')
4323149Seric 			{
4338078Seric 				if (cmntcnt <= 0)
4343149Seric 				{
435*63851Seric 					usrerr("653 Unbalanced ')' (fixed)");
43663844Seric 					c = NOCHAR;
4373149Seric 				}
4388078Seric 				else
4398078Seric 					cmntcnt--;
4408078Seric 			}
4418078Seric 			else if (cmntcnt > 0)
4428078Seric 				c = NOCHAR;
4438423Seric 			else if (c == '<')
4448423Seric 				anglecnt++;
4458423Seric 			else if (c == '>')
4468423Seric 			{
4478423Seric 				if (anglecnt <= 0)
4488423Seric 				{
449*63851Seric 					usrerr("653 Unbalanced '>' (fixed)");
45063844Seric 					c = NOCHAR;
4518423Seric 				}
45263844Seric 				else
45363844Seric 					anglecnt--;
4548423Seric 			}
45558050Seric 			else if (delim == ' ' && isascii(c) && isspace(c))
45611423Seric 				c = ' ';
4573149Seric 
4588078Seric 			if (c == NOCHAR)
4598078Seric 				continue;
4603149Seric 
4618078Seric 			/* see if this is end of input */
46211405Seric 			if (c == delim && anglecnt <= 0 && state != QST)
4633149Seric 				break;
4643149Seric 
4658078Seric 			newstate = StateTab[state][toktype(c)];
4668078Seric 			if (tTd(22, 101))
4678078Seric 				printf("ns=%02o\n", newstate);
4688078Seric 			state = newstate & TYPE;
4698078Seric 			if (bitset(M, newstate))
4708078Seric 				c = NOCHAR;
4718078Seric 			if (bitset(B, newstate))
4724228Seric 				break;
473297Seric 		}
4743149Seric 
4753149Seric 		/* new token */
4768078Seric 		if (tok != q)
4771378Seric 		{
4788078Seric 			*q++ = '\0';
4798078Seric 			if (tTd(22, 36))
480297Seric 			{
4818078Seric 				printf("tok=");
4828078Seric 				xputs(tok);
48323109Seric 				(void) putchar('\n');
484297Seric 			}
4858078Seric 			if (avp >= &av[MAXATOM])
486297Seric 			{
48758151Seric 				syserr("553 prescan: too many tokens");
48858333Seric 				if (delimptr != NULL)
48958333Seric 					*delimptr = p;
49059747Seric 				CurEnv->e_to = saveto;
4918078Seric 				return (NULL);
492297Seric 			}
4938078Seric 			*avp++ = tok;
494297Seric 		}
4958423Seric 	} while (c != '\0' && (c != delim || anglecnt > 0));
4963149Seric 	*avp = NULL;
49758333Seric 	p--;
49858333Seric 	if (delimptr != NULL)
49958333Seric 		*delimptr = p;
50056764Seric 	if (tTd(22, 12))
50156764Seric 	{
50256764Seric 		printf("prescan==>");
50356764Seric 		printav(av);
50456764Seric 	}
50559747Seric 	CurEnv->e_to = saveto;
50658546Seric 	if (av[0] == NULL)
50758546Seric 		return (NULL);
50858403Seric 	return (av);
5093149Seric }
5103149Seric /*
5113149Seric **  TOKTYPE -- return token type
5123149Seric **
5133149Seric **	Parameters:
5143149Seric **		c -- the character in question.
5153149Seric **
5163149Seric **	Returns:
5173149Seric **		Its type.
5183149Seric **
5193149Seric **	Side Effects:
5203149Seric **		none.
5213149Seric */
522297Seric 
5233149Seric toktype(c)
52458050Seric 	register int c;
5253149Seric {
5263380Seric 	static char buf[50];
5273382Seric 	static bool firstime = TRUE;
5283380Seric 
5293382Seric 	if (firstime)
5303380Seric 	{
5313382Seric 		firstime = FALSE;
53258050Seric 		expand("\201o", buf, &buf[sizeof buf - 1], CurEnv);
5337005Seric 		(void) strcat(buf, DELIMCHARS);
5343380Seric 	}
53558050Seric 	c &= 0377;
53656327Seric 	if (c == MATCHCLASS || c == MATCHREPL || c == MATCHNCLASS)
5378078Seric 		return (ONE);
53859027Seric 	if (c == MACRODEXPAND)
53959027Seric 		return (ONE);
5408078Seric 	if (c == '"')
5418078Seric 		return (QST);
54258050Seric 	if ((c & 0340) == 0200)
54358050Seric 		return (OPR);
5444100Seric 	if (!isascii(c))
5458078Seric 		return (ATM);
5468078Seric 	if (isspace(c) || c == ')')
5478078Seric 		return (SPC);
54858050Seric 	if (strchr(buf, c) != NULL)
5498078Seric 		return (OPR);
5508078Seric 	return (ATM);
5513149Seric }
5523149Seric /*
5533149Seric **  REWRITE -- apply rewrite rules to token vector.
5543149Seric **
5554476Seric **	This routine is an ordered production system.  Each rewrite
5564476Seric **	rule has a LHS (called the pattern) and a RHS (called the
5574476Seric **	rewrite); 'rwr' points the the current rewrite rule.
5584476Seric **
5594476Seric **	For each rewrite rule, 'avp' points the address vector we
5604476Seric **	are trying to match against, and 'pvp' points to the pattern.
5618058Seric **	If pvp points to a special match value (MATCHZANY, MATCHANY,
5629585Seric **	MATCHONE, MATCHCLASS, MATCHNCLASS) then the address in avp
5639585Seric **	matched is saved away in the match vector (pointed to by 'mvp').
5644476Seric **
5654476Seric **	When a match between avp & pvp does not match, we try to
5669585Seric **	back out.  If we back up over MATCHONE, MATCHCLASS, or MATCHNCLASS
5674476Seric **	we must also back out the match in mvp.  If we reach a
5688058Seric **	MATCHANY or MATCHZANY we just extend the match and start
5698058Seric **	over again.
5704476Seric **
5714476Seric **	When we finally match, we rewrite the address vector
5724476Seric **	and try over again.
5734476Seric **
5743149Seric **	Parameters:
5753149Seric **		pvp -- pointer to token vector.
57659027Seric **		ruleset -- the ruleset to use for rewriting.
57759027Seric **		e -- the current envelope.
5783149Seric **
5793149Seric **	Returns:
58059084Seric **		A status code.  If EX_TEMPFAIL, higher level code should
58159084Seric **			attempt recovery.
5823149Seric **
5833149Seric **	Side Effects:
5843149Seric **		pvp is modified.
5853149Seric */
5862091Seric 
5873149Seric struct match
5883149Seric {
5894468Seric 	char	**first;	/* first token matched */
5904468Seric 	char	**last;		/* last token matched */
59158825Seric 	char	**pattern;	/* pointer to pattern */
5923149Seric };
5933149Seric 
5944468Seric # define MAXMATCH	9	/* max params per rewrite */
5953149Seric 
5963149Seric 
59759084Seric int
59859027Seric rewrite(pvp, ruleset, e)
5993149Seric 	char **pvp;
6004070Seric 	int ruleset;
60159027Seric 	register ENVELOPE *e;
6023149Seric {
6033149Seric 	register char *ap;		/* address pointer */
6043149Seric 	register char *rp;		/* rewrite pointer */
6053149Seric 	register char **avp;		/* address vector pointer */
6063149Seric 	register char **rvp;		/* rewrite vector pointer */
6078058Seric 	register struct match *mlp;	/* cur ptr into mlist */
6088058Seric 	register struct rewrite *rwr;	/* pointer to current rewrite rule */
60958866Seric 	int ruleno;			/* current rule number */
61059084Seric 	int rstat = EX_OK;		/* return status */
61156678Seric 	struct match mlist[MAXMATCH];	/* stores match on LHS */
6123149Seric 	char *npvp[MAXATOM+1];		/* temporary space for rebuild */
6133149Seric 
6149279Seric 	if (OpMode == MD_TEST || tTd(21, 2))
6153149Seric 	{
6168959Seric 		printf("rewrite: ruleset %2d   input:", ruleset);
61756678Seric 		printav(pvp);
6183149Seric 	}
61956678Seric 	if (ruleset < 0 || ruleset >= MAXRWSETS)
62056326Seric 	{
62158151Seric 		syserr("554 rewrite: illegal ruleset number %d", ruleset);
62259084Seric 		return EX_CONFIG;
62356326Seric 	}
62456678Seric 	if (pvp == NULL)
62559084Seric 		return EX_USAGE;
62656326Seric 
6273149Seric 	/*
62856678Seric 	**  Run through the list of rewrite rules, applying
62956678Seric 	**	any that match.
6303149Seric 	*/
6313149Seric 
63258866Seric 	ruleno = 1;
6334070Seric 	for (rwr = RewriteRules[ruleset]; rwr != NULL; )
6343149Seric 	{
63556678Seric 		int loopcount = 0;
63656678Seric 
6377675Seric 		if (tTd(21, 12))
638297Seric 		{
6398069Seric 			printf("-----trying rule:");
64056678Seric 			printav(rwr->r_lhs);
6413149Seric 		}
6423149Seric 
6433149Seric 		/* try to match on this rule */
6444468Seric 		mlp = mlist;
6458058Seric 		rvp = rwr->r_lhs;
6468058Seric 		avp = pvp;
64758866Seric 		if (++loopcount > 100)
6483149Seric 		{
64958866Seric 			syserr("554 Infinite loop in ruleset %d, rule %d",
65058866Seric 				ruleset, ruleno);
65158866Seric 			if (tTd(21, 1))
65252637Seric 			{
65356678Seric 				printf("workspace: ");
65456678Seric 				printav(pvp);
65552637Seric 			}
65658866Seric 			break;
65758866Seric 		}
65858866Seric 
65958866Seric 		while ((ap = *avp) != NULL || *rvp != NULL)
66058866Seric 		{
6613149Seric 			rp = *rvp;
6628058Seric 			if (tTd(21, 35))
6638058Seric 			{
66458825Seric 				printf("ADVANCE rp=");
66557531Seric 				xputs(rp);
66657532Seric 				printf(", ap=");
6678058Seric 				xputs(ap);
6688069Seric 				printf("\n");
6698058Seric 			}
67056678Seric 			if (rp == NULL)
67156326Seric 			{
6723149Seric 				/* end-of-pattern before end-of-address */
6738058Seric 				goto backup;
67456678Seric 			}
67558173Seric 			if (ap == NULL && (*rp & 0377) != MATCHZANY &&
67658827Seric 			    (*rp & 0377) != MATCHZERO)
67756326Seric 			{
67858825Seric 				/* end-of-input with patterns left */
67958825Seric 				goto backup;
680297Seric 			}
68156326Seric 
68258050Seric 			switch (*rp & 0377)
6838058Seric 			{
68456678Seric 				register STAB *s;
68558814Seric 				char buf[MAXLINE];
68656326Seric 
68756678Seric 			  case MATCHCLASS:
68858825Seric 				/* match any phrase in a class */
68958825Seric 				mlp->pattern = rvp;
69058814Seric 				mlp->first = avp;
69158814Seric 	extendclass:
69258825Seric 				ap = *avp;
69358825Seric 				if (ap == NULL)
69458814Seric 					goto backup;
69558814Seric 				mlp->last = avp++;
69658814Seric 				cataddr(mlp->first, mlp->last, buf, sizeof buf, '\0');
69758814Seric 				s = stab(buf, ST_CLASS, ST_FIND);
69856678Seric 				if (s == NULL || !bitnset(rp[1], s->s_class))
69956326Seric 				{
70058825Seric 					if (tTd(21, 36))
70158825Seric 					{
70258825Seric 						printf("EXTEND  rp=");
70358825Seric 						xputs(rp);
70458825Seric 						printf(", ap=");
70558825Seric 						xputs(ap);
70658825Seric 						printf("\n");
70758825Seric 					}
70858825Seric 					goto extendclass;
70956326Seric 				}
71058825Seric 				if (tTd(21, 36))
71158825Seric 					printf("CLMATCH\n");
71258814Seric 				mlp++;
71358814Seric 				break;
7144060Seric 
71558825Seric 			  case MATCHNCLASS:
71658825Seric 				/* match any token not in a class */
71758825Seric 				s = stab(ap, ST_CLASS, ST_FIND);
71858825Seric 				if (s != NULL && bitnset(rp[1], s->s_class))
71958825Seric 					goto backup;
72058825Seric 
72158825Seric 				/* fall through */
72258825Seric 
72356678Seric 			  case MATCHONE:
72456678Seric 			  case MATCHANY:
72556678Seric 				/* match exactly one token */
72658825Seric 				mlp->pattern = rvp;
72756678Seric 				mlp->first = avp;
72856678Seric 				mlp->last = avp++;
7298058Seric 				mlp++;
73056678Seric 				break;
7318058Seric 
73256678Seric 			  case MATCHZANY:
73356678Seric 				/* match zero or more tokens */
73458825Seric 				mlp->pattern = rvp;
73556678Seric 				mlp->first = avp;
73656678Seric 				mlp->last = avp - 1;
73756678Seric 				mlp++;
73856678Seric 				break;
73956326Seric 
74058827Seric 			  case MATCHZERO:
74158173Seric 				/* match zero tokens */
74258173Seric 				break;
74358173Seric 
74459027Seric 			  case MACRODEXPAND:
74559027Seric 				/*
74659027Seric 				**  Match against run-time macro.
74759027Seric 				**  This algorithm is broken for the
74859027Seric 				**  general case (no recursive macros,
74959027Seric 				**  improper tokenization) but should
75059027Seric 				**  work for the usual cases.
75159027Seric 				*/
75259027Seric 
75359027Seric 				ap = macvalue(rp[1], e);
75459027Seric 				mlp->first = avp;
75559027Seric 				if (tTd(21, 2))
75659027Seric 					printf("rewrite: LHS $&%c => \"%s\"\n",
75759027Seric 						rp[1],
75859027Seric 						ap == NULL ? "(NULL)" : ap);
75959027Seric 
76059027Seric 				if (ap == NULL)
76159027Seric 					break;
76260502Seric 				while (*ap != '\0')
76359027Seric 				{
76459027Seric 					if (*avp == NULL ||
76559027Seric 					    strncasecmp(ap, *avp, strlen(*avp)) != 0)
76659027Seric 					{
76759027Seric 						/* no match */
76859027Seric 						avp = mlp->first;
76959027Seric 						goto backup;
77059027Seric 					}
77159027Seric 					ap += strlen(*avp++);
77259027Seric 				}
77359027Seric 
77459027Seric 				/* match */
77559027Seric 				break;
77659027Seric 
77756678Seric 			  default:
77856678Seric 				/* must have exact match */
77956678Seric 				if (strcasecmp(rp, ap))
7808058Seric 					goto backup;
7814468Seric 				avp++;
78256678Seric 				break;
7833149Seric 			}
7843149Seric 
78556678Seric 			/* successful match on this token */
7863149Seric 			rvp++;
7873149Seric 			continue;
7883149Seric 
78958825Seric 	  backup:
79056678Seric 			/* match failed -- back up */
79158825Seric 			while (--mlp >= mlist)
7923149Seric 			{
79358825Seric 				rvp = mlp->pattern;
79456678Seric 				rp = *rvp;
79558825Seric 				avp = mlp->last + 1;
79658825Seric 				ap = *avp;
79758825Seric 
79858825Seric 				if (tTd(21, 36))
79958825Seric 				{
80058825Seric 					printf("BACKUP  rp=");
80158825Seric 					xputs(rp);
80258825Seric 					printf(", ap=");
80358825Seric 					xputs(ap);
80458825Seric 					printf("\n");
80558825Seric 				}
80658825Seric 
80758825Seric 				if (ap == NULL)
80858825Seric 				{
80958825Seric 					/* run off the end -- back up again */
81058825Seric 					continue;
81158825Seric 				}
81258050Seric 				if ((*rp & 0377) == MATCHANY ||
81358050Seric 				    (*rp & 0377) == MATCHZANY)
8144468Seric 				{
81556678Seric 					/* extend binding and continue */
81658825Seric 					mlp->last = avp++;
81756678Seric 					rvp++;
81858825Seric 					mlp++;
81956678Seric 					break;
8204468Seric 				}
82158825Seric 				if ((*rp & 0377) == MATCHCLASS)
82256678Seric 				{
82358814Seric 					/* extend binding and try again */
82463397Seric 					mlp->last = avp;
82558814Seric 					goto extendclass;
82658814Seric 				}
8273149Seric 			}
8283149Seric 
82958825Seric 			if (mlp < mlist)
83056678Seric 			{
83156678Seric 				/* total failure to match */
83256326Seric 				break;
8333149Seric 			}
834297Seric 		}
8353149Seric 
8363149Seric 		/*
83756678Seric 		**  See if we successfully matched
8383149Seric 		*/
8393149Seric 
84058827Seric 		if (mlp < mlist || *rvp != NULL)
8413149Seric 		{
8429374Seric 			if (tTd(21, 10))
8439374Seric 				printf("----- rule fails\n");
8449374Seric 			rwr = rwr->r_next;
84558866Seric 			ruleno++;
8469374Seric 			continue;
8479374Seric 		}
8483149Seric 
8499374Seric 		rvp = rwr->r_rhs;
8509374Seric 		if (tTd(21, 12))
8519374Seric 		{
8529374Seric 			printf("-----rule matches:");
85356678Seric 			printav(rvp);
8549374Seric 		}
8559374Seric 
8569374Seric 		rp = *rvp;
85758050Seric 		if ((*rp & 0377) == CANONUSER)
8589374Seric 		{
8599374Seric 			rvp++;
8609374Seric 			rwr = rwr->r_next;
86158866Seric 			ruleno++;
8629374Seric 		}
86358050Seric 		else if ((*rp & 0377) == CANONHOST)
8649374Seric 		{
8659374Seric 			rvp++;
8669374Seric 			rwr = NULL;
8679374Seric 		}
86858050Seric 		else if ((*rp & 0377) == CANONNET)
8699374Seric 			rwr = NULL;
8709374Seric 
8719374Seric 		/* substitute */
8729374Seric 		for (avp = npvp; *rvp != NULL; rvp++)
8739374Seric 		{
8749374Seric 			register struct match *m;
8759374Seric 			register char **pp;
8769374Seric 
8778058Seric 			rp = *rvp;
87858050Seric 			if ((*rp & 0377) == MATCHREPL)
8798058Seric 			{
88016914Seric 				/* substitute from LHS */
88116914Seric 				m = &mlist[rp[1] - '1'];
88256678Seric 				if (m < mlist || m >= mlp)
8839374Seric 				{
88458151Seric 					syserr("554 rewrite: ruleset %d: replacement $%c out of bounds",
88556326Seric 						ruleset, rp[1]);
88659084Seric 					return EX_CONFIG;
8879374Seric 				}
88816914Seric 				if (tTd(21, 15))
88916914Seric 				{
89016914Seric 					printf("$%c:", rp[1]);
89116914Seric 					pp = m->first;
89256678Seric 					while (pp <= m->last)
89316914Seric 					{
89416914Seric 						printf(" %x=\"", *pp);
89516914Seric 						(void) fflush(stdout);
89616914Seric 						printf("%s\"", *pp++);
89716914Seric 					}
89816914Seric 					printf("\n");
89916914Seric 				}
9009374Seric 				pp = m->first;
90156678Seric 				while (pp <= m->last)
9023149Seric 				{
90316914Seric 					if (avp >= &npvp[MAXATOM])
90456678Seric 					{
90558151Seric 						syserr("554 rewrite: expansion too long");
90659084Seric 						return EX_DATAERR;
90756678Seric 					}
90816914Seric 					*avp++ = *pp++;
9093149Seric 				}
9103149Seric 			}
91116914Seric 			else
9128226Seric 			{
91316914Seric 				/* vanilla replacement */
9149374Seric 				if (avp >= &npvp[MAXATOM])
91516889Seric 				{
91656678Seric 	toolong:
91758151Seric 					syserr("554 rewrite: expansion too long");
91859084Seric 					return EX_DATAERR;
91916889Seric 				}
92059027Seric 				if ((*rp & 0377) != MACRODEXPAND)
92159027Seric 					*avp++ = rp;
92259027Seric 				else
92359027Seric 				{
92459027Seric 					*avp = macvalue(rp[1], e);
92559027Seric 					if (tTd(21, 2))
92659027Seric 						printf("rewrite: RHS $&%c => \"%s\"\n",
92759027Seric 							rp[1],
92859027Seric 							*avp == NULL ? "(NULL)" : *avp);
92959027Seric 					if (*avp != NULL)
93059027Seric 						avp++;
93159027Seric 				}
9328226Seric 			}
9339374Seric 		}
9349374Seric 		*avp++ = NULL;
93516914Seric 
93616914Seric 		/*
93756678Seric 		**  Check for any hostname/keyword lookups.
93816914Seric 		*/
93916914Seric 
94016914Seric 		for (rvp = npvp; *rvp != NULL; rvp++)
94116914Seric 		{
94256678Seric 			char **hbrvp;
94316914Seric 			char **xpvp;
94416914Seric 			int trsize;
94556678Seric 			char *replac;
94656678Seric 			int endtoken;
94756678Seric 			STAB *map;
94856678Seric 			char *mapname;
94956678Seric 			char **key_rvp;
95056678Seric 			char **arg_rvp;
95156678Seric 			char **default_rvp;
95256678Seric 			char buf[MAXNAME + 1];
95316914Seric 			char *pvpb1[MAXATOM + 1];
95456823Seric 			char *argvect[10];
95517174Seric 			char pvpbuf[PSBUFSIZE];
95616914Seric 
95758050Seric 			if ((**rvp & 0377) != HOSTBEGIN &&
95858050Seric 			    (**rvp & 0377) != LOOKUPBEGIN)
95916914Seric 				continue;
96016914Seric 
96116914Seric 			/*
96256678Seric 			**  Got a hostname/keyword lookup.
96316914Seric 			**
96416914Seric 			**	This could be optimized fairly easily.
96516914Seric 			*/
96616914Seric 
96716914Seric 			hbrvp = rvp;
96858050Seric 			if ((**rvp & 0377) == HOSTBEGIN)
96956327Seric 			{
97056678Seric 				endtoken = HOSTEND;
97156678Seric 				mapname = "host";
97256327Seric 			}
97356326Seric 			else
97456327Seric 			{
97556678Seric 				endtoken = LOOKUPEND;
97656678Seric 				mapname = *++rvp;
97756327Seric 			}
97856678Seric 			map = stab(mapname, ST_MAP, ST_FIND);
97956678Seric 			if (map == NULL)
98058151Seric 				syserr("554 rewrite: map %s not found", mapname);
98156678Seric 
98256678Seric 			/* extract the match part */
98356678Seric 			key_rvp = ++rvp;
98456823Seric 			default_rvp = NULL;
98556823Seric 			arg_rvp = argvect;
98656823Seric 			xpvp = NULL;
98756823Seric 			replac = pvpbuf;
98858050Seric 			while (*rvp != NULL && (**rvp & 0377) != endtoken)
98953654Seric 			{
99058050Seric 				int nodetype = **rvp & 0377;
99156823Seric 
99256823Seric 				if (nodetype != CANONHOST && nodetype != CANONUSER)
99356678Seric 				{
99456823Seric 					rvp++;
99556823Seric 					continue;
99656823Seric 				}
99756823Seric 
99856823Seric 				*rvp++ = NULL;
99956823Seric 
100056823Seric 				if (xpvp != NULL)
100156823Seric 				{
100258814Seric 					cataddr(xpvp, NULL, replac,
100358082Seric 						&pvpbuf[sizeof pvpbuf] - replac,
100458082Seric 						'\0');
100556823Seric 					*++arg_rvp = replac;
100656823Seric 					replac += strlen(replac) + 1;
100756823Seric 					xpvp = NULL;
100856823Seric 				}
100956823Seric 				switch (nodetype)
101056823Seric 				{
101156678Seric 				  case CANONHOST:
101256823Seric 					xpvp = rvp;
101356678Seric 					break;
101456678Seric 
101556678Seric 				  case CANONUSER:
101656678Seric 					default_rvp = rvp;
101756678Seric 					break;
101856678Seric 				}
101953654Seric 			}
102016914Seric 			if (*rvp != NULL)
102116914Seric 				*rvp++ = NULL;
102256823Seric 			if (xpvp != NULL)
102356823Seric 			{
102458814Seric 				cataddr(xpvp, NULL, replac,
102558082Seric 					&pvpbuf[sizeof pvpbuf] - replac,
102658082Seric 					'\0');
102756823Seric 				*++arg_rvp = replac;
102856823Seric 			}
102956823Seric 			*++arg_rvp = NULL;
103016914Seric 
103116914Seric 			/* save the remainder of the input string */
103216914Seric 			trsize = (int) (avp - rvp + 1) * sizeof *rvp;
103316914Seric 			bcopy((char *) rvp, (char *) pvpb1, trsize);
103416914Seric 
103556678Seric 			/* look it up */
103658814Seric 			cataddr(key_rvp, NULL, buf, sizeof buf, '\0');
103756823Seric 			argvect[0] = buf;
103860538Seric 			if (map != NULL && bitset(MF_OPEN, map->s_map.map_mflags))
103956836Seric 			{
104059084Seric 				auto int stat = EX_OK;
104156836Seric 
104260215Seric 				/* XXX should try to auto-open the map here */
104360215Seric 
104458796Seric 				if (tTd(60, 1))
104558796Seric 					printf("map_lookup(%s, %s) => ",
104658796Seric 						mapname, buf);
104756836Seric 				replac = (*map->s_map.map_class->map_lookup)(&map->s_map,
104860089Seric 						buf, argvect, &stat);
104958796Seric 				if (tTd(60, 1))
105059084Seric 					printf("%s (%d)\n",
105159084Seric 						replac ? replac : "NOT FOUND",
105259084Seric 						stat);
105359084Seric 
105459084Seric 				/* should recover if stat == EX_TEMPFAIL */
105559084Seric 				if (stat == EX_TEMPFAIL)
105659084Seric 					rstat = stat;
105756836Seric 			}
105853654Seric 			else
105956678Seric 				replac = NULL;
106056678Seric 
106156678Seric 			/* if no replacement, use default */
106256823Seric 			if (replac == NULL && default_rvp != NULL)
106356823Seric 			{
106460089Seric 				/* create the default */
106560089Seric 				cataddr(default_rvp, NULL, buf, sizeof buf, '\0');
106656823Seric 				replac = buf;
106756823Seric 			}
106856823Seric 
106956678Seric 			if (replac == NULL)
107051317Seric 			{
107156823Seric 				xpvp = key_rvp;
107253654Seric 			}
107356678Seric 			else
107453654Seric 			{
107556678Seric 				/* scan the new replacement */
107658333Seric 				xpvp = prescan(replac, '\0', pvpbuf, NULL);
107753654Seric 				if (xpvp == NULL)
107851317Seric 				{
107958403Seric 					/* prescan already printed error */
108059084Seric 					return EX_DATAERR;
108156678Seric 				}
108251317Seric 			}
108351317Seric 
108416914Seric 			/* append it to the token list */
108556678Seric 			for (avp = hbrvp; *xpvp != NULL; xpvp++)
108656678Seric 			{
108717174Seric 				*avp++ = newstr(*xpvp);
108816920Seric 				if (avp >= &npvp[MAXATOM])
108916914Seric 					goto toolong;
109017174Seric 			}
109116914Seric 
109216914Seric 			/* restore the old trailing information */
109356678Seric 			for (xpvp = pvpb1; (*avp++ = *xpvp++) != NULL; )
109416920Seric 				if (avp >= &npvp[MAXATOM])
109516914Seric 					goto toolong;
109617174Seric 
109756678Seric 			break;
109816914Seric 		}
109916914Seric 
110016914Seric 		/*
110116914Seric 		**  Check for subroutine calls.
110216914Seric 		*/
110316914Seric 
110458050Seric 		if (*npvp != NULL && (**npvp & 0377) == CALLSUBR)
110556678Seric 		{
110659084Seric 			int stat;
110759084Seric 
110856678Seric 			bcopy((char *) &npvp[2], (char *) pvp,
110956678Seric 				(int) (avp - npvp - 2) * sizeof *avp);
111056678Seric 			if (tTd(21, 3))
111156678Seric 				printf("-----callsubr %s\n", npvp[1]);
111259084Seric 			stat = rewrite(pvp, atoi(npvp[1]), e);
111359084Seric 			if (rstat == EX_OK || stat == EX_TEMPFAIL)
111459084Seric 				rstat = stat;
111563581Seric 			if ((**pvp & 0377) == CANONNET)
111663581Seric 				rwr = NULL;
111756678Seric 		}
111856678Seric 		else
111956678Seric 		{
112017348Seric 			bcopy((char *) npvp, (char *) pvp,
112116900Seric 				(int) (avp - npvp) * sizeof *avp);
112256678Seric 		}
11239374Seric 		if (tTd(21, 4))
11249374Seric 		{
11259374Seric 			printf("rewritten as:");
112656678Seric 			printav(pvp);
11279374Seric 		}
1128297Seric 	}
11298069Seric 
11309279Seric 	if (OpMode == MD_TEST || tTd(21, 2))
11318069Seric 	{
11328959Seric 		printf("rewrite: ruleset %2d returns:", ruleset);
113356678Seric 		printav(pvp);
11348069Seric 	}
113559084Seric 
113659084Seric 	return rstat;
11373149Seric }
11383149Seric /*
11393149Seric **  BUILDADDR -- build address from token vector.
11403149Seric **
11413149Seric **	Parameters:
11423149Seric **		tv -- token vector.
11433149Seric **		a -- pointer to address descriptor to fill.
11443149Seric **			If NULL, one will be allocated.
114558966Seric **		e -- the current envelope.
11463149Seric **
11473149Seric **	Returns:
11484279Seric **		NULL if there was an error.
11494279Seric **		'a' otherwise.
11503149Seric **
11513149Seric **	Side Effects:
11523149Seric **		fills in 'a'
11533149Seric */
11543149Seric 
115557249Seric struct errcodes
115657249Seric {
115757249Seric 	char	*ec_name;		/* name of error code */
115857249Seric 	int	ec_code;		/* numeric code */
115957249Seric } ErrorCodes[] =
116057249Seric {
116157249Seric 	"usage",	EX_USAGE,
116257249Seric 	"nouser",	EX_NOUSER,
116357249Seric 	"nohost",	EX_NOHOST,
116457249Seric 	"unavailable",	EX_UNAVAILABLE,
116557249Seric 	"software",	EX_SOFTWARE,
116657249Seric 	"tempfail",	EX_TEMPFAIL,
116757249Seric 	"protocol",	EX_PROTOCOL,
116857249Seric #ifdef EX_CONFIG
116957249Seric 	"config",	EX_CONFIG,
117057249Seric #endif
117157249Seric 	NULL,		EX_UNAVAILABLE,
117257249Seric };
117357249Seric 
117456678Seric ADDRESS *
117558966Seric buildaddr(tv, a, e)
11763149Seric 	register char **tv;
11773149Seric 	register ADDRESS *a;
117858966Seric 	register ENVELOPE *e;
11793149Seric {
11803149Seric 	struct mailer **mp;
11813149Seric 	register struct mailer *m;
118258008Seric 	char *bp;
118358008Seric 	int spaceleft;
118457402Seric 	static char buf[MAXNAME];
11853149Seric 
11863149Seric 	if (a == NULL)
11873149Seric 		a = (ADDRESS *) xalloc(sizeof *a);
118816889Seric 	bzero((char *) a, sizeof *a);
11893149Seric 
11903149Seric 	/* figure out what net/mailer to use */
119158050Seric 	if ((**tv & 0377) != CANONNET)
11924279Seric 	{
119358151Seric 		syserr("554 buildaddr: no net");
11944279Seric 		return (NULL);
11954279Seric 	}
11963149Seric 	tv++;
119758680Seric 	if (strcasecmp(*tv, "error") == 0)
11984279Seric 	{
119958050Seric 		if ((**++tv & 0377) == CANONHOST)
120010183Seric 		{
120157249Seric 			register struct errcodes *ep;
120257249Seric 
120358050Seric 			if (isascii(**++tv) && isdigit(**tv))
120457249Seric 			{
120557249Seric 				setstat(atoi(*tv));
120657249Seric 			}
120757249Seric 			else
120857249Seric 			{
120957249Seric 				for (ep = ErrorCodes; ep->ec_name != NULL; ep++)
121057249Seric 					if (strcasecmp(ep->ec_name, *tv) == 0)
121157249Seric 						break;
121257249Seric 				setstat(ep->ec_code);
121357249Seric 			}
121410183Seric 			tv++;
121510183Seric 		}
121658050Seric 		if ((**tv & 0377) != CANONUSER)
121758151Seric 			syserr("554 buildaddr: error: no user");
121858814Seric 		cataddr(++tv, NULL, buf, sizeof buf, ' ');
121958082Seric 		stripquotes(buf);
12204279Seric 		usrerr(buf);
12214279Seric 		return (NULL);
12224279Seric 	}
122357402Seric 
12244598Seric 	for (mp = Mailer; (m = *mp++) != NULL; )
12253149Seric 	{
122658680Seric 		if (strcasecmp(m->m_name, *tv) == 0)
12273149Seric 			break;
12283149Seric 	}
12293149Seric 	if (m == NULL)
12304279Seric 	{
123158151Seric 		syserr("554 buildaddr: unknown mailer %s", *tv);
12324279Seric 		return (NULL);
12334279Seric 	}
12344598Seric 	a->q_mailer = m;
12353149Seric 
12363149Seric 	/* figure out what host (if any) */
123756678Seric 	tv++;
123858509Seric 	if ((**tv & 0377) == CANONHOST)
12393149Seric 	{
124058008Seric 		bp = buf;
124158008Seric 		spaceleft = sizeof buf - 1;
124258050Seric 		while (*++tv != NULL && (**tv & 0377) != CANONUSER)
124358008Seric 		{
124458008Seric 			int i = strlen(*tv);
124558008Seric 
124658008Seric 			if (i > spaceleft)
124758008Seric 			{
124858008Seric 				/* out of space for this address */
124958008Seric 				if (spaceleft >= 0)
125058151Seric 					syserr("554 buildaddr: host too long (%.40s...)",
125158008Seric 						buf);
125258008Seric 				i = spaceleft;
125358008Seric 				spaceleft = 0;
125458008Seric 			}
125558008Seric 			if (i <= 0)
125658008Seric 				continue;
125758008Seric 			bcopy(*tv, bp, i);
125858008Seric 			bp += i;
125958008Seric 			spaceleft -= i;
126058008Seric 		}
126158010Seric 		*bp = '\0';
12625704Seric 		a->q_host = newstr(buf);
12633149Seric 	}
126457249Seric 	else
126558509Seric 	{
126658509Seric 		if (!bitnset(M_LOCALMAILER, m->m_flags))
126758509Seric 		{
126858509Seric 			syserr("554 buildaddr: no host");
126958509Seric 			return (NULL);
127058509Seric 		}
127157249Seric 		a->q_host = NULL;
127258509Seric 	}
12733149Seric 
12743149Seric 	/* figure out the user */
127558050Seric 	if (*tv == NULL || (**tv & 0377) != CANONUSER)
12764279Seric 	{
127758151Seric 		syserr("554 buildaddr: no user");
12784279Seric 		return (NULL);
12794279Seric 	}
128057402Seric 	tv++;
128151317Seric 
128257402Seric 	/* do special mapping for local mailer */
128357402Seric 	if (m == LocalMailer && *tv != NULL)
128457402Seric 	{
128557454Seric 		register char *p = *tv;
128657454Seric 
128757454Seric 		if (*p == '"')
128857454Seric 			p++;
128957454Seric 		if (*p == '|')
129057402Seric 			a->q_mailer = m = ProgMailer;
129157454Seric 		else if (*p == '/')
129257402Seric 			a->q_mailer = m = FileMailer;
129357454Seric 		else if (*p == ':')
129457402Seric 		{
129557402Seric 			/* may be :include: */
129658814Seric 			cataddr(tv, NULL, buf, sizeof buf, '\0');
129758008Seric 			stripquotes(buf);
129858008Seric 			if (strncasecmp(buf, ":include:", 9) == 0)
129958008Seric 			{
130058008Seric 				/* if :include:, don't need further rewriting */
130157402Seric 				a->q_mailer = m = InclMailer;
130258008Seric 				a->q_user = &buf[9];
130358008Seric 				return (a);
130458008Seric 			}
130557402Seric 		}
130657402Seric 	}
130757402Seric 
130858008Seric 	if (m == LocalMailer && *tv != NULL && strcmp(*tv, "@") == 0)
130958008Seric 	{
131058008Seric 		tv++;
131158008Seric 		a->q_flags |= QNOTREMOTE;
131258008Seric 	}
131358008Seric 
131458673Seric 	/* do cleanup of final address */
131559084Seric 	(void) rewrite(tv, 4, e);
131619040Seric 
131719040Seric 	/* save the result for the command line/RCPT argument */
131858814Seric 	cataddr(tv, NULL, buf, sizeof buf, '\0');
13193149Seric 	a->q_user = buf;
13203149Seric 
132158670Seric 	/*
132258670Seric 	**  Do mapping to lower case as requested by mailer
132358670Seric 	*/
132458670Seric 
132558670Seric 	if (a->q_host != NULL && !bitnset(M_HST_UPPER, m->m_flags))
132658670Seric 		makelower(a->q_host);
132758670Seric 	if (!bitnset(M_USR_UPPER, m->m_flags))
132858670Seric 		makelower(a->q_user);
132958670Seric 
13303149Seric 	return (a);
13313149Seric }
13323188Seric /*
13334228Seric **  CATADDR -- concatenate pieces of addresses (putting in <LWSP> subs)
13344228Seric **
13354228Seric **	Parameters:
13364228Seric **		pvp -- parameter vector to rebuild.
133758814Seric **		evp -- last parameter to include.  Can be NULL to
133858814Seric **			use entire pvp.
13394228Seric **		buf -- buffer to build the string into.
13404228Seric **		sz -- size of buf.
134158082Seric **		spacesub -- the space separator character; if null,
134258082Seric **			use SpaceSub.
13434228Seric **
13444228Seric **	Returns:
13454228Seric **		none.
13464228Seric **
13474228Seric **	Side Effects:
13484228Seric **		Destroys buf.
13494228Seric */
13504228Seric 
135158814Seric cataddr(pvp, evp, buf, sz, spacesub)
13524228Seric 	char **pvp;
135358814Seric 	char **evp;
13544228Seric 	char *buf;
13554228Seric 	register int sz;
135658082Seric 	char spacesub;
13574228Seric {
13584228Seric 	bool oatomtok = FALSE;
135956678Seric 	bool natomtok = FALSE;
13604228Seric 	register int i;
13614228Seric 	register char *p;
13624228Seric 
136358082Seric 	if (spacesub == '\0')
136458082Seric 		spacesub = SpaceSub;
136558082Seric 
13668423Seric 	if (pvp == NULL)
13678423Seric 	{
136823109Seric 		(void) strcpy(buf, "");
13698423Seric 		return;
13708423Seric 	}
13714228Seric 	p = buf;
137211156Seric 	sz -= 2;
13734228Seric 	while (*pvp != NULL && (i = strlen(*pvp)) < sz)
13744228Seric 	{
13758078Seric 		natomtok = (toktype(**pvp) == ATM);
13764228Seric 		if (oatomtok && natomtok)
137758082Seric 			*p++ = spacesub;
13784228Seric 		(void) strcpy(p, *pvp);
13794228Seric 		oatomtok = natomtok;
13804228Seric 		p += i;
138111156Seric 		sz -= i + 1;
138258814Seric 		if (pvp++ == evp)
138358814Seric 			break;
13844228Seric 	}
13854228Seric 	*p = '\0';
13864228Seric }
13874228Seric /*
13883188Seric **  SAMEADDR -- Determine if two addresses are the same
13893188Seric **
13903188Seric **	This is not just a straight comparison -- if the mailer doesn't
13913188Seric **	care about the host we just ignore it, etc.
13923188Seric **
13933188Seric **	Parameters:
13943188Seric **		a, b -- pointers to the internal forms to compare.
13953188Seric **
13963188Seric **	Returns:
13973188Seric **		TRUE -- they represent the same mailbox.
13983188Seric **		FALSE -- they don't.
13993188Seric **
14003188Seric **	Side Effects:
14013188Seric **		none.
14023188Seric */
14033188Seric 
14043188Seric bool
14059374Seric sameaddr(a, b)
14063188Seric 	register ADDRESS *a;
14073188Seric 	register ADDRESS *b;
14083188Seric {
14093188Seric 	/* if they don't have the same mailer, forget it */
14103188Seric 	if (a->q_mailer != b->q_mailer)
14113188Seric 		return (FALSE);
14123188Seric 
14133188Seric 	/* if the user isn't the same, we can drop out */
141456678Seric 	if (strcmp(a->q_user, b->q_user) != 0)
14153188Seric 		return (FALSE);
14163188Seric 
141758438Seric 	/* if we have good uids for both but the differ, these are different */
141858438Seric 	if (bitset(QGOODUID, a->q_flags & b->q_flags) && a->q_uid != b->q_uid)
141958438Seric 		return (FALSE);
142058438Seric 
142158509Seric 	/* otherwise compare hosts (but be careful for NULL ptrs) */
142258509Seric 	if (a->q_host == b->q_host)
142358509Seric 	{
142458509Seric 		/* probably both null pointers */
14253188Seric 		return (TRUE);
142658509Seric 	}
14273188Seric 	if (a->q_host == NULL || b->q_host == NULL)
142858509Seric 	{
142958509Seric 		/* only one is a null pointer */
14303188Seric 		return (FALSE);
143158509Seric 	}
143256678Seric 	if (strcmp(a->q_host, b->q_host) != 0)
14333188Seric 		return (FALSE);
14343188Seric 
14353188Seric 	return (TRUE);
14363188Seric }
14373234Seric /*
14383234Seric **  PRINTADDR -- print address (for debugging)
14393234Seric **
14403234Seric **	Parameters:
14413234Seric **		a -- the address to print
14423234Seric **		follow -- follow the q_next chain.
14433234Seric **
14443234Seric **	Returns:
14453234Seric **		none.
14463234Seric **
14473234Seric **	Side Effects:
14483234Seric **		none.
14493234Seric */
14503234Seric 
14513234Seric printaddr(a, follow)
14523234Seric 	register ADDRESS *a;
14533234Seric 	bool follow;
14543234Seric {
14555001Seric 	bool first = TRUE;
145657731Seric 	register MAILER *m;
145757731Seric 	MAILER pseudomailer;
14585001Seric 
14593234Seric 	while (a != NULL)
14603234Seric 	{
14615001Seric 		first = FALSE;
14624443Seric 		printf("%x=", a);
14634085Seric 		(void) fflush(stdout);
146457731Seric 
146557731Seric 		/* find the mailer -- carefully */
146657731Seric 		m = a->q_mailer;
146757731Seric 		if (m == NULL)
146857731Seric 		{
146957731Seric 			m = &pseudomailer;
147057731Seric 			m->m_mno = -1;
147157731Seric 			m->m_name = "NULL";
147257731Seric 		}
147357731Seric 
147458680Seric 		printf("%s:\n\tmailer %d (%s), host `%s', user `%s', ruser `%s'\n",
147557731Seric 		       a->q_paddr, m->m_mno, m->m_name,
147663756Seric 		       a->q_host, a->q_user,
147763756Seric 		       a->q_ruser ? a->q_ruser : "<null>");
147859269Seric 		printf("\tnext=%x, flags=%o, alias %x, uid %d, gid %d\n",
147959269Seric 		       a->q_next, a->q_flags, a->q_alias, a->q_uid, a->q_gid);
148059269Seric 		printf("\towner=%s, home=\"%s\", fullname=\"%s\"\n",
148159269Seric 		       a->q_owner == NULL ? "(none)" : a->q_owner,
148263756Seric 		       a->q_home == NULL ? "(none)" : a->q_home,
148363756Seric 		       a->q_fullname == NULL ? "(none)" : a->q_fullname);
14844996Seric 
14853234Seric 		if (!follow)
14863234Seric 			return;
14874996Seric 		a = a->q_next;
14883234Seric 	}
14895001Seric 	if (first)
14904443Seric 		printf("[NULL]\n");
14913234Seric }
14924317Seric 
14937682Seric /*
14947682Seric **  REMOTENAME -- return the name relative to the current mailer
14957682Seric **
14967682Seric **	Parameters:
14977682Seric **		name -- the name to translate.
14988069Seric **		m -- the mailer that we want to do rewriting relative
14998069Seric **			to.
150059163Seric **		flags -- fine tune operations.
150159163Seric **		pstat -- pointer to status word.
150258020Seric **		e -- the current envelope.
15037682Seric **
15047682Seric **	Returns:
15057682Seric **		the text string representing this address relative to
15067682Seric **			the receiving mailer.
15077682Seric **
15087682Seric **	Side Effects:
15097682Seric **		none.
15107682Seric **
15117682Seric **	Warnings:
15127682Seric **		The text string returned is tucked away locally;
15137682Seric **			copy it if you intend to save it.
15147682Seric */
15157682Seric 
15167682Seric char *
151759163Seric remotename(name, m, flags, pstat, e)
15187682Seric 	char *name;
151956678Seric 	struct mailer *m;
152059163Seric 	int flags;
152159163Seric 	int *pstat;
152256678Seric 	register ENVELOPE *e;
15237682Seric {
15248069Seric 	register char **pvp;
15258069Seric 	char *fancy;
152656678Seric 	char *oldg = macvalue('g', e);
152758020Seric 	int rwset;
15287682Seric 	static char buf[MAXNAME];
15297682Seric 	char lbuf[MAXNAME];
153016914Seric 	char pvpbuf[PSBUFSIZE];
153156678Seric 	extern char *crackaddr();
15327682Seric 
15337755Seric 	if (tTd(12, 1))
15347755Seric 		printf("remotename(%s)\n", name);
15357755Seric 
153610177Seric 	/* don't do anything if we are tagging it as special */
153759163Seric 	if (bitset(RF_SENDERADDR, flags))
153859163Seric 		rwset = bitset(RF_HEADERADDR, flags) ? m->m_sh_rwset
153959163Seric 						     : m->m_se_rwset;
154058020Seric 	else
154159163Seric 		rwset = bitset(RF_HEADERADDR, flags) ? m->m_rh_rwset
154259163Seric 						     : 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 
155159163Seric 	if (bitset(RF_CANONICAL, flags) || 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);
156759163Seric 	if (rewrite(pvp, 3, e) == EX_TEMPFAIL)
156859163Seric 		*pstat = EX_TEMPFAIL;
156959163Seric 	if (bitset(RF_ADDDOMAIN, flags) && e->e_fromdomain != NULL)
15708181Seric 	{
15718181Seric 		/* append from domain to this address */
15728181Seric 		register char **pxp = pvp;
15738181Seric 
15749594Seric 		/* see if there is an "@domain" in the current name */
15758181Seric 		while (*pxp != NULL && strcmp(*pxp, "@") != 0)
15768181Seric 			pxp++;
15778181Seric 		if (*pxp == NULL)
15788181Seric 		{
15799594Seric 			/* no.... append the "@domain" from the sender */
158056678Seric 			register char **qxq = e->e_fromdomain;
15818181Seric 
15829594Seric 			while ((*pxp++ = *qxq++) != NULL)
15839594Seric 				continue;
158459163Seric 			if (rewrite(pvp, 3, e) == EX_TEMPFAIL)
158559163Seric 				*pstat = EX_TEMPFAIL;
15868181Seric 		}
15878181Seric 	}
15888181Seric 
15898181Seric 	/*
15908959Seric 	**  Do more specific rewriting.
159156678Seric 	**	Rewrite using ruleset 1 or 2 depending on whether this is
159256678Seric 	**		a sender address or not.
15938181Seric 	**	Then run it through any receiving-mailer-specific rulesets.
15948181Seric 	*/
15958181Seric 
159659163Seric 	if (bitset(RF_SENDERADDR, flags))
159759541Seric 	{
159859163Seric 		if (rewrite(pvp, 1, e) == EX_TEMPFAIL)
159959163Seric 			*pstat = EX_TEMPFAIL;
160059541Seric 	}
16018069Seric 	else
160259541Seric 	{
160359163Seric 		if (rewrite(pvp, 2, e) == EX_TEMPFAIL)
160459163Seric 			*pstat = EX_TEMPFAIL;
160559541Seric 	}
160658020Seric 	if (rwset > 0)
160759541Seric 	{
160859163Seric 		if (rewrite(pvp, rwset, e) == EX_TEMPFAIL)
160959163Seric 			*pstat = EX_TEMPFAIL;
161059541Seric 	}
16117682Seric 
16128181Seric 	/*
16138959Seric 	**  Do any final sanitation the address may require.
16148959Seric 	**	This will normally be used to turn internal forms
16158959Seric 	**	(e.g., user@host.LOCAL) into external form.  This
16168959Seric 	**	may be used as a default to the above rules.
16178959Seric 	*/
16188959Seric 
161959163Seric 	if (rewrite(pvp, 4, e) == EX_TEMPFAIL)
162059163Seric 		*pstat = EX_TEMPFAIL;
16218959Seric 
16228959Seric 	/*
16238181Seric 	**  Now restore the comment information we had at the beginning.
16248181Seric 	*/
16258181Seric 
162658825Seric 	cataddr(pvp, NULL, lbuf, sizeof lbuf, '\0');
162756678Seric 	define('g', lbuf, e);
162856678Seric 	expand(fancy, buf, &buf[sizeof buf - 1], e);
162956678Seric 	define('g', oldg, e);
16307682Seric 
16317682Seric 	if (tTd(12, 1))
16327755Seric 		printf("remotename => `%s'\n", buf);
16337682Seric 	return (buf);
16347682Seric }
163551317Seric /*
163656678Seric **  MAPLOCALUSER -- run local username through ruleset 5 for final redirection
163751317Seric **
163851317Seric **	Parameters:
163956678Seric **		a -- the address to map (but just the user name part).
164056678Seric **		sendq -- the sendq in which to install any replacement
164156678Seric **			addresses.
164251317Seric **
164351317Seric **	Returns:
164451317Seric **		none.
164551317Seric */
164651317Seric 
164756678Seric maplocaluser(a, sendq, e)
164856678Seric 	register ADDRESS *a;
164956678Seric 	ADDRESS **sendq;
165056678Seric 	ENVELOPE *e;
165151317Seric {
165256678Seric 	register char **pvp;
165356678Seric 	register ADDRESS *a1 = NULL;
165458333Seric 	auto char *delimptr;
165556678Seric 	char pvpbuf[PSBUFSIZE];
165651317Seric 
165756678Seric 	if (tTd(29, 1))
165856678Seric 	{
165956678Seric 		printf("maplocaluser: ");
166056678Seric 		printaddr(a, FALSE);
166156678Seric 	}
166258333Seric 	pvp = prescan(a->q_user, '\0', pvpbuf, &delimptr);
166356678Seric 	if (pvp == NULL)
166456678Seric 		return;
166551317Seric 
166659084Seric 	(void) rewrite(pvp, 5, e);
166758050Seric 	if (pvp[0] == NULL || (pvp[0][0] & 0377) != CANONNET)
166856678Seric 		return;
166951317Seric 
167056678Seric 	/* if non-null, mailer destination specified -- has it changed? */
167158966Seric 	a1 = buildaddr(pvp, NULL, e);
167256678Seric 	if (a1 == NULL || sameaddr(a, a1))
167356678Seric 		return;
167451317Seric 
167556678Seric 	/* mark old address as dead; insert new address */
167656678Seric 	a->q_flags |= QDONTSEND;
167757731Seric 	if (tTd(29, 5))
167857731Seric 	{
167957731Seric 		printf("maplocaluser: QDONTSEND ");
168057731Seric 		printaddr(a, FALSE);
168157731Seric 	}
168256678Seric 	a1->q_alias = a;
168358333Seric 	allocaddr(a1, 1, NULL, delimptr);
168456678Seric 	(void) recipient(a1, sendq, e);
168551317Seric }
168658802Seric /*
168758802Seric **  DEQUOTE_INIT -- initialize dequote map
168858802Seric **
168958802Seric **	This is a no-op.
169058802Seric **
169158802Seric **	Parameters:
169258802Seric **		map -- the internal map structure.
169358802Seric **		args -- arguments.
169458802Seric **
169558802Seric **	Returns:
169658802Seric **		TRUE.
169758802Seric */
169858802Seric 
169958802Seric bool
170060219Seric dequote_init(map, args)
170158802Seric 	MAP *map;
170258802Seric 	char *args;
170358802Seric {
170458805Seric 	register char *p = args;
170558805Seric 
170658805Seric 	for (;;)
170758805Seric 	{
170858805Seric 		while (isascii(*p) && isspace(*p))
170958805Seric 			p++;
171058805Seric 		if (*p != '-')
171158805Seric 			break;
171258805Seric 		switch (*++p)
171358805Seric 		{
171458805Seric 		  case 'a':
171558805Seric 			map->map_app = ++p;
171658805Seric 			break;
171758805Seric 		}
171858805Seric 		while (*p != '\0' && !(isascii(*p) && isspace(*p)))
171958805Seric 			p++;
172058805Seric 		if (*p != '\0')
172158805Seric 			*p = '\0';
172258805Seric 	}
172358805Seric 	if (map->map_app != NULL)
172458805Seric 		map->map_app = newstr(map->map_app);
172558805Seric 
172658802Seric 	return TRUE;
172758802Seric }
172858802Seric /*
172958802Seric **  DEQUOTE_MAP -- unquote an address
173058802Seric **
173158802Seric **	Parameters:
173258802Seric **		map -- the internal map structure (ignored).
173360089Seric **		name -- the name to dequote.
173458802Seric **		av -- arguments (ignored).
173559084Seric **		statp -- pointer to status out-parameter.
173658802Seric **
173758802Seric **	Returns:
173858802Seric **		NULL -- if there were no quotes, or if the resulting
173958802Seric **			unquoted buffer would not be acceptable to prescan.
174058802Seric **		else -- The dequoted buffer.
174158802Seric */
174258802Seric 
174358802Seric char *
174460089Seric dequote_map(map, name, av, statp)
174558802Seric 	MAP *map;
174660089Seric 	char *name;
174758802Seric 	char **av;
174859084Seric 	int *statp;
174958802Seric {
175058802Seric 	register char *p;
175158802Seric 	register char *q;
175258802Seric 	register char c;
175358802Seric 	int anglecnt;
175458802Seric 	int cmntcnt;
175558802Seric 	int quotecnt;
175659089Seric 	int spacecnt;
175758802Seric 	bool quotemode;
175858802Seric 	bool bslashmode;
175958802Seric 
176058802Seric 	anglecnt = 0;
176158802Seric 	cmntcnt = 0;
176258802Seric 	quotecnt = 0;
176359089Seric 	spacecnt = 0;
176458802Seric 	quotemode = FALSE;
176558802Seric 	bslashmode = FALSE;
176658802Seric 
176760089Seric 	for (p = q = name; (c = *p++) != '\0'; )
176858802Seric 	{
176958802Seric 		if (bslashmode)
177058802Seric 		{
177158802Seric 			bslashmode = FALSE;
177258802Seric 			*q++ = c;
177358802Seric 			continue;
177458802Seric 		}
177558802Seric 
177658802Seric 		switch (c)
177758802Seric 		{
177858802Seric 		  case '\\':
177958802Seric 			bslashmode = TRUE;
178058802Seric 			break;
178158802Seric 
178258802Seric 		  case '(':
178358802Seric 			cmntcnt++;
178458802Seric 			break;
178558802Seric 
178658802Seric 		  case ')':
178758802Seric 			if (cmntcnt-- <= 0)
178858802Seric 				return NULL;
178958802Seric 			break;
179059089Seric 
179159089Seric 		  case ' ':
179259089Seric 			spacecnt++;
179359089Seric 			break;
179458802Seric 		}
179558802Seric 
179658802Seric 		if (cmntcnt > 0)
179758802Seric 		{
179858802Seric 			*q++ = c;
179958802Seric 			continue;
180058802Seric 		}
180158802Seric 
180258802Seric 		switch (c)
180358802Seric 		{
180458802Seric 		  case '"':
180558802Seric 			quotemode = !quotemode;
180658802Seric 			quotecnt++;
180758802Seric 			continue;
180858802Seric 
180958802Seric 		  case '<':
181058802Seric 			anglecnt++;
181158802Seric 			break;
181258802Seric 
181358802Seric 		  case '>':
181458802Seric 			if (anglecnt-- <= 0)
181558802Seric 				return NULL;
181658802Seric 			break;
181758802Seric 		}
181858802Seric 		*q++ = c;
181958802Seric 	}
182058802Seric 
182158802Seric 	if (anglecnt != 0 || cmntcnt != 0 || bslashmode ||
182259089Seric 	    quotemode || quotecnt <= 0 || spacecnt != 0)
182358802Seric 		return NULL;
182458802Seric 	*q++ = '\0';
182560089Seric 	return name;
182658802Seric }
1827