2 ** m_getfld2 -- replacement for m_getfld()
3 ** read an RFC 822 message
7 #define _POSIX_C_SOURCE 200809L
24 static enum threestate is_falted(FILE *);
25 static size_t copyname(char *, char *);
26 static boolean is_separator(char *);
30 ** FLD2: We read a (complete) header field
31 ** BODY2: We read a body line
32 ** LENERR2: Line is too long (>998, as defined by RFC 822)
33 ** FMTERR2: Header field invalid
34 ** IOERR2: Failure to read
35 ** FILEEOF2: We're at the end of the file
37 ** f->name is only filled in FLD2.
39 ** In FLD2, f->value contains the field's (complete) value only;
40 ** in BODY2, LENERR2 and FMTERR2 f->value contains the whole line;
41 ** in IOERR2 and FILEEOF2 f->value is not set.
44 m_getfld2(enum state s, struct field *f, FILE *msg)
49 enum threestate falted = B_FALSE;
54 nchars = getline(&tmpline, &len, msg);
57 *f = (struct field) { "\0", 0, NULL, 0, 0 };
65 if (nchars >= NAMESZ) {
69 if (*(tmpline + nchars - 1) != '\n') {
73 if (ret == FLD2 && is_separator(tmpline)) {
74 /* header/body separator found */
76 return m_getfld2(BODY2, f, msg);
79 f->namelen = copyname(f->name, tmpline);
86 /* copy the field's value */
87 if (f->alloclen <= nchars - f->namelen) {
88 f->value = mh_xrealloc(f->value, f->alloclen + len);
91 if (f->namelen != 0) {
92 strcpy(f->value, tmpline + f->namelen + 1);
93 f->valuelen = nchars - f->namelen - 1;
95 strcpy(f->value, tmpline);
99 while ((ret == FLD2 || ret == LENERR2) && (falted = is_falted(msg)) == B_TRUE) {
100 nchars = getline(&tmpline, &len, msg);
106 if (nchars >= NAMESZ) {
110 if (*(tmpline + nchars - 1) != '\n') {
114 if (f->alloclen - f->valuelen <= nchars) {
115 f->value = mh_xrealloc(f->value,
119 strcpy(f->value + f->valuelen, tmpline);
120 f->valuelen += nchars;
124 if (falted == FAIL) {
135 nchars = getline(&tmpline, &len, msg);
150 f->valuelen = nchars;
155 /* give error states back as received */
160 static enum threestate
166 if ((c = getc(msg)) < 0) {
179 if (ungetc(c, msg) != c) {
186 copyname(char *dst, char *src)
191 if (!(sep = strchr(src, ':'))) {
194 /* whitespace is forbidden in name */
195 for (cp=src; cp<sep; cp++) {
202 if (len >= NAMESZ - 1) {
213 is_separator(char *line)
216 ** In MH, lines that are consists of dashes only are
217 ** separators as well ... not so in RFC 822.
219 while (*line == '-') {
222 if (strcmp("\n", line) == 0 || strcmp("\r\n", line) == 0 ) {