[PATCH] libosmocore[master]: vty: derive node name from prompt, use as XML ids

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/.

Neels Hofmeyr gerrit-no-reply at lists.osmocom.org
Mon Sep 18 15:46:12 UTC 2017


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

vty: derive node name from prompt, use as XML ids

The 'show online-help' produces XML output with <node id="..."> ids.  We
reference those from the osmo-gsm-manuals.

Instead of numeric IDs coming from internal code, rather use a human-readable
node ID. In order to obtain such while remaining backwards compatible, derive
such a name from the command node's prompt by stripping non-alnum characters.

If this is merged, we need to adjust the references in osmo-gsm-manuals.git.

Change-Id: I8fa555570268b231c5e01727c661da92fad265de
---
M include/osmocom/vty/command.h
M src/vty/command.c
2 files changed, 49 insertions(+), 1 deletion(-)


  git pull ssh://gerrit.osmocom.org:29418/libosmocore refs/changes/79/3979/1

diff --git a/include/osmocom/vty/command.h b/include/osmocom/vty/command.h
index 0fa5175..580c58e 100644
--- a/include/osmocom/vty/command.h
+++ b/include/osmocom/vty/command.h
@@ -123,6 +123,11 @@
 
 	/*! Vector of this node's command list. */
 	vector cmd_vector;
+
+	/*! Human-readable ID of this node. Should only contain alphanumeric
+	 * plus '-' and '_' characters (is used as XML ID for 'show
+	 * online-help'). If left NUL, this is derived from the prompt.*/
+	char name[64];
 };
 
 enum {
diff --git a/src/vty/command.c b/src/vty/command.c
index 8512ec8..76b6851 100644
--- a/src/vty/command.c
+++ b/src/vty/command.c
@@ -123,12 +123,40 @@
 	return str;
 }
 
+/* For the lack of a better defined way to derive a node name from user input,
+ * mangle the provided prompt to make a node name that consists of only alnum,
+ * '-' and '_'. For example used for XML IDs in 'show online-help'. */
+static const char *node_name_from_prompt(const char *prompt, char *name_buf, size_t name_buf_size)
+{
+	const char *pos;
+	int dest = 0;
+
+	if (!prompt || !*prompt)
+		return "";
+
+	for (pos = prompt; *pos && dest < (name_buf_size-1); pos++) {
+		if (pos[0] == '%' && pos[1]) {
+			/* skip "%s"; loop pos++ does the second one. */
+			pos++;
+			continue;
+		}
+		if (!(isalnum(pos[0]) || pos[0] == '-' || pos[0] == '_'))
+			continue;
+		name_buf[dest] = pos[0];
+		dest++;
+	}
+	name_buf[dest] = '\0';
+	return name_buf;
+}
+
 /*! Install top node of command vector. */
 void install_node(struct cmd_node *node, int (*func) (struct vty *))
 {
 	vector_set_index(cmdvec, node->node, node);
 	node->func = func;
 	node->cmd_vector = vector_init(VECTOR_MIN_SIZE);
+	if (!node->name || !*node->name)
+		node_name_from_prompt(node->prompt, node->name, sizeof(node->name));
 }
 
 /* Compare two command's string.  Used in sort_node (). */
@@ -570,6 +598,7 @@
 static int vty_dump_nodes(struct vty *vty)
 {
 	int i, j;
+	int same_name_count;
 
 	vty_out(vty, "<vtydoc xmlns='urn:osmocom:xml:libosmocore:vty:doc:1.0'>%s", VTY_NEWLINE);
 
@@ -579,7 +608,21 @@
 		if (!cnode)
 			continue;
 
-		vty_out(vty, "  <node id='%d'>%s", cnode->node, VTY_NEWLINE);
+		/* How many times has this same prompt been used before? */
+		same_name_count = 1;
+		for (j = 0; j < i; ++j) {
+			struct cmd_node *cnode2;
+			cnode2 = vector_slot(cmdvec, j);
+			if (!cnode2)
+				continue;
+			if (strcmp(cnode->name, cnode2->name) == 0)
+				same_name_count ++;
+		}
+
+		vty_out(vty, "  <node id='%s", cnode->name);
+		if (same_name_count > 1 || !*cnode->name)
+			vty_out(vty, "_%d", same_name_count);
+		vty_out(vty, "'>%s", VTY_NEWLINE);
 
 		for (j = 0; j < vector_active(cnode->cmd_vector); ++j) {
 			struct cmd_element *elem;

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

Gerrit-MessageType: newchange
Gerrit-Change-Id: I8fa555570268b231c5e01727c661da92fad265de
Gerrit-PatchSet: 1
Gerrit-Project: libosmocore
Gerrit-Branch: master
Gerrit-Owner: Neels Hofmeyr <nhofmeyr at sysmocom.de>



More information about the gerrit-log mailing list