Merge commit '28dbfe9bf7a799ab1da2563fd5e007d007b54168'
[osmocom-bb.git] / src / shared / libosmocore / include / osmocom / vty / command.h
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.
11  *
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 <stdio.h>
27 #include <sys/types.h>
28 #include "vector.h"
29 #include "vty.h"
30
31 /* Host configuration variable */
32 struct host {
33         /* Host name of this router. */
34         char *name;
35
36         /* Password for vty interface. */
37         char *password;
38         char *password_encrypt;
39
40         /* Enable password */
41         char *enable;
42         char *enable_encrypt;
43
44         /* System wide terminal lines. */
45         int lines;
46
47         /* Log filename. */
48         char *logfile;
49
50         /* config file name of this host */
51         char *config;
52
53         /* Flags for services */
54         int advanced;
55         int encrypt;
56
57         /* Banner configuration. */
58         const char *motd;
59         char *motdfile;
60
61         const struct vty_app_info *app_info;
62 };
63
64 /* There are some command levels which called from command node. */
65 enum node_type {
66         AUTH_NODE,              /* Authentication mode of vty interface. */
67         VIEW_NODE,              /* View node. Default mode of vty interface. */
68         AUTH_ENABLE_NODE,       /* Authentication mode for change enable. */
69         ENABLE_NODE,            /* Enable node. */
70         CONFIG_NODE,            /* Config node. Default mode of config file. */
71         SERVICE_NODE,           /* Service node. */
72         DEBUG_NODE,             /* Debug node. */
73         CFG_LOG_NODE,           /* Configure the logging */
74
75         VTY_NODE,               /* Vty node. */
76
77         _LAST_OSMOVTY_NODE
78 };
79
80 /* Node which has some commands and prompt string and configuration
81    function pointer . */
82 struct cmd_node {
83         /* Node index. */
84         enum node_type node;
85
86         /* Prompt character at vty interface. */
87         const char *prompt;
88
89         /* Is this node's configuration goes to vtysh ? */
90         int vtysh;
91
92         /* Node's configuration write function */
93         int (*func) (struct vty *);
94
95         /* Vector of this node's command list. */
96         vector cmd_vector;
97 };
98
99 enum {
100         CMD_ATTR_DEPRECATED = 1,
101         CMD_ATTR_HIDDEN,
102 };
103
104 /* Structure of command element. */
105 struct cmd_element {
106         const char *string;     /* Command specification by string. */
107         int (*func) (struct cmd_element *, struct vty *, int, const char *[]);
108         const char *doc;        /* Documentation of this command. */
109         int daemon;             /* Daemon to which this command belong. */
110         vector strvec;          /* Pointing out each description vector. */
111         unsigned int cmdsize;   /* Command index count. */
112         char *config;           /* Configuration string */
113         vector subconfig;       /* Sub configuration string */
114         u_char attr;            /* Command attributes */
115 };
116
117 /* Command description structure. */
118 struct desc {
119         const char *cmd;        /* Command string. */
120         const char *str;        /* Command's description. */
121 };
122
123 /* Return value of the commands. */
124 #define CMD_SUCCESS              0
125 #define CMD_WARNING              1
126 #define CMD_ERR_NO_MATCH         2
127 #define CMD_ERR_AMBIGUOUS        3
128 #define CMD_ERR_INCOMPLETE       4
129 #define CMD_ERR_EXEED_ARGC_MAX   5
130 #define CMD_ERR_NOTHING_TODO     6
131 #define CMD_COMPLETE_FULL_MATCH  7
132 #define CMD_COMPLETE_MATCH       8
133 #define CMD_COMPLETE_LIST_MATCH  9
134 #define CMD_SUCCESS_DAEMON      10
135
136 /* Argc max counts. */
137 #define CMD_ARGC_MAX   25
138
139 /* Turn off these macros when uisng cpp with extract.pl */
140 #ifndef VTYSH_EXTRACT_PL
141
142 /* helper defines for end-user DEFUN* macros */
143 #define DEFUN_CMD_ELEMENT(funcname, cmdname, cmdstr, helpstr, attrs, dnum) \
144   static struct cmd_element cmdname = \
145   { \
146     .string = cmdstr, \
147     .func = funcname, \
148     .doc = helpstr, \
149     .attr = attrs, \
150     .daemon = dnum, \
151   };
152
153 /* global (non static) cmd_element */
154 #define gDEFUN_CMD_ELEMENT(funcname, cmdname, cmdstr, helpstr, attrs, dnum) \
155   struct cmd_element cmdname = \
156   { \
157     .string = cmdstr, \
158     .func = funcname, \
159     .doc = helpstr, \
160     .attr = attrs, \
161     .daemon = dnum, \
162   };
163
164 #define DEFUN_CMD_FUNC_DECL(funcname) \
165   static int funcname (struct cmd_element *, struct vty *, int, const char *[]); \
166
167 #define DEFUN_CMD_FUNC_TEXT(funcname) \
168   static int funcname \
169     (struct cmd_element *self, struct vty *vty, int argc, const char *argv[])
170
171 /* DEFUN for vty command interafce. Little bit hacky ;-). */
172 #define DEFUN(funcname, cmdname, cmdstr, helpstr) \
173   DEFUN_CMD_FUNC_DECL(funcname) \
174   DEFUN_CMD_ELEMENT(funcname, cmdname, cmdstr, helpstr, 0, 0) \
175   DEFUN_CMD_FUNC_TEXT(funcname)
176
177 /* global (non static) cmd_element */
178 #define gDEFUN(funcname, cmdname, cmdstr, helpstr) \
179   DEFUN_CMD_FUNC_DECL(funcname) \
180   gDEFUN_CMD_ELEMENT(funcname, cmdname, cmdstr, helpstr, 0, 0) \
181   DEFUN_CMD_FUNC_TEXT(funcname)
182
183 #define DEFUN_ATTR(funcname, cmdname, cmdstr, helpstr, attr) \
184   DEFUN_CMD_FUNC_DECL(funcname) \
185   DEFUN_CMD_ELEMENT(funcname, cmdname, cmdstr, helpstr, attr, 0) \
186   DEFUN_CMD_FUNC_TEXT(funcname)
187
188 #define DEFUN_HIDDEN(funcname, cmdname, cmdstr, helpstr) \
189   DEFUN_ATTR (funcname, cmdname, cmdstr, helpstr, CMD_ATTR_HIDDEN)
190
191 #define DEFUN_DEPRECATED(funcname, cmdname, cmdstr, helpstr) \
192   DEFUN_ATTR (funcname, cmdname, cmdstr, helpstr, CMD_ATTR_DEPRECATED) \
193
194 /* DEFUN_NOSH for commands that vtysh should ignore */
195 #define DEFUN_NOSH(funcname, cmdname, cmdstr, helpstr) \
196   DEFUN(funcname, cmdname, cmdstr, helpstr)
197
198 /* DEFSH for vtysh. */
199 #define DEFSH(daemon, cmdname, cmdstr, helpstr) \
200   DEFUN_CMD_ELEMENT(NULL, cmdname, cmdstr, helpstr, 0, daemon) \
201
202 /* DEFUN + DEFSH */
203 #define DEFUNSH(daemon, funcname, cmdname, cmdstr, helpstr) \
204   DEFUN_CMD_FUNC_DECL(funcname) \
205   DEFUN_CMD_ELEMENT(funcname, cmdname, cmdstr, helpstr, 0, daemon) \
206   DEFUN_CMD_FUNC_TEXT(funcname)
207
208 /* DEFUN + DEFSH with attributes */
209 #define DEFUNSH_ATTR(daemon, funcname, cmdname, cmdstr, helpstr, attr) \
210   DEFUN_CMD_FUNC_DECL(funcname) \
211   DEFUN_CMD_ELEMENT(funcname, cmdname, cmdstr, helpstr, attr, daemon) \
212   DEFUN_CMD_FUNC_TEXT(funcname)
213
214 #define DEFUNSH_HIDDEN(daemon, funcname, cmdname, cmdstr, helpstr) \
215   DEFUNSH_ATTR (daemon, funcname, cmdname, cmdstr, helpstr, CMD_ATTR_HIDDEN)
216
217 #define DEFUNSH_DEPRECATED(daemon, funcname, cmdname, cmdstr, helpstr) \
218   DEFUNSH_ATTR (daemon, funcname, cmdname, cmdstr, helpstr, CMD_ATTR_DEPRECATED)
219
220 /* ALIAS macro which define existing command's alias. */
221 #define ALIAS(funcname, cmdname, cmdstr, helpstr) \
222   DEFUN_CMD_ELEMENT(funcname, cmdname, cmdstr, helpstr, 0, 0)
223
224 /* global (non static) cmd_element */
225 #define gALIAS(funcname, cmdname, cmdstr, helpstr) \
226   gDEFUN_CMD_ELEMENT(funcname, cmdname, cmdstr, helpstr, 0, 0)
227
228 #define ALIAS_ATTR(funcname, cmdname, cmdstr, helpstr, attr) \
229   DEFUN_CMD_ELEMENT(funcname, cmdname, cmdstr, helpstr, attr, 0)
230
231 #define ALIAS_HIDDEN(funcname, cmdname, cmdstr, helpstr) \
232   DEFUN_CMD_ELEMENT(funcname, cmdname, cmdstr, helpstr, CMD_ATTR_HIDDEN, 0)
233
234 #define ALIAS_DEPRECATED(funcname, cmdname, cmdstr, helpstr) \
235   DEFUN_CMD_ELEMENT(funcname, cmdname, cmdstr, helpstr, CMD_ATTR_DEPRECATED, 0)
236
237 #define ALIAS_SH(daemon, funcname, cmdname, cmdstr, helpstr) \
238   DEFUN_CMD_ELEMENT(funcname, cmdname, cmdstr, helpstr, 0, daemon)
239
240 #define ALIAS_SH_HIDDEN(daemon, funcname, cmdname, cmdstr, helpstr) \
241   DEFUN_CMD_ELEMENT(funcname, cmdname, cmdstr, helpstr, CMD_ATTR_HIDDEN, daemon)
242
243 #define ALIAS_SH_DEPRECATED(daemon, funcname, cmdname, cmdstr, helpstr) \
244   DEFUN_CMD_ELEMENT(funcname, cmdname, cmdstr, helpstr, CMD_ATTR_DEPRECATED, daemon)
245
246 #endif                          /* VTYSH_EXTRACT_PL */
247
248 /* Some macroes */
249 #define CMD_OPTION(S)   ((S[0]) == '[')
250 #define CMD_VARIABLE(S) (((S[0]) >= 'A' && (S[0]) <= 'Z') || ((S[0]) == '<'))
251 #define CMD_VARARG(S)   ((S[0]) == '.')
252 #define CMD_RANGE(S)    ((S[0] == '<'))
253
254 #define CMD_IPV4(S)        ((strcmp ((S), "A.B.C.D") == 0))
255 #define CMD_IPV4_PREFIX(S) ((strcmp ((S), "A.B.C.D/M") == 0))
256 #define CMD_IPV6(S)        ((strcmp ((S), "X:X::X:X") == 0))
257 #define CMD_IPV6_PREFIX(S) ((strcmp ((S), "X:X::X:X/M") == 0))
258
259 /* Common descriptions. */
260 #define SHOW_STR "Show running system information\n"
261 #define IP_STR "IP information\n"
262 #define IPV6_STR "IPv6 information\n"
263 #define NO_STR "Negate a command or set its defaults\n"
264 #define CLEAR_STR "Reset functions\n"
265 #define RIP_STR "RIP information\n"
266 #define BGP_STR "BGP information\n"
267 #define OSPF_STR "OSPF information\n"
268 #define NEIGHBOR_STR "Specify neighbor router\n"
269 #define DEBUG_STR "Debugging functions (see also 'undebug')\n"
270 #define UNDEBUG_STR "Disable debugging functions (see also 'debug')\n"
271 #define ROUTER_STR "Enable a routing process\n"
272 #define AS_STR "AS number\n"
273 #define MBGP_STR "MBGP information\n"
274 #define MATCH_STR "Match values from routing table\n"
275 #define SET_STR "Set values in destination routing protocol\n"
276 #define OUT_STR "Filter outgoing routing updates\n"
277 #define IN_STR  "Filter incoming routing updates\n"
278 #define V4NOTATION_STR "specify by IPv4 address notation(e.g. 0.0.0.0)\n"
279 #define OSPF6_NUMBER_STR "Specify by number\n"
280 #define INTERFACE_STR "Interface infomation\n"
281 #define IFNAME_STR "Interface name(e.g. ep0)\n"
282 #define IP6_STR "IPv6 Information\n"
283 #define OSPF6_STR "Open Shortest Path First (OSPF) for IPv6\n"
284 #define OSPF6_ROUTER_STR "Enable a routing process\n"
285 #define OSPF6_INSTANCE_STR "<1-65535> Instance ID\n"
286 #define SECONDS_STR "<1-65535> Seconds\n"
287 #define ROUTE_STR "Routing Table\n"
288 #define PREFIX_LIST_STR "Build a prefix list\n"
289 #define OSPF6_DUMP_TYPE_LIST \
290 "(neighbor|interface|area|lsa|zebra|config|dbex|spf|route|lsdb|redistribute|hook|asbr|prefix|abr)"
291 #define ISIS_STR "IS-IS information\n"
292 #define AREA_TAG_STR "[area tag]\n"
293
294 #define CONF_BACKUP_EXT ".sav"
295
296 /* IPv4 only machine should not accept IPv6 address for peer's IP
297    address.  So we replace VTY command string like below. */
298 #ifdef HAVE_IPV6
299 #define NEIGHBOR_CMD       "neighbor (A.B.C.D|X:X::X:X) "
300 #define NO_NEIGHBOR_CMD    "no neighbor (A.B.C.D|X:X::X:X) "
301 #define NEIGHBOR_ADDR_STR  "Neighbor address\nIPv6 address\n"
302 #define NEIGHBOR_CMD2      "neighbor (A.B.C.D|X:X::X:X|WORD) "
303 #define NO_NEIGHBOR_CMD2   "no neighbor (A.B.C.D|X:X::X:X|WORD) "
304 #define NEIGHBOR_ADDR_STR2 "Neighbor address\nNeighbor IPv6 address\nNeighbor tag\n"
305 #else
306 #define NEIGHBOR_CMD       "neighbor A.B.C.D "
307 #define NO_NEIGHBOR_CMD    "no neighbor A.B.C.D "
308 #define NEIGHBOR_ADDR_STR  "Neighbor address\n"
309 #define NEIGHBOR_CMD2      "neighbor (A.B.C.D|WORD) "
310 #define NO_NEIGHBOR_CMD2   "no neighbor (A.B.C.D|WORD) "
311 #define NEIGHBOR_ADDR_STR2 "Neighbor address\nNeighbor tag\n"
312 #endif                          /* HAVE_IPV6 */
313
314 /* Prototypes. */
315 void install_node(struct cmd_node *, int (*)(struct vty *));
316 void install_default(enum node_type);
317 void install_element(enum node_type, struct cmd_element *);
318 void install_element_ve(struct cmd_element *cmd);
319 void sort_node();
320
321 /* Concatenates argv[shift] through argv[argc-1] into a single NUL-terminated
322    string with a space between each element (allocated using
323    XMALLOC(MTYPE_TMP)).  Returns NULL if shift >= argc. */
324 char *argv_concat(const char **argv, int argc, int shift);
325
326 vector cmd_make_strvec(const char *);
327 void cmd_free_strvec(vector);
328 vector cmd_describe_command();
329 char **cmd_complete_command();
330 const char *cmd_prompt(enum node_type);
331 int config_from_file(struct vty *, FILE *);
332 enum node_type node_parent(enum node_type);
333 int cmd_execute_command(vector, struct vty *, struct cmd_element **, int);
334 int cmd_execute_command_strict(vector, struct vty *, struct cmd_element **);
335 void config_replace_string(struct cmd_element *, char *, ...);
336 void cmd_init(int);
337
338 /* Export typical functions. */
339 extern struct cmd_element config_exit_cmd;
340 extern struct cmd_element config_help_cmd;
341 extern struct cmd_element config_list_cmd;
342 char *host_config_file();
343 void host_config_set(const char *);
344
345 /* This is called from main when a daemon is invoked with -v or --version. */
346 void print_version(int print_copyright);
347
348 extern void *tall_vty_cmd_ctx;
349
350 #endif                          /* _ZEBRA_COMMAND_H */