tFormat gopher menus as HTML - phroxy - Gopher to HTTP proxy
 (HTM) git clone git://git.z3bra.org/phroxy.git
 (DIR) Log
 (DIR) Files
 (DIR) Refs
 (DIR) LICENSE
       ---
 (DIR) commit d521a7e51213a29de0e339b7bd9fb1bb04f227df
 (DIR) parent 403dee6fb7818dbf5dc8df145436b03eb2841c9d
 (HTM) Author: Willy Goiffon <dev@z3bra.org>
       Date:   Mon, 14 Sep 2020 11:05:19 +0200
       
       Format gopher menus as HTML
       
       Diffstat:
         M phroxy.c                            |      34 ++++++++++++++++++++++++++++---
       
       1 file changed, 31 insertions(+), 3 deletions(-)
       ---
 (DIR) diff --git a/phroxy.c b/phroxy.c
       t@@ -1,5 +1,6 @@
        #include <err.h>
        #include <errno.h>
       +#include <limits.h>
        #include <netdb.h>
        #include <signal.h>
        #include <stdio.h>
       t@@ -208,15 +209,42 @@ printheaders(char *ctype)
        char *
        htmlize(char *data, size_t len, size_t *newsize)
        {
       -        size_t hlen;
       +        size_t alen, blen, hlen;
                char *html = NULL;
       +        char i, *p, *body, a[LINE_MAX], *f[4];
       +        char *fmt = "<a href='gopher://%s:%s/%c/%s'>%s</a><br/>\n";
       +
       +        blen = 0;
       +        body = NULL;
       +        p = data;
       +
       +        while((p = strsep(&data, "\n"))) {
       +                i = *p++;
       +                f[0] = strsep(&p, "\t");
       +                f[1] = strsep(&p, "\t");
       +                f[2] = strsep(&p, "\t");
       +                f[3] = strsep(&p, "\r");
       +                if (!f[1])
       +                        continue;
       +                switch(i) {
       +                case 'i':
       +                        snprintf(a, sizeof(a), "%s<br/>\n", f[0]);
       +                        break;
       +                default:
       +                        snprintf(a, sizeof(a), fmt, f[2], f[3], i, f[1], f[0]);
       +                }
       +                alen = strnlen(a, sizeof(a));
       +                body = realloc(body, blen + alen);
       +                memcpy(body + blen, a, alen);
       +                blen += alen;
       +        }
        
       -        hlen = len + strlen(htmlfmt);
       +        hlen = strlen(htmlfmt) + blen;
                html = malloc(hlen);
                if (!html)
                        return NULL;
        
       -        snprintf(html, hlen, htmlfmt, data);
       +        snprintf(html, hlen, htmlfmt, body);
        
                if (newsize)
                        *newsize = hlen;