aboutsummaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
authorNeels Hofmeyr <neels@hofmeyr.de>2017-09-18 16:42:06 +0200
committerNeels Hofmeyr <nhofmeyr@sysmocom.de>2017-09-23 12:24:58 +0000
commit657c5b6cadcef470c7ff9bffed3caab227026e6a (patch)
treebedff30a42b9b86ee1c5fb28932354147b479a00 /src
parent0b723f6a6cdc4171ee67ddd8d8c1bb812fb4cf06 (diff)
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
Diffstat (limited to 'src')
-rw-r--r--src/vty/command.c47
1 files changed, 46 insertions, 1 deletions
diff --git a/src/vty/command.c b/src/vty/command.c
index af936ee7..8ad2e975 100644
--- a/src/vty/command.c
+++ b/src/vty/command.c
@@ -123,12 +123,40 @@ char *argv_concat(const char **argv, int argc, int shift)
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_element(struct cmd_element *cmd, struct vty *vty)
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 @@ static int vty_dump_nodes(struct vty *vty)
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;