scribo

Email-based phlog generator
git clone git://git.z3bra.org/scribo.git
Log | Files | Refs

commit 384fc09310f57cddf07cdeaa7759963847d0c9bb
parent 12050fd0fc8de72fb50659a4efaa8e3e7a48bf3b
Author: Willy Goiffon <dev@z3bra.org>
Date:   Mon,  7 Sep 2020 18:07:53 +0200

Explode parseheaders() into multiple functions

Diffstat:
Mscribo.c | 41++++++++++++++++++++++++++++++-----------
1 file changed, 30 insertions(+), 11 deletions(-)

diff --git a/scribo.c b/scribo.c @@ -31,6 +31,21 @@ usage(char *pgm) fprintf(stderr, "usage: %s [-b basedir] [-io file]", pgm); } +struct hdr * +saveheader(struct headers *head, char *line) +{ + struct hdr *h; + + if (!(h = malloc(sizeof(*h)))) + return NULL; + + strlcpy(h->name, rfc5322_headername(line), sizeof(h->name)); + strlcpy(h->body, rfc5322_headerbody(line), sizeof(h->body)); + SLIST_INSERT_HEAD(head, h, entries); + + return h; +} + int parseheaders(FILE *fp, struct headers *head) { @@ -46,17 +61,11 @@ parseheaders(FILE *fp, struct headers *head) if (!strncmp(buf, "\r\n", 2)) break; - if (isblank(*buf)) { + if (isblank(*buf) && h) rfc5322_unfold(h->body, buf, sizeof(h->body)); - continue; - } - - if (!(h = malloc(sizeof(*h)))) - return -1; - strlcpy(h->name, rfc5322_headername(buf), sizeof(h->name)); - strlcpy(h->body, rfc5322_headerbody(buf), sizeof(h->body)); - SLIST_INSERT_HEAD(head, h, entries); + if (!isblank(*buf)) + h = saveheader(head, buf); } free(buf); @@ -68,11 +77,21 @@ parseheaders(FILE *fp, struct headers *head) } int +writelog(FILE *in, FILE *out) +{ + struct headers headers; + + if (parseheaders(in, &headers) < 0) + return -1; + + return 0; +} + +int main(int argc, char *argv[]) { FILE *in = stdin, *out = stdout; char *argv0, *infile, *outfile; - struct headers headers; infile = NULL; outfile = NULL; @@ -104,7 +123,7 @@ main(int argc, char *argv[]) if (!in || !out) return -1; - if (parseheaders(in, &headers) < 0) + if (writelog(in, out) < 0) return -1; fclose(in);