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[] = "@(#)macro.c 6.4 (Berkeley) 02/14/93"; 11 #endif /* not lint */ 12 13 # include "sendmail.h" 14 15 /* 16 ** EXPAND -- macro expand a string using $x escapes. 17 ** 18 ** Parameters: 19 ** s -- the string to expand. 20 ** buf -- the place to put the expansion. 21 ** buflim -- the buffer limit, i.e., the address 22 ** of the last usable position in buf. 23 ** e -- envelope in which to work. 24 ** 25 ** Returns: 26 ** none. 27 ** 28 ** Side Effects: 29 ** none. 30 */ 31 32 expand(s, buf, buflim, e) 33 register char *s; 34 register char *buf; 35 char *buflim; 36 register ENVELOPE *e; 37 { 38 register char *xp; 39 register char *q; 40 bool skipping; /* set if conditionally skipping output */ 41 bool recurse = FALSE; /* set if recursion required */ 42 int i; 43 int iflev; /* if nesting level */ 44 char xbuf[BUFSIZ]; 45 extern char *macvalue(); 46 47 if (tTd(35, 24)) 48 { 49 printf("expand("); 50 xputs(s); 51 printf(")\n"); 52 } 53 54 skipping = FALSE; 55 iflev = 0; 56 if (s == NULL) 57 s = ""; 58 for (xp = xbuf; *s != '\0'; s++) 59 { 60 char c; 61 62 /* 63 ** Check for non-ordinary (special?) character. 64 ** 'q' will be the interpolated quantity. 65 */ 66 67 q = NULL; 68 c = *s; 69 switch (c) 70 { 71 case CONDIF: /* see if var set */ 72 c = *++s; 73 if (skipping) 74 iflev++; 75 else 76 skipping = macvalue(c, e) == NULL; 77 continue; 78 79 case CONDELSE: /* change state of skipping */ 80 if (iflev == 0) 81 skipping = !skipping; 82 continue; 83 84 case CONDFI: /* stop skipping */ 85 if (iflev == 0) 86 skipping = FALSE; 87 if (skipping) 88 iflev--; 89 continue; 90 91 case '\001': /* macro interpolation */ 92 c = *++s; 93 q = macvalue(c & 0177, e); 94 if (q == NULL) 95 continue; 96 break; 97 } 98 99 /* 100 ** Interpolate q or output one character 101 */ 102 103 if (skipping || xp >= &xbuf[sizeof xbuf]) 104 continue; 105 if (q == NULL) 106 *xp++ = c; 107 else 108 { 109 /* copy to end of q or max space remaining in buf */ 110 while ((c = *q++) != '\0' && xp < &xbuf[sizeof xbuf - 1]) 111 { 112 if (iscntrl(c) && !isspace(c)) 113 recurse = TRUE; 114 *xp++ = c; 115 } 116 } 117 } 118 *xp = '\0'; 119 120 if (tTd(35, 24)) 121 { 122 printf("expand ==> "); 123 xputs(xbuf); 124 printf("\n"); 125 } 126 127 /* recurse as appropriate */ 128 if (recurse) 129 { 130 expand(xbuf, buf, buflim, e); 131 return; 132 } 133 134 /* copy results out */ 135 i = buflim - buf - 1; 136 if (i > xp - xbuf) 137 i = xp - xbuf; 138 bcopy(xbuf, buf, i); 139 buf[i] = '\0'; 140 } 141 /* 142 ** DEFINE -- define a macro. 143 ** 144 ** this would be better done using a #define macro. 145 ** 146 ** Parameters: 147 ** n -- the macro name. 148 ** v -- the macro value. 149 ** e -- the envelope to store the definition in. 150 ** 151 ** Returns: 152 ** none. 153 ** 154 ** Side Effects: 155 ** e->e_macro[n] is defined. 156 ** 157 ** Notes: 158 ** There is one macro for each ASCII character, 159 ** although they are not all used. The currently 160 ** defined macros are: 161 ** 162 ** $a date in ARPANET format (preferring the Date: line 163 ** of the message) 164 ** $b the current date (as opposed to the date as found 165 ** the message) in ARPANET format 166 ** $c hop count 167 ** $d (current) date in UNIX (ctime) format 168 ** $e the SMTP entry message+ 169 ** $f raw from address 170 ** $g translated from address 171 ** $h to host 172 ** $i queue id 173 ** $j official SMTP hostname, used in messages+ 174 ** $k UUCP node name 175 ** $l UNIX-style from line+ 176 ** $n name of sendmail ("MAILER-DAEMON" on local 177 ** net typically)+ 178 ** $o delimiters ("operators") for address tokens+ 179 ** $p my process id in decimal 180 ** $q the string that becomes an address -- this is 181 ** normally used to combine $g & $x. 182 ** $r protocol used to talk to sender 183 ** $s sender's host name 184 ** $t the current time in seconds since 1/1/1970 185 ** $u to user 186 ** $v version number of sendmail 187 ** $w our host name (if it can be determined) 188 ** $x signature (full name) of from person 189 ** $y the tty id of our terminal 190 ** $z home directory of to person 191 ** $< the return path (sender in envelope) relative to recipient 192 ** 193 ** Macros marked with + must be defined in the 194 ** configuration file and are used internally, but 195 ** are not set. 196 ** 197 ** There are also some macros that can be used 198 ** arbitrarily to make the configuration file 199 ** cleaner. In general all upper-case letters 200 ** are available. 201 */ 202 203 define(n, v, e) 204 char n; 205 char *v; 206 register ENVELOPE *e; 207 { 208 if (tTd(35, 9)) 209 { 210 printf("define(%c as ", n); 211 xputs(v); 212 printf(")\n"); 213 } 214 e->e_macro[n & 0177] = v; 215 } 216 /* 217 ** MACVALUE -- return uninterpreted value of a macro. 218 ** 219 ** Parameters: 220 ** n -- the name of the macro. 221 ** 222 ** Returns: 223 ** The value of n. 224 ** 225 ** Side Effects: 226 ** none. 227 */ 228 229 char * 230 macvalue(n, e) 231 char n; 232 register ENVELOPE *e; 233 { 234 n &= 0177; 235 while (e != NULL) 236 { 237 register char *p = e->e_macro[n]; 238 239 if (p != NULL) 240 return (p); 241 e = e->e_parent; 242 } 243 return (NULL); 244 } 245