FS-5945 - cond api escape special charactars

This commit is contained in:
Aron Podrigal 2014-12-15 14:23:33 -05:00
parent 17574a88e4
commit ca2d2c9b66

View File

@ -1893,13 +1893,13 @@ typedef enum {
SWITCH_STANDARD_API(cond_function)
{
int argc;
char *mydata = NULL, *argv[3];
char *mydata = NULL, *argv[2];
char *expr;
char *a, *b;
double a_f = 0.0, b_f = 0.0;
o_t o = O_NONE;
int is_true = 0;
char *p;
char *p = NULL;
if (!cmd) {
goto error;
@ -1908,45 +1908,38 @@ SWITCH_STANDARD_API(cond_function)
mydata = strdup(cmd);
switch_assert(mydata);
if ((p = strchr(mydata, '?'))) {
*p = ':';
} else {
goto error;
}
argc = switch_separate_string(mydata, ':', argv, (sizeof(argv) / sizeof(argv[0])));
if (! (argc >= 2 && argc <= 3)) {
goto error;
}
a = argv[0];
while(*a == ' ' || *a == '\t') a++;
a = mydata;
if (*a == '\'') {
if ((expr = switch_find_end_paren(a, '\'', '\''))) {
a++;
for (expr = ++a; expr && *expr; expr++) {
if (*expr == '\\') {
if (expr + 1 && (*(expr + 1) == '\\' || *(expr + 1) == '\'')) {
expr++;
}
} else if (*expr == '\'') {
break;
}
}
if (!expr) {
stream->write_function(stream, "-ERR while looking for closing quote near %s \n", a);
goto end;
}
*expr++ = '\0';
} else {
goto error;
if (expr && *expr != ' ' && *expr != '\t') {
stream->write_function(stream, "-ERR, Syntax error near %s \n", expr);
goto end;
}
} else {
if ((expr = strchr(a, ' '))) {
*expr++ = '\0';
} else {
expr = a;
stream->write_function(stream, "-ERR, Syntax error near %s \n", a);
goto end;
}
}
if (strspn(a, "!<>=")) {
expr = a;
}
if (expr == a) {
a = "";
}
while (*expr == ' ') expr++;
while (expr && (*expr == ' ' || *expr == '\t')) expr++;
while (expr && *expr) {
switch (*expr) {
@ -1954,14 +1947,14 @@ SWITCH_STANDARD_API(cond_function)
case '<':
case '>':
case '=':
goto done;
goto operator;
default:
expr++;
break;
}
}
done:
operator:
switch (*expr) {
case '!':
@ -2001,21 +1994,64 @@ SWITCH_STANDARD_API(cond_function)
break;
default:
goto error;
stream->write_function(stream, "-ERR, Syntax error near %s invalid conditional operator.\n", expr);
goto end;
}
if (o) {
char *s_a = NULL, *s_b = NULL;
int a_is_num, b_is_num;
expr++;
while (expr && (*expr == ' ' || *expr == '\t')) expr++;
b = expr;
if (b && *b == '\'') {
for (expr = ++b; expr && *expr; expr++) {
if (*expr == '\\') {
if (expr + 1 && (*(expr + 1) == '\\' || *(expr + 1) == '\'')) {
expr++;
}
} else if (*expr == '\'') {
break;
}
}
if (!expr) {
stream->write_function(stream, "-ERR while looking for closing quote near < %s >!\n", b);
goto end;
}
*expr++ = '\0';
s_a = switch_strip_spaces(a, SWITCH_TRUE);
s_b = switch_strip_spaces(b, SWITCH_TRUE);
if (expr && *expr != ' ' && *expr != '\t') {
stream->write_function(stream, "-ERR, Syntax error near %s \n", expr);
goto end;
}
} else {
if ((expr = strchr(b, ' '))) {
*expr++ = '\0';
} else {
stream->write_function(stream, "-ERR, Syntax error near %s \n", b);
goto end;
}
}
if ((p = strchr(expr, '?'))) {
expr = ++p;
while (expr && (*expr == ' ' || *expr == '\t')) expr++;
} else {
stream->write_function(stream, "-ERR, Syntax error near %s , no expression found.\n", expr);
goto end;
}
argc = switch_separate_string(expr, ':', argv, (sizeof (argv) / sizeof (argv[0])));
if (!(argc >= 2 && argc <= 3)) {
stream->write_function(stream, "-ERR, Syntax error near %s , Invalid expression.\n", expr);
goto end;
}
s_a = a;
s_b = b;
a_is_num = switch_is_number(s_a);
b_is_num = switch_is_number(s_b);
@ -2052,20 +2088,18 @@ SWITCH_STANDARD_API(cond_function)
default:
break;
}
switch_safe_free(s_a);
switch_safe_free(s_b);
if ((argc == 2 && !is_true)) {
if ((argc == 1 && !is_true)) {
stream->write_function(stream, "");
} else {
stream->write_function(stream, "%s", is_true ? argv[1] : argv[2]);
stream->write_function(stream, "%s", is_true ? argv[0] : argv[1]);
}
goto ok;
goto end;
}
error:
stream->write_function(stream, "-ERR");
ok:
end:
switch_safe_free(mydata);
return SWITCH_STATUS_SUCCESS;