make m_getfld2() called with an error state a noop
[mmh] / sbr / m_getfld2.c
index 6d96bdd..970a3f0 100644 (file)
@@ -23,6 +23,7 @@ enum threestate {
 */
 static enum threestate is_falted(FILE *);
 static size_t copyname(char *, char *);
+static int is_separator(char *);
 
 
 /*
@@ -47,29 +48,29 @@ m_getfld2(enum state s, struct field *f, FILE *msg)
        ssize_t nchars;
        enum threestate falted;
 
-       nchars = getline(&tmpline, &len, msg);
-       if (nchars == -1) {
-               if (feof(msg)) {
-                       return FILEEOF2;
-               } else {
-                       return IOERR2;
+       switch (s) {
+       case FLD2:
+               nchars = getline(&tmpline, &len, msg);
+               if (nchars < 1) {
+                       if (feof(msg)) {
+                               return FILEEOF2;
+                       } else {
+                               return IOERR2;
+                       }
                }
-       }
 
-       *f->name = '\0';
-       f->namelen = 0;
+               *f->name = '\0';
+               f->namelen = 0;
 
-       if (nchars >= NAMESZ) {
-               if (f->value) {
-                       free(f->value);
+               if (nchars >= NAMESZ) {
+                       if (f->value) {
+                               free(f->value);
+                       }
+                       f->value = tmpline;
+                       f->valuelen = nchars;
+                       return LENERR2;
                }
-               f->value = tmpline;
-               f->valuelen = nchars;
-               return LENERR2;
-       }
 
-       switch (s) {
-       case FLD2:
                if (*(tmpline + nchars - 1) != '\n') {
                        if (f->value) {
                                free(f->value);
@@ -80,7 +81,7 @@ m_getfld2(enum state s, struct field *f, FILE *msg)
                        return FMTERR2;
                }
 
-               if (nchars > 0 && (*tmpline == '\n' || *tmpline == '-')) {
+               if (is_separator(tmpline)) {
                        /* header/body separator found */
                        free(tmpline);
                        return m_getfld2(BODY2, f, msg);
@@ -150,6 +151,26 @@ m_getfld2(enum state s, struct field *f, FILE *msg)
                return FLD2;
 
        case BODY2:
+               nchars = getline(&tmpline, &len, msg);
+               if (nchars < 1) {
+                       if (feof(msg)) {
+                               return FILEEOF2;
+                       } else {
+                               return IOERR2;
+                       }
+               }
+
+               *f->name = '\0';
+               f->namelen = 0;
+
+               if (nchars >= NAMESZ) {
+                       if (f->value) {
+                               free(f->value);
+                       }
+                       f->value = tmpline;
+                       f->valuelen = nchars;
+                       return LENERR2;
+               }
                if (f->value) {
                        free(f->value);
                }
@@ -215,3 +236,19 @@ copyname(char *dst, char *src)
 
        return strlen(dst);
 }
+
+static int
+is_separator(char *line)
+{
+       /*
+       ** In MH, lines that are consists of dashes only are
+       ** separators as well ... not so in RFC 822.
+       */
+       while (*line == '-') {
+               line++;
+       }
+       if (strcmp("\n", line)==0) {
+               return 1;
+       }
+       return 0;
+}