dovecot-2.1: liblib: Added var_expand_with_funcs() to expand var...
dovecot at dovecot.org
dovecot at dovecot.org
Thu Feb 2 15:28:16 EET 2012
details: http://hg.dovecot.org/dovecot-2.1/rev/ba86a60e3059
changeset: 14053:ba86a60e3059
user: Timo Sirainen <tss at iki.fi>
date: Thu Feb 02 15:27:27 2012 +0200
description:
liblib: Added var_expand_with_funcs() to expand variables with function callbacks.
diffstat:
src/lib/var-expand.c | 45 +++++++++++++++++++++++++++++++++++++++------
src/lib/var-expand.h | 12 ++++++++++++
2 files changed, 51 insertions(+), 6 deletions(-)
diffs (107 lines):
diff -r 7edafe5c43da -r ba86a60e3059 src/lib/var-expand.c
--- a/src/lib/var-expand.c Thu Feb 02 15:25:04 2012 +0200
+++ b/src/lib/var-expand.c Thu Feb 02 15:27:27 2012 +0200
@@ -139,8 +139,25 @@
};
static const char *
+var_expand_func(const struct var_expand_func_table *func_table,
+ const char *key, const char *data, void *context)
+{
+ if (strcmp(key, "env") == 0)
+ return getenv(data);
+ if (func_table == NULL)
+ return NULL;
+
+ for (; func_table->key != NULL; func_table++) {
+ if (strcmp(func_table->key, key) == 0)
+ return func_table->func(data, context);
+ }
+ return 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_func_table *func_table,
+ const void *key_start, unsigned int key_len, void *context)
{
const struct var_expand_table *t;
const char *value = NULL;
@@ -171,14 +188,23 @@
value = my_hostname;
break;
}
- if (strncmp(key, "env:", 4) == 0)
- value = getenv(key + 4);
+ if (value == NULL) {
+ const char *data = strchr(key, ':');
+
+ if (data != NULL)
+ key = t_strdup_until(key, data++);
+ else
+ data = "";
+ value = var_expand_func(func_table, key, data, context);
+ }
} T_END;
return value;
}
-void var_expand(string_t *dest, const char *str,
- const struct var_expand_table *table)
+void var_expand_with_funcs(string_t *dest, const char *str,
+ const struct var_expand_table *table,
+ const struct var_expand_func_table *func_table,
+ void *context)
{
const struct var_expand_modifier *m;
const struct var_expand_table *t;
@@ -257,7 +283,8 @@
if (*str == '{' && (end = strchr(str, '}')) != NULL) {
/* %{long_key} */
len = end - (str + 1);
- var = var_expand_long(table, str+1, len);
+ var = var_expand_long(table, func_table,
+ str+1, len, context);
if (var != NULL)
str = end;
} else {
@@ -311,6 +338,12 @@
}
}
+void var_expand(string_t *dest, const char *str,
+ const struct var_expand_table *table)
+{
+ var_expand_with_funcs(dest, str, table, NULL, NULL);
+}
+
char var_get_key(const char *str)
{
const struct var_expand_modifier *m;
diff -r 7edafe5c43da -r ba86a60e3059 src/lib/var-expand.h
--- a/src/lib/var-expand.h Thu Feb 02 15:25:04 2012 +0200
+++ b/src/lib/var-expand.h Thu Feb 02 15:27:27 2012 +0200
@@ -7,10 +7,22 @@
const char *long_key;
};
+struct var_expand_func_table {
+ const char *key;
+ /* %{key:data}, or data is "" with %{key}, */
+ const char *(*func)(const char *data, void *context);
+};
+
/* Expand % variables in src and append the string in dest.
table must end with key = 0. */
void var_expand(string_t *dest, const char *str,
const struct var_expand_table *table);
+/* Like var_expand(), but support also callback functions for
+ variable expansion. */
+void var_expand_with_funcs(string_t *dest, const char *str,
+ const struct var_expand_table *table,
+ const struct var_expand_func_table *func_table,
+ void *func_context);
/* Returns the actual key character for given string, ie. skip any modifiers
that are before it. The string should be the data after the '%' character. */
More information about the dovecot-cvs
mailing list