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