diff options
| author | Quentin Young <qlyoung@cumulusnetworks.com> | 2016-07-27 01:35:46 +0000 |
|---|---|---|
| committer | Quentin Young <qlyoung@cumulusnetworks.com> | 2016-07-27 01:35:46 +0000 |
| commit | eceb106640e0279ed89e476c25f37f3b2da860fc (patch) | |
| tree | b12bbe56de2a2ad613a8880ae1b7d825a7fd992e /lib/grammar_sandbox.c | |
| parent | a53fbbf5f0e52793c262f9326340a606cf37500f (diff) | |
lib: Add matching and argv support
Queries may be run against DFA's to find matching
cmd_element, and argument lists may be constructed.
Signed-off-by: Quentin Young <qlyoung@cumulusnetworks.com>
Diffstat (limited to 'lib/grammar_sandbox.c')
| -rw-r--r-- | lib/grammar_sandbox.c | 56 |
1 files changed, 45 insertions, 11 deletions
diff --git a/lib/grammar_sandbox.c b/lib/grammar_sandbox.c index 24f882c89f..6eee9c8b65 100644 --- a/lib/grammar_sandbox.c +++ b/lib/grammar_sandbox.c @@ -14,11 +14,11 @@ DEFUN (grammar_test, GRAMMAR_STR "command to pass to new parser\n") { - - const char* command = argv_concat(argv, argc, 0); - cmd_parse_format(command, "lol", nodegraph); + char* command = argv_concat(argv, argc, 0); + struct cmd_element *cmd = malloc(sizeof(struct cmd_element)); + cmd->string = command; + parse_command_format(nodegraph, cmd); walk_graph(nodegraph, 0); - return CMD_SUCCESS; } @@ -32,15 +32,15 @@ DEFUN (grammar_test_show, return CMD_SUCCESS; } -DEFUN (grammar_test_match, - grammar_test_match_cmd, - "grammar match .COMMAND", +DEFUN (grammar_test_complete, + grammar_test_complete_cmd, + "grammar complete .COMMAND", GRAMMAR_STR - "attempt to match input on DFA\n" - "command to match") + "attempt to complete input on DFA\n" + "command to complete") { const char* command = argv_concat(argv, argc, 0); - struct list *result = match_command(nodegraph, FILTER_STRICT, command); + struct list *result = match_command_complete (nodegraph, command, FILTER_STRICT); if (result->count == 0) // invalid command fprintf(stderr, "%% Unknown command\n"); @@ -53,7 +53,7 @@ DEFUN (grammar_test_match, // print possible next hops, if any for (ALL_LIST_ELEMENTS_RO(result,node,cnode)) { if (cnode->type == END_GN) - fprintf(stderr, "<cr>"); + fprintf(stderr, "<cr>\n"); else fprintf(stderr, "%s\n", describe_node(cnode, desc, 50)); } @@ -64,6 +64,39 @@ DEFUN (grammar_test_match, return CMD_SUCCESS; } +DEFUN (grammar_test_match, + grammar_test_match_cmd, + "grammar match .COMMAND", + GRAMMAR_STR + "attempt to match input on DFA\n" + "command to match") +{ + const char* command = argv_concat(argv, argc, 0); + struct cmd_element *element = match_command (nodegraph, command, FILTER_STRICT); + + if (element) + fprintf(stderr, "Matched: %s\n", element->string); + else { + fprintf(stderr, "Returned NULL\n"); + return CMD_SUCCESS; + } + + struct list *argvv = match_build_argv (command, element); + fprintf(stderr, "num args: %d\n", argvv->count); + + struct listnode *ln; + struct graph_node *gn; + for (ALL_LIST_ELEMENTS_RO(argvv,ln,gn)) { + fprintf(stderr, "node text: %s\n", gn->text); + if (gn->arg) + fprintf(stderr, "node arg: %s\n", gn->arg); + else + fprintf(stderr, "No arg.\n"); + } + + return CMD_SUCCESS; +} + void grammar_sandbox_init(void); void grammar_sandbox_init() { @@ -72,4 +105,5 @@ void grammar_sandbox_init() { install_element (ENABLE_NODE, &grammar_test_cmd); install_element (ENABLE_NODE, &grammar_test_show_cmd); install_element (ENABLE_NODE, &grammar_test_match_cmd); + install_element (ENABLE_NODE, &grammar_test_complete_cmd); } |
