From 880e24a1e4cc78cb23ebdd72f2e5cea861cf8be2 Mon Sep 17 00:00:00 2001 From: Quentin Young Date: Fri, 22 Jul 2016 19:04:16 +0000 Subject: lib: Reorganize some matching stuff Introduce new node type, END_GN, and remove is_leaf flags. Reorganize command_match.c & remove internal functions from command_match.h. Start rewriting command.h in command_new.h with changes for new backend. Signed-off-by: Quentin Young --- lib/command_graph.c | 39 ++++++++++++++++++++++----------------- 1 file changed, 22 insertions(+), 17 deletions(-) (limited to 'lib/command_graph.c') diff --git a/lib/command_graph.c b/lib/command_graph.c index 4e99884dc8..b81e35ac9a 100644 --- a/lib/command_graph.c +++ b/lib/command_graph.c @@ -53,6 +53,9 @@ cmp_node(struct graph_node *first, struct graph_node *second) case SELECTOR_GN: case OPTION_GN: return 0; + // end nodes are always considered equal, since each node may only + // have one at a time + case END_GN: default: break; } @@ -66,27 +69,23 @@ new_node(enum graph_node_type type) struct graph_node *node = malloc(sizeof(struct graph_node)); node->type = type; node->children = vector_init(VECTOR_MIN_SIZE); - node->is_leaf = 0; node->is_root = 0; node->end = NULL; node->text = NULL; node->value = 0; node->min = 0; node->max = 0; - node->func = NULL; + node->element = NULL; return node; } -const char * -describe_node(struct graph_node *node) +char * +describe_node(struct graph_node *node, char* buffer, unsigned int bufsize) { - const char *desc = NULL; - char num[21]; - if (node == NULL) { - desc = "(null node)"; - return desc; + snprintf(buffer, bufsize, "(null node)"); + return buffer; } // print this node @@ -98,32 +97,38 @@ describe_node(struct graph_node *node) case IPV6_PREFIX_GN: case VARIABLE_GN: case RANGE_GN: - desc = node->text; + snprintf(buffer, bufsize, node->text); break; case NUMBER_GN: - sprintf(num, "%d", node->value); + snprintf(buffer, bufsize, "%d", node->value); break; case SELECTOR_GN: - desc = "<>"; + snprintf(buffer, bufsize, "<>"); break; case OPTION_GN: - desc = "[]"; + snprintf(buffer, bufsize, "[]"); break; case NUL_GN: - desc = "NUL"; + snprintf(buffer, bufsize, "NUL"); + break; + case END_GN: + snprintf(buffer, bufsize, "END"); break; default: - desc = "ERROR"; + snprintf(buffer, bufsize, "ERROR"); } - return desc; + + return buffer; } void walk_graph(struct graph_node *start, int level) { + char* desc = malloc(50); // print this node - fprintf(stderr, "%s[%d] ", describe_node(start), vector_active(start->children)); + fprintf(stderr, "%s[%d] ", describe_node(start, desc, 50), vector_active(start->children)); + free(desc); if (vector_active(start->children)) { if (vector_active(start->children) == 1) -- cgit v1.2.3