Asterisk - The Open Source Telephony Project GIT-master-7e7a603
Functions | Variables
func_logic.c File Reference

Conditional logic dialplan functions. More...

#include "asterisk.h"
#include "asterisk/module.h"
#include "asterisk/channel.h"
#include "asterisk/pbx.h"
#include "asterisk/utils.h"
#include "asterisk/app.h"
Include dependency graph for func_logic.c:

Go to the source code of this file.

Functions

static void __reg_module (void)
 
static void __unreg_module (void)
 
static int acf_if (struct ast_channel *chan, const char *cmd, char *data, char *buf, size_t len)
 
struct ast_moduleAST_MODULE_SELF_SYM (void)
 
static int delete_write (struct ast_channel *chan, const char *cmd, char *data, const char *value)
 
static int exists (struct ast_channel *chan, const char *cmd, char *data, char *buf, size_t len)
 
static int iftime (struct ast_channel *chan, const char *cmd, char *data, char *buf, size_t len)
 
static int import_helper (struct ast_channel *chan, const char *cmd, char *data, char *buf, struct ast_str **str, ssize_t len)
 
static int import_read (struct ast_channel *chan, const char *cmd, char *data, char *buf, size_t len)
 
static int import_read2 (struct ast_channel *chan, const char *cmd, char *data, struct ast_str **str, ssize_t len)
 
static int isnull (struct ast_channel *chan, const char *cmd, char *data, char *buf, size_t len)
 
static int load_module (void)
 
static int set (struct ast_channel *chan, const char *cmd, char *data, char *buf, size_t len)
 
static int set2 (struct ast_channel *chan, const char *cmd, char *data, struct ast_str **str, ssize_t len)
 
static int unload_module (void)
 
static int variable_exists_read (struct ast_channel *chan, const char *cmd, char *data, char *buf, size_t len)
 

Variables

static struct ast_module_info __mod_info = { .name = AST_MODULE, .flags = AST_MODFLAG_LOAD_ORDER , .description = "Logical dialplan functions" , .key = "This paragraph is copyright (c) 2006 by Digium, Inc. \In order for your module to load, it must return this \key via a function called \"key\". Any code which \includes this paragraph must be licensed under the GNU \General Public License version 2 or later (at your \option). In addition to Digium's general reservations \of rights, Digium expressly reserves the right to \allow other parties to license this paragraph under \different terms. Any use of Digium, Inc. trademarks or \logos (including \"Asterisk\" or \"Digium\") without \express written permission of Digium, Inc. is prohibited.\n" , .buildopt_sum = AST_BUILDOPT_SUM, .load = load_module, .unload = unload_module, .load_pri = AST_MODPRI_DEFAULT, .support_level = AST_MODULE_SUPPORT_CORE, }
 
static const struct ast_module_infoast_module_info = &__mod_info
 
static struct ast_custom_function delete_function
 
static struct ast_custom_function exists_function
 
static struct ast_custom_function if_function
 
static struct ast_custom_function if_time_function
 
static struct ast_custom_function import_function
 
static struct ast_custom_function isnull_function
 
static struct ast_custom_function set_function
 
static struct ast_custom_function variable_exists_function
 

Detailed Description

Conditional logic dialplan functions.

Author
Anthony Minessale II

Definition in file func_logic.c.

Function Documentation

◆ __reg_module()

static void __reg_module ( void  )
static

Definition at line 402 of file func_logic.c.

◆ __unreg_module()

static void __unreg_module ( void  )
static

Definition at line 402 of file func_logic.c.

◆ acf_if()

static int acf_if ( struct ast_channel chan,
const char *  cmd,
char *  data,
char *  buf,
size_t  len 
)
static

Definition at line 201 of file func_logic.c.

203{
205 AST_APP_ARG(expr);
206 AST_APP_ARG(remainder);
207 );
209 AST_APP_ARG(iftrue);
210 AST_APP_ARG(iffalse);
211 );
212 args2.iftrue = args2.iffalse = NULL; /* you have to set these, because if there is nothing after the '?',
213 then args1.remainder will be NULL, not a pointer to a null string, and
214 then any garbage in args2.iffalse will not be cleared, and you'll crash.
215 -- and if you mod the ast_app_separate_args func instead, you'll really
216 mess things up badly, because the rest of everything depends on null args
217 for non-specified stuff. */
218
219 AST_NONSTANDARD_APP_ARGS(args1, data, '?');
220 AST_NONSTANDARD_APP_ARGS(args2, args1.remainder, ':');
221
222 if (ast_strlen_zero(args1.expr) || !(args2.iftrue || args2.iffalse)) {
223 ast_debug(1, "<expr>='%s', <true>='%s', and <false>='%s'\n", args1.expr, args2.iftrue, args2.iffalse);
224 return -1;
225 }
226
227 args1.expr = ast_strip(args1.expr);
228 if (args2.iftrue)
229 args2.iftrue = ast_strip(args2.iftrue);
230 if (args2.iffalse)
231 args2.iffalse = ast_strip(args2.iffalse);
232
233 ast_copy_string(buf, pbx_checkcondition(args1.expr) ? (S_OR(args2.iftrue, "")) : (S_OR(args2.iffalse, "")), len);
234
235 return 0;
236}
char buf[BUFSIZE]
Definition: eagi_proxy.c:66
static int len(struct ast_channel *chan, const char *cmd, char *data, char *buf, size_t buflen)
#define AST_APP_ARG(name)
Define an application argument.
#define AST_DECLARE_APP_ARGS(name, arglist)
Declare a structure to hold an application's arguments.
#define AST_NONSTANDARD_APP_ARGS(args, parse, sep)
Performs the 'nonstandard' argument separation process for an application.
#define ast_debug(level,...)
Log a DEBUG message.
int pbx_checkcondition(const char *condition)
Evaluate a condition.
Definition: pbx.c:8282
#define NULL
Definition: resample.c:96
#define S_OR(a, b)
returns the equivalent of logic or for strings: first one if not empty, otherwise second one.
Definition: strings.h:80
static force_inline int attribute_pure ast_strlen_zero(const char *s)
Definition: strings.h:65
void ast_copy_string(char *dst, const char *src, size_t size)
Size-limited null-terminating string copy.
Definition: strings.h:425
char * ast_strip(char *s)
Strip leading/trailing whitespace from a string.
Definition: strings.h:223

References AST_APP_ARG, ast_copy_string(), ast_debug, AST_DECLARE_APP_ARGS, AST_NONSTANDARD_APP_ARGS, ast_strip(), ast_strlen_zero(), buf, len(), NULL, pbx_checkcondition(), and S_OR.

◆ AST_MODULE_SELF_SYM()

struct ast_module * AST_MODULE_SELF_SYM ( void  )

Definition at line 402 of file func_logic.c.

◆ delete_write()

static int delete_write ( struct ast_channel chan,
const char *  cmd,
char *  data,
const char *  value 
)
static

Definition at line 309 of file func_logic.c.

310{
311 pbx_builtin_setvar_helper(chan, data, NULL);
312
313 return 0;
314}
int pbx_builtin_setvar_helper(struct ast_channel *chan, const char *name, const char *value)
Add a variable to the channel variable stack, removing the most recently set value for the same name.

References ast_channel::data, NULL, and pbx_builtin_setvar_helper().

◆ exists()

static int exists ( struct ast_channel chan,
const char *  cmd,
char *  data,
char *  buf,
size_t  len 
)
static

Definition at line 157 of file func_logic.c.

159{
160 strcpy(buf, data && *data ? "1" : "0");
161
162 return 0;
163}

References buf.

Referenced by ast_mwi_mailbox_update(), chan_pjsip_cng_tone_detected(), and socket_process_helper().

◆ iftime()

static int iftime ( struct ast_channel chan,
const char *  cmd,
char *  data,
char *  buf,
size_t  len 
)
static

Definition at line 165 of file func_logic.c.

167{
168 struct ast_timing timing;
169 char *expr;
170 char *iftrue;
171 char *iffalse;
172
173 data = ast_strip_quoted(data, "\"", "\"");
174 expr = strsep(&data, "?");
175 iftrue = strsep(&data, ":");
176 iffalse = data;
177
178 if (ast_strlen_zero(expr) || !(iftrue || iffalse)) {
180 "Syntax IFTIME(<timespec>?[<true>][:<false>])\n");
181 return -1;
182 }
183
184 if (!ast_build_timing(&timing, expr)) {
185 ast_log(LOG_WARNING, "Invalid Time Spec.\n");
186 ast_destroy_timing(&timing);
187 return -1;
188 }
189
190 if (iftrue)
191 iftrue = ast_strip_quoted(iftrue, "\"", "\"");
192 if (iffalse)
193 iffalse = ast_strip_quoted(iffalse, "\"", "\"");
194
195 ast_copy_string(buf, ast_check_timing(&timing) ? S_OR(iftrue, "") : S_OR(iffalse, ""), len);
196 ast_destroy_timing(&timing);
197
198 return 0;
199}
#define ast_log
Definition: astobj2.c:42
char * strsep(char **str, const char *delims)
#define LOG_WARNING
int ast_build_timing(struct ast_timing *i, const char *info_in)
Construct a timing bitmap, for use in time-based conditionals.
Definition: extconf.c:3806
int ast_check_timing(const struct ast_timing *i)
Evaluate a pre-constructed bitmap as to whether the current time falls within the range specified.
Definition: extconf.c:4000
int ast_destroy_timing(struct ast_timing *i)
Deallocates memory structures associated with a timing bitmap.
Definition: pbx_timing.c:279
char * ast_strip_quoted(char *s, const char *beg_quotes, const char *end_quotes)
Strip leading/trailing whitespace and quotes from a string.
Definition: utils.c:1818

References ast_build_timing(), ast_check_timing(), ast_copy_string(), ast_destroy_timing(), ast_log, ast_strip_quoted(), ast_strlen_zero(), buf, len(), LOG_WARNING, S_OR, and strsep().

◆ import_helper()

static int import_helper ( struct ast_channel chan,
const char *  cmd,
char *  data,
char *  buf,
struct ast_str **  str,
ssize_t  len 
)
static

Definition at line 268 of file func_logic.c.

269{
271 AST_APP_ARG(channel);
272 AST_APP_ARG(varname);
273 );
275 if (buf) {
276 *buf = '\0';
277 }
278
279 if (!ast_strlen_zero(args.varname)) {
280 struct ast_channel *chan2;
281
282 if ((chan2 = ast_channel_get_by_name(args.channel))) {
283 char *s = ast_alloca(strlen(args.varname) + 4);
284 sprintf(s, "${%s}", args.varname);
285 ast_channel_lock(chan2);
286 if (buf) {
288 } else {
290 }
291 ast_channel_unlock(chan2);
292 chan2 = ast_channel_unref(chan2);
293 }
294 }
295
296 return 0;
297}
const char * str
Definition: app_jack.c:147
#define ast_alloca(size)
call __builtin_alloca to ensure we get gcc builtin semantics
Definition: astmm.h:288
#define ast_channel_lock(chan)
Definition: channel.h:2922
#define ast_channel_unref(c)
Decrease channel reference count.
Definition: channel.h:2958
struct ast_channel * ast_channel_get_by_name(const char *name)
Find a channel by name.
Definition: channel.c:1454
#define ast_channel_unlock(chan)
Definition: channel.h:2923
#define AST_STANDARD_APP_ARGS(args, parse)
Performs the 'standard' argument separation process for an application.
void ast_str_substitute_variables(struct ast_str **buf, ssize_t maxlen, struct ast_channel *chan, const char *templ)
void pbx_substitute_variables_helper(struct ast_channel *c, const char *cp1, char *cp2, int count)
Definition: ael_main.c:211
Main Channel structure associated with a channel.
const char * args

References args, ast_alloca, AST_APP_ARG, ast_channel_get_by_name(), ast_channel_lock, ast_channel_unlock, ast_channel_unref, AST_DECLARE_APP_ARGS, AST_STANDARD_APP_ARGS, ast_str_substitute_variables(), ast_strlen_zero(), buf, len(), pbx_substitute_variables_helper(), and str.

Referenced by import_read(), and import_read2().

◆ import_read()

static int import_read ( struct ast_channel chan,
const char *  cmd,
char *  data,
char *  buf,
size_t  len 
)
static

Definition at line 299 of file func_logic.c.

300{
301 return import_helper(chan, cmd, data, buf, NULL, len);
302}
static int import_helper(struct ast_channel *chan, const char *cmd, char *data, char *buf, struct ast_str **str, ssize_t len)
Definition: func_logic.c:268

References buf, ast_channel::data, import_helper(), len(), and NULL.

◆ import_read2()

static int import_read2 ( struct ast_channel chan,
const char *  cmd,
char *  data,
struct ast_str **  str,
ssize_t  len 
)
static

Definition at line 304 of file func_logic.c.

305{
306 return import_helper(chan, cmd, data, NULL, str, len);
307}

References ast_channel::data, import_helper(), len(), NULL, and str.

◆ isnull()

static int isnull ( struct ast_channel chan,
const char *  cmd,
char *  data,
char *  buf,
size_t  len 
)
static

Definition at line 149 of file func_logic.c.

151{
152 strcpy(buf, data && *data ? "0" : "1");
153
154 return 0;
155}

References buf.

◆ load_module()

static int load_module ( void  )
static

Definition at line 386 of file func_logic.c.

387{
388 int res = 0;
389
398
399 return res;
400}
static struct ast_custom_function exists_function
Definition: func_logic.c:338
static struct ast_custom_function variable_exists_function
Definition: func_logic.c:365
static struct ast_custom_function import_function
Definition: func_logic.c:354
static struct ast_custom_function set_function
Definition: func_logic.c:332
static struct ast_custom_function if_function
Definition: func_logic.c:344
static struct ast_custom_function delete_function
Definition: func_logic.c:360
static struct ast_custom_function if_time_function
Definition: func_logic.c:349
static struct ast_custom_function isnull_function
Definition: func_logic.c:326
#define ast_custom_function_register(acf)
Register a custom function.
Definition: pbx.h:1558

References ast_custom_function_register, delete_function, exists_function, if_function, if_time_function, import_function, isnull_function, set_function, and variable_exists_function.

◆ set()

static int set ( struct ast_channel chan,
const char *  cmd,
char *  data,
char *  buf,
size_t  len 
)
static

Definition at line 238 of file func_logic.c.

240{
241 char *varname;
242 char *val;
243
244 varname = strsep(&data, "=");
245 val = data;
246
247 if (ast_strlen_zero(varname) || !val) {
248 ast_log(LOG_WARNING, "Syntax SET(<varname>=[<value>])\n");
249 return -1;
250 }
251
252 varname = ast_strip(varname);
253 val = ast_strip(val);
254 pbx_builtin_setvar_helper(chan, varname, val);
256
257 return 0;
258}
Definition: ast_expr2.c:325

References ast_copy_string(), ast_log, ast_strip(), ast_strlen_zero(), buf, len(), LOG_WARNING, pbx_builtin_setvar_helper(), and strsep().

Referenced by __big_keydata(), collect_data(), collect_key(), handle_video_on_join(), open_temp(), set2(), test_sorcery_transform(), and tmp().

◆ set2()

static int set2 ( struct ast_channel chan,
const char *  cmd,
char *  data,
struct ast_str **  str,
ssize_t  len 
)
static

Definition at line 260 of file func_logic.c.

261{
262 if (len > -1) {
263 ast_str_make_space(str, len == 0 ? strlen(data) : len);
264 }
265 return set(chan, cmd, data, ast_str_buffer(*str), ast_str_size(*str));
266}
static int set(struct ast_channel *chan, const char *cmd, char *data, char *buf, size_t len)
Definition: func_logic.c:238
char * ast_str_buffer(const struct ast_str *buf)
Returns the string buffer within the ast_str buf.
Definition: strings.h:761
#define ast_str_make_space(buf, new_len)
Definition: strings.h:828
size_t ast_str_size(const struct ast_str *buf)
Returns the current maximum length (without reallocation) of the current buffer.
Definition: strings.h:742

References ast_str_buffer(), ast_str_make_space, ast_str_size(), len(), set(), and str.

◆ unload_module()

static int unload_module ( void  )
static

◆ variable_exists_read()

static int variable_exists_read ( struct ast_channel chan,
const char *  cmd,
char *  data,
char *  buf,
size_t  len 
)
static

Definition at line 316 of file func_logic.c.

318{
319 const char *var = pbx_builtin_getvar_helper(chan, data);
320
321 strcpy(buf, var ? "1" : "0");
322
323 return 0;
324}
#define var
Definition: ast_expr2f.c:605
const char * pbx_builtin_getvar_helper(struct ast_channel *chan, const char *name)
Return a pointer to the value of the corresponding channel variable.

References buf, ast_channel::data, pbx_builtin_getvar_helper(), and var.

Variable Documentation

◆ __mod_info

struct ast_module_info __mod_info = { .name = AST_MODULE, .flags = AST_MODFLAG_LOAD_ORDER , .description = "Logical dialplan functions" , .key = "This paragraph is copyright (c) 2006 by Digium, Inc. \In order for your module to load, it must return this \key via a function called \"key\". Any code which \includes this paragraph must be licensed under the GNU \General Public License version 2 or later (at your \option). In addition to Digium's general reservations \of rights, Digium expressly reserves the right to \allow other parties to license this paragraph under \different terms. Any use of Digium, Inc. trademarks or \logos (including \"Asterisk\" or \"Digium\") without \express written permission of Digium, Inc. is prohibited.\n" , .buildopt_sum = AST_BUILDOPT_SUM, .load = load_module, .unload = unload_module, .load_pri = AST_MODPRI_DEFAULT, .support_level = AST_MODULE_SUPPORT_CORE, }
static

Definition at line 402 of file func_logic.c.

◆ ast_module_info

const struct ast_module_info* ast_module_info = &__mod_info
static

Definition at line 402 of file func_logic.c.

◆ delete_function

struct ast_custom_function delete_function
static
Initial value:
= {
.name = "DELETE",
.write = delete_write,
}
static int delete_write(struct ast_channel *chan, const char *cmd, char *data, const char *value)
Definition: func_logic.c:309

Definition at line 360 of file func_logic.c.

Referenced by load_module(), and unload_module().

◆ exists_function

struct ast_custom_function exists_function
static
Initial value:
= {
.name = "EXISTS",
.read = exists,
.read_max = 2,
}
static int exists(struct ast_channel *chan, const char *cmd, char *data, char *buf, size_t len)
Definition: func_logic.c:157

Definition at line 338 of file func_logic.c.

Referenced by load_module(), and unload_module().

◆ if_function

struct ast_custom_function if_function
static
Initial value:
= {
.name = "IF",
.read = acf_if,
}
static int acf_if(struct ast_channel *chan, const char *cmd, char *data, char *buf, size_t len)
Definition: func_logic.c:201

Definition at line 344 of file func_logic.c.

Referenced by load_module(), and unload_module().

◆ if_time_function

struct ast_custom_function if_time_function
static
Initial value:
= {
.name = "IFTIME",
.read = iftime,
}
static int iftime(struct ast_channel *chan, const char *cmd, char *data, char *buf, size_t len)
Definition: func_logic.c:165

Definition at line 349 of file func_logic.c.

Referenced by load_module(), and unload_module().

◆ import_function

struct ast_custom_function import_function
static
Initial value:
= {
.name = "IMPORT",
.read = import_read,
.read2 = import_read2,
}
static int import_read2(struct ast_channel *chan, const char *cmd, char *data, struct ast_str **str, ssize_t len)
Definition: func_logic.c:304
static int import_read(struct ast_channel *chan, const char *cmd, char *data, char *buf, size_t len)
Definition: func_logic.c:299

Definition at line 354 of file func_logic.c.

Referenced by load_module(), and unload_module().

◆ isnull_function

struct ast_custom_function isnull_function
static
Initial value:
= {
.name = "ISNULL",
.read = isnull,
.read_max = 2,
}
static int isnull(struct ast_channel *chan, const char *cmd, char *data, char *buf, size_t len)
Definition: func_logic.c:149

Definition at line 326 of file func_logic.c.

Referenced by load_module(), and unload_module().

◆ set_function

struct ast_custom_function set_function
static
Initial value:
= {
.name = "SET",
.read = set,
.read2 = set2,
}
static int set2(struct ast_channel *chan, const char *cmd, char *data, struct ast_str **str, ssize_t len)
Definition: func_logic.c:260

Definition at line 332 of file func_logic.c.

Referenced by load_module(), and unload_module().

◆ variable_exists_function

struct ast_custom_function variable_exists_function
static
Initial value:
= {
.name = "VARIABLE_EXISTS",
}
static int variable_exists_read(struct ast_channel *chan, const char *cmd, char *data, char *buf, size_t len)
Definition: func_logic.c:316

Definition at line 365 of file func_logic.c.

Referenced by load_module(), and unload_module().