Merged revisions 48956 via svnmerge from
[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 <sys/types.h>
35
36 #include "asterisk/module.h"
37 #include "asterisk/channel.h"
38 #include "asterisk/pbx.h"
39 #include "asterisk/logger.h"
40 #include "asterisk/utils.h"
41 #include "asterisk/app.h"
42 #include "asterisk/config.h"
43
44 enum TypeOfFunctions {
45         ADDFUNCTION,
46         DIVIDEFUNCTION,
47         MULTIPLYFUNCTION,
48         SUBTRACTFUNCTION,
49         MODULUSFUNCTION,
50         GTFUNCTION,
51         LTFUNCTION,
52         GTEFUNCTION,
53         LTEFUNCTION,
54         EQFUNCTION
55 };
56
57 enum TypeOfResult {
58         FLOAT_RESULT,
59         INT_RESULT,
60         HEX_RESULT,
61         CHAR_RESULT
62 };
63
64 static int math(struct ast_channel *chan, char *cmd, char *parse,
65                 char *buf, size_t len)
66 {
67         float fnum1;
68         float fnum2;
69         float ftmp = 0;
70         char *op;
71         int iaction = -1;
72         int type_of_result = FLOAT_RESULT;
73         char *mvalue1, *mvalue2 = NULL, *mtype_of_result;
74         AST_DECLARE_APP_ARGS(args,
75                              AST_APP_ARG(argv0);
76                              AST_APP_ARG(argv1);
77         );
78
79         if (ast_strlen_zero(parse)) {
80                 ast_log(LOG_WARNING, "Syntax: Math(<number1><op><number 2>[,<type_of_result>]) - missing argument!\n");
81                 return -1;
82         }
83
84         AST_STANDARD_APP_ARGS(args, parse);
85
86         if (args.argc < 1) {
87                 ast_log(LOG_WARNING, "Syntax: Math(<number1><op><number 2>[,<type_of_result>]) - missing argument!\n");
88                 return -1;
89         }
90
91         mvalue1 = args.argv0;
92
93         if ((op = strchr(mvalue1, '+'))) {
94                 iaction = ADDFUNCTION;
95                 *op = '\0';
96         } else if ((op = strchr(mvalue1, '-'))) {
97                 iaction = SUBTRACTFUNCTION;
98                 *op = '\0';
99         } else if ((op = strchr(mvalue1, '*'))) {
100                 iaction = MULTIPLYFUNCTION;
101                 *op = '\0';
102         } else if ((op = strchr(mvalue1, '/'))) {
103                 iaction = DIVIDEFUNCTION;
104                 *op = '\0';
105         } else if ((op = strchr(mvalue1, '%'))) {
106                 iaction = MODULUSFUNCTION;
107                 *op = '\0';
108         } else if ((op = strchr(mvalue1, '>'))) {
109                 iaction = GTFUNCTION;
110                 *op = '\0';
111                 if (*(op + 1) == '=') {
112                         *++op = '\0';
113                         iaction = GTEFUNCTION;
114                 }
115         } else if ((op = strchr(mvalue1, '<'))) {
116                 iaction = LTFUNCTION;
117                 *op = '\0';
118                 if (*(op + 1) == '=') {
119                         *++op = '\0';
120                         iaction = LTEFUNCTION;
121                 }
122         } else if ((op = strchr(mvalue1, '='))) {
123                 *op = '\0';
124                 if (*(op + 1) == '=') {
125                         *++op = '\0';
126                         iaction = EQFUNCTION;
127                 } else
128                         op = NULL;
129         }
130
131         if (op)
132                 mvalue2 = op + 1;
133
134         /* detect wanted type of result */
135         mtype_of_result = args.argv1;
136         if (mtype_of_result) {
137                 if (!strcasecmp(mtype_of_result, "float")
138                     || !strcasecmp(mtype_of_result, "f"))
139                         type_of_result = FLOAT_RESULT;
140                 else if (!strcasecmp(mtype_of_result, "int")
141                          || !strcasecmp(mtype_of_result, "i"))
142                         type_of_result = INT_RESULT;
143                 else if (!strcasecmp(mtype_of_result, "hex")
144                          || !strcasecmp(mtype_of_result, "h"))
145                         type_of_result = HEX_RESULT;
146                 else if (!strcasecmp(mtype_of_result, "char")
147                          || !strcasecmp(mtype_of_result, "c"))
148                         type_of_result = CHAR_RESULT;
149                 else {
150                         ast_log(LOG_WARNING, "Unknown type of result requested '%s'.\n",
151                                         mtype_of_result);
152                         return -1;
153                 }
154         }
155
156         if (!mvalue1 || !mvalue2) {
157                 ast_log(LOG_WARNING,
158                                 "Supply all the parameters - just this once, please\n");
159                 return -1;
160         }
161
162         if (sscanf(mvalue1, "%f", &fnum1) != 1) {
163                 ast_log(LOG_WARNING, "'%s' is not a valid number\n", mvalue1);
164                 return -1;
165         }
166
167         if (sscanf(mvalue2, "%f", &fnum2) != 1) {
168                 ast_log(LOG_WARNING, "'%s' is not a valid number\n", mvalue2);
169                 return -1;
170         }
171
172         switch (iaction) {
173         case ADDFUNCTION:
174                 ftmp = fnum1 + fnum2;
175                 break;
176         case DIVIDEFUNCTION:
177                 if (fnum2 <= 0)
178                         ftmp = 0;                       /* can't do a divide by 0 */
179                 else
180                         ftmp = (fnum1 / fnum2);
181                 break;
182         case MULTIPLYFUNCTION:
183                 ftmp = (fnum1 * fnum2);
184                 break;
185         case SUBTRACTFUNCTION:
186                 ftmp = (fnum1 - fnum2);
187                 break;
188         case MODULUSFUNCTION:
189                 {
190                         int inum1 = fnum1;
191                         int inum2 = fnum2;
192
193                         ftmp = (inum1 % inum2);
194
195                         break;
196                 }
197         case GTFUNCTION:
198                 ast_copy_string(buf, (fnum1 > fnum2) ? "TRUE" : "FALSE", len);
199                 break;
200         case LTFUNCTION:
201                 ast_copy_string(buf, (fnum1 < fnum2) ? "TRUE" : "FALSE", len);
202                 break;
203         case GTEFUNCTION:
204                 ast_copy_string(buf, (fnum1 >= fnum2) ? "TRUE" : "FALSE", len);
205                 break;
206         case LTEFUNCTION:
207                 ast_copy_string(buf, (fnum1 <= fnum2) ? "TRUE" : "FALSE", len);
208                 break;
209         case EQFUNCTION:
210                 ast_copy_string(buf, (fnum1 == fnum2) ? "TRUE" : "FALSE", len);
211                 break;
212         default:
213                 ast_log(LOG_WARNING,
214                                 "Something happened that neither of us should be proud of %d\n",
215                                 iaction);
216                 return -1;
217         }
218
219         if (iaction < GTFUNCTION || iaction > EQFUNCTION) {
220                 if (type_of_result == FLOAT_RESULT)
221                         snprintf(buf, len, "%f", ftmp);
222                 else if (type_of_result == INT_RESULT)
223                         snprintf(buf, len, "%i", (int) ftmp);
224                 else if (type_of_result == HEX_RESULT)
225                         snprintf(buf, len, "%x", (unsigned int) ftmp);
226                 else if (type_of_result == CHAR_RESULT)
227                         snprintf(buf, len, "%c", (unsigned char) ftmp);
228         }
229
230         return 0;
231 }
232
233 static struct ast_custom_function math_function = {
234         .name = "MATH",
235         .synopsis = "Performs Mathematical Functions",
236         .syntax = "MATH(<number1><op><number 2>[,<type_of_result>])",
237         .desc = "Perform calculation on number 1 to number 2. Valid ops are: \n"
238                 "    +,-,/,*,%,<,>,>=,<=,==\n"
239                 "and behave as their C equivalents.\n"
240                 "<type_of_result> - wanted type of result:\n"
241                 "       f, float - float(default)\n"
242                 "       i, int - integer,\n"
243                 "       h, hex - hex,\n"
244                 "       c, char - char\n"
245                 "Example: Set(i=${MATH(123%16,int)}) - sets var i=11",
246         .read = math
247 };
248
249 static int unload_module(void)
250 {
251         return ast_custom_function_unregister(&math_function);
252 }
253
254 static int load_module(void)
255 {
256         return ast_custom_function_register(&math_function);
257 }
258
259 AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "Mathematical dialplan function");