working template and variables - libgcgi - REST library for Gopher
 (HTM) git clone git://bitreich.org/libgcgi git://hg6vgqziawt5s4dj.onion/libgcgi
 (DIR) Log
 (DIR) Files
 (DIR) Refs
 (DIR) Tags
 (DIR) README
 (DIR) LICENSE
       ---
 (DIR) commit e52fd13abe19a46021a9661b499320da75fed2de
 (DIR) parent 092dce7972a8883e7532c848192785ed60ac9b67
 (HTM) Author: Josuah Demangeon <me@josuah.net>
       Date:   Sat, 30 Jul 2022 13:04:32 +0200
       
       working template and variables
       
       Diffstat:
         M Makefile                            |       8 +-------
         A db/vars                             |       1 +
         A gph/404.gph                         |       1 +
         M index.c                             |       6 +++++-
         M libgcgi.h                           |      16 ++++++----------
       
       5 files changed, 14 insertions(+), 18 deletions(-)
       ---
 (DIR) diff --git a/Makefile b/Makefile
       @@ -1,13 +1,7 @@
        LDFLAGS = -static
        CFLAGS = -g -pedantic -std=c99 -Wall -Wextra -Wno-unused-function
        
       -V = v0.0
       -
       -all: index.cgi tmp db/category db/item db/image
       -
       -tmp db/category db/item db/image:
       -        mkdir -p -m 700 $@
       -        chown www:www $@
       +all: index.cgi
        
        index.cgi: index.c libgcgi.h
                ${CC} ${LDFLAGS} ${CFLAGS} -o $@ index.c
 (DIR) diff --git a/db/vars b/db/vars
       @@ -0,0 +1 @@
       +Variable-From-Db: Lucky 777 Hat
 (DIR) diff --git a/gph/404.gph b/gph/404.gph
       @@ -0,0 +1 @@
       +Hello world!
 (DIR) diff --git a/index.c b/index.c
       @@ -13,11 +13,16 @@
        static void
        error_404(char **matches)
        {
       +        struct gcgi_var_list vars = {0};
                char *var;
        
       +        gcgi_read_var_list(&vars, "db/vars");
       +
                printf("sorry, I could not find %s\n", matches[0]);
                if ((var = gcgi_get_var(&gcgi_gopher_query, "var")) != NULL)
                        printf("I got the $var though! -> '%s'\n", var);
       +
       +        gcgi_template("gph/404.gph", &vars);
        }
        
        static struct gcgi_handler handlers[] = {
       @@ -30,7 +35,6 @@ main(int argc, char **argv)
        {
                /* restrict allowed paths */
                unveil("gph", "r");
       -        unveil("tmp", "rwc");
                unveil("db", "rwc");
        
                /* restrict allowed system calls */
 (DIR) diff --git a/libgcgi.h b/libgcgi.h
       @@ -24,9 +24,6 @@ static void gcgi_handle_request(struct gcgi_handler h[], char **argv, int argc);
        /* abort the program with an error message sent to the client */
        static void gcgi_fatal(char *fmt, ...);
        
       -/* receive a file payload from the client onto the disk at `path` */
       -static void gcgi_receive_file(char const *path);
       -
        /* print a template with every "{{name}}" looked up in `vars` */
        static void gcgi_template(char const *path, struct gcgi_var_list *vars);
        
       @@ -88,8 +85,10 @@ gcgi_fopenread(char *path)
                        return NULL;
                if ((buf = malloc(sz + 1)) == NULL)
                        return NULL;
       -        if (fread(buf, sz, 1, fp) != sz)
       +        if (fread(buf, sz, 1, fp) == sz) {
       +                errno = EFBIG;
                        goto error_free;
       +        }
                if (ferror(fp))
                        goto error_free;
                fclose(fp);
       @@ -321,17 +320,14 @@ static void
        gcgi_template(char const *path, struct gcgi_var_list *vars)
        {
                FILE *fp;
       -        ssize_t ssz;
                size_t sz;
       -        char *line, *head, *tail, *key;
       -        char *val;
       +        char *line, *head, *tail, *key, *val;
        
                if ((fp = fopen(path, "r")) == NULL)
                        gcgi_fatal("opening template %s", path);
       -
                sz = 0;
                line = NULL;
       -        while ((ssz = getline(&line, &sz, fp)) > 0) {
       +        while (getline(&line, &sz, fp) > 0) {
                        head = tail = line;
                        for (; (key = gcgi_next_var(head, &tail)); head = tail) {
                                fputs(head, stdout);
       @@ -342,7 +338,7 @@ gcgi_template(char const *path, struct gcgi_var_list *vars)
                        }
                        fputs(tail, stdout);
                }
       -        if (ssz == -1)
       +        if (ferror(fp))
                        gcgi_fatal("reading from template: %s", strerror(errno));
                fclose(fp);
        }