]> diplodocus.org Git - nmh/blobdiff - sbr/mf.c
Document argsplit changes in mh-profile man page.
[nmh] / sbr / mf.c
index c7959103d7f34ea5db392613ce2c5008d2759800..975e396423ea57a66dd06fce7064230a5493ca86 100644 (file)
--- a/sbr/mf.c
+++ b/sbr/mf.c
@@ -2,18 +2,20 @@
 /*
  * mf.c -- mail filter subroutines
  *
- * $Id$
+ * This code is Copyright (c) 2002, by the authors of nmh.  See the
+ * COPYRIGHT file in the root directory of the nmh distribution for
+ * complete copyright information.
  */
 
 #include <h/mf.h>
 #include <ctype.h>
 #include <stdio.h>
+#include <h/utils.h>
 
 /*
  * static prototypes
  */
 static char *getcpy (char *);
-static char *add (char *, char *);
 static void compress (char *, char *);
 static int isat (char *);
 static int parse_address (void);
@@ -38,26 +40,12 @@ getcpy (char *s)
        for(;;)
            pause();
     }
-    if ((p = malloc ((size_t) (strlen (s) + 2))))
-       strcpy (p, s);
+    p = mh_xmalloc ((size_t) (strlen (s) + 2));
+    strcpy (p, s);
     return p;
 }
 
 
-static char *
-add (char *s1, char *s2)
-{
-    register char *p;
-
-    if (!s2)
-       return getcpy (s1);
-
-    if ((p = malloc ((size_t) (strlen (s1) + strlen (s2) + 2))))
-       sprintf (p, "%s%s", s2, s1);
-    free (s2);
-    return p;
-}
-
 int
 isfrom(char *string)
 {
@@ -73,8 +61,10 @@ lequal (char *a, char *b)
        if (*b == 0)
            return FALSE;
        else {
-           char c1 = islower (*a) ? toupper (*a) : *a;
-           char c2 = islower (*b) ? toupper (*b) : *b;
+           char c1 = islower ((unsigned char) *a) ?
+                                       toupper ((unsigned char) *a) : *a;
+           char c2 = islower ((unsigned char) *b) ?
+                                       toupper ((unsigned char) *b) : *b;
            if (c1 != c2)
                return FALSE;
        }
@@ -143,8 +133,8 @@ seekadrx (char *addrs)
 struct adrx *
 uucpadrx (char *addrs)
 {
-    register char *cp, *wp, *xp, *yp, *zp;
-    register struct adrx *adrxp = &adrxs1;
+    char *cp, *wp, *xp, *yp, *zp;
+    struct adrx *adrxp = &adrxs1;
 
     if (vp == NULL) {
        vp = tp = getcpy (addrs);
@@ -157,7 +147,7 @@ uucpadrx (char *addrs)
            return NULL;
        }
 
-    for (cp = tp; isspace (*cp); cp++)
+    for (cp = tp; isspace ((unsigned char) *cp); cp++)
        continue;
     if (*cp == 0) {
        free (vp);
@@ -168,11 +158,11 @@ uucpadrx (char *addrs)
     if ((wp = strchr(cp, ',')) == NULL) {
        if ((wp = strchr(cp, ' ')) != NULL) {
            xp = wp;
-           while (isspace (*xp))
+           while (isspace ((unsigned char) *xp))
                xp++;
            if (*xp != 0 && isat (--xp)) {
                yp = xp + 4;
-               while (isspace (*yp))
+               while (isspace ((unsigned char) *yp))
                    yp++;
                if (*yp != 0) {
                    if ((zp = strchr(yp, ' ')) != NULL)
@@ -218,10 +208,10 @@ uucpadrx (char *addrs)
 static void
 compress (char *fp, char *tp)
 {
-    register char c, *cp;
+    char c, *cp;
 
     for (c = ' ', cp = tp; (*tp = *fp++) != 0;)
-       if (isspace (*tp)) {
+       if (isspace ((unsigned char) *tp)) {
            if (c != ' ')
                *tp++ = c = ' ';
        }
@@ -426,10 +416,10 @@ getadrx (char *addrs)
            }
            break;
        }
-    while (isspace (*ap))
+    while (isspace ((unsigned char) *ap))
        ap++;
     if (cp)
-       sprintf (adr, "%.*s", cp - ap, ap);
+       sprintf (adr, "%.*s", (int)(cp - ap), ap);
     else
        strcpy (adr, ap);
     bp = adr + strlen (adr) - 1;
@@ -767,8 +757,12 @@ route (char *buffer)
 static int
 my_lex (char *buffer)
 {
+    /* buffer should be at least BUFSIZ bytes long */
     int i, gotat = 0;
-    register char c, *bp;
+    char c, *bp;
+
+/* Add C to the buffer bp. After use of this macro *bp is guaranteed to be within the buffer. */
+#define ADDCHR(C) do { *bp++ = (C); if ((bp - buffer) == (BUFSIZ-1)) goto my_lex_buffull; } while (0)
 
     bp = buffer;
     *bp = 0;
@@ -777,34 +771,35 @@ my_lex (char *buffer)
 
     gotat = isat (cp);
     c = *cp++;
-    while (isspace (c))
+    while (isspace ((unsigned char) c))
        c = *cp++;
     if (c == 0) {
        cp = NULL;
        return (last_lex = LX_END);
     }
 
-    if (c == '(')
-       for (*bp++ = c, i = 0;;)
+    if (c == '(') {
+       ADDCHR(c);
+       for (i = 0;;)
            switch (c = *cp++) {
                case 0: 
                    cp = NULL;
                    return (last_lex = LX_ERR);
                case QUOTE: 
-                   *bp++ = c;
+                   ADDCHR(c);
                    if ((c = *cp++) == 0) {
                        cp = NULL;
                        return (last_lex = LX_ERR);
                    }
-                   *bp++ = c;
+                   ADDCHR(c);
                    continue;
                case '(': 
                    i++;
                default: 
-                   *bp++ = c;
+                   ADDCHR(c);
                    continue;
                case ')': 
-                   *bp++ = c;
+                   ADDCHR(c);
                    if (--i < 0) {
                        *bp = 0;
                        note = note ? add (buffer, add (" ", note))
@@ -812,56 +807,61 @@ my_lex (char *buffer)
                        return my_lex (buffer);
                    }
            }
+    }
 
-    if (c == '"')
-       for (*bp++ = c;;)
+    if (c == '"') {
+       ADDCHR(c);
+       for (;;)
            switch (c = *cp++) {
                case 0: 
                    cp = NULL;
                    return (last_lex = LX_ERR);
                case QUOTE: 
-                   *bp++ = c;
+                   ADDCHR(c);
                    if ((c = *cp++) == 0) {
                        cp = NULL;
                        return (last_lex = LX_ERR);
                    }
                default: 
-                   *bp++ = c;
+                   ADDCHR(c);
                    continue;
                case '"': 
-                   *bp++ = c;
+                   ADDCHR(c);
                    *bp = 0;
                    return (last_lex = LX_QSTR);
            }
-
-    if (c == '[')
-       for (*bp++ = c;;)
+    }
+    
+    if (c == '[') {
+       ADDCHR(c);
+       for (;;)
            switch (c = *cp++) {
                case 0: 
                    cp = NULL;
                    return (last_lex = LX_ERR);
                case QUOTE: 
-                   *bp++ = c;
+                   ADDCHR(c);
                    if ((c = *cp++) == 0) {
                        cp = NULL;
                        return (last_lex = LX_ERR);
                    }
                default: 
-                   *bp++ = c;
+                   ADDCHR(c);
                    continue;
                case ']': 
-                   *bp++ = c;
+                   ADDCHR(c);
                    *bp = 0;
                    return (last_lex = LX_DLIT);
            }
-
-    *bp++ = c;
+    }
+    
+    ADDCHR(c);
     *bp = 0;
     for (i = 0; special[i].lx_chr != 0; i++)
        if (c == special[i].lx_chr)
            return (last_lex = special[i].lx_val);
 
-    if (iscntrl (c))
+    if (iscntrl ((unsigned char) c))
        return (last_lex = LX_ERR);
 
     for (;;) {
@@ -870,9 +870,9 @@ my_lex (char *buffer)
        for (i = 0; special[i].lx_chr != 0; i++)
            if (c == special[i].lx_chr)
                goto got_atom;
-       if (iscntrl (c) || isspace (c))
+       if (iscntrl ((unsigned char) c) || isspace ((unsigned char) c))
            break;
-       *bp++ = c;
+       ADDCHR(c);
     }
 got_atom: ;
     if (c == 0)
@@ -883,6 +883,11 @@ got_atom: ;
     last_lex = !gotat || cp == NULL || strchr(cp, '<') != NULL
        ? LX_ATOM : LX_AT;
     return last_lex;
+
+ my_lex_buffull:
+    /* Out of buffer space. *bp is the last byte in the buffer */
+    *bp = 0;
+    return (last_lex = LX_ERR);
 }
 
 
@@ -915,8 +920,7 @@ mfgets (FILE *in, char **bp)
     static char *pp = NULL;
 
     if (pp == NULL)
-       if (!(pp = malloc ((size_t) (len = BUFSIZ))))
-           return NOTOK;
+       pp = mh_xmalloc ((size_t) (len = BUFSIZ));
 
     for (ep = (cp = pp) + len - 2;;) {
        switch (i = getc (in)) {
@@ -956,13 +960,8 @@ mfgets (FILE *in, char **bp)
                break;
        }
        if (cp >= ep) {
-           if (!(dp = realloc (pp, (size_t) (len += BUFSIZ)))) {
-               free (pp);
-               pp = NULL;
-               return NOTOK;
-           }
-           else
-               cp += dp - pp, ep = (pp = cp) + len - 2;
+           dp = mh_xrealloc (pp, (size_t) (len += BUFSIZ));
+           cp += dp - pp, ep = (pp = cp) + len - 2;
        }
     }
 }