]> git.ipfire.org Git - thirdparty/dovecot/core.git/commitdiff
lib, global: var_expand_with_arrays() - Replace func_tables/func_contexts with struct...
authorTimo Sirainen <timo.sirainen@open-xchange.com>
Wed, 6 Mar 2024 18:24:25 +0000 (20:24 +0200)
committerAki Tuomi <aki.tuomi@open-xchange.com>
Fri, 17 Jan 2025 08:39:58 +0000 (10:39 +0200)
src/lib-settings/settings.c
src/lib/test-var-expand.c
src/lib/var-expand-if.c
src/lib/var-expand-private.h
src/lib/var-expand.c
src/lib/var-expand.h
src/plugins/var-expand-crypt/var-expand-crypt-plugin.c

index 6738d173a78c497146513094799bbf60a65ff517..902b7b5c327ee7bf84fb4a913dd841d5738be228 100644 (file)
@@ -143,8 +143,7 @@ struct settings_apply_ctx {
 
        string_t *str;
        const struct var_expand_table *const *tables;
-       const struct var_expand_func_table *const *func_tables;
-       void *const *func_contexts;
+       const struct var_expand_params_func *funcs;
 };
 
 static ARRAY(const struct setting_parser_info *) set_registered_infos;
@@ -533,8 +532,7 @@ settings_mmap_apply_key(struct settings_apply_ctx *ctx, unsigned int key_idx,
                        value = file.path;
                }
                if (var_expand_with_arrays(ctx->str, value, ctx->tables,
-                                          ctx->func_tables, ctx->func_contexts,
-                                          &error) <= 0 &&
+                                          ctx->funcs, &error) <= 0 &&
                    (ctx->flags & SETTINGS_GET_FLAG_FAKE_EXPAND) == 0) {
                        *error_r = t_strdup_printf(
                                "Failed to expand %s setting variables: %s",
@@ -601,9 +599,7 @@ settings_mmap_apply_defaults(struct settings_apply_ctx *ctx,
                        const char *error;
                        str_truncate(ctx->str, 0);
                        if (var_expand_with_arrays(ctx->str, value, ctx->tables,
-                                                  ctx->func_tables,
-                                                  ctx->func_contexts,
-                                                  &error) <= 0 &&
+                                                  ctx->funcs, &error) <= 0 &&
                            (ctx->flags & SETTINGS_GET_FLAG_FAKE_EXPAND) == 0) {
                                *error_r = t_strdup_printf(
                                        "Failed to expand default setting %s=%s variables: %s",
@@ -1052,15 +1048,12 @@ settings_var_expand_init(struct settings_apply_ctx *ctx)
 {
        struct event *event = ctx->event;
        ARRAY(const struct var_expand_table *) tables;
-       ARRAY(const struct var_expand_func_table *) func_tables;
-       ARRAY(void *) func_contexts;
+       ARRAY(struct var_expand_params_func) funcs;
        const struct var_expand_table *table;
        const struct var_expand_func_table *func_table;
-       void *func_context;
 
        t_array_init(&tables, 4);
-       t_array_init(&func_tables, 4);
-       t_array_init(&func_contexts, 4);
+       t_array_init(&funcs, 4);
 
        while (event != NULL) {
                settings_var_expand_t *callback =
@@ -1070,10 +1063,11 @@ settings_var_expand_init(struct settings_apply_ctx *ctx)
                        if (table != NULL)
                                array_push_back(&tables, &table);
                        if (func_table != NULL) {
-                               func_context = event_get_ptr(event,
+                               struct var_expand_params_func *func =
+                                       array_append_space(&funcs);
+                               func->table = func_table;
+                               func->context = event_get_ptr(event,
                                        SETTINGS_EVENT_VAR_EXPAND_FUNC_CONTEXT);
-                               array_push_back(&func_tables, &func_table);
-                               array_push_back(&func_contexts, &func_context);
                        }
                }
 
@@ -1083,21 +1077,19 @@ settings_var_expand_init(struct settings_apply_ctx *ctx)
 
                func_table = event_get_ptr(event, SETTINGS_EVENT_VAR_EXPAND_FUNC_TABLE);
                if (func_table != NULL) {
-                       func_context = event_get_ptr(event,
+                       struct var_expand_params_func *func =
+                               array_append_space(&funcs);
+                       func->table = func_table;
+                       func->context = event_get_ptr(event,
                                SETTINGS_EVENT_VAR_EXPAND_FUNC_CONTEXT);
-                       array_push_back(&func_tables, &func_table);
-                       array_push_back(&func_contexts, &func_context);
                }
 
                event = event_get_parent(event);
        }
-       array_append_zero(&tables);
-       array_append_zero(&func_tables);
+       array_append_zero(&funcs);
 
        ctx->tables = array_front(&tables);
-       ctx->func_tables = array_front(&func_tables);
-       ctx->func_contexts = array_count(&func_contexts) == 0 ? NULL :
-               array_front(&func_contexts);
+       ctx->funcs = array_front(&funcs);
 }
 
 static int settings_override_cmp(struct settings_override *const *set1,
@@ -1523,9 +1515,7 @@ settings_instance_override(struct settings_apply_ctx *ctx,
                        const char *error;
                        str_truncate(ctx->str, 0);
                        if (var_expand_with_arrays(ctx->str, value, ctx->tables,
-                                                  ctx->func_tables,
-                                                  ctx->func_contexts,
-                                                  &error) <= 0 &&
+                                                  ctx->funcs, &error) <= 0 &&
                            (ctx->flags & SETTINGS_GET_FLAG_FAKE_EXPAND) == 0) {
                                *error_r = t_strdup_printf(
                                        "Failed to expand default setting %s=%s variables: %s",
index 089adab162e4f42473490d69aad8414fe924d02f..f9b17192d9db9f14abfa4e714a8b21c116ff3999 100644 (file)
@@ -258,11 +258,10 @@ static void test_var_expand_with_arrays(void)
        static const struct var_expand_table *tables[] = {
                table1, table2, NULL
        };
-       static const struct var_expand_func_table *func_tables[] = {
-               func_table1, func_table2, NULL
-       };
-       static void *func_contexts[] = {
-               "context1", "context2",
+       static const struct var_expand_params_func funcs[] = {
+               { func_table1, "context1", },
+               { func_table2, "context2", },
+               { NULL, NULL }
        };
        const char *input = "%f, %s, %{first}, %{second}, %{func1}, %{func2}";
        const char *output = "firstvalue, secondvalue, firstvalue, secondvalue, context1, context2";
@@ -270,8 +269,7 @@ static void test_var_expand_with_arrays(void)
        const char *error;
 
        test_begin("var_expand_with_arrays");
-       test_assert(var_expand_with_arrays(str, input, tables, func_tables,
-                                          func_contexts, &error) == 1);
+       test_assert(var_expand_with_arrays(str, input, tables, funcs, &error) == 1);
        test_assert_strcmp(str_c(str), output);
        test_end();
 }
index 6da05a868288b60fb8dc5dd12531a4575cbf92b4..3050130399828fd13324f43c6e26505c9e057480 100644 (file)
@@ -250,8 +250,7 @@ int var_expand_if(struct var_expand_context *ctx,
                /* expand the parameters */
                string_t *param = t_str_new(64);
                ret = var_expand_with_arrays(param, *parms, ctx->tables,
-                                            ctx->func_tables, ctx->contexts,
-                                            error_r);
+                                            ctx->funcs, error_r);
                if (ret <= 0)
                        return ret;
                const char *p = str_c(param);
index 2de885dd3ac0a4784b18833291aacfc9e43820cd..868fca7a761fb02be913bc245ab78bf0d1683440 100644 (file)
@@ -4,10 +4,8 @@
 struct var_expand_context {
        /* NULL-terminated array of variable tables */
        const struct var_expand_table *const *tables;
-       /* NULL-terminated array of function tables */
-       const struct var_expand_func_table *const *func_tables;
-       /* contexts for each function table */
-       void *const *contexts;
+       /* table=NULL-terminated array of function tables */
+       const struct var_expand_params_func *funcs;
 
        /* last offset, negative counts from end*/
        int offset;
index e7bd02adcc76b6f0adf76c122ecf20b14bf4a0bf..77f107b1f5f25af39c1e190cfec8e825528471a3 100644 (file)
@@ -195,14 +195,14 @@ var_expand_short(const struct var_expand_context *ctx, char key,
                }
        }
 
-       for (unsigned int j = 0; ctx->func_tables[j] != NULL; j++) {
-               const struct var_expand_func_table *func_table = ctx->func_tables[j];
+       for (unsigned int j = 0; ctx->funcs[j].table != NULL; j++) {
+               const struct var_expand_func_table *func_table = ctx->funcs[j].table;
                for (unsigned int i = 0; func_table[i].key != NULL; i++) {
                        if (func_table[i].key[0] == key &&
                            func_table[i].key[1] == '\0') {
                                const char *value;
                                int ret = func_table->func(
-                                       "", ctx->contexts[j], &value, error_r);
+                                       "", ctx->funcs[j].context, &value, error_r);
                                *var_r = value != NULL ? value : "";
                                return ret;
                        }
@@ -309,8 +309,7 @@ var_expand_hash(struct var_expand_context *ctx,
                } else if (strcmp(k, "salt") == 0) {
                        str_truncate(salt, 0);
                        ret = var_expand_with_arrays(salt, value, ctx->tables,
-                                                    ctx->func_tables,
-                                                    ctx->contexts, error_r);
+                                                    ctx->funcs, error_r);
                        if (ret <= 0)
                                return ret;
                        break;
@@ -500,8 +499,8 @@ var_expand_dovecot(struct var_expand_context *ctx ATTR_UNUSED,
 }
 
 static int
-var_expand_func(const struct var_expand_func_table *const *func_tables,
-               const char *key, const char *data, void *const *contexts,
+var_expand_func(const struct var_expand_params_func *funcs,
+               const char *key, const char *data,
                const char **var_r, const char **error_r)
 {
        const char *value = NULL;
@@ -512,11 +511,11 @@ var_expand_func(const struct var_expand_func_table *const *func_tables,
                *var_r = value != NULL ? value : "";
                return 1;
        }
-       for (unsigned int i = 0; func_tables[i] != NULL; i++) {
-               const struct var_expand_func_table *func_table = func_tables[i];
+       for (unsigned int i = 0; funcs[i].table != NULL; i++) {
+               const struct var_expand_func_table *func_table = funcs[i].table;
                for (; func_table->key != NULL; func_table++) {
                        if (strcmp(func_table->key, key) == 0) {
-                               ret = func_table->func(data, contexts[i], &value, error_r);
+                               ret = func_table->func(data, funcs[i].context, &value, error_r);
                                if (*error_r == NULL)
                                        *error_r = t_strdup_printf("Unknown variables in function %%%s", key);
                                *var_r = value != NULL ? value : "";
@@ -551,8 +550,7 @@ var_expand_try_extension(struct var_expand_context *ctx,
                        return ret;
                }
        }
-       return var_expand_func(ctx->func_tables, key, data,
-                              ctx->contexts, var_r, error_r);
+       return var_expand_func(ctx->funcs, key, data, var_r, error_r);
 }
 
 
@@ -599,20 +597,21 @@ int var_expand_with_funcs(string_t *dest, const char *str,
                          void *context, const char **error_r)
 {
        const struct var_expand_table *tables[2] = { table, NULL };
-       const struct var_expand_func_table *func_tables[2] = { func_table, NULL };
-       void *contexts[2] = { context, NULL };
+       const struct var_expand_params_func funcs[] = {
+               { func_table, context },
+               { NULL, NULL }
+       };
 
-       return var_expand_with_arrays(dest, str, tables, func_tables,
-                                     contexts, error_r);
+       return var_expand_with_arrays(dest, str, tables, funcs, error_r);
 }
 
 int var_expand_with_arrays(string_t *dest, const char *str,
                           const struct var_expand_table *const *tables,
-                          const struct var_expand_func_table *const *func_tables,
-                          void *const *func_contexts, const char **error_r)
+                          const struct var_expand_params_func *funcs,
+                          const char **error_r)
 {
        static const struct var_expand_table *empty_table = NULL;
-       static const struct var_expand_func_table *empty_func_table = NULL;
+       static const struct var_expand_params_func empty_funcs = { NULL, NULL };
        const struct var_expand_modifier *m;
        const char *var;
        struct var_expand_context ctx;
@@ -627,8 +626,7 @@ int var_expand_with_arrays(string_t *dest, const char *str,
 
        i_zero(&ctx);
        ctx.tables = tables != NULL ? tables : &empty_table;
-       ctx.func_tables = func_tables != NULL ? func_tables : &empty_func_table;
-       ctx.contexts = func_contexts;
+       ctx.funcs = funcs != NULL ? funcs : &empty_funcs;
 
        for (; *str != '\0'; str++) {
                if (*str != '%')
index ee46af703f06df684e8e859d0dcc57133ec042b3..130c3ef2c2943fc25800061a2b23d797d6dd1d5f 100644 (file)
@@ -15,6 +15,11 @@ struct var_expand_func_table {
                    const char **value_r, const char **error_r);
 };
 
+struct var_expand_params_func {
+       const struct var_expand_func_table *table;
+       void *context;
+};
+
 /* Expand % variables in src and append the string in dest.
    table must end with key = 0. Returns 1 on success, 0 if the format string
    contained invalid/unknown %variables, -1 if one of the functions returned
@@ -33,8 +38,8 @@ int var_expand_with_funcs(string_t *dest, const char *str,
    Each func_table[n] has a matching func_context[n] */
 int var_expand_with_arrays(string_t *dest, const char *str,
                           const struct var_expand_table *const *tables,
-                          const struct var_expand_func_table *const *func_tables,
-                          void *const *func_contexts, const char **error_r);
+                          const struct var_expand_params_func *funcs,
+                          const char **error_r);
 
 /* 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.
index 79567ca974d7fbf556399bcb068c0e441b0dd8e5..0de0f7d5fdbfa774617ece2098992d3b5ffeb795 100644 (file)
@@ -54,8 +54,7 @@ var_expand_crypt_settings(struct var_expand_crypt_context *ctx,
                        str_truncate(ctx->iv, 0);
                        if ((ret = var_expand_with_arrays(ctx->iv, value,
                                                          ctx->ctx->tables,
-                                                         ctx->ctx->func_tables,
-                                                         ctx->ctx->contexts,
+                                                         ctx->ctx->funcs,
                                                          error_r)) <= 0) {
                                return ret;
                        }
@@ -71,8 +70,7 @@ var_expand_crypt_settings(struct var_expand_crypt_context *ctx,
                        str_truncate(ctx->enckey, 0);
                        if ((ret = var_expand_with_arrays(ctx->enckey, value,
                                                          ctx->ctx->tables,
-                                                         ctx->ctx->func_tables,
-                                                         ctx->ctx->contexts,
+                                                         ctx->ctx->funcs,
                                                          error_r)) <= 0) {
                                return ret;
                        }