initial revision
[ucgine.git] / examples / test-cmd / commands.c
1 /*
2  * Copyright (c) 2015, Olivier MATZ <zer0@droids-corp.org>
3  * All rights reserved.
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions are met:
6  *
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.
15  *
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.
26  */
27
28 #include <stdio.h>
29 #include <stdint.h>
30 #include <string.h>
31 #include <stdlib.h>
32 #include <stdarg.h>
33
34 #include <ucg_cmd_parse.h>
35 #include <ucg_cmd_parse_num.h>
36 #include <ucg_cmd_parse_string.h>
37 #include <ucg_cmd.h>
38
39 /**********************************************************/
40
41 struct cmd_hello_result {
42         ucg_cmd_fixed_string_t hello;
43         ucg_cmd_fixed_string_t name;
44 };
45
46 static void cmd_hello_parsed(void *parsed_result,
47         struct ucg_cmd *cl, void *data)
48 {
49         struct cmd_hello_result *res = parsed_result;
50
51         (void)data;
52         ucg_cmd_printf(cl, "hello %s\n", res->name);
53 }
54
55 static ucg_cmd_tk_string_t cmd_hello_hello =
56         UCG_CMD_TK_STRING(struct cmd_hello_result, hello, "hello");
57
58 static ucg_cmd_tk_string_t cmd_hello_name =
59         UCG_CMD_TK_STRING(struct cmd_hello_result, name, NULL);
60
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,
68                 NULL,
69         },
70 };
71
72 /**********************************************************/
73
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;
78 };
79
80 static void cmd_hello_tutu_parsed(void *parsed_result,
81         struct ucg_cmd *cl, void *data)
82 {
83         struct cmd_hello_tutu_result *res = parsed_result;
84
85         (void)data;
86         ucg_cmd_printf(cl, "hello %s and %s\n", res->name1, res->name2);
87 }
88
89 static ucg_cmd_tk_string_t cmd_hello_tutu_hello =
90         UCG_CMD_TK_STRING(struct cmd_hello_tutu_result, hello, "hello");
91
92 static ucg_cmd_tk_string_t cmd_hello_tutu_name1 =
93         UCG_CMD_TK_STRING(struct cmd_hello_tutu_result, name1, "tutu");
94
95 static ucg_cmd_tk_string_t cmd_hello_tutu_name2 =
96         UCG_CMD_TK_STRING(struct cmd_hello_tutu_result, name2, NULL);
97
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,
106                 NULL,
107         },
108 };
109
110 /**********************************************************/
111
112 struct cmd_hello_toto_result {
113         ucg_cmd_fixed_string_t hello;
114         ucg_cmd_fixed_string_t name;
115         uint16_t count;
116 };
117
118 static void cmd_hello_toto_parsed(void *parsed_result,
119         struct ucg_cmd *cl, void *data)
120 {
121         uint16_t i;
122         struct cmd_hello_toto_result *res = parsed_result;
123
124         (void)data;
125         for (i = 0; i < res->count; i++)
126                 ucg_cmd_printf(cl, "hello %s\n", res->name);
127 }
128
129 static ucg_cmd_tk_string_t cmd_hello_toto_hello =
130         UCG_CMD_TK_STRING(struct cmd_hello_toto_result, hello, "hello");
131
132 static ucg_cmd_tk_string_t cmd_hello_toto_name =
133         UCG_CMD_TK_STRING(struct cmd_hello_toto_result, name, "toto#titi");
134
135 static ucg_cmd_tk_num_t cmd_hello_toto_count =
136         UCG_CMD_TK_NUM(struct cmd_hello_toto_result, count, UINT16);
137
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,
146                 NULL,
147         },
148 };
149
150 /****** CONTEXT (list of instruction) */
151
152 ucg_cmd_ctx_t main_ctx = {
153         .name = "main",
154         .insts = {
155                 &cmd_hello,
156                 &cmd_hello_tutu,
157                 &cmd_hello_toto,
158                 NULL,
159         },
160 };