2 * Testing shim and API examples for the new CLI backend.
4 * This unit defines a number of commands in the old engine that can
5 * be used to test and interact with the new engine.
7 * Copyright (C) 2016 Cumulus Networks, Inc.
9 * This file is part of GNU Zebra.
11 * GNU Zebra is free software; you can redistribute it and/or modify it
12 * under the terms of the GNU General Public License as published by the
13 * Free Software Foundation; either version 2, or (at your option) any
16 * GNU Zebra is distributed in the hope that it will be useful, but
17 * WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19 * General Public License for more details.
21 * You should have received a copy of the GNU General Public License along
22 * with this program; see the file COPYING; if not, write to the Free Software
23 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
27 #include "memory_vty.h"
30 #include "command_match.h"
32 #define GRAMMAR_STR "CLI grammar sandbox\n"
34 DEFINE_MTYPE_STATIC(LIB
, CMD_TOKENS
, "Command desc")
39 void grammar_sandbox_init(void);
40 void pretty_print_graph(struct vty
*vty
, struct graph_node
*, int, int,
41 struct graph_node
**, size_t);
42 static void pretty_print_dot(FILE *ofd
, unsigned opts
, struct graph_node
*start
,
43 struct graph_node
**stack
, size_t stackpos
,
44 struct graph_node
**visited
, size_t *visitpos
);
45 void init_cmdgraph(struct vty
*, struct graph
**);
47 /** shim interface commands **/
48 struct graph
*nodegraph
= NULL
, *nodegraph_free
= NULL
;
50 #define check_nodegraph() \
53 vty_out(vty, "nodegraph not initialized\n"); \
60 "grammar parse LINE...",
63 "command to pass to new parser\n")
68 // make a string from tokenized command line
69 char *command
= argv_concat(argv
, argc
, idx_command
);
71 // create cmd_element for parser
72 struct cmd_element
*cmd
=
73 XCALLOC(MTYPE_CMD_TOKENS
, sizeof(struct cmd_element
));
74 cmd
->string
= command
;
76 "0\n1\n2\n3\n4\n5\n6\n7\n8\n9\n10\n11\n12\n13\n14\n15\n16\n17\n18\n19\n";
79 // parse the command and install it into the command graph
80 struct graph
*graph
= graph_new();
81 struct cmd_token
*token
=
82 cmd_token_new(START_TKN
, CMD_ATTR_NORMAL
, NULL
, NULL
);
83 graph_new_node(graph
, token
, (void (*)(void *)) & cmd_token_del
);
85 cmd_graph_parse(graph
, cmd
);
86 cmd_graph_merge(nodegraph
, graph
, +1);
91 DEFUN (grammar_test_complete
,
92 grammar_test_complete_cmd
,
93 "grammar complete COMMAND...",
95 "attempt to complete input on DFA\n"
96 "command to complete\n")
101 char *cmdstr
= argv_concat(argv
, argc
, idx_command
);
105 vector command
= cmd_make_strvec(cmdstr
);
107 XFREE(MTYPE_TMP
, cmdstr
);
111 // generate completions of user input
112 struct list
*completions
;
113 enum matcher_rv result
=
114 command_complete(nodegraph
, command
, &completions
);
116 // print completions or relevant error message
117 if (!MATCHER_ERROR(result
)) {
118 vector comps
= completions_to_vec(completions
);
119 struct cmd_token
*tkn
;
121 // calculate length of longest tkn->text in completions
122 unsigned int width
= 0, i
= 0;
123 for (i
= 0; i
< vector_active(comps
); i
++) {
124 tkn
= vector_slot(comps
, i
);
125 unsigned int len
= strlen(tkn
->text
);
126 width
= len
> width
? len
: width
;
130 for (i
= 0; i
< vector_active(comps
); i
++) {
131 tkn
= vector_slot(comps
, i
);
132 vty_out(vty
, " %-*s %s\n", width
, tkn
->text
,
136 for (i
= 0; i
< vector_active(comps
); i
++)
138 (struct cmd_token
*)vector_slot(comps
, i
));
141 vty_out(vty
, "%% No match\n");
144 list_delete_and_null(&completions
);
145 cmd_free_strvec(command
);
146 XFREE(MTYPE_TMP
, cmdstr
);
151 DEFUN (grammar_test_match
,
152 grammar_test_match_cmd
,
153 "grammar match COMMAND...",
155 "attempt to match input on DFA\n"
156 "command to match\n")
161 if (argv
[2]->arg
[0] == '#')
164 char *cmdstr
= argv_concat(argv
, argc
, idx_command
);
167 vector command
= cmd_make_strvec(cmdstr
);
169 XFREE(MTYPE_TMP
, cmdstr
);
173 struct list
*argvv
= NULL
;
174 const struct cmd_element
*element
= NULL
;
175 enum matcher_rv result
=
176 command_match(nodegraph
, command
, &argvv
, &element
);
178 // print completions or relevant error message
180 vty_out(vty
, "Matched: %s\n", element
->string
);
182 struct cmd_token
*token
;
183 for (ALL_LIST_ELEMENTS_RO(argvv
, ln
, token
))
184 vty_out(vty
, "%s -- %s\n", token
->text
, token
->arg
);
186 vty_out(vty
, "func: %p\n", element
->func
);
188 list_delete_and_null(&argvv
);
190 assert(MATCHER_ERROR(result
));
192 case MATCHER_NO_MATCH
:
193 vty_out(vty
, "%% Unknown command\n");
195 case MATCHER_INCOMPLETE
:
196 vty_out(vty
, "%% Incomplete command\n");
198 case MATCHER_AMBIGUOUS
:
199 vty_out(vty
, "%% Ambiguous command\n");
202 vty_out(vty
, "%% Unknown error\n");
208 cmd_free_strvec(command
);
209 XFREE(MTYPE_TMP
, cmdstr
);
215 * Testing shim to test docstrings
217 DEFUN (grammar_test_doc
,
218 grammar_test_doc_cmd
,
219 "grammar test docstring",
221 "Test function for docstring\n"
226 // create cmd_element with docstring
227 struct cmd_element
*cmd
=
228 XCALLOC(MTYPE_CMD_TOKENS
, sizeof(struct cmd_element
));
229 cmd
->string
= XSTRDUP(
231 "test docstring <example|selector follow> (1-255) end VARIABLE [OPTION|set lol] . VARARG");
232 cmd
->doc
= XSTRDUP(MTYPE_CMD_TOKENS
,
241 "optional variable\n"
248 cmd_graph_parse(nodegraph
, cmd
);
254 * Debugging command to print command graph
256 DEFUN (grammar_test_show
,
257 grammar_test_show_cmd
,
258 "grammar show [doc]",
260 "print current accumulated DFA\n"
261 "include docstrings\n")
265 struct graph_node
*stack
[MAXDEPTH
];
266 pretty_print_graph(vty
, vector_slot(nodegraph
->nodes
, 0), 0, argc
>= 3,
271 DEFUN (grammar_test_dot
,
272 grammar_test_dot_cmd
,
273 "grammar dotfile OUTNAME",
275 "print current graph for dot\n"
280 struct graph_node
*stack
[MAXDEPTH
];
281 struct graph_node
*visited
[MAXDEPTH
* MAXDEPTH
];
284 FILE *ofd
= fopen(argv
[2]->arg
, "w");
286 vty_out(vty
, "%s: %s\r\n", argv
[2]->arg
, strerror(errno
));
291 "digraph {\n graph [ rankdir = LR ];\n node [ fontname = \"Fira Mono\", fontsize = 9 ];\n\n");
292 pretty_print_dot(ofd
, 0, vector_slot(nodegraph
->nodes
, 0), stack
, 0,
299 struct cmd_permute_item
{
301 struct cmd_element
*el
;
304 static void cmd_permute_free(void *arg
)
306 struct cmd_permute_item
*i
= arg
;
307 XFREE(MTYPE_TMP
, i
->cmd
);
311 static int cmd_permute_cmp(void *a
, void *b
)
313 struct cmd_permute_item
*aa
= a
, *bb
= b
;
314 return strcmp(aa
->cmd
, bb
->cmd
);
317 static void cmd_graph_permute(struct list
*out
, struct graph_node
**stack
,
318 size_t stackpos
, char *cmd
)
320 struct graph_node
*gn
= stack
[stackpos
];
321 struct cmd_token
*tok
= gn
->data
;
322 char *appendp
= cmd
+ strlen(cmd
);
325 if (tok
->type
< SPECIAL_TKN
) {
326 sprintf(appendp
, "%s ", tok
->text
);
327 appendp
+= strlen(appendp
);
328 } else if (tok
->type
== END_TKN
) {
329 struct cmd_permute_item
*i
= XMALLOC(MTYPE_TMP
, sizeof(*i
));
330 i
->el
= ((struct graph_node
*)vector_slot(gn
->to
, 0))->data
;
331 i
->cmd
= XSTRDUP(MTYPE_TMP
, cmd
);
332 i
->cmd
[strlen(cmd
) - 1] = '\0';
333 listnode_add_sort(out
, i
);
337 if (++stackpos
== MAXDEPTH
)
340 for (i
= 0; i
< vector_active(gn
->to
); i
++) {
341 struct graph_node
*gnext
= vector_slot(gn
->to
, i
);
342 for (j
= 0; j
< stackpos
; j
++)
343 if (stack
[j
] == gnext
)
348 stack
[stackpos
] = gnext
;
350 cmd_graph_permute(out
, stack
, stackpos
, cmd
);
354 static struct list
*cmd_graph_permutations(struct graph
*graph
)
356 char accumulate
[2048] = "";
357 struct graph_node
*stack
[MAXDEPTH
];
359 struct list
*rv
= list_new();
360 rv
->cmp
= cmd_permute_cmp
;
361 rv
->del
= cmd_permute_free
;
362 stack
[0] = vector_slot(graph
->nodes
, 0);
363 cmd_graph_permute(rv
, stack
, 0, accumulate
);
367 extern vector cmdvec
;
369 DEFUN (grammar_findambig
,
370 grammar_findambig_cmd
,
371 "grammar find-ambiguous [{printall|nodescan}]",
373 "Find ambiguous commands\n"
374 "Print all permutations\n"
377 struct list
*commands
;
378 struct cmd_permute_item
*prev
= NULL
, *cur
= NULL
;
380 int i
, printall
, scan
, scannode
= 0;
384 printall
= argv_find(argv
, argc
, "printall", &i
);
386 scan
= argv_find(argv
, argc
, "nodescan", &i
);
388 if (scan
&& nodegraph_free
) {
389 graph_delete_graph(nodegraph_free
);
390 nodegraph_free
= NULL
;
393 if (!scan
&& !nodegraph
) {
394 vty_out(vty
, "nodegraph uninitialized\r\n");
395 return CMD_WARNING_CONFIG_FAILED
;
400 struct cmd_node
*cnode
=
401 vector_slot(cmdvec
, scannode
++);
404 nodegraph
= cnode
->cmdgraph
;
407 vty_out(vty
, "scanning node %d (%s)\n",
408 scannode
- 1, node_names
[scannode
- 1]);
411 commands
= cmd_graph_permutations(nodegraph
);
413 for (ALL_LIST_ELEMENTS_RO(commands
, ln
, cur
)) {
414 int same
= prev
&& !strcmp(prev
->cmd
, cur
->cmd
);
415 if (printall
&& !same
)
416 vty_out(vty
, "'%s' [%x]\n", cur
->cmd
,
419 vty_out(vty
, "'%s' AMBIGUOUS:\n", cur
->cmd
);
420 vty_out(vty
, " %s\n '%s'\n", prev
->el
->name
,
422 vty_out(vty
, " %s\n '%s'\n", cur
->el
->name
,
429 list_delete_and_null(&commands
);
432 } while (scan
&& scannode
< LINK_PARAMS_NODE
);
434 vty_out(vty
, "%d ambiguous commands found.\n", ambig
);
438 return ambig
== 0 ? CMD_SUCCESS
: CMD_WARNING_CONFIG_FAILED
;
441 DEFUN (grammar_init_graph
,
442 grammar_init_graph_cmd
,
445 "(re)initialize graph\n")
448 graph_delete_graph(nodegraph_free
);
449 nodegraph_free
= NULL
;
451 init_cmdgraph(vty
, &nodegraph
);
455 DEFUN (grammar_access
,
457 "grammar access (0-65535)",
459 "access node graph\n"
463 graph_delete_graph(nodegraph_free
);
464 nodegraph_free
= NULL
;
466 struct cmd_node
*cnode
;
468 cnode
= vector_slot(cmdvec
, atoi(argv
[2]->arg
));
470 vty_out(vty
, "%% no such node\n");
471 return CMD_WARNING_CONFIG_FAILED
;
474 vty_out(vty
, "node %d\n", (int)cnode
->node
);
475 nodegraph
= cnode
->cmdgraph
;
479 /* this is called in vtysh.c to set up the testing shim */
480 void grammar_sandbox_init(void)
482 // install all enable elements
483 install_element(ENABLE_NODE
, &grammar_test_cmd
);
484 install_element(ENABLE_NODE
, &grammar_test_show_cmd
);
485 install_element(ENABLE_NODE
, &grammar_test_dot_cmd
);
486 install_element(ENABLE_NODE
, &grammar_test_match_cmd
);
487 install_element(ENABLE_NODE
, &grammar_test_complete_cmd
);
488 install_element(ENABLE_NODE
, &grammar_test_doc_cmd
);
489 install_element(ENABLE_NODE
, &grammar_findambig_cmd
);
490 install_element(ENABLE_NODE
, &grammar_init_graph_cmd
);
491 install_element(ENABLE_NODE
, &grammar_access_cmd
);
494 #define item(x) { x, #x }
495 struct message tokennames
[] = {item(WORD_TKN
), // words
496 item(VARIABLE_TKN
), // almost anything
497 item(RANGE_TKN
), // integer range
498 item(IPV4_TKN
), // IPV4 addresses
499 item(IPV4_PREFIX_TKN
), // IPV4 network prefixes
500 item(IPV6_TKN
), // IPV6 prefixes
501 item(IPV6_PREFIX_TKN
), // IPV6 network prefixes
502 item(MAC_TKN
), // MAC address
503 item(MAC_PREFIX_TKN
), // MAC address w/ mask
508 item(START_TKN
), // first token in line
509 item(END_TKN
), // last token in line
513 * Pretty-prints a graph, assuming it is a tree.
515 * @param start the node to take as the root
516 * @param level indent level for recursive calls, always pass 0
518 void pretty_print_graph(struct vty
*vty
, struct graph_node
*start
, int level
,
519 int desc
, struct graph_node
**stack
, size_t stackpos
)
523 struct cmd_token
*tok
= start
->data
;
525 snprintf(tokennum
, sizeof(tokennum
), "%d?", tok
->type
);
526 vty_out(vty
, "%s", lookup_msg(tokennames
, tok
->type
, NULL
));
528 vty_out(vty
, ":\"%s\"", tok
->text
);
530 vty_out(vty
, " => %s", tok
->varname
);
532 vty_out(vty
, " ?'%s'", tok
->desc
);
535 if (stackpos
== MAXDEPTH
) {
536 vty_out(vty
, " -aborting! (depth limit)\n");
539 stack
[stackpos
++] = start
;
541 int numto
= desc
? 2 : vector_active(start
->to
);
545 for (unsigned int i
= 0; i
< vector_active(start
->to
); i
++) {
546 struct graph_node
*adj
= vector_slot(start
->to
, i
);
547 // if we're listing multiple children, indent!
549 for (int j
= 0; j
< level
+ 1; j
++)
551 // if this node is a vararg, just print *
554 else if (((struct cmd_token
*)adj
->data
)->type
556 vty_out(vty
, "--END\n");
559 for (k
= 0; k
< stackpos
; k
++)
560 if (stack
[k
] == adj
) {
561 vty_out(vty
, "<<loop@%zu \n",
568 numto
> 1 ? level
+ 1 : level
,
569 desc
, stack
, stackpos
);
576 static void pretty_print_dot(FILE *ofd
, unsigned opts
, struct graph_node
*start
,
577 struct graph_node
**stack
, size_t stackpos
,
578 struct graph_node
**visited
, size_t *visitpos
)
582 struct cmd_token
*tok
= start
->data
;
585 for (size_t i
= 0; i
< (*visitpos
); i
++)
586 if (visited
[i
] == start
)
588 visited
[(*visitpos
)++] = start
;
589 if ((*visitpos
) == MAXDEPTH
* MAXDEPTH
)
592 snprintf(tokennum
, sizeof(tokennum
), "%d?", tok
->type
);
593 fprintf(ofd
, " n%p [ shape=box, label=<", start
);
595 fprintf(ofd
, "<b>%s</b>", lookup_msg(tokennames
, tok
->type
, NULL
));
596 if (tok
->attr
== CMD_ATTR_DEPRECATED
)
597 fprintf(ofd
, " (d)");
598 else if (tok
->attr
== CMD_ATTR_HIDDEN
)
599 fprintf(ofd
, " (h)");
601 if (tok
->type
== WORD_TKN
)
603 "<br/>\"<font color=\"#0055ff\" point-size=\"11\"><b>%s</b></font>\"",
606 fprintf(ofd
, "<br/>%s", tok
->text
);
609 fprintf(ofd, " ?'%s'", tok->desc); */
627 fprintf(ofd
, ">, style = filled, fillcolor = \"%s\" ];\n", color
);
629 if (stackpos
== MAXDEPTH
)
631 stack
[stackpos
++] = start
;
633 for (unsigned int i
= 0; i
< vector_active(start
->to
); i
++) {
634 struct graph_node
*adj
= vector_slot(start
->to
, i
);
635 // if this node is a vararg, just print *
637 fprintf(ofd
, " n%p -> n%p;\n", start
, start
);
638 } else if (((struct cmd_token
*)adj
->data
)->type
== END_TKN
) {
639 // struct cmd_token *et = adj->data;
640 fprintf(ofd
, " n%p -> end%p;\n", start
, adj
);
642 " end%p [ shape=box, label=<end>, style = filled, fillcolor = \"#ffddaa\" ];\n",
645 fprintf(ofd
, " n%p -> n%p;\n", start
, adj
);
647 for (k
= 0; k
< stackpos
; k
++)
651 pretty_print_dot(ofd
, opts
, adj
, stack
,
652 stackpos
, visited
, visitpos
);
659 /** stuff that should go in command.c + command.h */
660 void init_cmdgraph(struct vty
*vty
, struct graph
**graph
)
662 // initialize graph, add start noe
663 *graph
= graph_new();
664 nodegraph_free
= *graph
;
665 struct cmd_token
*token
= cmd_token_new(START_TKN
, 0, NULL
, NULL
);
666 graph_new_node(*graph
, token
, (void (*)(void *)) & cmd_token_del
);
668 vty_out(vty
, "initialized graph\n");