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;
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",
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",
{
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 =
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);
}
}
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,
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",
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";
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();
}
/* 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);
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;
}
}
- 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;
}
} 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;
}
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;
*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 : "";
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);
}
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;
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 != '%')
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
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.
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;
}
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;
}