1 /* SPDX-License-Identifier: BSD-3-Clause
2 * Copyright(c) 2010-2014 Intel Corporation
10 #include <rte_common.h>
12 #include <cmdline_rdline.h>
13 #include <cmdline_parse.h>
14 #include <cmdline_parse_string.h>
15 #include <cmdline_parse_num.h>
18 #include "cmdline_test.h"
21 /* exit application */
23 struct cmd_quit_result {
24 cmdline_fixed_string_t quit;
28 cmd_quit_parsed(__rte_unused void *parsed_result,
30 __rte_unused void *data)
35 cmdline_parse_token_string_t cmd_quit_tok =
36 TOKEN_STRING_INITIALIZER(struct cmd_quit_result, quit,
39 cmdline_parse_inst_t cmd_quit = {
40 .f = cmd_quit_parsed, /* function to call */
41 .data = NULL, /* 2nd arg of func */
42 .help_str = "exit application",
43 .tokens = { /* token list, NULL terminated */
44 (void *)&cmd_quit_tok,
52 /* a simple single-word command */
54 struct cmd_single_result {
55 cmdline_fixed_string_t single;
59 cmd_single_parsed(__rte_unused void *parsed_result,
61 __rte_unused void *data)
63 cmdline_printf(cl, "Single word command parsed!\n");
66 cmdline_parse_token_string_t cmd_single_tok =
67 TOKEN_STRING_INITIALIZER(struct cmd_single_result, single,
70 cmdline_parse_inst_t cmd_single = {
71 .f = cmd_single_parsed, /* function to call */
72 .data = NULL, /* 2nd arg of func */
73 .help_str = "a simple single-word command",
74 .tokens = { /* token list, NULL terminated */
75 (void *)&cmd_single_tok,
83 /* a variant of "single" command. useful to test autocomplete */
85 struct cmd_single_long_result {
86 cmdline_fixed_string_t single_long;
90 cmd_single_long_parsed(__rte_unused void *parsed_result,
92 __rte_unused void *data)
94 cmdline_printf(cl, "Single long word command parsed!\n");
97 cmdline_parse_token_string_t cmd_single_long_tok =
98 TOKEN_STRING_INITIALIZER(struct cmd_single_long_result, single_long,
101 cmdline_parse_inst_t cmd_single_long = {
102 .f = cmd_single_long_parsed, /* function to call */
103 .data = NULL, /* 2nd arg of func */
104 .help_str = "a variant of \"single\" command, useful to test autocomplete",
105 .tokens = { /* token list, NULL terminated */
106 (void *)&cmd_single_long_tok,
113 /*** autocomplete_1 ***/
114 /* first command to test autocomplete when multiple commands have chars
115 * in common but none should complete due to ambiguity
118 struct cmd_autocomplete_1_result {
119 cmdline_fixed_string_t token;
123 cmd_autocomplete_1_parsed(__rte_unused void *parsed_result,
125 __rte_unused void *data)
127 cmdline_printf(cl, "Autocomplete command 1 parsed!\n");
130 cmdline_parse_token_string_t cmd_autocomplete_1_tok =
131 TOKEN_STRING_INITIALIZER(struct cmd_autocomplete_1_result, token,
134 cmdline_parse_inst_t cmd_autocomplete_1 = {
135 .f = cmd_autocomplete_1_parsed, /* function to call */
136 .data = NULL, /* 2nd arg of func */
137 .help_str = "first ambiguous autocomplete command",
138 .tokens = { /* token list, NULL terminated */
139 (void *)&cmd_autocomplete_1_tok,
146 /*** autocomplete_2 ***/
147 /* second command to test autocomplete when multiple commands have chars
148 * in common but none should complete due to ambiguity
151 struct cmd_autocomplete_2_result {
152 cmdline_fixed_string_t token;
156 cmd_autocomplete_2_parsed(__rte_unused void *parsed_result,
158 __rte_unused void *data)
160 cmdline_printf(cl, "Autocomplete command 2 parsed!\n");
163 cmdline_parse_token_string_t cmd_autocomplete_2_tok =
164 TOKEN_STRING_INITIALIZER(struct cmd_autocomplete_2_result, token,
167 cmdline_parse_inst_t cmd_autocomplete_2 = {
168 .f = cmd_autocomplete_2_parsed, /* function to call */
169 .data = NULL, /* 2nd arg of func */
170 .help_str = "second ambiguous autocomplete command",
171 .tokens = { /* token list, NULL terminated */
172 (void *)&cmd_autocomplete_2_tok,
179 /*** number command ***/
180 /* a command that simply returns whatever (uint32) number is supplied to it */
182 struct cmd_num_result {
187 cmd_num_parsed(void *parsed_result,
189 __rte_unused void *data)
191 unsigned result = ((struct cmd_num_result*)parsed_result)->num;
192 cmdline_printf(cl, "%u\n", result);
195 cmdline_parse_token_num_t cmd_num_tok =
196 TOKEN_NUM_INITIALIZER(struct cmd_num_result, num, RTE_UINT32);
198 cmdline_parse_inst_t cmd_num = {
199 .f = cmd_num_parsed, /* function to call */
200 .data = NULL, /* 2nd arg of func */
201 .help_str = "a command that simply returns whatever number is entered",
202 .tokens = { /* token list, NULL terminated */
203 (void *)&cmd_num_tok,
210 /*** ambiguous first|ambiguous ***/
211 /* first command used to test command ambiguity */
213 struct cmd_ambig_result_1 {
214 cmdline_fixed_string_t common_part;
215 cmdline_fixed_string_t ambig_part;
219 cmd_ambig_1_parsed(__rte_unused void *parsed_result,
221 __rte_unused void *data)
223 cmdline_printf(cl, "Command 1 parsed!\n");
226 cmdline_parse_token_string_t cmd_ambig_common_1 =
227 TOKEN_STRING_INITIALIZER(struct cmd_ambig_result_1, common_part,
229 cmdline_parse_token_string_t cmd_ambig_ambig_1 =
230 TOKEN_STRING_INITIALIZER(struct cmd_ambig_result_1, ambig_part,
231 "first#ambiguous#ambiguous2");
233 cmdline_parse_inst_t cmd_ambig_1 = {
234 .f = cmd_ambig_1_parsed, /* function to call */
235 .data = NULL, /* 2nd arg of func */
236 .help_str = "first command used to test command ambiguity",
237 .tokens = { /* token list, NULL terminated */
238 (void *)&cmd_ambig_common_1,
239 (void*)&cmd_ambig_ambig_1,
246 /*** ambiguous second|ambiguous ***/
247 /* second command used to test command ambiguity */
249 struct cmd_ambig_result_2 {
250 cmdline_fixed_string_t common_part;
251 cmdline_fixed_string_t ambig_part;
255 cmd_ambig_2_parsed(__rte_unused void *parsed_result,
257 __rte_unused void *data)
259 cmdline_printf(cl, "Command 2 parsed!\n");
262 cmdline_parse_token_string_t cmd_ambig_common_2 =
263 TOKEN_STRING_INITIALIZER(struct cmd_ambig_result_2, common_part,
265 cmdline_parse_token_string_t cmd_ambig_ambig_2 =
266 TOKEN_STRING_INITIALIZER(struct cmd_ambig_result_2, ambig_part,
267 "second#ambiguous#ambiguous2");
269 cmdline_parse_inst_t cmd_ambig_2 = {
270 .f = cmd_ambig_2_parsed, /* function to call */
271 .data = NULL, /* 2nd arg of func */
272 .help_str = "second command used to test command ambiguity",
273 .tokens = { /* token list, NULL terminated */
274 (void *)&cmd_ambig_common_2,
275 (void*)&cmd_ambig_ambig_2,
282 /*** get_history_bufsize ***/
283 /* command that displays total space in history buffer
284 * this will be useful for testing history (to fill it up just enough to
285 * remove the last entry, we need to know how big it is).
288 struct cmd_get_history_bufsize_result {
289 cmdline_fixed_string_t str;
293 cmd_get_history_bufsize_parsed(__rte_unused void *parsed_result,
295 __rte_unused void *data)
297 struct rdline *rdl = cmdline_get_rdline(cl);
299 cmdline_printf(cl, "History buffer size: %zu\n",
300 sizeof(rdl->history_buf));
303 cmdline_parse_token_string_t cmd_get_history_bufsize_tok =
304 TOKEN_STRING_INITIALIZER(struct cmd_get_history_bufsize_result, str,
305 "get_history_bufsize");
307 cmdline_parse_inst_t cmd_get_history_bufsize = {
308 .f = cmd_get_history_bufsize_parsed, /* function to call */
309 .data = NULL, /* 2nd arg of func */
310 .help_str = "command that displays total space in history buffer",
311 .tokens = { /* token list, NULL terminated */
312 (void *)&cmd_get_history_bufsize_tok,
319 /*** clear_history ***/
320 /* clears history buffer */
322 struct cmd_clear_history_result {
323 cmdline_fixed_string_t str;
327 cmd_clear_history_parsed(__rte_unused void *parsed_result,
329 __rte_unused void *data)
331 struct rdline *rdl = cmdline_get_rdline(cl);
333 rdline_clear_history(rdl);
336 cmdline_parse_token_string_t cmd_clear_history_tok =
337 TOKEN_STRING_INITIALIZER(struct cmd_clear_history_result, str,
340 cmdline_parse_inst_t cmd_clear_history = {
341 .f = cmd_clear_history_parsed, /* function to call */
342 .data = NULL, /* 2nd arg of func */
343 .help_str = "clear command history",
344 .tokens = { /* token list, NULL terminated */
345 (void *)&cmd_clear_history_tok,
354 cmdline_parse_ctx_t main_ctx[] = {
355 (cmdline_parse_inst_t *)&cmd_quit,
356 (cmdline_parse_inst_t *)&cmd_ambig_1,
357 (cmdline_parse_inst_t *)&cmd_ambig_2,
358 (cmdline_parse_inst_t *)&cmd_single,
359 (cmdline_parse_inst_t *)&cmd_single_long,
360 (cmdline_parse_inst_t *)&cmd_num,
361 (cmdline_parse_inst_t *)&cmd_get_history_bufsize,
362 (cmdline_parse_inst_t *)&cmd_clear_history,
363 (cmdline_parse_inst_t *)&cmd_autocomplete_1,
364 (cmdline_parse_inst_t *)&cmd_autocomplete_2,