ajs | 274a4a4 | 2004-12-07 15:39:31 +0000 | [diff] [blame] | 1 | /* |
ajs | 274a4a4 | 2004-12-07 15:39:31 +0000 | [diff] [blame] | 2 | Command interpreter routine for virtual terminal [aka TeletYpe] |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 3 | Copyright (C) 1997, 98, 99 Kunihiro Ishiguro |
Christian Franke | cd40b32 | 2013-09-30 12:27:51 +0000 | [diff] [blame] | 4 | Copyright (C) 2013 by Open Source Routing. |
| 5 | Copyright (C) 2013 by Internet Systems Consortium, Inc. ("ISC") |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 6 | |
| 7 | This file is part of GNU Zebra. |
| 8 | |
| 9 | GNU Zebra is free software; you can redistribute it and/or modify |
| 10 | it under the terms of the GNU General Public License as published |
| 11 | by the Free Software Foundation; either version 2, or (at your |
| 12 | option) any later version. |
| 13 | |
| 14 | GNU Zebra is distributed in the hope that it will be useful, but |
| 15 | WITHOUT ANY WARRANTY; without even the implied warranty of |
| 16 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 17 | General Public License for more details. |
| 18 | |
| 19 | You should have received a copy of the GNU General Public License |
| 20 | along with GNU Zebra; see the file COPYING. If not, write to the |
| 21 | Free Software Foundation, Inc., 59 Temple Place - Suite 330, |
| 22 | Boston, MA 02111-1307, USA. */ |
| 23 | |
| 24 | #include <zebra.h> |
| 25 | |
paul | b21b19c | 2003-06-15 01:28:29 +0000 | [diff] [blame] | 26 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 27 | #include "memory.h" |
| 28 | #include "log.h" |
gdt | 5e4fa16 | 2004-03-16 14:38:36 +0000 | [diff] [blame] | 29 | #include <lib/version.h> |
paul | 9ab6812 | 2003-01-18 01:16:20 +0000 | [diff] [blame] | 30 | #include "thread.h" |
paul | b21b19c | 2003-06-15 01:28:29 +0000 | [diff] [blame] | 31 | #include "vector.h" |
| 32 | #include "vty.h" |
| 33 | #include "command.h" |
paul | 354d119 | 2005-04-25 16:26:42 +0000 | [diff] [blame] | 34 | #include "workqueue.h" |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 35 | |
| 36 | /* Command vector which includes some level of command lists. Normally |
| 37 | each daemon maintains each own cmdvec. */ |
paul | eb820af | 2005-09-05 11:54:13 +0000 | [diff] [blame] | 38 | vector cmdvec = NULL; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 39 | |
Christian Franke | cd40b32 | 2013-09-30 12:27:51 +0000 | [diff] [blame] | 40 | struct cmd_token token_cr; |
Chris Caputo | 228da42 | 2009-07-18 05:44:03 +0000 | [diff] [blame] | 41 | char *command_cr = NULL; |
| 42 | |
Christian Franke | cd40b32 | 2013-09-30 12:27:51 +0000 | [diff] [blame] | 43 | enum filter_type |
| 44 | { |
| 45 | FILTER_RELAXED, |
| 46 | FILTER_STRICT |
| 47 | }; |
| 48 | |
| 49 | enum matcher_rv |
| 50 | { |
| 51 | MATCHER_OK, |
| 52 | MATCHER_COMPLETE, |
| 53 | MATCHER_INCOMPLETE, |
| 54 | MATCHER_NO_MATCH, |
| 55 | MATCHER_AMBIGUOUS, |
| 56 | MATCHER_EXCEED_ARGC_MAX |
| 57 | }; |
| 58 | |
| 59 | #define MATCHER_ERROR(matcher_rv) \ |
| 60 | ( (matcher_rv) == MATCHER_INCOMPLETE \ |
| 61 | || (matcher_rv) == MATCHER_NO_MATCH \ |
| 62 | || (matcher_rv) == MATCHER_AMBIGUOUS \ |
| 63 | || (matcher_rv) == MATCHER_EXCEED_ARGC_MAX \ |
| 64 | ) |
| 65 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 66 | /* Host information structure. */ |
| 67 | struct host host; |
| 68 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 69 | /* Standard command node structures. */ |
Stephen Hemminger | 7fc626d | 2008-12-01 11:10:34 -0800 | [diff] [blame] | 70 | static struct cmd_node auth_node = |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 71 | { |
| 72 | AUTH_NODE, |
| 73 | "Password: ", |
| 74 | }; |
| 75 | |
Stephen Hemminger | 7fc626d | 2008-12-01 11:10:34 -0800 | [diff] [blame] | 76 | static struct cmd_node view_node = |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 77 | { |
| 78 | VIEW_NODE, |
| 79 | "%s> ", |
| 80 | }; |
| 81 | |
Stephen Hemminger | 7fc626d | 2008-12-01 11:10:34 -0800 | [diff] [blame] | 82 | static struct cmd_node restricted_node = |
Paul Jakma | 62687ff | 2008-08-23 14:27:06 +0100 | [diff] [blame] | 83 | { |
| 84 | RESTRICTED_NODE, |
| 85 | "%s$ ", |
| 86 | }; |
| 87 | |
Stephen Hemminger | 7fc626d | 2008-12-01 11:10:34 -0800 | [diff] [blame] | 88 | static struct cmd_node auth_enable_node = |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 89 | { |
| 90 | AUTH_ENABLE_NODE, |
| 91 | "Password: ", |
| 92 | }; |
| 93 | |
Stephen Hemminger | 7fc626d | 2008-12-01 11:10:34 -0800 | [diff] [blame] | 94 | static struct cmd_node enable_node = |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 95 | { |
| 96 | ENABLE_NODE, |
| 97 | "%s# ", |
| 98 | }; |
| 99 | |
Stephen Hemminger | 7fc626d | 2008-12-01 11:10:34 -0800 | [diff] [blame] | 100 | static struct cmd_node config_node = |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 101 | { |
| 102 | CONFIG_NODE, |
| 103 | "%s(config)# ", |
| 104 | 1 |
| 105 | }; |
hasso | 6590f2c | 2004-10-19 20:40:08 +0000 | [diff] [blame] | 106 | |
| 107 | /* Default motd string. */ |
Everton Marques | 74b4fad | 2014-09-18 12:06:53 -0300 | [diff] [blame] | 108 | static const char *default_motd = |
hasso | 6590f2c | 2004-10-19 20:40:08 +0000 | [diff] [blame] | 109 | "\r\n\ |
| 110 | Hello, this is " QUAGGA_PROGNAME " (version " QUAGGA_VERSION ").\r\n\ |
| 111 | " QUAGGA_COPYRIGHT "\r\n\ |
David Lamparter | 0be793e | 2012-11-27 01:34:56 +0000 | [diff] [blame] | 112 | " GIT_INFO "\r\n"; |
hasso | 6590f2c | 2004-10-19 20:40:08 +0000 | [diff] [blame] | 113 | |
ajs | 274a4a4 | 2004-12-07 15:39:31 +0000 | [diff] [blame] | 114 | |
Stephen Hemminger | 2d362d1 | 2009-12-21 12:54:58 +0300 | [diff] [blame] | 115 | static const struct facility_map { |
ajs | 274a4a4 | 2004-12-07 15:39:31 +0000 | [diff] [blame] | 116 | int facility; |
| 117 | const char *name; |
| 118 | size_t match; |
| 119 | } syslog_facilities[] = |
| 120 | { |
| 121 | { LOG_KERN, "kern", 1 }, |
| 122 | { LOG_USER, "user", 2 }, |
| 123 | { LOG_MAIL, "mail", 1 }, |
| 124 | { LOG_DAEMON, "daemon", 1 }, |
| 125 | { LOG_AUTH, "auth", 1 }, |
| 126 | { LOG_SYSLOG, "syslog", 1 }, |
| 127 | { LOG_LPR, "lpr", 2 }, |
| 128 | { LOG_NEWS, "news", 1 }, |
| 129 | { LOG_UUCP, "uucp", 2 }, |
| 130 | { LOG_CRON, "cron", 1 }, |
| 131 | #ifdef LOG_FTP |
| 132 | { LOG_FTP, "ftp", 1 }, |
| 133 | #endif |
| 134 | { LOG_LOCAL0, "local0", 6 }, |
| 135 | { LOG_LOCAL1, "local1", 6 }, |
| 136 | { LOG_LOCAL2, "local2", 6 }, |
| 137 | { LOG_LOCAL3, "local3", 6 }, |
| 138 | { LOG_LOCAL4, "local4", 6 }, |
| 139 | { LOG_LOCAL5, "local5", 6 }, |
| 140 | { LOG_LOCAL6, "local6", 6 }, |
| 141 | { LOG_LOCAL7, "local7", 6 }, |
| 142 | { 0, NULL, 0 }, |
| 143 | }; |
| 144 | |
| 145 | static const char * |
| 146 | facility_name(int facility) |
| 147 | { |
Stephen Hemminger | 2d362d1 | 2009-12-21 12:54:58 +0300 | [diff] [blame] | 148 | const struct facility_map *fm; |
ajs | 274a4a4 | 2004-12-07 15:39:31 +0000 | [diff] [blame] | 149 | |
| 150 | for (fm = syslog_facilities; fm->name; fm++) |
| 151 | if (fm->facility == facility) |
| 152 | return fm->name; |
| 153 | return ""; |
| 154 | } |
| 155 | |
| 156 | static int |
| 157 | facility_match(const char *str) |
| 158 | { |
Stephen Hemminger | 2d362d1 | 2009-12-21 12:54:58 +0300 | [diff] [blame] | 159 | const struct facility_map *fm; |
ajs | 274a4a4 | 2004-12-07 15:39:31 +0000 | [diff] [blame] | 160 | |
| 161 | for (fm = syslog_facilities; fm->name; fm++) |
| 162 | if (!strncmp(str,fm->name,fm->match)) |
| 163 | return fm->facility; |
| 164 | return -1; |
| 165 | } |
| 166 | |
| 167 | static int |
| 168 | level_match(const char *s) |
| 169 | { |
| 170 | int level ; |
| 171 | |
| 172 | for ( level = 0 ; zlog_priority [level] != NULL ; level ++ ) |
| 173 | if (!strncmp (s, zlog_priority[level], 2)) |
| 174 | return level; |
| 175 | return ZLOG_DISABLED; |
| 176 | } |
| 177 | |
ajs | cb585b6 | 2005-01-14 17:09:38 +0000 | [diff] [blame] | 178 | /* This is called from main when a daemon is invoked with -v or --version. */ |
hasso | 6590f2c | 2004-10-19 20:40:08 +0000 | [diff] [blame] | 179 | void |
| 180 | print_version (const char *progname) |
| 181 | { |
ajs | cb585b6 | 2005-01-14 17:09:38 +0000 | [diff] [blame] | 182 | printf ("%s version %s\n", progname, QUAGGA_VERSION); |
| 183 | printf ("%s\n", QUAGGA_COPYRIGHT); |
David Lamparter | 7abd875 | 2014-11-22 10:43:29 -0800 | [diff] [blame] | 184 | printf ("configured with:\n\t%s\n", QUAGGA_CONFIG_ARGS); |
hasso | 6590f2c | 2004-10-19 20:40:08 +0000 | [diff] [blame] | 185 | } |
| 186 | |
David Lamparter | 6b0655a | 2014-06-04 06:53:35 +0200 | [diff] [blame] | 187 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 188 | /* Utility function to concatenate argv argument into a single string |
| 189 | with inserting ' ' character between each argument. */ |
| 190 | char * |
paul | 42d4986 | 2004-10-13 05:22:18 +0000 | [diff] [blame] | 191 | argv_concat (const char **argv, int argc, int shift) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 192 | { |
| 193 | int i; |
ajs | f6834d4 | 2005-01-28 20:28:35 +0000 | [diff] [blame] | 194 | size_t len; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 195 | char *str; |
ajs | f6834d4 | 2005-01-28 20:28:35 +0000 | [diff] [blame] | 196 | char *p; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 197 | |
ajs | f6834d4 | 2005-01-28 20:28:35 +0000 | [diff] [blame] | 198 | len = 0; |
| 199 | for (i = shift; i < argc; i++) |
| 200 | len += strlen(argv[i])+1; |
| 201 | if (!len) |
| 202 | return NULL; |
| 203 | p = str = XMALLOC(MTYPE_TMP, len); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 204 | for (i = shift; i < argc; i++) |
| 205 | { |
ajs | f6834d4 | 2005-01-28 20:28:35 +0000 | [diff] [blame] | 206 | size_t arglen; |
| 207 | memcpy(p, argv[i], (arglen = strlen(argv[i]))); |
| 208 | p += arglen; |
| 209 | *p++ = ' '; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 210 | } |
ajs | f6834d4 | 2005-01-28 20:28:35 +0000 | [diff] [blame] | 211 | *(p-1) = '\0'; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 212 | return str; |
| 213 | } |
| 214 | |
Paul Jakma | 9219366 | 2016-06-16 15:53:26 +0100 | [diff] [blame] | 215 | static unsigned int |
| 216 | cmd_hash_key (void *p) |
| 217 | { |
| 218 | return (uintptr_t) p; |
| 219 | } |
| 220 | |
| 221 | static int |
| 222 | cmd_hash_cmp (const void *a, const void *b) |
| 223 | { |
| 224 | return a == b; |
| 225 | } |
| 226 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 227 | /* Install top node of command vector. */ |
| 228 | void |
| 229 | install_node (struct cmd_node *node, |
| 230 | int (*func) (struct vty *)) |
| 231 | { |
| 232 | vector_set_index (cmdvec, node->node, node); |
| 233 | node->func = func; |
| 234 | node->cmd_vector = vector_init (VECTOR_MIN_SIZE); |
Paul Jakma | 9219366 | 2016-06-16 15:53:26 +0100 | [diff] [blame] | 235 | node->cmd_hash = hash_create (cmd_hash_key, cmd_hash_cmp); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 236 | } |
| 237 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 238 | /* Breaking up string into each command piece. I assume given |
| 239 | character is separated by a space character. Return value is a |
| 240 | vector which includes char ** data element. */ |
| 241 | vector |
hasso | ea8e9d9 | 2004-10-07 21:32:14 +0000 | [diff] [blame] | 242 | cmd_make_strvec (const char *string) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 243 | { |
hasso | ea8e9d9 | 2004-10-07 21:32:14 +0000 | [diff] [blame] | 244 | const char *cp, *start; |
| 245 | char *token; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 246 | int strlen; |
| 247 | vector strvec; |
| 248 | |
| 249 | if (string == NULL) |
| 250 | return NULL; |
| 251 | |
| 252 | cp = string; |
| 253 | |
| 254 | /* Skip white spaces. */ |
| 255 | while (isspace ((int) *cp) && *cp != '\0') |
| 256 | cp++; |
| 257 | |
| 258 | /* Return if there is only white spaces */ |
| 259 | if (*cp == '\0') |
| 260 | return NULL; |
| 261 | |
| 262 | if (*cp == '!' || *cp == '#') |
| 263 | return NULL; |
| 264 | |
| 265 | /* Prepare return vector. */ |
| 266 | strvec = vector_init (VECTOR_MIN_SIZE); |
| 267 | |
| 268 | /* Copy each command piece and set into vector. */ |
| 269 | while (1) |
| 270 | { |
| 271 | start = cp; |
| 272 | while (!(isspace ((int) *cp) || *cp == '\r' || *cp == '\n') && |
| 273 | *cp != '\0') |
| 274 | cp++; |
| 275 | strlen = cp - start; |
| 276 | token = XMALLOC (MTYPE_STRVEC, strlen + 1); |
| 277 | memcpy (token, start, strlen); |
| 278 | *(token + strlen) = '\0'; |
| 279 | vector_set (strvec, token); |
| 280 | |
| 281 | while ((isspace ((int) *cp) || *cp == '\n' || *cp == '\r') && |
| 282 | *cp != '\0') |
| 283 | cp++; |
| 284 | |
| 285 | if (*cp == '\0') |
| 286 | return strvec; |
| 287 | } |
| 288 | } |
| 289 | |
| 290 | /* Free allocated string vector. */ |
| 291 | void |
| 292 | cmd_free_strvec (vector v) |
| 293 | { |
hasso | 8c328f1 | 2004-10-05 21:01:23 +0000 | [diff] [blame] | 294 | unsigned int i; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 295 | char *cp; |
| 296 | |
| 297 | if (!v) |
| 298 | return; |
| 299 | |
paul | 55468c8 | 2005-03-14 20:19:01 +0000 | [diff] [blame] | 300 | for (i = 0; i < vector_active (v); i++) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 301 | if ((cp = vector_slot (v, i)) != NULL) |
| 302 | XFREE (MTYPE_STRVEC, cp); |
| 303 | |
| 304 | vector_free (v); |
| 305 | } |
| 306 | |
Christian Franke | cd40b32 | 2013-09-30 12:27:51 +0000 | [diff] [blame] | 307 | struct format_parser_state |
| 308 | { |
| 309 | vector topvect; /* Top level vector */ |
| 310 | vector intvect; /* Intermediate level vector, used when there's |
| 311 | * a multiple in a keyword. */ |
| 312 | vector curvect; /* current vector where read tokens should be |
| 313 | appended. */ |
| 314 | |
| 315 | const char *string; /* pointer to command string, not modified */ |
| 316 | const char *cp; /* pointer in command string, moved along while |
| 317 | parsing */ |
| 318 | const char *dp; /* pointer in description string, moved along while |
| 319 | parsing */ |
| 320 | |
| 321 | int in_keyword; /* flag to remember if we are in a keyword group */ |
| 322 | int in_multiple; /* flag to remember if we are in a multiple group */ |
| 323 | int just_read_word; /* flag to remember if the last thing we red was a |
| 324 | * real word and not some abstract token */ |
| 325 | }; |
| 326 | |
| 327 | static void |
| 328 | format_parser_error(struct format_parser_state *state, const char *message) |
| 329 | { |
| 330 | int offset = state->cp - state->string + 1; |
| 331 | |
| 332 | fprintf(stderr, "\nError parsing command: \"%s\"\n", state->string); |
| 333 | fprintf(stderr, " %*c\n", offset, '^'); |
| 334 | fprintf(stderr, "%s at offset %d.\n", message, offset); |
| 335 | fprintf(stderr, "This is a programming error. Check your DEFUNs etc.\n"); |
| 336 | exit(1); |
| 337 | } |
| 338 | |
ajs | 274a4a4 | 2004-12-07 15:39:31 +0000 | [diff] [blame] | 339 | static char * |
Christian Franke | cd40b32 | 2013-09-30 12:27:51 +0000 | [diff] [blame] | 340 | format_parser_desc_str(struct format_parser_state *state) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 341 | { |
hasso | 6ad96ea | 2004-10-07 19:33:46 +0000 | [diff] [blame] | 342 | const char *cp, *start; |
| 343 | char *token; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 344 | int strlen; |
Christian Franke | cd40b32 | 2013-09-30 12:27:51 +0000 | [diff] [blame] | 345 | |
| 346 | cp = state->dp; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 347 | |
| 348 | if (cp == NULL) |
| 349 | return NULL; |
| 350 | |
| 351 | /* Skip white spaces. */ |
| 352 | while (isspace ((int) *cp) && *cp != '\0') |
| 353 | cp++; |
| 354 | |
| 355 | /* Return if there is only white spaces */ |
| 356 | if (*cp == '\0') |
| 357 | return NULL; |
| 358 | |
| 359 | start = cp; |
| 360 | |
| 361 | while (!(*cp == '\r' || *cp == '\n') && *cp != '\0') |
| 362 | cp++; |
| 363 | |
| 364 | strlen = cp - start; |
Christian Franke | cd40b32 | 2013-09-30 12:27:51 +0000 | [diff] [blame] | 365 | token = XMALLOC (MTYPE_CMD_TOKENS, strlen + 1); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 366 | memcpy (token, start, strlen); |
| 367 | *(token + strlen) = '\0'; |
| 368 | |
Christian Franke | cd40b32 | 2013-09-30 12:27:51 +0000 | [diff] [blame] | 369 | state->dp = cp; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 370 | |
| 371 | return token; |
| 372 | } |
| 373 | |
Christian Franke | cd40b32 | 2013-09-30 12:27:51 +0000 | [diff] [blame] | 374 | static void |
| 375 | format_parser_begin_keyword(struct format_parser_state *state) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 376 | { |
Christian Franke | cd40b32 | 2013-09-30 12:27:51 +0000 | [diff] [blame] | 377 | struct cmd_token *token; |
| 378 | vector keyword_vect; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 379 | |
Christian Franke | cd40b32 | 2013-09-30 12:27:51 +0000 | [diff] [blame] | 380 | if (state->in_keyword |
| 381 | || state->in_multiple) |
| 382 | format_parser_error(state, "Unexpected '{'"); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 383 | |
Christian Franke | cd40b32 | 2013-09-30 12:27:51 +0000 | [diff] [blame] | 384 | state->cp++; |
| 385 | state->in_keyword = 1; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 386 | |
Christian Franke | cd40b32 | 2013-09-30 12:27:51 +0000 | [diff] [blame] | 387 | token = XCALLOC(MTYPE_CMD_TOKENS, sizeof(*token)); |
| 388 | token->type = TOKEN_KEYWORD; |
| 389 | token->keyword = vector_init(VECTOR_MIN_SIZE); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 390 | |
Christian Franke | cd40b32 | 2013-09-30 12:27:51 +0000 | [diff] [blame] | 391 | keyword_vect = vector_init(VECTOR_MIN_SIZE); |
| 392 | vector_set(token->keyword, keyword_vect); |
| 393 | |
| 394 | vector_set(state->curvect, token); |
| 395 | state->curvect = keyword_vect; |
| 396 | } |
| 397 | |
| 398 | static void |
| 399 | format_parser_begin_multiple(struct format_parser_state *state) |
| 400 | { |
| 401 | struct cmd_token *token; |
| 402 | |
| 403 | if (state->in_keyword == 1) |
| 404 | format_parser_error(state, "Keyword starting with '('"); |
| 405 | |
| 406 | if (state->in_multiple) |
| 407 | format_parser_error(state, "Nested group"); |
| 408 | |
| 409 | state->cp++; |
| 410 | state->in_multiple = 1; |
| 411 | state->just_read_word = 0; |
| 412 | |
| 413 | token = XCALLOC(MTYPE_CMD_TOKENS, sizeof(*token)); |
| 414 | token->type = TOKEN_MULTIPLE; |
| 415 | token->multiple = vector_init(VECTOR_MIN_SIZE); |
| 416 | |
| 417 | vector_set(state->curvect, token); |
| 418 | if (state->curvect != state->topvect) |
| 419 | state->intvect = state->curvect; |
| 420 | state->curvect = token->multiple; |
| 421 | } |
| 422 | |
| 423 | static void |
| 424 | format_parser_end_keyword(struct format_parser_state *state) |
| 425 | { |
| 426 | if (state->in_multiple |
| 427 | || !state->in_keyword) |
| 428 | format_parser_error(state, "Unexpected '}'"); |
| 429 | |
| 430 | if (state->in_keyword == 1) |
| 431 | format_parser_error(state, "Empty keyword group"); |
| 432 | |
| 433 | state->cp++; |
| 434 | state->in_keyword = 0; |
| 435 | state->curvect = state->topvect; |
| 436 | } |
| 437 | |
| 438 | static void |
| 439 | format_parser_end_multiple(struct format_parser_state *state) |
| 440 | { |
| 441 | char *dummy; |
| 442 | |
| 443 | if (!state->in_multiple) |
| 444 | format_parser_error(state, "Unepexted ')'"); |
| 445 | |
| 446 | if (vector_active(state->curvect) == 0) |
| 447 | format_parser_error(state, "Empty multiple section"); |
| 448 | |
| 449 | if (!state->just_read_word) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 450 | { |
Christian Franke | cd40b32 | 2013-09-30 12:27:51 +0000 | [diff] [blame] | 451 | /* There are constructions like |
| 452 | * 'show ip ospf database ... (self-originate|)' |
| 453 | * in use. |
| 454 | * The old parser reads a description string for the |
| 455 | * word '' between |) which will never match. |
| 456 | * Simulate this behvaior by dropping the next desc |
| 457 | * string in such a case. */ |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 458 | |
Christian Franke | cd40b32 | 2013-09-30 12:27:51 +0000 | [diff] [blame] | 459 | dummy = format_parser_desc_str(state); |
| 460 | XFREE(MTYPE_CMD_TOKENS, dummy); |
| 461 | } |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 462 | |
Christian Franke | cd40b32 | 2013-09-30 12:27:51 +0000 | [diff] [blame] | 463 | state->cp++; |
| 464 | state->in_multiple = 0; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 465 | |
Christian Franke | cd40b32 | 2013-09-30 12:27:51 +0000 | [diff] [blame] | 466 | if (state->intvect) |
| 467 | state->curvect = state->intvect; |
| 468 | else |
| 469 | state->curvect = state->topvect; |
| 470 | } |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 471 | |
Christian Franke | cd40b32 | 2013-09-30 12:27:51 +0000 | [diff] [blame] | 472 | static void |
| 473 | format_parser_handle_pipe(struct format_parser_state *state) |
| 474 | { |
| 475 | struct cmd_token *keyword_token; |
| 476 | vector keyword_vect; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 477 | |
Christian Franke | cd40b32 | 2013-09-30 12:27:51 +0000 | [diff] [blame] | 478 | if (state->in_multiple) |
| 479 | { |
| 480 | state->just_read_word = 0; |
| 481 | state->cp++; |
| 482 | } |
| 483 | else if (state->in_keyword) |
| 484 | { |
| 485 | state->in_keyword = 1; |
| 486 | state->cp++; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 487 | |
Christian Franke | cd40b32 | 2013-09-30 12:27:51 +0000 | [diff] [blame] | 488 | keyword_token = vector_slot(state->topvect, |
| 489 | vector_active(state->topvect) - 1); |
| 490 | keyword_vect = vector_init(VECTOR_MIN_SIZE); |
| 491 | vector_set(keyword_token->keyword, keyword_vect); |
| 492 | state->curvect = keyword_vect; |
| 493 | } |
| 494 | else |
| 495 | { |
| 496 | format_parser_error(state, "Unexpected '|'"); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 497 | } |
| 498 | } |
| 499 | |
Christian Franke | cd40b32 | 2013-09-30 12:27:51 +0000 | [diff] [blame] | 500 | static void |
| 501 | format_parser_read_word(struct format_parser_state *state) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 502 | { |
Christian Franke | cd40b32 | 2013-09-30 12:27:51 +0000 | [diff] [blame] | 503 | const char *start; |
| 504 | int len; |
| 505 | char *cmd; |
| 506 | struct cmd_token *token; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 507 | |
Christian Franke | cd40b32 | 2013-09-30 12:27:51 +0000 | [diff] [blame] | 508 | start = state->cp; |
| 509 | |
| 510 | while (state->cp[0] != '\0' |
| 511 | && !strchr("\r\n(){}|", state->cp[0]) |
| 512 | && !isspace((int)state->cp[0])) |
| 513 | state->cp++; |
| 514 | |
| 515 | len = state->cp - start; |
| 516 | cmd = XMALLOC(MTYPE_CMD_TOKENS, len + 1); |
| 517 | memcpy(cmd, start, len); |
| 518 | cmd[len] = '\0'; |
| 519 | |
| 520 | token = XCALLOC(MTYPE_CMD_TOKENS, sizeof(*token)); |
| 521 | token->type = TOKEN_TERMINAL; |
David Lamparter | 1416293 | 2015-05-12 17:18:04 +0200 | [diff] [blame] | 522 | if (strcmp (cmd, "A.B.C.D") == 0) |
David Lamparter | 10bac80 | 2015-05-05 11:10:20 +0200 | [diff] [blame] | 523 | token->terminal = TERMINAL_IPV4; |
| 524 | else if (strcmp (cmd, "A.B.C.D/M") == 0) |
| 525 | token->terminal = TERMINAL_IPV4_PREFIX; |
| 526 | else if (strcmp (cmd, "X:X::X:X") == 0) |
| 527 | token->terminal = TERMINAL_IPV6; |
| 528 | else if (strcmp (cmd, "X:X::X:X/M") == 0) |
| 529 | token->terminal = TERMINAL_IPV6_PREFIX; |
David Lamparter | 1416293 | 2015-05-12 17:18:04 +0200 | [diff] [blame] | 530 | else if (cmd[0] == '[') |
| 531 | token->terminal = TERMINAL_OPTION; |
| 532 | else if (cmd[0] == '.') |
| 533 | token->terminal = TERMINAL_VARARG; |
| 534 | else if (cmd[0] == '<') |
| 535 | token->terminal = TERMINAL_RANGE; |
| 536 | else if (cmd[0] >= 'A' && cmd[0] <= 'Z') |
| 537 | token->terminal = TERMINAL_VARIABLE; |
David Lamparter | 10bac80 | 2015-05-05 11:10:20 +0200 | [diff] [blame] | 538 | else |
| 539 | token->terminal = TERMINAL_LITERAL; |
| 540 | |
Christian Franke | cd40b32 | 2013-09-30 12:27:51 +0000 | [diff] [blame] | 541 | token->cmd = cmd; |
| 542 | token->desc = format_parser_desc_str(state); |
| 543 | vector_set(state->curvect, token); |
| 544 | |
| 545 | if (state->in_keyword == 1) |
| 546 | state->in_keyword = 2; |
| 547 | |
| 548 | state->just_read_word = 1; |
| 549 | } |
| 550 | |
| 551 | /** |
| 552 | * Parse a given command format string and build a tree of tokens from |
| 553 | * it that is suitable to be used by the command subsystem. |
| 554 | * |
| 555 | * @param string Command format string. |
| 556 | * @param descstr Description string. |
| 557 | * @return A vector of struct cmd_token representing the given command, |
| 558 | * or NULL on error. |
| 559 | */ |
| 560 | static vector |
| 561 | cmd_parse_format(const char *string, const char *descstr) |
| 562 | { |
| 563 | struct format_parser_state state; |
| 564 | |
| 565 | if (string == NULL) |
| 566 | return NULL; |
| 567 | |
| 568 | memset(&state, 0, sizeof(state)); |
| 569 | state.topvect = state.curvect = vector_init(VECTOR_MIN_SIZE); |
| 570 | state.cp = state.string = string; |
| 571 | state.dp = descstr; |
| 572 | |
| 573 | while (1) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 574 | { |
Christian Franke | cd40b32 | 2013-09-30 12:27:51 +0000 | [diff] [blame] | 575 | while (isspace((int)state.cp[0]) && state.cp[0] != '\0') |
| 576 | state.cp++; |
| 577 | |
| 578 | switch (state.cp[0]) |
| 579 | { |
| 580 | case '\0': |
| 581 | if (state.in_keyword |
| 582 | || state.in_multiple) |
| 583 | format_parser_error(&state, "Unclosed group/keyword"); |
| 584 | return state.topvect; |
| 585 | case '{': |
| 586 | format_parser_begin_keyword(&state); |
| 587 | break; |
| 588 | case '(': |
| 589 | format_parser_begin_multiple(&state); |
| 590 | break; |
| 591 | case '}': |
| 592 | format_parser_end_keyword(&state); |
| 593 | break; |
| 594 | case ')': |
| 595 | format_parser_end_multiple(&state); |
| 596 | break; |
| 597 | case '|': |
| 598 | format_parser_handle_pipe(&state); |
| 599 | break; |
| 600 | default: |
| 601 | format_parser_read_word(&state); |
| 602 | } |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 603 | } |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 604 | } |
| 605 | |
| 606 | /* Return prompt character of specified node. */ |
hasso | 8c328f1 | 2004-10-05 21:01:23 +0000 | [diff] [blame] | 607 | const char * |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 608 | cmd_prompt (enum node_type node) |
| 609 | { |
| 610 | struct cmd_node *cnode; |
| 611 | |
| 612 | cnode = vector_slot (cmdvec, node); |
| 613 | return cnode->prompt; |
| 614 | } |
| 615 | |
| 616 | /* Install a command into a node. */ |
| 617 | void |
| 618 | install_element (enum node_type ntype, struct cmd_element *cmd) |
| 619 | { |
| 620 | struct cmd_node *cnode; |
paul | eb820af | 2005-09-05 11:54:13 +0000 | [diff] [blame] | 621 | |
| 622 | /* cmd_init hasn't been called */ |
| 623 | if (!cmdvec) |
Paul Jakma | 9219366 | 2016-06-16 15:53:26 +0100 | [diff] [blame] | 624 | { |
| 625 | fprintf (stderr, "%s called before cmd_init, breakage likely\n", |
| 626 | __func__); |
| 627 | return; |
| 628 | } |
paul | eb820af | 2005-09-05 11:54:13 +0000 | [diff] [blame] | 629 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 630 | cnode = vector_slot (cmdvec, ntype); |
| 631 | |
| 632 | if (cnode == NULL) |
| 633 | { |
| 634 | fprintf (stderr, "Command node %d doesn't exist, please check it\n", |
| 635 | ntype); |
| 636 | exit (1); |
| 637 | } |
Paul Jakma | 9219366 | 2016-06-16 15:53:26 +0100 | [diff] [blame] | 638 | |
| 639 | if (hash_lookup (cnode->cmd_hash, cmd) != NULL) |
| 640 | { |
Nick Hilliard | 71e0ba7 | 2016-12-27 22:51:51 +0000 | [diff] [blame] | 641 | #ifdef DEV_BUILD |
Paul Jakma | 9219366 | 2016-06-16 15:53:26 +0100 | [diff] [blame] | 642 | fprintf (stderr, |
| 643 | "Multiple command installs to node %d of command:\n%s\n", |
| 644 | ntype, cmd->string); |
Nick Hilliard | 71e0ba7 | 2016-12-27 22:51:51 +0000 | [diff] [blame] | 645 | #endif |
Paul Jakma | 9219366 | 2016-06-16 15:53:26 +0100 | [diff] [blame] | 646 | return; |
| 647 | } |
| 648 | |
| 649 | assert (hash_get (cnode->cmd_hash, cmd, hash_alloc_intern)); |
| 650 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 651 | vector_set (cnode->cmd_vector, cmd); |
Christian Franke | cd40b32 | 2013-09-30 12:27:51 +0000 | [diff] [blame] | 652 | if (cmd->tokens == NULL) |
| 653 | cmd->tokens = cmd_parse_format(cmd->string, cmd->doc); |
Donald Sharp | b9ac2f3 | 2016-03-11 14:27:12 -0500 | [diff] [blame] | 654 | |
| 655 | if (ntype == VIEW_NODE) |
| 656 | install_element (ENABLE_NODE, cmd); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 657 | } |
| 658 | |
Stephen Hemminger | 2d362d1 | 2009-12-21 12:54:58 +0300 | [diff] [blame] | 659 | static const unsigned char itoa64[] = |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 660 | "./0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"; |
| 661 | |
ajs | 274a4a4 | 2004-12-07 15:39:31 +0000 | [diff] [blame] | 662 | static void |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 663 | to64(char *s, long v, int n) |
| 664 | { |
| 665 | while (--n >= 0) |
| 666 | { |
| 667 | *s++ = itoa64[v&0x3f]; |
| 668 | v >>= 6; |
| 669 | } |
| 670 | } |
| 671 | |
ajs | 274a4a4 | 2004-12-07 15:39:31 +0000 | [diff] [blame] | 672 | static char * |
| 673 | zencrypt (const char *passwd) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 674 | { |
| 675 | char salt[6]; |
| 676 | struct timeval tv; |
| 677 | char *crypt (const char *, const char *); |
| 678 | |
| 679 | gettimeofday(&tv,0); |
| 680 | |
| 681 | to64(&salt[0], random(), 3); |
| 682 | to64(&salt[3], tv.tv_usec, 3); |
| 683 | salt[5] = '\0'; |
| 684 | |
| 685 | return crypt (passwd, salt); |
| 686 | } |
| 687 | |
| 688 | /* This function write configuration of this host. */ |
ajs | 274a4a4 | 2004-12-07 15:39:31 +0000 | [diff] [blame] | 689 | static int |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 690 | config_write_host (struct vty *vty) |
| 691 | { |
| 692 | if (host.name) |
| 693 | vty_out (vty, "hostname %s%s", host.name, VTY_NEWLINE); |
| 694 | |
| 695 | if (host.encrypt) |
| 696 | { |
| 697 | if (host.password_encrypt) |
| 698 | vty_out (vty, "password 8 %s%s", host.password_encrypt, VTY_NEWLINE); |
| 699 | if (host.enable_encrypt) |
| 700 | vty_out (vty, "enable password 8 %s%s", host.enable_encrypt, VTY_NEWLINE); |
| 701 | } |
| 702 | else |
| 703 | { |
| 704 | if (host.password) |
| 705 | vty_out (vty, "password %s%s", host.password, VTY_NEWLINE); |
| 706 | if (host.enable) |
| 707 | vty_out (vty, "enable password %s%s", host.enable, VTY_NEWLINE); |
| 708 | } |
| 709 | |
ajs | 274a4a4 | 2004-12-07 15:39:31 +0000 | [diff] [blame] | 710 | if (zlog_default->default_lvl != LOG_DEBUG) |
ajs | 82146b8 | 2004-12-07 17:15:55 +0000 | [diff] [blame] | 711 | { |
| 712 | vty_out (vty, "! N.B. The 'log trap' command is deprecated.%s", |
| 713 | VTY_NEWLINE); |
| 714 | vty_out (vty, "log trap %s%s", |
| 715 | zlog_priority[zlog_default->default_lvl], VTY_NEWLINE); |
| 716 | } |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 717 | |
ajs | 274a4a4 | 2004-12-07 15:39:31 +0000 | [diff] [blame] | 718 | if (host.logfile && (zlog_default->maxlvl[ZLOG_DEST_FILE] != ZLOG_DISABLED)) |
paul | 12ab19f | 2003-07-26 06:14:55 +0000 | [diff] [blame] | 719 | { |
ajs | 274a4a4 | 2004-12-07 15:39:31 +0000 | [diff] [blame] | 720 | vty_out (vty, "log file %s", host.logfile); |
| 721 | if (zlog_default->maxlvl[ZLOG_DEST_FILE] != zlog_default->default_lvl) |
| 722 | vty_out (vty, " %s", |
| 723 | zlog_priority[zlog_default->maxlvl[ZLOG_DEST_FILE]]); |
paul | 12ab19f | 2003-07-26 06:14:55 +0000 | [diff] [blame] | 724 | vty_out (vty, "%s", VTY_NEWLINE); |
| 725 | } |
ajs | 274a4a4 | 2004-12-07 15:39:31 +0000 | [diff] [blame] | 726 | |
| 727 | if (zlog_default->maxlvl[ZLOG_DEST_STDOUT] != ZLOG_DISABLED) |
| 728 | { |
| 729 | vty_out (vty, "log stdout"); |
| 730 | if (zlog_default->maxlvl[ZLOG_DEST_STDOUT] != zlog_default->default_lvl) |
| 731 | vty_out (vty, " %s", |
| 732 | zlog_priority[zlog_default->maxlvl[ZLOG_DEST_STDOUT]]); |
| 733 | vty_out (vty, "%s", VTY_NEWLINE); |
| 734 | } |
| 735 | |
| 736 | if (zlog_default->maxlvl[ZLOG_DEST_MONITOR] == ZLOG_DISABLED) |
| 737 | vty_out(vty,"no log monitor%s",VTY_NEWLINE); |
| 738 | else if (zlog_default->maxlvl[ZLOG_DEST_MONITOR] != zlog_default->default_lvl) |
| 739 | vty_out(vty,"log monitor %s%s", |
| 740 | zlog_priority[zlog_default->maxlvl[ZLOG_DEST_MONITOR]],VTY_NEWLINE); |
| 741 | |
| 742 | if (zlog_default->maxlvl[ZLOG_DEST_SYSLOG] != ZLOG_DISABLED) |
| 743 | { |
| 744 | vty_out (vty, "log syslog"); |
| 745 | if (zlog_default->maxlvl[ZLOG_DEST_SYSLOG] != zlog_default->default_lvl) |
| 746 | vty_out (vty, " %s", |
| 747 | zlog_priority[zlog_default->maxlvl[ZLOG_DEST_SYSLOG]]); |
| 748 | vty_out (vty, "%s", VTY_NEWLINE); |
| 749 | } |
| 750 | |
| 751 | if (zlog_default->facility != LOG_DAEMON) |
| 752 | vty_out (vty, "log facility %s%s", |
| 753 | facility_name(zlog_default->facility), VTY_NEWLINE); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 754 | |
| 755 | if (zlog_default->record_priority == 1) |
| 756 | vty_out (vty, "log record-priority%s", VTY_NEWLINE); |
| 757 | |
Andrew J. Schorr | 1ed72e0 | 2007-04-28 22:14:10 +0000 | [diff] [blame] | 758 | if (zlog_default->timestamp_precision > 0) |
| 759 | vty_out (vty, "log timestamp precision %d%s", |
| 760 | zlog_default->timestamp_precision, VTY_NEWLINE); |
| 761 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 762 | if (host.advanced) |
| 763 | vty_out (vty, "service advanced-vty%s", VTY_NEWLINE); |
| 764 | |
| 765 | if (host.encrypt) |
| 766 | vty_out (vty, "service password-encryption%s", VTY_NEWLINE); |
| 767 | |
| 768 | if (host.lines >= 0) |
| 769 | vty_out (vty, "service terminal-length %d%s", host.lines, |
| 770 | VTY_NEWLINE); |
| 771 | |
paul | 3b0c5d9 | 2005-03-08 10:43:43 +0000 | [diff] [blame] | 772 | if (host.motdfile) |
| 773 | vty_out (vty, "banner motd file %s%s", host.motdfile, VTY_NEWLINE); |
| 774 | else if (! host.motd) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 775 | vty_out (vty, "no banner motd%s", VTY_NEWLINE); |
| 776 | |
| 777 | return 1; |
| 778 | } |
| 779 | |
| 780 | /* Utility function for getting command vector. */ |
ajs | 274a4a4 | 2004-12-07 15:39:31 +0000 | [diff] [blame] | 781 | static vector |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 782 | cmd_node_vector (vector v, enum node_type ntype) |
| 783 | { |
| 784 | struct cmd_node *cnode = vector_slot (v, ntype); |
| 785 | return cnode->cmd_vector; |
| 786 | } |
| 787 | |
ajs | 274a4a4 | 2004-12-07 15:39:31 +0000 | [diff] [blame] | 788 | #if 0 |
| 789 | /* Filter command vector by symbol. This function is not actually used; |
| 790 | * should it be deleted? */ |
| 791 | static int |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 792 | cmd_filter_by_symbol (char *command, char *symbol) |
| 793 | { |
| 794 | int i, lim; |
| 795 | |
| 796 | if (strcmp (symbol, "IPV4_ADDRESS") == 0) |
| 797 | { |
| 798 | i = 0; |
| 799 | lim = strlen (command); |
| 800 | while (i < lim) |
| 801 | { |
| 802 | if (! (isdigit ((int) command[i]) || command[i] == '.' || command[i] == '/')) |
| 803 | return 1; |
| 804 | i++; |
| 805 | } |
| 806 | return 0; |
| 807 | } |
| 808 | if (strcmp (symbol, "STRING") == 0) |
| 809 | { |
| 810 | i = 0; |
| 811 | lim = strlen (command); |
| 812 | while (i < lim) |
| 813 | { |
| 814 | if (! (isalpha ((int) command[i]) || command[i] == '_' || command[i] == '-')) |
| 815 | return 1; |
| 816 | i++; |
| 817 | } |
| 818 | return 0; |
| 819 | } |
| 820 | if (strcmp (symbol, "IFNAME") == 0) |
| 821 | { |
| 822 | i = 0; |
| 823 | lim = strlen (command); |
| 824 | while (i < lim) |
| 825 | { |
| 826 | if (! isalnum ((int) command[i])) |
| 827 | return 1; |
| 828 | i++; |
| 829 | } |
| 830 | return 0; |
| 831 | } |
| 832 | return 0; |
| 833 | } |
ajs | 274a4a4 | 2004-12-07 15:39:31 +0000 | [diff] [blame] | 834 | #endif |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 835 | |
| 836 | /* Completion match types. */ |
| 837 | enum match_type |
| 838 | { |
| 839 | no_match, |
| 840 | extend_match, |
| 841 | ipv4_prefix_match, |
| 842 | ipv4_match, |
| 843 | ipv6_prefix_match, |
| 844 | ipv6_match, |
| 845 | range_match, |
| 846 | vararg_match, |
| 847 | partly_match, |
| 848 | exact_match |
| 849 | }; |
| 850 | |
ajs | 274a4a4 | 2004-12-07 15:39:31 +0000 | [diff] [blame] | 851 | static enum match_type |
hasso | 8c328f1 | 2004-10-05 21:01:23 +0000 | [diff] [blame] | 852 | cmd_ipv4_match (const char *str) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 853 | { |
hasso | 8c328f1 | 2004-10-05 21:01:23 +0000 | [diff] [blame] | 854 | const char *sp; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 855 | int dots = 0, nums = 0; |
| 856 | char buf[4]; |
| 857 | |
| 858 | if (str == NULL) |
| 859 | return partly_match; |
| 860 | |
| 861 | for (;;) |
| 862 | { |
| 863 | memset (buf, 0, sizeof (buf)); |
| 864 | sp = str; |
| 865 | while (*str != '\0') |
| 866 | { |
| 867 | if (*str == '.') |
| 868 | { |
| 869 | if (dots >= 3) |
| 870 | return no_match; |
| 871 | |
| 872 | if (*(str + 1) == '.') |
| 873 | return no_match; |
| 874 | |
| 875 | if (*(str + 1) == '\0') |
| 876 | return partly_match; |
| 877 | |
| 878 | dots++; |
| 879 | break; |
| 880 | } |
| 881 | if (!isdigit ((int) *str)) |
| 882 | return no_match; |
| 883 | |
| 884 | str++; |
| 885 | } |
| 886 | |
| 887 | if (str - sp > 3) |
| 888 | return no_match; |
| 889 | |
| 890 | strncpy (buf, sp, str - sp); |
| 891 | if (atoi (buf) > 255) |
| 892 | return no_match; |
| 893 | |
| 894 | nums++; |
| 895 | |
| 896 | if (*str == '\0') |
| 897 | break; |
| 898 | |
| 899 | str++; |
| 900 | } |
| 901 | |
| 902 | if (nums < 4) |
| 903 | return partly_match; |
| 904 | |
| 905 | return exact_match; |
| 906 | } |
| 907 | |
ajs | 274a4a4 | 2004-12-07 15:39:31 +0000 | [diff] [blame] | 908 | static enum match_type |
hasso | 8c328f1 | 2004-10-05 21:01:23 +0000 | [diff] [blame] | 909 | cmd_ipv4_prefix_match (const char *str) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 910 | { |
hasso | 8c328f1 | 2004-10-05 21:01:23 +0000 | [diff] [blame] | 911 | const char *sp; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 912 | int dots = 0; |
| 913 | char buf[4]; |
| 914 | |
| 915 | if (str == NULL) |
| 916 | return partly_match; |
| 917 | |
| 918 | for (;;) |
| 919 | { |
| 920 | memset (buf, 0, sizeof (buf)); |
| 921 | sp = str; |
| 922 | while (*str != '\0' && *str != '/') |
| 923 | { |
| 924 | if (*str == '.') |
| 925 | { |
| 926 | if (dots == 3) |
| 927 | return no_match; |
| 928 | |
| 929 | if (*(str + 1) == '.' || *(str + 1) == '/') |
| 930 | return no_match; |
| 931 | |
| 932 | if (*(str + 1) == '\0') |
| 933 | return partly_match; |
| 934 | |
| 935 | dots++; |
| 936 | break; |
| 937 | } |
| 938 | |
| 939 | if (!isdigit ((int) *str)) |
| 940 | return no_match; |
| 941 | |
| 942 | str++; |
| 943 | } |
| 944 | |
| 945 | if (str - sp > 3) |
| 946 | return no_match; |
| 947 | |
| 948 | strncpy (buf, sp, str - sp); |
| 949 | if (atoi (buf) > 255) |
| 950 | return no_match; |
| 951 | |
| 952 | if (dots == 3) |
| 953 | { |
| 954 | if (*str == '/') |
| 955 | { |
| 956 | if (*(str + 1) == '\0') |
| 957 | return partly_match; |
| 958 | |
| 959 | str++; |
| 960 | break; |
| 961 | } |
| 962 | else if (*str == '\0') |
| 963 | return partly_match; |
| 964 | } |
| 965 | |
| 966 | if (*str == '\0') |
| 967 | return partly_match; |
| 968 | |
| 969 | str++; |
| 970 | } |
| 971 | |
| 972 | sp = str; |
| 973 | while (*str != '\0') |
| 974 | { |
| 975 | if (!isdigit ((int) *str)) |
| 976 | return no_match; |
| 977 | |
| 978 | str++; |
| 979 | } |
| 980 | |
| 981 | if (atoi (sp) > 32) |
| 982 | return no_match; |
| 983 | |
| 984 | return exact_match; |
| 985 | } |
| 986 | |
| 987 | #define IPV6_ADDR_STR "0123456789abcdefABCDEF:.%" |
| 988 | #define IPV6_PREFIX_STR "0123456789abcdefABCDEF:.%/" |
| 989 | #define STATE_START 1 |
| 990 | #define STATE_COLON 2 |
| 991 | #define STATE_DOUBLE 3 |
| 992 | #define STATE_ADDR 4 |
| 993 | #define STATE_DOT 5 |
| 994 | #define STATE_SLASH 6 |
| 995 | #define STATE_MASK 7 |
| 996 | |
paul | 22e0a9e | 2003-07-11 17:55:46 +0000 | [diff] [blame] | 997 | #ifdef HAVE_IPV6 |
| 998 | |
ajs | 274a4a4 | 2004-12-07 15:39:31 +0000 | [diff] [blame] | 999 | static enum match_type |
hasso | 8c328f1 | 2004-10-05 21:01:23 +0000 | [diff] [blame] | 1000 | cmd_ipv6_match (const char *str) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1001 | { |
hasso | 726f9b2 | 2003-05-25 21:04:54 +0000 | [diff] [blame] | 1002 | struct sockaddr_in6 sin6_dummy; |
| 1003 | int ret; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1004 | |
| 1005 | if (str == NULL) |
| 1006 | return partly_match; |
| 1007 | |
| 1008 | if (strspn (str, IPV6_ADDR_STR) != strlen (str)) |
| 1009 | return no_match; |
| 1010 | |
hasso | 726f9b2 | 2003-05-25 21:04:54 +0000 | [diff] [blame] | 1011 | /* use inet_pton that has a better support, |
| 1012 | * for example inet_pton can support the automatic addresses: |
| 1013 | * ::1.2.3.4 |
| 1014 | */ |
| 1015 | ret = inet_pton(AF_INET6, str, &sin6_dummy.sin6_addr); |
| 1016 | |
| 1017 | if (ret == 1) |
| 1018 | return exact_match; |
| 1019 | |
Roman Hoog Antink | 7c9c6ae | 2012-05-09 06:35:34 +0000 | [diff] [blame] | 1020 | return no_match; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1021 | } |
| 1022 | |
ajs | 274a4a4 | 2004-12-07 15:39:31 +0000 | [diff] [blame] | 1023 | static enum match_type |
hasso | 8c328f1 | 2004-10-05 21:01:23 +0000 | [diff] [blame] | 1024 | cmd_ipv6_prefix_match (const char *str) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1025 | { |
| 1026 | int state = STATE_START; |
| 1027 | int colons = 0, nums = 0, double_colon = 0; |
| 1028 | int mask; |
hasso | 8c328f1 | 2004-10-05 21:01:23 +0000 | [diff] [blame] | 1029 | const char *sp = NULL; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1030 | char *endptr = NULL; |
| 1031 | |
| 1032 | if (str == NULL) |
| 1033 | return partly_match; |
| 1034 | |
| 1035 | if (strspn (str, IPV6_PREFIX_STR) != strlen (str)) |
| 1036 | return no_match; |
| 1037 | |
| 1038 | while (*str != '\0' && state != STATE_MASK) |
| 1039 | { |
| 1040 | switch (state) |
| 1041 | { |
| 1042 | case STATE_START: |
| 1043 | if (*str == ':') |
| 1044 | { |
| 1045 | if (*(str + 1) != ':' && *(str + 1) != '\0') |
| 1046 | return no_match; |
| 1047 | colons--; |
| 1048 | state = STATE_COLON; |
| 1049 | } |
| 1050 | else |
| 1051 | { |
| 1052 | sp = str; |
| 1053 | state = STATE_ADDR; |
| 1054 | } |
| 1055 | |
| 1056 | continue; |
| 1057 | case STATE_COLON: |
| 1058 | colons++; |
| 1059 | if (*(str + 1) == '/') |
| 1060 | return no_match; |
| 1061 | else if (*(str + 1) == ':') |
| 1062 | state = STATE_DOUBLE; |
| 1063 | else |
| 1064 | { |
| 1065 | sp = str + 1; |
| 1066 | state = STATE_ADDR; |
| 1067 | } |
| 1068 | break; |
| 1069 | case STATE_DOUBLE: |
| 1070 | if (double_colon) |
| 1071 | return no_match; |
| 1072 | |
| 1073 | if (*(str + 1) == ':') |
| 1074 | return no_match; |
| 1075 | else |
| 1076 | { |
| 1077 | if (*(str + 1) != '\0' && *(str + 1) != '/') |
| 1078 | colons++; |
| 1079 | sp = str + 1; |
| 1080 | |
| 1081 | if (*(str + 1) == '/') |
| 1082 | state = STATE_SLASH; |
| 1083 | else |
| 1084 | state = STATE_ADDR; |
| 1085 | } |
| 1086 | |
| 1087 | double_colon++; |
| 1088 | nums += 1; |
| 1089 | break; |
| 1090 | case STATE_ADDR: |
| 1091 | if (*(str + 1) == ':' || *(str + 1) == '.' |
| 1092 | || *(str + 1) == '\0' || *(str + 1) == '/') |
| 1093 | { |
| 1094 | if (str - sp > 3) |
| 1095 | return no_match; |
| 1096 | |
| 1097 | for (; sp <= str; sp++) |
| 1098 | if (*sp == '/') |
| 1099 | return no_match; |
| 1100 | |
| 1101 | nums++; |
| 1102 | |
| 1103 | if (*(str + 1) == ':') |
| 1104 | state = STATE_COLON; |
| 1105 | else if (*(str + 1) == '.') |
David Lamparter | aa5cf24 | 2012-07-19 16:11:50 +0200 | [diff] [blame] | 1106 | { |
| 1107 | if (colons || double_colon) |
| 1108 | state = STATE_DOT; |
| 1109 | else |
| 1110 | return no_match; |
| 1111 | } |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1112 | else if (*(str + 1) == '/') |
| 1113 | state = STATE_SLASH; |
| 1114 | } |
| 1115 | break; |
| 1116 | case STATE_DOT: |
| 1117 | state = STATE_ADDR; |
| 1118 | break; |
| 1119 | case STATE_SLASH: |
| 1120 | if (*(str + 1) == '\0') |
| 1121 | return partly_match; |
| 1122 | |
| 1123 | state = STATE_MASK; |
| 1124 | break; |
| 1125 | default: |
| 1126 | break; |
| 1127 | } |
| 1128 | |
| 1129 | if (nums > 11) |
| 1130 | return no_match; |
| 1131 | |
| 1132 | if (colons > 7) |
| 1133 | return no_match; |
| 1134 | |
| 1135 | str++; |
| 1136 | } |
| 1137 | |
| 1138 | if (state < STATE_MASK) |
| 1139 | return partly_match; |
| 1140 | |
| 1141 | mask = strtol (str, &endptr, 10); |
| 1142 | if (*endptr != '\0') |
| 1143 | return no_match; |
| 1144 | |
| 1145 | if (mask < 0 || mask > 128) |
| 1146 | return no_match; |
| 1147 | |
| 1148 | /* I don't know why mask < 13 makes command match partly. |
| 1149 | Forgive me to make this comments. I Want to set static default route |
| 1150 | because of lack of function to originate default in ospf6d; sorry |
| 1151 | yasu |
| 1152 | if (mask < 13) |
| 1153 | return partly_match; |
| 1154 | */ |
| 1155 | |
| 1156 | return exact_match; |
| 1157 | } |
| 1158 | |
paul | 22e0a9e | 2003-07-11 17:55:46 +0000 | [diff] [blame] | 1159 | #endif /* HAVE_IPV6 */ |
| 1160 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1161 | #define DECIMAL_STRLEN_MAX 10 |
| 1162 | |
ajs | 274a4a4 | 2004-12-07 15:39:31 +0000 | [diff] [blame] | 1163 | static int |
hasso | 8c328f1 | 2004-10-05 21:01:23 +0000 | [diff] [blame] | 1164 | cmd_range_match (const char *range, const char *str) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1165 | { |
| 1166 | char *p; |
| 1167 | char buf[DECIMAL_STRLEN_MAX + 1]; |
| 1168 | char *endptr = NULL; |
| 1169 | unsigned long min, max, val; |
| 1170 | |
| 1171 | if (str == NULL) |
| 1172 | return 1; |
| 1173 | |
| 1174 | val = strtoul (str, &endptr, 10); |
| 1175 | if (*endptr != '\0') |
| 1176 | return 0; |
| 1177 | |
| 1178 | range++; |
| 1179 | p = strchr (range, '-'); |
| 1180 | if (p == NULL) |
| 1181 | return 0; |
| 1182 | if (p - range > DECIMAL_STRLEN_MAX) |
| 1183 | return 0; |
| 1184 | strncpy (buf, range, p - range); |
| 1185 | buf[p - range] = '\0'; |
| 1186 | min = strtoul (buf, &endptr, 10); |
| 1187 | if (*endptr != '\0') |
| 1188 | return 0; |
| 1189 | |
| 1190 | range = p + 1; |
| 1191 | p = strchr (range, '>'); |
| 1192 | if (p == NULL) |
| 1193 | return 0; |
| 1194 | if (p - range > DECIMAL_STRLEN_MAX) |
| 1195 | return 0; |
| 1196 | strncpy (buf, range, p - range); |
| 1197 | buf[p - range] = '\0'; |
| 1198 | max = strtoul (buf, &endptr, 10); |
| 1199 | if (*endptr != '\0') |
| 1200 | return 0; |
| 1201 | |
| 1202 | if (val < min || val > max) |
| 1203 | return 0; |
| 1204 | |
| 1205 | return 1; |
| 1206 | } |
| 1207 | |
ajs | 274a4a4 | 2004-12-07 15:39:31 +0000 | [diff] [blame] | 1208 | static enum match_type |
Christian Franke | cd40b32 | 2013-09-30 12:27:51 +0000 | [diff] [blame] | 1209 | cmd_word_match(struct cmd_token *token, |
| 1210 | enum filter_type filter, |
| 1211 | const char *word) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1212 | { |
hasso | 8c328f1 | 2004-10-05 21:01:23 +0000 | [diff] [blame] | 1213 | const char *str; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1214 | enum match_type match_type; |
paul | 909a215 | 2005-03-14 17:41:45 +0000 | [diff] [blame] | 1215 | |
Christian Franke | cd40b32 | 2013-09-30 12:27:51 +0000 | [diff] [blame] | 1216 | str = token->cmd; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1217 | |
Christian Franke | cd40b32 | 2013-09-30 12:27:51 +0000 | [diff] [blame] | 1218 | if (filter == FILTER_RELAXED) |
| 1219 | if (!word || !strlen(word)) |
| 1220 | return partly_match; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1221 | |
Christian Franke | cd40b32 | 2013-09-30 12:27:51 +0000 | [diff] [blame] | 1222 | if (!word) |
| 1223 | return no_match; |
paul | 909a215 | 2005-03-14 17:41:45 +0000 | [diff] [blame] | 1224 | |
David Lamparter | 10bac80 | 2015-05-05 11:10:20 +0200 | [diff] [blame] | 1225 | switch (token->terminal) |
Christian Franke | cd40b32 | 2013-09-30 12:27:51 +0000 | [diff] [blame] | 1226 | { |
David Lamparter | 10bac80 | 2015-05-05 11:10:20 +0200 | [diff] [blame] | 1227 | case TERMINAL_VARARG: |
| 1228 | return vararg_match; |
| 1229 | |
| 1230 | case TERMINAL_RANGE: |
| 1231 | if (cmd_range_match(str, word)) |
| 1232 | return range_match; |
| 1233 | break; |
| 1234 | |
| 1235 | case TERMINAL_IPV6: |
| 1236 | match_type = cmd_ipv6_match(word); |
| 1237 | if ((filter == FILTER_RELAXED && match_type != no_match) |
Christian Franke | cd40b32 | 2013-09-30 12:27:51 +0000 | [diff] [blame] | 1238 | || (filter == FILTER_STRICT && match_type == exact_match)) |
David Lamparter | 10bac80 | 2015-05-05 11:10:20 +0200 | [diff] [blame] | 1239 | return ipv6_match; |
| 1240 | break; |
| 1241 | |
| 1242 | case TERMINAL_IPV6_PREFIX: |
| 1243 | match_type = cmd_ipv6_prefix_match(word); |
| 1244 | if ((filter == FILTER_RELAXED && match_type != no_match) |
| 1245 | || (filter == FILTER_STRICT && match_type == exact_match)) |
| 1246 | return ipv6_prefix_match; |
| 1247 | break; |
| 1248 | |
| 1249 | case TERMINAL_IPV4: |
| 1250 | match_type = cmd_ipv4_match(word); |
| 1251 | if ((filter == FILTER_RELAXED && match_type != no_match) |
| 1252 | || (filter == FILTER_STRICT && match_type == exact_match)) |
| 1253 | return ipv4_match; |
| 1254 | break; |
| 1255 | |
| 1256 | case TERMINAL_IPV4_PREFIX: |
| 1257 | match_type = cmd_ipv4_prefix_match(word); |
| 1258 | if ((filter == FILTER_RELAXED && match_type != no_match) |
| 1259 | || (filter == FILTER_STRICT && match_type == exact_match)) |
| 1260 | return ipv4_prefix_match; |
| 1261 | break; |
| 1262 | |
| 1263 | case TERMINAL_OPTION: |
| 1264 | case TERMINAL_VARIABLE: |
| 1265 | return extend_match; |
| 1266 | |
| 1267 | case TERMINAL_LITERAL: |
| 1268 | if (filter == FILTER_RELAXED && !strncmp(str, word, strlen(word))) |
| 1269 | { |
| 1270 | if (!strcmp(str, word)) |
| 1271 | return exact_match; |
| 1272 | return partly_match; |
| 1273 | } |
| 1274 | if (filter == FILTER_STRICT && !strcmp(str, word)) |
| 1275 | return exact_match; |
| 1276 | break; |
| 1277 | |
| 1278 | default: |
| 1279 | assert (0); |
Christian Franke | cd40b32 | 2013-09-30 12:27:51 +0000 | [diff] [blame] | 1280 | } |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1281 | |
Christian Franke | cd40b32 | 2013-09-30 12:27:51 +0000 | [diff] [blame] | 1282 | return no_match; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1283 | } |
| 1284 | |
Christian Franke | cd40b32 | 2013-09-30 12:27:51 +0000 | [diff] [blame] | 1285 | struct cmd_matcher |
| 1286 | { |
| 1287 | struct cmd_element *cmd; /* The command element the matcher is using */ |
| 1288 | enum filter_type filter; /* Whether to use strict or relaxed matching */ |
| 1289 | vector vline; /* The tokenized commandline which is to be matched */ |
| 1290 | unsigned int index; /* The index up to which matching should be done */ |
| 1291 | |
| 1292 | /* If set, construct a list of matches at the position given by index */ |
| 1293 | enum match_type *match_type; |
| 1294 | vector *match; |
| 1295 | |
| 1296 | unsigned int word_index; /* iterating over vline */ |
| 1297 | }; |
| 1298 | |
| 1299 | static int |
| 1300 | push_argument(int *argc, const char **argv, const char *arg) |
| 1301 | { |
| 1302 | if (!arg || !strlen(arg)) |
| 1303 | arg = NULL; |
| 1304 | |
| 1305 | if (!argc || !argv) |
| 1306 | return 0; |
| 1307 | |
| 1308 | if (*argc >= CMD_ARGC_MAX) |
| 1309 | return -1; |
| 1310 | |
| 1311 | argv[(*argc)++] = arg; |
| 1312 | return 0; |
| 1313 | } |
| 1314 | |
| 1315 | static void |
| 1316 | cmd_matcher_record_match(struct cmd_matcher *matcher, |
| 1317 | enum match_type match_type, |
| 1318 | struct cmd_token *token) |
| 1319 | { |
| 1320 | if (matcher->word_index != matcher->index) |
| 1321 | return; |
| 1322 | |
| 1323 | if (matcher->match) |
| 1324 | { |
| 1325 | if (!*matcher->match) |
| 1326 | *matcher->match = vector_init(VECTOR_MIN_SIZE); |
| 1327 | vector_set(*matcher->match, token); |
| 1328 | } |
| 1329 | |
| 1330 | if (matcher->match_type) |
| 1331 | { |
| 1332 | if (match_type > *matcher->match_type) |
| 1333 | *matcher->match_type = match_type; |
| 1334 | } |
| 1335 | } |
| 1336 | |
| 1337 | static int |
| 1338 | cmd_matcher_words_left(struct cmd_matcher *matcher) |
| 1339 | { |
| 1340 | return matcher->word_index < vector_active(matcher->vline); |
| 1341 | } |
| 1342 | |
| 1343 | static const char* |
| 1344 | cmd_matcher_get_word(struct cmd_matcher *matcher) |
| 1345 | { |
| 1346 | assert(cmd_matcher_words_left(matcher)); |
| 1347 | |
| 1348 | return vector_slot(matcher->vline, matcher->word_index); |
| 1349 | } |
| 1350 | |
| 1351 | static enum matcher_rv |
| 1352 | cmd_matcher_match_terminal(struct cmd_matcher *matcher, |
| 1353 | struct cmd_token *token, |
| 1354 | int *argc, const char **argv) |
| 1355 | { |
| 1356 | const char *word; |
| 1357 | enum match_type word_match; |
| 1358 | |
| 1359 | assert(token->type == TOKEN_TERMINAL); |
| 1360 | |
| 1361 | if (!cmd_matcher_words_left(matcher)) |
| 1362 | { |
David Lamparter | 10bac80 | 2015-05-05 11:10:20 +0200 | [diff] [blame] | 1363 | if (token->terminal == TERMINAL_OPTION) |
Christian Franke | cd40b32 | 2013-09-30 12:27:51 +0000 | [diff] [blame] | 1364 | return MATCHER_OK; /* missing optional args are NOT pushed as NULL */ |
| 1365 | else |
| 1366 | return MATCHER_INCOMPLETE; |
| 1367 | } |
| 1368 | |
| 1369 | word = cmd_matcher_get_word(matcher); |
| 1370 | word_match = cmd_word_match(token, matcher->filter, word); |
| 1371 | if (word_match == no_match) |
| 1372 | return MATCHER_NO_MATCH; |
| 1373 | |
| 1374 | /* We have to record the input word as argument if it matched |
| 1375 | * against a variable. */ |
David Lamparter | 1416293 | 2015-05-12 17:18:04 +0200 | [diff] [blame] | 1376 | if (TERMINAL_RECORD (token->terminal)) |
Christian Franke | cd40b32 | 2013-09-30 12:27:51 +0000 | [diff] [blame] | 1377 | { |
| 1378 | if (push_argument(argc, argv, word)) |
| 1379 | return MATCHER_EXCEED_ARGC_MAX; |
| 1380 | } |
| 1381 | |
| 1382 | cmd_matcher_record_match(matcher, word_match, token); |
| 1383 | |
| 1384 | matcher->word_index++; |
| 1385 | |
| 1386 | /* A vararg token should consume all left over words as arguments */ |
David Lamparter | 10bac80 | 2015-05-05 11:10:20 +0200 | [diff] [blame] | 1387 | if (token->terminal == TERMINAL_VARARG) |
Christian Franke | cd40b32 | 2013-09-30 12:27:51 +0000 | [diff] [blame] | 1388 | while (cmd_matcher_words_left(matcher)) |
| 1389 | { |
| 1390 | word = cmd_matcher_get_word(matcher); |
| 1391 | if (word && strlen(word)) |
| 1392 | push_argument(argc, argv, word); |
| 1393 | matcher->word_index++; |
| 1394 | } |
| 1395 | |
| 1396 | return MATCHER_OK; |
| 1397 | } |
| 1398 | |
| 1399 | static enum matcher_rv |
| 1400 | cmd_matcher_match_multiple(struct cmd_matcher *matcher, |
| 1401 | struct cmd_token *token, |
| 1402 | int *argc, const char **argv) |
| 1403 | { |
| 1404 | enum match_type multiple_match; |
| 1405 | unsigned int multiple_index; |
| 1406 | const char *word; |
David Lamparter | ab90fc0 | 2015-03-03 09:07:25 +0100 | [diff] [blame] | 1407 | const char *arg = NULL; |
Christian Franke | cd40b32 | 2013-09-30 12:27:51 +0000 | [diff] [blame] | 1408 | struct cmd_token *word_token; |
| 1409 | enum match_type word_match; |
| 1410 | |
| 1411 | assert(token->type == TOKEN_MULTIPLE); |
| 1412 | |
| 1413 | multiple_match = no_match; |
| 1414 | |
| 1415 | if (!cmd_matcher_words_left(matcher)) |
| 1416 | return MATCHER_INCOMPLETE; |
| 1417 | |
| 1418 | word = cmd_matcher_get_word(matcher); |
| 1419 | for (multiple_index = 0; |
| 1420 | multiple_index < vector_active(token->multiple); |
| 1421 | multiple_index++) |
| 1422 | { |
| 1423 | word_token = vector_slot(token->multiple, multiple_index); |
| 1424 | |
| 1425 | word_match = cmd_word_match(word_token, matcher->filter, word); |
| 1426 | if (word_match == no_match) |
| 1427 | continue; |
| 1428 | |
| 1429 | cmd_matcher_record_match(matcher, word_match, word_token); |
| 1430 | |
| 1431 | if (word_match > multiple_match) |
| 1432 | { |
| 1433 | multiple_match = word_match; |
| 1434 | arg = word; |
| 1435 | } |
| 1436 | /* To mimic the behavior of the old command implementation, we |
| 1437 | * tolerate any ambiguities here :/ */ |
| 1438 | } |
| 1439 | |
| 1440 | matcher->word_index++; |
| 1441 | |
| 1442 | if (multiple_match == no_match) |
| 1443 | return MATCHER_NO_MATCH; |
| 1444 | |
| 1445 | if (push_argument(argc, argv, arg)) |
| 1446 | return MATCHER_EXCEED_ARGC_MAX; |
| 1447 | |
| 1448 | return MATCHER_OK; |
| 1449 | } |
| 1450 | |
| 1451 | static enum matcher_rv |
| 1452 | cmd_matcher_read_keywords(struct cmd_matcher *matcher, |
| 1453 | struct cmd_token *token, |
| 1454 | vector args_vector) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1455 | { |
hasso | 8c328f1 | 2004-10-05 21:01:23 +0000 | [diff] [blame] | 1456 | unsigned int i; |
Christian Franke | cd40b32 | 2013-09-30 12:27:51 +0000 | [diff] [blame] | 1457 | unsigned long keyword_mask; |
| 1458 | unsigned int keyword_found; |
| 1459 | enum match_type keyword_match; |
| 1460 | enum match_type word_match; |
| 1461 | vector keyword_vector; |
| 1462 | struct cmd_token *word_token; |
| 1463 | const char *word; |
| 1464 | int keyword_argc; |
| 1465 | const char **keyword_argv; |
Paul Jakma | 7aa9dce | 2014-09-19 14:42:23 +0100 | [diff] [blame] | 1466 | enum matcher_rv rv = MATCHER_NO_MATCH; |
Christian Franke | cd40b32 | 2013-09-30 12:27:51 +0000 | [diff] [blame] | 1467 | |
| 1468 | keyword_mask = 0; |
| 1469 | while (1) |
| 1470 | { |
| 1471 | if (!cmd_matcher_words_left(matcher)) |
| 1472 | return MATCHER_OK; |
| 1473 | |
| 1474 | word = cmd_matcher_get_word(matcher); |
| 1475 | |
| 1476 | keyword_found = -1; |
| 1477 | keyword_match = no_match; |
| 1478 | for (i = 0; i < vector_active(token->keyword); i++) |
| 1479 | { |
| 1480 | if (keyword_mask & (1 << i)) |
| 1481 | continue; |
| 1482 | |
| 1483 | keyword_vector = vector_slot(token->keyword, i); |
| 1484 | word_token = vector_slot(keyword_vector, 0); |
| 1485 | |
| 1486 | word_match = cmd_word_match(word_token, matcher->filter, word); |
| 1487 | if (word_match == no_match) |
| 1488 | continue; |
| 1489 | |
| 1490 | cmd_matcher_record_match(matcher, word_match, word_token); |
| 1491 | |
| 1492 | if (word_match > keyword_match) |
| 1493 | { |
| 1494 | keyword_match = word_match; |
| 1495 | keyword_found = i; |
| 1496 | } |
| 1497 | else if (word_match == keyword_match) |
| 1498 | { |
| 1499 | if (matcher->word_index != matcher->index || args_vector) |
| 1500 | return MATCHER_AMBIGUOUS; |
| 1501 | } |
| 1502 | } |
| 1503 | |
| 1504 | if (keyword_found == (unsigned int)-1) |
| 1505 | return MATCHER_NO_MATCH; |
| 1506 | |
| 1507 | matcher->word_index++; |
| 1508 | |
| 1509 | if (matcher->word_index > matcher->index) |
| 1510 | return MATCHER_OK; |
| 1511 | |
| 1512 | keyword_mask |= (1 << keyword_found); |
| 1513 | |
| 1514 | if (args_vector) |
| 1515 | { |
| 1516 | keyword_argc = 0; |
| 1517 | keyword_argv = XMALLOC(MTYPE_TMP, (CMD_ARGC_MAX + 1) * sizeof(char*)); |
| 1518 | /* We use -1 as a marker for unused fields as NULL might be a valid value */ |
| 1519 | for (i = 0; i < CMD_ARGC_MAX + 1; i++) |
| 1520 | keyword_argv[i] = (void*)-1; |
| 1521 | vector_set_index(args_vector, keyword_found, keyword_argv); |
| 1522 | } |
| 1523 | else |
| 1524 | { |
| 1525 | keyword_argv = NULL; |
| 1526 | } |
| 1527 | |
| 1528 | keyword_vector = vector_slot(token->keyword, keyword_found); |
| 1529 | /* the keyword itself is at 0. We are only interested in the arguments, |
| 1530 | * so start counting at 1. */ |
| 1531 | for (i = 1; i < vector_active(keyword_vector); i++) |
| 1532 | { |
| 1533 | word_token = vector_slot(keyword_vector, i); |
| 1534 | |
| 1535 | switch (word_token->type) |
| 1536 | { |
| 1537 | case TOKEN_TERMINAL: |
| 1538 | rv = cmd_matcher_match_terminal(matcher, word_token, |
| 1539 | &keyword_argc, keyword_argv); |
| 1540 | break; |
| 1541 | case TOKEN_MULTIPLE: |
| 1542 | rv = cmd_matcher_match_multiple(matcher, word_token, |
| 1543 | &keyword_argc, keyword_argv); |
| 1544 | break; |
| 1545 | case TOKEN_KEYWORD: |
| 1546 | assert(!"Keywords should never be nested."); |
| 1547 | break; |
| 1548 | } |
| 1549 | |
| 1550 | if (MATCHER_ERROR(rv)) |
| 1551 | return rv; |
| 1552 | |
| 1553 | if (matcher->word_index > matcher->index) |
| 1554 | return MATCHER_OK; |
| 1555 | } |
| 1556 | } |
| 1557 | /* not reached */ |
| 1558 | } |
| 1559 | |
| 1560 | static enum matcher_rv |
| 1561 | cmd_matcher_build_keyword_args(struct cmd_matcher *matcher, |
| 1562 | struct cmd_token *token, |
| 1563 | int *argc, const char **argv, |
| 1564 | vector keyword_args_vector) |
| 1565 | { |
| 1566 | unsigned int i, j; |
| 1567 | const char **keyword_args; |
| 1568 | vector keyword_vector; |
| 1569 | struct cmd_token *word_token; |
| 1570 | const char *arg; |
| 1571 | enum matcher_rv rv; |
| 1572 | |
| 1573 | rv = MATCHER_OK; |
| 1574 | |
| 1575 | if (keyword_args_vector == NULL) |
| 1576 | return rv; |
| 1577 | |
| 1578 | for (i = 0; i < vector_active(token->keyword); i++) |
| 1579 | { |
| 1580 | keyword_vector = vector_slot(token->keyword, i); |
| 1581 | keyword_args = vector_lookup(keyword_args_vector, i); |
| 1582 | |
| 1583 | if (vector_active(keyword_vector) == 1) |
| 1584 | { |
| 1585 | /* this is a keyword without arguments */ |
| 1586 | if (keyword_args) |
| 1587 | { |
| 1588 | word_token = vector_slot(keyword_vector, 0); |
| 1589 | arg = word_token->cmd; |
| 1590 | } |
| 1591 | else |
| 1592 | { |
| 1593 | arg = NULL; |
| 1594 | } |
| 1595 | |
| 1596 | if (push_argument(argc, argv, arg)) |
| 1597 | rv = MATCHER_EXCEED_ARGC_MAX; |
| 1598 | } |
| 1599 | else |
| 1600 | { |
| 1601 | /* this is a keyword with arguments */ |
| 1602 | if (keyword_args) |
| 1603 | { |
| 1604 | /* the keyword was present, so just fill in the arguments */ |
| 1605 | for (j = 0; keyword_args[j] != (void*)-1; j++) |
| 1606 | if (push_argument(argc, argv, keyword_args[j])) |
| 1607 | rv = MATCHER_EXCEED_ARGC_MAX; |
| 1608 | XFREE(MTYPE_TMP, keyword_args); |
| 1609 | } |
| 1610 | else |
| 1611 | { |
| 1612 | /* the keyword was not present, insert NULL for the arguments |
| 1613 | * the keyword would have taken. */ |
| 1614 | for (j = 1; j < vector_active(keyword_vector); j++) |
| 1615 | { |
| 1616 | word_token = vector_slot(keyword_vector, j); |
| 1617 | if ((word_token->type == TOKEN_TERMINAL |
David Lamparter | 1416293 | 2015-05-12 17:18:04 +0200 | [diff] [blame] | 1618 | && TERMINAL_RECORD (word_token->terminal)) |
Christian Franke | cd40b32 | 2013-09-30 12:27:51 +0000 | [diff] [blame] | 1619 | || word_token->type == TOKEN_MULTIPLE) |
| 1620 | { |
| 1621 | if (push_argument(argc, argv, NULL)) |
| 1622 | rv = MATCHER_EXCEED_ARGC_MAX; |
| 1623 | } |
| 1624 | } |
| 1625 | } |
| 1626 | } |
| 1627 | } |
| 1628 | vector_free(keyword_args_vector); |
| 1629 | return rv; |
| 1630 | } |
| 1631 | |
| 1632 | static enum matcher_rv |
| 1633 | cmd_matcher_match_keyword(struct cmd_matcher *matcher, |
| 1634 | struct cmd_token *token, |
| 1635 | int *argc, const char **argv) |
| 1636 | { |
| 1637 | vector keyword_args_vector; |
| 1638 | enum matcher_rv reader_rv; |
| 1639 | enum matcher_rv builder_rv; |
| 1640 | |
| 1641 | assert(token->type == TOKEN_KEYWORD); |
| 1642 | |
| 1643 | if (argc && argv) |
| 1644 | keyword_args_vector = vector_init(VECTOR_MIN_SIZE); |
| 1645 | else |
| 1646 | keyword_args_vector = NULL; |
| 1647 | |
| 1648 | reader_rv = cmd_matcher_read_keywords(matcher, token, keyword_args_vector); |
| 1649 | builder_rv = cmd_matcher_build_keyword_args(matcher, token, argc, |
| 1650 | argv, keyword_args_vector); |
| 1651 | /* keyword_args_vector is consumed by cmd_matcher_build_keyword_args */ |
| 1652 | |
| 1653 | if (!MATCHER_ERROR(reader_rv) && MATCHER_ERROR(builder_rv)) |
| 1654 | return builder_rv; |
| 1655 | |
| 1656 | return reader_rv; |
| 1657 | } |
| 1658 | |
| 1659 | static void |
| 1660 | cmd_matcher_init(struct cmd_matcher *matcher, |
| 1661 | struct cmd_element *cmd, |
| 1662 | enum filter_type filter, |
| 1663 | vector vline, |
| 1664 | unsigned int index, |
| 1665 | enum match_type *match_type, |
| 1666 | vector *match) |
| 1667 | { |
| 1668 | memset(matcher, 0, sizeof(*matcher)); |
| 1669 | |
| 1670 | matcher->cmd = cmd; |
| 1671 | matcher->filter = filter; |
| 1672 | matcher->vline = vline; |
| 1673 | matcher->index = index; |
| 1674 | |
| 1675 | matcher->match_type = match_type; |
| 1676 | if (matcher->match_type) |
| 1677 | *matcher->match_type = no_match; |
| 1678 | matcher->match = match; |
| 1679 | |
| 1680 | matcher->word_index = 0; |
| 1681 | } |
| 1682 | |
| 1683 | static enum matcher_rv |
| 1684 | cmd_element_match(struct cmd_element *cmd_element, |
| 1685 | enum filter_type filter, |
| 1686 | vector vline, |
| 1687 | unsigned int index, |
| 1688 | enum match_type *match_type, |
| 1689 | vector *match, |
| 1690 | int *argc, |
| 1691 | const char **argv) |
| 1692 | { |
| 1693 | struct cmd_matcher matcher; |
| 1694 | unsigned int token_index; |
Paul Jakma | 7aa9dce | 2014-09-19 14:42:23 +0100 | [diff] [blame] | 1695 | enum matcher_rv rv = MATCHER_NO_MATCH; |
Christian Franke | cd40b32 | 2013-09-30 12:27:51 +0000 | [diff] [blame] | 1696 | |
| 1697 | cmd_matcher_init(&matcher, cmd_element, filter, |
| 1698 | vline, index, match_type, match); |
| 1699 | |
| 1700 | if (argc != NULL) |
| 1701 | *argc = 0; |
| 1702 | |
| 1703 | for (token_index = 0; |
| 1704 | token_index < vector_active(cmd_element->tokens); |
| 1705 | token_index++) |
| 1706 | { |
| 1707 | struct cmd_token *token = vector_slot(cmd_element->tokens, token_index); |
| 1708 | |
| 1709 | switch (token->type) |
| 1710 | { |
| 1711 | case TOKEN_TERMINAL: |
| 1712 | rv = cmd_matcher_match_terminal(&matcher, token, argc, argv); |
| 1713 | break; |
| 1714 | case TOKEN_MULTIPLE: |
| 1715 | rv = cmd_matcher_match_multiple(&matcher, token, argc, argv); |
| 1716 | break; |
| 1717 | case TOKEN_KEYWORD: |
| 1718 | rv = cmd_matcher_match_keyword(&matcher, token, argc, argv); |
| 1719 | } |
| 1720 | |
| 1721 | if (MATCHER_ERROR(rv)) |
| 1722 | return rv; |
| 1723 | |
| 1724 | if (matcher.word_index > index) |
| 1725 | return MATCHER_OK; |
| 1726 | } |
| 1727 | |
| 1728 | /* return MATCHER_COMPLETE if all words were consumed */ |
| 1729 | if (matcher.word_index >= vector_active(vline)) |
| 1730 | return MATCHER_COMPLETE; |
| 1731 | |
| 1732 | /* return MATCHER_COMPLETE also if only an empty word is left. */ |
| 1733 | if (matcher.word_index == vector_active(vline) - 1 |
| 1734 | && (!vector_slot(vline, matcher.word_index) |
| 1735 | || !strlen((char*)vector_slot(vline, matcher.word_index)))) |
| 1736 | return MATCHER_COMPLETE; |
| 1737 | |
| 1738 | return MATCHER_NO_MATCH; /* command is too long to match */ |
| 1739 | } |
| 1740 | |
| 1741 | /** |
| 1742 | * Filter a given vector of commands against a given commandline and |
| 1743 | * calculate possible completions. |
| 1744 | * |
| 1745 | * @param commands A vector of struct cmd_element*. Commands that don't |
| 1746 | * match against the given command line will be overwritten |
| 1747 | * with NULL in that vector. |
| 1748 | * @param filter Either FILTER_RELAXED or FILTER_STRICT. This basically |
| 1749 | * determines how incomplete commands are handled, compare with |
| 1750 | * cmd_word_match for details. |
| 1751 | * @param vline A vector of char* containing the tokenized commandline. |
| 1752 | * @param index Only match up to the given token of the commandline. |
| 1753 | * @param match_type Record the type of the best match here. |
| 1754 | * @param matches Record the matches here. For each cmd_element in the commands |
| 1755 | * vector, a match vector will be created in the matches vector. |
| 1756 | * That vector will contain all struct command_token* of the |
| 1757 | * cmd_element which matched against the given vline at the given |
| 1758 | * index. |
| 1759 | * @return A code specifying if an error occured. If all went right, it's |
| 1760 | * CMD_SUCCESS. |
| 1761 | */ |
| 1762 | static int |
| 1763 | cmd_vector_filter(vector commands, |
| 1764 | enum filter_type filter, |
| 1765 | vector vline, |
| 1766 | unsigned int index, |
| 1767 | enum match_type *match_type, |
| 1768 | vector *matches) |
| 1769 | { |
| 1770 | unsigned int i; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1771 | struct cmd_element *cmd_element; |
Christian Franke | cd40b32 | 2013-09-30 12:27:51 +0000 | [diff] [blame] | 1772 | enum match_type best_match; |
| 1773 | enum match_type element_match; |
| 1774 | enum matcher_rv matcher_rv; |
paul | 909a215 | 2005-03-14 17:41:45 +0000 | [diff] [blame] | 1775 | |
Christian Franke | cd40b32 | 2013-09-30 12:27:51 +0000 | [diff] [blame] | 1776 | best_match = no_match; |
| 1777 | *matches = vector_init(VECTOR_MIN_SIZE); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1778 | |
Christian Franke | cd40b32 | 2013-09-30 12:27:51 +0000 | [diff] [blame] | 1779 | for (i = 0; i < vector_active (commands); i++) |
| 1780 | if ((cmd_element = vector_slot (commands, i)) != NULL) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1781 | { |
Christian Franke | cd40b32 | 2013-09-30 12:27:51 +0000 | [diff] [blame] | 1782 | vector_set_index(*matches, i, NULL); |
| 1783 | matcher_rv = cmd_element_match(cmd_element, filter, |
| 1784 | vline, index, |
| 1785 | &element_match, |
| 1786 | (vector*)&vector_slot(*matches, i), |
| 1787 | NULL, NULL); |
| 1788 | if (MATCHER_ERROR(matcher_rv)) |
| 1789 | { |
| 1790 | vector_slot(commands, i) = NULL; |
| 1791 | if (matcher_rv == MATCHER_AMBIGUOUS) |
| 1792 | return CMD_ERR_AMBIGUOUS; |
| 1793 | if (matcher_rv == MATCHER_EXCEED_ARGC_MAX) |
| 1794 | return CMD_ERR_EXEED_ARGC_MAX; |
| 1795 | } |
| 1796 | else if (element_match > best_match) |
| 1797 | { |
| 1798 | best_match = element_match; |
| 1799 | } |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1800 | } |
Christian Franke | cd40b32 | 2013-09-30 12:27:51 +0000 | [diff] [blame] | 1801 | *match_type = best_match; |
| 1802 | return CMD_SUCCESS; |
| 1803 | } |
| 1804 | |
| 1805 | /** |
| 1806 | * Check whether a given commandline is complete if used for a specific |
| 1807 | * cmd_element. |
| 1808 | * |
| 1809 | * @param cmd_element A cmd_element against which the commandline should be |
| 1810 | * checked. |
| 1811 | * @param vline The tokenized commandline. |
| 1812 | * @return 1 if the given commandline is complete, 0 otherwise. |
| 1813 | */ |
| 1814 | static int |
| 1815 | cmd_is_complete(struct cmd_element *cmd_element, |
| 1816 | vector vline) |
| 1817 | { |
| 1818 | enum matcher_rv rv; |
| 1819 | |
| 1820 | rv = cmd_element_match(cmd_element, |
| 1821 | FILTER_RELAXED, |
| 1822 | vline, -1, |
| 1823 | NULL, NULL, |
| 1824 | NULL, NULL); |
| 1825 | return (rv == MATCHER_COMPLETE); |
| 1826 | } |
| 1827 | |
| 1828 | /** |
| 1829 | * Parse a given commandline and construct a list of arguments for the |
| 1830 | * given command_element. |
| 1831 | * |
| 1832 | * @param cmd_element The cmd_element for which we want to construct arguments. |
| 1833 | * @param vline The tokenized commandline. |
| 1834 | * @param argc Where to store the argument count. |
| 1835 | * @param argv Where to store the argument list. Should be at least |
| 1836 | * CMD_ARGC_MAX elements long. |
| 1837 | * @return CMD_SUCCESS if everything went alright, an error otherwise. |
| 1838 | */ |
| 1839 | static int |
| 1840 | cmd_parse(struct cmd_element *cmd_element, |
| 1841 | vector vline, |
| 1842 | int *argc, const char **argv) |
| 1843 | { |
| 1844 | enum matcher_rv rv = cmd_element_match(cmd_element, |
| 1845 | FILTER_RELAXED, |
| 1846 | vline, -1, |
| 1847 | NULL, NULL, |
| 1848 | argc, argv); |
| 1849 | switch (rv) |
| 1850 | { |
| 1851 | case MATCHER_COMPLETE: |
| 1852 | return CMD_SUCCESS; |
| 1853 | |
| 1854 | case MATCHER_NO_MATCH: |
| 1855 | return CMD_ERR_NO_MATCH; |
| 1856 | |
| 1857 | case MATCHER_AMBIGUOUS: |
| 1858 | return CMD_ERR_AMBIGUOUS; |
| 1859 | |
| 1860 | case MATCHER_EXCEED_ARGC_MAX: |
| 1861 | return CMD_ERR_EXEED_ARGC_MAX; |
| 1862 | |
| 1863 | default: |
| 1864 | return CMD_ERR_INCOMPLETE; |
| 1865 | } |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1866 | } |
| 1867 | |
| 1868 | /* Check ambiguous match */ |
ajs | 274a4a4 | 2004-12-07 15:39:31 +0000 | [diff] [blame] | 1869 | static int |
Christian Franke | cd40b32 | 2013-09-30 12:27:51 +0000 | [diff] [blame] | 1870 | is_cmd_ambiguous (vector cmd_vector, |
| 1871 | const char *command, |
| 1872 | vector matches, |
| 1873 | enum match_type type) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1874 | { |
hasso | 8c328f1 | 2004-10-05 21:01:23 +0000 | [diff] [blame] | 1875 | unsigned int i; |
| 1876 | unsigned int j; |
| 1877 | const char *str = NULL; |
hasso | 8c328f1 | 2004-10-05 21:01:23 +0000 | [diff] [blame] | 1878 | const char *matched = NULL; |
Christian Franke | cd40b32 | 2013-09-30 12:27:51 +0000 | [diff] [blame] | 1879 | vector match_vector; |
| 1880 | struct cmd_token *cmd_token; |
paul | 909a215 | 2005-03-14 17:41:45 +0000 | [diff] [blame] | 1881 | |
Christian Franke | cd40b32 | 2013-09-30 12:27:51 +0000 | [diff] [blame] | 1882 | if (command == NULL) |
| 1883 | command = ""; |
| 1884 | |
| 1885 | for (i = 0; i < vector_active (matches); i++) |
| 1886 | if ((match_vector = vector_slot (matches, i)) != NULL) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1887 | { |
| 1888 | int match = 0; |
| 1889 | |
Christian Franke | cd40b32 | 2013-09-30 12:27:51 +0000 | [diff] [blame] | 1890 | for (j = 0; j < vector_active (match_vector); j++) |
| 1891 | if ((cmd_token = vector_slot (match_vector, j)) != NULL) |
paul | 909a215 | 2005-03-14 17:41:45 +0000 | [diff] [blame] | 1892 | { |
| 1893 | enum match_type ret; |
Christian Franke | cd40b32 | 2013-09-30 12:27:51 +0000 | [diff] [blame] | 1894 | |
| 1895 | assert(cmd_token->type == TOKEN_TERMINAL); |
| 1896 | if (cmd_token->type != TOKEN_TERMINAL) |
| 1897 | continue; |
| 1898 | |
| 1899 | str = cmd_token->cmd; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1900 | |
paul | 909a215 | 2005-03-14 17:41:45 +0000 | [diff] [blame] | 1901 | switch (type) |
| 1902 | { |
| 1903 | case exact_match: |
David Lamparter | 1416293 | 2015-05-12 17:18:04 +0200 | [diff] [blame] | 1904 | if (!TERMINAL_RECORD (cmd_token->terminal) |
paul | 909a215 | 2005-03-14 17:41:45 +0000 | [diff] [blame] | 1905 | && strcmp (command, str) == 0) |
| 1906 | match++; |
| 1907 | break; |
| 1908 | case partly_match: |
David Lamparter | 1416293 | 2015-05-12 17:18:04 +0200 | [diff] [blame] | 1909 | if (!TERMINAL_RECORD (cmd_token->terminal) |
paul | 909a215 | 2005-03-14 17:41:45 +0000 | [diff] [blame] | 1910 | && strncmp (command, str, strlen (command)) == 0) |
| 1911 | { |
| 1912 | if (matched && strcmp (matched, str) != 0) |
| 1913 | return 1; /* There is ambiguous match. */ |
| 1914 | else |
| 1915 | matched = str; |
| 1916 | match++; |
| 1917 | } |
| 1918 | break; |
| 1919 | case range_match: |
| 1920 | if (cmd_range_match (str, command)) |
| 1921 | { |
| 1922 | if (matched && strcmp (matched, str) != 0) |
| 1923 | return 1; |
| 1924 | else |
| 1925 | matched = str; |
| 1926 | match++; |
| 1927 | } |
| 1928 | break; |
| 1929 | #ifdef HAVE_IPV6 |
| 1930 | case ipv6_match: |
David Lamparter | 10bac80 | 2015-05-05 11:10:20 +0200 | [diff] [blame] | 1931 | if (cmd_token->terminal == TERMINAL_IPV6) |
paul | 909a215 | 2005-03-14 17:41:45 +0000 | [diff] [blame] | 1932 | match++; |
| 1933 | break; |
| 1934 | case ipv6_prefix_match: |
| 1935 | if ((ret = cmd_ipv6_prefix_match (command)) != no_match) |
| 1936 | { |
| 1937 | if (ret == partly_match) |
| 1938 | return 2; /* There is incomplete match. */ |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1939 | |
paul | 909a215 | 2005-03-14 17:41:45 +0000 | [diff] [blame] | 1940 | match++; |
| 1941 | } |
| 1942 | break; |
| 1943 | #endif /* HAVE_IPV6 */ |
| 1944 | case ipv4_match: |
David Lamparter | 10bac80 | 2015-05-05 11:10:20 +0200 | [diff] [blame] | 1945 | if (cmd_token->terminal == TERMINAL_IPV4) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1946 | match++; |
paul | 909a215 | 2005-03-14 17:41:45 +0000 | [diff] [blame] | 1947 | break; |
| 1948 | case ipv4_prefix_match: |
| 1949 | if ((ret = cmd_ipv4_prefix_match (command)) != no_match) |
| 1950 | { |
| 1951 | if (ret == partly_match) |
| 1952 | return 2; /* There is incomplete match. */ |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1953 | |
paul | 909a215 | 2005-03-14 17:41:45 +0000 | [diff] [blame] | 1954 | match++; |
| 1955 | } |
| 1956 | break; |
| 1957 | case extend_match: |
David Lamparter | 1416293 | 2015-05-12 17:18:04 +0200 | [diff] [blame] | 1958 | if (TERMINAL_RECORD (cmd_token->terminal)) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1959 | match++; |
paul | 909a215 | 2005-03-14 17:41:45 +0000 | [diff] [blame] | 1960 | break; |
| 1961 | case no_match: |
| 1962 | default: |
| 1963 | break; |
| 1964 | } |
| 1965 | } |
| 1966 | if (!match) |
Christian Franke | cd40b32 | 2013-09-30 12:27:51 +0000 | [diff] [blame] | 1967 | vector_slot (cmd_vector, i) = NULL; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1968 | } |
| 1969 | return 0; |
| 1970 | } |
| 1971 | |
| 1972 | /* If src matches dst return dst string, otherwise return NULL */ |
ajs | 274a4a4 | 2004-12-07 15:39:31 +0000 | [diff] [blame] | 1973 | static const char * |
David Lamparter | 10bac80 | 2015-05-05 11:10:20 +0200 | [diff] [blame] | 1974 | cmd_entry_function (const char *src, struct cmd_token *token) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1975 | { |
David Lamparter | 10bac80 | 2015-05-05 11:10:20 +0200 | [diff] [blame] | 1976 | const char *dst = token->cmd; |
| 1977 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1978 | /* Skip variable arguments. */ |
David Lamparter | 1416293 | 2015-05-12 17:18:04 +0200 | [diff] [blame] | 1979 | if (TERMINAL_RECORD (token->terminal)) |
| 1980 | return NULL; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1981 | |
| 1982 | /* In case of 'command \t', given src is NULL string. */ |
| 1983 | if (src == NULL) |
| 1984 | return dst; |
| 1985 | |
| 1986 | /* Matched with input string. */ |
| 1987 | if (strncmp (src, dst, strlen (src)) == 0) |
| 1988 | return dst; |
| 1989 | |
| 1990 | return NULL; |
| 1991 | } |
| 1992 | |
| 1993 | /* If src matches dst return dst string, otherwise return NULL */ |
| 1994 | /* This version will return the dst string always if it is |
| 1995 | CMD_VARIABLE for '?' key processing */ |
ajs | 274a4a4 | 2004-12-07 15:39:31 +0000 | [diff] [blame] | 1996 | static const char * |
David Lamparter | 10bac80 | 2015-05-05 11:10:20 +0200 | [diff] [blame] | 1997 | cmd_entry_function_desc (const char *src, struct cmd_token *token) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1998 | { |
David Lamparter | 10bac80 | 2015-05-05 11:10:20 +0200 | [diff] [blame] | 1999 | const char *dst = token->cmd; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 2000 | |
David Lamparter | 10bac80 | 2015-05-05 11:10:20 +0200 | [diff] [blame] | 2001 | switch (token->terminal) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 2002 | { |
David Lamparter | 10bac80 | 2015-05-05 11:10:20 +0200 | [diff] [blame] | 2003 | case TERMINAL_VARARG: |
| 2004 | return dst; |
| 2005 | |
| 2006 | case TERMINAL_RANGE: |
| 2007 | if (cmd_range_match (dst, src)) |
| 2008 | return dst; |
| 2009 | else |
| 2010 | return NULL; |
| 2011 | |
| 2012 | case TERMINAL_IPV6: |
| 2013 | if (cmd_ipv6_match (src)) |
| 2014 | return dst; |
| 2015 | else |
| 2016 | return NULL; |
| 2017 | |
| 2018 | case TERMINAL_IPV6_PREFIX: |
| 2019 | if (cmd_ipv6_prefix_match (src)) |
| 2020 | return dst; |
| 2021 | else |
| 2022 | return NULL; |
| 2023 | |
| 2024 | case TERMINAL_IPV4: |
| 2025 | if (cmd_ipv4_match (src)) |
| 2026 | return dst; |
| 2027 | else |
| 2028 | return NULL; |
| 2029 | |
| 2030 | case TERMINAL_IPV4_PREFIX: |
| 2031 | if (cmd_ipv4_prefix_match (src)) |
| 2032 | return dst; |
| 2033 | else |
| 2034 | return NULL; |
| 2035 | |
| 2036 | /* Optional or variable commands always match on '?' */ |
| 2037 | case TERMINAL_OPTION: |
| 2038 | case TERMINAL_VARIABLE: |
| 2039 | return dst; |
| 2040 | |
| 2041 | case TERMINAL_LITERAL: |
| 2042 | /* In case of 'command \t', given src is NULL string. */ |
| 2043 | if (src == NULL) |
| 2044 | return dst; |
| 2045 | |
| 2046 | if (strncmp (src, dst, strlen (src)) == 0) |
| 2047 | return dst; |
| 2048 | else |
| 2049 | return NULL; |
| 2050 | |
| 2051 | default: |
| 2052 | assert(0); |
David Lamparter | f1fc327 | 2015-05-13 12:44:50 +0200 | [diff] [blame] | 2053 | return NULL; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 2054 | } |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 2055 | } |
| 2056 | |
Christian Franke | cd40b32 | 2013-09-30 12:27:51 +0000 | [diff] [blame] | 2057 | /** |
| 2058 | * Check whether a string is already present in a vector of strings. |
| 2059 | * @param v A vector of char*. |
| 2060 | * @param str A char*. |
| 2061 | * @return 0 if str is already present in the vector, 1 otherwise. |
| 2062 | */ |
ajs | 274a4a4 | 2004-12-07 15:39:31 +0000 | [diff] [blame] | 2063 | static int |
hasso | 8c328f1 | 2004-10-05 21:01:23 +0000 | [diff] [blame] | 2064 | cmd_unique_string (vector v, const char *str) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 2065 | { |
hasso | 8c328f1 | 2004-10-05 21:01:23 +0000 | [diff] [blame] | 2066 | unsigned int i; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 2067 | char *match; |
| 2068 | |
paul | 55468c8 | 2005-03-14 20:19:01 +0000 | [diff] [blame] | 2069 | for (i = 0; i < vector_active (v); i++) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 2070 | if ((match = vector_slot (v, i)) != NULL) |
| 2071 | if (strcmp (match, str) == 0) |
| 2072 | return 0; |
| 2073 | return 1; |
| 2074 | } |
| 2075 | |
Christian Franke | cd40b32 | 2013-09-30 12:27:51 +0000 | [diff] [blame] | 2076 | /** |
| 2077 | * Check whether a struct cmd_token matching a given string is already |
| 2078 | * present in a vector of struct cmd_token. |
| 2079 | * @param v A vector of struct cmd_token*. |
| 2080 | * @param str A char* which should be searched for. |
| 2081 | * @return 0 if there is a struct cmd_token* with its cmd matching str, |
| 2082 | * 1 otherwise. |
| 2083 | */ |
ajs | 274a4a4 | 2004-12-07 15:39:31 +0000 | [diff] [blame] | 2084 | static int |
hasso | 8c328f1 | 2004-10-05 21:01:23 +0000 | [diff] [blame] | 2085 | desc_unique_string (vector v, const char *str) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 2086 | { |
hasso | 8c328f1 | 2004-10-05 21:01:23 +0000 | [diff] [blame] | 2087 | unsigned int i; |
Christian Franke | cd40b32 | 2013-09-30 12:27:51 +0000 | [diff] [blame] | 2088 | struct cmd_token *token; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 2089 | |
paul | 55468c8 | 2005-03-14 20:19:01 +0000 | [diff] [blame] | 2090 | for (i = 0; i < vector_active (v); i++) |
Christian Franke | cd40b32 | 2013-09-30 12:27:51 +0000 | [diff] [blame] | 2091 | if ((token = vector_slot (v, i)) != NULL) |
| 2092 | if (strcmp (token->cmd, str) == 0) |
| 2093 | return 0; |
| 2094 | return 1; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 2095 | } |
| 2096 | |
ajs | 274a4a4 | 2004-12-07 15:39:31 +0000 | [diff] [blame] | 2097 | static int |
paul | b92938a | 2002-12-13 21:20:42 +0000 | [diff] [blame] | 2098 | cmd_try_do_shortcut (enum node_type node, char* first_word) { |
| 2099 | if ( first_word != NULL && |
| 2100 | node != AUTH_NODE && |
| 2101 | node != VIEW_NODE && |
| 2102 | node != AUTH_ENABLE_NODE && |
| 2103 | node != ENABLE_NODE && |
Paul Jakma | 62687ff | 2008-08-23 14:27:06 +0100 | [diff] [blame] | 2104 | node != RESTRICTED_NODE && |
paul | b92938a | 2002-12-13 21:20:42 +0000 | [diff] [blame] | 2105 | 0 == strcmp( "do", first_word ) ) |
| 2106 | return 1; |
| 2107 | return 0; |
| 2108 | } |
| 2109 | |
Christian Franke | cd40b32 | 2013-09-30 12:27:51 +0000 | [diff] [blame] | 2110 | static void |
| 2111 | cmd_matches_free(vector *matches) |
| 2112 | { |
| 2113 | unsigned int i; |
| 2114 | vector cmd_matches; |
| 2115 | |
| 2116 | for (i = 0; i < vector_active(*matches); i++) |
| 2117 | if ((cmd_matches = vector_slot(*matches, i)) != NULL) |
| 2118 | vector_free(cmd_matches); |
| 2119 | vector_free(*matches); |
| 2120 | *matches = NULL; |
| 2121 | } |
| 2122 | |
| 2123 | static int |
| 2124 | cmd_describe_cmp(const void *a, const void *b) |
| 2125 | { |
| 2126 | const struct cmd_token *first = *(struct cmd_token * const *)a; |
| 2127 | const struct cmd_token *second = *(struct cmd_token * const *)b; |
| 2128 | |
| 2129 | return strcmp(first->cmd, second->cmd); |
| 2130 | } |
| 2131 | |
| 2132 | static void |
| 2133 | cmd_describe_sort(vector matchvec) |
| 2134 | { |
| 2135 | qsort(matchvec->index, vector_active(matchvec), |
| 2136 | sizeof(void*), cmd_describe_cmp); |
| 2137 | } |
| 2138 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 2139 | /* '?' describe command support. */ |
ajs | 274a4a4 | 2004-12-07 15:39:31 +0000 | [diff] [blame] | 2140 | static vector |
paul | b92938a | 2002-12-13 21:20:42 +0000 | [diff] [blame] | 2141 | cmd_describe_command_real (vector vline, struct vty *vty, int *status) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 2142 | { |
paul | b896147 | 2005-03-14 17:35:52 +0000 | [diff] [blame] | 2143 | unsigned int i; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 2144 | vector cmd_vector; |
| 2145 | #define INIT_MATCHVEC_SIZE 10 |
| 2146 | vector matchvec; |
| 2147 | struct cmd_element *cmd_element; |
paul | b896147 | 2005-03-14 17:35:52 +0000 | [diff] [blame] | 2148 | unsigned int index; |
paul | 54aba54 | 2003-08-21 20:28:24 +0000 | [diff] [blame] | 2149 | int ret; |
| 2150 | enum match_type match; |
| 2151 | char *command; |
Christian Franke | cd40b32 | 2013-09-30 12:27:51 +0000 | [diff] [blame] | 2152 | vector matches = NULL; |
| 2153 | vector match_vector; |
Donald Sharp | f733280 | 2015-07-17 22:36:57 -0400 | [diff] [blame] | 2154 | uint32_t command_found = 0; |
| 2155 | const char *last_word; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 2156 | |
| 2157 | /* Set index. */ |
paul | 55468c8 | 2005-03-14 20:19:01 +0000 | [diff] [blame] | 2158 | if (vector_active (vline) == 0) |
paul | b896147 | 2005-03-14 17:35:52 +0000 | [diff] [blame] | 2159 | { |
| 2160 | *status = CMD_ERR_NO_MATCH; |
| 2161 | return NULL; |
| 2162 | } |
Christian Franke | cd40b32 | 2013-09-30 12:27:51 +0000 | [diff] [blame] | 2163 | |
| 2164 | index = vector_active (vline) - 1; |
| 2165 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 2166 | /* Make copy vector of current node's command vector. */ |
| 2167 | cmd_vector = vector_copy (cmd_node_vector (cmdvec, vty->node)); |
| 2168 | |
| 2169 | /* Prepare match vector */ |
| 2170 | matchvec = vector_init (INIT_MATCHVEC_SIZE); |
| 2171 | |
Christian Franke | cd40b32 | 2013-09-30 12:27:51 +0000 | [diff] [blame] | 2172 | /* Filter commands and build a list how they could possibly continue. */ |
| 2173 | for (i = 0; i <= index; i++) |
| 2174 | { |
| 2175 | command = vector_slot (vline, i); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 2176 | |
Christian Franke | cd40b32 | 2013-09-30 12:27:51 +0000 | [diff] [blame] | 2177 | if (matches) |
| 2178 | cmd_matches_free(&matches); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 2179 | |
Christian Franke | cd40b32 | 2013-09-30 12:27:51 +0000 | [diff] [blame] | 2180 | ret = cmd_vector_filter(cmd_vector, |
| 2181 | FILTER_RELAXED, |
| 2182 | vline, i, |
| 2183 | &match, |
| 2184 | &matches); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 2185 | |
Christian Franke | cd40b32 | 2013-09-30 12:27:51 +0000 | [diff] [blame] | 2186 | if (ret != CMD_SUCCESS) |
| 2187 | { |
| 2188 | vector_free (cmd_vector); |
| 2189 | vector_free (matchvec); |
| 2190 | cmd_matches_free(&matches); |
| 2191 | *status = ret; |
| 2192 | return NULL; |
| 2193 | } |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 2194 | |
Christian Franke | cd40b32 | 2013-09-30 12:27:51 +0000 | [diff] [blame] | 2195 | /* The last match may well be ambigious, so break here */ |
| 2196 | if (i == index) |
| 2197 | break; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 2198 | |
Christian Franke | cd40b32 | 2013-09-30 12:27:51 +0000 | [diff] [blame] | 2199 | if (match == vararg_match) |
| 2200 | { |
| 2201 | /* We found a vararg match - so we can throw out the current matches here |
| 2202 | * and don't need to continue checking the command input */ |
| 2203 | unsigned int j, k; |
| 2204 | |
| 2205 | for (j = 0; j < vector_active (matches); j++) |
| 2206 | if ((match_vector = vector_slot (matches, j)) != NULL) |
| 2207 | for (k = 0; k < vector_active (match_vector); k++) |
| 2208 | { |
| 2209 | struct cmd_token *token = vector_slot (match_vector, k); |
| 2210 | vector_set (matchvec, token); |
| 2211 | } |
| 2212 | |
| 2213 | *status = CMD_SUCCESS; |
| 2214 | vector_set(matchvec, &token_cr); |
| 2215 | vector_free (cmd_vector); |
| 2216 | cmd_matches_free(&matches); |
| 2217 | cmd_describe_sort(matchvec); |
| 2218 | return matchvec; |
| 2219 | } |
| 2220 | |
| 2221 | ret = is_cmd_ambiguous(cmd_vector, command, matches, match); |
| 2222 | if (ret == 1) |
| 2223 | { |
| 2224 | vector_free (cmd_vector); |
| 2225 | vector_free (matchvec); |
| 2226 | cmd_matches_free(&matches); |
| 2227 | *status = CMD_ERR_AMBIGUOUS; |
| 2228 | return NULL; |
| 2229 | } |
| 2230 | else if (ret == 2) |
| 2231 | { |
| 2232 | vector_free (cmd_vector); |
| 2233 | vector_free (matchvec); |
| 2234 | cmd_matches_free(&matches); |
| 2235 | *status = CMD_ERR_NO_MATCH; |
| 2236 | return NULL; |
| 2237 | } |
| 2238 | } |
paul | 54aba54 | 2003-08-21 20:28:24 +0000 | [diff] [blame] | 2239 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 2240 | /* Make description vector. */ |
Christian Franke | cd40b32 | 2013-09-30 12:27:51 +0000 | [diff] [blame] | 2241 | for (i = 0; i < vector_active (matches); i++) |
Donald Sharp | f733280 | 2015-07-17 22:36:57 -0400 | [diff] [blame] | 2242 | { |
| 2243 | if ((cmd_element = vector_slot (cmd_vector, i)) != NULL) |
| 2244 | { |
| 2245 | unsigned int j; |
| 2246 | vector vline_trimmed; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 2247 | |
Donald Sharp | f733280 | 2015-07-17 22:36:57 -0400 | [diff] [blame] | 2248 | command_found++; |
| 2249 | last_word = vector_slot(vline, vector_active(vline) - 1); |
| 2250 | if (last_word == NULL || !strlen(last_word)) |
| 2251 | { |
| 2252 | vline_trimmed = vector_copy(vline); |
| 2253 | vector_unset(vline_trimmed, vector_active(vline_trimmed) - 1); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 2254 | |
Donald Sharp | f733280 | 2015-07-17 22:36:57 -0400 | [diff] [blame] | 2255 | if (cmd_is_complete(cmd_element, vline_trimmed) |
| 2256 | && desc_unique_string(matchvec, command_cr)) |
| 2257 | { |
| 2258 | if (match != vararg_match) |
| 2259 | vector_set(matchvec, &token_cr); |
| 2260 | } |
Chris Caputo | 228da42 | 2009-07-18 05:44:03 +0000 | [diff] [blame] | 2261 | |
Donald Sharp | f733280 | 2015-07-17 22:36:57 -0400 | [diff] [blame] | 2262 | vector_free(vline_trimmed); |
| 2263 | } |
Christian Franke | cd40b32 | 2013-09-30 12:27:51 +0000 | [diff] [blame] | 2264 | |
Donald Sharp | f733280 | 2015-07-17 22:36:57 -0400 | [diff] [blame] | 2265 | match_vector = vector_slot (matches, i); |
| 2266 | if (match_vector) |
| 2267 | { |
| 2268 | for (j = 0; j < vector_active(match_vector); j++) |
| 2269 | { |
| 2270 | struct cmd_token *token = vector_slot(match_vector, j); |
| 2271 | const char *string; |
Christian Franke | cd40b32 | 2013-09-30 12:27:51 +0000 | [diff] [blame] | 2272 | |
Donald Sharp | f733280 | 2015-07-17 22:36:57 -0400 | [diff] [blame] | 2273 | string = cmd_entry_function_desc(command, token); |
| 2274 | if (string && desc_unique_string(matchvec, string)) |
| 2275 | vector_set(matchvec, token); |
| 2276 | } |
| 2277 | } |
| 2278 | } |
| 2279 | } |
| 2280 | |
| 2281 | /* |
| 2282 | * We can get into this situation when the command is complete |
| 2283 | * but the last part of the command is an optional piece of |
| 2284 | * the cli. |
| 2285 | */ |
| 2286 | last_word = vector_slot(vline, vector_active(vline) - 1); |
| 2287 | if (command_found == 0 && (last_word == NULL || !strlen(last_word))) |
| 2288 | vector_set(matchvec, &token_cr); |
| 2289 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 2290 | vector_free (cmd_vector); |
Christian Franke | cd40b32 | 2013-09-30 12:27:51 +0000 | [diff] [blame] | 2291 | cmd_matches_free(&matches); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 2292 | |
| 2293 | if (vector_slot (matchvec, 0) == NULL) |
| 2294 | { |
| 2295 | vector_free (matchvec); |
paul | 909a215 | 2005-03-14 17:41:45 +0000 | [diff] [blame] | 2296 | *status = CMD_ERR_NO_MATCH; |
Paul Jakma | 5fc6051 | 2006-05-12 23:24:09 +0000 | [diff] [blame] | 2297 | return NULL; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 2298 | } |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 2299 | |
Paul Jakma | 5fc6051 | 2006-05-12 23:24:09 +0000 | [diff] [blame] | 2300 | *status = CMD_SUCCESS; |
Christian Franke | cd40b32 | 2013-09-30 12:27:51 +0000 | [diff] [blame] | 2301 | cmd_describe_sort(matchvec); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 2302 | return matchvec; |
| 2303 | } |
| 2304 | |
paul | b92938a | 2002-12-13 21:20:42 +0000 | [diff] [blame] | 2305 | vector |
| 2306 | cmd_describe_command (vector vline, struct vty *vty, int *status) |
| 2307 | { |
| 2308 | vector ret; |
| 2309 | |
| 2310 | if ( cmd_try_do_shortcut(vty->node, vector_slot(vline, 0) ) ) |
| 2311 | { |
| 2312 | enum node_type onode; |
| 2313 | vector shifted_vline; |
hasso | 8c328f1 | 2004-10-05 21:01:23 +0000 | [diff] [blame] | 2314 | unsigned int index; |
paul | b92938a | 2002-12-13 21:20:42 +0000 | [diff] [blame] | 2315 | |
| 2316 | onode = vty->node; |
| 2317 | vty->node = ENABLE_NODE; |
| 2318 | /* We can try it on enable node, cos' the vty is authenticated */ |
| 2319 | |
| 2320 | shifted_vline = vector_init (vector_count(vline)); |
| 2321 | /* use memcpy? */ |
paul | 55468c8 | 2005-03-14 20:19:01 +0000 | [diff] [blame] | 2322 | for (index = 1; index < vector_active (vline); index++) |
paul | b92938a | 2002-12-13 21:20:42 +0000 | [diff] [blame] | 2323 | { |
| 2324 | vector_set_index (shifted_vline, index-1, vector_lookup(vline, index)); |
| 2325 | } |
| 2326 | |
| 2327 | ret = cmd_describe_command_real (shifted_vline, vty, status); |
| 2328 | |
| 2329 | vector_free(shifted_vline); |
| 2330 | vty->node = onode; |
| 2331 | return ret; |
| 2332 | } |
| 2333 | |
| 2334 | |
| 2335 | return cmd_describe_command_real (vline, vty, status); |
| 2336 | } |
| 2337 | |
| 2338 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 2339 | /* Check LCD of matched command. */ |
ajs | 274a4a4 | 2004-12-07 15:39:31 +0000 | [diff] [blame] | 2340 | static int |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 2341 | cmd_lcd (char **matched) |
| 2342 | { |
| 2343 | int i; |
| 2344 | int j; |
| 2345 | int lcd = -1; |
| 2346 | char *s1, *s2; |
| 2347 | char c1, c2; |
| 2348 | |
| 2349 | if (matched[0] == NULL || matched[1] == NULL) |
| 2350 | return 0; |
| 2351 | |
| 2352 | for (i = 1; matched[i] != NULL; i++) |
| 2353 | { |
| 2354 | s1 = matched[i - 1]; |
| 2355 | s2 = matched[i]; |
| 2356 | |
| 2357 | for (j = 0; (c1 = s1[j]) && (c2 = s2[j]); j++) |
| 2358 | if (c1 != c2) |
| 2359 | break; |
| 2360 | |
| 2361 | if (lcd < 0) |
| 2362 | lcd = j; |
| 2363 | else |
| 2364 | { |
| 2365 | if (lcd > j) |
| 2366 | lcd = j; |
| 2367 | } |
| 2368 | } |
| 2369 | return lcd; |
| 2370 | } |
| 2371 | |
Christian Franke | cd40b32 | 2013-09-30 12:27:51 +0000 | [diff] [blame] | 2372 | static int |
| 2373 | cmd_complete_cmp(const void *a, const void *b) |
| 2374 | { |
| 2375 | const char *first = *(char * const *)a; |
| 2376 | const char *second = *(char * const *)b; |
| 2377 | |
| 2378 | if (!first) |
| 2379 | { |
| 2380 | if (!second) |
| 2381 | return 0; |
| 2382 | return 1; |
| 2383 | } |
| 2384 | if (!second) |
| 2385 | return -1; |
| 2386 | |
| 2387 | return strcmp(first, second); |
| 2388 | } |
| 2389 | |
| 2390 | static void |
| 2391 | cmd_complete_sort(vector matchvec) |
| 2392 | { |
| 2393 | qsort(matchvec->index, vector_active(matchvec), |
| 2394 | sizeof(void*), cmd_complete_cmp); |
| 2395 | } |
| 2396 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 2397 | /* Command line completion support. */ |
ajs | 274a4a4 | 2004-12-07 15:39:31 +0000 | [diff] [blame] | 2398 | static char ** |
Lou Berger | 6729003 | 2016-01-12 13:41:46 -0500 | [diff] [blame] | 2399 | cmd_complete_command_real (vector vline, struct vty *vty, int *status, int islib) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 2400 | { |
paul | b896147 | 2005-03-14 17:35:52 +0000 | [diff] [blame] | 2401 | unsigned int i; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 2402 | vector cmd_vector = vector_copy (cmd_node_vector (cmdvec, vty->node)); |
| 2403 | #define INIT_MATCHVEC_SIZE 10 |
| 2404 | vector matchvec; |
paul | b896147 | 2005-03-14 17:35:52 +0000 | [diff] [blame] | 2405 | unsigned int index; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 2406 | char **match_str; |
Christian Franke | cd40b32 | 2013-09-30 12:27:51 +0000 | [diff] [blame] | 2407 | struct cmd_token *token; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 2408 | char *command; |
| 2409 | int lcd; |
Christian Franke | cd40b32 | 2013-09-30 12:27:51 +0000 | [diff] [blame] | 2410 | vector matches = NULL; |
| 2411 | vector match_vector; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 2412 | |
paul | 55468c8 | 2005-03-14 20:19:01 +0000 | [diff] [blame] | 2413 | if (vector_active (vline) == 0) |
paul | b896147 | 2005-03-14 17:35:52 +0000 | [diff] [blame] | 2414 | { |
Paul Jakma | d251996 | 2006-05-12 23:19:37 +0000 | [diff] [blame] | 2415 | vector_free (cmd_vector); |
paul | b896147 | 2005-03-14 17:35:52 +0000 | [diff] [blame] | 2416 | *status = CMD_ERR_NO_MATCH; |
| 2417 | return NULL; |
| 2418 | } |
| 2419 | else |
paul | 55468c8 | 2005-03-14 20:19:01 +0000 | [diff] [blame] | 2420 | index = vector_active (vline) - 1; |
paul | b896147 | 2005-03-14 17:35:52 +0000 | [diff] [blame] | 2421 | |
Christian Franke | cd40b32 | 2013-09-30 12:27:51 +0000 | [diff] [blame] | 2422 | /* First, filter by command string */ |
| 2423 | for (i = 0; i <= index; i++) |
| 2424 | { |
| 2425 | command = vector_slot (vline, i); |
| 2426 | enum match_type match; |
| 2427 | int ret; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 2428 | |
Christian Franke | cd40b32 | 2013-09-30 12:27:51 +0000 | [diff] [blame] | 2429 | if (matches) |
| 2430 | cmd_matches_free(&matches); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 2431 | |
Christian Franke | cd40b32 | 2013-09-30 12:27:51 +0000 | [diff] [blame] | 2432 | /* First try completion match, if there is exactly match return 1 */ |
| 2433 | ret = cmd_vector_filter(cmd_vector, |
| 2434 | FILTER_RELAXED, |
| 2435 | vline, i, |
| 2436 | &match, |
| 2437 | &matches); |
| 2438 | |
| 2439 | if (ret != CMD_SUCCESS) |
| 2440 | { |
| 2441 | vector_free(cmd_vector); |
| 2442 | cmd_matches_free(&matches); |
| 2443 | *status = ret; |
| 2444 | return NULL; |
| 2445 | } |
| 2446 | |
| 2447 | /* Break here - the completion mustn't be checked to be non-ambiguous */ |
| 2448 | if (i == index) |
| 2449 | break; |
| 2450 | |
| 2451 | /* If there is exact match then filter ambiguous match else check |
| 2452 | ambiguousness. */ |
| 2453 | ret = is_cmd_ambiguous (cmd_vector, command, matches, match); |
| 2454 | if (ret == 1) |
| 2455 | { |
| 2456 | vector_free (cmd_vector); |
| 2457 | cmd_matches_free(&matches); |
| 2458 | *status = CMD_ERR_AMBIGUOUS; |
| 2459 | return NULL; |
| 2460 | } |
| 2461 | /* |
paul | 909a215 | 2005-03-14 17:41:45 +0000 | [diff] [blame] | 2462 | else if (ret == 2) |
| 2463 | { |
| 2464 | vector_free (cmd_vector); |
Christian Franke | cd40b32 | 2013-09-30 12:27:51 +0000 | [diff] [blame] | 2465 | cmd_matches_free(&matches); |
paul | 909a215 | 2005-03-14 17:41:45 +0000 | [diff] [blame] | 2466 | *status = CMD_ERR_NO_MATCH; |
| 2467 | return NULL; |
| 2468 | } |
| 2469 | */ |
Christian Franke | cd40b32 | 2013-09-30 12:27:51 +0000 | [diff] [blame] | 2470 | } |
paul | 909a215 | 2005-03-14 17:41:45 +0000 | [diff] [blame] | 2471 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 2472 | /* Prepare match vector. */ |
| 2473 | matchvec = vector_init (INIT_MATCHVEC_SIZE); |
| 2474 | |
Christian Franke | cd40b32 | 2013-09-30 12:27:51 +0000 | [diff] [blame] | 2475 | /* Build the possible list of continuations into a list of completions */ |
| 2476 | for (i = 0; i < vector_active (matches); i++) |
| 2477 | if ((match_vector = vector_slot (matches, i))) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 2478 | { |
hasso | 8c328f1 | 2004-10-05 21:01:23 +0000 | [diff] [blame] | 2479 | const char *string; |
Christian Franke | cd40b32 | 2013-09-30 12:27:51 +0000 | [diff] [blame] | 2480 | unsigned int j; |
paul | 909a215 | 2005-03-14 17:41:45 +0000 | [diff] [blame] | 2481 | |
Christian Franke | cd40b32 | 2013-09-30 12:27:51 +0000 | [diff] [blame] | 2482 | for (j = 0; j < vector_active (match_vector); j++) |
| 2483 | if ((token = vector_slot (match_vector, j))) |
Lou Berger | 6729003 | 2016-01-12 13:41:46 -0500 | [diff] [blame] | 2484 | { |
| 2485 | string = cmd_entry_function (vector_slot (vline, index), token); |
| 2486 | if (string && cmd_unique_string (matchvec, string)) |
| 2487 | vector_set (matchvec, (islib != 0 ? |
| 2488 | XSTRDUP (MTYPE_TMP, string) : |
| 2489 | strdup (string) /* rl freed */)); |
| 2490 | } |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 2491 | } |
| 2492 | |
| 2493 | /* We don't need cmd_vector any more. */ |
| 2494 | vector_free (cmd_vector); |
Christian Franke | cd40b32 | 2013-09-30 12:27:51 +0000 | [diff] [blame] | 2495 | cmd_matches_free(&matches); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 2496 | |
| 2497 | /* No matched command */ |
| 2498 | if (vector_slot (matchvec, 0) == NULL) |
| 2499 | { |
| 2500 | vector_free (matchvec); |
| 2501 | |
| 2502 | /* In case of 'command \t' pattern. Do you need '?' command at |
| 2503 | the end of the line. */ |
| 2504 | if (vector_slot (vline, index) == '\0') |
| 2505 | *status = CMD_ERR_NOTHING_TODO; |
| 2506 | else |
| 2507 | *status = CMD_ERR_NO_MATCH; |
| 2508 | return NULL; |
| 2509 | } |
| 2510 | |
| 2511 | /* Only one matched */ |
| 2512 | if (vector_slot (matchvec, 1) == NULL) |
| 2513 | { |
| 2514 | match_str = (char **) matchvec->index; |
| 2515 | vector_only_wrapper_free (matchvec); |
| 2516 | *status = CMD_COMPLETE_FULL_MATCH; |
| 2517 | return match_str; |
| 2518 | } |
| 2519 | /* Make it sure last element is NULL. */ |
| 2520 | vector_set (matchvec, NULL); |
| 2521 | |
| 2522 | /* Check LCD of matched strings. */ |
| 2523 | if (vector_slot (vline, index) != NULL) |
| 2524 | { |
| 2525 | lcd = cmd_lcd ((char **) matchvec->index); |
| 2526 | |
| 2527 | if (lcd) |
| 2528 | { |
| 2529 | int len = strlen (vector_slot (vline, index)); |
paul | 909a215 | 2005-03-14 17:41:45 +0000 | [diff] [blame] | 2530 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 2531 | if (len < lcd) |
| 2532 | { |
| 2533 | char *lcdstr; |
paul | 909a215 | 2005-03-14 17:41:45 +0000 | [diff] [blame] | 2534 | |
Lou Berger | 6729003 | 2016-01-12 13:41:46 -0500 | [diff] [blame] | 2535 | lcdstr = (islib != 0 ? |
| 2536 | XMALLOC (MTYPE_TMP, lcd + 1) : |
| 2537 | malloc(lcd + 1)); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 2538 | memcpy (lcdstr, matchvec->index[0], lcd); |
| 2539 | lcdstr[lcd] = '\0'; |
| 2540 | |
| 2541 | /* match_str = (char **) &lcdstr; */ |
| 2542 | |
| 2543 | /* Free matchvec. */ |
paul | 55468c8 | 2005-03-14 20:19:01 +0000 | [diff] [blame] | 2544 | for (i = 0; i < vector_active (matchvec); i++) |
Lou Berger | 6729003 | 2016-01-12 13:41:46 -0500 | [diff] [blame] | 2545 | { |
| 2546 | if (vector_slot (matchvec, i)) |
| 2547 | { |
| 2548 | if (islib != 0) |
| 2549 | XFREE (MTYPE_TMP, vector_slot (matchvec, i)); |
| 2550 | else |
| 2551 | free (vector_slot (matchvec, i)); |
| 2552 | } |
| 2553 | } |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 2554 | vector_free (matchvec); |
| 2555 | |
paul | 909a215 | 2005-03-14 17:41:45 +0000 | [diff] [blame] | 2556 | /* Make new matchvec. */ |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 2557 | matchvec = vector_init (INIT_MATCHVEC_SIZE); |
| 2558 | vector_set (matchvec, lcdstr); |
| 2559 | match_str = (char **) matchvec->index; |
| 2560 | vector_only_wrapper_free (matchvec); |
| 2561 | |
| 2562 | *status = CMD_COMPLETE_MATCH; |
| 2563 | return match_str; |
| 2564 | } |
| 2565 | } |
| 2566 | } |
| 2567 | |
| 2568 | match_str = (char **) matchvec->index; |
Christian Franke | cd40b32 | 2013-09-30 12:27:51 +0000 | [diff] [blame] | 2569 | cmd_complete_sort(matchvec); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 2570 | vector_only_wrapper_free (matchvec); |
| 2571 | *status = CMD_COMPLETE_LIST_MATCH; |
| 2572 | return match_str; |
| 2573 | } |
| 2574 | |
paul | b92938a | 2002-12-13 21:20:42 +0000 | [diff] [blame] | 2575 | char ** |
Lou Berger | 6729003 | 2016-01-12 13:41:46 -0500 | [diff] [blame] | 2576 | cmd_complete_command_lib (vector vline, struct vty *vty, int *status, int islib) |
paul | b92938a | 2002-12-13 21:20:42 +0000 | [diff] [blame] | 2577 | { |
| 2578 | char **ret; |
| 2579 | |
| 2580 | if ( cmd_try_do_shortcut(vty->node, vector_slot(vline, 0) ) ) |
| 2581 | { |
| 2582 | enum node_type onode; |
| 2583 | vector shifted_vline; |
hasso | 8c328f1 | 2004-10-05 21:01:23 +0000 | [diff] [blame] | 2584 | unsigned int index; |
paul | b92938a | 2002-12-13 21:20:42 +0000 | [diff] [blame] | 2585 | |
| 2586 | onode = vty->node; |
| 2587 | vty->node = ENABLE_NODE; |
| 2588 | /* We can try it on enable node, cos' the vty is authenticated */ |
| 2589 | |
| 2590 | shifted_vline = vector_init (vector_count(vline)); |
| 2591 | /* use memcpy? */ |
paul | 55468c8 | 2005-03-14 20:19:01 +0000 | [diff] [blame] | 2592 | for (index = 1; index < vector_active (vline); index++) |
paul | b92938a | 2002-12-13 21:20:42 +0000 | [diff] [blame] | 2593 | { |
| 2594 | vector_set_index (shifted_vline, index-1, vector_lookup(vline, index)); |
| 2595 | } |
| 2596 | |
Lou Berger | 6729003 | 2016-01-12 13:41:46 -0500 | [diff] [blame] | 2597 | ret = cmd_complete_command_real (shifted_vline, vty, status, islib); |
paul | b92938a | 2002-12-13 21:20:42 +0000 | [diff] [blame] | 2598 | |
| 2599 | vector_free(shifted_vline); |
| 2600 | vty->node = onode; |
| 2601 | return ret; |
| 2602 | } |
| 2603 | |
Lou Berger | 6729003 | 2016-01-12 13:41:46 -0500 | [diff] [blame] | 2604 | return cmd_complete_command_real (vline, vty, status, islib); |
| 2605 | } |
paul | b92938a | 2002-12-13 21:20:42 +0000 | [diff] [blame] | 2606 | |
Lou Berger | 6729003 | 2016-01-12 13:41:46 -0500 | [diff] [blame] | 2607 | char ** |
| 2608 | cmd_complete_command (vector vline, struct vty *vty, int *status) |
| 2609 | { |
| 2610 | return cmd_complete_command_lib (vline, vty, status, 0); |
paul | b92938a | 2002-12-13 21:20:42 +0000 | [diff] [blame] | 2611 | } |
| 2612 | |
| 2613 | /* return parent node */ |
| 2614 | /* MUST eventually converge on CONFIG_NODE */ |
hasso | 13bfca7 | 2005-01-23 21:42:25 +0000 | [diff] [blame] | 2615 | enum node_type |
ajs | 274a4a4 | 2004-12-07 15:39:31 +0000 | [diff] [blame] | 2616 | node_parent ( enum node_type node ) |
paul | b92938a | 2002-12-13 21:20:42 +0000 | [diff] [blame] | 2617 | { |
| 2618 | enum node_type ret; |
| 2619 | |
paul | 9ab6812 | 2003-01-18 01:16:20 +0000 | [diff] [blame] | 2620 | assert (node > CONFIG_NODE); |
| 2621 | |
| 2622 | switch (node) |
| 2623 | { |
| 2624 | case BGP_VPNV4_NODE: |
Lou Berger | 13c378d | 2016-01-12 13:41:56 -0500 | [diff] [blame] | 2625 | case BGP_VPNV6_NODE: |
Lou Berger | a3fda88 | 2016-01-12 13:42:04 -0500 | [diff] [blame] | 2626 | case BGP_ENCAP_NODE: |
| 2627 | case BGP_ENCAPV6_NODE: |
paul | 9ab6812 | 2003-01-18 01:16:20 +0000 | [diff] [blame] | 2628 | case BGP_IPV4_NODE: |
| 2629 | case BGP_IPV4M_NODE: |
| 2630 | case BGP_IPV6_NODE: |
paul | 1e83659 | 2005-08-22 22:39:56 +0000 | [diff] [blame] | 2631 | case BGP_IPV6M_NODE: |
paul | 9ab6812 | 2003-01-18 01:16:20 +0000 | [diff] [blame] | 2632 | ret = BGP_NODE; |
| 2633 | break; |
| 2634 | case KEYCHAIN_KEY_NODE: |
| 2635 | ret = KEYCHAIN_NODE; |
| 2636 | break; |
Olivier Dugeon | ae51c9d | 2016-04-19 16:21:46 +0200 | [diff] [blame] | 2637 | case LINK_PARAMS_NODE: |
| 2638 | ret = INTERFACE_NODE; |
| 2639 | break; |
paul | 9ab6812 | 2003-01-18 01:16:20 +0000 | [diff] [blame] | 2640 | default: |
| 2641 | ret = CONFIG_NODE; |
Olivier Dugeon | ae51c9d | 2016-04-19 16:21:46 +0200 | [diff] [blame] | 2642 | break; |
paul | b92938a | 2002-12-13 21:20:42 +0000 | [diff] [blame] | 2643 | } |
| 2644 | |
| 2645 | return ret; |
| 2646 | } |
| 2647 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 2648 | /* Execute command by argument vline vector. */ |
ajs | 274a4a4 | 2004-12-07 15:39:31 +0000 | [diff] [blame] | 2649 | static int |
Christian Franke | cd40b32 | 2013-09-30 12:27:51 +0000 | [diff] [blame] | 2650 | cmd_execute_command_real (vector vline, |
| 2651 | enum filter_type filter, |
| 2652 | struct vty *vty, |
paul | b896147 | 2005-03-14 17:35:52 +0000 | [diff] [blame] | 2653 | struct cmd_element **cmd) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 2654 | { |
hasso | 8c328f1 | 2004-10-05 21:01:23 +0000 | [diff] [blame] | 2655 | unsigned int i; |
| 2656 | unsigned int index; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 2657 | vector cmd_vector; |
| 2658 | struct cmd_element *cmd_element; |
| 2659 | struct cmd_element *matched_element; |
| 2660 | unsigned int matched_count, incomplete_count; |
| 2661 | int argc; |
paul | 9035efa | 2004-10-10 11:56:56 +0000 | [diff] [blame] | 2662 | const char *argv[CMD_ARGC_MAX]; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 2663 | enum match_type match = 0; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 2664 | char *command; |
Christian Franke | cd40b32 | 2013-09-30 12:27:51 +0000 | [diff] [blame] | 2665 | int ret; |
| 2666 | vector matches; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 2667 | |
| 2668 | /* Make copy of command elements. */ |
| 2669 | cmd_vector = vector_copy (cmd_node_vector (cmdvec, vty->node)); |
| 2670 | |
paul | 55468c8 | 2005-03-14 20:19:01 +0000 | [diff] [blame] | 2671 | for (index = 0; index < vector_active (vline); index++) |
Christian Franke | cd40b32 | 2013-09-30 12:27:51 +0000 | [diff] [blame] | 2672 | { |
| 2673 | command = vector_slot (vline, index); |
| 2674 | ret = cmd_vector_filter(cmd_vector, |
| 2675 | filter, |
| 2676 | vline, index, |
| 2677 | &match, |
| 2678 | &matches); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 2679 | |
Christian Franke | cd40b32 | 2013-09-30 12:27:51 +0000 | [diff] [blame] | 2680 | if (ret != CMD_SUCCESS) |
| 2681 | { |
| 2682 | cmd_matches_free(&matches); |
| 2683 | return ret; |
| 2684 | } |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 2685 | |
Christian Franke | cd40b32 | 2013-09-30 12:27:51 +0000 | [diff] [blame] | 2686 | if (match == vararg_match) |
| 2687 | { |
| 2688 | cmd_matches_free(&matches); |
paul | 909a215 | 2005-03-14 17:41:45 +0000 | [diff] [blame] | 2689 | break; |
Christian Franke | cd40b32 | 2013-09-30 12:27:51 +0000 | [diff] [blame] | 2690 | } |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 2691 | |
Christian Franke | cd40b32 | 2013-09-30 12:27:51 +0000 | [diff] [blame] | 2692 | ret = is_cmd_ambiguous (cmd_vector, command, matches, match); |
| 2693 | cmd_matches_free(&matches); |
| 2694 | |
| 2695 | if (ret == 1) |
| 2696 | { |
| 2697 | vector_free(cmd_vector); |
| 2698 | return CMD_ERR_AMBIGUOUS; |
| 2699 | } |
| 2700 | else if (ret == 2) |
| 2701 | { |
| 2702 | vector_free(cmd_vector); |
| 2703 | return CMD_ERR_NO_MATCH; |
| 2704 | } |
| 2705 | } |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 2706 | |
| 2707 | /* Check matched count. */ |
| 2708 | matched_element = NULL; |
| 2709 | matched_count = 0; |
| 2710 | incomplete_count = 0; |
| 2711 | |
paul | 55468c8 | 2005-03-14 20:19:01 +0000 | [diff] [blame] | 2712 | for (i = 0; i < vector_active (cmd_vector); i++) |
paul | b896147 | 2005-03-14 17:35:52 +0000 | [diff] [blame] | 2713 | if ((cmd_element = vector_slot (cmd_vector, i))) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 2714 | { |
Christian Franke | cd40b32 | 2013-09-30 12:27:51 +0000 | [diff] [blame] | 2715 | if (cmd_is_complete(cmd_element, vline)) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 2716 | { |
| 2717 | matched_element = cmd_element; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 2718 | matched_count++; |
| 2719 | } |
| 2720 | else |
| 2721 | { |
| 2722 | incomplete_count++; |
| 2723 | } |
| 2724 | } |
paul | 909a215 | 2005-03-14 17:41:45 +0000 | [diff] [blame] | 2725 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 2726 | /* Finish of using cmd_vector. */ |
| 2727 | vector_free (cmd_vector); |
| 2728 | |
paul | 909a215 | 2005-03-14 17:41:45 +0000 | [diff] [blame] | 2729 | /* To execute command, matched_count must be 1. */ |
| 2730 | if (matched_count == 0) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 2731 | { |
| 2732 | if (incomplete_count) |
| 2733 | return CMD_ERR_INCOMPLETE; |
| 2734 | else |
| 2735 | return CMD_ERR_NO_MATCH; |
| 2736 | } |
| 2737 | |
paul | 909a215 | 2005-03-14 17:41:45 +0000 | [diff] [blame] | 2738 | if (matched_count > 1) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 2739 | return CMD_ERR_AMBIGUOUS; |
| 2740 | |
Christian Franke | cd40b32 | 2013-09-30 12:27:51 +0000 | [diff] [blame] | 2741 | ret = cmd_parse(matched_element, vline, &argc, argv); |
| 2742 | if (ret != CMD_SUCCESS) |
| 2743 | return ret; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 2744 | |
| 2745 | /* For vtysh execution. */ |
| 2746 | if (cmd) |
| 2747 | *cmd = matched_element; |
| 2748 | |
| 2749 | if (matched_element->daemon) |
| 2750 | return CMD_SUCCESS_DAEMON; |
| 2751 | |
| 2752 | /* Execute matched command. */ |
| 2753 | return (*matched_element->func) (matched_element, vty, argc, argv); |
| 2754 | } |
| 2755 | |
Christian Franke | cd40b32 | 2013-09-30 12:27:51 +0000 | [diff] [blame] | 2756 | /** |
| 2757 | * Execute a given command, handling things like "do ..." and checking |
| 2758 | * whether the given command might apply at a parent node if doesn't |
| 2759 | * apply for the current node. |
| 2760 | * |
| 2761 | * @param vline Command line input, vector of char* where each element is |
| 2762 | * one input token. |
| 2763 | * @param vty The vty context in which the command should be executed. |
| 2764 | * @param cmd Pointer where the struct cmd_element of the matched command |
| 2765 | * will be stored, if any. May be set to NULL if this info is |
| 2766 | * not needed. |
| 2767 | * @param vtysh If set != 0, don't lookup the command at parent nodes. |
| 2768 | * @return The status of the command that has been executed or an error code |
| 2769 | * as to why no command could be executed. |
| 2770 | */ |
paul | b92938a | 2002-12-13 21:20:42 +0000 | [diff] [blame] | 2771 | int |
hasso | 87d683b | 2005-01-16 23:31:54 +0000 | [diff] [blame] | 2772 | cmd_execute_command (vector vline, struct vty *vty, struct cmd_element **cmd, |
| 2773 | int vtysh) { |
paul | 9ab6812 | 2003-01-18 01:16:20 +0000 | [diff] [blame] | 2774 | int ret, saved_ret, tried = 0; |
| 2775 | enum node_type onode, try_node; |
| 2776 | |
| 2777 | onode = try_node = vty->node; |
paul | b92938a | 2002-12-13 21:20:42 +0000 | [diff] [blame] | 2778 | |
| 2779 | if ( cmd_try_do_shortcut(vty->node, vector_slot(vline, 0) ) ) |
| 2780 | { |
| 2781 | vector shifted_vline; |
hasso | 8c328f1 | 2004-10-05 21:01:23 +0000 | [diff] [blame] | 2782 | unsigned int index; |
paul | b92938a | 2002-12-13 21:20:42 +0000 | [diff] [blame] | 2783 | |
| 2784 | vty->node = ENABLE_NODE; |
| 2785 | /* We can try it on enable node, cos' the vty is authenticated */ |
| 2786 | |
| 2787 | shifted_vline = vector_init (vector_count(vline)); |
| 2788 | /* use memcpy? */ |
paul | 55468c8 | 2005-03-14 20:19:01 +0000 | [diff] [blame] | 2789 | for (index = 1; index < vector_active (vline); index++) |
paul | b92938a | 2002-12-13 21:20:42 +0000 | [diff] [blame] | 2790 | { |
| 2791 | vector_set_index (shifted_vline, index-1, vector_lookup(vline, index)); |
| 2792 | } |
| 2793 | |
Christian Franke | cd40b32 | 2013-09-30 12:27:51 +0000 | [diff] [blame] | 2794 | ret = cmd_execute_command_real (shifted_vline, FILTER_RELAXED, vty, cmd); |
paul | b92938a | 2002-12-13 21:20:42 +0000 | [diff] [blame] | 2795 | |
| 2796 | vector_free(shifted_vline); |
| 2797 | vty->node = onode; |
| 2798 | return ret; |
| 2799 | } |
| 2800 | |
| 2801 | |
Christian Franke | cd40b32 | 2013-09-30 12:27:51 +0000 | [diff] [blame] | 2802 | saved_ret = ret = cmd_execute_command_real (vline, FILTER_RELAXED, vty, cmd); |
paul | b92938a | 2002-12-13 21:20:42 +0000 | [diff] [blame] | 2803 | |
hasso | 87d683b | 2005-01-16 23:31:54 +0000 | [diff] [blame] | 2804 | if (vtysh) |
| 2805 | return saved_ret; |
| 2806 | |
paul | b92938a | 2002-12-13 21:20:42 +0000 | [diff] [blame] | 2807 | /* This assumes all nodes above CONFIG_NODE are childs of CONFIG_NODE */ |
paul | 9ab6812 | 2003-01-18 01:16:20 +0000 | [diff] [blame] | 2808 | while ( ret != CMD_SUCCESS && ret != CMD_WARNING |
paul | b92938a | 2002-12-13 21:20:42 +0000 | [diff] [blame] | 2809 | && vty->node > CONFIG_NODE ) |
| 2810 | { |
paul | 9ab6812 | 2003-01-18 01:16:20 +0000 | [diff] [blame] | 2811 | try_node = node_parent(try_node); |
| 2812 | vty->node = try_node; |
Christian Franke | cd40b32 | 2013-09-30 12:27:51 +0000 | [diff] [blame] | 2813 | ret = cmd_execute_command_real (vline, FILTER_RELAXED, vty, cmd); |
paul | 9ab6812 | 2003-01-18 01:16:20 +0000 | [diff] [blame] | 2814 | tried = 1; |
| 2815 | if (ret == CMD_SUCCESS || ret == CMD_WARNING) |
paul | b92938a | 2002-12-13 21:20:42 +0000 | [diff] [blame] | 2816 | { |
paul | 9ab6812 | 2003-01-18 01:16:20 +0000 | [diff] [blame] | 2817 | /* succesfull command, leave the node as is */ |
paul | b92938a | 2002-12-13 21:20:42 +0000 | [diff] [blame] | 2818 | return ret; |
| 2819 | } |
paul | b92938a | 2002-12-13 21:20:42 +0000 | [diff] [blame] | 2820 | } |
paul | 9ab6812 | 2003-01-18 01:16:20 +0000 | [diff] [blame] | 2821 | /* no command succeeded, reset the vty to the original node and |
| 2822 | return the error for this node */ |
| 2823 | if ( tried ) |
| 2824 | vty->node = onode; |
| 2825 | return saved_ret; |
paul | eda031f | 2003-01-18 00:39:19 +0000 | [diff] [blame] | 2826 | } |
| 2827 | |
Christian Franke | cd40b32 | 2013-09-30 12:27:51 +0000 | [diff] [blame] | 2828 | /** |
| 2829 | * Execute a given command, matching it strictly against the current node. |
| 2830 | * This mode is used when reading config files. |
| 2831 | * |
| 2832 | * @param vline Command line input, vector of char* where each element is |
| 2833 | * one input token. |
| 2834 | * @param vty The vty context in which the command should be executed. |
| 2835 | * @param cmd Pointer where the struct cmd_element* of the matched command |
| 2836 | * will be stored, if any. May be set to NULL if this info is |
| 2837 | * not needed. |
| 2838 | * @return The status of the command that has been executed or an error code |
| 2839 | * as to why no command could be executed. |
| 2840 | */ |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 2841 | int |
paul | 909a215 | 2005-03-14 17:41:45 +0000 | [diff] [blame] | 2842 | cmd_execute_command_strict (vector vline, struct vty *vty, |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 2843 | struct cmd_element **cmd) |
| 2844 | { |
Christian Franke | cd40b32 | 2013-09-30 12:27:51 +0000 | [diff] [blame] | 2845 | return cmd_execute_command_real(vline, FILTER_STRICT, vty, cmd); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 2846 | } |
| 2847 | |
Donald Sharp | d8aa4be | 2015-09-28 20:10:40 -0400 | [diff] [blame] | 2848 | /** |
| 2849 | * Parse one line of config, walking up the parse tree attempting to find a match |
| 2850 | * |
| 2851 | * @param vty The vty context in which the command should be executed. |
| 2852 | * @param cmd Pointer where the struct cmd_element* of the match command |
| 2853 | * will be stored, if any. May be set to NULL if this info is |
| 2854 | * not needed. |
| 2855 | * @param use_daemon Boolean to control whether or not we match on CMD_SUCCESS_DAEMON |
| 2856 | * or not. |
| 2857 | * @return The status of the command that has been executed or an error code |
| 2858 | * as to why no command could be executed. |
| 2859 | */ |
| 2860 | int |
| 2861 | command_config_read_one_line (struct vty *vty, struct cmd_element **cmd, int use_daemon) |
| 2862 | { |
| 2863 | vector vline; |
| 2864 | int saved_node; |
| 2865 | int ret; |
| 2866 | |
| 2867 | vline = cmd_make_strvec (vty->buf); |
| 2868 | |
| 2869 | /* In case of comment line */ |
| 2870 | if (vline == NULL) |
| 2871 | return CMD_SUCCESS; |
| 2872 | |
| 2873 | /* Execute configuration command : this is strict match */ |
| 2874 | ret = cmd_execute_command_strict (vline, vty, cmd); |
| 2875 | |
| 2876 | saved_node = vty->node; |
| 2877 | |
| 2878 | while (!(use_daemon && ret == CMD_SUCCESS_DAEMON) && |
| 2879 | ret != CMD_SUCCESS && ret != CMD_WARNING && |
| 2880 | ret != CMD_ERR_NOTHING_TODO && vty->node != CONFIG_NODE) { |
| 2881 | vty->node = node_parent(vty->node); |
| 2882 | ret = cmd_execute_command_strict (vline, vty, NULL); |
| 2883 | } |
| 2884 | |
| 2885 | // If climbing the tree did not work then ignore the command and |
| 2886 | // stay at the same node |
| 2887 | if (!(use_daemon && ret == CMD_SUCCESS_DAEMON) && |
| 2888 | ret != CMD_SUCCESS && ret != CMD_WARNING && |
| 2889 | ret != CMD_ERR_NOTHING_TODO) |
| 2890 | { |
| 2891 | vty->node = saved_node; |
| 2892 | } |
| 2893 | |
| 2894 | cmd_free_strvec (vline); |
| 2895 | |
| 2896 | return ret; |
| 2897 | } |
| 2898 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 2899 | /* Configration make from file. */ |
| 2900 | int |
Steve Hill | ea55500 | 2009-07-28 16:36:14 -0400 | [diff] [blame] | 2901 | config_from_file (struct vty *vty, FILE *fp, unsigned int *line_num) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 2902 | { |
| 2903 | int ret; |
Steve Hill | ea55500 | 2009-07-28 16:36:14 -0400 | [diff] [blame] | 2904 | *line_num = 0; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 2905 | |
Quentin Young | b7ceefe | 2017-01-10 23:33:50 +0000 | [diff] [blame] | 2906 | while (fgets (vty->buf, vty->max, fp)) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 2907 | { |
Steve Hill | ea55500 | 2009-07-28 16:36:14 -0400 | [diff] [blame] | 2908 | ++(*line_num); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 2909 | |
Donald Sharp | d8aa4be | 2015-09-28 20:10:40 -0400 | [diff] [blame] | 2910 | ret = command_config_read_one_line (vty, NULL, 0); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 2911 | |
hasso | ddd85ed | 2004-10-13 08:18:07 +0000 | [diff] [blame] | 2912 | if (ret != CMD_SUCCESS && ret != CMD_WARNING |
| 2913 | && ret != CMD_ERR_NOTHING_TODO) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 2914 | return ret; |
| 2915 | } |
| 2916 | return CMD_SUCCESS; |
| 2917 | } |
| 2918 | |
| 2919 | /* Configration from terminal */ |
| 2920 | DEFUN (config_terminal, |
| 2921 | config_terminal_cmd, |
| 2922 | "configure terminal", |
| 2923 | "Configuration from vty interface\n" |
| 2924 | "Configuration terminal\n") |
| 2925 | { |
| 2926 | if (vty_config_lock (vty)) |
| 2927 | vty->node = CONFIG_NODE; |
| 2928 | else |
| 2929 | { |
| 2930 | vty_out (vty, "VTY configuration is locked by other VTY%s", VTY_NEWLINE); |
| 2931 | return CMD_WARNING; |
| 2932 | } |
| 2933 | return CMD_SUCCESS; |
| 2934 | } |
| 2935 | |
| 2936 | /* Enable command */ |
| 2937 | DEFUN (enable, |
| 2938 | config_enable_cmd, |
| 2939 | "enable", |
| 2940 | "Turn on privileged mode command\n") |
| 2941 | { |
| 2942 | /* If enable password is NULL, change to ENABLE_NODE */ |
| 2943 | if ((host.enable == NULL && host.enable_encrypt == NULL) || |
| 2944 | vty->type == VTY_SHELL_SERV) |
| 2945 | vty->node = ENABLE_NODE; |
| 2946 | else |
| 2947 | vty->node = AUTH_ENABLE_NODE; |
| 2948 | |
| 2949 | return CMD_SUCCESS; |
| 2950 | } |
| 2951 | |
| 2952 | /* Disable command */ |
| 2953 | DEFUN (disable, |
| 2954 | config_disable_cmd, |
| 2955 | "disable", |
| 2956 | "Turn off privileged mode command\n") |
| 2957 | { |
| 2958 | if (vty->node == ENABLE_NODE) |
| 2959 | vty->node = VIEW_NODE; |
| 2960 | return CMD_SUCCESS; |
| 2961 | } |
| 2962 | |
| 2963 | /* Down vty node level. */ |
| 2964 | DEFUN (config_exit, |
| 2965 | config_exit_cmd, |
| 2966 | "exit", |
| 2967 | "Exit current mode and down to previous mode\n") |
| 2968 | { |
| 2969 | switch (vty->node) |
| 2970 | { |
| 2971 | case VIEW_NODE: |
| 2972 | case ENABLE_NODE: |
Paul Jakma | 62687ff | 2008-08-23 14:27:06 +0100 | [diff] [blame] | 2973 | case RESTRICTED_NODE: |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 2974 | if (vty_shell (vty)) |
| 2975 | exit (0); |
| 2976 | else |
| 2977 | vty->status = VTY_CLOSE; |
| 2978 | break; |
| 2979 | case CONFIG_NODE: |
| 2980 | vty->node = ENABLE_NODE; |
| 2981 | vty_config_unlock (vty); |
| 2982 | break; |
| 2983 | case INTERFACE_NODE: |
| 2984 | case ZEBRA_NODE: |
| 2985 | case BGP_NODE: |
| 2986 | case RIP_NODE: |
| 2987 | case RIPNG_NODE: |
Paul Jakma | 5734509 | 2011-12-25 17:52:09 +0100 | [diff] [blame] | 2988 | case BABEL_NODE: |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 2989 | case OSPF_NODE: |
| 2990 | case OSPF6_NODE: |
jardin | 9e867fe | 2003-12-23 08:56:18 +0000 | [diff] [blame] | 2991 | case ISIS_NODE: |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 2992 | case KEYCHAIN_NODE: |
| 2993 | case MASC_NODE: |
| 2994 | case RMAP_NODE: |
Everton Marques | 42e3078 | 2009-11-18 17:19:43 -0200 | [diff] [blame] | 2995 | case PIM_NODE: |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 2996 | case VTY_NODE: |
| 2997 | vty->node = CONFIG_NODE; |
| 2998 | break; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 2999 | case BGP_IPV4_NODE: |
| 3000 | case BGP_IPV4M_NODE: |
Lou Berger | 13c378d | 2016-01-12 13:41:56 -0500 | [diff] [blame] | 3001 | case BGP_VPNV4_NODE: |
| 3002 | case BGP_VPNV6_NODE: |
Lou Berger | a3fda88 | 2016-01-12 13:42:04 -0500 | [diff] [blame] | 3003 | case BGP_ENCAP_NODE: |
| 3004 | case BGP_ENCAPV6_NODE: |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 3005 | case BGP_IPV6_NODE: |
paul | 1e83659 | 2005-08-22 22:39:56 +0000 | [diff] [blame] | 3006 | case BGP_IPV6M_NODE: |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 3007 | vty->node = BGP_NODE; |
| 3008 | break; |
| 3009 | case KEYCHAIN_KEY_NODE: |
| 3010 | vty->node = KEYCHAIN_NODE; |
| 3011 | break; |
Olivier Dugeon | ae51c9d | 2016-04-19 16:21:46 +0200 | [diff] [blame] | 3012 | case LINK_PARAMS_NODE: |
| 3013 | vty->node = INTERFACE_NODE; |
| 3014 | break; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 3015 | default: |
| 3016 | break; |
| 3017 | } |
| 3018 | return CMD_SUCCESS; |
| 3019 | } |
| 3020 | |
| 3021 | /* quit is alias of exit. */ |
| 3022 | ALIAS (config_exit, |
| 3023 | config_quit_cmd, |
| 3024 | "quit", |
| 3025 | "Exit current mode and down to previous mode\n") |
| 3026 | |
| 3027 | /* End of configuration. */ |
| 3028 | DEFUN (config_end, |
| 3029 | config_end_cmd, |
| 3030 | "end", |
| 3031 | "End current mode and change to enable mode.") |
| 3032 | { |
| 3033 | switch (vty->node) |
| 3034 | { |
| 3035 | case VIEW_NODE: |
| 3036 | case ENABLE_NODE: |
Paul Jakma | 62687ff | 2008-08-23 14:27:06 +0100 | [diff] [blame] | 3037 | case RESTRICTED_NODE: |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 3038 | /* Nothing to do. */ |
| 3039 | break; |
| 3040 | case CONFIG_NODE: |
| 3041 | case INTERFACE_NODE: |
| 3042 | case ZEBRA_NODE: |
| 3043 | case RIP_NODE: |
| 3044 | case RIPNG_NODE: |
Paul Jakma | 5734509 | 2011-12-25 17:52:09 +0100 | [diff] [blame] | 3045 | case BABEL_NODE: |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 3046 | case BGP_NODE: |
Lou Berger | a3fda88 | 2016-01-12 13:42:04 -0500 | [diff] [blame] | 3047 | case BGP_ENCAP_NODE: |
| 3048 | case BGP_ENCAPV6_NODE: |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 3049 | case BGP_VPNV4_NODE: |
Lou Berger | 13c378d | 2016-01-12 13:41:56 -0500 | [diff] [blame] | 3050 | case BGP_VPNV6_NODE: |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 3051 | case BGP_IPV4_NODE: |
| 3052 | case BGP_IPV4M_NODE: |
| 3053 | case BGP_IPV6_NODE: |
paul | 1e83659 | 2005-08-22 22:39:56 +0000 | [diff] [blame] | 3054 | case BGP_IPV6M_NODE: |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 3055 | case RMAP_NODE: |
| 3056 | case OSPF_NODE: |
| 3057 | case OSPF6_NODE: |
jardin | 9e867fe | 2003-12-23 08:56:18 +0000 | [diff] [blame] | 3058 | case ISIS_NODE: |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 3059 | case KEYCHAIN_NODE: |
| 3060 | case KEYCHAIN_KEY_NODE: |
| 3061 | case MASC_NODE: |
Everton Marques | 42e3078 | 2009-11-18 17:19:43 -0200 | [diff] [blame] | 3062 | case PIM_NODE: |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 3063 | case VTY_NODE: |
Olivier Dugeon | ae51c9d | 2016-04-19 16:21:46 +0200 | [diff] [blame] | 3064 | case LINK_PARAMS_NODE: |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 3065 | vty_config_unlock (vty); |
| 3066 | vty->node = ENABLE_NODE; |
| 3067 | break; |
| 3068 | default: |
| 3069 | break; |
| 3070 | } |
| 3071 | return CMD_SUCCESS; |
| 3072 | } |
| 3073 | |
| 3074 | /* Show version. */ |
| 3075 | DEFUN (show_version, |
| 3076 | show_version_cmd, |
| 3077 | "show version", |
| 3078 | SHOW_STR |
| 3079 | "Displays zebra version\n") |
| 3080 | { |
hasso | 12f6ea2 | 2005-03-07 08:35:39 +0000 | [diff] [blame] | 3081 | vty_out (vty, "Quagga %s (%s).%s", QUAGGA_VERSION, host.name?host.name:"", |
| 3082 | VTY_NEWLINE); |
David Lamparter | 0be793e | 2012-11-27 01:34:56 +0000 | [diff] [blame] | 3083 | vty_out (vty, "%s%s%s", QUAGGA_COPYRIGHT, GIT_INFO, VTY_NEWLINE); |
David Lamparter | 7abd875 | 2014-11-22 10:43:29 -0800 | [diff] [blame] | 3084 | vty_out (vty, "configured with:%s %s%s", VTY_NEWLINE, |
| 3085 | QUAGGA_CONFIG_ARGS, VTY_NEWLINE); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 3086 | |
| 3087 | return CMD_SUCCESS; |
| 3088 | } |
| 3089 | |
| 3090 | /* Help display function for all node. */ |
| 3091 | DEFUN (config_help, |
| 3092 | config_help_cmd, |
| 3093 | "help", |
| 3094 | "Description of the interactive help system\n") |
| 3095 | { |
| 3096 | vty_out (vty, |
hasso | 6590f2c | 2004-10-19 20:40:08 +0000 | [diff] [blame] | 3097 | "Quagga VTY provides advanced help feature. When you need help,%s\ |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 3098 | anytime at the command line please press '?'.%s\ |
| 3099 | %s\ |
| 3100 | If nothing matches, the help list will be empty and you must backup%s\ |
| 3101 | until entering a '?' shows the available options.%s\ |
| 3102 | Two styles of help are provided:%s\ |
| 3103 | 1. Full help is available when you are ready to enter a%s\ |
| 3104 | command argument (e.g. 'show ?') and describes each possible%s\ |
| 3105 | argument.%s\ |
| 3106 | 2. Partial help is provided when an abbreviated argument is entered%s\ |
| 3107 | and you want to know what arguments match the input%s\ |
| 3108 | (e.g. 'show me?'.)%s%s", VTY_NEWLINE, VTY_NEWLINE, VTY_NEWLINE, |
| 3109 | VTY_NEWLINE, VTY_NEWLINE, VTY_NEWLINE, VTY_NEWLINE, VTY_NEWLINE, |
| 3110 | VTY_NEWLINE, VTY_NEWLINE, VTY_NEWLINE, VTY_NEWLINE, VTY_NEWLINE); |
| 3111 | return CMD_SUCCESS; |
| 3112 | } |
| 3113 | |
| 3114 | /* Help display function for all node. */ |
| 3115 | DEFUN (config_list, |
| 3116 | config_list_cmd, |
| 3117 | "list", |
| 3118 | "Print command list\n") |
| 3119 | { |
hasso | 8c328f1 | 2004-10-05 21:01:23 +0000 | [diff] [blame] | 3120 | unsigned int i; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 3121 | struct cmd_node *cnode = vector_slot (cmdvec, vty->node); |
| 3122 | struct cmd_element *cmd; |
| 3123 | |
paul | 55468c8 | 2005-03-14 20:19:01 +0000 | [diff] [blame] | 3124 | for (i = 0; i < vector_active (cnode->cmd_vector); i++) |
paul | 4275b1d | 2005-03-09 13:42:23 +0000 | [diff] [blame] | 3125 | if ((cmd = vector_slot (cnode->cmd_vector, i)) != NULL |
| 3126 | && !(cmd->attr == CMD_ATTR_DEPRECATED |
| 3127 | || cmd->attr == CMD_ATTR_HIDDEN)) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 3128 | vty_out (vty, " %s%s", cmd->string, |
| 3129 | VTY_NEWLINE); |
| 3130 | return CMD_SUCCESS; |
| 3131 | } |
| 3132 | |
| 3133 | /* Write current configuration into file. */ |
| 3134 | DEFUN (config_write_file, |
| 3135 | config_write_file_cmd, |
| 3136 | "write file", |
| 3137 | "Write running configuration to memory, network, or terminal\n" |
| 3138 | "Write to configuration file\n") |
| 3139 | { |
hasso | 8c328f1 | 2004-10-05 21:01:23 +0000 | [diff] [blame] | 3140 | unsigned int i; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 3141 | int fd; |
| 3142 | struct cmd_node *node; |
| 3143 | char *config_file; |
| 3144 | char *config_file_tmp = NULL; |
| 3145 | char *config_file_sav = NULL; |
paul | 05865c9 | 2005-10-26 05:49:54 +0000 | [diff] [blame] | 3146 | int ret = CMD_WARNING; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 3147 | struct vty *file_vty; |
| 3148 | |
| 3149 | /* Check and see if we are operating under vtysh configuration */ |
| 3150 | if (host.config == NULL) |
| 3151 | { |
| 3152 | vty_out (vty, "Can't save to configuration file, using vtysh.%s", |
| 3153 | VTY_NEWLINE); |
| 3154 | return CMD_WARNING; |
| 3155 | } |
| 3156 | |
| 3157 | /* Get filename. */ |
| 3158 | config_file = host.config; |
| 3159 | |
paul | 05865c9 | 2005-10-26 05:49:54 +0000 | [diff] [blame] | 3160 | config_file_sav = |
| 3161 | XMALLOC (MTYPE_TMP, strlen (config_file) + strlen (CONF_BACKUP_EXT) + 1); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 3162 | strcpy (config_file_sav, config_file); |
| 3163 | strcat (config_file_sav, CONF_BACKUP_EXT); |
| 3164 | |
| 3165 | |
paul | 05865c9 | 2005-10-26 05:49:54 +0000 | [diff] [blame] | 3166 | config_file_tmp = XMALLOC (MTYPE_TMP, strlen (config_file) + 8); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 3167 | sprintf (config_file_tmp, "%s.XXXXXX", config_file); |
| 3168 | |
| 3169 | /* Open file to configuration write. */ |
| 3170 | fd = mkstemp (config_file_tmp); |
| 3171 | if (fd < 0) |
| 3172 | { |
| 3173 | vty_out (vty, "Can't open configuration file %s.%s", config_file_tmp, |
| 3174 | VTY_NEWLINE); |
paul | 05865c9 | 2005-10-26 05:49:54 +0000 | [diff] [blame] | 3175 | goto finished; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 3176 | } |
| 3177 | |
| 3178 | /* Make vty for configuration file. */ |
| 3179 | file_vty = vty_new (); |
David Lamparter | 4715a53 | 2013-05-30 16:31:49 +0200 | [diff] [blame] | 3180 | file_vty->wfd = fd; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 3181 | file_vty->type = VTY_FILE; |
| 3182 | |
| 3183 | /* Config file header print. */ |
| 3184 | vty_out (file_vty, "!\n! Zebra configuration saved from vty\n! "); |
| 3185 | vty_time_print (file_vty, 1); |
| 3186 | vty_out (file_vty, "!\n"); |
| 3187 | |
paul | 55468c8 | 2005-03-14 20:19:01 +0000 | [diff] [blame] | 3188 | for (i = 0; i < vector_active (cmdvec); i++) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 3189 | if ((node = vector_slot (cmdvec, i)) && node->func) |
| 3190 | { |
| 3191 | if ((*node->func) (file_vty)) |
| 3192 | vty_out (file_vty, "!\n"); |
| 3193 | } |
| 3194 | vty_close (file_vty); |
| 3195 | |
| 3196 | if (unlink (config_file_sav) != 0) |
| 3197 | if (errno != ENOENT) |
| 3198 | { |
| 3199 | vty_out (vty, "Can't unlink backup configuration file %s.%s", config_file_sav, |
| 3200 | VTY_NEWLINE); |
paul | 05865c9 | 2005-10-26 05:49:54 +0000 | [diff] [blame] | 3201 | goto finished; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 3202 | } |
| 3203 | if (link (config_file, config_file_sav) != 0) |
| 3204 | { |
| 3205 | vty_out (vty, "Can't backup old configuration file %s.%s", config_file_sav, |
| 3206 | VTY_NEWLINE); |
paul | 05865c9 | 2005-10-26 05:49:54 +0000 | [diff] [blame] | 3207 | goto finished; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 3208 | } |
| 3209 | sync (); |
| 3210 | if (unlink (config_file) != 0) |
| 3211 | { |
| 3212 | vty_out (vty, "Can't unlink configuration file %s.%s", config_file, |
| 3213 | VTY_NEWLINE); |
paul | 05865c9 | 2005-10-26 05:49:54 +0000 | [diff] [blame] | 3214 | goto finished; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 3215 | } |
| 3216 | if (link (config_file_tmp, config_file) != 0) |
| 3217 | { |
| 3218 | vty_out (vty, "Can't save configuration file %s.%s", config_file, |
| 3219 | VTY_NEWLINE); |
paul | 05865c9 | 2005-10-26 05:49:54 +0000 | [diff] [blame] | 3220 | goto finished; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 3221 | } |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 3222 | sync (); |
| 3223 | |
gdt | aa593d5 | 2003-12-22 20:15:53 +0000 | [diff] [blame] | 3224 | if (chmod (config_file, CONFIGFILE_MASK) != 0) |
| 3225 | { |
| 3226 | vty_out (vty, "Can't chmod configuration file %s: %s (%d).%s", |
ajs | 6099b3b | 2004-11-20 02:06:59 +0000 | [diff] [blame] | 3227 | config_file, safe_strerror(errno), errno, VTY_NEWLINE); |
paul | 05865c9 | 2005-10-26 05:49:54 +0000 | [diff] [blame] | 3228 | goto finished; |
gdt | aa593d5 | 2003-12-22 20:15:53 +0000 | [diff] [blame] | 3229 | } |
| 3230 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 3231 | vty_out (vty, "Configuration saved to %s%s", config_file, |
| 3232 | VTY_NEWLINE); |
paul | 05865c9 | 2005-10-26 05:49:54 +0000 | [diff] [blame] | 3233 | ret = CMD_SUCCESS; |
| 3234 | |
| 3235 | finished: |
| 3236 | unlink (config_file_tmp); |
| 3237 | XFREE (MTYPE_TMP, config_file_tmp); |
| 3238 | XFREE (MTYPE_TMP, config_file_sav); |
| 3239 | return ret; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 3240 | } |
| 3241 | |
| 3242 | ALIAS (config_write_file, |
| 3243 | config_write_cmd, |
| 3244 | "write", |
| 3245 | "Write running configuration to memory, network, or terminal\n") |
| 3246 | |
| 3247 | ALIAS (config_write_file, |
| 3248 | config_write_memory_cmd, |
| 3249 | "write memory", |
| 3250 | "Write running configuration to memory, network, or terminal\n" |
| 3251 | "Write configuration to the file (same as write file)\n") |
| 3252 | |
| 3253 | ALIAS (config_write_file, |
| 3254 | copy_runningconfig_startupconfig_cmd, |
| 3255 | "copy running-config startup-config", |
| 3256 | "Copy configuration\n" |
| 3257 | "Copy running config to... \n" |
| 3258 | "Copy running config to startup config (same as write file)\n") |
| 3259 | |
| 3260 | /* Write current configuration into the terminal. */ |
| 3261 | DEFUN (config_write_terminal, |
| 3262 | config_write_terminal_cmd, |
| 3263 | "write terminal", |
| 3264 | "Write running configuration to memory, network, or terminal\n" |
| 3265 | "Write to terminal\n") |
| 3266 | { |
hasso | 8c328f1 | 2004-10-05 21:01:23 +0000 | [diff] [blame] | 3267 | unsigned int i; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 3268 | struct cmd_node *node; |
| 3269 | |
| 3270 | if (vty->type == VTY_SHELL_SERV) |
| 3271 | { |
paul | 55468c8 | 2005-03-14 20:19:01 +0000 | [diff] [blame] | 3272 | for (i = 0; i < vector_active (cmdvec); i++) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 3273 | if ((node = vector_slot (cmdvec, i)) && node->func && node->vtysh) |
| 3274 | { |
| 3275 | if ((*node->func) (vty)) |
| 3276 | vty_out (vty, "!%s", VTY_NEWLINE); |
| 3277 | } |
| 3278 | } |
| 3279 | else |
| 3280 | { |
| 3281 | vty_out (vty, "%sCurrent configuration:%s", VTY_NEWLINE, |
| 3282 | VTY_NEWLINE); |
| 3283 | vty_out (vty, "!%s", VTY_NEWLINE); |
| 3284 | |
paul | 55468c8 | 2005-03-14 20:19:01 +0000 | [diff] [blame] | 3285 | for (i = 0; i < vector_active (cmdvec); i++) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 3286 | if ((node = vector_slot (cmdvec, i)) && node->func) |
| 3287 | { |
| 3288 | if ((*node->func) (vty)) |
| 3289 | vty_out (vty, "!%s", VTY_NEWLINE); |
| 3290 | } |
| 3291 | vty_out (vty, "end%s",VTY_NEWLINE); |
| 3292 | } |
| 3293 | return CMD_SUCCESS; |
| 3294 | } |
| 3295 | |
| 3296 | /* Write current configuration into the terminal. */ |
| 3297 | ALIAS (config_write_terminal, |
| 3298 | show_running_config_cmd, |
| 3299 | "show running-config", |
| 3300 | SHOW_STR |
| 3301 | "running configuration\n") |
| 3302 | |
| 3303 | /* Write startup configuration into the terminal. */ |
| 3304 | DEFUN (show_startup_config, |
| 3305 | show_startup_config_cmd, |
| 3306 | "show startup-config", |
| 3307 | SHOW_STR |
| 3308 | "Contentes of startup configuration\n") |
| 3309 | { |
| 3310 | char buf[BUFSIZ]; |
| 3311 | FILE *confp; |
| 3312 | |
| 3313 | confp = fopen (host.config, "r"); |
| 3314 | if (confp == NULL) |
| 3315 | { |
| 3316 | vty_out (vty, "Can't open configuration file [%s]%s", |
| 3317 | host.config, VTY_NEWLINE); |
| 3318 | return CMD_WARNING; |
| 3319 | } |
| 3320 | |
| 3321 | while (fgets (buf, BUFSIZ, confp)) |
| 3322 | { |
| 3323 | char *cp = buf; |
| 3324 | |
| 3325 | while (*cp != '\r' && *cp != '\n' && *cp != '\0') |
| 3326 | cp++; |
| 3327 | *cp = '\0'; |
| 3328 | |
| 3329 | vty_out (vty, "%s%s", buf, VTY_NEWLINE); |
| 3330 | } |
| 3331 | |
| 3332 | fclose (confp); |
| 3333 | |
| 3334 | return CMD_SUCCESS; |
| 3335 | } |
| 3336 | |
| 3337 | /* Hostname configuration */ |
| 3338 | DEFUN (config_hostname, |
| 3339 | hostname_cmd, |
| 3340 | "hostname WORD", |
| 3341 | "Set system's network name\n" |
| 3342 | "This system's network name\n") |
| 3343 | { |
| 3344 | if (!isalpha((int) *argv[0])) |
| 3345 | { |
| 3346 | vty_out (vty, "Please specify string starting with alphabet%s", VTY_NEWLINE); |
| 3347 | return CMD_WARNING; |
| 3348 | } |
| 3349 | |
| 3350 | if (host.name) |
paul | 05865c9 | 2005-10-26 05:49:54 +0000 | [diff] [blame] | 3351 | XFREE (MTYPE_HOST, host.name); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 3352 | |
paul | 05865c9 | 2005-10-26 05:49:54 +0000 | [diff] [blame] | 3353 | host.name = XSTRDUP (MTYPE_HOST, argv[0]); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 3354 | return CMD_SUCCESS; |
| 3355 | } |
| 3356 | |
| 3357 | DEFUN (config_no_hostname, |
| 3358 | no_hostname_cmd, |
| 3359 | "no hostname [HOSTNAME]", |
| 3360 | NO_STR |
| 3361 | "Reset system's network name\n" |
| 3362 | "Host name of this router\n") |
| 3363 | { |
| 3364 | if (host.name) |
paul | 05865c9 | 2005-10-26 05:49:54 +0000 | [diff] [blame] | 3365 | XFREE (MTYPE_HOST, host.name); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 3366 | host.name = NULL; |
| 3367 | return CMD_SUCCESS; |
| 3368 | } |
| 3369 | |
| 3370 | /* VTY interface password set. */ |
| 3371 | DEFUN (config_password, password_cmd, |
| 3372 | "password (8|) WORD", |
| 3373 | "Assign the terminal connection password\n" |
| 3374 | "Specifies a HIDDEN password will follow\n" |
| 3375 | "dummy string \n" |
| 3376 | "The HIDDEN line password string\n") |
| 3377 | { |
| 3378 | /* Argument check. */ |
| 3379 | if (argc == 0) |
| 3380 | { |
| 3381 | vty_out (vty, "Please specify password.%s", VTY_NEWLINE); |
| 3382 | return CMD_WARNING; |
| 3383 | } |
| 3384 | |
| 3385 | if (argc == 2) |
| 3386 | { |
| 3387 | if (*argv[0] == '8') |
| 3388 | { |
| 3389 | if (host.password) |
paul | 05865c9 | 2005-10-26 05:49:54 +0000 | [diff] [blame] | 3390 | XFREE (MTYPE_HOST, host.password); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 3391 | host.password = NULL; |
| 3392 | if (host.password_encrypt) |
paul | 05865c9 | 2005-10-26 05:49:54 +0000 | [diff] [blame] | 3393 | XFREE (MTYPE_HOST, host.password_encrypt); |
| 3394 | host.password_encrypt = XSTRDUP (MTYPE_HOST, argv[1]); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 3395 | return CMD_SUCCESS; |
| 3396 | } |
| 3397 | else |
| 3398 | { |
| 3399 | vty_out (vty, "Unknown encryption type.%s", VTY_NEWLINE); |
| 3400 | return CMD_WARNING; |
| 3401 | } |
| 3402 | } |
| 3403 | |
| 3404 | if (!isalnum ((int) *argv[0])) |
| 3405 | { |
| 3406 | vty_out (vty, |
| 3407 | "Please specify string starting with alphanumeric%s", VTY_NEWLINE); |
| 3408 | return CMD_WARNING; |
| 3409 | } |
| 3410 | |
| 3411 | if (host.password) |
paul | 05865c9 | 2005-10-26 05:49:54 +0000 | [diff] [blame] | 3412 | XFREE (MTYPE_HOST, host.password); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 3413 | host.password = NULL; |
| 3414 | |
| 3415 | if (host.encrypt) |
| 3416 | { |
| 3417 | if (host.password_encrypt) |
paul | 05865c9 | 2005-10-26 05:49:54 +0000 | [diff] [blame] | 3418 | XFREE (MTYPE_HOST, host.password_encrypt); |
| 3419 | host.password_encrypt = XSTRDUP (MTYPE_HOST, zencrypt (argv[0])); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 3420 | } |
| 3421 | else |
paul | 05865c9 | 2005-10-26 05:49:54 +0000 | [diff] [blame] | 3422 | host.password = XSTRDUP (MTYPE_HOST, argv[0]); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 3423 | |
| 3424 | return CMD_SUCCESS; |
| 3425 | } |
| 3426 | |
| 3427 | ALIAS (config_password, password_text_cmd, |
| 3428 | "password LINE", |
| 3429 | "Assign the terminal connection password\n" |
| 3430 | "The UNENCRYPTED (cleartext) line password\n") |
| 3431 | |
| 3432 | /* VTY enable password set. */ |
| 3433 | DEFUN (config_enable_password, enable_password_cmd, |
| 3434 | "enable password (8|) WORD", |
| 3435 | "Modify enable password parameters\n" |
| 3436 | "Assign the privileged level password\n" |
| 3437 | "Specifies a HIDDEN password will follow\n" |
| 3438 | "dummy string \n" |
| 3439 | "The HIDDEN 'enable' password string\n") |
| 3440 | { |
| 3441 | /* Argument check. */ |
| 3442 | if (argc == 0) |
| 3443 | { |
| 3444 | vty_out (vty, "Please specify password.%s", VTY_NEWLINE); |
| 3445 | return CMD_WARNING; |
| 3446 | } |
| 3447 | |
| 3448 | /* Crypt type is specified. */ |
| 3449 | if (argc == 2) |
| 3450 | { |
| 3451 | if (*argv[0] == '8') |
| 3452 | { |
| 3453 | if (host.enable) |
paul | 05865c9 | 2005-10-26 05:49:54 +0000 | [diff] [blame] | 3454 | XFREE (MTYPE_HOST, host.enable); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 3455 | host.enable = NULL; |
| 3456 | |
| 3457 | if (host.enable_encrypt) |
paul | 05865c9 | 2005-10-26 05:49:54 +0000 | [diff] [blame] | 3458 | XFREE (MTYPE_HOST, host.enable_encrypt); |
| 3459 | host.enable_encrypt = XSTRDUP (MTYPE_HOST, argv[1]); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 3460 | |
| 3461 | return CMD_SUCCESS; |
| 3462 | } |
| 3463 | else |
| 3464 | { |
| 3465 | vty_out (vty, "Unknown encryption type.%s", VTY_NEWLINE); |
| 3466 | return CMD_WARNING; |
| 3467 | } |
| 3468 | } |
| 3469 | |
| 3470 | if (!isalnum ((int) *argv[0])) |
| 3471 | { |
| 3472 | vty_out (vty, |
| 3473 | "Please specify string starting with alphanumeric%s", VTY_NEWLINE); |
| 3474 | return CMD_WARNING; |
| 3475 | } |
| 3476 | |
| 3477 | if (host.enable) |
paul | 05865c9 | 2005-10-26 05:49:54 +0000 | [diff] [blame] | 3478 | XFREE (MTYPE_HOST, host.enable); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 3479 | host.enable = NULL; |
| 3480 | |
| 3481 | /* Plain password input. */ |
| 3482 | if (host.encrypt) |
| 3483 | { |
| 3484 | if (host.enable_encrypt) |
paul | 05865c9 | 2005-10-26 05:49:54 +0000 | [diff] [blame] | 3485 | XFREE (MTYPE_HOST, host.enable_encrypt); |
| 3486 | host.enable_encrypt = XSTRDUP (MTYPE_HOST, zencrypt (argv[0])); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 3487 | } |
| 3488 | else |
paul | 05865c9 | 2005-10-26 05:49:54 +0000 | [diff] [blame] | 3489 | host.enable = XSTRDUP (MTYPE_HOST, argv[0]); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 3490 | |
| 3491 | return CMD_SUCCESS; |
| 3492 | } |
| 3493 | |
| 3494 | ALIAS (config_enable_password, |
| 3495 | enable_password_text_cmd, |
| 3496 | "enable password LINE", |
| 3497 | "Modify enable password parameters\n" |
| 3498 | "Assign the privileged level password\n" |
| 3499 | "The UNENCRYPTED (cleartext) 'enable' password\n") |
| 3500 | |
| 3501 | /* VTY enable password delete. */ |
| 3502 | DEFUN (no_config_enable_password, no_enable_password_cmd, |
| 3503 | "no enable password", |
| 3504 | NO_STR |
| 3505 | "Modify enable password parameters\n" |
| 3506 | "Assign the privileged level password\n") |
| 3507 | { |
| 3508 | if (host.enable) |
paul | 05865c9 | 2005-10-26 05:49:54 +0000 | [diff] [blame] | 3509 | XFREE (MTYPE_HOST, host.enable); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 3510 | host.enable = NULL; |
| 3511 | |
| 3512 | if (host.enable_encrypt) |
paul | 05865c9 | 2005-10-26 05:49:54 +0000 | [diff] [blame] | 3513 | XFREE (MTYPE_HOST, host.enable_encrypt); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 3514 | host.enable_encrypt = NULL; |
| 3515 | |
| 3516 | return CMD_SUCCESS; |
| 3517 | } |
| 3518 | |
| 3519 | DEFUN (service_password_encrypt, |
| 3520 | service_password_encrypt_cmd, |
| 3521 | "service password-encryption", |
| 3522 | "Set up miscellaneous service\n" |
| 3523 | "Enable encrypted passwords\n") |
| 3524 | { |
| 3525 | if (host.encrypt) |
| 3526 | return CMD_SUCCESS; |
| 3527 | |
| 3528 | host.encrypt = 1; |
| 3529 | |
| 3530 | if (host.password) |
| 3531 | { |
| 3532 | if (host.password_encrypt) |
paul | 05865c9 | 2005-10-26 05:49:54 +0000 | [diff] [blame] | 3533 | XFREE (MTYPE_HOST, host.password_encrypt); |
| 3534 | host.password_encrypt = XSTRDUP (MTYPE_HOST, zencrypt (host.password)); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 3535 | } |
| 3536 | if (host.enable) |
| 3537 | { |
| 3538 | if (host.enable_encrypt) |
paul | 05865c9 | 2005-10-26 05:49:54 +0000 | [diff] [blame] | 3539 | XFREE (MTYPE_HOST, host.enable_encrypt); |
| 3540 | host.enable_encrypt = XSTRDUP (MTYPE_HOST, zencrypt (host.enable)); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 3541 | } |
| 3542 | |
| 3543 | return CMD_SUCCESS; |
| 3544 | } |
| 3545 | |
| 3546 | DEFUN (no_service_password_encrypt, |
| 3547 | no_service_password_encrypt_cmd, |
| 3548 | "no service password-encryption", |
| 3549 | NO_STR |
| 3550 | "Set up miscellaneous service\n" |
| 3551 | "Enable encrypted passwords\n") |
| 3552 | { |
| 3553 | if (! host.encrypt) |
| 3554 | return CMD_SUCCESS; |
| 3555 | |
| 3556 | host.encrypt = 0; |
| 3557 | |
| 3558 | if (host.password_encrypt) |
paul | 05865c9 | 2005-10-26 05:49:54 +0000 | [diff] [blame] | 3559 | XFREE (MTYPE_HOST, host.password_encrypt); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 3560 | host.password_encrypt = NULL; |
| 3561 | |
| 3562 | if (host.enable_encrypt) |
paul | 05865c9 | 2005-10-26 05:49:54 +0000 | [diff] [blame] | 3563 | XFREE (MTYPE_HOST, host.enable_encrypt); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 3564 | host.enable_encrypt = NULL; |
| 3565 | |
| 3566 | return CMD_SUCCESS; |
| 3567 | } |
| 3568 | |
| 3569 | DEFUN (config_terminal_length, config_terminal_length_cmd, |
| 3570 | "terminal length <0-512>", |
| 3571 | "Set terminal line parameters\n" |
| 3572 | "Set number of lines on a screen\n" |
| 3573 | "Number of lines on screen (0 for no pausing)\n") |
| 3574 | { |
| 3575 | int lines; |
| 3576 | char *endptr = NULL; |
| 3577 | |
| 3578 | lines = strtol (argv[0], &endptr, 10); |
| 3579 | if (lines < 0 || lines > 512 || *endptr != '\0') |
| 3580 | { |
| 3581 | vty_out (vty, "length is malformed%s", VTY_NEWLINE); |
| 3582 | return CMD_WARNING; |
| 3583 | } |
| 3584 | vty->lines = lines; |
| 3585 | |
| 3586 | return CMD_SUCCESS; |
| 3587 | } |
| 3588 | |
| 3589 | DEFUN (config_terminal_no_length, config_terminal_no_length_cmd, |
| 3590 | "terminal no length", |
| 3591 | "Set terminal line parameters\n" |
| 3592 | NO_STR |
| 3593 | "Set number of lines on a screen\n") |
| 3594 | { |
| 3595 | vty->lines = -1; |
| 3596 | return CMD_SUCCESS; |
| 3597 | } |
| 3598 | |
| 3599 | DEFUN (service_terminal_length, service_terminal_length_cmd, |
| 3600 | "service terminal-length <0-512>", |
| 3601 | "Set up miscellaneous service\n" |
| 3602 | "System wide terminal length configuration\n" |
| 3603 | "Number of lines of VTY (0 means no line control)\n") |
| 3604 | { |
| 3605 | int lines; |
| 3606 | char *endptr = NULL; |
| 3607 | |
| 3608 | lines = strtol (argv[0], &endptr, 10); |
| 3609 | if (lines < 0 || lines > 512 || *endptr != '\0') |
| 3610 | { |
| 3611 | vty_out (vty, "length is malformed%s", VTY_NEWLINE); |
| 3612 | return CMD_WARNING; |
| 3613 | } |
| 3614 | host.lines = lines; |
| 3615 | |
| 3616 | return CMD_SUCCESS; |
| 3617 | } |
| 3618 | |
| 3619 | DEFUN (no_service_terminal_length, no_service_terminal_length_cmd, |
| 3620 | "no service terminal-length [<0-512>]", |
| 3621 | NO_STR |
| 3622 | "Set up miscellaneous service\n" |
| 3623 | "System wide terminal length configuration\n" |
| 3624 | "Number of lines of VTY (0 means no line control)\n") |
| 3625 | { |
| 3626 | host.lines = -1; |
| 3627 | return CMD_SUCCESS; |
| 3628 | } |
| 3629 | |
ajs | 2885f72 | 2004-12-17 23:16:33 +0000 | [diff] [blame] | 3630 | DEFUN_HIDDEN (do_echo, |
| 3631 | echo_cmd, |
| 3632 | "echo .MESSAGE", |
| 3633 | "Echo a message back to the vty\n" |
| 3634 | "The message to echo\n") |
| 3635 | { |
| 3636 | char *message; |
| 3637 | |
ajs | f6834d4 | 2005-01-28 20:28:35 +0000 | [diff] [blame] | 3638 | vty_out (vty, "%s%s", ((message = argv_concat(argv, argc, 0)) ? message : ""), |
| 3639 | VTY_NEWLINE); |
| 3640 | if (message) |
| 3641 | XFREE(MTYPE_TMP, message); |
ajs | 2885f72 | 2004-12-17 23:16:33 +0000 | [diff] [blame] | 3642 | return CMD_SUCCESS; |
| 3643 | } |
| 3644 | |
ajs | 274a4a4 | 2004-12-07 15:39:31 +0000 | [diff] [blame] | 3645 | DEFUN (config_logmsg, |
| 3646 | config_logmsg_cmd, |
| 3647 | "logmsg "LOG_LEVELS" .MESSAGE", |
| 3648 | "Send a message to enabled logging destinations\n" |
| 3649 | LOG_LEVEL_DESC |
| 3650 | "The message to send\n") |
| 3651 | { |
| 3652 | int level; |
| 3653 | char *message; |
| 3654 | |
| 3655 | if ((level = level_match(argv[0])) == ZLOG_DISABLED) |
| 3656 | return CMD_ERR_NO_MATCH; |
| 3657 | |
Christian Hammers | fc95186 | 2011-03-23 13:07:55 +0300 | [diff] [blame] | 3658 | zlog(NULL, level, "%s", ((message = argv_concat(argv, argc, 1)) ? message : "")); |
ajs | f6834d4 | 2005-01-28 20:28:35 +0000 | [diff] [blame] | 3659 | if (message) |
| 3660 | XFREE(MTYPE_TMP, message); |
ajs | 274a4a4 | 2004-12-07 15:39:31 +0000 | [diff] [blame] | 3661 | return CMD_SUCCESS; |
| 3662 | } |
| 3663 | |
| 3664 | DEFUN (show_logging, |
| 3665 | show_logging_cmd, |
| 3666 | "show logging", |
| 3667 | SHOW_STR |
| 3668 | "Show current logging configuration\n") |
| 3669 | { |
| 3670 | struct zlog *zl = zlog_default; |
| 3671 | |
| 3672 | vty_out (vty, "Syslog logging: "); |
| 3673 | if (zl->maxlvl[ZLOG_DEST_SYSLOG] == ZLOG_DISABLED) |
| 3674 | vty_out (vty, "disabled"); |
| 3675 | else |
| 3676 | vty_out (vty, "level %s, facility %s, ident %s", |
| 3677 | zlog_priority[zl->maxlvl[ZLOG_DEST_SYSLOG]], |
| 3678 | facility_name(zl->facility), zl->ident); |
| 3679 | vty_out (vty, "%s", VTY_NEWLINE); |
| 3680 | |
| 3681 | vty_out (vty, "Stdout logging: "); |
| 3682 | if (zl->maxlvl[ZLOG_DEST_STDOUT] == ZLOG_DISABLED) |
| 3683 | vty_out (vty, "disabled"); |
| 3684 | else |
| 3685 | vty_out (vty, "level %s", |
| 3686 | zlog_priority[zl->maxlvl[ZLOG_DEST_STDOUT]]); |
| 3687 | vty_out (vty, "%s", VTY_NEWLINE); |
| 3688 | |
| 3689 | vty_out (vty, "Monitor logging: "); |
| 3690 | if (zl->maxlvl[ZLOG_DEST_MONITOR] == ZLOG_DISABLED) |
| 3691 | vty_out (vty, "disabled"); |
| 3692 | else |
| 3693 | vty_out (vty, "level %s", |
| 3694 | zlog_priority[zl->maxlvl[ZLOG_DEST_MONITOR]]); |
| 3695 | vty_out (vty, "%s", VTY_NEWLINE); |
| 3696 | |
| 3697 | vty_out (vty, "File logging: "); |
| 3698 | if ((zl->maxlvl[ZLOG_DEST_FILE] == ZLOG_DISABLED) || |
| 3699 | !zl->fp) |
| 3700 | vty_out (vty, "disabled"); |
| 3701 | else |
| 3702 | vty_out (vty, "level %s, filename %s", |
| 3703 | zlog_priority[zl->maxlvl[ZLOG_DEST_FILE]], |
| 3704 | zl->filename); |
| 3705 | vty_out (vty, "%s", VTY_NEWLINE); |
| 3706 | |
| 3707 | vty_out (vty, "Protocol name: %s%s", |
| 3708 | zlog_proto_names[zl->protocol], VTY_NEWLINE); |
| 3709 | vty_out (vty, "Record priority: %s%s", |
| 3710 | (zl->record_priority ? "enabled" : "disabled"), VTY_NEWLINE); |
Andrew J. Schorr | 1ed72e0 | 2007-04-28 22:14:10 +0000 | [diff] [blame] | 3711 | vty_out (vty, "Timestamp precision: %d%s", |
| 3712 | zl->timestamp_precision, VTY_NEWLINE); |
ajs | 274a4a4 | 2004-12-07 15:39:31 +0000 | [diff] [blame] | 3713 | |
| 3714 | return CMD_SUCCESS; |
| 3715 | } |
| 3716 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 3717 | DEFUN (config_log_stdout, |
| 3718 | config_log_stdout_cmd, |
| 3719 | "log stdout", |
| 3720 | "Logging control\n" |
ajs | 274a4a4 | 2004-12-07 15:39:31 +0000 | [diff] [blame] | 3721 | "Set stdout logging level\n") |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 3722 | { |
ajs | 274a4a4 | 2004-12-07 15:39:31 +0000 | [diff] [blame] | 3723 | zlog_set_level (NULL, ZLOG_DEST_STDOUT, zlog_default->default_lvl); |
| 3724 | return CMD_SUCCESS; |
| 3725 | } |
| 3726 | |
| 3727 | DEFUN (config_log_stdout_level, |
| 3728 | config_log_stdout_level_cmd, |
| 3729 | "log stdout "LOG_LEVELS, |
| 3730 | "Logging control\n" |
| 3731 | "Set stdout logging level\n" |
| 3732 | LOG_LEVEL_DESC) |
| 3733 | { |
| 3734 | int level; |
| 3735 | |
| 3736 | if ((level = level_match(argv[0])) == ZLOG_DISABLED) |
| 3737 | return CMD_ERR_NO_MATCH; |
| 3738 | zlog_set_level (NULL, ZLOG_DEST_STDOUT, level); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 3739 | return CMD_SUCCESS; |
| 3740 | } |
| 3741 | |
| 3742 | DEFUN (no_config_log_stdout, |
| 3743 | no_config_log_stdout_cmd, |
ajs | 274a4a4 | 2004-12-07 15:39:31 +0000 | [diff] [blame] | 3744 | "no log stdout [LEVEL]", |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 3745 | NO_STR |
| 3746 | "Logging control\n" |
ajs | 274a4a4 | 2004-12-07 15:39:31 +0000 | [diff] [blame] | 3747 | "Cancel logging to stdout\n" |
| 3748 | "Logging level\n") |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 3749 | { |
ajs | 274a4a4 | 2004-12-07 15:39:31 +0000 | [diff] [blame] | 3750 | zlog_set_level (NULL, ZLOG_DEST_STDOUT, ZLOG_DISABLED); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 3751 | return CMD_SUCCESS; |
| 3752 | } |
| 3753 | |
ajs | 274a4a4 | 2004-12-07 15:39:31 +0000 | [diff] [blame] | 3754 | DEFUN (config_log_monitor, |
| 3755 | config_log_monitor_cmd, |
| 3756 | "log monitor", |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 3757 | "Logging control\n" |
ajs | 274a4a4 | 2004-12-07 15:39:31 +0000 | [diff] [blame] | 3758 | "Set terminal line (monitor) logging level\n") |
| 3759 | { |
| 3760 | zlog_set_level (NULL, ZLOG_DEST_MONITOR, zlog_default->default_lvl); |
| 3761 | return CMD_SUCCESS; |
| 3762 | } |
| 3763 | |
| 3764 | DEFUN (config_log_monitor_level, |
| 3765 | config_log_monitor_level_cmd, |
| 3766 | "log monitor "LOG_LEVELS, |
| 3767 | "Logging control\n" |
| 3768 | "Set terminal line (monitor) logging level\n" |
| 3769 | LOG_LEVEL_DESC) |
| 3770 | { |
| 3771 | int level; |
| 3772 | |
| 3773 | if ((level = level_match(argv[0])) == ZLOG_DISABLED) |
| 3774 | return CMD_ERR_NO_MATCH; |
| 3775 | zlog_set_level (NULL, ZLOG_DEST_MONITOR, level); |
| 3776 | return CMD_SUCCESS; |
| 3777 | } |
| 3778 | |
| 3779 | DEFUN (no_config_log_monitor, |
| 3780 | no_config_log_monitor_cmd, |
| 3781 | "no log monitor [LEVEL]", |
| 3782 | NO_STR |
| 3783 | "Logging control\n" |
| 3784 | "Disable terminal line (monitor) logging\n" |
| 3785 | "Logging level\n") |
| 3786 | { |
| 3787 | zlog_set_level (NULL, ZLOG_DEST_MONITOR, ZLOG_DISABLED); |
| 3788 | return CMD_SUCCESS; |
| 3789 | } |
| 3790 | |
| 3791 | static int |
| 3792 | set_log_file(struct vty *vty, const char *fname, int loglevel) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 3793 | { |
| 3794 | int ret; |
paul | 9035efa | 2004-10-10 11:56:56 +0000 | [diff] [blame] | 3795 | char *p = NULL; |
| 3796 | const char *fullpath; |
| 3797 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 3798 | /* Path detection. */ |
ajs | 274a4a4 | 2004-12-07 15:39:31 +0000 | [diff] [blame] | 3799 | if (! IS_DIRECTORY_SEP (*fname)) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 3800 | { |
paul | 9035efa | 2004-10-10 11:56:56 +0000 | [diff] [blame] | 3801 | char cwd[MAXPATHLEN+1]; |
| 3802 | cwd[MAXPATHLEN] = '\0'; |
| 3803 | |
| 3804 | if (getcwd (cwd, MAXPATHLEN) == NULL) |
| 3805 | { |
| 3806 | zlog_err ("config_log_file: Unable to alloc mem!"); |
| 3807 | return CMD_WARNING; |
| 3808 | } |
| 3809 | |
ajs | 274a4a4 | 2004-12-07 15:39:31 +0000 | [diff] [blame] | 3810 | if ( (p = XMALLOC (MTYPE_TMP, strlen (cwd) + strlen (fname) + 2)) |
paul | 9035efa | 2004-10-10 11:56:56 +0000 | [diff] [blame] | 3811 | == NULL) |
| 3812 | { |
| 3813 | zlog_err ("config_log_file: Unable to alloc mem!"); |
| 3814 | return CMD_WARNING; |
| 3815 | } |
ajs | 274a4a4 | 2004-12-07 15:39:31 +0000 | [diff] [blame] | 3816 | sprintf (p, "%s/%s", cwd, fname); |
paul | 9035efa | 2004-10-10 11:56:56 +0000 | [diff] [blame] | 3817 | fullpath = p; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 3818 | } |
| 3819 | else |
ajs | 274a4a4 | 2004-12-07 15:39:31 +0000 | [diff] [blame] | 3820 | fullpath = fname; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 3821 | |
ajs | 274a4a4 | 2004-12-07 15:39:31 +0000 | [diff] [blame] | 3822 | ret = zlog_set_file (NULL, fullpath, loglevel); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 3823 | |
paul | 9035efa | 2004-10-10 11:56:56 +0000 | [diff] [blame] | 3824 | if (p) |
| 3825 | XFREE (MTYPE_TMP, p); |
| 3826 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 3827 | if (!ret) |
| 3828 | { |
ajs | 274a4a4 | 2004-12-07 15:39:31 +0000 | [diff] [blame] | 3829 | vty_out (vty, "can't open logfile %s\n", fname); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 3830 | return CMD_WARNING; |
| 3831 | } |
| 3832 | |
| 3833 | if (host.logfile) |
paul | 05865c9 | 2005-10-26 05:49:54 +0000 | [diff] [blame] | 3834 | XFREE (MTYPE_HOST, host.logfile); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 3835 | |
paul | 05865c9 | 2005-10-26 05:49:54 +0000 | [diff] [blame] | 3836 | host.logfile = XSTRDUP (MTYPE_HOST, fname); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 3837 | |
| 3838 | return CMD_SUCCESS; |
| 3839 | } |
| 3840 | |
ajs | 274a4a4 | 2004-12-07 15:39:31 +0000 | [diff] [blame] | 3841 | DEFUN (config_log_file, |
| 3842 | config_log_file_cmd, |
| 3843 | "log file FILENAME", |
| 3844 | "Logging control\n" |
| 3845 | "Logging to file\n" |
| 3846 | "Logging filename\n") |
| 3847 | { |
| 3848 | return set_log_file(vty, argv[0], zlog_default->default_lvl); |
| 3849 | } |
| 3850 | |
| 3851 | DEFUN (config_log_file_level, |
| 3852 | config_log_file_level_cmd, |
| 3853 | "log file FILENAME "LOG_LEVELS, |
| 3854 | "Logging control\n" |
| 3855 | "Logging to file\n" |
| 3856 | "Logging filename\n" |
| 3857 | LOG_LEVEL_DESC) |
| 3858 | { |
| 3859 | int level; |
| 3860 | |
| 3861 | if ((level = level_match(argv[1])) == ZLOG_DISABLED) |
| 3862 | return CMD_ERR_NO_MATCH; |
| 3863 | return set_log_file(vty, argv[0], level); |
| 3864 | } |
| 3865 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 3866 | DEFUN (no_config_log_file, |
| 3867 | no_config_log_file_cmd, |
| 3868 | "no log file [FILENAME]", |
| 3869 | NO_STR |
| 3870 | "Logging control\n" |
| 3871 | "Cancel logging to file\n" |
| 3872 | "Logging file name\n") |
| 3873 | { |
| 3874 | zlog_reset_file (NULL); |
| 3875 | |
| 3876 | if (host.logfile) |
paul | 05865c9 | 2005-10-26 05:49:54 +0000 | [diff] [blame] | 3877 | XFREE (MTYPE_HOST, host.logfile); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 3878 | |
| 3879 | host.logfile = NULL; |
| 3880 | |
| 3881 | return CMD_SUCCESS; |
| 3882 | } |
| 3883 | |
ajs | 274a4a4 | 2004-12-07 15:39:31 +0000 | [diff] [blame] | 3884 | ALIAS (no_config_log_file, |
| 3885 | no_config_log_file_level_cmd, |
| 3886 | "no log file FILENAME LEVEL", |
| 3887 | NO_STR |
| 3888 | "Logging control\n" |
| 3889 | "Cancel logging to file\n" |
| 3890 | "Logging file name\n" |
| 3891 | "Logging level\n") |
| 3892 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 3893 | DEFUN (config_log_syslog, |
| 3894 | config_log_syslog_cmd, |
| 3895 | "log syslog", |
| 3896 | "Logging control\n" |
ajs | 274a4a4 | 2004-12-07 15:39:31 +0000 | [diff] [blame] | 3897 | "Set syslog logging level\n") |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 3898 | { |
ajs | 274a4a4 | 2004-12-07 15:39:31 +0000 | [diff] [blame] | 3899 | zlog_set_level (NULL, ZLOG_DEST_SYSLOG, zlog_default->default_lvl); |
paul | 12ab19f | 2003-07-26 06:14:55 +0000 | [diff] [blame] | 3900 | return CMD_SUCCESS; |
| 3901 | } |
| 3902 | |
ajs | 274a4a4 | 2004-12-07 15:39:31 +0000 | [diff] [blame] | 3903 | DEFUN (config_log_syslog_level, |
| 3904 | config_log_syslog_level_cmd, |
| 3905 | "log syslog "LOG_LEVELS, |
paul | 12ab19f | 2003-07-26 06:14:55 +0000 | [diff] [blame] | 3906 | "Logging control\n" |
ajs | 274a4a4 | 2004-12-07 15:39:31 +0000 | [diff] [blame] | 3907 | "Set syslog logging level\n" |
| 3908 | LOG_LEVEL_DESC) |
paul | 12ab19f | 2003-07-26 06:14:55 +0000 | [diff] [blame] | 3909 | { |
ajs | 274a4a4 | 2004-12-07 15:39:31 +0000 | [diff] [blame] | 3910 | int level; |
paul | 12ab19f | 2003-07-26 06:14:55 +0000 | [diff] [blame] | 3911 | |
ajs | 274a4a4 | 2004-12-07 15:39:31 +0000 | [diff] [blame] | 3912 | if ((level = level_match(argv[0])) == ZLOG_DISABLED) |
| 3913 | return CMD_ERR_NO_MATCH; |
| 3914 | zlog_set_level (NULL, ZLOG_DEST_SYSLOG, level); |
| 3915 | return CMD_SUCCESS; |
| 3916 | } |
paul | 12ab19f | 2003-07-26 06:14:55 +0000 | [diff] [blame] | 3917 | |
ajs | 274a4a4 | 2004-12-07 15:39:31 +0000 | [diff] [blame] | 3918 | DEFUN_DEPRECATED (config_log_syslog_facility, |
| 3919 | config_log_syslog_facility_cmd, |
| 3920 | "log syslog facility "LOG_FACILITIES, |
| 3921 | "Logging control\n" |
| 3922 | "Logging goes to syslog\n" |
| 3923 | "(Deprecated) Facility parameter for syslog messages\n" |
| 3924 | LOG_FACILITY_DESC) |
| 3925 | { |
| 3926 | int facility; |
paul | 12ab19f | 2003-07-26 06:14:55 +0000 | [diff] [blame] | 3927 | |
ajs | 274a4a4 | 2004-12-07 15:39:31 +0000 | [diff] [blame] | 3928 | if ((facility = facility_match(argv[0])) < 0) |
| 3929 | return CMD_ERR_NO_MATCH; |
| 3930 | |
| 3931 | zlog_set_level (NULL, ZLOG_DEST_SYSLOG, zlog_default->default_lvl); |
paul | 12ab19f | 2003-07-26 06:14:55 +0000 | [diff] [blame] | 3932 | zlog_default->facility = facility; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 3933 | return CMD_SUCCESS; |
| 3934 | } |
| 3935 | |
| 3936 | DEFUN (no_config_log_syslog, |
| 3937 | no_config_log_syslog_cmd, |
ajs | 274a4a4 | 2004-12-07 15:39:31 +0000 | [diff] [blame] | 3938 | "no log syslog [LEVEL]", |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 3939 | NO_STR |
| 3940 | "Logging control\n" |
ajs | 274a4a4 | 2004-12-07 15:39:31 +0000 | [diff] [blame] | 3941 | "Cancel logging to syslog\n" |
| 3942 | "Logging level\n") |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 3943 | { |
ajs | 274a4a4 | 2004-12-07 15:39:31 +0000 | [diff] [blame] | 3944 | zlog_set_level (NULL, ZLOG_DEST_SYSLOG, ZLOG_DISABLED); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 3945 | return CMD_SUCCESS; |
| 3946 | } |
| 3947 | |
paul | 12ab19f | 2003-07-26 06:14:55 +0000 | [diff] [blame] | 3948 | ALIAS (no_config_log_syslog, |
| 3949 | no_config_log_syslog_facility_cmd, |
ajs | 274a4a4 | 2004-12-07 15:39:31 +0000 | [diff] [blame] | 3950 | "no log syslog facility "LOG_FACILITIES, |
paul | 12ab19f | 2003-07-26 06:14:55 +0000 | [diff] [blame] | 3951 | NO_STR |
| 3952 | "Logging control\n" |
| 3953 | "Logging goes to syslog\n" |
| 3954 | "Facility parameter for syslog messages\n" |
ajs | 274a4a4 | 2004-12-07 15:39:31 +0000 | [diff] [blame] | 3955 | LOG_FACILITY_DESC) |
paul | 12ab19f | 2003-07-26 06:14:55 +0000 | [diff] [blame] | 3956 | |
ajs | 274a4a4 | 2004-12-07 15:39:31 +0000 | [diff] [blame] | 3957 | DEFUN (config_log_facility, |
| 3958 | config_log_facility_cmd, |
| 3959 | "log facility "LOG_FACILITIES, |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 3960 | "Logging control\n" |
ajs | 274a4a4 | 2004-12-07 15:39:31 +0000 | [diff] [blame] | 3961 | "Facility parameter for syslog messages\n" |
| 3962 | LOG_FACILITY_DESC) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 3963 | { |
ajs | 274a4a4 | 2004-12-07 15:39:31 +0000 | [diff] [blame] | 3964 | int facility; |
| 3965 | |
| 3966 | if ((facility = facility_match(argv[0])) < 0) |
| 3967 | return CMD_ERR_NO_MATCH; |
| 3968 | zlog_default->facility = facility; |
| 3969 | return CMD_SUCCESS; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 3970 | } |
| 3971 | |
ajs | 274a4a4 | 2004-12-07 15:39:31 +0000 | [diff] [blame] | 3972 | DEFUN (no_config_log_facility, |
| 3973 | no_config_log_facility_cmd, |
| 3974 | "no log facility [FACILITY]", |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 3975 | NO_STR |
| 3976 | "Logging control\n" |
ajs | 274a4a4 | 2004-12-07 15:39:31 +0000 | [diff] [blame] | 3977 | "Reset syslog facility to default (daemon)\n" |
| 3978 | "Syslog facility\n") |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 3979 | { |
ajs | 274a4a4 | 2004-12-07 15:39:31 +0000 | [diff] [blame] | 3980 | zlog_default->facility = LOG_DAEMON; |
| 3981 | return CMD_SUCCESS; |
| 3982 | } |
| 3983 | |
| 3984 | DEFUN_DEPRECATED (config_log_trap, |
| 3985 | config_log_trap_cmd, |
| 3986 | "log trap "LOG_LEVELS, |
| 3987 | "Logging control\n" |
| 3988 | "(Deprecated) Set logging level and default for all destinations\n" |
| 3989 | LOG_LEVEL_DESC) |
| 3990 | { |
| 3991 | int new_level ; |
| 3992 | int i; |
| 3993 | |
| 3994 | if ((new_level = level_match(argv[0])) == ZLOG_DISABLED) |
| 3995 | return CMD_ERR_NO_MATCH; |
| 3996 | |
| 3997 | zlog_default->default_lvl = new_level; |
| 3998 | for (i = 0; i < ZLOG_NUM_DESTS; i++) |
| 3999 | if (zlog_default->maxlvl[i] != ZLOG_DISABLED) |
| 4000 | zlog_default->maxlvl[i] = new_level; |
| 4001 | return CMD_SUCCESS; |
| 4002 | } |
| 4003 | |
| 4004 | DEFUN_DEPRECATED (no_config_log_trap, |
| 4005 | no_config_log_trap_cmd, |
| 4006 | "no log trap [LEVEL]", |
| 4007 | NO_STR |
| 4008 | "Logging control\n" |
| 4009 | "Permit all logging information\n" |
| 4010 | "Logging level\n") |
| 4011 | { |
| 4012 | zlog_default->default_lvl = LOG_DEBUG; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 4013 | return CMD_SUCCESS; |
| 4014 | } |
| 4015 | |
| 4016 | DEFUN (config_log_record_priority, |
| 4017 | config_log_record_priority_cmd, |
| 4018 | "log record-priority", |
| 4019 | "Logging control\n" |
| 4020 | "Log the priority of the message within the message\n") |
| 4021 | { |
| 4022 | zlog_default->record_priority = 1 ; |
| 4023 | return CMD_SUCCESS; |
| 4024 | } |
| 4025 | |
| 4026 | DEFUN (no_config_log_record_priority, |
| 4027 | no_config_log_record_priority_cmd, |
| 4028 | "no log record-priority", |
| 4029 | NO_STR |
| 4030 | "Logging control\n" |
| 4031 | "Do not log the priority of the message within the message\n") |
| 4032 | { |
| 4033 | zlog_default->record_priority = 0 ; |
| 4034 | return CMD_SUCCESS; |
| 4035 | } |
| 4036 | |
Andrew J. Schorr | 1ed72e0 | 2007-04-28 22:14:10 +0000 | [diff] [blame] | 4037 | DEFUN (config_log_timestamp_precision, |
| 4038 | config_log_timestamp_precision_cmd, |
| 4039 | "log timestamp precision <0-6>", |
| 4040 | "Logging control\n" |
| 4041 | "Timestamp configuration\n" |
| 4042 | "Set the timestamp precision\n" |
| 4043 | "Number of subsecond digits\n") |
| 4044 | { |
| 4045 | if (argc != 1) |
| 4046 | { |
| 4047 | vty_out (vty, "Insufficient arguments%s", VTY_NEWLINE); |
| 4048 | return CMD_WARNING; |
| 4049 | } |
| 4050 | |
| 4051 | VTY_GET_INTEGER_RANGE("Timestamp Precision", |
| 4052 | zlog_default->timestamp_precision, argv[0], 0, 6); |
| 4053 | return CMD_SUCCESS; |
| 4054 | } |
| 4055 | |
| 4056 | DEFUN (no_config_log_timestamp_precision, |
| 4057 | no_config_log_timestamp_precision_cmd, |
| 4058 | "no log timestamp precision", |
| 4059 | NO_STR |
| 4060 | "Logging control\n" |
| 4061 | "Timestamp configuration\n" |
| 4062 | "Reset the timestamp precision to the default value of 0\n") |
| 4063 | { |
| 4064 | zlog_default->timestamp_precision = 0 ; |
| 4065 | return CMD_SUCCESS; |
| 4066 | } |
| 4067 | |
paul | 3b0c5d9 | 2005-03-08 10:43:43 +0000 | [diff] [blame] | 4068 | DEFUN (banner_motd_file, |
| 4069 | banner_motd_file_cmd, |
| 4070 | "banner motd file [FILE]", |
| 4071 | "Set banner\n" |
| 4072 | "Banner for motd\n" |
| 4073 | "Banner from a file\n" |
| 4074 | "Filename\n") |
| 4075 | { |
paul | b45da6f | 2005-03-08 15:16:57 +0000 | [diff] [blame] | 4076 | if (host.motdfile) |
paul | 05865c9 | 2005-10-26 05:49:54 +0000 | [diff] [blame] | 4077 | XFREE (MTYPE_HOST, host.motdfile); |
| 4078 | host.motdfile = XSTRDUP (MTYPE_HOST, argv[0]); |
paul | b45da6f | 2005-03-08 15:16:57 +0000 | [diff] [blame] | 4079 | |
paul | 3b0c5d9 | 2005-03-08 10:43:43 +0000 | [diff] [blame] | 4080 | return CMD_SUCCESS; |
| 4081 | } |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 4082 | |
| 4083 | DEFUN (banner_motd_default, |
| 4084 | banner_motd_default_cmd, |
| 4085 | "banner motd default", |
| 4086 | "Set banner string\n" |
| 4087 | "Strings for motd\n" |
| 4088 | "Default string\n") |
| 4089 | { |
| 4090 | host.motd = default_motd; |
| 4091 | return CMD_SUCCESS; |
| 4092 | } |
| 4093 | |
| 4094 | DEFUN (no_banner_motd, |
| 4095 | no_banner_motd_cmd, |
| 4096 | "no banner motd", |
| 4097 | NO_STR |
| 4098 | "Set banner string\n" |
| 4099 | "Strings for motd\n") |
| 4100 | { |
| 4101 | host.motd = NULL; |
paul | 2208518 | 2005-03-08 16:00:12 +0000 | [diff] [blame] | 4102 | if (host.motdfile) |
paul | 05865c9 | 2005-10-26 05:49:54 +0000 | [diff] [blame] | 4103 | XFREE (MTYPE_HOST, host.motdfile); |
paul | 3b0c5d9 | 2005-03-08 10:43:43 +0000 | [diff] [blame] | 4104 | host.motdfile = NULL; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 4105 | return CMD_SUCCESS; |
| 4106 | } |
| 4107 | |
Lou Berger | f9ec419 | 2016-01-12 13:41:48 -0500 | [diff] [blame] | 4108 | DEFUN (show_commandtree, |
| 4109 | show_commandtree_cmd, |
| 4110 | "show commandtree", |
| 4111 | NO_STR |
| 4112 | "Show command tree\n") |
| 4113 | { |
| 4114 | /* TBD */ |
| 4115 | vector cmd_vector; |
| 4116 | unsigned int i; |
| 4117 | |
| 4118 | vty_out (vty, "Current node id: %d%s", vty->node, VTY_NEWLINE); |
| 4119 | |
| 4120 | /* vector of all commands installed at this node */ |
| 4121 | cmd_vector = vector_copy (cmd_node_vector (cmdvec, vty->node)); |
| 4122 | |
| 4123 | /* loop over all commands at this node */ |
| 4124 | for (i = 0; i < vector_active(cmd_vector); ++i) |
| 4125 | { |
| 4126 | struct cmd_element *cmd_element; |
| 4127 | |
| 4128 | /* A cmd_element (seems to be) is an individual command */ |
| 4129 | if ((cmd_element = vector_slot (cmd_vector, i)) == NULL) |
| 4130 | continue; |
| 4131 | |
| 4132 | vty_out (vty, " %s%s", cmd_element->string, VTY_NEWLINE); |
| 4133 | } |
| 4134 | |
| 4135 | vector_free (cmd_vector); |
| 4136 | return CMD_SUCCESS; |
| 4137 | } |
| 4138 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 4139 | /* Set config filename. Called from vty.c */ |
| 4140 | void |
| 4141 | host_config_set (char *filename) |
| 4142 | { |
Chris Caputo | 228da42 | 2009-07-18 05:44:03 +0000 | [diff] [blame] | 4143 | if (host.config) |
| 4144 | XFREE (MTYPE_HOST, host.config); |
paul | 05865c9 | 2005-10-26 05:49:54 +0000 | [diff] [blame] | 4145 | host.config = XSTRDUP (MTYPE_HOST, filename); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 4146 | } |
| 4147 | |
Christian Franke | 41de629 | 2016-05-03 19:59:41 +0200 | [diff] [blame] | 4148 | const char * |
| 4149 | host_config_get (void) |
| 4150 | { |
| 4151 | return host.config; |
| 4152 | } |
| 4153 | |
Paul Jakma | 9219366 | 2016-06-16 15:53:26 +0100 | [diff] [blame] | 4154 | static void |
| 4155 | install_default_basic (enum node_type node) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 4156 | { |
| 4157 | install_element (node, &config_exit_cmd); |
| 4158 | install_element (node, &config_quit_cmd); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 4159 | install_element (node, &config_help_cmd); |
| 4160 | install_element (node, &config_list_cmd); |
Paul Jakma | 9219366 | 2016-06-16 15:53:26 +0100 | [diff] [blame] | 4161 | } |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 4162 | |
Paul Jakma | 9219366 | 2016-06-16 15:53:26 +0100 | [diff] [blame] | 4163 | /* Install common/default commands for a privileged node */ |
| 4164 | void |
| 4165 | install_default (enum node_type node) |
| 4166 | { |
| 4167 | /* VIEW_NODE is inited below, via install_default_basic, and |
| 4168 | install_element's of commands to VIEW_NODE automatically are |
| 4169 | also installed to ENABLE_NODE. |
| 4170 | |
| 4171 | For all other nodes, we must ensure install_default_basic is |
| 4172 | also called/ |
| 4173 | */ |
| 4174 | if (node != VIEW_NODE && node != ENABLE_NODE) |
| 4175 | install_default_basic (node); |
| 4176 | |
| 4177 | install_element (node, &config_end_cmd); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 4178 | install_element (node, &config_write_terminal_cmd); |
| 4179 | install_element (node, &config_write_file_cmd); |
| 4180 | install_element (node, &config_write_memory_cmd); |
| 4181 | install_element (node, &config_write_cmd); |
| 4182 | install_element (node, &show_running_config_cmd); |
| 4183 | } |
| 4184 | |
| 4185 | /* Initialize command interface. Install basic nodes and commands. */ |
| 4186 | void |
| 4187 | cmd_init (int terminal) |
| 4188 | { |
Christian Franke | cd40b32 | 2013-09-30 12:27:51 +0000 | [diff] [blame] | 4189 | command_cr = XSTRDUP(MTYPE_CMD_TOKENS, "<cr>"); |
| 4190 | token_cr.type = TOKEN_TERMINAL; |
David Lamparter | 10bac80 | 2015-05-05 11:10:20 +0200 | [diff] [blame] | 4191 | token_cr.terminal = TERMINAL_LITERAL; |
Christian Franke | cd40b32 | 2013-09-30 12:27:51 +0000 | [diff] [blame] | 4192 | token_cr.cmd = command_cr; |
| 4193 | token_cr.desc = XSTRDUP(MTYPE_CMD_TOKENS, ""); |
Chris Caputo | 228da42 | 2009-07-18 05:44:03 +0000 | [diff] [blame] | 4194 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 4195 | /* Allocate initial top vector of commands. */ |
| 4196 | cmdvec = vector_init (VECTOR_MIN_SIZE); |
Paul Jakma | 9219366 | 2016-06-16 15:53:26 +0100 | [diff] [blame] | 4197 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 4198 | /* Default host value settings. */ |
| 4199 | host.name = NULL; |
| 4200 | host.password = NULL; |
| 4201 | host.enable = NULL; |
| 4202 | host.logfile = NULL; |
| 4203 | host.config = NULL; |
| 4204 | host.lines = -1; |
| 4205 | host.motd = default_motd; |
paul | 3b0c5d9 | 2005-03-08 10:43:43 +0000 | [diff] [blame] | 4206 | host.motdfile = NULL; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 4207 | |
| 4208 | /* Install top nodes. */ |
| 4209 | install_node (&view_node, NULL); |
| 4210 | install_node (&enable_node, NULL); |
| 4211 | install_node (&auth_node, NULL); |
| 4212 | install_node (&auth_enable_node, NULL); |
Paul Jakma | 62687ff | 2008-08-23 14:27:06 +0100 | [diff] [blame] | 4213 | install_node (&restricted_node, NULL); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 4214 | install_node (&config_node, config_write_host); |
| 4215 | |
| 4216 | /* Each node's basic commands. */ |
| 4217 | install_element (VIEW_NODE, &show_version_cmd); |
| 4218 | if (terminal) |
| 4219 | { |
Paul Jakma | 9219366 | 2016-06-16 15:53:26 +0100 | [diff] [blame] | 4220 | install_default_basic (VIEW_NODE); |
| 4221 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 4222 | install_element (VIEW_NODE, &config_enable_cmd); |
| 4223 | install_element (VIEW_NODE, &config_terminal_length_cmd); |
| 4224 | install_element (VIEW_NODE, &config_terminal_no_length_cmd); |
ajs | 274a4a4 | 2004-12-07 15:39:31 +0000 | [diff] [blame] | 4225 | install_element (VIEW_NODE, &show_logging_cmd); |
Lou Berger | f9ec419 | 2016-01-12 13:41:48 -0500 | [diff] [blame] | 4226 | install_element (VIEW_NODE, &show_commandtree_cmd); |
ajs | 2885f72 | 2004-12-17 23:16:33 +0000 | [diff] [blame] | 4227 | install_element (VIEW_NODE, &echo_cmd); |
Paul Jakma | 62687ff | 2008-08-23 14:27:06 +0100 | [diff] [blame] | 4228 | |
Paul Jakma | 62687ff | 2008-08-23 14:27:06 +0100 | [diff] [blame] | 4229 | install_element (RESTRICTED_NODE, &config_enable_cmd); |
| 4230 | install_element (RESTRICTED_NODE, &config_terminal_length_cmd); |
| 4231 | install_element (RESTRICTED_NODE, &config_terminal_no_length_cmd); |
Lou Berger | f9ec419 | 2016-01-12 13:41:48 -0500 | [diff] [blame] | 4232 | install_element (RESTRICTED_NODE, &show_commandtree_cmd); |
Paul Jakma | 62687ff | 2008-08-23 14:27:06 +0100 | [diff] [blame] | 4233 | install_element (RESTRICTED_NODE, &echo_cmd); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 4234 | } |
| 4235 | |
| 4236 | if (terminal) |
| 4237 | { |
| 4238 | install_default (ENABLE_NODE); |
| 4239 | install_element (ENABLE_NODE, &config_disable_cmd); |
| 4240 | install_element (ENABLE_NODE, &config_terminal_cmd); |
| 4241 | install_element (ENABLE_NODE, ©_runningconfig_startupconfig_cmd); |
| 4242 | } |
| 4243 | install_element (ENABLE_NODE, &show_startup_config_cmd); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 4244 | |
| 4245 | if (terminal) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 4246 | { |
ajs | 274a4a4 | 2004-12-07 15:39:31 +0000 | [diff] [blame] | 4247 | install_element (ENABLE_NODE, &config_logmsg_cmd); |
hasso | e7168df | 2004-10-03 20:11:32 +0000 | [diff] [blame] | 4248 | |
| 4249 | install_default (CONFIG_NODE); |
hasso | ea8e9d9 | 2004-10-07 21:32:14 +0000 | [diff] [blame] | 4250 | } |
| 4251 | |
| 4252 | install_element (CONFIG_NODE, &hostname_cmd); |
| 4253 | install_element (CONFIG_NODE, &no_hostname_cmd); |
hasso | e7168df | 2004-10-03 20:11:32 +0000 | [diff] [blame] | 4254 | |
hasso | ea8e9d9 | 2004-10-07 21:32:14 +0000 | [diff] [blame] | 4255 | if (terminal) |
| 4256 | { |
hasso | e7168df | 2004-10-03 20:11:32 +0000 | [diff] [blame] | 4257 | install_element (CONFIG_NODE, &password_cmd); |
| 4258 | install_element (CONFIG_NODE, &password_text_cmd); |
| 4259 | install_element (CONFIG_NODE, &enable_password_cmd); |
| 4260 | install_element (CONFIG_NODE, &enable_password_text_cmd); |
| 4261 | install_element (CONFIG_NODE, &no_enable_password_cmd); |
| 4262 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 4263 | install_element (CONFIG_NODE, &config_log_stdout_cmd); |
ajs | 274a4a4 | 2004-12-07 15:39:31 +0000 | [diff] [blame] | 4264 | install_element (CONFIG_NODE, &config_log_stdout_level_cmd); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 4265 | install_element (CONFIG_NODE, &no_config_log_stdout_cmd); |
ajs | 274a4a4 | 2004-12-07 15:39:31 +0000 | [diff] [blame] | 4266 | install_element (CONFIG_NODE, &config_log_monitor_cmd); |
| 4267 | install_element (CONFIG_NODE, &config_log_monitor_level_cmd); |
| 4268 | install_element (CONFIG_NODE, &no_config_log_monitor_cmd); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 4269 | install_element (CONFIG_NODE, &config_log_file_cmd); |
ajs | 274a4a4 | 2004-12-07 15:39:31 +0000 | [diff] [blame] | 4270 | install_element (CONFIG_NODE, &config_log_file_level_cmd); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 4271 | install_element (CONFIG_NODE, &no_config_log_file_cmd); |
ajs | 274a4a4 | 2004-12-07 15:39:31 +0000 | [diff] [blame] | 4272 | install_element (CONFIG_NODE, &no_config_log_file_level_cmd); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 4273 | install_element (CONFIG_NODE, &config_log_syslog_cmd); |
ajs | 274a4a4 | 2004-12-07 15:39:31 +0000 | [diff] [blame] | 4274 | install_element (CONFIG_NODE, &config_log_syslog_level_cmd); |
paul | 12ab19f | 2003-07-26 06:14:55 +0000 | [diff] [blame] | 4275 | install_element (CONFIG_NODE, &config_log_syslog_facility_cmd); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 4276 | install_element (CONFIG_NODE, &no_config_log_syslog_cmd); |
paul | 12ab19f | 2003-07-26 06:14:55 +0000 | [diff] [blame] | 4277 | install_element (CONFIG_NODE, &no_config_log_syslog_facility_cmd); |
ajs | 274a4a4 | 2004-12-07 15:39:31 +0000 | [diff] [blame] | 4278 | install_element (CONFIG_NODE, &config_log_facility_cmd); |
| 4279 | install_element (CONFIG_NODE, &no_config_log_facility_cmd); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 4280 | install_element (CONFIG_NODE, &config_log_trap_cmd); |
| 4281 | install_element (CONFIG_NODE, &no_config_log_trap_cmd); |
| 4282 | install_element (CONFIG_NODE, &config_log_record_priority_cmd); |
| 4283 | install_element (CONFIG_NODE, &no_config_log_record_priority_cmd); |
Andrew J. Schorr | 1ed72e0 | 2007-04-28 22:14:10 +0000 | [diff] [blame] | 4284 | install_element (CONFIG_NODE, &config_log_timestamp_precision_cmd); |
| 4285 | install_element (CONFIG_NODE, &no_config_log_timestamp_precision_cmd); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 4286 | install_element (CONFIG_NODE, &service_password_encrypt_cmd); |
| 4287 | install_element (CONFIG_NODE, &no_service_password_encrypt_cmd); |
| 4288 | install_element (CONFIG_NODE, &banner_motd_default_cmd); |
paul | 3b0c5d9 | 2005-03-08 10:43:43 +0000 | [diff] [blame] | 4289 | install_element (CONFIG_NODE, &banner_motd_file_cmd); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 4290 | install_element (CONFIG_NODE, &no_banner_motd_cmd); |
| 4291 | install_element (CONFIG_NODE, &service_terminal_length_cmd); |
| 4292 | install_element (CONFIG_NODE, &no_service_terminal_length_cmd); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 4293 | |
paul | 354d119 | 2005-04-25 16:26:42 +0000 | [diff] [blame] | 4294 | install_element (VIEW_NODE, &show_thread_cpu_cmd); |
Paul Jakma | 62687ff | 2008-08-23 14:27:06 +0100 | [diff] [blame] | 4295 | install_element (RESTRICTED_NODE, &show_thread_cpu_cmd); |
Paul Jakma | e276eb8 | 2010-01-09 16:15:00 +0000 | [diff] [blame] | 4296 | |
| 4297 | install_element (ENABLE_NODE, &clear_thread_cpu_cmd); |
paul | 354d119 | 2005-04-25 16:26:42 +0000 | [diff] [blame] | 4298 | install_element (VIEW_NODE, &show_work_queues_cmd); |
paul | 9ab6812 | 2003-01-18 01:16:20 +0000 | [diff] [blame] | 4299 | } |
Lou Berger | f9ec419 | 2016-01-12 13:41:48 -0500 | [diff] [blame] | 4300 | install_element (CONFIG_NODE, &show_commandtree_cmd); |
Donald Sharp | f31bab4 | 2015-06-19 19:26:19 -0400 | [diff] [blame] | 4301 | srandom(time(NULL)); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 4302 | } |
Chris Caputo | 228da42 | 2009-07-18 05:44:03 +0000 | [diff] [blame] | 4303 | |
Christian Franke | cd40b32 | 2013-09-30 12:27:51 +0000 | [diff] [blame] | 4304 | static void |
| 4305 | cmd_terminate_token(struct cmd_token *token) |
| 4306 | { |
| 4307 | unsigned int i, j; |
| 4308 | vector keyword_vect; |
| 4309 | |
| 4310 | if (token->multiple) |
| 4311 | { |
| 4312 | for (i = 0; i < vector_active(token->multiple); i++) |
| 4313 | cmd_terminate_token(vector_slot(token->multiple, i)); |
| 4314 | vector_free(token->multiple); |
| 4315 | token->multiple = NULL; |
| 4316 | } |
| 4317 | |
| 4318 | if (token->keyword) |
| 4319 | { |
| 4320 | for (i = 0; i < vector_active(token->keyword); i++) |
| 4321 | { |
| 4322 | keyword_vect = vector_slot(token->keyword, i); |
| 4323 | for (j = 0; j < vector_active(keyword_vect); j++) |
| 4324 | cmd_terminate_token(vector_slot(keyword_vect, j)); |
| 4325 | vector_free(keyword_vect); |
| 4326 | } |
| 4327 | vector_free(token->keyword); |
| 4328 | token->keyword = NULL; |
| 4329 | } |
| 4330 | |
| 4331 | XFREE(MTYPE_CMD_TOKENS, token->cmd); |
| 4332 | XFREE(MTYPE_CMD_TOKENS, token->desc); |
| 4333 | |
| 4334 | XFREE(MTYPE_CMD_TOKENS, token); |
| 4335 | } |
| 4336 | |
| 4337 | static void |
| 4338 | cmd_terminate_element(struct cmd_element *cmd) |
| 4339 | { |
| 4340 | unsigned int i; |
| 4341 | |
| 4342 | if (cmd->tokens == NULL) |
| 4343 | return; |
| 4344 | |
| 4345 | for (i = 0; i < vector_active(cmd->tokens); i++) |
| 4346 | cmd_terminate_token(vector_slot(cmd->tokens, i)); |
| 4347 | |
| 4348 | vector_free(cmd->tokens); |
| 4349 | cmd->tokens = NULL; |
| 4350 | } |
| 4351 | |
Chris Caputo | 228da42 | 2009-07-18 05:44:03 +0000 | [diff] [blame] | 4352 | void |
| 4353 | cmd_terminate () |
| 4354 | { |
Christian Franke | cd40b32 | 2013-09-30 12:27:51 +0000 | [diff] [blame] | 4355 | unsigned int i, j; |
Chris Caputo | 228da42 | 2009-07-18 05:44:03 +0000 | [diff] [blame] | 4356 | struct cmd_node *cmd_node; |
| 4357 | struct cmd_element *cmd_element; |
Christian Franke | cd40b32 | 2013-09-30 12:27:51 +0000 | [diff] [blame] | 4358 | vector cmd_node_v; |
Chris Caputo | 228da42 | 2009-07-18 05:44:03 +0000 | [diff] [blame] | 4359 | |
| 4360 | if (cmdvec) |
| 4361 | { |
| 4362 | for (i = 0; i < vector_active (cmdvec); i++) |
| 4363 | if ((cmd_node = vector_slot (cmdvec, i)) != NULL) |
| 4364 | { |
| 4365 | cmd_node_v = cmd_node->cmd_vector; |
| 4366 | |
| 4367 | for (j = 0; j < vector_active (cmd_node_v); j++) |
Christian Franke | cd40b32 | 2013-09-30 12:27:51 +0000 | [diff] [blame] | 4368 | if ((cmd_element = vector_slot (cmd_node_v, j)) != NULL) |
| 4369 | cmd_terminate_element(cmd_element); |
Chris Caputo | 228da42 | 2009-07-18 05:44:03 +0000 | [diff] [blame] | 4370 | |
| 4371 | vector_free (cmd_node_v); |
Paul Jakma | 9219366 | 2016-06-16 15:53:26 +0100 | [diff] [blame] | 4372 | hash_clean (cmd_node->cmd_hash, NULL); |
| 4373 | hash_free (cmd_node->cmd_hash); |
| 4374 | cmd_node->cmd_hash = NULL; |
Chris Caputo | 228da42 | 2009-07-18 05:44:03 +0000 | [diff] [blame] | 4375 | } |
| 4376 | |
| 4377 | vector_free (cmdvec); |
| 4378 | cmdvec = NULL; |
| 4379 | } |
| 4380 | |
| 4381 | if (command_cr) |
Christian Franke | cd40b32 | 2013-09-30 12:27:51 +0000 | [diff] [blame] | 4382 | XFREE(MTYPE_CMD_TOKENS, command_cr); |
| 4383 | if (token_cr.desc) |
| 4384 | XFREE(MTYPE_CMD_TOKENS, token_cr.desc); |
Chris Caputo | 228da42 | 2009-07-18 05:44:03 +0000 | [diff] [blame] | 4385 | if (host.name) |
| 4386 | XFREE (MTYPE_HOST, host.name); |
| 4387 | if (host.password) |
| 4388 | XFREE (MTYPE_HOST, host.password); |
| 4389 | if (host.password_encrypt) |
| 4390 | XFREE (MTYPE_HOST, host.password_encrypt); |
| 4391 | if (host.enable) |
| 4392 | XFREE (MTYPE_HOST, host.enable); |
| 4393 | if (host.enable_encrypt) |
| 4394 | XFREE (MTYPE_HOST, host.enable_encrypt); |
| 4395 | if (host.logfile) |
| 4396 | XFREE (MTYPE_HOST, host.logfile); |
| 4397 | if (host.motdfile) |
| 4398 | XFREE (MTYPE_HOST, host.motdfile); |
| 4399 | if (host.config) |
| 4400 | XFREE (MTYPE_HOST, host.config); |
| 4401 | } |