]> git.proxmox.com Git - mirror_frr.git/blame - lib/command.h
lib: parser: rewrite token deduplication
[mirror_frr.git] / lib / command.h
CommitLineData
718e3744 1/*
2 * Zebra configuration command interface routine
3 * Copyright (C) 1997, 98 Kunihiro Ishiguro
4 *
5 * This file is part of GNU Zebra.
6 *
7 * GNU Zebra is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published
9 * by the Free Software Foundation; either version 2, or (at your
10 * option) any later version.
d0bfb22c 11 *
718e3744 12 * GNU Zebra is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with GNU Zebra; see the file COPYING. If not, write to the
19 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
20 * Boston, MA 02111-1307, USA.
21 */
22
23#ifndef _ZEBRA_COMMAND_H
24#define _ZEBRA_COMMAND_H
25
26#include "vector.h"
27#include "vty.h"
9c42a6eb 28#include "lib/route_types.h"
d0bfb22c 29#include "graph.h"
4a1ab8e4 30#include "memory.h"
274f29b2 31#include "hash.h"
4a1ab8e4
DL
32
33DECLARE_MTYPE(HOST)
83364d20 34DECLARE_MTYPE(CMD_ARG)
4a1ab8e4
DL
35
36/* for test-commands.c */
37DECLARE_MTYPE(STRVEC)
718e3744 38
39/* Host configuration variable */
40struct host
41{
42 /* Host name of this router. */
43 char *name;
44
45 /* Password for vty interface. */
46 char *password;
47 char *password_encrypt;
48
49 /* Enable password */
50 char *enable;
51 char *enable_encrypt;
52
53 /* System wide terminal lines. */
54 int lines;
55
56 /* Log filename. */
57 char *logfile;
58
718e3744 59 /* config file name of this host */
60 char *config;
87f44e2f 61 int noconfig;
718e3744 62
63 /* Flags for services */
64 int advanced;
65 int encrypt;
66
67 /* Banner configuration. */
8c328f11 68 const char *motd;
3b0c5d9a 69 char *motdfile;
718e3744 70};
71
72/* There are some command levels which called from command node. */
d0bfb22c 73enum node_type
718e3744 74{
d0bfb22c 75 AUTH_NODE, /* Authentication mode of vty interface. */
d0bfb22c
QY
76 VIEW_NODE, /* View node. Default mode of vty interface. */
77 AUTH_ENABLE_NODE, /* Authentication mode for change enable. */
78 ENABLE_NODE, /* Enable node. */
79 CONFIG_NODE, /* Config node. Default mode of config file. */
80 SERVICE_NODE, /* Service node. */
81 DEBUG_NODE, /* Debug node. */
19dc275e 82 VRF_DEBUG_NODE, /* Vrf Debug node. */
65efcfce 83 DEBUG_VNC_NODE, /* Debug VNC node. */
d0bfb22c
QY
84 AAA_NODE, /* AAA node. */
85 KEYCHAIN_NODE, /* Key-chain node. */
86 KEYCHAIN_KEY_NODE, /* Key-chain key node. */
844ec28c 87 NS_NODE, /* Logical-Router node. */
d0bfb22c
QY
88 VRF_NODE, /* VRF mode node. */
89 INTERFACE_NODE, /* Interface mode node. */
90 ZEBRA_NODE, /* zebra connection node. */
91 TABLE_NODE, /* rtm_table selection node. */
92 RIP_NODE, /* RIP protocol mode node. */
93 RIPNG_NODE, /* RIPng protocol mode node. */
94 BGP_NODE, /* BGP protocol mode which includes BGP4+ */
95 BGP_VPNV4_NODE, /* BGP MPLS-VPN PE exchange. */
96 BGP_VPNV6_NODE, /* BGP MPLS-VPN PE exchange. */
97 BGP_IPV4_NODE, /* BGP IPv4 unicast address family. */
98 BGP_IPV4M_NODE, /* BGP IPv4 multicast address family. */
99 BGP_IPV6_NODE, /* BGP IPv6 address family */
100 BGP_IPV6M_NODE, /* BGP IPv6 multicast address family. */
101 BGP_ENCAP_NODE, /* BGP ENCAP SAFI */
102 BGP_ENCAPV6_NODE, /* BGP ENCAP SAFI */
5ff06872 103 BGP_VRF_POLICY_NODE, /* BGP VRF policy */
65efcfce
LB
104 BGP_VNC_DEFAULTS_NODE, /* BGP VNC nve defaults */
105 BGP_VNC_NVE_GROUP_NODE, /* BGP VNC nve group */
106 BGP_VNC_L2_GROUP_NODE, /* BGP VNC L2 group */
107 RFP_DEFAULTS_NODE, /* RFP defaults node */
d0bfb22c
QY
108 OSPF_NODE, /* OSPF protocol mode */
109 OSPF6_NODE, /* OSPF protocol for IPv6 mode */
eac6e3f0
RW
110 LDP_NODE, /* LDP protocol mode */
111 LDP_IPV4_NODE, /* LDP IPv4 address family */
112 LDP_IPV6_NODE, /* LDP IPv6 address family */
113 LDP_IPV4_IFACE_NODE, /* LDP IPv4 Interface */
114 LDP_IPV6_IFACE_NODE, /* LDP IPv6 Interface */
115 LDP_L2VPN_NODE, /* LDP L2VPN node */
116 LDP_PSEUDOWIRE_NODE, /* LDP Pseudowire node */
d0bfb22c
QY
117 ISIS_NODE, /* ISIS protocol mode */
118 PIM_NODE, /* PIM protocol mode */
119 MASC_NODE, /* MASC for multicast. */
120 IRDP_NODE, /* ICMP Router Discovery Protocol mode. */
121 IP_NODE, /* Static ip route node. */
122 ACCESS_NODE, /* Access list node. */
123 PREFIX_NODE, /* Prefix list node. */
124 ACCESS_IPV6_NODE, /* Access list node. */
125 PREFIX_IPV6_NODE, /* Prefix list node. */
126 AS_LIST_NODE, /* AS list node. */
127 COMMUNITY_LIST_NODE, /* Community list node. */
128 RMAP_NODE, /* Route map node. */
129 SMUX_NODE, /* SNMP configuration node. */
130 DUMP_NODE, /* Packet dump node. */
131 FORWARDING_NODE, /* IP forwarding node. */
7514fb77 132 PROTOCOL_NODE, /* protocol filtering node */
7758e3f3 133 MPLS_NODE, /* MPLS config node */
d0bfb22c 134 VTY_NODE, /* Vty node. */
844ec28c 135 LINK_PARAMS_NODE, /* Link-parameters node */
718e3744 136};
137
138/* Node which has some commands and prompt string and configuration
139 function pointer . */
d0bfb22c 140struct cmd_node
718e3744 141{
142 /* Node index. */
d0bfb22c 143 enum node_type node;
718e3744 144
145 /* Prompt character at vty interface. */
d0bfb22c 146 const char *prompt;
718e3744 147
148 /* Is this node's configuration goes to vtysh ? */
149 int vtysh;
d0bfb22c 150
718e3744 151 /* Node's configuration write function */
152 int (*func) (struct vty *);
153
d0bfb22c
QY
154 /* Node's command graph */
155 struct graph *cmdgraph;
156
718e3744 157 /* Vector of this node's command list. */
d0bfb22c 158 vector cmd_vector;
274f29b2
PJ
159
160 /* Hashed index of command node list, for de-dupping primarily */
161 struct hash *cmd_hash;
718e3744 162};
163
d0bfb22c
QY
164/**
165 * Types for tokens.
166 *
167 * The type determines what kind of data the token can match (in the
168 * matching use case) or hold (in the argv use case).
169 */
170enum cmd_token_type
406d6716 171{
d0bfb22c 172 WORD_TKN, // words
d0bfb22c
QY
173 VARIABLE_TKN, // almost anything
174 RANGE_TKN, // integer range
175 IPV4_TKN, // IPV4 addresses
176 IPV4_PREFIX_TKN, // IPV4 network prefixes
177 IPV6_TKN, // IPV6 prefixes
178 IPV6_PREFIX_TKN, // IPV6 network prefixes
179
180 /* plumbing types */
0bf5b1cb
DL
181 FORK_TKN, // marks subgraph beginning
182 JOIN_TKN, // marks subgraph end
d0bfb22c
QY
183 START_TKN, // first token in line
184 END_TKN, // last token in line
0bf5b1cb
DL
185
186 SPECIAL_TKN = FORK_TKN,
406d6716 187};
188
ce882f81
QY
189/* Command attributes */
190enum
191{
192 CMD_ATTR_NORMAL,
193 CMD_ATTR_DEPRECATED,
194 CMD_ATTR_HIDDEN,
195};
196
197/* Comamand token struct. */
d0bfb22c 198struct cmd_token
718e3744 199{
ebacb4ed 200 enum cmd_token_type type; // token type
ce882f81 201 u_char attr; // token attributes
4d94b292 202 bool allowrepeat; // matcher allowed to match token repetively?
de8f7a39 203 uint32_t refcnt;
4d94b292 204
d0bfb22c
QY
205 char *text; // token text
206 char *desc; // token description
d0bfb22c 207 long long min, max; // for ranges
d0bfb22c 208 char *arg; // user input that matches this token
ab037159
DL
209
210 struct graph_node *forkjoin; // paired FORK/JOIN for JOIN/FORK
cd40b329
CF
211};
212
d0bfb22c
QY
213/* Structure of command element. */
214struct cmd_element
718e3744 215{
d0bfb22c
QY
216 const char *string; /* Command specification by string. */
217 const char *doc; /* Documentation of this command. */
218 int daemon; /* Daemon to which this command belong. */
219 u_char attr; /* Command attributes */
cd40b329 220
d0bfb22c 221 /* handler function for command */
17aca20b 222 int (*func) (const struct cmd_element *, struct vty *, int, struct cmd_token *[]);
718e3744 223};
224
225/* Return value of the commands. */
226#define CMD_SUCCESS 0
227#define CMD_WARNING 1
228#define CMD_ERR_NO_MATCH 2
229#define CMD_ERR_AMBIGUOUS 3
230#define CMD_ERR_INCOMPLETE 4
231#define CMD_ERR_EXEED_ARGC_MAX 5
232#define CMD_ERR_NOTHING_TODO 6
233#define CMD_COMPLETE_FULL_MATCH 7
234#define CMD_COMPLETE_MATCH 8
235#define CMD_COMPLETE_LIST_MATCH 9
236#define CMD_SUCCESS_DAEMON 10
1db63918 237#define CMD_ERR_NO_FILE 11
95c4aff2 238#define CMD_SUSPEND 12
718e3744 239
240/* Argc max counts. */
241#define CMD_ARGC_MAX 25
242
243/* Turn off these macros when uisng cpp with extract.pl */
d0bfb22c 244#ifndef VTYSH_EXTRACT_PL
718e3744 245
406d6716 246/* helper defines for end-user DEFUN* macros */
247#define DEFUN_CMD_ELEMENT(funcname, cmdname, cmdstr, helpstr, attrs, dnum) \
555a5bfe 248 static struct cmd_element cmdname = \
718e3744 249 { \
9035efaa 250 .string = cmdstr, \
251 .func = funcname, \
406d6716 252 .doc = helpstr, \
253 .attr = attrs, \
254 .daemon = dnum, \
255 };
256
257#define DEFUN_CMD_FUNC_DECL(funcname) \
17aca20b 258 static int funcname (const struct cmd_element *, struct vty *, int, struct cmd_token *[]);
406d6716 259
260#define DEFUN_CMD_FUNC_TEXT(funcname) \
274a4a44 261 static int funcname \
17aca20b 262 (const struct cmd_element *self __attribute__ ((unused)), \
6cf0cf0c
PJ
263 struct vty *vty __attribute__ ((unused)), \
264 int argc __attribute__ ((unused)), \
d0bfb22c 265 struct cmd_token *argv[] __attribute__ ((unused)) )
406d6716 266
406d6716 267#define DEFUN(funcname, cmdname, cmdstr, helpstr) \
268 DEFUN_CMD_FUNC_DECL(funcname) \
269 DEFUN_CMD_ELEMENT(funcname, cmdname, cmdstr, helpstr, 0, 0) \
270 DEFUN_CMD_FUNC_TEXT(funcname)
271
272#define DEFUN_ATTR(funcname, cmdname, cmdstr, helpstr, attr) \
273 DEFUN_CMD_FUNC_DECL(funcname) \
274 DEFUN_CMD_ELEMENT(funcname, cmdname, cmdstr, helpstr, attr, 0) \
275 DEFUN_CMD_FUNC_TEXT(funcname)
276
277#define DEFUN_HIDDEN(funcname, cmdname, cmdstr, helpstr) \
278 DEFUN_ATTR (funcname, cmdname, cmdstr, helpstr, CMD_ATTR_HIDDEN)
279
280#define DEFUN_DEPRECATED(funcname, cmdname, cmdstr, helpstr) \
2557aed1 281 DEFUN_ATTR (funcname, cmdname, cmdstr, helpstr, CMD_ATTR_DEPRECATED) \
718e3744 282
283/* DEFUN_NOSH for commands that vtysh should ignore */
284#define DEFUN_NOSH(funcname, cmdname, cmdstr, helpstr) \
285 DEFUN(funcname, cmdname, cmdstr, helpstr)
286
287/* DEFSH for vtysh. */
288#define DEFSH(daemon, cmdname, cmdstr, helpstr) \
406d6716 289 DEFUN_CMD_ELEMENT(NULL, cmdname, cmdstr, helpstr, 0, daemon) \
718e3744 290
abaaab4e
DW
291#define DEFSH_HIDDEN(daemon, cmdname, cmdstr, helpstr) \
292 DEFUN_CMD_ELEMENT(NULL, cmdname, cmdstr, helpstr, CMD_ATTR_HIDDEN, daemon) \
293
718e3744 294/* DEFUN + DEFSH */
295#define DEFUNSH(daemon, funcname, cmdname, cmdstr, helpstr) \
406d6716 296 DEFUN_CMD_FUNC_DECL(funcname) \
297 DEFUN_CMD_ELEMENT(funcname, cmdname, cmdstr, helpstr, 0, daemon) \
298 DEFUN_CMD_FUNC_TEXT(funcname)
718e3744 299
274a4a44 300/* DEFUN + DEFSH with attributes */
301#define DEFUNSH_ATTR(daemon, funcname, cmdname, cmdstr, helpstr, attr) \
302 DEFUN_CMD_FUNC_DECL(funcname) \
303 DEFUN_CMD_ELEMENT(funcname, cmdname, cmdstr, helpstr, attr, daemon) \
304 DEFUN_CMD_FUNC_TEXT(funcname)
305
306#define DEFUNSH_HIDDEN(daemon, funcname, cmdname, cmdstr, helpstr) \
307 DEFUNSH_ATTR (daemon, funcname, cmdname, cmdstr, helpstr, CMD_ATTR_HIDDEN)
308
309#define DEFUNSH_DEPRECATED(daemon, funcname, cmdname, cmdstr, helpstr) \
310 DEFUNSH_ATTR (daemon, funcname, cmdname, cmdstr, helpstr, CMD_ATTR_DEPRECATED)
311
718e3744 312/* ALIAS macro which define existing command's alias. */
313#define ALIAS(funcname, cmdname, cmdstr, helpstr) \
406d6716 314 DEFUN_CMD_ELEMENT(funcname, cmdname, cmdstr, helpstr, 0, 0)
315
316#define ALIAS_ATTR(funcname, cmdname, cmdstr, helpstr, attr) \
274a4a44 317 DEFUN_CMD_ELEMENT(funcname, cmdname, cmdstr, helpstr, attr, 0)
406d6716 318
319#define ALIAS_HIDDEN(funcname, cmdname, cmdstr, helpstr) \
320 DEFUN_CMD_ELEMENT(funcname, cmdname, cmdstr, helpstr, CMD_ATTR_HIDDEN, 0)
321
322#define ALIAS_DEPRECATED(funcname, cmdname, cmdstr, helpstr) \
323 DEFUN_CMD_ELEMENT(funcname, cmdname, cmdstr, helpstr, CMD_ATTR_DEPRECATED, 0)
718e3744 324
274a4a44 325#define ALIAS_SH(daemon, funcname, cmdname, cmdstr, helpstr) \
326 DEFUN_CMD_ELEMENT(funcname, cmdname, cmdstr, helpstr, 0, daemon)
327
328#define ALIAS_SH_HIDDEN(daemon, funcname, cmdname, cmdstr, helpstr) \
329 DEFUN_CMD_ELEMENT(funcname, cmdname, cmdstr, helpstr, CMD_ATTR_HIDDEN, daemon)
330
331#define ALIAS_SH_DEPRECATED(daemon, funcname, cmdname, cmdstr, helpstr) \
332 DEFUN_CMD_ELEMENT(funcname, cmdname, cmdstr, helpstr, CMD_ATTR_DEPRECATED, daemon)
333
718e3744 334#endif /* VTYSH_EXTRACT_PL */
335
336/* Some macroes */
ba6d273b
DS
337
338/*
339 * Sometimes #defines create maximum values that
340 * need to have strings created from them that
341 * allow the parser to match against them.
342 * These macros allow that.
343 */
344#define CMD_CREATE_STR(s) CMD_CREATE_STR_HELPER(s)
345#define CMD_CREATE_STR_HELPER(s) #s
ba6d273b 346
718e3744 347/* Common descriptions. */
348#define SHOW_STR "Show running system information\n"
349#define IP_STR "IP information\n"
350#define IPV6_STR "IPv6 information\n"
351#define NO_STR "Negate a command or set its defaults\n"
9c42a6eb 352#define REDIST_STR "Redistribute information from another routing protocol\n"
718e3744 353#define CLEAR_STR "Reset functions\n"
354#define RIP_STR "RIP information\n"
355#define BGP_STR "BGP information\n"
e0bce756
DS
356#define BGP_SOFT_STR "Soft reconfig inbound and outbound updates\n"
357#define BGP_SOFT_IN_STR "Send route-refresh unless using 'soft-reconfiguration inbound'\n"
358#define BGP_SOFT_OUT_STR "Resend all outbound updates\n"
359#define BGP_SOFT_RSCLIENT_RIB_STR "Soft reconfig for rsclient RIB\n"
718e3744 360#define OSPF_STR "OSPF information\n"
361#define NEIGHBOR_STR "Specify neighbor router\n"
362#define DEBUG_STR "Debugging functions (see also 'undebug')\n"
363#define UNDEBUG_STR "Disable debugging functions (see also 'debug')\n"
364#define ROUTER_STR "Enable a routing process\n"
365#define AS_STR "AS number\n"
366#define MBGP_STR "MBGP information\n"
367#define MATCH_STR "Match values from routing table\n"
368#define SET_STR "Set values in destination routing protocol\n"
369#define OUT_STR "Filter outgoing routing updates\n"
370#define IN_STR "Filter incoming routing updates\n"
371#define V4NOTATION_STR "specify by IPv4 address notation(e.g. 0.0.0.0)\n"
372#define OSPF6_NUMBER_STR "Specify by number\n"
373#define INTERFACE_STR "Interface infomation\n"
374#define IFNAME_STR "Interface name(e.g. ep0)\n"
375#define IP6_STR "IPv6 Information\n"
376#define OSPF6_STR "Open Shortest Path First (OSPF) for IPv6\n"
d0bfb22c 377#define OSPF6_INSTANCE_STR "(1-65535) Instance ID\n"
98cfd06b 378#define SECONDS_STR "Seconds\n"
718e3744 379#define ROUTE_STR "Routing Table\n"
380#define PREFIX_LIST_STR "Build a prefix list\n"
381#define OSPF6_DUMP_TYPE_LIST \
d0bfb22c 382"<neighbor|interface|area|lsa|zebra|config|dbex|spf|route|lsdb|redistribute|hook|asbr|prefix|abr>"
9e867fe6 383#define ISIS_STR "IS-IS information\n"
384#define AREA_TAG_STR "[area tag]\n"
d0bfb22c
QY
385#define COMMUNITY_AANN_STR "Community number where AA and NN are (0-65535)\n"
386#define COMMUNITY_VAL_STR "Community number in AA:NN format (where AA and NN are (0-65535)) or local-AS|no-advertise|no-export|internet or additive\n"
16f1b9ee
OD
387#define MPLS_TE_STR "MPLS-TE specific commands\n"
388#define LINK_PARAMS_STR "Configure interface link parameters\n"
389#define OSPF_RI_STR "OSPF Router Information specific commands\n"
390#define PCE_STR "PCE Router Information specific commands\n"
7758e3f3 391#define MPLS_STR "MPLS information\n"
718e3744 392
393#define CONF_BACKUP_EXT ".sav"
394
395/* IPv4 only machine should not accept IPv6 address for peer's IP
396 address. So we replace VTY command string like below. */
718e3744 397#define NEIGHBOR_ADDR_STR "Neighbor address\nIPv6 address\n"
a80beece
DS
398#define NEIGHBOR_ADDR_STR2 "Neighbor address\nNeighbor IPv6 address\nInterface name or neighbor tag\n"
399#define NEIGHBOR_ADDR_STR3 "Neighbor address\nIPv6 address\nInterface name\n"
718e3744 400
401/* Prototypes. */
8cc4198f 402extern void install_node (struct cmd_node *, int (*) (struct vty *));
403extern void install_default (enum node_type);
404extern void install_element (enum node_type, struct cmd_element *);
718e3744 405
de8f7a39
DL
406/* known issue with uninstall_element: changes to cmd_token->attr (i.e.
407 * deprecated/hidden) are not reversed. */
408extern void uninstall_element (enum node_type, struct cmd_element *);
409
f6834d4c 410/* Concatenates argv[shift] through argv[argc-1] into a single NUL-terminated
411 string with a space between each element (allocated using
412 XMALLOC(MTYPE_TMP)). Returns NULL if shift >= argc. */
d0bfb22c 413extern char *argv_concat (struct cmd_token **argv, int argc, int shift);
ae19d7dd 414extern int argv_find (struct cmd_token **argv, int argc, const char *text, int *index);
8cc4198f 415
416extern vector cmd_make_strvec (const char *);
417extern void cmd_free_strvec (vector);
418extern vector cmd_describe_command (vector, struct vty *, int *status);
419extern char **cmd_complete_command (vector, struct vty *, int *status);
420extern const char *cmd_prompt (enum node_type);
17aca20b 421extern int command_config_read_one_line (struct vty *vty, const struct cmd_element **, int use_config_node);
13fbc82d 422extern int config_from_file (struct vty *, FILE *, unsigned int *line_num);
8cc4198f 423extern enum node_type node_parent (enum node_type);
17aca20b
QY
424extern int cmd_execute_command (vector, struct vty *, const struct cmd_element **, int);
425extern int cmd_execute_command_strict (vector, struct vty *, const struct cmd_element **);
8cc4198f 426extern void cmd_init (int);
228da428 427extern void cmd_terminate (void);
0b84f294
DL
428extern void cmd_exit (struct vty *vty);
429extern int cmd_list_cmds (struct vty *vty, int do_permute);
718e3744 430
bff9c3e9
DL
431extern int cmd_hostname_set (const char *hostname);
432
af2567b6
DL
433/* NOT safe for general use; call this only if DEV_BUILD! */
434extern void grammar_sandbox_init (void);
435
d0bfb22c 436/* memory management for cmd_token */
83364d20
DL
437extern struct cmd_token *new_cmd_token (enum cmd_token_type, u_char attr,
438 const char *text, const char *desc);
439extern void del_cmd_token (struct cmd_token *);
440extern struct cmd_token *copy_cmd_token (struct cmd_token *);
d0bfb22c 441
af2567b6 442extern vector completions_to_vec (struct list *completions);
de8f7a39 443extern void cmd_merge_graphs (struct graph *old, struct graph *new, int direction);
2ab40270
DL
444extern void command_parse_format (struct graph *graph, struct cmd_element *cmd);
445
718e3744 446/* Export typical functions. */
57387fb2 447extern const char *host_config_get (void);
c0e8c16f 448extern void host_config_set (const char *);
6590f2c3 449
8cc4198f 450extern void print_version (const char *);
86228fde 451
7cfc61d3
DS
452extern int cmd_banner_motd_file (const char *);
453
86228fde 454/* struct host global, ick */
d0bfb22c
QY
455extern struct host host;
456
457/* text for <cr> command */
458#define CMD_CR_TEXT "<cr>"
228da428 459
718e3744 460#endif /* _ZEBRA_COMMAND_H */