This rather large commit changes the way modules are loaded.
[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 <stdlib.h>
28 #include <stdio.h>
29 #include <string.h>
30 #include <sys/types.h>
31
32 #include "asterisk.h"
33
34 ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
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                 iaction = GTFUNCTION;
124                 *op = '\0';
125                 if (*(op + 1) == '=') {
126                         *++op = '\0';
127                         iaction = EQFUNCTION;
128                 } else
129                         op = NULL;
130         }
131
132         if (op)
133                 mvalue2 = op + 1;
134
135         /* detect wanted type of result */
136         mtype_of_result = args.argv1;
137         if (mtype_of_result) {
138                 if (!strcasecmp(mtype_of_result, "float")
139                     || !strcasecmp(mtype_of_result, "f"))
140                         type_of_result = FLOAT_RESULT;
141                 else if (!strcasecmp(mtype_of_result, "int")
142                          || !strcasecmp(mtype_of_result, "i"))
143                         type_of_result = INT_RESULT;
144                 else if (!strcasecmp(mtype_of_result, "hex")
145                          || !strcasecmp(mtype_of_result, "h"))
146                         type_of_result = HEX_RESULT;
147                 else if (!strcasecmp(mtype_of_result, "char")
148                          || !strcasecmp(mtype_of_result, "c"))
149                         type_of_result = CHAR_RESULT;
150                 else {
151                         ast_log(LOG_WARNING, "Unknown type of result requested '%s'.\n",
152                                         mtype_of_result);
153                         return -1;
154                 }
155         }
156
157         if (!mvalue1 || !mvalue2) {
158                 ast_log(LOG_WARNING,
159                                 "Supply all the parameters - just this once, please\n");
160                 return -1;
161         }
162
163         if (sscanf(mvalue1, "%f", &fnum1) != 1) {
164                 ast_log(LOG_WARNING, "'%s' is not a valid number\n", mvalue1);
165                 return -1;
166         }
167
168         if (sscanf(mvalue2, "%f", &fnum2) != 1) {
169                 ast_log(LOG_WARNING, "'%s' is not a valid number\n", mvalue2);
170                 return -1;
171         }
172
173         switch (iaction) {
174         case ADDFUNCTION:
175                 ftmp = fnum1 + fnum2;
176                 break;
177         case DIVIDEFUNCTION:
178                 if (fnum2 <= 0)
179                         ftmp = 0;                       /* can't do a divide by 0 */
180                 else
181                         ftmp = (fnum1 / fnum2);
182                 break;
183         case MULTIPLYFUNCTION:
184                 ftmp = (fnum1 * fnum2);
185                 break;
186         case SUBTRACTFUNCTION:
187                 ftmp = (fnum1 - fnum2);
188                 break;
189         case MODULUSFUNCTION:
190                 {
191                         int inum1 = fnum1;
192                         int inum2 = fnum2;
193
194                         ftmp = (inum1 % inum2);
195
196                         break;
197                 }
198         case GTFUNCTION:
199                 ast_copy_string(buf, (fnum1 > fnum2) ? "TRUE" : "FALSE", len);
200                 break;
201         case LTFUNCTION:
202                 ast_copy_string(buf, (fnum1 < fnum2) ? "TRUE" : "FALSE", len);
203                 break;
204         case GTEFUNCTION:
205                 ast_copy_string(buf, (fnum1 >= fnum2) ? "TRUE" : "FALSE", len);
206                 break;
207         case LTEFUNCTION:
208                 ast_copy_string(buf, (fnum1 <= fnum2) ? "TRUE" : "FALSE", len);
209                 break;
210         case EQFUNCTION:
211                 ast_copy_string(buf, (fnum1 == fnum2) ? "TRUE" : "FALSE", len);
212                 break;
213         default:
214                 ast_log(LOG_WARNING,
215                                 "Something happened that neither of us should be proud of %d\n",
216                                 iaction);
217                 return -1;
218         }
219
220         if (iaction < GTFUNCTION || iaction > EQFUNCTION) {
221                 if (type_of_result == FLOAT_RESULT)
222                         snprintf(buf, len, "%f", ftmp);
223                 else if (type_of_result == INT_RESULT)
224                         snprintf(buf, len, "%i", (int) ftmp);
225                 else if (type_of_result == HEX_RESULT)
226                         snprintf(buf, len, "%x", (unsigned int) ftmp);
227                 else if (type_of_result == CHAR_RESULT)
228                         snprintf(buf, len, "%c", (unsigned char) ftmp);
229         }
230
231         return 0;
232 }
233
234 static struct ast_custom_function math_function = {
235         .name = "MATH",
236         .synopsis = "Performs Mathematical Functions",
237         .syntax = "MATH(<number1><op><number 2>[,<type_of_result>])",
238         .desc = "Perform calculation on number 1 to number 2. Valid ops are: \n"
239                 "    +,-,/,*,%,<,>,>=,<=,==\n"
240                 "and behave as their C equivalents.\n"
241                 "<type_of_result> - wanted type of result:\n"
242                 "       f, float - float(default)\n"
243                 "       i, int - integer,\n"
244                 "       h, hex - hex,\n"
245                 "       c, char - char\n"
246                 "Example: Set(i=${MATH(123%16,int)}) - sets var i=11",
247         .read = math
248 };
249
250 static char *tdesc = "Mathematical dialplan function";
251
252 static int unload_module(void *mod)
253 {
254         return ast_custom_function_unregister(&math_function);
255 }
256
257 static int load_module(void *mod)
258 {
259         return ast_custom_function_register(&math_function);
260 }
261
262 static const char *description(void)
263 {
264         return tdesc;
265 }
266
267 static const char *key(void)
268 {
269         return ASTERISK_GPL_KEY;
270 }
271
272 STD_MOD(MOD_1 | NO_USECOUNT, NULL, NULL, NULL);
273