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 int 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;
53 nchars = getline(&tmpline, &len, msg);
65 if (nchars >= NAMESZ) {
74 if (*(tmpline + nchars - 1) != '\n') {
84 if (is_separator(tmpline)) {
85 /* header/body separator found */
87 return m_getfld2(BODY2, f, msg);
90 f->namelen = copyname(f->name, tmpline);
101 /* copy the field's value */
102 if (f->alloclen <= nchars - f->namelen) {
103 f->value = mh_xrealloc(f->value, f->alloclen + len);
106 strcpy(f->value, tmpline + f->namelen + 1);
107 f->valuelen = nchars - f->namelen - 1;
109 while ((falted = is_falted(msg)) == B_TRUE) {
110 nchars = getline(&tmpline, &len, msg);
116 if (nchars >= NAMESZ) {
121 f->valuelen = nchars;
125 if (*(tmpline + nchars - 1) != '\n') {
130 f->valuelen = nchars;
135 if (f->alloclen - f->valuelen <= nchars) {
136 f->value = mh_xrealloc(f->value,
140 strcpy(f->value + f->valuelen, tmpline);
141 f->valuelen += nchars;
145 if (falted == FAIL) {
154 nchars = getline(&tmpline, &len, msg);
166 if (nchars >= NAMESZ) {
171 f->valuelen = nchars;
178 f->valuelen = nchars;
183 /* give error states back as received */
188 static enum threestate
194 if ((c = getc(msg)) < 0) {
207 if (ungetc(c, msg) != c) {
214 copyname(char *dst, char *src)
219 if (!(sep = strchr(src, ':'))) {
222 /* whitespace is forbidden in name */
223 for (cp=src; cp<sep; cp++) {
241 is_separator(char *line)
244 ** In MH, lines that are consists of dashes only are
245 ** separators as well ... not so in RFC 822.
247 while (*line == '-') {
250 if (strcmp("\n", line)==0) {