Added AND, OR, and XOR bitwise operations to MATH for issue 9891, thanks jcmoore
[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  * \ingroup functions
27  */
28
29 #include "asterisk.h"
30
31 ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
32
33 #include <stdlib.h>
34 #include <stdio.h>
35 #include <string.h>
36 #include <math.h>
37 #include <sys/types.h>
38
39 #include "asterisk/module.h"
40 #include "asterisk/channel.h"
41 #include "asterisk/pbx.h"
42 #include "asterisk/logger.h"
43 #include "asterisk/utils.h"
44 #include "asterisk/app.h"
45 #include "asterisk/config.h"
46
47 enum TypeOfFunctions {
48         ADDFUNCTION,
49         DIVIDEFUNCTION,
50         MULTIPLYFUNCTION,
51         SUBTRACTFUNCTION,
52         MODULUSFUNCTION,
53         POWFUNCTION,
54         SHLEFTFUNCTION,
55         SHRIGHTFUNCTION,
56         BITWISEANDFUNCTION,
57         BITWISEXORFUNCTION,
58         BITWISEORFUNCTION,
59         GTFUNCTION,
60         LTFUNCTION,
61         GTEFUNCTION,
62         LTEFUNCTION,
63         EQFUNCTION
64 };
65
66 enum TypeOfResult {
67         FLOAT_RESULT,
68         INT_RESULT,
69         HEX_RESULT,
70         CHAR_RESULT
71 };
72
73 static int math(struct ast_channel *chan, const char *cmd, char *parse,
74                 char *buf, size_t len)
75 {
76         double fnum1;
77         double fnum2;
78         double ftmp = 0;
79         char *op;
80         int iaction = -1;
81         int type_of_result = FLOAT_RESULT;
82         char *mvalue1, *mvalue2 = NULL, *mtype_of_result;
83         int negvalue1 = 0;
84         AST_DECLARE_APP_ARGS(args,
85                              AST_APP_ARG(argv0);
86                              AST_APP_ARG(argv1);
87         );
88
89         if (ast_strlen_zero(parse)) {
90                 ast_log(LOG_WARNING, "Syntax: Math(<number1><op><number 2>[,<type_of_result>]) - missing argument!\n");
91                 return -1;
92         }
93
94         AST_STANDARD_APP_ARGS(args, parse);
95
96         if (args.argc < 1) {
97                 ast_log(LOG_WARNING, "Syntax: Math(<number1><op><number 2>[,<type_of_result>]) - missing argument!\n");
98                 return -1;
99         }
100
101         mvalue1 = args.argv0;
102
103         if (mvalue1[0] == '-') {
104                 negvalue1 = 1;
105                 mvalue1++;
106         }
107
108         if ((op = strchr(mvalue1, '*'))) {
109                 iaction = MULTIPLYFUNCTION;
110                 *op = '\0';
111         } else if ((op = strchr(mvalue1, '/'))) {
112                 iaction = DIVIDEFUNCTION;
113                 *op = '\0';
114         } else if ((op = strchr(mvalue1, '%'))) {
115                 iaction = MODULUSFUNCTION;
116                 *op = '\0';
117         } else if ((op = strchr(mvalue1, '^'))) {
118                 iaction = POWFUNCTION;
119                 *op = '\0';
120         } else if ((op = strstr(mvalue1, "AND"))) {
121                 iaction = BITWISEANDFUNCTION;
122                 op += 3;
123                 *op = '\0';
124         } else if ((op = strstr(mvalue1, "XOR"))) {
125                 iaction = BITWISEXORFUNCTION;
126                 op += 3;
127                 *op = '\0';
128         } else if ((op = strstr(mvalue1, "OR"))) {
129                 iaction = BITWISEORFUNCTION;
130                 op += 2;
131                 *op = '\0';
132         } else if ((op = strchr(mvalue1, '>'))) {
133                 iaction = GTFUNCTION;
134                 *op = '\0';
135                 if (*(op + 1) == '=') {
136                         *++op = '\0';
137                         iaction = GTEFUNCTION;
138                 } else if (*(op + 1) == '>') {
139                         *++op = '\0';
140                         iaction = SHRIGHTFUNCTION;
141                 }
142         } else if ((op = strchr(mvalue1, '<'))) {
143                 iaction = LTFUNCTION;
144                 *op = '\0';
145                 if (*(op + 1) == '=') {
146                         *++op = '\0';
147                         iaction = LTEFUNCTION;
148                 } else if (*(op + 1) == '<') {
149                         *++op = '\0';
150                         iaction = SHLEFTFUNCTION;
151                 }
152         } else if ((op = strchr(mvalue1, '='))) {
153                 *op = '\0';
154                 if (*(op + 1) == '=') {
155                         *++op = '\0';
156                         iaction = EQFUNCTION;
157                 } else
158                         op = NULL;
159         } else if ((op = strchr(mvalue1, '+'))) {
160                 iaction = ADDFUNCTION;
161                 *op = '\0';
162         } else if ((op = strchr(mvalue1, '-'))) { /* subtraction MUST always be last, in case we have a negative first number */
163                 iaction = SUBTRACTFUNCTION;
164                 *op = '\0';
165         }
166
167         if (op)
168                 mvalue2 = op + 1;
169
170         /* detect wanted type of result */
171         mtype_of_result = args.argv1;
172         if (mtype_of_result) {
173                 if (!strcasecmp(mtype_of_result, "float")
174                     || !strcasecmp(mtype_of_result, "f"))
175                         type_of_result = FLOAT_RESULT;
176                 else if (!strcasecmp(mtype_of_result, "int")
177                          || !strcasecmp(mtype_of_result, "i"))
178                         type_of_result = INT_RESULT;
179                 else if (!strcasecmp(mtype_of_result, "hex")
180                          || !strcasecmp(mtype_of_result, "h"))
181                         type_of_result = HEX_RESULT;
182                 else if (!strcasecmp(mtype_of_result, "char")
183                          || !strcasecmp(mtype_of_result, "c"))
184                         type_of_result = CHAR_RESULT;
185                 else {
186                         ast_log(LOG_WARNING, "Unknown type of result requested '%s'.\n",
187                                         mtype_of_result);
188                         return -1;
189                 }
190         }
191
192         if (!mvalue1 || !mvalue2) {
193                 ast_log(LOG_WARNING,
194                                 "Supply all the parameters - just this once, please\n");
195                 return -1;
196         }
197
198         if (sscanf(mvalue1, "%lf", &fnum1) != 1) {
199                 ast_log(LOG_WARNING, "'%s' is not a valid number\n", mvalue1);
200                 return -1;
201         }
202
203         if (sscanf(mvalue2, "%lf", &fnum2) != 1) {
204                 ast_log(LOG_WARNING, "'%s' is not a valid number\n", mvalue2);
205                 return -1;
206         }
207
208         if (negvalue1)
209                 fnum1 = 0 - fnum1;
210
211         switch (iaction) {
212         case ADDFUNCTION:
213                 ftmp = fnum1 + fnum2;
214                 break;
215         case DIVIDEFUNCTION:
216                 if (fnum2 <= 0)
217                         ftmp = 0;                       /* can't do a divide by 0 */
218                 else
219                         ftmp = (fnum1 / fnum2);
220                 break;
221         case MULTIPLYFUNCTION:
222                 ftmp = (fnum1 * fnum2);
223                 break;
224         case SUBTRACTFUNCTION:
225                 ftmp = (fnum1 - fnum2);
226                 break;
227         case MODULUSFUNCTION:
228                 {
229                         int inum1 = fnum1;
230                         int inum2 = fnum2;
231
232                         ftmp = (inum1 % inum2);
233
234                         break;
235                 }
236         case POWFUNCTION:
237                 ftmp = pow(fnum1, fnum2);
238                 break;
239         case SHLEFTFUNCTION:
240                 {
241                         int inum1 = fnum1;
242                         int inum2 = fnum2;
243
244                         ftmp = (inum1 << inum2);
245                         break;
246                 }
247         case SHRIGHTFUNCTION:
248                 {
249                         int inum1 = fnum1;
250                         int inum2 = fnum2;
251
252                         ftmp = (inum1 >> inum2);
253                         break;
254                 }
255         case BITWISEANDFUNCTION:
256                 {
257                         int inum1 = fnum1;
258                         int inum2 = fnum2;
259                         ftmp = (inum1 & inum2);
260                         break;
261                 }
262         case BITWISEXORFUNCTION:
263                 {
264                         int inum1 = fnum1;
265                         int inum2 = fnum2;
266                         ftmp = (inum1 ^ inum2);
267                         break;
268                 }
269         case BITWISEORFUNCTION:
270                 {
271                         int inum1 = fnum1;
272                         int inum2 = fnum2;
273                         ftmp = (inum1 | inum2);
274                         break;
275                 }
276         case GTFUNCTION:
277                 ast_copy_string(buf, (fnum1 > fnum2) ? "TRUE" : "FALSE", len);
278                 break;
279         case LTFUNCTION:
280                 ast_copy_string(buf, (fnum1 < fnum2) ? "TRUE" : "FALSE", len);
281                 break;
282         case GTEFUNCTION:
283                 ast_copy_string(buf, (fnum1 >= fnum2) ? "TRUE" : "FALSE", len);
284                 break;
285         case LTEFUNCTION:
286                 ast_copy_string(buf, (fnum1 <= fnum2) ? "TRUE" : "FALSE", len);
287                 break;
288         case EQFUNCTION:
289                 ast_copy_string(buf, (fnum1 == fnum2) ? "TRUE" : "FALSE", len);
290                 break;
291         default:
292                 ast_log(LOG_WARNING,
293                                 "Something happened that neither of us should be proud of %d\n",
294                                 iaction);
295                 return -1;
296         }
297
298         if (iaction < GTFUNCTION || iaction > EQFUNCTION) {
299                 if (type_of_result == FLOAT_RESULT)
300                         snprintf(buf, len, "%f", ftmp);
301                 else if (type_of_result == INT_RESULT)
302                         snprintf(buf, len, "%i", (int) ftmp);
303                 else if (type_of_result == HEX_RESULT)
304                         snprintf(buf, len, "%x", (unsigned int) ftmp);
305                 else if (type_of_result == CHAR_RESULT)
306                         snprintf(buf, len, "%c", (unsigned char) ftmp);
307         }
308
309         return 0;
310 }
311
312 static struct ast_custom_function math_function = {
313         .name = "MATH",
314         .synopsis = "Performs Mathematical Functions",
315         .syntax = "MATH(<number1><op><number2>[,<type_of_result>])",
316         .desc = "Perform calculation on number1 to number2. Valid ops are: \n"
317                 "    +,-,/,*,%,<<,>>,^,AND,OR,XOR,<,>,>=,<=,==\n"
318                 "and behave as their C equivalents.\n"
319                 "<type_of_result> - wanted type of result:\n"
320                 "       f, float - float(default)\n"
321                 "       i, int - integer,\n"
322                 "       h, hex - hex,\n"
323                 "       c, char - char\n"
324                 "Example: Set(i=${MATH(123%16,int)}) - sets var i=11",
325         .read = math
326 };
327
328 static int unload_module(void)
329 {
330         return ast_custom_function_unregister(&math_function);
331 }
332
333 static int load_module(void)
334 {
335         return ast_custom_function_register(&math_function);
336 }
337
338 AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "Mathematical dialplan function");