]> git.proxmox.com Git - mirror_frr.git/blob - lib/grammar_sandbox.c
lib: Add docstring 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_doc,
28 grammar_test_doc_cmd,
29 "grammar test docstring",
30 GRAMMAR_STR
31 "Test function for docstring\n"
32 "Command end\n")
33 {
34 struct cmd_element *cmd = malloc(sizeof(struct cmd_element));
35 cmd->string = "test docstring <example|selector follow> (1-255) end VARIABLE [OPTION|set lol] . VARARG";
36 cmd->doc = "Test stuff\n"
37 "docstring thing\n"
38 "first example\n"
39 "second example\n"
40 "follow\n"
41 "random range\n"
42 "end thingy\n"
43 "variable\n"
44 "optional variable\n"
45 "optional set\n"
46 "optional lol\n"
47 "vararg!\n";
48 cmd->func = NULL;
49 cmd->tokens = vector_init(VECTOR_MIN_SIZE);
50 parse_command_format(nodegraph, cmd);
51 return CMD_SUCCESS;
52 }
53
54 DEFUN (grammar_test_show,
55 grammar_test_show_cmd,
56 "grammar tree",
57 GRAMMAR_STR
58 "print current accumulated DFA\n")
59 {
60 if (!nodegraph)
61 fprintf(stderr, "!nodegraph\n");
62 else
63 walk_graph(nodegraph, 0);
64 return CMD_SUCCESS;
65 }
66
67 DEFUN (grammar_test_complete,
68 grammar_test_complete_cmd,
69 "grammar complete .COMMAND",
70 GRAMMAR_STR
71 "attempt to complete input on DFA\n"
72 "command to complete")
73 {
74 const char* command = argv_concat(argv, argc, 0);
75 struct list *result = match_command_complete (nodegraph, command);
76
77 if (result->count == 0) // invalid command
78 fprintf(stderr, "%% Unknown command\n");
79 else
80 {
81 fprintf(stderr, "%% Matched full input, possible completions:\n");
82 char* desc = malloc(30);
83 struct listnode *node;
84 struct graph_node *cnode;
85 // print possible next hops, if any
86 for (ALL_LIST_ELEMENTS_RO(result,node,cnode)) {
87 if (cnode->type == END_GN)
88 fprintf(stderr, "<cr> %p\n", cnode->element->func);
89 else
90 fprintf(stderr, "%-30s%s\n", describe_node(cnode, desc, 30), cnode->doc);
91 }
92 free(desc);
93 }
94 list_delete(result);
95
96 return CMD_SUCCESS;
97 }
98
99 DEFUN (grammar_test_match,
100 grammar_test_match_cmd,
101 "grammar match .COMMAND",
102 GRAMMAR_STR
103 "attempt to match input on DFA\n"
104 "command to match")
105 {
106 const char *line = argv_concat(argv, argc, 0);
107
108 struct list *argvv = NULL;
109 struct cmd_element *element = NULL;
110 enum matcher_rv result = match_command (nodegraph, line, &argvv, &element);
111
112 if (element) {
113 fprintf(stderr, "Matched: %s\n", element->string);
114 struct listnode *ln;
115 struct graph_node *gn;
116 for (ALL_LIST_ELEMENTS_RO(argvv,ln,gn))
117 fprintf(stderr, "%s -- %s\n", gn->text, gn->arg);
118 }
119 else {
120 switch (result) {
121 case MATCHER_NO_MATCH:
122 fprintf(stderr, "%% Unknown command\n");
123 break;
124 case MATCHER_INCOMPLETE:
125 fprintf(stderr, "%% Incomplete command\n");
126 break;
127 case MATCHER_AMBIGUOUS:
128 fprintf(stderr, "%% Ambiguous command\n");
129 break;
130 default:
131 fprintf(stderr, "%% Unknown error\n");
132 break;
133 }
134 }
135
136 return CMD_SUCCESS;
137 }
138
139
140 void grammar_sandbox_init(void);
141 void grammar_sandbox_init() {
142 fprintf(stderr, "reinitializing graph\n");
143 nodegraph = new_node(START_GN);
144 install_element (ENABLE_NODE, &grammar_test_cmd);
145 install_element (ENABLE_NODE, &grammar_test_show_cmd);
146 install_element (ENABLE_NODE, &grammar_test_match_cmd);
147 install_element (ENABLE_NODE, &grammar_test_complete_cmd);
148 install_element (ENABLE_NODE, &grammar_test_doc_cmd);
149 }