217 char *mvalue1, *mvalue2 =
NULL, *mtype_of_result;
225 ast_log(
LOG_WARNING,
"Syntax: MATH(<number1><op><number 2>[,<type_of_result>]) - missing argument!\n");
232 ast_log(
LOG_WARNING,
"Syntax: MATH(<number1><op><number 2>[,<type_of_result>]) - missing argument!\n");
236 mvalue1 =
args.argv0;
238 if (mvalue1[0] ==
'-') {
243 if ((op = strchr(mvalue1,
'*'))) {
246 }
else if ((op = strchr(mvalue1,
'/'))) {
249 }
else if ((op = strchr(mvalue1,
'%'))) {
252 }
else if ((op = strchr(mvalue1,
'^'))) {
255 }
else if ((op = strstr(mvalue1,
"AND"))) {
259 }
else if ((op = strstr(mvalue1,
"XOR"))) {
263 }
else if ((op = strstr(mvalue1,
"OR"))) {
267 }
else if ((op = strchr(mvalue1,
'>'))) {
270 if (*(op + 1) ==
'=') {
273 }
else if (*(op + 1) ==
'>') {
277 }
else if ((op = strchr(mvalue1,
'<'))) {
280 if (*(op + 1) ==
'=') {
283 }
else if (*(op + 1) ==
'<') {
287 }
else if ((op = strchr(mvalue1,
'='))) {
289 if (*(op + 1) ==
'=') {
294 }
else if ((op = strchr(mvalue1,
'+'))) {
297 }
else if ((op = strchr(mvalue1,
'-'))) {
306 mtype_of_result =
args.argv1;
307 if (mtype_of_result) {
308 if (!strcasecmp(mtype_of_result,
"float")
309 || !strcasecmp(mtype_of_result,
"f"))
311 else if (!strcasecmp(mtype_of_result,
"int")
312 || !strcasecmp(mtype_of_result,
"i"))
314 else if (!strcasecmp(mtype_of_result,
"hex")
315 || !strcasecmp(mtype_of_result,
"h"))
317 else if (!strcasecmp(mtype_of_result,
"char")
318 || !strcasecmp(mtype_of_result,
"c"))
329 "Supply all the parameters - just this once, please\n");
333 if (sscanf(mvalue1,
"%30lf", &fnum1) != 1) {
338 if (sscanf(mvalue2,
"%30lf", &fnum2) != 1) {
348 ftmp = fnum1 + fnum2;
354 ftmp = (fnum1 / fnum2);
357 ftmp = (fnum1 * fnum2);
360 ftmp = (fnum1 - fnum2);
370 ftmp = (inum1 % inum2);
376 ftmp = pow(fnum1, fnum2);
383 ftmp = (inum1 << inum2);
391 ftmp = (inum1 >> inum2);
398 ftmp = (inum1 & inum2);
405 ftmp = (inum1 ^ inum2);
412 ftmp = (inum1 | inum2);
432 "Something happened that neither of us should be proud of %d\n",
437 if (iaction < GTFUNCTION || iaction >
EQFUNCTION) {
439 snprintf(
buf,
len,
"%f", ftmp);
441 snprintf(
buf,
len,
"%i", (
int) ftmp);
443 snprintf(
buf,
len,
"%x", (
unsigned int) ftmp);
445 snprintf(
buf,
len,
"%c", (
unsigned char) ftmp);
452 char *data,
char *
buf,
size_t len)
458 char endchar = 0, returnvar[12];
479 ast_log(
LOG_NOTICE,
"Variable %s doesn't exist - are you sure you wrote it correctly?\n", data);
484 if (sscanf(
var,
"%30d%1c", &int_value, &endchar) == 0 || endchar != 0) {
485 ast_log(
LOG_NOTICE,
"The content of ${%s} is not a numeric value - bailing out!\n", data);
491 if (!strcasecmp(cmd,
"INC")) {
494 }
else if (!strcasecmp(cmd,
"DEC")) {
499 if (snprintf(returnvar,
sizeof(returnvar),
"%d", int_value) > 0) {
510 ast_log(
LOG_NOTICE,
"Variable %s refused to be %sREMENTED, setting value to 0", data, cmd);
520 char *parse,
char *buffer,
size_t buflen)
522 double num1, num2, response_num = 0;
537 }
else if (sscanf(
args.num1,
"%30lf", &num1) != 1) {
544 }
else if (sscanf(
args.num2,
"%30lf", &num2) != 1) {
549 if (response_num == -1) {
552 response_num = (num1 > num2) ? num2 : num1;
555 ast_debug(1,
"%f is the minimum of [%f,%f]\n", response_num, num1, num2);
556 if ((
int) response_num == response_num) {
557 snprintf(buffer, buflen,
"%d", (
int) response_num);
559 snprintf(buffer, buflen,
"%f", response_num);
566 char *parse,
char *buffer,
size_t buflen)
568 double num1, num2, response_num = 0;
583 }
else if (sscanf(
args.num1,
"%30lf", &num1) != 1) {
590 }
else if (sscanf(
args.num2,
"%30lf", &num2) != 1) {
595 if (response_num == -1) {
598 response_num = (num1 < num2) ? num2 : num1;
601 ast_debug(1,
"%f is the maximum of [%f,%f]\n", response_num, num1, num2);
602 if ((
int) response_num == response_num) {
603 snprintf(buffer, buflen,
"%d", (
int) response_num);
605 snprintf(buffer, buflen,
"%f", response_num);
612 char *parse,
char *buffer,
size_t buflen)
614 double num1, response_num;
626 response_num = fabs(num1);
627 ast_debug(1,
"%f is the absolute value of %f\n", response_num, num1);
628 if ((
int) response_num == response_num) {
629 snprintf(buffer, buflen,
"%d", (
int) response_num);
631 snprintf(buffer, buflen,
"%f", response_num);
678 info->name =
"test_MATH_function";
679 info->category =
"/main/pbx/";
680 info->summary =
"Test MATH function substitution";
682 "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)