summaryrefslogtreecommitdiff
path: root/lib/grammar_sandbox.c
diff options
context:
space:
mode:
authorQuentin Young <qlyoung@cumulusnetworks.com>2016-09-07 23:02:39 +0000
committerQuentin Young <qlyoung@cumulusnetworks.com>2016-09-07 23:02:39 +0000
commit97f13f08655c924e782e2aa82933b4a7eec68de2 (patch)
tree361548b82b146cfb730458dd9c357a36252edd0f /lib/grammar_sandbox.c
parent1eb5e8dcd17d84959a46a2d837ae719fc8eb3516 (diff)
lib: Fix deduplication bug, reinstate graph print
Comparing the wrong nodes led to duplication during graph construction, fixed. Also update graph pretty print to use new graph struct. Signed-off-by: Quentin Young <qlyoung@cumulusnetworks.com>
Diffstat (limited to 'lib/grammar_sandbox.c')
-rw-r--r--lib/grammar_sandbox.c62
1 files changed, 29 insertions, 33 deletions
diff --git a/lib/grammar_sandbox.c b/lib/grammar_sandbox.c
index 6f323d6319..4a1a5fea5e 100644
--- a/lib/grammar_sandbox.c
+++ b/lib/grammar_sandbox.c
@@ -39,7 +39,7 @@
void
grammar_sandbox_init (void);
void
-pretty_print_graph (struct graph *start, int level);
+pretty_print_graph (struct graph_node *, int);
/*
* Start node for testing command graph.
@@ -139,11 +139,9 @@ DEFUN (grammar_test_match,
{
zlog_info ("Matched: %s", element->string);
struct listnode *ln;
- struct graph_node *gn;
- for (ALL_LIST_ELEMENTS_RO(argvv,ln,gn)) {
- struct cmd_token_t *token = gn->data;
+ struct cmd_token_t *token;
+ for (ALL_LIST_ELEMENTS_RO(argvv,ln,token))
zlog_info ("%s -- %s", token->text, token->arg);
- }
zlog_info ("func: %p", element->func);
@@ -220,7 +218,7 @@ DEFUN (grammar_test_show,
if (!nodegraph)
zlog_info("nodegraph uninitialized");
else
- pretty_print_graph (nodegraph, 0);
+ pretty_print_graph (vector_slot (nodegraph->nodes, 0), 0);
return CMD_SUCCESS;
}
@@ -241,42 +239,40 @@ void grammar_sandbox_init() {
install_element (ENABLE_NODE, &grammar_test_doc_cmd);
}
-/* recursive pretty-print for command graph */
+/**
+ * Pretty-prints a graph, assuming it is a tree.
+ *
+ * @param start the node to take as the root
+ * @param level indent level for recursive calls, always pass 0
+ */
void
-pretty_print_graph (struct graph *graph, int level)
+pretty_print_graph (struct graph_node *start, int level)
{
- /*
// print this node
- fprintf (stdout, "%s[%d] ", start->text, vector_active (start->children));
+ struct cmd_token_t *tok = start->data;
+ fprintf (stdout, "%s[%d] ", tok->text, tok->type);
- if (vector_active (start->children))
+ int numto = vector_active (start->to);
+ if (numto)
{
- if (vector_active (start->children) == 1)
+ if (numto > 1)
+ fprintf (stdout, "\n");
+ for (unsigned int i = 0; i < vector_active (start->to); i++)
{
- struct graph_node *child = vector_slot (start->children, 0);
- if (child == start)
+ struct graph_node *adj = vector_slot (start->to, i);
+ // if we're listing multiple children, indent!
+ if (numto > 1)
+ for (int j = 0; j < level+1; j++)
+ fprintf (stdout, " ");
+ // if this node is a vararg, just print *
+ if (adj == start)
fprintf (stdout, "*");
else
- pretty_print_graph (vector_slot (start->children, 0), level);
- }
- else
- {
- fprintf(stdout, "\n");
- for (unsigned int i = 0; i < vector_active (start->children); i++)
- {
- struct graph_node *r = vector_slot (start->children, i);
- for (int j = 0; j < level+1; j++)
- fprintf (stdout, " ");
- if (r == start)
- fprintf (stdout, "*");
- else
- pretty_print_graph (r, level+1);
- }
+ pretty_print_graph (adj, numto > 1 ? level+1 : level);
}
}
else
fprintf(stdout, "\n");
- */
}
/** stuff that should go in command.c + command.h */
@@ -305,9 +301,9 @@ struct cmd_token_t *
copy_cmd_token (struct cmd_token_t *token)
{
struct cmd_token_t *copy = new_cmd_token (token->type, NULL, NULL);
- copy->text = XSTRDUP (MTYPE_CMD_TOKENS, token->text);
- copy->desc = XSTRDUP (MTYPE_CMD_TOKENS, token->desc);
- copy->arg = copy->arg ? XSTRDUP (MTYPE_CMD_TOKENS, token->arg) : NULL;
+ copy->text = token->text ? XSTRDUP (MTYPE_CMD_TOKENS, token->text) : NULL;
+ copy->desc = token->desc ? XSTRDUP (MTYPE_CMD_TOKENS, token->desc) : NULL;
+ copy->arg = token->arg ? XSTRDUP (MTYPE_CMD_TOKENS, token->arg) : NULL;
return copy;
}