122707Sdist /* 234921Sbostic * Copyright (c) 1983 Eric P. Allman 333729Sbostic * Copyright (c) 1988 Regents of the University of California. 433729Sbostic * All rights reserved. 533729Sbostic * 642826Sbostic * %sccs.include.redist.c% 733729Sbostic */ 822707Sdist 922707Sdist #ifndef lint 10*57977Seric static char sccsid[] = "@(#)macro.c 6.4 (Berkeley) 02/14/93"; 1133729Sbostic #endif /* not lint */ 1222707Sdist 136056Seric # include "sendmail.h" 143379Seric 153379Seric /* 163379Seric ** EXPAND -- macro expand a string using $x escapes. 173379Seric ** 183379Seric ** Parameters: 193379Seric ** s -- the string to expand. 203379Seric ** buf -- the place to put the expansion. 213379Seric ** buflim -- the buffer limit, i.e., the address 223379Seric ** of the last usable position in buf. 236980Seric ** e -- envelope in which to work. 243379Seric ** 253379Seric ** Returns: 269382Seric ** none. 273379Seric ** 283379Seric ** Side Effects: 293379Seric ** none. 303379Seric */ 313379Seric 326980Seric expand(s, buf, buflim, e) 336980Seric register char *s; 346980Seric register char *buf; 356980Seric char *buflim; 366980Seric register ENVELOPE *e; 376980Seric { 3817349Seric register char *xp; 396056Seric register char *q; 409382Seric bool skipping; /* set if conditionally skipping output */ 4117349Seric bool recurse = FALSE; /* set if recursion required */ 4217349Seric int i; 4357589Seric int iflev; /* if nesting level */ 446056Seric char xbuf[BUFSIZ]; 458182Seric extern char *macvalue(); 463379Seric 4724941Seric if (tTd(35, 24)) 486056Seric { 496056Seric printf("expand("); 506056Seric xputs(s); 516056Seric printf(")\n"); 526056Seric } 533379Seric 543387Seric skipping = FALSE; 5557589Seric iflev = 0; 568064Seric if (s == NULL) 578064Seric s = ""; 5817349Seric for (xp = xbuf; *s != '\0'; s++) 593379Seric { 606056Seric char c; 614319Seric 624319Seric /* 636056Seric ** Check for non-ordinary (special?) character. 644319Seric ** 'q' will be the interpolated quantity. 654319Seric */ 664319Seric 673379Seric q = NULL; 686056Seric c = *s; 696056Seric switch (c) 703387Seric { 716056Seric case CONDIF: /* see if var set */ 72*57977Seric c = *++s; 73*57977Seric if (skipping) 74*57977Seric iflev++; 75*57977Seric else 7657589Seric skipping = macvalue(c, e) == NULL; 77*57977Seric continue; 783387Seric 796056Seric case CONDELSE: /* change state of skipping */ 80*57977Seric if (iflev == 0) 8157589Seric skipping = !skipping; 826056Seric continue; 833387Seric 846056Seric case CONDFI: /* stop skipping */ 85*57977Seric if (iflev == 0) 8657589Seric skipping = FALSE; 87*57977Seric if (skipping) 88*57977Seric iflev--; 89*57977Seric continue; 903387Seric 9116146Seric case '\001': /* macro interpolation */ 926056Seric c = *++s; 938182Seric q = macvalue(c & 0177, e); 949382Seric if (q == NULL) 953387Seric continue; 966056Seric break; 973387Seric } 983387Seric 993379Seric /* 1003379Seric ** Interpolate q or output one character 1013379Seric */ 1023379Seric 10317349Seric if (skipping || xp >= &xbuf[sizeof xbuf]) 1043387Seric continue; 10517349Seric if (q == NULL) 10617349Seric *xp++ = c; 10717349Seric else 1086056Seric { 10917349Seric /* copy to end of q or max space remaining in buf */ 11017349Seric while ((c = *q++) != '\0' && xp < &xbuf[sizeof xbuf - 1]) 1116056Seric { 11217349Seric if (iscntrl(c) && !isspace(c)) 11317349Seric recurse = TRUE; 1146056Seric *xp++ = c; 1156056Seric } 1166056Seric } 1173379Seric } 1186056Seric *xp = '\0'; 1193379Seric 12024941Seric if (tTd(35, 24)) 1216056Seric { 1228064Seric printf("expand ==> "); 1236056Seric xputs(xbuf); 1248064Seric printf("\n"); 1256056Seric } 1263379Seric 1276056Seric /* recurse as appropriate */ 12817349Seric if (recurse) 1299382Seric { 1309382Seric expand(xbuf, buf, buflim, e); 1319382Seric return; 1329382Seric } 1336056Seric 1346056Seric /* copy results out */ 13517349Seric i = buflim - buf - 1; 13617349Seric if (i > xp - xbuf) 13717349Seric i = xp - xbuf; 13817349Seric bcopy(xbuf, buf, i); 13917349Seric buf[i] = '\0'; 1403379Seric } 1413379Seric /* 1423379Seric ** DEFINE -- define a macro. 1433379Seric ** 1443379Seric ** this would be better done using a #define macro. 1453379Seric ** 1463379Seric ** Parameters: 1473379Seric ** n -- the macro name. 1483379Seric ** v -- the macro value. 1499382Seric ** e -- the envelope to store the definition in. 1503379Seric ** 1513379Seric ** Returns: 1523379Seric ** none. 1533379Seric ** 1543379Seric ** Side Effects: 1559382Seric ** e->e_macro[n] is defined. 1564092Seric ** 1574092Seric ** Notes: 1584092Seric ** There is one macro for each ASCII character, 1594092Seric ** although they are not all used. The currently 1604092Seric ** defined macros are: 1614092Seric ** 1624202Seric ** $a date in ARPANET format (preferring the Date: line 1634202Seric ** of the message) 1644202Seric ** $b the current date (as opposed to the date as found 1654202Seric ** the message) in ARPANET format 1664092Seric ** $c hop count 1674202Seric ** $d (current) date in UNIX (ctime) format 16810710Seric ** $e the SMTP entry message+ 1694092Seric ** $f raw from address 1704092Seric ** $g translated from address 1714092Seric ** $h to host 1727851Seric ** $i queue id 1737851Seric ** $j official SMTP hostname, used in messages+ 17457630Seric ** $k UUCP node name 1754092Seric ** $l UNIX-style from line+ 1764092Seric ** $n name of sendmail ("MAILER-DAEMON" on local 1774092Seric ** net typically)+ 1784092Seric ** $o delimiters ("operators") for address tokens+ 1794092Seric ** $p my process id in decimal 1805919Seric ** $q the string that becomes an address -- this is 1815919Seric ** normally used to combine $g & $x. 1825187Seric ** $r protocol used to talk to sender 1835187Seric ** $s sender's host name 1844092Seric ** $t the current time in seconds since 1/1/1970 1854092Seric ** $u to user 1864092Seric ** $v version number of sendmail 18710407Seric ** $w our host name (if it can be determined) 1884092Seric ** $x signature (full name) of from person 1894202Seric ** $y the tty id of our terminal 1904092Seric ** $z home directory of to person 19157630Seric ** $< the return path (sender in envelope) relative to recipient 1924092Seric ** 1934092Seric ** Macros marked with + must be defined in the 1944092Seric ** configuration file and are used internally, but 1954092Seric ** are not set. 1964092Seric ** 1974092Seric ** There are also some macros that can be used 1984092Seric ** arbitrarily to make the configuration file 1994092Seric ** cleaner. In general all upper-case letters 2004092Seric ** are available. 2013379Seric */ 2023379Seric 2039382Seric define(n, v, e) 2043379Seric char n; 2053379Seric char *v; 2069382Seric register ENVELOPE *e; 2073379Seric { 20824941Seric if (tTd(35, 9)) 2096056Seric { 2106056Seric printf("define(%c as ", n); 2116056Seric xputs(v); 2126056Seric printf(")\n"); 2136056Seric } 2149382Seric e->e_macro[n & 0177] = v; 2153379Seric } 2164454Seric /* 2174454Seric ** MACVALUE -- return uninterpreted value of a macro. 2184454Seric ** 2194454Seric ** Parameters: 2204454Seric ** n -- the name of the macro. 2214454Seric ** 2224454Seric ** Returns: 2234454Seric ** The value of n. 2244454Seric ** 2254454Seric ** Side Effects: 2264454Seric ** none. 2274454Seric */ 2284454Seric 2294454Seric char * 2308182Seric macvalue(n, e) 2314454Seric char n; 2328182Seric register ENVELOPE *e; 2334454Seric { 2348182Seric n &= 0177; 2358182Seric while (e != NULL) 2368182Seric { 2378182Seric register char *p = e->e_macro[n]; 2388182Seric 2398182Seric if (p != NULL) 2408182Seric return (p); 2418182Seric e = e->e_parent; 2428182Seric } 2438182Seric return (NULL); 2444454Seric } 245