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.36 (Berkeley) 04/05/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, e);
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 **		e -- the current envelope.
1062 **
1063 **	Returns:
1064 **		NULL if there was an error.
1065 **		'a' otherwise.
1066 **
1067 **	Side Effects:
1068 **		fills in 'a'
1069 */
1070 
1071 struct errcodes
1072 {
1073 	char	*ec_name;		/* name of error code */
1074 	int	ec_code;		/* numeric code */
1075 } ErrorCodes[] =
1076 {
1077 	"usage",	EX_USAGE,
1078 	"nouser",	EX_NOUSER,
1079 	"nohost",	EX_NOHOST,
1080 	"unavailable",	EX_UNAVAILABLE,
1081 	"software",	EX_SOFTWARE,
1082 	"tempfail",	EX_TEMPFAIL,
1083 	"protocol",	EX_PROTOCOL,
1084 #ifdef EX_CONFIG
1085 	"config",	EX_CONFIG,
1086 #endif
1087 	NULL,		EX_UNAVAILABLE,
1088 };
1089 
1090 ADDRESS *
1091 buildaddr(tv, a, e)
1092 	register char **tv;
1093 	register ADDRESS *a;
1094 	register ENVELOPE *e;
1095 {
1096 	struct mailer **mp;
1097 	register struct mailer *m;
1098 	char *bp;
1099 	int spaceleft;
1100 	static char buf[MAXNAME];
1101 
1102 	if (a == NULL)
1103 		a = (ADDRESS *) xalloc(sizeof *a);
1104 	bzero((char *) a, sizeof *a);
1105 
1106 	/* figure out what net/mailer to use */
1107 	if ((**tv & 0377) != CANONNET)
1108 	{
1109 		syserr("554 buildaddr: no net");
1110 		return (NULL);
1111 	}
1112 	tv++;
1113 	if (strcasecmp(*tv, "error") == 0)
1114 	{
1115 		if ((**++tv & 0377) == CANONHOST)
1116 		{
1117 			register struct errcodes *ep;
1118 
1119 			if (isascii(**++tv) && isdigit(**tv))
1120 			{
1121 				setstat(atoi(*tv));
1122 			}
1123 			else
1124 			{
1125 				for (ep = ErrorCodes; ep->ec_name != NULL; ep++)
1126 					if (strcasecmp(ep->ec_name, *tv) == 0)
1127 						break;
1128 				setstat(ep->ec_code);
1129 			}
1130 			tv++;
1131 		}
1132 		if ((**tv & 0377) != CANONUSER)
1133 			syserr("554 buildaddr: error: no user");
1134 		cataddr(++tv, NULL, buf, sizeof buf, ' ');
1135 		stripquotes(buf);
1136 		usrerr(buf);
1137 		if (e->e_message == NULL)
1138 			e->e_message = newstr(buf);
1139 		return (NULL);
1140 	}
1141 
1142 	for (mp = Mailer; (m = *mp++) != NULL; )
1143 	{
1144 		if (strcasecmp(m->m_name, *tv) == 0)
1145 			break;
1146 	}
1147 	if (m == NULL)
1148 	{
1149 		syserr("554 buildaddr: unknown mailer %s", *tv);
1150 		return (NULL);
1151 	}
1152 	a->q_mailer = m;
1153 
1154 	/* figure out what host (if any) */
1155 	tv++;
1156 	if ((**tv & 0377) == CANONHOST)
1157 	{
1158 		bp = buf;
1159 		spaceleft = sizeof buf - 1;
1160 		while (*++tv != NULL && (**tv & 0377) != CANONUSER)
1161 		{
1162 			int i = strlen(*tv);
1163 
1164 			if (i > spaceleft)
1165 			{
1166 				/* out of space for this address */
1167 				if (spaceleft >= 0)
1168 					syserr("554 buildaddr: host too long (%.40s...)",
1169 						buf);
1170 				i = spaceleft;
1171 				spaceleft = 0;
1172 			}
1173 			if (i <= 0)
1174 				continue;
1175 			bcopy(*tv, bp, i);
1176 			bp += i;
1177 			spaceleft -= i;
1178 		}
1179 		*bp = '\0';
1180 		a->q_host = newstr(buf);
1181 	}
1182 	else
1183 	{
1184 		if (!bitnset(M_LOCALMAILER, m->m_flags))
1185 		{
1186 			syserr("554 buildaddr: no host");
1187 			return (NULL);
1188 		}
1189 		a->q_host = NULL;
1190 	}
1191 
1192 	/* figure out the user */
1193 	if (*tv == NULL || (**tv & 0377) != CANONUSER)
1194 	{
1195 		syserr("554 buildaddr: no user");
1196 		return (NULL);
1197 	}
1198 	tv++;
1199 
1200 	/* do special mapping for local mailer */
1201 	if (m == LocalMailer && *tv != NULL)
1202 	{
1203 		register char *p = *tv;
1204 
1205 		if (*p == '"')
1206 			p++;
1207 		if (*p == '|')
1208 			a->q_mailer = m = ProgMailer;
1209 		else if (*p == '/')
1210 			a->q_mailer = m = FileMailer;
1211 		else if (*p == ':')
1212 		{
1213 			/* may be :include: */
1214 			cataddr(tv, NULL, buf, sizeof buf, '\0');
1215 			stripquotes(buf);
1216 			if (strncasecmp(buf, ":include:", 9) == 0)
1217 			{
1218 				/* if :include:, don't need further rewriting */
1219 				a->q_mailer = m = InclMailer;
1220 				a->q_user = &buf[9];
1221 				return (a);
1222 			}
1223 		}
1224 	}
1225 
1226 	if (m == LocalMailer && *tv != NULL && strcmp(*tv, "@") == 0)
1227 	{
1228 		tv++;
1229 		a->q_flags |= QNOTREMOTE;
1230 	}
1231 
1232 	/* do cleanup of final address */
1233 	rewrite(tv, 4);
1234 
1235 	/* save the result for the command line/RCPT argument */
1236 	cataddr(tv, NULL, buf, sizeof buf, '\0');
1237 	a->q_user = buf;
1238 
1239 	/*
1240 	**  Do mapping to lower case as requested by mailer
1241 	*/
1242 
1243 	if (a->q_host != NULL && !bitnset(M_HST_UPPER, m->m_flags))
1244 		makelower(a->q_host);
1245 	if (!bitnset(M_USR_UPPER, m->m_flags))
1246 		makelower(a->q_user);
1247 
1248 	return (a);
1249 }
1250 /*
1251 **  CATADDR -- concatenate pieces of addresses (putting in <LWSP> subs)
1252 **
1253 **	Parameters:
1254 **		pvp -- parameter vector to rebuild.
1255 **		evp -- last parameter to include.  Can be NULL to
1256 **			use entire pvp.
1257 **		buf -- buffer to build the string into.
1258 **		sz -- size of buf.
1259 **		spacesub -- the space separator character; if null,
1260 **			use SpaceSub.
1261 **
1262 **	Returns:
1263 **		none.
1264 **
1265 **	Side Effects:
1266 **		Destroys buf.
1267 */
1268 
1269 cataddr(pvp, evp, buf, sz, spacesub)
1270 	char **pvp;
1271 	char **evp;
1272 	char *buf;
1273 	register int sz;
1274 	char spacesub;
1275 {
1276 	bool oatomtok = FALSE;
1277 	bool natomtok = FALSE;
1278 	register int i;
1279 	register char *p;
1280 
1281 	if (spacesub == '\0')
1282 		spacesub = SpaceSub;
1283 
1284 	if (pvp == NULL)
1285 	{
1286 		(void) strcpy(buf, "");
1287 		return;
1288 	}
1289 	p = buf;
1290 	sz -= 2;
1291 	while (*pvp != NULL && (i = strlen(*pvp)) < sz)
1292 	{
1293 		natomtok = (toktype(**pvp) == ATM);
1294 		if (oatomtok && natomtok)
1295 			*p++ = spacesub;
1296 		(void) strcpy(p, *pvp);
1297 		oatomtok = natomtok;
1298 		p += i;
1299 		sz -= i + 1;
1300 		if (pvp++ == evp)
1301 			break;
1302 	}
1303 	*p = '\0';
1304 }
1305 /*
1306 **  SAMEADDR -- Determine if two addresses are the same
1307 **
1308 **	This is not just a straight comparison -- if the mailer doesn't
1309 **	care about the host we just ignore it, etc.
1310 **
1311 **	Parameters:
1312 **		a, b -- pointers to the internal forms to compare.
1313 **
1314 **	Returns:
1315 **		TRUE -- they represent the same mailbox.
1316 **		FALSE -- they don't.
1317 **
1318 **	Side Effects:
1319 **		none.
1320 */
1321 
1322 bool
1323 sameaddr(a, b)
1324 	register ADDRESS *a;
1325 	register ADDRESS *b;
1326 {
1327 	/* if they don't have the same mailer, forget it */
1328 	if (a->q_mailer != b->q_mailer)
1329 		return (FALSE);
1330 
1331 	/* if the user isn't the same, we can drop out */
1332 	if (strcmp(a->q_user, b->q_user) != 0)
1333 		return (FALSE);
1334 
1335 	/* if we have good uids for both but the differ, these are different */
1336 	if (bitset(QGOODUID, a->q_flags & b->q_flags) && a->q_uid != b->q_uid)
1337 		return (FALSE);
1338 
1339 	/* otherwise compare hosts (but be careful for NULL ptrs) */
1340 	if (a->q_host == b->q_host)
1341 	{
1342 		/* probably both null pointers */
1343 		return (TRUE);
1344 	}
1345 	if (a->q_host == NULL || b->q_host == NULL)
1346 	{
1347 		/* only one is a null pointer */
1348 		return (FALSE);
1349 	}
1350 	if (strcmp(a->q_host, b->q_host) != 0)
1351 		return (FALSE);
1352 
1353 	return (TRUE);
1354 }
1355 /*
1356 **  PRINTADDR -- print address (for debugging)
1357 **
1358 **	Parameters:
1359 **		a -- the address to print
1360 **		follow -- follow the q_next chain.
1361 **
1362 **	Returns:
1363 **		none.
1364 **
1365 **	Side Effects:
1366 **		none.
1367 */
1368 
1369 printaddr(a, follow)
1370 	register ADDRESS *a;
1371 	bool follow;
1372 {
1373 	bool first = TRUE;
1374 	register MAILER *m;
1375 	MAILER pseudomailer;
1376 
1377 	while (a != NULL)
1378 	{
1379 		first = FALSE;
1380 		printf("%x=", a);
1381 		(void) fflush(stdout);
1382 
1383 		/* find the mailer -- carefully */
1384 		m = a->q_mailer;
1385 		if (m == NULL)
1386 		{
1387 			m = &pseudomailer;
1388 			m->m_mno = -1;
1389 			m->m_name = "NULL";
1390 		}
1391 
1392 		printf("%s:\n\tmailer %d (%s), host `%s', user `%s', ruser `%s'\n",
1393 		       a->q_paddr, m->m_mno, m->m_name,
1394 		       a->q_host, a->q_user, a->q_ruser? a->q_ruser: "<null>");
1395 		printf("\tnext=%x, flags=%o, alias %x\n", a->q_next, a->q_flags,
1396 		       a->q_alias);
1397 		printf("\thome=\"%s\", fullname=\"%s\"\n", a->q_home,
1398 		       a->q_fullname);
1399 
1400 		if (!follow)
1401 			return;
1402 		a = a->q_next;
1403 	}
1404 	if (first)
1405 		printf("[NULL]\n");
1406 }
1407 
1408 /*
1409 **  REMOTENAME -- return the name relative to the current mailer
1410 **
1411 **	Parameters:
1412 **		name -- the name to translate.
1413 **		m -- the mailer that we want to do rewriting relative
1414 **			to.
1415 **		senderaddress -- if set, uses the sender rewriting rules
1416 **			rather than the recipient rewriting rules.
1417 **		header -- set if this address is in the header, rather
1418 **			than an envelope header.
1419 **		canonical -- if set, strip out any comment information,
1420 **			etc.
1421 **		adddomain -- if set, OK to do domain extension.
1422 **		e -- the current envelope.
1423 **
1424 **	Returns:
1425 **		the text string representing this address relative to
1426 **			the receiving mailer.
1427 **
1428 **	Side Effects:
1429 **		none.
1430 **
1431 **	Warnings:
1432 **		The text string returned is tucked away locally;
1433 **			copy it if you intend to save it.
1434 */
1435 
1436 char *
1437 remotename(name, m, senderaddress, header, canonical, adddomain, e)
1438 	char *name;
1439 	struct mailer *m;
1440 	bool senderaddress;
1441 	bool header;
1442 	bool canonical;
1443 	bool adddomain;
1444 	register ENVELOPE *e;
1445 {
1446 	register char **pvp;
1447 	char *fancy;
1448 	extern char *macvalue();
1449 	char *oldg = macvalue('g', e);
1450 	int rwset;
1451 	static char buf[MAXNAME];
1452 	char lbuf[MAXNAME];
1453 	char pvpbuf[PSBUFSIZE];
1454 	extern char **prescan();
1455 	extern char *crackaddr();
1456 
1457 	if (tTd(12, 1))
1458 		printf("remotename(%s)\n", name);
1459 
1460 	/* don't do anything if we are tagging it as special */
1461 	if (senderaddress)
1462 		rwset = header ? m->m_sh_rwset : m->m_se_rwset;
1463 	else
1464 		rwset = header ? m->m_rh_rwset : m->m_re_rwset;
1465 	if (rwset < 0)
1466 		return (name);
1467 
1468 	/*
1469 	**  Do a heuristic crack of this name to extract any comment info.
1470 	**	This will leave the name as a comment and a $g macro.
1471 	*/
1472 
1473 	if (canonical || bitnset(M_NOCOMMENT, m->m_flags))
1474 		fancy = "\201g";
1475 	else
1476 		fancy = crackaddr(name);
1477 
1478 	/*
1479 	**  Turn the name into canonical form.
1480 	**	Normally this will be RFC 822 style, i.e., "user@domain".
1481 	**	If this only resolves to "user", and the "C" flag is
1482 	**	specified in the sending mailer, then the sender's
1483 	**	domain will be appended.
1484 	*/
1485 
1486 	pvp = prescan(name, '\0', pvpbuf, NULL);
1487 	if (pvp == NULL)
1488 		return (name);
1489 	rewrite(pvp, 3);
1490 	if (adddomain && e->e_fromdomain != NULL)
1491 	{
1492 		/* append from domain to this address */
1493 		register char **pxp = pvp;
1494 
1495 		/* see if there is an "@domain" in the current name */
1496 		while (*pxp != NULL && strcmp(*pxp, "@") != 0)
1497 			pxp++;
1498 		if (*pxp == NULL)
1499 		{
1500 			/* no.... append the "@domain" from the sender */
1501 			register char **qxq = e->e_fromdomain;
1502 
1503 			while ((*pxp++ = *qxq++) != NULL)
1504 				continue;
1505 			rewrite(pvp, 3);
1506 		}
1507 	}
1508 
1509 	/*
1510 	**  Do more specific rewriting.
1511 	**	Rewrite using ruleset 1 or 2 depending on whether this is
1512 	**		a sender address or not.
1513 	**	Then run it through any receiving-mailer-specific rulesets.
1514 	*/
1515 
1516 	if (senderaddress)
1517 		rewrite(pvp, 1);
1518 	else
1519 		rewrite(pvp, 2);
1520 	if (rwset > 0)
1521 		rewrite(pvp, rwset);
1522 
1523 	/*
1524 	**  Do any final sanitation the address may require.
1525 	**	This will normally be used to turn internal forms
1526 	**	(e.g., user@host.LOCAL) into external form.  This
1527 	**	may be used as a default to the above rules.
1528 	*/
1529 
1530 	rewrite(pvp, 4);
1531 
1532 	/*
1533 	**  Now restore the comment information we had at the beginning.
1534 	*/
1535 
1536 	cataddr(pvp, NULL, lbuf, sizeof lbuf, '\0');
1537 	define('g', lbuf, e);
1538 	expand(fancy, buf, &buf[sizeof buf - 1], e);
1539 	define('g', oldg, e);
1540 
1541 	if (tTd(12, 1))
1542 		printf("remotename => `%s'\n", buf);
1543 	return (buf);
1544 }
1545 /*
1546 **  MAPLOCALUSER -- run local username through ruleset 5 for final redirection
1547 **
1548 **	Parameters:
1549 **		a -- the address to map (but just the user name part).
1550 **		sendq -- the sendq in which to install any replacement
1551 **			addresses.
1552 **
1553 **	Returns:
1554 **		none.
1555 */
1556 
1557 maplocaluser(a, sendq, e)
1558 	register ADDRESS *a;
1559 	ADDRESS **sendq;
1560 	ENVELOPE *e;
1561 {
1562 	register char **pvp;
1563 	register ADDRESS *a1 = NULL;
1564 	auto char *delimptr;
1565 	char pvpbuf[PSBUFSIZE];
1566 
1567 	if (tTd(29, 1))
1568 	{
1569 		printf("maplocaluser: ");
1570 		printaddr(a, FALSE);
1571 	}
1572 	pvp = prescan(a->q_user, '\0', pvpbuf, &delimptr);
1573 	if (pvp == NULL)
1574 		return;
1575 
1576 	rewrite(pvp, 5);
1577 	if (pvp[0] == NULL || (pvp[0][0] & 0377) != CANONNET)
1578 		return;
1579 
1580 	/* if non-null, mailer destination specified -- has it changed? */
1581 	a1 = buildaddr(pvp, NULL, e);
1582 	if (a1 == NULL || sameaddr(a, a1))
1583 		return;
1584 
1585 	/* mark old address as dead; insert new address */
1586 	a->q_flags |= QDONTSEND;
1587 	if (tTd(29, 5))
1588 	{
1589 		printf("maplocaluser: QDONTSEND ");
1590 		printaddr(a, FALSE);
1591 	}
1592 	a1->q_alias = a;
1593 	allocaddr(a1, 1, NULL, delimptr);
1594 	(void) recipient(a1, sendq, e);
1595 }
1596 /*
1597 **  DEQUOTE_INIT -- initialize dequote map
1598 **
1599 **	This is a no-op.
1600 **
1601 **	Parameters:
1602 **		map -- the internal map structure.
1603 **		mapname -- the name of the mapl.
1604 **		args -- arguments.
1605 **
1606 **	Returns:
1607 **		TRUE.
1608 */
1609 
1610 bool
1611 dequote_init(map, mapname, args)
1612 	MAP *map;
1613 	char *mapname;
1614 	char *args;
1615 {
1616 	register char *p = args;
1617 
1618 	for (;;)
1619 	{
1620 		while (isascii(*p) && isspace(*p))
1621 			p++;
1622 		if (*p != '-')
1623 			break;
1624 		switch (*++p)
1625 		{
1626 		  case 'a':
1627 			map->map_app = ++p;
1628 			break;
1629 		}
1630 		while (*p != '\0' && !(isascii(*p) && isspace(*p)))
1631 			p++;
1632 		if (*p != '\0')
1633 			*p = '\0';
1634 	}
1635 	if (map->map_app != NULL)
1636 		map->map_app = newstr(map->map_app);
1637 
1638 	return TRUE;
1639 }
1640 /*
1641 **  DEQUOTE_MAP -- unquote an address
1642 **
1643 **	Parameters:
1644 **		map -- the internal map structure (ignored).
1645 **		buf -- the buffer to dequote.
1646 **		bufsiz -- the size of that buffer.
1647 **		av -- arguments (ignored).
1648 **
1649 **	Returns:
1650 **		NULL -- if there were no quotes, or if the resulting
1651 **			unquoted buffer would not be acceptable to prescan.
1652 **		else -- The dequoted buffer.
1653 */
1654 
1655 char *
1656 dequote_map(map, buf, bufsiz, av)
1657 	MAP *map;
1658 	char buf[];
1659 	int bufsiz;
1660 	char **av;
1661 {
1662 	register char *p;
1663 	register char *q;
1664 	register char c;
1665 	int anglecnt;
1666 	int cmntcnt;
1667 	int quotecnt;
1668 	bool quotemode;
1669 	bool bslashmode;
1670 
1671 	anglecnt = 0;
1672 	cmntcnt = 0;
1673 	quotecnt = 0;
1674 	quotemode = FALSE;
1675 	bslashmode = FALSE;
1676 
1677 	for (p = q = buf; (c = *p++) != '\0'; )
1678 	{
1679 		if (bslashmode)
1680 		{
1681 			bslashmode = FALSE;
1682 			*q++ = c;
1683 			continue;
1684 		}
1685 
1686 		switch (c)
1687 		{
1688 		  case '\\':
1689 			bslashmode = TRUE;
1690 			break;
1691 
1692 		  case '(':
1693 			cmntcnt++;
1694 			break;
1695 
1696 		  case ')':
1697 			if (cmntcnt-- <= 0)
1698 				return NULL;
1699 			break;
1700 		}
1701 
1702 		if (cmntcnt > 0)
1703 		{
1704 			*q++ = c;
1705 			continue;
1706 		}
1707 
1708 		switch (c)
1709 		{
1710 		  case '"':
1711 			quotemode = !quotemode;
1712 			quotecnt++;
1713 			continue;
1714 
1715 		  case '<':
1716 			anglecnt++;
1717 			break;
1718 
1719 		  case '>':
1720 			if (anglecnt-- <= 0)
1721 				return NULL;
1722 			break;
1723 		}
1724 		*q++ = c;
1725 	}
1726 
1727 	if (anglecnt != 0 || cmntcnt != 0 || bslashmode ||
1728 	    quotemode || quotecnt <= 0)
1729 		return NULL;
1730 	*q++ = '\0';
1731 	return buf;
1732 }
1733