new copyright notice
[unix-history] / usr / src / usr.sbin / sendmail / src / macro.c
index 9678f85..37d7c2c 100644 (file)
@@ -1,8 +1,18 @@
+/*
+ * Copyright (c) 1983 Eric P. Allman
+ * Copyright (c) 1988 Regents of the University of California.
+ * All rights reserved.
+ *
+ * %sccs.include.redist.c%
+ */
+
+#ifndef lint
+static char sccsid[] = "@(#)macro.c    5.7 (Berkeley) %G%";
+#endif /* not lint */
+
 # include "sendmail.h"
 # include "conf.h"
 
 # include "sendmail.h"
 # include "conf.h"
 
-SCCSID(@(#)macro.c     3.15            %G%);
-
 /*
 **  EXPAND -- macro expand a string using $x escapes.
 **
 /*
 **  EXPAND -- macro expand a string using $x escapes.
 **
@@ -14,7 +24,7 @@ SCCSID(@(#)macro.c    3.15            %G%);
 **             e -- envelope in which to work.
 **
 **     Returns:
 **             e -- envelope in which to work.
 **
 **     Returns:
-**             End of interpolated output.
+**             none.
 **
 **     Side Effects:
 **             none.
 **
 **     Side Effects:
 **             none.
@@ -26,36 +36,25 @@ expand(s, buf, buflim, e)
        char *buflim;
        register ENVELOPE *e;
 {
        char *buflim;
        register ENVELOPE *e;
 {
-       extern char *expand2();
-
-       (void) expand2(s, buf, buflim, e);
-}
-
-
-char *
-expand2(s, buf, buflim, e)
-       register char *s;
-       register char *buf;
-       char *buflim;
-       register ENVELOPE *e;
-{
+       register char *xp;
        register char *q;
        register char *q;
-       char xbuf[BUFSIZ];
-       register char *xp = xbuf;
        bool skipping;          /* set if conditionally skipping output */
        bool skipping;          /* set if conditionally skipping output */
-       bool gotone = FALSE;    /* set if any expansion done */
+       bool recurse = FALSE;   /* set if recursion required */
+       int i;
+       char xbuf[BUFSIZ];
+       extern char *macvalue();
 
 
-# ifdef DEBUG
-       if (tTd(35, 4))
+       if (tTd(35, 24))
        {
                printf("expand(");
                xputs(s);
                printf(")\n");
        }
        {
                printf("expand(");
                xputs(s);
                printf(")\n");
        }
-# endif DEBUG
 
        skipping = FALSE;
 
        skipping = FALSE;
-       for (; *s != '\0'; s++)
+       if (s == NULL)
+               s = "";
+       for (xp = xbuf; *s != '\0'; s++)
        {
                char c;
 
        {
                char c;
 
@@ -70,7 +69,7 @@ expand2(s, buf, buflim, e)
                {
                  case CONDIF:          /* see if var set */
                        c = *++s;
                {
                  case CONDIF:          /* see if var set */
                        c = *++s;
-                       skipping = e->e_macro[c] == NULL;
+                       skipping = macvalue(c, e) == NULL;
                        continue;
 
                  case CONDELSE:        /* change state of skipping */
                        continue;
 
                  case CONDELSE:        /* change state of skipping */
@@ -81,12 +80,11 @@ expand2(s, buf, buflim, e)
                        skipping = FALSE;
                        continue;
 
                        skipping = FALSE;
                        continue;
 
-                 case '$':             /* macro interpolation */
+                 case '\001':          /* macro interpolation */
                        c = *++s;
                        c = *++s;
-                       q = e->e_macro[c & 0177];
-                       if (q == NULL && c != '$')
+                       q = macvalue(c & 0177, e);
+                       if (q == NULL)
                                continue;
                                continue;
-                       gotone = TRUE;
                        break;
                }
 
                        break;
                }
 
@@ -94,41 +92,43 @@ expand2(s, buf, buflim, e)
                **  Interpolate q or output one character
                */
 
                **  Interpolate q or output one character
                */
 
-               if (skipping)
+               if (skipping || xp >= &xbuf[sizeof xbuf])
                        continue;
                        continue;
-               while (xp < &xbuf[sizeof xbuf])
+               if (q == NULL)
+                       *xp++ = c;
+               else
                {
                {
-                       if (q == NULL)
+                       /* copy to end of q or max space remaining in buf */
+                       while ((c = *q++) != '\0' && xp < &xbuf[sizeof xbuf - 1])
                        {
                        {
+                               if (iscntrl(c) && !isspace(c))
+                                       recurse = TRUE;
                                *xp++ = c;
                                *xp++ = c;
-                               break;
                        }
                        }
-                       if (*q == NULL)
-                               break;
-                       *xp++ = *q++;
                }
        }
        *xp = '\0';
 
                }
        }
        *xp = '\0';
 
-# ifdef DEBUG
-       if (tTd(35, 4))
+       if (tTd(35, 24))
        {
        {
-               printf("expand ==> '");
+               printf("expand ==> ");
                xputs(xbuf);
                xputs(xbuf);
-               printf("'\n");
+               printf("\n");
        }
        }
-# endif DEBUG
 
        /* recurse as appropriate */
 
        /* recurse as appropriate */
-       if (gotone)
-               return (expand2(xbuf, buf, buflim, e));
+       if (recurse)
+       {
+               expand(xbuf, buf, buflim, e);
+               return;
+       }
 
        /* copy results out */
 
        /* copy results out */
-       for (q = buf, xp = xbuf; xp != '\0' && q < buflim-1; )
-               *q++ = *xp++;
-       *q = '\0';
-
-       return (q);
+       i = buflim - buf - 1;
+       if (i > xp - xbuf)
+               i = xp - xbuf;
+       bcopy(xbuf, buf, i);
+       buf[i] = '\0';
 }
 \f/*
 **  DEFINE -- define a macro.
 }
 \f/*
 **  DEFINE -- define a macro.
@@ -138,12 +138,13 @@ expand2(s, buf, buflim, e)
 **     Parameters:
 **             n -- the macro name.
 **             v -- the macro value.
 **     Parameters:
 **             n -- the macro name.
 **             v -- the macro value.
+**             e -- the envelope to store the definition in.
 **
 **     Returns:
 **             none.
 **
 **     Side Effects:
 **
 **     Returns:
 **             none.
 **
 **     Side Effects:
-**             CurEnv->e_macro[n] is defined.
+**             e->e_macro[n] is defined.
 **
 **     Notes:
 **             There is one macro for each ASCII character,
 **
 **     Notes:
 **             There is one macro for each ASCII character,
@@ -156,6 +157,7 @@ expand2(s, buf, buflim, e)
 **                  the message) in ARPANET format
 **             $c   hop count
 **             $d   (current) date in UNIX (ctime) format
 **                  the message) in ARPANET format
 **             $c   hop count
 **             $d   (current) date in UNIX (ctime) format
+**             $e   the SMTP entry message+
 **             $f   raw from address
 **             $g   translated from address
 **             $h   to host
 **             $f   raw from address
 **             $g   translated from address
 **             $h   to host
@@ -173,6 +175,7 @@ expand2(s, buf, buflim, e)
 **             $t   the current time in seconds since 1/1/1970
 **             $u   to user
 **             $v   version number of sendmail
 **             $t   the current time in seconds since 1/1/1970
 **             $u   to user
 **             $v   version number of sendmail
+**             $w   our host name (if it can be determined)
 **             $x   signature (full name) of from person
 **             $y   the tty id of our terminal
 **             $z   home directory of to person
 **             $x   signature (full name) of from person
 **             $y   the tty id of our terminal
 **             $z   home directory of to person
@@ -187,19 +190,18 @@ expand2(s, buf, buflim, e)
 **             are available.
 */
 
 **             are available.
 */
 
-define(n, v)
+define(n, v, e)
        char n;
        char *v;
        char n;
        char *v;
+       register ENVELOPE *e;
 {
 {
-# ifdef DEBUG
-       if (tTd(35, 3))
+       if (tTd(35, 9))
        {
                printf("define(%c as ", n);
                xputs(v);
                printf(")\n");
        }
        {
                printf("define(%c as ", n);
                xputs(v);
                printf(")\n");
        }
-# endif DEBUG
-       CurEnv->e_macro[n & 0177] = v;
+       e->e_macro[n & 0177] = v;
 }
 \f/*
 **  MACVALUE -- return uninterpreted value of a macro.
 }
 \f/*
 **  MACVALUE -- return uninterpreted value of a macro.
@@ -215,8 +217,18 @@ define(n, v)
 */
 
 char *
 */
 
 char *
-macvalue(n)
+macvalue(n, e)
        char n;
        char n;
+       register ENVELOPE *e;
 {
 {
-       return (CurEnv->e_macro[n & 0177]);
+       n &= 0177;
+       while (e != NULL)
+       {
+               register char *p = e->e_macro[n];
+
+               if (p != NULL)
+                       return (p);
+               e = e->e_parent;
+       }
+       return (NULL);
 }
 }