4dbfac802bf21d5b0f7ce4d337f43036ff2c38e8
[asterisk/asterisk.git] / funcs / func_math.c
1 /*
2  * Asterisk -- An open source telephony toolkit.
3  *
4  * Copyright (C) 2004 - 2006, Andy Powell 
5  *
6  * Updated by Mark Spencer <markster@digium.com>
7  *
8  * See http://www.asterisk.org for more information about
9  * the Asterisk project. Please do not directly contact
10  * any of the maintainers of this project for assistance;
11  * the project provides a web site, mailing lists and IRC
12  * channels for your use.
13  *
14  * This program is free software, distributed under the terms of
15  * the GNU General Public License Version 2. See the LICENSE file
16  * at the top of the source tree.
17  */
18
19 /*! \file
20  *
21  * \brief Math related dialplan function
22  *
23  * \author Andy Powell
24  * \author Mark Spencer <markster@digium.com>
25  */
26
27 #include "asterisk.h"
28
29 ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
30
31 #include <stdlib.h>
32 #include <stdio.h>
33 #include <string.h>
34 #include <math.h>
35 #include <sys/types.h>
36
37 #include "asterisk/module.h"
38 #include "asterisk/channel.h"
39 #include "asterisk/pbx.h"
40 #include "asterisk/logger.h"
41 #include "asterisk/utils.h"
42 #include "asterisk/app.h"
43 #include "asterisk/config.h"
44
45 enum TypeOfFunctions {
46         ADDFUNCTION,
47         DIVIDEFUNCTION,
48         MULTIPLYFUNCTION,
49         SUBTRACTFUNCTION,
50         MODULUSFUNCTION,
51         POWFUNCTION,
52         SHLEFTFUNCTION,
53         SHRIGHTFUNCTION,
54         GTFUNCTION,
55         LTFUNCTION,
56         GTEFUNCTION,
57         LTEFUNCTION,
58         EQFUNCTION
59 };
60
61 enum TypeOfResult {
62         FLOAT_RESULT,
63         INT_RESULT,
64         HEX_RESULT,
65         CHAR_RESULT
66 };
67
68 static int math(struct ast_channel *chan, const char *cmd, char *parse,
69                 char *buf, size_t len)
70 {
71         float fnum1;
72         float fnum2;
73         float ftmp = 0;
74         char *op;
75         int iaction = -1;
76         int type_of_result = FLOAT_RESULT;
77         char *mvalue1, *mvalue2 = NULL, *mtype_of_result;
78         AST_DECLARE_APP_ARGS(args,
79                              AST_APP_ARG(argv0);
80                              AST_APP_ARG(argv1);
81         );
82
83         if (ast_strlen_zero(parse)) {
84                 ast_log(LOG_WARNING, "Syntax: Math(<number1><op><number 2>[,<type_of_result>]) - missing argument!\n");
85                 return -1;
86         }
87
88         AST_STANDARD_APP_ARGS(args, parse);
89
90         if (args.argc < 1) {
91                 ast_log(LOG_WARNING, "Syntax: Math(<number1><op><number 2>[,<type_of_result>]) - missing argument!\n");
92                 return -1;
93         }
94
95         mvalue1 = args.argv0;
96
97         if ((op = strchr(mvalue1, '+'))) {
98                 iaction = ADDFUNCTION;
99                 *op = '\0';
100         } else if ((op = strchr(mvalue1, '-'))) {
101                 iaction = SUBTRACTFUNCTION;
102                 *op = '\0';
103         } else if ((op = strchr(mvalue1, '*'))) {
104                 iaction = MULTIPLYFUNCTION;
105                 *op = '\0';
106         } else if ((op = strchr(mvalue1, '/'))) {
107                 iaction = DIVIDEFUNCTION;
108                 *op = '\0';
109         } else if ((op = strchr(mvalue1, '%'))) {
110                 iaction = MODULUSFUNCTION;
111                 *op = '\0';
112         } else if ((op = strchr(mvalue1, '^'))) {
113                 iaction = POWFUNCTION;
114                 *op = '\0';
115         } else if ((op = strchr(mvalue1, '>'))) {
116                 iaction = GTFUNCTION;
117                 *op = '\0';
118                 if (*(op + 1) == '=') {
119                         *++op = '\0';
120                         iaction = GTEFUNCTION;
121                 } else if (*(op + 1) == '>') {
122                         *++op = '\0';
123                         iaction = SHRIGHTFUNCTION;
124                 }
125         } else if ((op = strchr(mvalue1, '<'))) {
126                 iaction = LTFUNCTION;
127                 *op = '\0';
128                 if (*(op + 1) == '=') {
129                         *++op = '\0';
130                         iaction = LTEFUNCTION;
131                 } else if (*(op + 1) == '<') {
132                         *++op = '\0';
133                         iaction = SHLEFTFUNCTION;
134                 }
135         } else if ((op = strchr(mvalue1, '='))) {
136                 *op = '\0';
137                 if (*(op + 1) == '=') {
138                         *++op = '\0';
139                         iaction = EQFUNCTION;
140                 } else
141                         op = NULL;
142         }
143
144         if (op)
145                 mvalue2 = op + 1;
146
147         /* detect wanted type of result */
148         mtype_of_result = args.argv1;
149         if (mtype_of_result) {
150                 if (!strcasecmp(mtype_of_result, "float")
151                     || !strcasecmp(mtype_of_result, "f"))
152                         type_of_result = FLOAT_RESULT;
153                 else if (!strcasecmp(mtype_of_result, "int")
154                          || !strcasecmp(mtype_of_result, "i"))
155                         type_of_result = INT_RESULT;
156                 else if (!strcasecmp(mtype_of_result, "hex")
157                          || !strcasecmp(mtype_of_result, "h"))
158                         type_of_result = HEX_RESULT;
159                 else if (!strcasecmp(mtype_of_result, "char")
160                          || !strcasecmp(mtype_of_result, "c"))
161                         type_of_result = CHAR_RESULT;
162                 else {
163                         ast_log(LOG_WARNING, "Unknown type of result requested '%s'.\n",
164                                         mtype_of_result);
165                         return -1;
166                 }
167         }
168
169         if (!mvalue1 || !mvalue2) {
170                 ast_log(LOG_WARNING,
171                                 "Supply all the parameters - just this once, please\n");
172                 return -1;
173         }
174
175         if (sscanf(mvalue1, "%f", &fnum1) != 1) {
176                 ast_log(LOG_WARNING, "'%s' is not a valid number\n", mvalue1);
177                 return -1;
178         }
179
180         if (sscanf(mvalue2, "%f", &fnum2) != 1) {
181                 ast_log(LOG_WARNING, "'%s' is not a valid number\n", mvalue2);
182                 return -1;
183         }
184
185         switch (iaction) {
186         case ADDFUNCTION:
187                 ftmp = fnum1 + fnum2;
188                 break;
189         case DIVIDEFUNCTION:
190                 if (fnum2 <= 0)
191                         ftmp = 0;                       /* can't do a divide by 0 */
192                 else
193                         ftmp = (fnum1 / fnum2);
194                 break;
195         case MULTIPLYFUNCTION:
196                 ftmp = (fnum1 * fnum2);
197                 break;
198         case SUBTRACTFUNCTION:
199                 ftmp = (fnum1 - fnum2);
200                 break;
201         case MODULUSFUNCTION:
202                 {
203                         int inum1 = fnum1;
204                         int inum2 = fnum2;
205
206                         ftmp = (inum1 % inum2);
207
208                         break;
209                 }
210         case POWFUNCTION:
211                 ftmp = pow(fnum1, fnum2);
212                 break;
213         case SHLEFTFUNCTION:
214                 {
215                         int inum1 = fnum1;
216                         int inum2 = fnum2;
217
218                         ftmp = (inum1 << inum2);
219                         break;
220                 }
221         case SHRIGHTFUNCTION:
222                 {
223                         int inum1 = fnum1;
224                         int inum2 = fnum2;
225
226                         ftmp = (inum1 >> inum2);
227                         break;
228                 }
229         case GTFUNCTION:
230                 ast_copy_string(buf, (fnum1 > fnum2) ? "TRUE" : "FALSE", len);
231                 break;
232         case LTFUNCTION:
233                 ast_copy_string(buf, (fnum1 < fnum2) ? "TRUE" : "FALSE", len);
234                 break;
235         case GTEFUNCTION:
236                 ast_copy_string(buf, (fnum1 >= fnum2) ? "TRUE" : "FALSE", len);
237                 break;
238         case LTEFUNCTION:
239                 ast_copy_string(buf, (fnum1 <= fnum2) ? "TRUE" : "FALSE", len);
240                 break;
241         case EQFUNCTION:
242                 ast_copy_string(buf, (fnum1 == fnum2) ? "TRUE" : "FALSE", len);
243                 break;
244         default:
245                 ast_log(LOG_WARNING,
246                                 "Something happened that neither of us should be proud of %d\n",
247                                 iaction);
248                 return -1;
249         }
250
251         if (iaction < GTFUNCTION || iaction > EQFUNCTION) {
252                 if (type_of_result == FLOAT_RESULT)
253                         snprintf(buf, len, "%f", ftmp);
254                 else if (type_of_result == INT_RESULT)
255                         snprintf(buf, len, "%i", (int) ftmp);
256                 else if (type_of_result == HEX_RESULT)
257                         snprintf(buf, len, "%x", (unsigned int) ftmp);
258                 else if (type_of_result == CHAR_RESULT)
259                         snprintf(buf, len, "%c", (unsigned char) ftmp);
260         }
261
262         return 0;
263 }
264
265 static struct ast_custom_function math_function = {
266         .name = "MATH",
267         .synopsis = "Performs Mathematical Functions",
268         .syntax = "MATH(<number1><op><number2>[,<type_of_result>])",
269         .desc = "Perform calculation on number1 to number2. Valid ops are: \n"
270                 "    +,-,/,*,%,<<,>>,^,<,>,>=,<=,==\n"
271                 "and behave as their C equivalents.\n"
272                 "<type_of_result> - wanted type of result:\n"
273                 "       f, float - float(default)\n"
274                 "       i, int - integer,\n"
275                 "       h, hex - hex,\n"
276                 "       c, char - char\n"
277                 "Example: Set(i=${MATH(123%16,int)}) - sets var i=11",
278         .read = math
279 };
280
281 static int unload_module(void)
282 {
283         return ast_custom_function_unregister(&math_function);
284 }
285
286 static int load_module(void)
287 {
288         return ast_custom_function_register(&math_function);
289 }
290
291 AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "Mathematical dialplan function");