	Reading MIME headers

	The function [[read_headers()]] reads header lines from file
	[[f]] until the [[stopline]] or an empty line is found. The
	whole header is copied to the field [[header]]. The starting
	positions of some header lines are stored in other fields.
	Newlines inside header lines are removed (i.e., continuation
	lines are resolved, but the [[header]] should not be printed
	in this form, because some lines may have become very long.

	The [[header]] string is allocated on the heap. To free it,
	you can use [[free_header()]].

	The routine [[parse_header]] does the work of parsing the
	lines after they have been read. It also tries to find out who
	was the article's author and if the message came from a
	mailing list.

<<*>>=
#include <config.h>
#include <str.h>
#include "ISOcharset.e"
#include "MIMEenc.e"
#include "MIMEhead.e"
#include "parse-head.e"

#define CR '\015'
#define LF '\012'

EXPORT void read_header(FILE *f, MIME_header *header, char *stopline)
{
    char line[MAXLINELEN], *buf = NULL;
    int h, len = 0;
    Bool ok;

    for (;;) {
	ok = fgets(line, sizeof(line), f) != NULL;
	if (!ok && errno == EAGAIN) sleep(1);	/* Wait, then try again */
	else if (!ok) return;			/* An error occurred */
	else if (eq(line, stopline)) break;	/* Last line to read */
	else if (line[0] == CR) break;		/* End of headers */
	else if (line[0] == LF) break;		/* End of headers */
	else {
	    h = len + strlen(line);
	    renewarray(buf, h + 1);
	    strcpy(buf + len, line);
	    len = h;
	}
    }
    parse_header(buf, header);
}
