]> git.proxmox.com Git - mirror_frr.git/blob - lib/grammar_sandbox.c
lib: Add partial completion support
[mirror_frr.git] / lib / grammar_sandbox.c
1 #include "command.h"
2 #include "command_graph.h"
3 #include "command_parse.h"
4 #include "command_match.h"
5 #include "linklist.h"
6
7 #define GRAMMAR_STR "CLI grammar sandbox\n"
8
9 struct graph_node * nodegraph;
10
11 DEFUN (grammar_test,
12 grammar_test_cmd,
13 "grammar parse .COMMAND",
14 GRAMMAR_STR
15 "command to pass to new parser\n")
16 {
17 char* command = argv_concat(argv, argc, 0);
18 struct cmd_element *cmd = malloc(sizeof(struct cmd_element));
19 cmd->string = command;
20 cmd->doc = NULL;
21 cmd->func = NULL;
22 cmd->tokens = vector_init(VECTOR_MIN_SIZE);
23 parse_command_format(nodegraph, cmd);
24 return CMD_SUCCESS;
25 }
26
27 DEFUN (grammar_test_show,
28 grammar_test_show_cmd,
29 "grammar tree",
30 GRAMMAR_STR
31 "print current accumulated DFA\n")
32 {
33 if (!nodegraph)
34 fprintf(stderr, "!nodegraph\n");
35 fprintf(stderr, "trying to print nodegraph->type\n");
36 fprintf(stderr, "%d\n", nodegraph->type);
37 walk_graph(nodegraph, 0);
38 return CMD_SUCCESS;
39 }
40
41 DEFUN (grammar_test_complete,
42 grammar_test_complete_cmd,
43 "grammar complete .COMMAND",
44 GRAMMAR_STR
45 "attempt to complete input on DFA\n"
46 "command to complete")
47 {
48 const char* command = argv_concat(argv, argc, 0);
49 struct list *result = match_command_complete (nodegraph, command);
50
51 if (result->count == 0) // invalid command
52 fprintf(stderr, "%% Unknown command\n");
53 else
54 {
55 fprintf(stderr, "%% Matched full input, possible completions:\n");
56 char* desc = malloc(50);
57 struct listnode *node;
58 struct graph_node *cnode;
59 // print possible next hops, if any
60 for (ALL_LIST_ELEMENTS_RO(result,node,cnode)) {
61 if (cnode->type == END_GN)
62 fprintf(stderr, "<cr> %p\n", cnode->element->func);
63 else
64 fprintf(stderr, "%s\n", describe_node(cnode, desc, 50));
65 }
66 free(desc);
67 }
68 list_delete(result);
69
70 return CMD_SUCCESS;
71 }
72
73 DEFUN (grammar_test_match,
74 grammar_test_match_cmd,
75 "grammar match .COMMAND",
76 GRAMMAR_STR
77 "attempt to match input on DFA\n"
78 "command to match")
79 {
80 struct list *argvv = NULL;
81 const char *command = argv_concat(argv, argc, 0);
82 struct cmd_element *element = match_command (nodegraph, command, &argvv);
83
84 if (element) {
85 fprintf(stderr, "Matched: %s\n", element->string);
86 struct listnode *ln;
87 struct graph_node *gn;
88 for (ALL_LIST_ELEMENTS_RO(argvv,ln,gn))
89 fprintf(stderr, "%s -- %s\n", gn->text, gn->arg);
90 }
91 else {
92 fprintf(stderr, "Returned NULL\n");
93 return CMD_SUCCESS;
94 }
95
96 return CMD_SUCCESS;
97 }
98
99
100 void grammar_sandbox_init(void);
101 void grammar_sandbox_init() {
102 fprintf(stderr, "reinitializing graph\n");
103 nodegraph = new_node(START_GN);
104 install_element (ENABLE_NODE, &grammar_test_cmd);
105 install_element (ENABLE_NODE, &grammar_test_show_cmd);
106 install_element (ENABLE_NODE, &grammar_test_match_cmd);
107 install_element (ENABLE_NODE, &grammar_test_complete_cmd);
108 }