dovecot-1.2: var_expand(): Added support for built-in host, pid ...

dovecot at dovecot.org dovecot at dovecot.org
Wed Dec 23 01:23:03 EET 2009


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

diffstat:

1 file changed, 39 insertions(+), 11 deletions(-)
src/lib/var-expand.c |   50 +++++++++++++++++++++++++++++++++++++++-----------

diffs (74 lines):

diff -r 4e2134570b76 -r ef7b42b237bd src/lib/var-expand.c
--- a/src/lib/var-expand.c	Tue Dec 22 14:04:15 2009 -0500
+++ b/src/lib/var-expand.c	Tue Dec 22 18:22:43 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