Simplify ctrl cmd lookup

Replace if-else ladder & gotos with single switch statement & explicit
return to make reading code easier.

Change-Id: Ida1b389b571c60c26813cd29e61b3e4423c5df0f
This commit is contained in:
Max 2017-05-02 16:24:12 +02:00
parent 85a6af213e
commit 33c7ba6934
2 changed files with 25 additions and 32 deletions

View File

@ -192,7 +192,7 @@ int ctrl_cmd_handle(struct ctrl_handle *ctrl, struct ctrl_cmd *cmd,
{ {
char *request; char *request;
int i, j, ret, node; int i, j, ret, node;
bool break_cycle = false;
vector vline, cmdvec, cmds_vec; vector vline, cmdvec, cmds_vec;
if (cmd->type == CTRL_TYPE_SET_REPLY || if (cmd->type == CTRL_TYPE_SET_REPLY ||
@ -250,14 +250,20 @@ int ctrl_cmd_handle(struct ctrl_handle *ctrl, struct ctrl_cmd *cmd,
} }
} }
if (rc == 1) { switch (rc) {
/* do nothing */ case 1: /* do nothing */
} else if (rc == -ENODEV) break;
goto err_missing; case -ENODEV:
else if (rc == -ERANGE) cmd_free_strvec(vline);
goto err_index; cmd->type = CTRL_TYPE_ERROR;
else { cmd->reply = "Error while resolving object";
/* If we're here the rest must be the command */ return ret;
case -ERANGE:
cmd_free_strvec(vline);
cmd->type = CTRL_TYPE_ERROR;
cmd->reply = "Error while parsing the index.";
return ret;
default: /* If we're here the rest must be the command */
cmdvec = vector_init(vector_active(vline)-i); cmdvec = vector_init(vector_active(vline)-i);
for (j=i; j<vector_active(vline); j++) { for (j=i; j<vector_active(vline); j++) {
vector_set(cmdvec, vector_slot(vline, j)); vector_set(cmdvec, vector_slot(vline, j));
@ -273,11 +279,14 @@ int ctrl_cmd_handle(struct ctrl_handle *ctrl, struct ctrl_cmd *cmd,
} }
ret = ctrl_cmd_exec(cmdvec, cmd, cmds_vec, data); ret = ctrl_cmd_exec(cmdvec, cmd, cmds_vec, data);
vector_free(cmdvec); vector_free(cmdvec);
break_cycle = true;
break; break;
} }
if (break_cycle)
break;
if (i+1 == vector_active(vline)) if (i+1 == vector_active(vline))
cmd->reply = "Command not present."; cmd->reply = "Command not present.";
} }
@ -304,17 +313,6 @@ err:
if (ret == CTRL_CMD_ERROR) if (ret == CTRL_CMD_ERROR)
cmd->type = CTRL_TYPE_ERROR; cmd->type = CTRL_TYPE_ERROR;
return ret; return ret;
err_missing:
cmd_free_strvec(vline);
cmd->type = CTRL_TYPE_ERROR;
cmd->reply = "Error while resolving object";
return ret;
err_index:
cmd_free_strvec(vline);
cmd->type = CTRL_TYPE_ERROR;
cmd->reply = "Error while parsing the index.";
return ret;
} }

View File

@ -27,10 +27,10 @@ static int fsm_ctrl_node_lookup(void *data, vector vline, int *node_type,
(*i)++; (*i)++;
fsm_name = vector_lookup(vline, *i); fsm_name = vector_lookup(vline, *i);
if (!fsm_name) if (!fsm_name)
goto err_index; return -ERANGE;
fsm = osmo_fsm_find_by_name(fsm_name); fsm = osmo_fsm_find_by_name(fsm_name);
if (!fsm) if (!fsm)
goto err_missing; return -ENODEV;
*node_data = fsm; *node_data = fsm;
*node_type = CTRL_NODE_FSM; *node_type = CTRL_NODE_FSM;
} else } else
@ -43,10 +43,10 @@ static int fsm_ctrl_node_lookup(void *data, vector vline, int *node_type,
(*i)++; (*i)++;
inst_name = vector_lookup(vline, *i); inst_name = vector_lookup(vline, *i);
if (!inst_name) if (!inst_name)
goto err_index; return -ERANGE;
fi = osmo_fsm_inst_find_by_name(fsm, inst_name); fi = osmo_fsm_inst_find_by_name(fsm, inst_name);
if (!fi) if (!fi)
goto err_missing; return -ENODEV;
*node_data = fi; *node_data = fi;
*node_type = CTRL_NODE_FSM_INST; *node_type = CTRL_NODE_FSM_INST;
} else if (!strcmp(token, "id")) { } else if (!strcmp(token, "id")) {
@ -54,10 +54,10 @@ static int fsm_ctrl_node_lookup(void *data, vector vline, int *node_type,
(*i)++; (*i)++;
inst_id = vector_lookup(vline, *i); inst_id = vector_lookup(vline, *i);
if (!inst_id) if (!inst_id)
goto err_index; return -ERANGE;
fi = osmo_fsm_inst_find_by_id(fsm, inst_id); fi = osmo_fsm_inst_find_by_id(fsm, inst_id);
if (!fi) if (!fi)
goto err_missing; return -ENODEV;
*node_data = fi; *node_data = fi;
*node_type = CTRL_NODE_FSM_INST; *node_type = CTRL_NODE_FSM_INST;
} }
@ -67,11 +67,6 @@ static int fsm_ctrl_node_lookup(void *data, vector vline, int *node_type,
} }
return 1; return 1;
err_index:
return -ERANGE;
err_missing:
return -ENODEV;
} }
static int get_fsm_inst_state(struct ctrl_cmd *cmd, void *data) static int get_fsm_inst_state(struct ctrl_cmd *cmd, void *data)