test_end();
}
+static int test_var_expand_func0(const char *data ATTR_UNUSED,
+ void *context ATTR_UNUSED,
+ const char **value_r,
+ const char **error_r ATTR_UNUSED)
+{
+ *value_r = "0";
+ return 1;
+}
+
static int test_var_expand_func1(const char *data, void *context,
const char **value_r,
const char **error_r ATTR_UNUSED)
static void test_var_expand_with_funcs(void)
{
static const struct var_expand_test tests[] = {
+ { "%f", "0", 1 },
{ "%{func1}", "<>", 1 },
{ "%{func1:foo}", "<foo>", 1 },
{ "%{func2}", "", 1 },
{ '\0', NULL, NULL }
};
static const struct var_expand_func_table func_table[] = {
+ { "f", test_var_expand_func0 },
{ "func1", test_var_expand_func1 },
{ "func2", test_var_expand_func2 },
{ "func3", test_var_expand_func3 },
};
static int
-var_expand_short(const struct var_expand_table *table, char key,
+var_expand_short(const struct var_expand_context *ctx, char key,
const char **var_r, const char **error_r)
{
const struct var_expand_table *t;
- if (table != NULL) {
- for (t = table; !TABLE_LAST(t); t++) {
+ if (ctx->table != NULL) {
+ for (t = ctx->table; !TABLE_LAST(t); t++) {
if (t->key == key) {
*var_r = t->value != NULL ? t->value : "";
return 1;
}
}
+ if (ctx->func_table != NULL) {
+ for (unsigned int i = 0; ctx->func_table[i].key != NULL; i++) {
+ if (ctx->func_table[i].key[0] == key &&
+ ctx->func_table[i].key[1] == '\0') {
+ const char *value;
+ int ret = ctx->func_table->func(
+ "", ctx->context, &value, error_r);
+ *var_r = value != NULL ? value : "";
+ return ret;
+ }
+ }
+ }
+
/* not found */
if (key == '%') {
*var_r = "%";
&var, error_r);
str = end;
} else {
- ret = var_expand_short(ctx.table, *str,
+ ret = var_expand_short(&ctx, *str,
&var, error_r);
}
i_assert(var != NULL);