diff scid/theme.c @ 33:1d0ddf9e6efd

misc: general documentation
author David Demelier <markand@malikania.fr>
date Thu, 04 Aug 2022 16:47:10 +0200
parents 081e1c258e64
children 752bb1cd2dd8
line wrap: on
line diff
--- a/scid/theme.c	Thu Aug 04 14:59:33 2022 +0200
+++ b/scid/theme.c	Thu Aug 04 16:47:10 2022 +0200
@@ -26,15 +26,13 @@
 #include <mustache.h>
 
 #include "log.h"
-#include "scid.h"
 #include "theme.h"
 #include "util.h"
 
 #define SIGNATURE DUK_HIDDEN_SYMBOL("File")
 
-static struct {
-	duk_context *ctx;
-} theme;
+static duk_context *context;
+static char base[PATH_MAX];
 
 /* {{{ mustache support */
 
@@ -289,24 +287,24 @@
 	size_t outsz = 0;
 	FILE *fp;
 
-	duk_get_global_string(theme.ctx, function);
+	duk_get_global_string(context, function);
 
-	if (duk_is_callable(theme.ctx, -1)) {
+	if (duk_is_callable(context, -1)) {
 		fp   = util_open_memstream(&out, &outsz);
 		dump = util_json_dump(json);
 
-		duk_push_pointer(theme.ctx, fp);
-		duk_push_string(theme.ctx, dump);
-		duk_json_decode(theme.ctx, -1);
+		duk_push_pointer(context, fp);
+		duk_push_string(context, dump);
+		duk_json_decode(context, -1);
 
-		if (duk_pcall(theme.ctx, 2) != 0)
-			log_warn("theme: %s", duk_safe_to_string(theme.ctx, -1));
+		if (duk_pcall(context, 2) != 0)
+			log_warn("theme: %s", duk_safe_to_string(context, -1));
 
-		duk_pop(theme.ctx);
+		duk_pop(context);
 		fclose(fp);
 		free(dump);
 	} else
-		duk_pop(theme.ctx);
+		duk_pop(context);
 
 	if (!out)
 		out = util_strdup("");
@@ -322,19 +320,20 @@
 	const char *path;
 	char *data;
 
-	theme.ctx = duk_create_heap_default();
+	util_strlcpy(base, directory, sizeof (base));
+	context = duk_create_heap_default();
 	path = theme_path("theme.js");
 
 	if (!(data = util_read(path)))
 		log_warn("theme: %s: %s", path, strerror(errno));
 	else {
-		if (duk_peval_string(theme.ctx, data) != 0)
-			log_warn("theme: %s", duk_safe_to_string(theme.ctx, -1));
+		if (duk_peval_string(context, data) != 0)
+			log_warn("theme: %s", duk_safe_to_string(context, -1));
 
-		duk_pop(theme.ctx);
-		duk_push_object(theme.ctx);
-		duk_put_function_list(theme.ctx, -1, functions);
-		duk_put_global_string(theme.ctx, "Scid");
+		duk_pop(context);
+		duk_push_object(context);
+		duk_put_function_list(context, -1, functions);
+		duk_put_global_string(context, "Scid");
 		free(data);
 	}
 }
@@ -347,7 +346,7 @@
 	/* Build path to the template file. */
 	static _Thread_local char path[PATH_MAX];
 
-	snprintf(path, sizeof (path), "%s/%s", scid.themedir, filename);
+	snprintf(path, sizeof (path), "%s/%s", base, filename);
 
 	return path;
 }
@@ -377,5 +376,5 @@
 void
 theme_free(void)
 {
-	duk_destroy_heap(theme.ctx);
+	duk_destroy_heap(context);
 }