summary refs log tree commit diff
diff options
context:
space:
mode:
Diffstat (limited to '')
-rw-r--r--archive.h4
-rw-r--r--html.c19
-rw-r--r--template.c20
3 files changed, 23 insertions, 20 deletions
diff --git a/archive.h b/archive.h
index 6cc7c5f..5a80748 100644
--- a/archive.h
+++ b/archive.h
@@ -59,7 +59,6 @@ static inline void envelopeFree(struct Envelope envelope) {
 }
 
 #define TEMPLATE(...) #__VA_ARGS__
-#define ESCAPE_URL_CAP(len) (3 * (len))
 
 struct Variable {
 	const char *name;
@@ -73,8 +72,9 @@ int escapeXML(FILE *file, const char *str);
 
 int templateRender(
 	FILE *file, const char *template,
-	const struct Variable *vars, EscapeFn *escape
+	const struct Variable vars[], EscapeFn *escape
 );
+char *templateURL(const char *template, const struct Variable vars[]);
 
 #define MBOX_HEADERS \
 	"Date Subject From Sender Reply-To To Cc Bcc " \
diff --git a/html.c b/html.c
index 85b6f6b..e404434 100644
--- a/html.c
+++ b/html.c
@@ -14,7 +14,6 @@
  * along with this program.  If not, see <https://www.gnu.org/licenses/>.
  */
 
-#include <assert.h>
 #include <err.h>
 #include <stdio.h>
 #include <stdlib.h>
@@ -50,21 +49,7 @@ int htmlEnvelope(FILE *file, const struct Envelope *envelope) {
 		{ "messageID", envelope->messageID },
 		{0},
 	};
-
-	size_t cap = sizeof(Mailto);
-	for (struct Variable *var = mailtoVars; var->value; ++var) {
-		cap += ESCAPE_URL_CAP(strlen(var->value));
-	}
-	char *mailto = malloc(cap);
-	if (!mailto) err(EX_OSERR, "malloc");
-
-	FILE *url = fmemopen(mailto, cap, "w");
-	if (!url) err(EX_OSERR, "fmemopen");
-
-	int error = 0
-		|| templateRender(url, Mailto, mailtoVars, escapeURL)
-		|| fclose(url);
-	assert(!error);
+	char *mailto = templateURL(Mailto, mailtoVars);
 
 	const char *from = envelope->from.name;
 	if (!from) from = envelope->from.mailbox;
@@ -84,7 +69,7 @@ int htmlEnvelope(FILE *file, const struct Envelope *envelope) {
 		{ "date", date },
 		{0},
 	};
-	error = templateRender(file, Summary, summaryVars, escapeXML);
+	int error = templateRender(file, Summary, summaryVars, escapeXML);
 	free(mailto);
 	if (error) return error;
 
diff --git a/template.c b/template.c
index 92c6381..426f1d0 100644
--- a/template.c
+++ b/template.c
@@ -68,7 +68,7 @@ int escapeXML(FILE *file, const char *str) {
 
 int templateRender(
 	FILE *file, const char *template,
-	const struct Variable *vars, EscapeFn *escape
+	const struct Variable vars[], EscapeFn *escape
 ) {
 	for (bool subst = false; *template; subst ^= true) {
 		size_t len = strcspn(template, "[]");
@@ -97,3 +97,21 @@ int templateRender(
 	}
 	return 0;
 }
+
+char *templateURL(const char *template, const struct Variable vars[]) {
+	size_t cap = 3 * strlen(template) + 1;
+	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);
+	assert(!error);
+
+	return buf;
+}
ollow=1'>Make sure new cap is actually larger than new lengthJune McEnroe 2022-02-20Remove unused mbs.len field from struct EditJune McEnroe 2022-02-19Remove unneeded includes in ui.cJune McEnroe 2022-02-19Reimplement tab completeJune McEnroe 2022-02-19Handle errors from editFn, etc.June McEnroe 2022-02-19Reimplement text macrosJune McEnroe 2022-02-19Factor out input handling to input.cJune McEnroe 2022-02-19Factor out window management to window.cJune McEnroe 2022-02-19Enable -Wmissing-prototypesJune McEnroe In other words, warn when a function is missing static. I don't see why this isn't in -Wextra. 2022-02-19Fix edit.[ch] license notice additional permissionsJune McEnroe 2022-02-19Run line editing testsJune McEnroe I know, it feels wrong. 2022-02-18Implement new line editing "library"June McEnroe Losing tab complete and text macros, for now. This new implementation works on an instance of a struct and does not interact with the rest of catgirl, making it possible to copy into another project. Unlike existing line editing libraries, this one is entirely abstract and can be rendered externally. My goal with this library is to be able to implement vi mode. Since it operates on struct instances rather than globals, it might also be possible to give catgirl separate line editing buffers for each window, which would be a nice UX improvement. 2022-02-18Simplify cursor positioning in inputJune McEnroe Do some extra work by adding the portion before the cursor to the input window twice, but simplify the interaction with the split point. This fixes the awkward behaviour when moving the cursor across colour codes where the code would be partially interpreted up to the cursor. 2022-02-18Fix M-f orderingJune McEnroe 2022-02-12Move sandman build to scripts/MakefileJune McEnroe 2022-02-12Use compat_readpassphrase.c on LinuxJune McEnroe 2022-02-12Copy RPP defines from oconfigureJune McEnroe