]> git.proxmox.com Git - mirror_frr.git/blob - lib/command_graph.h
Merge pull request #12780 from opensourcerouting/spdx-license-id
[mirror_frr.git] / lib / command_graph.h
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3 * CLI graph handling
4 *
5 * --
6 * Copyright (C) 2016 Cumulus Networks, Inc.
7 * Copyright (C) 1997, 98, 99 Kunihiro Ishiguro
8 * Copyright (C) 2013 by Open Source Routing.
9 * Copyright (C) 2013 by Internet Systems Consortium, Inc. ("ISC")
10 */
11
12 #ifndef _FRR_COMMAND_GRAPH_H
13 #define _FRR_COMMAND_GRAPH_H
14
15 #include <stdbool.h>
16 #include <stdint.h>
17
18 #include "memory.h"
19 #include "vector.h"
20 #include "graph.h"
21 #include "xref.h"
22
23 #ifdef __cplusplus
24 extern "C" {
25 #endif
26
27 DECLARE_MTYPE(CMD_ARG);
28
29 struct vty;
30
31 /**
32 * Types for tokens.
33 *
34 * The type determines what kind of data the token can match (in the
35 * matching use case) or hold (in the argv use case).
36 */
37 /* clang-format off */
38 enum cmd_token_type {
39 WORD_TKN, // words
40 VARIABLE_TKN, // almost anything
41 RANGE_TKN, // integer range
42 IPV4_TKN, // IPV4 addresses
43 IPV4_PREFIX_TKN, // IPV4 network prefixes
44 IPV6_TKN, // IPV6 prefixes
45 IPV6_PREFIX_TKN, // IPV6 network prefixes
46 MAC_TKN, // Ethernet address
47 MAC_PREFIX_TKN, // Ethernet address w/ CIDR mask
48
49 /* plumbing types */
50 FORK_TKN, // marks subgraph beginning
51 JOIN_TKN, // marks subgraph end
52 START_TKN, // first token in line
53 END_TKN, // last token in line
54 NEG_ONLY_TKN, // filter token, match if "no ..." command
55
56 SPECIAL_TKN = FORK_TKN,
57 };
58 /* clang-format on */
59
60 #define IS_VARYING_TOKEN(x) ((x) >= VARIABLE_TKN && (x) < FORK_TKN)
61
62 /* Command attributes */
63 enum {
64 CMD_ATTR_YANG = (1 << 0),
65 CMD_ATTR_HIDDEN = (1 << 1),
66 CMD_ATTR_DEPRECATED = (1 << 2),
67 CMD_ATTR_NOSH = (1 << 3),
68 };
69
70 enum varname_src {
71 VARNAME_NONE = 0,
72 VARNAME_AUTO,
73 VARNAME_VAR,
74 VARNAME_TEXT,
75 VARNAME_EXPLICIT,
76 };
77
78 /* Command token struct. */
79 struct cmd_token {
80 enum cmd_token_type type; // token type
81 uint8_t attr; // token attributes
82 bool allowrepeat; // matcher allowed to match token repetitively?
83 uint8_t varname_src;
84 uint32_t refcnt;
85
86 char *text; // token text
87 char *desc; // token description
88 long long min, max; // for ranges
89 char *arg; // user input that matches this token
90 char *varname;
91
92 struct graph_node *forkjoin; // paired FORK/JOIN for JOIN/FORK
93 };
94
95 /* Structure of command element. */
96 struct cmd_element {
97 const char *string; /* Command specification by string. */
98 const char *doc; /* Documentation of this command. */
99 int daemon; /* Daemon to which this command belong. */
100 uint32_t attr; /* Command attributes */
101
102 /* handler function for command */
103 int (*func)(const struct cmd_element *, struct vty *, int,
104 struct cmd_token *[]);
105
106 const char *name; /* symbol name for debugging */
107 struct xref xref;
108 };
109
110 /* text for <cr> command */
111 #define CMD_CR_TEXT "<cr>"
112
113 /* memory management for cmd_token */
114 extern struct cmd_token *cmd_token_new(enum cmd_token_type, uint8_t attr,
115 const char *text, const char *desc);
116 extern struct cmd_token *cmd_token_dup(struct cmd_token *);
117 extern void cmd_token_del(struct cmd_token *);
118 extern void cmd_token_varname_set(struct cmd_token *token, const char *varname);
119 extern void cmd_token_varname_seqappend(struct graph_node *n);
120 extern void cmd_token_varname_join(struct graph_node *n, const char *varname);
121
122 extern void cmd_graph_parse(struct graph *graph, const struct cmd_element *cmd);
123 extern void cmd_graph_names(struct graph *graph);
124 extern void cmd_graph_merge(struct graph *old, struct graph *n,
125 int direction);
126 /*
127 * Print callback for DOT dumping.
128 *
129 * See graph.h for more details.
130 */
131 extern void cmd_graph_node_print_cb(struct graph_node *gn, struct buffer *buf);
132 /*
133 * Dump command graph to DOT.
134 *
135 * cmdgraph
136 * A command graph to dump
137 *
138 * Returns:
139 * String allocated with MTYPE_TMP representing this graph
140 */
141 char *cmd_graph_dump_dot(struct graph *cmdgraph);
142
143 #ifdef __cplusplus
144 }
145 #endif
146
147 #endif /* _FRR_COMMAND_GRAPH_H */