Branch data Line data Source code
1 : : /* SPDX-License-Identifier: BSD-3-Clause
2 : : * Copyright(c) 2010-2014 Intel Corporation
3 : : */
4 : :
5 : : #include <stdio.h>
6 : : #include <stdlib.h>
7 : : #include <inttypes.h>
8 : :
9 : : #include <rte_common.h>
10 : :
11 : : #include <cmdline_rdline.h>
12 : : #include <cmdline_parse.h>
13 : : #include <cmdline_parse_string.h>
14 : : #include <cmdline_parse_num.h>
15 : : #include <cmdline.h>
16 : :
17 : : #include "cmdline_test.h"
18 : :
19 : : /*** quit ***/
20 : : /* exit application */
21 : :
22 : : struct cmd_quit_result {
23 : : cmdline_fixed_string_t quit;
24 : : };
25 : :
26 : : static void
27 : 0 : cmd_quit_parsed(__rte_unused void *parsed_result,
28 : : struct cmdline *cl,
29 : : __rte_unused void *data)
30 : : {
31 : 0 : cmdline_quit(cl);
32 : 0 : }
33 : :
34 : : cmdline_parse_token_string_t cmd_quit_tok =
35 : : TOKEN_STRING_INITIALIZER(struct cmd_quit_result, quit,
36 : : "quit");
37 : :
38 : : cmdline_parse_inst_t cmd_quit = {
39 : : .f = cmd_quit_parsed, /* function to call */
40 : : .data = NULL, /* 2nd arg of func */
41 : : .help_str = "exit application",
42 : : .tokens = { /* token list, NULL terminated */
43 : : (void *)&cmd_quit_tok,
44 : : NULL,
45 : : },
46 : : };
47 : :
48 : :
49 : :
50 : : /*** single ***/
51 : : /* a simple single-word command */
52 : :
53 : : struct cmd_single_result {
54 : : cmdline_fixed_string_t single;
55 : : };
56 : :
57 : : static void
58 : 0 : cmd_single_parsed(__rte_unused void *parsed_result,
59 : : struct cmdline *cl,
60 : : __rte_unused void *data)
61 : : {
62 : 0 : cmdline_printf(cl, "Single word command parsed!\n");
63 : 0 : }
64 : :
65 : : cmdline_parse_token_string_t cmd_single_tok =
66 : : TOKEN_STRING_INITIALIZER(struct cmd_single_result, single,
67 : : "single");
68 : :
69 : : cmdline_parse_inst_t cmd_single = {
70 : : .f = cmd_single_parsed, /* function to call */
71 : : .data = NULL, /* 2nd arg of func */
72 : : .help_str = "a simple single-word command",
73 : : .tokens = { /* token list, NULL terminated */
74 : : (void *)&cmd_single_tok,
75 : : NULL,
76 : : },
77 : : };
78 : :
79 : :
80 : :
81 : : /*** single_long ***/
82 : : /* a variant of "single" command. useful to test autocomplete */
83 : :
84 : : struct cmd_single_long_result {
85 : : cmdline_fixed_string_t single_long;
86 : : };
87 : :
88 : : static void
89 : 0 : cmd_single_long_parsed(__rte_unused void *parsed_result,
90 : : struct cmdline *cl,
91 : : __rte_unused void *data)
92 : : {
93 : 0 : cmdline_printf(cl, "Single long word command parsed!\n");
94 : 0 : }
95 : :
96 : : cmdline_parse_token_string_t cmd_single_long_tok =
97 : : TOKEN_STRING_INITIALIZER(struct cmd_single_long_result, single_long,
98 : : "single_long");
99 : :
100 : : cmdline_parse_inst_t cmd_single_long = {
101 : : .f = cmd_single_long_parsed, /* function to call */
102 : : .data = NULL, /* 2nd arg of func */
103 : : .help_str = "a variant of \"single\" command, useful to test autocomplete",
104 : : .tokens = { /* token list, NULL terminated */
105 : : (void *)&cmd_single_long_tok,
106 : : NULL,
107 : : },
108 : : };
109 : :
110 : :
111 : :
112 : : /*** autocomplete_1 ***/
113 : : /* first command to test autocomplete when multiple commands have chars
114 : : * in common but none should complete due to ambiguity
115 : : */
116 : :
117 : : struct cmd_autocomplete_1_result {
118 : : cmdline_fixed_string_t token;
119 : : };
120 : :
121 : : static void
122 : 0 : cmd_autocomplete_1_parsed(__rte_unused void *parsed_result,
123 : : struct cmdline *cl,
124 : : __rte_unused void *data)
125 : : {
126 : 0 : cmdline_printf(cl, "Autocomplete command 1 parsed!\n");
127 : 0 : }
128 : :
129 : : cmdline_parse_token_string_t cmd_autocomplete_1_tok =
130 : : TOKEN_STRING_INITIALIZER(struct cmd_autocomplete_1_result, token,
131 : : "autocomplete_1");
132 : :
133 : : cmdline_parse_inst_t cmd_autocomplete_1 = {
134 : : .f = cmd_autocomplete_1_parsed, /* function to call */
135 : : .data = NULL, /* 2nd arg of func */
136 : : .help_str = "first ambiguous autocomplete command",
137 : : .tokens = { /* token list, NULL terminated */
138 : : (void *)&cmd_autocomplete_1_tok,
139 : : NULL,
140 : : },
141 : : };
142 : :
143 : :
144 : :
145 : : /*** autocomplete_2 ***/
146 : : /* second command to test autocomplete when multiple commands have chars
147 : : * in common but none should complete due to ambiguity
148 : : */
149 : :
150 : : struct cmd_autocomplete_2_result {
151 : : cmdline_fixed_string_t token;
152 : : };
153 : :
154 : : static void
155 : 0 : cmd_autocomplete_2_parsed(__rte_unused void *parsed_result,
156 : : struct cmdline *cl,
157 : : __rte_unused void *data)
158 : : {
159 : 0 : cmdline_printf(cl, "Autocomplete command 2 parsed!\n");
160 : 0 : }
161 : :
162 : : cmdline_parse_token_string_t cmd_autocomplete_2_tok =
163 : : TOKEN_STRING_INITIALIZER(struct cmd_autocomplete_2_result, token,
164 : : "autocomplete_2");
165 : :
166 : : cmdline_parse_inst_t cmd_autocomplete_2 = {
167 : : .f = cmd_autocomplete_2_parsed, /* function to call */
168 : : .data = NULL, /* 2nd arg of func */
169 : : .help_str = "second ambiguous autocomplete command",
170 : : .tokens = { /* token list, NULL terminated */
171 : : (void *)&cmd_autocomplete_2_tok,
172 : : NULL,
173 : : },
174 : : };
175 : :
176 : :
177 : :
178 : : /*** number command ***/
179 : : /* a command that simply returns whatever (uint32) number is supplied to it */
180 : :
181 : : struct cmd_num_result {
182 : : unsigned num;
183 : : };
184 : :
185 : : static void
186 : 0 : cmd_num_parsed(void *parsed_result,
187 : : struct cmdline *cl,
188 : : __rte_unused void *data)
189 : : {
190 : 0 : unsigned result = ((struct cmd_num_result*)parsed_result)->num;
191 : 0 : cmdline_printf(cl, "%u\n", result);
192 : 0 : }
193 : :
194 : : cmdline_parse_token_num_t cmd_num_tok =
195 : : TOKEN_NUM_INITIALIZER(struct cmd_num_result, num, RTE_UINT32);
196 : :
197 : : cmdline_parse_inst_t cmd_num = {
198 : : .f = cmd_num_parsed, /* function to call */
199 : : .data = NULL, /* 2nd arg of func */
200 : : .help_str = "a command that simply returns whatever number is entered",
201 : : .tokens = { /* token list, NULL terminated */
202 : : (void *)&cmd_num_tok,
203 : : NULL,
204 : : },
205 : : };
206 : :
207 : :
208 : :
209 : : /*** ambiguous first|ambiguous ***/
210 : : /* first command used to test command ambiguity */
211 : :
212 : : struct cmd_ambig_result_1 {
213 : : cmdline_fixed_string_t common_part;
214 : : cmdline_fixed_string_t ambig_part;
215 : : };
216 : :
217 : : static void
218 : 0 : cmd_ambig_1_parsed(__rte_unused void *parsed_result,
219 : : struct cmdline *cl,
220 : : __rte_unused void *data)
221 : : {
222 : 0 : cmdline_printf(cl, "Command 1 parsed!\n");
223 : 0 : }
224 : :
225 : : cmdline_parse_token_string_t cmd_ambig_common_1 =
226 : : TOKEN_STRING_INITIALIZER(struct cmd_ambig_result_1, common_part,
227 : : "ambiguous");
228 : : cmdline_parse_token_string_t cmd_ambig_ambig_1 =
229 : : TOKEN_STRING_INITIALIZER(struct cmd_ambig_result_1, ambig_part,
230 : : "first#ambiguous#ambiguous2");
231 : :
232 : : cmdline_parse_inst_t cmd_ambig_1 = {
233 : : .f = cmd_ambig_1_parsed, /* function to call */
234 : : .data = NULL, /* 2nd arg of func */
235 : : .help_str = "first command used to test command ambiguity",
236 : : .tokens = { /* token list, NULL terminated */
237 : : (void *)&cmd_ambig_common_1,
238 : : (void*)&cmd_ambig_ambig_1,
239 : : NULL,
240 : : },
241 : : };
242 : :
243 : :
244 : :
245 : : /*** ambiguous second|ambiguous ***/
246 : : /* second command used to test command ambiguity */
247 : :
248 : : struct cmd_ambig_result_2 {
249 : : cmdline_fixed_string_t common_part;
250 : : cmdline_fixed_string_t ambig_part;
251 : : };
252 : :
253 : : static void
254 : 0 : cmd_ambig_2_parsed(__rte_unused void *parsed_result,
255 : : struct cmdline *cl,
256 : : __rte_unused void *data)
257 : : {
258 : 0 : cmdline_printf(cl, "Command 2 parsed!\n");
259 : 0 : }
260 : :
261 : : cmdline_parse_token_string_t cmd_ambig_common_2 =
262 : : TOKEN_STRING_INITIALIZER(struct cmd_ambig_result_2, common_part,
263 : : "ambiguous");
264 : : cmdline_parse_token_string_t cmd_ambig_ambig_2 =
265 : : TOKEN_STRING_INITIALIZER(struct cmd_ambig_result_2, ambig_part,
266 : : "second#ambiguous#ambiguous2");
267 : :
268 : : cmdline_parse_inst_t cmd_ambig_2 = {
269 : : .f = cmd_ambig_2_parsed, /* function to call */
270 : : .data = NULL, /* 2nd arg of func */
271 : : .help_str = "second command used to test command ambiguity",
272 : : .tokens = { /* token list, NULL terminated */
273 : : (void *)&cmd_ambig_common_2,
274 : : (void*)&cmd_ambig_ambig_2,
275 : : NULL,
276 : : },
277 : : };
278 : :
279 : :
280 : :
281 : : /*** get_history_bufsize ***/
282 : : /* command that displays total space in history buffer
283 : : * this will be useful for testing history (to fill it up just enough to
284 : : * remove the last entry, we need to know how big it is).
285 : : */
286 : :
287 : : struct cmd_get_history_bufsize_result {
288 : : cmdline_fixed_string_t str;
289 : : };
290 : :
291 : : static void
292 : 0 : cmd_get_history_bufsize_parsed(__rte_unused void *parsed_result,
293 : : struct cmdline *cl,
294 : : __rte_unused void *data)
295 : : {
296 : 0 : struct rdline *rdl = cmdline_get_rdline(cl);
297 : :
298 : 0 : cmdline_printf(cl, "History buffer size: %zu\n",
299 : : rdline_get_history_buffer_size(rdl));
300 : 0 : }
301 : :
302 : : cmdline_parse_token_string_t cmd_get_history_bufsize_tok =
303 : : TOKEN_STRING_INITIALIZER(struct cmd_get_history_bufsize_result, str,
304 : : "get_history_bufsize");
305 : :
306 : : cmdline_parse_inst_t cmd_get_history_bufsize = {
307 : : .f = cmd_get_history_bufsize_parsed, /* function to call */
308 : : .data = NULL, /* 2nd arg of func */
309 : : .help_str = "command that displays total space in history buffer",
310 : : .tokens = { /* token list, NULL terminated */
311 : : (void *)&cmd_get_history_bufsize_tok,
312 : : NULL,
313 : : },
314 : : };
315 : :
316 : :
317 : :
318 : : /*** clear_history ***/
319 : : /* clears history buffer */
320 : :
321 : : struct cmd_clear_history_result {
322 : : cmdline_fixed_string_t str;
323 : : };
324 : :
325 : : static void
326 : 0 : cmd_clear_history_parsed(__rte_unused void *parsed_result,
327 : : struct cmdline *cl,
328 : : __rte_unused void *data)
329 : : {
330 : 0 : struct rdline *rdl = cmdline_get_rdline(cl);
331 : :
332 : 0 : rdline_clear_history(rdl);
333 : 0 : }
334 : :
335 : : cmdline_parse_token_string_t cmd_clear_history_tok =
336 : : TOKEN_STRING_INITIALIZER(struct cmd_clear_history_result, str,
337 : : "clear_history");
338 : :
339 : : cmdline_parse_inst_t cmd_clear_history = {
340 : : .f = cmd_clear_history_parsed, /* function to call */
341 : : .data = NULL, /* 2nd arg of func */
342 : : .help_str = "clear command history",
343 : : .tokens = { /* token list, NULL terminated */
344 : : (void *)&cmd_clear_history_tok,
345 : : NULL,
346 : : },
347 : : };
348 : :
349 : :
350 : :
351 : : /****************/
352 : :
353 : : cmdline_parse_ctx_t main_ctx[] = {
354 : : (cmdline_parse_inst_t *)&cmd_quit,
355 : : (cmdline_parse_inst_t *)&cmd_ambig_1,
356 : : (cmdline_parse_inst_t *)&cmd_ambig_2,
357 : : (cmdline_parse_inst_t *)&cmd_single,
358 : : (cmdline_parse_inst_t *)&cmd_single_long,
359 : : (cmdline_parse_inst_t *)&cmd_num,
360 : : (cmdline_parse_inst_t *)&cmd_get_history_bufsize,
361 : : (cmdline_parse_inst_t *)&cmd_clear_history,
362 : : (cmdline_parse_inst_t *)&cmd_autocomplete_1,
363 : : (cmdline_parse_inst_t *)&cmd_autocomplete_2,
364 : : NULL,
365 : : };
|