Fix cleanup of target_else in conditional action, added support for else branch in "when" action.

personal/stbuehler/wip
Stefan Bühler 14 years ago
parent ead4172d75
commit e15193447d
  1. 6
      src/actions.c
  2. 2
      src/actions.h
  3. 2
      src/config_parser.rl
  4. 22
      src/plugin_core.c

@ -28,6 +28,7 @@ void action_release(server *srv, action *a) {
case ACTION_TCONDITION:
condition_release(srv, a->value.condition.cond);
action_release(srv, a->value.condition.target);
action_release(srv, a->value.condition.target_else);
break;
case ACTION_TLIST:
for (i = a->value.list->len; i-- > 0; ) {
@ -79,14 +80,15 @@ action *action_new_list() {
return a;
}
action *action_new_condition(condition *cond, action *target) {
action *action_new_condition(condition *cond, action *target, action *target_else) {
action *a;
a = g_slice_new0(action);
a = g_slice_new(action);
a->refcount = 1;
a->type = ACTION_TCONDITION;
a->value.condition.cond = cond;
a->value.condition.target = target;
a->value.condition.target_else = target_else;
return a;
}

@ -78,6 +78,6 @@ LI_API void action_acquire(action *a);
LI_API action *action_new_setting(option_set setting);
LI_API action *action_new_function(ActionFunc func, ActionFree free, gpointer param);
LI_API action *action_new_list();
LI_API action *action_new_condition(condition *cond, action *target);
LI_API action *action_new_condition(condition *cond, action *target, action *target_else);
#endif

@ -615,7 +615,7 @@
cond = g_queue_pop_head(ctx->condition_stack);
al = g_queue_pop_head(ctx->action_list_stack);
a = action_new_condition(cond, al);
a = action_new_condition(cond, al, NULL);
al = g_queue_peek_head(ctx->action_list_stack);
g_array_append_val(al->value.list, a);
}

@ -41,8 +41,8 @@ static action* core_list(server *srv, plugin* p, option *opt) {
}
static action* core_when(server *srv, plugin* p, option *opt) {
option *opt_cond, *opt_act;
action *a;
option *opt_cond, *opt_act, *opt_act_else;
action *a, *act_else;
UNUSED(p);
if (!opt) {
@ -53,12 +53,19 @@ static action* core_when(server *srv, plugin* p, option *opt) {
ERROR(srv, "expected list, got %s", option_type_string(opt->type));
return NULL;
}
if (opt->value.opt_list->len != 2) {
ERROR(srv, "expected list with length 2, has length %u", opt->value.opt_list->len);
if (opt->value.opt_list->len == 2) {
opt_act_else = NULL;
act_else = NULL;
} else if (opt->value.opt_list->len == 3) {
opt_act_else = g_array_index(opt->value.opt_list, option*, 2);
act_else = opt_act_else->value.opt_action.action;
} else {
ERROR(srv, "expected list with length 2 or 3, has length %u", opt->value.opt_list->len);
return NULL;
}
opt_cond = g_array_index(opt->value.opt_list, option*, 0);
opt_act = g_array_index(opt->value.opt_list, option*, 1);
if (opt_cond->type != OPTION_CONDITION) {
ERROR(srv, "expected condition as first parameter, got %s", option_type_string(opt_cond->type));
return NULL;
@ -67,9 +74,14 @@ static action* core_when(server *srv, plugin* p, option *opt) {
ERROR(srv, "expected action as second parameter, got %s", option_type_string(opt_act->type));
return NULL;
}
if (opt_act_else && opt_act_else->type != OPTION_ACTION) {
ERROR(srv, "expected action as third parameter, got %s", option_type_string(opt_act_else->type));
return NULL;
}
condition_acquire(opt_cond->value.opt_cond.cond);
action_acquire(opt_act->value.opt_action.action);
a = action_new_condition(opt_cond->value.opt_cond.cond, opt_act->value.opt_action.action);
if (act_else) action_acquire(act_else);
a = action_new_condition(opt_cond->value.opt_cond.cond, opt_act->value.opt_action.action, act_else);
option_free(opt);
return a;
}

Loading…
Cancel
Save