139 char *buf,
size_t len)
147 char *mvalue1, *mvalue2 = NULL, *mtype_of_result;
155 ast_log(
LOG_WARNING,
"Syntax: MATH(<number1><op><number 2>[,<type_of_result>]) - missing argument!\n");
162 ast_log(
LOG_WARNING,
"Syntax: MATH(<number1><op><number 2>[,<type_of_result>]) - missing argument!\n");
166 mvalue1 =
args.argv0;
168 if (mvalue1[0] ==
'-') {
173 if ((op = strchr(mvalue1,
'*'))) {
176 }
else if ((op = strchr(mvalue1,
'/'))) {
179 }
else if ((op = strchr(mvalue1,
'%'))) {
182 }
else if ((op = strchr(mvalue1,
'^'))) {
185 }
else if ((op = strstr(mvalue1,
"AND"))) {
189 }
else if ((op = strstr(mvalue1,
"XOR"))) {
193 }
else if ((op = strstr(mvalue1,
"OR"))) {
197 }
else if ((op = strchr(mvalue1,
'>'))) {
200 if (*(op + 1) ==
'=') {
203 }
else if (*(op + 1) ==
'>') {
207 }
else if ((op = strchr(mvalue1,
'<'))) {
210 if (*(op + 1) ==
'=') {
213 }
else if (*(op + 1) ==
'<') {
217 }
else if ((op = strchr(mvalue1,
'='))) {
219 if (*(op + 1) ==
'=') {
224 }
else if ((op = strchr(mvalue1,
'+'))) {
227 }
else if ((op = strchr(mvalue1,
'-'))) {
236 mtype_of_result =
args.argv1;
237 if (mtype_of_result) {
238 if (!strcasecmp(mtype_of_result,
"float")
239 || !strcasecmp(mtype_of_result,
"f"))
241 else if (!strcasecmp(mtype_of_result,
"int")
242 || !strcasecmp(mtype_of_result,
"i"))
244 else if (!strcasecmp(mtype_of_result,
"hex")
245 || !strcasecmp(mtype_of_result,
"h"))
247 else if (!strcasecmp(mtype_of_result,
"char")
248 || !strcasecmp(mtype_of_result,
"c"))
259 "Supply all the parameters - just this once, please\n");
263 if (sscanf(mvalue1,
"%30lf", &fnum1) != 1) {
268 if (sscanf(mvalue2,
"%30lf", &fnum2) != 1) {
278 ftmp = fnum1 + fnum2;
284 ftmp = (fnum1 / fnum2);
287 ftmp = (fnum1 * fnum2);
290 ftmp = (fnum1 - fnum2);
300 ftmp = (inum1 % inum2);
306 ftmp = pow(fnum1, fnum2);
313 ftmp = (inum1 << inum2);
321 ftmp = (inum1 >> inum2);
328 ftmp = (inum1 & inum2);
335 ftmp = (inum1 ^ inum2);
342 ftmp = (inum1 | inum2);
362 "Something happened that neither of us should be proud of %d\n",
367 if (iaction < GTFUNCTION || iaction >
EQFUNCTION) {
369 snprintf(buf, len,
"%f", ftmp);
371 snprintf(buf, len,
"%i", (
int) ftmp);
373 snprintf(buf, len,
"%x", (
unsigned int) ftmp);
375 snprintf(buf, len,
"%c", (
unsigned char) ftmp);
382 char *data,
char *buf,
size_t len)
388 char endchar = 0, returnvar[12];
409 ast_log(
LOG_NOTICE,
"Variable %s doesn't exist - are you sure you wrote it correctly?\n", data);
414 if (sscanf(var,
"%30d%1c", &int_value, &endchar) == 0 || endchar != 0) {
415 ast_log(
LOG_NOTICE,
"The content of ${%s} is not a numeric value - bailing out!\n", data);
421 if (!strcasecmp(cmd,
"INC")) {
424 }
else if (!strcasecmp(cmd,
"DEC")) {
429 if (snprintf(returnvar,
sizeof(returnvar),
"%d", int_value) > 0) {
440 ast_log(
LOG_NOTICE,
"Variable %s refused to be %sREMENTED, setting value to 0", data, cmd);
465 #ifdef TEST_FRAMEWORK
473 info->name =
"test_MATH_function";
474 info->category =
"/main/pbx/";
475 info->summary =
"Test MATH function substitution";
477 "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.
#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)
char * ast_str_buffer(const struct ast_str *buf)
Returns the string buffer within the ast_str buf.
#define AST_DECLARE_APP_ARGS(name, arglist)
Declare a structure to hold an application's arguments.
void ast_str_substitute_variables(struct ast_str **buf, ssize_t maxlen, struct ast_channel *chan, const char *templ)
#define AST_TEST_REGISTER(cb)
Configuration File Parser.
struct ast_str * ast_str_create(size_t init_len)
Create a malloc'ed dynamic length string.
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.
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.
static struct ast_custom_function increment_function
General Asterisk PBX channel definitions.
#define ast_test_status_update(a, b, c...)
static force_inline int attribute_pure ast_strlen_zero(const char *s)
Data structure associated with a custom dialplan function.
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)
void ast_log(int level, const char *file, int line, const char *function, const char *fmt,...)
Used for sending a log message This is the standard logger function. Probably the only way you will i...
#define ast_channel_unlock(chan)
static void parse(struct mgcp_request *req)
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.
Application convenience functions, designed to give consistent look and feel to Asterisk apps...
#define AST_TEST_DEFINE(hdr)
#define AST_APP_ARG(name)
Define an application argument.
static int load_module(void)
#define AST_STANDARD_APP_ARGS(args, parse)
Performs the 'standard' argument separation process for an application.
#define ASTERISK_GPL_KEY
The text the key() function should return.
Asterisk module definitions.
#define ast_custom_function_register(acf)
Register a custom function.
#define ASTERISK_FILE_VERSION(file, version)
Register/unregister a source code file with the core.