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