| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
#include "asterisk.h" |
| 38 |
|
| 39 |
#include <math.h> |
| 40 |
|
| 41 |
#include "asterisk/module.h" |
| 42 |
#include "asterisk/channel.h" |
| 43 |
#include "asterisk/pbx.h" |
| 44 |
#include "asterisk/utils.h" |
| 45 |
#include "asterisk/conversions.h" |
| 46 |
#include "asterisk/app.h" |
| 47 |
#include "asterisk/config.h" |
| 48 |
#include "asterisk/test.h" |
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
|
| 88 |
|
| 89 |
|
| 90 |
|
| 91 |
|
| 92 |
|
| 93 |
|
| 94 |
|
| 95 |
|
| 96 |
|
| 97 |
|
| 98 |
|
| 99 |
|
| 100 |
|
| 101 |
|
| 102 |
|
| 103 |
|
| 104 |
|
| 105 |
|
| 106 |
|
| 107 |
|
| 108 |
|
| 109 |
|
| 110 |
|
| 111 |
|
| 112 |
|
| 113 |
|
| 114 |
|
| 115 |
|
| 116 |
|
| 117 |
|
| 118 |
|
| 119 |
|
| 120 |
|
| 121 |
|
| 122 |
|
| 123 |
|
| 124 |
|
| 125 |
|
| 126 |
|
| 127 |
|
| 128 |
|
| 129 |
|
| 130 |
|
| 131 |
|
| 132 |
|
| 133 |
|
| 134 |
|
| 135 |
|
| 136 |
|
| 137 |
|
| 138 |
|
| 139 |
|
| 140 |
|
| 141 |
|
| 142 |
|
| 143 |
|
| 144 |
|
| 145 |
|
| 146 |
|
| 147 |
|
| 148 |
|
| 149 |
|
| 150 |
|
| 151 |
|
| 152 |
|
| 153 |
|
| 154 |
|
| 155 |
|
| 156 |
|
| 157 |
|
| 158 |
|
| 159 |
|
| 160 |
|
| 161 |
|
| 162 |
|
| 163 |
|
| 164 |
|
| 165 |
|
| 166 |
|
| 167 |
|
| 168 |
|
| 169 |
|
| 170 |
|
| 171 |
|
| 172 |
|
| 173 |
|
| 174 |
|
| 175 |
|
| 176 |
|
| 177 |
|
| 178 |
|
| 179 |
|
| 180 |
|
| 181 |
|
| 182 |
|
| 183 |
|
| 184 |
|
| 185 |
|
| 186 |
|
| 187 |
|
| 188 |
|
| 189 |
|
| 190 |
|
| 191 |
|
| 192 |
|
| 193 |
|
| 194 |
|
| 195 |
|
| 196 |
|
| 197 |
|
| 198 |
|
| 199 |
|
| 200 |
|
| 201 |
|
| 202 |
|
| 203 |
|
| 204 |
enum TypeOfFunctions { |
| 205 |
ADDFUNCTION, |
| 206 |
DIVIDEFUNCTION, |
| 207 |
MULTIPLYFUNCTION, |
| 208 |
SUBTRACTFUNCTION, |
| 209 |
MODULUSFUNCTION, |
| 210 |
POWFUNCTION, |
| 211 |
SHLEFTFUNCTION, |
| 212 |
SHRIGHTFUNCTION, |
| 213 |
BITWISEANDFUNCTION, |
| 214 |
BITWISEXORFUNCTION, |
| 215 |
BITWISEORFUNCTION, |
| 216 |
GTFUNCTION, |
| 217 |
LTFUNCTION, |
| 218 |
GTEFUNCTION, |
| 219 |
LTEFUNCTION, |
| 220 |
EQFUNCTION |
| 221 |
}; |
| 222 |
|
| 223 |
enum TypeOfResult { |
| 224 |
FLOAT_RESULT, |
| 225 |
INT_RESULT, |
| 226 |
HEX_RESULT, |
| 227 |
CHAR_RESULT |
| 228 |
}; |
| 229 |
|
| 230 |
static int math(struct ast_channel *chan, const char *cmd, char *parse, |
| 231 |
char *buf, size_t len) |
| 232 |
{ |
| 233 |
double fnum1; |
| 234 |
double fnum2; |
| 235 |
double ftmp = 0; |
| 236 |
char *op; |
| 237 |
int iaction = -1; |
| 238 |
int type_of_result = FLOAT_RESULT; |
| 239 |
char *mvalue1, *mvalue2 = NULL, *mtype_of_result; |
| 240 |
int negvalue1 = 0; |
| 241 |
AST_DECLARE_APP_ARGS(args, |
| 242 |
AST_APP_ARG(argv0); |
| 243 |
AST_APP_ARG(argv1); |
| 244 |
); |
| 245 |
|
| 246 |
if (ast_strlen_zero(parse)) { |
| 247 |
ast_log(LOG_WARNING, "Syntax: MATH(<number1><op><number 2>[,<type_of_result>]) - missing argument!\n"); |
| 248 |
return -1; |
| 249 |
} |
| 250 |
|
| 251 |
AST_STANDARD_APP_ARGS(args, parse); |
| 252 |
|
| 253 |
if (args.argc < 1) { |
| 254 |
ast_log(LOG_WARNING, "Syntax: MATH(<number1><op><number 2>[,<type_of_result>]) - missing argument!\n"); |
| 255 |
return -1; |
| 256 |
} |
| 257 |
|
| 258 |
mvalue1 = args.argv0; |
| 259 |
|
| 260 |
if (mvalue1[0] == '-') { |
| 261 |
negvalue1 = 1; |
| 262 |
mvalue1++; |
| 263 |
} |
| 264 |
|
| 265 |
if ((op = strchr(mvalue1, '*'))) { |
| 266 |
iaction = MULTIPLYFUNCTION; |
| 267 |
*op = '\0'; |
| 268 |
} else if ((op = strchr(mvalue1, '/'))) { |
| 269 |
iaction = DIVIDEFUNCTION; |
| 270 |
*op = '\0'; |
| 271 |
} else if ((op = strchr(mvalue1, '%'))) { |
| 272 |
iaction = MODULUSFUNCTION; |
| 273 |
*op = '\0'; |
| 274 |
} else if ((op = strchr(mvalue1, '^'))) { |
| 275 |
iaction = POWFUNCTION; |
| 276 |
*op = '\0'; |
| 277 |
} else if ((op = strstr(mvalue1, "AND"))) { |
| 278 |
iaction = BITWISEANDFUNCTION; |
| 279 |
*op = '\0'; |
| 280 |
op += 2; |
| 281 |
} else if ((op = strstr(mvalue1, "XOR"))) { |
| 282 |
iaction = BITWISEXORFUNCTION; |
| 283 |
*op = '\0'; |
| 284 |
op += 2; |
| 285 |
} else if ((op = strstr(mvalue1, "OR"))) { |
| 286 |
iaction = BITWISEORFUNCTION; |
| 287 |
*op = '\0'; |
| 288 |
++op; |
| 289 |
} else if ((op = strchr(mvalue1, '>'))) { |
| 290 |
iaction = GTFUNCTION; |
| 291 |
*op = '\0'; |
| 292 |
if (*(op + 1) == '=') { |
| 293 |
iaction = GTEFUNCTION; |
| 294 |
++op; |
| 295 |
} else if (*(op + 1) == '>') { |
| 296 |
iaction = SHRIGHTFUNCTION; |
| 297 |
++op; |
| 298 |
} |
| 299 |
} else if ((op = strchr(mvalue1, '<'))) { |
| 300 |
iaction = LTFUNCTION; |
| 301 |
*op = '\0'; |
| 302 |
if (*(op + 1) == '=') { |
| 303 |
iaction = LTEFUNCTION; |
| 304 |
++op; |
| 305 |
} else if (*(op + 1) == '<') { |
| 306 |
iaction = SHLEFTFUNCTION; |
| 307 |
++op; |
| 308 |
} |
| 309 |
} else if ((op = strchr(mvalue1, '='))) { |
| 310 |
*op = '\0'; |
| 311 |
if (*(op + 1) == '=') { |
| 312 |
iaction = EQFUNCTION; |
| 313 |
++op; |
| 314 |
} else |
| 315 |
op = NULL; |
| 316 |
} else if ((op = strchr(mvalue1, '+'))) { |
| 317 |
iaction = ADDFUNCTION; |
| 318 |
*op = '\0'; |
| 319 |
} else if ((op = strchr(mvalue1, '-'))) { |
| 320 |
iaction = SUBTRACTFUNCTION; |
| 321 |
*op = '\0'; |
| 322 |
} |
| 323 |
|
| 324 |
if (op) |
| 325 |
mvalue2 = op + 1; |
| 326 |
|
| 327 |
|
| 328 |
mtype_of_result = args.argv1; |
| 329 |
if (mtype_of_result) { |
| 330 |
if (!strcasecmp(mtype_of_result, "float") |
| 331 |
|| !strcasecmp(mtype_of_result, "f")) |
| 332 |
type_of_result = FLOAT_RESULT; |
| 333 |
else if (!strcasecmp(mtype_of_result, "int") |
| 334 |
|| !strcasecmp(mtype_of_result, "i")) |
| 335 |
type_of_result = INT_RESULT; |
| 336 |
else if (!strcasecmp(mtype_of_result, "hex") |
| 337 |
|| !strcasecmp(mtype_of_result, "h")) |
| 338 |
type_of_result = HEX_RESULT; |
| 339 |
else if (!strcasecmp(mtype_of_result, "char") |
| 340 |
|| !strcasecmp(mtype_of_result, "c")) |
| 341 |
type_of_result = CHAR_RESULT; |
| 342 |
else { |
| 343 |
ast_log(LOG_WARNING, "Unknown type of result requested '%s'.\n", |
| 344 |
mtype_of_result); |
| 345 |
return -1; |
| 346 |
} |
| 347 |
} |
| 348 |
|
| 349 |
if (!mvalue2) { |
| 350 |
ast_log(LOG_WARNING, |
| 351 |
"Supply all the parameters - just this once, please\n"); |
| 352 |
return -1; |
| 353 |
} |
| 354 |
|
| 355 |
if (sscanf(mvalue1, "%30lf", &fnum1) != 1) { |
| 356 |
ast_log(LOG_WARNING, "'%s' is not a valid number\n", mvalue1); |
| 357 |
return -1; |
| 358 |
} |
| 359 |
|
| 360 |
if (sscanf(mvalue2, "%30lf", &fnum2) != 1) { |
| 361 |
ast_log(LOG_WARNING, "'%s' is not a valid number\n", mvalue2); |
| 362 |
return -1; |
| 363 |
} |
| 364 |
|
| 365 |
if (negvalue1) |
| 366 |
fnum1 = 0 - fnum1; |
| 367 |
|
| 368 |
switch (iaction) { |
| 369 |
case ADDFUNCTION: |
| 370 |
ftmp = fnum1 + fnum2; |
| 371 |
break; |
| 372 |
case DIVIDEFUNCTION: |
| 373 |
if (fnum2 <= 0) |
| 374 |
ftmp = 0; |
| 375 |
else |
| 376 |
ftmp = (fnum1 / fnum2); |
| 377 |
break; |
| 378 |
case MULTIPLYFUNCTION: |
| 379 |
ftmp = (fnum1 * fnum2); |
| 380 |
break; |
| 381 |
case SUBTRACTFUNCTION: |
| 382 |
ftmp = (fnum1 - fnum2); |
| 383 |
break; |
| 384 |
case MODULUSFUNCTION: |
| 385 |
{ |
| 386 |
int inum1 = fnum1; |
| 387 |
int inum2 = fnum2; |
| 388 |
|
| 389 |
if (inum2 == 0) { |
| 390 |
ftmp = 0; |
| 391 |
} else { |
| 392 |
ftmp = (inum1 % inum2); |
| 393 |
} |
| 394 |
|
| 395 |
break; |
| 396 |
} |
| 397 |
case POWFUNCTION: |
| 398 |
ftmp = pow(fnum1, fnum2); |
| 399 |
break; |
| 400 |
case SHLEFTFUNCTION: |
| 401 |
{ |
| 402 |
int inum1 = fnum1; |
| 403 |
int inum2 = fnum2; |
| 404 |
|
| 405 |
ftmp = (inum1 << inum2); |
| 406 |
break; |
| 407 |
} |
| 408 |
case SHRIGHTFUNCTION: |
| 409 |
{ |
| 410 |
int inum1 = fnum1; |
| 411 |
int inum2 = fnum2; |
| 412 |
|
| 413 |
ftmp = (inum1 >> inum2); |
| 414 |
break; |
| 415 |
} |
| 416 |
case BITWISEANDFUNCTION: |
| 417 |
{ |
| 418 |
int inum1 = fnum1; |
| 419 |
int inum2 = fnum2; |
| 420 |
ftmp = (inum1 & inum2); |
| 421 |
break; |
| 422 |
} |
| 423 |
case BITWISEXORFUNCTION: |
| 424 |
{ |
| 425 |
int inum1 = fnum1; |
| 426 |
int inum2 = fnum2; |
| 427 |
ftmp = (inum1 ^ inum2); |
| 428 |
break; |
| 429 |
} |
| 430 |
case BITWISEORFUNCTION: |
| 431 |
{ |
| 432 |
int inum1 = fnum1; |
| 433 |
int inum2 = fnum2; |
| 434 |
ftmp = (inum1 | inum2); |
| 435 |
break; |
| 436 |
} |
| 437 |
case GTFUNCTION: |
| 438 |
ast_copy_string(buf, (fnum1 > fnum2) ? "TRUE" : "FALSE", len); |
| 439 |
break; |
| 440 |
case LTFUNCTION: |
| 441 |
ast_copy_string(buf, (fnum1 < fnum2) ? "TRUE" : "FALSE", len); |
| 442 |
break; |
| 443 |
case GTEFUNCTION: |
| 444 |
ast_copy_string(buf, (fnum1 >= fnum2) ? "TRUE" : "FALSE", len); |
| 445 |
break; |
| 446 |
case LTEFUNCTION: |
| 447 |
ast_copy_string(buf, (fnum1 <= fnum2) ? "TRUE" : "FALSE", len); |
| 448 |
break; |
| 449 |
case EQFUNCTION: |
| 450 |
ast_copy_string(buf, (fnum1 == fnum2) ? "TRUE" : "FALSE", len); |
| 451 |
break; |
| 452 |
default: |
| 453 |
ast_log(LOG_WARNING, |
| 454 |
"Something happened that neither of us should be proud of %d\n", |
| 455 |
iaction); |
| 456 |
return -1; |
| 457 |
} |
| 458 |
|
| 459 |
if (iaction < GTFUNCTION || iaction > EQFUNCTION) { |
| 460 |
if (type_of_result == FLOAT_RESULT) |
| 461 |
snprintf(buf, len, "%f", ftmp); |
| 462 |
else if (type_of_result == INT_RESULT) |
| 463 |
snprintf(buf, len, "%i", (int) ftmp); |
| 464 |
else if (type_of_result == HEX_RESULT) |
| 465 |
snprintf(buf, len, "%x", (unsigned int) ftmp); |
| 466 |
else if (type_of_result == CHAR_RESULT) |
| 467 |
snprintf(buf, len, "%c", (unsigned char) ftmp); |
| 468 |
} |
| 469 |
|
| 470 |
return 0; |
| 471 |
} |
| 472 |
|
| 473 |
static int crement_function_read(struct ast_channel *chan, const char *cmd, |
| 474 |
char *data, char *buf, size_t len) |
| 475 |
{ |
| 476 |
int ret = -1; |
| 477 |
int int_value = 0; |
| 478 |
int modify_orig = 0; |
| 479 |
const char *var; |
| 480 |
char endchar = 0, returnvar[12]; |
| 481 |
|
| 482 |
if (ast_strlen_zero(data)) { |
| 483 |
ast_log(LOG_WARNING, "Syntax: %s(<data>) - missing argument!\n", cmd); |
| 484 |
return -1; |
| 485 |
} |
| 486 |
|
| 487 |
if (!chan) { |
| 488 |
ast_log(LOG_WARNING, "No channel was provided to %s function.\n", cmd); |
| 489 |
return -1; |
| 490 |
} |
| 491 |
|
| 492 |
ast_channel_lock(chan); |
| 493 |
|
| 494 |
if (!(var = pbx_builtin_getvar_helper(chan, data))) { |
| 495 |
ast_log(LOG_NOTICE, "Failed to obtain variable %s, bailing out\n", data); |
| 496 |
ast_channel_unlock(chan); |
| 497 |
return -1; |
| 498 |
} |
| 499 |
|
| 500 |
if (ast_strlen_zero(var)) { |
| 501 |
ast_log(LOG_NOTICE, "Variable %s doesn't exist - are you sure you wrote it correctly?\n", data); |
| 502 |
ast_channel_unlock(chan); |
| 503 |
return -1; |
| 504 |
} |
| 505 |
|
| 506 |
if (sscanf(var, "%30d%1c", &int_value, &endchar) == 0 || endchar != 0) { |
| 507 |
ast_log(LOG_NOTICE, "The content of ${%s} is not a numeric value - bailing out!\n", data); |
| 508 |
ast_channel_unlock(chan); |
| 509 |
return -1; |
| 510 |
} |
| 511 |
|
| 512 |
|
| 513 |
if (!strcasecmp(cmd, "INC")) { |
| 514 |
int_value++; |
| 515 |
modify_orig = 1; |
| 516 |
} else if (!strcasecmp(cmd, "DEC")) { |
| 517 |
int_value--; |
| 518 |
modify_orig = 1; |
| 519 |
} |
| 520 |
|
| 521 |
if (snprintf(returnvar, sizeof(returnvar), "%d", int_value) > 0) { |
| 522 |
pbx_builtin_setvar_helper(chan, data, returnvar); |
| 523 |
if (modify_orig) { |
| 524 |
ast_copy_string(buf, returnvar, len); |
| 525 |
} |
| 526 |
ret = 0; |
| 527 |
} else { |
| 528 |
pbx_builtin_setvar_helper(chan, data, "0"); |
| 529 |
if (modify_orig) { |
| 530 |
ast_copy_string(buf, "0", len); |
| 531 |
} |
| 532 |
ast_log(LOG_NOTICE, "Variable %s refused to be %sREMENTED, setting value to 0", data, cmd); |
| 533 |
ret = 0; |
| 534 |
} |
| 535 |
|
| 536 |
ast_channel_unlock(chan); |
| 537 |
|
| 538 |
return ret; |
| 539 |
} |
| 540 |
|
| 541 |
static int acf_min_exec(struct ast_channel *chan, const char *cmd, |
| 542 |
char *parse, char *buffer, size_t buflen) |
| 543 |
{ |
| 544 |
double num1, num2, response_num = 0; |
| 545 |
AST_DECLARE_APP_ARGS(args, |
| 546 |
AST_APP_ARG(num1); |
| 547 |
AST_APP_ARG(num2); |
| 548 |
); |
| 549 |
|
| 550 |
AST_STANDARD_APP_ARGS(args, parse); |
| 551 |
|
| 552 |
if (ast_strlen_zero(args.num1) && ast_strlen_zero(args.num2)) { |
| 553 |
ast_log(LOG_ERROR, "Missing argument for number(s)."); |
| 554 |
return -1; |
| 555 |
} |
| 556 |
|
| 557 |
if (ast_strlen_zero(args.num1)) { |
| 558 |
response_num = -1; |
| 559 |
} else if (sscanf(args.num1, "%30lf", &num1) != 1) { |
| 560 |
ast_log(LOG_WARNING, "'%s' is not a valid number\n", args.num1); |
| 561 |
return -1; |
| 562 |
} |
| 563 |
|
| 564 |
if (ast_strlen_zero(args.num2)) { |
| 565 |
num2 = num1; |
| 566 |
} else if (sscanf(args.num2, "%30lf", &num2) != 1) { |
| 567 |
ast_log(LOG_WARNING, "'%s' is not a valid number\n", args.num2); |
| 568 |
return -1; |
| 569 |
} |
| 570 |
|
| 571 |
if (response_num == -1) { |
| 572 |
response_num = num2; |
| 573 |
} else { |
| 574 |
response_num = (num1 > num2) ? num2 : num1; |
| 575 |
} |
| 576 |
|
| 577 |
ast_debug(1, "%f is the minimum of [%f,%f]\n", response_num, num1, num2); |
| 578 |
if ((int) response_num == response_num) { |
| 579 |
snprintf(buffer, buflen, "%d", (int) response_num); |
| 580 |
} else { |
| 581 |
snprintf(buffer, buflen, "%f", response_num); |
| 582 |
} |
| 583 |
|
| 584 |
return 0; |
| 585 |
} |
| 586 |
|
| 587 |
static int acf_max_exec(struct ast_channel *chan, const char *cmd, |
| 588 |
char *parse, char *buffer, size_t buflen) |
| 589 |
{ |
| 590 |
double num1, num2, response_num = 0; |
| 591 |
AST_DECLARE_APP_ARGS(args, |
| 592 |
AST_APP_ARG(num1); |
| 593 |
AST_APP_ARG(num2); |
| 594 |
); |
| 595 |
|
| 596 |
AST_STANDARD_APP_ARGS(args, parse); |
| 597 |
|
| 598 |
if (ast_strlen_zero(args.num1) && ast_strlen_zero(args.num2)) { |
| 599 |
ast_log(LOG_ERROR, "Missing argument for number(s)."); |
| 600 |
return -1; |
| 601 |
} |
| 602 |
|
| 603 |
if (ast_strlen_zero(args.num1)) { |
| 604 |
response_num = -1; |
| 605 |
} else if (sscanf(args.num1, "%30lf", &num1) != 1) { |
| 606 |
ast_log(LOG_WARNING, "'%s' is not a valid number\n", args.num1); |
| 607 |
return -1; |
| 608 |
} |
| 609 |
|
| 610 |
if (ast_strlen_zero(args.num2)) { |
| 611 |
num2 = num1; |
| 612 |
} else if (sscanf(args.num2, "%30lf", &num2) != 1) { |
| 613 |
ast_log(LOG_WARNING, "'%s' is not a valid number\n", args.num2); |
| 614 |
return -1; |
| 615 |
} |
| 616 |
|
| 617 |
if (response_num == -1) { |
| 618 |
response_num = num2; |
| 619 |
} else { |
| 620 |
response_num = (num1 < num2) ? num2 : num1; |
| 621 |
} |
| 622 |
|
| 623 |
ast_debug(1, "%f is the maximum of [%f,%f]\n", response_num, num1, num2); |
| 624 |
if ((int) response_num == response_num) { |
| 625 |
snprintf(buffer, buflen, "%d", (int) response_num); |
| 626 |
} else { |
| 627 |
snprintf(buffer, buflen, "%f", response_num); |
| 628 |
} |
| 629 |
|
| 630 |
return 0; |
| 631 |
} |
| 632 |
|
| 633 |
static int acf_abs_exec(struct ast_channel *chan, const char *cmd, |
| 634 |
char *parse, char *buffer, size_t buflen) |
| 635 |
{ |
| 636 |
double num1, response_num; |
| 637 |
AST_DECLARE_APP_ARGS(args, |
| 638 |
AST_APP_ARG(num1); |
| 639 |
); |
| 640 |
|
| 641 |
AST_STANDARD_APP_ARGS(args, parse); |
| 642 |
|
| 643 |
if (ast_strlen_zero(args.num1) || sscanf(args.num1, "%30lf", &num1) != 1) { |
| 644 |
ast_log(LOG_WARNING, "Bad or missing argument for number: %s", args.num1); |
| 645 |
return -1; |
| 646 |
} |
| 647 |
|
| 648 |
response_num = fabs(num1); |
| 649 |
ast_debug(1, "%f is the absolute value of %f\n", response_num, num1); |
| 650 |
if ((int) response_num == response_num) { |
| 651 |
snprintf(buffer, buflen, "%d", (int) response_num); |
| 652 |
} else { |
| 653 |
snprintf(buffer, buflen, "%f", response_num); |
| 654 |
} |
| 655 |
|
| 656 |
return 0; |
| 657 |
} |
| 658 |
|
| 659 |
static int acf_digit_sum_exec(struct ast_channel *chan, const char *cmd, |
| 660 |
char *parse, char *buffer, size_t buflen) |
| 661 |
{ |
| 662 |
int sum = 0; |
| 663 |
|
| 664 |
if (ast_strlen_zero(parse)) { |
| 665 |
ast_log(LOG_WARNING, "Missing argument for number\n"); |
| 666 |
return -1; |
| 667 |
} |
| 668 |
|
| 669 |
for (; *parse; parse++) { |
| 670 |
if (*parse < '0' || *parse > '9') { |
| 671 |
continue; |
| 672 |
} |
| 673 |
sum += (*parse - '0'); |
| 674 |
} |
| 675 |
|
| 676 |
snprintf(buffer, buflen, "%d", sum); |
| 677 |
return 0; |
| 678 |
} |
| 679 |
|
| 680 |
static struct ast_custom_function math_function = { |
| 681 |
.name = "MATH", |
| 682 |
.read = math |
| 683 |
}; |
| 684 |
|
| 685 |
static struct ast_custom_function increment_function = { |
| 686 |
.name = "INC", |
| 687 |
.read = crement_function_read, |
| 688 |
}; |
| 689 |
|
| 690 |
static struct ast_custom_function decrement_function = { |
| 691 |
.name = "DEC", |
| 692 |
.read = crement_function_read, |
| 693 |
}; |
| 694 |
|
| 695 |
static struct ast_custom_function acf_min = { |
| 696 |
.name = "MIN", |
| 697 |
.read = acf_min_exec, |
| 698 |
.read_max = 12, |
| 699 |
}; |
| 700 |
|
| 701 |
static struct ast_custom_function acf_max = { |
| 702 |
.name = "MAX", |
| 703 |
.read = acf_max_exec, |
| 704 |
.read_max = 12, |
| 705 |
}; |
| 706 |
|
| 707 |
static struct ast_custom_function acf_abs = { |
| 708 |
.name = "ABS", |
| 709 |
.read = acf_abs_exec, |
| 710 |
.read_max = 12, |
| 711 |
}; |
| 712 |
|
| 713 |
static struct ast_custom_function acf_digit_sum = { |
| 714 |
.name = "DIGIT_SUM", |
| 715 |
.read = acf_digit_sum_exec, |
| 716 |
}; |
| 717 |
|
| 718 |
#ifdef TEST_FRAMEWORK |
| 719 |
AST_TEST_DEFINE(test_MATH_function) |
| 720 |
{ |
| 721 |
enum ast_test_result_state res = AST_TEST_PASS; |
| 722 |
struct ast_str *expr, *result; |
| 723 |
|
| 724 |
switch (cmd) { |
| 725 |
case TEST_INIT: |
| 726 |
info->name = "test_MATH_function"; |
| 727 |
info->category = "/main/pbx/"; |
| 728 |
info->summary = "Test MATH function substitution"; |
| 729 |
info->description = |
| 730 |
"Executes a series of variable substitutions using the MATH function and ensures that the expected results are received."; |
| 731 |
return AST_TEST_NOT_RUN; |
| 732 |
case TEST_EXECUTE: |
| 733 |
break; |
| 734 |
} |
| 735 |
|
| 736 |
ast_test_status_update(test, "Testing MATH() substitution ...\n"); |
| 737 |
|
| 738 |
if (!(expr = ast_str_create(16))) { |
| 739 |
return AST_TEST_FAIL; |
| 740 |
} |
| 741 |
if (!(result = ast_str_create(16))) { |
| 742 |
ast_free(expr); |
| 743 |
return AST_TEST_FAIL; |
| 744 |
} |
| 745 |
|
| 746 |
ast_str_set(&expr, 0, "${MATH(170 AND 63,i)}"); |
| 747 |
ast_str_substitute_variables(&result, 0, NULL, ast_str_buffer(expr)); |
| 748 |
if (strcmp(ast_str_buffer(result), "42") != 0) { |
| 749 |
ast_test_status_update(test, "Expected result '42' not returned! ('%s')\n", |
| 750 |
ast_str_buffer(result)); |
| 751 |
res = AST_TEST_FAIL; |
| 752 |
} |
| 753 |
|
| 754 |
ast_str_set(&expr, 0, "${MATH(170AND63,i)}"); |
| 755 |
ast_str_substitute_variables(&result, 0, NULL, ast_str_buffer(expr)); |
| 756 |
if (strcmp(ast_str_buffer(result), "42") != 0) { |
| 757 |
ast_test_status_update(test, "Expected result '42' not returned! ('%s')\n", |
| 758 |
ast_str_buffer(result)); |
| 759 |
res = AST_TEST_FAIL; |
| 760 |
} |
| 761 |
|
| 762 |
ast_free(expr); |
| 763 |
ast_free(result); |
| 764 |
|
| 765 |
return res; |
| 766 |
} |
| 767 |
#endif |
| 768 |
|
| 769 |
static int unload_module(void) |
| 770 |
{ |
| 771 |
int res = 0; |
| 772 |
|
| 773 |
res |= ast_custom_function_unregister(&math_function); |
| 774 |
res |= ast_custom_function_unregister(&increment_function); |
| 775 |
res |= ast_custom_function_unregister(&decrement_function); |
| 776 |
res |= ast_custom_function_unregister(&acf_min); |
| 777 |
res |= ast_custom_function_unregister(&acf_max); |
| 778 |
res |= ast_custom_function_unregister(&acf_abs); |
| 779 |
res |= ast_custom_function_unregister(&acf_digit_sum); |
| 780 |
AST_TEST_UNREGISTER(test_MATH_function); |
| 781 |
|
| 782 |
return res; |
| 783 |
} |
| 784 |
|
| 785 |
static int load_module(void) |
| 786 |
{ |
| 787 |
int res = 0; |
| 788 |
|
| 789 |
res |= ast_custom_function_register(&math_function); |
| 790 |
res |= ast_custom_function_register(&increment_function); |
| 791 |
res |= ast_custom_function_register(&decrement_function); |
| 792 |
res |= ast_custom_function_register(&acf_min); |
| 793 |
res |= ast_custom_function_register(&acf_max); |
| 794 |
res |= ast_custom_function_register(&acf_abs); |
| 795 |
res |= ast_custom_function_register(&acf_digit_sum); |
| 796 |
AST_TEST_REGISTER(test_MATH_function); |
| 797 |
|
| 798 |
return res; |
| 799 |
} |
| 800 |
|
| 801 |
AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "Mathematical dialplan function"); |