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