189 char *mvalue1, *mvalue2 =
NULL, *mtype_of_result;
197 ast_log(
LOG_WARNING,
"Syntax: MATH(<number1><op><number 2>[,<type_of_result>]) - missing argument!\n");
204 ast_log(
LOG_WARNING,
"Syntax: MATH(<number1><op><number 2>[,<type_of_result>]) - missing argument!\n");
208 mvalue1 =
args.argv0;
210 if (mvalue1[0] ==
'-') {
215 if ((op = strchr(mvalue1,
'*'))) {
218 }
else if ((op = strchr(mvalue1,
'/'))) {
221 }
else if ((op = strchr(mvalue1,
'%'))) {
224 }
else if ((op = strchr(mvalue1,
'^'))) {
227 }
else if ((op = strstr(mvalue1,
"AND"))) {
231 }
else if ((op = strstr(mvalue1,
"XOR"))) {
235 }
else if ((op = strstr(mvalue1,
"OR"))) {
239 }
else if ((op = strchr(mvalue1,
'>'))) {
242 if (*(op + 1) ==
'=') {
245 }
else if (*(op + 1) ==
'>') {
249 }
else if ((op = strchr(mvalue1,
'<'))) {
252 if (*(op + 1) ==
'=') {
255 }
else if (*(op + 1) ==
'<') {
259 }
else if ((op = strchr(mvalue1,
'='))) {
261 if (*(op + 1) ==
'=') {
266 }
else if ((op = strchr(mvalue1,
'+'))) {
269 }
else if ((op = strchr(mvalue1,
'-'))) {
278 mtype_of_result =
args.argv1;
279 if (mtype_of_result) {
280 if (!strcasecmp(mtype_of_result,
"float")
281 || !strcasecmp(mtype_of_result,
"f"))
283 else if (!strcasecmp(mtype_of_result,
"int")
284 || !strcasecmp(mtype_of_result,
"i"))
286 else if (!strcasecmp(mtype_of_result,
"hex")
287 || !strcasecmp(mtype_of_result,
"h"))
289 else if (!strcasecmp(mtype_of_result,
"char")
290 || !strcasecmp(mtype_of_result,
"c"))
301 "Supply all the parameters - just this once, please\n");
305 if (sscanf(mvalue1,
"%30lf", &fnum1) != 1) {
310 if (sscanf(mvalue2,
"%30lf", &fnum2) != 1) {
320 ftmp = fnum1 + fnum2;
326 ftmp = (fnum1 / fnum2);
329 ftmp = (fnum1 * fnum2);
332 ftmp = (fnum1 - fnum2);
342 ftmp = (inum1 % inum2);
348 ftmp = pow(fnum1, fnum2);
355 ftmp = (inum1 << inum2);
363 ftmp = (inum1 >> inum2);
370 ftmp = (inum1 & inum2);
377 ftmp = (inum1 ^ inum2);
384 ftmp = (inum1 | inum2);
404 "Something happened that neither of us should be proud of %d\n",
409 if (iaction < GTFUNCTION || iaction >
EQFUNCTION) {
411 snprintf(buf, len,
"%f", ftmp);
413 snprintf(buf, len,
"%i", (
int) ftmp);
415 snprintf(buf, len,
"%x", (
unsigned int) ftmp);
417 snprintf(buf, len,
"%c", (
unsigned char) ftmp);
424 char *data,
char *
buf,
size_t len)
430 char endchar = 0, returnvar[12];
451 ast_log(
LOG_NOTICE,
"Variable %s doesn't exist - are you sure you wrote it correctly?\n", data);
456 if (sscanf(var,
"%30d%1c", &int_value, &endchar) == 0 || endchar != 0) {
457 ast_log(
LOG_NOTICE,
"The content of ${%s} is not a numeric value - bailing out!\n", data);
463 if (!strcasecmp(cmd,
"INC")) {
466 }
else if (!strcasecmp(cmd,
"DEC")) {
471 if (snprintf(returnvar,
sizeof(returnvar),
"%d", int_value) > 0) {
482 ast_log(
LOG_NOTICE,
"Variable %s refused to be %sREMENTED, setting value to 0", data, cmd);
492 char *
parse,
char *buffer,
size_t buflen)
494 double num1, num2, response_num = 0;
509 }
else if (sscanf(
args.num1,
"%30lf", &num1) != 1) {
516 }
else if (sscanf(
args.num2,
"%30lf", &num2) != 1) {
521 if (response_num == -1) {
524 response_num = (num1 > num2) ? num2 : num1;
527 ast_debug(1,
"%f is the minimum of [%f,%f]\n", response_num, num1, num2);
528 if ((
int) response_num == response_num) {
529 snprintf(buffer, buflen,
"%d", (
int) response_num);
531 snprintf(buffer, buflen,
"%f", response_num);
538 char *
parse,
char *buffer,
size_t buflen)
540 double num1, num2, response_num = 0;
555 }
else if (sscanf(
args.num1,
"%30lf", &num1) != 1) {
562 }
else if (sscanf(
args.num2,
"%30lf", &num2) != 1) {
567 if (response_num == -1) {
570 response_num = (num1 < num2) ? num2 : num1;
573 ast_debug(1,
"%f is the maximum of [%f,%f]\n", response_num, num1, num2);
574 if ((
int) response_num == response_num) {
575 snprintf(buffer, buflen,
"%d", (
int) response_num);
577 snprintf(buffer, buflen,
"%f", response_num);
584 char *
parse,
char *buffer,
size_t buflen)
586 double num1, response_num;
598 response_num = fabs(num1);
599 ast_debug(1,
"%f is the absolute value of %f\n", response_num, num1);
600 if ((
int) response_num == response_num) {
601 snprintf(buffer, buflen,
"%d", (
int) response_num);
603 snprintf(buffer, buflen,
"%f", response_num);
642 #ifdef TEST_FRAMEWORK 650 info->name =
"test_MATH_function";
651 info->category =
"/main/pbx/";
652 info->summary =
"Test MATH function substitution";
654 "Executes a series of variable substitutions using the MATH function and ensures that the expected results are received.";
static struct ast_custom_function math_function
#define ast_channel_lock(chan)
Main Channel structure associated with a channel.
static struct ast_custom_function acf_max
#define AST_MODULE_INFO_STANDARD(keystr, desc)
Asterisk main include file. File version handling, generic pbx functions.
static struct ast_custom_function decrement_function
static int unload_module(void)
#define AST_STANDARD_APP_ARGS(args, parse)
Performs the 'standard' argument separation process for an application.
char * ast_str_buffer(const struct ast_str *buf)
Returns the string buffer within the ast_str buf.
AST_TEST_DEFINE(test_MATH_function)
static struct ast_custom_function acf_abs
void ast_str_substitute_variables(struct ast_str **buf, ssize_t maxlen, struct ast_channel *chan, const char *templ)
#define AST_TEST_REGISTER(cb)
static struct ast_custom_function acf_min
int ast_custom_function_unregister(struct ast_custom_function *acf)
Unregister a custom function.
const char * pbx_builtin_getvar_helper(struct ast_channel *chan, const char *name)
Return a pointer to the value of the corresponding channel variable.
#define ast_strlen_zero(foo)
static int crement_function_read(struct ast_channel *chan, const char *cmd, char *data, char *buf, size_t len)
int ast_str_set(struct ast_str **buf, ssize_t max_len, const char *fmt,...)
Set a dynamic string using variable arguments.
Configuration File Parser.
#define ast_debug(level,...)
Log a DEBUG message.
static struct ast_custom_function increment_function
General Asterisk PBX channel definitions.
#define ast_test_status_update(a, b, c...)
Data structure associated with a custom dialplan function.
Conversion utility functions.
Core PBX routines and definitions.
The descriptor of a dynamic string XXX storage will be optimized later if needed We use the ts field ...
#define AST_TEST_UNREGISTER(cb)
static int len(struct ast_channel *chan, const char *cmd, char *data, char *buf, size_t buflen)
#define ast_channel_unlock(chan)
static void parse(struct mgcp_request *req)
static int acf_min_exec(struct ast_channel *chan, const char *cmd, char *parse, char *buffer, size_t buflen)
static int math(struct ast_channel *chan, const char *cmd, char *parse, char *buf, size_t len)
int pbx_builtin_setvar_helper(struct ast_channel *chan, const char *name, const char *value)
Add a variable to the channel variable stack, removing the most recently set value for the same name...
void ast_copy_string(char *dst, const char *src, size_t size)
Size-limited null-terminating string copy.
static int acf_abs_exec(struct ast_channel *chan, const char *cmd, char *parse, char *buffer, size_t buflen)
static int load_module(void)
static int acf_max_exec(struct ast_channel *chan, const char *cmd, char *parse, char *buffer, size_t buflen)
#define ASTERISK_GPL_KEY
The text the key() function should return.
Asterisk module definitions.
#define AST_DECLARE_APP_ARGS(name, arglist)
Declare a structure to hold an application's arguments.
Application convenience functions, designed to give consistent look and feel to Asterisk apps...
#define ast_custom_function_register(acf)
Register a custom function.
#define ast_str_create(init_len)
Create a malloc'ed dynamic length string.
#define AST_APP_ARG(name)
Define an application argument.