cgit

commit 5c5d189635287a3bfaa1a4aef133dc0c582a27c2

Author: Lars Hjemli <hjemli@gmail.com>

Merge branch 'lh/macros'

 cgit.c | 10 ++++----
 cgit.h | 2 +
 shared.c | 71 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++


diff --git a/cgit.c b/cgit.c
index ab25b6a00efe4889863b7625c22e9ea180d0fe61..fde0757416798d90808809e5b434384d5261f6ed 100644
--- a/cgit.c
+++ b/cgit.c
@@ -152,7 +152,7 @@ 		ctx.cfg.max_stats = cgit_find_stats_period(value, NULL);
 	else if (!strcmp(name, "cache-size"))
 		ctx.cfg.cache_size = atoi(value);
 	else if (!strcmp(name, "cache-root"))
-		ctx.cfg.cache_root = xstrdup(value);
+		ctx.cfg.cache_root = xstrdup(expand_macros(value));
 	else if (!strcmp(name, "cache-root-ttl"))
 		ctx.cfg.cache_root_ttl = atoi(value);
 	else if (!strcmp(name, "cache-repo-ttl"))
@@ -183,9 +183,9 @@ 	else if (!strcmp(name, "max-commit-count"))
 		ctx.cfg.max_commit_count = atoi(value);
 	else if (!strcmp(name, "scan-path"))
 		if (!ctx.cfg.nocache && ctx.cfg.cache_size)
-			process_cached_repolist(value);
+			process_cached_repolist(expand_macros(value));
 		else
-			scan_tree(value, repo_config);
+			scan_tree(expand_macros(value), repo_config);
 	else if (!strcmp(name, "source-filter"))
 		ctx.cfg.source_filter = new_filter(value, 1);
 	else if (!strcmp(name, "summary-log"))
@@ -209,7 +209,7 @@ 		ctx.cfg.local_time = atoi(value);
 	else if (!prefixcmp(name, "mimetype."))
 		add_mimetype(name + 9, value);
 	else if (!strcmp(name, "include"))
-		parse_configfile(value, config_cb);
+		parse_configfile(expand_macros(value), config_cb);
 }
 
 static void querystring_cb(const char *name, const char *value)
@@ -705,7 +705,7 @@ 	cgit_repolist.count = 0;
 	cgit_repolist.repos = NULL;
 
 	cgit_parse_args(argc, argv);
-	parse_configfile(ctx.env.cgit_config, config_cb);
+	parse_configfile(expand_macros(ctx.env.cgit_config), config_cb);
 	ctx.repo = NULL;
 	http_parse_querystring(ctx.qry.raw, querystring_cb);
 




diff --git a/cgit.h b/cgit.h
index 2b28d636e20ba1f79c398a83f9383d43cba9e2e0..2bf6ab023fb44340b16e564c6cfee7a2b4442fa6 100644
--- a/cgit.h
+++ b/cgit.h
@@ -302,4 +302,6 @@ extern int cgit_close_filter(struct cgit_filter *filter);
 
 extern int readfile(const char *path, char **buf, size_t *size);
 
+extern char *expand_macros(const char *txt);
+
 #endif /* CGIT_H */




diff --git a/shared.c b/shared.c
index 06f70bb141587c25565664ef42cbdd9b1addec09..9f7d6a5cc0a7a83c689a4b345486ecf14ee985c8 100644
--- a/shared.c
+++ b/shared.c
@@ -432,3 +432,74 @@ 	(*buf)[*size] = '\0';
 	close(fd);
 	return (*size == st.st_size ? 0 : e);
 }
+
+int is_token_char(char c)
+{
+	return isalnum(c) || c == '_';
+}
+
+/* Replace name with getenv(name), return pointer to zero-terminating char
+ */
+char *expand_macro(char *name, int maxlength)
+{
+	char *value;
+	int len;
+
+	len = 0;
+	value = getenv(name);
+	if (value) {
+		len = strlen(value);
+		if (len > maxlength)
+			len = maxlength;
+		strncpy(name, value, len);
+	}
+	return name + len;
+}
+
+#define EXPBUFSIZE (1024 * 8)
+
+/* Replace all tokens prefixed by '$' in the specified text with the
+ * value of the named environment variable.
+ * NB: the return value is a static buffer, i.e. it must be strdup'd
+ * by the caller.
+ */
+char *expand_macros(const char *txt)
+{
+	static char result[EXPBUFSIZE];
+	char *p, *start;
+	int len;
+
+	p = result;
+	start = NULL;
+	while (p < result + EXPBUFSIZE - 1 && txt && *txt) {
+		*p = *txt;
+		if (start) {
+			if (!is_token_char(*txt)) {
+				if (p - start > 0) {
+					*p = '\0';
+					len = result + EXPBUFSIZE - start - 1;
+					p = expand_macro(start, len) - 1;
+				}
+				start = NULL;
+				txt--;
+			}
+			p++;
+			txt++;
+			continue;
+		}
+		if (*txt == '$') {
+			start = p;
+			txt++;
+			continue;
+		}
+		p++;
+		txt++;
+	}
+	*p = '\0';
+	if (start && p - start > 0) {
+		len = result + EXPBUFSIZE - start - 1;
+		p = expand_macro(start, len);
+		*p = '\0';
+	}
+	return result;
+}