summaryrefslogtreecommitdiffstats
path: root/lib/cmdtree.h
blob: 3d2366b00871eb56216a2f2cfb9ea89e12ad5507 (plain)
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
#include "vty.h"
#include "vector.h"

enum graph_node_type
{
  WORD_GN,
  IPV4_GN,
  IPV4_PREFIX_GN,
  IPV6_GN,
  IPV6_PREFIX_GN,
  VARIABLE_GN,
  RANGE_GN,
  NUMBER_GN,
  SELECTOR_GN,
  OPTION_GN,
  NUL_GN
};

struct graph_node
{
  enum graph_node_type type;
  vector children;
  int is_root;              // true if first token in command
  int is_leaf;              // true if last token in command
  struct graph_node * end;  // pointer to end for selector & option

  int (*func)(struct vty *, int, const char *[]);

  /* various data fields for nodes */
  char* text;       // for words and variables
  int value;        // for numbers
  int min, max;     // for ranges
};

/*
 * Adds a child to a node.
 * If the node already has the exact same child, nothing is done. This is
 * decided with cmp_node.
 *
 * @param[in] parent node
 * @param[in] child node
 * @return the new child, or the existing child if the parent already has the
 *         new child
 */
extern 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.
 */
extern 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
 */
extern struct graph_node *
new_node(enum graph_node_type);

/**
 * 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
 */
extern void
walk_graph(struct graph_node *, int);