]>
git.proxmox.com Git - mirror_frr.git/blob - lib/command_graph.c
0a866de330bf72f37ac4ac30a526c7f011bead16
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 vector_set(parent
->children
, child
);
22 new_node(enum graph_node_type type
)
24 struct graph_node
*node
=
25 XMALLOC(MTYPE_CMD_TOKENS
, sizeof(struct graph_node
));
28 node
->children
= vector_init(VECTOR_MIN_SIZE
);
43 free_node (struct graph_node
*node
)
46 if (node
->children
) vector_free (node
->children
);
47 if (node
->element
) free_cmd_element (node
->element
);
54 free_graph (struct graph_node
*start
)
56 if (start
&& start
->children
&& vector_active(start
->children
) > 0) {
57 for (unsigned int i
= 0; i
< vector_active(start
->children
); i
++) {
58 free_graph (vector_slot(start
->children
, i
));
59 vector_unset(start
->children
, i
);
63 if (--(start
->refs
) == 0)
68 describe_node(struct graph_node
*node
, char* buffer
, unsigned int bufsize
)
71 snprintf(buffer
, bufsize
, "(null node)");
84 snprintf(buffer
, bufsize
, node
->text
);
87 snprintf(buffer
, bufsize
, "%lld", node
->value
);
90 snprintf(buffer
, bufsize
, "<>");
93 snprintf(buffer
, bufsize
, "[]");
96 snprintf(buffer
, bufsize
, "NUL");
99 snprintf(buffer
, bufsize
, "END");
102 snprintf(buffer
, bufsize
, "START");
105 snprintf(buffer
, bufsize
, "ERROR");
112 walk_graph(struct graph_node
*start
, int level
)
114 char* desc
= malloc(50);
116 fprintf(stderr
, "%s[%d] ", describe_node(start
, desc
, 50), vector_active(start
->children
));
119 if (vector_active(start
->children
)) {
120 if (vector_active(start
->children
) == 1)
121 walk_graph(vector_slot(start
->children
, 0), level
);
123 fprintf(stderr
, "\n");
124 for (unsigned int i
= 0; i
< vector_active(start
->children
); i
++) {
125 struct graph_node
*r
= vector_slot(start
->children
, i
);
126 for (int j
= 0; j
< level
+1; j
++)
127 fprintf(stderr
, " ");
128 walk_graph(r
, level
+1);
133 fprintf(stderr
, "\n");
137 dump_node (struct graph_node
*node
)
140 describe_node(node
, buf
, 50);
141 fprintf(stderr
, "%s[%d]\n", buf
, node
->type
);
142 fprintf(stderr
, "\t->text: %s\n", node
->text
);
143 fprintf(stderr
, "\t->value: %lld\n", node
->value
);
144 fprintf(stderr
, "\t->is_start: %d\n", node
->is_start
);
145 fprintf(stderr
, "\t->element: %p\n", node
->element
);
146 fprintf(stderr
, "\t->min: %lld\n->max: %lld\n", node
->min
, node
->max
);
147 fprintf(stderr
, "\t->arg: %s\n", node
->arg
);
148 fprintf(stderr
, "\t->refs: %d\n", node
->refs
);
149 fprintf(stderr
, "\tnum children: %d\n", vector_active(node
->children
));