From 33c7ba693416bca679c60e15b31cf425bda75a2f Mon Sep 17 00:00:00 2001 From: Max Date: Tue, 2 May 2017 16:24:12 +0200 Subject: Simplify ctrl cmd lookup Replace if-else ladder & gotos with single switch statement & explicit return to make reading code easier. Change-Id: Ida1b389b571c60c26813cd29e61b3e4423c5df0f --- src/ctrl/control_if.c | 40 +++++++++++++++++++--------------------- src/ctrl/fsm_ctrl_commands.c | 17 ++++++----------- 2 files changed, 25 insertions(+), 32 deletions(-) diff --git a/src/ctrl/control_if.c b/src/ctrl/control_if.c index c4b6f910..5a84f7c0 100644 --- a/src/ctrl/control_if.c +++ b/src/ctrl/control_if.c @@ -192,7 +192,7 @@ int ctrl_cmd_handle(struct ctrl_handle *ctrl, struct ctrl_cmd *cmd, { char *request; int i, j, ret, node; - + bool break_cycle = false; vector vline, cmdvec, cmds_vec; 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) { - /* do nothing */ - } else if (rc == -ENODEV) - goto err_missing; - else if (rc == -ERANGE) - goto err_index; - else { - /* If we're here the rest must be the command */ + switch (rc) { + case 1: /* do nothing */ + break; + case -ENODEV: + cmd_free_strvec(vline); + cmd->type = CTRL_TYPE_ERROR; + cmd->reply = "Error while resolving object"; + 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); for (j=i; jreply = "Command not present."; } @@ -304,17 +313,6 @@ err: if (ret == CTRL_CMD_ERROR) cmd->type = CTRL_TYPE_ERROR; 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; } diff --git a/src/ctrl/fsm_ctrl_commands.c b/src/ctrl/fsm_ctrl_commands.c index 64324f2d..95d5fca6 100644 --- a/src/ctrl/fsm_ctrl_commands.c +++ b/src/ctrl/fsm_ctrl_commands.c @@ -27,10 +27,10 @@ static int fsm_ctrl_node_lookup(void *data, vector vline, int *node_type, (*i)++; fsm_name = vector_lookup(vline, *i); if (!fsm_name) - goto err_index; + return -ERANGE; fsm = osmo_fsm_find_by_name(fsm_name); if (!fsm) - goto err_missing; + return -ENODEV; *node_data = fsm; *node_type = CTRL_NODE_FSM; } else @@ -43,10 +43,10 @@ static int fsm_ctrl_node_lookup(void *data, vector vline, int *node_type, (*i)++; inst_name = vector_lookup(vline, *i); if (!inst_name) - goto err_index; + return -ERANGE; fi = osmo_fsm_inst_find_by_name(fsm, inst_name); if (!fi) - goto err_missing; + return -ENODEV; *node_data = fi; *node_type = CTRL_NODE_FSM_INST; } else if (!strcmp(token, "id")) { @@ -54,10 +54,10 @@ static int fsm_ctrl_node_lookup(void *data, vector vline, int *node_type, (*i)++; inst_id = vector_lookup(vline, *i); if (!inst_id) - goto err_index; + return -ERANGE; fi = osmo_fsm_inst_find_by_id(fsm, inst_id); if (!fi) - goto err_missing; + return -ENODEV; *node_data = fi; *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; - -err_index: - return -ERANGE; -err_missing: - return -ENODEV; } static int get_fsm_inst_state(struct ctrl_cmd *cmd, void *data) -- cgit v1.2.3