aboutsummaryrefslogtreecommitdiffstats
path: root/pbx.c
diff options
context:
space:
mode:
authormarkster <markster@f38db490-d61c-443f-a65b-d21fe96a405b>2005-03-17 20:20:37 +0000
committermarkster <markster@f38db490-d61c-443f-a65b-d21fe96a405b>2005-03-17 20:20:37 +0000
commit8aea38c12aa73dd2541b00420de81af711115403 (patch)
tree844afdaf01c0366409cfc4c6eebaf76cfbd72321 /pbx.c
parentaa81f2eb2a259ecf4324e4b3a61b750db949b6f1 (diff)
Merge builtin If function (bug #3779)
git-svn-id: http://svn.digium.com/svn/asterisk/trunk@5189 f38db490-d61c-443f-a65b-d21fe96a405b
Diffstat (limited to 'pbx.c')
-rwxr-xr-xpbx.c44
1 files changed, 44 insertions, 0 deletions
diff --git a/pbx.c b/pbx.c
index bc64dfd97..2b4078fb5 100755
--- a/pbx.c
+++ b/pbx.c
@@ -1183,6 +1183,41 @@ static char *builtin_function_exists(struct ast_channel *chan, char *cmd, char *
return cmd ? ret_true : ret_false;
}
+static char *builtin_function_if(struct ast_channel *chan, char *cmd, char *data, char *buf, size_t len)
+{
+ char *ret = NULL;
+ char *mydata = NULL;
+ char *expr = NULL;
+ char *iftrue = NULL;
+ char *iffalse = NULL;
+
+ if((mydata = ast_strdupa(data))) {
+ expr = mydata;
+ if ((iftrue = strchr(mydata, '?'))) {
+ *iftrue = '\0';
+ iftrue++;
+ if ((iffalse = strchr(iftrue, ':'))) {
+ *iffalse = '\0';
+ iffalse++;
+ }
+ } else
+ iffalse = "";
+ if (expr && iftrue) {
+ ret = ast_true(expr) ? iftrue : iffalse;
+ strncpy(buf, ret, len);
+ ret = buf;
+ } else {
+ ast_log(LOG_WARNING, "Syntax $(if <expr>?[<truecond>][:<falsecond>])\n");
+ ret = NULL;
+ }
+ } else {
+ ast_log(LOG_WARNING, "Memory Error!\n");
+ ret = NULL;
+ }
+
+ return ret;
+}
+
static char *builtin_function_regex(struct ast_channel *chan, char *cmd, char *data, char *buf, size_t len)
{
char *ret_true = "1", *ret_false = "0", *ret;
@@ -3392,6 +3427,14 @@ static struct ast_custom_function_obj exists_function = {
.function = builtin_function_exists,
};
+static struct ast_custom_function_obj if_function = {
+ .name = "if",
+ .desc = "Conditional: Returns the data following '?' if true else the data following ':'",
+ .syntax = "$(if <expr>?<true>:<false>)",
+ .function = builtin_function_if,
+};
+
+
/*
* CLI entries for upper commands ...
*/
@@ -5766,6 +5809,7 @@ int load_pbx(void)
ast_custom_function_register(&regex_function);
ast_custom_function_register(&isnull_function);
ast_custom_function_register(&exists_function);
+ ast_custom_function_register(&if_function);
/* Register builtin applications */
for (x=0; x<sizeof(builtins) / sizeof(struct pbx_builtin); x++) {