fix compilation warnings
[asterisk-bristuff.git] / funcs / func_logic.c
blobc5619fbfe7144bba2669f46fb8acfd49b5bd63cf
1 /*
2 * Asterisk -- An open source telephony toolkit.
4 * Copyright (C) 1999 - 2006, Digium, Inc.
5 * Portions Copyright (C) 2005, Anthony Minessale II
7 * See http://www.asterisk.org for more information about
8 * the Asterisk project. Please do not directly contact
9 * any of the maintainers of this project for assistance;
10 * the project provides a web site, mailing lists and IRC
11 * channels for your use.
13 * This program is free software, distributed under the terms of
14 * the GNU General Public License Version 2. See the LICENSE file
15 * at the top of the source tree.
18 /*! \file
20 * \brief Conditional logic dialplan functions
22 * \author Anthony Minessale II
25 #include "asterisk.h"
27 ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
29 #include <stdio.h>
30 #include <stdlib.h>
31 #include <string.h>
32 #include <sys/types.h>
34 #include "asterisk/module.h"
35 #include "asterisk/channel.h"
36 #include "asterisk/pbx.h"
37 #include "asterisk/logger.h"
38 #include "asterisk/utils.h"
39 #include "asterisk/app.h"
41 static int isnull(struct ast_channel *chan, char *cmd, char *data,
42 char *buf, size_t len)
44 strcpy(buf, data && *data ? "0" : "1");
46 return 0;
49 static int exists(struct ast_channel *chan, char *cmd, char *data, char *buf,
50 size_t len)
52 strcpy(buf, data && *data ? "1" : "0");
54 return 0;
57 static int iftime(struct ast_channel *chan, char *cmd, char *data, char *buf,
58 size_t len)
60 struct ast_timing timing;
61 char *expr;
62 char *iftrue;
63 char *iffalse;
65 data = ast_strip_quoted(data, "\"", "\"");
66 expr = strsep(&data, "?");
67 iftrue = strsep(&data, ":");
68 iffalse = data;
70 if (ast_strlen_zero(expr) || !(iftrue || iffalse)) {
71 ast_log(LOG_WARNING,
72 "Syntax IFTIME(<timespec>?[<true>][:<false>])\n");
73 return -1;
76 if (!ast_build_timing(&timing, expr)) {
77 ast_log(LOG_WARNING, "Invalid Time Spec.\n");
78 return -1;
81 if (iftrue)
82 iftrue = ast_strip_quoted(iftrue, "\"", "\"");
83 if (iffalse)
84 iffalse = ast_strip_quoted(iffalse, "\"", "\"");
86 ast_copy_string(buf, ast_check_timing(&timing) ? iftrue : iffalse, len);
88 return 0;
91 static int acf_if(struct ast_channel *chan, char *cmd, char *data, char *buf,
92 size_t len)
94 AST_DECLARE_APP_ARGS(args1,
95 AST_APP_ARG(expr);
96 AST_APP_ARG(remainder);
98 AST_DECLARE_APP_ARGS(args2,
99 AST_APP_ARG(iftrue);
100 AST_APP_ARG(iffalse);
102 args2.iftrue = args2.iffalse = NULL; /* you have to set these, because if there is nothing after the '?',
103 then args1.remainder will be NULL, not a pointer to a null string, and
104 then any garbage in args2.iffalse will not be cleared, and you'll crash.
105 -- and if you mod the ast_app_separate_args func instead, you'll really
106 mess things up badly, because the rest of everything depends on null args
107 for non-specified stuff. */
109 AST_NONSTANDARD_APP_ARGS(args1, data, '?');
110 AST_NONSTANDARD_APP_ARGS(args2, args1.remainder, ':');
112 if (ast_strlen_zero(args1.expr) || !(args2.iftrue || args2.iffalse)) {
113 ast_log(LOG_WARNING, "Syntax IF(<expr>?[<true>][:<false>]) (expr must be non-null, and either <true> or <false> must be non-null)\n");
114 ast_log(LOG_WARNING, " In this case, <expr>='%s', <true>='%s', and <false>='%s'\n", args1.expr, args2.iftrue, args2.iffalse);
115 return -1;
118 args1.expr = ast_strip(args1.expr);
119 if (args2.iftrue)
120 args2.iftrue = ast_strip(args2.iftrue);
121 if (args2.iffalse)
122 args2.iffalse = ast_strip(args2.iffalse);
124 ast_copy_string(buf, pbx_checkcondition(args1.expr) ? (S_OR(args2.iftrue, "")) : (S_OR(args2.iffalse, "")), len);
126 return 0;
129 static int set(struct ast_channel *chan, char *cmd, char *data, char *buf,
130 size_t len)
132 char *varname;
133 char *val;
135 varname = strsep(&data, "=");
136 val = data;
138 if (ast_strlen_zero(varname) || !val) {
139 ast_log(LOG_WARNING, "Syntax SET(<varname>=[<value>])\n");
140 return -1;
143 varname = ast_strip(varname);
144 val = ast_strip(val);
145 pbx_builtin_setvar_helper(chan, varname, val);
146 ast_copy_string(buf, val, len);
148 return 0;
151 static struct ast_custom_function isnull_function = {
152 .name = "ISNULL",
153 .synopsis = "NULL Test: Returns 1 if NULL or 0 otherwise",
154 .syntax = "ISNULL(<data>)",
155 .read = isnull,
158 static struct ast_custom_function set_function = {
159 .name = "SET",
160 .synopsis = "SET assigns a value to a channel variable",
161 .syntax = "SET(<varname>=[<value>])",
162 .read = set,
165 static struct ast_custom_function exists_function = {
166 .name = "EXISTS",
167 .synopsis = "Existence Test: Returns 1 if exists, 0 otherwise",
168 .syntax = "EXISTS(<data>)",
169 .read = exists,
172 static struct ast_custom_function if_function = {
173 .name = "IF",
174 .synopsis =
175 "Conditional: Returns the data following '?' if true else the data following ':'",
176 .syntax = "IF(<expr>?[<true>][:<false>])",
177 .read = acf_if,
180 static struct ast_custom_function if_time_function = {
181 .name = "IFTIME",
182 .synopsis =
183 "Temporal Conditional: Returns the data following '?' if true else the data following ':'",
184 .syntax = "IFTIME(<timespec>?[<true>][:<false>])",
185 .read = iftime,
188 static int unload_module(void)
190 int res = 0;
192 res |= ast_custom_function_unregister(&isnull_function);
193 res |= ast_custom_function_unregister(&set_function);
194 res |= ast_custom_function_unregister(&exists_function);
195 res |= ast_custom_function_unregister(&if_function);
196 res |= ast_custom_function_unregister(&if_time_function);
198 return res;
201 static int load_module(void)
203 int res = 0;
205 res |= ast_custom_function_register(&isnull_function);
206 res |= ast_custom_function_register(&set_function);
207 res |= ast_custom_function_register(&exists_function);
208 res |= ast_custom_function_register(&if_function);
209 res |= ast_custom_function_register(&if_time_function);
211 return res;
214 AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "Logical dialplan functions");