Merge pull request #140 in FS/freeswitch from ~ARON45/freeswitch:FS-5945 to master

* commit '2cad5a6940bb76458e909a56c5e5e35e622467ed':
  more whitespace cleanup
  fixed indentation
  cleanup
  cond api return -ERR on all syntax errors
  FS-5945 - cond api improved
  FS-5945 - cond api escape special charactars
This commit is contained in:
Brian West 2015-07-02 09:45:27 -05:00
commit 1b86c73034

View File

@ -1888,9 +1888,9 @@ SWITCH_STANDARD_API(cond_function)
char *expr; char *expr;
char *a, *b; char *a, *b;
double a_f = 0.0, b_f = 0.0; double a_f = 0.0, b_f = 0.0;
int a_is_quoted = 0, b_is_quoted = 0;
o_t o = O_NONE; o_t o = O_NONE;
int is_true = 0; int is_true = 0;
char *p;
if (!cmd) { if (!cmd) {
goto error; goto error;
@ -1899,62 +1899,50 @@ SWITCH_STANDARD_API(cond_function)
mydata = strdup(cmd); mydata = strdup(cmd);
switch_assert(mydata); switch_assert(mydata);
if ((p = strchr(mydata, '?'))) { a = 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++;
if (*a == '\'') { if (*a == '\'') {
if ((expr = switch_find_end_paren(a, '\'', '\''))) { a_is_quoted = 1;
a++; for (expr = ++a; *expr; expr++) {
*expr++ = '\0'; if (*expr == '\\') {
} else { if (*(expr + 1) == '\\' || *(expr + 1) == '\'') {
expr++;
}
} else if (*expr == '\'') {
break;
}
}
if (!*expr) {
goto error;
}
*expr++ = '\0';
if (!switch_isspace(*expr)) {
goto error; goto error;
} }
} else { } else {
if ((expr = strchr(a, ' '))) { if ((expr = strchr(a, ' '))) {
*expr++ = '\0'; *expr++ = '\0';
} else { } else {
expr = a; goto error;
} }
} }
if (strspn(a, "!<>=")) { while (switch_isspace(*expr)) expr++;
expr = a;
}
if (expr == a) {
a = "";
}
while (*expr == ' ') expr++;
while(expr && *expr) {
switch(*expr) {
case '!':
case '<':
case '>':
case '=':
goto done;
default:
expr++;
break;
}
}
done:
switch(*expr) { switch(*expr) {
case '!':
case '<':
case '>':
case '=':
goto operator;
default:
goto error;
}
operator:
switch (*expr) {
case '!': case '!':
*expr++ = '\0'; *expr++ = '\0';
if (*expr == '=') { if (*expr == '=') {
@ -1995,20 +1983,58 @@ SWITCH_STANDARD_API(cond_function)
goto error; goto error;
} }
if (o) { if (o) {
char *s_a = NULL, *s_b = NULL; char *s_a = NULL, *s_b = NULL;
int a_is_num, b_is_num; int a_is_num, b_is_num;
expr++; expr++;
while (switch_isspace(*expr)) expr++;
b = expr; b = expr;
if (*b == '\'') {
b_is_quoted = 1;
for (expr = ++b; *expr; expr++) {
if (*expr == '\\') {
if (*(expr + 1) == '\\' || *(expr + 1) == '\'') {
expr++;
}
} else if (*expr == '\'') {
break;
}
}
if (!*expr) {
goto error;
}
*expr++ = '\0';
s_a = switch_strip_spaces(a, SWITCH_TRUE); if (!switch_isspace(*expr)) {
s_b = switch_strip_spaces(b, SWITCH_TRUE); goto error;
}
} else {
if ((expr = strchr(b, ' '))) {
*expr++ = '\0';
} else {
goto error;
}
}
while (switch_isspace(*expr)) expr++;
a_is_num = switch_is_number(s_a); if (*expr != '?') {
b_is_num = switch_is_number(s_b); goto error;
}
*expr = ':';
argc = switch_separate_string(expr, ':', argv, (sizeof(argv) / sizeof(argv[0])));
if (!(argc >= 2 && argc <= 3)) {
goto error;
}
s_a = a;
s_b = b;
a_is_num = (switch_is_number(s_a) && !a_is_quoted);
b_is_num = (switch_is_number(s_b) && !b_is_quoted);
a_f = a_is_num ? atof(s_a) : (float) strlen(s_a); a_f = a_is_num ? atof(s_a) : (float) strlen(s_a);
b_f = b_is_num ? atof(s_b) : (float) strlen(s_b); b_f = b_is_num ? atof(s_b) : (float) strlen(s_b);
@ -2043,20 +2069,18 @@ SWITCH_STANDARD_API(cond_function)
default: default:
break; break;
} }
switch_safe_free(s_a);
switch_safe_free(s_b);
if ((argc == 2 && !is_true)) { if ((argc == 2 && !is_true)) {
stream->write_function(stream, ""); stream->write_function(stream, "");
} else { } else {
stream->write_function(stream, "%s", is_true ? argv[1] : argv[2]); stream->write_function(stream, "%s", is_true ? argv[1] : argv[2]);
} }
goto ok; goto end;
} }
error: error:
stream->write_function(stream, "-ERR"); stream->write_function(stream, "-ERR");
ok: end:
switch_safe_free(mydata); switch_safe_free(mydata);
return SWITCH_STATUS_SUCCESS; return SWITCH_STATUS_SUCCESS;