]> git.proxmox.com Git - mirror_frr.git/blob - lib/command_graph.h
lib: Cleanup parser memory management
[mirror_frr.git] / lib / command_graph.h
1 #ifndef COMMAND_GRAPH_H
2 #define COMMAND_GRAPH_H
3
4 #include "command.h"
5
6 enum graph_node_type
7 {
8 WORD_GN,
9 IPV4_GN,
10 IPV4_PREFIX_GN,
11 IPV6_GN,
12 IPV6_PREFIX_GN,
13 VARIABLE_GN,
14 RANGE_GN,
15 NUMBER_GN,
16 SELECTOR_GN,
17 OPTION_GN,
18 NUL_GN,
19 START_GN,
20 END_GN
21 };
22
23 struct graph_node
24 {
25 enum graph_node_type type;// data type this node matches or holds
26 int is_start; // whether this node is a start node
27 vector children; // this node's children
28 struct graph_node * end; // pointer to end for SELECTOR_GN & OPTION_GN
29
30 char* text; // for WORD_GN and VARIABLE_GN
31 long value; // for NUMBER_GN
32 long min, max; // for RANGE_GN
33
34 /* cmd_element struct pointer, only valid for END_GN */
35 struct cmd_element *element;
36 /* used for passing arguments to command functions */
37 char *arg;
38 };
39
40 /*
41 * Adds a node as a child of another node.
42 * If the new parent has a child that is equal to the prospective child, as
43 * determined by cmp_node, then a pointer to the existing node is returned and
44 * the prospective child is not added. Otherwise the return value is NULL.
45 *
46 * @param[in] parent node
47 * @param[in] child node
48 * @return pointer to child if it is added, pointer to existing child otherwise
49 */
50 extern struct graph_node *
51 add_node(struct graph_node *, struct graph_node *);
52
53 /*
54 * Compares two nodes for parsing equivalence.
55 * Equivalence in this case means that a single user input token
56 * should be able to unambiguously match one of the two nodes.
57 * For example, two nodes which have all fields equal except their
58 * function pointers would be considered equal.
59 *
60 * @param[in] first node to compare
61 * @param[in] second node to compare
62 * @return 1 if equal, zero otherwise.
63 */
64 extern int
65 cmp_node(struct graph_node *, struct graph_node *);
66
67 /*
68 * Create a new node.
69 * Initializes all fields to default values and sets the node type.
70 *
71 * @param[in] node type
72 * @return pointer to the newly allocated node
73 */
74 extern struct graph_node *
75 new_node(enum graph_node_type);
76
77 /**
78 * Walks a command DFA, printing structure to stdout.
79 * For debugging.
80 *
81 * @param[in] start node of graph to walk
82 * @param[in] graph depth for recursion, caller passes 0
83 */
84 extern void
85 walk_graph(struct graph_node *, int);
86
87 /**
88 * Returns a string representation of the given node.
89 * @param[in] the node to describe
90 * @param[out] the buffer to write the description into
91 * @return pointer to description string
92 */
93 extern char *
94 describe_node(struct graph_node *, char *, unsigned int);
95
96 /**
97 * Frees the data associated with a graph_node.
98 * @param[out] pointer to graph_node to free
99 */
100 void
101 free_node(struct graph_node *);
102 #endif