diff options
Diffstat (limited to 'lib/cmdtree.c')
| -rw-r--r-- | lib/cmdtree.c | 55 |
1 files changed, 45 insertions, 10 deletions
diff --git a/lib/cmdtree.c b/lib/cmdtree.c index da647a7ae3..38632dca40 100644 --- a/lib/cmdtree.c +++ b/lib/cmdtree.c @@ -13,12 +13,11 @@ struct graph_node * add_node(struct graph_node *parent, struct graph_node *child) { - unsigned int index; struct graph_node *p_child; - for (index = 0; index < vector_active(parent->children); index++) + for (unsigned int i = 0; i < vector_active(parent->children); i++) { - p_child = vector_slot(parent->children, index); + p_child = vector_slot(parent->children, i); if (cmp_node(child, p_child)) return p_child; } @@ -29,7 +28,36 @@ add_node(struct graph_node *parent, struct graph_node *child) int cmp_node(struct graph_node *first, struct graph_node *second) { - return 0; + // compare types + if (first->type != second->type) return 0; + + switch (first->type) { + case WORD_GN: // words and variables are equal if their + case VARIABLE_GN: // text value is equal + if (first->text && second->text) { + if (strcmp(first->text, second->text)) return 0; + } + else if (first->text != second->text) return 0; + break; + case RANGE_GN: // ranges are equal if their bounds are equal + if (first->min != second->min || first->max != second->max) + return 0; + break; + case NUMBER_GN: // numbers are equal if their values are equal + if (first->value != second->value) return 0; + break; + /* selectors and options should be equal if all paths are equal, + * but the graph isomorphism problem is not solvable in polynomial + * time so we consider selectors and options inequal in all cases + */ + case SELECTOR_GN: + case OPTION_GN: + return 0; + default: + break; + } + + return 1; } struct graph_node * @@ -40,6 +68,11 @@ new_node(enum graph_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; return node; @@ -77,22 +110,24 @@ walk_graph(struct graph_node *start, int level) fprintf(stderr, "[%d] ", vector_active(start->children)); if (vector_active(start->children)) - for (unsigned int i = 0; i < vector_active(start->children); i++) { + 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 (start->type == OPTION_GN || start->type == SELECTOR_GN) { + if (vector_active(start->children) > 1) { fprintf(stderr, "\n"); for (int i = 0; i < level+1; i++) - fprintf(stderr, "\t"); + fprintf(stderr, " "); + walk_graph(r, level+1); } - walk_graph(r, level+1); + else + walk_graph(r, level); } } - else { + if (level == 0) fprintf(stderr, "\n"); - } } |
