2016-06-23 16:47:32 +02:00
|
|
|
#include "command.h"
|
2016-07-19 23:14:27 +02:00
|
|
|
#include "command_graph.h"
|
2016-07-17 23:49:16 +02:00
|
|
|
#include "command_parse.h"
|
2016-07-19 23:14:27 +02:00
|
|
|
#include "command_match.h"
|
2016-07-21 23:38:03 +02:00
|
|
|
#include "linklist.h"
|
2016-06-23 16:47:32 +02:00
|
|
|
|
|
|
|
#define GRAMMAR_STR "CLI grammar sandbox\n"
|
|
|
|
|
2016-07-19 19:06:11 +02:00
|
|
|
struct graph_node * nodegraph;
|
|
|
|
|
2016-07-19 23:14:27 +02:00
|
|
|
DEFUN (grammar_test,
|
|
|
|
grammar_test_cmd,
|
|
|
|
"grammar parse .COMMAND",
|
|
|
|
GRAMMAR_STR
|
|
|
|
"command to pass to new parser\n")
|
|
|
|
{
|
2016-07-27 03:35:46 +02:00
|
|
|
char* command = argv_concat(argv, argc, 0);
|
|
|
|
struct cmd_element *cmd = malloc(sizeof(struct cmd_element));
|
|
|
|
cmd->string = command;
|
2016-08-01 20:36:30 +02:00
|
|
|
cmd->doc = NULL;
|
|
|
|
cmd->func = NULL;
|
2016-08-01 22:30:14 +02:00
|
|
|
cmd->tokens = vector_init(VECTOR_MIN_SIZE);
|
2016-07-27 03:35:46 +02:00
|
|
|
parse_command_format(nodegraph, cmd);
|
2016-07-17 23:49:16 +02:00
|
|
|
return CMD_SUCCESS;
|
2016-06-23 16:47:32 +02:00
|
|
|
}
|
|
|
|
|
2016-08-05 18:41:42 +02:00
|
|
|
DEFUN (grammar_test_doc,
|
|
|
|
grammar_test_doc_cmd,
|
|
|
|
"grammar test docstring",
|
|
|
|
GRAMMAR_STR
|
|
|
|
"Test function for docstring\n"
|
|
|
|
"Command end\n")
|
|
|
|
{
|
|
|
|
struct cmd_element *cmd = malloc(sizeof(struct cmd_element));
|
|
|
|
cmd->string = "test docstring <example|selector follow> (1-255) end VARIABLE [OPTION|set lol] . VARARG";
|
|
|
|
cmd->doc = "Test stuff\n"
|
|
|
|
"docstring thing\n"
|
|
|
|
"first example\n"
|
|
|
|
"second example\n"
|
|
|
|
"follow\n"
|
|
|
|
"random range\n"
|
|
|
|
"end thingy\n"
|
|
|
|
"variable\n"
|
|
|
|
"optional variable\n"
|
|
|
|
"optional set\n"
|
|
|
|
"optional lol\n"
|
|
|
|
"vararg!\n";
|
|
|
|
cmd->func = NULL;
|
|
|
|
cmd->tokens = vector_init(VECTOR_MIN_SIZE);
|
|
|
|
parse_command_format(nodegraph, cmd);
|
|
|
|
return CMD_SUCCESS;
|
|
|
|
}
|
|
|
|
|
2016-07-19 19:06:11 +02:00
|
|
|
DEFUN (grammar_test_show,
|
|
|
|
grammar_test_show_cmd,
|
|
|
|
"grammar tree",
|
|
|
|
GRAMMAR_STR
|
|
|
|
"print current accumulated DFA\n")
|
|
|
|
{
|
2016-07-29 17:54:03 +02:00
|
|
|
if (!nodegraph)
|
|
|
|
fprintf(stderr, "!nodegraph\n");
|
2016-08-04 20:16:26 +02:00
|
|
|
else
|
|
|
|
walk_graph(nodegraph, 0);
|
2016-07-19 23:14:27 +02:00
|
|
|
return CMD_SUCCESS;
|
|
|
|
}
|
|
|
|
|
2016-07-27 03:35:46 +02:00
|
|
|
DEFUN (grammar_test_complete,
|
|
|
|
grammar_test_complete_cmd,
|
|
|
|
"grammar complete .COMMAND",
|
2016-07-19 23:14:27 +02:00
|
|
|
GRAMMAR_STR
|
2016-07-27 03:35:46 +02:00
|
|
|
"attempt to complete input on DFA\n"
|
|
|
|
"command to complete")
|
2016-07-19 23:14:27 +02:00
|
|
|
{
|
|
|
|
const char* command = argv_concat(argv, argc, 0);
|
2016-08-01 22:30:14 +02:00
|
|
|
struct list *result = match_command_complete (nodegraph, command);
|
2016-07-21 23:38:03 +02:00
|
|
|
|
2016-07-22 21:04:16 +02:00
|
|
|
if (result->count == 0) // invalid command
|
2016-07-21 23:38:03 +02:00
|
|
|
fprintf(stderr, "%% Unknown command\n");
|
|
|
|
else
|
|
|
|
{
|
|
|
|
fprintf(stderr, "%% Matched full input, possible completions:\n");
|
2016-08-05 18:41:42 +02:00
|
|
|
char* desc = malloc(30);
|
2016-07-21 23:38:03 +02:00
|
|
|
struct listnode *node;
|
|
|
|
struct graph_node *cnode;
|
|
|
|
// print possible next hops, if any
|
2016-07-22 21:04:16 +02:00
|
|
|
for (ALL_LIST_ELEMENTS_RO(result,node,cnode)) {
|
|
|
|
if (cnode->type == END_GN)
|
2016-08-01 15:18:25 +02:00
|
|
|
fprintf(stderr, "<cr> %p\n", cnode->element->func);
|
2016-07-22 21:04:16 +02:00
|
|
|
else
|
2016-08-05 18:41:42 +02:00
|
|
|
fprintf(stderr, "%-30s%s\n", describe_node(cnode, desc, 30), cnode->doc);
|
2016-07-22 21:04:16 +02:00
|
|
|
}
|
|
|
|
free(desc);
|
2016-07-21 23:38:03 +02:00
|
|
|
}
|
2016-07-29 17:54:03 +02:00
|
|
|
list_delete(result);
|
2016-07-21 23:38:03 +02:00
|
|
|
|
2016-07-19 23:14:27 +02:00
|
|
|
return CMD_SUCCESS;
|
2016-07-19 19:06:11 +02:00
|
|
|
}
|
|
|
|
|
2016-07-27 03:35:46 +02:00
|
|
|
DEFUN (grammar_test_match,
|
|
|
|
grammar_test_match_cmd,
|
|
|
|
"grammar match .COMMAND",
|
|
|
|
GRAMMAR_STR
|
|
|
|
"attempt to match input on DFA\n"
|
|
|
|
"command to match")
|
|
|
|
{
|
2016-08-03 21:22:27 +02:00
|
|
|
const char *line = argv_concat(argv, argc, 0);
|
|
|
|
|
2016-07-29 17:54:03 +02:00
|
|
|
struct list *argvv = NULL;
|
2016-08-03 21:22:27 +02:00
|
|
|
struct cmd_element *element = NULL;
|
|
|
|
enum matcher_rv result = match_command (nodegraph, line, &argvv, &element);
|
2016-07-27 03:35:46 +02:00
|
|
|
|
2016-07-29 17:54:03 +02:00
|
|
|
if (element) {
|
2016-07-27 03:35:46 +02:00
|
|
|
fprintf(stderr, "Matched: %s\n", element->string);
|
2016-07-28 01:30:21 +02:00
|
|
|
struct listnode *ln;
|
|
|
|
struct graph_node *gn;
|
|
|
|
for (ALL_LIST_ELEMENTS_RO(argvv,ln,gn))
|
|
|
|
fprintf(stderr, "%s -- %s\n", gn->text, gn->arg);
|
2016-07-27 03:35:46 +02:00
|
|
|
}
|
2016-07-29 17:54:03 +02:00
|
|
|
else {
|
2016-08-03 21:22:27 +02:00
|
|
|
switch (result) {
|
|
|
|
case MATCHER_NO_MATCH:
|
|
|
|
fprintf(stderr, "%% Unknown command\n");
|
|
|
|
break;
|
|
|
|
case MATCHER_INCOMPLETE:
|
|
|
|
fprintf(stderr, "%% Incomplete command\n");
|
|
|
|
break;
|
|
|
|
case MATCHER_AMBIGUOUS:
|
|
|
|
fprintf(stderr, "%% Ambiguous command\n");
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
fprintf(stderr, "%% Unknown error\n");
|
|
|
|
break;
|
|
|
|
}
|
2016-07-29 17:54:03 +02:00
|
|
|
}
|
|
|
|
|
2016-07-27 03:35:46 +02:00
|
|
|
return CMD_SUCCESS;
|
|
|
|
}
|
|
|
|
|
2016-06-23 16:47:32 +02:00
|
|
|
|
|
|
|
void grammar_sandbox_init(void);
|
|
|
|
void grammar_sandbox_init() {
|
2016-07-19 19:06:11 +02:00
|
|
|
fprintf(stderr, "reinitializing graph\n");
|
2016-07-27 06:17:51 +02:00
|
|
|
nodegraph = new_node(START_GN);
|
2016-07-17 23:49:16 +02:00
|
|
|
install_element (ENABLE_NODE, &grammar_test_cmd);
|
2016-07-19 19:06:11 +02:00
|
|
|
install_element (ENABLE_NODE, &grammar_test_show_cmd);
|
2016-07-19 23:14:27 +02:00
|
|
|
install_element (ENABLE_NODE, &grammar_test_match_cmd);
|
2016-07-27 03:35:46 +02:00
|
|
|
install_element (ENABLE_NODE, &grammar_test_complete_cmd);
|
2016-08-05 18:41:42 +02:00
|
|
|
install_element (ENABLE_NODE, &grammar_test_doc_cmd);
|
2016-06-23 16:47:32 +02:00
|
|
|
}
|