dovecot-2.0-sslstream: var_expand(): Added support for built-in ...

dovecot at dovecot.org dovecot at dovecot.org
Sat Feb 13 02:57:14 EET 2010


details:   http://hg.dovecot.org/dovecot-2.0-sslstream/rev/e1b725d02c30
changeset: 10527:e1b725d02c30
user:      Timo Sirainen <tss at iki.fi>
date:      Tue Dec 22 18:18:55 2009 -0500
description:
var_expand(): Added support for built-in host, pid and env:* variables.

diffstat:

5 files changed, 86 insertions(+), 12 deletions(-)
src/lib/Makefile.am       |    3 +-
src/lib/test-lib.c        |    1 
src/lib/test-lib.h        |    1 
src/lib/test-var-expand.c |   43 ++++++++++++++++++++++++++++++++++++++
src/lib/var-expand.c      |   50 +++++++++++++++++++++++++++++++++++----------

diffs (155 lines):

diff -r c3d85a07888c -r e1b725d02c30 src/lib/Makefile.am
--- a/src/lib/Makefile.am	Tue Dec 22 18:12:33 2009 -0500
+++ b/src/lib/Makefile.am	Tue Dec 22 18:18:55 2009 -0500
@@ -238,7 +238,8 @@ test_lib_SOURCES = \
 	test-str-find.c \
 	test-str-sanitize.c \
 	test-time-util.c \
-	test-utc-mktime.c
+	test-utc-mktime.c \
+	test-var-expand.c
 
 test_headers = \
 	test-lib.h
diff -r c3d85a07888c -r e1b725d02c30 src/lib/test-lib.c
--- a/src/lib/test-lib.c	Tue Dec 22 18:12:33 2009 -0500
+++ b/src/lib/test-lib.c	Tue Dec 22 18:18:55 2009 -0500
@@ -27,6 +27,7 @@ int main(void)
 		test_str_sanitize,
 		test_time_util,
 		test_utc_mktime,
+		test_var_expand,
 		NULL
 	};
 	return test_run(test_functions);
diff -r c3d85a07888c -r e1b725d02c30 src/lib/test-lib.h
--- a/src/lib/test-lib.h	Tue Dec 22 18:12:33 2009 -0500
+++ b/src/lib/test-lib.h	Tue Dec 22 18:18:55 2009 -0500
@@ -26,5 +26,6 @@ void test_str_sanitize(void);
 void test_str_sanitize(void);
 void test_time_util(void);
 void test_utc_mktime(void);
+void test_var_expand(void);
 
 #endif
diff -r c3d85a07888c -r e1b725d02c30 src/lib/test-var-expand.c
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/lib/test-var-expand.c	Tue Dec 22 18:18:55 2009 -0500
@@ -0,0 +1,43 @@
+/* Copyright (c) 2009 Dovecot authors, see the included COPYING file */
+
+#include "test-lib.h"
+#include "str.h"
+#include "env-util.h"
+#include "hostpid.h"
+#include "var-expand.h"
+
+struct var_expand_test {
+	const char *in;
+	const char *out;
+};
+
+static void test_var_expand_builtin(void)
+{
+	static struct var_expand_test tests[] = {
+		{ "%{hostname}", NULL },
+		{ "%{pid}", NULL },
+		{ "a%{env:FOO}b", "abaRb" }
+	};
+	static struct var_expand_table table[] = {
+		{ '\0', NULL, NULL }
+	};
+	string_t *str = t_str_new(128);
+	unsigned int i;
+
+	tests[0].out = my_hostname;
+	tests[1].out = my_pid;
+	env_put("FOO=baR");
+
+	test_begin("var_expand");
+	for (i = 0; i < N_ELEMENTS(tests); i++) {
+		str_truncate(str, 0);
+		var_expand(str, tests[i].in, table);
+		test_assert(strcmp(tests[i].out, str_c(str)) == 0);
+	}
+	test_end();
+}
+
+void test_var_expand(void)
+{
+	test_var_expand_builtin();
+}
diff -r c3d85a07888c -r e1b725d02c30 src/lib/var-expand.c
--- a/src/lib/var-expand.c	Tue Dec 22 18:12:33 2009 -0500
+++ b/src/lib/var-expand.c	Tue Dec 22 18:18:55 2009 -0500
@@ -5,6 +5,7 @@
 #include "md5.h"
 #include "hash.h"
 #include "hex-binary.h"
+#include "hostpid.h"
 #include "str.h"
 #include "strescape.h"
 #include "var-expand.h"
@@ -136,6 +137,41 @@ static const struct var_expand_modifier 
 	{ '\0', NULL }
 };
 
+static const char *
+var_expand_long(const struct var_expand_table *table,
+		const void *key_start, unsigned int key_len)
+{
+        const struct var_expand_table *t;
+	const char *value = NULL;
+
+	for (t = table; !TABLE_LAST(t); t++) {
+		if (t->long_key != NULL &&
+		    strncmp(t->long_key, key_start, key_len) == 0 &&
+		    t->long_key[key_len] == '\0') {
+			return t->value != NULL ? t->value : "";
+		}
+	}
+
+	/* built-in variables: */
+	T_BEGIN {
+		const char *key = t_strndup(key_start, key_len);
+
+		switch (key_len) {
+		case 3:
+			if (strcmp(key, "pid") == 0)
+				value = my_pid;
+			break;
+		case 8:
+			if (strcmp(key, "hostname") == 0)
+				value = my_hostname;
+			break;
+		}
+		if (strncmp(key, "env:", 4) == 0)
+			value = getenv(key + 4);
+	} T_END;
+	return value;
+}
+
 void var_expand(string_t *dest, const char *str,
 		const struct var_expand_table *table)
 {
@@ -216,17 +252,9 @@ void var_expand(string_t *dest, const ch
 			if (*str == '{' && (end = strchr(str, '}')) != NULL) {
 				/* %{long_key} */
 				len = end - (str + 1);
-				for (t = table; !TABLE_LAST(t); t++) {
-					if (t->long_key != NULL &&
-					    strncmp(t->long_key, str+1,
-						    len) == 0 &&
-					    t->long_key[len] == '\0') {
-						var = t->value != NULL ?
-							t->value : "";
-						str = end;
-						break;
-					}
-				}
+				var = var_expand_long(table, str+1, len);
+				if (var != NULL)
+					str = end;
 			} else {
 				for (t = table; !TABLE_LAST(t); t++) {
 					if (t->key == *str) {


More information about the dovecot-cvs mailing list