1 /*
2  * Copyright (c) 1983 Eric P. Allman
3  * Copyright (c) 1988 Regents of the University of California.
4  * All rights reserved.
5  *
6  * %sccs.include.redist.c%
7  */
8 
9 #ifndef lint
10 static char sccsid[] = "@(#)parseaddr.c	6.35 (Berkeley) 03/30/93";
11 #endif /* not lint */
12 
13 # include "sendmail.h"
14 
15 /*
16 **  PARSEADDR -- Parse an address
17 **
18 **	Parses an address and breaks it up into three parts: a
19 **	net to transmit the message on, the host to transmit it
20 **	to, and a user on that host.  These are loaded into an
21 **	ADDRESS header with the values squirreled away if necessary.
22 **	The "user" part may not be a real user; the process may
23 **	just reoccur on that machine.  For example, on a machine
24 **	with an arpanet connection, the address
25 **		csvax.bill@berkeley
26 **	will break up to a "user" of 'csvax.bill' and a host
27 **	of 'berkeley' -- to be transmitted over the arpanet.
28 **
29 **	Parameters:
30 **		addr -- the address to parse.
31 **		a -- a pointer to the address descriptor buffer.
32 **			If NULL, a header will be created.
33 **		copyf -- determines what shall be copied:
34 **			-1 -- don't copy anything.  The printname
35 **				(q_paddr) is just addr, and the
36 **				user & host are allocated internally
37 **				to parse.
38 **			0 -- copy out the parsed user & host, but
39 **				don't copy the printname.
40 **			+1 -- copy everything.
41 **		delim -- the character to terminate the address, passed
42 **			to prescan.
43 **		delimptr -- if non-NULL, set to the location of the
44 **			delim character that was found.
45 **		e -- the envelope that will contain this address.
46 **
47 **	Returns:
48 **		A pointer to the address descriptor header (`a' if
49 **			`a' is non-NULL).
50 **		NULL on error.
51 **
52 **	Side Effects:
53 **		none
54 */
55 
56 /* following delimiters are inherent to the internal algorithms */
57 # define DELIMCHARS	"\201()<>,;\\\"\r\n"	/* word delimiters */
58 
59 ADDRESS *
60 parseaddr(addr, a, copyf, delim, delimptr, e)
61 	char *addr;
62 	register ADDRESS *a;
63 	int copyf;
64 	char delim;
65 	char **delimptr;
66 	register ENVELOPE *e;
67 {
68 	register char **pvp;
69 	auto char *delimptrbuf;
70 	char pvpbuf[PSBUFSIZE];
71 	extern char **prescan();
72 	extern ADDRESS *buildaddr();
73 	extern bool invalidaddr();
74 
75 	/*
76 	**  Initialize and prescan address.
77 	*/
78 
79 	e->e_to = addr;
80 	if (tTd(20, 1))
81 		printf("\n--parseaddr(%s)\n", addr);
82 
83 	if (invalidaddr(addr))
84 	{
85 		if (tTd(20, 1))
86 			printf("parseaddr-->bad address\n");
87 		return NULL;
88 	}
89 
90 	if (delimptr == NULL)
91 		delimptr = &delimptrbuf;
92 
93 	pvp = prescan(addr, delim, pvpbuf, delimptr);
94 	if (pvp == NULL)
95 	{
96 		if (tTd(20, 1))
97 			printf("parseaddr-->NULL\n");
98 		return (NULL);
99 	}
100 
101 	/*
102 	**  Apply rewriting rules.
103 	**	Ruleset 0 does basic parsing.  It must resolve.
104 	*/
105 
106 	rewrite(pvp, 3);
107 	rewrite(pvp, 0);
108 
109 	/*
110 	**  See if we resolved to a real mailer.
111 	*/
112 
113 	if ((pvp[0][0] & 0377) != CANONNET)
114 	{
115 		setstat(EX_USAGE);
116 		syserr("554 cannot resolve name");
117 		return (NULL);
118 	}
119 
120 	/*
121 	**  Build canonical address from pvp.
122 	*/
123 
124 	a = buildaddr(pvp, a);
125 	if (a == NULL)
126 		return (NULL);
127 
128 	/*
129 	**  Make local copies of the host & user and then
130 	**  transport them out.
131 	*/
132 
133 	allocaddr(a, copyf, addr, *delimptr);
134 
135 	/*
136 	**  Compute return value.
137 	*/
138 
139 	if (tTd(20, 1))
140 	{
141 		printf("parseaddr-->");
142 		printaddr(a, FALSE);
143 	}
144 
145 	return (a);
146 }
147 /*
148 **  INVALIDADDR -- check for address containing meta-characters
149 **
150 **	Parameters:
151 **		addr -- the address to check.
152 **
153 **	Returns:
154 **		TRUE -- if the address has any "wierd" characters
155 **		FALSE -- otherwise.
156 */
157 
158 bool
159 invalidaddr(addr)
160 	register char *addr;
161 {
162 	for (; *addr != '\0'; addr++)
163 	{
164 		if ((*addr & 0340) != 0200)
165 			continue;
166 		setstat(EX_USAGE);
167 		usrerr("553 Address contained invalid control characters");
168 		return TRUE;
169 	}
170 	return FALSE;
171 }
172 /*
173 **  ALLOCADDR -- do local allocations of address on demand.
174 **
175 **	Also lowercases the host name if requested.
176 **
177 **	Parameters:
178 **		a -- the address to reallocate.
179 **		copyf -- the copy flag (see parseaddr for description).
180 **		paddr -- the printname of the address.
181 **		delimptr -- a pointer to the address delimiter.  Must be set.
182 **
183 **	Returns:
184 **		none.
185 **
186 **	Side Effects:
187 **		Copies portions of a into local buffers as requested.
188 */
189 
190 allocaddr(a, copyf, paddr, delimptr)
191 	register ADDRESS *a;
192 	int copyf;
193 	char *paddr;
194 	char *delimptr;
195 {
196 	register MAILER *m = a->q_mailer;
197 
198 	if (tTd(24, 4))
199 		printf("allocaddr(copyf=%d, paddr=%s)\n", copyf, paddr);
200 
201 	if (copyf > 0 && paddr != NULL)
202 	{
203 		char savec = *delimptr;
204 
205 		*delimptr = '\0';
206 		a->q_paddr = newstr(paddr);
207 		*delimptr = savec;
208 	}
209 	else
210 		a->q_paddr = paddr;
211 
212 	if (a->q_user == NULL)
213 		a->q_user = "";
214 	if (a->q_host == NULL)
215 		a->q_host = "";
216 
217 	if (copyf >= 0)
218 	{
219 		a->q_host = newstr(a->q_host);
220 		if (a->q_user != a->q_paddr)
221 			a->q_user = newstr(a->q_user);
222 	}
223 
224 	if (a->q_paddr == NULL)
225 		a->q_paddr = a->q_user;
226 }
227 /*
228 **  PRESCAN -- Prescan name and make it canonical
229 **
230 **	Scans a name and turns it into a set of tokens.  This process
231 **	deletes blanks and comments (in parentheses).
232 **
233 **	This routine knows about quoted strings and angle brackets.
234 **
235 **	There are certain subtleties to this routine.  The one that
236 **	comes to mind now is that backslashes on the ends of names
237 **	are silently stripped off; this is intentional.  The problem
238 **	is that some versions of sndmsg (like at LBL) set the kill
239 **	character to something other than @ when reading addresses;
240 **	so people type "csvax.eric\@berkeley" -- which screws up the
241 **	berknet mailer.
242 **
243 **	Parameters:
244 **		addr -- the name to chomp.
245 **		delim -- the delimiter for the address, normally
246 **			'\0' or ','; \0 is accepted in any case.
247 **			If '\t' then we are reading the .cf file.
248 **		pvpbuf -- place to put the saved text -- note that
249 **			the pointers are static.
250 **		delimptr -- if non-NULL, set to the location of the
251 **			terminating delimiter.
252 **
253 **	Returns:
254 **		A pointer to a vector of tokens.
255 **		NULL on error.
256 */
257 
258 /* states and character types */
259 # define OPR		0	/* operator */
260 # define ATM		1	/* atom */
261 # define QST		2	/* in quoted string */
262 # define SPC		3	/* chewing up spaces */
263 # define ONE		4	/* pick up one character */
264 
265 # define NSTATES	5	/* number of states */
266 # define TYPE		017	/* mask to select state type */
267 
268 /* meta bits for table */
269 # define M		020	/* meta character; don't pass through */
270 # define B		040	/* cause a break */
271 # define MB		M|B	/* meta-break */
272 
273 static short StateTab[NSTATES][NSTATES] =
274 {
275    /*	oldst	chtype>	OPR	ATM	QST	SPC	ONE	*/
276 	/*OPR*/		OPR|B,	ATM|B,	QST|B,	SPC|MB,	ONE|B,
277 	/*ATM*/		OPR|B,	ATM,	QST|B,	SPC|MB,	ONE|B,
278 	/*QST*/		QST,	QST,	OPR,	QST,	QST,
279 	/*SPC*/		OPR,	ATM,	QST,	SPC|M,	ONE,
280 	/*ONE*/		OPR,	OPR,	OPR,	OPR,	OPR,
281 };
282 
283 # define NOCHAR		-1	/* signal nothing in lookahead token */
284 
285 char **
286 prescan(addr, delim, pvpbuf, delimptr)
287 	char *addr;
288 	char delim;
289 	char pvpbuf[];
290 	char **delimptr;
291 {
292 	register char *p;
293 	register char *q;
294 	register int c;
295 	char **avp;
296 	bool bslashmode;
297 	int cmntcnt;
298 	int anglecnt;
299 	char *tok;
300 	int state;
301 	int newstate;
302 	static char *av[MAXATOM+1];
303 	extern int errno;
304 
305 	/* make sure error messages don't have garbage on them */
306 	errno = 0;
307 
308 	q = pvpbuf;
309 	bslashmode = FALSE;
310 	cmntcnt = 0;
311 	anglecnt = 0;
312 	avp = av;
313 	state = ATM;
314 	c = NOCHAR;
315 	p = addr;
316 	if (tTd(22, 11))
317 	{
318 		printf("prescan: ");
319 		xputs(p);
320 		(void) putchar('\n');
321 	}
322 
323 	do
324 	{
325 		/* read a token */
326 		tok = q;
327 		for (;;)
328 		{
329 			/* store away any old lookahead character */
330 			if (c != NOCHAR)
331 			{
332 				/* see if there is room */
333 				if (q >= &pvpbuf[PSBUFSIZE - 5])
334 				{
335 					usrerr("553 Address too long");
336 					if (delimptr != NULL)
337 						*delimptr = p;
338 					return (NULL);
339 				}
340 
341 				/* squirrel it away */
342 				*q++ = c;
343 			}
344 
345 			/* read a new input character */
346 			c = *p++;
347 			if (c == '\0')
348 			{
349 				/* diagnose and patch up bad syntax */
350 				if (state == QST)
351 				{
352 					usrerr("553 Unbalanced '\"'");
353 					c = '"';
354 				}
355 				else if (cmntcnt > 0)
356 				{
357 					usrerr("553 Unbalanced '('");
358 					c = ')';
359 				}
360 				else if (anglecnt > 0)
361 				{
362 					c = '>';
363 					usrerr("553 Unbalanced '<'");
364 				}
365 				else
366 					break;
367 
368 				p--;
369 			}
370 			else if (c == delim && anglecnt <= 0 &&
371 					cmntcnt <= 0 && state != QST)
372 				break;
373 
374 			if (tTd(22, 101))
375 				printf("c=%c, s=%d; ", c, state);
376 
377 			/* chew up special characters */
378 			*q = '\0';
379 			if (bslashmode)
380 			{
381 				/* kludge \! for naive users */
382 				if (cmntcnt > 0)
383 					c = NOCHAR;
384 				else if (c != '!')
385 					*q++ = '\\';
386 				bslashmode = FALSE;
387 				continue;
388 			}
389 
390 			if (c == '\\')
391 			{
392 				bslashmode = TRUE;
393 				c = NOCHAR;
394 				continue;
395 			}
396 			else if (state == QST)
397 			{
398 				/* do nothing, just avoid next clauses */
399 			}
400 			else if (c == '(')
401 			{
402 				cmntcnt++;
403 				c = NOCHAR;
404 			}
405 			else if (c == ')')
406 			{
407 				if (cmntcnt <= 0)
408 				{
409 					usrerr("553 Unbalanced ')'");
410 					if (delimptr != NULL)
411 						*delimptr = p;
412 					return (NULL);
413 				}
414 				else
415 					cmntcnt--;
416 			}
417 			else if (cmntcnt > 0)
418 				c = NOCHAR;
419 			else if (c == '<')
420 				anglecnt++;
421 			else if (c == '>')
422 			{
423 				if (anglecnt <= 0)
424 				{
425 					usrerr("553 Unbalanced '>'");
426 					if (delimptr != NULL)
427 						*delimptr = p;
428 					return (NULL);
429 				}
430 				anglecnt--;
431 			}
432 			else if (delim == ' ' && isascii(c) && isspace(c))
433 				c = ' ';
434 
435 			if (c == NOCHAR)
436 				continue;
437 
438 			/* see if this is end of input */
439 			if (c == delim && anglecnt <= 0 && state != QST)
440 				break;
441 
442 			newstate = StateTab[state][toktype(c)];
443 			if (tTd(22, 101))
444 				printf("ns=%02o\n", newstate);
445 			state = newstate & TYPE;
446 			if (bitset(M, newstate))
447 				c = NOCHAR;
448 			if (bitset(B, newstate))
449 				break;
450 		}
451 
452 		/* new token */
453 		if (tok != q)
454 		{
455 			*q++ = '\0';
456 			if (tTd(22, 36))
457 			{
458 				printf("tok=");
459 				xputs(tok);
460 				(void) putchar('\n');
461 			}
462 			if (avp >= &av[MAXATOM])
463 			{
464 				syserr("553 prescan: too many tokens");
465 				if (delimptr != NULL)
466 					*delimptr = p;
467 				return (NULL);
468 			}
469 			*avp++ = tok;
470 		}
471 	} while (c != '\0' && (c != delim || anglecnt > 0));
472 	*avp = NULL;
473 	p--;
474 	if (delimptr != NULL)
475 		*delimptr = p;
476 	if (tTd(22, 12))
477 	{
478 		printf("prescan==>");
479 		printav(av);
480 	}
481 	if (av[0] == NULL)
482 		return (NULL);
483 	return (av);
484 }
485 /*
486 **  TOKTYPE -- return token type
487 **
488 **	Parameters:
489 **		c -- the character in question.
490 **
491 **	Returns:
492 **		Its type.
493 **
494 **	Side Effects:
495 **		none.
496 */
497 
498 toktype(c)
499 	register int c;
500 {
501 	static char buf[50];
502 	static bool firstime = TRUE;
503 
504 	if (firstime)
505 	{
506 		firstime = FALSE;
507 		expand("\201o", buf, &buf[sizeof buf - 1], CurEnv);
508 		(void) strcat(buf, DELIMCHARS);
509 	}
510 	c &= 0377;
511 	if (c == MATCHCLASS || c == MATCHREPL || c == MATCHNCLASS)
512 		return (ONE);
513 	if (c == '"')
514 		return (QST);
515 	if ((c & 0340) == 0200)
516 		return (OPR);
517 	if (!isascii(c))
518 		return (ATM);
519 	if (isspace(c) || c == ')')
520 		return (SPC);
521 	if (strchr(buf, c) != NULL)
522 		return (OPR);
523 	return (ATM);
524 }
525 /*
526 **  REWRITE -- apply rewrite rules to token vector.
527 **
528 **	This routine is an ordered production system.  Each rewrite
529 **	rule has a LHS (called the pattern) and a RHS (called the
530 **	rewrite); 'rwr' points the the current rewrite rule.
531 **
532 **	For each rewrite rule, 'avp' points the address vector we
533 **	are trying to match against, and 'pvp' points to the pattern.
534 **	If pvp points to a special match value (MATCHZANY, MATCHANY,
535 **	MATCHONE, MATCHCLASS, MATCHNCLASS) then the address in avp
536 **	matched is saved away in the match vector (pointed to by 'mvp').
537 **
538 **	When a match between avp & pvp does not match, we try to
539 **	back out.  If we back up over MATCHONE, MATCHCLASS, or MATCHNCLASS
540 **	we must also back out the match in mvp.  If we reach a
541 **	MATCHANY or MATCHZANY we just extend the match and start
542 **	over again.
543 **
544 **	When we finally match, we rewrite the address vector
545 **	and try over again.
546 **
547 **	Parameters:
548 **		pvp -- pointer to token vector.
549 **
550 **	Returns:
551 **		none.
552 **
553 **	Side Effects:
554 **		pvp is modified.
555 */
556 
557 struct match
558 {
559 	char	**first;	/* first token matched */
560 	char	**last;		/* last token matched */
561 	char	**pattern;	/* pointer to pattern */
562 };
563 
564 # define MAXMATCH	9	/* max params per rewrite */
565 
566 
567 rewrite(pvp, ruleset)
568 	char **pvp;
569 	int ruleset;
570 {
571 	register char *ap;		/* address pointer */
572 	register char *rp;		/* rewrite pointer */
573 	register char **avp;		/* address vector pointer */
574 	register char **rvp;		/* rewrite vector pointer */
575 	register struct match *mlp;	/* cur ptr into mlist */
576 	register struct rewrite *rwr;	/* pointer to current rewrite rule */
577 	int ruleno;			/* current rule number */
578 	struct match mlist[MAXMATCH];	/* stores match on LHS */
579 	char *npvp[MAXATOM+1];		/* temporary space for rebuild */
580 
581 	if (OpMode == MD_TEST || tTd(21, 2))
582 	{
583 		printf("rewrite: ruleset %2d   input:", ruleset);
584 		printav(pvp);
585 	}
586 	if (ruleset < 0 || ruleset >= MAXRWSETS)
587 	{
588 		syserr("554 rewrite: illegal ruleset number %d", ruleset);
589 		return;
590 	}
591 	if (pvp == NULL)
592 		return;
593 
594 	/*
595 	**  Run through the list of rewrite rules, applying
596 	**	any that match.
597 	*/
598 
599 	ruleno = 1;
600 	for (rwr = RewriteRules[ruleset]; rwr != NULL; )
601 	{
602 		int loopcount = 0;
603 
604 		if (tTd(21, 12))
605 		{
606 			printf("-----trying rule:");
607 			printav(rwr->r_lhs);
608 		}
609 
610 		/* try to match on this rule */
611 		mlp = mlist;
612 		rvp = rwr->r_lhs;
613 		avp = pvp;
614 		if (++loopcount > 100)
615 		{
616 			syserr("554 Infinite loop in ruleset %d, rule %d",
617 				ruleset, ruleno);
618 			if (tTd(21, 1))
619 			{
620 				printf("workspace: ");
621 				printav(pvp);
622 			}
623 			break;
624 		}
625 
626 		while ((ap = *avp) != NULL || *rvp != NULL)
627 		{
628 			rp = *rvp;
629 			if (tTd(21, 35))
630 			{
631 				printf("ADVANCE rp=");
632 				xputs(rp);
633 				printf(", ap=");
634 				xputs(ap);
635 				printf("\n");
636 			}
637 			if (rp == NULL)
638 			{
639 				/* end-of-pattern before end-of-address */
640 				goto backup;
641 			}
642 			if (ap == NULL && (*rp & 0377) != MATCHZANY &&
643 			    (*rp & 0377) != MATCHZERO)
644 			{
645 				/* end-of-input with patterns left */
646 				goto backup;
647 			}
648 
649 			switch (*rp & 0377)
650 			{
651 				register STAB *s;
652 				char buf[MAXLINE];
653 
654 			  case MATCHCLASS:
655 				/* match any phrase in a class */
656 				mlp->pattern = rvp;
657 				mlp->first = avp;
658 	extendclass:
659 				ap = *avp;
660 				if (ap == NULL)
661 					goto backup;
662 				mlp->last = avp++;
663 				cataddr(mlp->first, mlp->last, buf, sizeof buf, '\0');
664 				s = stab(buf, ST_CLASS, ST_FIND);
665 				if (s == NULL || !bitnset(rp[1], s->s_class))
666 				{
667 					if (tTd(21, 36))
668 					{
669 						printf("EXTEND  rp=");
670 						xputs(rp);
671 						printf(", ap=");
672 						xputs(ap);
673 						printf("\n");
674 					}
675 					goto extendclass;
676 				}
677 				if (tTd(21, 36))
678 					printf("CLMATCH\n");
679 				mlp++;
680 				break;
681 
682 			  case MATCHNCLASS:
683 				/* match any token not in a class */
684 				s = stab(ap, ST_CLASS, ST_FIND);
685 				if (s != NULL && bitnset(rp[1], s->s_class))
686 					goto backup;
687 
688 				/* fall through */
689 
690 			  case MATCHONE:
691 			  case MATCHANY:
692 				/* match exactly one token */
693 				mlp->pattern = rvp;
694 				mlp->first = avp;
695 				mlp->last = avp++;
696 				mlp++;
697 				break;
698 
699 			  case MATCHZANY:
700 				/* match zero or more tokens */
701 				mlp->pattern = rvp;
702 				mlp->first = avp;
703 				mlp->last = avp - 1;
704 				mlp++;
705 				break;
706 
707 			  case MATCHZERO:
708 				/* match zero tokens */
709 				break;
710 
711 			  default:
712 				/* must have exact match */
713 				if (strcasecmp(rp, ap))
714 					goto backup;
715 				avp++;
716 				break;
717 			}
718 
719 			/* successful match on this token */
720 			rvp++;
721 			continue;
722 
723 	  backup:
724 			/* match failed -- back up */
725 			while (--mlp >= mlist)
726 			{
727 				rvp = mlp->pattern;
728 				rp = *rvp;
729 				avp = mlp->last + 1;
730 				ap = *avp;
731 
732 				if (tTd(21, 36))
733 				{
734 					printf("BACKUP  rp=");
735 					xputs(rp);
736 					printf(", ap=");
737 					xputs(ap);
738 					printf("\n");
739 				}
740 
741 				if (ap == NULL)
742 				{
743 					/* run off the end -- back up again */
744 					continue;
745 				}
746 				if ((*rp & 0377) == MATCHANY ||
747 				    (*rp & 0377) == MATCHZANY)
748 				{
749 					/* extend binding and continue */
750 					mlp->last = avp++;
751 					rvp++;
752 					mlp++;
753 					break;
754 				}
755 				if ((*rp & 0377) == MATCHCLASS)
756 				{
757 					/* extend binding and try again */
758 					mlp->last = avp++;
759 					goto extendclass;
760 				}
761 			}
762 
763 			if (mlp < mlist)
764 			{
765 				/* total failure to match */
766 				break;
767 			}
768 		}
769 
770 		/*
771 		**  See if we successfully matched
772 		*/
773 
774 		if (mlp < mlist || *rvp != NULL)
775 		{
776 			if (tTd(21, 10))
777 				printf("----- rule fails\n");
778 			rwr = rwr->r_next;
779 			ruleno++;
780 			continue;
781 		}
782 
783 		rvp = rwr->r_rhs;
784 		if (tTd(21, 12))
785 		{
786 			printf("-----rule matches:");
787 			printav(rvp);
788 		}
789 
790 		rp = *rvp;
791 		if ((*rp & 0377) == CANONUSER)
792 		{
793 			rvp++;
794 			rwr = rwr->r_next;
795 			ruleno++;
796 		}
797 		else if ((*rp & 0377) == CANONHOST)
798 		{
799 			rvp++;
800 			rwr = NULL;
801 		}
802 		else if ((*rp & 0377) == CANONNET)
803 			rwr = NULL;
804 
805 		/* substitute */
806 		for (avp = npvp; *rvp != NULL; rvp++)
807 		{
808 			register struct match *m;
809 			register char **pp;
810 
811 			rp = *rvp;
812 			if ((*rp & 0377) == MATCHREPL)
813 			{
814 				/* substitute from LHS */
815 				m = &mlist[rp[1] - '1'];
816 				if (m < mlist || m >= mlp)
817 				{
818 					syserr("554 rewrite: ruleset %d: replacement $%c out of bounds",
819 						ruleset, rp[1]);
820 					return;
821 				}
822 				if (tTd(21, 15))
823 				{
824 					printf("$%c:", rp[1]);
825 					pp = m->first;
826 					while (pp <= m->last)
827 					{
828 						printf(" %x=\"", *pp);
829 						(void) fflush(stdout);
830 						printf("%s\"", *pp++);
831 					}
832 					printf("\n");
833 				}
834 				pp = m->first;
835 				while (pp <= m->last)
836 				{
837 					if (avp >= &npvp[MAXATOM])
838 					{
839 						syserr("554 rewrite: expansion too long");
840 						return;
841 					}
842 					*avp++ = *pp++;
843 				}
844 			}
845 			else
846 			{
847 				/* vanilla replacement */
848 				if (avp >= &npvp[MAXATOM])
849 				{
850 	toolong:
851 					syserr("554 rewrite: expansion too long");
852 					return;
853 				}
854 				*avp++ = rp;
855 			}
856 		}
857 		*avp++ = NULL;
858 
859 		/*
860 		**  Check for any hostname/keyword lookups.
861 		*/
862 
863 		for (rvp = npvp; *rvp != NULL; rvp++)
864 		{
865 			char **hbrvp;
866 			char **xpvp;
867 			int trsize;
868 			char *olddelimchar;
869 			char *replac;
870 			int endtoken;
871 			STAB *map;
872 			char *mapname;
873 			char **key_rvp;
874 			char **arg_rvp;
875 			char **default_rvp;
876 			char buf[MAXNAME + 1];
877 			char *pvpb1[MAXATOM + 1];
878 			char *argvect[10];
879 			char pvpbuf[PSBUFSIZE];
880 
881 			if ((**rvp & 0377) != HOSTBEGIN &&
882 			    (**rvp & 0377) != LOOKUPBEGIN)
883 				continue;
884 
885 			/*
886 			**  Got a hostname/keyword lookup.
887 			**
888 			**	This could be optimized fairly easily.
889 			*/
890 
891 			hbrvp = rvp;
892 			if ((**rvp & 0377) == HOSTBEGIN)
893 			{
894 				endtoken = HOSTEND;
895 				mapname = "host";
896 			}
897 			else
898 			{
899 				endtoken = LOOKUPEND;
900 				mapname = *++rvp;
901 			}
902 			map = stab(mapname, ST_MAP, ST_FIND);
903 			if (map == NULL)
904 				syserr("554 rewrite: map %s not found", mapname);
905 
906 			/* extract the match part */
907 			key_rvp = ++rvp;
908 			default_rvp = NULL;
909 			arg_rvp = argvect;
910 			xpvp = NULL;
911 			replac = pvpbuf;
912 			while (*rvp != NULL && (**rvp & 0377) != endtoken)
913 			{
914 				int nodetype = **rvp & 0377;
915 
916 				if (nodetype != CANONHOST && nodetype != CANONUSER)
917 				{
918 					rvp++;
919 					continue;
920 				}
921 
922 				*rvp++ = NULL;
923 
924 				if (xpvp != NULL)
925 				{
926 					cataddr(xpvp, NULL, replac,
927 						&pvpbuf[sizeof pvpbuf] - replac,
928 						'\0');
929 					*++arg_rvp = replac;
930 					replac += strlen(replac) + 1;
931 					xpvp = NULL;
932 				}
933 				switch (nodetype)
934 				{
935 				  case CANONHOST:
936 					xpvp = rvp;
937 					break;
938 
939 				  case CANONUSER:
940 					default_rvp = rvp;
941 					break;
942 				}
943 			}
944 			if (*rvp != NULL)
945 				*rvp++ = NULL;
946 			if (xpvp != NULL)
947 			{
948 				cataddr(xpvp, NULL, replac,
949 					&pvpbuf[sizeof pvpbuf] - replac,
950 					'\0');
951 				*++arg_rvp = replac;
952 			}
953 			*++arg_rvp = NULL;
954 
955 			/* save the remainder of the input string */
956 			trsize = (int) (avp - rvp + 1) * sizeof *rvp;
957 			bcopy((char *) rvp, (char *) pvpb1, trsize);
958 
959 			/* look it up */
960 			cataddr(key_rvp, NULL, buf, sizeof buf, '\0');
961 			argvect[0] = buf;
962 			if (map != NULL && bitset(MF_VALID, map->s_map.map_flags))
963 			{
964 				int bsize = sizeof buf - 1;
965 
966 				if (map->s_map.map_app != NULL)
967 					bsize -= strlen(map->s_map.map_app);
968 				if (tTd(60, 1))
969 					printf("map_lookup(%s, %s) => ",
970 						mapname, buf);
971 				replac = (*map->s_map.map_class->map_lookup)(&map->s_map,
972 						buf, sizeof buf - 1, argvect);
973 				if (replac != NULL && map->s_map.map_app != NULL)
974 					strcat(replac, map->s_map.map_app);
975 				if (tTd(60, 1))
976 					printf("%s\n", replac ? replac : "NOT FOUND");
977 			}
978 			else
979 				replac = NULL;
980 
981 			/* if no replacement, use default */
982 			if (replac == NULL && default_rvp != NULL)
983 			{
984 				char buf2[sizeof buf];
985 
986 				/* rewrite the default with % translations */
987 				cataddr(default_rvp, NULL, buf2, sizeof buf2, '\0');
988 				map_rewrite(buf2, sizeof buf2, buf, sizeof buf,
989 					argvect);
990 				replac = buf;
991 			}
992 
993 			if (replac == NULL)
994 			{
995 				xpvp = key_rvp;
996 			}
997 			else
998 			{
999 				/* scan the new replacement */
1000 				xpvp = prescan(replac, '\0', pvpbuf, NULL);
1001 				if (xpvp == NULL)
1002 				{
1003 					/* prescan already printed error */
1004 					return;
1005 				}
1006 			}
1007 
1008 			/* append it to the token list */
1009 			for (avp = hbrvp; *xpvp != NULL; xpvp++)
1010 			{
1011 				*avp++ = newstr(*xpvp);
1012 				if (avp >= &npvp[MAXATOM])
1013 					goto toolong;
1014 			}
1015 
1016 			/* restore the old trailing information */
1017 			for (xpvp = pvpb1; (*avp++ = *xpvp++) != NULL; )
1018 				if (avp >= &npvp[MAXATOM])
1019 					goto toolong;
1020 
1021 			break;
1022 		}
1023 
1024 		/*
1025 		**  Check for subroutine calls.
1026 		*/
1027 
1028 		if (*npvp != NULL && (**npvp & 0377) == CALLSUBR)
1029 		{
1030 			bcopy((char *) &npvp[2], (char *) pvp,
1031 				(int) (avp - npvp - 2) * sizeof *avp);
1032 			if (tTd(21, 3))
1033 				printf("-----callsubr %s\n", npvp[1]);
1034 			rewrite(pvp, atoi(npvp[1]));
1035 		}
1036 		else
1037 		{
1038 			bcopy((char *) npvp, (char *) pvp,
1039 				(int) (avp - npvp) * sizeof *avp);
1040 		}
1041 		if (tTd(21, 4))
1042 		{
1043 			printf("rewritten as:");
1044 			printav(pvp);
1045 		}
1046 	}
1047 
1048 	if (OpMode == MD_TEST || tTd(21, 2))
1049 	{
1050 		printf("rewrite: ruleset %2d returns:", ruleset);
1051 		printav(pvp);
1052 	}
1053 }
1054 /*
1055 **  BUILDADDR -- build address from token vector.
1056 **
1057 **	Parameters:
1058 **		tv -- token vector.
1059 **		a -- pointer to address descriptor to fill.
1060 **			If NULL, one will be allocated.
1061 **
1062 **	Returns:
1063 **		NULL if there was an error.
1064 **		'a' otherwise.
1065 **
1066 **	Side Effects:
1067 **		fills in 'a'
1068 */
1069 
1070 struct errcodes
1071 {
1072 	char	*ec_name;		/* name of error code */
1073 	int	ec_code;		/* numeric code */
1074 } ErrorCodes[] =
1075 {
1076 	"usage",	EX_USAGE,
1077 	"nouser",	EX_NOUSER,
1078 	"nohost",	EX_NOHOST,
1079 	"unavailable",	EX_UNAVAILABLE,
1080 	"software",	EX_SOFTWARE,
1081 	"tempfail",	EX_TEMPFAIL,
1082 	"protocol",	EX_PROTOCOL,
1083 #ifdef EX_CONFIG
1084 	"config",	EX_CONFIG,
1085 #endif
1086 	NULL,		EX_UNAVAILABLE,
1087 };
1088 
1089 ADDRESS *
1090 buildaddr(tv, a)
1091 	register char **tv;
1092 	register ADDRESS *a;
1093 {
1094 	struct mailer **mp;
1095 	register struct mailer *m;
1096 	char *bp;
1097 	int spaceleft;
1098 	static char buf[MAXNAME];
1099 
1100 	if (a == NULL)
1101 		a = (ADDRESS *) xalloc(sizeof *a);
1102 	bzero((char *) a, sizeof *a);
1103 
1104 	/* figure out what net/mailer to use */
1105 	if ((**tv & 0377) != CANONNET)
1106 	{
1107 		syserr("554 buildaddr: no net");
1108 		return (NULL);
1109 	}
1110 	tv++;
1111 	if (strcasecmp(*tv, "error") == 0)
1112 	{
1113 		if ((**++tv & 0377) == CANONHOST)
1114 		{
1115 			register struct errcodes *ep;
1116 
1117 			if (isascii(**++tv) && isdigit(**tv))
1118 			{
1119 				setstat(atoi(*tv));
1120 			}
1121 			else
1122 			{
1123 				for (ep = ErrorCodes; ep->ec_name != NULL; ep++)
1124 					if (strcasecmp(ep->ec_name, *tv) == 0)
1125 						break;
1126 				setstat(ep->ec_code);
1127 			}
1128 			tv++;
1129 		}
1130 		if ((**tv & 0377) != CANONUSER)
1131 			syserr("554 buildaddr: error: no user");
1132 		cataddr(++tv, NULL, buf, sizeof buf, ' ');
1133 		stripquotes(buf);
1134 		usrerr(buf);
1135 		return (NULL);
1136 	}
1137 
1138 	for (mp = Mailer; (m = *mp++) != NULL; )
1139 	{
1140 		if (strcasecmp(m->m_name, *tv) == 0)
1141 			break;
1142 	}
1143 	if (m == NULL)
1144 	{
1145 		syserr("554 buildaddr: unknown mailer %s", *tv);
1146 		return (NULL);
1147 	}
1148 	a->q_mailer = m;
1149 
1150 	/* figure out what host (if any) */
1151 	tv++;
1152 	if ((**tv & 0377) == CANONHOST)
1153 	{
1154 		bp = buf;
1155 		spaceleft = sizeof buf - 1;
1156 		while (*++tv != NULL && (**tv & 0377) != CANONUSER)
1157 		{
1158 			int i = strlen(*tv);
1159 
1160 			if (i > spaceleft)
1161 			{
1162 				/* out of space for this address */
1163 				if (spaceleft >= 0)
1164 					syserr("554 buildaddr: host too long (%.40s...)",
1165 						buf);
1166 				i = spaceleft;
1167 				spaceleft = 0;
1168 			}
1169 			if (i <= 0)
1170 				continue;
1171 			bcopy(*tv, bp, i);
1172 			bp += i;
1173 			spaceleft -= i;
1174 		}
1175 		*bp = '\0';
1176 		a->q_host = newstr(buf);
1177 	}
1178 	else
1179 	{
1180 		if (!bitnset(M_LOCALMAILER, m->m_flags))
1181 		{
1182 			syserr("554 buildaddr: no host");
1183 			return (NULL);
1184 		}
1185 		a->q_host = NULL;
1186 	}
1187 
1188 	/* figure out the user */
1189 	if (*tv == NULL || (**tv & 0377) != CANONUSER)
1190 	{
1191 		syserr("554 buildaddr: no user");
1192 		return (NULL);
1193 	}
1194 	tv++;
1195 
1196 	/* do special mapping for local mailer */
1197 	if (m == LocalMailer && *tv != NULL)
1198 	{
1199 		register char *p = *tv;
1200 
1201 		if (*p == '"')
1202 			p++;
1203 		if (*p == '|')
1204 			a->q_mailer = m = ProgMailer;
1205 		else if (*p == '/')
1206 			a->q_mailer = m = FileMailer;
1207 		else if (*p == ':')
1208 		{
1209 			/* may be :include: */
1210 			cataddr(tv, NULL, buf, sizeof buf, '\0');
1211 			stripquotes(buf);
1212 			if (strncasecmp(buf, ":include:", 9) == 0)
1213 			{
1214 				/* if :include:, don't need further rewriting */
1215 				a->q_mailer = m = InclMailer;
1216 				a->q_user = &buf[9];
1217 				return (a);
1218 			}
1219 		}
1220 	}
1221 
1222 	if (m == LocalMailer && *tv != NULL && strcmp(*tv, "@") == 0)
1223 	{
1224 		tv++;
1225 		a->q_flags |= QNOTREMOTE;
1226 	}
1227 
1228 	/* do cleanup of final address */
1229 	rewrite(tv, 4);
1230 
1231 	/* save the result for the command line/RCPT argument */
1232 	cataddr(tv, NULL, buf, sizeof buf, '\0');
1233 	a->q_user = buf;
1234 
1235 	/*
1236 	**  Do mapping to lower case as requested by mailer
1237 	*/
1238 
1239 	if (a->q_host != NULL && !bitnset(M_HST_UPPER, m->m_flags))
1240 		makelower(a->q_host);
1241 	if (!bitnset(M_USR_UPPER, m->m_flags))
1242 		makelower(a->q_user);
1243 
1244 	return (a);
1245 }
1246 /*
1247 **  CATADDR -- concatenate pieces of addresses (putting in <LWSP> subs)
1248 **
1249 **	Parameters:
1250 **		pvp -- parameter vector to rebuild.
1251 **		evp -- last parameter to include.  Can be NULL to
1252 **			use entire pvp.
1253 **		buf -- buffer to build the string into.
1254 **		sz -- size of buf.
1255 **		spacesub -- the space separator character; if null,
1256 **			use SpaceSub.
1257 **
1258 **	Returns:
1259 **		none.
1260 **
1261 **	Side Effects:
1262 **		Destroys buf.
1263 */
1264 
1265 cataddr(pvp, evp, buf, sz, spacesub)
1266 	char **pvp;
1267 	char **evp;
1268 	char *buf;
1269 	register int sz;
1270 	char spacesub;
1271 {
1272 	bool oatomtok = FALSE;
1273 	bool natomtok = FALSE;
1274 	register int i;
1275 	register char *p;
1276 
1277 	if (spacesub == '\0')
1278 		spacesub = SpaceSub;
1279 
1280 	if (pvp == NULL)
1281 	{
1282 		(void) strcpy(buf, "");
1283 		return;
1284 	}
1285 	p = buf;
1286 	sz -= 2;
1287 	while (*pvp != NULL && (i = strlen(*pvp)) < sz)
1288 	{
1289 		natomtok = (toktype(**pvp) == ATM);
1290 		if (oatomtok && natomtok)
1291 			*p++ = spacesub;
1292 		(void) strcpy(p, *pvp);
1293 		oatomtok = natomtok;
1294 		p += i;
1295 		sz -= i + 1;
1296 		if (pvp++ == evp)
1297 			break;
1298 	}
1299 	*p = '\0';
1300 }
1301 /*
1302 **  SAMEADDR -- Determine if two addresses are the same
1303 **
1304 **	This is not just a straight comparison -- if the mailer doesn't
1305 **	care about the host we just ignore it, etc.
1306 **
1307 **	Parameters:
1308 **		a, b -- pointers to the internal forms to compare.
1309 **
1310 **	Returns:
1311 **		TRUE -- they represent the same mailbox.
1312 **		FALSE -- they don't.
1313 **
1314 **	Side Effects:
1315 **		none.
1316 */
1317 
1318 bool
1319 sameaddr(a, b)
1320 	register ADDRESS *a;
1321 	register ADDRESS *b;
1322 {
1323 	/* if they don't have the same mailer, forget it */
1324 	if (a->q_mailer != b->q_mailer)
1325 		return (FALSE);
1326 
1327 	/* if the user isn't the same, we can drop out */
1328 	if (strcmp(a->q_user, b->q_user) != 0)
1329 		return (FALSE);
1330 
1331 	/* if we have good uids for both but the differ, these are different */
1332 	if (bitset(QGOODUID, a->q_flags & b->q_flags) && a->q_uid != b->q_uid)
1333 		return (FALSE);
1334 
1335 	/* otherwise compare hosts (but be careful for NULL ptrs) */
1336 	if (a->q_host == b->q_host)
1337 	{
1338 		/* probably both null pointers */
1339 		return (TRUE);
1340 	}
1341 	if (a->q_host == NULL || b->q_host == NULL)
1342 	{
1343 		/* only one is a null pointer */
1344 		return (FALSE);
1345 	}
1346 	if (strcmp(a->q_host, b->q_host) != 0)
1347 		return (FALSE);
1348 
1349 	return (TRUE);
1350 }
1351 /*
1352 **  PRINTADDR -- print address (for debugging)
1353 **
1354 **	Parameters:
1355 **		a -- the address to print
1356 **		follow -- follow the q_next chain.
1357 **
1358 **	Returns:
1359 **		none.
1360 **
1361 **	Side Effects:
1362 **		none.
1363 */
1364 
1365 printaddr(a, follow)
1366 	register ADDRESS *a;
1367 	bool follow;
1368 {
1369 	bool first = TRUE;
1370 	register MAILER *m;
1371 	MAILER pseudomailer;
1372 
1373 	while (a != NULL)
1374 	{
1375 		first = FALSE;
1376 		printf("%x=", a);
1377 		(void) fflush(stdout);
1378 
1379 		/* find the mailer -- carefully */
1380 		m = a->q_mailer;
1381 		if (m == NULL)
1382 		{
1383 			m = &pseudomailer;
1384 			m->m_mno = -1;
1385 			m->m_name = "NULL";
1386 		}
1387 
1388 		printf("%s:\n\tmailer %d (%s), host `%s', user `%s', ruser `%s'\n",
1389 		       a->q_paddr, m->m_mno, m->m_name,
1390 		       a->q_host, a->q_user, a->q_ruser? a->q_ruser: "<null>");
1391 		printf("\tnext=%x, flags=%o, alias %x\n", a->q_next, a->q_flags,
1392 		       a->q_alias);
1393 		printf("\thome=\"%s\", fullname=\"%s\"\n", a->q_home,
1394 		       a->q_fullname);
1395 
1396 		if (!follow)
1397 			return;
1398 		a = a->q_next;
1399 	}
1400 	if (first)
1401 		printf("[NULL]\n");
1402 }
1403 
1404 /*
1405 **  REMOTENAME -- return the name relative to the current mailer
1406 **
1407 **	Parameters:
1408 **		name -- the name to translate.
1409 **		m -- the mailer that we want to do rewriting relative
1410 **			to.
1411 **		senderaddress -- if set, uses the sender rewriting rules
1412 **			rather than the recipient rewriting rules.
1413 **		header -- set if this address is in the header, rather
1414 **			than an envelope header.
1415 **		canonical -- if set, strip out any comment information,
1416 **			etc.
1417 **		adddomain -- if set, OK to do domain extension.
1418 **		e -- the current envelope.
1419 **
1420 **	Returns:
1421 **		the text string representing this address relative to
1422 **			the receiving mailer.
1423 **
1424 **	Side Effects:
1425 **		none.
1426 **
1427 **	Warnings:
1428 **		The text string returned is tucked away locally;
1429 **			copy it if you intend to save it.
1430 */
1431 
1432 char *
1433 remotename(name, m, senderaddress, header, canonical, adddomain, e)
1434 	char *name;
1435 	struct mailer *m;
1436 	bool senderaddress;
1437 	bool header;
1438 	bool canonical;
1439 	bool adddomain;
1440 	register ENVELOPE *e;
1441 {
1442 	register char **pvp;
1443 	char *fancy;
1444 	extern char *macvalue();
1445 	char *oldg = macvalue('g', e);
1446 	int rwset;
1447 	static char buf[MAXNAME];
1448 	char lbuf[MAXNAME];
1449 	char pvpbuf[PSBUFSIZE];
1450 	extern char **prescan();
1451 	extern char *crackaddr();
1452 
1453 	if (tTd(12, 1))
1454 		printf("remotename(%s)\n", name);
1455 
1456 	/* don't do anything if we are tagging it as special */
1457 	if (senderaddress)
1458 		rwset = header ? m->m_sh_rwset : m->m_se_rwset;
1459 	else
1460 		rwset = header ? m->m_rh_rwset : m->m_re_rwset;
1461 	if (rwset < 0)
1462 		return (name);
1463 
1464 	/*
1465 	**  Do a heuristic crack of this name to extract any comment info.
1466 	**	This will leave the name as a comment and a $g macro.
1467 	*/
1468 
1469 	if (canonical || bitnset(M_NOCOMMENT, m->m_flags))
1470 		fancy = "\201g";
1471 	else
1472 		fancy = crackaddr(name);
1473 
1474 	/*
1475 	**  Turn the name into canonical form.
1476 	**	Normally this will be RFC 822 style, i.e., "user@domain".
1477 	**	If this only resolves to "user", and the "C" flag is
1478 	**	specified in the sending mailer, then the sender's
1479 	**	domain will be appended.
1480 	*/
1481 
1482 	pvp = prescan(name, '\0', pvpbuf, NULL);
1483 	if (pvp == NULL)
1484 		return (name);
1485 	rewrite(pvp, 3);
1486 	if (adddomain && e->e_fromdomain != NULL)
1487 	{
1488 		/* append from domain to this address */
1489 		register char **pxp = pvp;
1490 
1491 		/* see if there is an "@domain" in the current name */
1492 		while (*pxp != NULL && strcmp(*pxp, "@") != 0)
1493 			pxp++;
1494 		if (*pxp == NULL)
1495 		{
1496 			/* no.... append the "@domain" from the sender */
1497 			register char **qxq = e->e_fromdomain;
1498 
1499 			while ((*pxp++ = *qxq++) != NULL)
1500 				continue;
1501 			rewrite(pvp, 3);
1502 		}
1503 	}
1504 
1505 	/*
1506 	**  Do more specific rewriting.
1507 	**	Rewrite using ruleset 1 or 2 depending on whether this is
1508 	**		a sender address or not.
1509 	**	Then run it through any receiving-mailer-specific rulesets.
1510 	*/
1511 
1512 	if (senderaddress)
1513 		rewrite(pvp, 1);
1514 	else
1515 		rewrite(pvp, 2);
1516 	if (rwset > 0)
1517 		rewrite(pvp, rwset);
1518 
1519 	/*
1520 	**  Do any final sanitation the address may require.
1521 	**	This will normally be used to turn internal forms
1522 	**	(e.g., user@host.LOCAL) into external form.  This
1523 	**	may be used as a default to the above rules.
1524 	*/
1525 
1526 	rewrite(pvp, 4);
1527 
1528 	/*
1529 	**  Now restore the comment information we had at the beginning.
1530 	*/
1531 
1532 	cataddr(pvp, NULL, lbuf, sizeof lbuf, '\0');
1533 	define('g', lbuf, e);
1534 	expand(fancy, buf, &buf[sizeof buf - 1], e);
1535 	define('g', oldg, e);
1536 
1537 	if (tTd(12, 1))
1538 		printf("remotename => `%s'\n", buf);
1539 	return (buf);
1540 }
1541 /*
1542 **  MAPLOCALUSER -- run local username through ruleset 5 for final redirection
1543 **
1544 **	Parameters:
1545 **		a -- the address to map (but just the user name part).
1546 **		sendq -- the sendq in which to install any replacement
1547 **			addresses.
1548 **
1549 **	Returns:
1550 **		none.
1551 */
1552 
1553 maplocaluser(a, sendq, e)
1554 	register ADDRESS *a;
1555 	ADDRESS **sendq;
1556 	ENVELOPE *e;
1557 {
1558 	register char **pvp;
1559 	register ADDRESS *a1 = NULL;
1560 	auto char *delimptr;
1561 	char pvpbuf[PSBUFSIZE];
1562 
1563 	if (tTd(29, 1))
1564 	{
1565 		printf("maplocaluser: ");
1566 		printaddr(a, FALSE);
1567 	}
1568 	pvp = prescan(a->q_user, '\0', pvpbuf, &delimptr);
1569 	if (pvp == NULL)
1570 		return;
1571 
1572 	rewrite(pvp, 5);
1573 	if (pvp[0] == NULL || (pvp[0][0] & 0377) != CANONNET)
1574 		return;
1575 
1576 	/* if non-null, mailer destination specified -- has it changed? */
1577 	a1 = buildaddr(pvp, NULL);
1578 	if (a1 == NULL || sameaddr(a, a1))
1579 		return;
1580 
1581 	/* mark old address as dead; insert new address */
1582 	a->q_flags |= QDONTSEND;
1583 	if (tTd(29, 5))
1584 	{
1585 		printf("maplocaluser: QDONTSEND ");
1586 		printaddr(a, FALSE);
1587 	}
1588 	a1->q_alias = a;
1589 	allocaddr(a1, 1, NULL, delimptr);
1590 	(void) recipient(a1, sendq, e);
1591 }
1592 /*
1593 **  DEQUOTE_INIT -- initialize dequote map
1594 **
1595 **	This is a no-op.
1596 **
1597 **	Parameters:
1598 **		map -- the internal map structure.
1599 **		mapname -- the name of the mapl.
1600 **		args -- arguments.
1601 **
1602 **	Returns:
1603 **		TRUE.
1604 */
1605 
1606 bool
1607 dequote_init(map, mapname, args)
1608 	MAP *map;
1609 	char *mapname;
1610 	char *args;
1611 {
1612 	register char *p = args;
1613 
1614 	for (;;)
1615 	{
1616 		while (isascii(*p) && isspace(*p))
1617 			p++;
1618 		if (*p != '-')
1619 			break;
1620 		switch (*++p)
1621 		{
1622 		  case 'a':
1623 			map->map_app = ++p;
1624 			break;
1625 		}
1626 		while (*p != '\0' && !(isascii(*p) && isspace(*p)))
1627 			p++;
1628 		if (*p != '\0')
1629 			*p = '\0';
1630 	}
1631 	if (map->map_app != NULL)
1632 		map->map_app = newstr(map->map_app);
1633 
1634 	return TRUE;
1635 }
1636 /*
1637 **  DEQUOTE_MAP -- unquote an address
1638 **
1639 **	Parameters:
1640 **		map -- the internal map structure (ignored).
1641 **		buf -- the buffer to dequote.
1642 **		bufsiz -- the size of that buffer.
1643 **		av -- arguments (ignored).
1644 **
1645 **	Returns:
1646 **		NULL -- if there were no quotes, or if the resulting
1647 **			unquoted buffer would not be acceptable to prescan.
1648 **		else -- The dequoted buffer.
1649 */
1650 
1651 char *
1652 dequote_map(map, buf, bufsiz, av)
1653 	MAP *map;
1654 	char buf[];
1655 	int bufsiz;
1656 	char **av;
1657 {
1658 	register char *p;
1659 	register char *q;
1660 	register char c;
1661 	int anglecnt;
1662 	int cmntcnt;
1663 	int quotecnt;
1664 	bool quotemode;
1665 	bool bslashmode;
1666 
1667 	anglecnt = 0;
1668 	cmntcnt = 0;
1669 	quotecnt = 0;
1670 	quotemode = FALSE;
1671 	bslashmode = FALSE;
1672 
1673 	for (p = q = buf; (c = *p++) != '\0'; )
1674 	{
1675 		if (bslashmode)
1676 		{
1677 			bslashmode = FALSE;
1678 			*q++ = c;
1679 			continue;
1680 		}
1681 
1682 		switch (c)
1683 		{
1684 		  case '\\':
1685 			bslashmode = TRUE;
1686 			break;
1687 
1688 		  case '(':
1689 			cmntcnt++;
1690 			break;
1691 
1692 		  case ')':
1693 			if (cmntcnt-- <= 0)
1694 				return NULL;
1695 			break;
1696 		}
1697 
1698 		if (cmntcnt > 0)
1699 		{
1700 			*q++ = c;
1701 			continue;
1702 		}
1703 
1704 		switch (c)
1705 		{
1706 		  case '"':
1707 			quotemode = !quotemode;
1708 			quotecnt++;
1709 			continue;
1710 
1711 		  case '<':
1712 			anglecnt++;
1713 			break;
1714 
1715 		  case '>':
1716 			if (anglecnt-- <= 0)
1717 				return NULL;
1718 			break;
1719 		}
1720 		*q++ = c;
1721 	}
1722 
1723 	if (anglecnt != 0 || cmntcnt != 0 || bslashmode ||
1724 	    quotemode || quotecnt <= 0)
1725 		return NULL;
1726 	*q++ = '\0';
1727 	return buf;
1728 }
1729