4 * Copyright(c) 2010-2013 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.
40 #include <cmdline_rdline.h>
41 #include <cmdline_parse.h>
42 #include <cmdline_parse_string.h>
43 #include <cmdline_parse_num.h>
46 #include "cmdline_test.h"
49 /* exit application */
51 struct cmd_quit_result {
52 cmdline_fixed_string_t quit;
56 cmd_quit_parsed(__attribute__((unused)) void *parsed_result,
58 __attribute__((unused)) void *data)
63 cmdline_parse_token_string_t cmd_quit_tok =
64 TOKEN_STRING_INITIALIZER(struct cmd_quit_result, quit,
67 cmdline_parse_inst_t cmd_quit = {
68 .f = cmd_quit_parsed, /* function to call */
69 .data = NULL, /* 2nd arg of func */
70 .help_str = "exit application",
71 .tokens = { /* token list, NULL terminated */
72 (void *)&cmd_quit_tok,
80 /* a simple single-word command */
82 struct cmd_single_result {
83 cmdline_fixed_string_t single;
87 cmd_single_parsed(__attribute__((unused)) void *parsed_result,
89 __attribute__((unused)) void *data)
91 cmdline_printf(cl, "Single word command parsed!\n");
94 cmdline_parse_token_string_t cmd_single_tok =
95 TOKEN_STRING_INITIALIZER(struct cmd_single_result, single,
98 cmdline_parse_inst_t cmd_single = {
99 .f = cmd_single_parsed, /* function to call */
100 .data = NULL, /* 2nd arg of func */
101 .help_str = "a simple single-word command",
102 .tokens = { /* token list, NULL terminated */
103 (void *)&cmd_single_tok,
110 /*** single_long ***/
111 /* a variant of "single" command. useful to test autocomplete */
113 struct cmd_single_long_result {
114 cmdline_fixed_string_t single_long;
118 cmd_single_long_parsed(__attribute__((unused)) void *parsed_result,
120 __attribute__((unused)) void *data)
122 cmdline_printf(cl, "Single long word command parsed!\n");
125 cmdline_parse_token_string_t cmd_single_long_tok =
126 TOKEN_STRING_INITIALIZER(struct cmd_single_long_result, single_long,
129 cmdline_parse_inst_t cmd_single_long = {
130 .f = cmd_single_long_parsed, /* function to call */
131 .data = NULL, /* 2nd arg of func */
132 .help_str = "a variant of \"single\" command, useful to test autocomplete",
133 .tokens = { /* token list, NULL terminated */
134 (void *)&cmd_single_long_tok,
141 /*** autocomplete_1 ***/
142 /* first command to test autocomplete when multiple commands have chars
143 * in common but none should complete due to ambiguity
146 struct cmd_autocomplete_1_result {
147 cmdline_fixed_string_t token;
151 cmd_autocomplete_1_parsed(__attribute__((unused)) void *parsed_result,
153 __attribute__((unused)) void *data)
155 cmdline_printf(cl, "Autocomplete command 1 parsed!\n");
158 cmdline_parse_token_string_t cmd_autocomplete_1_tok =
159 TOKEN_STRING_INITIALIZER(struct cmd_autocomplete_1_result, token,
162 cmdline_parse_inst_t cmd_autocomplete_1 = {
163 .f = cmd_autocomplete_1_parsed, /* function to call */
164 .data = NULL, /* 2nd arg of func */
165 .help_str = "first ambiguous autocomplete command",
166 .tokens = { /* token list, NULL terminated */
167 (void *)&cmd_autocomplete_1_tok,
174 /*** autocomplete_2 ***/
175 /* second command to test autocomplete when multiple commands have chars
176 * in common but none should complete due to ambiguity
179 struct cmd_autocomplete_2_result {
180 cmdline_fixed_string_t token;
184 cmd_autocomplete_2_parsed(__attribute__((unused)) void *parsed_result,
186 __attribute__((unused)) void *data)
188 cmdline_printf(cl, "Autocomplete command 2 parsed!\n");
191 cmdline_parse_token_string_t cmd_autocomplete_2_tok =
192 TOKEN_STRING_INITIALIZER(struct cmd_autocomplete_2_result, token,
195 cmdline_parse_inst_t cmd_autocomplete_2 = {
196 .f = cmd_autocomplete_2_parsed, /* function to call */
197 .data = NULL, /* 2nd arg of func */
198 .help_str = "second ambiguous autocomplete command",
199 .tokens = { /* token list, NULL terminated */
200 (void *)&cmd_autocomplete_2_tok,
207 /*** number command ***/
208 /* a command that simply returns whatever (uint32) number is supplied to it */
210 struct cmd_num_result {
215 cmd_num_parsed(void *parsed_result,
217 __attribute__((unused)) void *data)
219 unsigned result = ((struct cmd_num_result*)parsed_result)->num;
220 cmdline_printf(cl, "%u\n", result);
223 cmdline_parse_token_num_t cmd_num_tok =
224 TOKEN_NUM_INITIALIZER(struct cmd_num_result, num, UINT32);
226 cmdline_parse_inst_t cmd_num = {
227 .f = cmd_num_parsed, /* function to call */
228 .data = NULL, /* 2nd arg of func */
229 .help_str = "a command that simply returns whatever number is entered",
230 .tokens = { /* token list, NULL terminated */
231 (void *)&cmd_num_tok,
238 /*** ambiguous first|ambiguous ***/
239 /* first command used to test command ambiguity */
241 struct cmd_ambig_result_1 {
242 cmdline_fixed_string_t common_part;
243 cmdline_fixed_string_t ambig_part;
247 cmd_ambig_1_parsed(__attribute__((unused)) void *parsed_result,
249 __attribute__((unused)) void *data)
251 cmdline_printf(cl, "Command 1 parsed!\n");
254 cmdline_parse_token_string_t cmd_ambig_common_1 =
255 TOKEN_STRING_INITIALIZER(struct cmd_ambig_result_1, common_part,
257 cmdline_parse_token_string_t cmd_ambig_ambig_1 =
258 TOKEN_STRING_INITIALIZER(struct cmd_ambig_result_1, ambig_part,
259 "first#ambiguous#ambiguous2");
261 cmdline_parse_inst_t cmd_ambig_1 = {
262 .f = cmd_ambig_1_parsed, /* function to call */
263 .data = NULL, /* 2nd arg of func */
264 .help_str = "first command used to test command ambiguity",
265 .tokens = { /* token list, NULL terminated */
266 (void *)&cmd_ambig_common_1,
267 (void*)&cmd_ambig_ambig_1,
274 /*** ambiguous second|ambiguous ***/
275 /* second command used to test command ambiguity */
277 struct cmd_ambig_result_2 {
278 cmdline_fixed_string_t common_part;
279 cmdline_fixed_string_t ambig_part;
283 cmd_ambig_2_parsed(__attribute__((unused)) void *parsed_result,
285 __attribute__((unused)) void *data)
287 cmdline_printf(cl, "Command 2 parsed!\n");
290 cmdline_parse_token_string_t cmd_ambig_common_2 =
291 TOKEN_STRING_INITIALIZER(struct cmd_ambig_result_2, common_part,
293 cmdline_parse_token_string_t cmd_ambig_ambig_2 =
294 TOKEN_STRING_INITIALIZER(struct cmd_ambig_result_2, ambig_part,
295 "second#ambiguous#ambiguous2");
297 cmdline_parse_inst_t cmd_ambig_2 = {
298 .f = cmd_ambig_2_parsed, /* function to call */
299 .data = NULL, /* 2nd arg of func */
300 .help_str = "second command used to test command ambiguity",
301 .tokens = { /* token list, NULL terminated */
302 (void *)&cmd_ambig_common_2,
303 (void*)&cmd_ambig_ambig_2,
310 /*** get_history_bufsize ***/
311 /* command that displays total space in history buffer
312 * this will be useful for testing history (to fill it up just enough to
313 * remove the last entry, we need to know how big it is).
316 struct cmd_get_history_bufsize_result {
317 cmdline_fixed_string_t str;
321 cmd_get_history_bufsize_parsed(__attribute__((unused)) void *parsed_result,
323 __attribute__((unused)) void *data)
325 cmdline_printf(cl, "History buffer size: %u\n",
326 sizeof(cl->rdl.history_buf));
329 cmdline_parse_token_string_t cmd_get_history_bufsize_tok =
330 TOKEN_STRING_INITIALIZER(struct cmd_get_history_bufsize_result, str,
331 "get_history_bufsize");
333 cmdline_parse_inst_t cmd_get_history_bufsize = {
334 .f = cmd_get_history_bufsize_parsed, /* function to call */
335 .data = NULL, /* 2nd arg of func */
336 .help_str = "command that displays total space in history buffer",
337 .tokens = { /* token list, NULL terminated */
338 (void *)&cmd_get_history_bufsize_tok,
345 /*** clear_history ***/
346 /* clears history buffer */
348 struct cmd_clear_history_result {
349 cmdline_fixed_string_t str;
353 cmd_clear_history_parsed(__attribute__((unused)) void *parsed_result,
355 __attribute__((unused)) void *data)
357 rdline_clear_history(&cl->rdl);
360 cmdline_parse_token_string_t cmd_clear_history_tok =
361 TOKEN_STRING_INITIALIZER(struct cmd_clear_history_result, str,
364 cmdline_parse_inst_t cmd_clear_history = {
365 .f = cmd_clear_history_parsed, /* function to call */
366 .data = NULL, /* 2nd arg of func */
367 .help_str = "clear command history",
368 .tokens = { /* token list, NULL terminated */
369 (void *)&cmd_clear_history_tok,
378 cmdline_parse_ctx_t main_ctx[] = {
379 (cmdline_parse_inst_t *)&cmd_quit,
380 (cmdline_parse_inst_t *)&cmd_ambig_1,
381 (cmdline_parse_inst_t *)&cmd_ambig_2,
382 (cmdline_parse_inst_t *)&cmd_single,
383 (cmdline_parse_inst_t *)&cmd_single_long,
384 (cmdline_parse_inst_t *)&cmd_num,
385 (cmdline_parse_inst_t *)&cmd_get_history_bufsize,
386 (cmdline_parse_inst_t *)&cmd_clear_history,
387 (cmdline_parse_inst_t *)&cmd_autocomplete_1,
388 (cmdline_parse_inst_t *)&cmd_autocomplete_2,