10c36081dSBruce Richardson /* SPDX-License-Identifier: BSD-3-Clause
20c36081dSBruce Richardson * Copyright(c) 2010-2014 Intel Corporation
30c36081dSBruce Richardson */
40c36081dSBruce Richardson
50c36081dSBruce Richardson #include <stdio.h>
60c36081dSBruce Richardson #include <stdlib.h>
70c36081dSBruce Richardson #include <termios.h>
80c36081dSBruce Richardson #include <inttypes.h>
90c36081dSBruce Richardson
10f2fc83b4SThomas Monjalon #include <rte_common.h>
11f2fc83b4SThomas Monjalon
120c36081dSBruce Richardson #include <cmdline_rdline.h>
130c36081dSBruce Richardson #include <cmdline_parse.h>
140c36081dSBruce Richardson #include <cmdline_parse_string.h>
150c36081dSBruce Richardson #include <cmdline_parse_num.h>
160c36081dSBruce Richardson #include <cmdline.h>
170c36081dSBruce Richardson
180c36081dSBruce Richardson #include "cmdline_test.h"
190c36081dSBruce Richardson
200c36081dSBruce Richardson /*** quit ***/
210c36081dSBruce Richardson /* exit application */
220c36081dSBruce Richardson
230c36081dSBruce Richardson struct cmd_quit_result {
240c36081dSBruce Richardson cmdline_fixed_string_t quit;
250c36081dSBruce Richardson };
260c36081dSBruce Richardson
270c36081dSBruce Richardson static void
cmd_quit_parsed(__rte_unused void * parsed_result,struct cmdline * cl,__rte_unused void * data)28f2fc83b4SThomas Monjalon cmd_quit_parsed(__rte_unused void *parsed_result,
290c36081dSBruce Richardson struct cmdline *cl,
30f2fc83b4SThomas Monjalon __rte_unused void *data)
310c36081dSBruce Richardson {
320c36081dSBruce Richardson cmdline_quit(cl);
330c36081dSBruce Richardson }
340c36081dSBruce Richardson
350c36081dSBruce Richardson cmdline_parse_token_string_t cmd_quit_tok =
360c36081dSBruce Richardson TOKEN_STRING_INITIALIZER(struct cmd_quit_result, quit,
370c36081dSBruce Richardson "quit");
380c36081dSBruce Richardson
390c36081dSBruce Richardson cmdline_parse_inst_t cmd_quit = {
400c36081dSBruce Richardson .f = cmd_quit_parsed, /* function to call */
410c36081dSBruce Richardson .data = NULL, /* 2nd arg of func */
420c36081dSBruce Richardson .help_str = "exit application",
430c36081dSBruce Richardson .tokens = { /* token list, NULL terminated */
440c36081dSBruce Richardson (void *)&cmd_quit_tok,
450c36081dSBruce Richardson NULL,
460c36081dSBruce Richardson },
470c36081dSBruce Richardson };
480c36081dSBruce Richardson
490c36081dSBruce Richardson
500c36081dSBruce Richardson
510c36081dSBruce Richardson /*** single ***/
520c36081dSBruce Richardson /* a simple single-word command */
530c36081dSBruce Richardson
540c36081dSBruce Richardson struct cmd_single_result {
550c36081dSBruce Richardson cmdline_fixed_string_t single;
560c36081dSBruce Richardson };
570c36081dSBruce Richardson
580c36081dSBruce Richardson static void
cmd_single_parsed(__rte_unused void * parsed_result,struct cmdline * cl,__rte_unused void * data)59f2fc83b4SThomas Monjalon cmd_single_parsed(__rte_unused void *parsed_result,
600c36081dSBruce Richardson struct cmdline *cl,
61f2fc83b4SThomas Monjalon __rte_unused void *data)
620c36081dSBruce Richardson {
630c36081dSBruce Richardson cmdline_printf(cl, "Single word command parsed!\n");
640c36081dSBruce Richardson }
650c36081dSBruce Richardson
660c36081dSBruce Richardson cmdline_parse_token_string_t cmd_single_tok =
670c36081dSBruce Richardson TOKEN_STRING_INITIALIZER(struct cmd_single_result, single,
680c36081dSBruce Richardson "single");
690c36081dSBruce Richardson
700c36081dSBruce Richardson cmdline_parse_inst_t cmd_single = {
710c36081dSBruce Richardson .f = cmd_single_parsed, /* function to call */
720c36081dSBruce Richardson .data = NULL, /* 2nd arg of func */
730c36081dSBruce Richardson .help_str = "a simple single-word command",
740c36081dSBruce Richardson .tokens = { /* token list, NULL terminated */
750c36081dSBruce Richardson (void *)&cmd_single_tok,
760c36081dSBruce Richardson NULL,
770c36081dSBruce Richardson },
780c36081dSBruce Richardson };
790c36081dSBruce Richardson
800c36081dSBruce Richardson
810c36081dSBruce Richardson
820c36081dSBruce Richardson /*** single_long ***/
830c36081dSBruce Richardson /* a variant of "single" command. useful to test autocomplete */
840c36081dSBruce Richardson
850c36081dSBruce Richardson struct cmd_single_long_result {
860c36081dSBruce Richardson cmdline_fixed_string_t single_long;
870c36081dSBruce Richardson };
880c36081dSBruce Richardson
890c36081dSBruce Richardson static void
cmd_single_long_parsed(__rte_unused void * parsed_result,struct cmdline * cl,__rte_unused void * data)90f2fc83b4SThomas Monjalon cmd_single_long_parsed(__rte_unused void *parsed_result,
910c36081dSBruce Richardson struct cmdline *cl,
92f2fc83b4SThomas Monjalon __rte_unused void *data)
930c36081dSBruce Richardson {
940c36081dSBruce Richardson cmdline_printf(cl, "Single long word command parsed!\n");
950c36081dSBruce Richardson }
960c36081dSBruce Richardson
970c36081dSBruce Richardson cmdline_parse_token_string_t cmd_single_long_tok =
980c36081dSBruce Richardson TOKEN_STRING_INITIALIZER(struct cmd_single_long_result, single_long,
990c36081dSBruce Richardson "single_long");
1000c36081dSBruce Richardson
1010c36081dSBruce Richardson cmdline_parse_inst_t cmd_single_long = {
1020c36081dSBruce Richardson .f = cmd_single_long_parsed, /* function to call */
1030c36081dSBruce Richardson .data = NULL, /* 2nd arg of func */
1040c36081dSBruce Richardson .help_str = "a variant of \"single\" command, useful to test autocomplete",
1050c36081dSBruce Richardson .tokens = { /* token list, NULL terminated */
1060c36081dSBruce Richardson (void *)&cmd_single_long_tok,
1070c36081dSBruce Richardson NULL,
1080c36081dSBruce Richardson },
1090c36081dSBruce Richardson };
1100c36081dSBruce Richardson
1110c36081dSBruce Richardson
1120c36081dSBruce Richardson
1130c36081dSBruce Richardson /*** autocomplete_1 ***/
1140c36081dSBruce Richardson /* first command to test autocomplete when multiple commands have chars
1150c36081dSBruce Richardson * in common but none should complete due to ambiguity
1160c36081dSBruce Richardson */
1170c36081dSBruce Richardson
1180c36081dSBruce Richardson struct cmd_autocomplete_1_result {
1190c36081dSBruce Richardson cmdline_fixed_string_t token;
1200c36081dSBruce Richardson };
1210c36081dSBruce Richardson
1220c36081dSBruce Richardson static void
cmd_autocomplete_1_parsed(__rte_unused void * parsed_result,struct cmdline * cl,__rte_unused void * data)123f2fc83b4SThomas Monjalon cmd_autocomplete_1_parsed(__rte_unused void *parsed_result,
1240c36081dSBruce Richardson struct cmdline *cl,
125f2fc83b4SThomas Monjalon __rte_unused void *data)
1260c36081dSBruce Richardson {
1270c36081dSBruce Richardson cmdline_printf(cl, "Autocomplete command 1 parsed!\n");
1280c36081dSBruce Richardson }
1290c36081dSBruce Richardson
1300c36081dSBruce Richardson cmdline_parse_token_string_t cmd_autocomplete_1_tok =
1310c36081dSBruce Richardson TOKEN_STRING_INITIALIZER(struct cmd_autocomplete_1_result, token,
1320c36081dSBruce Richardson "autocomplete_1");
1330c36081dSBruce Richardson
1340c36081dSBruce Richardson cmdline_parse_inst_t cmd_autocomplete_1 = {
1350c36081dSBruce Richardson .f = cmd_autocomplete_1_parsed, /* function to call */
1360c36081dSBruce Richardson .data = NULL, /* 2nd arg of func */
1370c36081dSBruce Richardson .help_str = "first ambiguous autocomplete command",
1380c36081dSBruce Richardson .tokens = { /* token list, NULL terminated */
1390c36081dSBruce Richardson (void *)&cmd_autocomplete_1_tok,
1400c36081dSBruce Richardson NULL,
1410c36081dSBruce Richardson },
1420c36081dSBruce Richardson };
1430c36081dSBruce Richardson
1440c36081dSBruce Richardson
1450c36081dSBruce Richardson
1460c36081dSBruce Richardson /*** autocomplete_2 ***/
1470c36081dSBruce Richardson /* second command to test autocomplete when multiple commands have chars
1480c36081dSBruce Richardson * in common but none should complete due to ambiguity
1490c36081dSBruce Richardson */
1500c36081dSBruce Richardson
1510c36081dSBruce Richardson struct cmd_autocomplete_2_result {
1520c36081dSBruce Richardson cmdline_fixed_string_t token;
1530c36081dSBruce Richardson };
1540c36081dSBruce Richardson
1550c36081dSBruce Richardson static void
cmd_autocomplete_2_parsed(__rte_unused void * parsed_result,struct cmdline * cl,__rte_unused void * data)156f2fc83b4SThomas Monjalon cmd_autocomplete_2_parsed(__rte_unused void *parsed_result,
1570c36081dSBruce Richardson struct cmdline *cl,
158f2fc83b4SThomas Monjalon __rte_unused void *data)
1590c36081dSBruce Richardson {
1600c36081dSBruce Richardson cmdline_printf(cl, "Autocomplete command 2 parsed!\n");
1610c36081dSBruce Richardson }
1620c36081dSBruce Richardson
1630c36081dSBruce Richardson cmdline_parse_token_string_t cmd_autocomplete_2_tok =
1640c36081dSBruce Richardson TOKEN_STRING_INITIALIZER(struct cmd_autocomplete_2_result, token,
1650c36081dSBruce Richardson "autocomplete_2");
1660c36081dSBruce Richardson
1670c36081dSBruce Richardson cmdline_parse_inst_t cmd_autocomplete_2 = {
1680c36081dSBruce Richardson .f = cmd_autocomplete_2_parsed, /* function to call */
1690c36081dSBruce Richardson .data = NULL, /* 2nd arg of func */
1700c36081dSBruce Richardson .help_str = "second ambiguous autocomplete command",
1710c36081dSBruce Richardson .tokens = { /* token list, NULL terminated */
1720c36081dSBruce Richardson (void *)&cmd_autocomplete_2_tok,
1730c36081dSBruce Richardson NULL,
1740c36081dSBruce Richardson },
1750c36081dSBruce Richardson };
1760c36081dSBruce Richardson
1770c36081dSBruce Richardson
1780c36081dSBruce Richardson
1790c36081dSBruce Richardson /*** number command ***/
1800c36081dSBruce Richardson /* a command that simply returns whatever (uint32) number is supplied to it */
1810c36081dSBruce Richardson
1820c36081dSBruce Richardson struct cmd_num_result {
1830c36081dSBruce Richardson unsigned num;
1840c36081dSBruce Richardson };
1850c36081dSBruce Richardson
1860c36081dSBruce Richardson static void
cmd_num_parsed(void * parsed_result,struct cmdline * cl,__rte_unused void * data)1870c36081dSBruce Richardson cmd_num_parsed(void *parsed_result,
1880c36081dSBruce Richardson struct cmdline *cl,
189f2fc83b4SThomas Monjalon __rte_unused void *data)
1900c36081dSBruce Richardson {
1910c36081dSBruce Richardson unsigned result = ((struct cmd_num_result*)parsed_result)->num;
1920c36081dSBruce Richardson cmdline_printf(cl, "%u\n", result);
1930c36081dSBruce Richardson }
1940c36081dSBruce Richardson
1950c36081dSBruce Richardson cmdline_parse_token_num_t cmd_num_tok =
196c2341bb6SDmitry Kozlyuk TOKEN_NUM_INITIALIZER(struct cmd_num_result, num, RTE_UINT32);
1970c36081dSBruce Richardson
1980c36081dSBruce Richardson cmdline_parse_inst_t cmd_num = {
1990c36081dSBruce Richardson .f = cmd_num_parsed, /* function to call */
2000c36081dSBruce Richardson .data = NULL, /* 2nd arg of func */
2010c36081dSBruce Richardson .help_str = "a command that simply returns whatever number is entered",
2020c36081dSBruce Richardson .tokens = { /* token list, NULL terminated */
2030c36081dSBruce Richardson (void *)&cmd_num_tok,
2040c36081dSBruce Richardson NULL,
2050c36081dSBruce Richardson },
2060c36081dSBruce Richardson };
2070c36081dSBruce Richardson
2080c36081dSBruce Richardson
2090c36081dSBruce Richardson
2100c36081dSBruce Richardson /*** ambiguous first|ambiguous ***/
2110c36081dSBruce Richardson /* first command used to test command ambiguity */
2120c36081dSBruce Richardson
2130c36081dSBruce Richardson struct cmd_ambig_result_1 {
2140c36081dSBruce Richardson cmdline_fixed_string_t common_part;
2150c36081dSBruce Richardson cmdline_fixed_string_t ambig_part;
2160c36081dSBruce Richardson };
2170c36081dSBruce Richardson
2180c36081dSBruce Richardson static void
cmd_ambig_1_parsed(__rte_unused void * parsed_result,struct cmdline * cl,__rte_unused void * data)219f2fc83b4SThomas Monjalon cmd_ambig_1_parsed(__rte_unused void *parsed_result,
2200c36081dSBruce Richardson struct cmdline *cl,
221f2fc83b4SThomas Monjalon __rte_unused void *data)
2220c36081dSBruce Richardson {
2230c36081dSBruce Richardson cmdline_printf(cl, "Command 1 parsed!\n");
2240c36081dSBruce Richardson }
2250c36081dSBruce Richardson
2260c36081dSBruce Richardson cmdline_parse_token_string_t cmd_ambig_common_1 =
2270c36081dSBruce Richardson TOKEN_STRING_INITIALIZER(struct cmd_ambig_result_1, common_part,
2280c36081dSBruce Richardson "ambiguous");
2290c36081dSBruce Richardson cmdline_parse_token_string_t cmd_ambig_ambig_1 =
2300c36081dSBruce Richardson TOKEN_STRING_INITIALIZER(struct cmd_ambig_result_1, ambig_part,
2310c36081dSBruce Richardson "first#ambiguous#ambiguous2");
2320c36081dSBruce Richardson
2330c36081dSBruce Richardson cmdline_parse_inst_t cmd_ambig_1 = {
2340c36081dSBruce Richardson .f = cmd_ambig_1_parsed, /* function to call */
2350c36081dSBruce Richardson .data = NULL, /* 2nd arg of func */
2360c36081dSBruce Richardson .help_str = "first command used to test command ambiguity",
2370c36081dSBruce Richardson .tokens = { /* token list, NULL terminated */
2380c36081dSBruce Richardson (void *)&cmd_ambig_common_1,
2390c36081dSBruce Richardson (void*)&cmd_ambig_ambig_1,
2400c36081dSBruce Richardson NULL,
2410c36081dSBruce Richardson },
2420c36081dSBruce Richardson };
2430c36081dSBruce Richardson
2440c36081dSBruce Richardson
2450c36081dSBruce Richardson
2460c36081dSBruce Richardson /*** ambiguous second|ambiguous ***/
2470c36081dSBruce Richardson /* second command used to test command ambiguity */
2480c36081dSBruce Richardson
2490c36081dSBruce Richardson struct cmd_ambig_result_2 {
2500c36081dSBruce Richardson cmdline_fixed_string_t common_part;
2510c36081dSBruce Richardson cmdline_fixed_string_t ambig_part;
2520c36081dSBruce Richardson };
2530c36081dSBruce Richardson
2540c36081dSBruce Richardson static void
cmd_ambig_2_parsed(__rte_unused void * parsed_result,struct cmdline * cl,__rte_unused void * data)255f2fc83b4SThomas Monjalon cmd_ambig_2_parsed(__rte_unused void *parsed_result,
2560c36081dSBruce Richardson struct cmdline *cl,
257f2fc83b4SThomas Monjalon __rte_unused void *data)
2580c36081dSBruce Richardson {
2590c36081dSBruce Richardson cmdline_printf(cl, "Command 2 parsed!\n");
2600c36081dSBruce Richardson }
2610c36081dSBruce Richardson
2620c36081dSBruce Richardson cmdline_parse_token_string_t cmd_ambig_common_2 =
2630c36081dSBruce Richardson TOKEN_STRING_INITIALIZER(struct cmd_ambig_result_2, common_part,
2640c36081dSBruce Richardson "ambiguous");
2650c36081dSBruce Richardson cmdline_parse_token_string_t cmd_ambig_ambig_2 =
2660c36081dSBruce Richardson TOKEN_STRING_INITIALIZER(struct cmd_ambig_result_2, ambig_part,
2670c36081dSBruce Richardson "second#ambiguous#ambiguous2");
2680c36081dSBruce Richardson
2690c36081dSBruce Richardson cmdline_parse_inst_t cmd_ambig_2 = {
2700c36081dSBruce Richardson .f = cmd_ambig_2_parsed, /* function to call */
2710c36081dSBruce Richardson .data = NULL, /* 2nd arg of func */
2720c36081dSBruce Richardson .help_str = "second command used to test command ambiguity",
2730c36081dSBruce Richardson .tokens = { /* token list, NULL terminated */
2740c36081dSBruce Richardson (void *)&cmd_ambig_common_2,
2750c36081dSBruce Richardson (void*)&cmd_ambig_ambig_2,
2760c36081dSBruce Richardson NULL,
2770c36081dSBruce Richardson },
2780c36081dSBruce Richardson };
2790c36081dSBruce Richardson
2800c36081dSBruce Richardson
2810c36081dSBruce Richardson
2820c36081dSBruce Richardson /*** get_history_bufsize ***/
2830c36081dSBruce Richardson /* command that displays total space in history buffer
2840c36081dSBruce Richardson * this will be useful for testing history (to fill it up just enough to
2850c36081dSBruce Richardson * remove the last entry, we need to know how big it is).
2860c36081dSBruce Richardson */
2870c36081dSBruce Richardson
2880c36081dSBruce Richardson struct cmd_get_history_bufsize_result {
2890c36081dSBruce Richardson cmdline_fixed_string_t str;
2900c36081dSBruce Richardson };
2910c36081dSBruce Richardson
2920c36081dSBruce Richardson static void
cmd_get_history_bufsize_parsed(__rte_unused void * parsed_result,struct cmdline * cl,__rte_unused void * data)293f2fc83b4SThomas Monjalon cmd_get_history_bufsize_parsed(__rte_unused void *parsed_result,
2940c36081dSBruce Richardson struct cmdline *cl,
295f2fc83b4SThomas Monjalon __rte_unused void *data)
2960c36081dSBruce Richardson {
29751fcb6a1SDmitry Kozlyuk struct rdline *rdl = cmdline_get_rdline(cl);
29851fcb6a1SDmitry Kozlyuk
2990c36081dSBruce Richardson cmdline_printf(cl, "History buffer size: %zu\n",
300*f8f8dc28SDmitry Kozlyuk rdline_get_history_buffer_size(rdl));
3010c36081dSBruce Richardson }
3020c36081dSBruce Richardson
3030c36081dSBruce Richardson cmdline_parse_token_string_t cmd_get_history_bufsize_tok =
3040c36081dSBruce Richardson TOKEN_STRING_INITIALIZER(struct cmd_get_history_bufsize_result, str,
3050c36081dSBruce Richardson "get_history_bufsize");
3060c36081dSBruce Richardson
3070c36081dSBruce Richardson cmdline_parse_inst_t cmd_get_history_bufsize = {
3080c36081dSBruce Richardson .f = cmd_get_history_bufsize_parsed, /* function to call */
3090c36081dSBruce Richardson .data = NULL, /* 2nd arg of func */
3100c36081dSBruce Richardson .help_str = "command that displays total space in history buffer",
3110c36081dSBruce Richardson .tokens = { /* token list, NULL terminated */
3120c36081dSBruce Richardson (void *)&cmd_get_history_bufsize_tok,
3130c36081dSBruce Richardson NULL,
3140c36081dSBruce Richardson },
3150c36081dSBruce Richardson };
3160c36081dSBruce Richardson
3170c36081dSBruce Richardson
3180c36081dSBruce Richardson
3190c36081dSBruce Richardson /*** clear_history ***/
3200c36081dSBruce Richardson /* clears history buffer */
3210c36081dSBruce Richardson
3220c36081dSBruce Richardson struct cmd_clear_history_result {
3230c36081dSBruce Richardson cmdline_fixed_string_t str;
3240c36081dSBruce Richardson };
3250c36081dSBruce Richardson
3260c36081dSBruce Richardson static void
cmd_clear_history_parsed(__rte_unused void * parsed_result,struct cmdline * cl,__rte_unused void * data)327f2fc83b4SThomas Monjalon cmd_clear_history_parsed(__rte_unused void *parsed_result,
3280c36081dSBruce Richardson struct cmdline *cl,
329f2fc83b4SThomas Monjalon __rte_unused void *data)
3300c36081dSBruce Richardson {
33151fcb6a1SDmitry Kozlyuk struct rdline *rdl = cmdline_get_rdline(cl);
33251fcb6a1SDmitry Kozlyuk
33351fcb6a1SDmitry Kozlyuk rdline_clear_history(rdl);
3340c36081dSBruce Richardson }
3350c36081dSBruce Richardson
3360c36081dSBruce Richardson cmdline_parse_token_string_t cmd_clear_history_tok =
3370c36081dSBruce Richardson TOKEN_STRING_INITIALIZER(struct cmd_clear_history_result, str,
3380c36081dSBruce Richardson "clear_history");
3390c36081dSBruce Richardson
3400c36081dSBruce Richardson cmdline_parse_inst_t cmd_clear_history = {
3410c36081dSBruce Richardson .f = cmd_clear_history_parsed, /* function to call */
3420c36081dSBruce Richardson .data = NULL, /* 2nd arg of func */
3430c36081dSBruce Richardson .help_str = "clear command history",
3440c36081dSBruce Richardson .tokens = { /* token list, NULL terminated */
3450c36081dSBruce Richardson (void *)&cmd_clear_history_tok,
3460c36081dSBruce Richardson NULL,
3470c36081dSBruce Richardson },
3480c36081dSBruce Richardson };
3490c36081dSBruce Richardson
3500c36081dSBruce Richardson
3510c36081dSBruce Richardson
3520c36081dSBruce Richardson /****************/
3530c36081dSBruce Richardson
3540c36081dSBruce Richardson cmdline_parse_ctx_t main_ctx[] = {
3550c36081dSBruce Richardson (cmdline_parse_inst_t *)&cmd_quit,
3560c36081dSBruce Richardson (cmdline_parse_inst_t *)&cmd_ambig_1,
3570c36081dSBruce Richardson (cmdline_parse_inst_t *)&cmd_ambig_2,
3580c36081dSBruce Richardson (cmdline_parse_inst_t *)&cmd_single,
3590c36081dSBruce Richardson (cmdline_parse_inst_t *)&cmd_single_long,
3600c36081dSBruce Richardson (cmdline_parse_inst_t *)&cmd_num,
3610c36081dSBruce Richardson (cmdline_parse_inst_t *)&cmd_get_history_bufsize,
3620c36081dSBruce Richardson (cmdline_parse_inst_t *)&cmd_clear_history,
3630c36081dSBruce Richardson (cmdline_parse_inst_t *)&cmd_autocomplete_1,
3640c36081dSBruce Richardson (cmdline_parse_inst_t *)&cmd_autocomplete_2,
3650c36081dSBruce Richardson NULL,
3660c36081dSBruce Richardson };
367