]> git.proxmox.com Git - mirror_frr.git/blame - lib/command_graph.h
lib: Cleanup some memory issues in CLI
[mirror_frr.git] / lib / command_graph.h
CommitLineData
9d0662e0
QY
1#ifndef COMMAND_GRAPH_H
2#define COMMAND_GRAPH_H
3
eceb1066 4#include "command.h"
782d9789
QY
5
6enum graph_node_type
7{
782d9789
QY
8 IPV4_GN,
9 IPV4_PREFIX_GN,
10 IPV6_GN,
11 IPV6_PREFIX_GN,
76699ae7 12 WORD_GN,
782d9789
QY
13 RANGE_GN,
14 NUMBER_GN,
76699ae7 15 VARIABLE_GN,
782d9789
QY
16 SELECTOR_GN,
17 OPTION_GN,
880e24a1 18 NUL_GN,
5a5d576b 19 START_GN,
880e24a1 20 END_GN
782d9789
QY
21};
22
23struct graph_node
24{
eceb1066
QY
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
880e24a1 28 struct graph_node * end; // pointer to end for SELECTOR_GN & OPTION_GN
478bdaeb 29
5a5d576b
QY
30 char* text; // for WORD_GN and VARIABLE_GN
31 long value; // for NUMBER_GN
32 long min, max; // for RANGE_GN
eceb1066
QY
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;
de9d7e4f
QY
38
39 /* refcount for node parents */
40 int refs;
782d9789
QY
41};
42
43/*
5a5d576b
QY
44 * Adds a node as a child of another node.
45 * If the new parent has a child that is equal to the prospective child, as
46 * determined by cmp_node, then a pointer to the existing node is returned and
76699ae7 47 * the prospective child is not added. Otherwise the child node is returned.
340a2b4a 48 *
478bdaeb
QY
49 * @param[in] parent node
50 * @param[in] child node
5a5d576b 51 * @return pointer to child if it is added, pointer to existing child otherwise
782d9789 52 */
de9d7e4f 53struct graph_node *
782d9789
QY
54add_node(struct graph_node *, struct graph_node *);
55
56/*
340a2b4a
QY
57 * Compares two nodes for parsing equivalence.
58 * Equivalence in this case means that a single user input token
59 * should be able to unambiguously match one of the two nodes.
60 * For example, two nodes which have all fields equal except their
61 * function pointers would be considered equal.
62 *
63 * @param[in] first node to compare
64 * @param[in] second node to compare
65 * @return 1 if equal, zero otherwise.
782d9789 66 */
de9d7e4f 67int
340a2b4a 68cmp_node(struct graph_node *, struct graph_node *);
782d9789 69
340a2b4a
QY
70/*
71 * Create a new node.
72 * Initializes all fields to default values and sets the node type.
73 *
74 * @param[in] node type
75 * @return pointer to the newly allocated node
76 */
de9d7e4f 77struct graph_node *
340a2b4a 78new_node(enum graph_node_type);
4b0abf24 79
de9d7e4f
QY
80/**
81 * Copies a node.
82 * The children vector is copied, but the pointers the vector
83 * holds point to the same data as the original vector.
84 * The element, if it exists, is copied.
85 *
86 * @param[in] pointer to node to copy
87 * @return pointer to copied node
88 */
89struct graph_node *
90copy_node(struct graph_node *);
91
92/**
93 * Frees the data associated with a graph_node.
94 * @param[out] pointer to graph_node to free
95 */
96void
97free_node(struct graph_node *);
98
99/**
100 * Recursively calls free_node on a graph node
101 * and all its children.
102 * @param[out] graph to free
103 */
104void
105free_graph(struct graph_node *);
106
340a2b4a
QY
107/**
108 * Walks a command DFA, printing structure to stdout.
109 * For debugging.
110 *
111 * @param[in] start node of graph to walk
112 * @param[in] graph depth for recursion, caller passes 0
113 */
de9d7e4f 114void
4b0abf24 115walk_graph(struct graph_node *, int);
9d0662e0 116
18be0e59
QY
117/**
118 * Returns a string representation of the given node.
119 * @param[in] the node to describe
880e24a1 120 * @param[out] the buffer to write the description into
18be0e59
QY
121 * @return pointer to description string
122 */
de9d7e4f 123char *
880e24a1 124describe_node(struct graph_node *, char *, unsigned int);
eceb1066 125
eceb1066 126void
de9d7e4f 127dump_node (struct graph_node *);
9d0662e0 128#endif