From: Timo Sirainen Date: Sat, 26 Feb 2022 20:07:17 +0000 (+0100) Subject: lib: var-expand - Support single character variable functions X-Git-Tag: 2.4.0~3373 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=5fd4963eaaee2de74d7a2ba17cb3ecd847bff858;p=thirdparty%2Fdovecot%2Fcore.git lib: var-expand - Support single character variable functions If the function name is "f", it's possible to use both %f and %{f} to access the function. --- diff --git a/src/lib/test-var-expand.c b/src/lib/test-var-expand.c index d6710a3c8e..ed9f87de25 100644 --- a/src/lib/test-var-expand.c +++ b/src/lib/test-var-expand.c @@ -110,6 +110,15 @@ static void test_var_get_key_range(void) test_end(); } +static int test_var_expand_func0(const char *data ATTR_UNUSED, + void *context ATTR_UNUSED, + const char **value_r, + const char **error_r ATTR_UNUSED) +{ + *value_r = "0"; + return 1; +} + static int test_var_expand_func1(const char *data, void *context, const char **value_r, const char **error_r ATTR_UNUSED) @@ -158,6 +167,7 @@ static int test_var_expand_func5(const char *data ATTR_UNUSED, static void test_var_expand_with_funcs(void) { static const struct var_expand_test tests[] = { + { "%f", "0", 1 }, { "%{func1}", "<>", 1 }, { "%{func1:foo}", "", 1 }, { "%{func2}", "", 1 }, @@ -171,6 +181,7 @@ static void test_var_expand_with_funcs(void) { '\0', NULL, NULL } }; static const struct var_expand_func_table func_table[] = { + { "f", test_var_expand_func0 }, { "func1", test_var_expand_func1 }, { "func2", test_var_expand_func2 }, { "func3", test_var_expand_func3 }, diff --git a/src/lib/var-expand.c b/src/lib/var-expand.c index ba74962faa..302b017be2 100644 --- a/src/lib/var-expand.c +++ b/src/lib/var-expand.c @@ -168,13 +168,13 @@ static const struct var_expand_modifier modifiers[] = { }; static int -var_expand_short(const struct var_expand_table *table, char key, +var_expand_short(const struct var_expand_context *ctx, char key, const char **var_r, const char **error_r) { const struct var_expand_table *t; - if (table != NULL) { - for (t = table; !TABLE_LAST(t); t++) { + if (ctx->table != NULL) { + for (t = ctx->table; !TABLE_LAST(t); t++) { if (t->key == key) { *var_r = t->value != NULL ? t->value : ""; return 1; @@ -182,6 +182,19 @@ var_expand_short(const struct var_expand_table *table, char key, } } + if (ctx->func_table != NULL) { + for (unsigned int i = 0; ctx->func_table[i].key != NULL; i++) { + if (ctx->func_table[i].key[0] == key && + ctx->func_table[i].key[1] == '\0') { + const char *value; + int ret = ctx->func_table->func( + "", ctx->context, &value, error_r); + *var_r = value != NULL ? value : ""; + return ret; + } + } + } + /* not found */ if (key == '%') { *var_r = "%"; @@ -597,7 +610,7 @@ int var_expand_with_funcs(string_t *dest, const char *str, &var, error_r); str = end; } else { - ret = var_expand_short(ctx.table, *str, + ret = var_expand_short(&ctx, *str, &var, error_r); } i_assert(var != NULL);