2 * Asterisk -- An open source telephony toolkit.
4 * Copyright (C) 2004 - 2006, Andy Powell
6 * Updated by Mark Spencer <markster@digium.com>
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.
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.
21 * \brief Math related dialplan function
24 * \author Mark Spencer <markster@digium.com>
29 ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
35 #include <sys/types.h>
37 #include "asterisk/module.h"
38 #include "asterisk/channel.h"
39 #include "asterisk/pbx.h"
40 #include "asterisk/logger.h"
41 #include "asterisk/utils.h"
42 #include "asterisk/app.h"
43 #include "asterisk/config.h"
45 enum TypeOfFunctions {
68 static int math(struct ast_channel *chan, char *cmd, char *parse,
69 char *buf, size_t len)
76 int type_of_result = FLOAT_RESULT;
77 char *mvalue1, *mvalue2 = NULL, *mtype_of_result;
78 AST_DECLARE_APP_ARGS(args,
83 if (ast_strlen_zero(parse)) {
84 ast_log(LOG_WARNING, "Syntax: Math(<number1><op><number 2>[,<type_of_result>]) - missing argument!\n");
88 AST_STANDARD_APP_ARGS(args, parse);
91 ast_log(LOG_WARNING, "Syntax: Math(<number1><op><number 2>[,<type_of_result>]) - missing argument!\n");
97 if ((op = strchr(mvalue1, '+'))) {
98 iaction = ADDFUNCTION;
100 } else if ((op = strchr(mvalue1, '-'))) {
101 iaction = SUBTRACTFUNCTION;
103 } else if ((op = strchr(mvalue1, '*'))) {
104 iaction = MULTIPLYFUNCTION;
106 } else if ((op = strchr(mvalue1, '/'))) {
107 iaction = DIVIDEFUNCTION;
109 } else if ((op = strchr(mvalue1, '%'))) {
110 iaction = MODULUSFUNCTION;
112 } else if ((op = strchr(mvalue1, '^'))) {
113 iaction = POWFUNCTION;
115 } else if ((op = strchr(mvalue1, '>'))) {
116 iaction = GTFUNCTION;
118 if (*(op + 1) == '=') {
120 iaction = GTEFUNCTION;
121 } else if (*(op + 1) == '>') {
123 iaction = SHRIGHTFUNCTION;
125 } else if ((op = strchr(mvalue1, '<'))) {
126 iaction = LTFUNCTION;
128 if (*(op + 1) == '=') {
130 iaction = LTEFUNCTION;
131 } else if (*(op + 1) == '<') {
133 iaction = SHLEFTFUNCTION;
135 } else if ((op = strchr(mvalue1, '='))) {
137 if (*(op + 1) == '=') {
139 iaction = EQFUNCTION;
147 /* detect wanted type of result */
148 mtype_of_result = args.argv1;
149 if (mtype_of_result) {
150 if (!strcasecmp(mtype_of_result, "float")
151 || !strcasecmp(mtype_of_result, "f"))
152 type_of_result = FLOAT_RESULT;
153 else if (!strcasecmp(mtype_of_result, "int")
154 || !strcasecmp(mtype_of_result, "i"))
155 type_of_result = INT_RESULT;
156 else if (!strcasecmp(mtype_of_result, "hex")
157 || !strcasecmp(mtype_of_result, "h"))
158 type_of_result = HEX_RESULT;
159 else if (!strcasecmp(mtype_of_result, "char")
160 || !strcasecmp(mtype_of_result, "c"))
161 type_of_result = CHAR_RESULT;
163 ast_log(LOG_WARNING, "Unknown type of result requested '%s'.\n",
169 if (!mvalue1 || !mvalue2) {
171 "Supply all the parameters - just this once, please\n");
175 if (sscanf(mvalue1, "%f", &fnum1) != 1) {
176 ast_log(LOG_WARNING, "'%s' is not a valid number\n", mvalue1);
180 if (sscanf(mvalue2, "%f", &fnum2) != 1) {
181 ast_log(LOG_WARNING, "'%s' is not a valid number\n", mvalue2);
187 ftmp = fnum1 + fnum2;
191 ftmp = 0; /* can't do a divide by 0 */
193 ftmp = (fnum1 / fnum2);
195 case MULTIPLYFUNCTION:
196 ftmp = (fnum1 * fnum2);
198 case SUBTRACTFUNCTION:
199 ftmp = (fnum1 - fnum2);
201 case MODULUSFUNCTION:
206 ftmp = (inum1 % inum2);
211 ftmp = pow(fnum1, fnum2);
218 ftmp = (inum1 << inum2);
221 case SHRIGHTFUNCTION:
226 ftmp = (inum1 >> inum2);
230 ast_copy_string(buf, (fnum1 > fnum2) ? "TRUE" : "FALSE", len);
233 ast_copy_string(buf, (fnum1 < fnum2) ? "TRUE" : "FALSE", len);
236 ast_copy_string(buf, (fnum1 >= fnum2) ? "TRUE" : "FALSE", len);
239 ast_copy_string(buf, (fnum1 <= fnum2) ? "TRUE" : "FALSE", len);
242 ast_copy_string(buf, (fnum1 == fnum2) ? "TRUE" : "FALSE", len);
246 "Something happened that neither of us should be proud of %d\n",
251 if (iaction < GTFUNCTION || iaction > EQFUNCTION) {
252 if (type_of_result == FLOAT_RESULT)
253 snprintf(buf, len, "%f", ftmp);
254 else if (type_of_result == INT_RESULT)
255 snprintf(buf, len, "%i", (int) ftmp);
256 else if (type_of_result == HEX_RESULT)
257 snprintf(buf, len, "%x", (unsigned int) ftmp);
258 else if (type_of_result == CHAR_RESULT)
259 snprintf(buf, len, "%c", (unsigned char) ftmp);
265 static struct ast_custom_function math_function = {
267 .synopsis = "Performs Mathematical Functions",
268 .syntax = "MATH(<number1><op><number 2>[,<type_of_result>])",
269 .desc = "Perform calculation on number 1 to number 2. Valid ops are: \n"
270 " +,-,/,*,%,<,>,>=,<=,==\n"
271 "and behave as their C equivalents.\n"
272 "<type_of_result> - wanted type of result:\n"
273 " f, float - float(default)\n"
274 " i, int - integer,\n"
277 "Example: Set(i=${MATH(123%16,int)}) - sets var i=11",
281 static int unload_module(void)
283 return ast_custom_function_unregister(&math_function);
286 static int load_module(void)
288 return ast_custom_function_register(&math_function);
291 AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "Mathematical dialplan function");