Add conditional support for noreturn functions.
[asterisk/asterisk.git] / funcs / func_math.c
index ab174d9..ae13058 100644 (file)
  * \ingroup functions
  */
 
+/*** MODULEINFO
+       <support_level>core</support_level>
+ ***/
+
 #include "asterisk.h"
 
-ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
+ASTERISK_REGISTER_FILE()
 
 #include <math.h>
 
@@ -40,6 +44,7 @@ ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
 #include "asterisk/utils.h"
 #include "asterisk/app.h"
 #include "asterisk/config.h"
+#include "asterisk/test.h"
 
 /*** DOCUMENTATION
        <function name="MATH" language="en_US">
@@ -52,7 +57,7 @@ ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
                                <replaceable>number1</replaceable><replaceable>op</replaceable><replaceable>number2</replaceable>
                                where the possible values for <replaceable>op</replaceable>
                                are:</para>
-                               <para>+,-,/,*,%,&lt;&lt;,&gt;&gt;,^,AND,OR,XOR,&lt;,%gt;,&gt;=,&lt;=,== (and behave as their C equivalents)</para>
+                               <para>+,-,/,*,%,&lt;&lt;,&gt;&gt;,^,AND,OR,XOR,&lt;,&gt;,&lt;=,&gt;=,== (and behave as their C equivalents)</para>
                        </parameter>
                        <parameter name="type">
                                <para>Wanted type of result:</para>
@@ -98,8 +103,8 @@ ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
                </syntax>
                <description>
                        <para>Decrements the value of a variable, while returning the updated value to the dialplan</para>
-                       <para>Example: DEC(MyVAR) - Increments MyVar</para>
-                       <para>Note: DEC(${MyVAR}) - Is wrong, as INC expects the variable name, not its value</para>
+                       <para>Example: DEC(MyVAR) - Decrements MyVar</para>
+                       <para>Note: DEC(${MyVAR}) - Is wrong, as DEC expects the variable name, not its value</para>
                </description>
        </function>
  ***/
@@ -179,47 +184,47 @@ static int math(struct ast_channel *chan, const char *cmd, char *parse,
                *op = '\0';
        } else if ((op = strstr(mvalue1, "AND"))) {
                iaction = BITWISEANDFUNCTION;
-               op += 3;
                *op = '\0';
+               op += 2;
        } else if ((op = strstr(mvalue1, "XOR"))) {
                iaction = BITWISEXORFUNCTION;
-               op += 3;
                *op = '\0';
+               op += 2;
        } else if ((op = strstr(mvalue1, "OR"))) {
                iaction = BITWISEORFUNCTION;
-               op += 2;
                *op = '\0';
+               ++op;
        } else if ((op = strchr(mvalue1, '>'))) {
                iaction = GTFUNCTION;
                *op = '\0';
                if (*(op + 1) == '=') {
-                       *++op = '\0';
                        iaction = GTEFUNCTION;
+                       ++op;
                } else if (*(op + 1) == '>') {
-                       *++op = '\0';
                        iaction = SHRIGHTFUNCTION;
+                       ++op;
                }
        } else if ((op = strchr(mvalue1, '<'))) {
                iaction = LTFUNCTION;
                *op = '\0';
                if (*(op + 1) == '=') {
-                       *++op = '\0';
                        iaction = LTEFUNCTION;
+                       ++op;
                } else if (*(op + 1) == '<') {
-                       *++op = '\0';
                        iaction = SHLEFTFUNCTION;
+                       ++op;
                }
        } else if ((op = strchr(mvalue1, '='))) {
                *op = '\0';
                if (*(op + 1) == '=') {
-                       *++op = '\0';
                        iaction = EQFUNCTION;
+                       ++op;
                } else
                        op = NULL;
        } else if ((op = strchr(mvalue1, '+'))) {
                iaction = ADDFUNCTION;
                *op = '\0';
-       } else if ((op = strchr(mvalue1, '-'))) { /* subtraction MUST always be last, in case we have a negative first number */
+       } else if ((op = strchr(mvalue1, '-'))) { /* subtraction MUST always be last, in case we have a negative second number */
                iaction = SUBTRACTFUNCTION;
                *op = '\0';
        }
@@ -249,7 +254,7 @@ static int math(struct ast_channel *chan, const char *cmd, char *parse,
                }
        }
 
-       if (!mvalue1 || !mvalue2) {
+       if (!mvalue2) {
                ast_log(LOG_WARNING,
                                "Supply all the parameters - just this once, please\n");
                return -1;
@@ -289,7 +294,11 @@ static int math(struct ast_channel *chan, const char *cmd, char *parse,
                        int inum1 = fnum1;
                        int inum2 = fnum2;
 
-                       ftmp = (inum1 % inum2);
+                       if (inum2 == 0) {
+                               ftmp = 0;
+                       } else {
+                               ftmp = (inum1 % inum2);
+                       }
 
                        break;
                }
@@ -383,6 +392,11 @@ static int crement_function_read(struct ast_channel *chan, const char *cmd,
                return -1;
        }
 
+       if (!chan) {
+               ast_log(LOG_WARNING, "No channel was provided to %s function.\n", cmd);
+               return -1;
+       }
+
        ast_channel_lock(chan);
 
        if (!(var = pbx_builtin_getvar_helper(chan, data))) {
@@ -412,8 +426,6 @@ static int crement_function_read(struct ast_channel *chan, const char *cmd,
                modify_orig = 1;
        }
 
-       ast_log(LOG_NOTICE, "The value is now: %d\n", int_value);
-
        if (snprintf(returnvar, sizeof(returnvar), "%d", int_value) > 0) {
                pbx_builtin_setvar_helper(chan, data, returnvar);
                if (modify_orig) {
@@ -450,6 +462,57 @@ static struct ast_custom_function decrement_function = {
        .read = crement_function_read,
 };
 
+#ifdef TEST_FRAMEWORK
+AST_TEST_DEFINE(test_MATH_function)
+{
+       enum ast_test_result_state res = AST_TEST_PASS;
+       struct ast_str *expr, *result;
+
+       switch (cmd) {
+       case TEST_INIT:
+               info->name = "test_MATH_function";
+               info->category = "/main/pbx/";
+               info->summary = "Test MATH function substitution";
+               info->description =
+                       "Executes a series of variable substitutions using the MATH function and ensures that the expected results are received.";
+               return AST_TEST_NOT_RUN;
+       case TEST_EXECUTE:
+               break;
+       }
+
+       ast_test_status_update(test, "Testing MATH() substitution ...\n");
+
+       if (!(expr = ast_str_create(16))) {
+               return AST_TEST_FAIL;
+       }
+       if (!(result = ast_str_create(16))) {
+               ast_free(expr);
+               return AST_TEST_FAIL;
+       }
+
+       ast_str_set(&expr, 0, "${MATH(170 AND 63,i)}");
+       ast_str_substitute_variables(&result, 0, NULL, ast_str_buffer(expr));
+       if (strcmp(ast_str_buffer(result), "42") != 0) {
+               ast_test_status_update(test, "Expected result '42' not returned! ('%s')\n",
+                               ast_str_buffer(result));
+               res = AST_TEST_FAIL;
+       }
+
+       ast_str_set(&expr, 0, "${MATH(170AND63,i)}");
+       ast_str_substitute_variables(&result, 0, NULL, ast_str_buffer(expr));
+       if (strcmp(ast_str_buffer(result), "42") != 0) {
+               ast_test_status_update(test, "Expected result '42' not returned! ('%s')\n",
+                               ast_str_buffer(result));
+               res = AST_TEST_FAIL;
+       }
+
+       ast_free(expr);
+       ast_free(result);
+
+       return res;
+}
+#endif
+
 static int unload_module(void)
 {
        int res = 0;
@@ -457,6 +520,7 @@ static int unload_module(void)
        res |= ast_custom_function_unregister(&math_function);
        res |= ast_custom_function_unregister(&increment_function);
        res |= ast_custom_function_unregister(&decrement_function);
+       AST_TEST_UNREGISTER(test_MATH_function);
 
        return res;
 }
@@ -468,6 +532,7 @@ static int load_module(void)
        res |= ast_custom_function_register(&math_function);
        res |= ast_custom_function_register(&increment_function);
        res |= ast_custom_function_register(&decrement_function);
+       AST_TEST_REGISTER(test_MATH_function);
 
        return res;
 }