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