2 * Copyright (c) 2015, Olivier MATZ <zer0@droids-corp.org>
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions are met:
7 * * Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer.
9 * * Redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution.
12 * * Neither the name of the University of California, Berkeley nor the
13 * names of its contributors may be used to endorse or promote products
14 * derived from this software without specific prior written permission.
16 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND ANY
17 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
18 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
19 * DISCLAIMED. IN NO EVENT SHALL THE REGENTS AND CONTRIBUTORS BE LIABLE FOR ANY
20 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
21 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
22 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
23 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
25 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
34 #include <ucg_cmd_parse.h>
35 #include <ucg_cmd_parse_num.h>
36 #include <ucg_cmd_parse_string.h>
39 /**********************************************************/
41 struct cmd_hello_result {
42 ucg_cmd_fixed_string_t hello;
43 ucg_cmd_fixed_string_t name;
46 static void cmd_hello_parsed(void *parsed_result,
47 struct ucg_cmd *cl, void *data)
49 struct cmd_hello_result *res = parsed_result;
52 ucg_cmd_printf(cl, "hello %s\n", res->name);
55 static ucg_cmd_tk_string_t cmd_hello_hello =
56 UCG_CMD_TK_STRING(struct cmd_hello_result, hello, "hello");
58 static ucg_cmd_tk_string_t cmd_hello_name =
59 UCG_CMD_TK_STRING(struct cmd_hello_result, name, NULL);
61 static ucg_cmd_inst_t cmd_hello = {
62 .f = cmd_hello_parsed, /* function to call */
63 .data = NULL, /* 2nd arg of func */
64 .help_str = "Say hello",
65 .tokens = { /* token list, NULL terminated */
66 (void *)&cmd_hello_hello,
67 (void *)&cmd_hello_name,
72 /**********************************************************/
74 struct cmd_hello_tutu_result {
75 ucg_cmd_fixed_string_t hello;
76 ucg_cmd_fixed_string_t name1;
77 ucg_cmd_fixed_string_t name2;
80 static void cmd_hello_tutu_parsed(void *parsed_result,
81 struct ucg_cmd *cl, void *data)
83 struct cmd_hello_tutu_result *res = parsed_result;
86 ucg_cmd_printf(cl, "hello %s and %s\n", res->name1, res->name2);
89 static ucg_cmd_tk_string_t cmd_hello_tutu_hello =
90 UCG_CMD_TK_STRING(struct cmd_hello_tutu_result, hello, "hello");
92 static ucg_cmd_tk_string_t cmd_hello_tutu_name1 =
93 UCG_CMD_TK_STRING(struct cmd_hello_tutu_result, name1, "tutu");
95 static ucg_cmd_tk_string_t cmd_hello_tutu_name2 =
96 UCG_CMD_TK_STRING(struct cmd_hello_tutu_result, name2, NULL);
98 static ucg_cmd_inst_t cmd_hello_tutu = {
99 .f = cmd_hello_tutu_parsed, /* function to call */
100 .data = NULL, /* 2nd arg of func */
101 .help_str = "Say hello to tutu and someone else",
102 .tokens = { /* token list, NULL terminated */
103 (void *)&cmd_hello_tutu_hello,
104 (void *)&cmd_hello_tutu_name1,
105 (void *)&cmd_hello_tutu_name2,
110 /**********************************************************/
112 struct cmd_hello_toto_result {
113 ucg_cmd_fixed_string_t hello;
114 ucg_cmd_fixed_string_t name;
118 static void cmd_hello_toto_parsed(void *parsed_result,
119 struct ucg_cmd *cl, void *data)
122 struct cmd_hello_toto_result *res = parsed_result;
125 for (i = 0; i < res->count; i++)
126 ucg_cmd_printf(cl, "hello %s\n", res->name);
129 static ucg_cmd_tk_string_t cmd_hello_toto_hello =
130 UCG_CMD_TK_STRING(struct cmd_hello_toto_result, hello, "hello");
132 static ucg_cmd_tk_string_t cmd_hello_toto_name =
133 UCG_CMD_TK_STRING(struct cmd_hello_toto_result, name, "toto#titi");
135 static ucg_cmd_tk_num_t cmd_hello_toto_count =
136 UCG_CMD_TK_NUM(struct cmd_hello_toto_result, count, UINT16);
138 static ucg_cmd_inst_t cmd_hello_toto = {
139 .f = cmd_hello_toto_parsed, /* function to call */
140 .data = NULL, /* 2nd arg of func */
141 .help_str = "Say hello to toto or titi several times",
142 .tokens = { /* token list, NULL terminated */
143 (void *)&cmd_hello_toto_hello,
144 (void *)&cmd_hello_toto_name,
145 (void *)&cmd_hello_toto_count,
150 /****** CONTEXT (list of instruction) */
152 ucg_cmd_ctx_t main_ctx = {