[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
Wed Sep 20 16:24:13 UTC 2017


Hello Jenkins Builder, Holger Freyther,

I'd like you to reexamine a change.  Please visit

    https://gerrit.osmocom.org/3979

to look at the new patch set (#5).

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 -- referencing id='config-msc' is much easier than referencing id='23'.

Add a char name[] to struct cmd_node, to hold this name. This may be provided
upon struct definition.

Since callers of the VTY API so far don't have a name yet, we would need to add
names everywhere to get meaningful node IDs. There is a way to get node ID
names without touching dependent code:

My first idea was to find out which command entered the node, i.e. command
'msc' enters the MSC_NODE. But it is impossible to derive which command entered
which node from data structs, it's hidden in the vty command definition.

But in fact all (TM) known API callers indeed provide a prompt string that
contains a logical and human readable string name. Thus, if the name is unset
in the struct, parse the prompt string and strip all "weird" characters to
obtain a node name from that. We can still set names later on, but for now will
have meaningful node IDs (e.g. 'config-msc' from '%s(config-msc)# ') without
touching any dependent code.

When VTY nodes get identical node names, which is quite possible, the XML
export de-dups these by appending _2, _3,... suffixes. The first occurence is
called e.g. 'name', the second 'name_2', then 'name_3', and so forth.

If a node has no name (even after parsing the prompt), it will be named merely
by the suffix. The first empty node will become id='_1', then '_2', '_3', and
so forth. This happens for nodes like VIEW_NODE or AUTH_NODE.

If this is merged, we need to adjust the references in osmo-gsm-manuals.git.
This can happen in our own time though, because we manually create the vty
reference xml and copy it to the osmo-gsm-manuals.git and then update the
references from the vty_additions.xml. This anyway has to happen because
currently the references tend to be hopelessly out of sync anyway, placing
comments at wildly unrelated VTY commands.

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


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

diff --git a/include/osmocom/vty/command.h b/include/osmocom/vty/command.h
index cb2edaa..58f248f 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 9ca7339..31ad721 100644
--- a/src/vty/command.c
+++ b/src/vty/command.c
@@ -123,12 +123,40 @@
 	return str;
 }
 
+/* Strip all characters from a string (prompt) except for alnum, '-' and '_'.
+ * For example used to derive a node->name from node->prompt if the user didn't provide a name;
+ * in turn, this name us 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_from_prompt(node->prompt, node->name, sizeof(node->name));
 }
 
 /* Compare two command's string.  Used in sort_node (). */
@@ -608,6 +636,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);
 
@@ -617,7 +646,23 @@
 		if (!cnode)
 			continue;
 
-		vty_out(vty, "  <node id='%d'>%s", cnode->node, VTY_NEWLINE);
+		/* De-dup node IDs: how many times has this same name been used before? Count the first
+		 * occurence as _1 and omit that first suffix, so that the first occurence is called
+		 * 'name', the second becomes 'name_2', then 'name_3', ... */
+		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: newpatchset
Gerrit-Change-Id: I8fa555570268b231c5e01727c661da92fad265de
Gerrit-PatchSet: 5
Gerrit-Project: libosmocore
Gerrit-Branch: master
Gerrit-Owner: Neels Hofmeyr <nhofmeyr at sysmocom.de>
Gerrit-Reviewer: Holger Freyther <holger at freyther.de>
Gerrit-Reviewer: Jenkins Builder
Gerrit-Reviewer: Neels Hofmeyr <nhofmeyr at sysmocom.de>



More information about the gerrit-log mailing list