]> diplodocus.org Git - nmh/blobdiff - sbr/mf.c
Alter mh-chart(7)'s NAME to be lowercase.
[nmh] / sbr / mf.c
index 37fcf7b955607d1aba08a880bb4dc1cce4755690..a604ce918b94b533a6c52d80ef9d499bf30e8b0a 100644 (file)
--- a/sbr/mf.c
+++ b/sbr/mf.c
@@ -8,15 +8,12 @@
  */
 
 #include <h/mf.h>
-#include <ctype.h>
-#include <stdio.h>
 #include <h/utils.h>
 
 /*
  * static prototypes
  */
-static char *getcpy (char *);
-static int isat (char *);
+static int isat (const char *);
 static int parse_address (void);
 static int phrase (char *);
 static int route_addr (char *);
@@ -26,59 +23,37 @@ static int route (char *);
 static int my_lex (char *);
 
 
-static char *
-getcpy (char *s)
-{
-    register char *p;
-
-    if (!s) {
-/* causes compiles to blow up because the symbol _cleanup is undefined 
-   where did this ever come from? */
-       /* _cleanup(); */
-       abort();
-       for(;;)
-           pause();
-    }
-    p = mh_xmalloc ((size_t) (strlen (s) + 2));
-    strcpy (p, s);
-    return p;
-}
-
-
 int
-isfrom(char *string)
+isfrom(const char *string)
 {
-    return (strncmp (string, "From ", 5) == 0
-           || strncmp (string, ">From ", 6) == 0);
+    return (has_prefix(string, "From ")
+           || has_prefix(string, ">From "));
 }
 
 
 int
-lequal (char *a, char *b)
+lequal (const char *a, const char *b)
 {
-    for (; *a; a++, b++)
+    char c1, c2;
+
+    for (; *a; a++, b++) {
        if (*b == 0)
            return FALSE;
-       else {
-           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;
-       }
+        c1 = toupper((unsigned char)*a);
+        c2 = toupper((unsigned char)*b);
+        if (c1 != c2)
+            return FALSE;
+    }
 
     return (*b == 0);
 }
 
 
 static int
-isat (char *p)
+isat (const char *p)
 {
-    return (strncmp (p, " AT ", 4)
-           && strncmp (p, " At ", 4)
-           && strncmp (p, " aT ", 4)
-           && strncmp (p, " at ", 4) ? FALSE : TRUE);
+    return has_prefix(p, " AT ") || has_prefix(p, " At ") ||
+        has_prefix(p, " aT ") || has_prefix(p, " at ");
 }
 
 
@@ -87,7 +62,7 @@ isat (char *p)
  * getadrx() implements a partial 822-style address parser.  The parser
  * is neither complete nor correct.  It does however recognize nearly all
  * of the 822 address syntax.  In addition it handles the majority of the
- * 733 syntax as well.  Most problems arise from trying to accomodate both.
+ * 733 syntax as well.  Most problems arise from trying to accommodate both.
  *
  * In terms of 822, the route-specification in 
  *
@@ -196,29 +171,24 @@ static char adr[BUFSIZ];
 static struct adrx  adrxs2;
 
 
+/* eai = Email Address Internationalization */
 struct adrx *
-getadrx (char *addrs)
+getadrx (const char *addrs, int eai)
 {
-    register char *bp;
-    register struct adrx *adrxp = &adrxs2;
-
-    if (pers)
-       free (pers);
-    if (mbox)
-       free (mbox);
-    if (host)
-       free (host);
-    if (path)
-       free (path);
-    if (grp)
-       free (grp);
-    if (note)
-       free (note);
+    char *bp;
+    struct adrx *adrxp = &adrxs2;
+
+    mh_xfree(pers);
+    mh_xfree(mbox);
+    mh_xfree(host);
+    mh_xfree(path);
+    mh_xfree(grp);
+    mh_xfree(note);
     pers = mbox = host = path = grp = note = NULL;
     err[0] = 0;
 
     if (dp == NULL) {
-       dp = cp = getcpy (addrs ? addrs : "");
+       dp = cp = strdup (addrs ? addrs : "");
        glevel = 0;
     }
     else
@@ -252,6 +222,17 @@ getadrx (char *addrs)
            break;
        }
 
+    if (! eai) {
+        /*
+         * Reject the address if key fields contain 8bit characters
+         */
+
+        if (contains8bit(mbox, NULL) || contains8bit(host, NULL) ||
+            contains8bit(path, NULL) || contains8bit(grp, NULL)) {
+            strcpy(err, "Address contains 8-bit characters");
+        }
+    }
+
     if (err[0])
        for (;;) {
            switch (last_lex) {
@@ -268,7 +249,7 @@ getadrx (char *addrs)
     while (isspace ((unsigned char) *ap))
        ap++;
     if (cp)
-       sprintf (adr, "%.*s", (int)(cp - ap), ap);
+       snprintf(adr, sizeof adr, "%.*s", (int)(cp - ap), ap);
     else
        strcpy (adr, ap);
     bp = adr + strlen (adr) - 1;
@@ -299,7 +280,7 @@ again: ;
     switch (my_lex (buffer)) {
        case LX_ATOM: 
        case LX_QSTR: 
-           pers = getcpy (buffer);
+           pers = strdup (buffer);
            break;
 
        case LX_SEMI: 
@@ -307,11 +288,10 @@ again: ;
                strcpy (err, "extraneous semi-colon");
                return NOTOK;
            }
+           /* FALLTHRU */
        case LX_COMA: 
-           if (note) {
-               free (note);
-               note = NULL;
-           }
+            mh_xfree(note);
+            note = NULL;
            goto again;
 
        case LX_END: 
@@ -327,7 +307,7 @@ again: ;
            return OK;          /* why be choosy? */
 
        default: 
-           sprintf (err, "illegal address construct (%s)", buffer);
+           snprintf(err, sizeof err, "illegal address construct (%s)", buffer);
            return NOTOK;
     }
 
@@ -346,13 +326,13 @@ again: ;
                        return NOTOK;
                    if (last_lex == LX_RBRK)
                        return OK;
-                   sprintf (err, "missing right-bracket (%s)", buffer);
+                   snprintf(err, sizeof err, "missing right-bracket (%s)", buffer);
                    return NOTOK;
 
                case LX_COLN: 
            get_group: ;
                    if (glevel++ > 0) {
-                       sprintf (err, "nested groups not allowed (%s)", pers);
+                       snprintf(err, sizeof err, "nested groups not allowed (%s)", pers);
                        return NOTOK;
                    }
                    grp = add (": ", pers);
@@ -381,7 +361,7 @@ again: ;
                    goto more_phrase;
 
                default: 
-                   sprintf (err, "no mailbox in address, only a phrase (%s%s)",
+                   snprintf(err, sizeof err, "no mailbox in address, only a phrase (%s%s)",
                            pers, buffer);
                    return NOTOK;
            }
@@ -412,12 +392,13 @@ again: ;
                        strcpy (err, "extraneous semi-colon");
                        return NOTOK;
                    }
+                   /* FALLTHRU */
                case LX_COMA: 
                case LX_END: 
                    return OK;
 
                default: 
-                   sprintf (err, "junk after local@domain (%s)", buffer);
+                   snprintf(err, sizeof err, "junk after local@domain (%s)", buffer);
                    return NOTOK;
            }
 
@@ -434,7 +415,7 @@ again: ;
            return OK;
 
        default: 
-           sprintf (err, "missing mailbox (%s)", buffer);
+           snprintf(err, sizeof err, "missing mailbox (%s)", buffer);
            return NOTOK;
     }
 }
@@ -459,7 +440,7 @@ phrase (char *buffer)
 static int
 route_addr (char *buffer)
 {
-    register char *pp = cp;
+    char *pp = cp;
 
     if (my_lex (buffer) == LX_AT) {
        if (route (buffer) == NOTOK)
@@ -482,7 +463,7 @@ route_addr (char *buffer)
            return OK;
 
        default: 
-           sprintf (err, "no at-sign after local-part (%s)", buffer);
+           snprintf(err, sizeof err, "no at-sign after local-part (%s)", buffer);
            return NOTOK;
     }
 }
@@ -501,7 +482,7 @@ local_part (char *buffer)
                break;
 
            default: 
-               sprintf (err, "no mailbox in local-part (%s)", buffer);
+               snprintf(err, sizeof err, "no mailbox in local-part (%s)", buffer);
                return NOTOK;
        }
 
@@ -528,7 +509,7 @@ domain (char *buffer)
                break;
 
            default: 
-               sprintf (err, "no sub-domain in domain-part of address (%s)", buffer);
+               snprintf(err, sizeof err, "no sub-domain in domain-part of address (%s)", buffer);
                return NOTOK;
        }
 
@@ -553,7 +534,7 @@ domain (char *buffer)
 static int
 route (char *buffer)
 {
-    path = getcpy ("@");
+    path = strdup ("@");
 
     for (;;) {
        switch (my_lex (buffer)) {
@@ -563,7 +544,7 @@ route (char *buffer)
                break;
 
            default: 
-               sprintf (err, "no sub-domain in domain-part of address (%s)", buffer);
+               snprintf(err, sizeof err, "no sub-domain in domain-part of address (%s)", buffer);
                return NOTOK;
        }
        switch (my_lex (buffer)) {
@@ -579,7 +560,7 @@ route (char *buffer)
                            break;
 
                        default: 
-                           sprintf (err, "no at-sign found for next domain in route (%s)",
+                           snprintf(err, sizeof err, "no at-sign found for next domain in route (%s)",
                                     buffer);
                    }
                    break;
@@ -596,7 +577,7 @@ route (char *buffer)
                return OK;
 
            default: 
-               sprintf (err, "no colon found to terminate route (%s)", buffer);
+               snprintf(err, sizeof err, "no colon found to terminate route (%s)", buffer);
                return NOTOK;
        }
     }
@@ -644,6 +625,7 @@ my_lex (char *buffer)
                    continue;
                case '(': 
                    i++;
+                   /* FALLTHRU */
                default: 
                    ADDCHR(c);
                    continue;
@@ -652,7 +634,7 @@ my_lex (char *buffer)
                    if (--i < 0) {
                        *bp = 0;
                        note = note ? add (buffer, add (" ", note))
-                           : getcpy (buffer);
+                           : strdup (buffer);
                        return my_lex (buffer);
                    }
            }
@@ -671,6 +653,7 @@ my_lex (char *buffer)
                        cp = NULL;
                        return (last_lex = LX_ERR);
                    }
+                   /* FALLTHRU */
                default: 
                    ADDCHR(c);
                    continue;
@@ -694,6 +677,7 @@ my_lex (char *buffer)
                        cp = NULL;
                        return (last_lex = LX_ERR);
                    }
+                   /* FALLTHRU */
                default: 
                    ADDCHR(c);
                    continue;
@@ -741,22 +725,22 @@ got_atom: ;
 
 
 char *
-legal_person (char *p)
+legal_person (const char *p)
 {
     int i;
-    register char *cp;
+    const char *cp;
     static char buffer[BUFSIZ];
 
     if (*p == '"')
-       return p;
+       return (char *) p;
     for (cp = p; *cp; cp++)
        for (i = 0; special[i].lx_chr; i++)
            if (*cp == special[i].lx_chr) {
-               sprintf (buffer, "\"%s\"", p);
+               snprintf(buffer, sizeof buffer, "\"%s\"", p);
                return buffer;
            }
 
-    return p;
+    return (char *) p;
 }
 
 
@@ -764,7 +748,7 @@ int
 mfgets (FILE *in, char **bp)
 {
     int i;
-    register char *cp, *dp, *ep;
+    char *cp, *dp, *ep;
     static int len = 0;
     static char *pp = NULL;
 
@@ -802,7 +786,8 @@ mfgets (FILE *in, char **bp)
                    case '\t': 
                        *cp++ = '\n';
                        break;
-               }               /* fall into default case */
+               }
+               /* FALLTHRU */
 
            default: 
                *cp++ = i;