]> diplodocus.org Git - nmh/blobdiff - sbr/m_getfld.c
Remove trailing spaces from lines in man pages.
[nmh] / sbr / m_getfld.c
index b4019b0cf7ccc4d31360dffba631c04872d0f725..4976b1c5faff005470361f560638a662a6bbde22 100644 (file)
  */
 struct m_getfld_state;
 static int m_Eom (m_getfld_state_t);
-static char *matchc(int, char *, int, char *);
 
 #define eom(c,s)       (s->msg_style != MS_DEFAULT && \
                         ((c) == *s->msg_delim && m_Eom(s)))
@@ -433,7 +432,8 @@ read_more (m_getfld_state_t s) {
     ssize_t retain = s->edelimlen;
     size_t num_read;
 
-    if (retain < s->end - s->readpos) retain = s->end - s->readpos;
+    if (retain < s->end - s->readpos)
+        retain = s->end - s->readpos;
     assert (retain <= s->readpos - s->msg_buf);
 
     /* Move what we want to retain at end of the buffer to the beginning. */
@@ -606,7 +606,7 @@ m_getfld (m_getfld_state_t *gstate, char name[NAMESZ], char *buf, int *bufsz,
            while (isspace ((unsigned char) *--cp) && cp >= name) continue;
            *++cp = 0;
            /* readpos points to the first character of the field body. */
-           /* fall through */
+           /* FALLTHRU */
 
        case FLDPLUS: {
            /*
@@ -621,10 +621,12 @@ 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) {
-                   if ((c = Getc (s)) != EOF) { *cp++ = c; }
+                   if ((c = Getc (s)) != EOF)
+                        *cp++ = c;
                }
 
-               if (c != EOF) c = Peek (s);
+               if (c != EOF)
+                    c = Peek (s);
                if (max < n) {
                    /* The dest buffer is full.  Need to back the read
                       pointer up by one because when m_getfld() is
@@ -681,7 +683,7 @@ m_getfld (m_getfld_state_t *gstate, char name[NAMESZ], char *buf, int *bufsz,
                 */
                char *ep;
 
-               if ((ep = matchc( s->fdelimlen, s->fdelim, c, bp )))
+                if ((ep = memmem(bp, c, s->fdelim, s->fdelimlen)))
                    c = ep - bp + 1;
                else {
                    /*
@@ -811,18 +813,32 @@ m_unknown(m_getfld_state_t *gstate, FILE *iob)
        delimstr = mmdlm2;
        s->msg_style = MS_MMDF;
     }
+
     c = strlen (delimstr);
-    s->fdelim = mh_xmalloc (c + 3);
+    s->fdelim = mh_xmalloc (c + 3); /* \0, \n, delimstr, \0 */
     *s->fdelim++ = '\0';
     *s->fdelim = '\n';
+    s->fdelimlen = c + 1;
     s->msg_delim = s->fdelim+1;
-    s->edelim = s->msg_delim+1;
-    s->fdelimlen = c;
-    s->edelimlen = c - 1; /* == strlen (delimstr) */
     strcpy (s->msg_delim, delimstr);
+    s->edelim = s->msg_delim+1;
+    s->edelimlen = c - 1;
     s->delimend = s->msg_delim + s->edelimlen;
     if (s->edelimlen <= 1)
        adios (NULL, "maildrop delimiter must be at least 2 bytes");
+
+    /* Now malloc'd memory at s->fdelim-1 referenced several times:
+     *
+     *     delimstr         "\nFrom "      "\001\001\001\001\n"
+     *     c                6              5
+     *     s->fdelim      \0"\n\nFrom "  \0"\n\001\001\001\001\n"
+     *     s->fdelimlen     6              5
+     *     s->msg_delim     "\nFrom "      "\001\001\001\001\n"
+     *     s->edelim        "From "        "\001\001\001\n"
+     *     s->edelimlen     5              4
+     *     s->delimend      " "            "\n"
+     */
+
     /*
      * build a Boyer-Moore end-position map for the matcher in m_getfld.
      * N.B. - we don't match just the first char (since it's the newline
@@ -892,27 +908,3 @@ m_Eom (m_getfld_state_t s)
 
     return 1;
 }
-
-
-static char *
-matchc(int patln, char *pat, int strln, char *str)
-{
-       char *es = str + strln - patln;
-       char *sp;
-       char *pp;
-       char *ep = pat + patln;
-       char pc = *pat++;
-
-       for(;;) {
-               while (pc != *str++)
-                       if (str > es)
-                               return 0;
-               if (str > es+1)
-                       return 0;
-               sp = str; pp = pat;
-               while (pp < ep && *sp++ == *pp)
-                       pp++;
-               if (pp >= ep)
-                       return --str;
-       }
-}