]>
git.proxmox.com Git - mirror_frr.git/blob - lib/command_graph.c
3 * Provides a DFA data structure and associated functions for manipulating it.
4 * Used to match user command line input.
6 * @author Quentin Young <qlyoung@cumulusnetworks.com>
9 #include "command_graph.h"
14 add_node(struct graph_node
*parent
, struct graph_node
*child
)
16 struct graph_node
*p_child
;
18 for (unsigned int i
= 0; i
< vector_active(parent
->children
); i
++)
20 p_child
= vector_slot(parent
->children
, i
);
21 if (cmp_node(child
, p_child
))
24 vector_set(parent
->children
, child
);
29 cmp_node(struct graph_node
*first
, struct graph_node
*second
)
32 if (first
->type
!= second
->type
) return 0;
34 switch (first
->type
) {
35 case WORD_GN
: // words and variables are equal if their
36 case VARIABLE_GN
: // text value is equal
37 if (first
->text
&& second
->text
) {
38 if (strcmp(first
->text
, second
->text
)) return 0;
40 else if (first
->text
!= second
->text
) return 0;
42 case RANGE_GN
: // ranges are equal if their bounds are equal
43 if (first
->min
!= second
->min
|| first
->max
!= second
->max
)
46 case NUMBER_GN
: // numbers are equal if their values are equal
47 if (first
->value
!= second
->value
) return 0;
49 /* selectors and options should be equal if all paths are equal,
50 * but the graph isomorphism problem is not solvable in polynomial
51 * time so we consider selectors and options inequal in all cases
56 // end nodes are always considered equal, since each node may only
67 new_node(enum graph_node_type type
)
69 struct graph_node
*node
= malloc(sizeof(struct graph_node
));
71 node
->children
= vector_init(VECTOR_MIN_SIZE
);
84 describe_node(struct graph_node
*node
, char* buffer
, unsigned int bufsize
)
87 snprintf(buffer
, bufsize
, "(null node)");
100 snprintf(buffer
, bufsize
, node
->text
);
103 snprintf(buffer
, bufsize
, "%ld", node
->value
);
106 snprintf(buffer
, bufsize
, "<>");
109 snprintf(buffer
, bufsize
, "[]");
112 snprintf(buffer
, bufsize
, "NUL");
115 snprintf(buffer
, bufsize
, "END");
118 snprintf(buffer
, bufsize
, "ERROR");
126 walk_graph(struct graph_node
*start
, int level
)
128 char* desc
= malloc(50);
130 fprintf(stderr
, "%s[%d] ", describe_node(start
, desc
, 50), vector_active(start
->children
));
133 if (vector_active(start
->children
)) {
134 if (vector_active(start
->children
) == 1)
135 walk_graph(vector_slot(start
->children
, 0), level
);
137 fprintf(stderr
, "\n");
138 for (unsigned int i
= 0; i
< vector_active(start
->children
); i
++) {
139 struct graph_node
*r
= vector_slot(start
->children
, i
);
140 for (int j
= 0; j
< level
+1; j
++)
141 fprintf(stderr
, " ");
142 walk_graph(r
, level
+1);
147 fprintf(stderr
, "\n");