about summary refs log tree commit diff
path: root/template.c
diff options
context:
space:
mode:
authorC. McEnroe <june@causal.agency>2020-04-19 11:27:26 -0400
committerC. McEnroe <june@causal.agency>2020-04-20 11:17:56 -0400
commit4d35c86401b4a947689eac375ec4ba29b3c99d6d (patch)
tree70b71841a1114c88f4d4c0bea0e645ff3b301ca7 /template.c
parentSkip leading whitespace in message IDs (diff)
downloadbubger-4d35c86401b4a947689eac375ec4ba29b3c99d6d.tar.gz
bubger-4d35c86401b4a947689eac375ec4ba29b3c99d6d.zip
Use template system for paths and URLs
This probably still needs a lot of cleaning up.
Diffstat (limited to 'template.c')
-rw-r--r--template.c35
1 files changed, 28 insertions, 7 deletions
diff --git a/template.c b/template.c
index afb9173..4f2d9dd 100644
--- a/template.c
+++ b/template.c
@@ -29,6 +29,25 @@ static int escapeNull(FILE *file, const char *str) {
 	return (n ? 0 : -1);
 }
 
+static const char SlashReplacement = ';';
+
+int escapePath(FILE *file, const char *str) {
+	while (*str) {
+		if (*str == '/') {
+			str++;
+			int n = fprintf(file, "%c", SlashReplacement);
+			if (n < 0) return n;
+		}
+		size_t len = strcspn(str, "/");
+		if (len) {
+			size_t n = fwrite(str, len, 1, file);
+			if (!n) return -1;
+		}
+		str += len;
+	}
+	return 0;
+}
+
 int escapeURL(FILE *file, const char *str) {
 	static const char *Safe = {
 		"$-_.+!*'(),"
@@ -44,7 +63,9 @@ int escapeURL(FILE *file, const char *str) {
 		}
 		str += len;
 		if (*str) {
-			int n = fprintf(file, "%%%02X", *str++);
+			char ch = *str++;
+			if (ch == '/') ch = SlashReplacement;
+			int n = fprintf(file, "%%%02X", ch);
 			if (n < 0) return n;
 		}
 	}
@@ -53,12 +74,6 @@ int escapeURL(FILE *file, const char *str) {
 
 int escapeXML(FILE *file, const char *str) {
 	while (*str) {
-		size_t len = strcspn(str, "\"&<");
-		if (len) {
-			size_t n = fwrite(str, len, 1, file);
-			if (!n) return -1;
-		}
-		str += len;
 		int n = 0;
 		switch (*str) {
 			break; case '"': str++; n = fprintf(file, "&quot;");
@@ -66,6 +81,12 @@ int escapeXML(FILE *file, const char *str) {
 			break; case '<': str++; n = fprintf(file, "&lt;");
 		}
 		if (n < 0) return n;
+		size_t len = strcspn(str, "\"&<");
+		if (len) {
+			size_t n = fwrite(str, len, 1, file);
+			if (!n) return -1;
+		}
+		str += len;
 	}
 	return 0;
 }