2005-06-09 22:59:08 +00:00
|
|
|
/*
|
2005-09-14 20:46:50 +00:00
|
|
|
* Asterisk -- An open source telephony toolkit.
|
2005-06-09 22:59:08 +00:00
|
|
|
*
|
2009-06-01 20:33:50 +00:00
|
|
|
* Copyright (C) 2004 - 2006, Andy Powell
|
2005-06-09 22:59:08 +00:00
|
|
|
*
|
|
|
|
* Updated by Mark Spencer <markster@digium.com>
|
2009-06-01 20:33:50 +00:00
|
|
|
* Updated by Nir Simionovich <nirs@greenfieldtech.net>
|
2005-06-09 22:59:08 +00:00
|
|
|
*
|
2005-09-14 20:46:50 +00:00
|
|
|
* See http://www.asterisk.org for more information about
|
|
|
|
* the Asterisk project. Please do not directly contact
|
|
|
|
* any of the maintainers of this project for assistance;
|
|
|
|
* the project provides a web site, mailing lists and IRC
|
|
|
|
* channels for your use.
|
|
|
|
*
|
|
|
|
* This program is free software, distributed under the terms of
|
|
|
|
* the GNU General Public License Version 2. See the LICENSE file
|
|
|
|
* at the top of the source tree.
|
|
|
|
*/
|
|
|
|
|
2005-10-24 20:12:06 +00:00
|
|
|
/*! \file
|
2005-09-14 20:46:50 +00:00
|
|
|
*
|
2006-02-12 04:28:58 +00:00
|
|
|
* \brief Math related dialplan function
|
2005-12-30 21:18:06 +00:00
|
|
|
*
|
|
|
|
* \author Andy Powell
|
|
|
|
* \author Mark Spencer <markster@digium.com>
|
2009-06-01 20:33:50 +00:00
|
|
|
* \author Nir Simionovich <nirs@greenfieldtech.net>
|
2007-01-24 09:05:29 +00:00
|
|
|
*
|
|
|
|
* \ingroup functions
|
2005-06-09 22:59:08 +00:00
|
|
|
*/
|
|
|
|
|
2011-07-14 20:28:54 +00:00
|
|
|
/*** MODULEINFO
|
|
|
|
<support_level>core</support_level>
|
|
|
|
***/
|
|
|
|
|
2006-06-07 18:54:56 +00:00
|
|
|
#include "asterisk.h"
|
|
|
|
|
git migration: Refactor the ASTERISK_FILE_VERSION macro
Git does not support the ability to replace a token with a version
string during check-in. While it does have support for replacing a
token on clone, this is somewhat sub-optimal: the token is replaced
with the object hash, which is not particularly easy for human
consumption. What's more, in practice, the source file version was often
not terribly useful. Generally, when triaging bugs, the overall version
of Asterisk is far more useful than an individual SVN version of a file. As a
result, this patch removes Asterisk's support for showing source file
versions.
Specifically, it does the following:
* Rename ASTERISK_FILE_VERSION macro to ASTERISK_REGISTER_FILE, and
remove passing the version in with the macro. Other facilities
than 'core show file version' make use of the file names, such as
setting a debug level only on a specific file. As such, the act of
registering source files with the Asterisk core still has use. The
macro rename now reflects the new macro purpose.
* main/asterisk:
- Refactor the file_version structure to reflect that it no longer
tracks a version field.
- Remove the "core show file version" CLI command. Without the file
version, it is no longer useful.
- Remove the ast_file_version_find function. The file version is no
longer tracked.
- Rename ast_register_file_version/ast_unregister_file_version to
ast_register_file/ast_unregister_file, respectively.
* main/manager: Remove value from the Version key of the ModuleCheck
Action. The actual key itself has not been removed, as doing so would
absolutely constitute a backwards incompatible change. However, since
the file version is no longer tracked, there is no need to attempt to
include it in the Version key.
* UPGRADE: Add notes for:
- Modification to the ModuleCheck AMI Action
- Removal of the "core show file version" CLI command
Change-Id: I6cf0ff280e1668bf4957dc21f32a5ff43444a40e
2015-04-11 21:38:22 -05:00
|
|
|
ASTERISK_REGISTER_FILE()
|
2006-06-07 18:54:56 +00:00
|
|
|
|
2006-12-31 05:20:18 +00:00
|
|
|
#include <math.h>
|
2005-06-09 22:59:08 +00:00
|
|
|
|
2006-02-11 03:14:05 +00:00
|
|
|
#include "asterisk/module.h"
|
2005-06-09 22:59:08 +00:00
|
|
|
#include "asterisk/channel.h"
|
|
|
|
#include "asterisk/pbx.h"
|
|
|
|
#include "asterisk/utils.h"
|
|
|
|
#include "asterisk/app.h"
|
|
|
|
#include "asterisk/config.h"
|
2010-02-02 18:54:33 +00:00
|
|
|
#include "asterisk/test.h"
|
2005-06-09 22:59:08 +00:00
|
|
|
|
2008-11-01 21:10:07 +00:00
|
|
|
/*** DOCUMENTATION
|
|
|
|
<function name="MATH" language="en_US">
|
|
|
|
<synopsis>
|
|
|
|
Performs Mathematical Functions.
|
|
|
|
</synopsis>
|
|
|
|
<syntax>
|
|
|
|
<parameter name="expression" required="true">
|
|
|
|
<para>Is of the form:
|
|
|
|
<replaceable>number1</replaceable><replaceable>op</replaceable><replaceable>number2</replaceable>
|
|
|
|
where the possible values for <replaceable>op</replaceable>
|
|
|
|
are:</para>
|
2012-05-31 18:39:30 +00:00
|
|
|
<para>+,-,/,*,%,<<,>>,^,AND,OR,XOR,<,>,<=,>=,== (and behave as their C equivalents)</para>
|
2008-11-01 21:10:07 +00:00
|
|
|
</parameter>
|
|
|
|
<parameter name="type">
|
|
|
|
<para>Wanted type of result:</para>
|
|
|
|
<para>f, float - float(default)</para>
|
|
|
|
<para>i, int - integer</para>
|
|
|
|
<para>h, hex - hex</para>
|
|
|
|
<para>c, char - char</para>
|
|
|
|
</parameter>
|
|
|
|
</syntax>
|
|
|
|
<description>
|
2008-11-02 02:50:33 +00:00
|
|
|
<para>Performs mathematical functions based on two parameters and an operator. The returned
|
2008-11-01 21:10:07 +00:00
|
|
|
value type is <replaceable>type</replaceable></para>
|
|
|
|
<para>Example: Set(i=${MATH(123%16,int)}) - sets var i=11</para>
|
|
|
|
</description>
|
|
|
|
</function>
|
2009-06-01 20:33:50 +00:00
|
|
|
<function name="INC" language="en_US">
|
|
|
|
<synopsis>
|
|
|
|
Increments the value of a variable, while returning the updated value to the dialplan
|
|
|
|
</synopsis>
|
|
|
|
<syntax>
|
|
|
|
<parameter name="variable" required="true">
|
|
|
|
<para>
|
|
|
|
The variable name to be manipulated, without the braces.
|
|
|
|
</para>
|
|
|
|
</parameter>
|
|
|
|
</syntax>
|
|
|
|
<description>
|
|
|
|
<para>Increments the value of a variable, while returning the updated value to the dialplan</para>
|
|
|
|
<para>Example: INC(MyVAR) - Increments MyVar</para>
|
|
|
|
<para>Note: INC(${MyVAR}) - Is wrong, as INC expects the variable name, not its value</para>
|
|
|
|
</description>
|
|
|
|
</function>
|
|
|
|
<function name="DEC" language="en_US">
|
|
|
|
<synopsis>
|
|
|
|
Decrements the value of a variable, while returning the updated value to the dialplan
|
|
|
|
</synopsis>
|
|
|
|
<syntax>
|
|
|
|
<parameter name="variable" required="true">
|
|
|
|
<para>
|
|
|
|
The variable name to be manipulated, without the braces.
|
|
|
|
</para>
|
|
|
|
</parameter>
|
|
|
|
</syntax>
|
|
|
|
<description>
|
|
|
|
<para>Decrements the value of a variable, while returning the updated value to the dialplan</para>
|
2012-07-12 14:38:44 +00:00
|
|
|
<para>Example: DEC(MyVAR) - Decrements MyVar</para>
|
|
|
|
<para>Note: DEC(${MyVAR}) - Is wrong, as DEC expects the variable name, not its value</para>
|
2009-06-01 20:33:50 +00:00
|
|
|
</description>
|
|
|
|
</function>
|
2008-11-01 21:10:07 +00:00
|
|
|
***/
|
|
|
|
|
2006-02-12 04:28:58 +00:00
|
|
|
enum TypeOfFunctions {
|
|
|
|
ADDFUNCTION,
|
|
|
|
DIVIDEFUNCTION,
|
|
|
|
MULTIPLYFUNCTION,
|
|
|
|
SUBTRACTFUNCTION,
|
|
|
|
MODULUSFUNCTION,
|
2006-12-31 05:20:18 +00:00
|
|
|
POWFUNCTION,
|
|
|
|
SHLEFTFUNCTION,
|
|
|
|
SHRIGHTFUNCTION,
|
2007-06-28 20:52:22 +00:00
|
|
|
BITWISEANDFUNCTION,
|
|
|
|
BITWISEXORFUNCTION,
|
|
|
|
BITWISEORFUNCTION,
|
2006-02-12 04:28:58 +00:00
|
|
|
GTFUNCTION,
|
|
|
|
LTFUNCTION,
|
|
|
|
GTEFUNCTION,
|
|
|
|
LTEFUNCTION,
|
|
|
|
EQFUNCTION
|
2005-06-09 22:59:08 +00:00
|
|
|
};
|
|
|
|
|
2006-02-12 04:28:58 +00:00
|
|
|
enum TypeOfResult {
|
|
|
|
FLOAT_RESULT,
|
|
|
|
INT_RESULT,
|
|
|
|
HEX_RESULT,
|
|
|
|
CHAR_RESULT
|
2005-06-09 22:59:08 +00:00
|
|
|
};
|
|
|
|
|
2007-01-06 00:13:33 +00:00
|
|
|
static int math(struct ast_channel *chan, const char *cmd, char *parse,
|
2006-02-12 04:28:58 +00:00
|
|
|
char *buf, size_t len)
|
2005-06-09 22:59:08 +00:00
|
|
|
{
|
2007-06-04 23:45:05 +00:00
|
|
|
double fnum1;
|
|
|
|
double fnum2;
|
|
|
|
double ftmp = 0;
|
2005-06-09 22:59:08 +00:00
|
|
|
char *op;
|
2006-02-12 04:28:58 +00:00
|
|
|
int iaction = -1;
|
|
|
|
int type_of_result = FLOAT_RESULT;
|
|
|
|
char *mvalue1, *mvalue2 = NULL, *mtype_of_result;
|
2007-05-24 15:35:50 +00:00
|
|
|
int negvalue1 = 0;
|
2006-01-10 16:08:28 +00:00
|
|
|
AST_DECLARE_APP_ARGS(args,
|
2006-02-12 04:28:58 +00:00
|
|
|
AST_APP_ARG(argv0);
|
|
|
|
AST_APP_ARG(argv1);
|
2006-01-10 16:08:28 +00:00
|
|
|
);
|
2006-02-12 04:28:58 +00:00
|
|
|
|
|
|
|
if (ast_strlen_zero(parse)) {
|
2008-10-06 21:09:05 +00:00
|
|
|
ast_log(LOG_WARNING, "Syntax: MATH(<number1><op><number 2>[,<type_of_result>]) - missing argument!\n");
|
2006-02-12 04:28:58 +00:00
|
|
|
return -1;
|
2005-06-09 22:59:08 +00:00
|
|
|
}
|
|
|
|
|
2006-01-10 16:08:28 +00:00
|
|
|
AST_STANDARD_APP_ARGS(args, parse);
|
2006-02-12 04:28:58 +00:00
|
|
|
|
2006-01-10 16:08:28 +00:00
|
|
|
if (args.argc < 1) {
|
2008-10-06 21:09:05 +00:00
|
|
|
ast_log(LOG_WARNING, "Syntax: MATH(<number1><op><number 2>[,<type_of_result>]) - missing argument!\n");
|
2006-02-12 04:28:58 +00:00
|
|
|
return -1;
|
2005-06-09 22:59:08 +00:00
|
|
|
}
|
|
|
|
|
2006-01-10 16:08:28 +00:00
|
|
|
mvalue1 = args.argv0;
|
2006-02-12 04:28:58 +00:00
|
|
|
|
2007-05-24 15:35:50 +00:00
|
|
|
if (mvalue1[0] == '-') {
|
|
|
|
negvalue1 = 1;
|
|
|
|
mvalue1++;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ((op = strchr(mvalue1, '*'))) {
|
2005-06-09 22:59:08 +00:00
|
|
|
iaction = MULTIPLYFUNCTION;
|
|
|
|
*op = '\0';
|
|
|
|
} else if ((op = strchr(mvalue1, '/'))) {
|
|
|
|
iaction = DIVIDEFUNCTION;
|
|
|
|
*op = '\0';
|
|
|
|
} else if ((op = strchr(mvalue1, '%'))) {
|
|
|
|
iaction = MODULUSFUNCTION;
|
|
|
|
*op = '\0';
|
2006-12-31 05:20:18 +00:00
|
|
|
} else if ((op = strchr(mvalue1, '^'))) {
|
|
|
|
iaction = POWFUNCTION;
|
|
|
|
*op = '\0';
|
2007-06-28 20:52:22 +00:00
|
|
|
} else if ((op = strstr(mvalue1, "AND"))) {
|
|
|
|
iaction = BITWISEANDFUNCTION;
|
|
|
|
*op = '\0';
|
2010-02-02 18:54:33 +00:00
|
|
|
op += 2;
|
2007-06-28 20:52:22 +00:00
|
|
|
} else if ((op = strstr(mvalue1, "XOR"))) {
|
|
|
|
iaction = BITWISEXORFUNCTION;
|
|
|
|
*op = '\0';
|
2010-02-02 18:54:33 +00:00
|
|
|
op += 2;
|
2007-06-28 20:52:22 +00:00
|
|
|
} else if ((op = strstr(mvalue1, "OR"))) {
|
|
|
|
iaction = BITWISEORFUNCTION;
|
|
|
|
*op = '\0';
|
2010-02-02 18:54:33 +00:00
|
|
|
++op;
|
2005-06-09 22:59:08 +00:00
|
|
|
} else if ((op = strchr(mvalue1, '>'))) {
|
|
|
|
iaction = GTFUNCTION;
|
|
|
|
*op = '\0';
|
2006-02-12 04:28:58 +00:00
|
|
|
if (*(op + 1) == '=') {
|
2005-06-09 22:59:08 +00:00
|
|
|
iaction = GTEFUNCTION;
|
2010-02-02 18:54:33 +00:00
|
|
|
++op;
|
2006-12-31 05:20:18 +00:00
|
|
|
} else if (*(op + 1) == '>') {
|
|
|
|
iaction = SHRIGHTFUNCTION;
|
2010-02-02 18:54:33 +00:00
|
|
|
++op;
|
2005-06-09 22:59:08 +00:00
|
|
|
}
|
|
|
|
} else if ((op = strchr(mvalue1, '<'))) {
|
|
|
|
iaction = LTFUNCTION;
|
|
|
|
*op = '\0';
|
2006-02-12 04:28:58 +00:00
|
|
|
if (*(op + 1) == '=') {
|
2005-06-09 22:59:08 +00:00
|
|
|
iaction = LTEFUNCTION;
|
2010-02-02 18:54:33 +00:00
|
|
|
++op;
|
2006-12-31 05:20:18 +00:00
|
|
|
} else if (*(op + 1) == '<') {
|
|
|
|
iaction = SHLEFTFUNCTION;
|
2010-02-02 18:54:33 +00:00
|
|
|
++op;
|
2005-06-09 22:59:08 +00:00
|
|
|
}
|
|
|
|
} else if ((op = strchr(mvalue1, '='))) {
|
|
|
|
*op = '\0';
|
2006-02-12 04:28:58 +00:00
|
|
|
if (*(op + 1) == '=') {
|
2005-06-09 22:59:08 +00:00
|
|
|
iaction = EQFUNCTION;
|
2010-02-02 18:54:33 +00:00
|
|
|
++op;
|
2005-06-09 22:59:08 +00:00
|
|
|
} else
|
|
|
|
op = NULL;
|
2007-05-24 15:35:50 +00:00
|
|
|
} else if ((op = strchr(mvalue1, '+'))) {
|
|
|
|
iaction = ADDFUNCTION;
|
|
|
|
*op = '\0';
|
2010-02-02 18:54:33 +00:00
|
|
|
} else if ((op = strchr(mvalue1, '-'))) { /* subtraction MUST always be last, in case we have a negative second number */
|
2007-05-24 15:35:50 +00:00
|
|
|
iaction = SUBTRACTFUNCTION;
|
|
|
|
*op = '\0';
|
2006-02-12 04:28:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (op)
|
2005-06-09 22:59:08 +00:00
|
|
|
mvalue2 = op + 1;
|
|
|
|
|
|
|
|
/* detect wanted type of result */
|
2006-01-10 16:08:28 +00:00
|
|
|
mtype_of_result = args.argv1;
|
2006-02-12 04:28:58 +00:00
|
|
|
if (mtype_of_result) {
|
|
|
|
if (!strcasecmp(mtype_of_result, "float")
|
|
|
|
|| !strcasecmp(mtype_of_result, "f"))
|
|
|
|
type_of_result = FLOAT_RESULT;
|
|
|
|
else if (!strcasecmp(mtype_of_result, "int")
|
|
|
|
|| !strcasecmp(mtype_of_result, "i"))
|
|
|
|
type_of_result = INT_RESULT;
|
|
|
|
else if (!strcasecmp(mtype_of_result, "hex")
|
|
|
|
|| !strcasecmp(mtype_of_result, "h"))
|
|
|
|
type_of_result = HEX_RESULT;
|
|
|
|
else if (!strcasecmp(mtype_of_result, "char")
|
|
|
|
|| !strcasecmp(mtype_of_result, "c"))
|
|
|
|
type_of_result = CHAR_RESULT;
|
|
|
|
else {
|
|
|
|
ast_log(LOG_WARNING, "Unknown type of result requested '%s'.\n",
|
|
|
|
mtype_of_result);
|
|
|
|
return -1;
|
2005-06-09 22:59:08 +00:00
|
|
|
}
|
|
|
|
}
|
2006-02-12 04:28:58 +00:00
|
|
|
|
2012-05-31 18:39:30 +00:00
|
|
|
if (!mvalue2) {
|
2006-02-12 04:28:58 +00:00
|
|
|
ast_log(LOG_WARNING,
|
|
|
|
"Supply all the parameters - just this once, please\n");
|
|
|
|
return -1;
|
2005-06-09 22:59:08 +00:00
|
|
|
}
|
|
|
|
|
2009-08-10 19:20:57 +00:00
|
|
|
if (sscanf(mvalue1, "%30lf", &fnum1) != 1) {
|
2005-06-09 22:59:08 +00:00
|
|
|
ast_log(LOG_WARNING, "'%s' is not a valid number\n", mvalue1);
|
2006-02-12 04:28:58 +00:00
|
|
|
return -1;
|
2005-06-09 22:59:08 +00:00
|
|
|
}
|
|
|
|
|
2009-08-10 19:20:57 +00:00
|
|
|
if (sscanf(mvalue2, "%30lf", &fnum2) != 1) {
|
2005-06-09 22:59:08 +00:00
|
|
|
ast_log(LOG_WARNING, "'%s' is not a valid number\n", mvalue2);
|
2006-02-12 04:28:58 +00:00
|
|
|
return -1;
|
2005-06-09 22:59:08 +00:00
|
|
|
}
|
|
|
|
|
2007-05-24 15:35:50 +00:00
|
|
|
if (negvalue1)
|
|
|
|
fnum1 = 0 - fnum1;
|
|
|
|
|
2005-06-09 22:59:08 +00:00
|
|
|
switch (iaction) {
|
2006-02-12 04:28:58 +00:00
|
|
|
case ADDFUNCTION:
|
2005-06-09 22:59:08 +00:00
|
|
|
ftmp = fnum1 + fnum2;
|
|
|
|
break;
|
2006-02-12 04:28:58 +00:00
|
|
|
case DIVIDEFUNCTION:
|
2005-06-09 22:59:08 +00:00
|
|
|
if (fnum2 <= 0)
|
2006-02-12 04:28:58 +00:00
|
|
|
ftmp = 0; /* can't do a divide by 0 */
|
2005-06-09 22:59:08 +00:00
|
|
|
else
|
|
|
|
ftmp = (fnum1 / fnum2);
|
|
|
|
break;
|
2006-02-12 04:28:58 +00:00
|
|
|
case MULTIPLYFUNCTION:
|
2005-06-09 22:59:08 +00:00
|
|
|
ftmp = (fnum1 * fnum2);
|
|
|
|
break;
|
2006-02-12 04:28:58 +00:00
|
|
|
case SUBTRACTFUNCTION:
|
2005-06-09 22:59:08 +00:00
|
|
|
ftmp = (fnum1 - fnum2);
|
|
|
|
break;
|
2006-02-12 04:28:58 +00:00
|
|
|
case MODULUSFUNCTION:
|
|
|
|
{
|
|
|
|
int inum1 = fnum1;
|
|
|
|
int inum2 = fnum2;
|
|
|
|
|
2010-01-21 05:54:30 +00:00
|
|
|
if (inum2 == 0) {
|
|
|
|
ftmp = 0;
|
|
|
|
} else {
|
|
|
|
ftmp = (inum1 % inum2);
|
|
|
|
}
|
2006-02-12 04:28:58 +00:00
|
|
|
|
2006-12-31 05:20:18 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
case POWFUNCTION:
|
|
|
|
ftmp = pow(fnum1, fnum2);
|
|
|
|
break;
|
|
|
|
case SHLEFTFUNCTION:
|
|
|
|
{
|
|
|
|
int inum1 = fnum1;
|
|
|
|
int inum2 = fnum2;
|
|
|
|
|
|
|
|
ftmp = (inum1 << inum2);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case SHRIGHTFUNCTION:
|
|
|
|
{
|
|
|
|
int inum1 = fnum1;
|
|
|
|
int inum2 = fnum2;
|
|
|
|
|
|
|
|
ftmp = (inum1 >> inum2);
|
2006-02-12 04:28:58 +00:00
|
|
|
break;
|
|
|
|
}
|
2007-06-28 20:52:22 +00:00
|
|
|
case BITWISEANDFUNCTION:
|
|
|
|
{
|
|
|
|
int inum1 = fnum1;
|
|
|
|
int inum2 = fnum2;
|
|
|
|
ftmp = (inum1 & inum2);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case BITWISEXORFUNCTION:
|
|
|
|
{
|
|
|
|
int inum1 = fnum1;
|
|
|
|
int inum2 = fnum2;
|
|
|
|
ftmp = (inum1 ^ inum2);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case BITWISEORFUNCTION:
|
|
|
|
{
|
|
|
|
int inum1 = fnum1;
|
|
|
|
int inum2 = fnum2;
|
|
|
|
ftmp = (inum1 | inum2);
|
|
|
|
break;
|
|
|
|
}
|
2006-02-12 04:28:58 +00:00
|
|
|
case GTFUNCTION:
|
|
|
|
ast_copy_string(buf, (fnum1 > fnum2) ? "TRUE" : "FALSE", len);
|
2005-06-09 22:59:08 +00:00
|
|
|
break;
|
2006-02-12 04:28:58 +00:00
|
|
|
case LTFUNCTION:
|
|
|
|
ast_copy_string(buf, (fnum1 < fnum2) ? "TRUE" : "FALSE", len);
|
2005-06-09 22:59:08 +00:00
|
|
|
break;
|
2006-02-12 04:28:58 +00:00
|
|
|
case GTEFUNCTION:
|
|
|
|
ast_copy_string(buf, (fnum1 >= fnum2) ? "TRUE" : "FALSE", len);
|
2005-06-09 22:59:08 +00:00
|
|
|
break;
|
2006-02-12 04:28:58 +00:00
|
|
|
case LTEFUNCTION:
|
|
|
|
ast_copy_string(buf, (fnum1 <= fnum2) ? "TRUE" : "FALSE", len);
|
2005-06-09 22:59:08 +00:00
|
|
|
break;
|
2006-02-12 04:28:58 +00:00
|
|
|
case EQFUNCTION:
|
|
|
|
ast_copy_string(buf, (fnum1 == fnum2) ? "TRUE" : "FALSE", len);
|
2005-06-09 22:59:08 +00:00
|
|
|
break;
|
2006-02-12 04:28:58 +00:00
|
|
|
default:
|
|
|
|
ast_log(LOG_WARNING,
|
|
|
|
"Something happened that neither of us should be proud of %d\n",
|
|
|
|
iaction);
|
|
|
|
return -1;
|
2005-06-09 22:59:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (iaction < GTFUNCTION || iaction > EQFUNCTION) {
|
2006-02-12 04:28:58 +00:00
|
|
|
if (type_of_result == FLOAT_RESULT)
|
|
|
|
snprintf(buf, len, "%f", ftmp);
|
|
|
|
else if (type_of_result == INT_RESULT)
|
|
|
|
snprintf(buf, len, "%i", (int) ftmp);
|
|
|
|
else if (type_of_result == HEX_RESULT)
|
|
|
|
snprintf(buf, len, "%x", (unsigned int) ftmp);
|
|
|
|
else if (type_of_result == CHAR_RESULT)
|
|
|
|
snprintf(buf, len, "%c", (unsigned char) ftmp);
|
2005-06-09 22:59:08 +00:00
|
|
|
}
|
2006-02-12 04:28:58 +00:00
|
|
|
|
|
|
|
return 0;
|
2005-06-09 22:59:08 +00:00
|
|
|
}
|
|
|
|
|
2009-06-01 20:33:50 +00:00
|
|
|
static int crement_function_read(struct ast_channel *chan, const char *cmd,
|
|
|
|
char *data, char *buf, size_t len)
|
|
|
|
{
|
|
|
|
int ret = -1;
|
|
|
|
int int_value = 0;
|
|
|
|
int modify_orig = 0;
|
|
|
|
const char *var;
|
|
|
|
char endchar = 0, returnvar[12]; /* If you need a variable longer than 11 digits - something is way wrong */
|
|
|
|
|
|
|
|
if (ast_strlen_zero(data)) {
|
|
|
|
ast_log(LOG_WARNING, "Syntax: %s(<data>) - missing argument!\n", cmd);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2014-03-27 19:21:44 +00:00
|
|
|
if (!chan) {
|
|
|
|
ast_log(LOG_WARNING, "No channel was provided to %s function.\n", cmd);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2009-06-01 20:33:50 +00:00
|
|
|
ast_channel_lock(chan);
|
|
|
|
|
|
|
|
if (!(var = pbx_builtin_getvar_helper(chan, data))) {
|
|
|
|
ast_log(LOG_NOTICE, "Failed to obtain variable %s, bailing out\n", data);
|
|
|
|
ast_channel_unlock(chan);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (ast_strlen_zero(var)) {
|
2009-07-30 16:07:05 +00:00
|
|
|
ast_log(LOG_NOTICE, "Variable %s doesn't exist - are you sure you wrote it correctly?\n", data);
|
2009-06-01 20:33:50 +00:00
|
|
|
ast_channel_unlock(chan);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2009-08-10 19:20:57 +00:00
|
|
|
if (sscanf(var, "%30d%1c", &int_value, &endchar) == 0 || endchar != 0) {
|
2009-06-01 20:33:50 +00:00
|
|
|
ast_log(LOG_NOTICE, "The content of ${%s} is not a numeric value - bailing out!\n", data);
|
|
|
|
ast_channel_unlock(chan);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* now we'll actually do something useful */
|
|
|
|
if (!strcasecmp(cmd, "INC")) { /* Increment variable */
|
|
|
|
int_value++;
|
|
|
|
modify_orig = 1;
|
|
|
|
} else if (!strcasecmp(cmd, "DEC")) { /* Decrement variable */
|
|
|
|
int_value--;
|
|
|
|
modify_orig = 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (snprintf(returnvar, sizeof(returnvar), "%d", int_value) > 0) {
|
|
|
|
pbx_builtin_setvar_helper(chan, data, returnvar);
|
|
|
|
if (modify_orig) {
|
|
|
|
ast_copy_string(buf, returnvar, len);
|
|
|
|
}
|
|
|
|
ret = 0;
|
|
|
|
} else {
|
|
|
|
pbx_builtin_setvar_helper(chan, data, "0");
|
|
|
|
if (modify_orig) {
|
|
|
|
ast_copy_string(buf, "0", len);
|
|
|
|
}
|
|
|
|
ast_log(LOG_NOTICE, "Variable %s refused to be %sREMENTED, setting value to 0", data, cmd);
|
|
|
|
ret = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
ast_channel_unlock(chan);
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2006-02-11 03:14:05 +00:00
|
|
|
static struct ast_custom_function math_function = {
|
2005-06-09 22:59:08 +00:00
|
|
|
.name = "MATH",
|
2006-02-11 03:14:05 +00:00
|
|
|
.read = math
|
2005-06-09 22:59:08 +00:00
|
|
|
};
|
2006-02-11 03:14:05 +00:00
|
|
|
|
2009-06-01 20:33:50 +00:00
|
|
|
static struct ast_custom_function increment_function = {
|
|
|
|
.name = "INC",
|
|
|
|
.read = crement_function_read,
|
|
|
|
};
|
|
|
|
|
|
|
|
static struct ast_custom_function decrement_function = {
|
|
|
|
.name = "DEC",
|
|
|
|
.read = crement_function_read,
|
|
|
|
};
|
|
|
|
|
2010-02-02 18:54:33 +00:00
|
|
|
#ifdef TEST_FRAMEWORK
|
|
|
|
AST_TEST_DEFINE(test_MATH_function)
|
|
|
|
{
|
|
|
|
enum ast_test_result_state res = AST_TEST_PASS;
|
|
|
|
struct ast_str *expr, *result;
|
|
|
|
|
|
|
|
switch (cmd) {
|
|
|
|
case TEST_INIT:
|
|
|
|
info->name = "test_MATH_function";
|
2010-07-09 17:00:22 +00:00
|
|
|
info->category = "/main/pbx/";
|
2010-02-02 18:54:33 +00:00
|
|
|
info->summary = "Test MATH function substitution";
|
|
|
|
info->description =
|
|
|
|
"Executes a series of variable substitutions using the MATH function and ensures that the expected results are received.";
|
|
|
|
return AST_TEST_NOT_RUN;
|
|
|
|
case TEST_EXECUTE:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
Various updates to the unit test API.
1) It occurred to me that the difference in usage between the error ast_str and
the ast_test_update_status() usage has turned out to be a bit ambiguous in
practice. In a lot of cases, the same message was being sent to both.
In other cases, it was only sent to one or the other. My opinion now is that
in every case, I think it makes sense to do both; we should output it to the
CLI as well as save it off for logging purposes.
This change results in most of the changes in this diff, since it required
changes to all existing unit tests. It also allowed for some simplifications
of unit test API implementation code.
2) Update ast_test_status_update() to include the file, function, and line
number for the code providing the update.
3) There are some formatting tweaks here and there. Hopefully they aren't too
distracting for code review purposes. Reviewboard's diff viewer seems to do a
pretty good job of pointing out when something is a whitespace change.
4) I moved the md5_test and sha1_test into the test_utils module. It seemed
like a better approach since these tests are so tiny.
5) I changed the number of nodes used in heap_test_2 from 1 million to
100 thousand. The only reason for this was to reduce the time it took
for this test to run.
6) Remove an unused function prototype that was at the bottom of utils.h.
7) Simplify test_insert() using the LIST_INSERT_SORTALPHA() macro. The one
minor difference in behavior is that it no longer checks for a test registered
with the same name.
8) Expand the code in test_alloc() to provide specific error messages for each
failure case, to clearly inform developers if they forget to set the name,
summary, description, etc.
9) Tweak the output of the "test show registered" CLI command. I swapped the
name and category to have the category first. It seemed more natural since
that is the sort key.
10) Don't output the status ast_str in the "test show results" CLI command.
This is going to tend to be pretty verbose, so just leave that for the
detailed test logs (test generate results).
Review: https://reviewboard.asterisk.org/r/493/
git-svn-id: https://origsvn.digium.com/svn/asterisk/trunk@245864 65c4cc65-6c06-0410-ace0-fbb531ad65f3
2010-02-09 23:32:14 +00:00
|
|
|
ast_test_status_update(test, "Testing MATH() substitution ...\n");
|
2010-02-02 18:54:33 +00:00
|
|
|
|
2015-04-07 02:10:31 +00:00
|
|
|
if (!(expr = ast_str_create(16))) {
|
|
|
|
return AST_TEST_FAIL;
|
|
|
|
}
|
|
|
|
if (!(result = ast_str_create(16))) {
|
|
|
|
ast_free(expr);
|
2010-02-02 18:54:33 +00:00
|
|
|
return AST_TEST_FAIL;
|
|
|
|
}
|
|
|
|
|
|
|
|
ast_str_set(&expr, 0, "${MATH(170 AND 63,i)}");
|
|
|
|
ast_str_substitute_variables(&result, 0, NULL, ast_str_buffer(expr));
|
|
|
|
if (strcmp(ast_str_buffer(result), "42") != 0) {
|
Various updates to the unit test API.
1) It occurred to me that the difference in usage between the error ast_str and
the ast_test_update_status() usage has turned out to be a bit ambiguous in
practice. In a lot of cases, the same message was being sent to both.
In other cases, it was only sent to one or the other. My opinion now is that
in every case, I think it makes sense to do both; we should output it to the
CLI as well as save it off for logging purposes.
This change results in most of the changes in this diff, since it required
changes to all existing unit tests. It also allowed for some simplifications
of unit test API implementation code.
2) Update ast_test_status_update() to include the file, function, and line
number for the code providing the update.
3) There are some formatting tweaks here and there. Hopefully they aren't too
distracting for code review purposes. Reviewboard's diff viewer seems to do a
pretty good job of pointing out when something is a whitespace change.
4) I moved the md5_test and sha1_test into the test_utils module. It seemed
like a better approach since these tests are so tiny.
5) I changed the number of nodes used in heap_test_2 from 1 million to
100 thousand. The only reason for this was to reduce the time it took
for this test to run.
6) Remove an unused function prototype that was at the bottom of utils.h.
7) Simplify test_insert() using the LIST_INSERT_SORTALPHA() macro. The one
minor difference in behavior is that it no longer checks for a test registered
with the same name.
8) Expand the code in test_alloc() to provide specific error messages for each
failure case, to clearly inform developers if they forget to set the name,
summary, description, etc.
9) Tweak the output of the "test show registered" CLI command. I swapped the
name and category to have the category first. It seemed more natural since
that is the sort key.
10) Don't output the status ast_str in the "test show results" CLI command.
This is going to tend to be pretty verbose, so just leave that for the
detailed test logs (test generate results).
Review: https://reviewboard.asterisk.org/r/493/
git-svn-id: https://origsvn.digium.com/svn/asterisk/trunk@245864 65c4cc65-6c06-0410-ace0-fbb531ad65f3
2010-02-09 23:32:14 +00:00
|
|
|
ast_test_status_update(test, "Expected result '42' not returned! ('%s')\n",
|
|
|
|
ast_str_buffer(result));
|
2010-02-02 18:54:33 +00:00
|
|
|
res = AST_TEST_FAIL;
|
|
|
|
}
|
|
|
|
|
|
|
|
ast_str_set(&expr, 0, "${MATH(170AND63,i)}");
|
|
|
|
ast_str_substitute_variables(&result, 0, NULL, ast_str_buffer(expr));
|
|
|
|
if (strcmp(ast_str_buffer(result), "42") != 0) {
|
Various updates to the unit test API.
1) It occurred to me that the difference in usage between the error ast_str and
the ast_test_update_status() usage has turned out to be a bit ambiguous in
practice. In a lot of cases, the same message was being sent to both.
In other cases, it was only sent to one or the other. My opinion now is that
in every case, I think it makes sense to do both; we should output it to the
CLI as well as save it off for logging purposes.
This change results in most of the changes in this diff, since it required
changes to all existing unit tests. It also allowed for some simplifications
of unit test API implementation code.
2) Update ast_test_status_update() to include the file, function, and line
number for the code providing the update.
3) There are some formatting tweaks here and there. Hopefully they aren't too
distracting for code review purposes. Reviewboard's diff viewer seems to do a
pretty good job of pointing out when something is a whitespace change.
4) I moved the md5_test and sha1_test into the test_utils module. It seemed
like a better approach since these tests are so tiny.
5) I changed the number of nodes used in heap_test_2 from 1 million to
100 thousand. The only reason for this was to reduce the time it took
for this test to run.
6) Remove an unused function prototype that was at the bottom of utils.h.
7) Simplify test_insert() using the LIST_INSERT_SORTALPHA() macro. The one
minor difference in behavior is that it no longer checks for a test registered
with the same name.
8) Expand the code in test_alloc() to provide specific error messages for each
failure case, to clearly inform developers if they forget to set the name,
summary, description, etc.
9) Tweak the output of the "test show registered" CLI command. I swapped the
name and category to have the category first. It seemed more natural since
that is the sort key.
10) Don't output the status ast_str in the "test show results" CLI command.
This is going to tend to be pretty verbose, so just leave that for the
detailed test logs (test generate results).
Review: https://reviewboard.asterisk.org/r/493/
git-svn-id: https://origsvn.digium.com/svn/asterisk/trunk@245864 65c4cc65-6c06-0410-ace0-fbb531ad65f3
2010-02-09 23:32:14 +00:00
|
|
|
ast_test_status_update(test, "Expected result '42' not returned! ('%s')\n",
|
|
|
|
ast_str_buffer(result));
|
2010-02-02 18:54:33 +00:00
|
|
|
res = AST_TEST_FAIL;
|
|
|
|
}
|
|
|
|
|
2013-10-23 20:10:30 +00:00
|
|
|
ast_free(expr);
|
|
|
|
ast_free(result);
|
|
|
|
|
2010-02-02 18:54:33 +00:00
|
|
|
return res;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2006-08-21 02:11:39 +00:00
|
|
|
static int unload_module(void)
|
2006-02-11 03:14:05 +00:00
|
|
|
{
|
2009-06-01 20:33:50 +00:00
|
|
|
int res = 0;
|
|
|
|
|
|
|
|
res |= ast_custom_function_unregister(&math_function);
|
|
|
|
res |= ast_custom_function_unregister(&increment_function);
|
|
|
|
res |= ast_custom_function_unregister(&decrement_function);
|
2010-02-02 18:54:33 +00:00
|
|
|
AST_TEST_UNREGISTER(test_MATH_function);
|
2009-06-01 20:33:50 +00:00
|
|
|
|
|
|
|
return res;
|
2006-02-11 03:14:05 +00:00
|
|
|
}
|
|
|
|
|
2006-08-21 02:11:39 +00:00
|
|
|
static int load_module(void)
|
2006-02-11 03:14:05 +00:00
|
|
|
{
|
2009-06-01 20:33:50 +00:00
|
|
|
int res = 0;
|
|
|
|
|
|
|
|
res |= ast_custom_function_register(&math_function);
|
|
|
|
res |= ast_custom_function_register(&increment_function);
|
|
|
|
res |= ast_custom_function_register(&decrement_function);
|
2010-02-02 18:54:33 +00:00
|
|
|
AST_TEST_REGISTER(test_MATH_function);
|
2009-06-01 20:33:50 +00:00
|
|
|
|
|
|
|
return res;
|
2006-02-11 03:14:05 +00:00
|
|
|
}
|
|
|
|
|
2006-08-21 02:11:39 +00:00
|
|
|
AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "Mathematical dialplan function");
|