+#define MS_DEFAULT 0 /* default (one msg per file) */
+#define MS_UNKNOWN 1 /* type not known yet */
+#define MS_MBOX 2 /* Unix-style "from" lines */
+#define MS_MMDF 3 /* string MMDF_DELIM */
+
+/* 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.
+ */
+#define MSG_INPUT_SIZE NMH_BUFSIZ
+#define MAX_DELIMITER_SIZE 5
+
+struct m_getfld_state {
+ /* The file to read from; I/O block. Caller keeps passing it after
+ * initialisation due to historic interface so it keeps getting
+ * updated, presumably to the same value. */
+ FILE *iob;
+
+ /* Holds content of iob. */
+ char msg_buf[2 * MSG_INPUT_SIZE + MAX_DELIMITER_SIZE];
+ /* Points to the next byte to read from msg_buf. */
+ char *readpos;
+ /* Points to just after the last valid byte in msg_buf. If readpos
+ * equals end then msg_buf is empty. */
+ char *end;
+
+ /* Whether the caller intends to ftell(3)/fseek(3) iob's position,
+ * and thus whether m_getfld() needs to detect that and compensate. */
+ int track_filepos;
+ /* Position in iob given what's been consumed ready for returning to
+ * the caller. Further than this may have been read into msg_buf. */
+ off_t total_bytes_read;
+ /* Bytes of iob consumed during this call. */
+ off_t bytes_read;
+ /* What fseeko(3) tells us iob's position is having just explicitly
+ * set it to total_bytes_read. Surely always the same? */
+ off_t last_caller_pos;
+ /* Saved position in iob from filling msg_buf, prior to returning. */
+ off_t last_internal_pos;
+
+ /* One of the MS_* macros tracking the type of iob's content and
+ * thus if it's a single email, or several with delimeters. Default
+ * is MS_DEFAULT. */
+ int msg_style;
+
+ /* The message delimeter if iob has multiple emails, else NULL. For
+ * MS_MBOX it's the string that separates two emails, "\nFrom ",
+ * i.e. the terminating blank line of the previous email, and the
+ * starting From_ line of the next, but for MS_MMDF it's
+ * "\001\001\001\001\n" that may start or terminate an email. */
+ char *msg_delim;
+ /* The last non-NUL char of msg_delim. */
+ char *delimend;
+ /* When searching for msg_delim after an email, it's only of
+ * interest at the start of the line, i.e. when preceded by a
+ * linefeed. fdelim points to msg_delim[-1] that contains '\n' so
+ * it can be used as the needle. */
+ char *fdelim;
+ /* strlen(fdelim). */
+ int fdelimlen;
+ /* The second char of msg_delim. Used when the first char has
+ * already been matched to test the rest. */
+ char *edelim;
+ /* strlen(edelim). */
+ int edelimlen;
+ /* The relationship between all of these pointers and lengths for
+ * the two possible msg_delim values.
+ *
+ * "\0\n\nFrom \0" 9 "\0\n\001\001\001\001\n\0" 8
+ * | || | | | | |
+ * | || s->delimend | | | s->delimend
+ * | || | | |
+ * | |s->edelim s->edelimlen=5 | | s->edelim s->edelimlen=4
+ * | | | |
+ * | s->msg_delim | s->msg_delim
+ * | |
+ * s->fdelim s->fdelimlen=7 s->fdelim s->fdelimlen=6
+ */
+
+ /* Maps all the bytes of msg_delim, apart from the last two,
+ * including the NUL, onto the last position in msg_delim where they
+ * occur. Bytes not present are NULL. */
+ char **pat_map;