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.
32 #include <netinet/in.h>
34 #include <cmdline_parse.h>
35 #include <cmdline_parse_num.h>
36 #include <cmdline_parse_string.h>
41 /**********************************************************/
42 /* operations on float */
44 /* this structure is filled when cmd_float is parsed successfully */
45 struct cmd_float_result {
47 cmdline_fixed_string_t op;
51 /* function called when cmd_float is parsed successfully */
52 static void cmd_float_parsed(void *parsed_result, struct cmdline *cl, void *data)
54 struct cmd_float_result *cmd = (struct cmd_float_result *) parsed_result;
58 case '+': res = cmd->a + cmd->b; break;
59 case '-': res = cmd->a - cmd->b; break;
60 case '*': res = cmd->a * cmd->b; break;
61 case '/': res = cmd->a / cmd->b; break;
64 cmdline_printf(cl, "%f\n", res);
67 cmdline_parse_token_num_t cmd_float_a = TOKEN_NUM_INITIALIZER(struct cmd_float_result, a, FLOAT);
68 cmdline_parse_token_string_t cmd_float_op = TOKEN_STRING_INITIALIZER(struct cmd_float_result, op, "+#-#*#/");
69 cmdline_parse_token_num_t cmd_float_b = TOKEN_NUM_INITIALIZER(struct cmd_float_result, b, FLOAT);
71 cmdline_parse_inst_t cmd_float = {
72 .f = cmd_float_parsed, /* function to call */
73 .data = NULL, /* 2nd arg of func */
74 .help_str = "Operation on float (ex: '2 + 5.4')",
75 .tokens = { /* token list, NULL terminated */
77 (void *)&cmd_float_op,
84 /**********************************************************/
85 /* operations trigo */
87 /* this structure is filled when cmd_trigo is parsed successfully */
88 struct cmd_trigo_result {
89 cmdline_fixed_string_t op;
93 /* function called when cmd_trigo is parsed successfully */
94 static void cmd_trigo_parsed(void *parsed_result, struct cmdline *cl, void *data)
96 struct cmd_trigo_result *cmd = (struct cmd_trigo_result *) parsed_result;
99 if (!strcmp(cmd->op, "sin")) {
103 else if (!strcmp(cmd->op, "cos")) {
107 else if (!strcmp(cmd->op, "tan")) {
111 cmdline_printf(cl, "%f\n", res);
114 cmdline_parse_token_string_t cmd_trigo_op = TOKEN_STRING_INITIALIZER(struct cmd_trigo_result, op, "sin#tan#cos");
115 cmdline_parse_token_num_t cmd_trigo_a = TOKEN_NUM_INITIALIZER(struct cmd_trigo_result, a, FLOAT);
117 cmdline_parse_inst_t cmd_trigo = {
118 .f = cmd_trigo_parsed, /* function to call */
119 .data = NULL, /* 2nd arg of func */
120 .help_str = "Trigonometric operations (ex: 'sin 2.03')",
121 .tokens = { /* token list, NULL terminated */
122 (void *)&cmd_trigo_op,
123 (void *)&cmd_trigo_a,
129 /**********************************************************/
132 /* this structure is filled when cmd_help is parsed successfully */
133 struct cmd_help_result {
134 cmdline_fixed_string_t arg0;
137 /* function called when cmd_help is parsed successfully */
138 static void cmd_help_parsed(void *parsed_result, struct cmdline *cl, void *data)
140 cmdline_printf(cl, "== Simple calculator program ==\n"
141 "You can do simple operations on floats, like '1 + 3'\n"
142 "or '4.4 * 2.' (space is important).\n"
143 "Some trigonometric operations are available, like\n"
147 cmdline_parse_token_string_t cmd_help_arg0 = TOKEN_STRING_INITIALIZER(struct cmd_help_result, arg0, "help");
149 cmdline_parse_inst_t cmd_help = {
150 .f = cmd_help_parsed, /* function to call */
151 .data = NULL, /* 2nd arg of func */
152 .help_str = "Display help",
153 .tokens = { /* token list, NULL terminated */
154 (void *)&cmd_help_arg0,
162 /**********************************************************/
163 /**********************************************************/
164 /****** CONTEXT (list of instruction) */
167 cmdline_parse_ctx_t main_ctx[] = {
168 (cmdline_parse_inst_t *)&cmd_float,
169 (cmdline_parse_inst_t *)&cmd_trigo,
170 (cmdline_parse_inst_t *)&cmd_help,