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
33 #include "command_match.h"
35 #define GRAMMAR_STR "CLI grammar sandbox\n"
37 DEFINE_MTYPE_STATIC(LIB
, CMD_TOKENS
, "Command desc");
40 void grammar_sandbox_init(void);
41 static void pretty_print_graph(struct vty
*vty
, struct graph_node
*, int, int,
42 struct graph_node
**, size_t);
43 static void init_cmdgraph(struct vty
*, struct graph
**);
45 /** shim interface commands **/
46 static struct graph
*nodegraph
= NULL
, *nodegraph_free
= NULL
;
48 #define check_nodegraph() \
51 vty_out(vty, "nodegraph not initialized\n"); \
58 "grammar parse LINE...",
61 "command to pass to new parser\n")
66 // make a string from tokenized command line
67 char *command
= argv_concat(argv
, argc
, idx_command
);
69 // create cmd_element for parser
70 struct cmd_element
*cmd
=
71 XCALLOC(MTYPE_CMD_TOKENS
, sizeof(struct cmd_element
));
72 cmd
->string
= command
;
74 "0\n1\n2\n3\n4\n5\n6\n7\n8\n9\n10\n11\n12\n13\n14\n15\n16\n17\n18\n19\n";
77 // parse the command and install it into the command graph
78 struct graph
*graph
= graph_new();
79 struct cmd_token
*token
= cmd_token_new(START_TKN
, 0, NULL
, NULL
);
80 graph_new_node(graph
, token
, (void (*)(void *)) & cmd_token_del
);
82 cmd_graph_parse(graph
, cmd
);
83 cmd_graph_merge(nodegraph
, graph
, +1);
88 DEFUN (grammar_test_complete
,
89 grammar_test_complete_cmd
,
90 "grammar complete COMMAND...",
92 "attempt to complete input on DFA\n"
93 "command to complete\n")
98 char *cmdstr
= argv_concat(argv
, argc
, idx_command
);
102 vector command
= cmd_make_strvec(cmdstr
);
104 XFREE(MTYPE_TMP
, cmdstr
);
108 // generate completions of user input
109 struct list
*completions
;
110 enum matcher_rv result
=
111 command_complete(nodegraph
, command
, &completions
);
113 // print completions or relevant error message
114 if (!MATCHER_ERROR(result
)) {
115 vector comps
= completions_to_vec(completions
);
116 struct cmd_token
*tkn
;
118 // calculate length of longest tkn->text in completions
119 unsigned int width
= 0, i
= 0;
120 for (i
= 0; i
< vector_active(comps
); i
++) {
121 tkn
= vector_slot(comps
, i
);
122 unsigned int len
= strlen(tkn
->text
);
123 width
= len
> width
? len
: width
;
127 for (i
= 0; i
< vector_active(comps
); i
++) {
128 tkn
= vector_slot(comps
, i
);
129 vty_out(vty
, " %-*s %s\n", width
, tkn
->text
,
133 for (i
= 0; i
< vector_active(comps
); i
++)
135 (struct cmd_token
*)vector_slot(comps
, i
));
138 vty_out(vty
, "%% No match\n");
141 list_delete(&completions
);
142 cmd_free_strvec(command
);
143 XFREE(MTYPE_TMP
, cmdstr
);
148 DEFUN (grammar_test_match
,
149 grammar_test_match_cmd
,
150 "grammar match COMMAND...",
152 "attempt to match input on DFA\n"
153 "command to match\n")
158 if (argv
[2]->arg
[0] == '#')
161 char *cmdstr
= argv_concat(argv
, argc
, idx_command
);
164 vector command
= cmd_make_strvec(cmdstr
);
166 XFREE(MTYPE_TMP
, cmdstr
);
170 struct list
*argvv
= NULL
;
171 const struct cmd_element
*element
= NULL
;
172 enum matcher_rv result
=
173 command_match(nodegraph
, command
, &argvv
, &element
);
175 // print completions or relevant error message
177 vty_out(vty
, "Matched: %s\n", element
->string
);
179 struct cmd_token
*token
;
180 for (ALL_LIST_ELEMENTS_RO(argvv
, ln
, token
))
181 vty_out(vty
, "%s -- %s\n", token
->text
, token
->arg
);
183 vty_out(vty
, "func: %p\n", element
->func
);
187 assert(MATCHER_ERROR(result
));
189 case MATCHER_NO_MATCH
:
190 vty_out(vty
, "%% Unknown command\n");
192 case MATCHER_INCOMPLETE
:
193 vty_out(vty
, "%% Incomplete command\n");
195 case MATCHER_AMBIGUOUS
:
196 vty_out(vty
, "%% Ambiguous command\n");
199 vty_out(vty
, "%% Unknown error\n");
205 cmd_free_strvec(command
);
206 XFREE(MTYPE_TMP
, cmdstr
);
212 * Testing shim to test docstrings
214 DEFUN (grammar_test_doc
,
215 grammar_test_doc_cmd
,
216 "grammar test docstring",
218 "Test function for docstring\n"
223 // create cmd_element with docstring
224 struct cmd_element
*cmd
=
225 XCALLOC(MTYPE_CMD_TOKENS
, sizeof(struct cmd_element
));
226 cmd
->string
= XSTRDUP(
228 "test docstring <example|selector follow> (1-255) end VARIABLE [OPTION|set lol] . VARARG");
229 cmd
->doc
= XSTRDUP(MTYPE_CMD_TOKENS
,
238 "optional variable\n"
245 cmd_graph_parse(nodegraph
, cmd
);
251 * Debugging command to print command graph
253 DEFUN (grammar_test_show
,
254 grammar_test_show_cmd
,
255 "grammar show [doc]",
257 "print current accumulated DFA\n"
258 "include docstrings\n")
262 struct graph_node
*stack
[CMD_ARGC_MAX
];
263 pretty_print_graph(vty
, vector_slot(nodegraph
->nodes
, 0), 0, argc
>= 3,
268 DEFUN (grammar_test_dot
,
269 grammar_test_dot_cmd
,
270 "grammar dotfile OUTNAME",
272 "print current graph for dot\n"
276 FILE *ofd
= fopen(argv
[2]->arg
, "w");
279 vty_out(vty
, "%s: %s\r\n", argv
[2]->arg
, strerror(errno
));
283 char *dot
= cmd_graph_dump_dot(nodegraph
);
285 fprintf(ofd
, "%s", dot
);
287 XFREE(MTYPE_TMP
, dot
);
292 struct cmd_permute_item
{
294 struct cmd_element
*el
;
297 static void cmd_permute_free(void *arg
)
299 struct cmd_permute_item
*i
= arg
;
300 XFREE(MTYPE_TMP
, i
->cmd
);
304 static int cmd_permute_cmp(void *a
, void *b
)
306 struct cmd_permute_item
*aa
= a
, *bb
= b
;
307 return strcmp(aa
->cmd
, bb
->cmd
);
310 static void cmd_graph_permute(struct list
*out
, struct graph_node
**stack
,
311 size_t stackpos
, char *cmd
)
313 struct graph_node
*gn
= stack
[stackpos
];
314 struct cmd_token
*tok
= gn
->data
;
315 char *appendp
= cmd
+ strlen(cmd
);
318 if (tok
->type
< SPECIAL_TKN
) {
319 sprintf(appendp
, "%s ", tok
->text
);
320 appendp
+= strlen(appendp
);
321 } else if (tok
->type
== END_TKN
) {
322 struct cmd_permute_item
*i
= XMALLOC(MTYPE_TMP
, sizeof(*i
));
323 i
->el
= ((struct graph_node
*)vector_slot(gn
->to
, 0))->data
;
324 i
->cmd
= XSTRDUP(MTYPE_TMP
, cmd
);
325 i
->cmd
[strlen(cmd
) - 1] = '\0';
326 listnode_add_sort(out
, i
);
330 if (++stackpos
== CMD_ARGC_MAX
)
333 for (size_t i
= 0; i
< vector_active(gn
->to
); i
++) {
334 struct graph_node
*gnext
= vector_slot(gn
->to
, i
);
335 for (j
= 0; j
< stackpos
; j
++)
336 if (stack
[j
] == gnext
)
341 stack
[stackpos
] = gnext
;
343 cmd_graph_permute(out
, stack
, stackpos
, cmd
);
347 static struct list
*cmd_graph_permutations(struct graph
*graph
)
349 char accumulate
[2048] = "";
350 struct graph_node
*stack
[CMD_ARGC_MAX
];
352 struct list
*rv
= list_new();
353 rv
->cmp
= cmd_permute_cmp
;
354 rv
->del
= cmd_permute_free
;
355 stack
[0] = vector_slot(graph
->nodes
, 0);
356 cmd_graph_permute(rv
, stack
, 0, accumulate
);
360 extern vector cmdvec
;
362 DEFUN (grammar_findambig
,
363 grammar_findambig_cmd
,
364 "grammar find-ambiguous [{printall|nodescan}]",
366 "Find ambiguous commands\n"
367 "Print all permutations\n"
370 struct list
*commands
;
371 struct cmd_permute_item
*prev
= NULL
, *cur
= NULL
;
373 int i
, printall
, scan
, scannode
= 0;
377 printall
= argv_find(argv
, argc
, "printall", &i
);
379 scan
= argv_find(argv
, argc
, "nodescan", &i
);
381 if (scan
&& nodegraph_free
) {
382 graph_delete_graph(nodegraph_free
);
383 nodegraph_free
= NULL
;
386 if (!scan
&& !nodegraph
) {
387 vty_out(vty
, "nodegraph uninitialized\r\n");
388 return CMD_WARNING_CONFIG_FAILED
;
393 struct cmd_node
*cnode
=
394 vector_slot(cmdvec
, scannode
++);
397 cmd_finalize_node(cnode
);
398 nodegraph
= cnode
->cmdgraph
;
401 vty_out(vty
, "scanning node %d (%s)\n", scannode
- 1,
405 commands
= cmd_graph_permutations(nodegraph
);
407 for (ALL_LIST_ELEMENTS_RO(commands
, ln
, cur
)) {
408 int same
= prev
&& !strcmp(prev
->cmd
, cur
->cmd
);
409 if (printall
&& !same
)
410 vty_out(vty
, "'%s' [%x]\n", cur
->cmd
,
413 vty_out(vty
, "'%s' AMBIGUOUS:\n", cur
->cmd
);
414 vty_out(vty
, " %s\n '%s'\n", prev
->el
->name
,
416 vty_out(vty
, " %s\n '%s'\n", cur
->el
->name
,
423 list_delete(&commands
);
426 } while (scan
&& scannode
< LINK_PARAMS_NODE
);
428 vty_out(vty
, "%d ambiguous commands found.\n", ambig
);
432 return ambig
== 0 ? CMD_SUCCESS
: CMD_WARNING_CONFIG_FAILED
;
435 DEFUN (grammar_init_graph
,
436 grammar_init_graph_cmd
,
439 "(re)initialize graph\n")
442 graph_delete_graph(nodegraph_free
);
443 nodegraph_free
= NULL
;
445 init_cmdgraph(vty
, &nodegraph
);
449 DEFUN (grammar_access
,
451 "grammar access (0-65535)",
453 "access node graph\n"
457 graph_delete_graph(nodegraph_free
);
458 nodegraph_free
= NULL
;
460 struct cmd_node
*cnode
;
462 cnode
= vector_slot(cmdvec
, atoi(argv
[2]->arg
));
464 vty_out(vty
, "%% no such node\n");
465 return CMD_WARNING_CONFIG_FAILED
;
468 vty_out(vty
, "node %d\n", (int)cnode
->node
);
469 cmd_finalize_node(cnode
);
470 nodegraph
= cnode
->cmdgraph
;
474 /* this is called in vtysh.c to set up the testing shim */
475 void grammar_sandbox_init(void)
477 // install all enable elements
478 install_element(ENABLE_NODE
, &grammar_test_cmd
);
479 install_element(ENABLE_NODE
, &grammar_test_show_cmd
);
480 install_element(ENABLE_NODE
, &grammar_test_dot_cmd
);
481 install_element(ENABLE_NODE
, &grammar_test_match_cmd
);
482 install_element(ENABLE_NODE
, &grammar_test_complete_cmd
);
483 install_element(ENABLE_NODE
, &grammar_test_doc_cmd
);
484 install_element(ENABLE_NODE
, &grammar_findambig_cmd
);
485 install_element(ENABLE_NODE
, &grammar_init_graph_cmd
);
486 install_element(ENABLE_NODE
, &grammar_access_cmd
);
490 * Pretty-prints a graph, assuming it is a tree.
492 * @param start the node to take as the root
493 * @param level indent level for recursive calls, always pass 0
495 static void pretty_print_graph(struct vty
*vty
, struct graph_node
*start
,
496 int level
, int desc
, struct graph_node
**stack
,
501 struct cmd_token
*tok
= start
->data
;
503 snprintf(tokennum
, sizeof(tokennum
), "%d?", tok
->type
);
504 vty_out(vty
, "%s", lookup_msg(tokennames
, tok
->type
, NULL
));
506 vty_out(vty
, ":\"%s\"", tok
->text
);
508 vty_out(vty
, " => %s", tok
->varname
);
510 vty_out(vty
, " ?'%s'", tok
->desc
);
513 if (stackpos
== CMD_ARGC_MAX
) {
514 vty_out(vty
, " -aborting! (depth limit)\n");
517 stack
[stackpos
++] = start
;
519 int numto
= desc
? 2 : vector_active(start
->to
);
523 for (unsigned int i
= 0; i
< vector_active(start
->to
); i
++) {
524 struct graph_node
*adj
= vector_slot(start
->to
, i
);
525 // if we're listing multiple children, indent!
527 for (int j
= 0; j
< level
+ 1; j
++)
529 // if this node is a vararg, just print *
532 else if (((struct cmd_token
*)adj
->data
)->type
534 vty_out(vty
, "--END\n");
537 for (k
= 0; k
< stackpos
; k
++)
538 if (stack
[k
] == adj
) {
539 vty_out(vty
, "<<loop@%zu \n",
546 numto
> 1 ? level
+ 1 : level
,
547 desc
, stack
, stackpos
);
554 /** stuff that should go in command.c + command.h */
555 static void init_cmdgraph(struct vty
*vty
, struct graph
**graph
)
557 // initialize graph, add start noe
558 *graph
= graph_new();
559 nodegraph_free
= *graph
;
560 struct cmd_token
*token
= cmd_token_new(START_TKN
, 0, NULL
, NULL
);
561 graph_new_node(*graph
, token
, (void (*)(void *)) & cmd_token_del
);
563 vty_out(vty
, "initialized graph\n");