tExplode parseheaders() into multiple functions - scribo - Email-based phlog generator
 (HTM) git clone git://git.z3bra.org/scribo.git
 (DIR) Log
 (DIR) Files
 (DIR) Refs
 (DIR) README
 (DIR) LICENSE
       ---
 (DIR) commit 384fc09310f57cddf07cdeaa7759963847d0c9bb
 (DIR) parent 12050fd0fc8de72fb50659a4efaa8e3e7a48bf3b
 (HTM) Author: Willy Goiffon <dev@z3bra.org>
       Date:   Mon,  7 Sep 2020 18:07:53 +0200
       
       Explode parseheaders() into multiple functions
       
       Diffstat:
         M scribo.c                            |      41 ++++++++++++++++++++++---------
       
       1 file changed, 30 insertions(+), 11 deletions(-)
       ---
 (DIR) diff --git a/scribo.c b/scribo.c
       t@@ -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)
        {
       t@@ -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);
       t@@ -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;
       t@@ -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);