blob: 8ae27de920fa4b199aac86365a5237075ffb1fc3 [file] [log] [blame]
ajs274a4a42004-12-07 15:39:31 +00001/*
ajs274a4a42004-12-07 15:39:31 +00002 Command interpreter routine for virtual terminal [aka TeletYpe]
paul718e3742002-12-13 20:15:29 +00003 Copyright (C) 1997, 98, 99 Kunihiro Ishiguro
Christian Frankecd40b322013-09-30 12:27:51 +00004 Copyright (C) 2013 by Open Source Routing.
5 Copyright (C) 2013 by Internet Systems Consortium, Inc. ("ISC")
paul718e3742002-12-13 20:15:29 +00006
7This file is part of GNU Zebra.
8
9GNU Zebra is free software; you can redistribute it and/or modify
10it under the terms of the GNU General Public License as published
11by the Free Software Foundation; either version 2, or (at your
12option) any later version.
13
14GNU Zebra is distributed in the hope that it will be useful, but
15WITHOUT ANY WARRANTY; without even the implied warranty of
16MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17General Public License for more details.
18
19You should have received a copy of the GNU General Public License
20along with GNU Zebra; see the file COPYING. If not, write to the
21Free Software Foundation, Inc., 59 Temple Place - Suite 330,
22Boston, MA 02111-1307, USA. */
23
24#include <zebra.h>
25
paulb21b19c2003-06-15 01:28:29 +000026
paul718e3742002-12-13 20:15:29 +000027#include "memory.h"
28#include "log.h"
gdt5e4fa162004-03-16 14:38:36 +000029#include <lib/version.h>
paul9ab68122003-01-18 01:16:20 +000030#include "thread.h"
paulb21b19c2003-06-15 01:28:29 +000031#include "vector.h"
32#include "vty.h"
33#include "command.h"
paul354d1192005-04-25 16:26:42 +000034#include "workqueue.h"
paul718e3742002-12-13 20:15:29 +000035
36/* Command vector which includes some level of command lists. Normally
37 each daemon maintains each own cmdvec. */
pauleb820af2005-09-05 11:54:13 +000038vector cmdvec = NULL;
paul718e3742002-12-13 20:15:29 +000039
Christian Frankecd40b322013-09-30 12:27:51 +000040struct cmd_token token_cr;
Chris Caputo228da422009-07-18 05:44:03 +000041char *command_cr = NULL;
42
Christian Frankecd40b322013-09-30 12:27:51 +000043enum filter_type
44{
45 FILTER_RELAXED,
46 FILTER_STRICT
47};
48
49enum matcher_rv
50{
51 MATCHER_OK,
52 MATCHER_COMPLETE,
53 MATCHER_INCOMPLETE,
54 MATCHER_NO_MATCH,
55 MATCHER_AMBIGUOUS,
56 MATCHER_EXCEED_ARGC_MAX
57};
58
59#define MATCHER_ERROR(matcher_rv) \
60 ( (matcher_rv) == MATCHER_INCOMPLETE \
61 || (matcher_rv) == MATCHER_NO_MATCH \
62 || (matcher_rv) == MATCHER_AMBIGUOUS \
63 || (matcher_rv) == MATCHER_EXCEED_ARGC_MAX \
64 )
65
paul718e3742002-12-13 20:15:29 +000066/* Host information structure. */
67struct host host;
68
paul718e3742002-12-13 20:15:29 +000069/* Standard command node structures. */
Stephen Hemminger7fc626d2008-12-01 11:10:34 -080070static struct cmd_node auth_node =
paul718e3742002-12-13 20:15:29 +000071{
72 AUTH_NODE,
73 "Password: ",
74};
75
Stephen Hemminger7fc626d2008-12-01 11:10:34 -080076static struct cmd_node view_node =
paul718e3742002-12-13 20:15:29 +000077{
78 VIEW_NODE,
79 "%s> ",
80};
81
Stephen Hemminger7fc626d2008-12-01 11:10:34 -080082static struct cmd_node restricted_node =
Paul Jakma62687ff2008-08-23 14:27:06 +010083{
84 RESTRICTED_NODE,
85 "%s$ ",
86};
87
Stephen Hemminger7fc626d2008-12-01 11:10:34 -080088static struct cmd_node auth_enable_node =
paul718e3742002-12-13 20:15:29 +000089{
90 AUTH_ENABLE_NODE,
91 "Password: ",
92};
93
Stephen Hemminger7fc626d2008-12-01 11:10:34 -080094static struct cmd_node enable_node =
paul718e3742002-12-13 20:15:29 +000095{
96 ENABLE_NODE,
97 "%s# ",
98};
99
Stephen Hemminger7fc626d2008-12-01 11:10:34 -0800100static struct cmd_node config_node =
paul718e3742002-12-13 20:15:29 +0000101{
102 CONFIG_NODE,
103 "%s(config)# ",
104 1
105};
hasso6590f2c2004-10-19 20:40:08 +0000106
107/* Default motd string. */
Everton Marques74b4fad2014-09-18 12:06:53 -0300108static const char *default_motd =
hasso6590f2c2004-10-19 20:40:08 +0000109"\r\n\
110Hello, this is " QUAGGA_PROGNAME " (version " QUAGGA_VERSION ").\r\n\
111" QUAGGA_COPYRIGHT "\r\n\
David Lamparter0be793e2012-11-27 01:34:56 +0000112" GIT_INFO "\r\n";
hasso6590f2c2004-10-19 20:40:08 +0000113
ajs274a4a42004-12-07 15:39:31 +0000114
Stephen Hemminger2d362d12009-12-21 12:54:58 +0300115static const struct facility_map {
ajs274a4a42004-12-07 15:39:31 +0000116 int facility;
117 const char *name;
118 size_t match;
119} syslog_facilities[] =
120 {
121 { LOG_KERN, "kern", 1 },
122 { LOG_USER, "user", 2 },
123 { LOG_MAIL, "mail", 1 },
124 { LOG_DAEMON, "daemon", 1 },
125 { LOG_AUTH, "auth", 1 },
126 { LOG_SYSLOG, "syslog", 1 },
127 { LOG_LPR, "lpr", 2 },
128 { LOG_NEWS, "news", 1 },
129 { LOG_UUCP, "uucp", 2 },
130 { LOG_CRON, "cron", 1 },
131#ifdef LOG_FTP
132 { LOG_FTP, "ftp", 1 },
133#endif
134 { LOG_LOCAL0, "local0", 6 },
135 { LOG_LOCAL1, "local1", 6 },
136 { LOG_LOCAL2, "local2", 6 },
137 { LOG_LOCAL3, "local3", 6 },
138 { LOG_LOCAL4, "local4", 6 },
139 { LOG_LOCAL5, "local5", 6 },
140 { LOG_LOCAL6, "local6", 6 },
141 { LOG_LOCAL7, "local7", 6 },
142 { 0, NULL, 0 },
143 };
144
145static const char *
146facility_name(int facility)
147{
Stephen Hemminger2d362d12009-12-21 12:54:58 +0300148 const struct facility_map *fm;
ajs274a4a42004-12-07 15:39:31 +0000149
150 for (fm = syslog_facilities; fm->name; fm++)
151 if (fm->facility == facility)
152 return fm->name;
153 return "";
154}
155
156static int
157facility_match(const char *str)
158{
Stephen Hemminger2d362d12009-12-21 12:54:58 +0300159 const struct facility_map *fm;
ajs274a4a42004-12-07 15:39:31 +0000160
161 for (fm = syslog_facilities; fm->name; fm++)
162 if (!strncmp(str,fm->name,fm->match))
163 return fm->facility;
164 return -1;
165}
166
167static int
168level_match(const char *s)
169{
170 int level ;
171
172 for ( level = 0 ; zlog_priority [level] != NULL ; level ++ )
173 if (!strncmp (s, zlog_priority[level], 2))
174 return level;
175 return ZLOG_DISABLED;
176}
177
ajscb585b62005-01-14 17:09:38 +0000178/* This is called from main when a daemon is invoked with -v or --version. */
hasso6590f2c2004-10-19 20:40:08 +0000179void
180print_version (const char *progname)
181{
ajscb585b62005-01-14 17:09:38 +0000182 printf ("%s version %s\n", progname, QUAGGA_VERSION);
183 printf ("%s\n", QUAGGA_COPYRIGHT);
David Lamparter7abd8752014-11-22 10:43:29 -0800184 printf ("configured with:\n\t%s\n", QUAGGA_CONFIG_ARGS);
hasso6590f2c2004-10-19 20:40:08 +0000185}
186
David Lamparter6b0655a2014-06-04 06:53:35 +0200187
paul718e3742002-12-13 20:15:29 +0000188/* Utility function to concatenate argv argument into a single string
189 with inserting ' ' character between each argument. */
190char *
paul42d49862004-10-13 05:22:18 +0000191argv_concat (const char **argv, int argc, int shift)
paul718e3742002-12-13 20:15:29 +0000192{
193 int i;
ajsf6834d42005-01-28 20:28:35 +0000194 size_t len;
paul718e3742002-12-13 20:15:29 +0000195 char *str;
ajsf6834d42005-01-28 20:28:35 +0000196 char *p;
paul718e3742002-12-13 20:15:29 +0000197
ajsf6834d42005-01-28 20:28:35 +0000198 len = 0;
199 for (i = shift; i < argc; i++)
200 len += strlen(argv[i])+1;
201 if (!len)
202 return NULL;
203 p = str = XMALLOC(MTYPE_TMP, len);
paul718e3742002-12-13 20:15:29 +0000204 for (i = shift; i < argc; i++)
205 {
ajsf6834d42005-01-28 20:28:35 +0000206 size_t arglen;
207 memcpy(p, argv[i], (arglen = strlen(argv[i])));
208 p += arglen;
209 *p++ = ' ';
paul718e3742002-12-13 20:15:29 +0000210 }
ajsf6834d42005-01-28 20:28:35 +0000211 *(p-1) = '\0';
paul718e3742002-12-13 20:15:29 +0000212 return str;
213}
214
215/* Install top node of command vector. */
216void
217install_node (struct cmd_node *node,
218 int (*func) (struct vty *))
219{
220 vector_set_index (cmdvec, node->node, node);
221 node->func = func;
222 node->cmd_vector = vector_init (VECTOR_MIN_SIZE);
223}
224
paul718e3742002-12-13 20:15:29 +0000225/* Breaking up string into each command piece. I assume given
226 character is separated by a space character. Return value is a
227 vector which includes char ** data element. */
228vector
hassoea8e9d92004-10-07 21:32:14 +0000229cmd_make_strvec (const char *string)
paul718e3742002-12-13 20:15:29 +0000230{
hassoea8e9d92004-10-07 21:32:14 +0000231 const char *cp, *start;
232 char *token;
paul718e3742002-12-13 20:15:29 +0000233 int strlen;
234 vector strvec;
235
236 if (string == NULL)
237 return NULL;
238
239 cp = string;
240
241 /* Skip white spaces. */
242 while (isspace ((int) *cp) && *cp != '\0')
243 cp++;
244
245 /* Return if there is only white spaces */
246 if (*cp == '\0')
247 return NULL;
248
249 if (*cp == '!' || *cp == '#')
250 return NULL;
251
252 /* Prepare return vector. */
253 strvec = vector_init (VECTOR_MIN_SIZE);
254
255 /* Copy each command piece and set into vector. */
256 while (1)
257 {
258 start = cp;
259 while (!(isspace ((int) *cp) || *cp == '\r' || *cp == '\n') &&
260 *cp != '\0')
261 cp++;
262 strlen = cp - start;
263 token = XMALLOC (MTYPE_STRVEC, strlen + 1);
264 memcpy (token, start, strlen);
265 *(token + strlen) = '\0';
266 vector_set (strvec, token);
267
268 while ((isspace ((int) *cp) || *cp == '\n' || *cp == '\r') &&
269 *cp != '\0')
270 cp++;
271
272 if (*cp == '\0')
273 return strvec;
274 }
275}
276
277/* Free allocated string vector. */
278void
279cmd_free_strvec (vector v)
280{
hasso8c328f12004-10-05 21:01:23 +0000281 unsigned int i;
paul718e3742002-12-13 20:15:29 +0000282 char *cp;
283
284 if (!v)
285 return;
286
paul55468c82005-03-14 20:19:01 +0000287 for (i = 0; i < vector_active (v); i++)
paul718e3742002-12-13 20:15:29 +0000288 if ((cp = vector_slot (v, i)) != NULL)
289 XFREE (MTYPE_STRVEC, cp);
290
291 vector_free (v);
292}
293
Christian Frankecd40b322013-09-30 12:27:51 +0000294struct format_parser_state
295{
296 vector topvect; /* Top level vector */
297 vector intvect; /* Intermediate level vector, used when there's
298 * a multiple in a keyword. */
299 vector curvect; /* current vector where read tokens should be
300 appended. */
301
302 const char *string; /* pointer to command string, not modified */
303 const char *cp; /* pointer in command string, moved along while
304 parsing */
305 const char *dp; /* pointer in description string, moved along while
306 parsing */
307
308 int in_keyword; /* flag to remember if we are in a keyword group */
309 int in_multiple; /* flag to remember if we are in a multiple group */
310 int just_read_word; /* flag to remember if the last thing we red was a
311 * real word and not some abstract token */
312};
313
314static void
315format_parser_error(struct format_parser_state *state, const char *message)
316{
317 int offset = state->cp - state->string + 1;
318
319 fprintf(stderr, "\nError parsing command: \"%s\"\n", state->string);
320 fprintf(stderr, " %*c\n", offset, '^');
321 fprintf(stderr, "%s at offset %d.\n", message, offset);
322 fprintf(stderr, "This is a programming error. Check your DEFUNs etc.\n");
323 exit(1);
324}
325
ajs274a4a42004-12-07 15:39:31 +0000326static char *
Christian Frankecd40b322013-09-30 12:27:51 +0000327format_parser_desc_str(struct format_parser_state *state)
paul718e3742002-12-13 20:15:29 +0000328{
hasso6ad96ea2004-10-07 19:33:46 +0000329 const char *cp, *start;
330 char *token;
paul718e3742002-12-13 20:15:29 +0000331 int strlen;
Christian Frankecd40b322013-09-30 12:27:51 +0000332
333 cp = state->dp;
paul718e3742002-12-13 20:15:29 +0000334
335 if (cp == NULL)
336 return NULL;
337
338 /* Skip white spaces. */
339 while (isspace ((int) *cp) && *cp != '\0')
340 cp++;
341
342 /* Return if there is only white spaces */
343 if (*cp == '\0')
344 return NULL;
345
346 start = cp;
347
348 while (!(*cp == '\r' || *cp == '\n') && *cp != '\0')
349 cp++;
350
351 strlen = cp - start;
Christian Frankecd40b322013-09-30 12:27:51 +0000352 token = XMALLOC (MTYPE_CMD_TOKENS, strlen + 1);
paul718e3742002-12-13 20:15:29 +0000353 memcpy (token, start, strlen);
354 *(token + strlen) = '\0';
355
Christian Frankecd40b322013-09-30 12:27:51 +0000356 state->dp = cp;
paul718e3742002-12-13 20:15:29 +0000357
358 return token;
359}
360
Christian Frankecd40b322013-09-30 12:27:51 +0000361static void
362format_parser_begin_keyword(struct format_parser_state *state)
paul718e3742002-12-13 20:15:29 +0000363{
Christian Frankecd40b322013-09-30 12:27:51 +0000364 struct cmd_token *token;
365 vector keyword_vect;
paul718e3742002-12-13 20:15:29 +0000366
Christian Frankecd40b322013-09-30 12:27:51 +0000367 if (state->in_keyword
368 || state->in_multiple)
369 format_parser_error(state, "Unexpected '{'");
paul718e3742002-12-13 20:15:29 +0000370
Christian Frankecd40b322013-09-30 12:27:51 +0000371 state->cp++;
372 state->in_keyword = 1;
paul718e3742002-12-13 20:15:29 +0000373
Christian Frankecd40b322013-09-30 12:27:51 +0000374 token = XCALLOC(MTYPE_CMD_TOKENS, sizeof(*token));
375 token->type = TOKEN_KEYWORD;
376 token->keyword = vector_init(VECTOR_MIN_SIZE);
paul718e3742002-12-13 20:15:29 +0000377
Christian Frankecd40b322013-09-30 12:27:51 +0000378 keyword_vect = vector_init(VECTOR_MIN_SIZE);
379 vector_set(token->keyword, keyword_vect);
380
381 vector_set(state->curvect, token);
382 state->curvect = keyword_vect;
383}
384
385static void
386format_parser_begin_multiple(struct format_parser_state *state)
387{
388 struct cmd_token *token;
389
390 if (state->in_keyword == 1)
391 format_parser_error(state, "Keyword starting with '('");
392
393 if (state->in_multiple)
394 format_parser_error(state, "Nested group");
395
396 state->cp++;
397 state->in_multiple = 1;
398 state->just_read_word = 0;
399
400 token = XCALLOC(MTYPE_CMD_TOKENS, sizeof(*token));
401 token->type = TOKEN_MULTIPLE;
402 token->multiple = vector_init(VECTOR_MIN_SIZE);
403
404 vector_set(state->curvect, token);
405 if (state->curvect != state->topvect)
406 state->intvect = state->curvect;
407 state->curvect = token->multiple;
408}
409
410static void
411format_parser_end_keyword(struct format_parser_state *state)
412{
413 if (state->in_multiple
414 || !state->in_keyword)
415 format_parser_error(state, "Unexpected '}'");
416
417 if (state->in_keyword == 1)
418 format_parser_error(state, "Empty keyword group");
419
420 state->cp++;
421 state->in_keyword = 0;
422 state->curvect = state->topvect;
423}
424
425static void
426format_parser_end_multiple(struct format_parser_state *state)
427{
428 char *dummy;
429
430 if (!state->in_multiple)
431 format_parser_error(state, "Unepexted ')'");
432
433 if (vector_active(state->curvect) == 0)
434 format_parser_error(state, "Empty multiple section");
435
436 if (!state->just_read_word)
paul718e3742002-12-13 20:15:29 +0000437 {
Christian Frankecd40b322013-09-30 12:27:51 +0000438 /* There are constructions like
439 * 'show ip ospf database ... (self-originate|)'
440 * in use.
441 * The old parser reads a description string for the
442 * word '' between |) which will never match.
443 * Simulate this behvaior by dropping the next desc
444 * string in such a case. */
paul718e3742002-12-13 20:15:29 +0000445
Christian Frankecd40b322013-09-30 12:27:51 +0000446 dummy = format_parser_desc_str(state);
447 XFREE(MTYPE_CMD_TOKENS, dummy);
448 }
paul718e3742002-12-13 20:15:29 +0000449
Christian Frankecd40b322013-09-30 12:27:51 +0000450 state->cp++;
451 state->in_multiple = 0;
paul718e3742002-12-13 20:15:29 +0000452
Christian Frankecd40b322013-09-30 12:27:51 +0000453 if (state->intvect)
454 state->curvect = state->intvect;
455 else
456 state->curvect = state->topvect;
457}
paul718e3742002-12-13 20:15:29 +0000458
Christian Frankecd40b322013-09-30 12:27:51 +0000459static void
460format_parser_handle_pipe(struct format_parser_state *state)
461{
462 struct cmd_token *keyword_token;
463 vector keyword_vect;
paul718e3742002-12-13 20:15:29 +0000464
Christian Frankecd40b322013-09-30 12:27:51 +0000465 if (state->in_multiple)
466 {
467 state->just_read_word = 0;
468 state->cp++;
469 }
470 else if (state->in_keyword)
471 {
472 state->in_keyword = 1;
473 state->cp++;
paul718e3742002-12-13 20:15:29 +0000474
Christian Frankecd40b322013-09-30 12:27:51 +0000475 keyword_token = vector_slot(state->topvect,
476 vector_active(state->topvect) - 1);
477 keyword_vect = vector_init(VECTOR_MIN_SIZE);
478 vector_set(keyword_token->keyword, keyword_vect);
479 state->curvect = keyword_vect;
480 }
481 else
482 {
483 format_parser_error(state, "Unexpected '|'");
paul718e3742002-12-13 20:15:29 +0000484 }
485}
486
Christian Frankecd40b322013-09-30 12:27:51 +0000487static void
488format_parser_read_word(struct format_parser_state *state)
paul718e3742002-12-13 20:15:29 +0000489{
Christian Frankecd40b322013-09-30 12:27:51 +0000490 const char *start;
491 int len;
492 char *cmd;
493 struct cmd_token *token;
paul718e3742002-12-13 20:15:29 +0000494
Christian Frankecd40b322013-09-30 12:27:51 +0000495 start = state->cp;
496
497 while (state->cp[0] != '\0'
498 && !strchr("\r\n(){}|", state->cp[0])
499 && !isspace((int)state->cp[0]))
500 state->cp++;
501
502 len = state->cp - start;
503 cmd = XMALLOC(MTYPE_CMD_TOKENS, len + 1);
504 memcpy(cmd, start, len);
505 cmd[len] = '\0';
506
507 token = XCALLOC(MTYPE_CMD_TOKENS, sizeof(*token));
508 token->type = TOKEN_TERMINAL;
David Lamparter14162932015-05-12 17:18:04 +0200509 if (strcmp (cmd, "A.B.C.D") == 0)
David Lamparter10bac802015-05-05 11:10:20 +0200510 token->terminal = TERMINAL_IPV4;
511 else if (strcmp (cmd, "A.B.C.D/M") == 0)
512 token->terminal = TERMINAL_IPV4_PREFIX;
513 else if (strcmp (cmd, "X:X::X:X") == 0)
514 token->terminal = TERMINAL_IPV6;
515 else if (strcmp (cmd, "X:X::X:X/M") == 0)
516 token->terminal = TERMINAL_IPV6_PREFIX;
David Lamparter14162932015-05-12 17:18:04 +0200517 else if (cmd[0] == '[')
518 token->terminal = TERMINAL_OPTION;
519 else if (cmd[0] == '.')
520 token->terminal = TERMINAL_VARARG;
521 else if (cmd[0] == '<')
522 token->terminal = TERMINAL_RANGE;
523 else if (cmd[0] >= 'A' && cmd[0] <= 'Z')
524 token->terminal = TERMINAL_VARIABLE;
David Lamparter10bac802015-05-05 11:10:20 +0200525 else
526 token->terminal = TERMINAL_LITERAL;
527
Christian Frankecd40b322013-09-30 12:27:51 +0000528 token->cmd = cmd;
529 token->desc = format_parser_desc_str(state);
530 vector_set(state->curvect, token);
531
532 if (state->in_keyword == 1)
533 state->in_keyword = 2;
534
535 state->just_read_word = 1;
536}
537
538/**
539 * Parse a given command format string and build a tree of tokens from
540 * it that is suitable to be used by the command subsystem.
541 *
542 * @param string Command format string.
543 * @param descstr Description string.
544 * @return A vector of struct cmd_token representing the given command,
545 * or NULL on error.
546 */
547static vector
548cmd_parse_format(const char *string, const char *descstr)
549{
550 struct format_parser_state state;
551
552 if (string == NULL)
553 return NULL;
554
555 memset(&state, 0, sizeof(state));
556 state.topvect = state.curvect = vector_init(VECTOR_MIN_SIZE);
557 state.cp = state.string = string;
558 state.dp = descstr;
559
560 while (1)
paul718e3742002-12-13 20:15:29 +0000561 {
Christian Frankecd40b322013-09-30 12:27:51 +0000562 while (isspace((int)state.cp[0]) && state.cp[0] != '\0')
563 state.cp++;
564
565 switch (state.cp[0])
566 {
567 case '\0':
568 if (state.in_keyword
569 || state.in_multiple)
570 format_parser_error(&state, "Unclosed group/keyword");
571 return state.topvect;
572 case '{':
573 format_parser_begin_keyword(&state);
574 break;
575 case '(':
576 format_parser_begin_multiple(&state);
577 break;
578 case '}':
579 format_parser_end_keyword(&state);
580 break;
581 case ')':
582 format_parser_end_multiple(&state);
583 break;
584 case '|':
585 format_parser_handle_pipe(&state);
586 break;
587 default:
588 format_parser_read_word(&state);
589 }
paul718e3742002-12-13 20:15:29 +0000590 }
paul718e3742002-12-13 20:15:29 +0000591}
592
593/* Return prompt character of specified node. */
hasso8c328f12004-10-05 21:01:23 +0000594const char *
paul718e3742002-12-13 20:15:29 +0000595cmd_prompt (enum node_type node)
596{
597 struct cmd_node *cnode;
598
599 cnode = vector_slot (cmdvec, node);
600 return cnode->prompt;
601}
602
603/* Install a command into a node. */
604void
605install_element (enum node_type ntype, struct cmd_element *cmd)
606{
607 struct cmd_node *cnode;
pauleb820af2005-09-05 11:54:13 +0000608
609 /* cmd_init hasn't been called */
610 if (!cmdvec)
611 return;
612
paul718e3742002-12-13 20:15:29 +0000613 cnode = vector_slot (cmdvec, ntype);
614
615 if (cnode == NULL)
616 {
617 fprintf (stderr, "Command node %d doesn't exist, please check it\n",
618 ntype);
619 exit (1);
620 }
621
622 vector_set (cnode->cmd_vector, cmd);
Christian Frankecd40b322013-09-30 12:27:51 +0000623 if (cmd->tokens == NULL)
624 cmd->tokens = cmd_parse_format(cmd->string, cmd->doc);
paul718e3742002-12-13 20:15:29 +0000625}
626
Stephen Hemminger2d362d12009-12-21 12:54:58 +0300627static const unsigned char itoa64[] =
paul718e3742002-12-13 20:15:29 +0000628"./0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
629
ajs274a4a42004-12-07 15:39:31 +0000630static void
paul718e3742002-12-13 20:15:29 +0000631to64(char *s, long v, int n)
632{
633 while (--n >= 0)
634 {
635 *s++ = itoa64[v&0x3f];
636 v >>= 6;
637 }
638}
639
ajs274a4a42004-12-07 15:39:31 +0000640static char *
641zencrypt (const char *passwd)
paul718e3742002-12-13 20:15:29 +0000642{
643 char salt[6];
644 struct timeval tv;
645 char *crypt (const char *, const char *);
646
647 gettimeofday(&tv,0);
648
649 to64(&salt[0], random(), 3);
650 to64(&salt[3], tv.tv_usec, 3);
651 salt[5] = '\0';
652
653 return crypt (passwd, salt);
654}
655
656/* This function write configuration of this host. */
ajs274a4a42004-12-07 15:39:31 +0000657static int
paul718e3742002-12-13 20:15:29 +0000658config_write_host (struct vty *vty)
659{
660 if (host.name)
661 vty_out (vty, "hostname %s%s", host.name, VTY_NEWLINE);
662
663 if (host.encrypt)
664 {
665 if (host.password_encrypt)
666 vty_out (vty, "password 8 %s%s", host.password_encrypt, VTY_NEWLINE);
667 if (host.enable_encrypt)
668 vty_out (vty, "enable password 8 %s%s", host.enable_encrypt, VTY_NEWLINE);
669 }
670 else
671 {
672 if (host.password)
673 vty_out (vty, "password %s%s", host.password, VTY_NEWLINE);
674 if (host.enable)
675 vty_out (vty, "enable password %s%s", host.enable, VTY_NEWLINE);
676 }
677
ajs274a4a42004-12-07 15:39:31 +0000678 if (zlog_default->default_lvl != LOG_DEBUG)
ajs82146b82004-12-07 17:15:55 +0000679 {
680 vty_out (vty, "! N.B. The 'log trap' command is deprecated.%s",
681 VTY_NEWLINE);
682 vty_out (vty, "log trap %s%s",
683 zlog_priority[zlog_default->default_lvl], VTY_NEWLINE);
684 }
paul718e3742002-12-13 20:15:29 +0000685
ajs274a4a42004-12-07 15:39:31 +0000686 if (host.logfile && (zlog_default->maxlvl[ZLOG_DEST_FILE] != ZLOG_DISABLED))
paul12ab19f2003-07-26 06:14:55 +0000687 {
ajs274a4a42004-12-07 15:39:31 +0000688 vty_out (vty, "log file %s", host.logfile);
689 if (zlog_default->maxlvl[ZLOG_DEST_FILE] != zlog_default->default_lvl)
690 vty_out (vty, " %s",
691 zlog_priority[zlog_default->maxlvl[ZLOG_DEST_FILE]]);
paul12ab19f2003-07-26 06:14:55 +0000692 vty_out (vty, "%s", VTY_NEWLINE);
693 }
ajs274a4a42004-12-07 15:39:31 +0000694
695 if (zlog_default->maxlvl[ZLOG_DEST_STDOUT] != ZLOG_DISABLED)
696 {
697 vty_out (vty, "log stdout");
698 if (zlog_default->maxlvl[ZLOG_DEST_STDOUT] != zlog_default->default_lvl)
699 vty_out (vty, " %s",
700 zlog_priority[zlog_default->maxlvl[ZLOG_DEST_STDOUT]]);
701 vty_out (vty, "%s", VTY_NEWLINE);
702 }
703
704 if (zlog_default->maxlvl[ZLOG_DEST_MONITOR] == ZLOG_DISABLED)
705 vty_out(vty,"no log monitor%s",VTY_NEWLINE);
706 else if (zlog_default->maxlvl[ZLOG_DEST_MONITOR] != zlog_default->default_lvl)
707 vty_out(vty,"log monitor %s%s",
708 zlog_priority[zlog_default->maxlvl[ZLOG_DEST_MONITOR]],VTY_NEWLINE);
709
710 if (zlog_default->maxlvl[ZLOG_DEST_SYSLOG] != ZLOG_DISABLED)
711 {
712 vty_out (vty, "log syslog");
713 if (zlog_default->maxlvl[ZLOG_DEST_SYSLOG] != zlog_default->default_lvl)
714 vty_out (vty, " %s",
715 zlog_priority[zlog_default->maxlvl[ZLOG_DEST_SYSLOG]]);
716 vty_out (vty, "%s", VTY_NEWLINE);
717 }
718
719 if (zlog_default->facility != LOG_DAEMON)
720 vty_out (vty, "log facility %s%s",
721 facility_name(zlog_default->facility), VTY_NEWLINE);
paul718e3742002-12-13 20:15:29 +0000722
723 if (zlog_default->record_priority == 1)
724 vty_out (vty, "log record-priority%s", VTY_NEWLINE);
725
Andrew J. Schorr1ed72e02007-04-28 22:14:10 +0000726 if (zlog_default->timestamp_precision > 0)
727 vty_out (vty, "log timestamp precision %d%s",
728 zlog_default->timestamp_precision, VTY_NEWLINE);
729
paul718e3742002-12-13 20:15:29 +0000730 if (host.advanced)
731 vty_out (vty, "service advanced-vty%s", VTY_NEWLINE);
732
733 if (host.encrypt)
734 vty_out (vty, "service password-encryption%s", VTY_NEWLINE);
735
736 if (host.lines >= 0)
737 vty_out (vty, "service terminal-length %d%s", host.lines,
738 VTY_NEWLINE);
739
paul3b0c5d92005-03-08 10:43:43 +0000740 if (host.motdfile)
741 vty_out (vty, "banner motd file %s%s", host.motdfile, VTY_NEWLINE);
742 else if (! host.motd)
paul718e3742002-12-13 20:15:29 +0000743 vty_out (vty, "no banner motd%s", VTY_NEWLINE);
744
745 return 1;
746}
747
748/* Utility function for getting command vector. */
ajs274a4a42004-12-07 15:39:31 +0000749static vector
paul718e3742002-12-13 20:15:29 +0000750cmd_node_vector (vector v, enum node_type ntype)
751{
752 struct cmd_node *cnode = vector_slot (v, ntype);
753 return cnode->cmd_vector;
754}
755
ajs274a4a42004-12-07 15:39:31 +0000756#if 0
757/* Filter command vector by symbol. This function is not actually used;
758 * should it be deleted? */
759static int
paul718e3742002-12-13 20:15:29 +0000760cmd_filter_by_symbol (char *command, char *symbol)
761{
762 int i, lim;
763
764 if (strcmp (symbol, "IPV4_ADDRESS") == 0)
765 {
766 i = 0;
767 lim = strlen (command);
768 while (i < lim)
769 {
770 if (! (isdigit ((int) command[i]) || command[i] == '.' || command[i] == '/'))
771 return 1;
772 i++;
773 }
774 return 0;
775 }
776 if (strcmp (symbol, "STRING") == 0)
777 {
778 i = 0;
779 lim = strlen (command);
780 while (i < lim)
781 {
782 if (! (isalpha ((int) command[i]) || command[i] == '_' || command[i] == '-'))
783 return 1;
784 i++;
785 }
786 return 0;
787 }
788 if (strcmp (symbol, "IFNAME") == 0)
789 {
790 i = 0;
791 lim = strlen (command);
792 while (i < lim)
793 {
794 if (! isalnum ((int) command[i]))
795 return 1;
796 i++;
797 }
798 return 0;
799 }
800 return 0;
801}
ajs274a4a42004-12-07 15:39:31 +0000802#endif
paul718e3742002-12-13 20:15:29 +0000803
804/* Completion match types. */
805enum match_type
806{
807 no_match,
808 extend_match,
809 ipv4_prefix_match,
810 ipv4_match,
811 ipv6_prefix_match,
812 ipv6_match,
813 range_match,
814 vararg_match,
815 partly_match,
816 exact_match
817};
818
ajs274a4a42004-12-07 15:39:31 +0000819static enum match_type
hasso8c328f12004-10-05 21:01:23 +0000820cmd_ipv4_match (const char *str)
paul718e3742002-12-13 20:15:29 +0000821{
hasso8c328f12004-10-05 21:01:23 +0000822 const char *sp;
paul718e3742002-12-13 20:15:29 +0000823 int dots = 0, nums = 0;
824 char buf[4];
825
826 if (str == NULL)
827 return partly_match;
828
829 for (;;)
830 {
831 memset (buf, 0, sizeof (buf));
832 sp = str;
833 while (*str != '\0')
834 {
835 if (*str == '.')
836 {
837 if (dots >= 3)
838 return no_match;
839
840 if (*(str + 1) == '.')
841 return no_match;
842
843 if (*(str + 1) == '\0')
844 return partly_match;
845
846 dots++;
847 break;
848 }
849 if (!isdigit ((int) *str))
850 return no_match;
851
852 str++;
853 }
854
855 if (str - sp > 3)
856 return no_match;
857
858 strncpy (buf, sp, str - sp);
859 if (atoi (buf) > 255)
860 return no_match;
861
862 nums++;
863
864 if (*str == '\0')
865 break;
866
867 str++;
868 }
869
870 if (nums < 4)
871 return partly_match;
872
873 return exact_match;
874}
875
ajs274a4a42004-12-07 15:39:31 +0000876static enum match_type
hasso8c328f12004-10-05 21:01:23 +0000877cmd_ipv4_prefix_match (const char *str)
paul718e3742002-12-13 20:15:29 +0000878{
hasso8c328f12004-10-05 21:01:23 +0000879 const char *sp;
paul718e3742002-12-13 20:15:29 +0000880 int dots = 0;
881 char buf[4];
882
883 if (str == NULL)
884 return partly_match;
885
886 for (;;)
887 {
888 memset (buf, 0, sizeof (buf));
889 sp = str;
890 while (*str != '\0' && *str != '/')
891 {
892 if (*str == '.')
893 {
894 if (dots == 3)
895 return no_match;
896
897 if (*(str + 1) == '.' || *(str + 1) == '/')
898 return no_match;
899
900 if (*(str + 1) == '\0')
901 return partly_match;
902
903 dots++;
904 break;
905 }
906
907 if (!isdigit ((int) *str))
908 return no_match;
909
910 str++;
911 }
912
913 if (str - sp > 3)
914 return no_match;
915
916 strncpy (buf, sp, str - sp);
917 if (atoi (buf) > 255)
918 return no_match;
919
920 if (dots == 3)
921 {
922 if (*str == '/')
923 {
924 if (*(str + 1) == '\0')
925 return partly_match;
926
927 str++;
928 break;
929 }
930 else if (*str == '\0')
931 return partly_match;
932 }
933
934 if (*str == '\0')
935 return partly_match;
936
937 str++;
938 }
939
940 sp = str;
941 while (*str != '\0')
942 {
943 if (!isdigit ((int) *str))
944 return no_match;
945
946 str++;
947 }
948
949 if (atoi (sp) > 32)
950 return no_match;
951
952 return exact_match;
953}
954
955#define IPV6_ADDR_STR "0123456789abcdefABCDEF:.%"
956#define IPV6_PREFIX_STR "0123456789abcdefABCDEF:.%/"
957#define STATE_START 1
958#define STATE_COLON 2
959#define STATE_DOUBLE 3
960#define STATE_ADDR 4
961#define STATE_DOT 5
962#define STATE_SLASH 6
963#define STATE_MASK 7
964
paul22e0a9e2003-07-11 17:55:46 +0000965#ifdef HAVE_IPV6
966
ajs274a4a42004-12-07 15:39:31 +0000967static enum match_type
hasso8c328f12004-10-05 21:01:23 +0000968cmd_ipv6_match (const char *str)
paul718e3742002-12-13 20:15:29 +0000969{
hasso726f9b22003-05-25 21:04:54 +0000970 struct sockaddr_in6 sin6_dummy;
971 int ret;
paul718e3742002-12-13 20:15:29 +0000972
973 if (str == NULL)
974 return partly_match;
975
976 if (strspn (str, IPV6_ADDR_STR) != strlen (str))
977 return no_match;
978
hasso726f9b22003-05-25 21:04:54 +0000979 /* use inet_pton that has a better support,
980 * for example inet_pton can support the automatic addresses:
981 * ::1.2.3.4
982 */
983 ret = inet_pton(AF_INET6, str, &sin6_dummy.sin6_addr);
984
985 if (ret == 1)
986 return exact_match;
987
Roman Hoog Antink7c9c6ae2012-05-09 06:35:34 +0000988 return no_match;
paul718e3742002-12-13 20:15:29 +0000989}
990
ajs274a4a42004-12-07 15:39:31 +0000991static enum match_type
hasso8c328f12004-10-05 21:01:23 +0000992cmd_ipv6_prefix_match (const char *str)
paul718e3742002-12-13 20:15:29 +0000993{
994 int state = STATE_START;
995 int colons = 0, nums = 0, double_colon = 0;
996 int mask;
hasso8c328f12004-10-05 21:01:23 +0000997 const char *sp = NULL;
paul718e3742002-12-13 20:15:29 +0000998 char *endptr = NULL;
999
1000 if (str == NULL)
1001 return partly_match;
1002
1003 if (strspn (str, IPV6_PREFIX_STR) != strlen (str))
1004 return no_match;
1005
1006 while (*str != '\0' && state != STATE_MASK)
1007 {
1008 switch (state)
1009 {
1010 case STATE_START:
1011 if (*str == ':')
1012 {
1013 if (*(str + 1) != ':' && *(str + 1) != '\0')
1014 return no_match;
1015 colons--;
1016 state = STATE_COLON;
1017 }
1018 else
1019 {
1020 sp = str;
1021 state = STATE_ADDR;
1022 }
1023
1024 continue;
1025 case STATE_COLON:
1026 colons++;
1027 if (*(str + 1) == '/')
1028 return no_match;
1029 else if (*(str + 1) == ':')
1030 state = STATE_DOUBLE;
1031 else
1032 {
1033 sp = str + 1;
1034 state = STATE_ADDR;
1035 }
1036 break;
1037 case STATE_DOUBLE:
1038 if (double_colon)
1039 return no_match;
1040
1041 if (*(str + 1) == ':')
1042 return no_match;
1043 else
1044 {
1045 if (*(str + 1) != '\0' && *(str + 1) != '/')
1046 colons++;
1047 sp = str + 1;
1048
1049 if (*(str + 1) == '/')
1050 state = STATE_SLASH;
1051 else
1052 state = STATE_ADDR;
1053 }
1054
1055 double_colon++;
1056 nums += 1;
1057 break;
1058 case STATE_ADDR:
1059 if (*(str + 1) == ':' || *(str + 1) == '.'
1060 || *(str + 1) == '\0' || *(str + 1) == '/')
1061 {
1062 if (str - sp > 3)
1063 return no_match;
1064
1065 for (; sp <= str; sp++)
1066 if (*sp == '/')
1067 return no_match;
1068
1069 nums++;
1070
1071 if (*(str + 1) == ':')
1072 state = STATE_COLON;
1073 else if (*(str + 1) == '.')
David Lamparteraa5cf242012-07-19 16:11:50 +02001074 {
1075 if (colons || double_colon)
1076 state = STATE_DOT;
1077 else
1078 return no_match;
1079 }
paul718e3742002-12-13 20:15:29 +00001080 else if (*(str + 1) == '/')
1081 state = STATE_SLASH;
1082 }
1083 break;
1084 case STATE_DOT:
1085 state = STATE_ADDR;
1086 break;
1087 case STATE_SLASH:
1088 if (*(str + 1) == '\0')
1089 return partly_match;
1090
1091 state = STATE_MASK;
1092 break;
1093 default:
1094 break;
1095 }
1096
1097 if (nums > 11)
1098 return no_match;
1099
1100 if (colons > 7)
1101 return no_match;
1102
1103 str++;
1104 }
1105
1106 if (state < STATE_MASK)
1107 return partly_match;
1108
1109 mask = strtol (str, &endptr, 10);
1110 if (*endptr != '\0')
1111 return no_match;
1112
1113 if (mask < 0 || mask > 128)
1114 return no_match;
1115
1116/* I don't know why mask < 13 makes command match partly.
1117 Forgive me to make this comments. I Want to set static default route
1118 because of lack of function to originate default in ospf6d; sorry
1119 yasu
1120 if (mask < 13)
1121 return partly_match;
1122*/
1123
1124 return exact_match;
1125}
1126
paul22e0a9e2003-07-11 17:55:46 +00001127#endif /* HAVE_IPV6 */
1128
paul718e3742002-12-13 20:15:29 +00001129#define DECIMAL_STRLEN_MAX 10
1130
ajs274a4a42004-12-07 15:39:31 +00001131static int
hasso8c328f12004-10-05 21:01:23 +00001132cmd_range_match (const char *range, const char *str)
paul718e3742002-12-13 20:15:29 +00001133{
1134 char *p;
1135 char buf[DECIMAL_STRLEN_MAX + 1];
1136 char *endptr = NULL;
1137 unsigned long min, max, val;
1138
1139 if (str == NULL)
1140 return 1;
1141
1142 val = strtoul (str, &endptr, 10);
1143 if (*endptr != '\0')
1144 return 0;
1145
1146 range++;
1147 p = strchr (range, '-');
1148 if (p == NULL)
1149 return 0;
1150 if (p - range > DECIMAL_STRLEN_MAX)
1151 return 0;
1152 strncpy (buf, range, p - range);
1153 buf[p - range] = '\0';
1154 min = strtoul (buf, &endptr, 10);
1155 if (*endptr != '\0')
1156 return 0;
1157
1158 range = p + 1;
1159 p = strchr (range, '>');
1160 if (p == NULL)
1161 return 0;
1162 if (p - range > DECIMAL_STRLEN_MAX)
1163 return 0;
1164 strncpy (buf, range, p - range);
1165 buf[p - range] = '\0';
1166 max = strtoul (buf, &endptr, 10);
1167 if (*endptr != '\0')
1168 return 0;
1169
1170 if (val < min || val > max)
1171 return 0;
1172
1173 return 1;
1174}
1175
ajs274a4a42004-12-07 15:39:31 +00001176static enum match_type
Christian Frankecd40b322013-09-30 12:27:51 +00001177cmd_word_match(struct cmd_token *token,
1178 enum filter_type filter,
1179 const char *word)
paul718e3742002-12-13 20:15:29 +00001180{
hasso8c328f12004-10-05 21:01:23 +00001181 const char *str;
paul718e3742002-12-13 20:15:29 +00001182 enum match_type match_type;
paul909a2152005-03-14 17:41:45 +00001183
Christian Frankecd40b322013-09-30 12:27:51 +00001184 str = token->cmd;
paul718e3742002-12-13 20:15:29 +00001185
Christian Frankecd40b322013-09-30 12:27:51 +00001186 if (filter == FILTER_RELAXED)
1187 if (!word || !strlen(word))
1188 return partly_match;
paul718e3742002-12-13 20:15:29 +00001189
Christian Frankecd40b322013-09-30 12:27:51 +00001190 if (!word)
1191 return no_match;
paul909a2152005-03-14 17:41:45 +00001192
David Lamparter10bac802015-05-05 11:10:20 +02001193 switch (token->terminal)
Christian Frankecd40b322013-09-30 12:27:51 +00001194 {
David Lamparter10bac802015-05-05 11:10:20 +02001195 case TERMINAL_VARARG:
1196 return vararg_match;
1197
1198 case TERMINAL_RANGE:
1199 if (cmd_range_match(str, word))
1200 return range_match;
1201 break;
1202
1203 case TERMINAL_IPV6:
1204 match_type = cmd_ipv6_match(word);
1205 if ((filter == FILTER_RELAXED && match_type != no_match)
Christian Frankecd40b322013-09-30 12:27:51 +00001206 || (filter == FILTER_STRICT && match_type == exact_match))
David Lamparter10bac802015-05-05 11:10:20 +02001207 return ipv6_match;
1208 break;
1209
1210 case TERMINAL_IPV6_PREFIX:
1211 match_type = cmd_ipv6_prefix_match(word);
1212 if ((filter == FILTER_RELAXED && match_type != no_match)
1213 || (filter == FILTER_STRICT && match_type == exact_match))
1214 return ipv6_prefix_match;
1215 break;
1216
1217 case TERMINAL_IPV4:
1218 match_type = cmd_ipv4_match(word);
1219 if ((filter == FILTER_RELAXED && match_type != no_match)
1220 || (filter == FILTER_STRICT && match_type == exact_match))
1221 return ipv4_match;
1222 break;
1223
1224 case TERMINAL_IPV4_PREFIX:
1225 match_type = cmd_ipv4_prefix_match(word);
1226 if ((filter == FILTER_RELAXED && match_type != no_match)
1227 || (filter == FILTER_STRICT && match_type == exact_match))
1228 return ipv4_prefix_match;
1229 break;
1230
1231 case TERMINAL_OPTION:
1232 case TERMINAL_VARIABLE:
1233 return extend_match;
1234
1235 case TERMINAL_LITERAL:
1236 if (filter == FILTER_RELAXED && !strncmp(str, word, strlen(word)))
1237 {
1238 if (!strcmp(str, word))
1239 return exact_match;
1240 return partly_match;
1241 }
1242 if (filter == FILTER_STRICT && !strcmp(str, word))
1243 return exact_match;
1244 break;
1245
1246 default:
1247 assert (0);
Christian Frankecd40b322013-09-30 12:27:51 +00001248 }
paul718e3742002-12-13 20:15:29 +00001249
Christian Frankecd40b322013-09-30 12:27:51 +00001250 return no_match;
paul718e3742002-12-13 20:15:29 +00001251}
1252
Christian Frankecd40b322013-09-30 12:27:51 +00001253struct cmd_matcher
1254{
1255 struct cmd_element *cmd; /* The command element the matcher is using */
1256 enum filter_type filter; /* Whether to use strict or relaxed matching */
1257 vector vline; /* The tokenized commandline which is to be matched */
1258 unsigned int index; /* The index up to which matching should be done */
1259
1260 /* If set, construct a list of matches at the position given by index */
1261 enum match_type *match_type;
1262 vector *match;
1263
1264 unsigned int word_index; /* iterating over vline */
1265};
1266
1267static int
1268push_argument(int *argc, const char **argv, const char *arg)
1269{
1270 if (!arg || !strlen(arg))
1271 arg = NULL;
1272
1273 if (!argc || !argv)
1274 return 0;
1275
1276 if (*argc >= CMD_ARGC_MAX)
1277 return -1;
1278
1279 argv[(*argc)++] = arg;
1280 return 0;
1281}
1282
1283static void
1284cmd_matcher_record_match(struct cmd_matcher *matcher,
1285 enum match_type match_type,
1286 struct cmd_token *token)
1287{
1288 if (matcher->word_index != matcher->index)
1289 return;
1290
1291 if (matcher->match)
1292 {
1293 if (!*matcher->match)
1294 *matcher->match = vector_init(VECTOR_MIN_SIZE);
1295 vector_set(*matcher->match, token);
1296 }
1297
1298 if (matcher->match_type)
1299 {
1300 if (match_type > *matcher->match_type)
1301 *matcher->match_type = match_type;
1302 }
1303}
1304
1305static int
1306cmd_matcher_words_left(struct cmd_matcher *matcher)
1307{
1308 return matcher->word_index < vector_active(matcher->vline);
1309}
1310
1311static const char*
1312cmd_matcher_get_word(struct cmd_matcher *matcher)
1313{
1314 assert(cmd_matcher_words_left(matcher));
1315
1316 return vector_slot(matcher->vline, matcher->word_index);
1317}
1318
1319static enum matcher_rv
1320cmd_matcher_match_terminal(struct cmd_matcher *matcher,
1321 struct cmd_token *token,
1322 int *argc, const char **argv)
1323{
1324 const char *word;
1325 enum match_type word_match;
1326
1327 assert(token->type == TOKEN_TERMINAL);
1328
1329 if (!cmd_matcher_words_left(matcher))
1330 {
David Lamparter10bac802015-05-05 11:10:20 +02001331 if (token->terminal == TERMINAL_OPTION)
Christian Frankecd40b322013-09-30 12:27:51 +00001332 return MATCHER_OK; /* missing optional args are NOT pushed as NULL */
1333 else
1334 return MATCHER_INCOMPLETE;
1335 }
1336
1337 word = cmd_matcher_get_word(matcher);
1338 word_match = cmd_word_match(token, matcher->filter, word);
1339 if (word_match == no_match)
1340 return MATCHER_NO_MATCH;
1341
1342 /* We have to record the input word as argument if it matched
1343 * against a variable. */
David Lamparter14162932015-05-12 17:18:04 +02001344 if (TERMINAL_RECORD (token->terminal))
Christian Frankecd40b322013-09-30 12:27:51 +00001345 {
1346 if (push_argument(argc, argv, word))
1347 return MATCHER_EXCEED_ARGC_MAX;
1348 }
1349
1350 cmd_matcher_record_match(matcher, word_match, token);
1351
1352 matcher->word_index++;
1353
1354 /* A vararg token should consume all left over words as arguments */
David Lamparter10bac802015-05-05 11:10:20 +02001355 if (token->terminal == TERMINAL_VARARG)
Christian Frankecd40b322013-09-30 12:27:51 +00001356 while (cmd_matcher_words_left(matcher))
1357 {
1358 word = cmd_matcher_get_word(matcher);
1359 if (word && strlen(word))
1360 push_argument(argc, argv, word);
1361 matcher->word_index++;
1362 }
1363
1364 return MATCHER_OK;
1365}
1366
1367static enum matcher_rv
1368cmd_matcher_match_multiple(struct cmd_matcher *matcher,
1369 struct cmd_token *token,
1370 int *argc, const char **argv)
1371{
1372 enum match_type multiple_match;
1373 unsigned int multiple_index;
1374 const char *word;
David Lamparterab90fc02015-03-03 09:07:25 +01001375 const char *arg = NULL;
Christian Frankecd40b322013-09-30 12:27:51 +00001376 struct cmd_token *word_token;
1377 enum match_type word_match;
1378
1379 assert(token->type == TOKEN_MULTIPLE);
1380
1381 multiple_match = no_match;
1382
1383 if (!cmd_matcher_words_left(matcher))
1384 return MATCHER_INCOMPLETE;
1385
1386 word = cmd_matcher_get_word(matcher);
1387 for (multiple_index = 0;
1388 multiple_index < vector_active(token->multiple);
1389 multiple_index++)
1390 {
1391 word_token = vector_slot(token->multiple, multiple_index);
1392
1393 word_match = cmd_word_match(word_token, matcher->filter, word);
1394 if (word_match == no_match)
1395 continue;
1396
1397 cmd_matcher_record_match(matcher, word_match, word_token);
1398
1399 if (word_match > multiple_match)
1400 {
1401 multiple_match = word_match;
1402 arg = word;
1403 }
1404 /* To mimic the behavior of the old command implementation, we
1405 * tolerate any ambiguities here :/ */
1406 }
1407
1408 matcher->word_index++;
1409
1410 if (multiple_match == no_match)
1411 return MATCHER_NO_MATCH;
1412
1413 if (push_argument(argc, argv, arg))
1414 return MATCHER_EXCEED_ARGC_MAX;
1415
1416 return MATCHER_OK;
1417}
1418
1419static enum matcher_rv
1420cmd_matcher_read_keywords(struct cmd_matcher *matcher,
1421 struct cmd_token *token,
1422 vector args_vector)
paul718e3742002-12-13 20:15:29 +00001423{
hasso8c328f12004-10-05 21:01:23 +00001424 unsigned int i;
Christian Frankecd40b322013-09-30 12:27:51 +00001425 unsigned long keyword_mask;
1426 unsigned int keyword_found;
1427 enum match_type keyword_match;
1428 enum match_type word_match;
1429 vector keyword_vector;
1430 struct cmd_token *word_token;
1431 const char *word;
1432 int keyword_argc;
1433 const char **keyword_argv;
Paul Jakma7aa9dce2014-09-19 14:42:23 +01001434 enum matcher_rv rv = MATCHER_NO_MATCH;
Christian Frankecd40b322013-09-30 12:27:51 +00001435
1436 keyword_mask = 0;
1437 while (1)
1438 {
1439 if (!cmd_matcher_words_left(matcher))
1440 return MATCHER_OK;
1441
1442 word = cmd_matcher_get_word(matcher);
1443
1444 keyword_found = -1;
1445 keyword_match = no_match;
1446 for (i = 0; i < vector_active(token->keyword); i++)
1447 {
1448 if (keyword_mask & (1 << i))
1449 continue;
1450
1451 keyword_vector = vector_slot(token->keyword, i);
1452 word_token = vector_slot(keyword_vector, 0);
1453
1454 word_match = cmd_word_match(word_token, matcher->filter, word);
1455 if (word_match == no_match)
1456 continue;
1457
1458 cmd_matcher_record_match(matcher, word_match, word_token);
1459
1460 if (word_match > keyword_match)
1461 {
1462 keyword_match = word_match;
1463 keyword_found = i;
1464 }
1465 else if (word_match == keyword_match)
1466 {
1467 if (matcher->word_index != matcher->index || args_vector)
1468 return MATCHER_AMBIGUOUS;
1469 }
1470 }
1471
1472 if (keyword_found == (unsigned int)-1)
1473 return MATCHER_NO_MATCH;
1474
1475 matcher->word_index++;
1476
1477 if (matcher->word_index > matcher->index)
1478 return MATCHER_OK;
1479
1480 keyword_mask |= (1 << keyword_found);
1481
1482 if (args_vector)
1483 {
1484 keyword_argc = 0;
1485 keyword_argv = XMALLOC(MTYPE_TMP, (CMD_ARGC_MAX + 1) * sizeof(char*));
1486 /* We use -1 as a marker for unused fields as NULL might be a valid value */
1487 for (i = 0; i < CMD_ARGC_MAX + 1; i++)
1488 keyword_argv[i] = (void*)-1;
1489 vector_set_index(args_vector, keyword_found, keyword_argv);
1490 }
1491 else
1492 {
1493 keyword_argv = NULL;
1494 }
1495
1496 keyword_vector = vector_slot(token->keyword, keyword_found);
1497 /* the keyword itself is at 0. We are only interested in the arguments,
1498 * so start counting at 1. */
1499 for (i = 1; i < vector_active(keyword_vector); i++)
1500 {
1501 word_token = vector_slot(keyword_vector, i);
1502
1503 switch (word_token->type)
1504 {
1505 case TOKEN_TERMINAL:
1506 rv = cmd_matcher_match_terminal(matcher, word_token,
1507 &keyword_argc, keyword_argv);
1508 break;
1509 case TOKEN_MULTIPLE:
1510 rv = cmd_matcher_match_multiple(matcher, word_token,
1511 &keyword_argc, keyword_argv);
1512 break;
1513 case TOKEN_KEYWORD:
1514 assert(!"Keywords should never be nested.");
1515 break;
1516 }
1517
1518 if (MATCHER_ERROR(rv))
1519 return rv;
1520
1521 if (matcher->word_index > matcher->index)
1522 return MATCHER_OK;
1523 }
1524 }
1525 /* not reached */
1526}
1527
1528static enum matcher_rv
1529cmd_matcher_build_keyword_args(struct cmd_matcher *matcher,
1530 struct cmd_token *token,
1531 int *argc, const char **argv,
1532 vector keyword_args_vector)
1533{
1534 unsigned int i, j;
1535 const char **keyword_args;
1536 vector keyword_vector;
1537 struct cmd_token *word_token;
1538 const char *arg;
1539 enum matcher_rv rv;
1540
1541 rv = MATCHER_OK;
1542
1543 if (keyword_args_vector == NULL)
1544 return rv;
1545
1546 for (i = 0; i < vector_active(token->keyword); i++)
1547 {
1548 keyword_vector = vector_slot(token->keyword, i);
1549 keyword_args = vector_lookup(keyword_args_vector, i);
1550
1551 if (vector_active(keyword_vector) == 1)
1552 {
1553 /* this is a keyword without arguments */
1554 if (keyword_args)
1555 {
1556 word_token = vector_slot(keyword_vector, 0);
1557 arg = word_token->cmd;
1558 }
1559 else
1560 {
1561 arg = NULL;
1562 }
1563
1564 if (push_argument(argc, argv, arg))
1565 rv = MATCHER_EXCEED_ARGC_MAX;
1566 }
1567 else
1568 {
1569 /* this is a keyword with arguments */
1570 if (keyword_args)
1571 {
1572 /* the keyword was present, so just fill in the arguments */
1573 for (j = 0; keyword_args[j] != (void*)-1; j++)
1574 if (push_argument(argc, argv, keyword_args[j]))
1575 rv = MATCHER_EXCEED_ARGC_MAX;
1576 XFREE(MTYPE_TMP, keyword_args);
1577 }
1578 else
1579 {
1580 /* the keyword was not present, insert NULL for the arguments
1581 * the keyword would have taken. */
1582 for (j = 1; j < vector_active(keyword_vector); j++)
1583 {
1584 word_token = vector_slot(keyword_vector, j);
1585 if ((word_token->type == TOKEN_TERMINAL
David Lamparter14162932015-05-12 17:18:04 +02001586 && TERMINAL_RECORD (word_token->terminal))
Christian Frankecd40b322013-09-30 12:27:51 +00001587 || word_token->type == TOKEN_MULTIPLE)
1588 {
1589 if (push_argument(argc, argv, NULL))
1590 rv = MATCHER_EXCEED_ARGC_MAX;
1591 }
1592 }
1593 }
1594 }
1595 }
1596 vector_free(keyword_args_vector);
1597 return rv;
1598}
1599
1600static enum matcher_rv
1601cmd_matcher_match_keyword(struct cmd_matcher *matcher,
1602 struct cmd_token *token,
1603 int *argc, const char **argv)
1604{
1605 vector keyword_args_vector;
1606 enum matcher_rv reader_rv;
1607 enum matcher_rv builder_rv;
1608
1609 assert(token->type == TOKEN_KEYWORD);
1610
1611 if (argc && argv)
1612 keyword_args_vector = vector_init(VECTOR_MIN_SIZE);
1613 else
1614 keyword_args_vector = NULL;
1615
1616 reader_rv = cmd_matcher_read_keywords(matcher, token, keyword_args_vector);
1617 builder_rv = cmd_matcher_build_keyword_args(matcher, token, argc,
1618 argv, keyword_args_vector);
1619 /* keyword_args_vector is consumed by cmd_matcher_build_keyword_args */
1620
1621 if (!MATCHER_ERROR(reader_rv) && MATCHER_ERROR(builder_rv))
1622 return builder_rv;
1623
1624 return reader_rv;
1625}
1626
1627static void
1628cmd_matcher_init(struct cmd_matcher *matcher,
1629 struct cmd_element *cmd,
1630 enum filter_type filter,
1631 vector vline,
1632 unsigned int index,
1633 enum match_type *match_type,
1634 vector *match)
1635{
1636 memset(matcher, 0, sizeof(*matcher));
1637
1638 matcher->cmd = cmd;
1639 matcher->filter = filter;
1640 matcher->vline = vline;
1641 matcher->index = index;
1642
1643 matcher->match_type = match_type;
1644 if (matcher->match_type)
1645 *matcher->match_type = no_match;
1646 matcher->match = match;
1647
1648 matcher->word_index = 0;
1649}
1650
1651static enum matcher_rv
1652cmd_element_match(struct cmd_element *cmd_element,
1653 enum filter_type filter,
1654 vector vline,
1655 unsigned int index,
1656 enum match_type *match_type,
1657 vector *match,
1658 int *argc,
1659 const char **argv)
1660{
1661 struct cmd_matcher matcher;
1662 unsigned int token_index;
Paul Jakma7aa9dce2014-09-19 14:42:23 +01001663 enum matcher_rv rv = MATCHER_NO_MATCH;
Christian Frankecd40b322013-09-30 12:27:51 +00001664
1665 cmd_matcher_init(&matcher, cmd_element, filter,
1666 vline, index, match_type, match);
1667
1668 if (argc != NULL)
1669 *argc = 0;
1670
1671 for (token_index = 0;
1672 token_index < vector_active(cmd_element->tokens);
1673 token_index++)
1674 {
1675 struct cmd_token *token = vector_slot(cmd_element->tokens, token_index);
1676
1677 switch (token->type)
1678 {
1679 case TOKEN_TERMINAL:
1680 rv = cmd_matcher_match_terminal(&matcher, token, argc, argv);
1681 break;
1682 case TOKEN_MULTIPLE:
1683 rv = cmd_matcher_match_multiple(&matcher, token, argc, argv);
1684 break;
1685 case TOKEN_KEYWORD:
1686 rv = cmd_matcher_match_keyword(&matcher, token, argc, argv);
1687 }
1688
1689 if (MATCHER_ERROR(rv))
1690 return rv;
1691
1692 if (matcher.word_index > index)
1693 return MATCHER_OK;
1694 }
1695
1696 /* return MATCHER_COMPLETE if all words were consumed */
1697 if (matcher.word_index >= vector_active(vline))
1698 return MATCHER_COMPLETE;
1699
1700 /* return MATCHER_COMPLETE also if only an empty word is left. */
1701 if (matcher.word_index == vector_active(vline) - 1
1702 && (!vector_slot(vline, matcher.word_index)
1703 || !strlen((char*)vector_slot(vline, matcher.word_index))))
1704 return MATCHER_COMPLETE;
1705
1706 return MATCHER_NO_MATCH; /* command is too long to match */
1707}
1708
1709/**
1710 * Filter a given vector of commands against a given commandline and
1711 * calculate possible completions.
1712 *
1713 * @param commands A vector of struct cmd_element*. Commands that don't
1714 * match against the given command line will be overwritten
1715 * with NULL in that vector.
1716 * @param filter Either FILTER_RELAXED or FILTER_STRICT. This basically
1717 * determines how incomplete commands are handled, compare with
1718 * cmd_word_match for details.
1719 * @param vline A vector of char* containing the tokenized commandline.
1720 * @param index Only match up to the given token of the commandline.
1721 * @param match_type Record the type of the best match here.
1722 * @param matches Record the matches here. For each cmd_element in the commands
1723 * vector, a match vector will be created in the matches vector.
1724 * That vector will contain all struct command_token* of the
1725 * cmd_element which matched against the given vline at the given
1726 * index.
1727 * @return A code specifying if an error occured. If all went right, it's
1728 * CMD_SUCCESS.
1729 */
1730static int
1731cmd_vector_filter(vector commands,
1732 enum filter_type filter,
1733 vector vline,
1734 unsigned int index,
1735 enum match_type *match_type,
1736 vector *matches)
1737{
1738 unsigned int i;
paul718e3742002-12-13 20:15:29 +00001739 struct cmd_element *cmd_element;
Christian Frankecd40b322013-09-30 12:27:51 +00001740 enum match_type best_match;
1741 enum match_type element_match;
1742 enum matcher_rv matcher_rv;
paul909a2152005-03-14 17:41:45 +00001743
Christian Frankecd40b322013-09-30 12:27:51 +00001744 best_match = no_match;
1745 *matches = vector_init(VECTOR_MIN_SIZE);
paul718e3742002-12-13 20:15:29 +00001746
Christian Frankecd40b322013-09-30 12:27:51 +00001747 for (i = 0; i < vector_active (commands); i++)
1748 if ((cmd_element = vector_slot (commands, i)) != NULL)
paul718e3742002-12-13 20:15:29 +00001749 {
Christian Frankecd40b322013-09-30 12:27:51 +00001750 vector_set_index(*matches, i, NULL);
1751 matcher_rv = cmd_element_match(cmd_element, filter,
1752 vline, index,
1753 &element_match,
1754 (vector*)&vector_slot(*matches, i),
1755 NULL, NULL);
1756 if (MATCHER_ERROR(matcher_rv))
1757 {
1758 vector_slot(commands, i) = NULL;
1759 if (matcher_rv == MATCHER_AMBIGUOUS)
1760 return CMD_ERR_AMBIGUOUS;
1761 if (matcher_rv == MATCHER_EXCEED_ARGC_MAX)
1762 return CMD_ERR_EXEED_ARGC_MAX;
1763 }
1764 else if (element_match > best_match)
1765 {
1766 best_match = element_match;
1767 }
paul718e3742002-12-13 20:15:29 +00001768 }
Christian Frankecd40b322013-09-30 12:27:51 +00001769 *match_type = best_match;
1770 return CMD_SUCCESS;
1771}
1772
1773/**
1774 * Check whether a given commandline is complete if used for a specific
1775 * cmd_element.
1776 *
1777 * @param cmd_element A cmd_element against which the commandline should be
1778 * checked.
1779 * @param vline The tokenized commandline.
1780 * @return 1 if the given commandline is complete, 0 otherwise.
1781 */
1782static int
1783cmd_is_complete(struct cmd_element *cmd_element,
1784 vector vline)
1785{
1786 enum matcher_rv rv;
1787
1788 rv = cmd_element_match(cmd_element,
1789 FILTER_RELAXED,
1790 vline, -1,
1791 NULL, NULL,
1792 NULL, NULL);
1793 return (rv == MATCHER_COMPLETE);
1794}
1795
1796/**
1797 * Parse a given commandline and construct a list of arguments for the
1798 * given command_element.
1799 *
1800 * @param cmd_element The cmd_element for which we want to construct arguments.
1801 * @param vline The tokenized commandline.
1802 * @param argc Where to store the argument count.
1803 * @param argv Where to store the argument list. Should be at least
1804 * CMD_ARGC_MAX elements long.
1805 * @return CMD_SUCCESS if everything went alright, an error otherwise.
1806 */
1807static int
1808cmd_parse(struct cmd_element *cmd_element,
1809 vector vline,
1810 int *argc, const char **argv)
1811{
1812 enum matcher_rv rv = cmd_element_match(cmd_element,
1813 FILTER_RELAXED,
1814 vline, -1,
1815 NULL, NULL,
1816 argc, argv);
1817 switch (rv)
1818 {
1819 case MATCHER_COMPLETE:
1820 return CMD_SUCCESS;
1821
1822 case MATCHER_NO_MATCH:
1823 return CMD_ERR_NO_MATCH;
1824
1825 case MATCHER_AMBIGUOUS:
1826 return CMD_ERR_AMBIGUOUS;
1827
1828 case MATCHER_EXCEED_ARGC_MAX:
1829 return CMD_ERR_EXEED_ARGC_MAX;
1830
1831 default:
1832 return CMD_ERR_INCOMPLETE;
1833 }
paul718e3742002-12-13 20:15:29 +00001834}
1835
1836/* Check ambiguous match */
ajs274a4a42004-12-07 15:39:31 +00001837static int
Christian Frankecd40b322013-09-30 12:27:51 +00001838is_cmd_ambiguous (vector cmd_vector,
1839 const char *command,
1840 vector matches,
1841 enum match_type type)
paul718e3742002-12-13 20:15:29 +00001842{
hasso8c328f12004-10-05 21:01:23 +00001843 unsigned int i;
1844 unsigned int j;
1845 const char *str = NULL;
hasso8c328f12004-10-05 21:01:23 +00001846 const char *matched = NULL;
Christian Frankecd40b322013-09-30 12:27:51 +00001847 vector match_vector;
1848 struct cmd_token *cmd_token;
paul909a2152005-03-14 17:41:45 +00001849
Christian Frankecd40b322013-09-30 12:27:51 +00001850 if (command == NULL)
1851 command = "";
1852
1853 for (i = 0; i < vector_active (matches); i++)
1854 if ((match_vector = vector_slot (matches, i)) != NULL)
paul718e3742002-12-13 20:15:29 +00001855 {
1856 int match = 0;
1857
Christian Frankecd40b322013-09-30 12:27:51 +00001858 for (j = 0; j < vector_active (match_vector); j++)
1859 if ((cmd_token = vector_slot (match_vector, j)) != NULL)
paul909a2152005-03-14 17:41:45 +00001860 {
1861 enum match_type ret;
Christian Frankecd40b322013-09-30 12:27:51 +00001862
1863 assert(cmd_token->type == TOKEN_TERMINAL);
1864 if (cmd_token->type != TOKEN_TERMINAL)
1865 continue;
1866
1867 str = cmd_token->cmd;
paul718e3742002-12-13 20:15:29 +00001868
paul909a2152005-03-14 17:41:45 +00001869 switch (type)
1870 {
1871 case exact_match:
David Lamparter14162932015-05-12 17:18:04 +02001872 if (!TERMINAL_RECORD (cmd_token->terminal)
paul909a2152005-03-14 17:41:45 +00001873 && strcmp (command, str) == 0)
1874 match++;
1875 break;
1876 case partly_match:
David Lamparter14162932015-05-12 17:18:04 +02001877 if (!TERMINAL_RECORD (cmd_token->terminal)
paul909a2152005-03-14 17:41:45 +00001878 && strncmp (command, str, strlen (command)) == 0)
1879 {
1880 if (matched && strcmp (matched, str) != 0)
1881 return 1; /* There is ambiguous match. */
1882 else
1883 matched = str;
1884 match++;
1885 }
1886 break;
1887 case range_match:
1888 if (cmd_range_match (str, command))
1889 {
1890 if (matched && strcmp (matched, str) != 0)
1891 return 1;
1892 else
1893 matched = str;
1894 match++;
1895 }
1896 break;
1897#ifdef HAVE_IPV6
1898 case ipv6_match:
David Lamparter10bac802015-05-05 11:10:20 +02001899 if (cmd_token->terminal == TERMINAL_IPV6)
paul909a2152005-03-14 17:41:45 +00001900 match++;
1901 break;
1902 case ipv6_prefix_match:
1903 if ((ret = cmd_ipv6_prefix_match (command)) != no_match)
1904 {
1905 if (ret == partly_match)
1906 return 2; /* There is incomplete match. */
paul718e3742002-12-13 20:15:29 +00001907
paul909a2152005-03-14 17:41:45 +00001908 match++;
1909 }
1910 break;
1911#endif /* HAVE_IPV6 */
1912 case ipv4_match:
David Lamparter10bac802015-05-05 11:10:20 +02001913 if (cmd_token->terminal == TERMINAL_IPV4)
paul718e3742002-12-13 20:15:29 +00001914 match++;
paul909a2152005-03-14 17:41:45 +00001915 break;
1916 case ipv4_prefix_match:
1917 if ((ret = cmd_ipv4_prefix_match (command)) != no_match)
1918 {
1919 if (ret == partly_match)
1920 return 2; /* There is incomplete match. */
paul718e3742002-12-13 20:15:29 +00001921
paul909a2152005-03-14 17:41:45 +00001922 match++;
1923 }
1924 break;
1925 case extend_match:
David Lamparter14162932015-05-12 17:18:04 +02001926 if (TERMINAL_RECORD (cmd_token->terminal))
paul718e3742002-12-13 20:15:29 +00001927 match++;
paul909a2152005-03-14 17:41:45 +00001928 break;
1929 case no_match:
1930 default:
1931 break;
1932 }
1933 }
1934 if (!match)
Christian Frankecd40b322013-09-30 12:27:51 +00001935 vector_slot (cmd_vector, i) = NULL;
paul718e3742002-12-13 20:15:29 +00001936 }
1937 return 0;
1938}
1939
1940/* If src matches dst return dst string, otherwise return NULL */
ajs274a4a42004-12-07 15:39:31 +00001941static const char *
David Lamparter10bac802015-05-05 11:10:20 +02001942cmd_entry_function (const char *src, struct cmd_token *token)
paul718e3742002-12-13 20:15:29 +00001943{
David Lamparter10bac802015-05-05 11:10:20 +02001944 const char *dst = token->cmd;
1945
paul718e3742002-12-13 20:15:29 +00001946 /* Skip variable arguments. */
David Lamparter14162932015-05-12 17:18:04 +02001947 if (TERMINAL_RECORD (token->terminal))
1948 return NULL;
paul718e3742002-12-13 20:15:29 +00001949
1950 /* In case of 'command \t', given src is NULL string. */
1951 if (src == NULL)
1952 return dst;
1953
1954 /* Matched with input string. */
1955 if (strncmp (src, dst, strlen (src)) == 0)
1956 return dst;
1957
1958 return NULL;
1959}
1960
1961/* If src matches dst return dst string, otherwise return NULL */
1962/* This version will return the dst string always if it is
1963 CMD_VARIABLE for '?' key processing */
ajs274a4a42004-12-07 15:39:31 +00001964static const char *
David Lamparter10bac802015-05-05 11:10:20 +02001965cmd_entry_function_desc (const char *src, struct cmd_token *token)
paul718e3742002-12-13 20:15:29 +00001966{
David Lamparter10bac802015-05-05 11:10:20 +02001967 const char *dst = token->cmd;
paul718e3742002-12-13 20:15:29 +00001968
David Lamparter10bac802015-05-05 11:10:20 +02001969 switch (token->terminal)
paul718e3742002-12-13 20:15:29 +00001970 {
David Lamparter10bac802015-05-05 11:10:20 +02001971 case TERMINAL_VARARG:
1972 return dst;
1973
1974 case TERMINAL_RANGE:
1975 if (cmd_range_match (dst, src))
1976 return dst;
1977 else
1978 return NULL;
1979
1980 case TERMINAL_IPV6:
1981 if (cmd_ipv6_match (src))
1982 return dst;
1983 else
1984 return NULL;
1985
1986 case TERMINAL_IPV6_PREFIX:
1987 if (cmd_ipv6_prefix_match (src))
1988 return dst;
1989 else
1990 return NULL;
1991
1992 case TERMINAL_IPV4:
1993 if (cmd_ipv4_match (src))
1994 return dst;
1995 else
1996 return NULL;
1997
1998 case TERMINAL_IPV4_PREFIX:
1999 if (cmd_ipv4_prefix_match (src))
2000 return dst;
2001 else
2002 return NULL;
2003
2004 /* Optional or variable commands always match on '?' */
2005 case TERMINAL_OPTION:
2006 case TERMINAL_VARIABLE:
2007 return dst;
2008
2009 case TERMINAL_LITERAL:
2010 /* In case of 'command \t', given src is NULL string. */
2011 if (src == NULL)
2012 return dst;
2013
2014 if (strncmp (src, dst, strlen (src)) == 0)
2015 return dst;
2016 else
2017 return NULL;
2018
2019 default:
2020 assert(0);
David Lamparterf1fc3272015-05-13 12:44:50 +02002021 return NULL;
paul718e3742002-12-13 20:15:29 +00002022 }
paul718e3742002-12-13 20:15:29 +00002023}
2024
Christian Frankecd40b322013-09-30 12:27:51 +00002025/**
2026 * Check whether a string is already present in a vector of strings.
2027 * @param v A vector of char*.
2028 * @param str A char*.
2029 * @return 0 if str is already present in the vector, 1 otherwise.
2030 */
ajs274a4a42004-12-07 15:39:31 +00002031static int
hasso8c328f12004-10-05 21:01:23 +00002032cmd_unique_string (vector v, const char *str)
paul718e3742002-12-13 20:15:29 +00002033{
hasso8c328f12004-10-05 21:01:23 +00002034 unsigned int i;
paul718e3742002-12-13 20:15:29 +00002035 char *match;
2036
paul55468c82005-03-14 20:19:01 +00002037 for (i = 0; i < vector_active (v); i++)
paul718e3742002-12-13 20:15:29 +00002038 if ((match = vector_slot (v, i)) != NULL)
2039 if (strcmp (match, str) == 0)
2040 return 0;
2041 return 1;
2042}
2043
Christian Frankecd40b322013-09-30 12:27:51 +00002044/**
2045 * Check whether a struct cmd_token matching a given string is already
2046 * present in a vector of struct cmd_token.
2047 * @param v A vector of struct cmd_token*.
2048 * @param str A char* which should be searched for.
2049 * @return 0 if there is a struct cmd_token* with its cmd matching str,
2050 * 1 otherwise.
2051 */
ajs274a4a42004-12-07 15:39:31 +00002052static int
hasso8c328f12004-10-05 21:01:23 +00002053desc_unique_string (vector v, const char *str)
paul718e3742002-12-13 20:15:29 +00002054{
hasso8c328f12004-10-05 21:01:23 +00002055 unsigned int i;
Christian Frankecd40b322013-09-30 12:27:51 +00002056 struct cmd_token *token;
paul718e3742002-12-13 20:15:29 +00002057
paul55468c82005-03-14 20:19:01 +00002058 for (i = 0; i < vector_active (v); i++)
Christian Frankecd40b322013-09-30 12:27:51 +00002059 if ((token = vector_slot (v, i)) != NULL)
2060 if (strcmp (token->cmd, str) == 0)
2061 return 0;
2062 return 1;
paul718e3742002-12-13 20:15:29 +00002063}
2064
ajs274a4a42004-12-07 15:39:31 +00002065static int
paulb92938a2002-12-13 21:20:42 +00002066cmd_try_do_shortcut (enum node_type node, char* first_word) {
2067 if ( first_word != NULL &&
2068 node != AUTH_NODE &&
2069 node != VIEW_NODE &&
2070 node != AUTH_ENABLE_NODE &&
2071 node != ENABLE_NODE &&
Paul Jakma62687ff2008-08-23 14:27:06 +01002072 node != RESTRICTED_NODE &&
paulb92938a2002-12-13 21:20:42 +00002073 0 == strcmp( "do", first_word ) )
2074 return 1;
2075 return 0;
2076}
2077
Christian Frankecd40b322013-09-30 12:27:51 +00002078static void
2079cmd_matches_free(vector *matches)
2080{
2081 unsigned int i;
2082 vector cmd_matches;
2083
2084 for (i = 0; i < vector_active(*matches); i++)
2085 if ((cmd_matches = vector_slot(*matches, i)) != NULL)
2086 vector_free(cmd_matches);
2087 vector_free(*matches);
2088 *matches = NULL;
2089}
2090
2091static int
2092cmd_describe_cmp(const void *a, const void *b)
2093{
2094 const struct cmd_token *first = *(struct cmd_token * const *)a;
2095 const struct cmd_token *second = *(struct cmd_token * const *)b;
2096
2097 return strcmp(first->cmd, second->cmd);
2098}
2099
2100static void
2101cmd_describe_sort(vector matchvec)
2102{
2103 qsort(matchvec->index, vector_active(matchvec),
2104 sizeof(void*), cmd_describe_cmp);
2105}
2106
paul718e3742002-12-13 20:15:29 +00002107/* '?' describe command support. */
ajs274a4a42004-12-07 15:39:31 +00002108static vector
paulb92938a2002-12-13 21:20:42 +00002109cmd_describe_command_real (vector vline, struct vty *vty, int *status)
paul718e3742002-12-13 20:15:29 +00002110{
paulb8961472005-03-14 17:35:52 +00002111 unsigned int i;
paul718e3742002-12-13 20:15:29 +00002112 vector cmd_vector;
2113#define INIT_MATCHVEC_SIZE 10
2114 vector matchvec;
2115 struct cmd_element *cmd_element;
paulb8961472005-03-14 17:35:52 +00002116 unsigned int index;
paul54aba542003-08-21 20:28:24 +00002117 int ret;
2118 enum match_type match;
2119 char *command;
Christian Frankecd40b322013-09-30 12:27:51 +00002120 vector matches = NULL;
2121 vector match_vector;
paul718e3742002-12-13 20:15:29 +00002122
2123 /* Set index. */
paul55468c82005-03-14 20:19:01 +00002124 if (vector_active (vline) == 0)
paulb8961472005-03-14 17:35:52 +00002125 {
2126 *status = CMD_ERR_NO_MATCH;
2127 return NULL;
2128 }
Christian Frankecd40b322013-09-30 12:27:51 +00002129
2130 index = vector_active (vline) - 1;
2131
paul718e3742002-12-13 20:15:29 +00002132 /* Make copy vector of current node's command vector. */
2133 cmd_vector = vector_copy (cmd_node_vector (cmdvec, vty->node));
2134
2135 /* Prepare match vector */
2136 matchvec = vector_init (INIT_MATCHVEC_SIZE);
2137
Christian Frankecd40b322013-09-30 12:27:51 +00002138 /* Filter commands and build a list how they could possibly continue. */
2139 for (i = 0; i <= index; i++)
2140 {
2141 command = vector_slot (vline, i);
paul718e3742002-12-13 20:15:29 +00002142
Christian Frankecd40b322013-09-30 12:27:51 +00002143 if (matches)
2144 cmd_matches_free(&matches);
paul718e3742002-12-13 20:15:29 +00002145
Christian Frankecd40b322013-09-30 12:27:51 +00002146 ret = cmd_vector_filter(cmd_vector,
2147 FILTER_RELAXED,
2148 vline, i,
2149 &match,
2150 &matches);
paul718e3742002-12-13 20:15:29 +00002151
Christian Frankecd40b322013-09-30 12:27:51 +00002152 if (ret != CMD_SUCCESS)
2153 {
2154 vector_free (cmd_vector);
2155 vector_free (matchvec);
2156 cmd_matches_free(&matches);
2157 *status = ret;
2158 return NULL;
2159 }
paul718e3742002-12-13 20:15:29 +00002160
Christian Frankecd40b322013-09-30 12:27:51 +00002161 /* The last match may well be ambigious, so break here */
2162 if (i == index)
2163 break;
paul718e3742002-12-13 20:15:29 +00002164
Christian Frankecd40b322013-09-30 12:27:51 +00002165 if (match == vararg_match)
2166 {
2167 /* We found a vararg match - so we can throw out the current matches here
2168 * and don't need to continue checking the command input */
2169 unsigned int j, k;
2170
2171 for (j = 0; j < vector_active (matches); j++)
2172 if ((match_vector = vector_slot (matches, j)) != NULL)
2173 for (k = 0; k < vector_active (match_vector); k++)
2174 {
2175 struct cmd_token *token = vector_slot (match_vector, k);
2176 vector_set (matchvec, token);
2177 }
2178
2179 *status = CMD_SUCCESS;
2180 vector_set(matchvec, &token_cr);
2181 vector_free (cmd_vector);
2182 cmd_matches_free(&matches);
2183 cmd_describe_sort(matchvec);
2184 return matchvec;
2185 }
2186
2187 ret = is_cmd_ambiguous(cmd_vector, command, matches, match);
2188 if (ret == 1)
2189 {
2190 vector_free (cmd_vector);
2191 vector_free (matchvec);
2192 cmd_matches_free(&matches);
2193 *status = CMD_ERR_AMBIGUOUS;
2194 return NULL;
2195 }
2196 else if (ret == 2)
2197 {
2198 vector_free (cmd_vector);
2199 vector_free (matchvec);
2200 cmd_matches_free(&matches);
2201 *status = CMD_ERR_NO_MATCH;
2202 return NULL;
2203 }
2204 }
paul54aba542003-08-21 20:28:24 +00002205
paul718e3742002-12-13 20:15:29 +00002206 /* Make description vector. */
Christian Frankecd40b322013-09-30 12:27:51 +00002207 for (i = 0; i < vector_active (matches); i++)
paul718e3742002-12-13 20:15:29 +00002208 if ((cmd_element = vector_slot (cmd_vector, i)) != NULL)
2209 {
Christian Frankecd40b322013-09-30 12:27:51 +00002210 unsigned int j;
2211 const char *last_word;
2212 vector vline_trimmed;
paul718e3742002-12-13 20:15:29 +00002213
Christian Frankecd40b322013-09-30 12:27:51 +00002214 last_word = vector_slot(vline, vector_active(vline) - 1);
2215 if (last_word == NULL || !strlen(last_word))
2216 {
2217 vline_trimmed = vector_copy(vline);
2218 vector_unset(vline_trimmed, vector_active(vline_trimmed) - 1);
paul718e3742002-12-13 20:15:29 +00002219
Christian Frankecd40b322013-09-30 12:27:51 +00002220 if (cmd_is_complete(cmd_element, vline_trimmed)
2221 && desc_unique_string(matchvec, command_cr))
2222 {
2223 if (match != vararg_match)
2224 vector_set(matchvec, &token_cr);
2225 }
Chris Caputo228da422009-07-18 05:44:03 +00002226
Christian Frankecd40b322013-09-30 12:27:51 +00002227 vector_free(vline_trimmed);
2228 }
2229
2230 match_vector = vector_slot (matches, i);
2231 if (match_vector)
2232 for (j = 0; j < vector_active(match_vector); j++)
2233 {
2234 struct cmd_token *token = vector_slot(match_vector, j);
2235 const char *string;
2236
David Lamparter10bac802015-05-05 11:10:20 +02002237 string = cmd_entry_function_desc(command, token);
Christian Frankecd40b322013-09-30 12:27:51 +00002238 if (string && desc_unique_string(matchvec, string))
2239 vector_set(matchvec, token);
2240 }
paul718e3742002-12-13 20:15:29 +00002241 }
2242 vector_free (cmd_vector);
Christian Frankecd40b322013-09-30 12:27:51 +00002243 cmd_matches_free(&matches);
paul718e3742002-12-13 20:15:29 +00002244
2245 if (vector_slot (matchvec, 0) == NULL)
2246 {
2247 vector_free (matchvec);
paul909a2152005-03-14 17:41:45 +00002248 *status = CMD_ERR_NO_MATCH;
Paul Jakma5fc60512006-05-12 23:24:09 +00002249 return NULL;
paul718e3742002-12-13 20:15:29 +00002250 }
paul718e3742002-12-13 20:15:29 +00002251
Paul Jakma5fc60512006-05-12 23:24:09 +00002252 *status = CMD_SUCCESS;
Christian Frankecd40b322013-09-30 12:27:51 +00002253 cmd_describe_sort(matchvec);
paul718e3742002-12-13 20:15:29 +00002254 return matchvec;
2255}
2256
paulb92938a2002-12-13 21:20:42 +00002257vector
2258cmd_describe_command (vector vline, struct vty *vty, int *status)
2259{
2260 vector ret;
2261
2262 if ( cmd_try_do_shortcut(vty->node, vector_slot(vline, 0) ) )
2263 {
2264 enum node_type onode;
2265 vector shifted_vline;
hasso8c328f12004-10-05 21:01:23 +00002266 unsigned int index;
paulb92938a2002-12-13 21:20:42 +00002267
2268 onode = vty->node;
2269 vty->node = ENABLE_NODE;
2270 /* We can try it on enable node, cos' the vty is authenticated */
2271
2272 shifted_vline = vector_init (vector_count(vline));
2273 /* use memcpy? */
paul55468c82005-03-14 20:19:01 +00002274 for (index = 1; index < vector_active (vline); index++)
paulb92938a2002-12-13 21:20:42 +00002275 {
2276 vector_set_index (shifted_vline, index-1, vector_lookup(vline, index));
2277 }
2278
2279 ret = cmd_describe_command_real (shifted_vline, vty, status);
2280
2281 vector_free(shifted_vline);
2282 vty->node = onode;
2283 return ret;
2284 }
2285
2286
2287 return cmd_describe_command_real (vline, vty, status);
2288}
2289
2290
paul718e3742002-12-13 20:15:29 +00002291/* Check LCD of matched command. */
ajs274a4a42004-12-07 15:39:31 +00002292static int
paul718e3742002-12-13 20:15:29 +00002293cmd_lcd (char **matched)
2294{
2295 int i;
2296 int j;
2297 int lcd = -1;
2298 char *s1, *s2;
2299 char c1, c2;
2300
2301 if (matched[0] == NULL || matched[1] == NULL)
2302 return 0;
2303
2304 for (i = 1; matched[i] != NULL; i++)
2305 {
2306 s1 = matched[i - 1];
2307 s2 = matched[i];
2308
2309 for (j = 0; (c1 = s1[j]) && (c2 = s2[j]); j++)
2310 if (c1 != c2)
2311 break;
2312
2313 if (lcd < 0)
2314 lcd = j;
2315 else
2316 {
2317 if (lcd > j)
2318 lcd = j;
2319 }
2320 }
2321 return lcd;
2322}
2323
Christian Frankecd40b322013-09-30 12:27:51 +00002324static int
2325cmd_complete_cmp(const void *a, const void *b)
2326{
2327 const char *first = *(char * const *)a;
2328 const char *second = *(char * const *)b;
2329
2330 if (!first)
2331 {
2332 if (!second)
2333 return 0;
2334 return 1;
2335 }
2336 if (!second)
2337 return -1;
2338
2339 return strcmp(first, second);
2340}
2341
2342static void
2343cmd_complete_sort(vector matchvec)
2344{
2345 qsort(matchvec->index, vector_active(matchvec),
2346 sizeof(void*), cmd_complete_cmp);
2347}
2348
paul718e3742002-12-13 20:15:29 +00002349/* Command line completion support. */
ajs274a4a42004-12-07 15:39:31 +00002350static char **
paulb92938a2002-12-13 21:20:42 +00002351cmd_complete_command_real (vector vline, struct vty *vty, int *status)
paul718e3742002-12-13 20:15:29 +00002352{
paulb8961472005-03-14 17:35:52 +00002353 unsigned int i;
paul718e3742002-12-13 20:15:29 +00002354 vector cmd_vector = vector_copy (cmd_node_vector (cmdvec, vty->node));
2355#define INIT_MATCHVEC_SIZE 10
2356 vector matchvec;
paulb8961472005-03-14 17:35:52 +00002357 unsigned int index;
paul718e3742002-12-13 20:15:29 +00002358 char **match_str;
Christian Frankecd40b322013-09-30 12:27:51 +00002359 struct cmd_token *token;
paul718e3742002-12-13 20:15:29 +00002360 char *command;
2361 int lcd;
Christian Frankecd40b322013-09-30 12:27:51 +00002362 vector matches = NULL;
2363 vector match_vector;
paul718e3742002-12-13 20:15:29 +00002364
paul55468c82005-03-14 20:19:01 +00002365 if (vector_active (vline) == 0)
paulb8961472005-03-14 17:35:52 +00002366 {
Paul Jakmad2519962006-05-12 23:19:37 +00002367 vector_free (cmd_vector);
paulb8961472005-03-14 17:35:52 +00002368 *status = CMD_ERR_NO_MATCH;
2369 return NULL;
2370 }
2371 else
paul55468c82005-03-14 20:19:01 +00002372 index = vector_active (vline) - 1;
paulb8961472005-03-14 17:35:52 +00002373
Christian Frankecd40b322013-09-30 12:27:51 +00002374 /* First, filter by command string */
2375 for (i = 0; i <= index; i++)
2376 {
2377 command = vector_slot (vline, i);
2378 enum match_type match;
2379 int ret;
paul718e3742002-12-13 20:15:29 +00002380
Christian Frankecd40b322013-09-30 12:27:51 +00002381 if (matches)
2382 cmd_matches_free(&matches);
paul718e3742002-12-13 20:15:29 +00002383
Christian Frankecd40b322013-09-30 12:27:51 +00002384 /* First try completion match, if there is exactly match return 1 */
2385 ret = cmd_vector_filter(cmd_vector,
2386 FILTER_RELAXED,
2387 vline, i,
2388 &match,
2389 &matches);
2390
2391 if (ret != CMD_SUCCESS)
2392 {
2393 vector_free(cmd_vector);
2394 cmd_matches_free(&matches);
2395 *status = ret;
2396 return NULL;
2397 }
2398
2399 /* Break here - the completion mustn't be checked to be non-ambiguous */
2400 if (i == index)
2401 break;
2402
2403 /* If there is exact match then filter ambiguous match else check
2404 ambiguousness. */
2405 ret = is_cmd_ambiguous (cmd_vector, command, matches, match);
2406 if (ret == 1)
2407 {
2408 vector_free (cmd_vector);
2409 cmd_matches_free(&matches);
2410 *status = CMD_ERR_AMBIGUOUS;
2411 return NULL;
2412 }
2413 /*
paul909a2152005-03-14 17:41:45 +00002414 else if (ret == 2)
2415 {
2416 vector_free (cmd_vector);
Christian Frankecd40b322013-09-30 12:27:51 +00002417 cmd_matches_free(&matches);
paul909a2152005-03-14 17:41:45 +00002418 *status = CMD_ERR_NO_MATCH;
2419 return NULL;
2420 }
2421 */
Christian Frankecd40b322013-09-30 12:27:51 +00002422 }
paul909a2152005-03-14 17:41:45 +00002423
paul718e3742002-12-13 20:15:29 +00002424 /* Prepare match vector. */
2425 matchvec = vector_init (INIT_MATCHVEC_SIZE);
2426
Christian Frankecd40b322013-09-30 12:27:51 +00002427 /* Build the possible list of continuations into a list of completions */
2428 for (i = 0; i < vector_active (matches); i++)
2429 if ((match_vector = vector_slot (matches, i)))
paul718e3742002-12-13 20:15:29 +00002430 {
hasso8c328f12004-10-05 21:01:23 +00002431 const char *string;
Christian Frankecd40b322013-09-30 12:27:51 +00002432 unsigned int j;
paul909a2152005-03-14 17:41:45 +00002433
Christian Frankecd40b322013-09-30 12:27:51 +00002434 for (j = 0; j < vector_active (match_vector); j++)
2435 if ((token = vector_slot (match_vector, j)))
paul909a2152005-03-14 17:41:45 +00002436 {
paulb8961472005-03-14 17:35:52 +00002437 if ((string =
2438 cmd_entry_function (vector_slot (vline, index),
David Lamparter10bac802015-05-05 11:10:20 +02002439 token)))
paul909a2152005-03-14 17:41:45 +00002440 if (cmd_unique_string (matchvec, string))
2441 vector_set (matchvec, XSTRDUP (MTYPE_TMP, string));
2442 }
paul718e3742002-12-13 20:15:29 +00002443 }
2444
2445 /* We don't need cmd_vector any more. */
2446 vector_free (cmd_vector);
Christian Frankecd40b322013-09-30 12:27:51 +00002447 cmd_matches_free(&matches);
paul718e3742002-12-13 20:15:29 +00002448
2449 /* No matched command */
2450 if (vector_slot (matchvec, 0) == NULL)
2451 {
2452 vector_free (matchvec);
2453
2454 /* In case of 'command \t' pattern. Do you need '?' command at
2455 the end of the line. */
2456 if (vector_slot (vline, index) == '\0')
2457 *status = CMD_ERR_NOTHING_TODO;
2458 else
2459 *status = CMD_ERR_NO_MATCH;
2460 return NULL;
2461 }
2462
2463 /* Only one matched */
2464 if (vector_slot (matchvec, 1) == NULL)
2465 {
2466 match_str = (char **) matchvec->index;
2467 vector_only_wrapper_free (matchvec);
2468 *status = CMD_COMPLETE_FULL_MATCH;
2469 return match_str;
2470 }
2471 /* Make it sure last element is NULL. */
2472 vector_set (matchvec, NULL);
2473
2474 /* Check LCD of matched strings. */
2475 if (vector_slot (vline, index) != NULL)
2476 {
2477 lcd = cmd_lcd ((char **) matchvec->index);
2478
2479 if (lcd)
2480 {
2481 int len = strlen (vector_slot (vline, index));
paul909a2152005-03-14 17:41:45 +00002482
paul718e3742002-12-13 20:15:29 +00002483 if (len < lcd)
2484 {
2485 char *lcdstr;
paul909a2152005-03-14 17:41:45 +00002486
Christian Frankecd40b322013-09-30 12:27:51 +00002487 lcdstr = XMALLOC (MTYPE_TMP, lcd + 1);
paul718e3742002-12-13 20:15:29 +00002488 memcpy (lcdstr, matchvec->index[0], lcd);
2489 lcdstr[lcd] = '\0';
2490
2491 /* match_str = (char **) &lcdstr; */
2492
2493 /* Free matchvec. */
paul55468c82005-03-14 20:19:01 +00002494 for (i = 0; i < vector_active (matchvec); i++)
paul718e3742002-12-13 20:15:29 +00002495 {
2496 if (vector_slot (matchvec, i))
Christian Frankecd40b322013-09-30 12:27:51 +00002497 XFREE (MTYPE_TMP, vector_slot (matchvec, i));
paul718e3742002-12-13 20:15:29 +00002498 }
2499 vector_free (matchvec);
2500
paul909a2152005-03-14 17:41:45 +00002501 /* Make new matchvec. */
paul718e3742002-12-13 20:15:29 +00002502 matchvec = vector_init (INIT_MATCHVEC_SIZE);
2503 vector_set (matchvec, lcdstr);
2504 match_str = (char **) matchvec->index;
2505 vector_only_wrapper_free (matchvec);
2506
2507 *status = CMD_COMPLETE_MATCH;
2508 return match_str;
2509 }
2510 }
2511 }
2512
2513 match_str = (char **) matchvec->index;
Christian Frankecd40b322013-09-30 12:27:51 +00002514 cmd_complete_sort(matchvec);
paul718e3742002-12-13 20:15:29 +00002515 vector_only_wrapper_free (matchvec);
2516 *status = CMD_COMPLETE_LIST_MATCH;
2517 return match_str;
2518}
2519
paulb92938a2002-12-13 21:20:42 +00002520char **
paul9ab68122003-01-18 01:16:20 +00002521cmd_complete_command (vector vline, struct vty *vty, int *status)
paulb92938a2002-12-13 21:20:42 +00002522{
2523 char **ret;
2524
2525 if ( cmd_try_do_shortcut(vty->node, vector_slot(vline, 0) ) )
2526 {
2527 enum node_type onode;
2528 vector shifted_vline;
hasso8c328f12004-10-05 21:01:23 +00002529 unsigned int index;
paulb92938a2002-12-13 21:20:42 +00002530
2531 onode = vty->node;
2532 vty->node = ENABLE_NODE;
2533 /* We can try it on enable node, cos' the vty is authenticated */
2534
2535 shifted_vline = vector_init (vector_count(vline));
2536 /* use memcpy? */
paul55468c82005-03-14 20:19:01 +00002537 for (index = 1; index < vector_active (vline); index++)
paulb92938a2002-12-13 21:20:42 +00002538 {
2539 vector_set_index (shifted_vline, index-1, vector_lookup(vline, index));
2540 }
2541
2542 ret = cmd_complete_command_real (shifted_vline, vty, status);
2543
2544 vector_free(shifted_vline);
2545 vty->node = onode;
2546 return ret;
2547 }
2548
2549
2550 return cmd_complete_command_real (vline, vty, status);
2551}
2552
2553/* return parent node */
2554/* MUST eventually converge on CONFIG_NODE */
hasso13bfca72005-01-23 21:42:25 +00002555enum node_type
ajs274a4a42004-12-07 15:39:31 +00002556node_parent ( enum node_type node )
paulb92938a2002-12-13 21:20:42 +00002557{
2558 enum node_type ret;
2559
paul9ab68122003-01-18 01:16:20 +00002560 assert (node > CONFIG_NODE);
2561
2562 switch (node)
2563 {
2564 case BGP_VPNV4_NODE:
2565 case BGP_IPV4_NODE:
2566 case BGP_IPV4M_NODE:
2567 case BGP_IPV6_NODE:
paul1e836592005-08-22 22:39:56 +00002568 case BGP_IPV6M_NODE:
paul9ab68122003-01-18 01:16:20 +00002569 ret = BGP_NODE;
2570 break;
2571 case KEYCHAIN_KEY_NODE:
2572 ret = KEYCHAIN_NODE;
2573 break;
2574 default:
2575 ret = CONFIG_NODE;
paulb92938a2002-12-13 21:20:42 +00002576 }
2577
2578 return ret;
2579}
2580
paul718e3742002-12-13 20:15:29 +00002581/* Execute command by argument vline vector. */
ajs274a4a42004-12-07 15:39:31 +00002582static int
Christian Frankecd40b322013-09-30 12:27:51 +00002583cmd_execute_command_real (vector vline,
2584 enum filter_type filter,
2585 struct vty *vty,
paulb8961472005-03-14 17:35:52 +00002586 struct cmd_element **cmd)
paul718e3742002-12-13 20:15:29 +00002587{
hasso8c328f12004-10-05 21:01:23 +00002588 unsigned int i;
2589 unsigned int index;
paul718e3742002-12-13 20:15:29 +00002590 vector cmd_vector;
2591 struct cmd_element *cmd_element;
2592 struct cmd_element *matched_element;
2593 unsigned int matched_count, incomplete_count;
2594 int argc;
paul9035efa2004-10-10 11:56:56 +00002595 const char *argv[CMD_ARGC_MAX];
paul718e3742002-12-13 20:15:29 +00002596 enum match_type match = 0;
paul718e3742002-12-13 20:15:29 +00002597 char *command;
Christian Frankecd40b322013-09-30 12:27:51 +00002598 int ret;
2599 vector matches;
paul718e3742002-12-13 20:15:29 +00002600
2601 /* Make copy of command elements. */
2602 cmd_vector = vector_copy (cmd_node_vector (cmdvec, vty->node));
2603
paul55468c82005-03-14 20:19:01 +00002604 for (index = 0; index < vector_active (vline); index++)
Christian Frankecd40b322013-09-30 12:27:51 +00002605 {
2606 command = vector_slot (vline, index);
2607 ret = cmd_vector_filter(cmd_vector,
2608 filter,
2609 vline, index,
2610 &match,
2611 &matches);
paul718e3742002-12-13 20:15:29 +00002612
Christian Frankecd40b322013-09-30 12:27:51 +00002613 if (ret != CMD_SUCCESS)
2614 {
2615 cmd_matches_free(&matches);
2616 return ret;
2617 }
paul718e3742002-12-13 20:15:29 +00002618
Christian Frankecd40b322013-09-30 12:27:51 +00002619 if (match == vararg_match)
2620 {
2621 cmd_matches_free(&matches);
paul909a2152005-03-14 17:41:45 +00002622 break;
Christian Frankecd40b322013-09-30 12:27:51 +00002623 }
paul718e3742002-12-13 20:15:29 +00002624
Christian Frankecd40b322013-09-30 12:27:51 +00002625 ret = is_cmd_ambiguous (cmd_vector, command, matches, match);
2626 cmd_matches_free(&matches);
2627
2628 if (ret == 1)
2629 {
2630 vector_free(cmd_vector);
2631 return CMD_ERR_AMBIGUOUS;
2632 }
2633 else if (ret == 2)
2634 {
2635 vector_free(cmd_vector);
2636 return CMD_ERR_NO_MATCH;
2637 }
2638 }
paul718e3742002-12-13 20:15:29 +00002639
2640 /* Check matched count. */
2641 matched_element = NULL;
2642 matched_count = 0;
2643 incomplete_count = 0;
2644
paul55468c82005-03-14 20:19:01 +00002645 for (i = 0; i < vector_active (cmd_vector); i++)
paulb8961472005-03-14 17:35:52 +00002646 if ((cmd_element = vector_slot (cmd_vector, i)))
paul718e3742002-12-13 20:15:29 +00002647 {
Christian Frankecd40b322013-09-30 12:27:51 +00002648 if (cmd_is_complete(cmd_element, vline))
paul718e3742002-12-13 20:15:29 +00002649 {
2650 matched_element = cmd_element;
paul718e3742002-12-13 20:15:29 +00002651 matched_count++;
2652 }
2653 else
2654 {
2655 incomplete_count++;
2656 }
2657 }
paul909a2152005-03-14 17:41:45 +00002658
paul718e3742002-12-13 20:15:29 +00002659 /* Finish of using cmd_vector. */
2660 vector_free (cmd_vector);
2661
paul909a2152005-03-14 17:41:45 +00002662 /* To execute command, matched_count must be 1. */
2663 if (matched_count == 0)
paul718e3742002-12-13 20:15:29 +00002664 {
2665 if (incomplete_count)
2666 return CMD_ERR_INCOMPLETE;
2667 else
2668 return CMD_ERR_NO_MATCH;
2669 }
2670
paul909a2152005-03-14 17:41:45 +00002671 if (matched_count > 1)
paul718e3742002-12-13 20:15:29 +00002672 return CMD_ERR_AMBIGUOUS;
2673
Christian Frankecd40b322013-09-30 12:27:51 +00002674 ret = cmd_parse(matched_element, vline, &argc, argv);
2675 if (ret != CMD_SUCCESS)
2676 return ret;
paul718e3742002-12-13 20:15:29 +00002677
2678 /* For vtysh execution. */
2679 if (cmd)
2680 *cmd = matched_element;
2681
2682 if (matched_element->daemon)
2683 return CMD_SUCCESS_DAEMON;
2684
2685 /* Execute matched command. */
2686 return (*matched_element->func) (matched_element, vty, argc, argv);
2687}
2688
Christian Frankecd40b322013-09-30 12:27:51 +00002689/**
2690 * Execute a given command, handling things like "do ..." and checking
2691 * whether the given command might apply at a parent node if doesn't
2692 * apply for the current node.
2693 *
2694 * @param vline Command line input, vector of char* where each element is
2695 * one input token.
2696 * @param vty The vty context in which the command should be executed.
2697 * @param cmd Pointer where the struct cmd_element of the matched command
2698 * will be stored, if any. May be set to NULL if this info is
2699 * not needed.
2700 * @param vtysh If set != 0, don't lookup the command at parent nodes.
2701 * @return The status of the command that has been executed or an error code
2702 * as to why no command could be executed.
2703 */
paulb92938a2002-12-13 21:20:42 +00002704int
hasso87d683b2005-01-16 23:31:54 +00002705cmd_execute_command (vector vline, struct vty *vty, struct cmd_element **cmd,
2706 int vtysh) {
paul9ab68122003-01-18 01:16:20 +00002707 int ret, saved_ret, tried = 0;
2708 enum node_type onode, try_node;
2709
2710 onode = try_node = vty->node;
paulb92938a2002-12-13 21:20:42 +00002711
2712 if ( cmd_try_do_shortcut(vty->node, vector_slot(vline, 0) ) )
2713 {
2714 vector shifted_vline;
hasso8c328f12004-10-05 21:01:23 +00002715 unsigned int index;
paulb92938a2002-12-13 21:20:42 +00002716
2717 vty->node = ENABLE_NODE;
2718 /* We can try it on enable node, cos' the vty is authenticated */
2719
2720 shifted_vline = vector_init (vector_count(vline));
2721 /* use memcpy? */
paul55468c82005-03-14 20:19:01 +00002722 for (index = 1; index < vector_active (vline); index++)
paulb92938a2002-12-13 21:20:42 +00002723 {
2724 vector_set_index (shifted_vline, index-1, vector_lookup(vline, index));
2725 }
2726
Christian Frankecd40b322013-09-30 12:27:51 +00002727 ret = cmd_execute_command_real (shifted_vline, FILTER_RELAXED, vty, cmd);
paulb92938a2002-12-13 21:20:42 +00002728
2729 vector_free(shifted_vline);
2730 vty->node = onode;
2731 return ret;
2732 }
2733
2734
Christian Frankecd40b322013-09-30 12:27:51 +00002735 saved_ret = ret = cmd_execute_command_real (vline, FILTER_RELAXED, vty, cmd);
paulb92938a2002-12-13 21:20:42 +00002736
hasso87d683b2005-01-16 23:31:54 +00002737 if (vtysh)
2738 return saved_ret;
2739
paulb92938a2002-12-13 21:20:42 +00002740 /* This assumes all nodes above CONFIG_NODE are childs of CONFIG_NODE */
paul9ab68122003-01-18 01:16:20 +00002741 while ( ret != CMD_SUCCESS && ret != CMD_WARNING
paulb92938a2002-12-13 21:20:42 +00002742 && vty->node > CONFIG_NODE )
2743 {
paul9ab68122003-01-18 01:16:20 +00002744 try_node = node_parent(try_node);
2745 vty->node = try_node;
Christian Frankecd40b322013-09-30 12:27:51 +00002746 ret = cmd_execute_command_real (vline, FILTER_RELAXED, vty, cmd);
paul9ab68122003-01-18 01:16:20 +00002747 tried = 1;
2748 if (ret == CMD_SUCCESS || ret == CMD_WARNING)
paulb92938a2002-12-13 21:20:42 +00002749 {
paul9ab68122003-01-18 01:16:20 +00002750 /* succesfull command, leave the node as is */
paulb92938a2002-12-13 21:20:42 +00002751 return ret;
2752 }
paulb92938a2002-12-13 21:20:42 +00002753 }
paul9ab68122003-01-18 01:16:20 +00002754 /* no command succeeded, reset the vty to the original node and
2755 return the error for this node */
2756 if ( tried )
2757 vty->node = onode;
2758 return saved_ret;
pauleda031f2003-01-18 00:39:19 +00002759}
2760
Christian Frankecd40b322013-09-30 12:27:51 +00002761/**
2762 * Execute a given command, matching it strictly against the current node.
2763 * This mode is used when reading config files.
2764 *
2765 * @param vline Command line input, vector of char* where each element is
2766 * one input token.
2767 * @param vty The vty context in which the command should be executed.
2768 * @param cmd Pointer where the struct cmd_element* of the matched command
2769 * will be stored, if any. May be set to NULL if this info is
2770 * not needed.
2771 * @return The status of the command that has been executed or an error code
2772 * as to why no command could be executed.
2773 */
paul718e3742002-12-13 20:15:29 +00002774int
paul909a2152005-03-14 17:41:45 +00002775cmd_execute_command_strict (vector vline, struct vty *vty,
paul718e3742002-12-13 20:15:29 +00002776 struct cmd_element **cmd)
2777{
Christian Frankecd40b322013-09-30 12:27:51 +00002778 return cmd_execute_command_real(vline, FILTER_STRICT, vty, cmd);
paul718e3742002-12-13 20:15:29 +00002779}
2780
2781/* Configration make from file. */
2782int
Steve Hillea555002009-07-28 16:36:14 -04002783config_from_file (struct vty *vty, FILE *fp, unsigned int *line_num)
paul718e3742002-12-13 20:15:29 +00002784{
2785 int ret;
Steve Hillea555002009-07-28 16:36:14 -04002786 *line_num = 0;
paul718e3742002-12-13 20:15:29 +00002787 vector vline;
2788
2789 while (fgets (vty->buf, VTY_BUFSIZ, fp))
2790 {
Steve Hillea555002009-07-28 16:36:14 -04002791 ++(*line_num);
paul718e3742002-12-13 20:15:29 +00002792 vline = cmd_make_strvec (vty->buf);
2793
2794 /* In case of comment line */
2795 if (vline == NULL)
2796 continue;
2797 /* Execute configuration command : this is strict match */
2798 ret = cmd_execute_command_strict (vline, vty, NULL);
2799
2800 /* Try again with setting node to CONFIG_NODE */
paulb92938a2002-12-13 21:20:42 +00002801 while (ret != CMD_SUCCESS && ret != CMD_WARNING
hassoddd85ed2004-10-13 08:18:07 +00002802 && ret != CMD_ERR_NOTHING_TODO && vty->node != CONFIG_NODE)
2803 {
paulb92938a2002-12-13 21:20:42 +00002804 vty->node = node_parent(vty->node);
hassoddd85ed2004-10-13 08:18:07 +00002805 ret = cmd_execute_command_strict (vline, vty, NULL);
2806 }
paul9ab68122003-01-18 01:16:20 +00002807
paul718e3742002-12-13 20:15:29 +00002808 cmd_free_strvec (vline);
2809
hassoddd85ed2004-10-13 08:18:07 +00002810 if (ret != CMD_SUCCESS && ret != CMD_WARNING
2811 && ret != CMD_ERR_NOTHING_TODO)
paul718e3742002-12-13 20:15:29 +00002812 return ret;
2813 }
2814 return CMD_SUCCESS;
2815}
2816
2817/* Configration from terminal */
2818DEFUN (config_terminal,
2819 config_terminal_cmd,
2820 "configure terminal",
2821 "Configuration from vty interface\n"
2822 "Configuration terminal\n")
2823{
2824 if (vty_config_lock (vty))
2825 vty->node = CONFIG_NODE;
2826 else
2827 {
2828 vty_out (vty, "VTY configuration is locked by other VTY%s", VTY_NEWLINE);
2829 return CMD_WARNING;
2830 }
2831 return CMD_SUCCESS;
2832}
2833
2834/* Enable command */
2835DEFUN (enable,
2836 config_enable_cmd,
2837 "enable",
2838 "Turn on privileged mode command\n")
2839{
2840 /* If enable password is NULL, change to ENABLE_NODE */
2841 if ((host.enable == NULL && host.enable_encrypt == NULL) ||
2842 vty->type == VTY_SHELL_SERV)
2843 vty->node = ENABLE_NODE;
2844 else
2845 vty->node = AUTH_ENABLE_NODE;
2846
2847 return CMD_SUCCESS;
2848}
2849
2850/* Disable command */
2851DEFUN (disable,
2852 config_disable_cmd,
2853 "disable",
2854 "Turn off privileged mode command\n")
2855{
2856 if (vty->node == ENABLE_NODE)
2857 vty->node = VIEW_NODE;
2858 return CMD_SUCCESS;
2859}
2860
2861/* Down vty node level. */
2862DEFUN (config_exit,
2863 config_exit_cmd,
2864 "exit",
2865 "Exit current mode and down to previous mode\n")
2866{
2867 switch (vty->node)
2868 {
2869 case VIEW_NODE:
2870 case ENABLE_NODE:
Paul Jakma62687ff2008-08-23 14:27:06 +01002871 case RESTRICTED_NODE:
paul718e3742002-12-13 20:15:29 +00002872 if (vty_shell (vty))
2873 exit (0);
2874 else
2875 vty->status = VTY_CLOSE;
2876 break;
2877 case CONFIG_NODE:
2878 vty->node = ENABLE_NODE;
2879 vty_config_unlock (vty);
2880 break;
2881 case INTERFACE_NODE:
2882 case ZEBRA_NODE:
2883 case BGP_NODE:
2884 case RIP_NODE:
2885 case RIPNG_NODE:
Paul Jakma57345092011-12-25 17:52:09 +01002886 case BABEL_NODE:
paul718e3742002-12-13 20:15:29 +00002887 case OSPF_NODE:
2888 case OSPF6_NODE:
jardin9e867fe2003-12-23 08:56:18 +00002889 case ISIS_NODE:
paul718e3742002-12-13 20:15:29 +00002890 case KEYCHAIN_NODE:
2891 case MASC_NODE:
2892 case RMAP_NODE:
Everton Marques42e30782009-11-18 17:19:43 -02002893 case PIM_NODE:
paul718e3742002-12-13 20:15:29 +00002894 case VTY_NODE:
2895 vty->node = CONFIG_NODE;
2896 break;
2897 case BGP_VPNV4_NODE:
2898 case BGP_IPV4_NODE:
2899 case BGP_IPV4M_NODE:
2900 case BGP_IPV6_NODE:
paul1e836592005-08-22 22:39:56 +00002901 case BGP_IPV6M_NODE:
paul718e3742002-12-13 20:15:29 +00002902 vty->node = BGP_NODE;
2903 break;
2904 case KEYCHAIN_KEY_NODE:
2905 vty->node = KEYCHAIN_NODE;
2906 break;
2907 default:
2908 break;
2909 }
2910 return CMD_SUCCESS;
2911}
2912
2913/* quit is alias of exit. */
2914ALIAS (config_exit,
2915 config_quit_cmd,
2916 "quit",
2917 "Exit current mode and down to previous mode\n")
2918
2919/* End of configuration. */
2920DEFUN (config_end,
2921 config_end_cmd,
2922 "end",
2923 "End current mode and change to enable mode.")
2924{
2925 switch (vty->node)
2926 {
2927 case VIEW_NODE:
2928 case ENABLE_NODE:
Paul Jakma62687ff2008-08-23 14:27:06 +01002929 case RESTRICTED_NODE:
paul718e3742002-12-13 20:15:29 +00002930 /* Nothing to do. */
2931 break;
2932 case CONFIG_NODE:
2933 case INTERFACE_NODE:
2934 case ZEBRA_NODE:
2935 case RIP_NODE:
2936 case RIPNG_NODE:
Paul Jakma57345092011-12-25 17:52:09 +01002937 case BABEL_NODE:
paul718e3742002-12-13 20:15:29 +00002938 case BGP_NODE:
2939 case BGP_VPNV4_NODE:
2940 case BGP_IPV4_NODE:
2941 case BGP_IPV4M_NODE:
2942 case BGP_IPV6_NODE:
paul1e836592005-08-22 22:39:56 +00002943 case BGP_IPV6M_NODE:
paul718e3742002-12-13 20:15:29 +00002944 case RMAP_NODE:
2945 case OSPF_NODE:
2946 case OSPF6_NODE:
jardin9e867fe2003-12-23 08:56:18 +00002947 case ISIS_NODE:
paul718e3742002-12-13 20:15:29 +00002948 case KEYCHAIN_NODE:
2949 case KEYCHAIN_KEY_NODE:
2950 case MASC_NODE:
Everton Marques42e30782009-11-18 17:19:43 -02002951 case PIM_NODE:
paul718e3742002-12-13 20:15:29 +00002952 case VTY_NODE:
2953 vty_config_unlock (vty);
2954 vty->node = ENABLE_NODE;
2955 break;
2956 default:
2957 break;
2958 }
2959 return CMD_SUCCESS;
2960}
2961
2962/* Show version. */
2963DEFUN (show_version,
2964 show_version_cmd,
2965 "show version",
2966 SHOW_STR
2967 "Displays zebra version\n")
2968{
hasso12f6ea22005-03-07 08:35:39 +00002969 vty_out (vty, "Quagga %s (%s).%s", QUAGGA_VERSION, host.name?host.name:"",
2970 VTY_NEWLINE);
David Lamparter0be793e2012-11-27 01:34:56 +00002971 vty_out (vty, "%s%s%s", QUAGGA_COPYRIGHT, GIT_INFO, VTY_NEWLINE);
David Lamparter7abd8752014-11-22 10:43:29 -08002972 vty_out (vty, "configured with:%s %s%s", VTY_NEWLINE,
2973 QUAGGA_CONFIG_ARGS, VTY_NEWLINE);
paul718e3742002-12-13 20:15:29 +00002974
2975 return CMD_SUCCESS;
2976}
2977
2978/* Help display function for all node. */
2979DEFUN (config_help,
2980 config_help_cmd,
2981 "help",
2982 "Description of the interactive help system\n")
2983{
2984 vty_out (vty,
hasso6590f2c2004-10-19 20:40:08 +00002985 "Quagga VTY provides advanced help feature. When you need help,%s\
paul718e3742002-12-13 20:15:29 +00002986anytime at the command line please press '?'.%s\
2987%s\
2988If nothing matches, the help list will be empty and you must backup%s\
2989 until entering a '?' shows the available options.%s\
2990Two styles of help are provided:%s\
29911. Full help is available when you are ready to enter a%s\
2992command argument (e.g. 'show ?') and describes each possible%s\
2993argument.%s\
29942. Partial help is provided when an abbreviated argument is entered%s\
2995 and you want to know what arguments match the input%s\
2996 (e.g. 'show me?'.)%s%s", VTY_NEWLINE, VTY_NEWLINE, VTY_NEWLINE,
2997 VTY_NEWLINE, VTY_NEWLINE, VTY_NEWLINE, VTY_NEWLINE, VTY_NEWLINE,
2998 VTY_NEWLINE, VTY_NEWLINE, VTY_NEWLINE, VTY_NEWLINE, VTY_NEWLINE);
2999 return CMD_SUCCESS;
3000}
3001
3002/* Help display function for all node. */
3003DEFUN (config_list,
3004 config_list_cmd,
3005 "list",
3006 "Print command list\n")
3007{
hasso8c328f12004-10-05 21:01:23 +00003008 unsigned int i;
paul718e3742002-12-13 20:15:29 +00003009 struct cmd_node *cnode = vector_slot (cmdvec, vty->node);
3010 struct cmd_element *cmd;
3011
paul55468c82005-03-14 20:19:01 +00003012 for (i = 0; i < vector_active (cnode->cmd_vector); i++)
paul4275b1d2005-03-09 13:42:23 +00003013 if ((cmd = vector_slot (cnode->cmd_vector, i)) != NULL
3014 && !(cmd->attr == CMD_ATTR_DEPRECATED
3015 || cmd->attr == CMD_ATTR_HIDDEN))
paul718e3742002-12-13 20:15:29 +00003016 vty_out (vty, " %s%s", cmd->string,
3017 VTY_NEWLINE);
3018 return CMD_SUCCESS;
3019}
3020
3021/* Write current configuration into file. */
3022DEFUN (config_write_file,
3023 config_write_file_cmd,
3024 "write file",
3025 "Write running configuration to memory, network, or terminal\n"
3026 "Write to configuration file\n")
3027{
hasso8c328f12004-10-05 21:01:23 +00003028 unsigned int i;
paul718e3742002-12-13 20:15:29 +00003029 int fd;
3030 struct cmd_node *node;
3031 char *config_file;
3032 char *config_file_tmp = NULL;
3033 char *config_file_sav = NULL;
paul05865c92005-10-26 05:49:54 +00003034 int ret = CMD_WARNING;
paul718e3742002-12-13 20:15:29 +00003035 struct vty *file_vty;
3036
3037 /* Check and see if we are operating under vtysh configuration */
3038 if (host.config == NULL)
3039 {
3040 vty_out (vty, "Can't save to configuration file, using vtysh.%s",
3041 VTY_NEWLINE);
3042 return CMD_WARNING;
3043 }
3044
3045 /* Get filename. */
3046 config_file = host.config;
3047
paul05865c92005-10-26 05:49:54 +00003048 config_file_sav =
3049 XMALLOC (MTYPE_TMP, strlen (config_file) + strlen (CONF_BACKUP_EXT) + 1);
paul718e3742002-12-13 20:15:29 +00003050 strcpy (config_file_sav, config_file);
3051 strcat (config_file_sav, CONF_BACKUP_EXT);
3052
3053
paul05865c92005-10-26 05:49:54 +00003054 config_file_tmp = XMALLOC (MTYPE_TMP, strlen (config_file) + 8);
paul718e3742002-12-13 20:15:29 +00003055 sprintf (config_file_tmp, "%s.XXXXXX", config_file);
3056
3057 /* Open file to configuration write. */
3058 fd = mkstemp (config_file_tmp);
3059 if (fd < 0)
3060 {
3061 vty_out (vty, "Can't open configuration file %s.%s", config_file_tmp,
3062 VTY_NEWLINE);
paul05865c92005-10-26 05:49:54 +00003063 goto finished;
paul718e3742002-12-13 20:15:29 +00003064 }
3065
3066 /* Make vty for configuration file. */
3067 file_vty = vty_new ();
3068 file_vty->fd = fd;
3069 file_vty->type = VTY_FILE;
3070
3071 /* Config file header print. */
3072 vty_out (file_vty, "!\n! Zebra configuration saved from vty\n! ");
3073 vty_time_print (file_vty, 1);
3074 vty_out (file_vty, "!\n");
3075
paul55468c82005-03-14 20:19:01 +00003076 for (i = 0; i < vector_active (cmdvec); i++)
paul718e3742002-12-13 20:15:29 +00003077 if ((node = vector_slot (cmdvec, i)) && node->func)
3078 {
3079 if ((*node->func) (file_vty))
3080 vty_out (file_vty, "!\n");
3081 }
3082 vty_close (file_vty);
3083
3084 if (unlink (config_file_sav) != 0)
3085 if (errno != ENOENT)
3086 {
3087 vty_out (vty, "Can't unlink backup configuration file %s.%s", config_file_sav,
3088 VTY_NEWLINE);
paul05865c92005-10-26 05:49:54 +00003089 goto finished;
paul718e3742002-12-13 20:15:29 +00003090 }
3091 if (link (config_file, config_file_sav) != 0)
3092 {
3093 vty_out (vty, "Can't backup old configuration file %s.%s", config_file_sav,
3094 VTY_NEWLINE);
paul05865c92005-10-26 05:49:54 +00003095 goto finished;
paul718e3742002-12-13 20:15:29 +00003096 }
3097 sync ();
3098 if (unlink (config_file) != 0)
3099 {
3100 vty_out (vty, "Can't unlink configuration file %s.%s", config_file,
3101 VTY_NEWLINE);
paul05865c92005-10-26 05:49:54 +00003102 goto finished;
paul718e3742002-12-13 20:15:29 +00003103 }
3104 if (link (config_file_tmp, config_file) != 0)
3105 {
3106 vty_out (vty, "Can't save configuration file %s.%s", config_file,
3107 VTY_NEWLINE);
paul05865c92005-10-26 05:49:54 +00003108 goto finished;
paul718e3742002-12-13 20:15:29 +00003109 }
paul718e3742002-12-13 20:15:29 +00003110 sync ();
3111
gdtaa593d52003-12-22 20:15:53 +00003112 if (chmod (config_file, CONFIGFILE_MASK) != 0)
3113 {
3114 vty_out (vty, "Can't chmod configuration file %s: %s (%d).%s",
ajs6099b3b2004-11-20 02:06:59 +00003115 config_file, safe_strerror(errno), errno, VTY_NEWLINE);
paul05865c92005-10-26 05:49:54 +00003116 goto finished;
gdtaa593d52003-12-22 20:15:53 +00003117 }
3118
paul718e3742002-12-13 20:15:29 +00003119 vty_out (vty, "Configuration saved to %s%s", config_file,
3120 VTY_NEWLINE);
paul05865c92005-10-26 05:49:54 +00003121 ret = CMD_SUCCESS;
3122
3123finished:
3124 unlink (config_file_tmp);
3125 XFREE (MTYPE_TMP, config_file_tmp);
3126 XFREE (MTYPE_TMP, config_file_sav);
3127 return ret;
paul718e3742002-12-13 20:15:29 +00003128}
3129
3130ALIAS (config_write_file,
3131 config_write_cmd,
3132 "write",
3133 "Write running configuration to memory, network, or terminal\n")
3134
3135ALIAS (config_write_file,
3136 config_write_memory_cmd,
3137 "write memory",
3138 "Write running configuration to memory, network, or terminal\n"
3139 "Write configuration to the file (same as write file)\n")
3140
3141ALIAS (config_write_file,
3142 copy_runningconfig_startupconfig_cmd,
3143 "copy running-config startup-config",
3144 "Copy configuration\n"
3145 "Copy running config to... \n"
3146 "Copy running config to startup config (same as write file)\n")
3147
3148/* Write current configuration into the terminal. */
3149DEFUN (config_write_terminal,
3150 config_write_terminal_cmd,
3151 "write terminal",
3152 "Write running configuration to memory, network, or terminal\n"
3153 "Write to terminal\n")
3154{
hasso8c328f12004-10-05 21:01:23 +00003155 unsigned int i;
paul718e3742002-12-13 20:15:29 +00003156 struct cmd_node *node;
3157
3158 if (vty->type == VTY_SHELL_SERV)
3159 {
paul55468c82005-03-14 20:19:01 +00003160 for (i = 0; i < vector_active (cmdvec); i++)
paul718e3742002-12-13 20:15:29 +00003161 if ((node = vector_slot (cmdvec, i)) && node->func && node->vtysh)
3162 {
3163 if ((*node->func) (vty))
3164 vty_out (vty, "!%s", VTY_NEWLINE);
3165 }
3166 }
3167 else
3168 {
3169 vty_out (vty, "%sCurrent configuration:%s", VTY_NEWLINE,
3170 VTY_NEWLINE);
3171 vty_out (vty, "!%s", VTY_NEWLINE);
3172
paul55468c82005-03-14 20:19:01 +00003173 for (i = 0; i < vector_active (cmdvec); i++)
paul718e3742002-12-13 20:15:29 +00003174 if ((node = vector_slot (cmdvec, i)) && node->func)
3175 {
3176 if ((*node->func) (vty))
3177 vty_out (vty, "!%s", VTY_NEWLINE);
3178 }
3179 vty_out (vty, "end%s",VTY_NEWLINE);
3180 }
3181 return CMD_SUCCESS;
3182}
3183
3184/* Write current configuration into the terminal. */
3185ALIAS (config_write_terminal,
3186 show_running_config_cmd,
3187 "show running-config",
3188 SHOW_STR
3189 "running configuration\n")
3190
3191/* Write startup configuration into the terminal. */
3192DEFUN (show_startup_config,
3193 show_startup_config_cmd,
3194 "show startup-config",
3195 SHOW_STR
3196 "Contentes of startup configuration\n")
3197{
3198 char buf[BUFSIZ];
3199 FILE *confp;
3200
3201 confp = fopen (host.config, "r");
3202 if (confp == NULL)
3203 {
3204 vty_out (vty, "Can't open configuration file [%s]%s",
3205 host.config, VTY_NEWLINE);
3206 return CMD_WARNING;
3207 }
3208
3209 while (fgets (buf, BUFSIZ, confp))
3210 {
3211 char *cp = buf;
3212
3213 while (*cp != '\r' && *cp != '\n' && *cp != '\0')
3214 cp++;
3215 *cp = '\0';
3216
3217 vty_out (vty, "%s%s", buf, VTY_NEWLINE);
3218 }
3219
3220 fclose (confp);
3221
3222 return CMD_SUCCESS;
3223}
3224
3225/* Hostname configuration */
3226DEFUN (config_hostname,
3227 hostname_cmd,
3228 "hostname WORD",
3229 "Set system's network name\n"
3230 "This system's network name\n")
3231{
3232 if (!isalpha((int) *argv[0]))
3233 {
3234 vty_out (vty, "Please specify string starting with alphabet%s", VTY_NEWLINE);
3235 return CMD_WARNING;
3236 }
3237
3238 if (host.name)
paul05865c92005-10-26 05:49:54 +00003239 XFREE (MTYPE_HOST, host.name);
paul718e3742002-12-13 20:15:29 +00003240
paul05865c92005-10-26 05:49:54 +00003241 host.name = XSTRDUP (MTYPE_HOST, argv[0]);
paul718e3742002-12-13 20:15:29 +00003242 return CMD_SUCCESS;
3243}
3244
3245DEFUN (config_no_hostname,
3246 no_hostname_cmd,
3247 "no hostname [HOSTNAME]",
3248 NO_STR
3249 "Reset system's network name\n"
3250 "Host name of this router\n")
3251{
3252 if (host.name)
paul05865c92005-10-26 05:49:54 +00003253 XFREE (MTYPE_HOST, host.name);
paul718e3742002-12-13 20:15:29 +00003254 host.name = NULL;
3255 return CMD_SUCCESS;
3256}
3257
3258/* VTY interface password set. */
3259DEFUN (config_password, password_cmd,
3260 "password (8|) WORD",
3261 "Assign the terminal connection password\n"
3262 "Specifies a HIDDEN password will follow\n"
3263 "dummy string \n"
3264 "The HIDDEN line password string\n")
3265{
3266 /* Argument check. */
3267 if (argc == 0)
3268 {
3269 vty_out (vty, "Please specify password.%s", VTY_NEWLINE);
3270 return CMD_WARNING;
3271 }
3272
3273 if (argc == 2)
3274 {
3275 if (*argv[0] == '8')
3276 {
3277 if (host.password)
paul05865c92005-10-26 05:49:54 +00003278 XFREE (MTYPE_HOST, host.password);
paul718e3742002-12-13 20:15:29 +00003279 host.password = NULL;
3280 if (host.password_encrypt)
paul05865c92005-10-26 05:49:54 +00003281 XFREE (MTYPE_HOST, host.password_encrypt);
3282 host.password_encrypt = XSTRDUP (MTYPE_HOST, argv[1]);
paul718e3742002-12-13 20:15:29 +00003283 return CMD_SUCCESS;
3284 }
3285 else
3286 {
3287 vty_out (vty, "Unknown encryption type.%s", VTY_NEWLINE);
3288 return CMD_WARNING;
3289 }
3290 }
3291
3292 if (!isalnum ((int) *argv[0]))
3293 {
3294 vty_out (vty,
3295 "Please specify string starting with alphanumeric%s", VTY_NEWLINE);
3296 return CMD_WARNING;
3297 }
3298
3299 if (host.password)
paul05865c92005-10-26 05:49:54 +00003300 XFREE (MTYPE_HOST, host.password);
paul718e3742002-12-13 20:15:29 +00003301 host.password = NULL;
3302
3303 if (host.encrypt)
3304 {
3305 if (host.password_encrypt)
paul05865c92005-10-26 05:49:54 +00003306 XFREE (MTYPE_HOST, host.password_encrypt);
3307 host.password_encrypt = XSTRDUP (MTYPE_HOST, zencrypt (argv[0]));
paul718e3742002-12-13 20:15:29 +00003308 }
3309 else
paul05865c92005-10-26 05:49:54 +00003310 host.password = XSTRDUP (MTYPE_HOST, argv[0]);
paul718e3742002-12-13 20:15:29 +00003311
3312 return CMD_SUCCESS;
3313}
3314
3315ALIAS (config_password, password_text_cmd,
3316 "password LINE",
3317 "Assign the terminal connection password\n"
3318 "The UNENCRYPTED (cleartext) line password\n")
3319
3320/* VTY enable password set. */
3321DEFUN (config_enable_password, enable_password_cmd,
3322 "enable password (8|) WORD",
3323 "Modify enable password parameters\n"
3324 "Assign the privileged level password\n"
3325 "Specifies a HIDDEN password will follow\n"
3326 "dummy string \n"
3327 "The HIDDEN 'enable' password string\n")
3328{
3329 /* Argument check. */
3330 if (argc == 0)
3331 {
3332 vty_out (vty, "Please specify password.%s", VTY_NEWLINE);
3333 return CMD_WARNING;
3334 }
3335
3336 /* Crypt type is specified. */
3337 if (argc == 2)
3338 {
3339 if (*argv[0] == '8')
3340 {
3341 if (host.enable)
paul05865c92005-10-26 05:49:54 +00003342 XFREE (MTYPE_HOST, host.enable);
paul718e3742002-12-13 20:15:29 +00003343 host.enable = NULL;
3344
3345 if (host.enable_encrypt)
paul05865c92005-10-26 05:49:54 +00003346 XFREE (MTYPE_HOST, host.enable_encrypt);
3347 host.enable_encrypt = XSTRDUP (MTYPE_HOST, argv[1]);
paul718e3742002-12-13 20:15:29 +00003348
3349 return CMD_SUCCESS;
3350 }
3351 else
3352 {
3353 vty_out (vty, "Unknown encryption type.%s", VTY_NEWLINE);
3354 return CMD_WARNING;
3355 }
3356 }
3357
3358 if (!isalnum ((int) *argv[0]))
3359 {
3360 vty_out (vty,
3361 "Please specify string starting with alphanumeric%s", VTY_NEWLINE);
3362 return CMD_WARNING;
3363 }
3364
3365 if (host.enable)
paul05865c92005-10-26 05:49:54 +00003366 XFREE (MTYPE_HOST, host.enable);
paul718e3742002-12-13 20:15:29 +00003367 host.enable = NULL;
3368
3369 /* Plain password input. */
3370 if (host.encrypt)
3371 {
3372 if (host.enable_encrypt)
paul05865c92005-10-26 05:49:54 +00003373 XFREE (MTYPE_HOST, host.enable_encrypt);
3374 host.enable_encrypt = XSTRDUP (MTYPE_HOST, zencrypt (argv[0]));
paul718e3742002-12-13 20:15:29 +00003375 }
3376 else
paul05865c92005-10-26 05:49:54 +00003377 host.enable = XSTRDUP (MTYPE_HOST, argv[0]);
paul718e3742002-12-13 20:15:29 +00003378
3379 return CMD_SUCCESS;
3380}
3381
3382ALIAS (config_enable_password,
3383 enable_password_text_cmd,
3384 "enable password LINE",
3385 "Modify enable password parameters\n"
3386 "Assign the privileged level password\n"
3387 "The UNENCRYPTED (cleartext) 'enable' password\n")
3388
3389/* VTY enable password delete. */
3390DEFUN (no_config_enable_password, no_enable_password_cmd,
3391 "no enable password",
3392 NO_STR
3393 "Modify enable password parameters\n"
3394 "Assign the privileged level password\n")
3395{
3396 if (host.enable)
paul05865c92005-10-26 05:49:54 +00003397 XFREE (MTYPE_HOST, host.enable);
paul718e3742002-12-13 20:15:29 +00003398 host.enable = NULL;
3399
3400 if (host.enable_encrypt)
paul05865c92005-10-26 05:49:54 +00003401 XFREE (MTYPE_HOST, host.enable_encrypt);
paul718e3742002-12-13 20:15:29 +00003402 host.enable_encrypt = NULL;
3403
3404 return CMD_SUCCESS;
3405}
3406
3407DEFUN (service_password_encrypt,
3408 service_password_encrypt_cmd,
3409 "service password-encryption",
3410 "Set up miscellaneous service\n"
3411 "Enable encrypted passwords\n")
3412{
3413 if (host.encrypt)
3414 return CMD_SUCCESS;
3415
3416 host.encrypt = 1;
3417
3418 if (host.password)
3419 {
3420 if (host.password_encrypt)
paul05865c92005-10-26 05:49:54 +00003421 XFREE (MTYPE_HOST, host.password_encrypt);
3422 host.password_encrypt = XSTRDUP (MTYPE_HOST, zencrypt (host.password));
paul718e3742002-12-13 20:15:29 +00003423 }
3424 if (host.enable)
3425 {
3426 if (host.enable_encrypt)
paul05865c92005-10-26 05:49:54 +00003427 XFREE (MTYPE_HOST, host.enable_encrypt);
3428 host.enable_encrypt = XSTRDUP (MTYPE_HOST, zencrypt (host.enable));
paul718e3742002-12-13 20:15:29 +00003429 }
3430
3431 return CMD_SUCCESS;
3432}
3433
3434DEFUN (no_service_password_encrypt,
3435 no_service_password_encrypt_cmd,
3436 "no service password-encryption",
3437 NO_STR
3438 "Set up miscellaneous service\n"
3439 "Enable encrypted passwords\n")
3440{
3441 if (! host.encrypt)
3442 return CMD_SUCCESS;
3443
3444 host.encrypt = 0;
3445
3446 if (host.password_encrypt)
paul05865c92005-10-26 05:49:54 +00003447 XFREE (MTYPE_HOST, host.password_encrypt);
paul718e3742002-12-13 20:15:29 +00003448 host.password_encrypt = NULL;
3449
3450 if (host.enable_encrypt)
paul05865c92005-10-26 05:49:54 +00003451 XFREE (MTYPE_HOST, host.enable_encrypt);
paul718e3742002-12-13 20:15:29 +00003452 host.enable_encrypt = NULL;
3453
3454 return CMD_SUCCESS;
3455}
3456
3457DEFUN (config_terminal_length, config_terminal_length_cmd,
3458 "terminal length <0-512>",
3459 "Set terminal line parameters\n"
3460 "Set number of lines on a screen\n"
3461 "Number of lines on screen (0 for no pausing)\n")
3462{
3463 int lines;
3464 char *endptr = NULL;
3465
3466 lines = strtol (argv[0], &endptr, 10);
3467 if (lines < 0 || lines > 512 || *endptr != '\0')
3468 {
3469 vty_out (vty, "length is malformed%s", VTY_NEWLINE);
3470 return CMD_WARNING;
3471 }
3472 vty->lines = lines;
3473
3474 return CMD_SUCCESS;
3475}
3476
3477DEFUN (config_terminal_no_length, config_terminal_no_length_cmd,
3478 "terminal no length",
3479 "Set terminal line parameters\n"
3480 NO_STR
3481 "Set number of lines on a screen\n")
3482{
3483 vty->lines = -1;
3484 return CMD_SUCCESS;
3485}
3486
3487DEFUN (service_terminal_length, service_terminal_length_cmd,
3488 "service terminal-length <0-512>",
3489 "Set up miscellaneous service\n"
3490 "System wide terminal length configuration\n"
3491 "Number of lines of VTY (0 means no line control)\n")
3492{
3493 int lines;
3494 char *endptr = NULL;
3495
3496 lines = strtol (argv[0], &endptr, 10);
3497 if (lines < 0 || lines > 512 || *endptr != '\0')
3498 {
3499 vty_out (vty, "length is malformed%s", VTY_NEWLINE);
3500 return CMD_WARNING;
3501 }
3502 host.lines = lines;
3503
3504 return CMD_SUCCESS;
3505}
3506
3507DEFUN (no_service_terminal_length, no_service_terminal_length_cmd,
3508 "no service terminal-length [<0-512>]",
3509 NO_STR
3510 "Set up miscellaneous service\n"
3511 "System wide terminal length configuration\n"
3512 "Number of lines of VTY (0 means no line control)\n")
3513{
3514 host.lines = -1;
3515 return CMD_SUCCESS;
3516}
3517
ajs2885f722004-12-17 23:16:33 +00003518DEFUN_HIDDEN (do_echo,
3519 echo_cmd,
3520 "echo .MESSAGE",
3521 "Echo a message back to the vty\n"
3522 "The message to echo\n")
3523{
3524 char *message;
3525
ajsf6834d42005-01-28 20:28:35 +00003526 vty_out (vty, "%s%s", ((message = argv_concat(argv, argc, 0)) ? message : ""),
3527 VTY_NEWLINE);
3528 if (message)
3529 XFREE(MTYPE_TMP, message);
ajs2885f722004-12-17 23:16:33 +00003530 return CMD_SUCCESS;
3531}
3532
ajs274a4a42004-12-07 15:39:31 +00003533DEFUN (config_logmsg,
3534 config_logmsg_cmd,
3535 "logmsg "LOG_LEVELS" .MESSAGE",
3536 "Send a message to enabled logging destinations\n"
3537 LOG_LEVEL_DESC
3538 "The message to send\n")
3539{
3540 int level;
3541 char *message;
3542
3543 if ((level = level_match(argv[0])) == ZLOG_DISABLED)
3544 return CMD_ERR_NO_MATCH;
3545
Christian Hammersfc951862011-03-23 13:07:55 +03003546 zlog(NULL, level, "%s", ((message = argv_concat(argv, argc, 1)) ? message : ""));
ajsf6834d42005-01-28 20:28:35 +00003547 if (message)
3548 XFREE(MTYPE_TMP, message);
ajs274a4a42004-12-07 15:39:31 +00003549 return CMD_SUCCESS;
3550}
3551
3552DEFUN (show_logging,
3553 show_logging_cmd,
3554 "show logging",
3555 SHOW_STR
3556 "Show current logging configuration\n")
3557{
3558 struct zlog *zl = zlog_default;
3559
3560 vty_out (vty, "Syslog logging: ");
3561 if (zl->maxlvl[ZLOG_DEST_SYSLOG] == ZLOG_DISABLED)
3562 vty_out (vty, "disabled");
3563 else
3564 vty_out (vty, "level %s, facility %s, ident %s",
3565 zlog_priority[zl->maxlvl[ZLOG_DEST_SYSLOG]],
3566 facility_name(zl->facility), zl->ident);
3567 vty_out (vty, "%s", VTY_NEWLINE);
3568
3569 vty_out (vty, "Stdout logging: ");
3570 if (zl->maxlvl[ZLOG_DEST_STDOUT] == ZLOG_DISABLED)
3571 vty_out (vty, "disabled");
3572 else
3573 vty_out (vty, "level %s",
3574 zlog_priority[zl->maxlvl[ZLOG_DEST_STDOUT]]);
3575 vty_out (vty, "%s", VTY_NEWLINE);
3576
3577 vty_out (vty, "Monitor logging: ");
3578 if (zl->maxlvl[ZLOG_DEST_MONITOR] == ZLOG_DISABLED)
3579 vty_out (vty, "disabled");
3580 else
3581 vty_out (vty, "level %s",
3582 zlog_priority[zl->maxlvl[ZLOG_DEST_MONITOR]]);
3583 vty_out (vty, "%s", VTY_NEWLINE);
3584
3585 vty_out (vty, "File logging: ");
3586 if ((zl->maxlvl[ZLOG_DEST_FILE] == ZLOG_DISABLED) ||
3587 !zl->fp)
3588 vty_out (vty, "disabled");
3589 else
3590 vty_out (vty, "level %s, filename %s",
3591 zlog_priority[zl->maxlvl[ZLOG_DEST_FILE]],
3592 zl->filename);
3593 vty_out (vty, "%s", VTY_NEWLINE);
3594
3595 vty_out (vty, "Protocol name: %s%s",
3596 zlog_proto_names[zl->protocol], VTY_NEWLINE);
3597 vty_out (vty, "Record priority: %s%s",
3598 (zl->record_priority ? "enabled" : "disabled"), VTY_NEWLINE);
Andrew J. Schorr1ed72e02007-04-28 22:14:10 +00003599 vty_out (vty, "Timestamp precision: %d%s",
3600 zl->timestamp_precision, VTY_NEWLINE);
ajs274a4a42004-12-07 15:39:31 +00003601
3602 return CMD_SUCCESS;
3603}
3604
paul718e3742002-12-13 20:15:29 +00003605DEFUN (config_log_stdout,
3606 config_log_stdout_cmd,
3607 "log stdout",
3608 "Logging control\n"
ajs274a4a42004-12-07 15:39:31 +00003609 "Set stdout logging level\n")
paul718e3742002-12-13 20:15:29 +00003610{
ajs274a4a42004-12-07 15:39:31 +00003611 zlog_set_level (NULL, ZLOG_DEST_STDOUT, zlog_default->default_lvl);
3612 return CMD_SUCCESS;
3613}
3614
3615DEFUN (config_log_stdout_level,
3616 config_log_stdout_level_cmd,
3617 "log stdout "LOG_LEVELS,
3618 "Logging control\n"
3619 "Set stdout logging level\n"
3620 LOG_LEVEL_DESC)
3621{
3622 int level;
3623
3624 if ((level = level_match(argv[0])) == ZLOG_DISABLED)
3625 return CMD_ERR_NO_MATCH;
3626 zlog_set_level (NULL, ZLOG_DEST_STDOUT, level);
paul718e3742002-12-13 20:15:29 +00003627 return CMD_SUCCESS;
3628}
3629
3630DEFUN (no_config_log_stdout,
3631 no_config_log_stdout_cmd,
ajs274a4a42004-12-07 15:39:31 +00003632 "no log stdout [LEVEL]",
paul718e3742002-12-13 20:15:29 +00003633 NO_STR
3634 "Logging control\n"
ajs274a4a42004-12-07 15:39:31 +00003635 "Cancel logging to stdout\n"
3636 "Logging level\n")
paul718e3742002-12-13 20:15:29 +00003637{
ajs274a4a42004-12-07 15:39:31 +00003638 zlog_set_level (NULL, ZLOG_DEST_STDOUT, ZLOG_DISABLED);
paul718e3742002-12-13 20:15:29 +00003639 return CMD_SUCCESS;
3640}
3641
ajs274a4a42004-12-07 15:39:31 +00003642DEFUN (config_log_monitor,
3643 config_log_monitor_cmd,
3644 "log monitor",
paul718e3742002-12-13 20:15:29 +00003645 "Logging control\n"
ajs274a4a42004-12-07 15:39:31 +00003646 "Set terminal line (monitor) logging level\n")
3647{
3648 zlog_set_level (NULL, ZLOG_DEST_MONITOR, zlog_default->default_lvl);
3649 return CMD_SUCCESS;
3650}
3651
3652DEFUN (config_log_monitor_level,
3653 config_log_monitor_level_cmd,
3654 "log monitor "LOG_LEVELS,
3655 "Logging control\n"
3656 "Set terminal line (monitor) logging level\n"
3657 LOG_LEVEL_DESC)
3658{
3659 int level;
3660
3661 if ((level = level_match(argv[0])) == ZLOG_DISABLED)
3662 return CMD_ERR_NO_MATCH;
3663 zlog_set_level (NULL, ZLOG_DEST_MONITOR, level);
3664 return CMD_SUCCESS;
3665}
3666
3667DEFUN (no_config_log_monitor,
3668 no_config_log_monitor_cmd,
3669 "no log monitor [LEVEL]",
3670 NO_STR
3671 "Logging control\n"
3672 "Disable terminal line (monitor) logging\n"
3673 "Logging level\n")
3674{
3675 zlog_set_level (NULL, ZLOG_DEST_MONITOR, ZLOG_DISABLED);
3676 return CMD_SUCCESS;
3677}
3678
3679static int
3680set_log_file(struct vty *vty, const char *fname, int loglevel)
paul718e3742002-12-13 20:15:29 +00003681{
3682 int ret;
paul9035efa2004-10-10 11:56:56 +00003683 char *p = NULL;
3684 const char *fullpath;
3685
paul718e3742002-12-13 20:15:29 +00003686 /* Path detection. */
ajs274a4a42004-12-07 15:39:31 +00003687 if (! IS_DIRECTORY_SEP (*fname))
paul718e3742002-12-13 20:15:29 +00003688 {
paul9035efa2004-10-10 11:56:56 +00003689 char cwd[MAXPATHLEN+1];
3690 cwd[MAXPATHLEN] = '\0';
3691
3692 if (getcwd (cwd, MAXPATHLEN) == NULL)
3693 {
3694 zlog_err ("config_log_file: Unable to alloc mem!");
3695 return CMD_WARNING;
3696 }
3697
ajs274a4a42004-12-07 15:39:31 +00003698 if ( (p = XMALLOC (MTYPE_TMP, strlen (cwd) + strlen (fname) + 2))
paul9035efa2004-10-10 11:56:56 +00003699 == NULL)
3700 {
3701 zlog_err ("config_log_file: Unable to alloc mem!");
3702 return CMD_WARNING;
3703 }
ajs274a4a42004-12-07 15:39:31 +00003704 sprintf (p, "%s/%s", cwd, fname);
paul9035efa2004-10-10 11:56:56 +00003705 fullpath = p;
paul718e3742002-12-13 20:15:29 +00003706 }
3707 else
ajs274a4a42004-12-07 15:39:31 +00003708 fullpath = fname;
paul718e3742002-12-13 20:15:29 +00003709
ajs274a4a42004-12-07 15:39:31 +00003710 ret = zlog_set_file (NULL, fullpath, loglevel);
paul718e3742002-12-13 20:15:29 +00003711
paul9035efa2004-10-10 11:56:56 +00003712 if (p)
3713 XFREE (MTYPE_TMP, p);
3714
paul718e3742002-12-13 20:15:29 +00003715 if (!ret)
3716 {
ajs274a4a42004-12-07 15:39:31 +00003717 vty_out (vty, "can't open logfile %s\n", fname);
paul718e3742002-12-13 20:15:29 +00003718 return CMD_WARNING;
3719 }
3720
3721 if (host.logfile)
paul05865c92005-10-26 05:49:54 +00003722 XFREE (MTYPE_HOST, host.logfile);
paul718e3742002-12-13 20:15:29 +00003723
paul05865c92005-10-26 05:49:54 +00003724 host.logfile = XSTRDUP (MTYPE_HOST, fname);
paul718e3742002-12-13 20:15:29 +00003725
3726 return CMD_SUCCESS;
3727}
3728
ajs274a4a42004-12-07 15:39:31 +00003729DEFUN (config_log_file,
3730 config_log_file_cmd,
3731 "log file FILENAME",
3732 "Logging control\n"
3733 "Logging to file\n"
3734 "Logging filename\n")
3735{
3736 return set_log_file(vty, argv[0], zlog_default->default_lvl);
3737}
3738
3739DEFUN (config_log_file_level,
3740 config_log_file_level_cmd,
3741 "log file FILENAME "LOG_LEVELS,
3742 "Logging control\n"
3743 "Logging to file\n"
3744 "Logging filename\n"
3745 LOG_LEVEL_DESC)
3746{
3747 int level;
3748
3749 if ((level = level_match(argv[1])) == ZLOG_DISABLED)
3750 return CMD_ERR_NO_MATCH;
3751 return set_log_file(vty, argv[0], level);
3752}
3753
paul718e3742002-12-13 20:15:29 +00003754DEFUN (no_config_log_file,
3755 no_config_log_file_cmd,
3756 "no log file [FILENAME]",
3757 NO_STR
3758 "Logging control\n"
3759 "Cancel logging to file\n"
3760 "Logging file name\n")
3761{
3762 zlog_reset_file (NULL);
3763
3764 if (host.logfile)
paul05865c92005-10-26 05:49:54 +00003765 XFREE (MTYPE_HOST, host.logfile);
paul718e3742002-12-13 20:15:29 +00003766
3767 host.logfile = NULL;
3768
3769 return CMD_SUCCESS;
3770}
3771
ajs274a4a42004-12-07 15:39:31 +00003772ALIAS (no_config_log_file,
3773 no_config_log_file_level_cmd,
3774 "no log file FILENAME LEVEL",
3775 NO_STR
3776 "Logging control\n"
3777 "Cancel logging to file\n"
3778 "Logging file name\n"
3779 "Logging level\n")
3780
paul718e3742002-12-13 20:15:29 +00003781DEFUN (config_log_syslog,
3782 config_log_syslog_cmd,
3783 "log syslog",
3784 "Logging control\n"
ajs274a4a42004-12-07 15:39:31 +00003785 "Set syslog logging level\n")
paul718e3742002-12-13 20:15:29 +00003786{
ajs274a4a42004-12-07 15:39:31 +00003787 zlog_set_level (NULL, ZLOG_DEST_SYSLOG, zlog_default->default_lvl);
paul12ab19f2003-07-26 06:14:55 +00003788 return CMD_SUCCESS;
3789}
3790
ajs274a4a42004-12-07 15:39:31 +00003791DEFUN (config_log_syslog_level,
3792 config_log_syslog_level_cmd,
3793 "log syslog "LOG_LEVELS,
paul12ab19f2003-07-26 06:14:55 +00003794 "Logging control\n"
ajs274a4a42004-12-07 15:39:31 +00003795 "Set syslog logging level\n"
3796 LOG_LEVEL_DESC)
paul12ab19f2003-07-26 06:14:55 +00003797{
ajs274a4a42004-12-07 15:39:31 +00003798 int level;
paul12ab19f2003-07-26 06:14:55 +00003799
ajs274a4a42004-12-07 15:39:31 +00003800 if ((level = level_match(argv[0])) == ZLOG_DISABLED)
3801 return CMD_ERR_NO_MATCH;
3802 zlog_set_level (NULL, ZLOG_DEST_SYSLOG, level);
3803 return CMD_SUCCESS;
3804}
paul12ab19f2003-07-26 06:14:55 +00003805
ajs274a4a42004-12-07 15:39:31 +00003806DEFUN_DEPRECATED (config_log_syslog_facility,
3807 config_log_syslog_facility_cmd,
3808 "log syslog facility "LOG_FACILITIES,
3809 "Logging control\n"
3810 "Logging goes to syslog\n"
3811 "(Deprecated) Facility parameter for syslog messages\n"
3812 LOG_FACILITY_DESC)
3813{
3814 int facility;
paul12ab19f2003-07-26 06:14:55 +00003815
ajs274a4a42004-12-07 15:39:31 +00003816 if ((facility = facility_match(argv[0])) < 0)
3817 return CMD_ERR_NO_MATCH;
3818
3819 zlog_set_level (NULL, ZLOG_DEST_SYSLOG, zlog_default->default_lvl);
paul12ab19f2003-07-26 06:14:55 +00003820 zlog_default->facility = facility;
paul718e3742002-12-13 20:15:29 +00003821 return CMD_SUCCESS;
3822}
3823
3824DEFUN (no_config_log_syslog,
3825 no_config_log_syslog_cmd,
ajs274a4a42004-12-07 15:39:31 +00003826 "no log syslog [LEVEL]",
paul718e3742002-12-13 20:15:29 +00003827 NO_STR
3828 "Logging control\n"
ajs274a4a42004-12-07 15:39:31 +00003829 "Cancel logging to syslog\n"
3830 "Logging level\n")
paul718e3742002-12-13 20:15:29 +00003831{
ajs274a4a42004-12-07 15:39:31 +00003832 zlog_set_level (NULL, ZLOG_DEST_SYSLOG, ZLOG_DISABLED);
paul718e3742002-12-13 20:15:29 +00003833 return CMD_SUCCESS;
3834}
3835
paul12ab19f2003-07-26 06:14:55 +00003836ALIAS (no_config_log_syslog,
3837 no_config_log_syslog_facility_cmd,
ajs274a4a42004-12-07 15:39:31 +00003838 "no log syslog facility "LOG_FACILITIES,
paul12ab19f2003-07-26 06:14:55 +00003839 NO_STR
3840 "Logging control\n"
3841 "Logging goes to syslog\n"
3842 "Facility parameter for syslog messages\n"
ajs274a4a42004-12-07 15:39:31 +00003843 LOG_FACILITY_DESC)
paul12ab19f2003-07-26 06:14:55 +00003844
ajs274a4a42004-12-07 15:39:31 +00003845DEFUN (config_log_facility,
3846 config_log_facility_cmd,
3847 "log facility "LOG_FACILITIES,
paul718e3742002-12-13 20:15:29 +00003848 "Logging control\n"
ajs274a4a42004-12-07 15:39:31 +00003849 "Facility parameter for syslog messages\n"
3850 LOG_FACILITY_DESC)
paul718e3742002-12-13 20:15:29 +00003851{
ajs274a4a42004-12-07 15:39:31 +00003852 int facility;
3853
3854 if ((facility = facility_match(argv[0])) < 0)
3855 return CMD_ERR_NO_MATCH;
3856 zlog_default->facility = facility;
3857 return CMD_SUCCESS;
paul718e3742002-12-13 20:15:29 +00003858}
3859
ajs274a4a42004-12-07 15:39:31 +00003860DEFUN (no_config_log_facility,
3861 no_config_log_facility_cmd,
3862 "no log facility [FACILITY]",
paul718e3742002-12-13 20:15:29 +00003863 NO_STR
3864 "Logging control\n"
ajs274a4a42004-12-07 15:39:31 +00003865 "Reset syslog facility to default (daemon)\n"
3866 "Syslog facility\n")
paul718e3742002-12-13 20:15:29 +00003867{
ajs274a4a42004-12-07 15:39:31 +00003868 zlog_default->facility = LOG_DAEMON;
3869 return CMD_SUCCESS;
3870}
3871
3872DEFUN_DEPRECATED (config_log_trap,
3873 config_log_trap_cmd,
3874 "log trap "LOG_LEVELS,
3875 "Logging control\n"
3876 "(Deprecated) Set logging level and default for all destinations\n"
3877 LOG_LEVEL_DESC)
3878{
3879 int new_level ;
3880 int i;
3881
3882 if ((new_level = level_match(argv[0])) == ZLOG_DISABLED)
3883 return CMD_ERR_NO_MATCH;
3884
3885 zlog_default->default_lvl = new_level;
3886 for (i = 0; i < ZLOG_NUM_DESTS; i++)
3887 if (zlog_default->maxlvl[i] != ZLOG_DISABLED)
3888 zlog_default->maxlvl[i] = new_level;
3889 return CMD_SUCCESS;
3890}
3891
3892DEFUN_DEPRECATED (no_config_log_trap,
3893 no_config_log_trap_cmd,
3894 "no log trap [LEVEL]",
3895 NO_STR
3896 "Logging control\n"
3897 "Permit all logging information\n"
3898 "Logging level\n")
3899{
3900 zlog_default->default_lvl = LOG_DEBUG;
paul718e3742002-12-13 20:15:29 +00003901 return CMD_SUCCESS;
3902}
3903
3904DEFUN (config_log_record_priority,
3905 config_log_record_priority_cmd,
3906 "log record-priority",
3907 "Logging control\n"
3908 "Log the priority of the message within the message\n")
3909{
3910 zlog_default->record_priority = 1 ;
3911 return CMD_SUCCESS;
3912}
3913
3914DEFUN (no_config_log_record_priority,
3915 no_config_log_record_priority_cmd,
3916 "no log record-priority",
3917 NO_STR
3918 "Logging control\n"
3919 "Do not log the priority of the message within the message\n")
3920{
3921 zlog_default->record_priority = 0 ;
3922 return CMD_SUCCESS;
3923}
3924
Andrew J. Schorr1ed72e02007-04-28 22:14:10 +00003925DEFUN (config_log_timestamp_precision,
3926 config_log_timestamp_precision_cmd,
3927 "log timestamp precision <0-6>",
3928 "Logging control\n"
3929 "Timestamp configuration\n"
3930 "Set the timestamp precision\n"
3931 "Number of subsecond digits\n")
3932{
3933 if (argc != 1)
3934 {
3935 vty_out (vty, "Insufficient arguments%s", VTY_NEWLINE);
3936 return CMD_WARNING;
3937 }
3938
3939 VTY_GET_INTEGER_RANGE("Timestamp Precision",
3940 zlog_default->timestamp_precision, argv[0], 0, 6);
3941 return CMD_SUCCESS;
3942}
3943
3944DEFUN (no_config_log_timestamp_precision,
3945 no_config_log_timestamp_precision_cmd,
3946 "no log timestamp precision",
3947 NO_STR
3948 "Logging control\n"
3949 "Timestamp configuration\n"
3950 "Reset the timestamp precision to the default value of 0\n")
3951{
3952 zlog_default->timestamp_precision = 0 ;
3953 return CMD_SUCCESS;
3954}
3955
paul3b0c5d92005-03-08 10:43:43 +00003956DEFUN (banner_motd_file,
3957 banner_motd_file_cmd,
3958 "banner motd file [FILE]",
3959 "Set banner\n"
3960 "Banner for motd\n"
3961 "Banner from a file\n"
3962 "Filename\n")
3963{
paulb45da6f2005-03-08 15:16:57 +00003964 if (host.motdfile)
paul05865c92005-10-26 05:49:54 +00003965 XFREE (MTYPE_HOST, host.motdfile);
3966 host.motdfile = XSTRDUP (MTYPE_HOST, argv[0]);
paulb45da6f2005-03-08 15:16:57 +00003967
paul3b0c5d92005-03-08 10:43:43 +00003968 return CMD_SUCCESS;
3969}
paul718e3742002-12-13 20:15:29 +00003970
3971DEFUN (banner_motd_default,
3972 banner_motd_default_cmd,
3973 "banner motd default",
3974 "Set banner string\n"
3975 "Strings for motd\n"
3976 "Default string\n")
3977{
3978 host.motd = default_motd;
3979 return CMD_SUCCESS;
3980}
3981
3982DEFUN (no_banner_motd,
3983 no_banner_motd_cmd,
3984 "no banner motd",
3985 NO_STR
3986 "Set banner string\n"
3987 "Strings for motd\n")
3988{
3989 host.motd = NULL;
paul22085182005-03-08 16:00:12 +00003990 if (host.motdfile)
paul05865c92005-10-26 05:49:54 +00003991 XFREE (MTYPE_HOST, host.motdfile);
paul3b0c5d92005-03-08 10:43:43 +00003992 host.motdfile = NULL;
paul718e3742002-12-13 20:15:29 +00003993 return CMD_SUCCESS;
3994}
3995
3996/* Set config filename. Called from vty.c */
3997void
3998host_config_set (char *filename)
3999{
Chris Caputo228da422009-07-18 05:44:03 +00004000 if (host.config)
4001 XFREE (MTYPE_HOST, host.config);
paul05865c92005-10-26 05:49:54 +00004002 host.config = XSTRDUP (MTYPE_HOST, filename);
paul718e3742002-12-13 20:15:29 +00004003}
4004
4005void
4006install_default (enum node_type node)
4007{
4008 install_element (node, &config_exit_cmd);
4009 install_element (node, &config_quit_cmd);
4010 install_element (node, &config_end_cmd);
4011 install_element (node, &config_help_cmd);
4012 install_element (node, &config_list_cmd);
4013
4014 install_element (node, &config_write_terminal_cmd);
4015 install_element (node, &config_write_file_cmd);
4016 install_element (node, &config_write_memory_cmd);
4017 install_element (node, &config_write_cmd);
4018 install_element (node, &show_running_config_cmd);
4019}
4020
4021/* Initialize command interface. Install basic nodes and commands. */
4022void
4023cmd_init (int terminal)
4024{
Christian Frankecd40b322013-09-30 12:27:51 +00004025 command_cr = XSTRDUP(MTYPE_CMD_TOKENS, "<cr>");
4026 token_cr.type = TOKEN_TERMINAL;
David Lamparter10bac802015-05-05 11:10:20 +02004027 token_cr.terminal = TERMINAL_LITERAL;
Christian Frankecd40b322013-09-30 12:27:51 +00004028 token_cr.cmd = command_cr;
4029 token_cr.desc = XSTRDUP(MTYPE_CMD_TOKENS, "");
Chris Caputo228da422009-07-18 05:44:03 +00004030
paul718e3742002-12-13 20:15:29 +00004031 /* Allocate initial top vector of commands. */
4032 cmdvec = vector_init (VECTOR_MIN_SIZE);
4033
4034 /* Default host value settings. */
4035 host.name = NULL;
4036 host.password = NULL;
4037 host.enable = NULL;
4038 host.logfile = NULL;
4039 host.config = NULL;
4040 host.lines = -1;
4041 host.motd = default_motd;
paul3b0c5d92005-03-08 10:43:43 +00004042 host.motdfile = NULL;
paul718e3742002-12-13 20:15:29 +00004043
4044 /* Install top nodes. */
4045 install_node (&view_node, NULL);
4046 install_node (&enable_node, NULL);
4047 install_node (&auth_node, NULL);
4048 install_node (&auth_enable_node, NULL);
Paul Jakma62687ff2008-08-23 14:27:06 +01004049 install_node (&restricted_node, NULL);
paul718e3742002-12-13 20:15:29 +00004050 install_node (&config_node, config_write_host);
4051
4052 /* Each node's basic commands. */
4053 install_element (VIEW_NODE, &show_version_cmd);
4054 if (terminal)
4055 {
4056 install_element (VIEW_NODE, &config_list_cmd);
4057 install_element (VIEW_NODE, &config_exit_cmd);
4058 install_element (VIEW_NODE, &config_quit_cmd);
4059 install_element (VIEW_NODE, &config_help_cmd);
4060 install_element (VIEW_NODE, &config_enable_cmd);
4061 install_element (VIEW_NODE, &config_terminal_length_cmd);
4062 install_element (VIEW_NODE, &config_terminal_no_length_cmd);
ajs274a4a42004-12-07 15:39:31 +00004063 install_element (VIEW_NODE, &show_logging_cmd);
ajs2885f722004-12-17 23:16:33 +00004064 install_element (VIEW_NODE, &echo_cmd);
Paul Jakma62687ff2008-08-23 14:27:06 +01004065
4066 install_element (RESTRICTED_NODE, &config_list_cmd);
4067 install_element (RESTRICTED_NODE, &config_exit_cmd);
4068 install_element (RESTRICTED_NODE, &config_quit_cmd);
4069 install_element (RESTRICTED_NODE, &config_help_cmd);
4070 install_element (RESTRICTED_NODE, &config_enable_cmd);
4071 install_element (RESTRICTED_NODE, &config_terminal_length_cmd);
4072 install_element (RESTRICTED_NODE, &config_terminal_no_length_cmd);
4073 install_element (RESTRICTED_NODE, &echo_cmd);
paul718e3742002-12-13 20:15:29 +00004074 }
4075
4076 if (terminal)
4077 {
4078 install_default (ENABLE_NODE);
4079 install_element (ENABLE_NODE, &config_disable_cmd);
4080 install_element (ENABLE_NODE, &config_terminal_cmd);
4081 install_element (ENABLE_NODE, &copy_runningconfig_startupconfig_cmd);
4082 }
4083 install_element (ENABLE_NODE, &show_startup_config_cmd);
4084 install_element (ENABLE_NODE, &show_version_cmd);
paul718e3742002-12-13 20:15:29 +00004085
4086 if (terminal)
paul718e3742002-12-13 20:15:29 +00004087 {
hassoe7168df2004-10-03 20:11:32 +00004088 install_element (ENABLE_NODE, &config_terminal_length_cmd);
4089 install_element (ENABLE_NODE, &config_terminal_no_length_cmd);
ajs274a4a42004-12-07 15:39:31 +00004090 install_element (ENABLE_NODE, &show_logging_cmd);
ajs2885f722004-12-17 23:16:33 +00004091 install_element (ENABLE_NODE, &echo_cmd);
ajs274a4a42004-12-07 15:39:31 +00004092 install_element (ENABLE_NODE, &config_logmsg_cmd);
hassoe7168df2004-10-03 20:11:32 +00004093
4094 install_default (CONFIG_NODE);
hassoea8e9d92004-10-07 21:32:14 +00004095 }
4096
4097 install_element (CONFIG_NODE, &hostname_cmd);
4098 install_element (CONFIG_NODE, &no_hostname_cmd);
hassoe7168df2004-10-03 20:11:32 +00004099
hassoea8e9d92004-10-07 21:32:14 +00004100 if (terminal)
4101 {
hassoe7168df2004-10-03 20:11:32 +00004102 install_element (CONFIG_NODE, &password_cmd);
4103 install_element (CONFIG_NODE, &password_text_cmd);
4104 install_element (CONFIG_NODE, &enable_password_cmd);
4105 install_element (CONFIG_NODE, &enable_password_text_cmd);
4106 install_element (CONFIG_NODE, &no_enable_password_cmd);
4107
paul718e3742002-12-13 20:15:29 +00004108 install_element (CONFIG_NODE, &config_log_stdout_cmd);
ajs274a4a42004-12-07 15:39:31 +00004109 install_element (CONFIG_NODE, &config_log_stdout_level_cmd);
paul718e3742002-12-13 20:15:29 +00004110 install_element (CONFIG_NODE, &no_config_log_stdout_cmd);
ajs274a4a42004-12-07 15:39:31 +00004111 install_element (CONFIG_NODE, &config_log_monitor_cmd);
4112 install_element (CONFIG_NODE, &config_log_monitor_level_cmd);
4113 install_element (CONFIG_NODE, &no_config_log_monitor_cmd);
paul718e3742002-12-13 20:15:29 +00004114 install_element (CONFIG_NODE, &config_log_file_cmd);
ajs274a4a42004-12-07 15:39:31 +00004115 install_element (CONFIG_NODE, &config_log_file_level_cmd);
paul718e3742002-12-13 20:15:29 +00004116 install_element (CONFIG_NODE, &no_config_log_file_cmd);
ajs274a4a42004-12-07 15:39:31 +00004117 install_element (CONFIG_NODE, &no_config_log_file_level_cmd);
paul718e3742002-12-13 20:15:29 +00004118 install_element (CONFIG_NODE, &config_log_syslog_cmd);
ajs274a4a42004-12-07 15:39:31 +00004119 install_element (CONFIG_NODE, &config_log_syslog_level_cmd);
paul12ab19f2003-07-26 06:14:55 +00004120 install_element (CONFIG_NODE, &config_log_syslog_facility_cmd);
paul718e3742002-12-13 20:15:29 +00004121 install_element (CONFIG_NODE, &no_config_log_syslog_cmd);
paul12ab19f2003-07-26 06:14:55 +00004122 install_element (CONFIG_NODE, &no_config_log_syslog_facility_cmd);
ajs274a4a42004-12-07 15:39:31 +00004123 install_element (CONFIG_NODE, &config_log_facility_cmd);
4124 install_element (CONFIG_NODE, &no_config_log_facility_cmd);
paul718e3742002-12-13 20:15:29 +00004125 install_element (CONFIG_NODE, &config_log_trap_cmd);
4126 install_element (CONFIG_NODE, &no_config_log_trap_cmd);
4127 install_element (CONFIG_NODE, &config_log_record_priority_cmd);
4128 install_element (CONFIG_NODE, &no_config_log_record_priority_cmd);
Andrew J. Schorr1ed72e02007-04-28 22:14:10 +00004129 install_element (CONFIG_NODE, &config_log_timestamp_precision_cmd);
4130 install_element (CONFIG_NODE, &no_config_log_timestamp_precision_cmd);
paul718e3742002-12-13 20:15:29 +00004131 install_element (CONFIG_NODE, &service_password_encrypt_cmd);
4132 install_element (CONFIG_NODE, &no_service_password_encrypt_cmd);
4133 install_element (CONFIG_NODE, &banner_motd_default_cmd);
paul3b0c5d92005-03-08 10:43:43 +00004134 install_element (CONFIG_NODE, &banner_motd_file_cmd);
paul718e3742002-12-13 20:15:29 +00004135 install_element (CONFIG_NODE, &no_banner_motd_cmd);
4136 install_element (CONFIG_NODE, &service_terminal_length_cmd);
4137 install_element (CONFIG_NODE, &no_service_terminal_length_cmd);
paul718e3742002-12-13 20:15:29 +00004138
paul354d1192005-04-25 16:26:42 +00004139 install_element (VIEW_NODE, &show_thread_cpu_cmd);
4140 install_element (ENABLE_NODE, &show_thread_cpu_cmd);
Paul Jakma62687ff2008-08-23 14:27:06 +01004141 install_element (RESTRICTED_NODE, &show_thread_cpu_cmd);
Paul Jakmae276eb82010-01-09 16:15:00 +00004142
4143 install_element (ENABLE_NODE, &clear_thread_cpu_cmd);
paul354d1192005-04-25 16:26:42 +00004144 install_element (VIEW_NODE, &show_work_queues_cmd);
4145 install_element (ENABLE_NODE, &show_work_queues_cmd);
paul9ab68122003-01-18 01:16:20 +00004146 }
paul718e3742002-12-13 20:15:29 +00004147 srand(time(NULL));
4148}
Chris Caputo228da422009-07-18 05:44:03 +00004149
Christian Frankecd40b322013-09-30 12:27:51 +00004150static void
4151cmd_terminate_token(struct cmd_token *token)
4152{
4153 unsigned int i, j;
4154 vector keyword_vect;
4155
4156 if (token->multiple)
4157 {
4158 for (i = 0; i < vector_active(token->multiple); i++)
4159 cmd_terminate_token(vector_slot(token->multiple, i));
4160 vector_free(token->multiple);
4161 token->multiple = NULL;
4162 }
4163
4164 if (token->keyword)
4165 {
4166 for (i = 0; i < vector_active(token->keyword); i++)
4167 {
4168 keyword_vect = vector_slot(token->keyword, i);
4169 for (j = 0; j < vector_active(keyword_vect); j++)
4170 cmd_terminate_token(vector_slot(keyword_vect, j));
4171 vector_free(keyword_vect);
4172 }
4173 vector_free(token->keyword);
4174 token->keyword = NULL;
4175 }
4176
4177 XFREE(MTYPE_CMD_TOKENS, token->cmd);
4178 XFREE(MTYPE_CMD_TOKENS, token->desc);
4179
4180 XFREE(MTYPE_CMD_TOKENS, token);
4181}
4182
4183static void
4184cmd_terminate_element(struct cmd_element *cmd)
4185{
4186 unsigned int i;
4187
4188 if (cmd->tokens == NULL)
4189 return;
4190
4191 for (i = 0; i < vector_active(cmd->tokens); i++)
4192 cmd_terminate_token(vector_slot(cmd->tokens, i));
4193
4194 vector_free(cmd->tokens);
4195 cmd->tokens = NULL;
4196}
4197
Chris Caputo228da422009-07-18 05:44:03 +00004198void
4199cmd_terminate ()
4200{
Christian Frankecd40b322013-09-30 12:27:51 +00004201 unsigned int i, j;
Chris Caputo228da422009-07-18 05:44:03 +00004202 struct cmd_node *cmd_node;
4203 struct cmd_element *cmd_element;
Christian Frankecd40b322013-09-30 12:27:51 +00004204 vector cmd_node_v;
Chris Caputo228da422009-07-18 05:44:03 +00004205
4206 if (cmdvec)
4207 {
4208 for (i = 0; i < vector_active (cmdvec); i++)
4209 if ((cmd_node = vector_slot (cmdvec, i)) != NULL)
4210 {
4211 cmd_node_v = cmd_node->cmd_vector;
4212
4213 for (j = 0; j < vector_active (cmd_node_v); j++)
Christian Frankecd40b322013-09-30 12:27:51 +00004214 if ((cmd_element = vector_slot (cmd_node_v, j)) != NULL)
4215 cmd_terminate_element(cmd_element);
Chris Caputo228da422009-07-18 05:44:03 +00004216
4217 vector_free (cmd_node_v);
4218 }
4219
4220 vector_free (cmdvec);
4221 cmdvec = NULL;
4222 }
4223
4224 if (command_cr)
Christian Frankecd40b322013-09-30 12:27:51 +00004225 XFREE(MTYPE_CMD_TOKENS, command_cr);
4226 if (token_cr.desc)
4227 XFREE(MTYPE_CMD_TOKENS, token_cr.desc);
Chris Caputo228da422009-07-18 05:44:03 +00004228 if (host.name)
4229 XFREE (MTYPE_HOST, host.name);
4230 if (host.password)
4231 XFREE (MTYPE_HOST, host.password);
4232 if (host.password_encrypt)
4233 XFREE (MTYPE_HOST, host.password_encrypt);
4234 if (host.enable)
4235 XFREE (MTYPE_HOST, host.enable);
4236 if (host.enable_encrypt)
4237 XFREE (MTYPE_HOST, host.enable_encrypt);
4238 if (host.logfile)
4239 XFREE (MTYPE_HOST, host.logfile);
4240 if (host.motdfile)
4241 XFREE (MTYPE_HOST, host.motdfile);
4242 if (host.config)
4243 XFREE (MTYPE_HOST, host.config);
4244}