libosmovty  0.9.3
Osmocom VTY library
include/osmocom/vty/command.h
Go to the documentation of this file.
00001 /*
00002  * Zebra configuration command interface routine
00003  * Copyright (C) 1997, 98 Kunihiro Ishiguro
00004  *
00005  * This file is part of GNU Zebra.
00006  *
00007  * GNU Zebra is free software; you can redistribute it and/or modify
00008  * it under the terms of the GNU General Public License as published
00009  * by the Free Software Foundation; either version 2, or (at your
00010  * option) any later version.
00011  *
00012  * GNU Zebra is distributed in the hope that it will be useful, but
00013  * WITHOUT ANY WARRANTY; without even the implied warranty of
00014  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00015  * General Public License for more details.
00016  *
00017  * You should have received a copy of the GNU General Public License
00018  * along with GNU Zebra; see the file COPYING.  If not, write to the
00019  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
00020  * Boston, MA 02111-1307, USA.
00021  */
00022 
00023 #pragma once
00024 
00025 #include <stdio.h>
00026 #include <sys/types.h>
00027 #include "vector.h"
00028 
00035 struct host {
00037         char *name;
00038 
00040         char *password;
00041         char *password_encrypt;
00042 
00044         char *enable;
00045         char *enable_encrypt;
00046 
00048         int lines;
00049 
00051         char *logfile;
00052 
00054         char *config;
00055 
00057         int advanced;
00058         int encrypt;
00059 
00061         const char *motd;
00062         char *motdfile;
00063 
00065         const struct vty_app_info *app_info;
00066 };
00067 
00069 enum node_type {
00070         AUTH_NODE,              
00071         VIEW_NODE,              
00072         AUTH_ENABLE_NODE,       
00073         ENABLE_NODE,            
00074         CONFIG_NODE,            
00075         SERVICE_NODE,           
00076         DEBUG_NODE,             
00077         CFG_LOG_NODE,           
00078         CFG_STATS_NODE,         
00080         VTY_NODE,               
00082         L_E1INP_NODE,           
00083         L_IPA_NODE,             
00084         L_NS_NODE,              
00085         L_BSSGP_NODE,           
00087         /*
00088          * When adding new nodes to the libosmocore project, these nodes can be
00089          * used to avoid ABI changes for unrelated projects.
00090          */
00091         RESERVED1_NODE,         
00092         RESERVED2_NODE,         
00093         RESERVED3_NODE,         
00094         RESERVED4_NODE,         
00096         _LAST_OSMOVTY_NODE
00097 };
00098 
00099 #include "vty.h"
00100 
00103 struct cmd_node {
00105         int node;
00106 
00108         const char *prompt;
00109 
00111         int vtysh;
00112 
00114         int (*func) (struct vty *);
00115 
00117         vector cmd_vector;
00118 };
00119 
00120 enum {
00121         CMD_ATTR_DEPRECATED = 1,
00122         CMD_ATTR_HIDDEN,
00123 };
00124 
00126 struct cmd_element {
00127         const char *string;     
00128         int (*func) (struct cmd_element *, struct vty *, int, const char *[]);
00129         const char *doc;        
00130         int daemon;             
00131         vector strvec;          
00132         unsigned int cmdsize;   
00133         char *config;           
00134         vector subconfig;       
00135         unsigned char attr;     
00136 };
00137 
00139 struct desc {
00140         const char *cmd;        
00141         const char *str;        
00142 };
00143 
00145 #define CMD_SUCCESS              0
00146 #define CMD_WARNING              1
00147 #define CMD_ERR_NO_MATCH         2
00148 #define CMD_ERR_AMBIGUOUS        3
00149 #define CMD_ERR_INCOMPLETE       4
00150 #define CMD_ERR_EXEED_ARGC_MAX   5
00151 #define CMD_ERR_NOTHING_TODO     6
00152 #define CMD_COMPLETE_FULL_MATCH  7
00153 #define CMD_COMPLETE_MATCH       8
00154 #define CMD_COMPLETE_LIST_MATCH  9
00155 #define CMD_SUCCESS_DAEMON      10
00156 
00157 /* Argc max counts. */
00158 #define CMD_ARGC_MAX   256
00159 
00160 /* Turn off these macros when uisng cpp with extract.pl */
00161 #ifndef VTYSH_EXTRACT_PL
00162 
00163 /* helper defines for end-user DEFUN* macros */
00164 #define DEFUN_CMD_ELEMENT(funcname, cmdname, cmdstr, helpstr, attrs, dnum) \
00165   static struct cmd_element cmdname = \
00166   { \
00167     .string = cmdstr, \
00168     .func = funcname, \
00169     .doc = helpstr, \
00170     .attr = attrs, \
00171     .daemon = dnum, \
00172   };
00173 
00174 /* global (non static) cmd_element */
00175 #define gDEFUN_CMD_ELEMENT(funcname, cmdname, cmdstr, helpstr, attrs, dnum) \
00176   struct cmd_element cmdname = \
00177   { \
00178     .string = cmdstr, \
00179     .func = funcname, \
00180     .doc = helpstr, \
00181     .attr = attrs, \
00182     .daemon = dnum, \
00183   };
00184 
00185 #define DEFUN_CMD_FUNC_DECL(funcname) \
00186   static int funcname (struct cmd_element *, struct vty *, int, const char *[]); \
00187 
00188 #define DEFUN_CMD_FUNC_TEXT(funcname) \
00189   static int funcname \
00190     (struct cmd_element *self, struct vty *vty, int argc, const char *argv[])
00191 
00198 #define DEFUN(funcname, cmdname, cmdstr, helpstr) \
00199   DEFUN_CMD_FUNC_DECL(funcname) \
00200   DEFUN_CMD_ELEMENT(funcname, cmdname, cmdstr, helpstr, 0, 0) \
00201   DEFUN_CMD_FUNC_TEXT(funcname)
00202 
00209 #define gDEFUN(funcname, cmdname, cmdstr, helpstr) \
00210   DEFUN_CMD_FUNC_DECL(funcname) \
00211   gDEFUN_CMD_ELEMENT(funcname, cmdname, cmdstr, helpstr, 0, 0) \
00212   DEFUN_CMD_FUNC_TEXT(funcname)
00213 
00214 #define DEFUN_ATTR(funcname, cmdname, cmdstr, helpstr, attr) \
00215   DEFUN_CMD_FUNC_DECL(funcname) \
00216   DEFUN_CMD_ELEMENT(funcname, cmdname, cmdstr, helpstr, attr, 0) \
00217   DEFUN_CMD_FUNC_TEXT(funcname)
00218 
00219 #define DEFUN_HIDDEN(funcname, cmdname, cmdstr, helpstr) \
00220   DEFUN_ATTR (funcname, cmdname, cmdstr, helpstr, CMD_ATTR_HIDDEN)
00221 
00222 #define DEFUN_DEPRECATED(funcname, cmdname, cmdstr, helpstr) \
00223   DEFUN_ATTR (funcname, cmdname, cmdstr, helpstr, CMD_ATTR_DEPRECATED) \
00224 
00225 /* DEFUN_NOSH for commands that vtysh should ignore */
00226 #define DEFUN_NOSH(funcname, cmdname, cmdstr, helpstr) \
00227   DEFUN(funcname, cmdname, cmdstr, helpstr)
00228 
00229 /* DEFSH for vtysh. */
00230 #define DEFSH(daemon, cmdname, cmdstr, helpstr) \
00231   DEFUN_CMD_ELEMENT(NULL, cmdname, cmdstr, helpstr, 0, daemon) \
00232 
00233 /* DEFUN + DEFSH */
00234 #define DEFUNSH(daemon, funcname, cmdname, cmdstr, helpstr) \
00235   DEFUN_CMD_FUNC_DECL(funcname) \
00236   DEFUN_CMD_ELEMENT(funcname, cmdname, cmdstr, helpstr, 0, daemon) \
00237   DEFUN_CMD_FUNC_TEXT(funcname)
00238 
00239 /* DEFUN + DEFSH with attributes */
00240 #define DEFUNSH_ATTR(daemon, funcname, cmdname, cmdstr, helpstr, attr) \
00241   DEFUN_CMD_FUNC_DECL(funcname) \
00242   DEFUN_CMD_ELEMENT(funcname, cmdname, cmdstr, helpstr, attr, daemon) \
00243   DEFUN_CMD_FUNC_TEXT(funcname)
00244 
00245 #define DEFUNSH_HIDDEN(daemon, funcname, cmdname, cmdstr, helpstr) \
00246   DEFUNSH_ATTR (daemon, funcname, cmdname, cmdstr, helpstr, CMD_ATTR_HIDDEN)
00247 
00248 #define DEFUNSH_DEPRECATED(daemon, funcname, cmdname, cmdstr, helpstr) \
00249   DEFUNSH_ATTR (daemon, funcname, cmdname, cmdstr, helpstr, CMD_ATTR_DEPRECATED)
00250 
00251 /* ALIAS macro which define existing command's alias. */
00252 #define ALIAS(funcname, cmdname, cmdstr, helpstr) \
00253   DEFUN_CMD_ELEMENT(funcname, cmdname, cmdstr, helpstr, 0, 0)
00254 
00255 /* global (non static) cmd_element */
00256 #define gALIAS(funcname, cmdname, cmdstr, helpstr) \
00257   gDEFUN_CMD_ELEMENT(funcname, cmdname, cmdstr, helpstr, 0, 0)
00258 
00259 #define ALIAS_ATTR(funcname, cmdname, cmdstr, helpstr, attr) \
00260   DEFUN_CMD_ELEMENT(funcname, cmdname, cmdstr, helpstr, attr, 0)
00261 
00262 #define ALIAS_HIDDEN(funcname, cmdname, cmdstr, helpstr) \
00263   DEFUN_CMD_ELEMENT(funcname, cmdname, cmdstr, helpstr, CMD_ATTR_HIDDEN, 0)
00264 
00265 #define ALIAS_DEPRECATED(funcname, cmdname, cmdstr, helpstr) \
00266   DEFUN_CMD_ELEMENT(funcname, cmdname, cmdstr, helpstr, CMD_ATTR_DEPRECATED, 0)
00267 
00268 #define ALIAS_SH(daemon, funcname, cmdname, cmdstr, helpstr) \
00269   DEFUN_CMD_ELEMENT(funcname, cmdname, cmdstr, helpstr, 0, daemon)
00270 
00271 #define ALIAS_SH_HIDDEN(daemon, funcname, cmdname, cmdstr, helpstr) \
00272   DEFUN_CMD_ELEMENT(funcname, cmdname, cmdstr, helpstr, CMD_ATTR_HIDDEN, daemon)
00273 
00274 #define ALIAS_SH_DEPRECATED(daemon, funcname, cmdname, cmdstr, helpstr) \
00275   DEFUN_CMD_ELEMENT(funcname, cmdname, cmdstr, helpstr, CMD_ATTR_DEPRECATED, daemon)
00276 
00277 #endif                          /* VTYSH_EXTRACT_PL */
00278 
00279 /* Some macroes */
00280 #define CMD_OPTION(S)   ((S[0]) == '[')
00281 #define CMD_VARIABLE(S) (((S[0]) >= 'A' && (S[0]) <= 'Z') || ((S[0]) == '<'))
00282 #define CMD_VARARG(S)   ((S[0]) == '.')
00283 #define CMD_RANGE(S)    ((S[0] == '<'))
00284 
00285 #define CMD_IPV4(S)        ((strcmp ((S), "A.B.C.D") == 0))
00286 #define CMD_IPV4_PREFIX(S) ((strcmp ((S), "A.B.C.D/M") == 0))
00287 #define CMD_IPV6(S)        ((strcmp ((S), "X:X::X:X") == 0))
00288 #define CMD_IPV6_PREFIX(S) ((strcmp ((S), "X:X::X:X/M") == 0))
00289 
00290 /* Common descriptions. */
00291 #define SHOW_STR "Show running system information\n"
00292 #define IP_STR "IP information\n"
00293 #define IPV6_STR "IPv6 information\n"
00294 #define NO_STR "Negate a command or set its defaults\n"
00295 #define CLEAR_STR "Reset functions\n"
00296 #define RIP_STR "RIP information\n"
00297 #define BGP_STR "BGP information\n"
00298 #define OSPF_STR "OSPF information\n"
00299 #define NEIGHBOR_STR "Specify neighbor router\n"
00300 #define DEBUG_STR "Debugging functions (see also 'undebug')\n"
00301 #define UNDEBUG_STR "Disable debugging functions (see also 'debug')\n"
00302 #define ROUTER_STR "Enable a routing process\n"
00303 #define AS_STR "AS number\n"
00304 #define MBGP_STR "MBGP information\n"
00305 #define MATCH_STR "Match values from routing table\n"
00306 #define SET_STR "Set values in destination routing protocol\n"
00307 #define OUT_STR "Filter outgoing routing updates\n"
00308 #define IN_STR  "Filter incoming routing updates\n"
00309 #define V4NOTATION_STR "specify by IPv4 address notation(e.g. 0.0.0.0)\n"
00310 #define OSPF6_NUMBER_STR "Specify by number\n"
00311 #define INTERFACE_STR "Interface infomation\n"
00312 #define IFNAME_STR "Interface name(e.g. ep0)\n"
00313 #define IP6_STR "IPv6 Information\n"
00314 #define OSPF6_STR "Open Shortest Path First (OSPF) for IPv6\n"
00315 #define OSPF6_ROUTER_STR "Enable a routing process\n"
00316 #define OSPF6_INSTANCE_STR "<1-65535> Instance ID\n"
00317 #define SECONDS_STR "<1-65535> Seconds\n"
00318 #define ROUTE_STR "Routing Table\n"
00319 #define PREFIX_LIST_STR "Build a prefix list\n"
00320 #define OSPF6_DUMP_TYPE_LIST \
00321 "(neighbor|interface|area|lsa|zebra|config|dbex|spf|route|lsdb|redistribute|hook|asbr|prefix|abr)"
00322 #define ISIS_STR "IS-IS information\n"
00323 #define AREA_TAG_STR "[area tag]\n"
00324 
00325 #define CONF_BACKUP_EXT ".sav"
00326 
00327 /* IPv4 only machine should not accept IPv6 address for peer's IP
00328    address.  So we replace VTY command string like below. */
00329 #ifdef HAVE_IPV6
00330 #define NEIGHBOR_CMD       "neighbor (A.B.C.D|X:X::X:X) "
00331 #define NO_NEIGHBOR_CMD    "no neighbor (A.B.C.D|X:X::X:X) "
00332 #define NEIGHBOR_ADDR_STR  "Neighbor address\nIPv6 address\n"
00333 #define NEIGHBOR_CMD2      "neighbor (A.B.C.D|X:X::X:X|WORD) "
00334 #define NO_NEIGHBOR_CMD2   "no neighbor (A.B.C.D|X:X::X:X|WORD) "
00335 #define NEIGHBOR_ADDR_STR2 "Neighbor address\nNeighbor IPv6 address\nNeighbor tag\n"
00336 #else
00337 #define NEIGHBOR_CMD       "neighbor A.B.C.D "
00338 #define NO_NEIGHBOR_CMD    "no neighbor A.B.C.D "
00339 #define NEIGHBOR_ADDR_STR  "Neighbor address\n"
00340 #define NEIGHBOR_CMD2      "neighbor (A.B.C.D|WORD) "
00341 #define NO_NEIGHBOR_CMD2   "no neighbor (A.B.C.D|WORD) "
00342 #define NEIGHBOR_ADDR_STR2 "Neighbor address\nNeighbor tag\n"
00343 #endif                          /* HAVE_IPV6 */
00344 
00345 /* Prototypes. */
00346 void install_node(struct cmd_node *, int (*)(struct vty *));
00347 void install_default(int node_type);
00348 void install_element(int node_type, struct cmd_element *);
00349 void install_element_ve(struct cmd_element *cmd);
00350 void sort_node(void);
00351 
00352 /* This is similar to install_default() but it also creates
00353  * 'exit' and 'end' commands.
00354  */
00355 void vty_install_default(int node_type);
00356 
00357 /* Concatenates argv[shift] through argv[argc-1] into a single NUL-terminated
00358    string with a space between each element (allocated using
00359    XMALLOC(MTYPE_TMP)).  Returns NULL if shift >= argc. */
00360 char *argv_concat(const char **argv, int argc, int shift);
00361 
00362 vector cmd_make_strvec(const char *);
00363 void cmd_free_strvec(vector);
00364 vector cmd_describe_command();
00365 char **cmd_complete_command();
00366 const char *cmd_prompt(enum node_type);
00367 int config_from_file(struct vty *, FILE *);
00368 enum node_type node_parent(enum node_type);
00369 int cmd_execute_command(vector, struct vty *, struct cmd_element **, int);
00370 int cmd_execute_command_strict(vector, struct vty *, struct cmd_element **);
00371 void config_replace_string(struct cmd_element *, char *, ...);
00372 void cmd_init(int);
00373 
00374 /* Export typical functions. */
00375 extern struct cmd_element config_exit_cmd;
00376 extern struct cmd_element config_help_cmd;
00377 extern struct cmd_element config_list_cmd;
00378 extern struct cmd_element config_end_cmd;
00379 char *host_config_file();
00380 void host_config_set(const char *);
00381 
00382 /* This is called from main when a daemon is invoked with -v or --version. */
00383 void print_version(int print_copyright);
00384 
00385 extern void *tall_vty_cmd_ctx;
00386