about summary refs log tree commit diff
diff options
context:
space:
mode:
Diffstat (limited to '')
-rw-r--r--template.c13
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;
 }