From: Timo Sirainen Date: Thu, 7 Mar 2024 07:30:30 +0000 (+0200) Subject: lib: Add var_expand() with new API and use it to implement other var_expand_*() X-Git-Tag: 2.4.1~950 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=823271dd29b2f0ed8d2162f7cd63b623144cc21b;p=thirdparty%2Fdovecot%2Fcore.git lib: Add var_expand() with new API and use it to implement other var_expand_*() --- diff --git a/src/lib/var-expand.c b/src/lib/var-expand.c index 77f107b1f5..33b98c8ec7 100644 --- a/src/lib/var-expand.c +++ b/src/lib/var-expand.c @@ -596,22 +596,34 @@ int var_expand_with_funcs(string_t *dest, const char *str, const struct var_expand_func_table *func_table, void *context, const char **error_r) { - const struct var_expand_table *tables[2] = { table, NULL }; - const struct var_expand_params_func funcs[] = { - { func_table, context }, - { NULL, NULL } + const struct var_expand_params params = { + .table = table, + .func_table = func_table, + .func_context = context, }; - - return var_expand_with_arrays(dest, str, tables, funcs, error_r); + return var_expand(dest, str, ¶ms, error_r); } int var_expand_with_arrays(string_t *dest, const char *str, const struct var_expand_table *const *tables, const struct var_expand_params_func *funcs, const char **error_r) +{ + const struct var_expand_params params = { + .tables_arr = tables, + .funcs_arr = funcs, + }; + return var_expand(dest, str, ¶ms, error_r); +} + +int var_expand(string_t *dest, const char *str, + const struct var_expand_params *params, const char **error_r) { static const struct var_expand_table *empty_table = NULL; static const struct var_expand_params_func empty_funcs = { NULL, NULL }; + const struct var_expand_table *tables[2] = { NULL, NULL }; + struct var_expand_params_func funcs[2] = { + { NULL, NULL }, { NULL, NULL } }; const struct var_expand_modifier *m; const char *var; struct var_expand_context ctx; @@ -625,8 +637,24 @@ int var_expand_with_arrays(string_t *dest, const char *str, *error_r = NULL; i_zero(&ctx); - ctx.tables = tables != NULL ? tables : &empty_table; - ctx.funcs = funcs != NULL ? funcs : &empty_funcs; + if (params->table != NULL) { + i_assert(params->tables_arr == NULL); + tables[0] = params->table; + ctx.tables = tables; + } else if (params->tables_arr != NULL) + ctx.tables = params->tables_arr; + else + ctx.tables = &empty_table; + + if (params->func_table != NULL) { + i_assert(params->funcs_arr == NULL); + funcs[0].table = params->func_table; + funcs[0].context = params->func_context; + ctx.funcs = funcs; + } else if (params->funcs_arr != NULL) + ctx.funcs = params->funcs_arr; + else + ctx.funcs = &empty_funcs; for (; *str != '\0'; str++) { if (*str != '%') @@ -783,7 +811,10 @@ int var_expand_with_table(string_t *dest, const char *str, const struct var_expand_table *table, const char **error_r) { - return var_expand_with_funcs(dest, str, table, NULL, NULL, error_r); + const struct var_expand_params params = { + .table = table, + }; + return var_expand(dest, str, ¶ms, error_r); } static bool diff --git a/src/lib/var-expand.h b/src/lib/var-expand.h index 130c3ef2c2..7272efe5c5 100644 --- a/src/lib/var-expand.h +++ b/src/lib/var-expand.h @@ -20,6 +20,23 @@ struct var_expand_params_func { void *context; }; +struct var_expand_params { + /* Single table: */ + const struct var_expand_table *table; + const struct var_expand_func_table *func_table; + void *func_context; + + /* Alternatively, multiple tables: */ + const struct var_expand_table *const *tables_arr; + const struct var_expand_params_func *funcs_arr; +}; + +/* Expand % variables in src and append the string in dest. Returns 1 on + success, 0 if the format string contained invalid/unknown %variables, -1 if + one of the functions returned temporary error. Even in case of errors the + dest string is still written as fully as possible. */ +int var_expand(string_t *dest, const char *str, + const struct var_expand_params *params, const char **error_r); /* 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