]> git.proxmox.com Git - mirror_frr.git/blob - lib/command_graph.h
lib: Mostly complete matcher
[mirror_frr.git] / lib / command_graph.h
1 #ifndef COMMAND_GRAPH_H
2 #define COMMAND_GRAPH_H
3
4 #include "vty.h"
5 #include "vector.h"
6
7 enum graph_node_type
8 {
9 WORD_GN,
10 IPV4_GN,
11 IPV4_PREFIX_GN,
12 IPV6_GN,
13 IPV6_PREFIX_GN,
14 VARIABLE_GN,
15 RANGE_GN,
16 NUMBER_GN,
17 SELECTOR_GN,
18 OPTION_GN,
19 NUL_GN
20 };
21
22 struct graph_node
23 {
24 enum graph_node_type type;
25 vector children;
26 int is_root; // true if first token in command
27 int is_leaf; // true if last token in command
28 struct graph_node * end; // pointer to end for selector & option
29
30 int (*func)(struct vty *, int, const char *[]);
31
32 /* various data fields for nodes */
33 char* text; // for words and variables
34 int value; // for numbers
35 int min, max; // for ranges
36 };
37
38 /*
39 * Adds a child to a node.
40 * If the node already has the exact same child, nothing is done. This is
41 * decided with cmp_node.
42 *
43 * @param[in] parent node
44 * @param[in] child node
45 * @return the new child, or the existing child if the parent already has the
46 * new child
47 */
48 extern struct graph_node *
49 add_node(struct graph_node *, struct graph_node *);
50
51 /*
52 * Compares two nodes for parsing equivalence.
53 * Equivalence in this case means that a single user input token
54 * should be able to unambiguously match one of the two nodes.
55 * For example, two nodes which have all fields equal except their
56 * function pointers would be considered equal.
57 *
58 * @param[in] first node to compare
59 * @param[in] second node to compare
60 * @return 1 if equal, zero otherwise.
61 */
62 extern int
63 cmp_node(struct graph_node *, struct graph_node *);
64
65 /*
66 * Create a new node.
67 * Initializes all fields to default values and sets the node type.
68 *
69 * @param[in] node type
70 * @return pointer to the newly allocated node
71 */
72 extern struct graph_node *
73 new_node(enum graph_node_type);
74
75 /**
76 * Walks a command DFA, printing structure to stdout.
77 * For debugging.
78 *
79 * @param[in] start node of graph to walk
80 * @param[in] graph depth for recursion, caller passes 0
81 */
82 extern void
83 walk_graph(struct graph_node *, int);
84
85 /**
86 * Returns a string representation of the given node.
87 * @param[in] the node to describe
88 * @return pointer to description string
89 */
90 extern const char *
91 describe_node(struct graph_node *);
92 #endif