Changed msg_style and msg_delim to be file static to m_getfld.c
[mmh] / uip / burst.c
index f9dd951..93fd5e0 100644 (file)
@@ -2,14 +2,13 @@
 /*
  * burst.c -- explode digests into individual messages
  *
- * $Id$
- *
  * This code is Copyright (c) 2002, by the authors of nmh.  See the
  * COPYRIGHT file in the root directory of the nmh distribution for
  * complete copyright information.
  */
 
 #include <h/mh.h>
+#include <h/utils.h>
 
 static struct swit switches[] = {
 #define        INPLSW  0
@@ -31,11 +30,9 @@ static struct swit switches[] = {
     { NULL, 0 }
 };
 
-static char delim3[] = "-------";
-
 struct smsg {
-    long s_start;
-    long s_stop;
+    off_t s_start;
+    off_t s_stop;
 };
 
 /*
@@ -45,14 +42,25 @@ static int find_delim (int, struct smsg *);
 static void burst (struct msgs **, int, struct smsg *, int, int, int, char *);
 static void cpybrst (FILE *, FILE *, char *, char *, int);
 
+/*
+ * A macro to check to see if we have reached a message delimiter
+ * (an encapsulation boundary, EB, in RFC 934 parlance).
+ *
+ * According to RFC 934, an EB is simply a line which starts with
+ * a "-" and is NOT followed by a space.  So even a single "-" on a line
+ * by itself would be an EB.
+ */
+
+#define CHECKDELIM(buffer) (buffer[0] == '-' && buffer[1] != ' ')
 
 int
 main (int argc, char **argv)
 {
     int inplace = 0, quietsw = 0, verbosw = 0;
-    int msgp = 0, hi, msgnum, numburst;
+    int hi, msgnum, numburst;
     char *cp, *maildir, *folder = NULL, buf[BUFSIZ];
-    char **argp, **arguments, *msgs[MAXARGS];
+    char **argp, **arguments;
+    struct msgs_array msgs = { 0, 0, NULL };
     struct smsg *smsgs;
     struct msgs *mp;
 
@@ -80,10 +88,10 @@ main (int argc, char **argv)
                snprintf (buf, sizeof(buf), "%s [+folder] [msgs] [switches]",
                        invo_name);
                print_help (buf, switches, 1);
-               done (1);
+               done (0);
            case VERSIONSW:
                print_version(invo_name);
-               done (1);
+               done (0);
 
            case INPLSW: 
                inplace++;
@@ -113,14 +121,14 @@ main (int argc, char **argv)
            else
                folder = pluspath (cp);
        } else {
-           msgs[msgp++] = cp;
+           app_msgarg(&msgs, cp);
        }
     }
 
     if (!context_find ("path"))
        free (path ("./", TFOLDER));
-    if (!msgp)
-       msgs[msgp++] = "cur";
+    if (!msgs.size)
+       app_msgarg(&msgs, "cur");
     if (!folder)
        folder = getfolder (1);
     maildir = m_maildir (folder);
@@ -137,8 +145,8 @@ main (int argc, char **argv)
        adios (NULL, "no messages in %s", folder);
 
     /* parse all the message ranges/sequences and set SELECTED */
-    for (msgnum = 0; msgnum < msgp; msgnum++)
-       if (!m_convert (mp, msgs[msgnum]))
+    for (msgnum = 0; msgnum < msgs.size; msgnum++)
+       if (!m_convert (mp, msgs.msgs[msgnum]))
            done (1);
     seq_setprev (mp);  /* set the previous-sequence */
 
@@ -202,38 +210,56 @@ main (int argc, char **argv)
 static int
 find_delim (int msgnum, struct smsg *smsgs)
 {
-    int ld3, wasdlm, msgp;
-    long pos;
+    int wasdlm = 0, msgp;
+    off_t pos;
     char c, *msgnam;
-    int cc;
     char buffer[BUFSIZ];
     FILE *in;
 
-    ld3 = strlen (delim3);
-
     if ((in = fopen (msgnam = m_name (msgnum), "r")) == NULL)
        adios (msgnam, "unable to read message");
 
     for (msgp = 0, pos = 0L; msgp <= MAXFOLDER;) {
+       /*
+        * We're either at the beginning of the whole message, or
+        * we're just past the delimiter of the last message.
+        * Swallow lines until we get to something that's not a newline
+        */
        while (fgets (buffer, sizeof(buffer), in) && buffer[0] == '\n')
            pos += (long) strlen (buffer);
        if (feof (in))
            break;
-       fseek (in, pos, SEEK_SET);
+
+       /*
+        * Reset to the beginning of the last non-blank line, and save our
+        * starting position.  This is where the encapsulated message
+        * starts.
+        */
+       fseeko (in, pos, SEEK_SET);
        smsgs[msgp].s_start = pos;
 
+       /*
+        * Read in lines until we get to a message delimiter.
+        *
+        * Previously we checked to make sure the preceeding line and
+        * next line was a newline.  That actually does not comply with
+        * RFC 934, so make sure we break on a message delimiter even
+        * if the previous character was NOT a newline.
+        */
        for (c = 0; fgets (buffer, sizeof(buffer), in); c = buffer[0]) {
-           if (strncmp (buffer, delim3, ld3) == 0
-                   && (msgp == 1 || c == '\n')
-                   && ((cc = peekc (in)) == '\n' || cc == EOF))
+           if ((wasdlm = CHECKDELIM(buffer)))
                break;
            else
                pos += (long) strlen (buffer);
        }
 
-       wasdlm = strncmp (buffer, delim3, ld3) == 0;
-       if (smsgs[msgp].s_start != pos)
+       /*
+        * Only count as a new message if we got the message delimiter.
+        * Swallow a blank line if it was right before the message delimiter.
+        */
+       if (smsgs[msgp].s_start != pos && wasdlm)
            smsgs[msgp++].s_stop = (c == '\n' && wasdlm) ? pos - 1 : pos;
+
        if (feof (in)) {
 #if 0
            if (wasdlm) {
@@ -247,7 +273,7 @@ find_delim (int msgnum, struct smsg *smsgs)
     }
 
     fclose (in);
-    return (msgp - 1);         /* toss "End of XXX Digest" */
+    return (msgp - 1);         /* return the number of messages burst */
 }
 
 
@@ -269,7 +295,8 @@ burst (struct msgs **mpp, int msgnum, struct smsg *smsgs, int numburst,
     if ((in = fopen (msgnam = m_name (msgnum), "r")) == NULL)
        adios (msgnam, "unable to read message");
 
-    mode = fstat (fileno(in), &st) != NOTOK ? (st.st_mode & 0777) : m_gmprot();
+    mode =
+      fstat (fileno(in), &st) != NOTOK ? (int) (st.st_mode & 0777) : m_gmprot();
     mp = *mpp;
 
     /*
@@ -353,7 +380,7 @@ burst (struct msgs **mpp, int msgnum, struct smsg *smsgs, int numburst,
            printf ("message %d of digest %d becomes message %d\n", j, msgnum, i);
 
        chmod (f2, mode);
-       fseek (in, smsgs[j].s_start, SEEK_SET);
+       fseeko (in, smsgs[j].s_start, SEEK_SET);
        cpybrst (in, out, msgnam, f2,
                (int) (smsgs[j].s_stop - smsgs[j].s_start));
        fclose (out);