char *parse_line(char *str, char *ltoken, int lsz) { char *sp = str; char *ep, *p; int len; unescapestr(str); uncommentstr(str); while (*sp && isspace(*sp)) sp++; if (!*sp || *sp == '#') return NULL; ep = sp + strlen(sp) - 1; while (isspace(*ep) && ep >= sp) *ep-- = '\0'; if (*ep == '"' || *ep == '\'') *ep = 0; if (!(p = strchr(sp, '='))) return NULL; len = p - sp; if (len >= lsz) return NULL; strncpy(ltoken, sp, len); ltoken[len] = 0; p++; if (*p == '"' || *p == '\'' ) p++; return p; }
struct rfc822_header* rfc822_parse_stanza(FILE *file) { struct rfc822_header *head, **tail, *cur; static size_t buflen = 8192; static char *buf = NULL; if (!buf) { buf = malloc(buflen * sizeof *buf); if (!buf) DIE("Out of memory"); } head = NULL; tail = &head; cur = NULL; /* fprintf(stderr,"rfc822_parse_stanza(file)\n");*/ while (fgets(buf, buflen, file)) { char *tmp; size_t tmplen = strlen(buf); if (*buf == '\n') break; while (buf[tmplen - 1] != '\n') { buflen += 8192; buf = realloc(buf, buflen * sizeof *buf); if (!buf) DIE("Out of memory"); if (!fgets(buf + tmplen, buflen - tmplen, file)) break; tmplen += strlen(buf + tmplen); } CHOMP(buf); tmp = buf; if (isspace(*tmp)) { /* continuation line, just append it */ int len; if (cur == NULL) break; /* should report an error here */ len = strlen(cur->value) + strlen(tmp) + 2; cur->value = realloc(cur->value, len); strvacat(cur->value, len, "\n", tmp, NULL); } else { while (*tmp != 0 && *tmp != ':') tmp++; *tmp++ = '\0'; cur = NEW(struct rfc822_header); if (cur == NULL) return NULL; memset(cur, '\0',sizeof(struct rfc822_header)); cur->header = strdup(buf); while (isspace(*tmp)) tmp++; cur->value = strdup(unescapestr(tmp)); *tail = cur; tail = &cur->next; } } return head; }