cmdline: big rework and clean of cmdline library
[libcmdline.git] / src / calculator_server / commands.c
1 /*
2  * Copyright (c) 2009, 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 <math.h>
29 #include <stdio.h>
30 #include <stdint.h>
31 #include <string.h>
32 #include <stdarg.h>
33 #include <netinet/in.h>
34
35 #include <cmdline_parse.h>
36 #include <cmdline_parse_num.h>
37 #include <cmdline_parse_string.h>
38 #include <cmdline.h>
39
40
41
42 /**********************************************************/
43 /* operations on float */
44
45 /* this structure is filled when cmd_float is parsed successfully */
46 struct cmd_float_result {
47         float a;
48         cmdline_fixed_string_t op;
49         float b;
50 };
51
52 /* function called when cmd_float is parsed successfully */
53 static void cmd_float_parsed(void *parsed_result, struct cmdline *cl, void *data)
54 {
55         struct cmd_float_result *cmd = (struct cmd_float_result *) parsed_result;
56         float res=0.;
57
58         switch(cmd->op[0]) {
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;
63         default: break;
64         }
65         cmdline_printf(cl, "%f\n", res);
66 }
67
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);
71
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 */
77                 (void *)&cmd_float_a, 
78                 (void *)&cmd_float_op, 
79                 (void *)&cmd_float_b, 
80                 NULL,
81         },
82 };
83
84
85 /**********************************************************/
86 /* operations trigo */
87
88 /* this structure is filled when cmd_trigo is parsed successfully */
89 struct cmd_trigo_result {
90         cmdline_fixed_string_t op;
91         float a;
92 };
93
94 /* function called when cmd_trigo is parsed successfully */
95 static void cmd_trigo_parsed(void *parsed_result, struct cmdline *cl, void *data)
96 {
97         struct cmd_trigo_result *cmd = (struct cmd_trigo_result *) parsed_result;
98         float res=0.;
99         
100         if (!strcmp(cmd->op, "sin")) {
101                 res = sin(cmd->a);
102         }
103
104         else if (!strcmp(cmd->op, "cos")) {
105                 res = cos(cmd->a);
106         }
107
108         else if (!strcmp(cmd->op, "tan")) {
109                 res = tan(cmd->a);
110         }
111
112         cmdline_printf(cl, "%f\n", res);
113 }
114
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);
117
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, 
125                 NULL,
126         },
127 };
128
129
130 /**********************************************************/
131 /* Help */
132
133 /* this structure is filled when cmd_help is parsed successfully */
134 struct cmd_help_result {
135         cmdline_fixed_string_t arg0;
136 };
137
138 /* function called when cmd_help is parsed successfully */
139 static void cmd_help_parsed(void *parsed_result, struct cmdline *cl, void *data)
140 {
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"
145                        "'sin 4.5'.\n");
146 }
147
148 cmdline_parse_token_string_t cmd_help_arg0 = TOKEN_STRING_INITIALIZER(struct cmd_help_result, arg0, "help");
149
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, 
156                 NULL,
157         },
158 };
159
160
161
162
163 /**********************************************************/
164 /**********************************************************/
165 /****** CONTEXT (list of instruction) */
166
167 /* in progmem */
168 cmdline_parse_ctx_t main_ctx = {
169         .name = "main",
170         .insts = {
171                 &cmd_float,
172                 &cmd_trigo,
173                 &cmd_help,
174                 NULL,
175         },
176 };
177