1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
|
/*
* Command DFA module.
* Provides a DFA data structure and associated functions for manipulating it.
* Used to match user command line input.
*
* @author Quentin Young <qlyoung@cumulusnetworks.com>
*/
#include "command_graph.h"
#include <zebra.h>
#include "memory.h"
struct graph_node *
add_node(struct graph_node *parent, struct graph_node *child)
{
vector_set(parent->children, child);
child->refs++;
return child;
}
int
cmp_node(struct graph_node *first, struct graph_node *second)
{
// compare types
if (first->type != second->type) return 0;
switch (first->type) {
case WORD_GN:
case VARIABLE_GN:
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:
if (first->min != second->min || first->max != second->max)
return 0;
break;
case NUMBER_GN:
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;
/* end nodes are always considered equal, since each node may only
* have one at a time
*/
case START_GN:
case END_GN:
case NUL_GN:
default:
break;
}
return 1;
}
struct graph_node *
new_node(enum graph_node_type type)
{
struct graph_node *node =
XMALLOC(MTYPE_CMD_TOKENS, sizeof(struct graph_node));
node->type = type;
node->children = vector_init(VECTOR_MIN_SIZE);
node->end = NULL;
node->text = NULL;
node->element = NULL;
node->arg = NULL;
node->is_start = 0;
node->value = 0;
node->min = 0;
node->max = 0;
node->refs = 0;
return node;
}
void
free_node (struct graph_node *node)
{
if (!node) return;
if (node->children) vector_free (node->children);
if (node->element) free_cmd_element (node->element);
free (node->text);
free (node->arg);
free (node);
}
void
free_graph (struct graph_node *start)
{
if (start && start->children && vector_active(start->children) > 0) {
for (unsigned int i = 0; i < vector_active(start->children); i++) {
free_graph (vector_slot(start->children, i));
vector_unset(start->children, i);
}
}
if (--(start->refs) == 0)
free_node (start);
}
char *
describe_node(struct graph_node *node, char* buffer, unsigned int bufsize)
{
if (node == NULL) {
snprintf(buffer, bufsize, "(null node)");
return buffer;
}
// print this node
switch (node->type) {
case WORD_GN:
case IPV4_GN:
case IPV4_PREFIX_GN:
case IPV6_GN:
case IPV6_PREFIX_GN:
case VARIABLE_GN:
case RANGE_GN:
snprintf(buffer, bufsize, node->text);
break;
case NUMBER_GN:
snprintf(buffer, bufsize, "%ld", node->value);
break;
case SELECTOR_GN:
snprintf(buffer, bufsize, "<>");
break;
case OPTION_GN:
snprintf(buffer, bufsize, "[]");
break;
case NUL_GN:
snprintf(buffer, bufsize, "NUL");
break;
case END_GN:
snprintf(buffer, bufsize, "END");
break;
case START_GN:
snprintf(buffer, bufsize, "START");
break;
default:
snprintf(buffer, bufsize, "ERROR");
}
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, desc, 50), vector_active(start->children));
free(desc);
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);
}
}
}
else
fprintf(stderr, "\n");
}
void
dump_node (struct graph_node *node)
{
char buf[50];
describe_node(node, buf, 50);
fprintf(stderr, "%s[%d]\n", buf, node->type);
fprintf(stderr, "\t->text: %s\n", node->text);
fprintf(stderr, "\t->value: %ld\n", node->value);
fprintf(stderr, "\t->is_start: %d\n", node->is_start);
fprintf(stderr, "\t->element: %p\n", node->element);
fprintf(stderr, "\t->min: %lld\n->max: %lld\n", node->min, node->max);
fprintf(stderr, "\t->arg: %s\n", node->arg);
fprintf(stderr, "\t->refs: %d\n", node->refs);
fprintf(stderr, "\tnum children: %d\n", vector_active(node->children));
}
|