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