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); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 2021 | } |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 2022 | } |
| 2023 | |
Christian Franke | cd40b32 | 2013-09-30 12:27:51 +0000 | [diff] [blame] | 2024 | /** |
| 2025 | * Check whether a string is already present in a vector of strings. |
| 2026 | * @param v A vector of char*. |
| 2027 | * @param str A char*. |
| 2028 | * @return 0 if str is already present in the vector, 1 otherwise. |
| 2029 | */ |
ajs | 274a4a4 | 2004-12-07 15:39:31 +0000 | [diff] [blame] | 2030 | static int |
hasso | 8c328f1 | 2004-10-05 21:01:23 +0000 | [diff] [blame] | 2031 | cmd_unique_string (vector v, const char *str) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 2032 | { |
hasso | 8c328f1 | 2004-10-05 21:01:23 +0000 | [diff] [blame] | 2033 | unsigned int i; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 2034 | char *match; |
| 2035 | |
paul | 55468c8 | 2005-03-14 20:19:01 +0000 | [diff] [blame] | 2036 | for (i = 0; i < vector_active (v); i++) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 2037 | if ((match = vector_slot (v, i)) != NULL) |
| 2038 | if (strcmp (match, str) == 0) |
| 2039 | return 0; |
| 2040 | return 1; |
| 2041 | } |
| 2042 | |
Christian Franke | cd40b32 | 2013-09-30 12:27:51 +0000 | [diff] [blame] | 2043 | /** |
| 2044 | * Check whether a struct cmd_token matching a given string is already |
| 2045 | * present in a vector of struct cmd_token. |
| 2046 | * @param v A vector of struct cmd_token*. |
| 2047 | * @param str A char* which should be searched for. |
| 2048 | * @return 0 if there is a struct cmd_token* with its cmd matching str, |
| 2049 | * 1 otherwise. |
| 2050 | */ |
ajs | 274a4a4 | 2004-12-07 15:39:31 +0000 | [diff] [blame] | 2051 | static int |
hasso | 8c328f1 | 2004-10-05 21:01:23 +0000 | [diff] [blame] | 2052 | desc_unique_string (vector v, const char *str) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 2053 | { |
hasso | 8c328f1 | 2004-10-05 21:01:23 +0000 | [diff] [blame] | 2054 | unsigned int i; |
Christian Franke | cd40b32 | 2013-09-30 12:27:51 +0000 | [diff] [blame] | 2055 | struct cmd_token *token; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 2056 | |
paul | 55468c8 | 2005-03-14 20:19:01 +0000 | [diff] [blame] | 2057 | for (i = 0; i < vector_active (v); i++) |
Christian Franke | cd40b32 | 2013-09-30 12:27:51 +0000 | [diff] [blame] | 2058 | if ((token = vector_slot (v, i)) != NULL) |
| 2059 | if (strcmp (token->cmd, str) == 0) |
| 2060 | return 0; |
| 2061 | return 1; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 2062 | } |
| 2063 | |
ajs | 274a4a4 | 2004-12-07 15:39:31 +0000 | [diff] [blame] | 2064 | static int |
paul | b92938a | 2002-12-13 21:20:42 +0000 | [diff] [blame] | 2065 | cmd_try_do_shortcut (enum node_type node, char* first_word) { |
| 2066 | if ( first_word != NULL && |
| 2067 | node != AUTH_NODE && |
| 2068 | node != VIEW_NODE && |
| 2069 | node != AUTH_ENABLE_NODE && |
| 2070 | node != ENABLE_NODE && |
Paul Jakma | 62687ff | 2008-08-23 14:27:06 +0100 | [diff] [blame] | 2071 | node != RESTRICTED_NODE && |
paul | b92938a | 2002-12-13 21:20:42 +0000 | [diff] [blame] | 2072 | 0 == strcmp( "do", first_word ) ) |
| 2073 | return 1; |
| 2074 | return 0; |
| 2075 | } |
| 2076 | |
Christian Franke | cd40b32 | 2013-09-30 12:27:51 +0000 | [diff] [blame] | 2077 | static void |
| 2078 | cmd_matches_free(vector *matches) |
| 2079 | { |
| 2080 | unsigned int i; |
| 2081 | vector cmd_matches; |
| 2082 | |
| 2083 | for (i = 0; i < vector_active(*matches); i++) |
| 2084 | if ((cmd_matches = vector_slot(*matches, i)) != NULL) |
| 2085 | vector_free(cmd_matches); |
| 2086 | vector_free(*matches); |
| 2087 | *matches = NULL; |
| 2088 | } |
| 2089 | |
| 2090 | static int |
| 2091 | cmd_describe_cmp(const void *a, const void *b) |
| 2092 | { |
| 2093 | const struct cmd_token *first = *(struct cmd_token * const *)a; |
| 2094 | const struct cmd_token *second = *(struct cmd_token * const *)b; |
| 2095 | |
| 2096 | return strcmp(first->cmd, second->cmd); |
| 2097 | } |
| 2098 | |
| 2099 | static void |
| 2100 | cmd_describe_sort(vector matchvec) |
| 2101 | { |
| 2102 | qsort(matchvec->index, vector_active(matchvec), |
| 2103 | sizeof(void*), cmd_describe_cmp); |
| 2104 | } |
| 2105 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 2106 | /* '?' describe command support. */ |
ajs | 274a4a4 | 2004-12-07 15:39:31 +0000 | [diff] [blame] | 2107 | static vector |
paul | b92938a | 2002-12-13 21:20:42 +0000 | [diff] [blame] | 2108 | cmd_describe_command_real (vector vline, struct vty *vty, int *status) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 2109 | { |
paul | b896147 | 2005-03-14 17:35:52 +0000 | [diff] [blame] | 2110 | unsigned int i; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 2111 | vector cmd_vector; |
| 2112 | #define INIT_MATCHVEC_SIZE 10 |
| 2113 | vector matchvec; |
| 2114 | struct cmd_element *cmd_element; |
paul | b896147 | 2005-03-14 17:35:52 +0000 | [diff] [blame] | 2115 | unsigned int index; |
paul | 54aba54 | 2003-08-21 20:28:24 +0000 | [diff] [blame] | 2116 | int ret; |
| 2117 | enum match_type match; |
| 2118 | char *command; |
Christian Franke | cd40b32 | 2013-09-30 12:27:51 +0000 | [diff] [blame] | 2119 | vector matches = NULL; |
| 2120 | vector match_vector; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 2121 | |
| 2122 | /* Set index. */ |
paul | 55468c8 | 2005-03-14 20:19:01 +0000 | [diff] [blame] | 2123 | if (vector_active (vline) == 0) |
paul | b896147 | 2005-03-14 17:35:52 +0000 | [diff] [blame] | 2124 | { |
| 2125 | *status = CMD_ERR_NO_MATCH; |
| 2126 | return NULL; |
| 2127 | } |
Christian Franke | cd40b32 | 2013-09-30 12:27:51 +0000 | [diff] [blame] | 2128 | |
| 2129 | index = vector_active (vline) - 1; |
| 2130 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 2131 | /* Make copy vector of current node's command vector. */ |
| 2132 | cmd_vector = vector_copy (cmd_node_vector (cmdvec, vty->node)); |
| 2133 | |
| 2134 | /* Prepare match vector */ |
| 2135 | matchvec = vector_init (INIT_MATCHVEC_SIZE); |
| 2136 | |
Christian Franke | cd40b32 | 2013-09-30 12:27:51 +0000 | [diff] [blame] | 2137 | /* Filter commands and build a list how they could possibly continue. */ |
| 2138 | for (i = 0; i <= index; i++) |
| 2139 | { |
| 2140 | command = vector_slot (vline, i); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 2141 | |
Christian Franke | cd40b32 | 2013-09-30 12:27:51 +0000 | [diff] [blame] | 2142 | if (matches) |
| 2143 | cmd_matches_free(&matches); |
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 | ret = cmd_vector_filter(cmd_vector, |
| 2146 | FILTER_RELAXED, |
| 2147 | vline, i, |
| 2148 | &match, |
| 2149 | &matches); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 2150 | |
Christian Franke | cd40b32 | 2013-09-30 12:27:51 +0000 | [diff] [blame] | 2151 | if (ret != CMD_SUCCESS) |
| 2152 | { |
| 2153 | vector_free (cmd_vector); |
| 2154 | vector_free (matchvec); |
| 2155 | cmd_matches_free(&matches); |
| 2156 | *status = ret; |
| 2157 | return NULL; |
| 2158 | } |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 2159 | |
Christian Franke | cd40b32 | 2013-09-30 12:27:51 +0000 | [diff] [blame] | 2160 | /* The last match may well be ambigious, so break here */ |
| 2161 | if (i == index) |
| 2162 | break; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 2163 | |
Christian Franke | cd40b32 | 2013-09-30 12:27:51 +0000 | [diff] [blame] | 2164 | if (match == vararg_match) |
| 2165 | { |
| 2166 | /* We found a vararg match - so we can throw out the current matches here |
| 2167 | * and don't need to continue checking the command input */ |
| 2168 | unsigned int j, k; |
| 2169 | |
| 2170 | for (j = 0; j < vector_active (matches); j++) |
| 2171 | if ((match_vector = vector_slot (matches, j)) != NULL) |
| 2172 | for (k = 0; k < vector_active (match_vector); k++) |
| 2173 | { |
| 2174 | struct cmd_token *token = vector_slot (match_vector, k); |
| 2175 | vector_set (matchvec, token); |
| 2176 | } |
| 2177 | |
| 2178 | *status = CMD_SUCCESS; |
| 2179 | vector_set(matchvec, &token_cr); |
| 2180 | vector_free (cmd_vector); |
| 2181 | cmd_matches_free(&matches); |
| 2182 | cmd_describe_sort(matchvec); |
| 2183 | return matchvec; |
| 2184 | } |
| 2185 | |
| 2186 | ret = is_cmd_ambiguous(cmd_vector, command, matches, match); |
| 2187 | if (ret == 1) |
| 2188 | { |
| 2189 | vector_free (cmd_vector); |
| 2190 | vector_free (matchvec); |
| 2191 | cmd_matches_free(&matches); |
| 2192 | *status = CMD_ERR_AMBIGUOUS; |
| 2193 | return NULL; |
| 2194 | } |
| 2195 | else if (ret == 2) |
| 2196 | { |
| 2197 | vector_free (cmd_vector); |
| 2198 | vector_free (matchvec); |
| 2199 | cmd_matches_free(&matches); |
| 2200 | *status = CMD_ERR_NO_MATCH; |
| 2201 | return NULL; |
| 2202 | } |
| 2203 | } |
paul | 54aba54 | 2003-08-21 20:28:24 +0000 | [diff] [blame] | 2204 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 2205 | /* Make description vector. */ |
Christian Franke | cd40b32 | 2013-09-30 12:27:51 +0000 | [diff] [blame] | 2206 | for (i = 0; i < vector_active (matches); i++) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 2207 | if ((cmd_element = vector_slot (cmd_vector, i)) != NULL) |
| 2208 | { |
Christian Franke | cd40b32 | 2013-09-30 12:27:51 +0000 | [diff] [blame] | 2209 | unsigned int j; |
| 2210 | const char *last_word; |
| 2211 | vector vline_trimmed; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 2212 | |
Christian Franke | cd40b32 | 2013-09-30 12:27:51 +0000 | [diff] [blame] | 2213 | last_word = vector_slot(vline, vector_active(vline) - 1); |
| 2214 | if (last_word == NULL || !strlen(last_word)) |
| 2215 | { |
| 2216 | vline_trimmed = vector_copy(vline); |
| 2217 | vector_unset(vline_trimmed, vector_active(vline_trimmed) - 1); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 2218 | |
Christian Franke | cd40b32 | 2013-09-30 12:27:51 +0000 | [diff] [blame] | 2219 | if (cmd_is_complete(cmd_element, vline_trimmed) |
| 2220 | && desc_unique_string(matchvec, command_cr)) |
| 2221 | { |
| 2222 | if (match != vararg_match) |
| 2223 | vector_set(matchvec, &token_cr); |
| 2224 | } |
Chris Caputo | 228da42 | 2009-07-18 05:44:03 +0000 | [diff] [blame] | 2225 | |
Christian Franke | cd40b32 | 2013-09-30 12:27:51 +0000 | [diff] [blame] | 2226 | vector_free(vline_trimmed); |
| 2227 | } |
| 2228 | |
| 2229 | match_vector = vector_slot (matches, i); |
| 2230 | if (match_vector) |
| 2231 | for (j = 0; j < vector_active(match_vector); j++) |
| 2232 | { |
| 2233 | struct cmd_token *token = vector_slot(match_vector, j); |
| 2234 | const char *string; |
| 2235 | |
David Lamparter | 10bac80 | 2015-05-05 11:10:20 +0200 | [diff] [blame] | 2236 | string = cmd_entry_function_desc(command, token); |
Christian Franke | cd40b32 | 2013-09-30 12:27:51 +0000 | [diff] [blame] | 2237 | if (string && desc_unique_string(matchvec, string)) |
| 2238 | vector_set(matchvec, token); |
| 2239 | } |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 2240 | } |
| 2241 | vector_free (cmd_vector); |
Christian Franke | cd40b32 | 2013-09-30 12:27:51 +0000 | [diff] [blame] | 2242 | cmd_matches_free(&matches); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 2243 | |
| 2244 | if (vector_slot (matchvec, 0) == NULL) |
| 2245 | { |
| 2246 | vector_free (matchvec); |
paul | 909a215 | 2005-03-14 17:41:45 +0000 | [diff] [blame] | 2247 | *status = CMD_ERR_NO_MATCH; |
Paul Jakma | 5fc6051 | 2006-05-12 23:24:09 +0000 | [diff] [blame] | 2248 | return NULL; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 2249 | } |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 2250 | |
Paul Jakma | 5fc6051 | 2006-05-12 23:24:09 +0000 | [diff] [blame] | 2251 | *status = CMD_SUCCESS; |
Christian Franke | cd40b32 | 2013-09-30 12:27:51 +0000 | [diff] [blame] | 2252 | cmd_describe_sort(matchvec); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 2253 | return matchvec; |
| 2254 | } |
| 2255 | |
paul | b92938a | 2002-12-13 21:20:42 +0000 | [diff] [blame] | 2256 | vector |
| 2257 | cmd_describe_command (vector vline, struct vty *vty, int *status) |
| 2258 | { |
| 2259 | vector ret; |
| 2260 | |
| 2261 | if ( cmd_try_do_shortcut(vty->node, vector_slot(vline, 0) ) ) |
| 2262 | { |
| 2263 | enum node_type onode; |
| 2264 | vector shifted_vline; |
hasso | 8c328f1 | 2004-10-05 21:01:23 +0000 | [diff] [blame] | 2265 | unsigned int index; |
paul | b92938a | 2002-12-13 21:20:42 +0000 | [diff] [blame] | 2266 | |
| 2267 | onode = vty->node; |
| 2268 | vty->node = ENABLE_NODE; |
| 2269 | /* We can try it on enable node, cos' the vty is authenticated */ |
| 2270 | |
| 2271 | shifted_vline = vector_init (vector_count(vline)); |
| 2272 | /* use memcpy? */ |
paul | 55468c8 | 2005-03-14 20:19:01 +0000 | [diff] [blame] | 2273 | for (index = 1; index < vector_active (vline); index++) |
paul | b92938a | 2002-12-13 21:20:42 +0000 | [diff] [blame] | 2274 | { |
| 2275 | vector_set_index (shifted_vline, index-1, vector_lookup(vline, index)); |
| 2276 | } |
| 2277 | |
| 2278 | ret = cmd_describe_command_real (shifted_vline, vty, status); |
| 2279 | |
| 2280 | vector_free(shifted_vline); |
| 2281 | vty->node = onode; |
| 2282 | return ret; |
| 2283 | } |
| 2284 | |
| 2285 | |
| 2286 | return cmd_describe_command_real (vline, vty, status); |
| 2287 | } |
| 2288 | |
| 2289 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 2290 | /* Check LCD of matched command. */ |
ajs | 274a4a4 | 2004-12-07 15:39:31 +0000 | [diff] [blame] | 2291 | static int |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 2292 | cmd_lcd (char **matched) |
| 2293 | { |
| 2294 | int i; |
| 2295 | int j; |
| 2296 | int lcd = -1; |
| 2297 | char *s1, *s2; |
| 2298 | char c1, c2; |
| 2299 | |
| 2300 | if (matched[0] == NULL || matched[1] == NULL) |
| 2301 | return 0; |
| 2302 | |
| 2303 | for (i = 1; matched[i] != NULL; i++) |
| 2304 | { |
| 2305 | s1 = matched[i - 1]; |
| 2306 | s2 = matched[i]; |
| 2307 | |
| 2308 | for (j = 0; (c1 = s1[j]) && (c2 = s2[j]); j++) |
| 2309 | if (c1 != c2) |
| 2310 | break; |
| 2311 | |
| 2312 | if (lcd < 0) |
| 2313 | lcd = j; |
| 2314 | else |
| 2315 | { |
| 2316 | if (lcd > j) |
| 2317 | lcd = j; |
| 2318 | } |
| 2319 | } |
| 2320 | return lcd; |
| 2321 | } |
| 2322 | |
Christian Franke | cd40b32 | 2013-09-30 12:27:51 +0000 | [diff] [blame] | 2323 | static int |
| 2324 | cmd_complete_cmp(const void *a, const void *b) |
| 2325 | { |
| 2326 | const char *first = *(char * const *)a; |
| 2327 | const char *second = *(char * const *)b; |
| 2328 | |
| 2329 | if (!first) |
| 2330 | { |
| 2331 | if (!second) |
| 2332 | return 0; |
| 2333 | return 1; |
| 2334 | } |
| 2335 | if (!second) |
| 2336 | return -1; |
| 2337 | |
| 2338 | return strcmp(first, second); |
| 2339 | } |
| 2340 | |
| 2341 | static void |
| 2342 | cmd_complete_sort(vector matchvec) |
| 2343 | { |
| 2344 | qsort(matchvec->index, vector_active(matchvec), |
| 2345 | sizeof(void*), cmd_complete_cmp); |
| 2346 | } |
| 2347 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 2348 | /* Command line completion support. */ |
ajs | 274a4a4 | 2004-12-07 15:39:31 +0000 | [diff] [blame] | 2349 | static char ** |
paul | b92938a | 2002-12-13 21:20:42 +0000 | [diff] [blame] | 2350 | cmd_complete_command_real (vector vline, struct vty *vty, int *status) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 2351 | { |
paul | b896147 | 2005-03-14 17:35:52 +0000 | [diff] [blame] | 2352 | unsigned int i; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 2353 | vector cmd_vector = vector_copy (cmd_node_vector (cmdvec, vty->node)); |
| 2354 | #define INIT_MATCHVEC_SIZE 10 |
| 2355 | vector matchvec; |
paul | b896147 | 2005-03-14 17:35:52 +0000 | [diff] [blame] | 2356 | unsigned int index; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 2357 | char **match_str; |
Christian Franke | cd40b32 | 2013-09-30 12:27:51 +0000 | [diff] [blame] | 2358 | struct cmd_token *token; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 2359 | char *command; |
| 2360 | int lcd; |
Christian Franke | cd40b32 | 2013-09-30 12:27:51 +0000 | [diff] [blame] | 2361 | vector matches = NULL; |
| 2362 | vector match_vector; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 2363 | |
paul | 55468c8 | 2005-03-14 20:19:01 +0000 | [diff] [blame] | 2364 | if (vector_active (vline) == 0) |
paul | b896147 | 2005-03-14 17:35:52 +0000 | [diff] [blame] | 2365 | { |
Paul Jakma | d251996 | 2006-05-12 23:19:37 +0000 | [diff] [blame] | 2366 | vector_free (cmd_vector); |
paul | b896147 | 2005-03-14 17:35:52 +0000 | [diff] [blame] | 2367 | *status = CMD_ERR_NO_MATCH; |
| 2368 | return NULL; |
| 2369 | } |
| 2370 | else |
paul | 55468c8 | 2005-03-14 20:19:01 +0000 | [diff] [blame] | 2371 | index = vector_active (vline) - 1; |
paul | b896147 | 2005-03-14 17:35:52 +0000 | [diff] [blame] | 2372 | |
Christian Franke | cd40b32 | 2013-09-30 12:27:51 +0000 | [diff] [blame] | 2373 | /* First, filter by command string */ |
| 2374 | for (i = 0; i <= index; i++) |
| 2375 | { |
| 2376 | command = vector_slot (vline, i); |
| 2377 | enum match_type match; |
| 2378 | int ret; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 2379 | |
Christian Franke | cd40b32 | 2013-09-30 12:27:51 +0000 | [diff] [blame] | 2380 | if (matches) |
| 2381 | cmd_matches_free(&matches); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 2382 | |
Christian Franke | cd40b32 | 2013-09-30 12:27:51 +0000 | [diff] [blame] | 2383 | /* First try completion match, if there is exactly match return 1 */ |
| 2384 | ret = cmd_vector_filter(cmd_vector, |
| 2385 | FILTER_RELAXED, |
| 2386 | vline, i, |
| 2387 | &match, |
| 2388 | &matches); |
| 2389 | |
| 2390 | if (ret != CMD_SUCCESS) |
| 2391 | { |
| 2392 | vector_free(cmd_vector); |
| 2393 | cmd_matches_free(&matches); |
| 2394 | *status = ret; |
| 2395 | return NULL; |
| 2396 | } |
| 2397 | |
| 2398 | /* Break here - the completion mustn't be checked to be non-ambiguous */ |
| 2399 | if (i == index) |
| 2400 | break; |
| 2401 | |
| 2402 | /* If there is exact match then filter ambiguous match else check |
| 2403 | ambiguousness. */ |
| 2404 | ret = is_cmd_ambiguous (cmd_vector, command, matches, match); |
| 2405 | if (ret == 1) |
| 2406 | { |
| 2407 | vector_free (cmd_vector); |
| 2408 | cmd_matches_free(&matches); |
| 2409 | *status = CMD_ERR_AMBIGUOUS; |
| 2410 | return NULL; |
| 2411 | } |
| 2412 | /* |
paul | 909a215 | 2005-03-14 17:41:45 +0000 | [diff] [blame] | 2413 | else if (ret == 2) |
| 2414 | { |
| 2415 | vector_free (cmd_vector); |
Christian Franke | cd40b32 | 2013-09-30 12:27:51 +0000 | [diff] [blame] | 2416 | cmd_matches_free(&matches); |
paul | 909a215 | 2005-03-14 17:41:45 +0000 | [diff] [blame] | 2417 | *status = CMD_ERR_NO_MATCH; |
| 2418 | return NULL; |
| 2419 | } |
| 2420 | */ |
Christian Franke | cd40b32 | 2013-09-30 12:27:51 +0000 | [diff] [blame] | 2421 | } |
paul | 909a215 | 2005-03-14 17:41:45 +0000 | [diff] [blame] | 2422 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 2423 | /* Prepare match vector. */ |
| 2424 | matchvec = vector_init (INIT_MATCHVEC_SIZE); |
| 2425 | |
Christian Franke | cd40b32 | 2013-09-30 12:27:51 +0000 | [diff] [blame] | 2426 | /* Build the possible list of continuations into a list of completions */ |
| 2427 | for (i = 0; i < vector_active (matches); i++) |
| 2428 | if ((match_vector = vector_slot (matches, i))) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 2429 | { |
hasso | 8c328f1 | 2004-10-05 21:01:23 +0000 | [diff] [blame] | 2430 | const char *string; |
Christian Franke | cd40b32 | 2013-09-30 12:27:51 +0000 | [diff] [blame] | 2431 | unsigned int j; |
paul | 909a215 | 2005-03-14 17:41:45 +0000 | [diff] [blame] | 2432 | |
Christian Franke | cd40b32 | 2013-09-30 12:27:51 +0000 | [diff] [blame] | 2433 | for (j = 0; j < vector_active (match_vector); j++) |
| 2434 | if ((token = vector_slot (match_vector, j))) |
paul | 909a215 | 2005-03-14 17:41:45 +0000 | [diff] [blame] | 2435 | { |
paul | b896147 | 2005-03-14 17:35:52 +0000 | [diff] [blame] | 2436 | if ((string = |
| 2437 | cmd_entry_function (vector_slot (vline, index), |
David Lamparter | 10bac80 | 2015-05-05 11:10:20 +0200 | [diff] [blame] | 2438 | token))) |
paul | 909a215 | 2005-03-14 17:41:45 +0000 | [diff] [blame] | 2439 | if (cmd_unique_string (matchvec, string)) |
| 2440 | vector_set (matchvec, XSTRDUP (MTYPE_TMP, string)); |
| 2441 | } |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 2442 | } |
| 2443 | |
| 2444 | /* We don't need cmd_vector any more. */ |
| 2445 | vector_free (cmd_vector); |
Christian Franke | cd40b32 | 2013-09-30 12:27:51 +0000 | [diff] [blame] | 2446 | cmd_matches_free(&matches); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 2447 | |
| 2448 | /* No matched command */ |
| 2449 | if (vector_slot (matchvec, 0) == NULL) |
| 2450 | { |
| 2451 | vector_free (matchvec); |
| 2452 | |
| 2453 | /* In case of 'command \t' pattern. Do you need '?' command at |
| 2454 | the end of the line. */ |
| 2455 | if (vector_slot (vline, index) == '\0') |
| 2456 | *status = CMD_ERR_NOTHING_TODO; |
| 2457 | else |
| 2458 | *status = CMD_ERR_NO_MATCH; |
| 2459 | return NULL; |
| 2460 | } |
| 2461 | |
| 2462 | /* Only one matched */ |
| 2463 | if (vector_slot (matchvec, 1) == NULL) |
| 2464 | { |
| 2465 | match_str = (char **) matchvec->index; |
| 2466 | vector_only_wrapper_free (matchvec); |
| 2467 | *status = CMD_COMPLETE_FULL_MATCH; |
| 2468 | return match_str; |
| 2469 | } |
| 2470 | /* Make it sure last element is NULL. */ |
| 2471 | vector_set (matchvec, NULL); |
| 2472 | |
| 2473 | /* Check LCD of matched strings. */ |
| 2474 | if (vector_slot (vline, index) != NULL) |
| 2475 | { |
| 2476 | lcd = cmd_lcd ((char **) matchvec->index); |
| 2477 | |
| 2478 | if (lcd) |
| 2479 | { |
| 2480 | int len = strlen (vector_slot (vline, index)); |
paul | 909a215 | 2005-03-14 17:41:45 +0000 | [diff] [blame] | 2481 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 2482 | if (len < lcd) |
| 2483 | { |
| 2484 | char *lcdstr; |
paul | 909a215 | 2005-03-14 17:41:45 +0000 | [diff] [blame] | 2485 | |
Christian Franke | cd40b32 | 2013-09-30 12:27:51 +0000 | [diff] [blame] | 2486 | lcdstr = XMALLOC (MTYPE_TMP, lcd + 1); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 2487 | memcpy (lcdstr, matchvec->index[0], lcd); |
| 2488 | lcdstr[lcd] = '\0'; |
| 2489 | |
| 2490 | /* match_str = (char **) &lcdstr; */ |
| 2491 | |
| 2492 | /* Free matchvec. */ |
paul | 55468c8 | 2005-03-14 20:19:01 +0000 | [diff] [blame] | 2493 | for (i = 0; i < vector_active (matchvec); i++) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 2494 | { |
| 2495 | if (vector_slot (matchvec, i)) |
Christian Franke | cd40b32 | 2013-09-30 12:27:51 +0000 | [diff] [blame] | 2496 | XFREE (MTYPE_TMP, vector_slot (matchvec, i)); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 2497 | } |
| 2498 | vector_free (matchvec); |
| 2499 | |
paul | 909a215 | 2005-03-14 17:41:45 +0000 | [diff] [blame] | 2500 | /* Make new matchvec. */ |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 2501 | matchvec = vector_init (INIT_MATCHVEC_SIZE); |
| 2502 | vector_set (matchvec, lcdstr); |
| 2503 | match_str = (char **) matchvec->index; |
| 2504 | vector_only_wrapper_free (matchvec); |
| 2505 | |
| 2506 | *status = CMD_COMPLETE_MATCH; |
| 2507 | return match_str; |
| 2508 | } |
| 2509 | } |
| 2510 | } |
| 2511 | |
| 2512 | match_str = (char **) matchvec->index; |
Christian Franke | cd40b32 | 2013-09-30 12:27:51 +0000 | [diff] [blame] | 2513 | cmd_complete_sort(matchvec); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 2514 | vector_only_wrapper_free (matchvec); |
| 2515 | *status = CMD_COMPLETE_LIST_MATCH; |
| 2516 | return match_str; |
| 2517 | } |
| 2518 | |
paul | b92938a | 2002-12-13 21:20:42 +0000 | [diff] [blame] | 2519 | char ** |
paul | 9ab6812 | 2003-01-18 01:16:20 +0000 | [diff] [blame] | 2520 | cmd_complete_command (vector vline, struct vty *vty, int *status) |
paul | b92938a | 2002-12-13 21:20:42 +0000 | [diff] [blame] | 2521 | { |
| 2522 | char **ret; |
| 2523 | |
| 2524 | if ( cmd_try_do_shortcut(vty->node, vector_slot(vline, 0) ) ) |
| 2525 | { |
| 2526 | enum node_type onode; |
| 2527 | vector shifted_vline; |
hasso | 8c328f1 | 2004-10-05 21:01:23 +0000 | [diff] [blame] | 2528 | unsigned int index; |
paul | b92938a | 2002-12-13 21:20:42 +0000 | [diff] [blame] | 2529 | |
| 2530 | onode = vty->node; |
| 2531 | vty->node = ENABLE_NODE; |
| 2532 | /* We can try it on enable node, cos' the vty is authenticated */ |
| 2533 | |
| 2534 | shifted_vline = vector_init (vector_count(vline)); |
| 2535 | /* use memcpy? */ |
paul | 55468c8 | 2005-03-14 20:19:01 +0000 | [diff] [blame] | 2536 | for (index = 1; index < vector_active (vline); index++) |
paul | b92938a | 2002-12-13 21:20:42 +0000 | [diff] [blame] | 2537 | { |
| 2538 | vector_set_index (shifted_vline, index-1, vector_lookup(vline, index)); |
| 2539 | } |
| 2540 | |
| 2541 | ret = cmd_complete_command_real (shifted_vline, vty, status); |
| 2542 | |
| 2543 | vector_free(shifted_vline); |
| 2544 | vty->node = onode; |
| 2545 | return ret; |
| 2546 | } |
| 2547 | |
| 2548 | |
| 2549 | return cmd_complete_command_real (vline, vty, status); |
| 2550 | } |
| 2551 | |
| 2552 | /* return parent node */ |
| 2553 | /* MUST eventually converge on CONFIG_NODE */ |
hasso | 13bfca7 | 2005-01-23 21:42:25 +0000 | [diff] [blame] | 2554 | enum node_type |
ajs | 274a4a4 | 2004-12-07 15:39:31 +0000 | [diff] [blame] | 2555 | node_parent ( enum node_type node ) |
paul | b92938a | 2002-12-13 21:20:42 +0000 | [diff] [blame] | 2556 | { |
| 2557 | enum node_type ret; |
| 2558 | |
paul | 9ab6812 | 2003-01-18 01:16:20 +0000 | [diff] [blame] | 2559 | assert (node > CONFIG_NODE); |
| 2560 | |
| 2561 | switch (node) |
| 2562 | { |
| 2563 | case BGP_VPNV4_NODE: |
| 2564 | case BGP_IPV4_NODE: |
| 2565 | case BGP_IPV4M_NODE: |
| 2566 | case BGP_IPV6_NODE: |
paul | 1e83659 | 2005-08-22 22:39:56 +0000 | [diff] [blame] | 2567 | case BGP_IPV6M_NODE: |
paul | 9ab6812 | 2003-01-18 01:16:20 +0000 | [diff] [blame] | 2568 | ret = BGP_NODE; |
| 2569 | break; |
| 2570 | case KEYCHAIN_KEY_NODE: |
| 2571 | ret = KEYCHAIN_NODE; |
| 2572 | break; |
| 2573 | default: |
| 2574 | ret = CONFIG_NODE; |
paul | b92938a | 2002-12-13 21:20:42 +0000 | [diff] [blame] | 2575 | } |
| 2576 | |
| 2577 | return ret; |
| 2578 | } |
| 2579 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 2580 | /* Execute command by argument vline vector. */ |
ajs | 274a4a4 | 2004-12-07 15:39:31 +0000 | [diff] [blame] | 2581 | static int |
Christian Franke | cd40b32 | 2013-09-30 12:27:51 +0000 | [diff] [blame] | 2582 | cmd_execute_command_real (vector vline, |
| 2583 | enum filter_type filter, |
| 2584 | struct vty *vty, |
paul | b896147 | 2005-03-14 17:35:52 +0000 | [diff] [blame] | 2585 | struct cmd_element **cmd) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 2586 | { |
hasso | 8c328f1 | 2004-10-05 21:01:23 +0000 | [diff] [blame] | 2587 | unsigned int i; |
| 2588 | unsigned int index; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 2589 | vector cmd_vector; |
| 2590 | struct cmd_element *cmd_element; |
| 2591 | struct cmd_element *matched_element; |
| 2592 | unsigned int matched_count, incomplete_count; |
| 2593 | int argc; |
paul | 9035efa | 2004-10-10 11:56:56 +0000 | [diff] [blame] | 2594 | const char *argv[CMD_ARGC_MAX]; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 2595 | enum match_type match = 0; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 2596 | char *command; |
Christian Franke | cd40b32 | 2013-09-30 12:27:51 +0000 | [diff] [blame] | 2597 | int ret; |
| 2598 | vector matches; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 2599 | |
| 2600 | /* Make copy of command elements. */ |
| 2601 | cmd_vector = vector_copy (cmd_node_vector (cmdvec, vty->node)); |
| 2602 | |
paul | 55468c8 | 2005-03-14 20:19:01 +0000 | [diff] [blame] | 2603 | for (index = 0; index < vector_active (vline); index++) |
Christian Franke | cd40b32 | 2013-09-30 12:27:51 +0000 | [diff] [blame] | 2604 | { |
| 2605 | command = vector_slot (vline, index); |
| 2606 | ret = cmd_vector_filter(cmd_vector, |
| 2607 | filter, |
| 2608 | vline, index, |
| 2609 | &match, |
| 2610 | &matches); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 2611 | |
Christian Franke | cd40b32 | 2013-09-30 12:27:51 +0000 | [diff] [blame] | 2612 | if (ret != CMD_SUCCESS) |
| 2613 | { |
| 2614 | cmd_matches_free(&matches); |
| 2615 | return ret; |
| 2616 | } |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 2617 | |
Christian Franke | cd40b32 | 2013-09-30 12:27:51 +0000 | [diff] [blame] | 2618 | if (match == vararg_match) |
| 2619 | { |
| 2620 | cmd_matches_free(&matches); |
paul | 909a215 | 2005-03-14 17:41:45 +0000 | [diff] [blame] | 2621 | break; |
Christian Franke | cd40b32 | 2013-09-30 12:27:51 +0000 | [diff] [blame] | 2622 | } |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 2623 | |
Christian Franke | cd40b32 | 2013-09-30 12:27:51 +0000 | [diff] [blame] | 2624 | ret = is_cmd_ambiguous (cmd_vector, command, matches, match); |
| 2625 | cmd_matches_free(&matches); |
| 2626 | |
| 2627 | if (ret == 1) |
| 2628 | { |
| 2629 | vector_free(cmd_vector); |
| 2630 | return CMD_ERR_AMBIGUOUS; |
| 2631 | } |
| 2632 | else if (ret == 2) |
| 2633 | { |
| 2634 | vector_free(cmd_vector); |
| 2635 | return CMD_ERR_NO_MATCH; |
| 2636 | } |
| 2637 | } |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 2638 | |
| 2639 | /* Check matched count. */ |
| 2640 | matched_element = NULL; |
| 2641 | matched_count = 0; |
| 2642 | incomplete_count = 0; |
| 2643 | |
paul | 55468c8 | 2005-03-14 20:19:01 +0000 | [diff] [blame] | 2644 | for (i = 0; i < vector_active (cmd_vector); i++) |
paul | b896147 | 2005-03-14 17:35:52 +0000 | [diff] [blame] | 2645 | if ((cmd_element = vector_slot (cmd_vector, i))) |
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 (cmd_is_complete(cmd_element, vline)) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 2648 | { |
| 2649 | matched_element = cmd_element; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 2650 | matched_count++; |
| 2651 | } |
| 2652 | else |
| 2653 | { |
| 2654 | incomplete_count++; |
| 2655 | } |
| 2656 | } |
paul | 909a215 | 2005-03-14 17:41:45 +0000 | [diff] [blame] | 2657 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 2658 | /* Finish of using cmd_vector. */ |
| 2659 | vector_free (cmd_vector); |
| 2660 | |
paul | 909a215 | 2005-03-14 17:41:45 +0000 | [diff] [blame] | 2661 | /* To execute command, matched_count must be 1. */ |
| 2662 | if (matched_count == 0) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 2663 | { |
| 2664 | if (incomplete_count) |
| 2665 | return CMD_ERR_INCOMPLETE; |
| 2666 | else |
| 2667 | return CMD_ERR_NO_MATCH; |
| 2668 | } |
| 2669 | |
paul | 909a215 | 2005-03-14 17:41:45 +0000 | [diff] [blame] | 2670 | if (matched_count > 1) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 2671 | return CMD_ERR_AMBIGUOUS; |
| 2672 | |
Christian Franke | cd40b32 | 2013-09-30 12:27:51 +0000 | [diff] [blame] | 2673 | ret = cmd_parse(matched_element, vline, &argc, argv); |
| 2674 | if (ret != CMD_SUCCESS) |
| 2675 | return ret; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 2676 | |
| 2677 | /* For vtysh execution. */ |
| 2678 | if (cmd) |
| 2679 | *cmd = matched_element; |
| 2680 | |
| 2681 | if (matched_element->daemon) |
| 2682 | return CMD_SUCCESS_DAEMON; |
| 2683 | |
| 2684 | /* Execute matched command. */ |
| 2685 | return (*matched_element->func) (matched_element, vty, argc, argv); |
| 2686 | } |
| 2687 | |
Christian Franke | cd40b32 | 2013-09-30 12:27:51 +0000 | [diff] [blame] | 2688 | /** |
| 2689 | * Execute a given command, handling things like "do ..." and checking |
| 2690 | * whether the given command might apply at a parent node if doesn't |
| 2691 | * apply for the current node. |
| 2692 | * |
| 2693 | * @param vline Command line input, vector of char* where each element is |
| 2694 | * one input token. |
| 2695 | * @param vty The vty context in which the command should be executed. |
| 2696 | * @param cmd Pointer where the struct cmd_element of the matched command |
| 2697 | * will be stored, if any. May be set to NULL if this info is |
| 2698 | * not needed. |
| 2699 | * @param vtysh If set != 0, don't lookup the command at parent nodes. |
| 2700 | * @return The status of the command that has been executed or an error code |
| 2701 | * as to why no command could be executed. |
| 2702 | */ |
paul | b92938a | 2002-12-13 21:20:42 +0000 | [diff] [blame] | 2703 | int |
hasso | 87d683b | 2005-01-16 23:31:54 +0000 | [diff] [blame] | 2704 | cmd_execute_command (vector vline, struct vty *vty, struct cmd_element **cmd, |
| 2705 | int vtysh) { |
paul | 9ab6812 | 2003-01-18 01:16:20 +0000 | [diff] [blame] | 2706 | int ret, saved_ret, tried = 0; |
| 2707 | enum node_type onode, try_node; |
| 2708 | |
| 2709 | onode = try_node = vty->node; |
paul | b92938a | 2002-12-13 21:20:42 +0000 | [diff] [blame] | 2710 | |
| 2711 | if ( cmd_try_do_shortcut(vty->node, vector_slot(vline, 0) ) ) |
| 2712 | { |
| 2713 | vector shifted_vline; |
hasso | 8c328f1 | 2004-10-05 21:01:23 +0000 | [diff] [blame] | 2714 | unsigned int index; |
paul | b92938a | 2002-12-13 21:20:42 +0000 | [diff] [blame] | 2715 | |
| 2716 | vty->node = ENABLE_NODE; |
| 2717 | /* We can try it on enable node, cos' the vty is authenticated */ |
| 2718 | |
| 2719 | shifted_vline = vector_init (vector_count(vline)); |
| 2720 | /* use memcpy? */ |
paul | 55468c8 | 2005-03-14 20:19:01 +0000 | [diff] [blame] | 2721 | for (index = 1; index < vector_active (vline); index++) |
paul | b92938a | 2002-12-13 21:20:42 +0000 | [diff] [blame] | 2722 | { |
| 2723 | vector_set_index (shifted_vline, index-1, vector_lookup(vline, index)); |
| 2724 | } |
| 2725 | |
Christian Franke | cd40b32 | 2013-09-30 12:27:51 +0000 | [diff] [blame] | 2726 | ret = cmd_execute_command_real (shifted_vline, FILTER_RELAXED, vty, cmd); |
paul | b92938a | 2002-12-13 21:20:42 +0000 | [diff] [blame] | 2727 | |
| 2728 | vector_free(shifted_vline); |
| 2729 | vty->node = onode; |
| 2730 | return ret; |
| 2731 | } |
| 2732 | |
| 2733 | |
Christian Franke | cd40b32 | 2013-09-30 12:27:51 +0000 | [diff] [blame] | 2734 | saved_ret = ret = cmd_execute_command_real (vline, FILTER_RELAXED, vty, cmd); |
paul | b92938a | 2002-12-13 21:20:42 +0000 | [diff] [blame] | 2735 | |
hasso | 87d683b | 2005-01-16 23:31:54 +0000 | [diff] [blame] | 2736 | if (vtysh) |
| 2737 | return saved_ret; |
| 2738 | |
paul | b92938a | 2002-12-13 21:20:42 +0000 | [diff] [blame] | 2739 | /* This assumes all nodes above CONFIG_NODE are childs of CONFIG_NODE */ |
paul | 9ab6812 | 2003-01-18 01:16:20 +0000 | [diff] [blame] | 2740 | while ( ret != CMD_SUCCESS && ret != CMD_WARNING |
paul | b92938a | 2002-12-13 21:20:42 +0000 | [diff] [blame] | 2741 | && vty->node > CONFIG_NODE ) |
| 2742 | { |
paul | 9ab6812 | 2003-01-18 01:16:20 +0000 | [diff] [blame] | 2743 | try_node = node_parent(try_node); |
| 2744 | vty->node = try_node; |
Christian Franke | cd40b32 | 2013-09-30 12:27:51 +0000 | [diff] [blame] | 2745 | ret = cmd_execute_command_real (vline, FILTER_RELAXED, vty, cmd); |
paul | 9ab6812 | 2003-01-18 01:16:20 +0000 | [diff] [blame] | 2746 | tried = 1; |
| 2747 | if (ret == CMD_SUCCESS || ret == CMD_WARNING) |
paul | b92938a | 2002-12-13 21:20:42 +0000 | [diff] [blame] | 2748 | { |
paul | 9ab6812 | 2003-01-18 01:16:20 +0000 | [diff] [blame] | 2749 | /* succesfull command, leave the node as is */ |
paul | b92938a | 2002-12-13 21:20:42 +0000 | [diff] [blame] | 2750 | return ret; |
| 2751 | } |
paul | b92938a | 2002-12-13 21:20:42 +0000 | [diff] [blame] | 2752 | } |
paul | 9ab6812 | 2003-01-18 01:16:20 +0000 | [diff] [blame] | 2753 | /* no command succeeded, reset the vty to the original node and |
| 2754 | return the error for this node */ |
| 2755 | if ( tried ) |
| 2756 | vty->node = onode; |
| 2757 | return saved_ret; |
paul | eda031f | 2003-01-18 00:39:19 +0000 | [diff] [blame] | 2758 | } |
| 2759 | |
Christian Franke | cd40b32 | 2013-09-30 12:27:51 +0000 | [diff] [blame] | 2760 | /** |
| 2761 | * Execute a given command, matching it strictly against the current node. |
| 2762 | * This mode is used when reading config files. |
| 2763 | * |
| 2764 | * @param vline Command line input, vector of char* where each element is |
| 2765 | * one input token. |
| 2766 | * @param vty The vty context in which the command should be executed. |
| 2767 | * @param cmd Pointer where the struct cmd_element* of the matched command |
| 2768 | * will be stored, if any. May be set to NULL if this info is |
| 2769 | * not needed. |
| 2770 | * @return The status of the command that has been executed or an error code |
| 2771 | * as to why no command could be executed. |
| 2772 | */ |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 2773 | int |
paul | 909a215 | 2005-03-14 17:41:45 +0000 | [diff] [blame] | 2774 | cmd_execute_command_strict (vector vline, struct vty *vty, |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 2775 | struct cmd_element **cmd) |
| 2776 | { |
Christian Franke | cd40b32 | 2013-09-30 12:27:51 +0000 | [diff] [blame] | 2777 | return cmd_execute_command_real(vline, FILTER_STRICT, vty, cmd); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 2778 | } |
| 2779 | |
| 2780 | /* Configration make from file. */ |
| 2781 | int |
Steve Hill | ea55500 | 2009-07-28 16:36:14 -0400 | [diff] [blame] | 2782 | config_from_file (struct vty *vty, FILE *fp, unsigned int *line_num) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 2783 | { |
| 2784 | int ret; |
Steve Hill | ea55500 | 2009-07-28 16:36:14 -0400 | [diff] [blame] | 2785 | *line_num = 0; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 2786 | vector vline; |
| 2787 | |
| 2788 | while (fgets (vty->buf, VTY_BUFSIZ, fp)) |
| 2789 | { |
Steve Hill | ea55500 | 2009-07-28 16:36:14 -0400 | [diff] [blame] | 2790 | ++(*line_num); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 2791 | vline = cmd_make_strvec (vty->buf); |
| 2792 | |
| 2793 | /* In case of comment line */ |
| 2794 | if (vline == NULL) |
| 2795 | continue; |
| 2796 | /* Execute configuration command : this is strict match */ |
| 2797 | ret = cmd_execute_command_strict (vline, vty, NULL); |
| 2798 | |
| 2799 | /* Try again with setting node to CONFIG_NODE */ |
paul | b92938a | 2002-12-13 21:20:42 +0000 | [diff] [blame] | 2800 | while (ret != CMD_SUCCESS && ret != CMD_WARNING |
hasso | ddd85ed | 2004-10-13 08:18:07 +0000 | [diff] [blame] | 2801 | && ret != CMD_ERR_NOTHING_TODO && vty->node != CONFIG_NODE) |
| 2802 | { |
paul | b92938a | 2002-12-13 21:20:42 +0000 | [diff] [blame] | 2803 | vty->node = node_parent(vty->node); |
hasso | ddd85ed | 2004-10-13 08:18:07 +0000 | [diff] [blame] | 2804 | ret = cmd_execute_command_strict (vline, vty, NULL); |
| 2805 | } |
paul | 9ab6812 | 2003-01-18 01:16:20 +0000 | [diff] [blame] | 2806 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 2807 | cmd_free_strvec (vline); |
| 2808 | |
hasso | ddd85ed | 2004-10-13 08:18:07 +0000 | [diff] [blame] | 2809 | if (ret != CMD_SUCCESS && ret != CMD_WARNING |
| 2810 | && ret != CMD_ERR_NOTHING_TODO) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 2811 | return ret; |
| 2812 | } |
| 2813 | return CMD_SUCCESS; |
| 2814 | } |
| 2815 | |
| 2816 | /* Configration from terminal */ |
| 2817 | DEFUN (config_terminal, |
| 2818 | config_terminal_cmd, |
| 2819 | "configure terminal", |
| 2820 | "Configuration from vty interface\n" |
| 2821 | "Configuration terminal\n") |
| 2822 | { |
| 2823 | if (vty_config_lock (vty)) |
| 2824 | vty->node = CONFIG_NODE; |
| 2825 | else |
| 2826 | { |
| 2827 | vty_out (vty, "VTY configuration is locked by other VTY%s", VTY_NEWLINE); |
| 2828 | return CMD_WARNING; |
| 2829 | } |
| 2830 | return CMD_SUCCESS; |
| 2831 | } |
| 2832 | |
| 2833 | /* Enable command */ |
| 2834 | DEFUN (enable, |
| 2835 | config_enable_cmd, |
| 2836 | "enable", |
| 2837 | "Turn on privileged mode command\n") |
| 2838 | { |
| 2839 | /* If enable password is NULL, change to ENABLE_NODE */ |
| 2840 | if ((host.enable == NULL && host.enable_encrypt == NULL) || |
| 2841 | vty->type == VTY_SHELL_SERV) |
| 2842 | vty->node = ENABLE_NODE; |
| 2843 | else |
| 2844 | vty->node = AUTH_ENABLE_NODE; |
| 2845 | |
| 2846 | return CMD_SUCCESS; |
| 2847 | } |
| 2848 | |
| 2849 | /* Disable command */ |
| 2850 | DEFUN (disable, |
| 2851 | config_disable_cmd, |
| 2852 | "disable", |
| 2853 | "Turn off privileged mode command\n") |
| 2854 | { |
| 2855 | if (vty->node == ENABLE_NODE) |
| 2856 | vty->node = VIEW_NODE; |
| 2857 | return CMD_SUCCESS; |
| 2858 | } |
| 2859 | |
| 2860 | /* Down vty node level. */ |
| 2861 | DEFUN (config_exit, |
| 2862 | config_exit_cmd, |
| 2863 | "exit", |
| 2864 | "Exit current mode and down to previous mode\n") |
| 2865 | { |
| 2866 | switch (vty->node) |
| 2867 | { |
| 2868 | case VIEW_NODE: |
| 2869 | case ENABLE_NODE: |
Paul Jakma | 62687ff | 2008-08-23 14:27:06 +0100 | [diff] [blame] | 2870 | case RESTRICTED_NODE: |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 2871 | if (vty_shell (vty)) |
| 2872 | exit (0); |
| 2873 | else |
| 2874 | vty->status = VTY_CLOSE; |
| 2875 | break; |
| 2876 | case CONFIG_NODE: |
| 2877 | vty->node = ENABLE_NODE; |
| 2878 | vty_config_unlock (vty); |
| 2879 | break; |
| 2880 | case INTERFACE_NODE: |
| 2881 | case ZEBRA_NODE: |
| 2882 | case BGP_NODE: |
| 2883 | case RIP_NODE: |
| 2884 | case RIPNG_NODE: |
Paul Jakma | 5734509 | 2011-12-25 17:52:09 +0100 | [diff] [blame] | 2885 | case BABEL_NODE: |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 2886 | case OSPF_NODE: |
| 2887 | case OSPF6_NODE: |
jardin | 9e867fe | 2003-12-23 08:56:18 +0000 | [diff] [blame] | 2888 | case ISIS_NODE: |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 2889 | case KEYCHAIN_NODE: |
| 2890 | case MASC_NODE: |
| 2891 | case RMAP_NODE: |
Everton Marques | 42e3078 | 2009-11-18 17:19:43 -0200 | [diff] [blame] | 2892 | case PIM_NODE: |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 2893 | case VTY_NODE: |
| 2894 | vty->node = CONFIG_NODE; |
| 2895 | break; |
| 2896 | case BGP_VPNV4_NODE: |
| 2897 | case BGP_IPV4_NODE: |
| 2898 | case BGP_IPV4M_NODE: |
| 2899 | case BGP_IPV6_NODE: |
paul | 1e83659 | 2005-08-22 22:39:56 +0000 | [diff] [blame] | 2900 | case BGP_IPV6M_NODE: |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 2901 | vty->node = BGP_NODE; |
| 2902 | break; |
| 2903 | case KEYCHAIN_KEY_NODE: |
| 2904 | vty->node = KEYCHAIN_NODE; |
| 2905 | break; |
| 2906 | default: |
| 2907 | break; |
| 2908 | } |
| 2909 | return CMD_SUCCESS; |
| 2910 | } |
| 2911 | |
| 2912 | /* quit is alias of exit. */ |
| 2913 | ALIAS (config_exit, |
| 2914 | config_quit_cmd, |
| 2915 | "quit", |
| 2916 | "Exit current mode and down to previous mode\n") |
| 2917 | |
| 2918 | /* End of configuration. */ |
| 2919 | DEFUN (config_end, |
| 2920 | config_end_cmd, |
| 2921 | "end", |
| 2922 | "End current mode and change to enable mode.") |
| 2923 | { |
| 2924 | switch (vty->node) |
| 2925 | { |
| 2926 | case VIEW_NODE: |
| 2927 | case ENABLE_NODE: |
Paul Jakma | 62687ff | 2008-08-23 14:27:06 +0100 | [diff] [blame] | 2928 | case RESTRICTED_NODE: |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 2929 | /* Nothing to do. */ |
| 2930 | break; |
| 2931 | case CONFIG_NODE: |
| 2932 | case INTERFACE_NODE: |
| 2933 | case ZEBRA_NODE: |
| 2934 | case RIP_NODE: |
| 2935 | case RIPNG_NODE: |
Paul Jakma | 5734509 | 2011-12-25 17:52:09 +0100 | [diff] [blame] | 2936 | case BABEL_NODE: |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 2937 | case BGP_NODE: |
| 2938 | case BGP_VPNV4_NODE: |
| 2939 | case BGP_IPV4_NODE: |
| 2940 | case BGP_IPV4M_NODE: |
| 2941 | case BGP_IPV6_NODE: |
paul | 1e83659 | 2005-08-22 22:39:56 +0000 | [diff] [blame] | 2942 | case BGP_IPV6M_NODE: |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 2943 | case RMAP_NODE: |
| 2944 | case OSPF_NODE: |
| 2945 | case OSPF6_NODE: |
jardin | 9e867fe | 2003-12-23 08:56:18 +0000 | [diff] [blame] | 2946 | case ISIS_NODE: |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 2947 | case KEYCHAIN_NODE: |
| 2948 | case KEYCHAIN_KEY_NODE: |
| 2949 | case MASC_NODE: |
Everton Marques | 42e3078 | 2009-11-18 17:19:43 -0200 | [diff] [blame] | 2950 | case PIM_NODE: |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 2951 | case VTY_NODE: |
| 2952 | vty_config_unlock (vty); |
| 2953 | vty->node = ENABLE_NODE; |
| 2954 | break; |
| 2955 | default: |
| 2956 | break; |
| 2957 | } |
| 2958 | return CMD_SUCCESS; |
| 2959 | } |
| 2960 | |
| 2961 | /* Show version. */ |
| 2962 | DEFUN (show_version, |
| 2963 | show_version_cmd, |
| 2964 | "show version", |
| 2965 | SHOW_STR |
| 2966 | "Displays zebra version\n") |
| 2967 | { |
hasso | 12f6ea2 | 2005-03-07 08:35:39 +0000 | [diff] [blame] | 2968 | vty_out (vty, "Quagga %s (%s).%s", QUAGGA_VERSION, host.name?host.name:"", |
| 2969 | VTY_NEWLINE); |
David Lamparter | 0be793e | 2012-11-27 01:34:56 +0000 | [diff] [blame] | 2970 | vty_out (vty, "%s%s%s", QUAGGA_COPYRIGHT, GIT_INFO, VTY_NEWLINE); |
David Lamparter | 7abd875 | 2014-11-22 10:43:29 -0800 | [diff] [blame] | 2971 | vty_out (vty, "configured with:%s %s%s", VTY_NEWLINE, |
| 2972 | QUAGGA_CONFIG_ARGS, VTY_NEWLINE); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 2973 | |
| 2974 | return CMD_SUCCESS; |
| 2975 | } |
| 2976 | |
| 2977 | /* Help display function for all node. */ |
| 2978 | DEFUN (config_help, |
| 2979 | config_help_cmd, |
| 2980 | "help", |
| 2981 | "Description of the interactive help system\n") |
| 2982 | { |
| 2983 | vty_out (vty, |
hasso | 6590f2c | 2004-10-19 20:40:08 +0000 | [diff] [blame] | 2984 | "Quagga VTY provides advanced help feature. When you need help,%s\ |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 2985 | anytime at the command line please press '?'.%s\ |
| 2986 | %s\ |
| 2987 | If nothing matches, the help list will be empty and you must backup%s\ |
| 2988 | until entering a '?' shows the available options.%s\ |
| 2989 | Two styles of help are provided:%s\ |
| 2990 | 1. Full help is available when you are ready to enter a%s\ |
| 2991 | command argument (e.g. 'show ?') and describes each possible%s\ |
| 2992 | argument.%s\ |
| 2993 | 2. Partial help is provided when an abbreviated argument is entered%s\ |
| 2994 | and you want to know what arguments match the input%s\ |
| 2995 | (e.g. 'show me?'.)%s%s", VTY_NEWLINE, VTY_NEWLINE, VTY_NEWLINE, |
| 2996 | VTY_NEWLINE, VTY_NEWLINE, VTY_NEWLINE, VTY_NEWLINE, VTY_NEWLINE, |
| 2997 | VTY_NEWLINE, VTY_NEWLINE, VTY_NEWLINE, VTY_NEWLINE, VTY_NEWLINE); |
| 2998 | return CMD_SUCCESS; |
| 2999 | } |
| 3000 | |
| 3001 | /* Help display function for all node. */ |
| 3002 | DEFUN (config_list, |
| 3003 | config_list_cmd, |
| 3004 | "list", |
| 3005 | "Print command list\n") |
| 3006 | { |
hasso | 8c328f1 | 2004-10-05 21:01:23 +0000 | [diff] [blame] | 3007 | unsigned int i; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 3008 | struct cmd_node *cnode = vector_slot (cmdvec, vty->node); |
| 3009 | struct cmd_element *cmd; |
| 3010 | |
paul | 55468c8 | 2005-03-14 20:19:01 +0000 | [diff] [blame] | 3011 | for (i = 0; i < vector_active (cnode->cmd_vector); i++) |
paul | 4275b1d | 2005-03-09 13:42:23 +0000 | [diff] [blame] | 3012 | if ((cmd = vector_slot (cnode->cmd_vector, i)) != NULL |
| 3013 | && !(cmd->attr == CMD_ATTR_DEPRECATED |
| 3014 | || cmd->attr == CMD_ATTR_HIDDEN)) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 3015 | vty_out (vty, " %s%s", cmd->string, |
| 3016 | VTY_NEWLINE); |
| 3017 | return CMD_SUCCESS; |
| 3018 | } |
| 3019 | |
| 3020 | /* Write current configuration into file. */ |
| 3021 | DEFUN (config_write_file, |
| 3022 | config_write_file_cmd, |
| 3023 | "write file", |
| 3024 | "Write running configuration to memory, network, or terminal\n" |
| 3025 | "Write to configuration file\n") |
| 3026 | { |
hasso | 8c328f1 | 2004-10-05 21:01:23 +0000 | [diff] [blame] | 3027 | unsigned int i; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 3028 | int fd; |
| 3029 | struct cmd_node *node; |
| 3030 | char *config_file; |
| 3031 | char *config_file_tmp = NULL; |
| 3032 | char *config_file_sav = NULL; |
paul | 05865c9 | 2005-10-26 05:49:54 +0000 | [diff] [blame] | 3033 | int ret = CMD_WARNING; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 3034 | struct vty *file_vty; |
| 3035 | |
| 3036 | /* Check and see if we are operating under vtysh configuration */ |
| 3037 | if (host.config == NULL) |
| 3038 | { |
| 3039 | vty_out (vty, "Can't save to configuration file, using vtysh.%s", |
| 3040 | VTY_NEWLINE); |
| 3041 | return CMD_WARNING; |
| 3042 | } |
| 3043 | |
| 3044 | /* Get filename. */ |
| 3045 | config_file = host.config; |
| 3046 | |
paul | 05865c9 | 2005-10-26 05:49:54 +0000 | [diff] [blame] | 3047 | config_file_sav = |
| 3048 | XMALLOC (MTYPE_TMP, strlen (config_file) + strlen (CONF_BACKUP_EXT) + 1); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 3049 | strcpy (config_file_sav, config_file); |
| 3050 | strcat (config_file_sav, CONF_BACKUP_EXT); |
| 3051 | |
| 3052 | |
paul | 05865c9 | 2005-10-26 05:49:54 +0000 | [diff] [blame] | 3053 | config_file_tmp = XMALLOC (MTYPE_TMP, strlen (config_file) + 8); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 3054 | sprintf (config_file_tmp, "%s.XXXXXX", config_file); |
| 3055 | |
| 3056 | /* Open file to configuration write. */ |
| 3057 | fd = mkstemp (config_file_tmp); |
| 3058 | if (fd < 0) |
| 3059 | { |
| 3060 | vty_out (vty, "Can't open configuration file %s.%s", config_file_tmp, |
| 3061 | VTY_NEWLINE); |
paul | 05865c9 | 2005-10-26 05:49:54 +0000 | [diff] [blame] | 3062 | goto finished; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 3063 | } |
| 3064 | |
| 3065 | /* Make vty for configuration file. */ |
| 3066 | file_vty = vty_new (); |
| 3067 | file_vty->fd = fd; |
| 3068 | file_vty->type = VTY_FILE; |
| 3069 | |
| 3070 | /* Config file header print. */ |
| 3071 | vty_out (file_vty, "!\n! Zebra configuration saved from vty\n! "); |
| 3072 | vty_time_print (file_vty, 1); |
| 3073 | vty_out (file_vty, "!\n"); |
| 3074 | |
paul | 55468c8 | 2005-03-14 20:19:01 +0000 | [diff] [blame] | 3075 | for (i = 0; i < vector_active (cmdvec); i++) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 3076 | if ((node = vector_slot (cmdvec, i)) && node->func) |
| 3077 | { |
| 3078 | if ((*node->func) (file_vty)) |
| 3079 | vty_out (file_vty, "!\n"); |
| 3080 | } |
| 3081 | vty_close (file_vty); |
| 3082 | |
| 3083 | if (unlink (config_file_sav) != 0) |
| 3084 | if (errno != ENOENT) |
| 3085 | { |
| 3086 | vty_out (vty, "Can't unlink backup configuration file %s.%s", config_file_sav, |
| 3087 | VTY_NEWLINE); |
paul | 05865c9 | 2005-10-26 05:49:54 +0000 | [diff] [blame] | 3088 | goto finished; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 3089 | } |
| 3090 | if (link (config_file, config_file_sav) != 0) |
| 3091 | { |
| 3092 | vty_out (vty, "Can't backup old configuration file %s.%s", config_file_sav, |
| 3093 | VTY_NEWLINE); |
paul | 05865c9 | 2005-10-26 05:49:54 +0000 | [diff] [blame] | 3094 | goto finished; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 3095 | } |
| 3096 | sync (); |
| 3097 | if (unlink (config_file) != 0) |
| 3098 | { |
| 3099 | vty_out (vty, "Can't unlink configuration file %s.%s", config_file, |
| 3100 | VTY_NEWLINE); |
paul | 05865c9 | 2005-10-26 05:49:54 +0000 | [diff] [blame] | 3101 | goto finished; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 3102 | } |
| 3103 | if (link (config_file_tmp, config_file) != 0) |
| 3104 | { |
| 3105 | vty_out (vty, "Can't save configuration file %s.%s", config_file, |
| 3106 | VTY_NEWLINE); |
paul | 05865c9 | 2005-10-26 05:49:54 +0000 | [diff] [blame] | 3107 | goto finished; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 3108 | } |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 3109 | sync (); |
| 3110 | |
gdt | aa593d5 | 2003-12-22 20:15:53 +0000 | [diff] [blame] | 3111 | if (chmod (config_file, CONFIGFILE_MASK) != 0) |
| 3112 | { |
| 3113 | vty_out (vty, "Can't chmod configuration file %s: %s (%d).%s", |
ajs | 6099b3b | 2004-11-20 02:06:59 +0000 | [diff] [blame] | 3114 | config_file, safe_strerror(errno), errno, VTY_NEWLINE); |
paul | 05865c9 | 2005-10-26 05:49:54 +0000 | [diff] [blame] | 3115 | goto finished; |
gdt | aa593d5 | 2003-12-22 20:15:53 +0000 | [diff] [blame] | 3116 | } |
| 3117 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 3118 | vty_out (vty, "Configuration saved to %s%s", config_file, |
| 3119 | VTY_NEWLINE); |
paul | 05865c9 | 2005-10-26 05:49:54 +0000 | [diff] [blame] | 3120 | ret = CMD_SUCCESS; |
| 3121 | |
| 3122 | finished: |
| 3123 | unlink (config_file_tmp); |
| 3124 | XFREE (MTYPE_TMP, config_file_tmp); |
| 3125 | XFREE (MTYPE_TMP, config_file_sav); |
| 3126 | return ret; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 3127 | } |
| 3128 | |
| 3129 | ALIAS (config_write_file, |
| 3130 | config_write_cmd, |
| 3131 | "write", |
| 3132 | "Write running configuration to memory, network, or terminal\n") |
| 3133 | |
| 3134 | ALIAS (config_write_file, |
| 3135 | config_write_memory_cmd, |
| 3136 | "write memory", |
| 3137 | "Write running configuration to memory, network, or terminal\n" |
| 3138 | "Write configuration to the file (same as write file)\n") |
| 3139 | |
| 3140 | ALIAS (config_write_file, |
| 3141 | copy_runningconfig_startupconfig_cmd, |
| 3142 | "copy running-config startup-config", |
| 3143 | "Copy configuration\n" |
| 3144 | "Copy running config to... \n" |
| 3145 | "Copy running config to startup config (same as write file)\n") |
| 3146 | |
| 3147 | /* Write current configuration into the terminal. */ |
| 3148 | DEFUN (config_write_terminal, |
| 3149 | config_write_terminal_cmd, |
| 3150 | "write terminal", |
| 3151 | "Write running configuration to memory, network, or terminal\n" |
| 3152 | "Write to terminal\n") |
| 3153 | { |
hasso | 8c328f1 | 2004-10-05 21:01:23 +0000 | [diff] [blame] | 3154 | unsigned int i; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 3155 | struct cmd_node *node; |
| 3156 | |
| 3157 | if (vty->type == VTY_SHELL_SERV) |
| 3158 | { |
paul | 55468c8 | 2005-03-14 20:19:01 +0000 | [diff] [blame] | 3159 | for (i = 0; i < vector_active (cmdvec); i++) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 3160 | if ((node = vector_slot (cmdvec, i)) && node->func && node->vtysh) |
| 3161 | { |
| 3162 | if ((*node->func) (vty)) |
| 3163 | vty_out (vty, "!%s", VTY_NEWLINE); |
| 3164 | } |
| 3165 | } |
| 3166 | else |
| 3167 | { |
| 3168 | vty_out (vty, "%sCurrent configuration:%s", VTY_NEWLINE, |
| 3169 | VTY_NEWLINE); |
| 3170 | vty_out (vty, "!%s", VTY_NEWLINE); |
| 3171 | |
paul | 55468c8 | 2005-03-14 20:19:01 +0000 | [diff] [blame] | 3172 | for (i = 0; i < vector_active (cmdvec); i++) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 3173 | if ((node = vector_slot (cmdvec, i)) && node->func) |
| 3174 | { |
| 3175 | if ((*node->func) (vty)) |
| 3176 | vty_out (vty, "!%s", VTY_NEWLINE); |
| 3177 | } |
| 3178 | vty_out (vty, "end%s",VTY_NEWLINE); |
| 3179 | } |
| 3180 | return CMD_SUCCESS; |
| 3181 | } |
| 3182 | |
| 3183 | /* Write current configuration into the terminal. */ |
| 3184 | ALIAS (config_write_terminal, |
| 3185 | show_running_config_cmd, |
| 3186 | "show running-config", |
| 3187 | SHOW_STR |
| 3188 | "running configuration\n") |
| 3189 | |
| 3190 | /* Write startup configuration into the terminal. */ |
| 3191 | DEFUN (show_startup_config, |
| 3192 | show_startup_config_cmd, |
| 3193 | "show startup-config", |
| 3194 | SHOW_STR |
| 3195 | "Contentes of startup configuration\n") |
| 3196 | { |
| 3197 | char buf[BUFSIZ]; |
| 3198 | FILE *confp; |
| 3199 | |
| 3200 | confp = fopen (host.config, "r"); |
| 3201 | if (confp == NULL) |
| 3202 | { |
| 3203 | vty_out (vty, "Can't open configuration file [%s]%s", |
| 3204 | host.config, VTY_NEWLINE); |
| 3205 | return CMD_WARNING; |
| 3206 | } |
| 3207 | |
| 3208 | while (fgets (buf, BUFSIZ, confp)) |
| 3209 | { |
| 3210 | char *cp = buf; |
| 3211 | |
| 3212 | while (*cp != '\r' && *cp != '\n' && *cp != '\0') |
| 3213 | cp++; |
| 3214 | *cp = '\0'; |
| 3215 | |
| 3216 | vty_out (vty, "%s%s", buf, VTY_NEWLINE); |
| 3217 | } |
| 3218 | |
| 3219 | fclose (confp); |
| 3220 | |
| 3221 | return CMD_SUCCESS; |
| 3222 | } |
| 3223 | |
| 3224 | /* Hostname configuration */ |
| 3225 | DEFUN (config_hostname, |
| 3226 | hostname_cmd, |
| 3227 | "hostname WORD", |
| 3228 | "Set system's network name\n" |
| 3229 | "This system's network name\n") |
| 3230 | { |
| 3231 | if (!isalpha((int) *argv[0])) |
| 3232 | { |
| 3233 | vty_out (vty, "Please specify string starting with alphabet%s", VTY_NEWLINE); |
| 3234 | return CMD_WARNING; |
| 3235 | } |
| 3236 | |
| 3237 | if (host.name) |
paul | 05865c9 | 2005-10-26 05:49:54 +0000 | [diff] [blame] | 3238 | XFREE (MTYPE_HOST, host.name); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 3239 | |
paul | 05865c9 | 2005-10-26 05:49:54 +0000 | [diff] [blame] | 3240 | host.name = XSTRDUP (MTYPE_HOST, argv[0]); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 3241 | return CMD_SUCCESS; |
| 3242 | } |
| 3243 | |
| 3244 | DEFUN (config_no_hostname, |
| 3245 | no_hostname_cmd, |
| 3246 | "no hostname [HOSTNAME]", |
| 3247 | NO_STR |
| 3248 | "Reset system's network name\n" |
| 3249 | "Host name of this router\n") |
| 3250 | { |
| 3251 | if (host.name) |
paul | 05865c9 | 2005-10-26 05:49:54 +0000 | [diff] [blame] | 3252 | XFREE (MTYPE_HOST, host.name); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 3253 | host.name = NULL; |
| 3254 | return CMD_SUCCESS; |
| 3255 | } |
| 3256 | |
| 3257 | /* VTY interface password set. */ |
| 3258 | DEFUN (config_password, password_cmd, |
| 3259 | "password (8|) WORD", |
| 3260 | "Assign the terminal connection password\n" |
| 3261 | "Specifies a HIDDEN password will follow\n" |
| 3262 | "dummy string \n" |
| 3263 | "The HIDDEN line password string\n") |
| 3264 | { |
| 3265 | /* Argument check. */ |
| 3266 | if (argc == 0) |
| 3267 | { |
| 3268 | vty_out (vty, "Please specify password.%s", VTY_NEWLINE); |
| 3269 | return CMD_WARNING; |
| 3270 | } |
| 3271 | |
| 3272 | if (argc == 2) |
| 3273 | { |
| 3274 | if (*argv[0] == '8') |
| 3275 | { |
| 3276 | if (host.password) |
paul | 05865c9 | 2005-10-26 05:49:54 +0000 | [diff] [blame] | 3277 | XFREE (MTYPE_HOST, host.password); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 3278 | host.password = NULL; |
| 3279 | if (host.password_encrypt) |
paul | 05865c9 | 2005-10-26 05:49:54 +0000 | [diff] [blame] | 3280 | XFREE (MTYPE_HOST, host.password_encrypt); |
| 3281 | host.password_encrypt = XSTRDUP (MTYPE_HOST, argv[1]); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 3282 | return CMD_SUCCESS; |
| 3283 | } |
| 3284 | else |
| 3285 | { |
| 3286 | vty_out (vty, "Unknown encryption type.%s", VTY_NEWLINE); |
| 3287 | return CMD_WARNING; |
| 3288 | } |
| 3289 | } |
| 3290 | |
| 3291 | if (!isalnum ((int) *argv[0])) |
| 3292 | { |
| 3293 | vty_out (vty, |
| 3294 | "Please specify string starting with alphanumeric%s", VTY_NEWLINE); |
| 3295 | return CMD_WARNING; |
| 3296 | } |
| 3297 | |
| 3298 | if (host.password) |
paul | 05865c9 | 2005-10-26 05:49:54 +0000 | [diff] [blame] | 3299 | XFREE (MTYPE_HOST, host.password); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 3300 | host.password = NULL; |
| 3301 | |
| 3302 | if (host.encrypt) |
| 3303 | { |
| 3304 | if (host.password_encrypt) |
paul | 05865c9 | 2005-10-26 05:49:54 +0000 | [diff] [blame] | 3305 | XFREE (MTYPE_HOST, host.password_encrypt); |
| 3306 | host.password_encrypt = XSTRDUP (MTYPE_HOST, zencrypt (argv[0])); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 3307 | } |
| 3308 | else |
paul | 05865c9 | 2005-10-26 05:49:54 +0000 | [diff] [blame] | 3309 | host.password = XSTRDUP (MTYPE_HOST, argv[0]); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 3310 | |
| 3311 | return CMD_SUCCESS; |
| 3312 | } |
| 3313 | |
| 3314 | ALIAS (config_password, password_text_cmd, |
| 3315 | "password LINE", |
| 3316 | "Assign the terminal connection password\n" |
| 3317 | "The UNENCRYPTED (cleartext) line password\n") |
| 3318 | |
| 3319 | /* VTY enable password set. */ |
| 3320 | DEFUN (config_enable_password, enable_password_cmd, |
| 3321 | "enable password (8|) WORD", |
| 3322 | "Modify enable password parameters\n" |
| 3323 | "Assign the privileged level password\n" |
| 3324 | "Specifies a HIDDEN password will follow\n" |
| 3325 | "dummy string \n" |
| 3326 | "The HIDDEN 'enable' password string\n") |
| 3327 | { |
| 3328 | /* Argument check. */ |
| 3329 | if (argc == 0) |
| 3330 | { |
| 3331 | vty_out (vty, "Please specify password.%s", VTY_NEWLINE); |
| 3332 | return CMD_WARNING; |
| 3333 | } |
| 3334 | |
| 3335 | /* Crypt type is specified. */ |
| 3336 | if (argc == 2) |
| 3337 | { |
| 3338 | if (*argv[0] == '8') |
| 3339 | { |
| 3340 | if (host.enable) |
paul | 05865c9 | 2005-10-26 05:49:54 +0000 | [diff] [blame] | 3341 | XFREE (MTYPE_HOST, host.enable); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 3342 | host.enable = NULL; |
| 3343 | |
| 3344 | if (host.enable_encrypt) |
paul | 05865c9 | 2005-10-26 05:49:54 +0000 | [diff] [blame] | 3345 | XFREE (MTYPE_HOST, host.enable_encrypt); |
| 3346 | host.enable_encrypt = XSTRDUP (MTYPE_HOST, argv[1]); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 3347 | |
| 3348 | return CMD_SUCCESS; |
| 3349 | } |
| 3350 | else |
| 3351 | { |
| 3352 | vty_out (vty, "Unknown encryption type.%s", VTY_NEWLINE); |
| 3353 | return CMD_WARNING; |
| 3354 | } |
| 3355 | } |
| 3356 | |
| 3357 | if (!isalnum ((int) *argv[0])) |
| 3358 | { |
| 3359 | vty_out (vty, |
| 3360 | "Please specify string starting with alphanumeric%s", VTY_NEWLINE); |
| 3361 | return CMD_WARNING; |
| 3362 | } |
| 3363 | |
| 3364 | if (host.enable) |
paul | 05865c9 | 2005-10-26 05:49:54 +0000 | [diff] [blame] | 3365 | XFREE (MTYPE_HOST, host.enable); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 3366 | host.enable = NULL; |
| 3367 | |
| 3368 | /* Plain password input. */ |
| 3369 | if (host.encrypt) |
| 3370 | { |
| 3371 | if (host.enable_encrypt) |
paul | 05865c9 | 2005-10-26 05:49:54 +0000 | [diff] [blame] | 3372 | XFREE (MTYPE_HOST, host.enable_encrypt); |
| 3373 | host.enable_encrypt = XSTRDUP (MTYPE_HOST, zencrypt (argv[0])); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 3374 | } |
| 3375 | else |
paul | 05865c9 | 2005-10-26 05:49:54 +0000 | [diff] [blame] | 3376 | host.enable = XSTRDUP (MTYPE_HOST, argv[0]); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 3377 | |
| 3378 | return CMD_SUCCESS; |
| 3379 | } |
| 3380 | |
| 3381 | ALIAS (config_enable_password, |
| 3382 | enable_password_text_cmd, |
| 3383 | "enable password LINE", |
| 3384 | "Modify enable password parameters\n" |
| 3385 | "Assign the privileged level password\n" |
| 3386 | "The UNENCRYPTED (cleartext) 'enable' password\n") |
| 3387 | |
| 3388 | /* VTY enable password delete. */ |
| 3389 | DEFUN (no_config_enable_password, no_enable_password_cmd, |
| 3390 | "no enable password", |
| 3391 | NO_STR |
| 3392 | "Modify enable password parameters\n" |
| 3393 | "Assign the privileged level password\n") |
| 3394 | { |
| 3395 | if (host.enable) |
paul | 05865c9 | 2005-10-26 05:49:54 +0000 | [diff] [blame] | 3396 | XFREE (MTYPE_HOST, host.enable); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 3397 | host.enable = NULL; |
| 3398 | |
| 3399 | if (host.enable_encrypt) |
paul | 05865c9 | 2005-10-26 05:49:54 +0000 | [diff] [blame] | 3400 | XFREE (MTYPE_HOST, host.enable_encrypt); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 3401 | host.enable_encrypt = NULL; |
| 3402 | |
| 3403 | return CMD_SUCCESS; |
| 3404 | } |
| 3405 | |
| 3406 | DEFUN (service_password_encrypt, |
| 3407 | service_password_encrypt_cmd, |
| 3408 | "service password-encryption", |
| 3409 | "Set up miscellaneous service\n" |
| 3410 | "Enable encrypted passwords\n") |
| 3411 | { |
| 3412 | if (host.encrypt) |
| 3413 | return CMD_SUCCESS; |
| 3414 | |
| 3415 | host.encrypt = 1; |
| 3416 | |
| 3417 | if (host.password) |
| 3418 | { |
| 3419 | if (host.password_encrypt) |
paul | 05865c9 | 2005-10-26 05:49:54 +0000 | [diff] [blame] | 3420 | XFREE (MTYPE_HOST, host.password_encrypt); |
| 3421 | host.password_encrypt = XSTRDUP (MTYPE_HOST, zencrypt (host.password)); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 3422 | } |
| 3423 | if (host.enable) |
| 3424 | { |
| 3425 | if (host.enable_encrypt) |
paul | 05865c9 | 2005-10-26 05:49:54 +0000 | [diff] [blame] | 3426 | XFREE (MTYPE_HOST, host.enable_encrypt); |
| 3427 | host.enable_encrypt = XSTRDUP (MTYPE_HOST, zencrypt (host.enable)); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 3428 | } |
| 3429 | |
| 3430 | return CMD_SUCCESS; |
| 3431 | } |
| 3432 | |
| 3433 | DEFUN (no_service_password_encrypt, |
| 3434 | no_service_password_encrypt_cmd, |
| 3435 | "no service password-encryption", |
| 3436 | NO_STR |
| 3437 | "Set up miscellaneous service\n" |
| 3438 | "Enable encrypted passwords\n") |
| 3439 | { |
| 3440 | if (! host.encrypt) |
| 3441 | return CMD_SUCCESS; |
| 3442 | |
| 3443 | host.encrypt = 0; |
| 3444 | |
| 3445 | if (host.password_encrypt) |
paul | 05865c9 | 2005-10-26 05:49:54 +0000 | [diff] [blame] | 3446 | XFREE (MTYPE_HOST, host.password_encrypt); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 3447 | host.password_encrypt = NULL; |
| 3448 | |
| 3449 | if (host.enable_encrypt) |
paul | 05865c9 | 2005-10-26 05:49:54 +0000 | [diff] [blame] | 3450 | XFREE (MTYPE_HOST, host.enable_encrypt); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 3451 | host.enable_encrypt = NULL; |
| 3452 | |
| 3453 | return CMD_SUCCESS; |
| 3454 | } |
| 3455 | |
| 3456 | DEFUN (config_terminal_length, config_terminal_length_cmd, |
| 3457 | "terminal length <0-512>", |
| 3458 | "Set terminal line parameters\n" |
| 3459 | "Set number of lines on a screen\n" |
| 3460 | "Number of lines on screen (0 for no pausing)\n") |
| 3461 | { |
| 3462 | int lines; |
| 3463 | char *endptr = NULL; |
| 3464 | |
| 3465 | lines = strtol (argv[0], &endptr, 10); |
| 3466 | if (lines < 0 || lines > 512 || *endptr != '\0') |
| 3467 | { |
| 3468 | vty_out (vty, "length is malformed%s", VTY_NEWLINE); |
| 3469 | return CMD_WARNING; |
| 3470 | } |
| 3471 | vty->lines = lines; |
| 3472 | |
| 3473 | return CMD_SUCCESS; |
| 3474 | } |
| 3475 | |
| 3476 | DEFUN (config_terminal_no_length, config_terminal_no_length_cmd, |
| 3477 | "terminal no length", |
| 3478 | "Set terminal line parameters\n" |
| 3479 | NO_STR |
| 3480 | "Set number of lines on a screen\n") |
| 3481 | { |
| 3482 | vty->lines = -1; |
| 3483 | return CMD_SUCCESS; |
| 3484 | } |
| 3485 | |
| 3486 | DEFUN (service_terminal_length, service_terminal_length_cmd, |
| 3487 | "service terminal-length <0-512>", |
| 3488 | "Set up miscellaneous service\n" |
| 3489 | "System wide terminal length configuration\n" |
| 3490 | "Number of lines of VTY (0 means no line control)\n") |
| 3491 | { |
| 3492 | int lines; |
| 3493 | char *endptr = NULL; |
| 3494 | |
| 3495 | lines = strtol (argv[0], &endptr, 10); |
| 3496 | if (lines < 0 || lines > 512 || *endptr != '\0') |
| 3497 | { |
| 3498 | vty_out (vty, "length is malformed%s", VTY_NEWLINE); |
| 3499 | return CMD_WARNING; |
| 3500 | } |
| 3501 | host.lines = lines; |
| 3502 | |
| 3503 | return CMD_SUCCESS; |
| 3504 | } |
| 3505 | |
| 3506 | DEFUN (no_service_terminal_length, no_service_terminal_length_cmd, |
| 3507 | "no service terminal-length [<0-512>]", |
| 3508 | NO_STR |
| 3509 | "Set up miscellaneous service\n" |
| 3510 | "System wide terminal length configuration\n" |
| 3511 | "Number of lines of VTY (0 means no line control)\n") |
| 3512 | { |
| 3513 | host.lines = -1; |
| 3514 | return CMD_SUCCESS; |
| 3515 | } |
| 3516 | |
ajs | 2885f72 | 2004-12-17 23:16:33 +0000 | [diff] [blame] | 3517 | DEFUN_HIDDEN (do_echo, |
| 3518 | echo_cmd, |
| 3519 | "echo .MESSAGE", |
| 3520 | "Echo a message back to the vty\n" |
| 3521 | "The message to echo\n") |
| 3522 | { |
| 3523 | char *message; |
| 3524 | |
ajs | f6834d4 | 2005-01-28 20:28:35 +0000 | [diff] [blame] | 3525 | vty_out (vty, "%s%s", ((message = argv_concat(argv, argc, 0)) ? message : ""), |
| 3526 | VTY_NEWLINE); |
| 3527 | if (message) |
| 3528 | XFREE(MTYPE_TMP, message); |
ajs | 2885f72 | 2004-12-17 23:16:33 +0000 | [diff] [blame] | 3529 | return CMD_SUCCESS; |
| 3530 | } |
| 3531 | |
ajs | 274a4a4 | 2004-12-07 15:39:31 +0000 | [diff] [blame] | 3532 | DEFUN (config_logmsg, |
| 3533 | config_logmsg_cmd, |
| 3534 | "logmsg "LOG_LEVELS" .MESSAGE", |
| 3535 | "Send a message to enabled logging destinations\n" |
| 3536 | LOG_LEVEL_DESC |
| 3537 | "The message to send\n") |
| 3538 | { |
| 3539 | int level; |
| 3540 | char *message; |
| 3541 | |
| 3542 | if ((level = level_match(argv[0])) == ZLOG_DISABLED) |
| 3543 | return CMD_ERR_NO_MATCH; |
| 3544 | |
Christian Hammers | fc95186 | 2011-03-23 13:07:55 +0300 | [diff] [blame] | 3545 | zlog(NULL, level, "%s", ((message = argv_concat(argv, argc, 1)) ? message : "")); |
ajs | f6834d4 | 2005-01-28 20:28:35 +0000 | [diff] [blame] | 3546 | if (message) |
| 3547 | XFREE(MTYPE_TMP, message); |
ajs | 274a4a4 | 2004-12-07 15:39:31 +0000 | [diff] [blame] | 3548 | return CMD_SUCCESS; |
| 3549 | } |
| 3550 | |
| 3551 | DEFUN (show_logging, |
| 3552 | show_logging_cmd, |
| 3553 | "show logging", |
| 3554 | SHOW_STR |
| 3555 | "Show current logging configuration\n") |
| 3556 | { |
| 3557 | struct zlog *zl = zlog_default; |
| 3558 | |
| 3559 | vty_out (vty, "Syslog logging: "); |
| 3560 | if (zl->maxlvl[ZLOG_DEST_SYSLOG] == ZLOG_DISABLED) |
| 3561 | vty_out (vty, "disabled"); |
| 3562 | else |
| 3563 | vty_out (vty, "level %s, facility %s, ident %s", |
| 3564 | zlog_priority[zl->maxlvl[ZLOG_DEST_SYSLOG]], |
| 3565 | facility_name(zl->facility), zl->ident); |
| 3566 | vty_out (vty, "%s", VTY_NEWLINE); |
| 3567 | |
| 3568 | vty_out (vty, "Stdout logging: "); |
| 3569 | if (zl->maxlvl[ZLOG_DEST_STDOUT] == ZLOG_DISABLED) |
| 3570 | vty_out (vty, "disabled"); |
| 3571 | else |
| 3572 | vty_out (vty, "level %s", |
| 3573 | zlog_priority[zl->maxlvl[ZLOG_DEST_STDOUT]]); |
| 3574 | vty_out (vty, "%s", VTY_NEWLINE); |
| 3575 | |
| 3576 | vty_out (vty, "Monitor logging: "); |
| 3577 | if (zl->maxlvl[ZLOG_DEST_MONITOR] == ZLOG_DISABLED) |
| 3578 | vty_out (vty, "disabled"); |
| 3579 | else |
| 3580 | vty_out (vty, "level %s", |
| 3581 | zlog_priority[zl->maxlvl[ZLOG_DEST_MONITOR]]); |
| 3582 | vty_out (vty, "%s", VTY_NEWLINE); |
| 3583 | |
| 3584 | vty_out (vty, "File logging: "); |
| 3585 | if ((zl->maxlvl[ZLOG_DEST_FILE] == ZLOG_DISABLED) || |
| 3586 | !zl->fp) |
| 3587 | vty_out (vty, "disabled"); |
| 3588 | else |
| 3589 | vty_out (vty, "level %s, filename %s", |
| 3590 | zlog_priority[zl->maxlvl[ZLOG_DEST_FILE]], |
| 3591 | zl->filename); |
| 3592 | vty_out (vty, "%s", VTY_NEWLINE); |
| 3593 | |
| 3594 | vty_out (vty, "Protocol name: %s%s", |
| 3595 | zlog_proto_names[zl->protocol], VTY_NEWLINE); |
| 3596 | vty_out (vty, "Record priority: %s%s", |
| 3597 | (zl->record_priority ? "enabled" : "disabled"), VTY_NEWLINE); |
Andrew J. Schorr | 1ed72e0 | 2007-04-28 22:14:10 +0000 | [diff] [blame] | 3598 | vty_out (vty, "Timestamp precision: %d%s", |
| 3599 | zl->timestamp_precision, VTY_NEWLINE); |
ajs | 274a4a4 | 2004-12-07 15:39:31 +0000 | [diff] [blame] | 3600 | |
| 3601 | return CMD_SUCCESS; |
| 3602 | } |
| 3603 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 3604 | DEFUN (config_log_stdout, |
| 3605 | config_log_stdout_cmd, |
| 3606 | "log stdout", |
| 3607 | "Logging control\n" |
ajs | 274a4a4 | 2004-12-07 15:39:31 +0000 | [diff] [blame] | 3608 | "Set stdout logging level\n") |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 3609 | { |
ajs | 274a4a4 | 2004-12-07 15:39:31 +0000 | [diff] [blame] | 3610 | zlog_set_level (NULL, ZLOG_DEST_STDOUT, zlog_default->default_lvl); |
| 3611 | return CMD_SUCCESS; |
| 3612 | } |
| 3613 | |
| 3614 | DEFUN (config_log_stdout_level, |
| 3615 | config_log_stdout_level_cmd, |
| 3616 | "log stdout "LOG_LEVELS, |
| 3617 | "Logging control\n" |
| 3618 | "Set stdout logging level\n" |
| 3619 | LOG_LEVEL_DESC) |
| 3620 | { |
| 3621 | int level; |
| 3622 | |
| 3623 | if ((level = level_match(argv[0])) == ZLOG_DISABLED) |
| 3624 | return CMD_ERR_NO_MATCH; |
| 3625 | zlog_set_level (NULL, ZLOG_DEST_STDOUT, level); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 3626 | return CMD_SUCCESS; |
| 3627 | } |
| 3628 | |
| 3629 | DEFUN (no_config_log_stdout, |
| 3630 | no_config_log_stdout_cmd, |
ajs | 274a4a4 | 2004-12-07 15:39:31 +0000 | [diff] [blame] | 3631 | "no log stdout [LEVEL]", |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 3632 | NO_STR |
| 3633 | "Logging control\n" |
ajs | 274a4a4 | 2004-12-07 15:39:31 +0000 | [diff] [blame] | 3634 | "Cancel logging to stdout\n" |
| 3635 | "Logging level\n") |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 3636 | { |
ajs | 274a4a4 | 2004-12-07 15:39:31 +0000 | [diff] [blame] | 3637 | zlog_set_level (NULL, ZLOG_DEST_STDOUT, ZLOG_DISABLED); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 3638 | return CMD_SUCCESS; |
| 3639 | } |
| 3640 | |
ajs | 274a4a4 | 2004-12-07 15:39:31 +0000 | [diff] [blame] | 3641 | DEFUN (config_log_monitor, |
| 3642 | config_log_monitor_cmd, |
| 3643 | "log monitor", |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 3644 | "Logging control\n" |
ajs | 274a4a4 | 2004-12-07 15:39:31 +0000 | [diff] [blame] | 3645 | "Set terminal line (monitor) logging level\n") |
| 3646 | { |
| 3647 | zlog_set_level (NULL, ZLOG_DEST_MONITOR, zlog_default->default_lvl); |
| 3648 | return CMD_SUCCESS; |
| 3649 | } |
| 3650 | |
| 3651 | DEFUN (config_log_monitor_level, |
| 3652 | config_log_monitor_level_cmd, |
| 3653 | "log monitor "LOG_LEVELS, |
| 3654 | "Logging control\n" |
| 3655 | "Set terminal line (monitor) logging level\n" |
| 3656 | LOG_LEVEL_DESC) |
| 3657 | { |
| 3658 | int level; |
| 3659 | |
| 3660 | if ((level = level_match(argv[0])) == ZLOG_DISABLED) |
| 3661 | return CMD_ERR_NO_MATCH; |
| 3662 | zlog_set_level (NULL, ZLOG_DEST_MONITOR, level); |
| 3663 | return CMD_SUCCESS; |
| 3664 | } |
| 3665 | |
| 3666 | DEFUN (no_config_log_monitor, |
| 3667 | no_config_log_monitor_cmd, |
| 3668 | "no log monitor [LEVEL]", |
| 3669 | NO_STR |
| 3670 | "Logging control\n" |
| 3671 | "Disable terminal line (monitor) logging\n" |
| 3672 | "Logging level\n") |
| 3673 | { |
| 3674 | zlog_set_level (NULL, ZLOG_DEST_MONITOR, ZLOG_DISABLED); |
| 3675 | return CMD_SUCCESS; |
| 3676 | } |
| 3677 | |
| 3678 | static int |
| 3679 | set_log_file(struct vty *vty, const char *fname, int loglevel) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 3680 | { |
| 3681 | int ret; |
paul | 9035efa | 2004-10-10 11:56:56 +0000 | [diff] [blame] | 3682 | char *p = NULL; |
| 3683 | const char *fullpath; |
| 3684 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 3685 | /* Path detection. */ |
ajs | 274a4a4 | 2004-12-07 15:39:31 +0000 | [diff] [blame] | 3686 | if (! IS_DIRECTORY_SEP (*fname)) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 3687 | { |
paul | 9035efa | 2004-10-10 11:56:56 +0000 | [diff] [blame] | 3688 | char cwd[MAXPATHLEN+1]; |
| 3689 | cwd[MAXPATHLEN] = '\0'; |
| 3690 | |
| 3691 | if (getcwd (cwd, MAXPATHLEN) == NULL) |
| 3692 | { |
| 3693 | zlog_err ("config_log_file: Unable to alloc mem!"); |
| 3694 | return CMD_WARNING; |
| 3695 | } |
| 3696 | |
ajs | 274a4a4 | 2004-12-07 15:39:31 +0000 | [diff] [blame] | 3697 | if ( (p = XMALLOC (MTYPE_TMP, strlen (cwd) + strlen (fname) + 2)) |
paul | 9035efa | 2004-10-10 11:56:56 +0000 | [diff] [blame] | 3698 | == NULL) |
| 3699 | { |
| 3700 | zlog_err ("config_log_file: Unable to alloc mem!"); |
| 3701 | return CMD_WARNING; |
| 3702 | } |
ajs | 274a4a4 | 2004-12-07 15:39:31 +0000 | [diff] [blame] | 3703 | sprintf (p, "%s/%s", cwd, fname); |
paul | 9035efa | 2004-10-10 11:56:56 +0000 | [diff] [blame] | 3704 | fullpath = p; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 3705 | } |
| 3706 | else |
ajs | 274a4a4 | 2004-12-07 15:39:31 +0000 | [diff] [blame] | 3707 | fullpath = fname; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 3708 | |
ajs | 274a4a4 | 2004-12-07 15:39:31 +0000 | [diff] [blame] | 3709 | ret = zlog_set_file (NULL, fullpath, loglevel); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 3710 | |
paul | 9035efa | 2004-10-10 11:56:56 +0000 | [diff] [blame] | 3711 | if (p) |
| 3712 | XFREE (MTYPE_TMP, p); |
| 3713 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 3714 | if (!ret) |
| 3715 | { |
ajs | 274a4a4 | 2004-12-07 15:39:31 +0000 | [diff] [blame] | 3716 | vty_out (vty, "can't open logfile %s\n", fname); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 3717 | return CMD_WARNING; |
| 3718 | } |
| 3719 | |
| 3720 | if (host.logfile) |
paul | 05865c9 | 2005-10-26 05:49:54 +0000 | [diff] [blame] | 3721 | XFREE (MTYPE_HOST, host.logfile); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 3722 | |
paul | 05865c9 | 2005-10-26 05:49:54 +0000 | [diff] [blame] | 3723 | host.logfile = XSTRDUP (MTYPE_HOST, fname); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 3724 | |
| 3725 | return CMD_SUCCESS; |
| 3726 | } |
| 3727 | |
ajs | 274a4a4 | 2004-12-07 15:39:31 +0000 | [diff] [blame] | 3728 | DEFUN (config_log_file, |
| 3729 | config_log_file_cmd, |
| 3730 | "log file FILENAME", |
| 3731 | "Logging control\n" |
| 3732 | "Logging to file\n" |
| 3733 | "Logging filename\n") |
| 3734 | { |
| 3735 | return set_log_file(vty, argv[0], zlog_default->default_lvl); |
| 3736 | } |
| 3737 | |
| 3738 | DEFUN (config_log_file_level, |
| 3739 | config_log_file_level_cmd, |
| 3740 | "log file FILENAME "LOG_LEVELS, |
| 3741 | "Logging control\n" |
| 3742 | "Logging to file\n" |
| 3743 | "Logging filename\n" |
| 3744 | LOG_LEVEL_DESC) |
| 3745 | { |
| 3746 | int level; |
| 3747 | |
| 3748 | if ((level = level_match(argv[1])) == ZLOG_DISABLED) |
| 3749 | return CMD_ERR_NO_MATCH; |
| 3750 | return set_log_file(vty, argv[0], level); |
| 3751 | } |
| 3752 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 3753 | DEFUN (no_config_log_file, |
| 3754 | no_config_log_file_cmd, |
| 3755 | "no log file [FILENAME]", |
| 3756 | NO_STR |
| 3757 | "Logging control\n" |
| 3758 | "Cancel logging to file\n" |
| 3759 | "Logging file name\n") |
| 3760 | { |
| 3761 | zlog_reset_file (NULL); |
| 3762 | |
| 3763 | if (host.logfile) |
paul | 05865c9 | 2005-10-26 05:49:54 +0000 | [diff] [blame] | 3764 | XFREE (MTYPE_HOST, host.logfile); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 3765 | |
| 3766 | host.logfile = NULL; |
| 3767 | |
| 3768 | return CMD_SUCCESS; |
| 3769 | } |
| 3770 | |
ajs | 274a4a4 | 2004-12-07 15:39:31 +0000 | [diff] [blame] | 3771 | ALIAS (no_config_log_file, |
| 3772 | no_config_log_file_level_cmd, |
| 3773 | "no log file FILENAME LEVEL", |
| 3774 | NO_STR |
| 3775 | "Logging control\n" |
| 3776 | "Cancel logging to file\n" |
| 3777 | "Logging file name\n" |
| 3778 | "Logging level\n") |
| 3779 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 3780 | DEFUN (config_log_syslog, |
| 3781 | config_log_syslog_cmd, |
| 3782 | "log syslog", |
| 3783 | "Logging control\n" |
ajs | 274a4a4 | 2004-12-07 15:39:31 +0000 | [diff] [blame] | 3784 | "Set syslog logging level\n") |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 3785 | { |
ajs | 274a4a4 | 2004-12-07 15:39:31 +0000 | [diff] [blame] | 3786 | zlog_set_level (NULL, ZLOG_DEST_SYSLOG, zlog_default->default_lvl); |
paul | 12ab19f | 2003-07-26 06:14:55 +0000 | [diff] [blame] | 3787 | return CMD_SUCCESS; |
| 3788 | } |
| 3789 | |
ajs | 274a4a4 | 2004-12-07 15:39:31 +0000 | [diff] [blame] | 3790 | DEFUN (config_log_syslog_level, |
| 3791 | config_log_syslog_level_cmd, |
| 3792 | "log syslog "LOG_LEVELS, |
paul | 12ab19f | 2003-07-26 06:14:55 +0000 | [diff] [blame] | 3793 | "Logging control\n" |
ajs | 274a4a4 | 2004-12-07 15:39:31 +0000 | [diff] [blame] | 3794 | "Set syslog logging level\n" |
| 3795 | LOG_LEVEL_DESC) |
paul | 12ab19f | 2003-07-26 06:14:55 +0000 | [diff] [blame] | 3796 | { |
ajs | 274a4a4 | 2004-12-07 15:39:31 +0000 | [diff] [blame] | 3797 | int level; |
paul | 12ab19f | 2003-07-26 06:14:55 +0000 | [diff] [blame] | 3798 | |
ajs | 274a4a4 | 2004-12-07 15:39:31 +0000 | [diff] [blame] | 3799 | if ((level = level_match(argv[0])) == ZLOG_DISABLED) |
| 3800 | return CMD_ERR_NO_MATCH; |
| 3801 | zlog_set_level (NULL, ZLOG_DEST_SYSLOG, level); |
| 3802 | return CMD_SUCCESS; |
| 3803 | } |
paul | 12ab19f | 2003-07-26 06:14:55 +0000 | [diff] [blame] | 3804 | |
ajs | 274a4a4 | 2004-12-07 15:39:31 +0000 | [diff] [blame] | 3805 | DEFUN_DEPRECATED (config_log_syslog_facility, |
| 3806 | config_log_syslog_facility_cmd, |
| 3807 | "log syslog facility "LOG_FACILITIES, |
| 3808 | "Logging control\n" |
| 3809 | "Logging goes to syslog\n" |
| 3810 | "(Deprecated) Facility parameter for syslog messages\n" |
| 3811 | LOG_FACILITY_DESC) |
| 3812 | { |
| 3813 | int facility; |
paul | 12ab19f | 2003-07-26 06:14:55 +0000 | [diff] [blame] | 3814 | |
ajs | 274a4a4 | 2004-12-07 15:39:31 +0000 | [diff] [blame] | 3815 | if ((facility = facility_match(argv[0])) < 0) |
| 3816 | return CMD_ERR_NO_MATCH; |
| 3817 | |
| 3818 | zlog_set_level (NULL, ZLOG_DEST_SYSLOG, zlog_default->default_lvl); |
paul | 12ab19f | 2003-07-26 06:14:55 +0000 | [diff] [blame] | 3819 | zlog_default->facility = facility; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 3820 | return CMD_SUCCESS; |
| 3821 | } |
| 3822 | |
| 3823 | DEFUN (no_config_log_syslog, |
| 3824 | no_config_log_syslog_cmd, |
ajs | 274a4a4 | 2004-12-07 15:39:31 +0000 | [diff] [blame] | 3825 | "no log syslog [LEVEL]", |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 3826 | NO_STR |
| 3827 | "Logging control\n" |
ajs | 274a4a4 | 2004-12-07 15:39:31 +0000 | [diff] [blame] | 3828 | "Cancel logging to syslog\n" |
| 3829 | "Logging level\n") |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 3830 | { |
ajs | 274a4a4 | 2004-12-07 15:39:31 +0000 | [diff] [blame] | 3831 | zlog_set_level (NULL, ZLOG_DEST_SYSLOG, ZLOG_DISABLED); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 3832 | return CMD_SUCCESS; |
| 3833 | } |
| 3834 | |
paul | 12ab19f | 2003-07-26 06:14:55 +0000 | [diff] [blame] | 3835 | ALIAS (no_config_log_syslog, |
| 3836 | no_config_log_syslog_facility_cmd, |
ajs | 274a4a4 | 2004-12-07 15:39:31 +0000 | [diff] [blame] | 3837 | "no log syslog facility "LOG_FACILITIES, |
paul | 12ab19f | 2003-07-26 06:14:55 +0000 | [diff] [blame] | 3838 | NO_STR |
| 3839 | "Logging control\n" |
| 3840 | "Logging goes to syslog\n" |
| 3841 | "Facility parameter for syslog messages\n" |
ajs | 274a4a4 | 2004-12-07 15:39:31 +0000 | [diff] [blame] | 3842 | LOG_FACILITY_DESC) |
paul | 12ab19f | 2003-07-26 06:14:55 +0000 | [diff] [blame] | 3843 | |
ajs | 274a4a4 | 2004-12-07 15:39:31 +0000 | [diff] [blame] | 3844 | DEFUN (config_log_facility, |
| 3845 | config_log_facility_cmd, |
| 3846 | "log facility "LOG_FACILITIES, |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 3847 | "Logging control\n" |
ajs | 274a4a4 | 2004-12-07 15:39:31 +0000 | [diff] [blame] | 3848 | "Facility parameter for syslog messages\n" |
| 3849 | LOG_FACILITY_DESC) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 3850 | { |
ajs | 274a4a4 | 2004-12-07 15:39:31 +0000 | [diff] [blame] | 3851 | int facility; |
| 3852 | |
| 3853 | if ((facility = facility_match(argv[0])) < 0) |
| 3854 | return CMD_ERR_NO_MATCH; |
| 3855 | zlog_default->facility = facility; |
| 3856 | return CMD_SUCCESS; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 3857 | } |
| 3858 | |
ajs | 274a4a4 | 2004-12-07 15:39:31 +0000 | [diff] [blame] | 3859 | DEFUN (no_config_log_facility, |
| 3860 | no_config_log_facility_cmd, |
| 3861 | "no log facility [FACILITY]", |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 3862 | NO_STR |
| 3863 | "Logging control\n" |
ajs | 274a4a4 | 2004-12-07 15:39:31 +0000 | [diff] [blame] | 3864 | "Reset syslog facility to default (daemon)\n" |
| 3865 | "Syslog facility\n") |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 3866 | { |
ajs | 274a4a4 | 2004-12-07 15:39:31 +0000 | [diff] [blame] | 3867 | zlog_default->facility = LOG_DAEMON; |
| 3868 | return CMD_SUCCESS; |
| 3869 | } |
| 3870 | |
| 3871 | DEFUN_DEPRECATED (config_log_trap, |
| 3872 | config_log_trap_cmd, |
| 3873 | "log trap "LOG_LEVELS, |
| 3874 | "Logging control\n" |
| 3875 | "(Deprecated) Set logging level and default for all destinations\n" |
| 3876 | LOG_LEVEL_DESC) |
| 3877 | { |
| 3878 | int new_level ; |
| 3879 | int i; |
| 3880 | |
| 3881 | if ((new_level = level_match(argv[0])) == ZLOG_DISABLED) |
| 3882 | return CMD_ERR_NO_MATCH; |
| 3883 | |
| 3884 | zlog_default->default_lvl = new_level; |
| 3885 | for (i = 0; i < ZLOG_NUM_DESTS; i++) |
| 3886 | if (zlog_default->maxlvl[i] != ZLOG_DISABLED) |
| 3887 | zlog_default->maxlvl[i] = new_level; |
| 3888 | return CMD_SUCCESS; |
| 3889 | } |
| 3890 | |
| 3891 | DEFUN_DEPRECATED (no_config_log_trap, |
| 3892 | no_config_log_trap_cmd, |
| 3893 | "no log trap [LEVEL]", |
| 3894 | NO_STR |
| 3895 | "Logging control\n" |
| 3896 | "Permit all logging information\n" |
| 3897 | "Logging level\n") |
| 3898 | { |
| 3899 | zlog_default->default_lvl = LOG_DEBUG; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 3900 | return CMD_SUCCESS; |
| 3901 | } |
| 3902 | |
| 3903 | DEFUN (config_log_record_priority, |
| 3904 | config_log_record_priority_cmd, |
| 3905 | "log record-priority", |
| 3906 | "Logging control\n" |
| 3907 | "Log the priority of the message within the message\n") |
| 3908 | { |
| 3909 | zlog_default->record_priority = 1 ; |
| 3910 | return CMD_SUCCESS; |
| 3911 | } |
| 3912 | |
| 3913 | DEFUN (no_config_log_record_priority, |
| 3914 | no_config_log_record_priority_cmd, |
| 3915 | "no log record-priority", |
| 3916 | NO_STR |
| 3917 | "Logging control\n" |
| 3918 | "Do not log the priority of the message within the message\n") |
| 3919 | { |
| 3920 | zlog_default->record_priority = 0 ; |
| 3921 | return CMD_SUCCESS; |
| 3922 | } |
| 3923 | |
Andrew J. Schorr | 1ed72e0 | 2007-04-28 22:14:10 +0000 | [diff] [blame] | 3924 | DEFUN (config_log_timestamp_precision, |
| 3925 | config_log_timestamp_precision_cmd, |
| 3926 | "log timestamp precision <0-6>", |
| 3927 | "Logging control\n" |
| 3928 | "Timestamp configuration\n" |
| 3929 | "Set the timestamp precision\n" |
| 3930 | "Number of subsecond digits\n") |
| 3931 | { |
| 3932 | if (argc != 1) |
| 3933 | { |
| 3934 | vty_out (vty, "Insufficient arguments%s", VTY_NEWLINE); |
| 3935 | return CMD_WARNING; |
| 3936 | } |
| 3937 | |
| 3938 | VTY_GET_INTEGER_RANGE("Timestamp Precision", |
| 3939 | zlog_default->timestamp_precision, argv[0], 0, 6); |
| 3940 | return CMD_SUCCESS; |
| 3941 | } |
| 3942 | |
| 3943 | DEFUN (no_config_log_timestamp_precision, |
| 3944 | no_config_log_timestamp_precision_cmd, |
| 3945 | "no log timestamp precision", |
| 3946 | NO_STR |
| 3947 | "Logging control\n" |
| 3948 | "Timestamp configuration\n" |
| 3949 | "Reset the timestamp precision to the default value of 0\n") |
| 3950 | { |
| 3951 | zlog_default->timestamp_precision = 0 ; |
| 3952 | return CMD_SUCCESS; |
| 3953 | } |
| 3954 | |
paul | 3b0c5d9 | 2005-03-08 10:43:43 +0000 | [diff] [blame] | 3955 | DEFUN (banner_motd_file, |
| 3956 | banner_motd_file_cmd, |
| 3957 | "banner motd file [FILE]", |
| 3958 | "Set banner\n" |
| 3959 | "Banner for motd\n" |
| 3960 | "Banner from a file\n" |
| 3961 | "Filename\n") |
| 3962 | { |
paul | b45da6f | 2005-03-08 15:16:57 +0000 | [diff] [blame] | 3963 | if (host.motdfile) |
paul | 05865c9 | 2005-10-26 05:49:54 +0000 | [diff] [blame] | 3964 | XFREE (MTYPE_HOST, host.motdfile); |
| 3965 | host.motdfile = XSTRDUP (MTYPE_HOST, argv[0]); |
paul | b45da6f | 2005-03-08 15:16:57 +0000 | [diff] [blame] | 3966 | |
paul | 3b0c5d9 | 2005-03-08 10:43:43 +0000 | [diff] [blame] | 3967 | return CMD_SUCCESS; |
| 3968 | } |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 3969 | |
| 3970 | DEFUN (banner_motd_default, |
| 3971 | banner_motd_default_cmd, |
| 3972 | "banner motd default", |
| 3973 | "Set banner string\n" |
| 3974 | "Strings for motd\n" |
| 3975 | "Default string\n") |
| 3976 | { |
| 3977 | host.motd = default_motd; |
| 3978 | return CMD_SUCCESS; |
| 3979 | } |
| 3980 | |
| 3981 | DEFUN (no_banner_motd, |
| 3982 | no_banner_motd_cmd, |
| 3983 | "no banner motd", |
| 3984 | NO_STR |
| 3985 | "Set banner string\n" |
| 3986 | "Strings for motd\n") |
| 3987 | { |
| 3988 | host.motd = NULL; |
paul | 2208518 | 2005-03-08 16:00:12 +0000 | [diff] [blame] | 3989 | if (host.motdfile) |
paul | 05865c9 | 2005-10-26 05:49:54 +0000 | [diff] [blame] | 3990 | XFREE (MTYPE_HOST, host.motdfile); |
paul | 3b0c5d9 | 2005-03-08 10:43:43 +0000 | [diff] [blame] | 3991 | host.motdfile = NULL; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 3992 | return CMD_SUCCESS; |
| 3993 | } |
| 3994 | |
| 3995 | /* Set config filename. Called from vty.c */ |
| 3996 | void |
| 3997 | host_config_set (char *filename) |
| 3998 | { |
Chris Caputo | 228da42 | 2009-07-18 05:44:03 +0000 | [diff] [blame] | 3999 | if (host.config) |
| 4000 | XFREE (MTYPE_HOST, host.config); |
paul | 05865c9 | 2005-10-26 05:49:54 +0000 | [diff] [blame] | 4001 | host.config = XSTRDUP (MTYPE_HOST, filename); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 4002 | } |
| 4003 | |
| 4004 | void |
| 4005 | install_default (enum node_type node) |
| 4006 | { |
| 4007 | install_element (node, &config_exit_cmd); |
| 4008 | install_element (node, &config_quit_cmd); |
| 4009 | install_element (node, &config_end_cmd); |
| 4010 | install_element (node, &config_help_cmd); |
| 4011 | install_element (node, &config_list_cmd); |
| 4012 | |
| 4013 | install_element (node, &config_write_terminal_cmd); |
| 4014 | install_element (node, &config_write_file_cmd); |
| 4015 | install_element (node, &config_write_memory_cmd); |
| 4016 | install_element (node, &config_write_cmd); |
| 4017 | install_element (node, &show_running_config_cmd); |
| 4018 | } |
| 4019 | |
| 4020 | /* Initialize command interface. Install basic nodes and commands. */ |
| 4021 | void |
| 4022 | cmd_init (int terminal) |
| 4023 | { |
Christian Franke | cd40b32 | 2013-09-30 12:27:51 +0000 | [diff] [blame] | 4024 | command_cr = XSTRDUP(MTYPE_CMD_TOKENS, "<cr>"); |
| 4025 | token_cr.type = TOKEN_TERMINAL; |
David Lamparter | 10bac80 | 2015-05-05 11:10:20 +0200 | [diff] [blame] | 4026 | token_cr.terminal = TERMINAL_LITERAL; |
Christian Franke | cd40b32 | 2013-09-30 12:27:51 +0000 | [diff] [blame] | 4027 | token_cr.cmd = command_cr; |
| 4028 | token_cr.desc = XSTRDUP(MTYPE_CMD_TOKENS, ""); |
Chris Caputo | 228da42 | 2009-07-18 05:44:03 +0000 | [diff] [blame] | 4029 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 4030 | /* Allocate initial top vector of commands. */ |
| 4031 | cmdvec = vector_init (VECTOR_MIN_SIZE); |
| 4032 | |
| 4033 | /* Default host value settings. */ |
| 4034 | host.name = NULL; |
| 4035 | host.password = NULL; |
| 4036 | host.enable = NULL; |
| 4037 | host.logfile = NULL; |
| 4038 | host.config = NULL; |
| 4039 | host.lines = -1; |
| 4040 | host.motd = default_motd; |
paul | 3b0c5d9 | 2005-03-08 10:43:43 +0000 | [diff] [blame] | 4041 | host.motdfile = NULL; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 4042 | |
| 4043 | /* Install top nodes. */ |
| 4044 | install_node (&view_node, NULL); |
| 4045 | install_node (&enable_node, NULL); |
| 4046 | install_node (&auth_node, NULL); |
| 4047 | install_node (&auth_enable_node, NULL); |
Paul Jakma | 62687ff | 2008-08-23 14:27:06 +0100 | [diff] [blame] | 4048 | install_node (&restricted_node, NULL); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 4049 | install_node (&config_node, config_write_host); |
| 4050 | |
| 4051 | /* Each node's basic commands. */ |
| 4052 | install_element (VIEW_NODE, &show_version_cmd); |
| 4053 | if (terminal) |
| 4054 | { |
| 4055 | install_element (VIEW_NODE, &config_list_cmd); |
| 4056 | install_element (VIEW_NODE, &config_exit_cmd); |
| 4057 | install_element (VIEW_NODE, &config_quit_cmd); |
| 4058 | install_element (VIEW_NODE, &config_help_cmd); |
| 4059 | install_element (VIEW_NODE, &config_enable_cmd); |
| 4060 | install_element (VIEW_NODE, &config_terminal_length_cmd); |
| 4061 | install_element (VIEW_NODE, &config_terminal_no_length_cmd); |
ajs | 274a4a4 | 2004-12-07 15:39:31 +0000 | [diff] [blame] | 4062 | install_element (VIEW_NODE, &show_logging_cmd); |
ajs | 2885f72 | 2004-12-17 23:16:33 +0000 | [diff] [blame] | 4063 | install_element (VIEW_NODE, &echo_cmd); |
Paul Jakma | 62687ff | 2008-08-23 14:27:06 +0100 | [diff] [blame] | 4064 | |
| 4065 | install_element (RESTRICTED_NODE, &config_list_cmd); |
| 4066 | install_element (RESTRICTED_NODE, &config_exit_cmd); |
| 4067 | install_element (RESTRICTED_NODE, &config_quit_cmd); |
| 4068 | install_element (RESTRICTED_NODE, &config_help_cmd); |
| 4069 | install_element (RESTRICTED_NODE, &config_enable_cmd); |
| 4070 | install_element (RESTRICTED_NODE, &config_terminal_length_cmd); |
| 4071 | install_element (RESTRICTED_NODE, &config_terminal_no_length_cmd); |
| 4072 | install_element (RESTRICTED_NODE, &echo_cmd); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 4073 | } |
| 4074 | |
| 4075 | if (terminal) |
| 4076 | { |
| 4077 | install_default (ENABLE_NODE); |
| 4078 | install_element (ENABLE_NODE, &config_disable_cmd); |
| 4079 | install_element (ENABLE_NODE, &config_terminal_cmd); |
| 4080 | install_element (ENABLE_NODE, ©_runningconfig_startupconfig_cmd); |
| 4081 | } |
| 4082 | install_element (ENABLE_NODE, &show_startup_config_cmd); |
| 4083 | install_element (ENABLE_NODE, &show_version_cmd); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 4084 | |
| 4085 | if (terminal) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 4086 | { |
hasso | e7168df | 2004-10-03 20:11:32 +0000 | [diff] [blame] | 4087 | install_element (ENABLE_NODE, &config_terminal_length_cmd); |
| 4088 | install_element (ENABLE_NODE, &config_terminal_no_length_cmd); |
ajs | 274a4a4 | 2004-12-07 15:39:31 +0000 | [diff] [blame] | 4089 | install_element (ENABLE_NODE, &show_logging_cmd); |
ajs | 2885f72 | 2004-12-17 23:16:33 +0000 | [diff] [blame] | 4090 | install_element (ENABLE_NODE, &echo_cmd); |
ajs | 274a4a4 | 2004-12-07 15:39:31 +0000 | [diff] [blame] | 4091 | install_element (ENABLE_NODE, &config_logmsg_cmd); |
hasso | e7168df | 2004-10-03 20:11:32 +0000 | [diff] [blame] | 4092 | |
| 4093 | install_default (CONFIG_NODE); |
hasso | ea8e9d9 | 2004-10-07 21:32:14 +0000 | [diff] [blame] | 4094 | } |
| 4095 | |
| 4096 | install_element (CONFIG_NODE, &hostname_cmd); |
| 4097 | install_element (CONFIG_NODE, &no_hostname_cmd); |
hasso | e7168df | 2004-10-03 20:11:32 +0000 | [diff] [blame] | 4098 | |
hasso | ea8e9d9 | 2004-10-07 21:32:14 +0000 | [diff] [blame] | 4099 | if (terminal) |
| 4100 | { |
hasso | e7168df | 2004-10-03 20:11:32 +0000 | [diff] [blame] | 4101 | install_element (CONFIG_NODE, &password_cmd); |
| 4102 | install_element (CONFIG_NODE, &password_text_cmd); |
| 4103 | install_element (CONFIG_NODE, &enable_password_cmd); |
| 4104 | install_element (CONFIG_NODE, &enable_password_text_cmd); |
| 4105 | install_element (CONFIG_NODE, &no_enable_password_cmd); |
| 4106 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 4107 | install_element (CONFIG_NODE, &config_log_stdout_cmd); |
ajs | 274a4a4 | 2004-12-07 15:39:31 +0000 | [diff] [blame] | 4108 | install_element (CONFIG_NODE, &config_log_stdout_level_cmd); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 4109 | install_element (CONFIG_NODE, &no_config_log_stdout_cmd); |
ajs | 274a4a4 | 2004-12-07 15:39:31 +0000 | [diff] [blame] | 4110 | install_element (CONFIG_NODE, &config_log_monitor_cmd); |
| 4111 | install_element (CONFIG_NODE, &config_log_monitor_level_cmd); |
| 4112 | install_element (CONFIG_NODE, &no_config_log_monitor_cmd); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 4113 | install_element (CONFIG_NODE, &config_log_file_cmd); |
ajs | 274a4a4 | 2004-12-07 15:39:31 +0000 | [diff] [blame] | 4114 | install_element (CONFIG_NODE, &config_log_file_level_cmd); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 4115 | install_element (CONFIG_NODE, &no_config_log_file_cmd); |
ajs | 274a4a4 | 2004-12-07 15:39:31 +0000 | [diff] [blame] | 4116 | install_element (CONFIG_NODE, &no_config_log_file_level_cmd); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 4117 | install_element (CONFIG_NODE, &config_log_syslog_cmd); |
ajs | 274a4a4 | 2004-12-07 15:39:31 +0000 | [diff] [blame] | 4118 | install_element (CONFIG_NODE, &config_log_syslog_level_cmd); |
paul | 12ab19f | 2003-07-26 06:14:55 +0000 | [diff] [blame] | 4119 | install_element (CONFIG_NODE, &config_log_syslog_facility_cmd); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 4120 | install_element (CONFIG_NODE, &no_config_log_syslog_cmd); |
paul | 12ab19f | 2003-07-26 06:14:55 +0000 | [diff] [blame] | 4121 | install_element (CONFIG_NODE, &no_config_log_syslog_facility_cmd); |
ajs | 274a4a4 | 2004-12-07 15:39:31 +0000 | [diff] [blame] | 4122 | install_element (CONFIG_NODE, &config_log_facility_cmd); |
| 4123 | install_element (CONFIG_NODE, &no_config_log_facility_cmd); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 4124 | install_element (CONFIG_NODE, &config_log_trap_cmd); |
| 4125 | install_element (CONFIG_NODE, &no_config_log_trap_cmd); |
| 4126 | install_element (CONFIG_NODE, &config_log_record_priority_cmd); |
| 4127 | install_element (CONFIG_NODE, &no_config_log_record_priority_cmd); |
Andrew J. Schorr | 1ed72e0 | 2007-04-28 22:14:10 +0000 | [diff] [blame] | 4128 | install_element (CONFIG_NODE, &config_log_timestamp_precision_cmd); |
| 4129 | install_element (CONFIG_NODE, &no_config_log_timestamp_precision_cmd); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 4130 | install_element (CONFIG_NODE, &service_password_encrypt_cmd); |
| 4131 | install_element (CONFIG_NODE, &no_service_password_encrypt_cmd); |
| 4132 | install_element (CONFIG_NODE, &banner_motd_default_cmd); |
paul | 3b0c5d9 | 2005-03-08 10:43:43 +0000 | [diff] [blame] | 4133 | install_element (CONFIG_NODE, &banner_motd_file_cmd); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 4134 | install_element (CONFIG_NODE, &no_banner_motd_cmd); |
| 4135 | install_element (CONFIG_NODE, &service_terminal_length_cmd); |
| 4136 | install_element (CONFIG_NODE, &no_service_terminal_length_cmd); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 4137 | |
paul | 354d119 | 2005-04-25 16:26:42 +0000 | [diff] [blame] | 4138 | install_element (VIEW_NODE, &show_thread_cpu_cmd); |
| 4139 | install_element (ENABLE_NODE, &show_thread_cpu_cmd); |
Paul Jakma | 62687ff | 2008-08-23 14:27:06 +0100 | [diff] [blame] | 4140 | install_element (RESTRICTED_NODE, &show_thread_cpu_cmd); |
Paul Jakma | e276eb8 | 2010-01-09 16:15:00 +0000 | [diff] [blame] | 4141 | |
| 4142 | install_element (ENABLE_NODE, &clear_thread_cpu_cmd); |
paul | 354d119 | 2005-04-25 16:26:42 +0000 | [diff] [blame] | 4143 | install_element (VIEW_NODE, &show_work_queues_cmd); |
| 4144 | install_element (ENABLE_NODE, &show_work_queues_cmd); |
paul | 9ab6812 | 2003-01-18 01:16:20 +0000 | [diff] [blame] | 4145 | } |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 4146 | srand(time(NULL)); |
| 4147 | } |
Chris Caputo | 228da42 | 2009-07-18 05:44:03 +0000 | [diff] [blame] | 4148 | |
Christian Franke | cd40b32 | 2013-09-30 12:27:51 +0000 | [diff] [blame] | 4149 | static void |
| 4150 | cmd_terminate_token(struct cmd_token *token) |
| 4151 | { |
| 4152 | unsigned int i, j; |
| 4153 | vector keyword_vect; |
| 4154 | |
| 4155 | if (token->multiple) |
| 4156 | { |
| 4157 | for (i = 0; i < vector_active(token->multiple); i++) |
| 4158 | cmd_terminate_token(vector_slot(token->multiple, i)); |
| 4159 | vector_free(token->multiple); |
| 4160 | token->multiple = NULL; |
| 4161 | } |
| 4162 | |
| 4163 | if (token->keyword) |
| 4164 | { |
| 4165 | for (i = 0; i < vector_active(token->keyword); i++) |
| 4166 | { |
| 4167 | keyword_vect = vector_slot(token->keyword, i); |
| 4168 | for (j = 0; j < vector_active(keyword_vect); j++) |
| 4169 | cmd_terminate_token(vector_slot(keyword_vect, j)); |
| 4170 | vector_free(keyword_vect); |
| 4171 | } |
| 4172 | vector_free(token->keyword); |
| 4173 | token->keyword = NULL; |
| 4174 | } |
| 4175 | |
| 4176 | XFREE(MTYPE_CMD_TOKENS, token->cmd); |
| 4177 | XFREE(MTYPE_CMD_TOKENS, token->desc); |
| 4178 | |
| 4179 | XFREE(MTYPE_CMD_TOKENS, token); |
| 4180 | } |
| 4181 | |
| 4182 | static void |
| 4183 | cmd_terminate_element(struct cmd_element *cmd) |
| 4184 | { |
| 4185 | unsigned int i; |
| 4186 | |
| 4187 | if (cmd->tokens == NULL) |
| 4188 | return; |
| 4189 | |
| 4190 | for (i = 0; i < vector_active(cmd->tokens); i++) |
| 4191 | cmd_terminate_token(vector_slot(cmd->tokens, i)); |
| 4192 | |
| 4193 | vector_free(cmd->tokens); |
| 4194 | cmd->tokens = NULL; |
| 4195 | } |
| 4196 | |
Chris Caputo | 228da42 | 2009-07-18 05:44:03 +0000 | [diff] [blame] | 4197 | void |
| 4198 | cmd_terminate () |
| 4199 | { |
Christian Franke | cd40b32 | 2013-09-30 12:27:51 +0000 | [diff] [blame] | 4200 | unsigned int i, j; |
Chris Caputo | 228da42 | 2009-07-18 05:44:03 +0000 | [diff] [blame] | 4201 | struct cmd_node *cmd_node; |
| 4202 | struct cmd_element *cmd_element; |
Christian Franke | cd40b32 | 2013-09-30 12:27:51 +0000 | [diff] [blame] | 4203 | vector cmd_node_v; |
Chris Caputo | 228da42 | 2009-07-18 05:44:03 +0000 | [diff] [blame] | 4204 | |
| 4205 | if (cmdvec) |
| 4206 | { |
| 4207 | for (i = 0; i < vector_active (cmdvec); i++) |
| 4208 | if ((cmd_node = vector_slot (cmdvec, i)) != NULL) |
| 4209 | { |
| 4210 | cmd_node_v = cmd_node->cmd_vector; |
| 4211 | |
| 4212 | for (j = 0; j < vector_active (cmd_node_v); j++) |
Christian Franke | cd40b32 | 2013-09-30 12:27:51 +0000 | [diff] [blame] | 4213 | if ((cmd_element = vector_slot (cmd_node_v, j)) != NULL) |
| 4214 | cmd_terminate_element(cmd_element); |
Chris Caputo | 228da42 | 2009-07-18 05:44:03 +0000 | [diff] [blame] | 4215 | |
| 4216 | vector_free (cmd_node_v); |
| 4217 | } |
| 4218 | |
| 4219 | vector_free (cmdvec); |
| 4220 | cmdvec = NULL; |
| 4221 | } |
| 4222 | |
| 4223 | if (command_cr) |
Christian Franke | cd40b32 | 2013-09-30 12:27:51 +0000 | [diff] [blame] | 4224 | XFREE(MTYPE_CMD_TOKENS, command_cr); |
| 4225 | if (token_cr.desc) |
| 4226 | XFREE(MTYPE_CMD_TOKENS, token_cr.desc); |
Chris Caputo | 228da42 | 2009-07-18 05:44:03 +0000 | [diff] [blame] | 4227 | if (host.name) |
| 4228 | XFREE (MTYPE_HOST, host.name); |
| 4229 | if (host.password) |
| 4230 | XFREE (MTYPE_HOST, host.password); |
| 4231 | if (host.password_encrypt) |
| 4232 | XFREE (MTYPE_HOST, host.password_encrypt); |
| 4233 | if (host.enable) |
| 4234 | XFREE (MTYPE_HOST, host.enable); |
| 4235 | if (host.enable_encrypt) |
| 4236 | XFREE (MTYPE_HOST, host.enable_encrypt); |
| 4237 | if (host.logfile) |
| 4238 | XFREE (MTYPE_HOST, host.logfile); |
| 4239 | if (host.motdfile) |
| 4240 | XFREE (MTYPE_HOST, host.motdfile); |
| 4241 | if (host.config) |
| 4242 | XFREE (MTYPE_HOST, host.config); |
| 4243 | } |