]> git.ipfire.org Git - thirdparty/asterisk.git/commitdiff
merged qwell's func_math patch for issue 9507
authorDwayne M. Hubbard <dwayne.hubbard@gmail.com>
Thu, 24 May 2007 15:08:56 +0000 (15:08 +0000)
committerDwayne M. Hubbard <dwayne.hubbard@gmail.com>
Thu, 24 May 2007 15:08:56 +0000 (15:08 +0000)
git-svn-id: https://origsvn.digium.com/svn/asterisk/branches/1.4@65866 65c4cc65-6c06-0410-ace0-fbb531ad65f3

funcs/func_math.c

index 386897dbd8de131c751060c0a13fa400749b8d8d..ea409130be2f8184fbf72ddeb580fdfbb782969e 100644 (file)
@@ -71,6 +71,7 @@ static int math(struct ast_channel *chan, char *cmd, char *parse,
        int iaction = -1;
        int type_of_result = FLOAT_RESULT;
        char *mvalue1, *mvalue2 = NULL, *mtype_of_result;
+       int negvalue1 = 0;
        AST_DECLARE_APP_ARGS(args,
                             AST_APP_ARG(argv0);
                             AST_APP_ARG(argv1);
@@ -90,13 +91,12 @@ static int math(struct ast_channel *chan, char *cmd, char *parse,
 
        mvalue1 = args.argv0;
 
-       if ((op = strchr(mvalue1, '+'))) {
-               iaction = ADDFUNCTION;
-               *op = '\0';
-       } else if ((op = strchr(mvalue1, '-'))) {
-               iaction = SUBTRACTFUNCTION;
-               *op = '\0';
-       } else if ((op = strchr(mvalue1, '*'))) {
+       if (mvalue1[0] == '-') {
+               negvalue1 = 1;
+               mvalue1++;
+       }
+
+       if ((op = strchr(mvalue1, '*'))) {
                iaction = MULTIPLYFUNCTION;
                *op = '\0';
        } else if ((op = strchr(mvalue1, '/'))) {
@@ -126,6 +126,12 @@ static int math(struct ast_channel *chan, char *cmd, char *parse,
                        iaction = EQFUNCTION;
                } 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 */
+               iaction = SUBTRACTFUNCTION;
+               *op = '\0';
        }
 
        if (op)
@@ -169,6 +175,9 @@ static int math(struct ast_channel *chan, char *cmd, char *parse,
                return -1;
        }
 
+       if (negvalue1)
+               fnum1 = 0 - fnum1;
+
        switch (iaction) {
        case ADDFUNCTION:
                ftmp = fnum1 + fnum2;