]> diplodocus.org Git - nmh/blobdiff - sbr/m_getfld.c
Removed recommendation from mhstore man page that users not put
[nmh] / sbr / m_getfld.c
index 40ae2289a2d8d40166908c7c274ee8cad74aec6d..d278e5288828917ca1f48e1259f2d5c68c59155b 100644 (file)
@@ -313,7 +313,10 @@ void m_getfld_state_destroy (m_getfld_state_t *gstate) {
     m_getfld_state_t s = *gstate;
 
     if (s) {
-       if (s->fdelim) free (s->fdelim-1);
+       if (s->fdelim) {
+           free (s->fdelim-1);
+           free (s->pat_map);
+       }
        free (s);
        *gstate = 0;
     }
@@ -431,7 +434,7 @@ read_more (m_getfld_state_t s) {
     size_t num_read;
 
     if (retain < s->end - s->readpos) retain = s->end - s->readpos;
-    /* assert (retain <= s->readpos - s->msg_buf <= sizeof msg_buf); */
+    assert (retain <= s->readpos - s->msg_buf);
 
     /* Move what we want to retain at end of the buffer to the beginning. */
     memmove (s->msg_buf, s->readpos - retain, retain);
@@ -443,6 +446,9 @@ read_more (m_getfld_state_t s) {
     return num_read;
 }
 
+/* The return values of the following functions are a bit
+   subtle.  They can return 0x00 - 0xff as a valid character,
+   but EOF is typically 0xffffffff. */
 static int
 Getc (m_getfld_state_t s) {
     if (s->end - s->readpos < 1) {
@@ -454,7 +460,7 @@ Getc (m_getfld_state_t s) {
     }
 
     ++s->bytes_read;
-    return s->readpos < s->end  ?  *s->readpos++  :  EOF;
+    return s->readpos < s->end  ?  (unsigned char) *s->readpos++  :  EOF;
 }
 
 static int
@@ -467,7 +473,7 @@ Peek (m_getfld_state_t s) {
        }
     }
 
-    return s->readpos < s->end  ?  *s->readpos  :  EOF;
+    return s->readpos < s->end  ?  (unsigned char) *s->readpos  :  EOF;
 }
 
 static int
@@ -476,7 +482,7 @@ Ungetc (int c, m_getfld_state_t s) {
        return EOF;
     } else {
        --s->bytes_read;
-       return *--s->readpos = c;
+       return *--s->readpos = (unsigned char) c;
     }
 }
 
@@ -624,12 +630,21 @@ m_getfld (m_getfld_state_t *gstate, char name[NAMESZ], char *buf, int *bufsz,
            n = 0;
            for (finished = 0; ! finished; ) {
                while (c != '\n'  &&  c != EOF  &&  n++ < max) {
-                   *cp++ = c = Getc (s);
+                   if ((c = Getc (s)) != EOF) { *cp++ = c; }
                }
 
                if (c != EOF) c = Peek (s);
                if (max < n) {
-                   /* the dest buffer is full */
+                   /* The dest buffer is full.  Need to back the read
+                      pointer up by one because when m_getfld() is
+                      reentered, it will read a character.  Then
+                      we'll jump right to the FLDPLUS handling code,
+                      which will not store that character, but
+                      instead move on to the next one. */
+                   if (s->readpos > s->msg_buf) {
+                       --s->readpos;
+                       --s->bytes_read;
+                   }
                    s->state = FLDPLUS;
                    finished = 1;
                } else if (c != ' '  &&  c != '\t') {
@@ -796,6 +811,7 @@ m_unknown(m_getfld_state_t *gstate, FILE *iob)
     } else {
        /* not a Unix style maildrop */
        s->readpos -= s->bytes_read;
+       s->bytes_read = 0;
        delimstr = mmdlm2;
        s->msg_style = MS_MMDF;
     }
@@ -880,6 +896,7 @@ m_Eom (m_getfld_state_t s, int c)
           Note that on input, a character had already been read
           with Getc().  It will be unget by m_getfld () on return. */
        s->readpos -= s->bytes_read - 1;
+       s->bytes_read = 1;
        return 0;
     }