1 /* SPDX-License-Identifier: BSD-3-Clause
2 * Copyright(c) 2010-2014 Intel Corporation
10 #include <cmdline_rdline.h>
11 #include <cmdline_parse.h>
12 #include <cmdline_parse_string.h>
13 #include <cmdline_parse_num.h>
16 #include "cmdline_test.h"
19 /* exit application */
21 struct cmd_quit_result
{
22 cmdline_fixed_string_t quit
;
26 cmd_quit_parsed(__attribute__((unused
)) void *parsed_result
,
28 __attribute__((unused
)) void *data
)
33 cmdline_parse_token_string_t cmd_quit_tok
=
34 TOKEN_STRING_INITIALIZER(struct cmd_quit_result
, quit
,
37 cmdline_parse_inst_t cmd_quit
= {
38 .f
= cmd_quit_parsed
, /* function to call */
39 .data
= NULL
, /* 2nd arg of func */
40 .help_str
= "exit application",
41 .tokens
= { /* token list, NULL terminated */
42 (void *)&cmd_quit_tok
,
50 /* a simple single-word command */
52 struct cmd_single_result
{
53 cmdline_fixed_string_t single
;
57 cmd_single_parsed(__attribute__((unused
)) void *parsed_result
,
59 __attribute__((unused
)) void *data
)
61 cmdline_printf(cl
, "Single word command parsed!\n");
64 cmdline_parse_token_string_t cmd_single_tok
=
65 TOKEN_STRING_INITIALIZER(struct cmd_single_result
, single
,
68 cmdline_parse_inst_t cmd_single
= {
69 .f
= cmd_single_parsed
, /* function to call */
70 .data
= NULL
, /* 2nd arg of func */
71 .help_str
= "a simple single-word command",
72 .tokens
= { /* token list, NULL terminated */
73 (void *)&cmd_single_tok
,
81 /* a variant of "single" command. useful to test autocomplete */
83 struct cmd_single_long_result
{
84 cmdline_fixed_string_t single_long
;
88 cmd_single_long_parsed(__attribute__((unused
)) void *parsed_result
,
90 __attribute__((unused
)) void *data
)
92 cmdline_printf(cl
, "Single long word command parsed!\n");
95 cmdline_parse_token_string_t cmd_single_long_tok
=
96 TOKEN_STRING_INITIALIZER(struct cmd_single_long_result
, single_long
,
99 cmdline_parse_inst_t cmd_single_long
= {
100 .f
= cmd_single_long_parsed
, /* function to call */
101 .data
= NULL
, /* 2nd arg of func */
102 .help_str
= "a variant of \"single\" command, useful to test autocomplete",
103 .tokens
= { /* token list, NULL terminated */
104 (void *)&cmd_single_long_tok
,
111 /*** autocomplete_1 ***/
112 /* first command to test autocomplete when multiple commands have chars
113 * in common but none should complete due to ambiguity
116 struct cmd_autocomplete_1_result
{
117 cmdline_fixed_string_t token
;
121 cmd_autocomplete_1_parsed(__attribute__((unused
)) void *parsed_result
,
123 __attribute__((unused
)) void *data
)
125 cmdline_printf(cl
, "Autocomplete command 1 parsed!\n");
128 cmdline_parse_token_string_t cmd_autocomplete_1_tok
=
129 TOKEN_STRING_INITIALIZER(struct cmd_autocomplete_1_result
, token
,
132 cmdline_parse_inst_t cmd_autocomplete_1
= {
133 .f
= cmd_autocomplete_1_parsed
, /* function to call */
134 .data
= NULL
, /* 2nd arg of func */
135 .help_str
= "first ambiguous autocomplete command",
136 .tokens
= { /* token list, NULL terminated */
137 (void *)&cmd_autocomplete_1_tok
,
144 /*** autocomplete_2 ***/
145 /* second command to test autocomplete when multiple commands have chars
146 * in common but none should complete due to ambiguity
149 struct cmd_autocomplete_2_result
{
150 cmdline_fixed_string_t token
;
154 cmd_autocomplete_2_parsed(__attribute__((unused
)) void *parsed_result
,
156 __attribute__((unused
)) void *data
)
158 cmdline_printf(cl
, "Autocomplete command 2 parsed!\n");
161 cmdline_parse_token_string_t cmd_autocomplete_2_tok
=
162 TOKEN_STRING_INITIALIZER(struct cmd_autocomplete_2_result
, token
,
165 cmdline_parse_inst_t cmd_autocomplete_2
= {
166 .f
= cmd_autocomplete_2_parsed
, /* function to call */
167 .data
= NULL
, /* 2nd arg of func */
168 .help_str
= "second ambiguous autocomplete command",
169 .tokens
= { /* token list, NULL terminated */
170 (void *)&cmd_autocomplete_2_tok
,
177 /*** number command ***/
178 /* a command that simply returns whatever (uint32) number is supplied to it */
180 struct cmd_num_result
{
185 cmd_num_parsed(void *parsed_result
,
187 __attribute__((unused
)) void *data
)
189 unsigned result
= ((struct cmd_num_result
*)parsed_result
)->num
;
190 cmdline_printf(cl
, "%u\n", result
);
193 cmdline_parse_token_num_t cmd_num_tok
=
194 TOKEN_NUM_INITIALIZER(struct cmd_num_result
, num
, UINT32
);
196 cmdline_parse_inst_t cmd_num
= {
197 .f
= cmd_num_parsed
, /* function to call */
198 .data
= NULL
, /* 2nd arg of func */
199 .help_str
= "a command that simply returns whatever number is entered",
200 .tokens
= { /* token list, NULL terminated */
201 (void *)&cmd_num_tok
,
208 /*** ambiguous first|ambiguous ***/
209 /* first command used to test command ambiguity */
211 struct cmd_ambig_result_1
{
212 cmdline_fixed_string_t common_part
;
213 cmdline_fixed_string_t ambig_part
;
217 cmd_ambig_1_parsed(__attribute__((unused
)) void *parsed_result
,
219 __attribute__((unused
)) void *data
)
221 cmdline_printf(cl
, "Command 1 parsed!\n");
224 cmdline_parse_token_string_t cmd_ambig_common_1
=
225 TOKEN_STRING_INITIALIZER(struct cmd_ambig_result_1
, common_part
,
227 cmdline_parse_token_string_t cmd_ambig_ambig_1
=
228 TOKEN_STRING_INITIALIZER(struct cmd_ambig_result_1
, ambig_part
,
229 "first#ambiguous#ambiguous2");
231 cmdline_parse_inst_t cmd_ambig_1
= {
232 .f
= cmd_ambig_1_parsed
, /* function to call */
233 .data
= NULL
, /* 2nd arg of func */
234 .help_str
= "first command used to test command ambiguity",
235 .tokens
= { /* token list, NULL terminated */
236 (void *)&cmd_ambig_common_1
,
237 (void*)&cmd_ambig_ambig_1
,
244 /*** ambiguous second|ambiguous ***/
245 /* second command used to test command ambiguity */
247 struct cmd_ambig_result_2
{
248 cmdline_fixed_string_t common_part
;
249 cmdline_fixed_string_t ambig_part
;
253 cmd_ambig_2_parsed(__attribute__((unused
)) void *parsed_result
,
255 __attribute__((unused
)) void *data
)
257 cmdline_printf(cl
, "Command 2 parsed!\n");
260 cmdline_parse_token_string_t cmd_ambig_common_2
=
261 TOKEN_STRING_INITIALIZER(struct cmd_ambig_result_2
, common_part
,
263 cmdline_parse_token_string_t cmd_ambig_ambig_2
=
264 TOKEN_STRING_INITIALIZER(struct cmd_ambig_result_2
, ambig_part
,
265 "second#ambiguous#ambiguous2");
267 cmdline_parse_inst_t cmd_ambig_2
= {
268 .f
= cmd_ambig_2_parsed
, /* function to call */
269 .data
= NULL
, /* 2nd arg of func */
270 .help_str
= "second command used to test command ambiguity",
271 .tokens
= { /* token list, NULL terminated */
272 (void *)&cmd_ambig_common_2
,
273 (void*)&cmd_ambig_ambig_2
,
280 /*** get_history_bufsize ***/
281 /* command that displays total space in history buffer
282 * this will be useful for testing history (to fill it up just enough to
283 * remove the last entry, we need to know how big it is).
286 struct cmd_get_history_bufsize_result
{
287 cmdline_fixed_string_t str
;
291 cmd_get_history_bufsize_parsed(__attribute__((unused
)) void *parsed_result
,
293 __attribute__((unused
)) void *data
)
295 cmdline_printf(cl
, "History buffer size: %zu\n",
296 sizeof(cl
->rdl
.history_buf
));
299 cmdline_parse_token_string_t cmd_get_history_bufsize_tok
=
300 TOKEN_STRING_INITIALIZER(struct cmd_get_history_bufsize_result
, str
,
301 "get_history_bufsize");
303 cmdline_parse_inst_t cmd_get_history_bufsize
= {
304 .f
= cmd_get_history_bufsize_parsed
, /* function to call */
305 .data
= NULL
, /* 2nd arg of func */
306 .help_str
= "command that displays total space in history buffer",
307 .tokens
= { /* token list, NULL terminated */
308 (void *)&cmd_get_history_bufsize_tok
,
315 /*** clear_history ***/
316 /* clears history buffer */
318 struct cmd_clear_history_result
{
319 cmdline_fixed_string_t str
;
323 cmd_clear_history_parsed(__attribute__((unused
)) void *parsed_result
,
325 __attribute__((unused
)) void *data
)
327 rdline_clear_history(&cl
->rdl
);
330 cmdline_parse_token_string_t cmd_clear_history_tok
=
331 TOKEN_STRING_INITIALIZER(struct cmd_clear_history_result
, str
,
334 cmdline_parse_inst_t cmd_clear_history
= {
335 .f
= cmd_clear_history_parsed
, /* function to call */
336 .data
= NULL
, /* 2nd arg of func */
337 .help_str
= "clear command history",
338 .tokens
= { /* token list, NULL terminated */
339 (void *)&cmd_clear_history_tok
,
348 cmdline_parse_ctx_t main_ctx
[] = {
349 (cmdline_parse_inst_t
*)&cmd_quit
,
350 (cmdline_parse_inst_t
*)&cmd_ambig_1
,
351 (cmdline_parse_inst_t
*)&cmd_ambig_2
,
352 (cmdline_parse_inst_t
*)&cmd_single
,
353 (cmdline_parse_inst_t
*)&cmd_single_long
,
354 (cmdline_parse_inst_t
*)&cmd_num
,
355 (cmdline_parse_inst_t
*)&cmd_get_history_bufsize
,
356 (cmdline_parse_inst_t
*)&cmd_clear_history
,
357 (cmdline_parse_inst_t
*)&cmd_autocomplete_1
,
358 (cmdline_parse_inst_t
*)&cmd_autocomplete_2
,