]> Pileus Git - ~andy/fetchmail/blobdiff - rfc822.c
Minor bug fixes for socket.c
[~andy/fetchmail] / rfc822.c
index 0fdc6a5ab4f1d73c794f973fa0399de29aacd097..15b88f05ef476312af280de1d2ec7183ed4768db 100644 (file)
--- a/rfc822.c
+++ b/rfc822.c
@@ -19,19 +19,24 @@ is part of fetchmail and the Unix Cookbook, and are released under the
 MIT license.  Compile with -DMAIN to build the demonstrator.
 
 ******************************************************************************/
+
+#include "config.h"
+
 #include  <stdio.h>
 #include  <ctype.h>
 #include  <string.h>
+#include  <strings.h>
 #include  <stdlib.h>
 
 #include "fetchmail.h"
+#include "sdump.h"
 
 #ifndef MAIN
 #include "i18n.h"
 #else
 #include  <unistd.h>
 static int verbose;
-char *program_name = "rfc822";
+const char *program_name = "rfc822";
 #endif /* MAIN */
 
 #ifndef TRUE
@@ -41,13 +46,15 @@ char *program_name = "rfc822";
 
 #define HEADER_END(p)  ((p)[0] == '\n' && ((p)[1] != ' ' && (p)[1] != '\t'))
 
-unsigned char *reply_hack(
-       unsigned char *buf              /* header to be hacked */,
-       const unsigned char *host       /* server hostname */,
+#define BEFORE_EOL(s)  (strcspn((s), "\r\n"))
+
+char *reply_hack(
+       char *buf               /* header to be hacked */,
+       const char *host        /* server hostname */,
        size_t *length)
 /* hack message headers so replies will work properly */
 {
-    unsigned char *from, *cp, last_nws = '\0', *parens_from = NULL;
+    char *from, *cp, last_nws = '\0', *parens_from = NULL;
     int parendepth, state, has_bare_name_part, has_host_part;
 #ifndef MAIN
     int addresscount = 1;
@@ -72,14 +79,16 @@ unsigned char *reply_hack(
     }
 
 #ifndef MAIN
-    if (outlevel >= O_DEBUG)
-       report_build(stdout, GT_("About to rewrite %s"), buf);
+    if (outlevel >= O_DEBUG) {
+       report_build(stdout, GT_("About to rewrite %s...\n"), (cp = sdump(buf, BEFORE_EOL(buf))));
+       xfree(cp);
+    }
 
     /* make room to hack the address; buf must be malloced */
     for (cp = buf; *cp; cp++)
-       if (*cp == ',' || isspace(*cp))
+       if (*cp == ',' || isspace((unsigned char)*cp))
            addresscount++;
-    buf = (unsigned char *)xrealloc(buf, strlen(buf) + addresscount * (strlen(host) + 1) + 1);
+    buf = (char *)xrealloc(buf, strlen(buf) + addresscount * (strlen(host) + 1) + 1);
 #endif /* MAIN */
 
     /*
@@ -95,8 +104,8 @@ unsigned char *reply_hack(
 #ifdef MAIN
        if (verbose)
        {
-           printf("state %d: %s", state, (char *)buf);
-           printf("%*s^\n", from - buf + 10, " ");
+           printf("state %d: %s", state, buf);
+           printf("%*s^\n", (int)(from - buf + 10), " ");
        }
 #endif /* MAIN */
        if (state != 2)
@@ -116,7 +125,7 @@ unsigned char *reply_hack(
                break;
 
            case 1:     /* we've seen the colon, we're looking for addresses */
-               if (!isspace(*from))
+               if (!isspace((unsigned char)*from))
                    last_nws = *from;
                if (*from == '<')
                    state = 3;
@@ -135,12 +144,12 @@ unsigned char *reply_hack(
                         && last_nws != ';')
                {
                    int hostlen;
-                   unsigned char *p;
+                   char *p;
 
                    p = from;
                    if (parens_from)
                        from = parens_from;
-                   while (isspace(*from) || (*from == ','))
+                   while (isspace((unsigned char)*from) || (*from == ','))
                        --from;
                    from++;
                    hostlen = strlen(host);
@@ -158,7 +167,7 @@ unsigned char *reply_hack(
                {
                    parens_from = from;
                } 
-               else if (!isspace(*from))
+               else if (!isspace((unsigned char)*from))
                    has_bare_name_part = TRUE;
                break;
 
@@ -208,22 +217,26 @@ unsigned char *reply_hack(
     }
 
 #ifndef MAIN
-    if (outlevel >= O_DEBUG)
-       report_complete(stdout, GT_("Rewritten version is %s\n"), buf);
+    if (outlevel >= O_DEBUG) {
+       report_complete(stdout, GT_("...rewritten version is %s.\n"),
+                       (cp = sdump(buf, BEFORE_EOL(buf))));
+       xfree(cp)
+    }
+
 #endif /* MAIN */
     *length = strlen(buf);
     return(buf);
 }
 
-unsigned char *nxtaddr(const unsigned char *hdr /* header to be parsed, NUL to continue previous hdr */)
+char *nxtaddr(const char *hdr /* header to be parsed, NUL to continue previous hdr */)
 /* parse addresses in succession out of a specified RFC822 header */
 {
-    static unsigned char address[BUFSIZ];
-    static int tp;
-    static const unsigned char *hp;
+    static char address[BUFSIZ];
+    static size_t tp;
+    static const char *hp;
     static int state, oldstate;
 #ifdef MAIN
-    static const unsigned char *orighdr;
+    static const char *orighdr;
 #endif /* MAIN */
     int parendepth = 0;
 
@@ -247,13 +260,15 @@ unsigned char *nxtaddr(const unsigned char *hdr /* header to be parsed, NUL to c
        tp = 0;
     }
 
+    if (!hp) return NULL;
+
     for (; *hp; hp++)
     {
 #ifdef MAIN
        if (verbose)
        {
-           printf("state %d: %s", state, (char *)orighdr);
-           printf("%*s^\n", hp - orighdr + 10, " ");
+           printf("state %d: %s", state, orighdr);
+           printf("%*s^\n", (int)(hp - orighdr + 10), " ");
        }
 #endif /* MAIN */
 
@@ -264,14 +279,13 @@ unsigned char *nxtaddr(const unsigned char *hdr /* header to be parsed, NUL to c
            state = ENDIT_ALL;
            if (tp)
            {
-               --tp;
-               while (tp >= 0 && isspace(address[tp--]))
-                   continue;
-               address[++tp] = '\0';
+               while (tp > 0 && isspace((unsigned char)address[tp - 1]))
+                   tp--;
+               address[tp] = '\0';
                tp = 0;
                return (address);
            }
-           return((unsigned char *)NULL);
+           return(NULL);
        }
        else if (*hp == '\\')           /* handle RFC822 escaping */
        {
@@ -306,7 +320,7 @@ unsigned char *nxtaddr(const unsigned char *hdr /* header to be parsed, NUL to c
                state = INSIDE_BRACKETS;
                tp = 0;
            }
-           else if (*hp != ',' && !isspace(*hp))
+           else if (*hp != ',' && !isspace((unsigned char)*hp))
            {
                --hp;
                state = BARE_ADDRESS;
@@ -341,18 +355,14 @@ unsigned char *nxtaddr(const unsigned char *hdr /* header to be parsed, NUL to c
                state = INSIDE_DQUOTE;
                 address[NEXTTP()] = *hp;
             }
-           else if (!isspace(*hp))     /* just take it, ignoring whitespace */
+           else if (!isspace((unsigned char)*hp))      /* just take it, ignoring whitespace */
                address[NEXTTP()] = *hp;
            break;
 
        case INSIDE_DQUOTE:     /* we're in a quoted string, copy verbatim */
-           if (*hp != '"')
-               address[NEXTTP()] = *hp;
-           else
-           {
-               address[NEXTTP()] = *hp;
+           address[NEXTTP()] = *hp;
+           if (*hp == '"')
                state = oldstate;
-           }
            break;
 
        case INSIDE_PARENS:     /* we're in a parenthesized comment, ignore */
@@ -391,10 +401,10 @@ unsigned char *nxtaddr(const unsigned char *hdr /* header to be parsed, NUL to c
 }
 
 #ifdef MAIN
-static void parsebuf(unsigned char *longbuf, int reply)
+static void parsebuf(char *longbuf, int reply)
 {
-    unsigned char      *cp;
-    size_t             dummy;
+    char       *cp;
+    size_t     dummy;
 
     if (reply)
     {
@@ -402,19 +412,19 @@ static void parsebuf(unsigned char *longbuf, int reply)
        printf("Rewritten buffer: %s", (char *)longbuf);
     }
     else
-       if ((cp = nxtaddr(longbuf)) != (unsigned char *)NULL)
+       if ((cp = nxtaddr(longbuf)) != (char *)NULL)
            do {
                printf("\t-> \"%s\"\n", (char *)cp);
            } while
-               ((cp = nxtaddr((unsigned char *)NULL)) != (unsigned char *)NULL);
+               ((cp = nxtaddr((char *)NULL)) != (char *)NULL);
 }
 
 
 
 int main(int argc, char *argv[])
 {
-    unsigned char      buf[BUFSIZ], longbuf[BUFSIZ];
-    int                        ch, reply;
+    char       buf[BUFSIZ], longbuf[BUFSIZ];
+    int                ch, reply;
     
     verbose = reply = FALSE;
     while ((ch = getopt(argc, argv, "rv")) != EOF)
@@ -429,6 +439,8 @@ int main(int argc, char *argv[])
            break;
        }
 
+    longbuf[0] = '\0';
+
     while (fgets(buf, sizeof(buf)-1, stdin))
     {
        if (buf[0] == ' ' || buf[0] == '\t')