16056Seric # include "sendmail.h"
23379Seric 
3*6980Seric SCCSID(@(#)macro.c	3.11.1.1		05/29/82);
43379Seric 
53379Seric /*
63379Seric **  EXPAND -- macro expand a string using $x escapes.
73379Seric **
83379Seric **	Parameters:
93379Seric **		s -- the string to expand.
103379Seric **		buf -- the place to put the expansion.
113379Seric **		buflim -- the buffer limit, i.e., the address
123379Seric **			of the last usable position in buf.
13*6980Seric **		e -- envelope in which to work.
143379Seric **
153379Seric **	Returns:
164319Seric **		End of interpolated output.
173379Seric **
183379Seric **	Side Effects:
193379Seric **		none.
203379Seric */
213379Seric 
22*6980Seric expand(s, buf, buflim, e)
23*6980Seric 	register char *s;
24*6980Seric 	register char *buf;
25*6980Seric 	char *buflim;
26*6980Seric 	register ENVELOPE *e;
27*6980Seric {
28*6980Seric 	extern char *expand2();
29*6980Seric 
30*6980Seric 	(void) expand2(s, buf, buflim, e);
31*6980Seric }
32*6980Seric 
33*6980Seric 
343379Seric char *
35*6980Seric expand2(s, buf, buflim, e)
363379Seric 	register char *s;
373379Seric 	register char *buf;
383379Seric 	char *buflim;
39*6980Seric 	register ENVELOPE *e;
403379Seric {
416056Seric 	register char *q;
426056Seric 	char xbuf[BUFSIZ];
436056Seric 	register char *xp = xbuf;
444319Seric 	bool skipping;		/* set if conditionally skipping output */
456056Seric 	bool gotone = FALSE;	/* set if any expansion done */
463379Seric 
473379Seric # ifdef DEBUG
484073Seric 	if (Debug > 3)
496056Seric 	{
506056Seric 		printf("expand(");
516056Seric 		xputs(s);
526056Seric 		printf(")\n");
536056Seric 	}
543379Seric # endif DEBUG
553379Seric 
563387Seric 	skipping = FALSE;
576056Seric 	for (; *s != '\0'; s++)
583379Seric 	{
596056Seric 		char c;
604319Seric 
614319Seric 		/*
626056Seric 		**  Check for non-ordinary (special?) character.
634319Seric 		**	'q' will be the interpolated quantity.
644319Seric 		*/
654319Seric 
663379Seric 		q = NULL;
676056Seric 		c = *s;
686056Seric 		switch (c)
693387Seric 		{
706056Seric 		  case CONDIF:		/* see if var set */
713387Seric 			c = *++s;
72*6980Seric 			skipping = e->e_macro[c] == NULL;
736056Seric 			continue;
743387Seric 
756056Seric 		  case CONDELSE:	/* change state of skipping */
766056Seric 			skipping = !skipping;
776056Seric 			continue;
783387Seric 
796056Seric 		  case CONDFI:		/* stop skipping */
806056Seric 			skipping = FALSE;
816056Seric 			continue;
823387Seric 
836056Seric 		  case '$':		/* macro interpolation */
846056Seric 			c = *++s;
85*6980Seric 			q = e->e_macro[c & 0177];
864172Seric 			if (q == NULL && c != '$')
873387Seric 				continue;
886056Seric 			gotone = TRUE;
896056Seric 			break;
903387Seric 		}
913387Seric 
923379Seric 		/*
933379Seric 		**  Interpolate q or output one character
943379Seric 		*/
953379Seric 
963387Seric 		if (skipping)
973387Seric 			continue;
986056Seric 		while (xp < &xbuf[sizeof xbuf])
996056Seric 		{
1006056Seric 			if (q == NULL)
1016056Seric 			{
1026056Seric 				*xp++ = c;
1036056Seric 				break;
1046056Seric 			}
1056056Seric 			if (*q == NULL)
1066056Seric 				break;
1076056Seric 			*xp++ = *q++;
1086056Seric 		}
1093379Seric 	}
1106056Seric 	*xp = '\0';
1113379Seric 
1123379Seric # ifdef DEBUG
1134073Seric 	if (Debug > 3)
1146056Seric 	{
1156056Seric 		printf("expand ==> '");
1166056Seric 		xputs(xbuf);
1176056Seric 		printf("'\n");
1186056Seric 	}
1193379Seric # endif DEBUG
1203379Seric 
1216056Seric 	/* recurse as appropriate */
1226056Seric 	if (gotone)
123*6980Seric 		return (expand2(xbuf, buf, buflim, e));
1246056Seric 
1256056Seric 	/* copy results out */
1266056Seric 	for (q = buf, xp = xbuf; xp != '\0' && q < buflim-1; )
1276056Seric 		*q++ = *xp++;
1286056Seric 	*q = '\0';
1296056Seric 
1306056Seric 	return (q);
1313379Seric }
1323379Seric /*
1333379Seric **  DEFINE -- define a macro.
1343379Seric **
1353379Seric **	this would be better done using a #define macro.
1363379Seric **
1373379Seric **	Parameters:
1383379Seric **		n -- the macro name.
1393379Seric **		v -- the macro value.
1403379Seric **
1413379Seric **	Returns:
1423379Seric **		none.
1433379Seric **
1443379Seric **	Side Effects:
145*6980Seric **		CurEnv->e_macro[n] is defined.
1464092Seric **
1474092Seric **	Notes:
1484092Seric **		There is one macro for each ASCII character,
1494092Seric **		although they are not all used.  The currently
1504092Seric **		defined macros are:
1514092Seric **
1524202Seric **		$a   date in ARPANET format (preferring the Date: line
1534202Seric **		     of the message)
1544202Seric **		$b   the current date (as opposed to the date as found
1554202Seric **		     the message) in ARPANET format
1564092Seric **		$c   hop count
1574202Seric **		$d   (current) date in UNIX (ctime) format
1584092Seric **		$f   raw from address
1594092Seric **		$g   translated from address
1604092Seric **		$h   to host
1614558Seric **		$i   official SMTP hostname, used in messages+
1624092Seric **		$l   UNIX-style from line+
1634092Seric **		$n   name of sendmail ("MAILER-DAEMON" on local
1644092Seric **		     net typically)+
1654092Seric **		$o   delimiters ("operators") for address tokens+
1664092Seric **		$p   my process id in decimal
1675919Seric **		$q   the string that becomes an address -- this is
1685919Seric **		     normally used to combine $g & $x.
1695187Seric **		$r   protocol used to talk to sender
1705187Seric **		$s   sender's host name
1714092Seric **		$t   the current time in seconds since 1/1/1970
1724092Seric **		$u   to user
1734092Seric **		$v   version number of sendmail
1744092Seric **		$x   signature (full name) of from person
1754202Seric **		$y   the tty id of our terminal
1764092Seric **		$z   home directory of to person
1774092Seric **
1784092Seric **		Macros marked with + must be defined in the
1794092Seric **		configuration file and are used internally, but
1804092Seric **		are not set.
1814092Seric **
1824092Seric **		There are also some macros that can be used
1834092Seric **		arbitrarily to make the configuration file
1844092Seric **		cleaner.  In general all upper-case letters
1854092Seric **		are available.
1863379Seric */
1873379Seric 
1883379Seric define(n, v)
1893379Seric 	char n;
1903379Seric 	char *v;
1913379Seric {
1923379Seric # ifdef DEBUG
1934073Seric 	if (Debug > 3)
1946056Seric 	{
1956056Seric 		printf("define(%c as ", n);
1966056Seric 		xputs(v);
1976056Seric 		printf(")\n");
1986056Seric 	}
1993379Seric # endif DEBUG
200*6980Seric 	CurEnv->e_macro[n & 0177] = v;
2013379Seric }
2024454Seric /*
2034454Seric **  MACVALUE -- return uninterpreted value of a macro.
2044454Seric **
2054454Seric **	Parameters:
2064454Seric **		n -- the name of the macro.
2074454Seric **
2084454Seric **	Returns:
2094454Seric **		The value of n.
2104454Seric **
2114454Seric **	Side Effects:
2124454Seric **		none.
2134454Seric */
2144454Seric 
2154454Seric char *
2164454Seric macvalue(n)
2174454Seric 	char n;
2184454Seric {
219*6980Seric 	return (CurEnv->e_macro[n & 0177]);
2204454Seric }
221