2 * Copyright (c) 1998 Sendmail, Inc. All rights reserved.
3 * Copyright (c) 1983, 1995-1997 Eric P. Allman. All rights reserved.
4 * Copyright (c) 1988, 1993
5 * The Regents of the University of California. All rights reserved.
7 * By using this file, you agree to the terms and conditions set
8 * forth in the LICENSE file which can be found at the top level of
9 * the sendmail distribution.
14 static char sccsid[] = "@(#)macro.c 8.26 (Berkeley) 11/8/1998";
17 # include "sendmail.h"
19 char *MacroName[256]; /* macro id to name table */
20 int NextMacroId = 0240; /* codes for long named macros */
24 ** EXPAND -- macro expand a string using $x escapes.
27 ** s -- the string to expand.
28 ** buf -- the place to put the expansion.
29 ** bufsize -- the size of the buffer.
30 ** e -- envelope in which to work.
40 expand(s, buf, bufsize, e)
48 bool skipping; /* set if conditionally skipping output */
49 bool recurse = FALSE; /* set if recursion required */
51 int skiplev; /* skipping nesting level */
52 int iflev; /* if nesting level */
53 char xbuf[MACBUFSIZE];
54 static int explevel = 0;
68 for (xp = xbuf; *s != '\0'; s++)
73 ** Check for non-ordinary (special?) character.
74 ** 'q' will be the interpolated quantity.
81 case CONDIF: /* see if var set */
87 skipping = macvalue(c, e) == NULL;
90 case CONDELSE: /* change state of skipping */
97 case CONDFI: /* stop skipping */
107 case MACROEXPAND: /* macro interpolation */
122 ** Interpolate q or output one character
125 if (skipping || xp >= &xbuf[sizeof xbuf - 1])
131 /* copy to end of q or max space remaining in buf */
132 while ((c = *q++) != '\0' && xp < &xbuf[sizeof xbuf - 1])
134 /* check for any sendmail metacharacters */
135 if ((c & 0340) == 0200)
145 printf("expand ==> ");
150 /* recurse as appropriate */
153 if (explevel < MaxMacroRecursion)
156 expand(xbuf, buf, bufsize, e);
160 syserr("expand: recursion too deep (%d max)",
164 /* copy results out */
172 ** DEFINE -- define a macro.
174 ** this would be better done using a #define macro.
177 ** n -- the macro name.
178 ** v -- the macro value.
179 ** e -- the envelope to store the definition in.
185 ** e->e_macro[n] is defined.
188 ** There is one macro for each ASCII character,
189 ** although they are not all used. The currently
190 ** defined macros are:
192 ** $a date in ARPANET format (preferring the Date: line
194 ** $b the current date (as opposed to the date as found
195 ** the message) in ARPANET format
197 ** $d (current) date in UNIX (ctime) format
198 ** $e the SMTP entry message+
199 ** $f raw from address
200 ** $g translated from address
203 ** $j official SMTP hostname, used in messages+
205 ** $l UNIX-style from line+
206 ** $m The domain part of our full name.
207 ** $n name of sendmail ("MAILER-DAEMON" on local
209 ** $o delimiters ("operators") for address tokens+
210 ** (set via OperatorChars option in V6 or later
211 ** sendmail.cf files)
212 ** $p my process id in decimal
213 ** $q the string that becomes an address -- this is
214 ** normally used to combine $g & $x.
215 ** $r protocol used to talk to sender
216 ** $s sender's host name
217 ** $t the current time in seconds since 1/1/1970
219 ** $v version number of sendmail
220 ** $w our host name (if it can be determined)
221 ** $x signature (full name) of from person
222 ** $y the tty id of our terminal
223 ** $z home directory of to person
224 ** $_ RFC1413 authenticated sender address
226 ** Macros marked with + must be defined in the
227 ** configuration file and are used internally, but
230 ** There are also some macros that can be used
231 ** arbitrarily to make the configuration file
232 ** cleaner. In general all upper-case letters
240 register ENVELOPE *e;
244 printf("%sdefine(%s as ",
245 (e->e_macro[n & 0377] == NULL) ? "" : "re", macname(n));
249 e->e_macro[n & 0377] = v;
252 ** MACVALUE -- return uninterpreted value of a macro.
255 ** n -- the name of the macro.
267 register ENVELOPE *e;
272 register char *p = e->e_macro[n];
281 ** MACNAME -- return the name of a macro given its internal id
284 ** n -- the id of the macro
302 char *p = MacroName[n];
306 return "***UNDEFINED MACRO***";
313 ** MACID -- return id of macro identified by its name
316 ** p -- pointer to name string -- either a single
317 ** character or {name}.
318 ** ep -- filled in with the pointer to the byte
322 ** The internal id code for this macro. This will
323 ** fit into a single byte.
326 ** If this is a new macro name, a new id is allocated.
336 char mbuf[MAXMACNAMELEN + 1];
345 if (*p == '\0' || (p[0] == '{' && p[1] == '}'))
347 syserr("Name required for macro/class");
356 /* the macro is its own code */
364 while (*++p != '\0' && *p != '}' && bp < &mbuf[sizeof mbuf - 1])
366 if (isascii(*p) && (isalnum(*p) || *p == '_'))
369 syserr("Invalid macro/class character %c", *p);
375 syserr("Unbalanced { on %s", mbuf); /* missing } */
379 syserr("Macro/class name ({%s}) too long (%d chars max)",
380 mbuf, sizeof mbuf - 1);
382 else if (mbuf[1] == '\0')
392 s = stab(mbuf, ST_MACRO, ST_ENTER);
397 if (NextMacroId > 0377)
399 syserr("Macro/class {%s}: too many long names", mbuf);
404 MacroName[NextMacroId] = s->s_name;
405 s->s_macro = mid = NextMacroId++;
413 printf("0x%x\n", mid);
417 ** WORDINCLASS -- tell if a word is in a specific class
420 ** str -- the name of the word to look up.
421 ** cl -- the class name.
424 ** TRUE if str can be found in cl.
435 s = stab(str, ST_CLASS, ST_FIND);
436 return s != NULL && bitnset(cl & 0xff, s->s_class);