BSD 4_3_Net_2 release
[unix-history] / usr / src / usr.sbin / sendmail / src / macro.c
index 88e0f4f..da35b46 100644 (file)
@@ -1,7 +1,42 @@
-# include "sendmail.h"
-# include "conf.h"
+/*
+ * Copyright (c) 1983 Eric P. Allman
+ * Copyright (c) 1988 Regents of the University of California.
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ *    notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ *    notice, this list of conditions and the following disclaimer in the
+ *    documentation and/or other materials provided with the distribution.
+ * 3. All advertising materials mentioning features or use of this software
+ *    must display the following acknowledgement:
+ *     This product includes software developed by the University of
+ *     California, Berkeley and its contributors.
+ * 4. Neither the name of the University nor the names of its contributors
+ *    may be used to endorse or promote products derived from this software
+ *    without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
+ * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
+ * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
+ * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
+ * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
+ * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
+ * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
+ * SUCH DAMAGE.
+ */
+
+#ifndef lint
+static char sccsid[] = "@(#)macro.c    5.7 (Berkeley) 6/1/90";
+#endif /* not lint */
 
 
-SCCSID(@(#)macro.c     3.18            %G%);
+# include "sendmail.h"
 
 /*
 **  EXPAND -- macro expand a string using $x escapes.
 
 /*
 **  EXPAND -- macro expand a string using $x escapes.
@@ -18,11 +53,6 @@ SCCSID(@(#)macro.c   3.18            %G%);
 **
 **     Side Effects:
 **             none.
 **
 **     Side Effects:
 **             none.
-**
-**     Bugs:
-**             The handling of $$ (to get one dollar) is rather bizarre,
-**                     especially if there should be another macro
-**                     expansion in the same string.
 */
 
 expand(s, buf, buflim, e)
 */
 
 expand(s, buf, buflim, e)
@@ -31,26 +61,25 @@ expand(s, buf, buflim, e)
        char *buflim;
        register ENVELOPE *e;
 {
        char *buflim;
        register ENVELOPE *e;
 {
+       register char *xp;
        register char *q;
        bool skipping;          /* set if conditionally skipping output */
        register char *q;
        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];
        char xbuf[BUFSIZ];
-       register char *xp = xbuf;
        extern char *macvalue();
 
        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;
        if (s == NULL)
                s = "";
 
        skipping = FALSE;
        if (s == NULL)
                s = "";
-       for (; *s != '\0'; s++)
+       for (xp = xbuf; *s != '\0'; s++)
        {
                char c;
 
        {
                char c;
 
@@ -76,14 +105,11 @@ expand(s, buf, buflim, e)
                        skipping = FALSE;
                        continue;
 
                        skipping = FALSE;
                        continue;
 
-                 case '$':             /* macro interpolation */
+                 case '\001':          /* macro interpolation */
                        c = *++s;
                        c = *++s;
-                       if (c == '$')
-                               break;
                        q = macvalue(c & 0177, e);
                        if (q == NULL)
                                continue;
                        q = macvalue(c & 0177, e);
                        if (q == NULL)
                                continue;
-                       gotone = TRUE;
                        break;
                }
 
                        break;
                }
 
@@ -91,42 +117,43 @@ expand(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 == '\0')
-                               break;
-                       *xp++ = *q++;
                }
        }
        *xp = '\0';
 
                }
        }
        *xp = '\0';
 
-# ifdef DEBUG
-       if (tTd(35, 4))
+       if (tTd(35, 24))
        {
                printf("expand ==> ");
                xputs(xbuf);
                printf("\n");
        }
        {
                printf("expand ==> ");
                xputs(xbuf);
                printf("\n");
        }
-# endif DEBUG
 
        /* recurse as appropriate */
 
        /* recurse as appropriate */
-       if (gotone)
+       if (recurse)
        {
                expand(xbuf, buf, buflim, e);
                return;
        }
 
        /* copy results out */
        {
                expand(xbuf, buf, buflim, e);
                return;
        }
 
        /* copy results out */
-       for (q = buf, xp = xbuf; xp != '\0' && q < buflim-1; )
-               *q++ = *xp++;
-       *q = '\0';
+       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.
@@ -155,6 +182,7 @@ expand(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
@@ -172,6 +200,7 @@ expand(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
@@ -191,14 +220,12 @@ define(n, v, e)
        char *v;
        register ENVELOPE *e;
 {
        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
        e->e_macro[n & 0177] = v;
 }
 \f/*
        e->e_macro[n & 0177] = v;
 }
 \f/*