mirror_frr/lib/command_graph.h
Quentin Young de9d7e4f3c lib: Cleanup some memory issues in CLI
Various memory leaks have been fixed and the quagga
memory macros are in use. Also consolidated the argv
and matching code into one graph traversal.

Signed-off-by: Quentin Young <qlyoung@cumulusnetworks.com>
2016-07-29 15:54:03 +00:00

129 lines
3.1 KiB
C

#ifndef COMMAND_GRAPH_H
#define COMMAND_GRAPH_H
#include "command.h"
enum graph_node_type
{
IPV4_GN,
IPV4_PREFIX_GN,
IPV6_GN,
IPV6_PREFIX_GN,
WORD_GN,
RANGE_GN,
NUMBER_GN,
VARIABLE_GN,
SELECTOR_GN,
OPTION_GN,
NUL_GN,
START_GN,
END_GN
};
struct graph_node
{
enum graph_node_type type;// data type this node matches or holds
int is_start; // whether this node is a start node
vector children; // this node's children
struct graph_node * end; // pointer to end for SELECTOR_GN & OPTION_GN
char* text; // for WORD_GN and VARIABLE_GN
long value; // for NUMBER_GN
long min, max; // for RANGE_GN
/* cmd_element struct pointer, only valid for END_GN */
struct cmd_element *element;
/* used for passing arguments to command functions */
char *arg;
/* refcount for node parents */
int refs;
};
/*
* Adds a node as a child of another node.
* If the new parent has a child that is equal to the prospective child, as
* determined by cmp_node, then a pointer to the existing node is returned and
* the prospective child is not added. Otherwise the child node is returned.
*
* @param[in] parent node
* @param[in] child node
* @return pointer to child if it is added, pointer to existing child otherwise
*/
struct graph_node *
add_node(struct graph_node *, struct graph_node *);
/*
* Compares two nodes for parsing equivalence.
* Equivalence in this case means that a single user input token
* should be able to unambiguously match one of the two nodes.
* For example, two nodes which have all fields equal except their
* function pointers would be considered equal.
*
* @param[in] first node to compare
* @param[in] second node to compare
* @return 1 if equal, zero otherwise.
*/
int
cmp_node(struct graph_node *, struct graph_node *);
/*
* Create a new node.
* Initializes all fields to default values and sets the node type.
*
* @param[in] node type
* @return pointer to the newly allocated node
*/
struct graph_node *
new_node(enum graph_node_type);
/**
* Copies a node.
* The children vector is copied, but the pointers the vector
* holds point to the same data as the original vector.
* The element, if it exists, is copied.
*
* @param[in] pointer to node to copy
* @return pointer to copied node
*/
struct graph_node *
copy_node(struct graph_node *);
/**
* Frees the data associated with a graph_node.
* @param[out] pointer to graph_node to free
*/
void
free_node(struct graph_node *);
/**
* Recursively calls free_node on a graph node
* and all its children.
* @param[out] graph to free
*/
void
free_graph(struct graph_node *);
/**
* Walks a command DFA, printing structure to stdout.
* For debugging.
*
* @param[in] start node of graph to walk
* @param[in] graph depth for recursion, caller passes 0
*/
void
walk_graph(struct graph_node *, int);
/**
* Returns a string representation of the given node.
* @param[in] the node to describe
* @param[out] the buffer to write the description into
* @return pointer to description string
*/
char *
describe_node(struct graph_node *, char *, unsigned int);
void
dump_node (struct graph_node *);
#endif