URI:
       tSet outfile based on subject line - scribo - Email-based phlog generator
  HTML git clone git://git.z3bra.org/scribo.git
   DIR Log
   DIR Files
   DIR Refs
   DIR README
   DIR LICENSE
       ---
   DIR commit c7fa199582f31a4dba0b45aea601a88349eee95e
   DIR parent c150b139555f493a41dd8cf9c102fa8fa7cef4e8
  HTML Author: Willy Goiffon <dev@z3bra.org>
       Date:   Mon,  7 Sep 2020 23:07:55 +0200
       
       Set outfile based on subject line
       
       Diffstat:
         M scribo.c                            |      59 ++++++++++++++++++++++++-------
       
       1 file changed, 47 insertions(+), 12 deletions(-)
       ---
   DIR diff --git a/scribo.c b/scribo.c
       t@@ -1,4 +1,5 @@
        #include <ctype.h>
       +#include <limits.h>
        #include <stdio.h>
        #include <stdlib.h>
        #include <string.h>
       t@@ -24,6 +25,9 @@ SLIST_HEAD(headers, hdr);
        #ifndef strlcpy
        size_t strlcpy(char *dst, const char *src, size_t siz);
        #endif
       +#ifndef strlcat
       +size_t strlcat(char *dst, const char *src, size_t siz);
       +#endif
        
        void
        usage(char *pgm)
       t@@ -32,6 +36,30 @@ usage(char *pgm)
        }
        
        char *
       +sanitize(const char *s)
       +{
       +        static char tmp[PATH_MAX];
       +        const char *p;
       +        char *w;
       +
       +        for (p = s, w = tmp; *p; p++) {
       +                switch (*p) {
       +                case '.':
       +                        *(w++) = *p;
       +                        break;
       +                case ' ':
       +                        *(w++) = '_';
       +                        break;
       +                default:
       +                        if (isalnum(*p))
       +                                *(w++) = tolower(*p);
       +                }
       +        }
       +
       +        return tmp;
       +}
       +
       +char *
        header(struct headers *head, char *key)
        {
                struct hdr *h;
       t@@ -89,19 +117,15 @@ parseheaders(FILE *fp, struct headers *head)
        }
        
        int
       -writelog(FILE *in, FILE *out)
       +writelog(FILE *in, FILE *out, struct headers *head)
        {
                struct tm tm = {.tm_isdst = -1};
                char stamp[BUFSIZ];
                char *subject, *date, *from;
       -        struct headers headers;
        
       -        if (parseheaders(in, &headers) < 0)
       -                return -1;
       -
       -        subject = header(&headers, "Subject");
       -        date = header(&headers, "Date");
       -        from = header(&headers, "From");
       +        subject = header(head, "Subject");
       +        date = header(head, "Date");
       +        from = header(head, "From");
        
                /* convert date to an appropriate format */
                strptime(date, "%a, %d %b %Y %T %z", &tm);
       t@@ -120,6 +144,8 @@ main(int argc, char *argv[])
        {
                FILE *in = stdin, *out = stdout;
                char *argv0, *infile, *outfile;
       +        char fn[PATH_MAX];
       +        struct headers headers;
        
                infile = NULL;
                outfile = NULL;
       t@@ -142,16 +168,25 @@ main(int argc, char *argv[])
                if (infile)
                        in = fopen(infile, "r");
        
       +        if (!in)
       +                return -1;
       +
                if (chdir(basedir) < 0)
                        return -1;
        
       +        if (parseheaders(in, &headers) < 0)
       +                return -1;
       +
       +        if (!outfile) {
       +                strlcpy(fn, sanitize(header(&headers, "Subject")), sizeof(fn));
       +                strlcat(fn, ".txt", sizeof(fn));
       +                outfile = fn;
       +        }
       +
                if (outfile)
                        out = fopen(outfile, "w");
        
       -        if (!in || !out)
       -                return -1;
       -
       -        if (writelog(in, out) < 0)
       +        if (writelog(in, out, &headers) < 0)
                        return -1;
        
                fclose(in);