ctrl: Allow installation of additional node lookup helpers

The existing code assumes that the main application knows about all
control command nodes and can thus present one lookup function.

As libraries are getting their own control interface handling, this
is too restrictive, and we need a way how library code can dynamically
register more node lookup helpers.   We can now do this by means of a
ctrl_lookup_register() function.

Change-Id: Ib69908d1c57f5bb721d5496e3b4a5258fca450e3
diff --git a/include/osmocom/ctrl/control_if.h b/include/osmocom/ctrl/control_if.h
index f2af1db..a740a96 100644
--- a/include/osmocom/ctrl/control_if.h
+++ b/include/osmocom/ctrl/control_if.h
@@ -29,3 +29,5 @@
 					       ctrl_cmd_lookup lookup);
 struct ctrl_connection *osmo_ctrl_conn_alloc(void *ctx, void *data);
 int ctrl_cmd_handle(struct ctrl_handle *ctrl, struct ctrl_cmd *cmd, void *data);
+
+int ctrl_lookup_register(ctrl_cmd_lookup lookup);
diff --git a/src/ctrl/control_if.c b/src/ctrl/control_if.c
index d2eb3e9..f1cc7ab 100644
--- a/src/ctrl/control_if.c
+++ b/src/ctrl/control_if.c
@@ -61,6 +61,13 @@
 
 vector ctrl_node_vec;
 
+/* global list of control interface lookup helpers */
+struct lookup_helper {
+	struct llist_head list;
+	ctrl_cmd_lookup lookup;
+};
+static LLIST_HEAD(ctrl_lookup_helpers);
+
 int ctrl_parse_get_num(vector vline, int i, long *num)
 {
 	char *token, *tmp;
@@ -225,6 +232,7 @@
 	}
 
 	for (i=0;i<vector_active(vline);i++) {
+		struct lookup_helper *lh;
 		int rc;
 
 		if (ctrl->lookup)
@@ -232,6 +240,14 @@
 		else
 			rc = 0;
 
+		if (!rc) {
+			llist_for_each_entry(lh, &ctrl_lookup_helpers, list) {
+				rc = lh->lookup(data, vline, &node, &cmd->node, &i);
+				if (rc)
+					break;
+			}
+		}
+
 		if (rc == 1) {
 			/* do nothing */
 		} else if (rc == -ENODEV)
@@ -733,3 +749,28 @@
 	talloc_free(ctrl);
 	return NULL;
 }
+
+/*! \brief Install a lookup helper function for control nodes
+ *  This function is used by e.g. library code to install lookup helpers
+ *  for additional nodes in the control interface.
+ *  \param[in] lookup The lookup helper function
+ *  \retuns - on success; negative on error.
+ */
+int ctrl_lookup_register(ctrl_cmd_lookup lookup)
+{
+	struct lookup_helper *lh;
+
+	/* avoid double registration */
+	llist_for_each_entry(lh, &ctrl_lookup_helpers, list) {
+		if (lh->lookup == lookup)
+			return -EEXIST;
+	}
+
+	lh = talloc_zero(NULL, struct lookup_helper);
+	if (!lh)
+		return -ENOMEM;
+
+	lh->lookup = lookup;
+	llist_add_tail(&lh->list, &ctrl_lookup_helpers);
+	return 0;
+}