]> diplodocus.org Git - nmh/commitdiff
Merge branch 'm_getfld'
authorDavid Levine <levinedl@acm.org>
Sun, 27 Jan 2013 16:37:54 +0000 (10:37 -0600)
committerDavid Levine <levinedl@acm.org>
Sun, 27 Jan 2013 16:37:54 +0000 (10:37 -0600)
34 files changed:
Makefile.am
configure.ac
h/mh.h
h/prototypes.h
mts/smtp/smtp.c
sbr/discard.c
sbr/m_getfld.c
sbr/m_msgdef.c [deleted file]
sbr/readconfig.c
sbr/seq_read.c
test/bad-input/test-header
uip/distsbr.c
uip/forwsbr.c
uip/inc.c
uip/mhbuildsbr.c
uip/mhcachesbr.c
uip/mhlsbr.c
uip/mhparse.c
uip/msh.c
uip/mshcmds.c
uip/new.c
uip/picksbr.c
uip/post.c
uip/prompter.c
uip/rcvdist.c
uip/rcvtty.c
uip/replsbr.c
uip/scan.c
uip/scansbr.c
uip/sendsbr.c
uip/show.c
uip/slocal.c
uip/sortm.c
uip/whatnowsbr.c

index 39ec846..c1c90d0 100644 (file)
@@ -508,7 +508,7 @@ sbr_libmh_a_SOURCES = sbr/addrsbr.c sbr/ambigsw.c sbr/atooi.c sbr/arglist.c \
                      sbr/showfile.c sbr/signals.c sbr/smatch.c \
                      sbr/snprintb.c sbr/ssequal.c sbr/strcasecmp.c \
                      sbr/strindex.c sbr/trimcpy.c sbr/uprf.c sbr/vfgets.c \
-                     sbr/fmt_def.c sbr/m_msgdef.c sbr/mf.c sbr/utils.c \
+                     sbr/fmt_def.c sbr/mf.c sbr/utils.c \
                      sbr/m_mktemp.c sbr/getansreadline.c config/config.c \
                      config/version.c
 
index d8a51a4..1aea372 100644 (file)
@@ -407,15 +407,6 @@ AC_HEADER_TIOCGWINSZ
 AC_CHECK_HEADERS([fcntl.h ncurses/termcap.h termcap.h langinfo.h \
                   wchar.h wctype.h sys/param.h sys/time.h sys/stream.h])
 
-dnl
-dnl Checks for _IO_write_ptr. A Linuxism used by nmh on linux. We
-dnl really use a whole set of them, but this check should be
-dnl sufficient.
-dnl
-AC_CHECK_HEADER(libio.h, [
-  AC_EGREP_HEADER(_IO_write_ptr, libio.h, [
-    AC_DEFINE(LINUX_STDIO,1,[Use the Linux _IO_*_ptr defines from <libio.h>.]) ]) ])
-
 AC_CHECK_HEADER([sys/ptem.h], AC_DEFINE(WINSIZE_IN_PTEM,1,
   [Define to 1 if `struct winsize' requires <sys/ptem.h>.]),,
 [[#if HAVE_SYS_STREAM_H
diff --git a/h/mh.h b/h/mh.h
index 2cd546b..253af9c 100644 (file)
--- a/h/mh.h
+++ b/h/mh.h
@@ -288,11 +288,12 @@ struct msgs {
 #define FMTERR  (-3)           /* Message Format error             */
 #define FLD      0             /* Field returned                   */
 #define FLDPLUS  1             /* Field returned with more to come */
-#define FLDEOF   2             /* Field returned ending at eom     */
 #define BODY     3             /* Body  returned with more to come */
-#define BODYEOF  4             /* Body  returned ending at eom     */
 #define FILEEOF  5             /* Reached end of input file        */
 
+struct m_getfld_state;
+typedef struct m_getfld_state *m_getfld_state_t;
+
 /*
  * Maildrop styles
  */
@@ -302,10 +303,6 @@ struct msgs {
 #define        MS_MMDF         3       /* string mmdlm2              */
 #define        MS_MSH          4       /* whacko msh                 */
 
-extern int msg_count;          /* m_getfld() indicators */
-extern int msg_style;          /*  .. */
-extern char *msg_delim;                /*  .. */
-
 #define        NOUSE   0               /* draft being re-used */
 
 #define TFOLDER 0              /* path() given a +folder */
index 443479f..28f5d0c 100644 (file)
@@ -76,8 +76,10 @@ int m_atoi (char *);
 char *m_backup (char *);
 int m_convert (struct msgs *, char *);
 char *m_draft (char *, char *, int, int *);
-void m_eomsbr (int (*)(int));
-int m_getfld (int, unsigned char *, unsigned char *, int, FILE *);
+void m_eomsbr (m_getfld_state_t, int (*)(int));
+void m_getfld_state_reset (m_getfld_state_t *);
+void m_getfld_state_destroy (m_getfld_state_t *);
+int m_getfld (m_getfld_state_t *, unsigned char[NAMESZ], unsigned char *, int *, FILE *);
 int m_gmprot (void);
 char *m_maildir (char *);
 char *m_mailpath (char *);
@@ -86,7 +88,7 @@ int m_putenv (char *, char *);
 int m_rand (unsigned char *, size_t);
 char *m_mktemp(const char *, int *, FILE **);
 char *m_mktemp2(const char *, const char *, int *, FILE **);
-void m_unknown(FILE *);
+void m_unknown(m_getfld_state_t *, FILE *);
 int makedir (char *);
 char *message_id (time_t, int);
 char *nmh_getpass(const char *);
@@ -107,6 +109,9 @@ void readconfig (struct node **, FILE *, char *, int);
 int refile (char **, char *);
 void ruserpass(char *, char **, char **);
 int remdir (char *);
+void scan_detect_mbox_style (FILE *);
+void scan_finished ();
+void scan_eom_action (int (*)());
 int seq_addmsg (struct msgs *, char *, int, int, int);
 int seq_addsel (struct msgs *, char *, int, int);
 char *seq_bits (struct msgs *);
index b1d57dd..6fd178b 100755 (executable)
@@ -798,9 +798,19 @@ sm_end (int type)
            if (sm_mts == MTS_SMTP)
                smtalk (SM_QUIT, "QUIT");
            else {
+               /* The SIGPIPE block replaces old calls to discard ().
+                  We're not sure what the discard () calls were for,
+                  maybe to prevent deadlock on old systems.  In any
+                  case, blocking SIGPIPE should be harmless.
+                  Because the file handles are closed below, leave it
+                  blocked. */
+               sigset_t set, oset;
+               sigemptyset (&set);
+               sigaddset (&set, SIGPIPE);
+               sigprocmask (SIG_BLOCK, &set, &oset);
+
                kill (sm_child, SIGKILL);
-               discard (sm_rfp);
-               discard (sm_wfp);
+               sm_child = NOTOK;
            }
            if (type == NOTOK) {
                sm_reply.code = sm_note.code;
@@ -840,9 +850,11 @@ sm_end (int type)
        if (sasl_inbuffer)
            free(sasl_inbuffer);
 #endif /* CYRUS_SASL */
-    } else {
+    } else if (sm_child != NOTOK) {
        status = pidwait (sm_child, OK);
        sm_child = NOTOK;
+    } else {
+       status = OK;
     }
 
     sm_rfp = sm_wfp = NULL;
index a1efa48..83b0bfb 100644 (file)
@@ -20,15 +20,7 @@ discard (FILE *io)
 
     tcflush (fileno(io), TCOFLUSH);
 
-#if defined(_FSTDIO) || defined(__DragonFly__)
-    fpurge (io);
-#else
-# ifdef LINUX_STDIO
-    io->_IO_write_ptr = io->_IO_write_base;
-# else
-    if ((io->_ptr = io->_base))
-       io->_cnt = 0;
-# endif
-#endif
+    /* There used to be an fpurge() here on some platforms, stdio
+       hackery on others.  But it didn't seem necessary. */
 }
 
index e139505..4a51226 100644 (file)
 #include <h/mts.h>
 #include <h/utils.h>
 
+/*
+   Purpose
+   =======
+   Reads an Internet message (RFC 5322), or one or more messages
+   stored in a maildrop in mbox (RFC 4155) or MMDF format, from a file
+   stream.  Each call to m_getfld() reads one header field, or a
+   portion of the body, in sequence.
+
+   Inputs
+   ======
+   gstate:  opaque parse state
+   bufsz:  maximum number of characters to load into buf
+   iob:  input file stream
+
+   Outputs
+   =======
+   name:  header field name (array of size NAMESZ=999)
+   buf:  either a header field body or message body
+   bufsz:  number of characters loaded into buf
+   (return value):  message parse state on return from function
+
+   Functions
+   =========
+   void m_getfld_state_destroy (m_getfld_state_t *gstate): destroys
+   the parse state pointed to by the gstate argument.
+
+   m_getfld_state_reset (m_getfld_state_t *gstate): resets the parse
+   state to FLD.
+
+   void m_unknown(FILE *iob):  Determines the message delimiter string
+   for the maildrop.  Called by inc, scan, and msh when reading from a
+   maildrop file.
+
+   void m_eomsbr (int (*action)(int)):  Sets the hook to check for end
+   of message in a maildrop.  Called only by msh.
+
+   State variables
+   ===============
+   m_getfld() retains state internally between calls in the
+   m_getfld_state_t variable.  These are used for detecting the end of
+   each message when reading maildrops:
+
+     unsigned char **pat_map
+     unsigned char *fdelim
+     unsigned char *delimend
+     int fdelimlen
+     unsigned char *edelim
+     int edelimlen
+     char *msg_delim
+     int msg_style
+     int (*eom_action)(int)
+
+   Usage
+   =====
+   m_getfld_state_t gstate = 0;
+      ...
+   int state = m_getfld (&gstate, ...);
+      ...
+   m_getfld_state_destroy (&gstate);
+
+   The state is retained internally by gstate.  To reset its state to FLD:
+   m_getfld_state_reset (&gstate);
+*/
+
+/* The following described the old implementation.  The high-level
+   structure hasn't changed, but some of the details have.  I'm
+   leaving this as-is, though, for posterity.
+ */
+
 /* This module has a long and checkered history.  First, it didn't burst
    maildrops correctly because it considered two CTRL-A:s in a row to be
    an inter-message delimiter.  It really is four CTRL-A:s followed by a
    it knows that _filbuf ignores the _ptr & _cnt and simply fills
    the buffer.  If stdio on your system doesn't work this way, you
    may have to make small changes in this routine.
-   
+
    This routine also "knows" that an EOF indication on a stream is
    "sticky" (i.e., you will keep getting EOF until you reposition the
    stream).  If your system doesn't work this way it is broken and you
    there is data in "name" or "buf").
   */
 
-
 /*
  * static prototypes
  */
-static int m_Eom (int, FILE *);
+struct m_getfld_state;
+static int m_Eom (m_getfld_state_t, int);
 static unsigned char *matchc(int, char *, int, char *);
-static unsigned char *locc(int, unsigned char *, unsigned char);
-
-#define Getc(iob)      getc(iob)
-#define eom(c,iob)     (msg_style != MS_DEFAULT && \
-                        (((c) == *msg_delim && m_Eom(c,iob)) ||\
-                         (eom_action && (*eom_action)(c))))
 
-static unsigned char **pat_map;
+#define eom(c,s)       (s->msg_style != MS_DEFAULT && \
+                        (((c) == *s->msg_delim && m_Eom(s,c)) || \
+                         (s->eom_action && (*s->eom_action)(c))))
 
-/*
- * defined in sbr/m_msgdef.c = 0
- * This is a disgusting hack for "inc" so it can know how many
- * characters were stuffed in the buffer on the last call
- * (see comments in uip/scansbr.c).
+/* This replaces the old approach, with its direct access to stdio
+ * internals.  It uses one fread() to load a buffer that we manage.
+ *
+ * MSG_INPUT_SIZE is the size of the buffer.
+ * MAX_DELIMITER_SIZE is the maximum size of the delimiter used to
+ * separate messages in a maildrop, such as mbox "From ".
+ *
+ * Some of the tests in the test suite assume a MSG_INPUT_SIZE
+ * of 8192.
  */
-extern int msg_count;
+#define MSG_INPUT_SIZE (BUFSIZ >= 1024 ? BUFSIZ : 1024)
+#define MAX_DELIMITER_SIZE 5
+
+struct m_getfld_state {
+    unsigned char msg_buf[2 * MSG_INPUT_SIZE + MAX_DELIMITER_SIZE];
+    unsigned char *readpos;
+    unsigned char *end;  /* One past the last character read in. */
+    /* The following support tracking of the read position in the
+       input file stream so that callers can interleave m_getfld()
+       calls with ftell() and fseek().  ytes_read replaces the old
+       m_getfld() msg_count global.  last_caller_pos is stored when
+       leaving m_getfld()/m_unknown(), then checked on the next entry.
+       last_internal_pos is used to remember the position used
+       internally by m_getfld() (read_more(), actually). */
+    off_t bytes_read;
+    off_t total_bytes_read; /* by caller, not necessarily from input file */
+    off_t last_caller_pos;
+    off_t last_internal_pos;
+    FILE *iob;
+
+    unsigned char **pat_map;
+    int msg_style;
+    /*
+     * The "full" delimiter string for a packed maildrop consists
+     * of a newline followed by the actual delimiter.  E.g., the
+     * full string for a Unix maildrop would be: "\n\nFrom ".
+     * "Fdelim" points to the start of the full string and is used
+     * in the BODY case of the main routine to search the buffer for
+     * a possible eom.  Msg_delim points to the first character of
+     * the actual delim. string (i.e., fdelim+1).  Edelim
+     * points to the 2nd character of actual delimiter string.  It
+     * is used in m_Eom because the first character of the string
+     * has been read and matched before m_Eom is called.
+     */
+    char *msg_delim;
+    unsigned char *fdelim;
+    unsigned char *delimend;
+    int fdelimlen;
+    unsigned char *edelim;
+    int edelimlen;
+    int (*eom_action)(int);
+    int state;
+};
+
+static
+void
+m_getfld_state_init (m_getfld_state_t *gstate, FILE *iob) {
+    m_getfld_state_t s;
+
+    s = *gstate = (m_getfld_state_t) mh_xmalloc(sizeof (struct m_getfld_state));
+    s->readpos = s->end = s->msg_buf;
+    s->bytes_read = s->total_bytes_read = 0;
+    s->last_caller_pos = s->last_internal_pos = 0;
+    s->iob = iob;
+    s->pat_map = NULL;
+    s->msg_style = MS_DEFAULT;
+    s->msg_delim = "";
+    s->fdelim = s->delimend = s->edelim = NULL;
+    s->fdelimlen = s->edelimlen = 0;
+    s->eom_action = NULL;
+    s->state = FLD;
+}
 
-/*
- * defined in sbr/m_msgdef.c = MS_DEFAULT
- */
-extern int msg_style;
+/* scan() needs to force a state an initial state of FLD for each message. */
+void
+m_getfld_state_reset (m_getfld_state_t *gstate) {
+    if (*gstate) {
+       (*gstate)->state = FLD;
+    }
+}
+
+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);
+       free (s);
+       *gstate = 0;
+    }
+}
 
 /*
- * The "full" delimiter string for a packed maildrop consists
- * of a newline followed by the actual delimiter.  E.g., the
- * full string for a Unix maildrop would be: "\n\nFrom ".
- * "Fdelim" points to the start of the full string and is used
- * in the BODY case of the main routine to search the buffer for
- * a possible eom.  Msg_delim points to the first character of
- * the actual delim. string (i.e., fdelim+1).  Edelim
- * points to the 2nd character of actual delimiter string.  It
- * is used in m_Eom because the first character of the string
- * has been read and matched before m_Eom is called.
- */
-extern char *msg_delim;         /* defined in sbr/m_msgdef.c = "" */
-static unsigned char *fdelim;
-static unsigned char *delimend;
-static int fdelimlen;
-static unsigned char *edelim;
-static int edelimlen;
+  Summary of file and message input buffer positions:
 
-static int (*eom_action)(int) = NULL;
+  input file      -------------------------------------------EOF
+                                 |              |
+                          last_caller_pos  last_internal_pos
 
-#ifdef _FSTDIO
-# define _ptr    _p            /* Gag   */
-# define _cnt    _r            /* Retch */
-# define _filbuf __srget       /* Puke  */
-# define DEFINED__FILBUF_TO_SOMETHING_SPECIFIC
 
-# if defined __CYGWIN__
-  /* Cygwin's stdio.h does not declare __srget(). */
-  int __srget(FILE *);
-# endif /* __CYGWIN__ */
-#endif
+  msg_buf                   --------------------EOF
+                            |         |         |
+                         msg_buf   readpos     end
 
-#ifndef DEFINED__FILBUF_TO_SOMETHING_SPECIFIC
-extern int  _filbuf(FILE*);
-#endif
+                            |<>|=retained characters, difference
+                                 between last_internal_pos and
+                                 first readpos value after reading
+                                 in new chunk in read_more()
+
+  When returning from m_getfld()/m_unknown():
+  1) Save the internal file position in last_internal_pos.  That's the
+     m_getfld() position reference in the input file.
+  2) Set file stream position so that callers can use ftell().
+
+  When entering m_getfld()/m_unknown():
+  Check to see if the call had changed the file position.  If so,
+  adjust the internal position reference accordingly.  If not, restore
+  the internal file position from last_internal_pos.
+*/
+
+
+static void
+enter_getfld (m_getfld_state_t *gstate, FILE *iob) {
+    m_getfld_state_t s;
+    off_t pos = ftello (iob);
+
+    if (! *gstate) {
+       m_getfld_state_init (gstate, iob);
+    }
+    s = *gstate;
+    s->bytes_read = 0;
+
+    /* This is ugly and no longer necessary, but is retained just in
+       case it's needed again.  The parser used to open the input file
+       multiple times, so we had to always use the FILE * that's
+       passed to m_getfld().  Now the parser inits a new
+       m_getfld_state for each file.  See comment below about the
+       readpos shift code being currently unused. */
+    s->iob = iob;
+
+    if (pos != 0  ||  s->last_internal_pos != 0) {
+       if (s->last_internal_pos == 0) {
+           s->total_bytes_read = pos;
+       } else {
+           off_t pos_movement = pos - s->last_caller_pos; /* Can be < 0. */
+
+           if (pos_movement == 0) {
+               pos = s->last_internal_pos;
+           } else {
+               /* The current file stream position differs from the
+                  last one, so caller must have called ftell/o().
+                  Or, this is the first call and the file position
+                  was not at 0. */
+
+               if (s->readpos + pos_movement >= s->msg_buf  &&
+                   s->readpos + pos_movement < s->end) {
+                   /* This is currently unused.  It could be used by
+                      parse_mime() if it was changed to use a global
+                      m_getfld_state. */
+                   /* We can shift readpos and remain within the
+                      bounds of msg_buf. */
+                   s->readpos += pos_movement;
+                   s->total_bytes_read += pos_movement;
+                   pos = s->last_internal_pos;
+               } else {
+                   size_t num_read;
+
+                   /* This seek skips past an integral number of
+                      chunks of size MSG_INPUT_SIZE. */
+                   fseeko (iob, pos/MSG_INPUT_SIZE * MSG_INPUT_SIZE, SEEK_SET);
+                   num_read = fread (s->msg_buf, 1, MSG_INPUT_SIZE, iob);
+                   s->readpos = s->msg_buf  +  pos % MSG_INPUT_SIZE;
+                   s->end = s->msg_buf + num_read;
+                   s->total_bytes_read = pos;
+               }
+           }
+
+           fseeko (iob, pos, SEEK_SET);
+       }
+    }
+}
+
+static void
+leave_getfld (m_getfld_state_t s) {
+    /* Save the internal file position that we use for the input buffer. */
+    s->last_internal_pos = ftello (s->iob);
+
+    /* Set file stream position so that callers can use ftell(). */
+    s->total_bytes_read += s->bytes_read;
+    fseeko (s->iob, s->total_bytes_read, SEEK_SET);
+    s->last_caller_pos = ftello (s->iob);
+}
+
+static size_t
+read_more (m_getfld_state_t s) {
+    /* Retain at least edelimlen characters that have already been
+       read so that we can back up to them in m_Eom(). */
+    ssize_t retain = s->edelimlen;
+    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); */
+
+    /* Move what we want to retain at end of the buffer to the beginning. */
+    memmove (s->msg_buf, s->readpos - retain, retain);
+
+    s->readpos = s->msg_buf + retain;
+    num_read = fread (s->readpos, 1, MSG_INPUT_SIZE, s->iob);
+    s->end = s->readpos + num_read;
+
+    return num_read;
+}
+
+static int
+Getc (m_getfld_state_t s) {
+    if (s->end - s->readpos < 1) {
+       if (read_more (s) == 0) {
+           /* Pretend that we read a character.  That's what stdio does. */
+           ++s->readpos;
+           return EOF;
+       }
+    }
+
+    ++s->bytes_read;
+    return s->readpos < s->end  ?  *s->readpos++  :  EOF;
+}
+
+static int
+Peek (m_getfld_state_t s) {
+    if (s->end - s->readpos < 1) {
+       if (read_more (s) == 0) {
+           /* Pretend that we read a character.  That's what stdio does. */
+           ++s->readpos;
+           return EOF;
+       }
+    }
+
+    return s->readpos < s->end  ?  *s->readpos  :  EOF;
+}
+
+static int
+Ungetc (int c, m_getfld_state_t s) {
+    if (s->readpos == s->msg_buf) {
+       return EOF;
+    } else {
+       --s->bytes_read;
+       return *--s->readpos = c;
+    }
+}
 
 
 int
-m_getfld (int state, unsigned char *name, unsigned char *buf,
-          int bufsz, FILE *iob)
+m_getfld (m_getfld_state_t *gstate, unsigned char name[NAMESZ],
+          unsigned char *buf, int *bufsz, FILE *iob)
 {
-    register unsigned char  *bp, *cp, *ep, *sp;
-    register int cnt, c, i, j;
+    m_getfld_state_t s;
+    register unsigned char *cp;
+    register int max, n, c;
+
+    enter_getfld (gstate, iob);
+    s = *gstate;
 
-    if ((c = Getc(iob)) < 0) {
-       msg_count = 0;
-       *buf = 0;
-       return FILEEOF;
+    if ((c = Getc(s)) < 0) {
+       *bufsz = *buf = 0;
+       leave_getfld (s);
+       return s->state = FILEEOF;
     }
-    if (eom (c, iob)) {
-       if (! eom_action) {
+    if (eom (c, s)) {
+       if (! s->eom_action) {
            /* flush null messages */
-           while ((c = Getc(iob)) >= 0 && eom (c, iob))
+           while ((c = Getc(s)) >= 0 && eom (c, s))
                ;
+
            if (c >= 0)
-               ungetc(c, iob);
+               Ungetc(c, s);
        }
-       msg_count = 0;
-       *buf = 0;
-       return FILEEOF;
+       *bufsz = *buf = 0;
+       leave_getfld (s);
+       return s->state = FILEEOF;
     }
 
-    switch (state) {
-       case FLDEOF: 
-       case BODYEOF: 
-       case FLD: 
+    switch (s->state) {
+       case FLD:
            if (c == '\n' || c == '-') {
                /* we hit the header/body separator */
-               while (c != '\n' && (c = Getc(iob)) >= 0)
-                   ;
+               while (c != '\n' && (c = Getc(s)) >= 0) continue;
 
-               if (c < 0 || (c = Getc(iob)) < 0 || eom (c, iob)) {
-                   if (! eom_action) {
+               if (c < 0 || (c = Getc(s)) < 0 || eom (c, s)) {
+                   if (! s->eom_action) {
                        /* flush null messages */
-                       while ((c = Getc(iob)) >= 0 && eom (c, iob))
+                       while ((c = Getc(s)) >= 0 && eom (c, s))
                            ;
                        if (c >= 0)
-                           ungetc(c, iob);
+                           Ungetc(c, s);
                    }
-                   msg_count = 0;
-                   *buf = 0;
-                   return FILEEOF;
+                   *bufsz = *buf = 0;
+                   leave_getfld (s);
+                   return s->state = FILEEOF;
                }
-               state = BODY;
+               s->state = BODY;
                goto body;
            }
            /*
             * get the name of this component.  take characters up
             * to a ':', a newline or NAMESZ-1 characters, whichever
-            * comes first.  
+            * comes first.
             */
            cp = name;
-           i = NAMESZ - 1;
-           for (;;) {
-#ifdef LINUX_STDIO
-               bp = sp = (unsigned char *) iob->_IO_read_ptr - 1;
-               j = (cnt = ((long) iob->_IO_read_end -
-                       (long) iob->_IO_read_ptr)  + 1) < i ? cnt : i;
-#elif defined(__DragonFly__)
-               bp = sp = (unsigned char *) ((struct __FILE_public *)iob)->_p - 1;
-               j = (cnt = ((struct __FILE_public *)iob)->_r+1) < i ? cnt : i;
-#else
-               bp = sp = (unsigned char *) iob->_ptr - 1;
-               j = (cnt = iob->_cnt+1) < i ? cnt : i;
-#endif
-               while (--j >= 0 && (c = *bp++) != ':' && c != '\n')
-                   *cp++ = c;
-
-               j = bp - sp;
-               if ((cnt -= j) <= 0) {
-#ifdef LINUX_STDIO
-                   iob->_IO_read_ptr = iob->_IO_read_end;
-                   if (__underflow(iob) == EOF) {
-#elif defined(__DragonFly__)
-                   if (__srget(iob) == EOF) {
-#else
-                   if (_filbuf(iob) == EOF) {
-#endif
-                       *cp = *buf = 0;
-                       advise (NULL, "eof encountered in field \"%s\"", name);
-                       return FMTERR;
-                   }
-#ifdef LINUX_STDIO
-               iob->_IO_read_ptr++; /* NOT automatic in __underflow()! */
-#endif
-               } else {
-#ifdef LINUX_STDIO
-                   iob->_IO_read_ptr = bp + 1;
-#elif defined(__DragonFly__)
-                   ((struct __FILE_public *)iob)->_p = bp + 1;
-                   ((struct __FILE_public *)iob)->_r = cnt - 1;
-#else
-                   iob->_ptr = bp + 1;
-                   iob->_cnt = cnt - 1;
-#endif
-               }
-               if (c == ':')
-                   break;
+           max = NAMESZ - 1;
+           /* Get the field name.  The first time through the loop,
+              this copies out the first character, which was loaded
+              into c prior to loop entry.  Initialize n to 1 to
+              account for that. */
+           for (n = 1;
+                c != ':'  &&  c != '\n'  &&  c != EOF  &&  n < max;
+                ++n, c = Getc (s)) {
+               *cp++ = c;
+           }
 
-               /*
-                * something went wrong.  possibilities are:
-                *  . hit a newline (error)
-                *  . got more than namesz chars. (error)
-                *  . hit the end of the buffer. (loop)
-                */
-               if (c == '\n') {
-                   /* We hit the end of the line without seeing ':' to
-                    * terminate the field name.  This is usually (always?)
-                    * spam.  But, blowing up is lame, especially when
-                    * scan(1)ing a folder with such messages.  Pretend such
-                    * lines are the first of the body (at least mutt also
-                    * handles it this way). */
-
-                   /* See if buf can hold this line, since we were assuming
-                    * we had a buffer of NAMESZ, not bufsz. */
-                   /* + 1 for the newline */
-                   if (bufsz < j + 1) {
-                       /* No, it can't.  Oh well, guess we'll blow up. */
-                       *cp = *buf = 0;
-                       advise (NULL, "eol encountered in field \"%s\"", name);
-                       state = FMTERR;
-                       goto finish;
-                   }
-                   memcpy (buf, name, j - 1);
-                   buf[j - 1] = '\n';
-                   buf[j] = '\0';
-                   /* mhparse.c:get_content wants to find the position of the
-                    * body start, but it thinks there's a blank line between
-                    * the header and the body (naturally!), so seek back so
-                    * that things line up even though we don't have that
-                    * blank line in this case.  Simpler parsers (e.g. mhl)
-                    * get extra newlines, but that should be harmless enough,
-                    * right?  This is a corrupt message anyway. */
-                   fseek (iob, ftell (iob) - 2, SEEK_SET);
-                   return BODY;
+           /* Check for next character, which is either the space after
+              the ':' or the first folded whitespace. */
+           {
+               int next_char;
+               if (c == EOF  ||  (next_char = Peek (s)) == EOF) {
+                   *bufsz = *cp = *buf = 0;
+                   advise (NULL, "eof encountered in field \"%s\"", name);
+                   leave_getfld (s);
+                   return s->state = FMTERR;
                }
-               if ((i -= j) <= 0) {
-                   *cp = *buf = 0;
-                   advise (NULL, "field name \"%s\" exceeds %d bytes", name, NAMESZ - 2);
-                   state = LENERR;
-                   goto finish;
+           }
+
+           /* If c isn't ':' here, something went wrong.  Possibilities are:
+            *  . hit a newline (error)
+            *  . got more than namesz chars. (error)
+            */
+           if (c == ':') {
+               /* Finished header name, fall through to FLDPLUS below. */
+           } else if (c == '\n') {
+               /* We hit the end of the line without seeing ':' to
+                * terminate the field name.  This is usually (always?)
+                * spam.  But, blowing up is lame, especially when
+                * scan(1)ing a folder with such messages.  Pretend such
+                * lines are the first of the body (at least mutt also
+                * handles it this way). */
+
+               /* See if buf can hold this line, since we were assuming
+                * we had a buffer of NAMESZ, not bufsz. */
+               /* + 1 for the newline */
+               if (*bufsz < n + 1) {
+                   /* No, it can't.  Oh well, guess we'll blow up. */
+                   *bufsz = *cp = *buf = 0;
+                   advise (NULL, "eol encountered in field \"%s\"", name);
+                   s->state = FMTERR;
+                   break;
                }
+               memcpy (buf, name, n - 1);
+               buf[n - 1] = '\n';
+               buf[n] = '\0';
+               /* The last character read was '\n'.  s->bytes_read
+                  (and n) include that, but it was not put into the
+                  name array in the for loop above.  So subtract 1. */
+               *bufsz = --s->bytes_read;  /* == n - 1 */
+               leave_getfld (s);
+               return s->state = BODY;
+           } else if (max <= n) {
+               /* By design, the loop above discards the last character
+                   it had read.  It's in c, use it. */
+               *cp++ = c;
+               *bufsz = *cp = *buf = 0;
+               advise (NULL, "field name \"%s\" exceeds %d bytes", name,
+                       NAMESZ - 2);
+               s->state = LENERR;
+               break;
            }
 
-           while (isspace (*--cp) && cp >= name)
-               ;
+           /* Trim any trailing spaces from the end of name. */
+           while (isspace (*--cp) && cp >= name) continue;
            *++cp = 0;
+           /* readpos points to the first character of the field body. */
            /* fall through */
 
-       case FLDPLUS: 
+       case FLDPLUS: {
            /*
-            * get (more of) the text of a field.  take
+            * get (more of) the text of a field.  Take
             * characters up to the end of this field (newline
             * followed by non-blank) or bufsz-1 characters.
             */
-           cp = buf; i = bufsz-1;
-           for (;;) {
-#ifdef LINUX_STDIO
-               cnt = (long) iob->_IO_read_end - (long) iob->_IO_read_ptr;
-               bp = (unsigned char *) --iob->_IO_read_ptr;
-#elif defined(__DragonFly__)
-               cnt = ((struct __FILE_public *)iob)->_r++;
-               bp = (unsigned char *) --((struct __FILE_public *)iob)->_p;
-#else
-               cnt = iob->_cnt++;
-               bp = (unsigned char *) --iob->_ptr;
-#endif
-               c = cnt < i ? cnt : i;
-               while ((ep = locc( c, bp, '\n' ))) {
-                   /*
-                    * if we hit the end of this field, return.
-                    */
-                   if ((j = *++ep) != ' ' && j != '\t') {
-#ifdef LINUX_STDIO
-                       j = ep - (unsigned char *) iob->_IO_read_ptr;
-                       memcpy (cp, iob->_IO_read_ptr, j);
-                       iob->_IO_read_ptr = ep;
-#elif defined(__DragonFly__)
-                       j = ep - (unsigned char *) ((struct __FILE_public *)iob)->_p;
-                       memcpy (cp, ((struct __FILE_public *)iob)->_p, j);
-                       ((struct __FILE_public *)iob)->_p = ep;
-                       ((struct __FILE_public *)iob)->_r -= j;
-#else
-                       j = ep - (unsigned char *) iob->_ptr;
-                       memcpy (cp, iob->_ptr, j);
-                       iob->_ptr = ep;
-                       iob->_cnt -= j;
-#endif
-                       cp += j;
-                       state = FLD;
-                       goto finish;
-                   }
-                   c -= ep - bp;
-                   bp = ep;
+           int finished;
+
+           cp = buf;
+           max = *bufsz-1;
+           n = 0;
+           for (finished = 0; ! finished; ) {
+               while (c != '\n'  &&  c != EOF  &&  n++ < max) {
+                   *cp++ = c = Getc (s);
                }
-               /*
-                * end of input or dest buffer - copy what we've found.
-                */
-#ifdef LINUX_STDIO
-               c += bp - (unsigned char *) iob->_IO_read_ptr;
-               memcpy( cp, iob->_IO_read_ptr, c);
-#elif defined(__DragonFly__)
-               c += bp - (unsigned char *) ((struct __FILE_public *)iob)->_p;
-               memcpy( cp, ((struct __FILE_public *)iob)->_p, c);
-#else
-               c += bp - (unsigned char *) iob->_ptr;
-               memcpy( cp, iob->_ptr, c);
-#endif
-               i -= c;
-               cp += c;
-               if (i <= 0) {
+
+               if (c != EOF) c = Peek (s);
+               if (max < n) {
                    /* the dest buffer is full */
-#ifdef LINUX_STDIO
-                   iob->_IO_read_ptr += c;
-#elif defined(__DragonFly__)
-                   ((struct __FILE_public *)iob)->_r -= c;
-                   ((struct __FILE_public *)iob)->_p += c;
-#else
-                   iob->_cnt -= c;
-                   iob->_ptr += c;
-#endif
-                   state = FLDPLUS;
-                   break;
-               }
-               /* 
-                * There's one character left in the input buffer.
-                * Copy it & fill the buffer.  If the last char
-                * was a newline and the next char is not whitespace,
-                * this is the end of the field.  Otherwise loop.
-                */
-               --i;
-#ifdef LINUX_STDIO
-               *cp++ = j = *(iob->_IO_read_ptr + c);
-               iob->_IO_read_ptr = iob->_IO_read_end;
-               c = __underflow(iob);
-               iob->_IO_read_ptr++;    /* NOT automatic! */
-#elif defined(__DragonFly__)
-               *cp++ =j = *(((struct __FILE_public *)iob)->_p + c);
-               c = __srget(iob);
-#else
-               *cp++ = j = *(iob->_ptr + c);
-               c = _filbuf(iob);
-#endif
-                if (c == EOF || 
-                 ((j == '\0' || j == '\n') && c != ' ' && c != '\t')) {
-                   if (c != EOF) {
-#ifdef LINUX_STDIO
-                       --iob->_IO_read_ptr;
-#elif defined(__DragonFly__)
-                       --((struct __FILE_public *)iob)->_p;
-                       ++((struct __FILE_public *)iob)->_r;
-#else
-                       --iob->_ptr;
-                       ++iob->_cnt;
-#endif
-                   }
-                   state = FLD;
-                   break;
+                   s->state = FLDPLUS;
+                   finished = 1;
+               } else if (c != ' '  &&  c != '\t') {
+                   /* The next character is not folded whitespace, so
+                      prepare to move on to the next field.  It's OK
+                      if c is EOF, it will be handled on the next
+                      call to m_getfld (). */
+                   s->state = FLD;
+                   finished = 1;
+               } else {
+                   /* Folded header field, continues on the next line. */
                }
            }
+           *bufsz = s->bytes_read;
            break;
+        }
 
-       case BODY: 
        body:
+       case BODY: {
            /*
             * get the message body up to bufsz characters or the
-            * end of the message.  Sleazy hack: if bufsz is negative
-            * we assume that we were called to copy directly into
-            * the output buffer and we don't add an eos.
+            * end of the message.
             */
-           i = (bufsz < 0) ? -bufsz : bufsz-1;
-#ifdef LINUX_STDIO
-           bp = (unsigned char *) --iob->_IO_read_ptr;
-           cnt = (long) iob->_IO_read_end - (long) iob->_IO_read_ptr;
-#elif defined(__DragonFly__)
-           bp = (unsigned char *) --((struct __FILE_public *)iob)->_p;
-           cnt = ++((struct __FILE_public *)iob)->_r;
-#else
-           bp = (unsigned char *) --iob->_ptr;
-           cnt = ++iob->_cnt;
-#endif
-           c = (cnt < i ? cnt : i);
-           if (msg_style != MS_DEFAULT && c > 1) {
+           unsigned char *bp;
+
+           max = *bufsz-1;
+           /* Back up and store the current position. */
+           bp = --s->readpos;
+           c = s->end - s->readpos < max  ?  s->end - s->readpos  :  max;
+           if (s->msg_style != MS_DEFAULT && c > 1) {
                /*
                 * packed maildrop - only take up to the (possible)
                 * start of the next message.  This "matchc" should
@@ -498,7 +655,9 @@ m_getfld (int state, unsigned char *name, unsigned char *buf,
                 * algorithms vs. brute force.)  Since I (currently)
                 * run MH on a vax, we use the matchc instruction. --vj
                 */
-               if ((ep = matchc( fdelimlen, fdelim, c, bp )))
+               unsigned char *ep;
+
+               if ((ep = matchc( s->fdelimlen, s->fdelim, c, bp )))
                    c = ep - bp + 1;
                else {
                    /*
@@ -514,8 +673,10 @@ m_getfld (int state, unsigned char *name, unsigned char *buf,
                     * ends with one of the characters in the pattern
                     * (excluding the first and last), we do only one test.
                     */
+                   unsigned char *sp;
+
                    ep = bp + c - 1;
-                   if ((sp = pat_map[*ep])) {
+                   if ((sp = s->pat_map[*ep])) {
                        do {
                            /* This if() is true unless (a) the buffer is too
                             * small to contain this delimiter prefix, or
@@ -527,7 +688,7 @@ m_getfld (int state, unsigned char *name, unsigned char *buf,
                             * should have found it.  Thus it's not a delim
                             * and we know we won't get a match.
                             */
-                           if (((sp - fdelim) + 2) <= c) {
+                           if (((sp - s->fdelim) + 2) <= c) {
                                cp = sp;
                                /* Unfortunately although fdelim has a preceding NUL
                                 * we can't use this as a sentinel in case the buffer
@@ -535,9 +696,9 @@ m_getfld (int state, unsigned char *name, unsigned char *buf,
                                 * would cause us to run off the front of fdelim).
                                 */
                                while (*--ep == *--cp)
-                                   if (cp < fdelim)
+                                   if (cp < s->fdelim)
                                        break;
-                               if (cp < fdelim) {
+                               if (cp < s->fdelim) {
                                    /* we matched the entire delim prefix,
                                     * so only take the buffer up to there.
                                     * we know ep >= bp -- check above prevents underrun
@@ -548,45 +709,47 @@ m_getfld (int state, unsigned char *name, unsigned char *buf,
                            }
                            /* try matching one less char of delim string */
                            ep = bp + c - 1;
-                       } while (--sp > fdelim);
+                       } while (--sp > s->fdelim);
                    }
                }
            }
            memcpy( buf, bp, c );
-#ifdef LINUX_STDIO
-           iob->_IO_read_ptr += c;
-#elif defined(__DragonFly__)
-           ((struct __FILE_public *)iob)->_r -= c;
-           ((struct __FILE_public *)iob)->_p += c;
-#else
-           iob->_cnt -= c;
-           iob->_ptr += c;
-#endif
-           if (bufsz < 0) {
-               msg_count = c;
-               return (state);
-           }
+           /* Advance the current position to reflect the copy out.
+              c is less than or equal to the number of bytes remaining
+              in the read buffer, so will not overrun it. */
+           s->readpos += c;
            cp = buf + c;
+           /* Subtract 1 from c because the first character was read by
+              Getc(), and therefore already accounted for in s->bytes_read. */
+           s->bytes_read += c - 1;
+           *bufsz = s->bytes_read;
            break;
+        }
 
-       default: 
-           adios (NULL, "m_getfld() called with bogus state of %d", state);
+       default:
+           adios (NULL, "m_getfld() called with bogus state of %d", s->state);
     }
-finish:
+
     *cp = 0;
-    msg_count = cp - buf;
-    return (state);
+    leave_getfld (s);
+
+    return s->state;
 }
 
 
 void
-m_unknown(FILE *iob)
+m_unknown(m_getfld_state_t *gstate, FILE *iob)
 {
+    m_getfld_state_t s;
     register int c;
-    register long pos;
-    char text[10];
+    char text[MAX_DELIMITER_SIZE];
+    char from[] = "From ";
     register char *cp;
     register char *delimstr;
+    unsigned int i;
+
+    enter_getfld (gstate, iob);
+    s = *gstate;
 
 /*
  * Figure out what the message delimitter string is for this
@@ -601,34 +764,35 @@ m_unknown(FILE *iob)
  * specified when nmh was built (or from the mts.conf file).
  */
 
-    msg_style = MS_UNKNOWN;
+    s->msg_style = MS_UNKNOWN;
 
-    pos = ftell (iob);
-    if (fread (text, sizeof(*text), 5, iob) == 5
-           && strncmp (text, "From ", 5) == 0) {
-       msg_style = MS_MBOX;
+    for (i = 0, cp = text; i < sizeof text; ++i, ++cp) {
+       if ((*cp = Getc (s)) == EOF) {
+           break;
+       }
+    }
+
+    if (i == sizeof from-1  &&  strncmp (text, "From ", sizeof from-1) == 0) {
+       s->msg_style = MS_MBOX;
        delimstr = "\nFrom ";
-       while ((c = getc (iob)) != '\n' && c >= 0)
-           ;
+       while ((c = Getc (s)) != '\n' && c >= 0) continue;
     } else {
        /* not a Unix style maildrop */
-       fseek (iob, pos, SEEK_SET);
-       if (mmdlm2 == NULL || *mmdlm2 == 0)
-           mmdlm2 = "\001\001\001\001\n";
+       s->readpos -= s->bytes_read;
        delimstr = mmdlm2;
-       msg_style = MS_MMDF;
+       s->msg_style = MS_MMDF;
     }
     c = strlen (delimstr);
-    fdelim = (unsigned char *) mh_xmalloc((size_t) (c + 3));
-    *fdelim++ = '\0';
-    *fdelim = '\n';
-    msg_delim = (char *)fdelim+1;
-    edelim = (unsigned char *)msg_delim+1;
-    fdelimlen = c + 1;
-    edelimlen = c - 1;
-    strcpy (msg_delim, delimstr);
-    delimend = (unsigned char *)msg_delim + edelimlen;
-    if (edelimlen <= 1)
+    s->fdelim = (unsigned char *) mh_xmalloc((size_t) (c + 3));
+    *s->fdelim++ = '\0';
+    *s->fdelim = '\n';
+    s->msg_delim = (char *)s->fdelim+1;
+    s->edelim = (unsigned char *)s->msg_delim+1;
+    s->fdelimlen = c + 1;
+    s->edelimlen = c - 1; /* == strlen (delimstr) */
+    strcpy (s->msg_delim, delimstr);
+    s->delimend = (unsigned char *)s->msg_delim + s->edelimlen;
+    if (s->edelimlen <= 1)
        adios (NULL, "maildrop delimiter must be at least 2 bytes");
     /*
      * build a Boyer-Moore end-position map for the matcher in m_getfld.
@@ -636,34 +800,36 @@ m_unknown(FILE *iob)
      * separator) or the last char (since the matchc would have found it
      * if it was a real delim).
      */
-    pat_map = (unsigned char **) calloc (256, sizeof(unsigned char *));
+    s->pat_map = (unsigned char **) calloc (256, sizeof(unsigned char *));
 
-    for (cp = (char *) fdelim + 1; cp < (char *) delimend; cp++ )
-       pat_map[(unsigned char)*cp] = (unsigned char *) cp;
+    for (cp = (char *) s->fdelim + 1; cp < (char *) s->delimend; cp++ )
+       s->pat_map[(unsigned char)*cp] = (unsigned char *) cp;
 
-    if (msg_style == MS_MMDF) {
+    if (s->msg_style == MS_MMDF) {
        /* flush extra msg hdrs */
-       while ((c = Getc(iob)) >= 0 && eom (c, iob))
+       while ((c = Getc(s)) >= 0 && eom (c, s))
            ;
        if (c >= 0)
-           ungetc(c, iob);
+           Ungetc(c, s);
     }
+
+    leave_getfld (s);
 }
 
 
 void
-m_eomsbr (int (*action)(int))
+m_eomsbr (m_getfld_state_t s, int (*action)(int))
 {
-    if ((eom_action = action)) {
-       msg_style = MS_MSH;
-       *msg_delim = 0;
-       fdelimlen = 1;
-       delimend = fdelim;
+    if ((s->eom_action = action)) {
+       s->msg_style = MS_MSH;
+       *s->msg_delim = 0;
+       s->fdelimlen = 1;
+       s->delimend = s->fdelim;
     } else {
-       msg_style = MS_MMDF;
-       msg_delim = (char *)fdelim + 1;
-       fdelimlen = strlen((char *)fdelim);
-       delimend = (unsigned char *)(msg_delim + edelimlen);
+       s->msg_style = MS_MMDF;
+       s->msg_delim = (char *)s->fdelim + 1;
+       s->fdelimlen = strlen((char *)s->fdelim);
+       s->delimend = (unsigned char *)(s->msg_delim + s->edelimlen);
     }
 }
 
@@ -673,32 +839,35 @@ m_eomsbr (int (*action)(int))
  */
 
 static int
-m_Eom (int c, FILE *iob)
+m_Eom (m_getfld_state_t s, int c)
 {
-    register long pos = 0L;
     register int i;
-    char text[10];
+    char text[MAX_DELIMITER_SIZE];
+    char *cp;
 
-    pos = ftell (iob);
-    if ((i = fread (text, sizeof *text, edelimlen, iob)) != edelimlen
-           || strncmp (text, (char *)edelim, edelimlen)) {
-       if (i == 0 && msg_style == MS_MBOX)
+    for (i = 0, cp = text; i < s->edelimlen; ++i, ++cp) {
+       if ((*cp = Getc (s)) == EOF) {
+           break;
+       }
+    }
+
+    if (i != s->edelimlen  ||
+        strncmp (text, (char *)s->edelim, s->edelimlen)) {
+       if (i == 0 && s->msg_style == MS_MBOX)
            /* the final newline in the (brain damaged) unix-format
             * maildrop is part of the delimitter - delete it.
             */
            return 1;
 
-#if 0
-       fseek (iob, pos, SEEK_SET);
-#endif
-
-       fseek (iob, (long)(pos-1), SEEK_SET);
-       getc (iob);             /* should be OK */
+       /* Did not find delimiter, so restore the read position.
+          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;
        return 0;
     }
 
-    if (msg_style == MS_MBOX) {
-       while ((c = getc (iob)) != '\n')
+    if (s->msg_style == MS_MBOX) {
+       while ((c = Getc (s)) != '\n')
            if (c < 0)
                break;
     }
@@ -725,22 +894,7 @@ matchc(int patln, char *pat, int strln, char *str)
                sp = str; pp = pat;
                while (pp < ep && *sp++ == *pp)
                        pp++;
-               if (pp >= ep) 
+               if (pp >= ep)
                        return ((unsigned char *)--str);
        }
 }
-
-
-/*
- * Locate character "term" in the next "cnt" characters of "src".
- * If found, return its address, otherwise return 0.
- */
-
-static unsigned char *
-locc(int cnt, unsigned char *src, unsigned char term)
-{
-    while (*src++ != term && --cnt > 0);
-
-    return (cnt > 0 ? --src : (unsigned char *)0);
-}
-
diff --git a/sbr/m_msgdef.c b/sbr/m_msgdef.c
deleted file mode 100644 (file)
index d602973..0000000
+++ /dev/null
@@ -1,33 +0,0 @@
-
-/*
- * m_msgdef.c -- some defines for sbr/m_getfld.c
- *
- * 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>
-
-/*
- * disgusting hack for "inc" so it can know how many characters
- * were stuffed in the buffer on the last call (see comments
- * in uip/scansbr.c)
- */
-int msg_count = 0;
-
-int msg_style = MS_DEFAULT;
-
-/*
- * The "full" delimiter string for a packed maildrop consists
- * of a newline followed by the actual delimiter.  E.g., the
- * full string for a Unix maildrop would be: "\n\nFrom ".
- * "Fdelim" points to the start of the full string and is used
- * in the BODY case of the main routine to search the buffer for
- * a possible eom.  Msg_delim points to the first character of
- * the actual delim. string (i.e., fdelim+1).  Edelim
- * points to the 2nd character of actual delimiter string.  It
- * is used in m_Eom because the first character of the string
- * has been read and matched before m_Eom is called.
- */
-char *msg_delim = "";
index 1ddd648..b25ea99 100644 (file)
@@ -52,17 +52,18 @@ readconfig (struct node **npp, FILE *ib, char *file, int ctx)
     char name[NAMESZ], field[BUFSIZ];
     register struct node *np;
     register struct procstr *ps;
+    m_getfld_state_t gstate = 0;
 
     if (npp == NULL && (npp = opp) == NULL) {
        admonish (NULL, "bug: readconfig called but pump not primed");
        return;
     }
 
-    for (state = FLD;;) {
-       switch (state = m_getfld (state, name, field, sizeof(field), ib)) {
+    for (;;) {
+       int fieldsz = sizeof field;
+       switch (state = m_getfld (&gstate, name, field, &fieldsz, ib)) {
            case FLD:
            case FLDPLUS:
-           case FLDEOF:
                np = (struct node *) mh_xmalloc (sizeof(*np));
                *npp = np;
                *(npp = &np->n_next) = NULL;
@@ -70,7 +71,8 @@ readconfig (struct node **npp, FILE *ib, char *file, int ctx)
                if (state == FLDPLUS) {
                    cp = getcpy (field);
                    while (state == FLDPLUS) {
-                       state = m_getfld (state, name, field, sizeof(field), ib);
+                       fieldsz = sizeof field;
+                       state = m_getfld (&gstate, name, field, &fieldsz, ib);
                        cp = add (field, cp);
                    }
                    np->n_field = trimcpy (cp);
@@ -89,12 +91,9 @@ readconfig (struct node **npp, FILE *ib, char *file, int ctx)
                        *ps->procnaddr = np->n_field;
                        break;
                    }
-               if (state == FLDEOF)
-                   break;
                continue;
 
            case BODY:
-           case BODYEOF:
                adios (NULL, "no blank lines are permitted in %s", file);
 
            case FILEEOF:
@@ -105,6 +104,7 @@ readconfig (struct node **npp, FILE *ib, char *file, int ctx)
        }
        break;
     }
+    m_getfld_state_destroy (&gstate);
 
     /*
      * Special handling for the pager processes: lproc and moreproc.
index 985c61e..a807ef7 100644 (file)
@@ -59,6 +59,7 @@ seq_public (struct msgs *mp)
     char *cp, seqfile[PATH_MAX];
     char name[NAMESZ], field[BUFSIZ];
     FILE *fp;
+    m_getfld_state_t gstate = 0;
 
     /*
      * If mh_seq == NULL (such as if nmh been compiled with
@@ -76,15 +77,16 @@ seq_public (struct msgs *mp)
        return;
 
     /* Use m_getfld to scan sequence file */
-    for (state = FLD;;) {
-       switch (state = m_getfld (state, name, field, sizeof(field), fp)) {
+    for (;;) {
+       int fieldsz = sizeof field;
+       switch (state = m_getfld (&gstate, name, field, &fieldsz, fp)) {
            case FLD: 
            case FLDPLUS:
-           case FLDEOF: 
                if (state == FLDPLUS) {
                    cp = getcpy (field);
                    while (state == FLDPLUS) {
-                       state = m_getfld (state, name, field, sizeof(field), fp);
+                       fieldsz = sizeof field;
+                       state = m_getfld (&gstate, name, field, &fieldsz, fp);
                        cp = add (field, cp);
                    }
                    seq_init (mp, getcpy (name), trimcpy (cp));
@@ -92,12 +94,9 @@ seq_public (struct msgs *mp)
                } else {
                    seq_init (mp, getcpy (name), trimcpy (field));
                }
-               if (state == FLDEOF)
-                   break;
                continue;
 
            case BODY: 
-           case BODYEOF: 
                adios (NULL, "no blank lines are permitted in %s", seqfile);
                /* fall */
 
@@ -109,6 +108,7 @@ seq_public (struct msgs *mp)
        }
        break;  /* break from for loop */
     }
+    m_getfld_state_destroy (&gstate);
 
     lkfclose (fp, seqfile);
 }
index 9e66296..e993cce 100755 (executable)
@@ -1,10 +1,12 @@
 #!/bin/sh
 ######################################################
 #
-# Test bogus headers (no blank line before body).
+# Test bogus headers (no blank line before body, etc.)
 #
 ######################################################
 
+set -e
+
 if test -z "${MH_OBJ_DIR}"; then
     srcdir=`dirname "$0"`/../..
     MH_OBJ_DIR=`cd "$srcdir" && pwd`; export MH_OBJ_DIR
@@ -14,13 +16,14 @@ fi
 
 setup_test
 
-expected=$MH_TEST_DIR/$$.expected
-actual=$MH_TEST_DIR/$$.actual
+expected="$MH_TEST_DIR/$$.expected"
+expected_err="$MH_TEST_DIR/$$.expected_err"
+actual="$MH_TEST_DIR/$$.actual"
+actual_err="$MH_TEST_DIR/$$.actual_err"
 
 # Write message with bogus header field (missing blank line, really).
-msgfile=`mhpath new`
-msgnum=`basename $msgfile`
-cat > $msgfile <<EOF
+msgfile="$MH_TEST_DIR/Mail/inbox/11"
+cat >"$msgfile" <<EOF
 Date: Sun, 18 Dec 2005 00:52:39 +0100
 From: foo@example.edu
 To: bar@example.edu
@@ -31,14 +34,14 @@ I am a stupid spammer.
 EOF
 
 # check scan
-cat > $expected <<EOF
+cat >"$expected" <<EOF
   11  12/18 foo@example.edu    test<<This is a multi-part message in MIME forma
 EOF
-scan -width 80 $msgnum > $actual 2>&1
-check $expected $actual
+scan -width 80 last > "$actual" 2>&1
+check "$expected" "$actual"
 
 # check show (mhl)
-cat > $expected <<EOF
+cat >"$expected" <<EOF
 (Message inbox:11)
 
 Date:    Sun, 18 Dec 2005 00:52:39 +0100
@@ -49,14 +52,13 @@ Subject: test
 
 This is a multi-part message in MIME format.
 
-
 I am a stupid spammer.
 EOF
-show $msgnum > $actual 2>&1
-check $expected $actual
+show last > "$actual" 2>&1
+check "$expected" "$actual"
 
 # check mhshow
-cat > $expected <<EOF
+cat >"$expected" <<EOF
 Date:    Sun, 18 Dec 2005 00:52:39 +0100
 To:      bar@example.edu
 From:    foo@example.edu
@@ -69,7 +71,72 @@ This is a multi-part message in MIME format.
 
 I am a stupid spammer.
 EOF
-mhshow -nopause $msgnum > $actual 2>&1
-check $expected $actual
+mhshow -nopause last > "$actual" 2>&1
+check "$expected" "$actual"
+
+# check m_getfld() handling of empty header field
+msgfile="$MH_TEST_DIR/Mail/inbox/12"
+printf 'Date: Sat, 12 Jan 2013 09:07:01 -0600\nReceived:' >"$msgfile"
+cat >"$expected" <<EOF
+  12  01/12 
+EOF
+cat >"$expected_err" <<EOF
+scan: eof encountered in field "Received"
+??Format error (message 12) in component 2
+EOF
+
+scan -width 13 last >"$actual" 2>"$actual_err"
+check "$expected" "$actual"
+check "$expected_err" "$actual_err"
+
+# check m_getfld() handling of excessively long header field name
+msgfile="$MH_TEST_DIR/Mail/inbox/13"
+cat >"$msgfile" <<EOF
+Date: Tue, 15 Jan 2013 21:13:12 -0600
+ThisHeaderFieldNameIsWaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaayTooLong: OK
+EOF
+cat >"$expected" <<EOF
+  13  01/15 
+EOF
+
+scan -width 13 last >"$actual" 2>"$actual_err"
+check "$expected" "$actual"
+# Cygwin has a BUFSIZ of 1024 so the error message gets truncated.
+# Deal with that by grepping to verify that scan showed the proper error.
+verify_string_in_file() {
+  if grep "$1" "$2" >/dev/null; then
+    :
+  else
+    echo "$0: did not receive expected error message \"$1\""
+    failed=`expr ${failed:-0} + 1`
+  fi
+}
+verify_string_in_file 'scan: field name "ThisHeaderFieldNameIsWa' "$actual_err"
+verify_string_in_file 'exceeds 997' "$actual_err"
+verify_string_in_file '??Format error (message 13) in component 2' "$actual_err"
+rm -f "$actual_err"
+
+# check m_getfld() handling of long header field name without a colon
+msgfile="$MH_TEST_DIR/Mail/inbox/14"
+cat >"$msgfile" <<EOF
+Date: Thu, 17 Jan 2013 19:33:46 -0600
+If a header field name has at least 512 characters without a newline or colon, it will raise a format error in m_getfld().  Here is a test of that.  01234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901
+
+Test
+
+
+EOF
+cat >"$expected" <<EOF
+  14  01/17 
+EOF
+cat >"$expected_err" <<EOF
+scan: eol encountered in field "If a header field name has at least 512 characters without a newline or colon, it will raise a format error in m_getfld().  Here is a test of that.  01234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901"
+??Format error (message 14) in component 2
+EOF
+
+scan -width 13 last >"$actual" 2>"$actual_err"
+check "$expected" "$actual"
+check "$expected_err" "$actual_err"
+
 
 exit $failed
index afd4e41..b5119fb 100644 (file)
@@ -32,6 +32,7 @@ distout (char *drft, char *msgnam, char *backup)
     register char *resent;
     char name[NAMESZ], buffer[BUFSIZ];
     register FILE *ifp, *ofp;
+    m_getfld_state_t gstate = 0;
 
     if (rename (drft, strcpy (backup, m_backup (drft))) == NOTOK)
        adios (backup, "unable to rename %s to",drft);
@@ -46,12 +47,11 @@ distout (char *drft, char *msgnam, char *backup)
     lseek (hdrfd, (off_t) 0, SEEK_SET); /* msgnam not accurate */
     cpydata (hdrfd, fileno (ofp), msgnam, drft);
 
-    for (state = FLD, resent = NULL;;)
-       switch (state =
-               m_getfld (state, name, buffer, sizeof buffer, ifp)) {
+    for (resent = NULL;;) {
+       int buffersz = sizeof buffer;
+       switch (state = m_getfld (&gstate, name, buffer, &buffersz, ifp)) {
            case FLD: 
            case FLDPLUS: 
-           case FLDEOF: 
                if (uprf (name, "distribute-"))
                    snprintf (name, sizeof(name), "%s%s", "Resent", &name[10]);
                if (uprf (name, "distribution-"))
@@ -65,17 +65,14 @@ distout (char *drft, char *msgnam, char *backup)
                resent = add (buffer, resent);
                fprintf (ofp, "%s: %s", name, buffer);
                while (state == FLDPLUS) {
-                   state = m_getfld (state, name,
-                           buffer, sizeof buffer, ifp);
+                   buffersz = sizeof buffer;
+                   state = m_getfld (&gstate, name, buffer, &buffersz, ifp);
                    resent = add (buffer, resent);
                    fputs (buffer, ofp);
                }
-               if (state == FLDEOF)
-                   goto process;
                break;
 
            case BODY: 
-           case BODYEOF: 
                for (dp = buffer; *dp; dp++)
                    if (!isspace (*dp)) {
                        advise (NULL, BADTXT, "draft");
@@ -99,7 +96,9 @@ distout (char *drft, char *msgnam, char *backup)
            default: 
                adios (NULL, "getfld() returned %d", state);
        }
+    }
 process: ;
+    m_getfld_state_destroy (&gstate);
     fclose (ifp);
     fflush (ofp);
 
@@ -131,6 +130,7 @@ ready_msg (char *msgnam)
     char name[NAMESZ], buffer[BUFSIZ], tmpfil[BUFSIZ];
     register FILE *ifp, *ofp;
     char *cp = NULL;
+    m_getfld_state_t gstate = 0;
 
     if (hdrfd != NOTOK)
        close (hdrfd), hdrfd = NOTOK;
@@ -151,26 +151,22 @@ ready_msg (char *msgnam)
        adios (NULL, "no file descriptors -- you lose big");
     unlink (tmpfil);
 
-    for (state = FLD;;)
-       switch (state =
-               m_getfld (state, name, buffer, sizeof buffer, ifp)) {
+    for (;;) {
+       int buffersz = sizeof buffer;
+       switch (state = m_getfld (&gstate, name, buffer, &buffersz, ifp)) {
            case FLD: 
            case FLDPLUS: 
-           case FLDEOF: 
                if (uprf (name, "resent"))
                    fprintf (ofp, "Prev-");
                fprintf (ofp, "%s: %s", name, buffer);
                while (state == FLDPLUS) {
-                   state = m_getfld (state, name,
-                           buffer, sizeof buffer, ifp);
+                   buffersz = sizeof buffer;
+                   state = m_getfld (&gstate, name, buffer, &buffersz, ifp);
                    fputs (buffer, ofp);
                }
-               if (state == FLDEOF)
-                   goto process;
                break;
 
            case BODY: 
-           case BODYEOF: 
                fclose (ofp);
 
                 cp = m_mktemp2(NULL, "dist", &txtfd, NULL);
@@ -185,8 +181,8 @@ ready_msg (char *msgnam)
                unlink (tmpfil);
                fprintf (ofp, "\n%s", buffer);
                while (state == BODY) {
-                   state = m_getfld (state, name,
-                           buffer, sizeof buffer, ifp);
+                   buffersz = sizeof buffer;
+                   state = m_getfld (&gstate, name, buffer, &buffersz, ifp);
                    fputs (buffer, ofp);
                }
            case FILEEOF: 
@@ -199,7 +195,9 @@ ready_msg (char *msgnam)
            default: 
                adios (NULL, "getfld() returned %d", state);
        }
+    }
 process: ;
+    m_getfld_state_destroy (&gstate);
     fclose (ifp);
     fclose (ofp);
 }
index 8625585..cf043c4 100644 (file)
@@ -46,7 +46,7 @@ build_form (char *form, char *digest, int *dat, char *from, char *to,
            char *cc, char *fcc, char *subject, char *inputfile)
 {
     int        in;
-    int fmtsize, state, char_read = 0;
+    int fmtsize, state;
     int i;
     register char *nfs;
     char *line, tmpfil[BUFSIZ], name[NAMESZ], **ap;
@@ -54,6 +54,7 @@ build_form (char *form, char *digest, int *dat, char *from, char *to,
     register struct comp *cptr;
     struct format *fmt;
     char *cp = NULL;
+    m_getfld_state_t gstate = 0;
 
     /*
      * Open the message we'll be scanning for components
@@ -86,8 +87,9 @@ build_form (char *form, char *digest, int *dat, char *from, char *to,
      * these routines?
      */
 
-    for (state = FLD;;) {
-       state = m_getfld(state, name, msgbuf, sizeof(msgbuf), tmp);
+    for (;;) {
+       int msg_count = sizeof msgbuf;
+       state = m_getfld (&gstate, name, msgbuf, &msg_count, tmp);
        switch (state) {
            case FLD:
            case FLDPLUS:
@@ -99,16 +101,16 @@ build_form (char *form, char *digest, int *dat, char *from, char *to,
 
                i = fmt_addcomptext(name, msgbuf);
                if (i != -1) {
-                   char_read += msg_count;
                    while (state == FLDPLUS) {
-                       state = m_getfld(state, name, msgbuf,
-                                        sizeof(msgbuf), tmp);
+                       msg_count = sizeof msgbuf;
+                       state = m_getfld (&gstate, name, msgbuf, &msg_count, tmp);
                        fmt_appendcomp(i, name, msgbuf);
-                       char_read += msg_count;
                    }
                }
-               while (state == FLDPLUS)
-                   state = m_getfld(state, name, msgbuf, sizeof(msgbuf), tmp);
+               while (state == FLDPLUS) {
+                   msg_count = sizeof msgbuf;
+                   state = m_getfld (&gstate, name, msgbuf, &msg_count, tmp);
+               }
                break;
 
            case LENERR:
@@ -121,6 +123,7 @@ build_form (char *form, char *digest, int *dat, char *from, char *to,
                adios(NULL, "m_getfld() returned %d", state);
        }
     }
+    m_getfld_state_destroy (&gstate);
 
     /*
      * Override any components just in case they were included in the
index 3ec47fa..2d791f6 100644 (file)
--- a/uip/inc.c
+++ b/uip/inc.c
@@ -569,6 +569,7 @@ go_to_it:
        fflush (stdout);
     }
 
+
     /*
      * Get the mail from a POP server
      */
@@ -702,7 +703,7 @@ go_to_it:
      * Get the mail from file (usually mail spool)
      */
     if (inc_type == INC_FILE && Maildir == NULL) {
-       m_unknown (in);         /* the MAGIC invocation... */
+       scan_detect_mbox_style (in);            /* the MAGIC invocation... */
        hghnum = msgnum = mp->hghmsg;
        for (;;) {
            /*
@@ -869,6 +870,8 @@ go_to_it:
        free (Maildir); /* From now on Maildir is just a flag - don't dref! */
     }
 
+    scan_finished ();
+
     if (incerr < 0) {          /* error */
        if (locked) {
             GETGROUPPRIVS();      /* Be sure we can unlock mail file */
index 653c5b3..7d187b3 100644 (file)
@@ -134,6 +134,7 @@ build_mime (char *infile, int directives)
     struct part **pp;
     CT ct;
     FILE *in;
+    m_getfld_state_t gstate = 0;
 
     directive_init(directives);
 
@@ -161,11 +162,11 @@ build_mime (char *infile, int directives)
      * draft into the linked list of header fields for
      * the new MIME message.
      */
-    for (compnum = 1, state = FLD;;) {
-       switch (state = m_getfld (state, name, buf, sizeof(buf), in)) {
+    for (compnum = 1;;) {
+       int bufsz = sizeof buf;
+       switch (state = m_getfld (&gstate, name, buf, &bufsz, in)) {
        case FLD:
        case FLDPLUS:
-       case FLDEOF:
            compnum++;
 
            /* abort if draft has Mime-Version header field */
@@ -178,8 +179,10 @@ build_mime (char *infile, int directives)
 
            /* ignore any Content-Type fields in the header */
            if (!mh_strcasecmp (name, TYPE_FIELD)) {
-               while (state == FLDPLUS)
-                   state = m_getfld (state, name, buf, sizeof(buf), in);
+               while (state == FLDPLUS) {
+                   bufsz = sizeof buf;
+                   state = m_getfld (&gstate, name, buf, &bufsz, in);
+               }
                goto finish_field;
            }
 
@@ -189,7 +192,8 @@ build_mime (char *infile, int directives)
 
            /* if necessary, get rest of field */
            while (state == FLDPLUS) {
-               state = m_getfld (state, name, buf, sizeof(buf), in);
+               bufsz = sizeof buf;
+               state = m_getfld (&gstate, name, buf, &bufsz, in);
                vp = add (buf, vp);     /* add to previous value */
            }
 
@@ -198,16 +202,13 @@ build_mime (char *infile, int directives)
 
 finish_field:
            /* if this wasn't the last header field, then continue */
-           if (state != FLDEOF)
-               continue;
-           /* else fall... */
+           continue;
 
        case FILEEOF:
            adios (NULL, "draft has empty body -- no directives!");
            /* NOTREACHED */
 
        case BODY:
-       case BODYEOF:
            fseek (in, (long) (-strlen (buf)), SEEK_CUR);
            break;
 
@@ -220,6 +221,7 @@ finish_field:
        }
        break;
     }
+    m_getfld_state_destroy (&gstate);
 
     /*
      * Now add the MIME-Version header field
index 5f7777d..25ac9ab 100644 (file)
@@ -376,25 +376,27 @@ find_cache_aux2 (char *mapfile, char *id, char *mapname, int namelen)
     int        state;
     char buf[BUFSIZ], name[NAMESZ];
     FILE *fp;
+    m_getfld_state_t gstate = 0;
 
     if (!(fp = lkfopen (mapfile, "r")))
        return NOTOK;
 
-    for (state = FLD;;) {
+    for (;;) {
        int result;
        char *cp, *dp;
+       int bufsz = sizeof buf;
 
-       switch (state = m_getfld (state, name, buf, sizeof(buf), fp)) {
+       switch (state = m_getfld (&gstate, name, buf, &bufsz, fp)) {
            case FLD:
            case FLDPLUS:
-           case FLDEOF:
                strncpy (mapname, name, namelen);
                if (state != FLDPLUS)
                    cp = buf;
                else {
                    cp = add (buf, NULL);
                    while (state == FLDPLUS) {
-                       state = m_getfld (state, name, buf, sizeof(buf), fp);
+                       bufsz = sizeof buf;
+                       state = m_getfld (&gstate, name, buf, &bufsz, fp);
                        cp = add (buf, cp);
                    }
                }
@@ -410,18 +412,16 @@ find_cache_aux2 (char *mapfile, char *id, char *mapname, int namelen)
                    lkfclose (fp, mapfile);
                    return OK;
                }
-               if (state != FLDEOF)
-                   continue;
-               /* else fall... */
+               continue;
 
            case BODY:
-           case BODYEOF:
            case FILEEOF:
            default:
                break;
        }
        break;
     }
+    m_getfld_state_destroy (&gstate);
 
     lkfclose (fp, mapfile);
     return NOTOK;
index e075933..95f8c54 100644 (file)
@@ -336,7 +336,8 @@ static void quitser (int);
 static void mhladios (char *, char *, ...);
 static void mhldone (int);
 static void m_popen (char *);
-static void filterbody (struct mcomp *, char *, int, int, FILE *);
+static void filterbody (struct mcomp *, char *, int, int, FILE *,
+                        m_getfld_state_t);
 static void compile_formatfield(struct mcomp *);
 static void compile_filterargs (void);
 
@@ -936,6 +937,7 @@ mhlfile (FILE *fp, char *mname, int ofilen, int ofilec)
     int state, bucket;
     struct mcomp *c1, *c2, *c3;
     char **ip, name[NAMESZ], buf[BUFSIZ];
+    m_getfld_state_t gstate = 0;
 
     compile_filterargs();
 
@@ -997,15 +999,17 @@ mhlfile (FILE *fp, char *mname, int ofilen, int ofilec)
        }
     }
 
-    for (state = FLD;;) {
-       switch (state = m_getfld (state, name, buf, sizeof(buf), fp)) {
+    for (;;) {
+       int bufsz = sizeof buf;
+       switch (state = m_getfld (&gstate, name, buf, &bufsz, fp)) {
            case FLD: 
            case FLDPLUS: 
                bucket = fmt_addcomptext(name, buf);
                for (ip = ignores; *ip; ip++)
                    if (!mh_strcasecmp (name, *ip)) {
                        while (state == FLDPLUS) {
-                           state = m_getfld (state, name, buf, sizeof(buf), fp);
+                           bufsz = sizeof buf;
+                           state = m_getfld (&gstate, name, buf, &bufsz, fp);
                            fmt_appendcomp(bucket, name, buf);
                        }
                        break;
@@ -1027,7 +1031,8 @@ mhlfile (FILE *fp, char *mname, int ofilen, int ofilec)
                if (c1 == NULL)
                    c1 = add_queue (&msghd, &msgtl, name, buf, 0);
                while (state == FLDPLUS) {
-                   state = m_getfld (state, name, buf, sizeof(buf), fp);
+                   bufsz = sizeof buf;
+                   state = m_getfld (&gstate, name, buf, &bufsz, fp);
                    c1->c_text = add (buf, c1->c_text);
                    fmt_appendcomp(bucket, name, buf);
                }
@@ -1060,14 +1065,15 @@ mhlfile (FILE *fp, char *mname, int ofilen, int ofilec)
                    if (dobody && !mh_strcasecmp (c1->c_name, "body")) {
                        if (c1->c_flags & FMTFILTER && state == BODY &&
                                                        formatproc != NULL) {
-                           filterbody(c1, buf, sizeof(buf), state, fp);
+                           filterbody(c1, buf, sizeof(buf), state, fp, gstate);
                        } else {
                            holder.c_text = mh_xmalloc (sizeof(buf));
                            strncpy (holder.c_text, buf, sizeof(buf));
                            while (state == BODY) {
                                putcomp (c1, &holder, BODYCOMP);
-                               state = m_getfld (state, name, holder.c_text,
-                                           sizeof(buf), fp);
+                               bufsz = sizeof buf;
+                               state = m_getfld (&gstate, name, holder.c_text,
+                                           &bufsz, fp);
                            }
                            free (holder.c_text);
                            holder.c_text = NULL;
@@ -1093,6 +1099,7 @@ mhlfile (FILE *fp, char *mname, int ofilen, int ofilec)
                adios (NULL, "getfld() returned %d", state);
        }
     }
+    m_getfld_state_destroy (&gstate);
 }
 
 
@@ -1767,7 +1774,8 @@ compile_filterargs (void)
  */
 
 static void
-filterbody (struct mcomp *c1, char *buf, int bufsz, int state, FILE *fp)
+filterbody (struct mcomp *c1, char *buf, int bufsz, int state, FILE *fp,
+            m_getfld_state_t gstate)
 {
     struct mcomp holder;
     char name[NAMESZ];
@@ -1821,8 +1829,9 @@ filterbody (struct mcomp *c1, char *buf, int bufsz, int state, FILE *fp)
         */
 
        while (state == BODY) {
+           int bufsz2 = bufsz;
            write(fdinput[1], buf, strlen(buf));
-           state = m_getfld(state, name, buf, bufsz, fp);
+           state = m_getfld (&gstate, name, buf, &bufsz2, fp);
        }
 
        /*
index 93f1f26..d4c6f10 100644 (file)
@@ -262,6 +262,7 @@ get_content (FILE *in, char *file, int toplevel)
     char *np, *vp;
     CT ct;
     HF hp;
+    m_getfld_state_t gstate = 0;
 
     /* allocate the content structure */
     if (!(ct = (CT) calloc (1, sizeof(*ct))))
@@ -275,11 +276,11 @@ get_content (FILE *in, char *file, int toplevel)
      * Parse the header fields for this
      * content into a linked list.
      */
-    for (compnum = 1, state = FLD;;) {
-       switch (state = m_getfld (state, name, buf, sizeof(buf), in)) {
+    for (compnum = 1;;) {
+       int bufsz = sizeof buf;
+       switch (state = m_getfld (&gstate, name, buf, &bufsz, in)) {
        case FLD:
        case FLDPLUS:
-       case FLDEOF:
            compnum++;
 
            /* get copies of the buffers */
@@ -288,22 +289,19 @@ get_content (FILE *in, char *file, int toplevel)
 
            /* if necessary, get rest of field */
            while (state == FLDPLUS) {
-               state = m_getfld (state, name, buf, sizeof(buf), in);
+               bufsz = sizeof buf;
+               state = m_getfld (&gstate, name, buf, &bufsz, in);
                vp = add (buf, vp);     /* add to previous value */
            }
 
            /* Now add the header data to the list */
            add_header (ct, np, vp);
 
-           /* continue, if this isn't the last header field */
-           if (state != FLDEOF) {
-               ct->c_begin = ftell (in) + 1;
-               continue;
-           }
-           /* else fall... */
+           /* continue, to see if this isn't the last header field */
+           ct->c_begin = ftell (in) + 1;
+           continue;
 
        case BODY:
-       case BODYEOF:
            ct->c_begin = ftell (in) - strlen (buf);
            break;
 
@@ -322,6 +320,7 @@ get_content (FILE *in, char *file, int toplevel)
        /* break out of the loop */
        break;
     }
+    m_getfld_state_destroy (&gstate);
 
     /*
      * Read the content headers.  We will parse the
index 6069cac..98f9afe 100644 (file)
--- a/uip/msh.c
+++ b/uip/msh.c
@@ -155,6 +155,8 @@ void seq_setcur (struct msgs *, int);
 void padios (char *, char *, ...);
 void padvise (char *, char *, ...);
 
+extern m_getfld_state_t gstate;        /* use the gstate in scansbr.c */
+
 
 /*
  * static prototypes
@@ -341,6 +343,7 @@ main (int argc, char **argv)
     display_info (id > 0 ? scansw : 0);
 
     msh (id > 0 ? scansw : 0);
+    scan_finished ();
 
     m_reset ();
     
@@ -682,7 +685,7 @@ setup (char *file)
     mp->msgattrs[0] = getcpy ("unseen");
     mp->msgattrs[1] = NULL;
 
-    m_unknown (fp);            /* the MAGIC invocation */    
+    scan_detect_mbox_style (fp);               /* the MAGIC invocation */
     if (fmsh) {
        free (fmsh);
        fmsh = NULL;
@@ -805,7 +808,7 @@ msh_ready (int msgnum, int full)
        return yp;
     }
 
-    m_eomsbr ((int (*)()) 0);  /* XXX */
+    scan_eom_action ((int (*)()) 0);   /* XXX */
     fseek (fp, Msgs[msgnum].m_start, SEEK_SET);
     return fp;
 }
@@ -974,15 +977,16 @@ readid (int msgnum)
        return Msgs[msgnum].m_bboard_id;
 
     zp = msh_ready (msgnum, 0);
-    for (state = FLD;;)
-       switch (state = m_getfld (state, name, buf, sizeof(buf), zp)) {
+    for (;;) {
+       int bufsz = sizeof buf;
+       switch (state = m_getfld (&gstate, name, buf, &bufsz, zp)) {
            case FLD: 
-           case FLDEOF: 
            case FLDPLUS: 
                if (!mh_strcasecmp (name, BBoard_ID)) {
                    bp = getcpy (buf);
                    while (state == FLDPLUS) {
-                       state = m_getfld (state, name, buf, sizeof(buf), zp);
+                       bufsz = sizeof buf;
+                       state = m_getfld (&gstate, name, buf, &bufsz, zp);
                        bp = add (buf, bp);
                    }
                    i = atoi (bp);
@@ -992,14 +996,16 @@ readid (int msgnum)
                    else
                        continue;
                }
-               while (state == FLDPLUS)
-                   state = m_getfld (state, name, buf, sizeof(buf), zp);
-               if (state != FLDEOF)
-                   continue;
+               while (state == FLDPLUS) {
+                   bufsz = sizeof buf;
+                   state = m_getfld (&gstate, name, buf, &bufsz, zp);
+               }
+               continue;
 
            default: 
                return 0;
        }
+    }
 }
 
 
index 8212375..6ea1126 100644 (file)
@@ -51,6 +51,8 @@ static int process (int, char *, int, char **);
 static void copy_message (int, FILE *);
 static void copy_digest (int, FILE *);
 
+extern m_getfld_state_t gstate;        /* use the gstate in scansbr.c */
+
 void
 forkcmd (char **args, char *pgm)
 {
@@ -950,7 +952,7 @@ forw (char *proc, char *filter, int vecp, char **vec)
                        args[i++] = getcpy (m_name (msgnum));
                args[i] = NULL;
                mhlsbr (i, args, mhl_action);
-               m_eomsbr ((int (*) ()) 0);
+               scan_eom_action ((int (*) ()) 0);
                fclose (stdout);
                _exit (0);
 
@@ -2211,7 +2213,7 @@ finish: ;
        if (mp->numsel == 1 && headersw)
            show (mp->lowsel);
        mhlsbr (vecp, vec, mhl_action);
-       m_eomsbr ((int (*)()) 0);
+       scan_eom_action ((int (*)()) 0);
        while (msgp < vecp)
            free (vec[msgp++]);
     } else {
@@ -2283,7 +2285,7 @@ mhl_action (char *name)
 
     mhlfp = msh_ready (msgnum, 1);
     if (!fmsh)
-       m_eomsbr (eom_action);
+       scan_eom_action (eom_action);
 
     return mhlfp;
 }
@@ -2341,11 +2343,11 @@ is_nontext (int msgnum)
 
     fp = msh_ready (msgnum, 1);
 
-    for (state = FLD;;)
-       switch (state = m_getfld (state, name, buf, sizeof buf, fp)) {
+    for (;;) {
+       int bufsz = sizeof buf;
+       switch (state = m_getfld (&gstate, name, buf, &bufsz, fp)) {
        case FLD:
        case FLDPLUS:
-       case FLDEOF:
            /*
             * Check Content-Type field
             */
@@ -2355,7 +2357,8 @@ is_nontext (int msgnum)
 
                cp = add (buf, NULL);
                while (state == FLDPLUS) {
-                   state = m_getfld (state, name, buf, sizeof buf, fp);
+                   bufsz = sizeof buf;
+                   state = m_getfld (&gstate, name, buf, &bufsz, fp);
                    cp = add (buf, cp);
                }
                bp = cp;
@@ -2458,7 +2461,8 @@ out:
            if (!mh_strcasecmp (name, ENCODING_FIELD)) {
                cp = add (buf, NULL);
                while (state == FLDPLUS) {
-                   state = m_getfld (state, name, buf, sizeof buf, fp);
+                   bufsz = sizeof buf;
+                   state = m_getfld (&gstate, name, buf, &bufsz, fp);
                    cp = add (buf, cp);
                }
                for (bp = cp; isspace (*bp); bp++)
@@ -2482,8 +2486,10 @@ out:
             * Just skip the rest of this header
             * field and go to next one.
             */
-           while (state == FLDPLUS)
-               state = m_getfld (state, name, buf, sizeof(buf), fp);
+           while (state == FLDPLUS) {
+               bufsz = sizeof buf;
+               state = m_getfld (&gstate, name, buf, &bufsz, fp);
+           }
            break;
 
            /*
@@ -2493,6 +2499,7 @@ out:
        default:
            return 0;
        }
+    }
 }
 
 
@@ -2645,15 +2652,17 @@ get_fields (char *datesw, char *subjsw, int msgnum, struct Msg *msgp)
     register FILE *zp;
 
     zp = msh_ready (msgnum, 0);
-    for (state = FLD;;) {
-       switch (state = m_getfld (state, name, buf, sizeof buf, zp)) {
+
+    for (;;) {
+       int bufsz = sizeof buf;
+       switch (state = m_getfld (&gstate, name, buf, &bufsz, zp)) {
            case FLD: 
-           case FLDEOF: 
            case FLDPLUS: 
                if (!mh_strcasecmp (name, datesw)) {
                    bp = getcpy (buf);
                    while (state == FLDPLUS) {
-                       state = m_getfld (state, name, buf, sizeof buf, zp);
+                       bufsz = sizeof buf;
+                       state = m_getfld (&gstate, name, buf, &bufsz, zp);
                        bp = add (buf, bp);
                    }
                    if ((tw = dparsetime (bp)) == NULL)
@@ -2670,7 +2679,8 @@ get_fields (char *datesw, char *subjsw, int msgnum, struct Msg *msgp)
                else if (subjsw && !mh_strcasecmp(name, subjsw)) {
                    bp = getcpy (buf);
                    while (state == FLDPLUS) {
-                       state = m_getfld (state, name, buf, sizeof buf, zp);
+                       bufsz = sizeof buf;
+                       state = m_getfld (&gstate, name, buf, &bufsz, zp);
                        bp = add (buf, bp);
                    }
                    msgp->m_scanl = sosmash(subjsw, bp);
@@ -2679,13 +2689,14 @@ get_fields (char *datesw, char *subjsw, int msgnum, struct Msg *msgp)
                    else
                        subjsw = (char *)0;/* subject done, need date */
                } else {
-                   while (state == FLDPLUS)    /* flush this one */
-                       state = m_getfld (state, name, buf, sizeof buf, zp);
+                   while (state == FLDPLUS) {  /* flush this one */
+                       bufsz = sizeof buf;
+                       state = m_getfld (&gstate, name, buf, &bufsz, zp);
+                   }
                }
                continue;
 
            case BODY: 
-           case BODYEOF: 
            case FILEEOF: 
                break;
 
index e88a0dd..e391ba2 100644 (file)
--- a/uip/new.c
+++ b/uip/new.c
@@ -99,6 +99,7 @@ get_msgnums(char *folder, char *sequences[])
     char name[NAMESZ], field[BUFSIZ];
     char *cp;
     char *msgnums = NULL, *this_msgnums, *old_msgnums;
+    m_getfld_state_t gstate = 0;
 
     /* no sequences file -> no messages */
     if (fp == NULL) {
@@ -106,16 +107,16 @@ get_msgnums(char *folder, char *sequences[])
     }
 
     /* copied from seq_read.c:seq_public */
-    for (state = FLD;;) {
-        switch (state = m_getfld (state, name, field, sizeof(field), fp)) {
+    for (;;) {
+       int fieldsz = sizeof field;
+       switch (state = m_getfld (&gstate, name, field, &fieldsz, fp)) {
             case FLD:
             case FLDPLUS:
-            case FLDEOF:
                 if (state == FLDPLUS) {
                     cp = getcpy (field);
                     while (state == FLDPLUS) {
-                        state = m_getfld (state, name, field,
-                                          sizeof(field), fp);
+                       fieldsz = sizeof field;
+                       state = m_getfld (&gstate, name, field, &fieldsz, fp);
                         cp = add (field, cp);
                     }
 
@@ -150,12 +151,9 @@ get_msgnums(char *folder, char *sequences[])
                     }
                 }
 
-                if (state == FLDEOF)
-                    break;
                 continue;
 
             case BODY:
-            case BODYEOF:
                 adios (NULL, "no blank lines are permitted in %s", seqfile);
                 /* fall */
 
@@ -167,6 +165,7 @@ get_msgnums(char *folder, char *sequences[])
         }
         break;  /* break from for loop */
     }
+    m_getfld_state_destroy (&gstate);
 
     fclose(fp);
 
index 7b64f81..c572cde 100644 (file)
@@ -934,28 +934,28 @@ plist
     register char *bp;
     char buf[BUFSIZ], name[NAMESZ];
     register struct tws *tw;
+    m_getfld_state_t gstate = 0;
     NMH_UNUSED (stop);
 
     fseek (fp, start, SEEK_SET);
-    for (state = FLD, bp = NULL;;) {
-       switch (state = m_getfld (state, name, buf, sizeof buf, fp)) {
+    for (bp = NULL;;) {
+       int bufsz = sizeof buf;
+       switch (state = m_getfld (&gstate, name, buf, &bufsz, fp)) {
            case FLD: 
-           case FLDEOF: 
            case FLDPLUS: 
                if (bp != NULL)
                    free (bp), bp = NULL;
                bp = add (buf, NULL);
                while (state == FLDPLUS) {
-                   state = m_getfld (state, name, buf, sizeof buf, fp);
+                   bufsz = sizeof buf;
+                   state = m_getfld (&gstate, name, buf, &bufsz, fp);
                    bp = add (buf, bp);
                }
                if (!mh_strcasecmp (name, n->n_datef))
                    break;
-               if (state != FLDEOF)
-                   continue;
+               continue;
 
            case BODY: 
-           case BODYEOF: 
            case FILEEOF: 
            case LENERR: 
            case FMTERR: 
@@ -970,6 +970,7 @@ plist
        }
        break;
     }
+    m_getfld_state_destroy (&gstate);
 
     if ((tw = dparsetime (bp)) == NULL)
        advise (NULL, "unable to parse %s field in message %d, matching...",
index f6f7924..59a8c8b 100644 (file)
@@ -286,6 +286,7 @@ main (int argc, char **argv)
     char *cp, *msg = NULL, **argp, **arguments, *envelope;
     char buf[BUFSIZ], name[NAMESZ];
     FILE *in, *out;
+    m_getfld_state_t gstate = 0;
 
 #ifdef LOCALE
     setlocale(LC_ALL, "");
@@ -522,7 +523,7 @@ main (int argc, char **argv)
     start_headers ();
     if (debug) {
        verbose++;
-       discard (out = stdout); /* XXX: reference discard() to help loader */
+       out = stdout;
     } else {
        if (whomsw) {
            if ((out = fopen ("/dev/null", "w")) == NULL)
@@ -542,32 +543,30 @@ main (int argc, char **argv)
 
     hdrtab = msgstate == NORMAL ? NHeaders : RHeaders;
 
-    for (compnum = 1, state = FLD;;) {
-       switch (state = m_getfld (state, name, buf, sizeof(buf), in)) {
+    for (compnum = 1;;) {
+       int bufsz = sizeof buf;
+       switch (state = m_getfld (&gstate, name, buf, &bufsz, in)) {
            case FLD: 
-           case FLDEOF: 
            case FLDPLUS: 
                compnum++;
                cp = add (buf, NULL);
                while (state == FLDPLUS) {
-                   state = m_getfld (state, name, buf, sizeof(buf), in);
+                   bufsz = sizeof buf;
+                   state = m_getfld (&gstate, name, buf, &bufsz, in);
                    cp = add (buf, cp);
                }
                putfmt (name, cp, out);
                free (cp);
-               if (state != FLDEOF)
-                   continue;
-               finish_headers (out);
-               break;
+               continue;
 
            case BODY: 
-           case BODYEOF: 
                finish_headers (out);
                if (whomsw)
                    break;
                fprintf (out, "\n%s", buf);
                while (state == BODY) {
-                   state = m_getfld (state, name, buf, sizeof(buf), in);
+                   bufsz = sizeof buf;
+                   state = m_getfld (&gstate, name, buf, &bufsz, in);
                    fputs (buf, out);
                }
                break;
@@ -585,6 +584,7 @@ main (int argc, char **argv)
        }
        break;
     }
+    m_getfld_state_destroy (&gstate);
 
     if (pfd != NOTOK)
        anno ();
index d950163..563da05 100644 (file)
@@ -78,6 +78,7 @@ main (int argc, char **argv)
     char **arguments, **argp;
     FILE *in, *out;
     char *tfile = NULL;
+    m_getfld_state_t gstate = 0;
 
 #ifdef LOCALE
     setlocale(LC_ALL, "");
@@ -202,10 +203,10 @@ main (int argc, char **argv)
     /*
      * Loop through the lines of the draft skeleton.
      */
-    for (state = FLD;;) {
-       switch (state = m_getfld (state, name, field, sizeof(field), in)) {
+    for (;;) {
+       int fieldsz = sizeof field;
+       switch (state = m_getfld (&gstate, name, field, &fieldsz, in)) {
            case FLD: 
-           case FLDEOF: 
            case FLDPLUS: 
                /*
                 * Check if the value of field contains anything
@@ -220,8 +221,8 @@ main (int argc, char **argv)
                    printf ("%s:%s", name, field);
                    fprintf (out, "%s:%s", name, field);
                    while (state == FLDPLUS) {
-                       state =
-                           m_getfld (state, name, field, sizeof(field), in);
+                       fieldsz = sizeof field;
+                       state = m_getfld (&gstate, name, field, &fieldsz, in);
                        printf ("%s", field);
                        fprintf (out, "%s", field);
                    }
@@ -251,17 +252,9 @@ abort:
                    }
                }
 
-               if (state == FLDEOF) {  /* moby hack */
-                   fprintf (out, "--------\n");
-                   printf ("--------\n");
-                   if (!body)
-                       break;
-                   goto no_body;
-               }
                continue;
 
            case BODY: 
-           case BODYEOF:
            case FILEEOF: 
                if (!body)
                    break;
@@ -287,13 +280,14 @@ abort:
                        if (!rapid && !sigint)
                            printf ("%s", field);
                    } while (state == BODY &&
-                           (state = m_getfld (state, name, field, sizeof(field), in)));
+                           (fieldsz = sizeof field,
+                            state = m_getfld (&gstate, name, field, &fieldsz, in)));
                    if (prepend || !body)
                        break;
                    else
                        printf ("\n--------Enter additional text\n\n");
                }
-no_body:
+
                fflush (stdout);
                for (;;) {
                    getln (field, sizeof(field));
@@ -310,6 +304,7 @@ no_body:
        }
        break;
     }
+    m_getfld_state_destroy (&gstate);
 
     if (body)
        printf ("--------\n");
index 8f88639..33890d5 100644 (file)
@@ -176,6 +176,7 @@ rcvdistout (FILE *inb, char *form, char *addrs)
     char *cp, *scanl, name[NAMESZ], tmpbuf[SBUFSIZ];
     register struct comp *cptr;
     FILE *out;
+    m_getfld_state_t gstate = 0;
 
     if (!(out = fopen (drft, "w")))
        adios (drft, "unable to create");
@@ -195,22 +196,26 @@ rcvdistout (FILE *inb, char *form, char *addrs)
     if (cptr)
        cptr->c_text = addrs;
 
-    for (state = FLD;;) {
-       switch (state = m_getfld (state, name, tmpbuf, SBUFSIZ, inb)) {
+    for (;;) {
+       int msg_count = SBUFSIZ;
+       switch (state = m_getfld (&gstate, name, tmpbuf, &msg_count, inb)) {
            case FLD: 
            case FLDPLUS: 
                i = fmt_addcomptext(name, tmpbuf);
                if (i != -1) {
                    char_read += msg_count;
                    while (state == FLDPLUS) {
-                       state = m_getfld (state, name, tmpbuf, SBUFSIZ, inb);
+                       msg_count = SBUFSIZ;
+                       state = m_getfld (&gstate, name, tmpbuf, &msg_count, inb);
                        fmt_appendcomp(i, name, tmpbuf);
                        char_read += msg_count;
                    }
                }
 
-               while (state == FLDPLUS)
-                   state = m_getfld (state, name, tmpbuf, SBUFSIZ, inb);
+               while (state == FLDPLUS) {
+                   msg_count = SBUFSIZ;
+                   state = m_getfld (&gstate, name, tmpbuf, &msg_count, inb);
+               }
                break;
 
            case LENERR: 
@@ -224,6 +229,7 @@ rcvdistout (FILE *inb, char *form, char *addrs)
        }
     }
 finished: ;
+    m_getfld_state_destroy (&gstate);
 
     i = format_len + char_read + 256;
     scanl = mh_xmalloc ((size_t) i + 2);
index 9cd77dd..92334c1 100644 (file)
@@ -257,6 +257,7 @@ header_fd (void)
     /* get new format string */
     nfs = new_fs (form, format, SCANFMT);
     scan (stdin, 0, 0, nfs, width, 0, 0, NULL, 0L, 0);
+    scan_finished ();
     if (newline)
         write (fd, "\n\r", 2);
     write (fd, scanl, strlen (scanl));
index 3dbc303..d81eb74 100644 (file)
@@ -72,6 +72,7 @@ replout (FILE *inb, char *msg, char *drft, struct msgs *mp, int outputlinelen,
     char name[NAMESZ], *scanl;
     unsigned char *cp;
     static int dat[5];                 /* aux. data for format routine */
+    m_getfld_state_t gstate = 0;
 
     FILE *out;
     NMH_UNUSED (msg);
@@ -131,8 +132,9 @@ replout (FILE *inb, char *msg, char *drft, struct msgs *mp, int outputlinelen,
     /*
      * pick any interesting stuff out of msg "inb"
      */
-    for (state = FLD;;) {
-       state = m_getfld (state, name, tmpbuf, sizeof(tmpbuf), inb);
+    for (;;) {
+       int msg_count = sizeof tmpbuf;
+       state = m_getfld (&gstate, name, tmpbuf, &msg_count, inb);
        switch (state) {
            case FLD: 
            case FLDPLUS: 
@@ -147,15 +149,17 @@ replout (FILE *inb, char *msg, char *drft, struct msgs *mp, int outputlinelen,
                if (i != -1) {
                    char_read += msg_count;
                    while (state == FLDPLUS) {
-                       state = m_getfld(state, name, tmpbuf,
-                                        sizeof(tmpbuf), inb);
+                       msg_count= sizeof tmpbuf;
+                       state = m_getfld (&gstate, name, tmpbuf, &msg_count, inb);
                        fmt_appendcomp(i, name, tmpbuf);
                        char_read += msg_count;
                    }
                }
 
-               while (state == FLDPLUS)
-                   state = m_getfld (state, name, tmpbuf, SBUFSIZ, inb);
+               while (state == FLDPLUS) {
+                   msg_count= sizeof tmpbuf;
+                   state = m_getfld (&gstate, name, tmpbuf, &msg_count, inb);
+               }
                break;
 
            case LENERR: 
@@ -168,6 +172,7 @@ replout (FILE *inb, char *msg, char *drft, struct msgs *mp, int outputlinelen,
                adios (NULL, "m_getfld() returned %d", state);
        }
     }
+    m_getfld_state_destroy (&gstate);
 
     /*
      * format and output the header lines.
index e1b9a16..459fcb0 100644 (file)
@@ -175,13 +175,14 @@ main (int argc, char **argv)
            printf ("FOLDER %s\t%s\n", file, dtimenow (1));
        }
 
-       m_unknown (in);
+       scan_detect_mbox_style (in);
        for (msgnum = 1; ; ++msgnum) {
            state = scan (in, msgnum, -1, nfs, width, 0, 0,
                    hdrflag ? file : NULL, 0L, 1);
            if (state != SCNMSG && state != SCNENC)
                break;
        }
+       scan_finished ();
        fclose (in);
        done (0);
     }
@@ -276,6 +277,7 @@ main (int argc, char **argv)
                    advise (NULL, "message %d: empty", msgnum);
                    break;
            }
+           scan_finished ();
            hdrflag = 0;
            fclose (in);
            if (ontty)
index b69f4a4..8fc8825 100644 (file)
@@ -36,6 +36,7 @@ static struct comp **used_buf = 0;    /* stack for comp that use buffers */
 static int dat[5];                     /* aux. data for format routine    */
 
 char *scanl = 0;                       /* text of most recent scanline    */
+m_getfld_state_t gstate;               /* for access by msh */
 
 #define DIEWRERR() adios (scnmsg, "write error on")
 
@@ -68,6 +69,7 @@ scan (FILE *inb, int innum, int outnum, char *nfs, int width, int curflg,
     char *scnmsg = NULL;
     FILE *scnout = NULL;
     char name[NAMESZ];
+    int bufsz;
     static int rlwidth, slwidth;
     static size_t scanl_size;
 
@@ -162,7 +164,9 @@ scan (FILE *inb, int innum, int outnum, char *nfs, int width, int curflg,
      * Get the first field.  If the message is non-empty
      * and we're doing an "inc", open the output file.
      */
-    if ((state = m_getfld (FLD, name, tmpbuf, rlwidth, inb)) == FILEEOF) {
+    bufsz = rlwidth;
+    m_getfld_state_reset (&gstate);
+    if ((state = m_getfld (&gstate, name, tmpbuf, &bufsz, inb)) == FILEEOF) {
        if (ferror(inb)) {
            advise("read", "unable to"); /* "read error" */
            return SCNFAT;
@@ -184,7 +188,8 @@ scan (FILE *inb, int innum, int outnum, char *nfs, int width, int curflg,
     }
 
     /* scan - main loop */
-    for (compnum = 1; ; state = m_getfld (state, name, tmpbuf, rlwidth, inb)) {
+    for (compnum = 1; ;
+       bufsz = rlwidth, state = m_getfld (&gstate, name, tmpbuf, &bufsz, inb)) {
        switch (state) {
            case FLD: 
            case FLDPLUS: 
@@ -215,7 +220,8 @@ scan (FILE *inb, int innum, int outnum, char *nfs, int width, int curflg,
                }
 
                while (state == FLDPLUS) {
-                   state = m_getfld (state, name, tmpbuf, rlwidth, inb);
+                   bufsz = rlwidth;
+                   state = m_getfld (&gstate, name, tmpbuf, &bufsz, inb);
                    if (outnum)
                        FPUTS (tmpbuf);
                }
@@ -229,8 +235,8 @@ scan (FILE *inb, int innum, int outnum, char *nfs, int width, int curflg,
                 */
 
                if ((i = strlen(tmpbuf)) < rlwidth) {
-                   state = m_getfld (state, name, tmpbuf + i,
-                                     rlwidth - i, inb);
+                   bufsz = rlwidth - i;
+                   state = m_getfld (&gstate, name, tmpbuf + i, &bufsz, inb);
                }
 
                if (! outnum) {
@@ -264,7 +270,8 @@ body:;
                }
 
                while (state == BODY) {
-                   state = m_getfld(state, name, tmpbuf, rlwidth, inb);
+                   bufsz = rlwidth;
+                   state = m_getfld (&gstate, name, tmpbuf, &bufsz, inb);
                    FPUTS(tmpbuf);
                }
                goto finished;
@@ -372,3 +379,18 @@ mh_fputs(char *s, FILE *stream)
     return (0);
 }
 
+/* The following three functions allow access to the global gstate above. */
+void
+scan_finished () {
+    m_getfld_state_destroy (&gstate);
+}
+
+void
+scan_detect_mbox_style (FILE *f) {
+    m_unknown (&gstate, f);
+}
+
+void
+scan_eom_action (int (*action)()) {
+    m_eomsbr (gstate, action);
+}
index dfd974f..4c44741 100644 (file)
@@ -591,6 +591,7 @@ splitmsg (char **vec, int vecp, char *drft, struct stat *st, int delay)
     char subject[BUFSIZ];
     char name[NAMESZ], partnum[BUFSIZ];
     FILE *in;
+    m_getfld_state_t gstate = 0;
 
     if ((in = fopen (drft, "r")) == NULL)
        adios (drft, "unable to open for reading");
@@ -602,19 +603,21 @@ splitmsg (char **vec, int vecp, char *drft, struct stat *st, int delay)
      * Scan through the message and examine the various header fields,
      * as well as locate the beginning of the message body.
      */
-    for (compnum = 1, state = FLD;;) {
-       switch (state = m_getfld (state, name, buffer, sizeof(buffer), in)) {
+    for (compnum = 1;;) {
+       int bufsz = sizeof buffer;
+       switch (state = m_getfld (&gstate, name, buffer, &bufsz, in)) {
            case FLD:
            case FLDPLUS:
-           case FLDEOF:
                compnum++;
 
                /*
                 * This header field is discarded.
                 */
                if (!mh_strcasecmp (name, "Message-ID")) {
-                   while (state == FLDPLUS)
-                       state = m_getfld (state, name, buffer, sizeof(buffer), in);
+                   while (state == FLDPLUS) {
+                       bufsz = sizeof buffer;
+                       state = m_getfld (&gstate, name, buffer, &bufsz, in);
+                   }
                } else if (uprf (name, XXX_FIELD_PRF)
                        || !mh_strcasecmp (name, VRSN_FIELD)
                        || !mh_strcasecmp (name, "Subject")
@@ -638,7 +641,8 @@ splitmsg (char **vec, int vecp, char *drft, struct stat *st, int delay)
 
                    dp = add (concat (name, ":", buffer, NULL), dp);
                    while (state == FLDPLUS) {
-                       state = m_getfld (state, name, buffer, sizeof(buffer), in);
+                       bufsz = sizeof buffer;
+                       state = m_getfld (&gstate, name, buffer, &bufsz, in);
                        dp = add (buffer, dp);
                    }
                } else {
@@ -648,19 +652,16 @@ splitmsg (char **vec, int vecp, char *drft, struct stat *st, int delay)
                     */
                    cp = add (concat (name, ":", buffer, NULL), cp);
                    while (state == FLDPLUS) {
-                       state = m_getfld (state, name, buffer, sizeof(buffer), in);
+                       bufsz = sizeof buffer;
+                       state = m_getfld (&gstate, name, buffer, &bufsz, in);
                        cp = add (buffer, cp);
                    }
                }
 
-               if (state != FLDEOF) {
-                   start = ftell (in) + 1;
-                   continue;
-               }
-               /* else fall... */
+               start = ftell (in) + 1;
+               continue;
 
           case BODY:
-          case BODYEOF:
           case FILEEOF:
                break;
 
@@ -674,6 +675,7 @@ splitmsg (char **vec, int vecp, char *drft, struct stat *st, int delay)
 
        break;
     }
+    m_getfld_state_destroy (&gstate);
     if (cp == NULL)
        adios (NULL, "headers missing from draft");
 
index 229e6ed..766aec9 100644 (file)
@@ -336,15 +336,16 @@ is_nontext (char *msgnam)
     char *cp;
     char buf[BUFSIZ], name[NAMESZ];
     FILE *fp;
+    m_getfld_state_t gstate = 0;
 
     if ((fp = fopen (msgnam, "r")) == NULL)
        return 0;
 
-    for (state = FLD;;) {
-       switch (state = m_getfld (state, name, buf, sizeof(buf), fp)) {
+    for (;;) {
+       int bufsz = sizeof buf;
+       switch (state = m_getfld (&gstate, name, buf, &bufsz, fp)) {
        case FLD:
        case FLDPLUS:
-       case FLDEOF:
            /*
             * Check Content-Type field
             */
@@ -354,7 +355,8 @@ is_nontext (char *msgnam)
 
                cp = add (buf, NULL);
                while (state == FLDPLUS) {
-                   state = m_getfld (state, name, buf, sizeof(buf), fp);
+                   bufsz = sizeof buf;
+                   state = m_getfld (&gstate, name, buf, &bufsz, fp);
                    cp = add (buf, cp);
                }
                bp = cp;
@@ -457,7 +459,8 @@ out:
            if (!mh_strcasecmp (name, ENCODING_FIELD)) {
                cp = add (buf, NULL);
                while (state == FLDPLUS) {
-                   state = m_getfld (state, name, buf, sizeof(buf), fp);
+                   bufsz = sizeof buf;
+                   state = m_getfld (&gstate, name, buf, &bufsz, fp);
                    cp = add (buf, cp);
                }
                for (bp = cp; isspace (*bp); bp++)
@@ -481,8 +484,10 @@ out:
             * Just skip the rest of this header
             * field and go to next one.
             */
-           while (state == FLDPLUS)
-               state = m_getfld (state, name, buf, sizeof(buf), fp);
+           while (state == FLDPLUS) {
+               bufsz = sizeof buf;
+               state = m_getfld (&gstate, name, buf, &bufsz, fp);
+           }
            break;
 
            /*
@@ -494,4 +499,5 @@ out:
            return 0;
        }
     }
+    m_getfld_state_destroy (&gstate);
 }
index 1c30dfc..3c5e512 100644 (file)
@@ -707,6 +707,7 @@ parse (int fd)
     char name[NAMESZ], field[BUFSIZ];
     struct pair *p, *q;
     FILE  *in;
+    m_getfld_state_t gstate = 0;
 
     if (parsed++)
        return 0;
@@ -730,14 +731,15 @@ parse (int fd)
      * Scan the headers of the message and build
      * a lookup table.
      */
-    for (i = 0, state = FLD;;) {
-       switch (state = m_getfld (state, name, field, sizeof(field), in)) {
+    for (i = 0;;) {
+       int fieldsz = sizeof field;
+       switch (state = m_getfld (&gstate, name, field, &fieldsz, in)) {
            case FLD: 
-           case FLDEOF: 
            case FLDPLUS: 
                lp = add (field, NULL);
                while (state == FLDPLUS) {
-                   state = m_getfld (state, name, field, sizeof(field), in);
+                   fieldsz = sizeof field;
+                   state = m_getfld (&gstate, name, field, &fieldsz, in);
                    lp = add (field, lp);
                }
                for (p = hdrs; p->p_name; p++) {
@@ -766,12 +768,9 @@ parse (int fd)
                    p++, i++;
                    p->p_name = NULL;
                }
-               if (state != FLDEOF)
-                   continue;
-               break;
+               continue;
 
            case BODY: 
-           case BODYEOF: 
            case FILEEOF: 
                break;
 
@@ -787,6 +786,7 @@ parse (int fd)
        }
        break;
     }
+    m_getfld_state_destroy (&gstate);
     fclose (in);
 
     if ((p = lookup (vars, "reply-to"))) {
@@ -1405,6 +1405,7 @@ suppress_duplicates (int fd, char *file)
     datum key, value;
     DBM *db;
     FILE *in;
+    m_getfld_state_t gstate = 0;
 
     if ((fd1 = dup (fd)) == -1)
        return -1;
@@ -1414,22 +1415,25 @@ suppress_duplicates (int fd, char *file)
     }
     rewind (in);
 
-    for (state = FLD;;) {
-       state = m_getfld (state, name, buf, sizeof(buf), in);
+    for (;;) {
+       int bufsz = sizeof buf;
+       state = m_getfld (&gstate, name, buf, &bufsz, in);
        switch (state) {
            case FLD:
            case FLDPLUS:
-           case FLDEOF:
                /* Search for the message ID */
                if (mh_strcasecmp (name, "Message-ID")) {
-                   while (state == FLDPLUS)
-                       state = m_getfld (state, name, buf, sizeof(buf), in);
+                   while (state == FLDPLUS) {
+                       bufsz = sizeof buf;
+                       state = m_getfld (&gstate, name, buf, &bufsz, in);
+                   }
                    continue;
                }
 
                cp = add (buf, NULL);
                while (state == FLDPLUS) {
-                   state = m_getfld (state, name, buf, sizeof(buf), in);
+                   bufsz = sizeof buf;
+                   state = m_getfld (&gstate, name, buf, &bufsz, in);
                    cp = add (buf, cp);
                }
                key.dptr = trimcpy (cp);
@@ -1477,7 +1481,6 @@ suppress_duplicates (int fd, char *file)
                break;
 
           case BODY:
-          case BODYEOF:
           case FILEEOF:
                break;
 
@@ -1489,6 +1492,7 @@ suppress_duplicates (int fd, char *file)
 
        break;
     }
+    m_getfld_state_destroy (&gstate);
 
     fclose (in);
     return 0;
index 677a86f..6813298 100644 (file)
@@ -357,21 +357,23 @@ get_fields (char *datesw, int msg, struct smsg *smsg)
     register struct tws *tw;
     register char *datecomp = NULL, *subjcomp = NULL;
     register FILE *in;
+    m_getfld_state_t gstate = 0;
 
     if ((in = fopen (msgnam = m_name (msg), "r")) == NULL) {
        admonish (msgnam, "unable to read message");
        return (0);
     }
-    for (compnum = 1, state = FLD;;) {
-       switch (state = m_getfld (state, nam, buf, sizeof(buf), in)) {
+    for (compnum = 1;;) {
+       int bufsz = sizeof buf;
+       switch (state = m_getfld (&gstate, nam, buf, &bufsz, in)) {
        case FLD:
-       case FLDEOF:
        case FLDPLUS:
            compnum++;
            if (!mh_strcasecmp (nam, datesw)) {
                datecomp = add (buf, datecomp);
                while (state == FLDPLUS) {
-                   state = m_getfld (state, nam, buf, sizeof(buf), in);
+                   bufsz = sizeof buf;
+                   state = m_getfld (&gstate, nam, buf, &bufsz, in);
                    datecomp = add (buf, datecomp);
                }
                if (!subjsort || subjcomp)
@@ -379,20 +381,22 @@ get_fields (char *datesw, int msg, struct smsg *smsg)
            } else if (subjsort && !mh_strcasecmp (nam, subjsort)) {
                subjcomp = add (buf, subjcomp);
                while (state == FLDPLUS) {
-                   state = m_getfld (state, nam, buf, sizeof(buf), in);
+                   bufsz = sizeof buf;
+                   state = m_getfld (&gstate, nam, buf, &bufsz, in);
                    subjcomp = add (buf, subjcomp);
                }
                if (datecomp)
                    break;
            } else {
                /* just flush this guy */
-               while (state == FLDPLUS)
-                   state = m_getfld (state, nam, buf, sizeof(buf), in);
+               while (state == FLDPLUS) {
+                   bufsz = sizeof buf;
+                   state = m_getfld (&gstate, nam, buf, &bufsz, in);
+               }
            }
            continue;
 
        case BODY:
-       case BODYEOF:
        case FILEEOF:
            break;
 
@@ -415,6 +419,7 @@ get_fields (char *datesw, int msg, struct smsg *smsg)
        }
        break;
     }
+    m_getfld_state_destroy (&gstate);
 
     /*
      * If no date component, then use the modification
index 9a9fec1..57d7282 100644 (file)
@@ -924,14 +924,15 @@ check_draft (char *msgnam)
     int        state;
     char buf[BUFSIZ], name[NAMESZ];
     FILE *fp;
+    m_getfld_state_t gstate = 0;
 
     if ((fp = fopen (msgnam, "r")) == NULL)
        return 0;
-    for (state = FLD;;)
-       switch (state = m_getfld (state, name, buf, sizeof(buf), fp)) {
+    for (;;) {
+       int bufsz = sizeof buf;
+       switch (state = m_getfld (&gstate, name, buf, &bufsz, fp)) {
            case FLD:
            case FLDPLUS:
-           case FLDEOF:
                /*
                 * If draft already contains any of the
                 * Content-XXX fields, then assume it already
@@ -941,8 +942,10 @@ check_draft (char *msgnam)
                    fclose (fp);
                    return 0;
                }
-               while (state == FLDPLUS)
-                   state = m_getfld (state, name, buf, sizeof(buf), fp);
+               while (state == FLDPLUS) {
+                   bufsz = sizeof buf;
+                   state = m_getfld (&gstate, name, buf, &bufsz, fp);
+               }
                break;
 
            case BODY:
@@ -955,7 +958,8 @@ check_draft (char *msgnam)
                            return 1;
                        }
 
-                   state = m_getfld (state, name, buf, sizeof(buf), fp);
+                   bufsz = sizeof buf;
+                   state = m_getfld (&gstate, name, buf, &bufsz, fp);
                } while (state == BODY);
                /* and fall... */
 
@@ -963,6 +967,8 @@ check_draft (char *msgnam)
                fclose (fp);
                return 0;
        }
+    }
+    m_getfld_state_destroy (&gstate);
 }