From ba8a212bcda9d9ce398881ca02a9f1dd863c4afc Mon Sep 17 00:00:00 2001 From: Timo Sirainen Date: Wed, 6 Mar 2024 20:24:25 +0200 Subject: [PATCH] lib, global: var_expand_with_arrays() - Replace func_tables/func_contexts with struct var_expand_params_func --- src/lib-settings/settings.c | 42 +++++++------------ src/lib/test-var-expand.c | 12 +++--- src/lib/var-expand-if.c | 3 +- src/lib/var-expand-private.h | 6 +-- src/lib/var-expand.c | 40 +++++++++--------- src/lib/var-expand.h | 9 +++- .../var-expand-crypt-plugin.c | 6 +-- 7 files changed, 52 insertions(+), 66 deletions(-) diff --git a/src/lib-settings/settings.c b/src/lib-settings/settings.c index 6738d173a7..902b7b5c32 100644 --- a/src/lib-settings/settings.c +++ b/src/lib-settings/settings.c @@ -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", diff --git a/src/lib/test-var-expand.c b/src/lib/test-var-expand.c index 089adab162..f9b17192d9 100644 --- a/src/lib/test-var-expand.c +++ b/src/lib/test-var-expand.c @@ -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(); } diff --git a/src/lib/var-expand-if.c b/src/lib/var-expand-if.c index 6da05a8682..3050130399 100644 --- a/src/lib/var-expand-if.c +++ b/src/lib/var-expand-if.c @@ -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); diff --git a/src/lib/var-expand-private.h b/src/lib/var-expand-private.h index 2de885dd3a..868fca7a76 100644 --- a/src/lib/var-expand-private.h +++ b/src/lib/var-expand-private.h @@ -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; diff --git a/src/lib/var-expand.c b/src/lib/var-expand.c index e7bd02adcc..77f107b1f5 100644 --- a/src/lib/var-expand.c +++ b/src/lib/var-expand.c @@ -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 != '%') diff --git a/src/lib/var-expand.h b/src/lib/var-expand.h index ee46af703f..130c3ef2c2 100644 --- a/src/lib/var-expand.h +++ b/src/lib/var-expand.h @@ -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. diff --git a/src/plugins/var-expand-crypt/var-expand-crypt-plugin.c b/src/plugins/var-expand-crypt/var-expand-crypt-plugin.c index 79567ca974..0de0f7d5fd 100644 --- a/src/plugins/var-expand-crypt/var-expand-crypt-plugin.c +++ b/src/plugins/var-expand-crypt/var-expand-crypt-plugin.c @@ -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; } -- 2.47.3