16056Seric # include "sendmail.h"
23379Seric 
3*7851Seric SCCSID(@(#)macro.c	3.15		08/22/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.
136980Seric **		e -- envelope in which to work.
143379Seric **
153379Seric **	Returns:
164319Seric **		End of interpolated output.
173379Seric **
183379Seric **	Side Effects:
193379Seric **		none.
203379Seric */
213379Seric 
226980Seric expand(s, buf, buflim, e)
236980Seric 	register char *s;
246980Seric 	register char *buf;
256980Seric 	char *buflim;
266980Seric 	register ENVELOPE *e;
276980Seric {
286980Seric 	extern char *expand2();
296980Seric 
306980Seric 	(void) expand2(s, buf, buflim, e);
316980Seric }
326980Seric 
336980Seric 
343379Seric char *
356980Seric expand2(s, buf, buflim, e)
363379Seric 	register char *s;
373379Seric 	register char *buf;
383379Seric 	char *buflim;
396980Seric 	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
487677Seric 	if (tTd(35, 4))
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;
726980Seric 			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;
856980Seric 			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
1137677Seric 	if (tTd(35, 4))
1146056Seric 	{
1156056Seric 		printf("expand ==> '");
1166056Seric 		xputs(xbuf);
1176056Seric 		printf("'\n");
1186056Seric 	}
1193379Seric # endif DEBUG
1203379Seric 
1216056Seric 	/* recurse as appropriate */
1226056Seric 	if (gotone)
1236980Seric 		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:
1456980Seric **		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
161*7851Seric **		$i   queue id
162*7851Seric **		$j   official SMTP hostname, used in messages+
1634092Seric **		$l   UNIX-style from line+
1644092Seric **		$n   name of sendmail ("MAILER-DAEMON" on local
1654092Seric **		     net typically)+
1664092Seric **		$o   delimiters ("operators") for address tokens+
1674092Seric **		$p   my process id in decimal
1685919Seric **		$q   the string that becomes an address -- this is
1695919Seric **		     normally used to combine $g & $x.
1705187Seric **		$r   protocol used to talk to sender
1715187Seric **		$s   sender's host name
1724092Seric **		$t   the current time in seconds since 1/1/1970
1734092Seric **		$u   to user
1744092Seric **		$v   version number of sendmail
1754092Seric **		$x   signature (full name) of from person
1764202Seric **		$y   the tty id of our terminal
1774092Seric **		$z   home directory of to person
1784092Seric **
1794092Seric **		Macros marked with + must be defined in the
1804092Seric **		configuration file and are used internally, but
1814092Seric **		are not set.
1824092Seric **
1834092Seric **		There are also some macros that can be used
1844092Seric **		arbitrarily to make the configuration file
1854092Seric **		cleaner.  In general all upper-case letters
1864092Seric **		are available.
1873379Seric */
1883379Seric 
1893379Seric define(n, v)
1903379Seric 	char n;
1913379Seric 	char *v;
1923379Seric {
1933379Seric # ifdef DEBUG
1947677Seric 	if (tTd(35, 3))
1956056Seric 	{
1966056Seric 		printf("define(%c as ", n);
1976056Seric 		xputs(v);
1986056Seric 		printf(")\n");
1996056Seric 	}
2003379Seric # endif DEBUG
2016980Seric 	CurEnv->e_macro[n & 0177] = v;
2023379Seric }
2034454Seric /*
2044454Seric **  MACVALUE -- return uninterpreted value of a macro.
2054454Seric **
2064454Seric **	Parameters:
2074454Seric **		n -- the name of the macro.
2084454Seric **
2094454Seric **	Returns:
2104454Seric **		The value of n.
2114454Seric **
2124454Seric **	Side Effects:
2134454Seric **		none.
2144454Seric */
2154454Seric 
2164454Seric char *
2174454Seric macvalue(n)
2184454Seric 	char n;
2194454Seric {
2206980Seric 	return (CurEnv->e_macro[n & 0177]);
2214454Seric }
222