blob: 922e7b58179bc0a120a93f6e640766bcbcada28b [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 Lamparter10bac802015-05-05 11:10:20 +0200509 if (cmd[0] == '[')
510 token->terminal = TERMINAL_OPTION;
511 else if ((cmd[0] >= 'A' && cmd[0] <= 'Z') || (cmd[0] == '<'))
512 token->terminal = TERMINAL_VARIABLE;
513 else if (cmd[0] == '.')
514 token->terminal = TERMINAL_VARARG;
515 else if (cmd[0] == '<')
516 token->terminal = TERMINAL_RANGE;
517 else if (strcmp (cmd, "A.B.C.D") == 0)
518 token->terminal = TERMINAL_IPV4;
519 else if (strcmp (cmd, "A.B.C.D/M") == 0)
520 token->terminal = TERMINAL_IPV4_PREFIX;
521 else if (strcmp (cmd, "X:X::X:X") == 0)
522 token->terminal = TERMINAL_IPV6;
523 else if (strcmp (cmd, "X:X::X:X/M") == 0)
524 token->terminal = TERMINAL_IPV6_PREFIX;
525 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 Lamparter10bac802015-05-05 11:10:20 +02001344 if (token->terminal == TERMINAL_VARARG
1345 || token->terminal == TERMINAL_VARIABLE
1346 || token->terminal == TERMINAL_OPTION)
Christian Frankecd40b322013-09-30 12:27:51 +00001347 {
1348 if (push_argument(argc, argv, word))
1349 return MATCHER_EXCEED_ARGC_MAX;
1350 }
1351
1352 cmd_matcher_record_match(matcher, word_match, token);
1353
1354 matcher->word_index++;
1355
1356 /* A vararg token should consume all left over words as arguments */
David Lamparter10bac802015-05-05 11:10:20 +02001357 if (token->terminal == TERMINAL_VARARG)
Christian Frankecd40b322013-09-30 12:27:51 +00001358 while (cmd_matcher_words_left(matcher))
1359 {
1360 word = cmd_matcher_get_word(matcher);
1361 if (word && strlen(word))
1362 push_argument(argc, argv, word);
1363 matcher->word_index++;
1364 }
1365
1366 return MATCHER_OK;
1367}
1368
1369static enum matcher_rv
1370cmd_matcher_match_multiple(struct cmd_matcher *matcher,
1371 struct cmd_token *token,
1372 int *argc, const char **argv)
1373{
1374 enum match_type multiple_match;
1375 unsigned int multiple_index;
1376 const char *word;
David Lamparterab90fc02015-03-03 09:07:25 +01001377 const char *arg = NULL;
Christian Frankecd40b322013-09-30 12:27:51 +00001378 struct cmd_token *word_token;
1379 enum match_type word_match;
1380
1381 assert(token->type == TOKEN_MULTIPLE);
1382
1383 multiple_match = no_match;
1384
1385 if (!cmd_matcher_words_left(matcher))
1386 return MATCHER_INCOMPLETE;
1387
1388 word = cmd_matcher_get_word(matcher);
1389 for (multiple_index = 0;
1390 multiple_index < vector_active(token->multiple);
1391 multiple_index++)
1392 {
1393 word_token = vector_slot(token->multiple, multiple_index);
1394
1395 word_match = cmd_word_match(word_token, matcher->filter, word);
1396 if (word_match == no_match)
1397 continue;
1398
1399 cmd_matcher_record_match(matcher, word_match, word_token);
1400
1401 if (word_match > multiple_match)
1402 {
1403 multiple_match = word_match;
1404 arg = word;
1405 }
1406 /* To mimic the behavior of the old command implementation, we
1407 * tolerate any ambiguities here :/ */
1408 }
1409
1410 matcher->word_index++;
1411
1412 if (multiple_match == no_match)
1413 return MATCHER_NO_MATCH;
1414
1415 if (push_argument(argc, argv, arg))
1416 return MATCHER_EXCEED_ARGC_MAX;
1417
1418 return MATCHER_OK;
1419}
1420
1421static enum matcher_rv
1422cmd_matcher_read_keywords(struct cmd_matcher *matcher,
1423 struct cmd_token *token,
1424 vector args_vector)
paul718e3742002-12-13 20:15:29 +00001425{
hasso8c328f12004-10-05 21:01:23 +00001426 unsigned int i;
Christian Frankecd40b322013-09-30 12:27:51 +00001427 unsigned long keyword_mask;
1428 unsigned int keyword_found;
1429 enum match_type keyword_match;
1430 enum match_type word_match;
1431 vector keyword_vector;
1432 struct cmd_token *word_token;
1433 const char *word;
1434 int keyword_argc;
1435 const char **keyword_argv;
Paul Jakma7aa9dce2014-09-19 14:42:23 +01001436 enum matcher_rv rv = MATCHER_NO_MATCH;
Christian Frankecd40b322013-09-30 12:27:51 +00001437
1438 keyword_mask = 0;
1439 while (1)
1440 {
1441 if (!cmd_matcher_words_left(matcher))
1442 return MATCHER_OK;
1443
1444 word = cmd_matcher_get_word(matcher);
1445
1446 keyword_found = -1;
1447 keyword_match = no_match;
1448 for (i = 0; i < vector_active(token->keyword); i++)
1449 {
1450 if (keyword_mask & (1 << i))
1451 continue;
1452
1453 keyword_vector = vector_slot(token->keyword, i);
1454 word_token = vector_slot(keyword_vector, 0);
1455
1456 word_match = cmd_word_match(word_token, matcher->filter, word);
1457 if (word_match == no_match)
1458 continue;
1459
1460 cmd_matcher_record_match(matcher, word_match, word_token);
1461
1462 if (word_match > keyword_match)
1463 {
1464 keyword_match = word_match;
1465 keyword_found = i;
1466 }
1467 else if (word_match == keyword_match)
1468 {
1469 if (matcher->word_index != matcher->index || args_vector)
1470 return MATCHER_AMBIGUOUS;
1471 }
1472 }
1473
1474 if (keyword_found == (unsigned int)-1)
1475 return MATCHER_NO_MATCH;
1476
1477 matcher->word_index++;
1478
1479 if (matcher->word_index > matcher->index)
1480 return MATCHER_OK;
1481
1482 keyword_mask |= (1 << keyword_found);
1483
1484 if (args_vector)
1485 {
1486 keyword_argc = 0;
1487 keyword_argv = XMALLOC(MTYPE_TMP, (CMD_ARGC_MAX + 1) * sizeof(char*));
1488 /* We use -1 as a marker for unused fields as NULL might be a valid value */
1489 for (i = 0; i < CMD_ARGC_MAX + 1; i++)
1490 keyword_argv[i] = (void*)-1;
1491 vector_set_index(args_vector, keyword_found, keyword_argv);
1492 }
1493 else
1494 {
1495 keyword_argv = NULL;
1496 }
1497
1498 keyword_vector = vector_slot(token->keyword, keyword_found);
1499 /* the keyword itself is at 0. We are only interested in the arguments,
1500 * so start counting at 1. */
1501 for (i = 1; i < vector_active(keyword_vector); i++)
1502 {
1503 word_token = vector_slot(keyword_vector, i);
1504
1505 switch (word_token->type)
1506 {
1507 case TOKEN_TERMINAL:
1508 rv = cmd_matcher_match_terminal(matcher, word_token,
1509 &keyword_argc, keyword_argv);
1510 break;
1511 case TOKEN_MULTIPLE:
1512 rv = cmd_matcher_match_multiple(matcher, word_token,
1513 &keyword_argc, keyword_argv);
1514 break;
1515 case TOKEN_KEYWORD:
1516 assert(!"Keywords should never be nested.");
1517 break;
1518 }
1519
1520 if (MATCHER_ERROR(rv))
1521 return rv;
1522
1523 if (matcher->word_index > matcher->index)
1524 return MATCHER_OK;
1525 }
1526 }
1527 /* not reached */
1528}
1529
1530static enum matcher_rv
1531cmd_matcher_build_keyword_args(struct cmd_matcher *matcher,
1532 struct cmd_token *token,
1533 int *argc, const char **argv,
1534 vector keyword_args_vector)
1535{
1536 unsigned int i, j;
1537 const char **keyword_args;
1538 vector keyword_vector;
1539 struct cmd_token *word_token;
1540 const char *arg;
1541 enum matcher_rv rv;
1542
1543 rv = MATCHER_OK;
1544
1545 if (keyword_args_vector == NULL)
1546 return rv;
1547
1548 for (i = 0; i < vector_active(token->keyword); i++)
1549 {
1550 keyword_vector = vector_slot(token->keyword, i);
1551 keyword_args = vector_lookup(keyword_args_vector, i);
1552
1553 if (vector_active(keyword_vector) == 1)
1554 {
1555 /* this is a keyword without arguments */
1556 if (keyword_args)
1557 {
1558 word_token = vector_slot(keyword_vector, 0);
1559 arg = word_token->cmd;
1560 }
1561 else
1562 {
1563 arg = NULL;
1564 }
1565
1566 if (push_argument(argc, argv, arg))
1567 rv = MATCHER_EXCEED_ARGC_MAX;
1568 }
1569 else
1570 {
1571 /* this is a keyword with arguments */
1572 if (keyword_args)
1573 {
1574 /* the keyword was present, so just fill in the arguments */
1575 for (j = 0; keyword_args[j] != (void*)-1; j++)
1576 if (push_argument(argc, argv, keyword_args[j]))
1577 rv = MATCHER_EXCEED_ARGC_MAX;
1578 XFREE(MTYPE_TMP, keyword_args);
1579 }
1580 else
1581 {
1582 /* the keyword was not present, insert NULL for the arguments
1583 * the keyword would have taken. */
1584 for (j = 1; j < vector_active(keyword_vector); j++)
1585 {
1586 word_token = vector_slot(keyword_vector, j);
1587 if ((word_token->type == TOKEN_TERMINAL
David Lamparter10bac802015-05-05 11:10:20 +02001588 && (word_token->terminal == TERMINAL_VARARG
1589 || word_token->terminal == TERMINAL_VARIABLE
1590 || word_token->terminal == TERMINAL_OPTION))
Christian Frankecd40b322013-09-30 12:27:51 +00001591 || word_token->type == TOKEN_MULTIPLE)
1592 {
1593 if (push_argument(argc, argv, NULL))
1594 rv = MATCHER_EXCEED_ARGC_MAX;
1595 }
1596 }
1597 }
1598 }
1599 }
1600 vector_free(keyword_args_vector);
1601 return rv;
1602}
1603
1604static enum matcher_rv
1605cmd_matcher_match_keyword(struct cmd_matcher *matcher,
1606 struct cmd_token *token,
1607 int *argc, const char **argv)
1608{
1609 vector keyword_args_vector;
1610 enum matcher_rv reader_rv;
1611 enum matcher_rv builder_rv;
1612
1613 assert(token->type == TOKEN_KEYWORD);
1614
1615 if (argc && argv)
1616 keyword_args_vector = vector_init(VECTOR_MIN_SIZE);
1617 else
1618 keyword_args_vector = NULL;
1619
1620 reader_rv = cmd_matcher_read_keywords(matcher, token, keyword_args_vector);
1621 builder_rv = cmd_matcher_build_keyword_args(matcher, token, argc,
1622 argv, keyword_args_vector);
1623 /* keyword_args_vector is consumed by cmd_matcher_build_keyword_args */
1624
1625 if (!MATCHER_ERROR(reader_rv) && MATCHER_ERROR(builder_rv))
1626 return builder_rv;
1627
1628 return reader_rv;
1629}
1630
1631static void
1632cmd_matcher_init(struct cmd_matcher *matcher,
1633 struct cmd_element *cmd,
1634 enum filter_type filter,
1635 vector vline,
1636 unsigned int index,
1637 enum match_type *match_type,
1638 vector *match)
1639{
1640 memset(matcher, 0, sizeof(*matcher));
1641
1642 matcher->cmd = cmd;
1643 matcher->filter = filter;
1644 matcher->vline = vline;
1645 matcher->index = index;
1646
1647 matcher->match_type = match_type;
1648 if (matcher->match_type)
1649 *matcher->match_type = no_match;
1650 matcher->match = match;
1651
1652 matcher->word_index = 0;
1653}
1654
1655static enum matcher_rv
1656cmd_element_match(struct cmd_element *cmd_element,
1657 enum filter_type filter,
1658 vector vline,
1659 unsigned int index,
1660 enum match_type *match_type,
1661 vector *match,
1662 int *argc,
1663 const char **argv)
1664{
1665 struct cmd_matcher matcher;
1666 unsigned int token_index;
Paul Jakma7aa9dce2014-09-19 14:42:23 +01001667 enum matcher_rv rv = MATCHER_NO_MATCH;
Christian Frankecd40b322013-09-30 12:27:51 +00001668
1669 cmd_matcher_init(&matcher, cmd_element, filter,
1670 vline, index, match_type, match);
1671
1672 if (argc != NULL)
1673 *argc = 0;
1674
1675 for (token_index = 0;
1676 token_index < vector_active(cmd_element->tokens);
1677 token_index++)
1678 {
1679 struct cmd_token *token = vector_slot(cmd_element->tokens, token_index);
1680
1681 switch (token->type)
1682 {
1683 case TOKEN_TERMINAL:
1684 rv = cmd_matcher_match_terminal(&matcher, token, argc, argv);
1685 break;
1686 case TOKEN_MULTIPLE:
1687 rv = cmd_matcher_match_multiple(&matcher, token, argc, argv);
1688 break;
1689 case TOKEN_KEYWORD:
1690 rv = cmd_matcher_match_keyword(&matcher, token, argc, argv);
1691 }
1692
1693 if (MATCHER_ERROR(rv))
1694 return rv;
1695
1696 if (matcher.word_index > index)
1697 return MATCHER_OK;
1698 }
1699
1700 /* return MATCHER_COMPLETE if all words were consumed */
1701 if (matcher.word_index >= vector_active(vline))
1702 return MATCHER_COMPLETE;
1703
1704 /* return MATCHER_COMPLETE also if only an empty word is left. */
1705 if (matcher.word_index == vector_active(vline) - 1
1706 && (!vector_slot(vline, matcher.word_index)
1707 || !strlen((char*)vector_slot(vline, matcher.word_index))))
1708 return MATCHER_COMPLETE;
1709
1710 return MATCHER_NO_MATCH; /* command is too long to match */
1711}
1712
1713/**
1714 * Filter a given vector of commands against a given commandline and
1715 * calculate possible completions.
1716 *
1717 * @param commands A vector of struct cmd_element*. Commands that don't
1718 * match against the given command line will be overwritten
1719 * with NULL in that vector.
1720 * @param filter Either FILTER_RELAXED or FILTER_STRICT. This basically
1721 * determines how incomplete commands are handled, compare with
1722 * cmd_word_match for details.
1723 * @param vline A vector of char* containing the tokenized commandline.
1724 * @param index Only match up to the given token of the commandline.
1725 * @param match_type Record the type of the best match here.
1726 * @param matches Record the matches here. For each cmd_element in the commands
1727 * vector, a match vector will be created in the matches vector.
1728 * That vector will contain all struct command_token* of the
1729 * cmd_element which matched against the given vline at the given
1730 * index.
1731 * @return A code specifying if an error occured. If all went right, it's
1732 * CMD_SUCCESS.
1733 */
1734static int
1735cmd_vector_filter(vector commands,
1736 enum filter_type filter,
1737 vector vline,
1738 unsigned int index,
1739 enum match_type *match_type,
1740 vector *matches)
1741{
1742 unsigned int i;
paul718e3742002-12-13 20:15:29 +00001743 struct cmd_element *cmd_element;
Christian Frankecd40b322013-09-30 12:27:51 +00001744 enum match_type best_match;
1745 enum match_type element_match;
1746 enum matcher_rv matcher_rv;
paul909a2152005-03-14 17:41:45 +00001747
Christian Frankecd40b322013-09-30 12:27:51 +00001748 best_match = no_match;
1749 *matches = vector_init(VECTOR_MIN_SIZE);
paul718e3742002-12-13 20:15:29 +00001750
Christian Frankecd40b322013-09-30 12:27:51 +00001751 for (i = 0; i < vector_active (commands); i++)
1752 if ((cmd_element = vector_slot (commands, i)) != NULL)
paul718e3742002-12-13 20:15:29 +00001753 {
Christian Frankecd40b322013-09-30 12:27:51 +00001754 vector_set_index(*matches, i, NULL);
1755 matcher_rv = cmd_element_match(cmd_element, filter,
1756 vline, index,
1757 &element_match,
1758 (vector*)&vector_slot(*matches, i),
1759 NULL, NULL);
1760 if (MATCHER_ERROR(matcher_rv))
1761 {
1762 vector_slot(commands, i) = NULL;
1763 if (matcher_rv == MATCHER_AMBIGUOUS)
1764 return CMD_ERR_AMBIGUOUS;
1765 if (matcher_rv == MATCHER_EXCEED_ARGC_MAX)
1766 return CMD_ERR_EXEED_ARGC_MAX;
1767 }
1768 else if (element_match > best_match)
1769 {
1770 best_match = element_match;
1771 }
paul718e3742002-12-13 20:15:29 +00001772 }
Christian Frankecd40b322013-09-30 12:27:51 +00001773 *match_type = best_match;
1774 return CMD_SUCCESS;
1775}
1776
1777/**
1778 * Check whether a given commandline is complete if used for a specific
1779 * cmd_element.
1780 *
1781 * @param cmd_element A cmd_element against which the commandline should be
1782 * checked.
1783 * @param vline The tokenized commandline.
1784 * @return 1 if the given commandline is complete, 0 otherwise.
1785 */
1786static int
1787cmd_is_complete(struct cmd_element *cmd_element,
1788 vector vline)
1789{
1790 enum matcher_rv rv;
1791
1792 rv = cmd_element_match(cmd_element,
1793 FILTER_RELAXED,
1794 vline, -1,
1795 NULL, NULL,
1796 NULL, NULL);
1797 return (rv == MATCHER_COMPLETE);
1798}
1799
1800/**
1801 * Parse a given commandline and construct a list of arguments for the
1802 * given command_element.
1803 *
1804 * @param cmd_element The cmd_element for which we want to construct arguments.
1805 * @param vline The tokenized commandline.
1806 * @param argc Where to store the argument count.
1807 * @param argv Where to store the argument list. Should be at least
1808 * CMD_ARGC_MAX elements long.
1809 * @return CMD_SUCCESS if everything went alright, an error otherwise.
1810 */
1811static int
1812cmd_parse(struct cmd_element *cmd_element,
1813 vector vline,
1814 int *argc, const char **argv)
1815{
1816 enum matcher_rv rv = cmd_element_match(cmd_element,
1817 FILTER_RELAXED,
1818 vline, -1,
1819 NULL, NULL,
1820 argc, argv);
1821 switch (rv)
1822 {
1823 case MATCHER_COMPLETE:
1824 return CMD_SUCCESS;
1825
1826 case MATCHER_NO_MATCH:
1827 return CMD_ERR_NO_MATCH;
1828
1829 case MATCHER_AMBIGUOUS:
1830 return CMD_ERR_AMBIGUOUS;
1831
1832 case MATCHER_EXCEED_ARGC_MAX:
1833 return CMD_ERR_EXEED_ARGC_MAX;
1834
1835 default:
1836 return CMD_ERR_INCOMPLETE;
1837 }
paul718e3742002-12-13 20:15:29 +00001838}
1839
1840/* Check ambiguous match */
ajs274a4a42004-12-07 15:39:31 +00001841static int
Christian Frankecd40b322013-09-30 12:27:51 +00001842is_cmd_ambiguous (vector cmd_vector,
1843 const char *command,
1844 vector matches,
1845 enum match_type type)
paul718e3742002-12-13 20:15:29 +00001846{
hasso8c328f12004-10-05 21:01:23 +00001847 unsigned int i;
1848 unsigned int j;
1849 const char *str = NULL;
hasso8c328f12004-10-05 21:01:23 +00001850 const char *matched = NULL;
Christian Frankecd40b322013-09-30 12:27:51 +00001851 vector match_vector;
1852 struct cmd_token *cmd_token;
paul909a2152005-03-14 17:41:45 +00001853
Christian Frankecd40b322013-09-30 12:27:51 +00001854 if (command == NULL)
1855 command = "";
1856
1857 for (i = 0; i < vector_active (matches); i++)
1858 if ((match_vector = vector_slot (matches, i)) != NULL)
paul718e3742002-12-13 20:15:29 +00001859 {
1860 int match = 0;
1861
Christian Frankecd40b322013-09-30 12:27:51 +00001862 for (j = 0; j < vector_active (match_vector); j++)
1863 if ((cmd_token = vector_slot (match_vector, j)) != NULL)
paul909a2152005-03-14 17:41:45 +00001864 {
1865 enum match_type ret;
Christian Frankecd40b322013-09-30 12:27:51 +00001866
1867 assert(cmd_token->type == TOKEN_TERMINAL);
1868 if (cmd_token->type != TOKEN_TERMINAL)
1869 continue;
1870
1871 str = cmd_token->cmd;
paul718e3742002-12-13 20:15:29 +00001872
paul909a2152005-03-14 17:41:45 +00001873 switch (type)
1874 {
1875 case exact_match:
David Lamparter10bac802015-05-05 11:10:20 +02001876 if (!(cmd_token->terminal == TERMINAL_OPTION
1877 || cmd_token->terminal == TERMINAL_VARIABLE)
paul909a2152005-03-14 17:41:45 +00001878 && strcmp (command, str) == 0)
1879 match++;
1880 break;
1881 case partly_match:
David Lamparter10bac802015-05-05 11:10:20 +02001882 if (!(cmd_token->terminal == TERMINAL_OPTION
1883 || cmd_token->terminal == TERMINAL_VARIABLE)
paul909a2152005-03-14 17:41:45 +00001884 && strncmp (command, str, strlen (command)) == 0)
1885 {
1886 if (matched && strcmp (matched, str) != 0)
1887 return 1; /* There is ambiguous match. */
1888 else
1889 matched = str;
1890 match++;
1891 }
1892 break;
1893 case range_match:
1894 if (cmd_range_match (str, command))
1895 {
1896 if (matched && strcmp (matched, str) != 0)
1897 return 1;
1898 else
1899 matched = str;
1900 match++;
1901 }
1902 break;
1903#ifdef HAVE_IPV6
1904 case ipv6_match:
David Lamparter10bac802015-05-05 11:10:20 +02001905 if (cmd_token->terminal == TERMINAL_IPV6)
paul909a2152005-03-14 17:41:45 +00001906 match++;
1907 break;
1908 case ipv6_prefix_match:
1909 if ((ret = cmd_ipv6_prefix_match (command)) != no_match)
1910 {
1911 if (ret == partly_match)
1912 return 2; /* There is incomplete match. */
paul718e3742002-12-13 20:15:29 +00001913
paul909a2152005-03-14 17:41:45 +00001914 match++;
1915 }
1916 break;
1917#endif /* HAVE_IPV6 */
1918 case ipv4_match:
David Lamparter10bac802015-05-05 11:10:20 +02001919 if (cmd_token->terminal == TERMINAL_IPV4)
paul718e3742002-12-13 20:15:29 +00001920 match++;
paul909a2152005-03-14 17:41:45 +00001921 break;
1922 case ipv4_prefix_match:
1923 if ((ret = cmd_ipv4_prefix_match (command)) != no_match)
1924 {
1925 if (ret == partly_match)
1926 return 2; /* There is incomplete match. */
paul718e3742002-12-13 20:15:29 +00001927
paul909a2152005-03-14 17:41:45 +00001928 match++;
1929 }
1930 break;
1931 case extend_match:
David Lamparter10bac802015-05-05 11:10:20 +02001932 if (cmd_token->terminal == TERMINAL_OPTION
1933 || cmd_token->terminal == TERMINAL_VARIABLE)
paul718e3742002-12-13 20:15:29 +00001934 match++;
paul909a2152005-03-14 17:41:45 +00001935 break;
1936 case no_match:
1937 default:
1938 break;
1939 }
1940 }
1941 if (!match)
Christian Frankecd40b322013-09-30 12:27:51 +00001942 vector_slot (cmd_vector, i) = NULL;
paul718e3742002-12-13 20:15:29 +00001943 }
1944 return 0;
1945}
1946
1947/* If src matches dst return dst string, otherwise return NULL */
ajs274a4a42004-12-07 15:39:31 +00001948static const char *
David Lamparter10bac802015-05-05 11:10:20 +02001949cmd_entry_function (const char *src, struct cmd_token *token)
paul718e3742002-12-13 20:15:29 +00001950{
David Lamparter10bac802015-05-05 11:10:20 +02001951 const char *dst = token->cmd;
1952
paul718e3742002-12-13 20:15:29 +00001953 /* Skip variable arguments. */
David Lamparter10bac802015-05-05 11:10:20 +02001954 switch (token->terminal)
1955 {
1956 case TERMINAL_OPTION:
1957 case TERMINAL_VARIABLE:
1958 case TERMINAL_VARARG:
1959 case TERMINAL_IPV4:
1960 case TERMINAL_IPV4_PREFIX:
1961 case TERMINAL_RANGE:
1962 return NULL;
1963 default:
1964 break;
1965 }
paul718e3742002-12-13 20:15:29 +00001966
1967 /* In case of 'command \t', given src is NULL string. */
1968 if (src == NULL)
1969 return dst;
1970
1971 /* Matched with input string. */
1972 if (strncmp (src, dst, strlen (src)) == 0)
1973 return dst;
1974
1975 return NULL;
1976}
1977
1978/* If src matches dst return dst string, otherwise return NULL */
1979/* This version will return the dst string always if it is
1980 CMD_VARIABLE for '?' key processing */
ajs274a4a42004-12-07 15:39:31 +00001981static const char *
David Lamparter10bac802015-05-05 11:10:20 +02001982cmd_entry_function_desc (const char *src, struct cmd_token *token)
paul718e3742002-12-13 20:15:29 +00001983{
David Lamparter10bac802015-05-05 11:10:20 +02001984 const char *dst = token->cmd;
paul718e3742002-12-13 20:15:29 +00001985
David Lamparter10bac802015-05-05 11:10:20 +02001986 switch (token->terminal)
paul718e3742002-12-13 20:15:29 +00001987 {
David Lamparter10bac802015-05-05 11:10:20 +02001988 case TERMINAL_VARARG:
1989 return dst;
1990
1991 case TERMINAL_RANGE:
1992 if (cmd_range_match (dst, src))
1993 return dst;
1994 else
1995 return NULL;
1996
1997 case TERMINAL_IPV6:
1998 if (cmd_ipv6_match (src))
1999 return dst;
2000 else
2001 return NULL;
2002
2003 case TERMINAL_IPV6_PREFIX:
2004 if (cmd_ipv6_prefix_match (src))
2005 return dst;
2006 else
2007 return NULL;
2008
2009 case TERMINAL_IPV4:
2010 if (cmd_ipv4_match (src))
2011 return dst;
2012 else
2013 return NULL;
2014
2015 case TERMINAL_IPV4_PREFIX:
2016 if (cmd_ipv4_prefix_match (src))
2017 return dst;
2018 else
2019 return NULL;
2020
2021 /* Optional or variable commands always match on '?' */
2022 case TERMINAL_OPTION:
2023 case TERMINAL_VARIABLE:
2024 return dst;
2025
2026 case TERMINAL_LITERAL:
2027 /* In case of 'command \t', given src is NULL string. */
2028 if (src == NULL)
2029 return dst;
2030
2031 if (strncmp (src, dst, strlen (src)) == 0)
2032 return dst;
2033 else
2034 return NULL;
2035
2036 default:
2037 assert(0);
paul718e3742002-12-13 20:15:29 +00002038 }
paul718e3742002-12-13 20:15:29 +00002039}
2040
Christian Frankecd40b322013-09-30 12:27:51 +00002041/**
2042 * Check whether a string is already present in a vector of strings.
2043 * @param v A vector of char*.
2044 * @param str A char*.
2045 * @return 0 if str is already present in the vector, 1 otherwise.
2046 */
ajs274a4a42004-12-07 15:39:31 +00002047static int
hasso8c328f12004-10-05 21:01:23 +00002048cmd_unique_string (vector v, const char *str)
paul718e3742002-12-13 20:15:29 +00002049{
hasso8c328f12004-10-05 21:01:23 +00002050 unsigned int i;
paul718e3742002-12-13 20:15:29 +00002051 char *match;
2052
paul55468c82005-03-14 20:19:01 +00002053 for (i = 0; i < vector_active (v); i++)
paul718e3742002-12-13 20:15:29 +00002054 if ((match = vector_slot (v, i)) != NULL)
2055 if (strcmp (match, str) == 0)
2056 return 0;
2057 return 1;
2058}
2059
Christian Frankecd40b322013-09-30 12:27:51 +00002060/**
2061 * Check whether a struct cmd_token matching a given string is already
2062 * present in a vector of struct cmd_token.
2063 * @param v A vector of struct cmd_token*.
2064 * @param str A char* which should be searched for.
2065 * @return 0 if there is a struct cmd_token* with its cmd matching str,
2066 * 1 otherwise.
2067 */
ajs274a4a42004-12-07 15:39:31 +00002068static int
hasso8c328f12004-10-05 21:01:23 +00002069desc_unique_string (vector v, const char *str)
paul718e3742002-12-13 20:15:29 +00002070{
hasso8c328f12004-10-05 21:01:23 +00002071 unsigned int i;
Christian Frankecd40b322013-09-30 12:27:51 +00002072 struct cmd_token *token;
paul718e3742002-12-13 20:15:29 +00002073
paul55468c82005-03-14 20:19:01 +00002074 for (i = 0; i < vector_active (v); i++)
Christian Frankecd40b322013-09-30 12:27:51 +00002075 if ((token = vector_slot (v, i)) != NULL)
2076 if (strcmp (token->cmd, str) == 0)
2077 return 0;
2078 return 1;
paul718e3742002-12-13 20:15:29 +00002079}
2080
ajs274a4a42004-12-07 15:39:31 +00002081static int
paulb92938a2002-12-13 21:20:42 +00002082cmd_try_do_shortcut (enum node_type node, char* first_word) {
2083 if ( first_word != NULL &&
2084 node != AUTH_NODE &&
2085 node != VIEW_NODE &&
2086 node != AUTH_ENABLE_NODE &&
2087 node != ENABLE_NODE &&
Paul Jakma62687ff2008-08-23 14:27:06 +01002088 node != RESTRICTED_NODE &&
paulb92938a2002-12-13 21:20:42 +00002089 0 == strcmp( "do", first_word ) )
2090 return 1;
2091 return 0;
2092}
2093
Christian Frankecd40b322013-09-30 12:27:51 +00002094static void
2095cmd_matches_free(vector *matches)
2096{
2097 unsigned int i;
2098 vector cmd_matches;
2099
2100 for (i = 0; i < vector_active(*matches); i++)
2101 if ((cmd_matches = vector_slot(*matches, i)) != NULL)
2102 vector_free(cmd_matches);
2103 vector_free(*matches);
2104 *matches = NULL;
2105}
2106
2107static int
2108cmd_describe_cmp(const void *a, const void *b)
2109{
2110 const struct cmd_token *first = *(struct cmd_token * const *)a;
2111 const struct cmd_token *second = *(struct cmd_token * const *)b;
2112
2113 return strcmp(first->cmd, second->cmd);
2114}
2115
2116static void
2117cmd_describe_sort(vector matchvec)
2118{
2119 qsort(matchvec->index, vector_active(matchvec),
2120 sizeof(void*), cmd_describe_cmp);
2121}
2122
paul718e3742002-12-13 20:15:29 +00002123/* '?' describe command support. */
ajs274a4a42004-12-07 15:39:31 +00002124static vector
paulb92938a2002-12-13 21:20:42 +00002125cmd_describe_command_real (vector vline, struct vty *vty, int *status)
paul718e3742002-12-13 20:15:29 +00002126{
paulb8961472005-03-14 17:35:52 +00002127 unsigned int i;
paul718e3742002-12-13 20:15:29 +00002128 vector cmd_vector;
2129#define INIT_MATCHVEC_SIZE 10
2130 vector matchvec;
2131 struct cmd_element *cmd_element;
paulb8961472005-03-14 17:35:52 +00002132 unsigned int index;
paul54aba542003-08-21 20:28:24 +00002133 int ret;
2134 enum match_type match;
2135 char *command;
Christian Frankecd40b322013-09-30 12:27:51 +00002136 vector matches = NULL;
2137 vector match_vector;
paul718e3742002-12-13 20:15:29 +00002138
2139 /* Set index. */
paul55468c82005-03-14 20:19:01 +00002140 if (vector_active (vline) == 0)
paulb8961472005-03-14 17:35:52 +00002141 {
2142 *status = CMD_ERR_NO_MATCH;
2143 return NULL;
2144 }
Christian Frankecd40b322013-09-30 12:27:51 +00002145
2146 index = vector_active (vline) - 1;
2147
paul718e3742002-12-13 20:15:29 +00002148 /* Make copy vector of current node's command vector. */
2149 cmd_vector = vector_copy (cmd_node_vector (cmdvec, vty->node));
2150
2151 /* Prepare match vector */
2152 matchvec = vector_init (INIT_MATCHVEC_SIZE);
2153
Christian Frankecd40b322013-09-30 12:27:51 +00002154 /* Filter commands and build a list how they could possibly continue. */
2155 for (i = 0; i <= index; i++)
2156 {
2157 command = vector_slot (vline, i);
paul718e3742002-12-13 20:15:29 +00002158
Christian Frankecd40b322013-09-30 12:27:51 +00002159 if (matches)
2160 cmd_matches_free(&matches);
paul718e3742002-12-13 20:15:29 +00002161
Christian Frankecd40b322013-09-30 12:27:51 +00002162 ret = cmd_vector_filter(cmd_vector,
2163 FILTER_RELAXED,
2164 vline, i,
2165 &match,
2166 &matches);
paul718e3742002-12-13 20:15:29 +00002167
Christian Frankecd40b322013-09-30 12:27:51 +00002168 if (ret != CMD_SUCCESS)
2169 {
2170 vector_free (cmd_vector);
2171 vector_free (matchvec);
2172 cmd_matches_free(&matches);
2173 *status = ret;
2174 return NULL;
2175 }
paul718e3742002-12-13 20:15:29 +00002176
Christian Frankecd40b322013-09-30 12:27:51 +00002177 /* The last match may well be ambigious, so break here */
2178 if (i == index)
2179 break;
paul718e3742002-12-13 20:15:29 +00002180
Christian Frankecd40b322013-09-30 12:27:51 +00002181 if (match == vararg_match)
2182 {
2183 /* We found a vararg match - so we can throw out the current matches here
2184 * and don't need to continue checking the command input */
2185 unsigned int j, k;
2186
2187 for (j = 0; j < vector_active (matches); j++)
2188 if ((match_vector = vector_slot (matches, j)) != NULL)
2189 for (k = 0; k < vector_active (match_vector); k++)
2190 {
2191 struct cmd_token *token = vector_slot (match_vector, k);
2192 vector_set (matchvec, token);
2193 }
2194
2195 *status = CMD_SUCCESS;
2196 vector_set(matchvec, &token_cr);
2197 vector_free (cmd_vector);
2198 cmd_matches_free(&matches);
2199 cmd_describe_sort(matchvec);
2200 return matchvec;
2201 }
2202
2203 ret = is_cmd_ambiguous(cmd_vector, command, matches, match);
2204 if (ret == 1)
2205 {
2206 vector_free (cmd_vector);
2207 vector_free (matchvec);
2208 cmd_matches_free(&matches);
2209 *status = CMD_ERR_AMBIGUOUS;
2210 return NULL;
2211 }
2212 else if (ret == 2)
2213 {
2214 vector_free (cmd_vector);
2215 vector_free (matchvec);
2216 cmd_matches_free(&matches);
2217 *status = CMD_ERR_NO_MATCH;
2218 return NULL;
2219 }
2220 }
paul54aba542003-08-21 20:28:24 +00002221
paul718e3742002-12-13 20:15:29 +00002222 /* Make description vector. */
Christian Frankecd40b322013-09-30 12:27:51 +00002223 for (i = 0; i < vector_active (matches); i++)
paul718e3742002-12-13 20:15:29 +00002224 if ((cmd_element = vector_slot (cmd_vector, i)) != NULL)
2225 {
Christian Frankecd40b322013-09-30 12:27:51 +00002226 unsigned int j;
2227 const char *last_word;
2228 vector vline_trimmed;
paul718e3742002-12-13 20:15:29 +00002229
Christian Frankecd40b322013-09-30 12:27:51 +00002230 last_word = vector_slot(vline, vector_active(vline) - 1);
2231 if (last_word == NULL || !strlen(last_word))
2232 {
2233 vline_trimmed = vector_copy(vline);
2234 vector_unset(vline_trimmed, vector_active(vline_trimmed) - 1);
paul718e3742002-12-13 20:15:29 +00002235
Christian Frankecd40b322013-09-30 12:27:51 +00002236 if (cmd_is_complete(cmd_element, vline_trimmed)
2237 && desc_unique_string(matchvec, command_cr))
2238 {
2239 if (match != vararg_match)
2240 vector_set(matchvec, &token_cr);
2241 }
Chris Caputo228da422009-07-18 05:44:03 +00002242
Christian Frankecd40b322013-09-30 12:27:51 +00002243 vector_free(vline_trimmed);
2244 }
2245
2246 match_vector = vector_slot (matches, i);
2247 if (match_vector)
2248 for (j = 0; j < vector_active(match_vector); j++)
2249 {
2250 struct cmd_token *token = vector_slot(match_vector, j);
2251 const char *string;
2252
David Lamparter10bac802015-05-05 11:10:20 +02002253 string = cmd_entry_function_desc(command, token);
Christian Frankecd40b322013-09-30 12:27:51 +00002254 if (string && desc_unique_string(matchvec, string))
2255 vector_set(matchvec, token);
2256 }
paul718e3742002-12-13 20:15:29 +00002257 }
2258 vector_free (cmd_vector);
Christian Frankecd40b322013-09-30 12:27:51 +00002259 cmd_matches_free(&matches);
paul718e3742002-12-13 20:15:29 +00002260
2261 if (vector_slot (matchvec, 0) == NULL)
2262 {
2263 vector_free (matchvec);
paul909a2152005-03-14 17:41:45 +00002264 *status = CMD_ERR_NO_MATCH;
Paul Jakma5fc60512006-05-12 23:24:09 +00002265 return NULL;
paul718e3742002-12-13 20:15:29 +00002266 }
paul718e3742002-12-13 20:15:29 +00002267
Paul Jakma5fc60512006-05-12 23:24:09 +00002268 *status = CMD_SUCCESS;
Christian Frankecd40b322013-09-30 12:27:51 +00002269 cmd_describe_sort(matchvec);
paul718e3742002-12-13 20:15:29 +00002270 return matchvec;
2271}
2272
paulb92938a2002-12-13 21:20:42 +00002273vector
2274cmd_describe_command (vector vline, struct vty *vty, int *status)
2275{
2276 vector ret;
2277
2278 if ( cmd_try_do_shortcut(vty->node, vector_slot(vline, 0) ) )
2279 {
2280 enum node_type onode;
2281 vector shifted_vline;
hasso8c328f12004-10-05 21:01:23 +00002282 unsigned int index;
paulb92938a2002-12-13 21:20:42 +00002283
2284 onode = vty->node;
2285 vty->node = ENABLE_NODE;
2286 /* We can try it on enable node, cos' the vty is authenticated */
2287
2288 shifted_vline = vector_init (vector_count(vline));
2289 /* use memcpy? */
paul55468c82005-03-14 20:19:01 +00002290 for (index = 1; index < vector_active (vline); index++)
paulb92938a2002-12-13 21:20:42 +00002291 {
2292 vector_set_index (shifted_vline, index-1, vector_lookup(vline, index));
2293 }
2294
2295 ret = cmd_describe_command_real (shifted_vline, vty, status);
2296
2297 vector_free(shifted_vline);
2298 vty->node = onode;
2299 return ret;
2300 }
2301
2302
2303 return cmd_describe_command_real (vline, vty, status);
2304}
2305
2306
paul718e3742002-12-13 20:15:29 +00002307/* Check LCD of matched command. */
ajs274a4a42004-12-07 15:39:31 +00002308static int
paul718e3742002-12-13 20:15:29 +00002309cmd_lcd (char **matched)
2310{
2311 int i;
2312 int j;
2313 int lcd = -1;
2314 char *s1, *s2;
2315 char c1, c2;
2316
2317 if (matched[0] == NULL || matched[1] == NULL)
2318 return 0;
2319
2320 for (i = 1; matched[i] != NULL; i++)
2321 {
2322 s1 = matched[i - 1];
2323 s2 = matched[i];
2324
2325 for (j = 0; (c1 = s1[j]) && (c2 = s2[j]); j++)
2326 if (c1 != c2)
2327 break;
2328
2329 if (lcd < 0)
2330 lcd = j;
2331 else
2332 {
2333 if (lcd > j)
2334 lcd = j;
2335 }
2336 }
2337 return lcd;
2338}
2339
Christian Frankecd40b322013-09-30 12:27:51 +00002340static int
2341cmd_complete_cmp(const void *a, const void *b)
2342{
2343 const char *first = *(char * const *)a;
2344 const char *second = *(char * const *)b;
2345
2346 if (!first)
2347 {
2348 if (!second)
2349 return 0;
2350 return 1;
2351 }
2352 if (!second)
2353 return -1;
2354
2355 return strcmp(first, second);
2356}
2357
2358static void
2359cmd_complete_sort(vector matchvec)
2360{
2361 qsort(matchvec->index, vector_active(matchvec),
2362 sizeof(void*), cmd_complete_cmp);
2363}
2364
paul718e3742002-12-13 20:15:29 +00002365/* Command line completion support. */
ajs274a4a42004-12-07 15:39:31 +00002366static char **
paulb92938a2002-12-13 21:20:42 +00002367cmd_complete_command_real (vector vline, struct vty *vty, int *status)
paul718e3742002-12-13 20:15:29 +00002368{
paulb8961472005-03-14 17:35:52 +00002369 unsigned int i;
paul718e3742002-12-13 20:15:29 +00002370 vector cmd_vector = vector_copy (cmd_node_vector (cmdvec, vty->node));
2371#define INIT_MATCHVEC_SIZE 10
2372 vector matchvec;
paulb8961472005-03-14 17:35:52 +00002373 unsigned int index;
paul718e3742002-12-13 20:15:29 +00002374 char **match_str;
Christian Frankecd40b322013-09-30 12:27:51 +00002375 struct cmd_token *token;
paul718e3742002-12-13 20:15:29 +00002376 char *command;
2377 int lcd;
Christian Frankecd40b322013-09-30 12:27:51 +00002378 vector matches = NULL;
2379 vector match_vector;
paul718e3742002-12-13 20:15:29 +00002380
paul55468c82005-03-14 20:19:01 +00002381 if (vector_active (vline) == 0)
paulb8961472005-03-14 17:35:52 +00002382 {
Paul Jakmad2519962006-05-12 23:19:37 +00002383 vector_free (cmd_vector);
paulb8961472005-03-14 17:35:52 +00002384 *status = CMD_ERR_NO_MATCH;
2385 return NULL;
2386 }
2387 else
paul55468c82005-03-14 20:19:01 +00002388 index = vector_active (vline) - 1;
paulb8961472005-03-14 17:35:52 +00002389
Christian Frankecd40b322013-09-30 12:27:51 +00002390 /* First, filter by command string */
2391 for (i = 0; i <= index; i++)
2392 {
2393 command = vector_slot (vline, i);
2394 enum match_type match;
2395 int ret;
paul718e3742002-12-13 20:15:29 +00002396
Christian Frankecd40b322013-09-30 12:27:51 +00002397 if (matches)
2398 cmd_matches_free(&matches);
paul718e3742002-12-13 20:15:29 +00002399
Christian Frankecd40b322013-09-30 12:27:51 +00002400 /* First try completion match, if there is exactly match return 1 */
2401 ret = cmd_vector_filter(cmd_vector,
2402 FILTER_RELAXED,
2403 vline, i,
2404 &match,
2405 &matches);
2406
2407 if (ret != CMD_SUCCESS)
2408 {
2409 vector_free(cmd_vector);
2410 cmd_matches_free(&matches);
2411 *status = ret;
2412 return NULL;
2413 }
2414
2415 /* Break here - the completion mustn't be checked to be non-ambiguous */
2416 if (i == index)
2417 break;
2418
2419 /* If there is exact match then filter ambiguous match else check
2420 ambiguousness. */
2421 ret = is_cmd_ambiguous (cmd_vector, command, matches, match);
2422 if (ret == 1)
2423 {
2424 vector_free (cmd_vector);
2425 cmd_matches_free(&matches);
2426 *status = CMD_ERR_AMBIGUOUS;
2427 return NULL;
2428 }
2429 /*
paul909a2152005-03-14 17:41:45 +00002430 else if (ret == 2)
2431 {
2432 vector_free (cmd_vector);
Christian Frankecd40b322013-09-30 12:27:51 +00002433 cmd_matches_free(&matches);
paul909a2152005-03-14 17:41:45 +00002434 *status = CMD_ERR_NO_MATCH;
2435 return NULL;
2436 }
2437 */
Christian Frankecd40b322013-09-30 12:27:51 +00002438 }
paul909a2152005-03-14 17:41:45 +00002439
paul718e3742002-12-13 20:15:29 +00002440 /* Prepare match vector. */
2441 matchvec = vector_init (INIT_MATCHVEC_SIZE);
2442
Christian Frankecd40b322013-09-30 12:27:51 +00002443 /* Build the possible list of continuations into a list of completions */
2444 for (i = 0; i < vector_active (matches); i++)
2445 if ((match_vector = vector_slot (matches, i)))
paul718e3742002-12-13 20:15:29 +00002446 {
hasso8c328f12004-10-05 21:01:23 +00002447 const char *string;
Christian Frankecd40b322013-09-30 12:27:51 +00002448 unsigned int j;
paul909a2152005-03-14 17:41:45 +00002449
Christian Frankecd40b322013-09-30 12:27:51 +00002450 for (j = 0; j < vector_active (match_vector); j++)
2451 if ((token = vector_slot (match_vector, j)))
paul909a2152005-03-14 17:41:45 +00002452 {
paulb8961472005-03-14 17:35:52 +00002453 if ((string =
2454 cmd_entry_function (vector_slot (vline, index),
David Lamparter10bac802015-05-05 11:10:20 +02002455 token)))
paul909a2152005-03-14 17:41:45 +00002456 if (cmd_unique_string (matchvec, string))
2457 vector_set (matchvec, XSTRDUP (MTYPE_TMP, string));
2458 }
paul718e3742002-12-13 20:15:29 +00002459 }
2460
2461 /* We don't need cmd_vector any more. */
2462 vector_free (cmd_vector);
Christian Frankecd40b322013-09-30 12:27:51 +00002463 cmd_matches_free(&matches);
paul718e3742002-12-13 20:15:29 +00002464
2465 /* No matched command */
2466 if (vector_slot (matchvec, 0) == NULL)
2467 {
2468 vector_free (matchvec);
2469
2470 /* In case of 'command \t' pattern. Do you need '?' command at
2471 the end of the line. */
2472 if (vector_slot (vline, index) == '\0')
2473 *status = CMD_ERR_NOTHING_TODO;
2474 else
2475 *status = CMD_ERR_NO_MATCH;
2476 return NULL;
2477 }
2478
2479 /* Only one matched */
2480 if (vector_slot (matchvec, 1) == NULL)
2481 {
2482 match_str = (char **) matchvec->index;
2483 vector_only_wrapper_free (matchvec);
2484 *status = CMD_COMPLETE_FULL_MATCH;
2485 return match_str;
2486 }
2487 /* Make it sure last element is NULL. */
2488 vector_set (matchvec, NULL);
2489
2490 /* Check LCD of matched strings. */
2491 if (vector_slot (vline, index) != NULL)
2492 {
2493 lcd = cmd_lcd ((char **) matchvec->index);
2494
2495 if (lcd)
2496 {
2497 int len = strlen (vector_slot (vline, index));
paul909a2152005-03-14 17:41:45 +00002498
paul718e3742002-12-13 20:15:29 +00002499 if (len < lcd)
2500 {
2501 char *lcdstr;
paul909a2152005-03-14 17:41:45 +00002502
Christian Frankecd40b322013-09-30 12:27:51 +00002503 lcdstr = XMALLOC (MTYPE_TMP, lcd + 1);
paul718e3742002-12-13 20:15:29 +00002504 memcpy (lcdstr, matchvec->index[0], lcd);
2505 lcdstr[lcd] = '\0';
2506
2507 /* match_str = (char **) &lcdstr; */
2508
2509 /* Free matchvec. */
paul55468c82005-03-14 20:19:01 +00002510 for (i = 0; i < vector_active (matchvec); i++)
paul718e3742002-12-13 20:15:29 +00002511 {
2512 if (vector_slot (matchvec, i))
Christian Frankecd40b322013-09-30 12:27:51 +00002513 XFREE (MTYPE_TMP, vector_slot (matchvec, i));
paul718e3742002-12-13 20:15:29 +00002514 }
2515 vector_free (matchvec);
2516
paul909a2152005-03-14 17:41:45 +00002517 /* Make new matchvec. */
paul718e3742002-12-13 20:15:29 +00002518 matchvec = vector_init (INIT_MATCHVEC_SIZE);
2519 vector_set (matchvec, lcdstr);
2520 match_str = (char **) matchvec->index;
2521 vector_only_wrapper_free (matchvec);
2522
2523 *status = CMD_COMPLETE_MATCH;
2524 return match_str;
2525 }
2526 }
2527 }
2528
2529 match_str = (char **) matchvec->index;
Christian Frankecd40b322013-09-30 12:27:51 +00002530 cmd_complete_sort(matchvec);
paul718e3742002-12-13 20:15:29 +00002531 vector_only_wrapper_free (matchvec);
2532 *status = CMD_COMPLETE_LIST_MATCH;
2533 return match_str;
2534}
2535
paulb92938a2002-12-13 21:20:42 +00002536char **
paul9ab68122003-01-18 01:16:20 +00002537cmd_complete_command (vector vline, struct vty *vty, int *status)
paulb92938a2002-12-13 21:20:42 +00002538{
2539 char **ret;
2540
2541 if ( cmd_try_do_shortcut(vty->node, vector_slot(vline, 0) ) )
2542 {
2543 enum node_type onode;
2544 vector shifted_vline;
hasso8c328f12004-10-05 21:01:23 +00002545 unsigned int index;
paulb92938a2002-12-13 21:20:42 +00002546
2547 onode = vty->node;
2548 vty->node = ENABLE_NODE;
2549 /* We can try it on enable node, cos' the vty is authenticated */
2550
2551 shifted_vline = vector_init (vector_count(vline));
2552 /* use memcpy? */
paul55468c82005-03-14 20:19:01 +00002553 for (index = 1; index < vector_active (vline); index++)
paulb92938a2002-12-13 21:20:42 +00002554 {
2555 vector_set_index (shifted_vline, index-1, vector_lookup(vline, index));
2556 }
2557
2558 ret = cmd_complete_command_real (shifted_vline, vty, status);
2559
2560 vector_free(shifted_vline);
2561 vty->node = onode;
2562 return ret;
2563 }
2564
2565
2566 return cmd_complete_command_real (vline, vty, status);
2567}
2568
2569/* return parent node */
2570/* MUST eventually converge on CONFIG_NODE */
hasso13bfca72005-01-23 21:42:25 +00002571enum node_type
ajs274a4a42004-12-07 15:39:31 +00002572node_parent ( enum node_type node )
paulb92938a2002-12-13 21:20:42 +00002573{
2574 enum node_type ret;
2575
paul9ab68122003-01-18 01:16:20 +00002576 assert (node > CONFIG_NODE);
2577
2578 switch (node)
2579 {
2580 case BGP_VPNV4_NODE:
2581 case BGP_IPV4_NODE:
2582 case BGP_IPV4M_NODE:
2583 case BGP_IPV6_NODE:
paul1e836592005-08-22 22:39:56 +00002584 case BGP_IPV6M_NODE:
paul9ab68122003-01-18 01:16:20 +00002585 ret = BGP_NODE;
2586 break;
2587 case KEYCHAIN_KEY_NODE:
2588 ret = KEYCHAIN_NODE;
2589 break;
2590 default:
2591 ret = CONFIG_NODE;
paulb92938a2002-12-13 21:20:42 +00002592 }
2593
2594 return ret;
2595}
2596
paul718e3742002-12-13 20:15:29 +00002597/* Execute command by argument vline vector. */
ajs274a4a42004-12-07 15:39:31 +00002598static int
Christian Frankecd40b322013-09-30 12:27:51 +00002599cmd_execute_command_real (vector vline,
2600 enum filter_type filter,
2601 struct vty *vty,
paulb8961472005-03-14 17:35:52 +00002602 struct cmd_element **cmd)
paul718e3742002-12-13 20:15:29 +00002603{
hasso8c328f12004-10-05 21:01:23 +00002604 unsigned int i;
2605 unsigned int index;
paul718e3742002-12-13 20:15:29 +00002606 vector cmd_vector;
2607 struct cmd_element *cmd_element;
2608 struct cmd_element *matched_element;
2609 unsigned int matched_count, incomplete_count;
2610 int argc;
paul9035efa2004-10-10 11:56:56 +00002611 const char *argv[CMD_ARGC_MAX];
paul718e3742002-12-13 20:15:29 +00002612 enum match_type match = 0;
paul718e3742002-12-13 20:15:29 +00002613 char *command;
Christian Frankecd40b322013-09-30 12:27:51 +00002614 int ret;
2615 vector matches;
paul718e3742002-12-13 20:15:29 +00002616
2617 /* Make copy of command elements. */
2618 cmd_vector = vector_copy (cmd_node_vector (cmdvec, vty->node));
2619
paul55468c82005-03-14 20:19:01 +00002620 for (index = 0; index < vector_active (vline); index++)
Christian Frankecd40b322013-09-30 12:27:51 +00002621 {
2622 command = vector_slot (vline, index);
2623 ret = cmd_vector_filter(cmd_vector,
2624 filter,
2625 vline, index,
2626 &match,
2627 &matches);
paul718e3742002-12-13 20:15:29 +00002628
Christian Frankecd40b322013-09-30 12:27:51 +00002629 if (ret != CMD_SUCCESS)
2630 {
2631 cmd_matches_free(&matches);
2632 return ret;
2633 }
paul718e3742002-12-13 20:15:29 +00002634
Christian Frankecd40b322013-09-30 12:27:51 +00002635 if (match == vararg_match)
2636 {
2637 cmd_matches_free(&matches);
paul909a2152005-03-14 17:41:45 +00002638 break;
Christian Frankecd40b322013-09-30 12:27:51 +00002639 }
paul718e3742002-12-13 20:15:29 +00002640
Christian Frankecd40b322013-09-30 12:27:51 +00002641 ret = is_cmd_ambiguous (cmd_vector, command, matches, match);
2642 cmd_matches_free(&matches);
2643
2644 if (ret == 1)
2645 {
2646 vector_free(cmd_vector);
2647 return CMD_ERR_AMBIGUOUS;
2648 }
2649 else if (ret == 2)
2650 {
2651 vector_free(cmd_vector);
2652 return CMD_ERR_NO_MATCH;
2653 }
2654 }
paul718e3742002-12-13 20:15:29 +00002655
2656 /* Check matched count. */
2657 matched_element = NULL;
2658 matched_count = 0;
2659 incomplete_count = 0;
2660
paul55468c82005-03-14 20:19:01 +00002661 for (i = 0; i < vector_active (cmd_vector); i++)
paulb8961472005-03-14 17:35:52 +00002662 if ((cmd_element = vector_slot (cmd_vector, i)))
paul718e3742002-12-13 20:15:29 +00002663 {
Christian Frankecd40b322013-09-30 12:27:51 +00002664 if (cmd_is_complete(cmd_element, vline))
paul718e3742002-12-13 20:15:29 +00002665 {
2666 matched_element = cmd_element;
paul718e3742002-12-13 20:15:29 +00002667 matched_count++;
2668 }
2669 else
2670 {
2671 incomplete_count++;
2672 }
2673 }
paul909a2152005-03-14 17:41:45 +00002674
paul718e3742002-12-13 20:15:29 +00002675 /* Finish of using cmd_vector. */
2676 vector_free (cmd_vector);
2677
paul909a2152005-03-14 17:41:45 +00002678 /* To execute command, matched_count must be 1. */
2679 if (matched_count == 0)
paul718e3742002-12-13 20:15:29 +00002680 {
2681 if (incomplete_count)
2682 return CMD_ERR_INCOMPLETE;
2683 else
2684 return CMD_ERR_NO_MATCH;
2685 }
2686
paul909a2152005-03-14 17:41:45 +00002687 if (matched_count > 1)
paul718e3742002-12-13 20:15:29 +00002688 return CMD_ERR_AMBIGUOUS;
2689
Christian Frankecd40b322013-09-30 12:27:51 +00002690 ret = cmd_parse(matched_element, vline, &argc, argv);
2691 if (ret != CMD_SUCCESS)
2692 return ret;
paul718e3742002-12-13 20:15:29 +00002693
2694 /* For vtysh execution. */
2695 if (cmd)
2696 *cmd = matched_element;
2697
2698 if (matched_element->daemon)
2699 return CMD_SUCCESS_DAEMON;
2700
2701 /* Execute matched command. */
2702 return (*matched_element->func) (matched_element, vty, argc, argv);
2703}
2704
Christian Frankecd40b322013-09-30 12:27:51 +00002705/**
2706 * Execute a given command, handling things like "do ..." and checking
2707 * whether the given command might apply at a parent node if doesn't
2708 * apply for the current node.
2709 *
2710 * @param vline Command line input, vector of char* where each element is
2711 * one input token.
2712 * @param vty The vty context in which the command should be executed.
2713 * @param cmd Pointer where the struct cmd_element of the matched command
2714 * will be stored, if any. May be set to NULL if this info is
2715 * not needed.
2716 * @param vtysh If set != 0, don't lookup the command at parent nodes.
2717 * @return The status of the command that has been executed or an error code
2718 * as to why no command could be executed.
2719 */
paulb92938a2002-12-13 21:20:42 +00002720int
hasso87d683b2005-01-16 23:31:54 +00002721cmd_execute_command (vector vline, struct vty *vty, struct cmd_element **cmd,
2722 int vtysh) {
paul9ab68122003-01-18 01:16:20 +00002723 int ret, saved_ret, tried = 0;
2724 enum node_type onode, try_node;
2725
2726 onode = try_node = vty->node;
paulb92938a2002-12-13 21:20:42 +00002727
2728 if ( cmd_try_do_shortcut(vty->node, vector_slot(vline, 0) ) )
2729 {
2730 vector shifted_vline;
hasso8c328f12004-10-05 21:01:23 +00002731 unsigned int index;
paulb92938a2002-12-13 21:20:42 +00002732
2733 vty->node = ENABLE_NODE;
2734 /* We can try it on enable node, cos' the vty is authenticated */
2735
2736 shifted_vline = vector_init (vector_count(vline));
2737 /* use memcpy? */
paul55468c82005-03-14 20:19:01 +00002738 for (index = 1; index < vector_active (vline); index++)
paulb92938a2002-12-13 21:20:42 +00002739 {
2740 vector_set_index (shifted_vline, index-1, vector_lookup(vline, index));
2741 }
2742
Christian Frankecd40b322013-09-30 12:27:51 +00002743 ret = cmd_execute_command_real (shifted_vline, FILTER_RELAXED, vty, cmd);
paulb92938a2002-12-13 21:20:42 +00002744
2745 vector_free(shifted_vline);
2746 vty->node = onode;
2747 return ret;
2748 }
2749
2750
Christian Frankecd40b322013-09-30 12:27:51 +00002751 saved_ret = ret = cmd_execute_command_real (vline, FILTER_RELAXED, vty, cmd);
paulb92938a2002-12-13 21:20:42 +00002752
hasso87d683b2005-01-16 23:31:54 +00002753 if (vtysh)
2754 return saved_ret;
2755
paulb92938a2002-12-13 21:20:42 +00002756 /* This assumes all nodes above CONFIG_NODE are childs of CONFIG_NODE */
paul9ab68122003-01-18 01:16:20 +00002757 while ( ret != CMD_SUCCESS && ret != CMD_WARNING
paulb92938a2002-12-13 21:20:42 +00002758 && vty->node > CONFIG_NODE )
2759 {
paul9ab68122003-01-18 01:16:20 +00002760 try_node = node_parent(try_node);
2761 vty->node = try_node;
Christian Frankecd40b322013-09-30 12:27:51 +00002762 ret = cmd_execute_command_real (vline, FILTER_RELAXED, vty, cmd);
paul9ab68122003-01-18 01:16:20 +00002763 tried = 1;
2764 if (ret == CMD_SUCCESS || ret == CMD_WARNING)
paulb92938a2002-12-13 21:20:42 +00002765 {
paul9ab68122003-01-18 01:16:20 +00002766 /* succesfull command, leave the node as is */
paulb92938a2002-12-13 21:20:42 +00002767 return ret;
2768 }
paulb92938a2002-12-13 21:20:42 +00002769 }
paul9ab68122003-01-18 01:16:20 +00002770 /* no command succeeded, reset the vty to the original node and
2771 return the error for this node */
2772 if ( tried )
2773 vty->node = onode;
2774 return saved_ret;
pauleda031f2003-01-18 00:39:19 +00002775}
2776
Christian Frankecd40b322013-09-30 12:27:51 +00002777/**
2778 * Execute a given command, matching it strictly against the current node.
2779 * This mode is used when reading config files.
2780 *
2781 * @param vline Command line input, vector of char* where each element is
2782 * one input token.
2783 * @param vty The vty context in which the command should be executed.
2784 * @param cmd Pointer where the struct cmd_element* of the matched command
2785 * will be stored, if any. May be set to NULL if this info is
2786 * not needed.
2787 * @return The status of the command that has been executed or an error code
2788 * as to why no command could be executed.
2789 */
paul718e3742002-12-13 20:15:29 +00002790int
paul909a2152005-03-14 17:41:45 +00002791cmd_execute_command_strict (vector vline, struct vty *vty,
paul718e3742002-12-13 20:15:29 +00002792 struct cmd_element **cmd)
2793{
Christian Frankecd40b322013-09-30 12:27:51 +00002794 return cmd_execute_command_real(vline, FILTER_STRICT, vty, cmd);
paul718e3742002-12-13 20:15:29 +00002795}
2796
2797/* Configration make from file. */
2798int
Steve Hillea555002009-07-28 16:36:14 -04002799config_from_file (struct vty *vty, FILE *fp, unsigned int *line_num)
paul718e3742002-12-13 20:15:29 +00002800{
2801 int ret;
Steve Hillea555002009-07-28 16:36:14 -04002802 *line_num = 0;
paul718e3742002-12-13 20:15:29 +00002803 vector vline;
2804
2805 while (fgets (vty->buf, VTY_BUFSIZ, fp))
2806 {
Steve Hillea555002009-07-28 16:36:14 -04002807 ++(*line_num);
paul718e3742002-12-13 20:15:29 +00002808 vline = cmd_make_strvec (vty->buf);
2809
2810 /* In case of comment line */
2811 if (vline == NULL)
2812 continue;
2813 /* Execute configuration command : this is strict match */
2814 ret = cmd_execute_command_strict (vline, vty, NULL);
2815
2816 /* Try again with setting node to CONFIG_NODE */
paulb92938a2002-12-13 21:20:42 +00002817 while (ret != CMD_SUCCESS && ret != CMD_WARNING
hassoddd85ed2004-10-13 08:18:07 +00002818 && ret != CMD_ERR_NOTHING_TODO && vty->node != CONFIG_NODE)
2819 {
paulb92938a2002-12-13 21:20:42 +00002820 vty->node = node_parent(vty->node);
hassoddd85ed2004-10-13 08:18:07 +00002821 ret = cmd_execute_command_strict (vline, vty, NULL);
2822 }
paul9ab68122003-01-18 01:16:20 +00002823
paul718e3742002-12-13 20:15:29 +00002824 cmd_free_strvec (vline);
2825
hassoddd85ed2004-10-13 08:18:07 +00002826 if (ret != CMD_SUCCESS && ret != CMD_WARNING
2827 && ret != CMD_ERR_NOTHING_TODO)
paul718e3742002-12-13 20:15:29 +00002828 return ret;
2829 }
2830 return CMD_SUCCESS;
2831}
2832
2833/* Configration from terminal */
2834DEFUN (config_terminal,
2835 config_terminal_cmd,
2836 "configure terminal",
2837 "Configuration from vty interface\n"
2838 "Configuration terminal\n")
2839{
2840 if (vty_config_lock (vty))
2841 vty->node = CONFIG_NODE;
2842 else
2843 {
2844 vty_out (vty, "VTY configuration is locked by other VTY%s", VTY_NEWLINE);
2845 return CMD_WARNING;
2846 }
2847 return CMD_SUCCESS;
2848}
2849
2850/* Enable command */
2851DEFUN (enable,
2852 config_enable_cmd,
2853 "enable",
2854 "Turn on privileged mode command\n")
2855{
2856 /* If enable password is NULL, change to ENABLE_NODE */
2857 if ((host.enable == NULL && host.enable_encrypt == NULL) ||
2858 vty->type == VTY_SHELL_SERV)
2859 vty->node = ENABLE_NODE;
2860 else
2861 vty->node = AUTH_ENABLE_NODE;
2862
2863 return CMD_SUCCESS;
2864}
2865
2866/* Disable command */
2867DEFUN (disable,
2868 config_disable_cmd,
2869 "disable",
2870 "Turn off privileged mode command\n")
2871{
2872 if (vty->node == ENABLE_NODE)
2873 vty->node = VIEW_NODE;
2874 return CMD_SUCCESS;
2875}
2876
2877/* Down vty node level. */
2878DEFUN (config_exit,
2879 config_exit_cmd,
2880 "exit",
2881 "Exit current mode and down to previous mode\n")
2882{
2883 switch (vty->node)
2884 {
2885 case VIEW_NODE:
2886 case ENABLE_NODE:
Paul Jakma62687ff2008-08-23 14:27:06 +01002887 case RESTRICTED_NODE:
paul718e3742002-12-13 20:15:29 +00002888 if (vty_shell (vty))
2889 exit (0);
2890 else
2891 vty->status = VTY_CLOSE;
2892 break;
2893 case CONFIG_NODE:
2894 vty->node = ENABLE_NODE;
2895 vty_config_unlock (vty);
2896 break;
2897 case INTERFACE_NODE:
2898 case ZEBRA_NODE:
2899 case BGP_NODE:
2900 case RIP_NODE:
2901 case RIPNG_NODE:
Paul Jakma57345092011-12-25 17:52:09 +01002902 case BABEL_NODE:
paul718e3742002-12-13 20:15:29 +00002903 case OSPF_NODE:
2904 case OSPF6_NODE:
jardin9e867fe2003-12-23 08:56:18 +00002905 case ISIS_NODE:
paul718e3742002-12-13 20:15:29 +00002906 case KEYCHAIN_NODE:
2907 case MASC_NODE:
2908 case RMAP_NODE:
Everton Marques42e30782009-11-18 17:19:43 -02002909 case PIM_NODE:
paul718e3742002-12-13 20:15:29 +00002910 case VTY_NODE:
2911 vty->node = CONFIG_NODE;
2912 break;
2913 case BGP_VPNV4_NODE:
2914 case BGP_IPV4_NODE:
2915 case BGP_IPV4M_NODE:
2916 case BGP_IPV6_NODE:
paul1e836592005-08-22 22:39:56 +00002917 case BGP_IPV6M_NODE:
paul718e3742002-12-13 20:15:29 +00002918 vty->node = BGP_NODE;
2919 break;
2920 case KEYCHAIN_KEY_NODE:
2921 vty->node = KEYCHAIN_NODE;
2922 break;
2923 default:
2924 break;
2925 }
2926 return CMD_SUCCESS;
2927}
2928
2929/* quit is alias of exit. */
2930ALIAS (config_exit,
2931 config_quit_cmd,
2932 "quit",
2933 "Exit current mode and down to previous mode\n")
2934
2935/* End of configuration. */
2936DEFUN (config_end,
2937 config_end_cmd,
2938 "end",
2939 "End current mode and change to enable mode.")
2940{
2941 switch (vty->node)
2942 {
2943 case VIEW_NODE:
2944 case ENABLE_NODE:
Paul Jakma62687ff2008-08-23 14:27:06 +01002945 case RESTRICTED_NODE:
paul718e3742002-12-13 20:15:29 +00002946 /* Nothing to do. */
2947 break;
2948 case CONFIG_NODE:
2949 case INTERFACE_NODE:
2950 case ZEBRA_NODE:
2951 case RIP_NODE:
2952 case RIPNG_NODE:
Paul Jakma57345092011-12-25 17:52:09 +01002953 case BABEL_NODE:
paul718e3742002-12-13 20:15:29 +00002954 case BGP_NODE:
2955 case BGP_VPNV4_NODE:
2956 case BGP_IPV4_NODE:
2957 case BGP_IPV4M_NODE:
2958 case BGP_IPV6_NODE:
paul1e836592005-08-22 22:39:56 +00002959 case BGP_IPV6M_NODE:
paul718e3742002-12-13 20:15:29 +00002960 case RMAP_NODE:
2961 case OSPF_NODE:
2962 case OSPF6_NODE:
jardin9e867fe2003-12-23 08:56:18 +00002963 case ISIS_NODE:
paul718e3742002-12-13 20:15:29 +00002964 case KEYCHAIN_NODE:
2965 case KEYCHAIN_KEY_NODE:
2966 case MASC_NODE:
Everton Marques42e30782009-11-18 17:19:43 -02002967 case PIM_NODE:
paul718e3742002-12-13 20:15:29 +00002968 case VTY_NODE:
2969 vty_config_unlock (vty);
2970 vty->node = ENABLE_NODE;
2971 break;
2972 default:
2973 break;
2974 }
2975 return CMD_SUCCESS;
2976}
2977
2978/* Show version. */
2979DEFUN (show_version,
2980 show_version_cmd,
2981 "show version",
2982 SHOW_STR
2983 "Displays zebra version\n")
2984{
hasso12f6ea22005-03-07 08:35:39 +00002985 vty_out (vty, "Quagga %s (%s).%s", QUAGGA_VERSION, host.name?host.name:"",
2986 VTY_NEWLINE);
David Lamparter0be793e2012-11-27 01:34:56 +00002987 vty_out (vty, "%s%s%s", QUAGGA_COPYRIGHT, GIT_INFO, VTY_NEWLINE);
David Lamparter7abd8752014-11-22 10:43:29 -08002988 vty_out (vty, "configured with:%s %s%s", VTY_NEWLINE,
2989 QUAGGA_CONFIG_ARGS, VTY_NEWLINE);
paul718e3742002-12-13 20:15:29 +00002990
2991 return CMD_SUCCESS;
2992}
2993
2994/* Help display function for all node. */
2995DEFUN (config_help,
2996 config_help_cmd,
2997 "help",
2998 "Description of the interactive help system\n")
2999{
3000 vty_out (vty,
hasso6590f2c2004-10-19 20:40:08 +00003001 "Quagga VTY provides advanced help feature. When you need help,%s\
paul718e3742002-12-13 20:15:29 +00003002anytime at the command line please press '?'.%s\
3003%s\
3004If nothing matches, the help list will be empty and you must backup%s\
3005 until entering a '?' shows the available options.%s\
3006Two styles of help are provided:%s\
30071. Full help is available when you are ready to enter a%s\
3008command argument (e.g. 'show ?') and describes each possible%s\
3009argument.%s\
30102. Partial help is provided when an abbreviated argument is entered%s\
3011 and you want to know what arguments match the input%s\
3012 (e.g. 'show me?'.)%s%s", VTY_NEWLINE, VTY_NEWLINE, VTY_NEWLINE,
3013 VTY_NEWLINE, VTY_NEWLINE, VTY_NEWLINE, VTY_NEWLINE, VTY_NEWLINE,
3014 VTY_NEWLINE, VTY_NEWLINE, VTY_NEWLINE, VTY_NEWLINE, VTY_NEWLINE);
3015 return CMD_SUCCESS;
3016}
3017
3018/* Help display function for all node. */
3019DEFUN (config_list,
3020 config_list_cmd,
3021 "list",
3022 "Print command list\n")
3023{
hasso8c328f12004-10-05 21:01:23 +00003024 unsigned int i;
paul718e3742002-12-13 20:15:29 +00003025 struct cmd_node *cnode = vector_slot (cmdvec, vty->node);
3026 struct cmd_element *cmd;
3027
paul55468c82005-03-14 20:19:01 +00003028 for (i = 0; i < vector_active (cnode->cmd_vector); i++)
paul4275b1d2005-03-09 13:42:23 +00003029 if ((cmd = vector_slot (cnode->cmd_vector, i)) != NULL
3030 && !(cmd->attr == CMD_ATTR_DEPRECATED
3031 || cmd->attr == CMD_ATTR_HIDDEN))
paul718e3742002-12-13 20:15:29 +00003032 vty_out (vty, " %s%s", cmd->string,
3033 VTY_NEWLINE);
3034 return CMD_SUCCESS;
3035}
3036
3037/* Write current configuration into file. */
3038DEFUN (config_write_file,
3039 config_write_file_cmd,
3040 "write file",
3041 "Write running configuration to memory, network, or terminal\n"
3042 "Write to configuration file\n")
3043{
hasso8c328f12004-10-05 21:01:23 +00003044 unsigned int i;
paul718e3742002-12-13 20:15:29 +00003045 int fd;
3046 struct cmd_node *node;
3047 char *config_file;
3048 char *config_file_tmp = NULL;
3049 char *config_file_sav = NULL;
paul05865c92005-10-26 05:49:54 +00003050 int ret = CMD_WARNING;
paul718e3742002-12-13 20:15:29 +00003051 struct vty *file_vty;
3052
3053 /* Check and see if we are operating under vtysh configuration */
3054 if (host.config == NULL)
3055 {
3056 vty_out (vty, "Can't save to configuration file, using vtysh.%s",
3057 VTY_NEWLINE);
3058 return CMD_WARNING;
3059 }
3060
3061 /* Get filename. */
3062 config_file = host.config;
3063
paul05865c92005-10-26 05:49:54 +00003064 config_file_sav =
3065 XMALLOC (MTYPE_TMP, strlen (config_file) + strlen (CONF_BACKUP_EXT) + 1);
paul718e3742002-12-13 20:15:29 +00003066 strcpy (config_file_sav, config_file);
3067 strcat (config_file_sav, CONF_BACKUP_EXT);
3068
3069
paul05865c92005-10-26 05:49:54 +00003070 config_file_tmp = XMALLOC (MTYPE_TMP, strlen (config_file) + 8);
paul718e3742002-12-13 20:15:29 +00003071 sprintf (config_file_tmp, "%s.XXXXXX", config_file);
3072
3073 /* Open file to configuration write. */
3074 fd = mkstemp (config_file_tmp);
3075 if (fd < 0)
3076 {
3077 vty_out (vty, "Can't open configuration file %s.%s", config_file_tmp,
3078 VTY_NEWLINE);
paul05865c92005-10-26 05:49:54 +00003079 goto finished;
paul718e3742002-12-13 20:15:29 +00003080 }
3081
3082 /* Make vty for configuration file. */
3083 file_vty = vty_new ();
3084 file_vty->fd = fd;
3085 file_vty->type = VTY_FILE;
3086
3087 /* Config file header print. */
3088 vty_out (file_vty, "!\n! Zebra configuration saved from vty\n! ");
3089 vty_time_print (file_vty, 1);
3090 vty_out (file_vty, "!\n");
3091
paul55468c82005-03-14 20:19:01 +00003092 for (i = 0; i < vector_active (cmdvec); i++)
paul718e3742002-12-13 20:15:29 +00003093 if ((node = vector_slot (cmdvec, i)) && node->func)
3094 {
3095 if ((*node->func) (file_vty))
3096 vty_out (file_vty, "!\n");
3097 }
3098 vty_close (file_vty);
3099
3100 if (unlink (config_file_sav) != 0)
3101 if (errno != ENOENT)
3102 {
3103 vty_out (vty, "Can't unlink backup configuration file %s.%s", config_file_sav,
3104 VTY_NEWLINE);
paul05865c92005-10-26 05:49:54 +00003105 goto finished;
paul718e3742002-12-13 20:15:29 +00003106 }
3107 if (link (config_file, config_file_sav) != 0)
3108 {
3109 vty_out (vty, "Can't backup old configuration file %s.%s", config_file_sav,
3110 VTY_NEWLINE);
paul05865c92005-10-26 05:49:54 +00003111 goto finished;
paul718e3742002-12-13 20:15:29 +00003112 }
3113 sync ();
3114 if (unlink (config_file) != 0)
3115 {
3116 vty_out (vty, "Can't unlink configuration file %s.%s", config_file,
3117 VTY_NEWLINE);
paul05865c92005-10-26 05:49:54 +00003118 goto finished;
paul718e3742002-12-13 20:15:29 +00003119 }
3120 if (link (config_file_tmp, config_file) != 0)
3121 {
3122 vty_out (vty, "Can't save configuration file %s.%s", config_file,
3123 VTY_NEWLINE);
paul05865c92005-10-26 05:49:54 +00003124 goto finished;
paul718e3742002-12-13 20:15:29 +00003125 }
paul718e3742002-12-13 20:15:29 +00003126 sync ();
3127
gdtaa593d52003-12-22 20:15:53 +00003128 if (chmod (config_file, CONFIGFILE_MASK) != 0)
3129 {
3130 vty_out (vty, "Can't chmod configuration file %s: %s (%d).%s",
ajs6099b3b2004-11-20 02:06:59 +00003131 config_file, safe_strerror(errno), errno, VTY_NEWLINE);
paul05865c92005-10-26 05:49:54 +00003132 goto finished;
gdtaa593d52003-12-22 20:15:53 +00003133 }
3134
paul718e3742002-12-13 20:15:29 +00003135 vty_out (vty, "Configuration saved to %s%s", config_file,
3136 VTY_NEWLINE);
paul05865c92005-10-26 05:49:54 +00003137 ret = CMD_SUCCESS;
3138
3139finished:
3140 unlink (config_file_tmp);
3141 XFREE (MTYPE_TMP, config_file_tmp);
3142 XFREE (MTYPE_TMP, config_file_sav);
3143 return ret;
paul718e3742002-12-13 20:15:29 +00003144}
3145
3146ALIAS (config_write_file,
3147 config_write_cmd,
3148 "write",
3149 "Write running configuration to memory, network, or terminal\n")
3150
3151ALIAS (config_write_file,
3152 config_write_memory_cmd,
3153 "write memory",
3154 "Write running configuration to memory, network, or terminal\n"
3155 "Write configuration to the file (same as write file)\n")
3156
3157ALIAS (config_write_file,
3158 copy_runningconfig_startupconfig_cmd,
3159 "copy running-config startup-config",
3160 "Copy configuration\n"
3161 "Copy running config to... \n"
3162 "Copy running config to startup config (same as write file)\n")
3163
3164/* Write current configuration into the terminal. */
3165DEFUN (config_write_terminal,
3166 config_write_terminal_cmd,
3167 "write terminal",
3168 "Write running configuration to memory, network, or terminal\n"
3169 "Write to terminal\n")
3170{
hasso8c328f12004-10-05 21:01:23 +00003171 unsigned int i;
paul718e3742002-12-13 20:15:29 +00003172 struct cmd_node *node;
3173
3174 if (vty->type == VTY_SHELL_SERV)
3175 {
paul55468c82005-03-14 20:19:01 +00003176 for (i = 0; i < vector_active (cmdvec); i++)
paul718e3742002-12-13 20:15:29 +00003177 if ((node = vector_slot (cmdvec, i)) && node->func && node->vtysh)
3178 {
3179 if ((*node->func) (vty))
3180 vty_out (vty, "!%s", VTY_NEWLINE);
3181 }
3182 }
3183 else
3184 {
3185 vty_out (vty, "%sCurrent configuration:%s", VTY_NEWLINE,
3186 VTY_NEWLINE);
3187 vty_out (vty, "!%s", VTY_NEWLINE);
3188
paul55468c82005-03-14 20:19:01 +00003189 for (i = 0; i < vector_active (cmdvec); i++)
paul718e3742002-12-13 20:15:29 +00003190 if ((node = vector_slot (cmdvec, i)) && node->func)
3191 {
3192 if ((*node->func) (vty))
3193 vty_out (vty, "!%s", VTY_NEWLINE);
3194 }
3195 vty_out (vty, "end%s",VTY_NEWLINE);
3196 }
3197 return CMD_SUCCESS;
3198}
3199
3200/* Write current configuration into the terminal. */
3201ALIAS (config_write_terminal,
3202 show_running_config_cmd,
3203 "show running-config",
3204 SHOW_STR
3205 "running configuration\n")
3206
3207/* Write startup configuration into the terminal. */
3208DEFUN (show_startup_config,
3209 show_startup_config_cmd,
3210 "show startup-config",
3211 SHOW_STR
3212 "Contentes of startup configuration\n")
3213{
3214 char buf[BUFSIZ];
3215 FILE *confp;
3216
3217 confp = fopen (host.config, "r");
3218 if (confp == NULL)
3219 {
3220 vty_out (vty, "Can't open configuration file [%s]%s",
3221 host.config, VTY_NEWLINE);
3222 return CMD_WARNING;
3223 }
3224
3225 while (fgets (buf, BUFSIZ, confp))
3226 {
3227 char *cp = buf;
3228
3229 while (*cp != '\r' && *cp != '\n' && *cp != '\0')
3230 cp++;
3231 *cp = '\0';
3232
3233 vty_out (vty, "%s%s", buf, VTY_NEWLINE);
3234 }
3235
3236 fclose (confp);
3237
3238 return CMD_SUCCESS;
3239}
3240
3241/* Hostname configuration */
3242DEFUN (config_hostname,
3243 hostname_cmd,
3244 "hostname WORD",
3245 "Set system's network name\n"
3246 "This system's network name\n")
3247{
3248 if (!isalpha((int) *argv[0]))
3249 {
3250 vty_out (vty, "Please specify string starting with alphabet%s", VTY_NEWLINE);
3251 return CMD_WARNING;
3252 }
3253
3254 if (host.name)
paul05865c92005-10-26 05:49:54 +00003255 XFREE (MTYPE_HOST, host.name);
paul718e3742002-12-13 20:15:29 +00003256
paul05865c92005-10-26 05:49:54 +00003257 host.name = XSTRDUP (MTYPE_HOST, argv[0]);
paul718e3742002-12-13 20:15:29 +00003258 return CMD_SUCCESS;
3259}
3260
3261DEFUN (config_no_hostname,
3262 no_hostname_cmd,
3263 "no hostname [HOSTNAME]",
3264 NO_STR
3265 "Reset system's network name\n"
3266 "Host name of this router\n")
3267{
3268 if (host.name)
paul05865c92005-10-26 05:49:54 +00003269 XFREE (MTYPE_HOST, host.name);
paul718e3742002-12-13 20:15:29 +00003270 host.name = NULL;
3271 return CMD_SUCCESS;
3272}
3273
3274/* VTY interface password set. */
3275DEFUN (config_password, password_cmd,
3276 "password (8|) WORD",
3277 "Assign the terminal connection password\n"
3278 "Specifies a HIDDEN password will follow\n"
3279 "dummy string \n"
3280 "The HIDDEN line password string\n")
3281{
3282 /* Argument check. */
3283 if (argc == 0)
3284 {
3285 vty_out (vty, "Please specify password.%s", VTY_NEWLINE);
3286 return CMD_WARNING;
3287 }
3288
3289 if (argc == 2)
3290 {
3291 if (*argv[0] == '8')
3292 {
3293 if (host.password)
paul05865c92005-10-26 05:49:54 +00003294 XFREE (MTYPE_HOST, host.password);
paul718e3742002-12-13 20:15:29 +00003295 host.password = NULL;
3296 if (host.password_encrypt)
paul05865c92005-10-26 05:49:54 +00003297 XFREE (MTYPE_HOST, host.password_encrypt);
3298 host.password_encrypt = XSTRDUP (MTYPE_HOST, argv[1]);
paul718e3742002-12-13 20:15:29 +00003299 return CMD_SUCCESS;
3300 }
3301 else
3302 {
3303 vty_out (vty, "Unknown encryption type.%s", VTY_NEWLINE);
3304 return CMD_WARNING;
3305 }
3306 }
3307
3308 if (!isalnum ((int) *argv[0]))
3309 {
3310 vty_out (vty,
3311 "Please specify string starting with alphanumeric%s", VTY_NEWLINE);
3312 return CMD_WARNING;
3313 }
3314
3315 if (host.password)
paul05865c92005-10-26 05:49:54 +00003316 XFREE (MTYPE_HOST, host.password);
paul718e3742002-12-13 20:15:29 +00003317 host.password = NULL;
3318
3319 if (host.encrypt)
3320 {
3321 if (host.password_encrypt)
paul05865c92005-10-26 05:49:54 +00003322 XFREE (MTYPE_HOST, host.password_encrypt);
3323 host.password_encrypt = XSTRDUP (MTYPE_HOST, zencrypt (argv[0]));
paul718e3742002-12-13 20:15:29 +00003324 }
3325 else
paul05865c92005-10-26 05:49:54 +00003326 host.password = XSTRDUP (MTYPE_HOST, argv[0]);
paul718e3742002-12-13 20:15:29 +00003327
3328 return CMD_SUCCESS;
3329}
3330
3331ALIAS (config_password, password_text_cmd,
3332 "password LINE",
3333 "Assign the terminal connection password\n"
3334 "The UNENCRYPTED (cleartext) line password\n")
3335
3336/* VTY enable password set. */
3337DEFUN (config_enable_password, enable_password_cmd,
3338 "enable password (8|) WORD",
3339 "Modify enable password parameters\n"
3340 "Assign the privileged level password\n"
3341 "Specifies a HIDDEN password will follow\n"
3342 "dummy string \n"
3343 "The HIDDEN 'enable' password string\n")
3344{
3345 /* Argument check. */
3346 if (argc == 0)
3347 {
3348 vty_out (vty, "Please specify password.%s", VTY_NEWLINE);
3349 return CMD_WARNING;
3350 }
3351
3352 /* Crypt type is specified. */
3353 if (argc == 2)
3354 {
3355 if (*argv[0] == '8')
3356 {
3357 if (host.enable)
paul05865c92005-10-26 05:49:54 +00003358 XFREE (MTYPE_HOST, host.enable);
paul718e3742002-12-13 20:15:29 +00003359 host.enable = NULL;
3360
3361 if (host.enable_encrypt)
paul05865c92005-10-26 05:49:54 +00003362 XFREE (MTYPE_HOST, host.enable_encrypt);
3363 host.enable_encrypt = XSTRDUP (MTYPE_HOST, argv[1]);
paul718e3742002-12-13 20:15:29 +00003364
3365 return CMD_SUCCESS;
3366 }
3367 else
3368 {
3369 vty_out (vty, "Unknown encryption type.%s", VTY_NEWLINE);
3370 return CMD_WARNING;
3371 }
3372 }
3373
3374 if (!isalnum ((int) *argv[0]))
3375 {
3376 vty_out (vty,
3377 "Please specify string starting with alphanumeric%s", VTY_NEWLINE);
3378 return CMD_WARNING;
3379 }
3380
3381 if (host.enable)
paul05865c92005-10-26 05:49:54 +00003382 XFREE (MTYPE_HOST, host.enable);
paul718e3742002-12-13 20:15:29 +00003383 host.enable = NULL;
3384
3385 /* Plain password input. */
3386 if (host.encrypt)
3387 {
3388 if (host.enable_encrypt)
paul05865c92005-10-26 05:49:54 +00003389 XFREE (MTYPE_HOST, host.enable_encrypt);
3390 host.enable_encrypt = XSTRDUP (MTYPE_HOST, zencrypt (argv[0]));
paul718e3742002-12-13 20:15:29 +00003391 }
3392 else
paul05865c92005-10-26 05:49:54 +00003393 host.enable = XSTRDUP (MTYPE_HOST, argv[0]);
paul718e3742002-12-13 20:15:29 +00003394
3395 return CMD_SUCCESS;
3396}
3397
3398ALIAS (config_enable_password,
3399 enable_password_text_cmd,
3400 "enable password LINE",
3401 "Modify enable password parameters\n"
3402 "Assign the privileged level password\n"
3403 "The UNENCRYPTED (cleartext) 'enable' password\n")
3404
3405/* VTY enable password delete. */
3406DEFUN (no_config_enable_password, no_enable_password_cmd,
3407 "no enable password",
3408 NO_STR
3409 "Modify enable password parameters\n"
3410 "Assign the privileged level password\n")
3411{
3412 if (host.enable)
paul05865c92005-10-26 05:49:54 +00003413 XFREE (MTYPE_HOST, host.enable);
paul718e3742002-12-13 20:15:29 +00003414 host.enable = NULL;
3415
3416 if (host.enable_encrypt)
paul05865c92005-10-26 05:49:54 +00003417 XFREE (MTYPE_HOST, host.enable_encrypt);
paul718e3742002-12-13 20:15:29 +00003418 host.enable_encrypt = NULL;
3419
3420 return CMD_SUCCESS;
3421}
3422
3423DEFUN (service_password_encrypt,
3424 service_password_encrypt_cmd,
3425 "service password-encryption",
3426 "Set up miscellaneous service\n"
3427 "Enable encrypted passwords\n")
3428{
3429 if (host.encrypt)
3430 return CMD_SUCCESS;
3431
3432 host.encrypt = 1;
3433
3434 if (host.password)
3435 {
3436 if (host.password_encrypt)
paul05865c92005-10-26 05:49:54 +00003437 XFREE (MTYPE_HOST, host.password_encrypt);
3438 host.password_encrypt = XSTRDUP (MTYPE_HOST, zencrypt (host.password));
paul718e3742002-12-13 20:15:29 +00003439 }
3440 if (host.enable)
3441 {
3442 if (host.enable_encrypt)
paul05865c92005-10-26 05:49:54 +00003443 XFREE (MTYPE_HOST, host.enable_encrypt);
3444 host.enable_encrypt = XSTRDUP (MTYPE_HOST, zencrypt (host.enable));
paul718e3742002-12-13 20:15:29 +00003445 }
3446
3447 return CMD_SUCCESS;
3448}
3449
3450DEFUN (no_service_password_encrypt,
3451 no_service_password_encrypt_cmd,
3452 "no service password-encryption",
3453 NO_STR
3454 "Set up miscellaneous service\n"
3455 "Enable encrypted passwords\n")
3456{
3457 if (! host.encrypt)
3458 return CMD_SUCCESS;
3459
3460 host.encrypt = 0;
3461
3462 if (host.password_encrypt)
paul05865c92005-10-26 05:49:54 +00003463 XFREE (MTYPE_HOST, host.password_encrypt);
paul718e3742002-12-13 20:15:29 +00003464 host.password_encrypt = NULL;
3465
3466 if (host.enable_encrypt)
paul05865c92005-10-26 05:49:54 +00003467 XFREE (MTYPE_HOST, host.enable_encrypt);
paul718e3742002-12-13 20:15:29 +00003468 host.enable_encrypt = NULL;
3469
3470 return CMD_SUCCESS;
3471}
3472
3473DEFUN (config_terminal_length, config_terminal_length_cmd,
3474 "terminal length <0-512>",
3475 "Set terminal line parameters\n"
3476 "Set number of lines on a screen\n"
3477 "Number of lines on screen (0 for no pausing)\n")
3478{
3479 int lines;
3480 char *endptr = NULL;
3481
3482 lines = strtol (argv[0], &endptr, 10);
3483 if (lines < 0 || lines > 512 || *endptr != '\0')
3484 {
3485 vty_out (vty, "length is malformed%s", VTY_NEWLINE);
3486 return CMD_WARNING;
3487 }
3488 vty->lines = lines;
3489
3490 return CMD_SUCCESS;
3491}
3492
3493DEFUN (config_terminal_no_length, config_terminal_no_length_cmd,
3494 "terminal no length",
3495 "Set terminal line parameters\n"
3496 NO_STR
3497 "Set number of lines on a screen\n")
3498{
3499 vty->lines = -1;
3500 return CMD_SUCCESS;
3501}
3502
3503DEFUN (service_terminal_length, service_terminal_length_cmd,
3504 "service terminal-length <0-512>",
3505 "Set up miscellaneous service\n"
3506 "System wide terminal length configuration\n"
3507 "Number of lines of VTY (0 means no line control)\n")
3508{
3509 int lines;
3510 char *endptr = NULL;
3511
3512 lines = strtol (argv[0], &endptr, 10);
3513 if (lines < 0 || lines > 512 || *endptr != '\0')
3514 {
3515 vty_out (vty, "length is malformed%s", VTY_NEWLINE);
3516 return CMD_WARNING;
3517 }
3518 host.lines = lines;
3519
3520 return CMD_SUCCESS;
3521}
3522
3523DEFUN (no_service_terminal_length, no_service_terminal_length_cmd,
3524 "no service terminal-length [<0-512>]",
3525 NO_STR
3526 "Set up miscellaneous service\n"
3527 "System wide terminal length configuration\n"
3528 "Number of lines of VTY (0 means no line control)\n")
3529{
3530 host.lines = -1;
3531 return CMD_SUCCESS;
3532}
3533
ajs2885f722004-12-17 23:16:33 +00003534DEFUN_HIDDEN (do_echo,
3535 echo_cmd,
3536 "echo .MESSAGE",
3537 "Echo a message back to the vty\n"
3538 "The message to echo\n")
3539{
3540 char *message;
3541
ajsf6834d42005-01-28 20:28:35 +00003542 vty_out (vty, "%s%s", ((message = argv_concat(argv, argc, 0)) ? message : ""),
3543 VTY_NEWLINE);
3544 if (message)
3545 XFREE(MTYPE_TMP, message);
ajs2885f722004-12-17 23:16:33 +00003546 return CMD_SUCCESS;
3547}
3548
ajs274a4a42004-12-07 15:39:31 +00003549DEFUN (config_logmsg,
3550 config_logmsg_cmd,
3551 "logmsg "LOG_LEVELS" .MESSAGE",
3552 "Send a message to enabled logging destinations\n"
3553 LOG_LEVEL_DESC
3554 "The message to send\n")
3555{
3556 int level;
3557 char *message;
3558
3559 if ((level = level_match(argv[0])) == ZLOG_DISABLED)
3560 return CMD_ERR_NO_MATCH;
3561
Christian Hammersfc951862011-03-23 13:07:55 +03003562 zlog(NULL, level, "%s", ((message = argv_concat(argv, argc, 1)) ? message : ""));
ajsf6834d42005-01-28 20:28:35 +00003563 if (message)
3564 XFREE(MTYPE_TMP, message);
ajs274a4a42004-12-07 15:39:31 +00003565 return CMD_SUCCESS;
3566}
3567
3568DEFUN (show_logging,
3569 show_logging_cmd,
3570 "show logging",
3571 SHOW_STR
3572 "Show current logging configuration\n")
3573{
3574 struct zlog *zl = zlog_default;
3575
3576 vty_out (vty, "Syslog logging: ");
3577 if (zl->maxlvl[ZLOG_DEST_SYSLOG] == ZLOG_DISABLED)
3578 vty_out (vty, "disabled");
3579 else
3580 vty_out (vty, "level %s, facility %s, ident %s",
3581 zlog_priority[zl->maxlvl[ZLOG_DEST_SYSLOG]],
3582 facility_name(zl->facility), zl->ident);
3583 vty_out (vty, "%s", VTY_NEWLINE);
3584
3585 vty_out (vty, "Stdout logging: ");
3586 if (zl->maxlvl[ZLOG_DEST_STDOUT] == ZLOG_DISABLED)
3587 vty_out (vty, "disabled");
3588 else
3589 vty_out (vty, "level %s",
3590 zlog_priority[zl->maxlvl[ZLOG_DEST_STDOUT]]);
3591 vty_out (vty, "%s", VTY_NEWLINE);
3592
3593 vty_out (vty, "Monitor logging: ");
3594 if (zl->maxlvl[ZLOG_DEST_MONITOR] == ZLOG_DISABLED)
3595 vty_out (vty, "disabled");
3596 else
3597 vty_out (vty, "level %s",
3598 zlog_priority[zl->maxlvl[ZLOG_DEST_MONITOR]]);
3599 vty_out (vty, "%s", VTY_NEWLINE);
3600
3601 vty_out (vty, "File logging: ");
3602 if ((zl->maxlvl[ZLOG_DEST_FILE] == ZLOG_DISABLED) ||
3603 !zl->fp)
3604 vty_out (vty, "disabled");
3605 else
3606 vty_out (vty, "level %s, filename %s",
3607 zlog_priority[zl->maxlvl[ZLOG_DEST_FILE]],
3608 zl->filename);
3609 vty_out (vty, "%s", VTY_NEWLINE);
3610
3611 vty_out (vty, "Protocol name: %s%s",
3612 zlog_proto_names[zl->protocol], VTY_NEWLINE);
3613 vty_out (vty, "Record priority: %s%s",
3614 (zl->record_priority ? "enabled" : "disabled"), VTY_NEWLINE);
Andrew J. Schorr1ed72e02007-04-28 22:14:10 +00003615 vty_out (vty, "Timestamp precision: %d%s",
3616 zl->timestamp_precision, VTY_NEWLINE);
ajs274a4a42004-12-07 15:39:31 +00003617
3618 return CMD_SUCCESS;
3619}
3620
paul718e3742002-12-13 20:15:29 +00003621DEFUN (config_log_stdout,
3622 config_log_stdout_cmd,
3623 "log stdout",
3624 "Logging control\n"
ajs274a4a42004-12-07 15:39:31 +00003625 "Set stdout logging level\n")
paul718e3742002-12-13 20:15:29 +00003626{
ajs274a4a42004-12-07 15:39:31 +00003627 zlog_set_level (NULL, ZLOG_DEST_STDOUT, zlog_default->default_lvl);
3628 return CMD_SUCCESS;
3629}
3630
3631DEFUN (config_log_stdout_level,
3632 config_log_stdout_level_cmd,
3633 "log stdout "LOG_LEVELS,
3634 "Logging control\n"
3635 "Set stdout logging level\n"
3636 LOG_LEVEL_DESC)
3637{
3638 int level;
3639
3640 if ((level = level_match(argv[0])) == ZLOG_DISABLED)
3641 return CMD_ERR_NO_MATCH;
3642 zlog_set_level (NULL, ZLOG_DEST_STDOUT, level);
paul718e3742002-12-13 20:15:29 +00003643 return CMD_SUCCESS;
3644}
3645
3646DEFUN (no_config_log_stdout,
3647 no_config_log_stdout_cmd,
ajs274a4a42004-12-07 15:39:31 +00003648 "no log stdout [LEVEL]",
paul718e3742002-12-13 20:15:29 +00003649 NO_STR
3650 "Logging control\n"
ajs274a4a42004-12-07 15:39:31 +00003651 "Cancel logging to stdout\n"
3652 "Logging level\n")
paul718e3742002-12-13 20:15:29 +00003653{
ajs274a4a42004-12-07 15:39:31 +00003654 zlog_set_level (NULL, ZLOG_DEST_STDOUT, ZLOG_DISABLED);
paul718e3742002-12-13 20:15:29 +00003655 return CMD_SUCCESS;
3656}
3657
ajs274a4a42004-12-07 15:39:31 +00003658DEFUN (config_log_monitor,
3659 config_log_monitor_cmd,
3660 "log monitor",
paul718e3742002-12-13 20:15:29 +00003661 "Logging control\n"
ajs274a4a42004-12-07 15:39:31 +00003662 "Set terminal line (monitor) logging level\n")
3663{
3664 zlog_set_level (NULL, ZLOG_DEST_MONITOR, zlog_default->default_lvl);
3665 return CMD_SUCCESS;
3666}
3667
3668DEFUN (config_log_monitor_level,
3669 config_log_monitor_level_cmd,
3670 "log monitor "LOG_LEVELS,
3671 "Logging control\n"
3672 "Set terminal line (monitor) logging level\n"
3673 LOG_LEVEL_DESC)
3674{
3675 int level;
3676
3677 if ((level = level_match(argv[0])) == ZLOG_DISABLED)
3678 return CMD_ERR_NO_MATCH;
3679 zlog_set_level (NULL, ZLOG_DEST_MONITOR, level);
3680 return CMD_SUCCESS;
3681}
3682
3683DEFUN (no_config_log_monitor,
3684 no_config_log_monitor_cmd,
3685 "no log monitor [LEVEL]",
3686 NO_STR
3687 "Logging control\n"
3688 "Disable terminal line (monitor) logging\n"
3689 "Logging level\n")
3690{
3691 zlog_set_level (NULL, ZLOG_DEST_MONITOR, ZLOG_DISABLED);
3692 return CMD_SUCCESS;
3693}
3694
3695static int
3696set_log_file(struct vty *vty, const char *fname, int loglevel)
paul718e3742002-12-13 20:15:29 +00003697{
3698 int ret;
paul9035efa2004-10-10 11:56:56 +00003699 char *p = NULL;
3700 const char *fullpath;
3701
paul718e3742002-12-13 20:15:29 +00003702 /* Path detection. */
ajs274a4a42004-12-07 15:39:31 +00003703 if (! IS_DIRECTORY_SEP (*fname))
paul718e3742002-12-13 20:15:29 +00003704 {
paul9035efa2004-10-10 11:56:56 +00003705 char cwd[MAXPATHLEN+1];
3706 cwd[MAXPATHLEN] = '\0';
3707
3708 if (getcwd (cwd, MAXPATHLEN) == NULL)
3709 {
3710 zlog_err ("config_log_file: Unable to alloc mem!");
3711 return CMD_WARNING;
3712 }
3713
ajs274a4a42004-12-07 15:39:31 +00003714 if ( (p = XMALLOC (MTYPE_TMP, strlen (cwd) + strlen (fname) + 2))
paul9035efa2004-10-10 11:56:56 +00003715 == NULL)
3716 {
3717 zlog_err ("config_log_file: Unable to alloc mem!");
3718 return CMD_WARNING;
3719 }
ajs274a4a42004-12-07 15:39:31 +00003720 sprintf (p, "%s/%s", cwd, fname);
paul9035efa2004-10-10 11:56:56 +00003721 fullpath = p;
paul718e3742002-12-13 20:15:29 +00003722 }
3723 else
ajs274a4a42004-12-07 15:39:31 +00003724 fullpath = fname;
paul718e3742002-12-13 20:15:29 +00003725
ajs274a4a42004-12-07 15:39:31 +00003726 ret = zlog_set_file (NULL, fullpath, loglevel);
paul718e3742002-12-13 20:15:29 +00003727
paul9035efa2004-10-10 11:56:56 +00003728 if (p)
3729 XFREE (MTYPE_TMP, p);
3730
paul718e3742002-12-13 20:15:29 +00003731 if (!ret)
3732 {
ajs274a4a42004-12-07 15:39:31 +00003733 vty_out (vty, "can't open logfile %s\n", fname);
paul718e3742002-12-13 20:15:29 +00003734 return CMD_WARNING;
3735 }
3736
3737 if (host.logfile)
paul05865c92005-10-26 05:49:54 +00003738 XFREE (MTYPE_HOST, host.logfile);
paul718e3742002-12-13 20:15:29 +00003739
paul05865c92005-10-26 05:49:54 +00003740 host.logfile = XSTRDUP (MTYPE_HOST, fname);
paul718e3742002-12-13 20:15:29 +00003741
3742 return CMD_SUCCESS;
3743}
3744
ajs274a4a42004-12-07 15:39:31 +00003745DEFUN (config_log_file,
3746 config_log_file_cmd,
3747 "log file FILENAME",
3748 "Logging control\n"
3749 "Logging to file\n"
3750 "Logging filename\n")
3751{
3752 return set_log_file(vty, argv[0], zlog_default->default_lvl);
3753}
3754
3755DEFUN (config_log_file_level,
3756 config_log_file_level_cmd,
3757 "log file FILENAME "LOG_LEVELS,
3758 "Logging control\n"
3759 "Logging to file\n"
3760 "Logging filename\n"
3761 LOG_LEVEL_DESC)
3762{
3763 int level;
3764
3765 if ((level = level_match(argv[1])) == ZLOG_DISABLED)
3766 return CMD_ERR_NO_MATCH;
3767 return set_log_file(vty, argv[0], level);
3768}
3769
paul718e3742002-12-13 20:15:29 +00003770DEFUN (no_config_log_file,
3771 no_config_log_file_cmd,
3772 "no log file [FILENAME]",
3773 NO_STR
3774 "Logging control\n"
3775 "Cancel logging to file\n"
3776 "Logging file name\n")
3777{
3778 zlog_reset_file (NULL);
3779
3780 if (host.logfile)
paul05865c92005-10-26 05:49:54 +00003781 XFREE (MTYPE_HOST, host.logfile);
paul718e3742002-12-13 20:15:29 +00003782
3783 host.logfile = NULL;
3784
3785 return CMD_SUCCESS;
3786}
3787
ajs274a4a42004-12-07 15:39:31 +00003788ALIAS (no_config_log_file,
3789 no_config_log_file_level_cmd,
3790 "no log file FILENAME LEVEL",
3791 NO_STR
3792 "Logging control\n"
3793 "Cancel logging to file\n"
3794 "Logging file name\n"
3795 "Logging level\n")
3796
paul718e3742002-12-13 20:15:29 +00003797DEFUN (config_log_syslog,
3798 config_log_syslog_cmd,
3799 "log syslog",
3800 "Logging control\n"
ajs274a4a42004-12-07 15:39:31 +00003801 "Set syslog logging level\n")
paul718e3742002-12-13 20:15:29 +00003802{
ajs274a4a42004-12-07 15:39:31 +00003803 zlog_set_level (NULL, ZLOG_DEST_SYSLOG, zlog_default->default_lvl);
paul12ab19f2003-07-26 06:14:55 +00003804 return CMD_SUCCESS;
3805}
3806
ajs274a4a42004-12-07 15:39:31 +00003807DEFUN (config_log_syslog_level,
3808 config_log_syslog_level_cmd,
3809 "log syslog "LOG_LEVELS,
paul12ab19f2003-07-26 06:14:55 +00003810 "Logging control\n"
ajs274a4a42004-12-07 15:39:31 +00003811 "Set syslog logging level\n"
3812 LOG_LEVEL_DESC)
paul12ab19f2003-07-26 06:14:55 +00003813{
ajs274a4a42004-12-07 15:39:31 +00003814 int level;
paul12ab19f2003-07-26 06:14:55 +00003815
ajs274a4a42004-12-07 15:39:31 +00003816 if ((level = level_match(argv[0])) == ZLOG_DISABLED)
3817 return CMD_ERR_NO_MATCH;
3818 zlog_set_level (NULL, ZLOG_DEST_SYSLOG, level);
3819 return CMD_SUCCESS;
3820}
paul12ab19f2003-07-26 06:14:55 +00003821
ajs274a4a42004-12-07 15:39:31 +00003822DEFUN_DEPRECATED (config_log_syslog_facility,
3823 config_log_syslog_facility_cmd,
3824 "log syslog facility "LOG_FACILITIES,
3825 "Logging control\n"
3826 "Logging goes to syslog\n"
3827 "(Deprecated) Facility parameter for syslog messages\n"
3828 LOG_FACILITY_DESC)
3829{
3830 int facility;
paul12ab19f2003-07-26 06:14:55 +00003831
ajs274a4a42004-12-07 15:39:31 +00003832 if ((facility = facility_match(argv[0])) < 0)
3833 return CMD_ERR_NO_MATCH;
3834
3835 zlog_set_level (NULL, ZLOG_DEST_SYSLOG, zlog_default->default_lvl);
paul12ab19f2003-07-26 06:14:55 +00003836 zlog_default->facility = facility;
paul718e3742002-12-13 20:15:29 +00003837 return CMD_SUCCESS;
3838}
3839
3840DEFUN (no_config_log_syslog,
3841 no_config_log_syslog_cmd,
ajs274a4a42004-12-07 15:39:31 +00003842 "no log syslog [LEVEL]",
paul718e3742002-12-13 20:15:29 +00003843 NO_STR
3844 "Logging control\n"
ajs274a4a42004-12-07 15:39:31 +00003845 "Cancel logging to syslog\n"
3846 "Logging level\n")
paul718e3742002-12-13 20:15:29 +00003847{
ajs274a4a42004-12-07 15:39:31 +00003848 zlog_set_level (NULL, ZLOG_DEST_SYSLOG, ZLOG_DISABLED);
paul718e3742002-12-13 20:15:29 +00003849 return CMD_SUCCESS;
3850}
3851
paul12ab19f2003-07-26 06:14:55 +00003852ALIAS (no_config_log_syslog,
3853 no_config_log_syslog_facility_cmd,
ajs274a4a42004-12-07 15:39:31 +00003854 "no log syslog facility "LOG_FACILITIES,
paul12ab19f2003-07-26 06:14:55 +00003855 NO_STR
3856 "Logging control\n"
3857 "Logging goes to syslog\n"
3858 "Facility parameter for syslog messages\n"
ajs274a4a42004-12-07 15:39:31 +00003859 LOG_FACILITY_DESC)
paul12ab19f2003-07-26 06:14:55 +00003860
ajs274a4a42004-12-07 15:39:31 +00003861DEFUN (config_log_facility,
3862 config_log_facility_cmd,
3863 "log facility "LOG_FACILITIES,
paul718e3742002-12-13 20:15:29 +00003864 "Logging control\n"
ajs274a4a42004-12-07 15:39:31 +00003865 "Facility parameter for syslog messages\n"
3866 LOG_FACILITY_DESC)
paul718e3742002-12-13 20:15:29 +00003867{
ajs274a4a42004-12-07 15:39:31 +00003868 int facility;
3869
3870 if ((facility = facility_match(argv[0])) < 0)
3871 return CMD_ERR_NO_MATCH;
3872 zlog_default->facility = facility;
3873 return CMD_SUCCESS;
paul718e3742002-12-13 20:15:29 +00003874}
3875
ajs274a4a42004-12-07 15:39:31 +00003876DEFUN (no_config_log_facility,
3877 no_config_log_facility_cmd,
3878 "no log facility [FACILITY]",
paul718e3742002-12-13 20:15:29 +00003879 NO_STR
3880 "Logging control\n"
ajs274a4a42004-12-07 15:39:31 +00003881 "Reset syslog facility to default (daemon)\n"
3882 "Syslog facility\n")
paul718e3742002-12-13 20:15:29 +00003883{
ajs274a4a42004-12-07 15:39:31 +00003884 zlog_default->facility = LOG_DAEMON;
3885 return CMD_SUCCESS;
3886}
3887
3888DEFUN_DEPRECATED (config_log_trap,
3889 config_log_trap_cmd,
3890 "log trap "LOG_LEVELS,
3891 "Logging control\n"
3892 "(Deprecated) Set logging level and default for all destinations\n"
3893 LOG_LEVEL_DESC)
3894{
3895 int new_level ;
3896 int i;
3897
3898 if ((new_level = level_match(argv[0])) == ZLOG_DISABLED)
3899 return CMD_ERR_NO_MATCH;
3900
3901 zlog_default->default_lvl = new_level;
3902 for (i = 0; i < ZLOG_NUM_DESTS; i++)
3903 if (zlog_default->maxlvl[i] != ZLOG_DISABLED)
3904 zlog_default->maxlvl[i] = new_level;
3905 return CMD_SUCCESS;
3906}
3907
3908DEFUN_DEPRECATED (no_config_log_trap,
3909 no_config_log_trap_cmd,
3910 "no log trap [LEVEL]",
3911 NO_STR
3912 "Logging control\n"
3913 "Permit all logging information\n"
3914 "Logging level\n")
3915{
3916 zlog_default->default_lvl = LOG_DEBUG;
paul718e3742002-12-13 20:15:29 +00003917 return CMD_SUCCESS;
3918}
3919
3920DEFUN (config_log_record_priority,
3921 config_log_record_priority_cmd,
3922 "log record-priority",
3923 "Logging control\n"
3924 "Log the priority of the message within the message\n")
3925{
3926 zlog_default->record_priority = 1 ;
3927 return CMD_SUCCESS;
3928}
3929
3930DEFUN (no_config_log_record_priority,
3931 no_config_log_record_priority_cmd,
3932 "no log record-priority",
3933 NO_STR
3934 "Logging control\n"
3935 "Do not log the priority of the message within the message\n")
3936{
3937 zlog_default->record_priority = 0 ;
3938 return CMD_SUCCESS;
3939}
3940
Andrew J. Schorr1ed72e02007-04-28 22:14:10 +00003941DEFUN (config_log_timestamp_precision,
3942 config_log_timestamp_precision_cmd,
3943 "log timestamp precision <0-6>",
3944 "Logging control\n"
3945 "Timestamp configuration\n"
3946 "Set the timestamp precision\n"
3947 "Number of subsecond digits\n")
3948{
3949 if (argc != 1)
3950 {
3951 vty_out (vty, "Insufficient arguments%s", VTY_NEWLINE);
3952 return CMD_WARNING;
3953 }
3954
3955 VTY_GET_INTEGER_RANGE("Timestamp Precision",
3956 zlog_default->timestamp_precision, argv[0], 0, 6);
3957 return CMD_SUCCESS;
3958}
3959
3960DEFUN (no_config_log_timestamp_precision,
3961 no_config_log_timestamp_precision_cmd,
3962 "no log timestamp precision",
3963 NO_STR
3964 "Logging control\n"
3965 "Timestamp configuration\n"
3966 "Reset the timestamp precision to the default value of 0\n")
3967{
3968 zlog_default->timestamp_precision = 0 ;
3969 return CMD_SUCCESS;
3970}
3971
paul3b0c5d92005-03-08 10:43:43 +00003972DEFUN (banner_motd_file,
3973 banner_motd_file_cmd,
3974 "banner motd file [FILE]",
3975 "Set banner\n"
3976 "Banner for motd\n"
3977 "Banner from a file\n"
3978 "Filename\n")
3979{
paulb45da6f2005-03-08 15:16:57 +00003980 if (host.motdfile)
paul05865c92005-10-26 05:49:54 +00003981 XFREE (MTYPE_HOST, host.motdfile);
3982 host.motdfile = XSTRDUP (MTYPE_HOST, argv[0]);
paulb45da6f2005-03-08 15:16:57 +00003983
paul3b0c5d92005-03-08 10:43:43 +00003984 return CMD_SUCCESS;
3985}
paul718e3742002-12-13 20:15:29 +00003986
3987DEFUN (banner_motd_default,
3988 banner_motd_default_cmd,
3989 "banner motd default",
3990 "Set banner string\n"
3991 "Strings for motd\n"
3992 "Default string\n")
3993{
3994 host.motd = default_motd;
3995 return CMD_SUCCESS;
3996}
3997
3998DEFUN (no_banner_motd,
3999 no_banner_motd_cmd,
4000 "no banner motd",
4001 NO_STR
4002 "Set banner string\n"
4003 "Strings for motd\n")
4004{
4005 host.motd = NULL;
paul22085182005-03-08 16:00:12 +00004006 if (host.motdfile)
paul05865c92005-10-26 05:49:54 +00004007 XFREE (MTYPE_HOST, host.motdfile);
paul3b0c5d92005-03-08 10:43:43 +00004008 host.motdfile = NULL;
paul718e3742002-12-13 20:15:29 +00004009 return CMD_SUCCESS;
4010}
4011
4012/* Set config filename. Called from vty.c */
4013void
4014host_config_set (char *filename)
4015{
Chris Caputo228da422009-07-18 05:44:03 +00004016 if (host.config)
4017 XFREE (MTYPE_HOST, host.config);
paul05865c92005-10-26 05:49:54 +00004018 host.config = XSTRDUP (MTYPE_HOST, filename);
paul718e3742002-12-13 20:15:29 +00004019}
4020
4021void
4022install_default (enum node_type node)
4023{
4024 install_element (node, &config_exit_cmd);
4025 install_element (node, &config_quit_cmd);
4026 install_element (node, &config_end_cmd);
4027 install_element (node, &config_help_cmd);
4028 install_element (node, &config_list_cmd);
4029
4030 install_element (node, &config_write_terminal_cmd);
4031 install_element (node, &config_write_file_cmd);
4032 install_element (node, &config_write_memory_cmd);
4033 install_element (node, &config_write_cmd);
4034 install_element (node, &show_running_config_cmd);
4035}
4036
4037/* Initialize command interface. Install basic nodes and commands. */
4038void
4039cmd_init (int terminal)
4040{
Christian Frankecd40b322013-09-30 12:27:51 +00004041 command_cr = XSTRDUP(MTYPE_CMD_TOKENS, "<cr>");
4042 token_cr.type = TOKEN_TERMINAL;
David Lamparter10bac802015-05-05 11:10:20 +02004043 token_cr.terminal = TERMINAL_LITERAL;
Christian Frankecd40b322013-09-30 12:27:51 +00004044 token_cr.cmd = command_cr;
4045 token_cr.desc = XSTRDUP(MTYPE_CMD_TOKENS, "");
Chris Caputo228da422009-07-18 05:44:03 +00004046
paul718e3742002-12-13 20:15:29 +00004047 /* Allocate initial top vector of commands. */
4048 cmdvec = vector_init (VECTOR_MIN_SIZE);
4049
4050 /* Default host value settings. */
4051 host.name = NULL;
4052 host.password = NULL;
4053 host.enable = NULL;
4054 host.logfile = NULL;
4055 host.config = NULL;
4056 host.lines = -1;
4057 host.motd = default_motd;
paul3b0c5d92005-03-08 10:43:43 +00004058 host.motdfile = NULL;
paul718e3742002-12-13 20:15:29 +00004059
4060 /* Install top nodes. */
4061 install_node (&view_node, NULL);
4062 install_node (&enable_node, NULL);
4063 install_node (&auth_node, NULL);
4064 install_node (&auth_enable_node, NULL);
Paul Jakma62687ff2008-08-23 14:27:06 +01004065 install_node (&restricted_node, NULL);
paul718e3742002-12-13 20:15:29 +00004066 install_node (&config_node, config_write_host);
4067
4068 /* Each node's basic commands. */
4069 install_element (VIEW_NODE, &show_version_cmd);
4070 if (terminal)
4071 {
4072 install_element (VIEW_NODE, &config_list_cmd);
4073 install_element (VIEW_NODE, &config_exit_cmd);
4074 install_element (VIEW_NODE, &config_quit_cmd);
4075 install_element (VIEW_NODE, &config_help_cmd);
4076 install_element (VIEW_NODE, &config_enable_cmd);
4077 install_element (VIEW_NODE, &config_terminal_length_cmd);
4078 install_element (VIEW_NODE, &config_terminal_no_length_cmd);
ajs274a4a42004-12-07 15:39:31 +00004079 install_element (VIEW_NODE, &show_logging_cmd);
ajs2885f722004-12-17 23:16:33 +00004080 install_element (VIEW_NODE, &echo_cmd);
Paul Jakma62687ff2008-08-23 14:27:06 +01004081
4082 install_element (RESTRICTED_NODE, &config_list_cmd);
4083 install_element (RESTRICTED_NODE, &config_exit_cmd);
4084 install_element (RESTRICTED_NODE, &config_quit_cmd);
4085 install_element (RESTRICTED_NODE, &config_help_cmd);
4086 install_element (RESTRICTED_NODE, &config_enable_cmd);
4087 install_element (RESTRICTED_NODE, &config_terminal_length_cmd);
4088 install_element (RESTRICTED_NODE, &config_terminal_no_length_cmd);
4089 install_element (RESTRICTED_NODE, &echo_cmd);
paul718e3742002-12-13 20:15:29 +00004090 }
4091
4092 if (terminal)
4093 {
4094 install_default (ENABLE_NODE);
4095 install_element (ENABLE_NODE, &config_disable_cmd);
4096 install_element (ENABLE_NODE, &config_terminal_cmd);
4097 install_element (ENABLE_NODE, &copy_runningconfig_startupconfig_cmd);
4098 }
4099 install_element (ENABLE_NODE, &show_startup_config_cmd);
4100 install_element (ENABLE_NODE, &show_version_cmd);
paul718e3742002-12-13 20:15:29 +00004101
4102 if (terminal)
paul718e3742002-12-13 20:15:29 +00004103 {
hassoe7168df2004-10-03 20:11:32 +00004104 install_element (ENABLE_NODE, &config_terminal_length_cmd);
4105 install_element (ENABLE_NODE, &config_terminal_no_length_cmd);
ajs274a4a42004-12-07 15:39:31 +00004106 install_element (ENABLE_NODE, &show_logging_cmd);
ajs2885f722004-12-17 23:16:33 +00004107 install_element (ENABLE_NODE, &echo_cmd);
ajs274a4a42004-12-07 15:39:31 +00004108 install_element (ENABLE_NODE, &config_logmsg_cmd);
hassoe7168df2004-10-03 20:11:32 +00004109
4110 install_default (CONFIG_NODE);
hassoea8e9d92004-10-07 21:32:14 +00004111 }
4112
4113 install_element (CONFIG_NODE, &hostname_cmd);
4114 install_element (CONFIG_NODE, &no_hostname_cmd);
hassoe7168df2004-10-03 20:11:32 +00004115
hassoea8e9d92004-10-07 21:32:14 +00004116 if (terminal)
4117 {
hassoe7168df2004-10-03 20:11:32 +00004118 install_element (CONFIG_NODE, &password_cmd);
4119 install_element (CONFIG_NODE, &password_text_cmd);
4120 install_element (CONFIG_NODE, &enable_password_cmd);
4121 install_element (CONFIG_NODE, &enable_password_text_cmd);
4122 install_element (CONFIG_NODE, &no_enable_password_cmd);
4123
paul718e3742002-12-13 20:15:29 +00004124 install_element (CONFIG_NODE, &config_log_stdout_cmd);
ajs274a4a42004-12-07 15:39:31 +00004125 install_element (CONFIG_NODE, &config_log_stdout_level_cmd);
paul718e3742002-12-13 20:15:29 +00004126 install_element (CONFIG_NODE, &no_config_log_stdout_cmd);
ajs274a4a42004-12-07 15:39:31 +00004127 install_element (CONFIG_NODE, &config_log_monitor_cmd);
4128 install_element (CONFIG_NODE, &config_log_monitor_level_cmd);
4129 install_element (CONFIG_NODE, &no_config_log_monitor_cmd);
paul718e3742002-12-13 20:15:29 +00004130 install_element (CONFIG_NODE, &config_log_file_cmd);
ajs274a4a42004-12-07 15:39:31 +00004131 install_element (CONFIG_NODE, &config_log_file_level_cmd);
paul718e3742002-12-13 20:15:29 +00004132 install_element (CONFIG_NODE, &no_config_log_file_cmd);
ajs274a4a42004-12-07 15:39:31 +00004133 install_element (CONFIG_NODE, &no_config_log_file_level_cmd);
paul718e3742002-12-13 20:15:29 +00004134 install_element (CONFIG_NODE, &config_log_syslog_cmd);
ajs274a4a42004-12-07 15:39:31 +00004135 install_element (CONFIG_NODE, &config_log_syslog_level_cmd);
paul12ab19f2003-07-26 06:14:55 +00004136 install_element (CONFIG_NODE, &config_log_syslog_facility_cmd);
paul718e3742002-12-13 20:15:29 +00004137 install_element (CONFIG_NODE, &no_config_log_syslog_cmd);
paul12ab19f2003-07-26 06:14:55 +00004138 install_element (CONFIG_NODE, &no_config_log_syslog_facility_cmd);
ajs274a4a42004-12-07 15:39:31 +00004139 install_element (CONFIG_NODE, &config_log_facility_cmd);
4140 install_element (CONFIG_NODE, &no_config_log_facility_cmd);
paul718e3742002-12-13 20:15:29 +00004141 install_element (CONFIG_NODE, &config_log_trap_cmd);
4142 install_element (CONFIG_NODE, &no_config_log_trap_cmd);
4143 install_element (CONFIG_NODE, &config_log_record_priority_cmd);
4144 install_element (CONFIG_NODE, &no_config_log_record_priority_cmd);
Andrew J. Schorr1ed72e02007-04-28 22:14:10 +00004145 install_element (CONFIG_NODE, &config_log_timestamp_precision_cmd);
4146 install_element (CONFIG_NODE, &no_config_log_timestamp_precision_cmd);
paul718e3742002-12-13 20:15:29 +00004147 install_element (CONFIG_NODE, &service_password_encrypt_cmd);
4148 install_element (CONFIG_NODE, &no_service_password_encrypt_cmd);
4149 install_element (CONFIG_NODE, &banner_motd_default_cmd);
paul3b0c5d92005-03-08 10:43:43 +00004150 install_element (CONFIG_NODE, &banner_motd_file_cmd);
paul718e3742002-12-13 20:15:29 +00004151 install_element (CONFIG_NODE, &no_banner_motd_cmd);
4152 install_element (CONFIG_NODE, &service_terminal_length_cmd);
4153 install_element (CONFIG_NODE, &no_service_terminal_length_cmd);
paul718e3742002-12-13 20:15:29 +00004154
paul354d1192005-04-25 16:26:42 +00004155 install_element (VIEW_NODE, &show_thread_cpu_cmd);
4156 install_element (ENABLE_NODE, &show_thread_cpu_cmd);
Paul Jakma62687ff2008-08-23 14:27:06 +01004157 install_element (RESTRICTED_NODE, &show_thread_cpu_cmd);
Paul Jakmae276eb82010-01-09 16:15:00 +00004158
4159 install_element (ENABLE_NODE, &clear_thread_cpu_cmd);
paul354d1192005-04-25 16:26:42 +00004160 install_element (VIEW_NODE, &show_work_queues_cmd);
4161 install_element (ENABLE_NODE, &show_work_queues_cmd);
paul9ab68122003-01-18 01:16:20 +00004162 }
paul718e3742002-12-13 20:15:29 +00004163 srand(time(NULL));
4164}
Chris Caputo228da422009-07-18 05:44:03 +00004165
Christian Frankecd40b322013-09-30 12:27:51 +00004166static void
4167cmd_terminate_token(struct cmd_token *token)
4168{
4169 unsigned int i, j;
4170 vector keyword_vect;
4171
4172 if (token->multiple)
4173 {
4174 for (i = 0; i < vector_active(token->multiple); i++)
4175 cmd_terminate_token(vector_slot(token->multiple, i));
4176 vector_free(token->multiple);
4177 token->multiple = NULL;
4178 }
4179
4180 if (token->keyword)
4181 {
4182 for (i = 0; i < vector_active(token->keyword); i++)
4183 {
4184 keyword_vect = vector_slot(token->keyword, i);
4185 for (j = 0; j < vector_active(keyword_vect); j++)
4186 cmd_terminate_token(vector_slot(keyword_vect, j));
4187 vector_free(keyword_vect);
4188 }
4189 vector_free(token->keyword);
4190 token->keyword = NULL;
4191 }
4192
4193 XFREE(MTYPE_CMD_TOKENS, token->cmd);
4194 XFREE(MTYPE_CMD_TOKENS, token->desc);
4195
4196 XFREE(MTYPE_CMD_TOKENS, token);
4197}
4198
4199static void
4200cmd_terminate_element(struct cmd_element *cmd)
4201{
4202 unsigned int i;
4203
4204 if (cmd->tokens == NULL)
4205 return;
4206
4207 for (i = 0; i < vector_active(cmd->tokens); i++)
4208 cmd_terminate_token(vector_slot(cmd->tokens, i));
4209
4210 vector_free(cmd->tokens);
4211 cmd->tokens = NULL;
4212}
4213
Chris Caputo228da422009-07-18 05:44:03 +00004214void
4215cmd_terminate ()
4216{
Christian Frankecd40b322013-09-30 12:27:51 +00004217 unsigned int i, j;
Chris Caputo228da422009-07-18 05:44:03 +00004218 struct cmd_node *cmd_node;
4219 struct cmd_element *cmd_element;
Christian Frankecd40b322013-09-30 12:27:51 +00004220 vector cmd_node_v;
Chris Caputo228da422009-07-18 05:44:03 +00004221
4222 if (cmdvec)
4223 {
4224 for (i = 0; i < vector_active (cmdvec); i++)
4225 if ((cmd_node = vector_slot (cmdvec, i)) != NULL)
4226 {
4227 cmd_node_v = cmd_node->cmd_vector;
4228
4229 for (j = 0; j < vector_active (cmd_node_v); j++)
Christian Frankecd40b322013-09-30 12:27:51 +00004230 if ((cmd_element = vector_slot (cmd_node_v, j)) != NULL)
4231 cmd_terminate_element(cmd_element);
Chris Caputo228da422009-07-18 05:44:03 +00004232
4233 vector_free (cmd_node_v);
4234 }
4235
4236 vector_free (cmdvec);
4237 cmdvec = NULL;
4238 }
4239
4240 if (command_cr)
Christian Frankecd40b322013-09-30 12:27:51 +00004241 XFREE(MTYPE_CMD_TOKENS, command_cr);
4242 if (token_cr.desc)
4243 XFREE(MTYPE_CMD_TOKENS, token_cr.desc);
Chris Caputo228da422009-07-18 05:44:03 +00004244 if (host.name)
4245 XFREE (MTYPE_HOST, host.name);
4246 if (host.password)
4247 XFREE (MTYPE_HOST, host.password);
4248 if (host.password_encrypt)
4249 XFREE (MTYPE_HOST, host.password_encrypt);
4250 if (host.enable)
4251 XFREE (MTYPE_HOST, host.enable);
4252 if (host.enable_encrypt)
4253 XFREE (MTYPE_HOST, host.enable_encrypt);
4254 if (host.logfile)
4255 XFREE (MTYPE_HOST, host.logfile);
4256 if (host.motdfile)
4257 XFREE (MTYPE_HOST, host.motdfile);
4258 if (host.config)
4259 XFREE (MTYPE_HOST, host.config);
4260}