]> git.ipfire.org Git - thirdparty/dovecot/core.git/commitdiff
lib: var-expand - Support single character variable functions
authorTimo Sirainen <timo.sirainen@open-xchange.com>
Sat, 26 Feb 2022 20:07:17 +0000 (21:07 +0100)
committerTimo Sirainen <timo.sirainen@open-xchange.com>
Wed, 30 Nov 2022 12:32:11 +0000 (14:32 +0200)
If the function name is "f", it's possible to use both %f and %{f} to
access the function.

src/lib/test-var-expand.c
src/lib/var-expand.c

index d6710a3c8e591ae05c9cc336bb6fc05862c6f292..ed9f87de25e725b9b60e078ede1c6719be4c2710 100644 (file)
@@ -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}", "<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 },
index ba74962faa51ce5036677fa5c6e7331345764c6d..302b017be28e27802b6763ad134c2c6284cb9cdd 100644 (file)
@@ -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);