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 *);
29 ** FLD2: We read a (complete) header field
30 ** BODY2: We read a body line
31 ** LENERR2: Line is too long (>998, as defined by RFC 822)
32 ** FMTERR2: Header field invalid
33 ** IOERR2: Failure to read
34 ** FILEEOF2: We're at the end of the file
36 ** f->name is only filled in FLD2.
38 ** In FLD2, f->value contains the field's (complete) value only;
39 ** in BODY2, LENERR2 and FMTERR2 f->value contains the whole line;
40 ** in IOERR2 and FILEEOF2 f->value is not set.
43 m_getfld2(enum state s, struct field *f, FILE *msg)
48 enum threestate falted;
50 nchars = getline(&tmpline, &len, msg);
62 if (nchars >= NAMESZ) {
73 if (*(tmpline + nchars - 1) != '\n') {
83 if (nchars > 0 && (*tmpline == '\n' || *tmpline == '-')) {
84 /* header/body separator found */
86 return m_getfld2(BODY2, f, msg);
89 f->namelen = copyname(f->name, tmpline);
100 /* copy the field's value */
101 if (f->alloclen <= nchars - f->namelen) {
102 f->value = mh_xrealloc(f->value, f->alloclen + len);
105 strcpy(f->value, tmpline + f->namelen + 1);
106 f->valuelen = nchars - f->namelen - 1;
108 while ((falted = is_falted(msg)) == B_TRUE) {
109 nchars = getline(&tmpline, &len, msg);
115 if (nchars >= NAMESZ) {
120 f->valuelen = nchars;
124 if (*(tmpline + nchars - 1) != '\n') {
129 f->valuelen = nchars;
134 if (f->alloclen - f->valuelen <= nchars) {
135 f->value = mh_xrealloc(f->value,
139 strcpy(f->value + f->valuelen, tmpline);
140 f->valuelen += nchars;
144 if (falted == FAIL) {
157 f->valuelen = nchars;
162 /* give error states back as received */
167 static enum threestate
173 if ((c = getc(msg)) < 0) {
186 if (ungetc(c, msg) != c) {
193 copyname(char *dst, char *src)
198 if (!(sep = strchr(src, ':'))) {
201 /* whitespace is forbidden in name */
202 for (cp=src; cp<sep; cp++) {