2 ** m_getfld2 -- replacement for m_getfld()
3 ** read an RFC 822 message
7 #define _POSIX_C_SOURCE 200809L
25 static enum threestate is_falted(FILE *);
26 static size_t copyname(char *, char *);
27 static bool is_separator(char *);
31 ** FLD2: We read a (complete) header field
32 ** BODY2: We read a body line
33 ** LENERR2: Line is too long (>998, as defined by RFC 822)
34 ** FMTERR2: Header field invalid
35 ** IOERR2: Failure to read
36 ** FILEEOF2: We're at the end of the file
38 ** f->name is only filled in FLD2.
40 ** In FLD2, f->value contains the field's (complete) value only;
41 ** in BODY2, LENERR2 and FMTERR2 f->value contains the whole line;
42 ** in IOERR2 and FILEEOF2 f->value is not set.
45 m_getfld2(enum state s, struct field *f, FILE *msg)
50 enum threestate falted = B_FALSE;
55 nchars = getline(&tmpline, &len, msg);
58 *f = (struct field) { "\0", 0, NULL, 0, 0 };
66 if (nchars >= NAMESZ) {
70 if (*(tmpline + nchars - 1) != '\n') {
74 if (ret == FLD2 && is_separator(tmpline)) {
75 /* header/body separator found */
77 return m_getfld2(BODY2, f, msg);
80 f->namelen = copyname(f->name, tmpline);
87 /* copy the field's value */
88 if (f->alloclen <= nchars - f->namelen) {
89 f->value = mh_xrealloc(f->value, f->alloclen + len);
92 if (f->namelen != 0) {
93 strcpy(f->value, tmpline + f->namelen + 1);
94 f->valuelen = nchars - f->namelen - 1;
96 strcpy(f->value, tmpline);
100 while (ret == FLD2 && (falted = is_falted(msg)) == B_TRUE) {
101 nchars = getline(&tmpline, &len, msg);
107 if (nchars >= NAMESZ) {
111 if (*(tmpline + nchars - 1) != '\n') {
115 if (f->alloclen - f->valuelen <= nchars) {
116 f->value = mh_xrealloc(f->value,
120 strcpy(f->value + f->valuelen, tmpline);
121 f->valuelen += nchars;
125 if (falted == FAIL) {
136 nchars = getline(&tmpline, &len, msg);
149 if (nchars >= NAMESZ) {
155 f->valuelen = nchars;
160 /* give error states back as received */
165 static enum threestate
171 if ((c = getc(msg)) < 0) {
184 if (ungetc(c, msg) != c) {
191 copyname(char *dst, char *src)
196 if (!(sep = strchr(src, ':'))) {
199 /* whitespace is forbidden in name */
200 for (cp=src; cp<sep; cp++) {
207 if (len >= NAMESZ - 1) {
218 is_separator(char *line)
221 ** In MH, lines that are consists of dashes only are
222 ** separators as well ... not so in RFC 822.
224 while (*line == '-') {
227 if (strcmp("\n", line) == 0 || strcmp("\r\n", line) == 0 ) {