]> git.ipfire.org Git - thirdparty/dovecot/core.git/commitdiff
lib: Add var_expand() with new API and use it to implement other var_expand_*()
authorTimo Sirainen <timo.sirainen@open-xchange.com>
Thu, 7 Mar 2024 07:30:30 +0000 (09:30 +0200)
committerAki Tuomi <aki.tuomi@open-xchange.com>
Wed, 12 Feb 2025 10:34:12 +0000 (12:34 +0200)
src/lib/var-expand.c
src/lib/var-expand.h

index 77f107b1f5f25af39c1e190cfec8e825528471a3..33b98c8ec776bc9fa94c69c45ff121dc920a78ee 100644 (file)
@@ -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, &params, 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, &params, 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, &params, error_r);
 }
 
 static bool
index 130c3ef2c2943fc25800061a2b23d797d6dd1d5f..7272efe5c5c52791a331f5527c36930df9c84cce 100644 (file)
@@ -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