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.5 (Berkeley) 02/18/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 & 0377) 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 MACROEXPAND: /* 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 /* check for any sendmail metacharacters */ 113 if ((c & 0340) == 0200) 114 recurse = TRUE; 115 *xp++ = c; 116 } 117 } 118 } 119 *xp = '\0'; 120 121 if (tTd(35, 24)) 122 { 123 printf("expand ==> "); 124 xputs(xbuf); 125 printf("\n"); 126 } 127 128 /* recurse as appropriate */ 129 if (recurse) 130 { 131 expand(xbuf, buf, buflim, e); 132 return; 133 } 134 135 /* copy results out */ 136 i = buflim - buf - 1; 137 if (i > xp - xbuf) 138 i = xp - xbuf; 139 bcopy(xbuf, buf, i); 140 buf[i] = '\0'; 141 } 142 /* 143 ** DEFINE -- define a macro. 144 ** 145 ** this would be better done using a #define macro. 146 ** 147 ** Parameters: 148 ** n -- the macro name. 149 ** v -- the macro value. 150 ** e -- the envelope to store the definition in. 151 ** 152 ** Returns: 153 ** none. 154 ** 155 ** Side Effects: 156 ** e->e_macro[n] is defined. 157 ** 158 ** Notes: 159 ** There is one macro for each ASCII character, 160 ** although they are not all used. The currently 161 ** defined macros are: 162 ** 163 ** $a date in ARPANET format (preferring the Date: line 164 ** of the message) 165 ** $b the current date (as opposed to the date as found 166 ** the message) in ARPANET format 167 ** $c hop count 168 ** $d (current) date in UNIX (ctime) format 169 ** $e the SMTP entry message+ 170 ** $f raw from address 171 ** $g translated from address 172 ** $h to host 173 ** $i queue id 174 ** $j official SMTP hostname, used in messages+ 175 ** $k UUCP node name 176 ** $l UNIX-style from line+ 177 ** $n name of sendmail ("MAILER-DAEMON" on local 178 ** net typically)+ 179 ** $o delimiters ("operators") for address tokens+ 180 ** $p my process id in decimal 181 ** $q the string that becomes an address -- this is 182 ** normally used to combine $g & $x. 183 ** $r protocol used to talk to sender 184 ** $s sender's host name 185 ** $t the current time in seconds since 1/1/1970 186 ** $u to user 187 ** $v version number of sendmail 188 ** $w our host name (if it can be determined) 189 ** $x signature (full name) of from person 190 ** $y the tty id of our terminal 191 ** $z home directory of to person 192 ** $< the return path (sender in envelope) relative to recipient 193 ** 194 ** Macros marked with + must be defined in the 195 ** configuration file and are used internally, but 196 ** are not set. 197 ** 198 ** There are also some macros that can be used 199 ** arbitrarily to make the configuration file 200 ** cleaner. In general all upper-case letters 201 ** are available. 202 */ 203 204 define(n, v, e) 205 char n; 206 char *v; 207 register ENVELOPE *e; 208 { 209 if (tTd(35, 9)) 210 { 211 printf("define(%c as ", n); 212 xputs(v); 213 printf(")\n"); 214 } 215 e->e_macro[n & 0177] = v; 216 } 217 /* 218 ** MACVALUE -- return uninterpreted value of a macro. 219 ** 220 ** Parameters: 221 ** n -- the name of the macro. 222 ** 223 ** Returns: 224 ** The value of n. 225 ** 226 ** Side Effects: 227 ** none. 228 */ 229 230 char * 231 macvalue(n, e) 232 char n; 233 register ENVELOPE *e; 234 { 235 n &= 0177; 236 while (e != NULL) 237 { 238 register char *p = e->e_macro[n]; 239 240 if (p != NULL) 241 return (p); 242 e = e->e_parent; 243 } 244 return (NULL); 245 } 246