2 * Copyright (c) 2009, 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.
33 #include <netinet/in.h>
35 #include <cmdline_parse.h>
36 #include <cmdline_parse_num.h>
37 #include <cmdline_parse_string.h>
42 /**********************************************************/
43 /* operations on float */
45 /* this structure is filled when cmd_float is parsed successfully */
46 struct cmd_float_result {
48 cmdline_fixed_string_t op;
52 /* function called when cmd_float is parsed successfully */
53 static void cmd_float_parsed(void *parsed_result, struct cmdline *cl, void *data)
55 struct cmd_float_result *cmd = (struct cmd_float_result *) parsed_result;
59 case '+': res = cmd->a + cmd->b; break;
60 case '-': res = cmd->a - cmd->b; break;
61 case '*': res = cmd->a * cmd->b; break;
62 case '/': res = cmd->a / cmd->b; break;
65 cmdline_printf(cl, "%f\n", res);
68 cmdline_parse_token_num_t cmd_float_a = TOKEN_NUM_INITIALIZER(struct cmd_float_result, a, FLOAT);
69 cmdline_parse_token_string_t cmd_float_op = TOKEN_STRING_INITIALIZER(struct cmd_float_result, op, "+#-#*#/");
70 cmdline_parse_token_num_t cmd_float_b = TOKEN_NUM_INITIALIZER(struct cmd_float_result, b, FLOAT);
72 cmdline_parse_inst_t cmd_float = {
73 .f = cmd_float_parsed, /* function to call */
74 .data = NULL, /* 2nd arg of func */
75 .help_str = "Operation on float (ex: '2 + 5.4')",
76 .tokens = { /* token list, NULL terminated */
78 (void *)&cmd_float_op,
85 /**********************************************************/
86 /* operations trigo */
88 /* this structure is filled when cmd_trigo is parsed successfully */
89 struct cmd_trigo_result {
90 cmdline_fixed_string_t op;
94 /* function called when cmd_trigo is parsed successfully */
95 static void cmd_trigo_parsed(void *parsed_result, struct cmdline *cl, void *data)
97 struct cmd_trigo_result *cmd = (struct cmd_trigo_result *) parsed_result;
100 if (!strcmp(cmd->op, "sin")) {
104 else if (!strcmp(cmd->op, "cos")) {
108 else if (!strcmp(cmd->op, "tan")) {
112 cmdline_printf(cl, "%f\n", res);
115 cmdline_parse_token_string_t cmd_trigo_op = TOKEN_STRING_INITIALIZER(struct cmd_trigo_result, op, "sin#tan#cos");
116 cmdline_parse_token_num_t cmd_trigo_a = TOKEN_NUM_INITIALIZER(struct cmd_trigo_result, a, FLOAT);
118 cmdline_parse_inst_t cmd_trigo = {
119 .f = cmd_trigo_parsed, /* function to call */
120 .data = NULL, /* 2nd arg of func */
121 .help_str = "Trigonometric operations (ex: 'sin 2.03')",
122 .tokens = { /* token list, NULL terminated */
123 (void *)&cmd_trigo_op,
124 (void *)&cmd_trigo_a,
130 /**********************************************************/
133 /* this structure is filled when cmd_help is parsed successfully */
134 struct cmd_help_result {
135 cmdline_fixed_string_t arg0;
138 /* function called when cmd_help is parsed successfully */
139 static void cmd_help_parsed(void *parsed_result, struct cmdline *cl, void *data)
141 cmdline_printf(cl, "== Simple calculator program ==\n"
142 "You can do simple operations on floats, like '1 + 3'\n"
143 "or '4.4 * 2.' (space is important).\n"
144 "Some trigonometric operations are available, like\n"
148 cmdline_parse_token_string_t cmd_help_arg0 = TOKEN_STRING_INITIALIZER(struct cmd_help_result, arg0, "help");
150 cmdline_parse_inst_t cmd_help = {
151 .f = cmd_help_parsed, /* function to call */
152 .data = NULL, /* 2nd arg of func */
153 .help_str = "Display help",
154 .tokens = { /* token list, NULL terminated */
155 (void *)&cmd_help_arg0,
163 /**********************************************************/
164 /**********************************************************/
165 /****** CONTEXT (list of instruction) */
168 cmdline_parse_ctx_t main_ctx = {