summaryrefslogtreecommitdiff
path: root/lib/command_graph.c
diff options
context:
space:
mode:
authorQuentin Young <qlyoung@cumulusnetworks.com>2016-07-21 21:38:03 +0000
committerQuentin Young <qlyoung@cumulusnetworks.com>2016-07-21 21:38:03 +0000
commit18be0e599d1ba666e59a3d027e973eb41798f46f (patch)
tree2d813d4a693a8af73e405aaec147e225c72b61ec /lib/command_graph.c
parent9d0662e009c0cf4532b88c9a1fb0f7c0dc174584 (diff)
lib: Mostly complete matcher
Input matching and completions works. Still some rough edges. Signed-off-by: Quentin Young <qlyoung@cumulusnetworks.com>
Diffstat (limited to 'lib/command_graph.c')
-rw-r--r--lib/command_graph.c66
1 files changed, 38 insertions, 28 deletions
diff --git a/lib/command_graph.c b/lib/command_graph.c
index 9b91eaf242..4e99884dc8 100644
--- a/lib/command_graph.c
+++ b/lib/command_graph.c
@@ -78,11 +78,19 @@ new_node(enum graph_node_type type)
return node;
}
-void
-walk_graph(struct graph_node *start, int level)
+const char *
+describe_node(struct graph_node *node)
{
+ const char *desc = NULL;
+ char num[21];
+
+ if (node == NULL) {
+ desc = "(null node)";
+ return desc;
+ }
+
// print this node
- switch (start->type) {
+ switch (node->type) {
case WORD_GN:
case IPV4_GN:
case IPV4_PREFIX_GN:
@@ -90,44 +98,46 @@ walk_graph(struct graph_node *start, int level)
case IPV6_PREFIX_GN:
case VARIABLE_GN:
case RANGE_GN:
- fprintf(stderr, "%s", start->text);
+ desc = node->text;
break;
case NUMBER_GN:
- fprintf(stderr, "%d", start->value);
+ sprintf(num, "%d", node->value);
break;
case SELECTOR_GN:
- fprintf(stderr, "<>");
+ desc = "<>";
break;
case OPTION_GN:
- fprintf(stderr, "[]");
+ desc = "[]";
break;
case NUL_GN:
- fprintf(stderr, "NUL");
+ desc = "NUL";
break;
default:
- fprintf(stderr, "ERROR");
+ desc = "ERROR";
}
- fprintf(stderr, "[%d] ", vector_active(start->children));
+ return desc;
+}
- if (vector_active(start->children))
- for (unsigned int i = 0; i < vector_active(start->children); i++)
- {
- struct graph_node *r = vector_slot(start->children, i);
- if (!r) {
- fprintf(stderr, "Child seems null?\n");
- break;
- }
- else {
- if (vector_active(start->children) > 1) {
- fprintf(stderr, "\n");
- for (int i = 0; i < level+1; i++)
- fprintf(stderr, " ");
- walk_graph(r, level+1);
- }
- else
- walk_graph(r, level);
+
+void
+walk_graph(struct graph_node *start, int level)
+{
+ // print this node
+ fprintf(stderr, "%s[%d] ", describe_node(start), vector_active(start->children));
+
+ if (vector_active(start->children)) {
+ if (vector_active(start->children) == 1)
+ walk_graph(vector_slot(start->children, 0), level);
+ else {
+ fprintf(stderr, "\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(stderr, " ");
+ walk_graph(r, level+1);
}
}
- if (level == 0)
+ }
+ else
fprintf(stderr, "\n");
}