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 *);
29 struct field free_field = { "\0", 0, NULL, 0, 0 };
33 ** FLD2: We read a (complete) header field
34 ** BODY2: We read a body line
35 ** LENERR2: Line is too long (>998, as defined by RFC 822)
36 ** FMTERR2: Header field invalid
37 ** IOERR2: Failure to read
38 ** FILEEOF2: We're at the end of the file
40 ** f->name is only filled in FLD2.
42 ** In FLD2, f->value contains the field's (complete) value only;
43 ** in BODY2, LENERR2 and FMTERR2 f->value contains the whole line;
44 ** in IOERR2 and FILEEOF2 f->value is not set.
47 m_getfld2(enum state s, struct field *f, FILE *msg)
52 enum threestate falted = B_FALSE;
57 nchars = getline(&tmpline, &len, msg);
68 if (nchars >= NAMESZ) {
72 if (*(tmpline + nchars - 1) != '\n') {
76 if (ret == FLD2 && is_separator(tmpline)) {
77 /* header/body separator found */
79 return m_getfld2(BODY2, f, msg);
82 f->namelen = copyname(f->name, tmpline);
89 /* copy the field's value */
90 if (f->alloclen <= nchars - f->namelen) {
91 f->value = mh_xrealloc(f->value, f->alloclen + len);
94 if (f->namelen != 0) {
95 strcpy(f->value, tmpline + f->namelen + 1);
96 f->valuelen = nchars - f->namelen - 1;
98 strcpy(f->value, tmpline);
102 while (ret == FLD2 && (falted = is_falted(msg)) == B_TRUE) {
103 nchars = getline(&tmpline, &len, msg);
109 if (nchars >= NAMESZ) {
113 if (*(tmpline + nchars - 1) != '\n') {
117 if (f->alloclen - f->valuelen <= nchars) {
118 f->value = mh_xrealloc(f->value,
122 strcpy(f->value + f->valuelen, tmpline);
123 f->valuelen += nchars;
127 if (falted == FAIL) {
138 nchars = getline(&tmpline, &len, msg);
148 if (nchars >= NAMESZ) {
153 f->valuelen = nchars;
158 /* give error states back as received */
163 static enum threestate
169 if ((c = getc(msg)) < 0) {
182 if (ungetc(c, msg) != c) {
189 copyname(char *dst, char *src)
194 if (!(sep = strchr(src, ':'))) {
197 /* whitespace is forbidden in name */
198 for (cp=src; cp<sep; cp++) {
205 if (len >= NAMESZ - 1) {
216 is_separator(char *line)
219 ** In MH, lines that are consists of dashes only are
220 ** separators as well ... not so in RFC 822.
222 while (*line == '-') {
225 if (strcmp("\n", line) == 0 || strcmp("\r\n", line) == 0 ) {