+#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;
+
+ /* The parser's current state. Also returned to the caller, amongst
+ * other possible values, to indicate the token consumed. One of
+ * FLD, FLDPLUS, BODY, or FILEEOF. */
+ int state;
+};
+
+m_getfld_state_t m_getfld_state_init(FILE *iob)
+{
+ m_getfld_state_t s;
+
+ NEW(s);
+ 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->state = FLD;
+ s->track_filepos = 0;
+
+ return s;
+}
+
+/* scan() needs to force an initial state of FLD for each message. */
+void
+m_getfld_state_reset (m_getfld_state_t *gstate) {
+ if (*gstate) {
+ (*gstate)->state = FLD;
+ }
+}
+
+/* If the caller interleaves ftell*()/fseek*() calls with m_getfld()
+ calls, m_getfld() must keep track of the file position. The caller
+ must use this function to inform m_getfld(). */
+void
+m_getfld_track_filepos (m_getfld_state_t *gstate, FILE *iob) {
+ if (! *gstate) {
+ *gstate = m_getfld_state_init(iob);
+ }
+
+ (*gstate)->track_filepos = 1;
+}
+
+/* m_getfld_track_filepos() with the existing iob. */
+void m_getfld_track_filepos2(m_getfld_state_t *gstate)
+{
+ if (!*gstate)
+ adios(NULL, "m_getfld_track_filepos2 without gstate");