[PATCH] libosmocore[master]: Simplify ctrl cmd lookup

This is merely a historical archive of years 2008-2021, before the migration to mailman3.

A maintained and still updated list archive can be found at https://lists.osmocom.org/hyperkitty/list/gerrit-log@lists.osmocom.org/.

Max gerrit-no-reply at lists.osmocom.org
Tue May 2 14:52:33 UTC 2017


Review at  https://gerrit.osmocom.org/2462

Simplify ctrl cmd lookup

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

Change-Id: Ida1b389b571c60c26813cd29e61b3e4423c5df0f
---
M src/ctrl/control_if.c
1 file changed, 19 insertions(+), 21 deletions(-)


  git pull ssh://gerrit.osmocom.org:29418/libosmocore refs/changes/62/2462/1

diff --git a/src/ctrl/control_if.c b/src/ctrl/control_if.c
index 7d3edd2..cdcccc7 100644
--- a/src/ctrl/control_if.c
+++ b/src/ctrl/control_if.c
@@ -192,7 +192,7 @@
 {
 	char *request;
 	int i, j, ret, node;
-
+	bool break_cycle = false;
 	vector vline, cmdvec, cmds_vec;
 
 	if (cmd->type == CTRL_TYPE_SET_REPLY ||
@@ -247,14 +247,20 @@
 			}
 		}
 
-		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; j<vector_active(vline); j++) {
 				vector_set(cmdvec, vector_slot(vline, j));
@@ -270,10 +276,13 @@
 			}
 
 			ret = ctrl_cmd_exec(cmdvec, cmd, cmds_vec, data);
-
 			vector_free(cmdvec);
+			break_cycle = true;
 			break;
 		}
+
+		if (break_cycle)
+			break;
 
 		if (i+1 == vector_active(vline))
 			cmd->reply = "Command not present.";
@@ -300,17 +309,6 @@
 
 	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;
 }
 

-- 
To view, visit https://gerrit.osmocom.org/2462
To unsubscribe, visit https://gerrit.osmocom.org/settings

Gerrit-MessageType: newchange
Gerrit-Change-Id: Ida1b389b571c60c26813cd29e61b3e4423c5df0f
Gerrit-PatchSet: 1
Gerrit-Project: libosmocore
Gerrit-Branch: master
Gerrit-Owner: Max <msuraev at sysmocom.de>



More information about the gerrit-log mailing list