add MATH() dialplan function (bug #4473, with mods)
[asterisk/asterisk.git] / funcs / func_math.c
1 /*
2  * Asterisk -- A telephony toolkit for Linux.
3  *
4  * Maths relatad dialplan functions
5  * 
6  * Copyright (C) 2004 - 2005, Andy Powell 
7  *
8  * Updated by Mark Spencer <markster@digium.com>
9  *
10  */
11
12 #include <stdlib.h>
13 #include <string.h>
14 #include <sys/types.h>
15
16 #include "asterisk.h"
17
18 /* ASTERISK_FILE_VERSION(__FILE__, "$Revision$") */
19
20 #include "asterisk/channel.h"
21 #include "asterisk/pbx.h"
22 #include "asterisk/logger.h"
23 #include "asterisk/utils.h"
24 #include "asterisk/app.h"
25 #include "asterisk/config.h"
26
27 enum TypeOfFunctions
28 {
29     ADDFUNCTION,
30     DIVIDEFUNCTION,
31     MULTIPLYFUNCTION,
32     SUBTRACTFUNCTION,
33     MODULUSFUNCTION,
34
35     GTFUNCTION,
36     LTFUNCTION,
37     GTEFUNCTION,
38     LTEFUNCTION,
39     EQFUNCTION
40 };
41
42 enum TypeOfResult
43 {
44     FLOAT_RESULT,
45     INT_RESULT,
46     HEX_RESULT,
47     CHAR_RESULT
48 };
49
50
51 static char *builtin_function_math(struct ast_channel *chan, char *cmd, char *data, char *buf, size_t len) 
52 {
53         int argc;
54         char *argv[2];
55         char *args;
56         float fnum1;
57         float fnum2;
58         float ftmp = 0;
59         char *op;
60         int iaction=-1;
61         int type_of_result=FLOAT_RESULT;
62
63         /* dunno, big calulations :D */
64         char user_result[30];
65
66         char *mvalue1, *mvalue2=NULL, *mtype_of_result;
67                 
68         if (!data || ast_strlen_zero(data)) {
69                 ast_log(LOG_WARNING, "Syntax: Math(<number1><op><number 2>[,<type_of_result>]) - missing argument!\n");
70                 return NULL;
71         }
72
73         args = ast_strdupa(data);       
74         argc = ast_separate_app_args(args, '|', argv, sizeof(argv) / sizeof(argv[0]));
75
76         if (argc < 1) {
77                 ast_log(LOG_WARNING, "Syntax: Math(<number1><op><number 2>[,<type_of_result>]) - missing argument!\n");
78                 return NULL;
79         }
80
81         mvalue1 = argv[0];
82         
83         if ((op = strchr(mvalue1, '+'))) {
84                 iaction = ADDFUNCTION;
85                 *op = '\0';
86         } else if ((op = strchr(mvalue1, '-'))) {
87                 iaction = SUBTRACTFUNCTION;
88                 *op = '\0';
89         } else if ((op = strchr(mvalue1, '*'))) {
90                 iaction = MULTIPLYFUNCTION;
91                 *op = '\0';
92         } else if ((op = strchr(mvalue1, '/'))) {
93                 iaction = DIVIDEFUNCTION;
94                 *op = '\0';
95         } else if ((op = strchr(mvalue1, '%'))) {
96                 iaction = MODULUSFUNCTION;
97                 *op = '\0';
98         } else if ((op = strchr(mvalue1, '>'))) {
99                 iaction = GTFUNCTION;
100                 *op = '\0';
101                 if (*(op+1) == '=') {
102                         *++op = '\0';
103                         iaction = GTEFUNCTION;
104                 }
105         } else if ((op = strchr(mvalue1, '<'))) {
106                 iaction = LTFUNCTION;
107                 *op = '\0';
108                 if (*(op+1) == '=') {
109                         *++op = '\0';
110                         iaction = LTEFUNCTION;
111                 }
112         } else if ((op = strchr(mvalue1, '='))) {
113                 iaction = GTFUNCTION;
114                 *op = '\0';
115                 if (*(op+1) == '=') {
116                         *++op = '\0';
117                         iaction = EQFUNCTION;
118                 } else
119                         op = NULL;
120         } 
121         
122         if (op) 
123                 mvalue2 = op + 1;
124
125         /* detect wanted type of result */
126         mtype_of_result = argv[1];
127         if (mtype_of_result)
128         {
129                 if (!strcasecmp(mtype_of_result,"float") || !strcasecmp(mtype_of_result,"f"))
130                         type_of_result=FLOAT_RESULT;
131                 else if (!strcasecmp(mtype_of_result,"int") || !strcasecmp(mtype_of_result,"i"))
132                         type_of_result=INT_RESULT;
133                 else if (!strcasecmp(mtype_of_result,"hex") || !strcasecmp(mtype_of_result,"h"))
134                         type_of_result=HEX_RESULT;
135                 else if (!strcasecmp(mtype_of_result,"char") || !strcasecmp(mtype_of_result,"c"))
136                         type_of_result=CHAR_RESULT;
137                 else
138                 {
139                         ast_log(LOG_WARNING, "Unknown type of result requested '%s'.\n", mtype_of_result);
140                         return NULL;
141                 }
142         }
143         
144         if (!mvalue1 || !mvalue2) {
145                 ast_log(LOG_WARNING, "Supply all the parameters - just this once, please\n");
146                 return NULL;
147         }
148
149         if (sscanf(mvalue1, "%f", &fnum1) != 1) {
150                 ast_log(LOG_WARNING, "'%s' is not a valid number\n", mvalue1);
151                 return NULL;
152         }
153
154         if (sscanf(mvalue2, "%f", &fnum2) != 1) {
155                 ast_log(LOG_WARNING, "'%s' is not a valid number\n", mvalue2);
156                 return NULL;
157         }
158
159         switch (iaction) {
160         case ADDFUNCTION :
161                 ftmp = fnum1 + fnum2;
162                 break;
163         case DIVIDEFUNCTION :
164                 if (fnum2 <= 0)
165                         ftmp = 0; /* can't do a divide by 0 */
166                 else
167                         ftmp = (fnum1 / fnum2);
168                 break;
169         case MULTIPLYFUNCTION :
170                 ftmp = (fnum1 * fnum2);
171                 break;
172         case SUBTRACTFUNCTION :
173                 ftmp = (fnum1 - fnum2);
174                 break;
175         case MODULUSFUNCTION :
176         {
177                 int inum1 = fnum1;
178                 int inum2 = fnum2;
179                         
180                 ftmp = (inum1 % inum2);
181                 
182                 break;
183         }
184         case GTFUNCTION :
185                 ast_copy_string (user_result, (fnum1 > fnum2)?"TRUE":"FALSE", sizeof (user_result));
186                 break;
187         case LTFUNCTION :
188                 ast_copy_string (user_result, (fnum1 < fnum2)?"TRUE":"FALSE", sizeof (user_result));
189                 break;
190         case GTEFUNCTION :
191                 ast_copy_string (user_result, (fnum1 >= fnum2)?"TRUE":"FALSE", sizeof (user_result));
192                 break;
193         case LTEFUNCTION :
194                 ast_copy_string (user_result, (fnum1 <= fnum2)?"TRUE":"FALSE", sizeof (user_result));
195                 break;                                  
196         case EQFUNCTION :
197                 ast_copy_string (user_result, (fnum1 == fnum2)?"TRUE":"FALSE", sizeof (user_result));
198                 break;
199         default :
200                 ast_log(LOG_WARNING, "Something happened that neither of us should be proud of %d\n", iaction);
201                 return NULL;
202         }
203
204         if (iaction < GTFUNCTION || iaction > EQFUNCTION) {
205             if (type_of_result == FLOAT_RESULT)
206                     snprintf(user_result, sizeof(user_result), "%f", ftmp);
207             else if (type_of_result == INT_RESULT)
208                     snprintf(user_result, sizeof(user_result), "%i", (int) ftmp);
209             else if (type_of_result == HEX_RESULT)
210                 snprintf(user_result, sizeof(user_result), "%x", (unsigned int) ftmp);
211             else if (type_of_result == CHAR_RESULT)
212                 snprintf(user_result, sizeof(user_result), "%c", (unsigned char) ftmp);
213         }
214                 
215         ast_copy_string(buf, user_result, len);
216         
217         return buf;
218 }
219
220 #ifndef BUILTIN_FUNC
221 static
222 #endif /* BUILTIN_FUNC */
223 struct ast_custom_function math_function = {
224         .name = "MATH",
225         .synopsis = "Performs Mathematical Functions",
226         .syntax = "MATH(<number1><op><number 2>[,<type_of_result>])",
227         .desc = "Perform calculation on number 1 to number 2. Valid ops are: \n"
228             "    +,-,/,*,%,<,>,>=,<=,==\n"
229                 "and behave as their C equivalents.\n"
230                 "<type_of_result> - wanted type of result:\n"
231                 "       f, float - float(default)\n"
232                 "       i, int - integer,\n"
233                 "       h, hex - hex,\n"
234                 "       c, char - char\n"
235                 "Example: Set(i=${MATH(123%16,int)}) - sets var i=11",
236         .read = builtin_function_math
237 };