diff options
author | June McEnroe <june@causal.agency> | 2020-04-13 13:56:59 -0400 |
---|---|---|
committer | June McEnroe <june@causal.agency> | 2020-04-13 13:58:54 -0400 |
commit | 9a3e3e396442918099d44f0bcc9184007ecbef35 (patch) | |
tree | f892f153c3c6b69aaa97fe0d74a9e013bc77bef6 | |
parent | Preserve original Date header in envelope (diff) | |
download | bubger-9a3e3e396442918099d44f0bcc9184007ecbef35.tar.gz bubger-9a3e3e396442918099d44f0bcc9184007ecbef35.zip |
Allow NULL vars and escape to templateRender
-rw-r--r-- | template.c | 13 |
1 files 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; } |