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