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$")
34 #include <sys/types.h>
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"
44 enum TypeOfFunctions {
64 static int math(struct ast_channel *chan, char *cmd, char *parse,
65 char *buf, size_t len)
72 int type_of_result = FLOAT_RESULT;
73 char *mvalue1, *mvalue2 = NULL, *mtype_of_result;
74 AST_DECLARE_APP_ARGS(args,
79 if (ast_strlen_zero(parse)) {
80 ast_log(LOG_WARNING, "Syntax: Math(<number1><op><number 2>[,<type_of_result>]) - missing argument!\n");
84 AST_STANDARD_APP_ARGS(args, parse);
87 ast_log(LOG_WARNING, "Syntax: Math(<number1><op><number 2>[,<type_of_result>]) - missing argument!\n");
93 if ((op = strchr(mvalue1, '+'))) {
94 iaction = ADDFUNCTION;
96 } else if ((op = strchr(mvalue1, '-'))) {
97 iaction = SUBTRACTFUNCTION;
99 } else if ((op = strchr(mvalue1, '*'))) {
100 iaction = MULTIPLYFUNCTION;
102 } else if ((op = strchr(mvalue1, '/'))) {
103 iaction = DIVIDEFUNCTION;
105 } else if ((op = strchr(mvalue1, '%'))) {
106 iaction = MODULUSFUNCTION;
108 } else if ((op = strchr(mvalue1, '>'))) {
109 iaction = GTFUNCTION;
111 if (*(op + 1) == '=') {
113 iaction = GTEFUNCTION;
115 } else if ((op = strchr(mvalue1, '<'))) {
116 iaction = LTFUNCTION;
118 if (*(op + 1) == '=') {
120 iaction = LTEFUNCTION;
122 } else if ((op = strchr(mvalue1, '='))) {
124 if (*(op + 1) == '=') {
126 iaction = EQFUNCTION;
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;
150 ast_log(LOG_WARNING, "Unknown type of result requested '%s'.\n",
156 if (!mvalue1 || !mvalue2) {
158 "Supply all the parameters - just this once, please\n");
162 if (sscanf(mvalue1, "%f", &fnum1) != 1) {
163 ast_log(LOG_WARNING, "'%s' is not a valid number\n", mvalue1);
167 if (sscanf(mvalue2, "%f", &fnum2) != 1) {
168 ast_log(LOG_WARNING, "'%s' is not a valid number\n", mvalue2);
174 ftmp = fnum1 + fnum2;
178 ftmp = 0; /* can't do a divide by 0 */
180 ftmp = (fnum1 / fnum2);
182 case MULTIPLYFUNCTION:
183 ftmp = (fnum1 * fnum2);
185 case SUBTRACTFUNCTION:
186 ftmp = (fnum1 - fnum2);
188 case MODULUSFUNCTION:
193 ftmp = (inum1 % inum2);
198 ast_copy_string(buf, (fnum1 > fnum2) ? "TRUE" : "FALSE", len);
201 ast_copy_string(buf, (fnum1 < fnum2) ? "TRUE" : "FALSE", len);
204 ast_copy_string(buf, (fnum1 >= fnum2) ? "TRUE" : "FALSE", len);
207 ast_copy_string(buf, (fnum1 <= fnum2) ? "TRUE" : "FALSE", len);
210 ast_copy_string(buf, (fnum1 == fnum2) ? "TRUE" : "FALSE", len);
214 "Something happened that neither of us should be proud of %d\n",
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);
233 static struct ast_custom_function math_function = {
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"
245 "Example: Set(i=${MATH(123%16,int)}) - sets var i=11",
249 static int unload_module(void)
251 return ast_custom_function_unregister(&math_function);
254 static int load_module(void)
256 return ast_custom_function_register(&math_function);
259 AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "Mathematical dialplan function");