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.33 (Berkeley) 03/26/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 	struct match mlist[MAXMATCH];	/* stores match on LHS */
578 	char *npvp[MAXATOM+1];		/* temporary space for rebuild */
579 
580 	if (OpMode == MD_TEST || tTd(21, 2))
581 	{
582 		printf("rewrite: ruleset %2d   input:", ruleset);
583 		printav(pvp);
584 	}
585 	if (ruleset < 0 || ruleset >= MAXRWSETS)
586 	{
587 		syserr("554 rewrite: illegal ruleset number %d", ruleset);
588 		return;
589 	}
590 	if (pvp == NULL)
591 		return;
592 
593 	/*
594 	**  Run through the list of rewrite rules, applying
595 	**	any that match.
596 	*/
597 
598 	for (rwr = RewriteRules[ruleset]; rwr != NULL; )
599 	{
600 		int loopcount = 0;
601 
602 		if (tTd(21, 12))
603 		{
604 			printf("-----trying rule:");
605 			printav(rwr->r_lhs);
606 		}
607 
608 		/* try to match on this rule */
609 		mlp = mlist;
610 		rvp = rwr->r_lhs;
611 		avp = pvp;
612 		while ((ap = *avp) != NULL || *rvp != NULL)
613 		{
614 			if (++loopcount > 100)
615 			{
616 				syserr("554 Infinite loop in ruleset %d", ruleset);
617 				printf("workspace: ");
618 				printav(pvp);
619 				break;
620 			}
621 			rp = *rvp;
622 			if (tTd(21, 35))
623 			{
624 				printf("ADVANCE rp=");
625 				xputs(rp);
626 				printf(", ap=");
627 				xputs(ap);
628 				printf("\n");
629 			}
630 			if (rp == NULL)
631 			{
632 				/* end-of-pattern before end-of-address */
633 				goto backup;
634 			}
635 			if (ap == NULL && (*rp & 0377) != MATCHZANY &&
636 			    (*rp & 0377) != CANONHOST)
637 			{
638 				/* end-of-input with patterns left */
639 				goto backup;
640 			}
641 
642 			switch (*rp & 0377)
643 			{
644 				register STAB *s;
645 				char buf[MAXLINE];
646 
647 			  case MATCHCLASS:
648 				/* match any phrase in a class */
649 				mlp->pattern = rvp;
650 				mlp->first = avp;
651 	extendclass:
652 				ap = *avp;
653 				if (ap == NULL)
654 					goto backup;
655 				mlp->last = avp++;
656 				cataddr(mlp->first, mlp->last, buf, sizeof buf, '\0');
657 				s = stab(buf, ST_CLASS, ST_FIND);
658 				if (s == NULL || !bitnset(rp[1], s->s_class))
659 				{
660 					if (tTd(21, 36))
661 					{
662 						printf("EXTEND  rp=");
663 						xputs(rp);
664 						printf(", ap=");
665 						xputs(ap);
666 						printf("\n");
667 					}
668 					goto extendclass;
669 				}
670 				if (tTd(21, 36))
671 					printf("CLMATCH\n");
672 				mlp++;
673 				break;
674 
675 			  case MATCHNCLASS:
676 				/* match any token not in a class */
677 				s = stab(ap, ST_CLASS, ST_FIND);
678 				if (s != NULL && bitnset(rp[1], s->s_class))
679 					goto backup;
680 
681 				/* fall through */
682 
683 			  case MATCHONE:
684 			  case MATCHANY:
685 				/* match exactly one token */
686 				mlp->pattern = rvp;
687 				mlp->first = avp;
688 				mlp->last = avp++;
689 				mlp++;
690 				break;
691 
692 			  case MATCHZANY:
693 				/* match zero or more tokens */
694 				mlp->pattern = rvp;
695 				mlp->first = avp;
696 				mlp->last = avp - 1;
697 				mlp++;
698 				break;
699 
700 			  case CANONHOST:
701 				/* match zero tokens */
702 				break;
703 
704 			  default:
705 				/* must have exact match */
706 				if (strcasecmp(rp, ap))
707 					goto backup;
708 				avp++;
709 				break;
710 			}
711 
712 			/* successful match on this token */
713 			rvp++;
714 			continue;
715 
716 	  backup:
717 			/* match failed -- back up */
718 			while (--mlp >= mlist)
719 			{
720 				rvp = mlp->pattern;
721 				rp = *rvp;
722 				avp = mlp->last + 1;
723 				ap = *avp;
724 
725 				if (tTd(21, 36))
726 				{
727 					printf("BACKUP  rp=");
728 					xputs(rp);
729 					printf(", ap=");
730 					xputs(ap);
731 					printf("\n");
732 				}
733 
734 				if (ap == NULL)
735 				{
736 					/* run off the end -- back up again */
737 					continue;
738 				}
739 				if ((*rp & 0377) == MATCHANY ||
740 				    (*rp & 0377) == MATCHZANY)
741 				{
742 					/* extend binding and continue */
743 					mlp->last = avp++;
744 					rvp++;
745 					mlp++;
746 					break;
747 				}
748 				if ((*rp & 0377) == MATCHCLASS)
749 				{
750 					/* extend binding and try again */
751 					mlp->last = avp++;
752 					goto extendclass;
753 				}
754 			}
755 
756 			if (mlp < mlist)
757 			{
758 				/* total failure to match */
759 				break;
760 			}
761 		}
762 
763 		/*
764 		**  See if we successfully matched
765 		*/
766 
767 		if (rvp < rwr->r_lhs || *rvp != NULL)
768 		{
769 			if (tTd(21, 10))
770 				printf("----- rule fails\n");
771 			rwr = rwr->r_next;
772 			continue;
773 		}
774 
775 		rvp = rwr->r_rhs;
776 		if (tTd(21, 12))
777 		{
778 			printf("-----rule matches:");
779 			printav(rvp);
780 		}
781 
782 		rp = *rvp;
783 		if ((*rp & 0377) == CANONUSER)
784 		{
785 			rvp++;
786 			rwr = rwr->r_next;
787 		}
788 		else if ((*rp & 0377) == CANONHOST)
789 		{
790 			rvp++;
791 			rwr = NULL;
792 		}
793 		else if ((*rp & 0377) == CANONNET)
794 			rwr = NULL;
795 
796 		/* substitute */
797 		for (avp = npvp; *rvp != NULL; rvp++)
798 		{
799 			register struct match *m;
800 			register char **pp;
801 
802 			rp = *rvp;
803 			if ((*rp & 0377) == MATCHREPL)
804 			{
805 				/* substitute from LHS */
806 				m = &mlist[rp[1] - '1'];
807 				if (m < mlist || m >= mlp)
808 				{
809 					syserr("554 rewrite: ruleset %d: replacement $%c out of bounds",
810 						ruleset, rp[1]);
811 					return;
812 				}
813 				if (tTd(21, 15))
814 				{
815 					printf("$%c:", rp[1]);
816 					pp = m->first;
817 					while (pp <= m->last)
818 					{
819 						printf(" %x=\"", *pp);
820 						(void) fflush(stdout);
821 						printf("%s\"", *pp++);
822 					}
823 					printf("\n");
824 				}
825 				pp = m->first;
826 				while (pp <= m->last)
827 				{
828 					if (avp >= &npvp[MAXATOM])
829 					{
830 						syserr("554 rewrite: expansion too long");
831 						return;
832 					}
833 					*avp++ = *pp++;
834 				}
835 			}
836 			else
837 			{
838 				/* vanilla replacement */
839 				if (avp >= &npvp[MAXATOM])
840 				{
841 	toolong:
842 					syserr("554 rewrite: expansion too long");
843 					return;
844 				}
845 				*avp++ = rp;
846 			}
847 		}
848 		*avp++ = NULL;
849 
850 		/*
851 		**  Check for any hostname/keyword lookups.
852 		*/
853 
854 		for (rvp = npvp; *rvp != NULL; rvp++)
855 		{
856 			char **hbrvp;
857 			char **xpvp;
858 			int trsize;
859 			char *olddelimchar;
860 			char *replac;
861 			int endtoken;
862 			STAB *map;
863 			char *mapname;
864 			char **key_rvp;
865 			char **arg_rvp;
866 			char **default_rvp;
867 			char buf[MAXNAME + 1];
868 			char *pvpb1[MAXATOM + 1];
869 			char *argvect[10];
870 			char pvpbuf[PSBUFSIZE];
871 
872 			if ((**rvp & 0377) != HOSTBEGIN &&
873 			    (**rvp & 0377) != LOOKUPBEGIN)
874 				continue;
875 
876 			/*
877 			**  Got a hostname/keyword lookup.
878 			**
879 			**	This could be optimized fairly easily.
880 			*/
881 
882 			hbrvp = rvp;
883 			if ((**rvp & 0377) == HOSTBEGIN)
884 			{
885 				endtoken = HOSTEND;
886 				mapname = "host";
887 			}
888 			else
889 			{
890 				endtoken = LOOKUPEND;
891 				mapname = *++rvp;
892 			}
893 			map = stab(mapname, ST_MAP, ST_FIND);
894 			if (map == NULL)
895 				syserr("554 rewrite: map %s not found", mapname);
896 
897 			/* extract the match part */
898 			key_rvp = ++rvp;
899 			default_rvp = NULL;
900 			arg_rvp = argvect;
901 			xpvp = NULL;
902 			replac = pvpbuf;
903 			while (*rvp != NULL && (**rvp & 0377) != endtoken)
904 			{
905 				int nodetype = **rvp & 0377;
906 
907 				if (nodetype != CANONHOST && nodetype != CANONUSER)
908 				{
909 					rvp++;
910 					continue;
911 				}
912 
913 				*rvp++ = NULL;
914 
915 				if (xpvp != NULL)
916 				{
917 					cataddr(xpvp, NULL, replac,
918 						&pvpbuf[sizeof pvpbuf] - replac,
919 						'\0');
920 					*++arg_rvp = replac;
921 					replac += strlen(replac) + 1;
922 					xpvp = NULL;
923 				}
924 				switch (nodetype)
925 				{
926 				  case CANONHOST:
927 					xpvp = rvp;
928 					break;
929 
930 				  case CANONUSER:
931 					default_rvp = rvp;
932 					break;
933 				}
934 			}
935 			if (*rvp != NULL)
936 				*rvp++ = NULL;
937 			if (xpvp != NULL)
938 			{
939 				cataddr(xpvp, NULL, replac,
940 					&pvpbuf[sizeof pvpbuf] - replac,
941 					'\0');
942 				*++arg_rvp = replac;
943 			}
944 			*++arg_rvp = NULL;
945 
946 			/* save the remainder of the input string */
947 			trsize = (int) (avp - rvp + 1) * sizeof *rvp;
948 			bcopy((char *) rvp, (char *) pvpb1, trsize);
949 
950 			/* look it up */
951 			cataddr(key_rvp, NULL, buf, sizeof buf, '\0');
952 			argvect[0] = buf;
953 			if (map != NULL && bitset(MF_VALID, map->s_map.map_flags))
954 			{
955 				int bsize = sizeof buf - 1;
956 
957 				if (map->s_map.map_app != NULL)
958 					bsize -= strlen(map->s_map.map_app);
959 				if (tTd(60, 1))
960 					printf("map_lookup(%s, %s) => ",
961 						mapname, buf);
962 				replac = (*map->s_map.map_class->map_lookup)(&map->s_map,
963 						buf, sizeof buf - 1, argvect);
964 				if (replac != NULL && map->s_map.map_app != NULL)
965 					strcat(replac, map->s_map.map_app);
966 				if (tTd(60, 1))
967 					printf("%s\n", replac ? replac : "NOT FOUND");
968 			}
969 			else
970 				replac = NULL;
971 
972 			/* if no replacement, use default */
973 			if (replac == NULL && default_rvp != NULL)
974 			{
975 				char buf2[sizeof buf];
976 
977 				/* rewrite the default with % translations */
978 				cataddr(default_rvp, NULL, buf2, sizeof buf2, '\0');
979 				map_rewrite(buf2, sizeof buf2, buf, sizeof buf,
980 					argvect);
981 				replac = buf;
982 			}
983 
984 			if (replac == NULL)
985 			{
986 				xpvp = key_rvp;
987 			}
988 			else
989 			{
990 				/* scan the new replacement */
991 				xpvp = prescan(replac, '\0', pvpbuf, NULL);
992 				if (xpvp == NULL)
993 				{
994 					/* prescan already printed error */
995 					return;
996 				}
997 			}
998 
999 			/* append it to the token list */
1000 			for (avp = hbrvp; *xpvp != NULL; xpvp++)
1001 			{
1002 				*avp++ = newstr(*xpvp);
1003 				if (avp >= &npvp[MAXATOM])
1004 					goto toolong;
1005 			}
1006 
1007 			/* restore the old trailing information */
1008 			for (xpvp = pvpb1; (*avp++ = *xpvp++) != NULL; )
1009 				if (avp >= &npvp[MAXATOM])
1010 					goto toolong;
1011 
1012 			break;
1013 		}
1014 
1015 		/*
1016 		**  Check for subroutine calls.
1017 		*/
1018 
1019 		if (*npvp != NULL && (**npvp & 0377) == CALLSUBR)
1020 		{
1021 			bcopy((char *) &npvp[2], (char *) pvp,
1022 				(int) (avp - npvp - 2) * sizeof *avp);
1023 			if (tTd(21, 3))
1024 				printf("-----callsubr %s\n", npvp[1]);
1025 			rewrite(pvp, atoi(npvp[1]));
1026 		}
1027 		else
1028 		{
1029 			bcopy((char *) npvp, (char *) pvp,
1030 				(int) (avp - npvp) * sizeof *avp);
1031 		}
1032 		if (tTd(21, 4))
1033 		{
1034 			printf("rewritten as:");
1035 			printav(pvp);
1036 		}
1037 	}
1038 
1039 	if (OpMode == MD_TEST || tTd(21, 2))
1040 	{
1041 		printf("rewrite: ruleset %2d returns:", ruleset);
1042 		printav(pvp);
1043 	}
1044 }
1045 /*
1046 **  BUILDADDR -- build address from token vector.
1047 **
1048 **	Parameters:
1049 **		tv -- token vector.
1050 **		a -- pointer to address descriptor to fill.
1051 **			If NULL, one will be allocated.
1052 **
1053 **	Returns:
1054 **		NULL if there was an error.
1055 **		'a' otherwise.
1056 **
1057 **	Side Effects:
1058 **		fills in 'a'
1059 */
1060 
1061 struct errcodes
1062 {
1063 	char	*ec_name;		/* name of error code */
1064 	int	ec_code;		/* numeric code */
1065 } ErrorCodes[] =
1066 {
1067 	"usage",	EX_USAGE,
1068 	"nouser",	EX_NOUSER,
1069 	"nohost",	EX_NOHOST,
1070 	"unavailable",	EX_UNAVAILABLE,
1071 	"software",	EX_SOFTWARE,
1072 	"tempfail",	EX_TEMPFAIL,
1073 	"protocol",	EX_PROTOCOL,
1074 #ifdef EX_CONFIG
1075 	"config",	EX_CONFIG,
1076 #endif
1077 	NULL,		EX_UNAVAILABLE,
1078 };
1079 
1080 ADDRESS *
1081 buildaddr(tv, a)
1082 	register char **tv;
1083 	register ADDRESS *a;
1084 {
1085 	struct mailer **mp;
1086 	register struct mailer *m;
1087 	char *bp;
1088 	int spaceleft;
1089 	static char buf[MAXNAME];
1090 
1091 	if (a == NULL)
1092 		a = (ADDRESS *) xalloc(sizeof *a);
1093 	bzero((char *) a, sizeof *a);
1094 
1095 	/* figure out what net/mailer to use */
1096 	if ((**tv & 0377) != CANONNET)
1097 	{
1098 		syserr("554 buildaddr: no net");
1099 		return (NULL);
1100 	}
1101 	tv++;
1102 	if (strcasecmp(*tv, "error") == 0)
1103 	{
1104 		if ((**++tv & 0377) == CANONHOST)
1105 		{
1106 			register struct errcodes *ep;
1107 
1108 			if (isascii(**++tv) && isdigit(**tv))
1109 			{
1110 				setstat(atoi(*tv));
1111 			}
1112 			else
1113 			{
1114 				for (ep = ErrorCodes; ep->ec_name != NULL; ep++)
1115 					if (strcasecmp(ep->ec_name, *tv) == 0)
1116 						break;
1117 				setstat(ep->ec_code);
1118 			}
1119 			tv++;
1120 		}
1121 		if ((**tv & 0377) != CANONUSER)
1122 			syserr("554 buildaddr: error: no user");
1123 		cataddr(++tv, NULL, buf, sizeof buf, ' ');
1124 		stripquotes(buf);
1125 		usrerr(buf);
1126 		return (NULL);
1127 	}
1128 
1129 	for (mp = Mailer; (m = *mp++) != NULL; )
1130 	{
1131 		if (strcasecmp(m->m_name, *tv) == 0)
1132 			break;
1133 	}
1134 	if (m == NULL)
1135 	{
1136 		syserr("554 buildaddr: unknown mailer %s", *tv);
1137 		return (NULL);
1138 	}
1139 	a->q_mailer = m;
1140 
1141 	/* figure out what host (if any) */
1142 	tv++;
1143 	if ((**tv & 0377) == CANONHOST)
1144 	{
1145 		bp = buf;
1146 		spaceleft = sizeof buf - 1;
1147 		while (*++tv != NULL && (**tv & 0377) != CANONUSER)
1148 		{
1149 			int i = strlen(*tv);
1150 
1151 			if (i > spaceleft)
1152 			{
1153 				/* out of space for this address */
1154 				if (spaceleft >= 0)
1155 					syserr("554 buildaddr: host too long (%.40s...)",
1156 						buf);
1157 				i = spaceleft;
1158 				spaceleft = 0;
1159 			}
1160 			if (i <= 0)
1161 				continue;
1162 			bcopy(*tv, bp, i);
1163 			bp += i;
1164 			spaceleft -= i;
1165 		}
1166 		*bp = '\0';
1167 		a->q_host = newstr(buf);
1168 	}
1169 	else
1170 	{
1171 		if (!bitnset(M_LOCALMAILER, m->m_flags))
1172 		{
1173 			syserr("554 buildaddr: no host");
1174 			return (NULL);
1175 		}
1176 		a->q_host = NULL;
1177 	}
1178 
1179 	/* figure out the user */
1180 	if (*tv == NULL || (**tv & 0377) != CANONUSER)
1181 	{
1182 		syserr("554 buildaddr: no user");
1183 		return (NULL);
1184 	}
1185 	tv++;
1186 
1187 	/* do special mapping for local mailer */
1188 	if (m == LocalMailer && *tv != NULL)
1189 	{
1190 		register char *p = *tv;
1191 
1192 		if (*p == '"')
1193 			p++;
1194 		if (*p == '|')
1195 			a->q_mailer = m = ProgMailer;
1196 		else if (*p == '/')
1197 			a->q_mailer = m = FileMailer;
1198 		else if (*p == ':')
1199 		{
1200 			/* may be :include: */
1201 			cataddr(tv, NULL, buf, sizeof buf, '\0');
1202 			stripquotes(buf);
1203 			if (strncasecmp(buf, ":include:", 9) == 0)
1204 			{
1205 				/* if :include:, don't need further rewriting */
1206 				a->q_mailer = m = InclMailer;
1207 				a->q_user = &buf[9];
1208 				return (a);
1209 			}
1210 		}
1211 	}
1212 
1213 	if (m == LocalMailer && *tv != NULL && strcmp(*tv, "@") == 0)
1214 	{
1215 		tv++;
1216 		a->q_flags |= QNOTREMOTE;
1217 	}
1218 
1219 	/* do cleanup of final address */
1220 	rewrite(tv, 4);
1221 
1222 	/* save the result for the command line/RCPT argument */
1223 	cataddr(tv, NULL, buf, sizeof buf, '\0');
1224 	a->q_user = buf;
1225 
1226 	/*
1227 	**  Do mapping to lower case as requested by mailer
1228 	*/
1229 
1230 	if (a->q_host != NULL && !bitnset(M_HST_UPPER, m->m_flags))
1231 		makelower(a->q_host);
1232 	if (!bitnset(M_USR_UPPER, m->m_flags))
1233 		makelower(a->q_user);
1234 
1235 	return (a);
1236 }
1237 /*
1238 **  CATADDR -- concatenate pieces of addresses (putting in <LWSP> subs)
1239 **
1240 **	Parameters:
1241 **		pvp -- parameter vector to rebuild.
1242 **		evp -- last parameter to include.  Can be NULL to
1243 **			use entire pvp.
1244 **		buf -- buffer to build the string into.
1245 **		sz -- size of buf.
1246 **		spacesub -- the space separator character; if null,
1247 **			use SpaceSub.
1248 **
1249 **	Returns:
1250 **		none.
1251 **
1252 **	Side Effects:
1253 **		Destroys buf.
1254 */
1255 
1256 cataddr(pvp, evp, buf, sz, spacesub)
1257 	char **pvp;
1258 	char **evp;
1259 	char *buf;
1260 	register int sz;
1261 	char spacesub;
1262 {
1263 	bool oatomtok = FALSE;
1264 	bool natomtok = FALSE;
1265 	register int i;
1266 	register char *p;
1267 
1268 	if (spacesub == '\0')
1269 		spacesub = SpaceSub;
1270 
1271 	if (pvp == NULL)
1272 	{
1273 		(void) strcpy(buf, "");
1274 		return;
1275 	}
1276 	p = buf;
1277 	sz -= 2;
1278 	while (*pvp != NULL && (i = strlen(*pvp)) < sz)
1279 	{
1280 		natomtok = (toktype(**pvp) == ATM);
1281 		if (oatomtok && natomtok)
1282 			*p++ = spacesub;
1283 		(void) strcpy(p, *pvp);
1284 		oatomtok = natomtok;
1285 		p += i;
1286 		sz -= i + 1;
1287 		if (pvp++ == evp)
1288 			break;
1289 	}
1290 	*p = '\0';
1291 }
1292 /*
1293 **  SAMEADDR -- Determine if two addresses are the same
1294 **
1295 **	This is not just a straight comparison -- if the mailer doesn't
1296 **	care about the host we just ignore it, etc.
1297 **
1298 **	Parameters:
1299 **		a, b -- pointers to the internal forms to compare.
1300 **
1301 **	Returns:
1302 **		TRUE -- they represent the same mailbox.
1303 **		FALSE -- they don't.
1304 **
1305 **	Side Effects:
1306 **		none.
1307 */
1308 
1309 bool
1310 sameaddr(a, b)
1311 	register ADDRESS *a;
1312 	register ADDRESS *b;
1313 {
1314 	/* if they don't have the same mailer, forget it */
1315 	if (a->q_mailer != b->q_mailer)
1316 		return (FALSE);
1317 
1318 	/* if the user isn't the same, we can drop out */
1319 	if (strcmp(a->q_user, b->q_user) != 0)
1320 		return (FALSE);
1321 
1322 	/* if we have good uids for both but the differ, these are different */
1323 	if (bitset(QGOODUID, a->q_flags & b->q_flags) && a->q_uid != b->q_uid)
1324 		return (FALSE);
1325 
1326 	/* otherwise compare hosts (but be careful for NULL ptrs) */
1327 	if (a->q_host == b->q_host)
1328 	{
1329 		/* probably both null pointers */
1330 		return (TRUE);
1331 	}
1332 	if (a->q_host == NULL || b->q_host == NULL)
1333 	{
1334 		/* only one is a null pointer */
1335 		return (FALSE);
1336 	}
1337 	if (strcmp(a->q_host, b->q_host) != 0)
1338 		return (FALSE);
1339 
1340 	return (TRUE);
1341 }
1342 /*
1343 **  PRINTADDR -- print address (for debugging)
1344 **
1345 **	Parameters:
1346 **		a -- the address to print
1347 **		follow -- follow the q_next chain.
1348 **
1349 **	Returns:
1350 **		none.
1351 **
1352 **	Side Effects:
1353 **		none.
1354 */
1355 
1356 printaddr(a, follow)
1357 	register ADDRESS *a;
1358 	bool follow;
1359 {
1360 	bool first = TRUE;
1361 	register MAILER *m;
1362 	MAILER pseudomailer;
1363 
1364 	while (a != NULL)
1365 	{
1366 		first = FALSE;
1367 		printf("%x=", a);
1368 		(void) fflush(stdout);
1369 
1370 		/* find the mailer -- carefully */
1371 		m = a->q_mailer;
1372 		if (m == NULL)
1373 		{
1374 			m = &pseudomailer;
1375 			m->m_mno = -1;
1376 			m->m_name = "NULL";
1377 		}
1378 
1379 		printf("%s:\n\tmailer %d (%s), host `%s', user `%s', ruser `%s'\n",
1380 		       a->q_paddr, m->m_mno, m->m_name,
1381 		       a->q_host, a->q_user, a->q_ruser? a->q_ruser: "<null>");
1382 		printf("\tnext=%x, flags=%o, alias %x\n", a->q_next, a->q_flags,
1383 		       a->q_alias);
1384 		printf("\thome=\"%s\", fullname=\"%s\"\n", a->q_home,
1385 		       a->q_fullname);
1386 
1387 		if (!follow)
1388 			return;
1389 		a = a->q_next;
1390 	}
1391 	if (first)
1392 		printf("[NULL]\n");
1393 }
1394 
1395 /*
1396 **  REMOTENAME -- return the name relative to the current mailer
1397 **
1398 **	Parameters:
1399 **		name -- the name to translate.
1400 **		m -- the mailer that we want to do rewriting relative
1401 **			to.
1402 **		senderaddress -- if set, uses the sender rewriting rules
1403 **			rather than the recipient rewriting rules.
1404 **		header -- set if this address is in the header, rather
1405 **			than an envelope header.
1406 **		canonical -- if set, strip out any comment information,
1407 **			etc.
1408 **		adddomain -- if set, OK to do domain extension.
1409 **		e -- the current envelope.
1410 **
1411 **	Returns:
1412 **		the text string representing this address relative to
1413 **			the receiving mailer.
1414 **
1415 **	Side Effects:
1416 **		none.
1417 **
1418 **	Warnings:
1419 **		The text string returned is tucked away locally;
1420 **			copy it if you intend to save it.
1421 */
1422 
1423 char *
1424 remotename(name, m, senderaddress, header, canonical, adddomain, e)
1425 	char *name;
1426 	struct mailer *m;
1427 	bool senderaddress;
1428 	bool header;
1429 	bool canonical;
1430 	bool adddomain;
1431 	register ENVELOPE *e;
1432 {
1433 	register char **pvp;
1434 	char *fancy;
1435 	extern char *macvalue();
1436 	char *oldg = macvalue('g', e);
1437 	int rwset;
1438 	static char buf[MAXNAME];
1439 	char lbuf[MAXNAME];
1440 	char pvpbuf[PSBUFSIZE];
1441 	extern char **prescan();
1442 	extern char *crackaddr();
1443 
1444 	if (tTd(12, 1))
1445 		printf("remotename(%s)\n", name);
1446 
1447 	/* don't do anything if we are tagging it as special */
1448 	if (senderaddress)
1449 		rwset = header ? m->m_sh_rwset : m->m_se_rwset;
1450 	else
1451 		rwset = header ? m->m_rh_rwset : m->m_re_rwset;
1452 	if (rwset < 0)
1453 		return (name);
1454 
1455 	/*
1456 	**  Do a heuristic crack of this name to extract any comment info.
1457 	**	This will leave the name as a comment and a $g macro.
1458 	*/
1459 
1460 	if (canonical || bitnset(M_NOCOMMENT, m->m_flags))
1461 		fancy = "\201g";
1462 	else
1463 		fancy = crackaddr(name);
1464 
1465 	/*
1466 	**  Turn the name into canonical form.
1467 	**	Normally this will be RFC 822 style, i.e., "user@domain".
1468 	**	If this only resolves to "user", and the "C" flag is
1469 	**	specified in the sending mailer, then the sender's
1470 	**	domain will be appended.
1471 	*/
1472 
1473 	pvp = prescan(name, '\0', pvpbuf, NULL);
1474 	if (pvp == NULL)
1475 		return (name);
1476 	rewrite(pvp, 3);
1477 	if (adddomain && e->e_fromdomain != NULL)
1478 	{
1479 		/* append from domain to this address */
1480 		register char **pxp = pvp;
1481 
1482 		/* see if there is an "@domain" in the current name */
1483 		while (*pxp != NULL && strcmp(*pxp, "@") != 0)
1484 			pxp++;
1485 		if (*pxp == NULL)
1486 		{
1487 			/* no.... append the "@domain" from the sender */
1488 			register char **qxq = e->e_fromdomain;
1489 
1490 			while ((*pxp++ = *qxq++) != NULL)
1491 				continue;
1492 			rewrite(pvp, 3);
1493 		}
1494 	}
1495 
1496 	/*
1497 	**  Do more specific rewriting.
1498 	**	Rewrite using ruleset 1 or 2 depending on whether this is
1499 	**		a sender address or not.
1500 	**	Then run it through any receiving-mailer-specific rulesets.
1501 	*/
1502 
1503 	if (senderaddress)
1504 		rewrite(pvp, 1);
1505 	else
1506 		rewrite(pvp, 2);
1507 	if (rwset > 0)
1508 		rewrite(pvp, rwset);
1509 
1510 	/*
1511 	**  Do any final sanitation the address may require.
1512 	**	This will normally be used to turn internal forms
1513 	**	(e.g., user@host.LOCAL) into external form.  This
1514 	**	may be used as a default to the above rules.
1515 	*/
1516 
1517 	rewrite(pvp, 4);
1518 
1519 	/*
1520 	**  Now restore the comment information we had at the beginning.
1521 	*/
1522 
1523 	cataddr(pvp, NULL, lbuf, sizeof lbuf, '\0');
1524 	define('g', lbuf, e);
1525 	expand(fancy, buf, &buf[sizeof buf - 1], e);
1526 	define('g', oldg, e);
1527 
1528 	if (tTd(12, 1))
1529 		printf("remotename => `%s'\n", buf);
1530 	return (buf);
1531 }
1532 /*
1533 **  MAPLOCALUSER -- run local username through ruleset 5 for final redirection
1534 **
1535 **	Parameters:
1536 **		a -- the address to map (but just the user name part).
1537 **		sendq -- the sendq in which to install any replacement
1538 **			addresses.
1539 **
1540 **	Returns:
1541 **		none.
1542 */
1543 
1544 maplocaluser(a, sendq, e)
1545 	register ADDRESS *a;
1546 	ADDRESS **sendq;
1547 	ENVELOPE *e;
1548 {
1549 	register char **pvp;
1550 	register ADDRESS *a1 = NULL;
1551 	auto char *delimptr;
1552 	char pvpbuf[PSBUFSIZE];
1553 
1554 	if (tTd(29, 1))
1555 	{
1556 		printf("maplocaluser: ");
1557 		printaddr(a, FALSE);
1558 	}
1559 	pvp = prescan(a->q_user, '\0', pvpbuf, &delimptr);
1560 	if (pvp == NULL)
1561 		return;
1562 
1563 	rewrite(pvp, 5);
1564 	if (pvp[0] == NULL || (pvp[0][0] & 0377) != CANONNET)
1565 		return;
1566 
1567 	/* if non-null, mailer destination specified -- has it changed? */
1568 	a1 = buildaddr(pvp, NULL);
1569 	if (a1 == NULL || sameaddr(a, a1))
1570 		return;
1571 
1572 	/* mark old address as dead; insert new address */
1573 	a->q_flags |= QDONTSEND;
1574 	if (tTd(29, 5))
1575 	{
1576 		printf("maplocaluser: QDONTSEND ");
1577 		printaddr(a, FALSE);
1578 	}
1579 	a1->q_alias = a;
1580 	allocaddr(a1, 1, NULL, delimptr);
1581 	(void) recipient(a1, sendq, e);
1582 }
1583 /*
1584 **  DEQUOTE_INIT -- initialize dequote map
1585 **
1586 **	This is a no-op.
1587 **
1588 **	Parameters:
1589 **		map -- the internal map structure.
1590 **		mapname -- the name of the mapl.
1591 **		args -- arguments.
1592 **
1593 **	Returns:
1594 **		TRUE.
1595 */
1596 
1597 bool
1598 dequote_init(map, mapname, args)
1599 	MAP *map;
1600 	char *mapname;
1601 	char *args;
1602 {
1603 	register char *p = args;
1604 
1605 	for (;;)
1606 	{
1607 		while (isascii(*p) && isspace(*p))
1608 			p++;
1609 		if (*p != '-')
1610 			break;
1611 		switch (*++p)
1612 		{
1613 		  case 'a':
1614 			map->map_app = ++p;
1615 			break;
1616 		}
1617 		while (*p != '\0' && !(isascii(*p) && isspace(*p)))
1618 			p++;
1619 		if (*p != '\0')
1620 			*p = '\0';
1621 	}
1622 	if (map->map_app != NULL)
1623 		map->map_app = newstr(map->map_app);
1624 
1625 	return TRUE;
1626 }
1627 /*
1628 **  DEQUOTE_MAP -- unquote an address
1629 **
1630 **	Parameters:
1631 **		map -- the internal map structure (ignored).
1632 **		buf -- the buffer to dequote.
1633 **		bufsiz -- the size of that buffer.
1634 **		av -- arguments (ignored).
1635 **
1636 **	Returns:
1637 **		NULL -- if there were no quotes, or if the resulting
1638 **			unquoted buffer would not be acceptable to prescan.
1639 **		else -- The dequoted buffer.
1640 */
1641 
1642 char *
1643 dequote_map(map, buf, bufsiz, av)
1644 	MAP *map;
1645 	char buf[];
1646 	int bufsiz;
1647 	char **av;
1648 {
1649 	register char *p;
1650 	register char *q;
1651 	register char c;
1652 	int anglecnt;
1653 	int cmntcnt;
1654 	int quotecnt;
1655 	bool quotemode;
1656 	bool bslashmode;
1657 
1658 	anglecnt = 0;
1659 	cmntcnt = 0;
1660 	quotecnt = 0;
1661 	quotemode = FALSE;
1662 	bslashmode = FALSE;
1663 
1664 	for (p = q = buf; (c = *p++) != '\0'; )
1665 	{
1666 		if (bslashmode)
1667 		{
1668 			bslashmode = FALSE;
1669 			*q++ = c;
1670 			continue;
1671 		}
1672 
1673 		switch (c)
1674 		{
1675 		  case '\\':
1676 			bslashmode = TRUE;
1677 			break;
1678 
1679 		  case '(':
1680 			cmntcnt++;
1681 			break;
1682 
1683 		  case ')':
1684 			if (cmntcnt-- <= 0)
1685 				return NULL;
1686 			break;
1687 		}
1688 
1689 		if (cmntcnt > 0)
1690 		{
1691 			*q++ = c;
1692 			continue;
1693 		}
1694 
1695 		switch (c)
1696 		{
1697 		  case '"':
1698 			quotemode = !quotemode;
1699 			quotecnt++;
1700 			continue;
1701 
1702 		  case '<':
1703 			anglecnt++;
1704 			break;
1705 
1706 		  case '>':
1707 			if (anglecnt-- <= 0)
1708 				return NULL;
1709 			break;
1710 		}
1711 		*q++ = c;
1712 	}
1713 
1714 	if (anglecnt != 0 || cmntcnt != 0 || bslashmode ||
1715 	    quotemode || quotecnt <= 0)
1716 		return NULL;
1717 	*q++ = '\0';
1718 	return buf;
1719 }
1720