From 9a3e3e396442918099d44f0bcc9184007ecbef35 Mon Sep 17 00:00:00 2001 From: "C. McEnroe" Date: Mon, 13 Apr 2020 13:56:59 -0400 Subject: Allow NULL vars and escape to templateRender --- template.c | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) diff --git a/template.c b/template.c index 750f339..fed3202 100644 --- a/template.c +++ b/template.c @@ -24,6 +24,11 @@ #include "archive.h" +static int escapeNull(FILE *file, const char *str) { + size_t n = fwrite(str, strlen(str), 1, file); + return (n ? 0 : -1); +} + int escapeURL(FILE *file, const char *str) { static const char *Safe = { "$-_.+!*'()," @@ -70,11 +75,12 @@ int templateRender( FILE *file, const char *template, const struct Variable vars[], EscapeFn *escape ) { + if (!escape) escape = escapeNull; for (bool subst = false; *template; subst ^= true) { size_t len = strcspn(template, "[]"); if (subst) { const char *value = NULL; - for (const struct Variable *var = vars; var->name; ++var) { + for (const struct Variable *var = vars; var && var->name; ++var) { if (strlen(var->name) != len) continue; if (strncmp(var->name, template, len)) continue; value = var->value; @@ -103,15 +109,14 @@ char *templateURL(const char *template, const struct Variable vars[]) { for (const struct Variable *var = vars; var->name; ++var) { cap += 3 * strlen(var->value); } - char *buf = malloc(cap); if (!buf) err(EX_OSERR, "malloc"); FILE *file = fmemopen(buf, cap, "w"); if (!file) err(EX_OSERR, "fmemopen"); - int error = templateRender(file, template, vars, escapeURL) || fclose(file); + int error = templateRender(file, template, vars, escapeURL) + || fclose(file); assert(!error); - return buf; } -- cgit 1.4.1