ajs | 274a4a4 | 2004-12-07 15:39:31 +0000 | [diff] [blame] | 1 | /* |
paul | 55468c8 | 2005-03-14 20:19:01 +0000 | [diff] [blame] | 2 | $Id: command.c,v 1.46 2005/03/14 20:19:01 paul Exp $ |
paul | 9e92eea | 2005-03-09 13:39:26 +0000 | [diff] [blame] | 3 | |
ajs | 274a4a4 | 2004-12-07 15:39:31 +0000 | [diff] [blame] | 4 | Command interpreter routine for virtual terminal [aka TeletYpe] |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 5 | Copyright (C) 1997, 98, 99 Kunihiro Ishiguro |
| 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 | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 34 | |
| 35 | /* Command vector which includes some level of command lists. Normally |
| 36 | each daemon maintains each own cmdvec. */ |
| 37 | vector cmdvec; |
| 38 | |
| 39 | /* Host information structure. */ |
| 40 | struct host host; |
| 41 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 42 | /* Standard command node structures. */ |
| 43 | struct cmd_node auth_node = |
| 44 | { |
| 45 | AUTH_NODE, |
| 46 | "Password: ", |
| 47 | }; |
| 48 | |
| 49 | struct cmd_node view_node = |
| 50 | { |
| 51 | VIEW_NODE, |
| 52 | "%s> ", |
| 53 | }; |
| 54 | |
| 55 | struct cmd_node auth_enable_node = |
| 56 | { |
| 57 | AUTH_ENABLE_NODE, |
| 58 | "Password: ", |
| 59 | }; |
| 60 | |
| 61 | struct cmd_node enable_node = |
| 62 | { |
| 63 | ENABLE_NODE, |
| 64 | "%s# ", |
| 65 | }; |
| 66 | |
| 67 | struct cmd_node config_node = |
| 68 | { |
| 69 | CONFIG_NODE, |
| 70 | "%s(config)# ", |
| 71 | 1 |
| 72 | }; |
hasso | 6590f2c | 2004-10-19 20:40:08 +0000 | [diff] [blame] | 73 | |
| 74 | /* Default motd string. */ |
| 75 | const char *default_motd = |
| 76 | "\r\n\ |
| 77 | Hello, this is " QUAGGA_PROGNAME " (version " QUAGGA_VERSION ").\r\n\ |
| 78 | " QUAGGA_COPYRIGHT "\r\n\ |
| 79 | \r\n"; |
| 80 | |
ajs | 274a4a4 | 2004-12-07 15:39:31 +0000 | [diff] [blame] | 81 | |
| 82 | static struct facility_map { |
| 83 | int facility; |
| 84 | const char *name; |
| 85 | size_t match; |
| 86 | } syslog_facilities[] = |
| 87 | { |
| 88 | { LOG_KERN, "kern", 1 }, |
| 89 | { LOG_USER, "user", 2 }, |
| 90 | { LOG_MAIL, "mail", 1 }, |
| 91 | { LOG_DAEMON, "daemon", 1 }, |
| 92 | { LOG_AUTH, "auth", 1 }, |
| 93 | { LOG_SYSLOG, "syslog", 1 }, |
| 94 | { LOG_LPR, "lpr", 2 }, |
| 95 | { LOG_NEWS, "news", 1 }, |
| 96 | { LOG_UUCP, "uucp", 2 }, |
| 97 | { LOG_CRON, "cron", 1 }, |
| 98 | #ifdef LOG_FTP |
| 99 | { LOG_FTP, "ftp", 1 }, |
| 100 | #endif |
| 101 | { LOG_LOCAL0, "local0", 6 }, |
| 102 | { LOG_LOCAL1, "local1", 6 }, |
| 103 | { LOG_LOCAL2, "local2", 6 }, |
| 104 | { LOG_LOCAL3, "local3", 6 }, |
| 105 | { LOG_LOCAL4, "local4", 6 }, |
| 106 | { LOG_LOCAL5, "local5", 6 }, |
| 107 | { LOG_LOCAL6, "local6", 6 }, |
| 108 | { LOG_LOCAL7, "local7", 6 }, |
| 109 | { 0, NULL, 0 }, |
| 110 | }; |
| 111 | |
| 112 | static const char * |
| 113 | facility_name(int facility) |
| 114 | { |
| 115 | struct facility_map *fm; |
| 116 | |
| 117 | for (fm = syslog_facilities; fm->name; fm++) |
| 118 | if (fm->facility == facility) |
| 119 | return fm->name; |
| 120 | return ""; |
| 121 | } |
| 122 | |
| 123 | static int |
| 124 | facility_match(const char *str) |
| 125 | { |
| 126 | struct facility_map *fm; |
| 127 | |
| 128 | for (fm = syslog_facilities; fm->name; fm++) |
| 129 | if (!strncmp(str,fm->name,fm->match)) |
| 130 | return fm->facility; |
| 131 | return -1; |
| 132 | } |
| 133 | |
| 134 | static int |
| 135 | level_match(const char *s) |
| 136 | { |
| 137 | int level ; |
| 138 | |
| 139 | for ( level = 0 ; zlog_priority [level] != NULL ; level ++ ) |
| 140 | if (!strncmp (s, zlog_priority[level], 2)) |
| 141 | return level; |
| 142 | return ZLOG_DISABLED; |
| 143 | } |
| 144 | |
ajs | cb585b6 | 2005-01-14 17:09:38 +0000 | [diff] [blame] | 145 | /* 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] | 146 | void |
| 147 | print_version (const char *progname) |
| 148 | { |
ajs | cb585b6 | 2005-01-14 17:09:38 +0000 | [diff] [blame] | 149 | printf ("%s version %s\n", progname, QUAGGA_VERSION); |
| 150 | printf ("%s\n", QUAGGA_COPYRIGHT); |
hasso | 6590f2c | 2004-10-19 20:40:08 +0000 | [diff] [blame] | 151 | } |
| 152 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 153 | |
| 154 | /* Utility function to concatenate argv argument into a single string |
| 155 | with inserting ' ' character between each argument. */ |
| 156 | char * |
paul | 42d4986 | 2004-10-13 05:22:18 +0000 | [diff] [blame] | 157 | argv_concat (const char **argv, int argc, int shift) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 158 | { |
| 159 | int i; |
ajs | f6834d4 | 2005-01-28 20:28:35 +0000 | [diff] [blame] | 160 | size_t len; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 161 | char *str; |
ajs | f6834d4 | 2005-01-28 20:28:35 +0000 | [diff] [blame] | 162 | char *p; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 163 | |
ajs | f6834d4 | 2005-01-28 20:28:35 +0000 | [diff] [blame] | 164 | len = 0; |
| 165 | for (i = shift; i < argc; i++) |
| 166 | len += strlen(argv[i])+1; |
| 167 | if (!len) |
| 168 | return NULL; |
| 169 | p = str = XMALLOC(MTYPE_TMP, len); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 170 | for (i = shift; i < argc; i++) |
| 171 | { |
ajs | f6834d4 | 2005-01-28 20:28:35 +0000 | [diff] [blame] | 172 | size_t arglen; |
| 173 | memcpy(p, argv[i], (arglen = strlen(argv[i]))); |
| 174 | p += arglen; |
| 175 | *p++ = ' '; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 176 | } |
ajs | f6834d4 | 2005-01-28 20:28:35 +0000 | [diff] [blame] | 177 | *(p-1) = '\0'; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 178 | return str; |
| 179 | } |
| 180 | |
| 181 | /* Install top node of command vector. */ |
| 182 | void |
| 183 | install_node (struct cmd_node *node, |
| 184 | int (*func) (struct vty *)) |
| 185 | { |
| 186 | vector_set_index (cmdvec, node->node, node); |
| 187 | node->func = func; |
| 188 | node->cmd_vector = vector_init (VECTOR_MIN_SIZE); |
| 189 | } |
| 190 | |
| 191 | /* Compare two command's string. Used in sort_node (). */ |
ajs | 274a4a4 | 2004-12-07 15:39:31 +0000 | [diff] [blame] | 192 | static int |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 193 | cmp_node (const void *p, const void *q) |
| 194 | { |
| 195 | struct cmd_element *a = *(struct cmd_element **)p; |
| 196 | struct cmd_element *b = *(struct cmd_element **)q; |
| 197 | |
| 198 | return strcmp (a->string, b->string); |
| 199 | } |
| 200 | |
ajs | 274a4a4 | 2004-12-07 15:39:31 +0000 | [diff] [blame] | 201 | static int |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 202 | cmp_desc (const void *p, const void *q) |
| 203 | { |
| 204 | struct desc *a = *(struct desc **)p; |
| 205 | struct desc *b = *(struct desc **)q; |
| 206 | |
| 207 | return strcmp (a->cmd, b->cmd); |
| 208 | } |
| 209 | |
| 210 | /* Sort each node's command element according to command string. */ |
| 211 | void |
| 212 | sort_node () |
| 213 | { |
hasso | 8c328f1 | 2004-10-05 21:01:23 +0000 | [diff] [blame] | 214 | unsigned int i, j; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 215 | struct cmd_node *cnode; |
| 216 | vector descvec; |
| 217 | struct cmd_element *cmd_element; |
| 218 | |
paul | 55468c8 | 2005-03-14 20:19:01 +0000 | [diff] [blame] | 219 | for (i = 0; i < vector_active (cmdvec); i++) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 220 | if ((cnode = vector_slot (cmdvec, i)) != NULL) |
| 221 | { |
| 222 | vector cmd_vector = cnode->cmd_vector; |
paul | 55468c8 | 2005-03-14 20:19:01 +0000 | [diff] [blame] | 223 | qsort (cmd_vector->index, vector_active (cmd_vector), |
paul | b896147 | 2005-03-14 17:35:52 +0000 | [diff] [blame] | 224 | sizeof (void *), cmp_node); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 225 | |
paul | 55468c8 | 2005-03-14 20:19:01 +0000 | [diff] [blame] | 226 | for (j = 0; j < vector_active (cmd_vector); j++) |
paul | b896147 | 2005-03-14 17:35:52 +0000 | [diff] [blame] | 227 | if ((cmd_element = vector_slot (cmd_vector, j)) != NULL |
paul | 55468c8 | 2005-03-14 20:19:01 +0000 | [diff] [blame] | 228 | && vector_active (cmd_element->strvec)) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 229 | { |
| 230 | descvec = vector_slot (cmd_element->strvec, |
paul | 55468c8 | 2005-03-14 20:19:01 +0000 | [diff] [blame] | 231 | vector_active (cmd_element->strvec) - 1); |
| 232 | qsort (descvec->index, vector_active (descvec), |
paul | b896147 | 2005-03-14 17:35:52 +0000 | [diff] [blame] | 233 | sizeof (void *), cmp_desc); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 234 | } |
| 235 | } |
| 236 | } |
| 237 | |
| 238 | /* Breaking up string into each command piece. I assume given |
| 239 | character is separated by a space character. Return value is a |
| 240 | vector which includes char ** data element. */ |
| 241 | vector |
hasso | ea8e9d9 | 2004-10-07 21:32:14 +0000 | [diff] [blame] | 242 | cmd_make_strvec (const char *string) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 243 | { |
hasso | ea8e9d9 | 2004-10-07 21:32:14 +0000 | [diff] [blame] | 244 | const char *cp, *start; |
| 245 | char *token; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 246 | int strlen; |
| 247 | vector strvec; |
| 248 | |
| 249 | if (string == NULL) |
| 250 | return NULL; |
| 251 | |
| 252 | cp = string; |
| 253 | |
| 254 | /* Skip white spaces. */ |
| 255 | while (isspace ((int) *cp) && *cp != '\0') |
| 256 | cp++; |
| 257 | |
| 258 | /* Return if there is only white spaces */ |
| 259 | if (*cp == '\0') |
| 260 | return NULL; |
| 261 | |
| 262 | if (*cp == '!' || *cp == '#') |
| 263 | return NULL; |
| 264 | |
| 265 | /* Prepare return vector. */ |
| 266 | strvec = vector_init (VECTOR_MIN_SIZE); |
| 267 | |
| 268 | /* Copy each command piece and set into vector. */ |
| 269 | while (1) |
| 270 | { |
| 271 | start = cp; |
| 272 | while (!(isspace ((int) *cp) || *cp == '\r' || *cp == '\n') && |
| 273 | *cp != '\0') |
| 274 | cp++; |
| 275 | strlen = cp - start; |
| 276 | token = XMALLOC (MTYPE_STRVEC, strlen + 1); |
| 277 | memcpy (token, start, strlen); |
| 278 | *(token + strlen) = '\0'; |
| 279 | vector_set (strvec, token); |
| 280 | |
| 281 | while ((isspace ((int) *cp) || *cp == '\n' || *cp == '\r') && |
| 282 | *cp != '\0') |
| 283 | cp++; |
| 284 | |
| 285 | if (*cp == '\0') |
| 286 | return strvec; |
| 287 | } |
| 288 | } |
| 289 | |
| 290 | /* Free allocated string vector. */ |
| 291 | void |
| 292 | cmd_free_strvec (vector v) |
| 293 | { |
hasso | 8c328f1 | 2004-10-05 21:01:23 +0000 | [diff] [blame] | 294 | unsigned int i; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 295 | char *cp; |
| 296 | |
| 297 | if (!v) |
| 298 | return; |
| 299 | |
paul | 55468c8 | 2005-03-14 20:19:01 +0000 | [diff] [blame] | 300 | for (i = 0; i < vector_active (v); i++) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 301 | if ((cp = vector_slot (v, i)) != NULL) |
| 302 | XFREE (MTYPE_STRVEC, cp); |
| 303 | |
| 304 | vector_free (v); |
| 305 | } |
| 306 | |
| 307 | /* Fetch next description. Used in cmd_make_descvec(). */ |
ajs | 274a4a4 | 2004-12-07 15:39:31 +0000 | [diff] [blame] | 308 | static char * |
hasso | 6ad96ea | 2004-10-07 19:33:46 +0000 | [diff] [blame] | 309 | cmd_desc_str (const char **string) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 310 | { |
hasso | 6ad96ea | 2004-10-07 19:33:46 +0000 | [diff] [blame] | 311 | const char *cp, *start; |
| 312 | char *token; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 313 | int strlen; |
| 314 | |
| 315 | cp = *string; |
| 316 | |
| 317 | if (cp == NULL) |
| 318 | return NULL; |
| 319 | |
| 320 | /* Skip white spaces. */ |
| 321 | while (isspace ((int) *cp) && *cp != '\0') |
| 322 | cp++; |
| 323 | |
| 324 | /* Return if there is only white spaces */ |
| 325 | if (*cp == '\0') |
| 326 | return NULL; |
| 327 | |
| 328 | start = cp; |
| 329 | |
| 330 | while (!(*cp == '\r' || *cp == '\n') && *cp != '\0') |
| 331 | cp++; |
| 332 | |
| 333 | strlen = cp - start; |
| 334 | token = XMALLOC (MTYPE_STRVEC, strlen + 1); |
| 335 | memcpy (token, start, strlen); |
| 336 | *(token + strlen) = '\0'; |
| 337 | |
| 338 | *string = cp; |
| 339 | |
| 340 | return token; |
| 341 | } |
| 342 | |
| 343 | /* New string vector. */ |
ajs | 274a4a4 | 2004-12-07 15:39:31 +0000 | [diff] [blame] | 344 | static vector |
hasso | 8c328f1 | 2004-10-05 21:01:23 +0000 | [diff] [blame] | 345 | cmd_make_descvec (const char *string, const char *descstr) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 346 | { |
| 347 | int multiple = 0; |
hasso | 8c328f1 | 2004-10-05 21:01:23 +0000 | [diff] [blame] | 348 | const char *sp; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 349 | char *token; |
| 350 | int len; |
hasso | 8c328f1 | 2004-10-05 21:01:23 +0000 | [diff] [blame] | 351 | const char *cp; |
| 352 | const char *dp; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 353 | vector allvec; |
| 354 | vector strvec = NULL; |
| 355 | struct desc *desc; |
| 356 | |
| 357 | cp = string; |
| 358 | dp = descstr; |
| 359 | |
| 360 | if (cp == NULL) |
| 361 | return NULL; |
| 362 | |
| 363 | allvec = vector_init (VECTOR_MIN_SIZE); |
| 364 | |
| 365 | while (1) |
| 366 | { |
| 367 | while (isspace ((int) *cp) && *cp != '\0') |
| 368 | cp++; |
| 369 | |
| 370 | if (*cp == '(') |
| 371 | { |
| 372 | multiple = 1; |
| 373 | cp++; |
| 374 | } |
| 375 | if (*cp == ')') |
| 376 | { |
| 377 | multiple = 0; |
| 378 | cp++; |
| 379 | } |
| 380 | if (*cp == '|') |
| 381 | { |
| 382 | if (! multiple) |
| 383 | { |
| 384 | fprintf (stderr, "Command parse error!: %s\n", string); |
| 385 | exit (1); |
| 386 | } |
| 387 | cp++; |
| 388 | } |
| 389 | |
| 390 | while (isspace ((int) *cp) && *cp != '\0') |
| 391 | cp++; |
| 392 | |
| 393 | if (*cp == '(') |
| 394 | { |
| 395 | multiple = 1; |
| 396 | cp++; |
| 397 | } |
| 398 | |
| 399 | if (*cp == '\0') |
| 400 | return allvec; |
| 401 | |
| 402 | sp = cp; |
| 403 | |
| 404 | while (! (isspace ((int) *cp) || *cp == '\r' || *cp == '\n' || *cp == ')' || *cp == '|') && *cp != '\0') |
| 405 | cp++; |
| 406 | |
| 407 | len = cp - sp; |
| 408 | |
| 409 | token = XMALLOC (MTYPE_STRVEC, len + 1); |
| 410 | memcpy (token, sp, len); |
| 411 | *(token + len) = '\0'; |
| 412 | |
| 413 | desc = XCALLOC (MTYPE_DESC, sizeof (struct desc)); |
| 414 | desc->cmd = token; |
| 415 | desc->str = cmd_desc_str (&dp); |
| 416 | |
| 417 | if (multiple) |
| 418 | { |
| 419 | if (multiple == 1) |
| 420 | { |
| 421 | strvec = vector_init (VECTOR_MIN_SIZE); |
| 422 | vector_set (allvec, strvec); |
| 423 | } |
| 424 | multiple++; |
| 425 | } |
| 426 | else |
| 427 | { |
| 428 | strvec = vector_init (VECTOR_MIN_SIZE); |
| 429 | vector_set (allvec, strvec); |
| 430 | } |
| 431 | vector_set (strvec, desc); |
| 432 | } |
| 433 | } |
| 434 | |
| 435 | /* Count mandantory string vector size. This is to determine inputed |
| 436 | command has enough command length. */ |
ajs | 274a4a4 | 2004-12-07 15:39:31 +0000 | [diff] [blame] | 437 | static int |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 438 | cmd_cmdsize (vector strvec) |
| 439 | { |
hasso | 8c328f1 | 2004-10-05 21:01:23 +0000 | [diff] [blame] | 440 | unsigned int i; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 441 | int size = 0; |
| 442 | vector descvec; |
paul | b896147 | 2005-03-14 17:35:52 +0000 | [diff] [blame] | 443 | struct desc *desc; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 444 | |
paul | 55468c8 | 2005-03-14 20:19:01 +0000 | [diff] [blame] | 445 | for (i = 0; i < vector_active (strvec); i++) |
paul | b896147 | 2005-03-14 17:35:52 +0000 | [diff] [blame] | 446 | if ((descvec = vector_slot (strvec, i)) != NULL) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 447 | { |
paul | 55468c8 | 2005-03-14 20:19:01 +0000 | [diff] [blame] | 448 | if ((vector_active (descvec)) == 1 |
paul | b896147 | 2005-03-14 17:35:52 +0000 | [diff] [blame] | 449 | && (desc = vector_slot (descvec, 0)) != NULL) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 450 | { |
hasso | 8c328f1 | 2004-10-05 21:01:23 +0000 | [diff] [blame] | 451 | if (desc->cmd == NULL || CMD_OPTION (desc->cmd)) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 452 | return size; |
| 453 | else |
| 454 | size++; |
| 455 | } |
| 456 | else |
| 457 | size++; |
| 458 | } |
| 459 | return size; |
| 460 | } |
| 461 | |
| 462 | /* Return prompt character of specified node. */ |
hasso | 8c328f1 | 2004-10-05 21:01:23 +0000 | [diff] [blame] | 463 | const char * |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 464 | cmd_prompt (enum node_type node) |
| 465 | { |
| 466 | struct cmd_node *cnode; |
| 467 | |
| 468 | cnode = vector_slot (cmdvec, node); |
| 469 | return cnode->prompt; |
| 470 | } |
| 471 | |
| 472 | /* Install a command into a node. */ |
| 473 | void |
| 474 | install_element (enum node_type ntype, struct cmd_element *cmd) |
| 475 | { |
| 476 | struct cmd_node *cnode; |
| 477 | |
| 478 | cnode = vector_slot (cmdvec, ntype); |
| 479 | |
| 480 | if (cnode == NULL) |
| 481 | { |
| 482 | fprintf (stderr, "Command node %d doesn't exist, please check it\n", |
| 483 | ntype); |
| 484 | exit (1); |
| 485 | } |
| 486 | |
| 487 | vector_set (cnode->cmd_vector, cmd); |
| 488 | |
| 489 | cmd->strvec = cmd_make_descvec (cmd->string, cmd->doc); |
| 490 | cmd->cmdsize = cmd_cmdsize (cmd->strvec); |
| 491 | } |
| 492 | |
| 493 | static unsigned char itoa64[] = |
| 494 | "./0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"; |
| 495 | |
ajs | 274a4a4 | 2004-12-07 15:39:31 +0000 | [diff] [blame] | 496 | static void |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 497 | to64(char *s, long v, int n) |
| 498 | { |
| 499 | while (--n >= 0) |
| 500 | { |
| 501 | *s++ = itoa64[v&0x3f]; |
| 502 | v >>= 6; |
| 503 | } |
| 504 | } |
| 505 | |
ajs | 274a4a4 | 2004-12-07 15:39:31 +0000 | [diff] [blame] | 506 | static char * |
| 507 | zencrypt (const char *passwd) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 508 | { |
| 509 | char salt[6]; |
| 510 | struct timeval tv; |
| 511 | char *crypt (const char *, const char *); |
| 512 | |
| 513 | gettimeofday(&tv,0); |
| 514 | |
| 515 | to64(&salt[0], random(), 3); |
| 516 | to64(&salt[3], tv.tv_usec, 3); |
| 517 | salt[5] = '\0'; |
| 518 | |
| 519 | return crypt (passwd, salt); |
| 520 | } |
| 521 | |
| 522 | /* This function write configuration of this host. */ |
ajs | 274a4a4 | 2004-12-07 15:39:31 +0000 | [diff] [blame] | 523 | static int |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 524 | config_write_host (struct vty *vty) |
| 525 | { |
| 526 | if (host.name) |
| 527 | vty_out (vty, "hostname %s%s", host.name, VTY_NEWLINE); |
| 528 | |
| 529 | if (host.encrypt) |
| 530 | { |
| 531 | if (host.password_encrypt) |
| 532 | vty_out (vty, "password 8 %s%s", host.password_encrypt, VTY_NEWLINE); |
| 533 | if (host.enable_encrypt) |
| 534 | vty_out (vty, "enable password 8 %s%s", host.enable_encrypt, VTY_NEWLINE); |
| 535 | } |
| 536 | else |
| 537 | { |
| 538 | if (host.password) |
| 539 | vty_out (vty, "password %s%s", host.password, VTY_NEWLINE); |
| 540 | if (host.enable) |
| 541 | vty_out (vty, "enable password %s%s", host.enable, VTY_NEWLINE); |
| 542 | } |
| 543 | |
ajs | 274a4a4 | 2004-12-07 15:39:31 +0000 | [diff] [blame] | 544 | if (zlog_default->default_lvl != LOG_DEBUG) |
ajs | 82146b8 | 2004-12-07 17:15:55 +0000 | [diff] [blame] | 545 | { |
| 546 | vty_out (vty, "! N.B. The 'log trap' command is deprecated.%s", |
| 547 | VTY_NEWLINE); |
| 548 | vty_out (vty, "log trap %s%s", |
| 549 | zlog_priority[zlog_default->default_lvl], VTY_NEWLINE); |
| 550 | } |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 551 | |
ajs | 274a4a4 | 2004-12-07 15:39:31 +0000 | [diff] [blame] | 552 | if (host.logfile && (zlog_default->maxlvl[ZLOG_DEST_FILE] != ZLOG_DISABLED)) |
paul | 12ab19f | 2003-07-26 06:14:55 +0000 | [diff] [blame] | 553 | { |
ajs | 274a4a4 | 2004-12-07 15:39:31 +0000 | [diff] [blame] | 554 | vty_out (vty, "log file %s", host.logfile); |
| 555 | if (zlog_default->maxlvl[ZLOG_DEST_FILE] != zlog_default->default_lvl) |
| 556 | vty_out (vty, " %s", |
| 557 | zlog_priority[zlog_default->maxlvl[ZLOG_DEST_FILE]]); |
paul | 12ab19f | 2003-07-26 06:14:55 +0000 | [diff] [blame] | 558 | vty_out (vty, "%s", VTY_NEWLINE); |
| 559 | } |
ajs | 274a4a4 | 2004-12-07 15:39:31 +0000 | [diff] [blame] | 560 | |
| 561 | if (zlog_default->maxlvl[ZLOG_DEST_STDOUT] != ZLOG_DISABLED) |
| 562 | { |
| 563 | vty_out (vty, "log stdout"); |
| 564 | if (zlog_default->maxlvl[ZLOG_DEST_STDOUT] != zlog_default->default_lvl) |
| 565 | vty_out (vty, " %s", |
| 566 | zlog_priority[zlog_default->maxlvl[ZLOG_DEST_STDOUT]]); |
| 567 | vty_out (vty, "%s", VTY_NEWLINE); |
| 568 | } |
| 569 | |
| 570 | if (zlog_default->maxlvl[ZLOG_DEST_MONITOR] == ZLOG_DISABLED) |
| 571 | vty_out(vty,"no log monitor%s",VTY_NEWLINE); |
| 572 | else if (zlog_default->maxlvl[ZLOG_DEST_MONITOR] != zlog_default->default_lvl) |
| 573 | vty_out(vty,"log monitor %s%s", |
| 574 | zlog_priority[zlog_default->maxlvl[ZLOG_DEST_MONITOR]],VTY_NEWLINE); |
| 575 | |
| 576 | if (zlog_default->maxlvl[ZLOG_DEST_SYSLOG] != ZLOG_DISABLED) |
| 577 | { |
| 578 | vty_out (vty, "log syslog"); |
| 579 | if (zlog_default->maxlvl[ZLOG_DEST_SYSLOG] != zlog_default->default_lvl) |
| 580 | vty_out (vty, " %s", |
| 581 | zlog_priority[zlog_default->maxlvl[ZLOG_DEST_SYSLOG]]); |
| 582 | vty_out (vty, "%s", VTY_NEWLINE); |
| 583 | } |
| 584 | |
| 585 | if (zlog_default->facility != LOG_DAEMON) |
| 586 | vty_out (vty, "log facility %s%s", |
| 587 | facility_name(zlog_default->facility), VTY_NEWLINE); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 588 | |
| 589 | if (zlog_default->record_priority == 1) |
| 590 | vty_out (vty, "log record-priority%s", VTY_NEWLINE); |
| 591 | |
| 592 | if (host.advanced) |
| 593 | vty_out (vty, "service advanced-vty%s", VTY_NEWLINE); |
| 594 | |
| 595 | if (host.encrypt) |
| 596 | vty_out (vty, "service password-encryption%s", VTY_NEWLINE); |
| 597 | |
| 598 | if (host.lines >= 0) |
| 599 | vty_out (vty, "service terminal-length %d%s", host.lines, |
| 600 | VTY_NEWLINE); |
| 601 | |
paul | 3b0c5d9 | 2005-03-08 10:43:43 +0000 | [diff] [blame] | 602 | if (host.motdfile) |
| 603 | vty_out (vty, "banner motd file %s%s", host.motdfile, VTY_NEWLINE); |
| 604 | else if (! host.motd) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 605 | vty_out (vty, "no banner motd%s", VTY_NEWLINE); |
| 606 | |
| 607 | return 1; |
| 608 | } |
| 609 | |
| 610 | /* Utility function for getting command vector. */ |
ajs | 274a4a4 | 2004-12-07 15:39:31 +0000 | [diff] [blame] | 611 | static vector |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 612 | cmd_node_vector (vector v, enum node_type ntype) |
| 613 | { |
| 614 | struct cmd_node *cnode = vector_slot (v, ntype); |
| 615 | return cnode->cmd_vector; |
| 616 | } |
| 617 | |
ajs | 274a4a4 | 2004-12-07 15:39:31 +0000 | [diff] [blame] | 618 | #if 0 |
| 619 | /* Filter command vector by symbol. This function is not actually used; |
| 620 | * should it be deleted? */ |
| 621 | static int |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 622 | cmd_filter_by_symbol (char *command, char *symbol) |
| 623 | { |
| 624 | int i, lim; |
| 625 | |
| 626 | if (strcmp (symbol, "IPV4_ADDRESS") == 0) |
| 627 | { |
| 628 | i = 0; |
| 629 | lim = strlen (command); |
| 630 | while (i < lim) |
| 631 | { |
| 632 | if (! (isdigit ((int) command[i]) || command[i] == '.' || command[i] == '/')) |
| 633 | return 1; |
| 634 | i++; |
| 635 | } |
| 636 | return 0; |
| 637 | } |
| 638 | if (strcmp (symbol, "STRING") == 0) |
| 639 | { |
| 640 | i = 0; |
| 641 | lim = strlen (command); |
| 642 | while (i < lim) |
| 643 | { |
| 644 | if (! (isalpha ((int) command[i]) || command[i] == '_' || command[i] == '-')) |
| 645 | return 1; |
| 646 | i++; |
| 647 | } |
| 648 | return 0; |
| 649 | } |
| 650 | if (strcmp (symbol, "IFNAME") == 0) |
| 651 | { |
| 652 | i = 0; |
| 653 | lim = strlen (command); |
| 654 | while (i < lim) |
| 655 | { |
| 656 | if (! isalnum ((int) command[i])) |
| 657 | return 1; |
| 658 | i++; |
| 659 | } |
| 660 | return 0; |
| 661 | } |
| 662 | return 0; |
| 663 | } |
ajs | 274a4a4 | 2004-12-07 15:39:31 +0000 | [diff] [blame] | 664 | #endif |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 665 | |
| 666 | /* Completion match types. */ |
| 667 | enum match_type |
| 668 | { |
| 669 | no_match, |
| 670 | extend_match, |
| 671 | ipv4_prefix_match, |
| 672 | ipv4_match, |
| 673 | ipv6_prefix_match, |
| 674 | ipv6_match, |
| 675 | range_match, |
| 676 | vararg_match, |
| 677 | partly_match, |
| 678 | exact_match |
| 679 | }; |
| 680 | |
ajs | 274a4a4 | 2004-12-07 15:39:31 +0000 | [diff] [blame] | 681 | static enum match_type |
hasso | 8c328f1 | 2004-10-05 21:01:23 +0000 | [diff] [blame] | 682 | cmd_ipv4_match (const char *str) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 683 | { |
hasso | 8c328f1 | 2004-10-05 21:01:23 +0000 | [diff] [blame] | 684 | const char *sp; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 685 | int dots = 0, nums = 0; |
| 686 | char buf[4]; |
| 687 | |
| 688 | if (str == NULL) |
| 689 | return partly_match; |
| 690 | |
| 691 | for (;;) |
| 692 | { |
| 693 | memset (buf, 0, sizeof (buf)); |
| 694 | sp = str; |
| 695 | while (*str != '\0') |
| 696 | { |
| 697 | if (*str == '.') |
| 698 | { |
| 699 | if (dots >= 3) |
| 700 | return no_match; |
| 701 | |
| 702 | if (*(str + 1) == '.') |
| 703 | return no_match; |
| 704 | |
| 705 | if (*(str + 1) == '\0') |
| 706 | return partly_match; |
| 707 | |
| 708 | dots++; |
| 709 | break; |
| 710 | } |
| 711 | if (!isdigit ((int) *str)) |
| 712 | return no_match; |
| 713 | |
| 714 | str++; |
| 715 | } |
| 716 | |
| 717 | if (str - sp > 3) |
| 718 | return no_match; |
| 719 | |
| 720 | strncpy (buf, sp, str - sp); |
| 721 | if (atoi (buf) > 255) |
| 722 | return no_match; |
| 723 | |
| 724 | nums++; |
| 725 | |
| 726 | if (*str == '\0') |
| 727 | break; |
| 728 | |
| 729 | str++; |
| 730 | } |
| 731 | |
| 732 | if (nums < 4) |
| 733 | return partly_match; |
| 734 | |
| 735 | return exact_match; |
| 736 | } |
| 737 | |
ajs | 274a4a4 | 2004-12-07 15:39:31 +0000 | [diff] [blame] | 738 | static enum match_type |
hasso | 8c328f1 | 2004-10-05 21:01:23 +0000 | [diff] [blame] | 739 | cmd_ipv4_prefix_match (const char *str) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 740 | { |
hasso | 8c328f1 | 2004-10-05 21:01:23 +0000 | [diff] [blame] | 741 | const char *sp; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 742 | int dots = 0; |
| 743 | char buf[4]; |
| 744 | |
| 745 | if (str == NULL) |
| 746 | return partly_match; |
| 747 | |
| 748 | for (;;) |
| 749 | { |
| 750 | memset (buf, 0, sizeof (buf)); |
| 751 | sp = str; |
| 752 | while (*str != '\0' && *str != '/') |
| 753 | { |
| 754 | if (*str == '.') |
| 755 | { |
| 756 | if (dots == 3) |
| 757 | return no_match; |
| 758 | |
| 759 | if (*(str + 1) == '.' || *(str + 1) == '/') |
| 760 | return no_match; |
| 761 | |
| 762 | if (*(str + 1) == '\0') |
| 763 | return partly_match; |
| 764 | |
| 765 | dots++; |
| 766 | break; |
| 767 | } |
| 768 | |
| 769 | if (!isdigit ((int) *str)) |
| 770 | return no_match; |
| 771 | |
| 772 | str++; |
| 773 | } |
| 774 | |
| 775 | if (str - sp > 3) |
| 776 | return no_match; |
| 777 | |
| 778 | strncpy (buf, sp, str - sp); |
| 779 | if (atoi (buf) > 255) |
| 780 | return no_match; |
| 781 | |
| 782 | if (dots == 3) |
| 783 | { |
| 784 | if (*str == '/') |
| 785 | { |
| 786 | if (*(str + 1) == '\0') |
| 787 | return partly_match; |
| 788 | |
| 789 | str++; |
| 790 | break; |
| 791 | } |
| 792 | else if (*str == '\0') |
| 793 | return partly_match; |
| 794 | } |
| 795 | |
| 796 | if (*str == '\0') |
| 797 | return partly_match; |
| 798 | |
| 799 | str++; |
| 800 | } |
| 801 | |
| 802 | sp = str; |
| 803 | while (*str != '\0') |
| 804 | { |
| 805 | if (!isdigit ((int) *str)) |
| 806 | return no_match; |
| 807 | |
| 808 | str++; |
| 809 | } |
| 810 | |
| 811 | if (atoi (sp) > 32) |
| 812 | return no_match; |
| 813 | |
| 814 | return exact_match; |
| 815 | } |
| 816 | |
| 817 | #define IPV6_ADDR_STR "0123456789abcdefABCDEF:.%" |
| 818 | #define IPV6_PREFIX_STR "0123456789abcdefABCDEF:.%/" |
| 819 | #define STATE_START 1 |
| 820 | #define STATE_COLON 2 |
| 821 | #define STATE_DOUBLE 3 |
| 822 | #define STATE_ADDR 4 |
| 823 | #define STATE_DOT 5 |
| 824 | #define STATE_SLASH 6 |
| 825 | #define STATE_MASK 7 |
| 826 | |
paul | 22e0a9e | 2003-07-11 17:55:46 +0000 | [diff] [blame] | 827 | #ifdef HAVE_IPV6 |
| 828 | |
ajs | 274a4a4 | 2004-12-07 15:39:31 +0000 | [diff] [blame] | 829 | static enum match_type |
hasso | 8c328f1 | 2004-10-05 21:01:23 +0000 | [diff] [blame] | 830 | cmd_ipv6_match (const char *str) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 831 | { |
| 832 | int state = STATE_START; |
| 833 | int colons = 0, nums = 0, double_colon = 0; |
hasso | 8c328f1 | 2004-10-05 21:01:23 +0000 | [diff] [blame] | 834 | const char *sp = NULL; |
hasso | 726f9b2 | 2003-05-25 21:04:54 +0000 | [diff] [blame] | 835 | struct sockaddr_in6 sin6_dummy; |
| 836 | int ret; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 837 | |
| 838 | if (str == NULL) |
| 839 | return partly_match; |
| 840 | |
| 841 | if (strspn (str, IPV6_ADDR_STR) != strlen (str)) |
| 842 | return no_match; |
| 843 | |
hasso | 726f9b2 | 2003-05-25 21:04:54 +0000 | [diff] [blame] | 844 | /* use inet_pton that has a better support, |
| 845 | * for example inet_pton can support the automatic addresses: |
| 846 | * ::1.2.3.4 |
| 847 | */ |
| 848 | ret = inet_pton(AF_INET6, str, &sin6_dummy.sin6_addr); |
| 849 | |
| 850 | if (ret == 1) |
| 851 | return exact_match; |
| 852 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 853 | while (*str != '\0') |
| 854 | { |
| 855 | switch (state) |
| 856 | { |
| 857 | case STATE_START: |
| 858 | if (*str == ':') |
| 859 | { |
| 860 | if (*(str + 1) != ':' && *(str + 1) != '\0') |
| 861 | return no_match; |
| 862 | colons--; |
| 863 | state = STATE_COLON; |
| 864 | } |
| 865 | else |
| 866 | { |
| 867 | sp = str; |
| 868 | state = STATE_ADDR; |
| 869 | } |
| 870 | |
| 871 | continue; |
| 872 | case STATE_COLON: |
| 873 | colons++; |
| 874 | if (*(str + 1) == ':') |
| 875 | state = STATE_DOUBLE; |
| 876 | else |
| 877 | { |
| 878 | sp = str + 1; |
| 879 | state = STATE_ADDR; |
| 880 | } |
| 881 | break; |
| 882 | case STATE_DOUBLE: |
| 883 | if (double_colon) |
| 884 | return no_match; |
| 885 | |
| 886 | if (*(str + 1) == ':') |
| 887 | return no_match; |
| 888 | else |
| 889 | { |
| 890 | if (*(str + 1) != '\0') |
| 891 | colons++; |
| 892 | sp = str + 1; |
| 893 | state = STATE_ADDR; |
| 894 | } |
| 895 | |
| 896 | double_colon++; |
| 897 | nums++; |
| 898 | break; |
| 899 | case STATE_ADDR: |
| 900 | if (*(str + 1) == ':' || *(str + 1) == '\0') |
| 901 | { |
| 902 | if (str - sp > 3) |
| 903 | return no_match; |
| 904 | |
| 905 | nums++; |
| 906 | state = STATE_COLON; |
| 907 | } |
| 908 | if (*(str + 1) == '.') |
| 909 | state = STATE_DOT; |
| 910 | break; |
| 911 | case STATE_DOT: |
| 912 | state = STATE_ADDR; |
| 913 | break; |
| 914 | default: |
| 915 | break; |
| 916 | } |
| 917 | |
| 918 | if (nums > 8) |
| 919 | return no_match; |
| 920 | |
| 921 | if (colons > 7) |
| 922 | return no_match; |
| 923 | |
| 924 | str++; |
| 925 | } |
| 926 | |
| 927 | #if 0 |
| 928 | if (nums < 11) |
| 929 | return partly_match; |
| 930 | #endif /* 0 */ |
| 931 | |
| 932 | return exact_match; |
| 933 | } |
| 934 | |
ajs | 274a4a4 | 2004-12-07 15:39:31 +0000 | [diff] [blame] | 935 | static enum match_type |
hasso | 8c328f1 | 2004-10-05 21:01:23 +0000 | [diff] [blame] | 936 | cmd_ipv6_prefix_match (const char *str) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 937 | { |
| 938 | int state = STATE_START; |
| 939 | int colons = 0, nums = 0, double_colon = 0; |
| 940 | int mask; |
hasso | 8c328f1 | 2004-10-05 21:01:23 +0000 | [diff] [blame] | 941 | const char *sp = NULL; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 942 | char *endptr = NULL; |
| 943 | |
| 944 | if (str == NULL) |
| 945 | return partly_match; |
| 946 | |
| 947 | if (strspn (str, IPV6_PREFIX_STR) != strlen (str)) |
| 948 | return no_match; |
| 949 | |
| 950 | while (*str != '\0' && state != STATE_MASK) |
| 951 | { |
| 952 | switch (state) |
| 953 | { |
| 954 | case STATE_START: |
| 955 | if (*str == ':') |
| 956 | { |
| 957 | if (*(str + 1) != ':' && *(str + 1) != '\0') |
| 958 | return no_match; |
| 959 | colons--; |
| 960 | state = STATE_COLON; |
| 961 | } |
| 962 | else |
| 963 | { |
| 964 | sp = str; |
| 965 | state = STATE_ADDR; |
| 966 | } |
| 967 | |
| 968 | continue; |
| 969 | case STATE_COLON: |
| 970 | colons++; |
| 971 | if (*(str + 1) == '/') |
| 972 | return no_match; |
| 973 | else if (*(str + 1) == ':') |
| 974 | state = STATE_DOUBLE; |
| 975 | else |
| 976 | { |
| 977 | sp = str + 1; |
| 978 | state = STATE_ADDR; |
| 979 | } |
| 980 | break; |
| 981 | case STATE_DOUBLE: |
| 982 | if (double_colon) |
| 983 | return no_match; |
| 984 | |
| 985 | if (*(str + 1) == ':') |
| 986 | return no_match; |
| 987 | else |
| 988 | { |
| 989 | if (*(str + 1) != '\0' && *(str + 1) != '/') |
| 990 | colons++; |
| 991 | sp = str + 1; |
| 992 | |
| 993 | if (*(str + 1) == '/') |
| 994 | state = STATE_SLASH; |
| 995 | else |
| 996 | state = STATE_ADDR; |
| 997 | } |
| 998 | |
| 999 | double_colon++; |
| 1000 | nums += 1; |
| 1001 | break; |
| 1002 | case STATE_ADDR: |
| 1003 | if (*(str + 1) == ':' || *(str + 1) == '.' |
| 1004 | || *(str + 1) == '\0' || *(str + 1) == '/') |
| 1005 | { |
| 1006 | if (str - sp > 3) |
| 1007 | return no_match; |
| 1008 | |
| 1009 | for (; sp <= str; sp++) |
| 1010 | if (*sp == '/') |
| 1011 | return no_match; |
| 1012 | |
| 1013 | nums++; |
| 1014 | |
| 1015 | if (*(str + 1) == ':') |
| 1016 | state = STATE_COLON; |
| 1017 | else if (*(str + 1) == '.') |
| 1018 | state = STATE_DOT; |
| 1019 | else if (*(str + 1) == '/') |
| 1020 | state = STATE_SLASH; |
| 1021 | } |
| 1022 | break; |
| 1023 | case STATE_DOT: |
| 1024 | state = STATE_ADDR; |
| 1025 | break; |
| 1026 | case STATE_SLASH: |
| 1027 | if (*(str + 1) == '\0') |
| 1028 | return partly_match; |
| 1029 | |
| 1030 | state = STATE_MASK; |
| 1031 | break; |
| 1032 | default: |
| 1033 | break; |
| 1034 | } |
| 1035 | |
| 1036 | if (nums > 11) |
| 1037 | return no_match; |
| 1038 | |
| 1039 | if (colons > 7) |
| 1040 | return no_match; |
| 1041 | |
| 1042 | str++; |
| 1043 | } |
| 1044 | |
| 1045 | if (state < STATE_MASK) |
| 1046 | return partly_match; |
| 1047 | |
| 1048 | mask = strtol (str, &endptr, 10); |
| 1049 | if (*endptr != '\0') |
| 1050 | return no_match; |
| 1051 | |
| 1052 | if (mask < 0 || mask > 128) |
| 1053 | return no_match; |
| 1054 | |
| 1055 | /* I don't know why mask < 13 makes command match partly. |
| 1056 | Forgive me to make this comments. I Want to set static default route |
| 1057 | because of lack of function to originate default in ospf6d; sorry |
| 1058 | yasu |
| 1059 | if (mask < 13) |
| 1060 | return partly_match; |
| 1061 | */ |
| 1062 | |
| 1063 | return exact_match; |
| 1064 | } |
| 1065 | |
paul | 22e0a9e | 2003-07-11 17:55:46 +0000 | [diff] [blame] | 1066 | #endif /* HAVE_IPV6 */ |
| 1067 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1068 | #define DECIMAL_STRLEN_MAX 10 |
| 1069 | |
ajs | 274a4a4 | 2004-12-07 15:39:31 +0000 | [diff] [blame] | 1070 | static int |
hasso | 8c328f1 | 2004-10-05 21:01:23 +0000 | [diff] [blame] | 1071 | cmd_range_match (const char *range, const char *str) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1072 | { |
| 1073 | char *p; |
| 1074 | char buf[DECIMAL_STRLEN_MAX + 1]; |
| 1075 | char *endptr = NULL; |
| 1076 | unsigned long min, max, val; |
| 1077 | |
| 1078 | if (str == NULL) |
| 1079 | return 1; |
| 1080 | |
| 1081 | val = strtoul (str, &endptr, 10); |
| 1082 | if (*endptr != '\0') |
| 1083 | return 0; |
| 1084 | |
| 1085 | range++; |
| 1086 | p = strchr (range, '-'); |
| 1087 | if (p == NULL) |
| 1088 | return 0; |
| 1089 | if (p - range > DECIMAL_STRLEN_MAX) |
| 1090 | return 0; |
| 1091 | strncpy (buf, range, p - range); |
| 1092 | buf[p - range] = '\0'; |
| 1093 | min = strtoul (buf, &endptr, 10); |
| 1094 | if (*endptr != '\0') |
| 1095 | return 0; |
| 1096 | |
| 1097 | range = p + 1; |
| 1098 | p = strchr (range, '>'); |
| 1099 | if (p == NULL) |
| 1100 | return 0; |
| 1101 | if (p - range > DECIMAL_STRLEN_MAX) |
| 1102 | return 0; |
| 1103 | strncpy (buf, range, p - range); |
| 1104 | buf[p - range] = '\0'; |
| 1105 | max = strtoul (buf, &endptr, 10); |
| 1106 | if (*endptr != '\0') |
| 1107 | return 0; |
| 1108 | |
| 1109 | if (val < min || val > max) |
| 1110 | return 0; |
| 1111 | |
| 1112 | return 1; |
| 1113 | } |
| 1114 | |
| 1115 | /* Make completion match and return match type flag. */ |
ajs | 274a4a4 | 2004-12-07 15:39:31 +0000 | [diff] [blame] | 1116 | static enum match_type |
hasso | 8c328f1 | 2004-10-05 21:01:23 +0000 | [diff] [blame] | 1117 | cmd_filter_by_completion (char *command, vector v, unsigned int index) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1118 | { |
hasso | 8c328f1 | 2004-10-05 21:01:23 +0000 | [diff] [blame] | 1119 | unsigned int i; |
| 1120 | const char *str; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1121 | struct cmd_element *cmd_element; |
| 1122 | enum match_type match_type; |
| 1123 | vector descvec; |
| 1124 | struct desc *desc; |
paul | 909a215 | 2005-03-14 17:41:45 +0000 | [diff] [blame] | 1125 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1126 | match_type = no_match; |
| 1127 | |
| 1128 | /* If command and cmd_element string does not match set NULL to vector */ |
paul | 55468c8 | 2005-03-14 20:19:01 +0000 | [diff] [blame] | 1129 | for (i = 0; i < vector_active (v); i++) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1130 | if ((cmd_element = vector_slot (v, i)) != NULL) |
| 1131 | { |
paul | 55468c8 | 2005-03-14 20:19:01 +0000 | [diff] [blame] | 1132 | if (index >= vector_active (cmd_element->strvec)) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1133 | vector_slot (v, i) = NULL; |
| 1134 | else |
| 1135 | { |
hasso | 8c328f1 | 2004-10-05 21:01:23 +0000 | [diff] [blame] | 1136 | unsigned int j; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1137 | int matched = 0; |
| 1138 | |
| 1139 | descvec = vector_slot (cmd_element->strvec, index); |
paul | 909a215 | 2005-03-14 17:41:45 +0000 | [diff] [blame] | 1140 | |
paul | 55468c8 | 2005-03-14 20:19:01 +0000 | [diff] [blame] | 1141 | for (j = 0; j < vector_active (descvec); j++) |
paul | b896147 | 2005-03-14 17:35:52 +0000 | [diff] [blame] | 1142 | if ((desc = vector_slot (descvec, j))) |
paul | 909a215 | 2005-03-14 17:41:45 +0000 | [diff] [blame] | 1143 | { |
| 1144 | str = desc->cmd; |
| 1145 | |
| 1146 | if (CMD_VARARG (str)) |
| 1147 | { |
| 1148 | if (match_type < vararg_match) |
| 1149 | match_type = vararg_match; |
| 1150 | matched++; |
| 1151 | } |
| 1152 | else if (CMD_RANGE (str)) |
| 1153 | { |
| 1154 | if (cmd_range_match (str, command)) |
| 1155 | { |
| 1156 | if (match_type < range_match) |
| 1157 | match_type = range_match; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1158 | |
paul | 909a215 | 2005-03-14 17:41:45 +0000 | [diff] [blame] | 1159 | matched++; |
| 1160 | } |
| 1161 | } |
paul | 22e0a9e | 2003-07-11 17:55:46 +0000 | [diff] [blame] | 1162 | #ifdef HAVE_IPV6 |
paul | 909a215 | 2005-03-14 17:41:45 +0000 | [diff] [blame] | 1163 | else if (CMD_IPV6 (str)) |
| 1164 | { |
| 1165 | if (cmd_ipv6_match (command)) |
| 1166 | { |
| 1167 | if (match_type < ipv6_match) |
| 1168 | match_type = ipv6_match; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1169 | |
paul | 909a215 | 2005-03-14 17:41:45 +0000 | [diff] [blame] | 1170 | matched++; |
| 1171 | } |
| 1172 | } |
| 1173 | else if (CMD_IPV6_PREFIX (str)) |
| 1174 | { |
| 1175 | if (cmd_ipv6_prefix_match (command)) |
| 1176 | { |
| 1177 | if (match_type < ipv6_prefix_match) |
| 1178 | match_type = ipv6_prefix_match; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1179 | |
paul | 909a215 | 2005-03-14 17:41:45 +0000 | [diff] [blame] | 1180 | matched++; |
| 1181 | } |
| 1182 | } |
| 1183 | #endif /* HAVE_IPV6 */ |
| 1184 | else if (CMD_IPV4 (str)) |
| 1185 | { |
| 1186 | if (cmd_ipv4_match (command)) |
| 1187 | { |
| 1188 | if (match_type < ipv4_match) |
| 1189 | match_type = ipv4_match; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1190 | |
paul | 909a215 | 2005-03-14 17:41:45 +0000 | [diff] [blame] | 1191 | matched++; |
| 1192 | } |
| 1193 | } |
| 1194 | else if (CMD_IPV4_PREFIX (str)) |
| 1195 | { |
| 1196 | if (cmd_ipv4_prefix_match (command)) |
| 1197 | { |
| 1198 | if (match_type < ipv4_prefix_match) |
| 1199 | match_type = ipv4_prefix_match; |
| 1200 | matched++; |
| 1201 | } |
| 1202 | } |
| 1203 | else |
| 1204 | /* Check is this point's argument optional ? */ |
| 1205 | if (CMD_OPTION (str) || CMD_VARIABLE (str)) |
| 1206 | { |
| 1207 | if (match_type < extend_match) |
| 1208 | match_type = extend_match; |
| 1209 | matched++; |
| 1210 | } |
| 1211 | else if (strncmp (command, str, strlen (command)) == 0) |
| 1212 | { |
| 1213 | if (strcmp (command, str) == 0) |
| 1214 | match_type = exact_match; |
| 1215 | else |
| 1216 | { |
| 1217 | if (match_type < partly_match) |
| 1218 | match_type = partly_match; |
| 1219 | } |
| 1220 | matched++; |
| 1221 | } |
| 1222 | } |
| 1223 | if (!matched) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1224 | vector_slot (v, i) = NULL; |
| 1225 | } |
| 1226 | } |
| 1227 | return match_type; |
| 1228 | } |
| 1229 | |
| 1230 | /* Filter vector by command character with index. */ |
ajs | 274a4a4 | 2004-12-07 15:39:31 +0000 | [diff] [blame] | 1231 | static enum match_type |
hasso | 8c328f1 | 2004-10-05 21:01:23 +0000 | [diff] [blame] | 1232 | cmd_filter_by_string (char *command, vector v, unsigned int index) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1233 | { |
hasso | 8c328f1 | 2004-10-05 21:01:23 +0000 | [diff] [blame] | 1234 | unsigned int i; |
| 1235 | const char *str; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1236 | struct cmd_element *cmd_element; |
| 1237 | enum match_type match_type; |
| 1238 | vector descvec; |
| 1239 | struct desc *desc; |
paul | 909a215 | 2005-03-14 17:41:45 +0000 | [diff] [blame] | 1240 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1241 | match_type = no_match; |
| 1242 | |
| 1243 | /* If command and cmd_element string does not match set NULL to vector */ |
paul | 55468c8 | 2005-03-14 20:19:01 +0000 | [diff] [blame] | 1244 | for (i = 0; i < vector_active (v); i++) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1245 | if ((cmd_element = vector_slot (v, i)) != NULL) |
| 1246 | { |
| 1247 | /* If given index is bigger than max string vector of command, |
paul | 909a215 | 2005-03-14 17:41:45 +0000 | [diff] [blame] | 1248 | set NULL */ |
paul | 55468c8 | 2005-03-14 20:19:01 +0000 | [diff] [blame] | 1249 | if (index >= vector_active (cmd_element->strvec)) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1250 | vector_slot (v, i) = NULL; |
paul | 909a215 | 2005-03-14 17:41:45 +0000 | [diff] [blame] | 1251 | else |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1252 | { |
hasso | 8c328f1 | 2004-10-05 21:01:23 +0000 | [diff] [blame] | 1253 | unsigned int j; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1254 | int matched = 0; |
| 1255 | |
| 1256 | descvec = vector_slot (cmd_element->strvec, index); |
| 1257 | |
paul | 55468c8 | 2005-03-14 20:19:01 +0000 | [diff] [blame] | 1258 | for (j = 0; j < vector_active (descvec); j++) |
paul | b896147 | 2005-03-14 17:35:52 +0000 | [diff] [blame] | 1259 | if ((desc = vector_slot (descvec, j))) |
paul | 909a215 | 2005-03-14 17:41:45 +0000 | [diff] [blame] | 1260 | { |
| 1261 | str = desc->cmd; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1262 | |
paul | 909a215 | 2005-03-14 17:41:45 +0000 | [diff] [blame] | 1263 | if (CMD_VARARG (str)) |
| 1264 | { |
| 1265 | if (match_type < vararg_match) |
| 1266 | match_type = vararg_match; |
| 1267 | matched++; |
| 1268 | } |
| 1269 | else if (CMD_RANGE (str)) |
| 1270 | { |
| 1271 | if (cmd_range_match (str, command)) |
| 1272 | { |
| 1273 | if (match_type < range_match) |
| 1274 | match_type = range_match; |
| 1275 | matched++; |
| 1276 | } |
| 1277 | } |
paul | 22e0a9e | 2003-07-11 17:55:46 +0000 | [diff] [blame] | 1278 | #ifdef HAVE_IPV6 |
paul | 909a215 | 2005-03-14 17:41:45 +0000 | [diff] [blame] | 1279 | else if (CMD_IPV6 (str)) |
| 1280 | { |
| 1281 | if (cmd_ipv6_match (command) == exact_match) |
| 1282 | { |
| 1283 | if (match_type < ipv6_match) |
| 1284 | match_type = ipv6_match; |
| 1285 | matched++; |
| 1286 | } |
| 1287 | } |
| 1288 | else if (CMD_IPV6_PREFIX (str)) |
| 1289 | { |
| 1290 | if (cmd_ipv6_prefix_match (command) == exact_match) |
| 1291 | { |
| 1292 | if (match_type < ipv6_prefix_match) |
| 1293 | match_type = ipv6_prefix_match; |
| 1294 | matched++; |
| 1295 | } |
| 1296 | } |
paul | 22e0a9e | 2003-07-11 17:55:46 +0000 | [diff] [blame] | 1297 | #endif /* HAVE_IPV6 */ |
paul | 909a215 | 2005-03-14 17:41:45 +0000 | [diff] [blame] | 1298 | else if (CMD_IPV4 (str)) |
| 1299 | { |
| 1300 | if (cmd_ipv4_match (command) == exact_match) |
| 1301 | { |
| 1302 | if (match_type < ipv4_match) |
| 1303 | match_type = ipv4_match; |
| 1304 | matched++; |
| 1305 | } |
| 1306 | } |
| 1307 | else if (CMD_IPV4_PREFIX (str)) |
| 1308 | { |
| 1309 | if (cmd_ipv4_prefix_match (command) == exact_match) |
| 1310 | { |
| 1311 | if (match_type < ipv4_prefix_match) |
| 1312 | match_type = ipv4_prefix_match; |
| 1313 | matched++; |
| 1314 | } |
| 1315 | } |
| 1316 | else if (CMD_OPTION (str) || CMD_VARIABLE (str)) |
| 1317 | { |
| 1318 | if (match_type < extend_match) |
| 1319 | match_type = extend_match; |
| 1320 | matched++; |
| 1321 | } |
| 1322 | else |
| 1323 | { |
| 1324 | if (strcmp (command, str) == 0) |
| 1325 | { |
| 1326 | match_type = exact_match; |
| 1327 | matched++; |
| 1328 | } |
| 1329 | } |
| 1330 | } |
| 1331 | if (!matched) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1332 | vector_slot (v, i) = NULL; |
| 1333 | } |
| 1334 | } |
| 1335 | return match_type; |
| 1336 | } |
| 1337 | |
| 1338 | /* Check ambiguous match */ |
ajs | 274a4a4 | 2004-12-07 15:39:31 +0000 | [diff] [blame] | 1339 | static int |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1340 | is_cmd_ambiguous (char *command, vector v, int index, enum match_type type) |
| 1341 | { |
hasso | 8c328f1 | 2004-10-05 21:01:23 +0000 | [diff] [blame] | 1342 | unsigned int i; |
| 1343 | unsigned int j; |
| 1344 | const char *str = NULL; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1345 | struct cmd_element *cmd_element; |
hasso | 8c328f1 | 2004-10-05 21:01:23 +0000 | [diff] [blame] | 1346 | const char *matched = NULL; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1347 | vector descvec; |
| 1348 | struct desc *desc; |
paul | 909a215 | 2005-03-14 17:41:45 +0000 | [diff] [blame] | 1349 | |
paul | 55468c8 | 2005-03-14 20:19:01 +0000 | [diff] [blame] | 1350 | for (i = 0; i < vector_active (v); i++) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1351 | if ((cmd_element = vector_slot (v, i)) != NULL) |
| 1352 | { |
| 1353 | int match = 0; |
| 1354 | |
| 1355 | descvec = vector_slot (cmd_element->strvec, index); |
| 1356 | |
paul | 55468c8 | 2005-03-14 20:19:01 +0000 | [diff] [blame] | 1357 | for (j = 0; j < vector_active (descvec); j++) |
paul | b896147 | 2005-03-14 17:35:52 +0000 | [diff] [blame] | 1358 | if ((desc = vector_slot (descvec, j))) |
paul | 909a215 | 2005-03-14 17:41:45 +0000 | [diff] [blame] | 1359 | { |
| 1360 | enum match_type ret; |
| 1361 | |
| 1362 | str = desc->cmd; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1363 | |
paul | 909a215 | 2005-03-14 17:41:45 +0000 | [diff] [blame] | 1364 | switch (type) |
| 1365 | { |
| 1366 | case exact_match: |
| 1367 | if (!(CMD_OPTION (str) || CMD_VARIABLE (str)) |
| 1368 | && strcmp (command, str) == 0) |
| 1369 | match++; |
| 1370 | break; |
| 1371 | case partly_match: |
| 1372 | if (!(CMD_OPTION (str) || CMD_VARIABLE (str)) |
| 1373 | && strncmp (command, str, strlen (command)) == 0) |
| 1374 | { |
| 1375 | if (matched && strcmp (matched, str) != 0) |
| 1376 | return 1; /* There is ambiguous match. */ |
| 1377 | else |
| 1378 | matched = str; |
| 1379 | match++; |
| 1380 | } |
| 1381 | break; |
| 1382 | case range_match: |
| 1383 | if (cmd_range_match (str, command)) |
| 1384 | { |
| 1385 | if (matched && strcmp (matched, str) != 0) |
| 1386 | return 1; |
| 1387 | else |
| 1388 | matched = str; |
| 1389 | match++; |
| 1390 | } |
| 1391 | break; |
| 1392 | #ifdef HAVE_IPV6 |
| 1393 | case ipv6_match: |
| 1394 | if (CMD_IPV6 (str)) |
| 1395 | match++; |
| 1396 | break; |
| 1397 | case ipv6_prefix_match: |
| 1398 | if ((ret = cmd_ipv6_prefix_match (command)) != no_match) |
| 1399 | { |
| 1400 | if (ret == partly_match) |
| 1401 | return 2; /* There is incomplete match. */ |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1402 | |
paul | 909a215 | 2005-03-14 17:41:45 +0000 | [diff] [blame] | 1403 | match++; |
| 1404 | } |
| 1405 | break; |
| 1406 | #endif /* HAVE_IPV6 */ |
| 1407 | case ipv4_match: |
| 1408 | if (CMD_IPV4 (str)) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1409 | match++; |
paul | 909a215 | 2005-03-14 17:41:45 +0000 | [diff] [blame] | 1410 | break; |
| 1411 | case ipv4_prefix_match: |
| 1412 | if ((ret = cmd_ipv4_prefix_match (command)) != no_match) |
| 1413 | { |
| 1414 | if (ret == partly_match) |
| 1415 | return 2; /* There is incomplete match. */ |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1416 | |
paul | 909a215 | 2005-03-14 17:41:45 +0000 | [diff] [blame] | 1417 | match++; |
| 1418 | } |
| 1419 | break; |
| 1420 | case extend_match: |
| 1421 | if (CMD_OPTION (str) || CMD_VARIABLE (str)) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1422 | match++; |
paul | 909a215 | 2005-03-14 17:41:45 +0000 | [diff] [blame] | 1423 | break; |
| 1424 | case no_match: |
| 1425 | default: |
| 1426 | break; |
| 1427 | } |
| 1428 | } |
| 1429 | if (!match) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1430 | vector_slot (v, i) = NULL; |
| 1431 | } |
| 1432 | return 0; |
| 1433 | } |
| 1434 | |
| 1435 | /* If src matches dst return dst string, otherwise return NULL */ |
ajs | 274a4a4 | 2004-12-07 15:39:31 +0000 | [diff] [blame] | 1436 | static const char * |
hasso | 8c328f1 | 2004-10-05 21:01:23 +0000 | [diff] [blame] | 1437 | cmd_entry_function (const char *src, const char *dst) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1438 | { |
| 1439 | /* Skip variable arguments. */ |
| 1440 | if (CMD_OPTION (dst) || CMD_VARIABLE (dst) || CMD_VARARG (dst) || |
| 1441 | CMD_IPV4 (dst) || CMD_IPV4_PREFIX (dst) || CMD_RANGE (dst)) |
| 1442 | return NULL; |
| 1443 | |
| 1444 | /* In case of 'command \t', given src is NULL string. */ |
| 1445 | if (src == NULL) |
| 1446 | return dst; |
| 1447 | |
| 1448 | /* Matched with input string. */ |
| 1449 | if (strncmp (src, dst, strlen (src)) == 0) |
| 1450 | return dst; |
| 1451 | |
| 1452 | return NULL; |
| 1453 | } |
| 1454 | |
| 1455 | /* If src matches dst return dst string, otherwise return NULL */ |
| 1456 | /* This version will return the dst string always if it is |
| 1457 | CMD_VARIABLE for '?' key processing */ |
ajs | 274a4a4 | 2004-12-07 15:39:31 +0000 | [diff] [blame] | 1458 | static const char * |
hasso | 8c328f1 | 2004-10-05 21:01:23 +0000 | [diff] [blame] | 1459 | cmd_entry_function_desc (const char *src, const char *dst) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1460 | { |
| 1461 | if (CMD_VARARG (dst)) |
| 1462 | return dst; |
| 1463 | |
| 1464 | if (CMD_RANGE (dst)) |
| 1465 | { |
| 1466 | if (cmd_range_match (dst, src)) |
| 1467 | return dst; |
| 1468 | else |
| 1469 | return NULL; |
| 1470 | } |
| 1471 | |
paul | 22e0a9e | 2003-07-11 17:55:46 +0000 | [diff] [blame] | 1472 | #ifdef HAVE_IPV6 |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1473 | if (CMD_IPV6 (dst)) |
| 1474 | { |
| 1475 | if (cmd_ipv6_match (src)) |
| 1476 | return dst; |
| 1477 | else |
| 1478 | return NULL; |
| 1479 | } |
| 1480 | |
| 1481 | if (CMD_IPV6_PREFIX (dst)) |
| 1482 | { |
| 1483 | if (cmd_ipv6_prefix_match (src)) |
| 1484 | return dst; |
| 1485 | else |
| 1486 | return NULL; |
| 1487 | } |
paul | 22e0a9e | 2003-07-11 17:55:46 +0000 | [diff] [blame] | 1488 | #endif /* HAVE_IPV6 */ |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1489 | |
| 1490 | if (CMD_IPV4 (dst)) |
| 1491 | { |
| 1492 | if (cmd_ipv4_match (src)) |
| 1493 | return dst; |
| 1494 | else |
| 1495 | return NULL; |
| 1496 | } |
| 1497 | |
| 1498 | if (CMD_IPV4_PREFIX (dst)) |
| 1499 | { |
| 1500 | if (cmd_ipv4_prefix_match (src)) |
| 1501 | return dst; |
| 1502 | else |
| 1503 | return NULL; |
| 1504 | } |
| 1505 | |
| 1506 | /* Optional or variable commands always match on '?' */ |
| 1507 | if (CMD_OPTION (dst) || CMD_VARIABLE (dst)) |
| 1508 | return dst; |
| 1509 | |
| 1510 | /* In case of 'command \t', given src is NULL string. */ |
| 1511 | if (src == NULL) |
| 1512 | return dst; |
| 1513 | |
| 1514 | if (strncmp (src, dst, strlen (src)) == 0) |
| 1515 | return dst; |
| 1516 | else |
| 1517 | return NULL; |
| 1518 | } |
| 1519 | |
| 1520 | /* Check same string element existence. If it isn't there return |
| 1521 | 1. */ |
ajs | 274a4a4 | 2004-12-07 15:39:31 +0000 | [diff] [blame] | 1522 | static int |
hasso | 8c328f1 | 2004-10-05 21:01:23 +0000 | [diff] [blame] | 1523 | cmd_unique_string (vector v, const char *str) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1524 | { |
hasso | 8c328f1 | 2004-10-05 21:01:23 +0000 | [diff] [blame] | 1525 | unsigned int i; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1526 | char *match; |
| 1527 | |
paul | 55468c8 | 2005-03-14 20:19:01 +0000 | [diff] [blame] | 1528 | for (i = 0; i < vector_active (v); i++) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1529 | if ((match = vector_slot (v, i)) != NULL) |
| 1530 | if (strcmp (match, str) == 0) |
| 1531 | return 0; |
| 1532 | return 1; |
| 1533 | } |
| 1534 | |
| 1535 | /* Compare string to description vector. If there is same string |
| 1536 | return 1 else return 0. */ |
ajs | 274a4a4 | 2004-12-07 15:39:31 +0000 | [diff] [blame] | 1537 | static int |
hasso | 8c328f1 | 2004-10-05 21:01:23 +0000 | [diff] [blame] | 1538 | desc_unique_string (vector v, const char *str) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1539 | { |
hasso | 8c328f1 | 2004-10-05 21:01:23 +0000 | [diff] [blame] | 1540 | unsigned int i; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1541 | struct desc *desc; |
| 1542 | |
paul | 55468c8 | 2005-03-14 20:19:01 +0000 | [diff] [blame] | 1543 | for (i = 0; i < vector_active (v); i++) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1544 | if ((desc = vector_slot (v, i)) != NULL) |
| 1545 | if (strcmp (desc->cmd, str) == 0) |
| 1546 | return 1; |
| 1547 | return 0; |
| 1548 | } |
| 1549 | |
ajs | 274a4a4 | 2004-12-07 15:39:31 +0000 | [diff] [blame] | 1550 | static int |
paul | b92938a | 2002-12-13 21:20:42 +0000 | [diff] [blame] | 1551 | cmd_try_do_shortcut (enum node_type node, char* first_word) { |
| 1552 | if ( first_word != NULL && |
| 1553 | node != AUTH_NODE && |
| 1554 | node != VIEW_NODE && |
| 1555 | node != AUTH_ENABLE_NODE && |
| 1556 | node != ENABLE_NODE && |
| 1557 | 0 == strcmp( "do", first_word ) ) |
| 1558 | return 1; |
| 1559 | return 0; |
| 1560 | } |
| 1561 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1562 | /* '?' describe command support. */ |
ajs | 274a4a4 | 2004-12-07 15:39:31 +0000 | [diff] [blame] | 1563 | static vector |
paul | b92938a | 2002-12-13 21:20:42 +0000 | [diff] [blame] | 1564 | cmd_describe_command_real (vector vline, struct vty *vty, int *status) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1565 | { |
paul | b896147 | 2005-03-14 17:35:52 +0000 | [diff] [blame] | 1566 | unsigned int i; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1567 | vector cmd_vector; |
| 1568 | #define INIT_MATCHVEC_SIZE 10 |
| 1569 | vector matchvec; |
| 1570 | struct cmd_element *cmd_element; |
paul | b896147 | 2005-03-14 17:35:52 +0000 | [diff] [blame] | 1571 | unsigned int index; |
paul | 54aba54 | 2003-08-21 20:28:24 +0000 | [diff] [blame] | 1572 | int ret; |
| 1573 | enum match_type match; |
| 1574 | char *command; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1575 | static struct desc desc_cr = { "<cr>", "" }; |
| 1576 | |
| 1577 | /* Set index. */ |
paul | 55468c8 | 2005-03-14 20:19:01 +0000 | [diff] [blame] | 1578 | if (vector_active (vline) == 0) |
paul | b896147 | 2005-03-14 17:35:52 +0000 | [diff] [blame] | 1579 | { |
| 1580 | *status = CMD_ERR_NO_MATCH; |
| 1581 | return NULL; |
| 1582 | } |
| 1583 | else |
paul | 55468c8 | 2005-03-14 20:19:01 +0000 | [diff] [blame] | 1584 | index = vector_active (vline) - 1; |
paul | 909a215 | 2005-03-14 17:41:45 +0000 | [diff] [blame] | 1585 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1586 | /* Make copy vector of current node's command vector. */ |
| 1587 | cmd_vector = vector_copy (cmd_node_vector (cmdvec, vty->node)); |
| 1588 | |
| 1589 | /* Prepare match vector */ |
| 1590 | matchvec = vector_init (INIT_MATCHVEC_SIZE); |
| 1591 | |
| 1592 | /* Filter commands. */ |
paul | 54aba54 | 2003-08-21 20:28:24 +0000 | [diff] [blame] | 1593 | /* Only words precedes current word will be checked in this loop. */ |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1594 | for (i = 0; i < index; i++) |
paul | b896147 | 2005-03-14 17:35:52 +0000 | [diff] [blame] | 1595 | if ((command = vector_slot (vline, i))) |
paul | 909a215 | 2005-03-14 17:41:45 +0000 | [diff] [blame] | 1596 | { |
| 1597 | match = cmd_filter_by_completion (command, cmd_vector, i); |
| 1598 | |
| 1599 | if (match == vararg_match) |
| 1600 | { |
| 1601 | struct cmd_element *cmd_element; |
| 1602 | vector descvec; |
| 1603 | unsigned int j, k; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1604 | |
paul | 55468c8 | 2005-03-14 20:19:01 +0000 | [diff] [blame] | 1605 | for (j = 0; j < vector_active (cmd_vector); j++) |
paul | b896147 | 2005-03-14 17:35:52 +0000 | [diff] [blame] | 1606 | if ((cmd_element = vector_slot (cmd_vector, j)) != NULL |
paul | 55468c8 | 2005-03-14 20:19:01 +0000 | [diff] [blame] | 1607 | && (vector_active (cmd_element->strvec))) |
paul | 909a215 | 2005-03-14 17:41:45 +0000 | [diff] [blame] | 1608 | { |
| 1609 | descvec = vector_slot (cmd_element->strvec, |
paul | 55468c8 | 2005-03-14 20:19:01 +0000 | [diff] [blame] | 1610 | vector_active (cmd_element->strvec) - 1); |
| 1611 | for (k = 0; k < vector_active (descvec); k++) |
paul | 909a215 | 2005-03-14 17:41:45 +0000 | [diff] [blame] | 1612 | { |
| 1613 | struct desc *desc = vector_slot (descvec, k); |
| 1614 | vector_set (matchvec, desc); |
| 1615 | } |
| 1616 | } |
| 1617 | |
| 1618 | vector_set (matchvec, &desc_cr); |
| 1619 | vector_free (cmd_vector); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1620 | |
paul | 909a215 | 2005-03-14 17:41:45 +0000 | [diff] [blame] | 1621 | return matchvec; |
| 1622 | } |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1623 | |
paul | 909a215 | 2005-03-14 17:41:45 +0000 | [diff] [blame] | 1624 | if ((ret = is_cmd_ambiguous (command, cmd_vector, i, match)) == 1) |
| 1625 | { |
| 1626 | vector_free (cmd_vector); |
| 1627 | *status = CMD_ERR_AMBIGUOUS; |
| 1628 | return NULL; |
| 1629 | } |
| 1630 | else if (ret == 2) |
| 1631 | { |
| 1632 | vector_free (cmd_vector); |
| 1633 | *status = CMD_ERR_NO_MATCH; |
| 1634 | return NULL; |
| 1635 | } |
| 1636 | } |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1637 | |
| 1638 | /* Prepare match vector */ |
| 1639 | /* matchvec = vector_init (INIT_MATCHVEC_SIZE); */ |
| 1640 | |
paul | 54aba54 | 2003-08-21 20:28:24 +0000 | [diff] [blame] | 1641 | /* Make sure that cmd_vector is filtered based on current word */ |
| 1642 | command = vector_slot (vline, index); |
| 1643 | if (command) |
| 1644 | match = cmd_filter_by_completion (command, cmd_vector, index); |
| 1645 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1646 | /* Make description vector. */ |
paul | 55468c8 | 2005-03-14 20:19:01 +0000 | [diff] [blame] | 1647 | for (i = 0; i < vector_active (cmd_vector); i++) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1648 | if ((cmd_element = vector_slot (cmd_vector, i)) != NULL) |
| 1649 | { |
hasso | 8c328f1 | 2004-10-05 21:01:23 +0000 | [diff] [blame] | 1650 | const char *string = NULL; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1651 | vector strvec = cmd_element->strvec; |
| 1652 | |
paul | 55468c8 | 2005-03-14 20:19:01 +0000 | [diff] [blame] | 1653 | /* if command is NULL, index may be equal to vector_active */ |
| 1654 | if (command && index >= vector_active (strvec)) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1655 | vector_slot (cmd_vector, i) = NULL; |
| 1656 | else |
| 1657 | { |
paul | 54aba54 | 2003-08-21 20:28:24 +0000 | [diff] [blame] | 1658 | /* Check if command is completed. */ |
paul | 55468c8 | 2005-03-14 20:19:01 +0000 | [diff] [blame] | 1659 | if (command == NULL && index == vector_active (strvec)) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1660 | { |
| 1661 | string = "<cr>"; |
paul | 909a215 | 2005-03-14 17:41:45 +0000 | [diff] [blame] | 1662 | if (!desc_unique_string (matchvec, string)) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1663 | vector_set (matchvec, &desc_cr); |
| 1664 | } |
| 1665 | else |
| 1666 | { |
hasso | 8c328f1 | 2004-10-05 21:01:23 +0000 | [diff] [blame] | 1667 | unsigned int j; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1668 | vector descvec = vector_slot (strvec, index); |
| 1669 | struct desc *desc; |
| 1670 | |
paul | 55468c8 | 2005-03-14 20:19:01 +0000 | [diff] [blame] | 1671 | for (j = 0; j < vector_active (descvec); j++) |
paul | b896147 | 2005-03-14 17:35:52 +0000 | [diff] [blame] | 1672 | if ((desc = vector_slot (descvec, j))) |
paul | 909a215 | 2005-03-14 17:41:45 +0000 | [diff] [blame] | 1673 | { |
| 1674 | string = cmd_entry_function_desc (command, desc->cmd); |
| 1675 | if (string) |
| 1676 | { |
| 1677 | /* Uniqueness check */ |
| 1678 | if (!desc_unique_string (matchvec, string)) |
| 1679 | vector_set (matchvec, desc); |
| 1680 | } |
| 1681 | } |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1682 | } |
| 1683 | } |
| 1684 | } |
| 1685 | vector_free (cmd_vector); |
| 1686 | |
| 1687 | if (vector_slot (matchvec, 0) == NULL) |
| 1688 | { |
| 1689 | vector_free (matchvec); |
paul | 909a215 | 2005-03-14 17:41:45 +0000 | [diff] [blame] | 1690 | *status = CMD_ERR_NO_MATCH; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1691 | } |
| 1692 | else |
| 1693 | *status = CMD_SUCCESS; |
| 1694 | |
| 1695 | return matchvec; |
| 1696 | } |
| 1697 | |
paul | b92938a | 2002-12-13 21:20:42 +0000 | [diff] [blame] | 1698 | vector |
| 1699 | cmd_describe_command (vector vline, struct vty *vty, int *status) |
| 1700 | { |
| 1701 | vector ret; |
| 1702 | |
| 1703 | if ( cmd_try_do_shortcut(vty->node, vector_slot(vline, 0) ) ) |
| 1704 | { |
| 1705 | enum node_type onode; |
| 1706 | vector shifted_vline; |
hasso | 8c328f1 | 2004-10-05 21:01:23 +0000 | [diff] [blame] | 1707 | unsigned int index; |
paul | b92938a | 2002-12-13 21:20:42 +0000 | [diff] [blame] | 1708 | |
| 1709 | onode = vty->node; |
| 1710 | vty->node = ENABLE_NODE; |
| 1711 | /* We can try it on enable node, cos' the vty is authenticated */ |
| 1712 | |
| 1713 | shifted_vline = vector_init (vector_count(vline)); |
| 1714 | /* use memcpy? */ |
paul | 55468c8 | 2005-03-14 20:19:01 +0000 | [diff] [blame] | 1715 | for (index = 1; index < vector_active (vline); index++) |
paul | b92938a | 2002-12-13 21:20:42 +0000 | [diff] [blame] | 1716 | { |
| 1717 | vector_set_index (shifted_vline, index-1, vector_lookup(vline, index)); |
| 1718 | } |
| 1719 | |
| 1720 | ret = cmd_describe_command_real (shifted_vline, vty, status); |
| 1721 | |
| 1722 | vector_free(shifted_vline); |
| 1723 | vty->node = onode; |
| 1724 | return ret; |
| 1725 | } |
| 1726 | |
| 1727 | |
| 1728 | return cmd_describe_command_real (vline, vty, status); |
| 1729 | } |
| 1730 | |
| 1731 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1732 | /* Check LCD of matched command. */ |
ajs | 274a4a4 | 2004-12-07 15:39:31 +0000 | [diff] [blame] | 1733 | static int |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1734 | cmd_lcd (char **matched) |
| 1735 | { |
| 1736 | int i; |
| 1737 | int j; |
| 1738 | int lcd = -1; |
| 1739 | char *s1, *s2; |
| 1740 | char c1, c2; |
| 1741 | |
| 1742 | if (matched[0] == NULL || matched[1] == NULL) |
| 1743 | return 0; |
| 1744 | |
| 1745 | for (i = 1; matched[i] != NULL; i++) |
| 1746 | { |
| 1747 | s1 = matched[i - 1]; |
| 1748 | s2 = matched[i]; |
| 1749 | |
| 1750 | for (j = 0; (c1 = s1[j]) && (c2 = s2[j]); j++) |
| 1751 | if (c1 != c2) |
| 1752 | break; |
| 1753 | |
| 1754 | if (lcd < 0) |
| 1755 | lcd = j; |
| 1756 | else |
| 1757 | { |
| 1758 | if (lcd > j) |
| 1759 | lcd = j; |
| 1760 | } |
| 1761 | } |
| 1762 | return lcd; |
| 1763 | } |
| 1764 | |
| 1765 | /* Command line completion support. */ |
ajs | 274a4a4 | 2004-12-07 15:39:31 +0000 | [diff] [blame] | 1766 | static char ** |
paul | b92938a | 2002-12-13 21:20:42 +0000 | [diff] [blame] | 1767 | cmd_complete_command_real (vector vline, struct vty *vty, int *status) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1768 | { |
paul | b896147 | 2005-03-14 17:35:52 +0000 | [diff] [blame] | 1769 | unsigned int i; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1770 | vector cmd_vector = vector_copy (cmd_node_vector (cmdvec, vty->node)); |
| 1771 | #define INIT_MATCHVEC_SIZE 10 |
| 1772 | vector matchvec; |
| 1773 | struct cmd_element *cmd_element; |
paul | b896147 | 2005-03-14 17:35:52 +0000 | [diff] [blame] | 1774 | unsigned int index; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1775 | char **match_str; |
| 1776 | struct desc *desc; |
| 1777 | vector descvec; |
| 1778 | char *command; |
| 1779 | int lcd; |
| 1780 | |
paul | 55468c8 | 2005-03-14 20:19:01 +0000 | [diff] [blame] | 1781 | if (vector_active (vline) == 0) |
paul | b896147 | 2005-03-14 17:35:52 +0000 | [diff] [blame] | 1782 | { |
| 1783 | *status = CMD_ERR_NO_MATCH; |
| 1784 | return NULL; |
| 1785 | } |
| 1786 | else |
paul | 55468c8 | 2005-03-14 20:19:01 +0000 | [diff] [blame] | 1787 | index = vector_active (vline) - 1; |
paul | b896147 | 2005-03-14 17:35:52 +0000 | [diff] [blame] | 1788 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1789 | /* First, filter by preceeding command string */ |
| 1790 | for (i = 0; i < index; i++) |
paul | b896147 | 2005-03-14 17:35:52 +0000 | [diff] [blame] | 1791 | if ((command = vector_slot (vline, i))) |
paul | 909a215 | 2005-03-14 17:41:45 +0000 | [diff] [blame] | 1792 | { |
| 1793 | enum match_type match; |
| 1794 | int ret; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1795 | |
paul | 909a215 | 2005-03-14 17:41:45 +0000 | [diff] [blame] | 1796 | /* First try completion match, if there is exactly match return 1 */ |
| 1797 | match = cmd_filter_by_completion (command, cmd_vector, i); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1798 | |
paul | 909a215 | 2005-03-14 17:41:45 +0000 | [diff] [blame] | 1799 | /* If there is exact match then filter ambiguous match else check |
| 1800 | ambiguousness. */ |
| 1801 | if ((ret = is_cmd_ambiguous (command, cmd_vector, i, match)) == 1) |
| 1802 | { |
| 1803 | vector_free (cmd_vector); |
| 1804 | *status = CMD_ERR_AMBIGUOUS; |
| 1805 | return NULL; |
| 1806 | } |
| 1807 | /* |
| 1808 | else if (ret == 2) |
| 1809 | { |
| 1810 | vector_free (cmd_vector); |
| 1811 | *status = CMD_ERR_NO_MATCH; |
| 1812 | return NULL; |
| 1813 | } |
| 1814 | */ |
| 1815 | } |
| 1816 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1817 | /* Prepare match vector. */ |
| 1818 | matchvec = vector_init (INIT_MATCHVEC_SIZE); |
| 1819 | |
| 1820 | /* Now we got into completion */ |
paul | 55468c8 | 2005-03-14 20:19:01 +0000 | [diff] [blame] | 1821 | for (i = 0; i < vector_active (cmd_vector); i++) |
paul | b896147 | 2005-03-14 17:35:52 +0000 | [diff] [blame] | 1822 | if ((cmd_element = vector_slot (cmd_vector, i))) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1823 | { |
hasso | 8c328f1 | 2004-10-05 21:01:23 +0000 | [diff] [blame] | 1824 | const char *string; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1825 | vector strvec = cmd_element->strvec; |
paul | 909a215 | 2005-03-14 17:41:45 +0000 | [diff] [blame] | 1826 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1827 | /* Check field length */ |
paul | 55468c8 | 2005-03-14 20:19:01 +0000 | [diff] [blame] | 1828 | if (index >= vector_active (strvec)) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1829 | vector_slot (cmd_vector, i) = NULL; |
paul | 909a215 | 2005-03-14 17:41:45 +0000 | [diff] [blame] | 1830 | else |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1831 | { |
hasso | 8c328f1 | 2004-10-05 21:01:23 +0000 | [diff] [blame] | 1832 | unsigned int j; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1833 | |
| 1834 | descvec = vector_slot (strvec, index); |
paul | 55468c8 | 2005-03-14 20:19:01 +0000 | [diff] [blame] | 1835 | for (j = 0; j < vector_active (descvec); j++) |
paul | b896147 | 2005-03-14 17:35:52 +0000 | [diff] [blame] | 1836 | if ((desc = vector_slot (descvec, j))) |
paul | 909a215 | 2005-03-14 17:41:45 +0000 | [diff] [blame] | 1837 | { |
paul | b896147 | 2005-03-14 17:35:52 +0000 | [diff] [blame] | 1838 | if ((string = |
| 1839 | cmd_entry_function (vector_slot (vline, index), |
paul | 909a215 | 2005-03-14 17:41:45 +0000 | [diff] [blame] | 1840 | desc->cmd))) |
| 1841 | if (cmd_unique_string (matchvec, string)) |
| 1842 | vector_set (matchvec, XSTRDUP (MTYPE_TMP, string)); |
| 1843 | } |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1844 | } |
| 1845 | } |
| 1846 | |
| 1847 | /* We don't need cmd_vector any more. */ |
| 1848 | vector_free (cmd_vector); |
| 1849 | |
| 1850 | /* No matched command */ |
| 1851 | if (vector_slot (matchvec, 0) == NULL) |
| 1852 | { |
| 1853 | vector_free (matchvec); |
| 1854 | |
| 1855 | /* In case of 'command \t' pattern. Do you need '?' command at |
| 1856 | the end of the line. */ |
| 1857 | if (vector_slot (vline, index) == '\0') |
| 1858 | *status = CMD_ERR_NOTHING_TODO; |
| 1859 | else |
| 1860 | *status = CMD_ERR_NO_MATCH; |
| 1861 | return NULL; |
| 1862 | } |
| 1863 | |
| 1864 | /* Only one matched */ |
| 1865 | if (vector_slot (matchvec, 1) == NULL) |
| 1866 | { |
| 1867 | match_str = (char **) matchvec->index; |
| 1868 | vector_only_wrapper_free (matchvec); |
| 1869 | *status = CMD_COMPLETE_FULL_MATCH; |
| 1870 | return match_str; |
| 1871 | } |
| 1872 | /* Make it sure last element is NULL. */ |
| 1873 | vector_set (matchvec, NULL); |
| 1874 | |
| 1875 | /* Check LCD of matched strings. */ |
| 1876 | if (vector_slot (vline, index) != NULL) |
| 1877 | { |
| 1878 | lcd = cmd_lcd ((char **) matchvec->index); |
| 1879 | |
| 1880 | if (lcd) |
| 1881 | { |
| 1882 | int len = strlen (vector_slot (vline, index)); |
paul | 909a215 | 2005-03-14 17:41:45 +0000 | [diff] [blame] | 1883 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1884 | if (len < lcd) |
| 1885 | { |
| 1886 | char *lcdstr; |
paul | 909a215 | 2005-03-14 17:41:45 +0000 | [diff] [blame] | 1887 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1888 | lcdstr = XMALLOC (MTYPE_TMP, lcd + 1); |
| 1889 | memcpy (lcdstr, matchvec->index[0], lcd); |
| 1890 | lcdstr[lcd] = '\0'; |
| 1891 | |
| 1892 | /* match_str = (char **) &lcdstr; */ |
| 1893 | |
| 1894 | /* Free matchvec. */ |
paul | 55468c8 | 2005-03-14 20:19:01 +0000 | [diff] [blame] | 1895 | for (i = 0; i < vector_active (matchvec); i++) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1896 | { |
| 1897 | if (vector_slot (matchvec, i)) |
| 1898 | XFREE (MTYPE_TMP, vector_slot (matchvec, i)); |
| 1899 | } |
| 1900 | vector_free (matchvec); |
| 1901 | |
paul | 909a215 | 2005-03-14 17:41:45 +0000 | [diff] [blame] | 1902 | /* Make new matchvec. */ |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1903 | matchvec = vector_init (INIT_MATCHVEC_SIZE); |
| 1904 | vector_set (matchvec, lcdstr); |
| 1905 | match_str = (char **) matchvec->index; |
| 1906 | vector_only_wrapper_free (matchvec); |
| 1907 | |
| 1908 | *status = CMD_COMPLETE_MATCH; |
| 1909 | return match_str; |
| 1910 | } |
| 1911 | } |
| 1912 | } |
| 1913 | |
| 1914 | match_str = (char **) matchvec->index; |
| 1915 | vector_only_wrapper_free (matchvec); |
| 1916 | *status = CMD_COMPLETE_LIST_MATCH; |
| 1917 | return match_str; |
| 1918 | } |
| 1919 | |
paul | b92938a | 2002-12-13 21:20:42 +0000 | [diff] [blame] | 1920 | char ** |
paul | 9ab6812 | 2003-01-18 01:16:20 +0000 | [diff] [blame] | 1921 | cmd_complete_command (vector vline, struct vty *vty, int *status) |
paul | b92938a | 2002-12-13 21:20:42 +0000 | [diff] [blame] | 1922 | { |
| 1923 | char **ret; |
| 1924 | |
| 1925 | if ( cmd_try_do_shortcut(vty->node, vector_slot(vline, 0) ) ) |
| 1926 | { |
| 1927 | enum node_type onode; |
| 1928 | vector shifted_vline; |
hasso | 8c328f1 | 2004-10-05 21:01:23 +0000 | [diff] [blame] | 1929 | unsigned int index; |
paul | b92938a | 2002-12-13 21:20:42 +0000 | [diff] [blame] | 1930 | |
| 1931 | onode = vty->node; |
| 1932 | vty->node = ENABLE_NODE; |
| 1933 | /* We can try it on enable node, cos' the vty is authenticated */ |
| 1934 | |
| 1935 | shifted_vline = vector_init (vector_count(vline)); |
| 1936 | /* use memcpy? */ |
paul | 55468c8 | 2005-03-14 20:19:01 +0000 | [diff] [blame] | 1937 | for (index = 1; index < vector_active (vline); index++) |
paul | b92938a | 2002-12-13 21:20:42 +0000 | [diff] [blame] | 1938 | { |
| 1939 | vector_set_index (shifted_vline, index-1, vector_lookup(vline, index)); |
| 1940 | } |
| 1941 | |
| 1942 | ret = cmd_complete_command_real (shifted_vline, vty, status); |
| 1943 | |
| 1944 | vector_free(shifted_vline); |
| 1945 | vty->node = onode; |
| 1946 | return ret; |
| 1947 | } |
| 1948 | |
| 1949 | |
| 1950 | return cmd_complete_command_real (vline, vty, status); |
| 1951 | } |
| 1952 | |
| 1953 | /* return parent node */ |
| 1954 | /* MUST eventually converge on CONFIG_NODE */ |
hasso | 13bfca7 | 2005-01-23 21:42:25 +0000 | [diff] [blame] | 1955 | enum node_type |
ajs | 274a4a4 | 2004-12-07 15:39:31 +0000 | [diff] [blame] | 1956 | node_parent ( enum node_type node ) |
paul | b92938a | 2002-12-13 21:20:42 +0000 | [diff] [blame] | 1957 | { |
| 1958 | enum node_type ret; |
| 1959 | |
paul | 9ab6812 | 2003-01-18 01:16:20 +0000 | [diff] [blame] | 1960 | assert (node > CONFIG_NODE); |
| 1961 | |
| 1962 | switch (node) |
| 1963 | { |
| 1964 | case BGP_VPNV4_NODE: |
| 1965 | case BGP_IPV4_NODE: |
| 1966 | case BGP_IPV4M_NODE: |
| 1967 | case BGP_IPV6_NODE: |
| 1968 | ret = BGP_NODE; |
| 1969 | break; |
| 1970 | case KEYCHAIN_KEY_NODE: |
| 1971 | ret = KEYCHAIN_NODE; |
| 1972 | break; |
| 1973 | default: |
| 1974 | ret = CONFIG_NODE; |
paul | b92938a | 2002-12-13 21:20:42 +0000 | [diff] [blame] | 1975 | } |
| 1976 | |
| 1977 | return ret; |
| 1978 | } |
| 1979 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1980 | /* Execute command by argument vline vector. */ |
ajs | 274a4a4 | 2004-12-07 15:39:31 +0000 | [diff] [blame] | 1981 | static int |
paul | b896147 | 2005-03-14 17:35:52 +0000 | [diff] [blame] | 1982 | cmd_execute_command_real (vector vline, struct vty *vty, |
| 1983 | struct cmd_element **cmd) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1984 | { |
hasso | 8c328f1 | 2004-10-05 21:01:23 +0000 | [diff] [blame] | 1985 | unsigned int i; |
| 1986 | unsigned int index; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1987 | vector cmd_vector; |
| 1988 | struct cmd_element *cmd_element; |
| 1989 | struct cmd_element *matched_element; |
| 1990 | unsigned int matched_count, incomplete_count; |
| 1991 | int argc; |
paul | 9035efa | 2004-10-10 11:56:56 +0000 | [diff] [blame] | 1992 | const char *argv[CMD_ARGC_MAX]; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1993 | enum match_type match = 0; |
| 1994 | int varflag; |
| 1995 | char *command; |
| 1996 | |
| 1997 | /* Make copy of command elements. */ |
| 1998 | cmd_vector = vector_copy (cmd_node_vector (cmdvec, vty->node)); |
| 1999 | |
paul | 55468c8 | 2005-03-14 20:19:01 +0000 | [diff] [blame] | 2000 | for (index = 0; index < vector_active (vline); index++) |
paul | b896147 | 2005-03-14 17:35:52 +0000 | [diff] [blame] | 2001 | if ((command = vector_slot (vline, index))) |
paul | 909a215 | 2005-03-14 17:41:45 +0000 | [diff] [blame] | 2002 | { |
| 2003 | int ret; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 2004 | |
paul | 909a215 | 2005-03-14 17:41:45 +0000 | [diff] [blame] | 2005 | match = cmd_filter_by_completion (command, cmd_vector, index); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 2006 | |
paul | 909a215 | 2005-03-14 17:41:45 +0000 | [diff] [blame] | 2007 | if (match == vararg_match) |
| 2008 | break; |
| 2009 | |
| 2010 | ret = is_cmd_ambiguous (command, cmd_vector, index, match); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 2011 | |
paul | 909a215 | 2005-03-14 17:41:45 +0000 | [diff] [blame] | 2012 | if (ret == 1) |
| 2013 | { |
| 2014 | vector_free (cmd_vector); |
| 2015 | return CMD_ERR_AMBIGUOUS; |
| 2016 | } |
| 2017 | else if (ret == 2) |
| 2018 | { |
| 2019 | vector_free (cmd_vector); |
| 2020 | return CMD_ERR_NO_MATCH; |
| 2021 | } |
| 2022 | } |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 2023 | |
| 2024 | /* Check matched count. */ |
| 2025 | matched_element = NULL; |
| 2026 | matched_count = 0; |
| 2027 | incomplete_count = 0; |
| 2028 | |
paul | 55468c8 | 2005-03-14 20:19:01 +0000 | [diff] [blame] | 2029 | for (i = 0; i < vector_active (cmd_vector); i++) |
paul | b896147 | 2005-03-14 17:35:52 +0000 | [diff] [blame] | 2030 | if ((cmd_element = vector_slot (cmd_vector, i))) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 2031 | { |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 2032 | if (match == vararg_match || index >= cmd_element->cmdsize) |
| 2033 | { |
| 2034 | matched_element = cmd_element; |
| 2035 | #if 0 |
| 2036 | printf ("DEBUG: %s\n", cmd_element->string); |
| 2037 | #endif |
| 2038 | matched_count++; |
| 2039 | } |
| 2040 | else |
| 2041 | { |
| 2042 | incomplete_count++; |
| 2043 | } |
| 2044 | } |
paul | 909a215 | 2005-03-14 17:41:45 +0000 | [diff] [blame] | 2045 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 2046 | /* Finish of using cmd_vector. */ |
| 2047 | vector_free (cmd_vector); |
| 2048 | |
paul | 909a215 | 2005-03-14 17:41:45 +0000 | [diff] [blame] | 2049 | /* To execute command, matched_count must be 1. */ |
| 2050 | if (matched_count == 0) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 2051 | { |
| 2052 | if (incomplete_count) |
| 2053 | return CMD_ERR_INCOMPLETE; |
| 2054 | else |
| 2055 | return CMD_ERR_NO_MATCH; |
| 2056 | } |
| 2057 | |
paul | 909a215 | 2005-03-14 17:41:45 +0000 | [diff] [blame] | 2058 | if (matched_count > 1) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 2059 | return CMD_ERR_AMBIGUOUS; |
| 2060 | |
| 2061 | /* Argument treatment */ |
| 2062 | varflag = 0; |
| 2063 | argc = 0; |
| 2064 | |
paul | 55468c8 | 2005-03-14 20:19:01 +0000 | [diff] [blame] | 2065 | for (i = 0; i < vector_active (vline); i++) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 2066 | { |
| 2067 | if (varflag) |
| 2068 | argv[argc++] = vector_slot (vline, i); |
| 2069 | else |
paul | 909a215 | 2005-03-14 17:41:45 +0000 | [diff] [blame] | 2070 | { |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 2071 | vector descvec = vector_slot (matched_element->strvec, i); |
| 2072 | |
paul | 55468c8 | 2005-03-14 20:19:01 +0000 | [diff] [blame] | 2073 | if (vector_active (descvec) == 1) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 2074 | { |
| 2075 | struct desc *desc = vector_slot (descvec, 0); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 2076 | |
hasso | 8c328f1 | 2004-10-05 21:01:23 +0000 | [diff] [blame] | 2077 | if (CMD_VARARG (desc->cmd)) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 2078 | varflag = 1; |
| 2079 | |
hasso | 8c328f1 | 2004-10-05 21:01:23 +0000 | [diff] [blame] | 2080 | if (varflag || CMD_VARIABLE (desc->cmd) || CMD_OPTION (desc->cmd)) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 2081 | argv[argc++] = vector_slot (vline, i); |
| 2082 | } |
| 2083 | else |
| 2084 | argv[argc++] = vector_slot (vline, i); |
| 2085 | } |
| 2086 | |
| 2087 | if (argc >= CMD_ARGC_MAX) |
| 2088 | return CMD_ERR_EXEED_ARGC_MAX; |
| 2089 | } |
| 2090 | |
| 2091 | /* For vtysh execution. */ |
| 2092 | if (cmd) |
| 2093 | *cmd = matched_element; |
| 2094 | |
| 2095 | if (matched_element->daemon) |
| 2096 | return CMD_SUCCESS_DAEMON; |
| 2097 | |
| 2098 | /* Execute matched command. */ |
| 2099 | return (*matched_element->func) (matched_element, vty, argc, argv); |
| 2100 | } |
| 2101 | |
paul | b92938a | 2002-12-13 21:20:42 +0000 | [diff] [blame] | 2102 | int |
hasso | 87d683b | 2005-01-16 23:31:54 +0000 | [diff] [blame] | 2103 | cmd_execute_command (vector vline, struct vty *vty, struct cmd_element **cmd, |
| 2104 | int vtysh) { |
paul | 9ab6812 | 2003-01-18 01:16:20 +0000 | [diff] [blame] | 2105 | int ret, saved_ret, tried = 0; |
| 2106 | enum node_type onode, try_node; |
| 2107 | |
| 2108 | onode = try_node = vty->node; |
paul | b92938a | 2002-12-13 21:20:42 +0000 | [diff] [blame] | 2109 | |
| 2110 | if ( cmd_try_do_shortcut(vty->node, vector_slot(vline, 0) ) ) |
| 2111 | { |
| 2112 | vector shifted_vline; |
hasso | 8c328f1 | 2004-10-05 21:01:23 +0000 | [diff] [blame] | 2113 | unsigned int index; |
paul | b92938a | 2002-12-13 21:20:42 +0000 | [diff] [blame] | 2114 | |
| 2115 | vty->node = ENABLE_NODE; |
| 2116 | /* We can try it on enable node, cos' the vty is authenticated */ |
| 2117 | |
| 2118 | shifted_vline = vector_init (vector_count(vline)); |
| 2119 | /* use memcpy? */ |
paul | 55468c8 | 2005-03-14 20:19:01 +0000 | [diff] [blame] | 2120 | for (index = 1; index < vector_active (vline); index++) |
paul | b92938a | 2002-12-13 21:20:42 +0000 | [diff] [blame] | 2121 | { |
| 2122 | vector_set_index (shifted_vline, index-1, vector_lookup(vline, index)); |
| 2123 | } |
| 2124 | |
| 2125 | ret = cmd_execute_command_real (shifted_vline, vty, cmd); |
| 2126 | |
| 2127 | vector_free(shifted_vline); |
| 2128 | vty->node = onode; |
| 2129 | return ret; |
| 2130 | } |
| 2131 | |
| 2132 | |
paul | 9ab6812 | 2003-01-18 01:16:20 +0000 | [diff] [blame] | 2133 | saved_ret = ret = cmd_execute_command_real (vline, vty, cmd); |
paul | b92938a | 2002-12-13 21:20:42 +0000 | [diff] [blame] | 2134 | |
hasso | 87d683b | 2005-01-16 23:31:54 +0000 | [diff] [blame] | 2135 | if (vtysh) |
| 2136 | return saved_ret; |
| 2137 | |
paul | b92938a | 2002-12-13 21:20:42 +0000 | [diff] [blame] | 2138 | /* This assumes all nodes above CONFIG_NODE are childs of CONFIG_NODE */ |
paul | 9ab6812 | 2003-01-18 01:16:20 +0000 | [diff] [blame] | 2139 | while ( ret != CMD_SUCCESS && ret != CMD_WARNING |
paul | b92938a | 2002-12-13 21:20:42 +0000 | [diff] [blame] | 2140 | && vty->node > CONFIG_NODE ) |
| 2141 | { |
paul | 9ab6812 | 2003-01-18 01:16:20 +0000 | [diff] [blame] | 2142 | try_node = node_parent(try_node); |
| 2143 | vty->node = try_node; |
paul | b92938a | 2002-12-13 21:20:42 +0000 | [diff] [blame] | 2144 | ret = cmd_execute_command_real (vline, vty, cmd); |
paul | 9ab6812 | 2003-01-18 01:16:20 +0000 | [diff] [blame] | 2145 | tried = 1; |
| 2146 | if (ret == CMD_SUCCESS || ret == CMD_WARNING) |
paul | b92938a | 2002-12-13 21:20:42 +0000 | [diff] [blame] | 2147 | { |
paul | 9ab6812 | 2003-01-18 01:16:20 +0000 | [diff] [blame] | 2148 | /* succesfull command, leave the node as is */ |
paul | b92938a | 2002-12-13 21:20:42 +0000 | [diff] [blame] | 2149 | return ret; |
| 2150 | } |
paul | b92938a | 2002-12-13 21:20:42 +0000 | [diff] [blame] | 2151 | } |
paul | 9ab6812 | 2003-01-18 01:16:20 +0000 | [diff] [blame] | 2152 | /* no command succeeded, reset the vty to the original node and |
| 2153 | return the error for this node */ |
| 2154 | if ( tried ) |
| 2155 | vty->node = onode; |
| 2156 | return saved_ret; |
paul | eda031f | 2003-01-18 00:39:19 +0000 | [diff] [blame] | 2157 | } |
| 2158 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 2159 | /* Execute command by argument readline. */ |
| 2160 | int |
paul | 909a215 | 2005-03-14 17:41:45 +0000 | [diff] [blame] | 2161 | cmd_execute_command_strict (vector vline, struct vty *vty, |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 2162 | struct cmd_element **cmd) |
| 2163 | { |
hasso | 8c328f1 | 2004-10-05 21:01:23 +0000 | [diff] [blame] | 2164 | unsigned int i; |
| 2165 | unsigned int index; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 2166 | vector cmd_vector; |
| 2167 | struct cmd_element *cmd_element; |
| 2168 | struct cmd_element *matched_element; |
| 2169 | unsigned int matched_count, incomplete_count; |
| 2170 | int argc; |
paul | 9035efa | 2004-10-10 11:56:56 +0000 | [diff] [blame] | 2171 | const char *argv[CMD_ARGC_MAX]; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 2172 | int varflag; |
| 2173 | enum match_type match = 0; |
| 2174 | char *command; |
| 2175 | |
| 2176 | /* Make copy of command element */ |
| 2177 | cmd_vector = vector_copy (cmd_node_vector (cmdvec, vty->node)); |
| 2178 | |
paul | 55468c8 | 2005-03-14 20:19:01 +0000 | [diff] [blame] | 2179 | for (index = 0; index < vector_active (vline); index++) |
paul | b896147 | 2005-03-14 17:35:52 +0000 | [diff] [blame] | 2180 | if ((command = vector_slot (vline, index))) |
paul | 909a215 | 2005-03-14 17:41:45 +0000 | [diff] [blame] | 2181 | { |
| 2182 | int ret; |
| 2183 | |
| 2184 | match = cmd_filter_by_string (vector_slot (vline, index), |
| 2185 | cmd_vector, index); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 2186 | |
paul | 909a215 | 2005-03-14 17:41:45 +0000 | [diff] [blame] | 2187 | /* If command meets '.VARARG' then finish matching. */ |
| 2188 | if (match == vararg_match) |
| 2189 | break; |
| 2190 | |
| 2191 | ret = is_cmd_ambiguous (command, cmd_vector, index, match); |
| 2192 | if (ret == 1) |
| 2193 | { |
| 2194 | vector_free (cmd_vector); |
| 2195 | return CMD_ERR_AMBIGUOUS; |
| 2196 | } |
| 2197 | if (ret == 2) |
| 2198 | { |
| 2199 | vector_free (cmd_vector); |
| 2200 | return CMD_ERR_NO_MATCH; |
| 2201 | } |
| 2202 | } |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 2203 | |
| 2204 | /* Check matched count. */ |
| 2205 | matched_element = NULL; |
| 2206 | matched_count = 0; |
| 2207 | incomplete_count = 0; |
paul | 55468c8 | 2005-03-14 20:19:01 +0000 | [diff] [blame] | 2208 | for (i = 0; i < vector_active (cmd_vector); i++) |
paul | 909a215 | 2005-03-14 17:41:45 +0000 | [diff] [blame] | 2209 | if (vector_slot (cmd_vector, i) != NULL) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 2210 | { |
paul | 909a215 | 2005-03-14 17:41:45 +0000 | [diff] [blame] | 2211 | cmd_element = vector_slot (cmd_vector, i); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 2212 | |
| 2213 | if (match == vararg_match || index >= cmd_element->cmdsize) |
| 2214 | { |
| 2215 | matched_element = cmd_element; |
| 2216 | matched_count++; |
| 2217 | } |
| 2218 | else |
| 2219 | incomplete_count++; |
| 2220 | } |
paul | 909a215 | 2005-03-14 17:41:45 +0000 | [diff] [blame] | 2221 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 2222 | /* Finish of using cmd_vector. */ |
| 2223 | vector_free (cmd_vector); |
| 2224 | |
paul | 909a215 | 2005-03-14 17:41:45 +0000 | [diff] [blame] | 2225 | /* To execute command, matched_count must be 1. */ |
| 2226 | if (matched_count == 0) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 2227 | { |
| 2228 | if (incomplete_count) |
| 2229 | return CMD_ERR_INCOMPLETE; |
| 2230 | else |
| 2231 | return CMD_ERR_NO_MATCH; |
| 2232 | } |
| 2233 | |
paul | 909a215 | 2005-03-14 17:41:45 +0000 | [diff] [blame] | 2234 | if (matched_count > 1) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 2235 | return CMD_ERR_AMBIGUOUS; |
| 2236 | |
| 2237 | /* Argument treatment */ |
| 2238 | varflag = 0; |
| 2239 | argc = 0; |
| 2240 | |
paul | 55468c8 | 2005-03-14 20:19:01 +0000 | [diff] [blame] | 2241 | for (i = 0; i < vector_active (vline); i++) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 2242 | { |
| 2243 | if (varflag) |
| 2244 | argv[argc++] = vector_slot (vline, i); |
| 2245 | else |
paul | 909a215 | 2005-03-14 17:41:45 +0000 | [diff] [blame] | 2246 | { |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 2247 | vector descvec = vector_slot (matched_element->strvec, i); |
| 2248 | |
paul | 55468c8 | 2005-03-14 20:19:01 +0000 | [diff] [blame] | 2249 | if (vector_active (descvec) == 1) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 2250 | { |
| 2251 | struct desc *desc = vector_slot (descvec, 0); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 2252 | |
hasso | 8c328f1 | 2004-10-05 21:01:23 +0000 | [diff] [blame] | 2253 | if (CMD_VARARG (desc->cmd)) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 2254 | varflag = 1; |
paul | 909a215 | 2005-03-14 17:41:45 +0000 | [diff] [blame] | 2255 | |
hasso | 8c328f1 | 2004-10-05 21:01:23 +0000 | [diff] [blame] | 2256 | if (varflag || CMD_VARIABLE (desc->cmd) || CMD_OPTION (desc->cmd)) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 2257 | argv[argc++] = vector_slot (vline, i); |
| 2258 | } |
| 2259 | else |
| 2260 | argv[argc++] = vector_slot (vline, i); |
| 2261 | } |
| 2262 | |
| 2263 | if (argc >= CMD_ARGC_MAX) |
| 2264 | return CMD_ERR_EXEED_ARGC_MAX; |
| 2265 | } |
| 2266 | |
| 2267 | /* For vtysh execution. */ |
| 2268 | if (cmd) |
| 2269 | *cmd = matched_element; |
| 2270 | |
| 2271 | if (matched_element->daemon) |
| 2272 | return CMD_SUCCESS_DAEMON; |
| 2273 | |
| 2274 | /* Now execute matched command */ |
| 2275 | return (*matched_element->func) (matched_element, vty, argc, argv); |
| 2276 | } |
| 2277 | |
| 2278 | /* Configration make from file. */ |
| 2279 | int |
| 2280 | config_from_file (struct vty *vty, FILE *fp) |
| 2281 | { |
| 2282 | int ret; |
| 2283 | vector vline; |
| 2284 | |
| 2285 | while (fgets (vty->buf, VTY_BUFSIZ, fp)) |
| 2286 | { |
| 2287 | vline = cmd_make_strvec (vty->buf); |
| 2288 | |
| 2289 | /* In case of comment line */ |
| 2290 | if (vline == NULL) |
| 2291 | continue; |
| 2292 | /* Execute configuration command : this is strict match */ |
| 2293 | ret = cmd_execute_command_strict (vline, vty, NULL); |
| 2294 | |
| 2295 | /* Try again with setting node to CONFIG_NODE */ |
paul | b92938a | 2002-12-13 21:20:42 +0000 | [diff] [blame] | 2296 | while (ret != CMD_SUCCESS && ret != CMD_WARNING |
hasso | ddd85ed | 2004-10-13 08:18:07 +0000 | [diff] [blame] | 2297 | && ret != CMD_ERR_NOTHING_TODO && vty->node != CONFIG_NODE) |
| 2298 | { |
paul | b92938a | 2002-12-13 21:20:42 +0000 | [diff] [blame] | 2299 | vty->node = node_parent(vty->node); |
hasso | ddd85ed | 2004-10-13 08:18:07 +0000 | [diff] [blame] | 2300 | ret = cmd_execute_command_strict (vline, vty, NULL); |
| 2301 | } |
paul | 9ab6812 | 2003-01-18 01:16:20 +0000 | [diff] [blame] | 2302 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 2303 | cmd_free_strvec (vline); |
| 2304 | |
hasso | ddd85ed | 2004-10-13 08:18:07 +0000 | [diff] [blame] | 2305 | if (ret != CMD_SUCCESS && ret != CMD_WARNING |
| 2306 | && ret != CMD_ERR_NOTHING_TODO) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 2307 | return ret; |
| 2308 | } |
| 2309 | return CMD_SUCCESS; |
| 2310 | } |
| 2311 | |
| 2312 | /* Configration from terminal */ |
| 2313 | DEFUN (config_terminal, |
| 2314 | config_terminal_cmd, |
| 2315 | "configure terminal", |
| 2316 | "Configuration from vty interface\n" |
| 2317 | "Configuration terminal\n") |
| 2318 | { |
| 2319 | if (vty_config_lock (vty)) |
| 2320 | vty->node = CONFIG_NODE; |
| 2321 | else |
| 2322 | { |
| 2323 | vty_out (vty, "VTY configuration is locked by other VTY%s", VTY_NEWLINE); |
| 2324 | return CMD_WARNING; |
| 2325 | } |
| 2326 | return CMD_SUCCESS; |
| 2327 | } |
| 2328 | |
| 2329 | /* Enable command */ |
| 2330 | DEFUN (enable, |
| 2331 | config_enable_cmd, |
| 2332 | "enable", |
| 2333 | "Turn on privileged mode command\n") |
| 2334 | { |
| 2335 | /* If enable password is NULL, change to ENABLE_NODE */ |
| 2336 | if ((host.enable == NULL && host.enable_encrypt == NULL) || |
| 2337 | vty->type == VTY_SHELL_SERV) |
| 2338 | vty->node = ENABLE_NODE; |
| 2339 | else |
| 2340 | vty->node = AUTH_ENABLE_NODE; |
| 2341 | |
| 2342 | return CMD_SUCCESS; |
| 2343 | } |
| 2344 | |
| 2345 | /* Disable command */ |
| 2346 | DEFUN (disable, |
| 2347 | config_disable_cmd, |
| 2348 | "disable", |
| 2349 | "Turn off privileged mode command\n") |
| 2350 | { |
| 2351 | if (vty->node == ENABLE_NODE) |
| 2352 | vty->node = VIEW_NODE; |
| 2353 | return CMD_SUCCESS; |
| 2354 | } |
| 2355 | |
| 2356 | /* Down vty node level. */ |
| 2357 | DEFUN (config_exit, |
| 2358 | config_exit_cmd, |
| 2359 | "exit", |
| 2360 | "Exit current mode and down to previous mode\n") |
| 2361 | { |
| 2362 | switch (vty->node) |
| 2363 | { |
| 2364 | case VIEW_NODE: |
| 2365 | case ENABLE_NODE: |
| 2366 | if (vty_shell (vty)) |
| 2367 | exit (0); |
| 2368 | else |
| 2369 | vty->status = VTY_CLOSE; |
| 2370 | break; |
| 2371 | case CONFIG_NODE: |
| 2372 | vty->node = ENABLE_NODE; |
| 2373 | vty_config_unlock (vty); |
| 2374 | break; |
| 2375 | case INTERFACE_NODE: |
| 2376 | case ZEBRA_NODE: |
| 2377 | case BGP_NODE: |
| 2378 | case RIP_NODE: |
| 2379 | case RIPNG_NODE: |
| 2380 | case OSPF_NODE: |
| 2381 | case OSPF6_NODE: |
jardin | 9e867fe | 2003-12-23 08:56:18 +0000 | [diff] [blame] | 2382 | case ISIS_NODE: |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 2383 | case KEYCHAIN_NODE: |
| 2384 | case MASC_NODE: |
| 2385 | case RMAP_NODE: |
| 2386 | case VTY_NODE: |
| 2387 | vty->node = CONFIG_NODE; |
| 2388 | break; |
| 2389 | case BGP_VPNV4_NODE: |
| 2390 | case BGP_IPV4_NODE: |
| 2391 | case BGP_IPV4M_NODE: |
| 2392 | case BGP_IPV6_NODE: |
| 2393 | vty->node = BGP_NODE; |
| 2394 | break; |
| 2395 | case KEYCHAIN_KEY_NODE: |
| 2396 | vty->node = KEYCHAIN_NODE; |
| 2397 | break; |
| 2398 | default: |
| 2399 | break; |
| 2400 | } |
| 2401 | return CMD_SUCCESS; |
| 2402 | } |
| 2403 | |
| 2404 | /* quit is alias of exit. */ |
| 2405 | ALIAS (config_exit, |
| 2406 | config_quit_cmd, |
| 2407 | "quit", |
| 2408 | "Exit current mode and down to previous mode\n") |
| 2409 | |
| 2410 | /* End of configuration. */ |
| 2411 | DEFUN (config_end, |
| 2412 | config_end_cmd, |
| 2413 | "end", |
| 2414 | "End current mode and change to enable mode.") |
| 2415 | { |
| 2416 | switch (vty->node) |
| 2417 | { |
| 2418 | case VIEW_NODE: |
| 2419 | case ENABLE_NODE: |
| 2420 | /* Nothing to do. */ |
| 2421 | break; |
| 2422 | case CONFIG_NODE: |
| 2423 | case INTERFACE_NODE: |
| 2424 | case ZEBRA_NODE: |
| 2425 | case RIP_NODE: |
| 2426 | case RIPNG_NODE: |
| 2427 | case BGP_NODE: |
| 2428 | case BGP_VPNV4_NODE: |
| 2429 | case BGP_IPV4_NODE: |
| 2430 | case BGP_IPV4M_NODE: |
| 2431 | case BGP_IPV6_NODE: |
| 2432 | case RMAP_NODE: |
| 2433 | case OSPF_NODE: |
| 2434 | case OSPF6_NODE: |
jardin | 9e867fe | 2003-12-23 08:56:18 +0000 | [diff] [blame] | 2435 | case ISIS_NODE: |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 2436 | case KEYCHAIN_NODE: |
| 2437 | case KEYCHAIN_KEY_NODE: |
| 2438 | case MASC_NODE: |
| 2439 | case VTY_NODE: |
| 2440 | vty_config_unlock (vty); |
| 2441 | vty->node = ENABLE_NODE; |
| 2442 | break; |
| 2443 | default: |
| 2444 | break; |
| 2445 | } |
| 2446 | return CMD_SUCCESS; |
| 2447 | } |
| 2448 | |
| 2449 | /* Show version. */ |
| 2450 | DEFUN (show_version, |
| 2451 | show_version_cmd, |
| 2452 | "show version", |
| 2453 | SHOW_STR |
| 2454 | "Displays zebra version\n") |
| 2455 | { |
hasso | 12f6ea2 | 2005-03-07 08:35:39 +0000 | [diff] [blame] | 2456 | vty_out (vty, "Quagga %s (%s).%s", QUAGGA_VERSION, host.name?host.name:"", |
| 2457 | VTY_NEWLINE); |
hasso | 6590f2c | 2004-10-19 20:40:08 +0000 | [diff] [blame] | 2458 | vty_out (vty, "%s%s", QUAGGA_COPYRIGHT, VTY_NEWLINE); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 2459 | |
| 2460 | return CMD_SUCCESS; |
| 2461 | } |
| 2462 | |
| 2463 | /* Help display function for all node. */ |
| 2464 | DEFUN (config_help, |
| 2465 | config_help_cmd, |
| 2466 | "help", |
| 2467 | "Description of the interactive help system\n") |
| 2468 | { |
| 2469 | vty_out (vty, |
hasso | 6590f2c | 2004-10-19 20:40:08 +0000 | [diff] [blame] | 2470 | "Quagga VTY provides advanced help feature. When you need help,%s\ |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 2471 | anytime at the command line please press '?'.%s\ |
| 2472 | %s\ |
| 2473 | If nothing matches, the help list will be empty and you must backup%s\ |
| 2474 | until entering a '?' shows the available options.%s\ |
| 2475 | Two styles of help are provided:%s\ |
| 2476 | 1. Full help is available when you are ready to enter a%s\ |
| 2477 | command argument (e.g. 'show ?') and describes each possible%s\ |
| 2478 | argument.%s\ |
| 2479 | 2. Partial help is provided when an abbreviated argument is entered%s\ |
| 2480 | and you want to know what arguments match the input%s\ |
| 2481 | (e.g. 'show me?'.)%s%s", VTY_NEWLINE, VTY_NEWLINE, VTY_NEWLINE, |
| 2482 | VTY_NEWLINE, VTY_NEWLINE, VTY_NEWLINE, VTY_NEWLINE, VTY_NEWLINE, |
| 2483 | VTY_NEWLINE, VTY_NEWLINE, VTY_NEWLINE, VTY_NEWLINE, VTY_NEWLINE); |
| 2484 | return CMD_SUCCESS; |
| 2485 | } |
| 2486 | |
| 2487 | /* Help display function for all node. */ |
| 2488 | DEFUN (config_list, |
| 2489 | config_list_cmd, |
| 2490 | "list", |
| 2491 | "Print command list\n") |
| 2492 | { |
hasso | 8c328f1 | 2004-10-05 21:01:23 +0000 | [diff] [blame] | 2493 | unsigned int i; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 2494 | struct cmd_node *cnode = vector_slot (cmdvec, vty->node); |
| 2495 | struct cmd_element *cmd; |
| 2496 | |
paul | 55468c8 | 2005-03-14 20:19:01 +0000 | [diff] [blame] | 2497 | for (i = 0; i < vector_active (cnode->cmd_vector); i++) |
paul | 4275b1d | 2005-03-09 13:42:23 +0000 | [diff] [blame] | 2498 | if ((cmd = vector_slot (cnode->cmd_vector, i)) != NULL |
| 2499 | && !(cmd->attr == CMD_ATTR_DEPRECATED |
| 2500 | || cmd->attr == CMD_ATTR_HIDDEN)) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 2501 | vty_out (vty, " %s%s", cmd->string, |
| 2502 | VTY_NEWLINE); |
| 2503 | return CMD_SUCCESS; |
| 2504 | } |
| 2505 | |
| 2506 | /* Write current configuration into file. */ |
| 2507 | DEFUN (config_write_file, |
| 2508 | config_write_file_cmd, |
| 2509 | "write file", |
| 2510 | "Write running configuration to memory, network, or terminal\n" |
| 2511 | "Write to configuration file\n") |
| 2512 | { |
hasso | 8c328f1 | 2004-10-05 21:01:23 +0000 | [diff] [blame] | 2513 | unsigned int i; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 2514 | int fd; |
| 2515 | struct cmd_node *node; |
| 2516 | char *config_file; |
| 2517 | char *config_file_tmp = NULL; |
| 2518 | char *config_file_sav = NULL; |
| 2519 | struct vty *file_vty; |
| 2520 | |
| 2521 | /* Check and see if we are operating under vtysh configuration */ |
| 2522 | if (host.config == NULL) |
| 2523 | { |
| 2524 | vty_out (vty, "Can't save to configuration file, using vtysh.%s", |
| 2525 | VTY_NEWLINE); |
| 2526 | return CMD_WARNING; |
| 2527 | } |
| 2528 | |
| 2529 | /* Get filename. */ |
| 2530 | config_file = host.config; |
| 2531 | |
| 2532 | config_file_sav = malloc (strlen (config_file) + strlen (CONF_BACKUP_EXT) + 1); |
| 2533 | strcpy (config_file_sav, config_file); |
| 2534 | strcat (config_file_sav, CONF_BACKUP_EXT); |
| 2535 | |
| 2536 | |
| 2537 | config_file_tmp = malloc (strlen (config_file) + 8); |
| 2538 | sprintf (config_file_tmp, "%s.XXXXXX", config_file); |
| 2539 | |
| 2540 | /* Open file to configuration write. */ |
| 2541 | fd = mkstemp (config_file_tmp); |
| 2542 | if (fd < 0) |
| 2543 | { |
| 2544 | vty_out (vty, "Can't open configuration file %s.%s", config_file_tmp, |
| 2545 | VTY_NEWLINE); |
| 2546 | free (config_file_tmp); |
| 2547 | free (config_file_sav); |
| 2548 | return CMD_WARNING; |
| 2549 | } |
| 2550 | |
| 2551 | /* Make vty for configuration file. */ |
| 2552 | file_vty = vty_new (); |
| 2553 | file_vty->fd = fd; |
| 2554 | file_vty->type = VTY_FILE; |
| 2555 | |
| 2556 | /* Config file header print. */ |
| 2557 | vty_out (file_vty, "!\n! Zebra configuration saved from vty\n! "); |
| 2558 | vty_time_print (file_vty, 1); |
| 2559 | vty_out (file_vty, "!\n"); |
| 2560 | |
paul | 55468c8 | 2005-03-14 20:19:01 +0000 | [diff] [blame] | 2561 | for (i = 0; i < vector_active (cmdvec); i++) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 2562 | if ((node = vector_slot (cmdvec, i)) && node->func) |
| 2563 | { |
| 2564 | if ((*node->func) (file_vty)) |
| 2565 | vty_out (file_vty, "!\n"); |
| 2566 | } |
| 2567 | vty_close (file_vty); |
| 2568 | |
| 2569 | if (unlink (config_file_sav) != 0) |
| 2570 | if (errno != ENOENT) |
| 2571 | { |
| 2572 | vty_out (vty, "Can't unlink backup configuration file %s.%s", config_file_sav, |
| 2573 | VTY_NEWLINE); |
| 2574 | free (config_file_sav); |
| 2575 | free (config_file_tmp); |
| 2576 | unlink (config_file_tmp); |
| 2577 | return CMD_WARNING; |
| 2578 | } |
| 2579 | if (link (config_file, config_file_sav) != 0) |
| 2580 | { |
| 2581 | vty_out (vty, "Can't backup old configuration file %s.%s", config_file_sav, |
| 2582 | VTY_NEWLINE); |
| 2583 | free (config_file_sav); |
| 2584 | free (config_file_tmp); |
| 2585 | unlink (config_file_tmp); |
| 2586 | return CMD_WARNING; |
| 2587 | } |
| 2588 | sync (); |
| 2589 | if (unlink (config_file) != 0) |
| 2590 | { |
| 2591 | vty_out (vty, "Can't unlink configuration file %s.%s", config_file, |
| 2592 | VTY_NEWLINE); |
| 2593 | free (config_file_sav); |
| 2594 | free (config_file_tmp); |
| 2595 | unlink (config_file_tmp); |
| 2596 | return CMD_WARNING; |
| 2597 | } |
| 2598 | if (link (config_file_tmp, config_file) != 0) |
| 2599 | { |
| 2600 | vty_out (vty, "Can't save configuration file %s.%s", config_file, |
| 2601 | VTY_NEWLINE); |
| 2602 | free (config_file_sav); |
| 2603 | free (config_file_tmp); |
| 2604 | unlink (config_file_tmp); |
| 2605 | return CMD_WARNING; |
| 2606 | } |
| 2607 | unlink (config_file_tmp); |
| 2608 | sync (); |
| 2609 | |
| 2610 | free (config_file_sav); |
| 2611 | free (config_file_tmp); |
gdt | aa593d5 | 2003-12-22 20:15:53 +0000 | [diff] [blame] | 2612 | |
| 2613 | if (chmod (config_file, CONFIGFILE_MASK) != 0) |
| 2614 | { |
| 2615 | vty_out (vty, "Can't chmod configuration file %s: %s (%d).%s", |
ajs | 6099b3b | 2004-11-20 02:06:59 +0000 | [diff] [blame] | 2616 | config_file, safe_strerror(errno), errno, VTY_NEWLINE); |
gdt | aa593d5 | 2003-12-22 20:15:53 +0000 | [diff] [blame] | 2617 | return CMD_WARNING; |
| 2618 | } |
| 2619 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 2620 | vty_out (vty, "Configuration saved to %s%s", config_file, |
| 2621 | VTY_NEWLINE); |
| 2622 | return CMD_SUCCESS; |
| 2623 | } |
| 2624 | |
| 2625 | ALIAS (config_write_file, |
| 2626 | config_write_cmd, |
| 2627 | "write", |
| 2628 | "Write running configuration to memory, network, or terminal\n") |
| 2629 | |
| 2630 | ALIAS (config_write_file, |
| 2631 | config_write_memory_cmd, |
| 2632 | "write memory", |
| 2633 | "Write running configuration to memory, network, or terminal\n" |
| 2634 | "Write configuration to the file (same as write file)\n") |
| 2635 | |
| 2636 | ALIAS (config_write_file, |
| 2637 | copy_runningconfig_startupconfig_cmd, |
| 2638 | "copy running-config startup-config", |
| 2639 | "Copy configuration\n" |
| 2640 | "Copy running config to... \n" |
| 2641 | "Copy running config to startup config (same as write file)\n") |
| 2642 | |
| 2643 | /* Write current configuration into the terminal. */ |
| 2644 | DEFUN (config_write_terminal, |
| 2645 | config_write_terminal_cmd, |
| 2646 | "write terminal", |
| 2647 | "Write running configuration to memory, network, or terminal\n" |
| 2648 | "Write to terminal\n") |
| 2649 | { |
hasso | 8c328f1 | 2004-10-05 21:01:23 +0000 | [diff] [blame] | 2650 | unsigned int i; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 2651 | struct cmd_node *node; |
| 2652 | |
| 2653 | if (vty->type == VTY_SHELL_SERV) |
| 2654 | { |
paul | 55468c8 | 2005-03-14 20:19:01 +0000 | [diff] [blame] | 2655 | for (i = 0; i < vector_active (cmdvec); i++) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 2656 | if ((node = vector_slot (cmdvec, i)) && node->func && node->vtysh) |
| 2657 | { |
| 2658 | if ((*node->func) (vty)) |
| 2659 | vty_out (vty, "!%s", VTY_NEWLINE); |
| 2660 | } |
| 2661 | } |
| 2662 | else |
| 2663 | { |
| 2664 | vty_out (vty, "%sCurrent configuration:%s", VTY_NEWLINE, |
| 2665 | VTY_NEWLINE); |
| 2666 | vty_out (vty, "!%s", VTY_NEWLINE); |
| 2667 | |
paul | 55468c8 | 2005-03-14 20:19:01 +0000 | [diff] [blame] | 2668 | for (i = 0; i < vector_active (cmdvec); i++) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 2669 | if ((node = vector_slot (cmdvec, i)) && node->func) |
| 2670 | { |
| 2671 | if ((*node->func) (vty)) |
| 2672 | vty_out (vty, "!%s", VTY_NEWLINE); |
| 2673 | } |
| 2674 | vty_out (vty, "end%s",VTY_NEWLINE); |
| 2675 | } |
| 2676 | return CMD_SUCCESS; |
| 2677 | } |
| 2678 | |
| 2679 | /* Write current configuration into the terminal. */ |
| 2680 | ALIAS (config_write_terminal, |
| 2681 | show_running_config_cmd, |
| 2682 | "show running-config", |
| 2683 | SHOW_STR |
| 2684 | "running configuration\n") |
| 2685 | |
| 2686 | /* Write startup configuration into the terminal. */ |
| 2687 | DEFUN (show_startup_config, |
| 2688 | show_startup_config_cmd, |
| 2689 | "show startup-config", |
| 2690 | SHOW_STR |
| 2691 | "Contentes of startup configuration\n") |
| 2692 | { |
| 2693 | char buf[BUFSIZ]; |
| 2694 | FILE *confp; |
| 2695 | |
| 2696 | confp = fopen (host.config, "r"); |
| 2697 | if (confp == NULL) |
| 2698 | { |
| 2699 | vty_out (vty, "Can't open configuration file [%s]%s", |
| 2700 | host.config, VTY_NEWLINE); |
| 2701 | return CMD_WARNING; |
| 2702 | } |
| 2703 | |
| 2704 | while (fgets (buf, BUFSIZ, confp)) |
| 2705 | { |
| 2706 | char *cp = buf; |
| 2707 | |
| 2708 | while (*cp != '\r' && *cp != '\n' && *cp != '\0') |
| 2709 | cp++; |
| 2710 | *cp = '\0'; |
| 2711 | |
| 2712 | vty_out (vty, "%s%s", buf, VTY_NEWLINE); |
| 2713 | } |
| 2714 | |
| 2715 | fclose (confp); |
| 2716 | |
| 2717 | return CMD_SUCCESS; |
| 2718 | } |
| 2719 | |
| 2720 | /* Hostname configuration */ |
| 2721 | DEFUN (config_hostname, |
| 2722 | hostname_cmd, |
| 2723 | "hostname WORD", |
| 2724 | "Set system's network name\n" |
| 2725 | "This system's network name\n") |
| 2726 | { |
| 2727 | if (!isalpha((int) *argv[0])) |
| 2728 | { |
| 2729 | vty_out (vty, "Please specify string starting with alphabet%s", VTY_NEWLINE); |
| 2730 | return CMD_WARNING; |
| 2731 | } |
| 2732 | |
| 2733 | if (host.name) |
| 2734 | XFREE (0, host.name); |
| 2735 | |
| 2736 | host.name = strdup (argv[0]); |
| 2737 | return CMD_SUCCESS; |
| 2738 | } |
| 2739 | |
| 2740 | DEFUN (config_no_hostname, |
| 2741 | no_hostname_cmd, |
| 2742 | "no hostname [HOSTNAME]", |
| 2743 | NO_STR |
| 2744 | "Reset system's network name\n" |
| 2745 | "Host name of this router\n") |
| 2746 | { |
| 2747 | if (host.name) |
| 2748 | XFREE (0, host.name); |
| 2749 | host.name = NULL; |
| 2750 | return CMD_SUCCESS; |
| 2751 | } |
| 2752 | |
| 2753 | /* VTY interface password set. */ |
| 2754 | DEFUN (config_password, password_cmd, |
| 2755 | "password (8|) WORD", |
| 2756 | "Assign the terminal connection password\n" |
| 2757 | "Specifies a HIDDEN password will follow\n" |
| 2758 | "dummy string \n" |
| 2759 | "The HIDDEN line password string\n") |
| 2760 | { |
| 2761 | /* Argument check. */ |
| 2762 | if (argc == 0) |
| 2763 | { |
| 2764 | vty_out (vty, "Please specify password.%s", VTY_NEWLINE); |
| 2765 | return CMD_WARNING; |
| 2766 | } |
| 2767 | |
| 2768 | if (argc == 2) |
| 2769 | { |
| 2770 | if (*argv[0] == '8') |
| 2771 | { |
| 2772 | if (host.password) |
| 2773 | XFREE (0, host.password); |
| 2774 | host.password = NULL; |
| 2775 | if (host.password_encrypt) |
| 2776 | XFREE (0, host.password_encrypt); |
| 2777 | host.password_encrypt = XSTRDUP (0, strdup (argv[1])); |
| 2778 | return CMD_SUCCESS; |
| 2779 | } |
| 2780 | else |
| 2781 | { |
| 2782 | vty_out (vty, "Unknown encryption type.%s", VTY_NEWLINE); |
| 2783 | return CMD_WARNING; |
| 2784 | } |
| 2785 | } |
| 2786 | |
| 2787 | if (!isalnum ((int) *argv[0])) |
| 2788 | { |
| 2789 | vty_out (vty, |
| 2790 | "Please specify string starting with alphanumeric%s", VTY_NEWLINE); |
| 2791 | return CMD_WARNING; |
| 2792 | } |
| 2793 | |
| 2794 | if (host.password) |
| 2795 | XFREE (0, host.password); |
| 2796 | host.password = NULL; |
| 2797 | |
| 2798 | if (host.encrypt) |
| 2799 | { |
| 2800 | if (host.password_encrypt) |
| 2801 | XFREE (0, host.password_encrypt); |
| 2802 | host.password_encrypt = XSTRDUP (0, zencrypt (argv[0])); |
| 2803 | } |
| 2804 | else |
| 2805 | host.password = XSTRDUP (0, argv[0]); |
| 2806 | |
| 2807 | return CMD_SUCCESS; |
| 2808 | } |
| 2809 | |
| 2810 | ALIAS (config_password, password_text_cmd, |
| 2811 | "password LINE", |
| 2812 | "Assign the terminal connection password\n" |
| 2813 | "The UNENCRYPTED (cleartext) line password\n") |
| 2814 | |
| 2815 | /* VTY enable password set. */ |
| 2816 | DEFUN (config_enable_password, enable_password_cmd, |
| 2817 | "enable password (8|) WORD", |
| 2818 | "Modify enable password parameters\n" |
| 2819 | "Assign the privileged level password\n" |
| 2820 | "Specifies a HIDDEN password will follow\n" |
| 2821 | "dummy string \n" |
| 2822 | "The HIDDEN 'enable' password string\n") |
| 2823 | { |
| 2824 | /* Argument check. */ |
| 2825 | if (argc == 0) |
| 2826 | { |
| 2827 | vty_out (vty, "Please specify password.%s", VTY_NEWLINE); |
| 2828 | return CMD_WARNING; |
| 2829 | } |
| 2830 | |
| 2831 | /* Crypt type is specified. */ |
| 2832 | if (argc == 2) |
| 2833 | { |
| 2834 | if (*argv[0] == '8') |
| 2835 | { |
| 2836 | if (host.enable) |
| 2837 | XFREE (0, host.enable); |
| 2838 | host.enable = NULL; |
| 2839 | |
| 2840 | if (host.enable_encrypt) |
| 2841 | XFREE (0, host.enable_encrypt); |
| 2842 | host.enable_encrypt = XSTRDUP (0, argv[1]); |
| 2843 | |
| 2844 | return CMD_SUCCESS; |
| 2845 | } |
| 2846 | else |
| 2847 | { |
| 2848 | vty_out (vty, "Unknown encryption type.%s", VTY_NEWLINE); |
| 2849 | return CMD_WARNING; |
| 2850 | } |
| 2851 | } |
| 2852 | |
| 2853 | if (!isalnum ((int) *argv[0])) |
| 2854 | { |
| 2855 | vty_out (vty, |
| 2856 | "Please specify string starting with alphanumeric%s", VTY_NEWLINE); |
| 2857 | return CMD_WARNING; |
| 2858 | } |
| 2859 | |
| 2860 | if (host.enable) |
| 2861 | XFREE (0, host.enable); |
| 2862 | host.enable = NULL; |
| 2863 | |
| 2864 | /* Plain password input. */ |
| 2865 | if (host.encrypt) |
| 2866 | { |
| 2867 | if (host.enable_encrypt) |
| 2868 | XFREE (0, host.enable_encrypt); |
| 2869 | host.enable_encrypt = XSTRDUP (0, zencrypt (argv[0])); |
| 2870 | } |
| 2871 | else |
| 2872 | host.enable = XSTRDUP (0, argv[0]); |
| 2873 | |
| 2874 | return CMD_SUCCESS; |
| 2875 | } |
| 2876 | |
| 2877 | ALIAS (config_enable_password, |
| 2878 | enable_password_text_cmd, |
| 2879 | "enable password LINE", |
| 2880 | "Modify enable password parameters\n" |
| 2881 | "Assign the privileged level password\n" |
| 2882 | "The UNENCRYPTED (cleartext) 'enable' password\n") |
| 2883 | |
| 2884 | /* VTY enable password delete. */ |
| 2885 | DEFUN (no_config_enable_password, no_enable_password_cmd, |
| 2886 | "no enable password", |
| 2887 | NO_STR |
| 2888 | "Modify enable password parameters\n" |
| 2889 | "Assign the privileged level password\n") |
| 2890 | { |
| 2891 | if (host.enable) |
| 2892 | XFREE (0, host.enable); |
| 2893 | host.enable = NULL; |
| 2894 | |
| 2895 | if (host.enable_encrypt) |
| 2896 | XFREE (0, host.enable_encrypt); |
| 2897 | host.enable_encrypt = NULL; |
| 2898 | |
| 2899 | return CMD_SUCCESS; |
| 2900 | } |
| 2901 | |
| 2902 | DEFUN (service_password_encrypt, |
| 2903 | service_password_encrypt_cmd, |
| 2904 | "service password-encryption", |
| 2905 | "Set up miscellaneous service\n" |
| 2906 | "Enable encrypted passwords\n") |
| 2907 | { |
| 2908 | if (host.encrypt) |
| 2909 | return CMD_SUCCESS; |
| 2910 | |
| 2911 | host.encrypt = 1; |
| 2912 | |
| 2913 | if (host.password) |
| 2914 | { |
| 2915 | if (host.password_encrypt) |
| 2916 | XFREE (0, host.password_encrypt); |
| 2917 | host.password_encrypt = XSTRDUP (0, zencrypt (host.password)); |
| 2918 | } |
| 2919 | if (host.enable) |
| 2920 | { |
| 2921 | if (host.enable_encrypt) |
| 2922 | XFREE (0, host.enable_encrypt); |
| 2923 | host.enable_encrypt = XSTRDUP (0, zencrypt (host.enable)); |
| 2924 | } |
| 2925 | |
| 2926 | return CMD_SUCCESS; |
| 2927 | } |
| 2928 | |
| 2929 | DEFUN (no_service_password_encrypt, |
| 2930 | no_service_password_encrypt_cmd, |
| 2931 | "no service password-encryption", |
| 2932 | NO_STR |
| 2933 | "Set up miscellaneous service\n" |
| 2934 | "Enable encrypted passwords\n") |
| 2935 | { |
| 2936 | if (! host.encrypt) |
| 2937 | return CMD_SUCCESS; |
| 2938 | |
| 2939 | host.encrypt = 0; |
| 2940 | |
| 2941 | if (host.password_encrypt) |
| 2942 | XFREE (0, host.password_encrypt); |
| 2943 | host.password_encrypt = NULL; |
| 2944 | |
| 2945 | if (host.enable_encrypt) |
| 2946 | XFREE (0, host.enable_encrypt); |
| 2947 | host.enable_encrypt = NULL; |
| 2948 | |
| 2949 | return CMD_SUCCESS; |
| 2950 | } |
| 2951 | |
| 2952 | DEFUN (config_terminal_length, config_terminal_length_cmd, |
| 2953 | "terminal length <0-512>", |
| 2954 | "Set terminal line parameters\n" |
| 2955 | "Set number of lines on a screen\n" |
| 2956 | "Number of lines on screen (0 for no pausing)\n") |
| 2957 | { |
| 2958 | int lines; |
| 2959 | char *endptr = NULL; |
| 2960 | |
| 2961 | lines = strtol (argv[0], &endptr, 10); |
| 2962 | if (lines < 0 || lines > 512 || *endptr != '\0') |
| 2963 | { |
| 2964 | vty_out (vty, "length is malformed%s", VTY_NEWLINE); |
| 2965 | return CMD_WARNING; |
| 2966 | } |
| 2967 | vty->lines = lines; |
| 2968 | |
| 2969 | return CMD_SUCCESS; |
| 2970 | } |
| 2971 | |
| 2972 | DEFUN (config_terminal_no_length, config_terminal_no_length_cmd, |
| 2973 | "terminal no length", |
| 2974 | "Set terminal line parameters\n" |
| 2975 | NO_STR |
| 2976 | "Set number of lines on a screen\n") |
| 2977 | { |
| 2978 | vty->lines = -1; |
| 2979 | return CMD_SUCCESS; |
| 2980 | } |
| 2981 | |
| 2982 | DEFUN (service_terminal_length, service_terminal_length_cmd, |
| 2983 | "service terminal-length <0-512>", |
| 2984 | "Set up miscellaneous service\n" |
| 2985 | "System wide terminal length configuration\n" |
| 2986 | "Number of lines of VTY (0 means no line control)\n") |
| 2987 | { |
| 2988 | int lines; |
| 2989 | char *endptr = NULL; |
| 2990 | |
| 2991 | lines = strtol (argv[0], &endptr, 10); |
| 2992 | if (lines < 0 || lines > 512 || *endptr != '\0') |
| 2993 | { |
| 2994 | vty_out (vty, "length is malformed%s", VTY_NEWLINE); |
| 2995 | return CMD_WARNING; |
| 2996 | } |
| 2997 | host.lines = lines; |
| 2998 | |
| 2999 | return CMD_SUCCESS; |
| 3000 | } |
| 3001 | |
| 3002 | DEFUN (no_service_terminal_length, no_service_terminal_length_cmd, |
| 3003 | "no service terminal-length [<0-512>]", |
| 3004 | NO_STR |
| 3005 | "Set up miscellaneous service\n" |
| 3006 | "System wide terminal length configuration\n" |
| 3007 | "Number of lines of VTY (0 means no line control)\n") |
| 3008 | { |
| 3009 | host.lines = -1; |
| 3010 | return CMD_SUCCESS; |
| 3011 | } |
| 3012 | |
ajs | 2885f72 | 2004-12-17 23:16:33 +0000 | [diff] [blame] | 3013 | DEFUN_HIDDEN (do_echo, |
| 3014 | echo_cmd, |
| 3015 | "echo .MESSAGE", |
| 3016 | "Echo a message back to the vty\n" |
| 3017 | "The message to echo\n") |
| 3018 | { |
| 3019 | char *message; |
| 3020 | |
ajs | f6834d4 | 2005-01-28 20:28:35 +0000 | [diff] [blame] | 3021 | vty_out (vty, "%s%s", ((message = argv_concat(argv, argc, 0)) ? message : ""), |
| 3022 | VTY_NEWLINE); |
| 3023 | if (message) |
| 3024 | XFREE(MTYPE_TMP, message); |
ajs | 2885f72 | 2004-12-17 23:16:33 +0000 | [diff] [blame] | 3025 | return CMD_SUCCESS; |
| 3026 | } |
| 3027 | |
ajs | 274a4a4 | 2004-12-07 15:39:31 +0000 | [diff] [blame] | 3028 | DEFUN (config_logmsg, |
| 3029 | config_logmsg_cmd, |
| 3030 | "logmsg "LOG_LEVELS" .MESSAGE", |
| 3031 | "Send a message to enabled logging destinations\n" |
| 3032 | LOG_LEVEL_DESC |
| 3033 | "The message to send\n") |
| 3034 | { |
| 3035 | int level; |
| 3036 | char *message; |
| 3037 | |
| 3038 | if ((level = level_match(argv[0])) == ZLOG_DISABLED) |
| 3039 | return CMD_ERR_NO_MATCH; |
| 3040 | |
ajs | f6834d4 | 2005-01-28 20:28:35 +0000 | [diff] [blame] | 3041 | zlog(NULL, level, ((message = argv_concat(argv, argc, 1)) ? message : "")); |
| 3042 | if (message) |
| 3043 | XFREE(MTYPE_TMP, message); |
ajs | 274a4a4 | 2004-12-07 15:39:31 +0000 | [diff] [blame] | 3044 | return CMD_SUCCESS; |
| 3045 | } |
| 3046 | |
| 3047 | DEFUN (show_logging, |
| 3048 | show_logging_cmd, |
| 3049 | "show logging", |
| 3050 | SHOW_STR |
| 3051 | "Show current logging configuration\n") |
| 3052 | { |
| 3053 | struct zlog *zl = zlog_default; |
| 3054 | |
| 3055 | vty_out (vty, "Syslog logging: "); |
| 3056 | if (zl->maxlvl[ZLOG_DEST_SYSLOG] == ZLOG_DISABLED) |
| 3057 | vty_out (vty, "disabled"); |
| 3058 | else |
| 3059 | vty_out (vty, "level %s, facility %s, ident %s", |
| 3060 | zlog_priority[zl->maxlvl[ZLOG_DEST_SYSLOG]], |
| 3061 | facility_name(zl->facility), zl->ident); |
| 3062 | vty_out (vty, "%s", VTY_NEWLINE); |
| 3063 | |
| 3064 | vty_out (vty, "Stdout logging: "); |
| 3065 | if (zl->maxlvl[ZLOG_DEST_STDOUT] == ZLOG_DISABLED) |
| 3066 | vty_out (vty, "disabled"); |
| 3067 | else |
| 3068 | vty_out (vty, "level %s", |
| 3069 | zlog_priority[zl->maxlvl[ZLOG_DEST_STDOUT]]); |
| 3070 | vty_out (vty, "%s", VTY_NEWLINE); |
| 3071 | |
| 3072 | vty_out (vty, "Monitor logging: "); |
| 3073 | if (zl->maxlvl[ZLOG_DEST_MONITOR] == ZLOG_DISABLED) |
| 3074 | vty_out (vty, "disabled"); |
| 3075 | else |
| 3076 | vty_out (vty, "level %s", |
| 3077 | zlog_priority[zl->maxlvl[ZLOG_DEST_MONITOR]]); |
| 3078 | vty_out (vty, "%s", VTY_NEWLINE); |
| 3079 | |
| 3080 | vty_out (vty, "File logging: "); |
| 3081 | if ((zl->maxlvl[ZLOG_DEST_FILE] == ZLOG_DISABLED) || |
| 3082 | !zl->fp) |
| 3083 | vty_out (vty, "disabled"); |
| 3084 | else |
| 3085 | vty_out (vty, "level %s, filename %s", |
| 3086 | zlog_priority[zl->maxlvl[ZLOG_DEST_FILE]], |
| 3087 | zl->filename); |
| 3088 | vty_out (vty, "%s", VTY_NEWLINE); |
| 3089 | |
| 3090 | vty_out (vty, "Protocol name: %s%s", |
| 3091 | zlog_proto_names[zl->protocol], VTY_NEWLINE); |
| 3092 | vty_out (vty, "Record priority: %s%s", |
| 3093 | (zl->record_priority ? "enabled" : "disabled"), VTY_NEWLINE); |
| 3094 | |
| 3095 | return CMD_SUCCESS; |
| 3096 | } |
| 3097 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 3098 | DEFUN (config_log_stdout, |
| 3099 | config_log_stdout_cmd, |
| 3100 | "log stdout", |
| 3101 | "Logging control\n" |
ajs | 274a4a4 | 2004-12-07 15:39:31 +0000 | [diff] [blame] | 3102 | "Set stdout logging level\n") |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 3103 | { |
ajs | 274a4a4 | 2004-12-07 15:39:31 +0000 | [diff] [blame] | 3104 | zlog_set_level (NULL, ZLOG_DEST_STDOUT, zlog_default->default_lvl); |
| 3105 | return CMD_SUCCESS; |
| 3106 | } |
| 3107 | |
| 3108 | DEFUN (config_log_stdout_level, |
| 3109 | config_log_stdout_level_cmd, |
| 3110 | "log stdout "LOG_LEVELS, |
| 3111 | "Logging control\n" |
| 3112 | "Set stdout logging level\n" |
| 3113 | LOG_LEVEL_DESC) |
| 3114 | { |
| 3115 | int level; |
| 3116 | |
| 3117 | if ((level = level_match(argv[0])) == ZLOG_DISABLED) |
| 3118 | return CMD_ERR_NO_MATCH; |
| 3119 | zlog_set_level (NULL, ZLOG_DEST_STDOUT, level); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 3120 | return CMD_SUCCESS; |
| 3121 | } |
| 3122 | |
| 3123 | DEFUN (no_config_log_stdout, |
| 3124 | no_config_log_stdout_cmd, |
ajs | 274a4a4 | 2004-12-07 15:39:31 +0000 | [diff] [blame] | 3125 | "no log stdout [LEVEL]", |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 3126 | NO_STR |
| 3127 | "Logging control\n" |
ajs | 274a4a4 | 2004-12-07 15:39:31 +0000 | [diff] [blame] | 3128 | "Cancel logging to stdout\n" |
| 3129 | "Logging level\n") |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 3130 | { |
ajs | 274a4a4 | 2004-12-07 15:39:31 +0000 | [diff] [blame] | 3131 | zlog_set_level (NULL, ZLOG_DEST_STDOUT, ZLOG_DISABLED); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 3132 | return CMD_SUCCESS; |
| 3133 | } |
| 3134 | |
ajs | 274a4a4 | 2004-12-07 15:39:31 +0000 | [diff] [blame] | 3135 | DEFUN (config_log_monitor, |
| 3136 | config_log_monitor_cmd, |
| 3137 | "log monitor", |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 3138 | "Logging control\n" |
ajs | 274a4a4 | 2004-12-07 15:39:31 +0000 | [diff] [blame] | 3139 | "Set terminal line (monitor) logging level\n") |
| 3140 | { |
| 3141 | zlog_set_level (NULL, ZLOG_DEST_MONITOR, zlog_default->default_lvl); |
| 3142 | return CMD_SUCCESS; |
| 3143 | } |
| 3144 | |
| 3145 | DEFUN (config_log_monitor_level, |
| 3146 | config_log_monitor_level_cmd, |
| 3147 | "log monitor "LOG_LEVELS, |
| 3148 | "Logging control\n" |
| 3149 | "Set terminal line (monitor) logging level\n" |
| 3150 | LOG_LEVEL_DESC) |
| 3151 | { |
| 3152 | int level; |
| 3153 | |
| 3154 | if ((level = level_match(argv[0])) == ZLOG_DISABLED) |
| 3155 | return CMD_ERR_NO_MATCH; |
| 3156 | zlog_set_level (NULL, ZLOG_DEST_MONITOR, level); |
| 3157 | return CMD_SUCCESS; |
| 3158 | } |
| 3159 | |
| 3160 | DEFUN (no_config_log_monitor, |
| 3161 | no_config_log_monitor_cmd, |
| 3162 | "no log monitor [LEVEL]", |
| 3163 | NO_STR |
| 3164 | "Logging control\n" |
| 3165 | "Disable terminal line (monitor) logging\n" |
| 3166 | "Logging level\n") |
| 3167 | { |
| 3168 | zlog_set_level (NULL, ZLOG_DEST_MONITOR, ZLOG_DISABLED); |
| 3169 | return CMD_SUCCESS; |
| 3170 | } |
| 3171 | |
| 3172 | static int |
| 3173 | set_log_file(struct vty *vty, const char *fname, int loglevel) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 3174 | { |
| 3175 | int ret; |
paul | 9035efa | 2004-10-10 11:56:56 +0000 | [diff] [blame] | 3176 | char *p = NULL; |
| 3177 | const char *fullpath; |
| 3178 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 3179 | /* Path detection. */ |
ajs | 274a4a4 | 2004-12-07 15:39:31 +0000 | [diff] [blame] | 3180 | if (! IS_DIRECTORY_SEP (*fname)) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 3181 | { |
paul | 9035efa | 2004-10-10 11:56:56 +0000 | [diff] [blame] | 3182 | char cwd[MAXPATHLEN+1]; |
| 3183 | cwd[MAXPATHLEN] = '\0'; |
| 3184 | |
| 3185 | if (getcwd (cwd, MAXPATHLEN) == NULL) |
| 3186 | { |
| 3187 | zlog_err ("config_log_file: Unable to alloc mem!"); |
| 3188 | return CMD_WARNING; |
| 3189 | } |
| 3190 | |
ajs | 274a4a4 | 2004-12-07 15:39:31 +0000 | [diff] [blame] | 3191 | if ( (p = XMALLOC (MTYPE_TMP, strlen (cwd) + strlen (fname) + 2)) |
paul | 9035efa | 2004-10-10 11:56:56 +0000 | [diff] [blame] | 3192 | == NULL) |
| 3193 | { |
| 3194 | zlog_err ("config_log_file: Unable to alloc mem!"); |
| 3195 | return CMD_WARNING; |
| 3196 | } |
ajs | 274a4a4 | 2004-12-07 15:39:31 +0000 | [diff] [blame] | 3197 | sprintf (p, "%s/%s", cwd, fname); |
paul | 9035efa | 2004-10-10 11:56:56 +0000 | [diff] [blame] | 3198 | fullpath = p; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 3199 | } |
| 3200 | else |
ajs | 274a4a4 | 2004-12-07 15:39:31 +0000 | [diff] [blame] | 3201 | fullpath = fname; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 3202 | |
ajs | 274a4a4 | 2004-12-07 15:39:31 +0000 | [diff] [blame] | 3203 | ret = zlog_set_file (NULL, fullpath, loglevel); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 3204 | |
paul | 9035efa | 2004-10-10 11:56:56 +0000 | [diff] [blame] | 3205 | if (p) |
| 3206 | XFREE (MTYPE_TMP, p); |
| 3207 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 3208 | if (!ret) |
| 3209 | { |
ajs | 274a4a4 | 2004-12-07 15:39:31 +0000 | [diff] [blame] | 3210 | vty_out (vty, "can't open logfile %s\n", fname); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 3211 | return CMD_WARNING; |
| 3212 | } |
| 3213 | |
| 3214 | if (host.logfile) |
| 3215 | XFREE (MTYPE_TMP, host.logfile); |
| 3216 | |
ajs | 274a4a4 | 2004-12-07 15:39:31 +0000 | [diff] [blame] | 3217 | host.logfile = XSTRDUP (MTYPE_TMP, fname); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 3218 | |
| 3219 | return CMD_SUCCESS; |
| 3220 | } |
| 3221 | |
ajs | 274a4a4 | 2004-12-07 15:39:31 +0000 | [diff] [blame] | 3222 | DEFUN (config_log_file, |
| 3223 | config_log_file_cmd, |
| 3224 | "log file FILENAME", |
| 3225 | "Logging control\n" |
| 3226 | "Logging to file\n" |
| 3227 | "Logging filename\n") |
| 3228 | { |
| 3229 | return set_log_file(vty, argv[0], zlog_default->default_lvl); |
| 3230 | } |
| 3231 | |
| 3232 | DEFUN (config_log_file_level, |
| 3233 | config_log_file_level_cmd, |
| 3234 | "log file FILENAME "LOG_LEVELS, |
| 3235 | "Logging control\n" |
| 3236 | "Logging to file\n" |
| 3237 | "Logging filename\n" |
| 3238 | LOG_LEVEL_DESC) |
| 3239 | { |
| 3240 | int level; |
| 3241 | |
| 3242 | if ((level = level_match(argv[1])) == ZLOG_DISABLED) |
| 3243 | return CMD_ERR_NO_MATCH; |
| 3244 | return set_log_file(vty, argv[0], level); |
| 3245 | } |
| 3246 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 3247 | DEFUN (no_config_log_file, |
| 3248 | no_config_log_file_cmd, |
| 3249 | "no log file [FILENAME]", |
| 3250 | NO_STR |
| 3251 | "Logging control\n" |
| 3252 | "Cancel logging to file\n" |
| 3253 | "Logging file name\n") |
| 3254 | { |
| 3255 | zlog_reset_file (NULL); |
| 3256 | |
| 3257 | if (host.logfile) |
| 3258 | XFREE (MTYPE_TMP, host.logfile); |
| 3259 | |
| 3260 | host.logfile = NULL; |
| 3261 | |
| 3262 | return CMD_SUCCESS; |
| 3263 | } |
| 3264 | |
ajs | 274a4a4 | 2004-12-07 15:39:31 +0000 | [diff] [blame] | 3265 | ALIAS (no_config_log_file, |
| 3266 | no_config_log_file_level_cmd, |
| 3267 | "no log file FILENAME LEVEL", |
| 3268 | NO_STR |
| 3269 | "Logging control\n" |
| 3270 | "Cancel logging to file\n" |
| 3271 | "Logging file name\n" |
| 3272 | "Logging level\n") |
| 3273 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 3274 | DEFUN (config_log_syslog, |
| 3275 | config_log_syslog_cmd, |
| 3276 | "log syslog", |
| 3277 | "Logging control\n" |
ajs | 274a4a4 | 2004-12-07 15:39:31 +0000 | [diff] [blame] | 3278 | "Set syslog logging level\n") |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 3279 | { |
ajs | 274a4a4 | 2004-12-07 15:39:31 +0000 | [diff] [blame] | 3280 | zlog_set_level (NULL, ZLOG_DEST_SYSLOG, zlog_default->default_lvl); |
paul | 12ab19f | 2003-07-26 06:14:55 +0000 | [diff] [blame] | 3281 | return CMD_SUCCESS; |
| 3282 | } |
| 3283 | |
ajs | 274a4a4 | 2004-12-07 15:39:31 +0000 | [diff] [blame] | 3284 | DEFUN (config_log_syslog_level, |
| 3285 | config_log_syslog_level_cmd, |
| 3286 | "log syslog "LOG_LEVELS, |
paul | 12ab19f | 2003-07-26 06:14:55 +0000 | [diff] [blame] | 3287 | "Logging control\n" |
ajs | 274a4a4 | 2004-12-07 15:39:31 +0000 | [diff] [blame] | 3288 | "Set syslog logging level\n" |
| 3289 | LOG_LEVEL_DESC) |
paul | 12ab19f | 2003-07-26 06:14:55 +0000 | [diff] [blame] | 3290 | { |
ajs | 274a4a4 | 2004-12-07 15:39:31 +0000 | [diff] [blame] | 3291 | int level; |
paul | 12ab19f | 2003-07-26 06:14:55 +0000 | [diff] [blame] | 3292 | |
ajs | 274a4a4 | 2004-12-07 15:39:31 +0000 | [diff] [blame] | 3293 | if ((level = level_match(argv[0])) == ZLOG_DISABLED) |
| 3294 | return CMD_ERR_NO_MATCH; |
| 3295 | zlog_set_level (NULL, ZLOG_DEST_SYSLOG, level); |
| 3296 | return CMD_SUCCESS; |
| 3297 | } |
paul | 12ab19f | 2003-07-26 06:14:55 +0000 | [diff] [blame] | 3298 | |
ajs | 274a4a4 | 2004-12-07 15:39:31 +0000 | [diff] [blame] | 3299 | DEFUN_DEPRECATED (config_log_syslog_facility, |
| 3300 | config_log_syslog_facility_cmd, |
| 3301 | "log syslog facility "LOG_FACILITIES, |
| 3302 | "Logging control\n" |
| 3303 | "Logging goes to syslog\n" |
| 3304 | "(Deprecated) Facility parameter for syslog messages\n" |
| 3305 | LOG_FACILITY_DESC) |
| 3306 | { |
| 3307 | int facility; |
paul | 12ab19f | 2003-07-26 06:14:55 +0000 | [diff] [blame] | 3308 | |
ajs | 274a4a4 | 2004-12-07 15:39:31 +0000 | [diff] [blame] | 3309 | if ((facility = facility_match(argv[0])) < 0) |
| 3310 | return CMD_ERR_NO_MATCH; |
| 3311 | |
| 3312 | zlog_set_level (NULL, ZLOG_DEST_SYSLOG, zlog_default->default_lvl); |
paul | 12ab19f | 2003-07-26 06:14:55 +0000 | [diff] [blame] | 3313 | zlog_default->facility = facility; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 3314 | return CMD_SUCCESS; |
| 3315 | } |
| 3316 | |
| 3317 | DEFUN (no_config_log_syslog, |
| 3318 | no_config_log_syslog_cmd, |
ajs | 274a4a4 | 2004-12-07 15:39:31 +0000 | [diff] [blame] | 3319 | "no log syslog [LEVEL]", |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 3320 | NO_STR |
| 3321 | "Logging control\n" |
ajs | 274a4a4 | 2004-12-07 15:39:31 +0000 | [diff] [blame] | 3322 | "Cancel logging to syslog\n" |
| 3323 | "Logging level\n") |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 3324 | { |
ajs | 274a4a4 | 2004-12-07 15:39:31 +0000 | [diff] [blame] | 3325 | zlog_set_level (NULL, ZLOG_DEST_SYSLOG, ZLOG_DISABLED); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 3326 | return CMD_SUCCESS; |
| 3327 | } |
| 3328 | |
paul | 12ab19f | 2003-07-26 06:14:55 +0000 | [diff] [blame] | 3329 | ALIAS (no_config_log_syslog, |
| 3330 | no_config_log_syslog_facility_cmd, |
ajs | 274a4a4 | 2004-12-07 15:39:31 +0000 | [diff] [blame] | 3331 | "no log syslog facility "LOG_FACILITIES, |
paul | 12ab19f | 2003-07-26 06:14:55 +0000 | [diff] [blame] | 3332 | NO_STR |
| 3333 | "Logging control\n" |
| 3334 | "Logging goes to syslog\n" |
| 3335 | "Facility parameter for syslog messages\n" |
ajs | 274a4a4 | 2004-12-07 15:39:31 +0000 | [diff] [blame] | 3336 | LOG_FACILITY_DESC) |
paul | 12ab19f | 2003-07-26 06:14:55 +0000 | [diff] [blame] | 3337 | |
ajs | 274a4a4 | 2004-12-07 15:39:31 +0000 | [diff] [blame] | 3338 | DEFUN (config_log_facility, |
| 3339 | config_log_facility_cmd, |
| 3340 | "log facility "LOG_FACILITIES, |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 3341 | "Logging control\n" |
ajs | 274a4a4 | 2004-12-07 15:39:31 +0000 | [diff] [blame] | 3342 | "Facility parameter for syslog messages\n" |
| 3343 | LOG_FACILITY_DESC) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 3344 | { |
ajs | 274a4a4 | 2004-12-07 15:39:31 +0000 | [diff] [blame] | 3345 | int facility; |
| 3346 | |
| 3347 | if ((facility = facility_match(argv[0])) < 0) |
| 3348 | return CMD_ERR_NO_MATCH; |
| 3349 | zlog_default->facility = facility; |
| 3350 | return CMD_SUCCESS; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 3351 | } |
| 3352 | |
ajs | 274a4a4 | 2004-12-07 15:39:31 +0000 | [diff] [blame] | 3353 | DEFUN (no_config_log_facility, |
| 3354 | no_config_log_facility_cmd, |
| 3355 | "no log facility [FACILITY]", |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 3356 | NO_STR |
| 3357 | "Logging control\n" |
ajs | 274a4a4 | 2004-12-07 15:39:31 +0000 | [diff] [blame] | 3358 | "Reset syslog facility to default (daemon)\n" |
| 3359 | "Syslog facility\n") |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 3360 | { |
ajs | 274a4a4 | 2004-12-07 15:39:31 +0000 | [diff] [blame] | 3361 | zlog_default->facility = LOG_DAEMON; |
| 3362 | return CMD_SUCCESS; |
| 3363 | } |
| 3364 | |
| 3365 | DEFUN_DEPRECATED (config_log_trap, |
| 3366 | config_log_trap_cmd, |
| 3367 | "log trap "LOG_LEVELS, |
| 3368 | "Logging control\n" |
| 3369 | "(Deprecated) Set logging level and default for all destinations\n" |
| 3370 | LOG_LEVEL_DESC) |
| 3371 | { |
| 3372 | int new_level ; |
| 3373 | int i; |
| 3374 | |
| 3375 | if ((new_level = level_match(argv[0])) == ZLOG_DISABLED) |
| 3376 | return CMD_ERR_NO_MATCH; |
| 3377 | |
| 3378 | zlog_default->default_lvl = new_level; |
| 3379 | for (i = 0; i < ZLOG_NUM_DESTS; i++) |
| 3380 | if (zlog_default->maxlvl[i] != ZLOG_DISABLED) |
| 3381 | zlog_default->maxlvl[i] = new_level; |
| 3382 | return CMD_SUCCESS; |
| 3383 | } |
| 3384 | |
| 3385 | DEFUN_DEPRECATED (no_config_log_trap, |
| 3386 | no_config_log_trap_cmd, |
| 3387 | "no log trap [LEVEL]", |
| 3388 | NO_STR |
| 3389 | "Logging control\n" |
| 3390 | "Permit all logging information\n" |
| 3391 | "Logging level\n") |
| 3392 | { |
| 3393 | zlog_default->default_lvl = LOG_DEBUG; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 3394 | return CMD_SUCCESS; |
| 3395 | } |
| 3396 | |
| 3397 | DEFUN (config_log_record_priority, |
| 3398 | config_log_record_priority_cmd, |
| 3399 | "log record-priority", |
| 3400 | "Logging control\n" |
| 3401 | "Log the priority of the message within the message\n") |
| 3402 | { |
| 3403 | zlog_default->record_priority = 1 ; |
| 3404 | return CMD_SUCCESS; |
| 3405 | } |
| 3406 | |
| 3407 | DEFUN (no_config_log_record_priority, |
| 3408 | no_config_log_record_priority_cmd, |
| 3409 | "no log record-priority", |
| 3410 | NO_STR |
| 3411 | "Logging control\n" |
| 3412 | "Do not log the priority of the message within the message\n") |
| 3413 | { |
| 3414 | zlog_default->record_priority = 0 ; |
| 3415 | return CMD_SUCCESS; |
| 3416 | } |
| 3417 | |
paul | 3b0c5d9 | 2005-03-08 10:43:43 +0000 | [diff] [blame] | 3418 | DEFUN (banner_motd_file, |
| 3419 | banner_motd_file_cmd, |
| 3420 | "banner motd file [FILE]", |
| 3421 | "Set banner\n" |
| 3422 | "Banner for motd\n" |
| 3423 | "Banner from a file\n" |
| 3424 | "Filename\n") |
| 3425 | { |
paul | b45da6f | 2005-03-08 15:16:57 +0000 | [diff] [blame] | 3426 | if (host.motdfile) |
| 3427 | XFREE (MTYPE_TMP, host.motdfile); |
| 3428 | host.motdfile = XSTRDUP (MTYPE_TMP, argv[0]); |
| 3429 | |
paul | 3b0c5d9 | 2005-03-08 10:43:43 +0000 | [diff] [blame] | 3430 | return CMD_SUCCESS; |
| 3431 | } |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 3432 | |
| 3433 | DEFUN (banner_motd_default, |
| 3434 | banner_motd_default_cmd, |
| 3435 | "banner motd default", |
| 3436 | "Set banner string\n" |
| 3437 | "Strings for motd\n" |
| 3438 | "Default string\n") |
| 3439 | { |
| 3440 | host.motd = default_motd; |
| 3441 | return CMD_SUCCESS; |
| 3442 | } |
| 3443 | |
| 3444 | DEFUN (no_banner_motd, |
| 3445 | no_banner_motd_cmd, |
| 3446 | "no banner motd", |
| 3447 | NO_STR |
| 3448 | "Set banner string\n" |
| 3449 | "Strings for motd\n") |
| 3450 | { |
| 3451 | host.motd = NULL; |
paul | 2208518 | 2005-03-08 16:00:12 +0000 | [diff] [blame] | 3452 | if (host.motdfile) |
| 3453 | XFREE (MTYPE_TMP, host.motdfile); |
paul | 3b0c5d9 | 2005-03-08 10:43:43 +0000 | [diff] [blame] | 3454 | host.motdfile = NULL; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 3455 | return CMD_SUCCESS; |
| 3456 | } |
| 3457 | |
| 3458 | /* Set config filename. Called from vty.c */ |
| 3459 | void |
| 3460 | host_config_set (char *filename) |
| 3461 | { |
| 3462 | host.config = strdup (filename); |
| 3463 | } |
| 3464 | |
| 3465 | void |
| 3466 | install_default (enum node_type node) |
| 3467 | { |
| 3468 | install_element (node, &config_exit_cmd); |
| 3469 | install_element (node, &config_quit_cmd); |
| 3470 | install_element (node, &config_end_cmd); |
| 3471 | install_element (node, &config_help_cmd); |
| 3472 | install_element (node, &config_list_cmd); |
| 3473 | |
| 3474 | install_element (node, &config_write_terminal_cmd); |
| 3475 | install_element (node, &config_write_file_cmd); |
| 3476 | install_element (node, &config_write_memory_cmd); |
| 3477 | install_element (node, &config_write_cmd); |
| 3478 | install_element (node, &show_running_config_cmd); |
| 3479 | } |
| 3480 | |
| 3481 | /* Initialize command interface. Install basic nodes and commands. */ |
| 3482 | void |
| 3483 | cmd_init (int terminal) |
| 3484 | { |
| 3485 | /* Allocate initial top vector of commands. */ |
| 3486 | cmdvec = vector_init (VECTOR_MIN_SIZE); |
| 3487 | |
| 3488 | /* Default host value settings. */ |
| 3489 | host.name = NULL; |
| 3490 | host.password = NULL; |
| 3491 | host.enable = NULL; |
| 3492 | host.logfile = NULL; |
| 3493 | host.config = NULL; |
| 3494 | host.lines = -1; |
| 3495 | host.motd = default_motd; |
paul | 3b0c5d9 | 2005-03-08 10:43:43 +0000 | [diff] [blame] | 3496 | host.motdfile = NULL; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 3497 | |
| 3498 | /* Install top nodes. */ |
| 3499 | install_node (&view_node, NULL); |
| 3500 | install_node (&enable_node, NULL); |
| 3501 | install_node (&auth_node, NULL); |
| 3502 | install_node (&auth_enable_node, NULL); |
| 3503 | install_node (&config_node, config_write_host); |
| 3504 | |
| 3505 | /* Each node's basic commands. */ |
| 3506 | install_element (VIEW_NODE, &show_version_cmd); |
| 3507 | if (terminal) |
| 3508 | { |
| 3509 | install_element (VIEW_NODE, &config_list_cmd); |
| 3510 | install_element (VIEW_NODE, &config_exit_cmd); |
| 3511 | install_element (VIEW_NODE, &config_quit_cmd); |
| 3512 | install_element (VIEW_NODE, &config_help_cmd); |
| 3513 | install_element (VIEW_NODE, &config_enable_cmd); |
| 3514 | install_element (VIEW_NODE, &config_terminal_length_cmd); |
| 3515 | install_element (VIEW_NODE, &config_terminal_no_length_cmd); |
ajs | 274a4a4 | 2004-12-07 15:39:31 +0000 | [diff] [blame] | 3516 | install_element (VIEW_NODE, &show_logging_cmd); |
ajs | 2885f72 | 2004-12-17 23:16:33 +0000 | [diff] [blame] | 3517 | install_element (VIEW_NODE, &echo_cmd); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 3518 | } |
| 3519 | |
| 3520 | if (terminal) |
| 3521 | { |
| 3522 | install_default (ENABLE_NODE); |
| 3523 | install_element (ENABLE_NODE, &config_disable_cmd); |
| 3524 | install_element (ENABLE_NODE, &config_terminal_cmd); |
| 3525 | install_element (ENABLE_NODE, ©_runningconfig_startupconfig_cmd); |
| 3526 | } |
| 3527 | install_element (ENABLE_NODE, &show_startup_config_cmd); |
| 3528 | install_element (ENABLE_NODE, &show_version_cmd); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 3529 | |
| 3530 | if (terminal) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 3531 | { |
hasso | e7168df | 2004-10-03 20:11:32 +0000 | [diff] [blame] | 3532 | install_element (ENABLE_NODE, &config_terminal_length_cmd); |
| 3533 | install_element (ENABLE_NODE, &config_terminal_no_length_cmd); |
ajs | 274a4a4 | 2004-12-07 15:39:31 +0000 | [diff] [blame] | 3534 | install_element (ENABLE_NODE, &show_logging_cmd); |
ajs | 2885f72 | 2004-12-17 23:16:33 +0000 | [diff] [blame] | 3535 | install_element (ENABLE_NODE, &echo_cmd); |
ajs | 274a4a4 | 2004-12-07 15:39:31 +0000 | [diff] [blame] | 3536 | install_element (ENABLE_NODE, &config_logmsg_cmd); |
hasso | e7168df | 2004-10-03 20:11:32 +0000 | [diff] [blame] | 3537 | |
| 3538 | install_default (CONFIG_NODE); |
hasso | ea8e9d9 | 2004-10-07 21:32:14 +0000 | [diff] [blame] | 3539 | } |
| 3540 | |
| 3541 | install_element (CONFIG_NODE, &hostname_cmd); |
| 3542 | install_element (CONFIG_NODE, &no_hostname_cmd); |
hasso | e7168df | 2004-10-03 20:11:32 +0000 | [diff] [blame] | 3543 | |
hasso | ea8e9d9 | 2004-10-07 21:32:14 +0000 | [diff] [blame] | 3544 | if (terminal) |
| 3545 | { |
hasso | e7168df | 2004-10-03 20:11:32 +0000 | [diff] [blame] | 3546 | install_element (CONFIG_NODE, &password_cmd); |
| 3547 | install_element (CONFIG_NODE, &password_text_cmd); |
| 3548 | install_element (CONFIG_NODE, &enable_password_cmd); |
| 3549 | install_element (CONFIG_NODE, &enable_password_text_cmd); |
| 3550 | install_element (CONFIG_NODE, &no_enable_password_cmd); |
| 3551 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 3552 | install_element (CONFIG_NODE, &config_log_stdout_cmd); |
ajs | 274a4a4 | 2004-12-07 15:39:31 +0000 | [diff] [blame] | 3553 | install_element (CONFIG_NODE, &config_log_stdout_level_cmd); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 3554 | install_element (CONFIG_NODE, &no_config_log_stdout_cmd); |
ajs | 274a4a4 | 2004-12-07 15:39:31 +0000 | [diff] [blame] | 3555 | install_element (CONFIG_NODE, &config_log_monitor_cmd); |
| 3556 | install_element (CONFIG_NODE, &config_log_monitor_level_cmd); |
| 3557 | install_element (CONFIG_NODE, &no_config_log_monitor_cmd); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 3558 | install_element (CONFIG_NODE, &config_log_file_cmd); |
ajs | 274a4a4 | 2004-12-07 15:39:31 +0000 | [diff] [blame] | 3559 | install_element (CONFIG_NODE, &config_log_file_level_cmd); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 3560 | install_element (CONFIG_NODE, &no_config_log_file_cmd); |
ajs | 274a4a4 | 2004-12-07 15:39:31 +0000 | [diff] [blame] | 3561 | install_element (CONFIG_NODE, &no_config_log_file_level_cmd); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 3562 | install_element (CONFIG_NODE, &config_log_syslog_cmd); |
ajs | 274a4a4 | 2004-12-07 15:39:31 +0000 | [diff] [blame] | 3563 | install_element (CONFIG_NODE, &config_log_syslog_level_cmd); |
paul | 12ab19f | 2003-07-26 06:14:55 +0000 | [diff] [blame] | 3564 | install_element (CONFIG_NODE, &config_log_syslog_facility_cmd); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 3565 | install_element (CONFIG_NODE, &no_config_log_syslog_cmd); |
paul | 12ab19f | 2003-07-26 06:14:55 +0000 | [diff] [blame] | 3566 | install_element (CONFIG_NODE, &no_config_log_syslog_facility_cmd); |
ajs | 274a4a4 | 2004-12-07 15:39:31 +0000 | [diff] [blame] | 3567 | install_element (CONFIG_NODE, &config_log_facility_cmd); |
| 3568 | install_element (CONFIG_NODE, &no_config_log_facility_cmd); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 3569 | install_element (CONFIG_NODE, &config_log_trap_cmd); |
| 3570 | install_element (CONFIG_NODE, &no_config_log_trap_cmd); |
| 3571 | install_element (CONFIG_NODE, &config_log_record_priority_cmd); |
| 3572 | install_element (CONFIG_NODE, &no_config_log_record_priority_cmd); |
| 3573 | install_element (CONFIG_NODE, &service_password_encrypt_cmd); |
| 3574 | install_element (CONFIG_NODE, &no_service_password_encrypt_cmd); |
| 3575 | install_element (CONFIG_NODE, &banner_motd_default_cmd); |
paul | 3b0c5d9 | 2005-03-08 10:43:43 +0000 | [diff] [blame] | 3576 | install_element (CONFIG_NODE, &banner_motd_file_cmd); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 3577 | install_element (CONFIG_NODE, &no_banner_motd_cmd); |
| 3578 | install_element (CONFIG_NODE, &service_terminal_length_cmd); |
| 3579 | install_element (CONFIG_NODE, &no_service_terminal_length_cmd); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 3580 | |
paul | 9ab6812 | 2003-01-18 01:16:20 +0000 | [diff] [blame] | 3581 | install_element(VIEW_NODE, &show_thread_cpu_cmd); |
| 3582 | install_element(ENABLE_NODE, &show_thread_cpu_cmd); |
| 3583 | } |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 3584 | srand(time(NULL)); |
| 3585 | } |