4 * Copyright(c) 2010-2014 Intel Corporation. All rights reserved.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
11 * * Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * * Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in
15 * the documentation and/or other materials provided with the
17 * * Neither the name of Intel Corporation nor the names of its
18 * contributors may be used to endorse or promote products derived
19 * from this software without specific prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
24 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
25 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
26 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
27 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
28 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
29 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
31 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
39 #include <cmdline_rdline.h>
40 #include <cmdline_parse.h>
41 #include <cmdline_parse_string.h>
42 #include <cmdline_parse_num.h>
45 #include "cmdline_test.h"
48 /* exit application */
50 struct cmd_quit_result {
51 cmdline_fixed_string_t quit;
55 cmd_quit_parsed(__attribute__((unused)) void *parsed_result,
57 __attribute__((unused)) void *data)
62 cmdline_parse_token_string_t cmd_quit_tok =
63 TOKEN_STRING_INITIALIZER(struct cmd_quit_result, quit,
66 cmdline_parse_inst_t cmd_quit = {
67 .f = cmd_quit_parsed, /* function to call */
68 .data = NULL, /* 2nd arg of func */
69 .help_str = "exit application",
70 .tokens = { /* token list, NULL terminated */
71 (void *)&cmd_quit_tok,
79 /* a simple single-word command */
81 struct cmd_single_result {
82 cmdline_fixed_string_t single;
86 cmd_single_parsed(__attribute__((unused)) void *parsed_result,
88 __attribute__((unused)) void *data)
90 cmdline_printf(cl, "Single word command parsed!\n");
93 cmdline_parse_token_string_t cmd_single_tok =
94 TOKEN_STRING_INITIALIZER(struct cmd_single_result, single,
97 cmdline_parse_inst_t cmd_single = {
98 .f = cmd_single_parsed, /* function to call */
99 .data = NULL, /* 2nd arg of func */
100 .help_str = "a simple single-word command",
101 .tokens = { /* token list, NULL terminated */
102 (void *)&cmd_single_tok,
109 /*** single_long ***/
110 /* a variant of "single" command. useful to test autocomplete */
112 struct cmd_single_long_result {
113 cmdline_fixed_string_t single_long;
117 cmd_single_long_parsed(__attribute__((unused)) void *parsed_result,
119 __attribute__((unused)) void *data)
121 cmdline_printf(cl, "Single long word command parsed!\n");
124 cmdline_parse_token_string_t cmd_single_long_tok =
125 TOKEN_STRING_INITIALIZER(struct cmd_single_long_result, single_long,
128 cmdline_parse_inst_t cmd_single_long = {
129 .f = cmd_single_long_parsed, /* function to call */
130 .data = NULL, /* 2nd arg of func */
131 .help_str = "a variant of \"single\" command, useful to test autocomplete",
132 .tokens = { /* token list, NULL terminated */
133 (void *)&cmd_single_long_tok,
140 /*** autocomplete_1 ***/
141 /* first command to test autocomplete when multiple commands have chars
142 * in common but none should complete due to ambiguity
145 struct cmd_autocomplete_1_result {
146 cmdline_fixed_string_t token;
150 cmd_autocomplete_1_parsed(__attribute__((unused)) void *parsed_result,
152 __attribute__((unused)) void *data)
154 cmdline_printf(cl, "Autocomplete command 1 parsed!\n");
157 cmdline_parse_token_string_t cmd_autocomplete_1_tok =
158 TOKEN_STRING_INITIALIZER(struct cmd_autocomplete_1_result, token,
161 cmdline_parse_inst_t cmd_autocomplete_1 = {
162 .f = cmd_autocomplete_1_parsed, /* function to call */
163 .data = NULL, /* 2nd arg of func */
164 .help_str = "first ambiguous autocomplete command",
165 .tokens = { /* token list, NULL terminated */
166 (void *)&cmd_autocomplete_1_tok,
173 /*** autocomplete_2 ***/
174 /* second command to test autocomplete when multiple commands have chars
175 * in common but none should complete due to ambiguity
178 struct cmd_autocomplete_2_result {
179 cmdline_fixed_string_t token;
183 cmd_autocomplete_2_parsed(__attribute__((unused)) void *parsed_result,
185 __attribute__((unused)) void *data)
187 cmdline_printf(cl, "Autocomplete command 2 parsed!\n");
190 cmdline_parse_token_string_t cmd_autocomplete_2_tok =
191 TOKEN_STRING_INITIALIZER(struct cmd_autocomplete_2_result, token,
194 cmdline_parse_inst_t cmd_autocomplete_2 = {
195 .f = cmd_autocomplete_2_parsed, /* function to call */
196 .data = NULL, /* 2nd arg of func */
197 .help_str = "second ambiguous autocomplete command",
198 .tokens = { /* token list, NULL terminated */
199 (void *)&cmd_autocomplete_2_tok,
206 /*** number command ***/
207 /* a command that simply returns whatever (uint32) number is supplied to it */
209 struct cmd_num_result {
214 cmd_num_parsed(void *parsed_result,
216 __attribute__((unused)) void *data)
218 unsigned result = ((struct cmd_num_result*)parsed_result)->num;
219 cmdline_printf(cl, "%u\n", result);
222 cmdline_parse_token_num_t cmd_num_tok =
223 TOKEN_NUM_INITIALIZER(struct cmd_num_result, num, UINT32);
225 cmdline_parse_inst_t cmd_num = {
226 .f = cmd_num_parsed, /* function to call */
227 .data = NULL, /* 2nd arg of func */
228 .help_str = "a command that simply returns whatever number is entered",
229 .tokens = { /* token list, NULL terminated */
230 (void *)&cmd_num_tok,
237 /*** ambiguous first|ambiguous ***/
238 /* first command used to test command ambiguity */
240 struct cmd_ambig_result_1 {
241 cmdline_fixed_string_t common_part;
242 cmdline_fixed_string_t ambig_part;
246 cmd_ambig_1_parsed(__attribute__((unused)) void *parsed_result,
248 __attribute__((unused)) void *data)
250 cmdline_printf(cl, "Command 1 parsed!\n");
253 cmdline_parse_token_string_t cmd_ambig_common_1 =
254 TOKEN_STRING_INITIALIZER(struct cmd_ambig_result_1, common_part,
256 cmdline_parse_token_string_t cmd_ambig_ambig_1 =
257 TOKEN_STRING_INITIALIZER(struct cmd_ambig_result_1, ambig_part,
258 "first#ambiguous#ambiguous2");
260 cmdline_parse_inst_t cmd_ambig_1 = {
261 .f = cmd_ambig_1_parsed, /* function to call */
262 .data = NULL, /* 2nd arg of func */
263 .help_str = "first command used to test command ambiguity",
264 .tokens = { /* token list, NULL terminated */
265 (void *)&cmd_ambig_common_1,
266 (void*)&cmd_ambig_ambig_1,
273 /*** ambiguous second|ambiguous ***/
274 /* second command used to test command ambiguity */
276 struct cmd_ambig_result_2 {
277 cmdline_fixed_string_t common_part;
278 cmdline_fixed_string_t ambig_part;
282 cmd_ambig_2_parsed(__attribute__((unused)) void *parsed_result,
284 __attribute__((unused)) void *data)
286 cmdline_printf(cl, "Command 2 parsed!\n");
289 cmdline_parse_token_string_t cmd_ambig_common_2 =
290 TOKEN_STRING_INITIALIZER(struct cmd_ambig_result_2, common_part,
292 cmdline_parse_token_string_t cmd_ambig_ambig_2 =
293 TOKEN_STRING_INITIALIZER(struct cmd_ambig_result_2, ambig_part,
294 "second#ambiguous#ambiguous2");
296 cmdline_parse_inst_t cmd_ambig_2 = {
297 .f = cmd_ambig_2_parsed, /* function to call */
298 .data = NULL, /* 2nd arg of func */
299 .help_str = "second command used to test command ambiguity",
300 .tokens = { /* token list, NULL terminated */
301 (void *)&cmd_ambig_common_2,
302 (void*)&cmd_ambig_ambig_2,
309 /*** get_history_bufsize ***/
310 /* command that displays total space in history buffer
311 * this will be useful for testing history (to fill it up just enough to
312 * remove the last entry, we need to know how big it is).
315 struct cmd_get_history_bufsize_result {
316 cmdline_fixed_string_t str;
320 cmd_get_history_bufsize_parsed(__attribute__((unused)) void *parsed_result,
322 __attribute__((unused)) void *data)
324 cmdline_printf(cl, "History buffer size: %u\n",
325 sizeof(cl->rdl.history_buf));
328 cmdline_parse_token_string_t cmd_get_history_bufsize_tok =
329 TOKEN_STRING_INITIALIZER(struct cmd_get_history_bufsize_result, str,
330 "get_history_bufsize");
332 cmdline_parse_inst_t cmd_get_history_bufsize = {
333 .f = cmd_get_history_bufsize_parsed, /* function to call */
334 .data = NULL, /* 2nd arg of func */
335 .help_str = "command that displays total space in history buffer",
336 .tokens = { /* token list, NULL terminated */
337 (void *)&cmd_get_history_bufsize_tok,
344 /*** clear_history ***/
345 /* clears history buffer */
347 struct cmd_clear_history_result {
348 cmdline_fixed_string_t str;
352 cmd_clear_history_parsed(__attribute__((unused)) void *parsed_result,
354 __attribute__((unused)) void *data)
356 rdline_clear_history(&cl->rdl);
359 cmdline_parse_token_string_t cmd_clear_history_tok =
360 TOKEN_STRING_INITIALIZER(struct cmd_clear_history_result, str,
363 cmdline_parse_inst_t cmd_clear_history = {
364 .f = cmd_clear_history_parsed, /* function to call */
365 .data = NULL, /* 2nd arg of func */
366 .help_str = "clear command history",
367 .tokens = { /* token list, NULL terminated */
368 (void *)&cmd_clear_history_tok,
377 cmdline_parse_ctx_t main_ctx[] = {
378 (cmdline_parse_inst_t *)&cmd_quit,
379 (cmdline_parse_inst_t *)&cmd_ambig_1,
380 (cmdline_parse_inst_t *)&cmd_ambig_2,
381 (cmdline_parse_inst_t *)&cmd_single,
382 (cmdline_parse_inst_t *)&cmd_single_long,
383 (cmdline_parse_inst_t *)&cmd_num,
384 (cmdline_parse_inst_t *)&cmd_get_history_bufsize,
385 (cmdline_parse_inst_t *)&cmd_clear_history,
386 (cmdline_parse_inst_t *)&cmd_autocomplete_1,
387 (cmdline_parse_inst_t *)&cmd_autocomplete_2,