211 char *mvalue1, *mvalue2 =
NULL, *mtype_of_result;
219 ast_log(
LOG_WARNING,
"Syntax: MATH(<number1><op><number 2>[,<type_of_result>]) - missing argument!\n");
226 ast_log(
LOG_WARNING,
"Syntax: MATH(<number1><op><number 2>[,<type_of_result>]) - missing argument!\n");
230 mvalue1 =
args.argv0;
232 if (mvalue1[0] ==
'-') {
237 if ((op = strchr(mvalue1,
'*'))) {
240 }
else if ((op = strchr(mvalue1,
'/'))) {
243 }
else if ((op = strchr(mvalue1,
'%'))) {
246 }
else if ((op = strchr(mvalue1,
'^'))) {
249 }
else if ((op = strstr(mvalue1,
"AND"))) {
253 }
else if ((op = strstr(mvalue1,
"XOR"))) {
257 }
else if ((op = strstr(mvalue1,
"OR"))) {
261 }
else if ((op = strchr(mvalue1,
'>'))) {
264 if (*(op + 1) ==
'=') {
267 }
else if (*(op + 1) ==
'>') {
271 }
else if ((op = strchr(mvalue1,
'<'))) {
274 if (*(op + 1) ==
'=') {
277 }
else if (*(op + 1) ==
'<') {
281 }
else if ((op = strchr(mvalue1,
'='))) {
283 if (*(op + 1) ==
'=') {
288 }
else if ((op = strchr(mvalue1,
'+'))) {
291 }
else if ((op = strchr(mvalue1,
'-'))) {
300 mtype_of_result =
args.argv1;
301 if (mtype_of_result) {
302 if (!strcasecmp(mtype_of_result,
"float")
303 || !strcasecmp(mtype_of_result,
"f"))
305 else if (!strcasecmp(mtype_of_result,
"int")
306 || !strcasecmp(mtype_of_result,
"i"))
308 else if (!strcasecmp(mtype_of_result,
"hex")
309 || !strcasecmp(mtype_of_result,
"h"))
311 else if (!strcasecmp(mtype_of_result,
"char")
312 || !strcasecmp(mtype_of_result,
"c"))
323 "Supply all the parameters - just this once, please\n");
327 if (sscanf(mvalue1,
"%30lf", &fnum1) != 1) {
332 if (sscanf(mvalue2,
"%30lf", &fnum2) != 1) {
342 ftmp = fnum1 + fnum2;
348 ftmp = (fnum1 / fnum2);
351 ftmp = (fnum1 * fnum2);
354 ftmp = (fnum1 - fnum2);
364 ftmp = (inum1 % inum2);
370 ftmp = pow(fnum1, fnum2);
377 ftmp = (inum1 << inum2);
385 ftmp = (inum1 >> inum2);
392 ftmp = (inum1 & inum2);
399 ftmp = (inum1 ^ inum2);
406 ftmp = (inum1 | inum2);
426 "Something happened that neither of us should be proud of %d\n",
431 if (iaction < GTFUNCTION || iaction >
EQFUNCTION) {
433 snprintf(
buf,
len,
"%f", ftmp);
435 snprintf(
buf,
len,
"%i", (
int) ftmp);
437 snprintf(
buf,
len,
"%x", (
unsigned int) ftmp);
439 snprintf(
buf,
len,
"%c", (
unsigned char) ftmp);
446 char *data,
char *
buf,
size_t len)
452 char endchar = 0, returnvar[12];
473 ast_log(
LOG_NOTICE,
"Variable %s doesn't exist - are you sure you wrote it correctly?\n", data);
478 if (sscanf(
var,
"%30d%1c", &int_value, &endchar) == 0 || endchar != 0) {
479 ast_log(
LOG_NOTICE,
"The content of ${%s} is not a numeric value - bailing out!\n", data);
485 if (!strcasecmp(cmd,
"INC")) {
488 }
else if (!strcasecmp(cmd,
"DEC")) {
493 if (snprintf(returnvar,
sizeof(returnvar),
"%d", int_value) > 0) {
504 ast_log(
LOG_NOTICE,
"Variable %s refused to be %sREMENTED, setting value to 0", data, cmd);
514 char *parse,
char *buffer,
size_t buflen)
516 double num1, num2, response_num = 0;
531 }
else if (sscanf(
args.num1,
"%30lf", &num1) != 1) {
538 }
else if (sscanf(
args.num2,
"%30lf", &num2) != 1) {
543 if (response_num == -1) {
546 response_num = (num1 > num2) ? num2 : num1;
549 ast_debug(1,
"%f is the minimum of [%f,%f]\n", response_num, num1, num2);
550 if ((
int) response_num == response_num) {
551 snprintf(buffer, buflen,
"%d", (
int) response_num);
553 snprintf(buffer, buflen,
"%f", response_num);
560 char *parse,
char *buffer,
size_t buflen)
562 double num1, num2, response_num = 0;
577 }
else if (sscanf(
args.num1,
"%30lf", &num1) != 1) {
584 }
else if (sscanf(
args.num2,
"%30lf", &num2) != 1) {
589 if (response_num == -1) {
592 response_num = (num1 < num2) ? num2 : num1;
595 ast_debug(1,
"%f is the maximum of [%f,%f]\n", response_num, num1, num2);
596 if ((
int) response_num == response_num) {
597 snprintf(buffer, buflen,
"%d", (
int) response_num);
599 snprintf(buffer, buflen,
"%f", response_num);
606 char *parse,
char *buffer,
size_t buflen)
608 double num1, response_num;
620 response_num = fabs(num1);
621 ast_debug(1,
"%f is the absolute value of %f\n", response_num, num1);
622 if ((
int) response_num == response_num) {
623 snprintf(buffer, buflen,
"%d", (
int) response_num);
625 snprintf(buffer, buflen,
"%f", response_num);
672 info->name =
"test_MATH_function";
673 info->category =
"/main/pbx/";
674 info->summary =
"Test MATH function substitution";
676 "Executes a series of variable substitutions using the MATH function and ensures that the expected results are received.";
Asterisk main include file. File version handling, generic pbx functions.
General Asterisk PBX channel definitions.
#define ast_channel_lock(chan)
#define ast_channel_unlock(chan)
Conversion utility functions.
static int acf_abs_exec(struct ast_channel *chan, const char *cmd, char *parse, char *buffer, size_t buflen)
static struct ast_custom_function increment_function
static int math(struct ast_channel *chan, const char *cmd, char *parse, char *buf, size_t len)
static struct ast_custom_function math_function
static int acf_max_exec(struct ast_channel *chan, const char *cmd, char *parse, char *buffer, size_t buflen)
static struct ast_custom_function decrement_function
static struct ast_custom_function acf_min
static int acf_min_exec(struct ast_channel *chan, const char *cmd, char *parse, char *buffer, size_t buflen)
static struct ast_custom_function acf_abs
static int load_module(void)
static struct ast_custom_function acf_max
static int unload_module(void)
static int crement_function_read(struct ast_channel *chan, const char *cmd, char *data, char *buf, size_t len)
static int len(struct ast_channel *chan, const char *cmd, char *data, char *buf, size_t buflen)
Application convenience functions, designed to give consistent look and feel to Asterisk apps.
#define AST_APP_ARG(name)
Define an application argument.
#define AST_DECLARE_APP_ARGS(name, arglist)
Declare a structure to hold an application's arguments.
#define AST_STANDARD_APP_ARGS(args, parse)
Performs the 'standard' argument separation process for an application.
Configuration File Parser.
#define ast_debug(level,...)
Log a DEBUG message.
Asterisk module definitions.
#define AST_MODULE_INFO_STANDARD(keystr, desc)
#define ASTERISK_GPL_KEY
The text the key() function should return.
Core PBX routines and definitions.
const char * pbx_builtin_getvar_helper(struct ast_channel *chan, const char *name)
Return a pointer to the value of the corresponding channel variable.
void ast_str_substitute_variables(struct ast_str **buf, ssize_t maxlen, struct ast_channel *chan, const char *templ)
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.
#define ast_custom_function_register(acf)
Register a custom function.
int ast_custom_function_unregister(struct ast_custom_function *acf)
Unregister a custom function.
char * ast_str_buffer(const struct ast_str *buf)
Returns the string buffer within the ast_str buf.
static force_inline int attribute_pure ast_strlen_zero(const char *s)
#define ast_str_create(init_len)
Create a malloc'ed dynamic length string.
int ast_str_set(struct ast_str **buf, ssize_t max_len, const char *fmt,...)
Set a dynamic string using variable arguments.
void ast_copy_string(char *dst, const char *src, size_t size)
Size-limited null-terminating string copy.
Main Channel structure associated with a channel.
Data structure associated with a custom dialplan function.
Support for dynamic strings.
#define AST_TEST_REGISTER(cb)
#define ast_test_status_update(a, b, c...)
#define AST_TEST_UNREGISTER(cb)
#define AST_TEST_DEFINE(hdr)