Wed Jan 8 2020 09:50:13

Asterisk developer's documentation


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"

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)
 
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)
 

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 = "ac1f6a56484a8820659555499174e588" , .load = load_module, .unload = unload_module, .load_pri = AST_MODPRI_DEFAULT, }
 
static struct ast_module_infoast_module_info = &__mod_info
 
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
 

Detailed Description

Conditional logic dialplan functions.

Author
Anthony Minessale II

Definition in file func_logic.c.

Function Documentation

static void __reg_module ( void  )
static

Definition at line 341 of file func_logic.c.

static void __unreg_module ( void  )
static

Definition at line 341 of file func_logic.c.

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

Definition at line 170 of file func_logic.c.

References AST_APP_ARG, ast_copy_string(), AST_DECLARE_APP_ARGS, ast_log(), AST_NONSTANDARD_APP_ARGS, ast_strip(), ast_strlen_zero(), LOG_WARNING, pbx_checkcondition(), and S_OR.

172 {
173  AST_DECLARE_APP_ARGS(args1,
174  AST_APP_ARG(expr);
175  AST_APP_ARG(remainder);
176  );
177  AST_DECLARE_APP_ARGS(args2,
178  AST_APP_ARG(iftrue);
179  AST_APP_ARG(iffalse);
180  );
181  args2.iftrue = args2.iffalse = NULL; /* you have to set these, because if there is nothing after the '?',
182  then args1.remainder will be NULL, not a pointer to a null string, and
183  then any garbage in args2.iffalse will not be cleared, and you'll crash.
184  -- and if you mod the ast_app_separate_args func instead, you'll really
185  mess things up badly, because the rest of everything depends on null args
186  for non-specified stuff. */
187 
188  AST_NONSTANDARD_APP_ARGS(args1, data, '?');
189  AST_NONSTANDARD_APP_ARGS(args2, args1.remainder, ':');
190 
191  if (ast_strlen_zero(args1.expr) || !(args2.iftrue || args2.iffalse)) {
192  ast_log(LOG_WARNING, "Syntax IF(<expr>?[<true>][:<false>]) (expr must be non-null, and either <true> or <false> must be non-null)\n");
193  ast_log(LOG_WARNING, " In this case, <expr>='%s', <true>='%s', and <false>='%s'\n", args1.expr, args2.iftrue, args2.iffalse);
194  return -1;
195  }
196 
197  args1.expr = ast_strip(args1.expr);
198  if (args2.iftrue)
199  args2.iftrue = ast_strip(args2.iftrue);
200  if (args2.iffalse)
201  args2.iffalse = ast_strip(args2.iffalse);
202 
203  ast_copy_string(buf, pbx_checkcondition(args1.expr) ? (S_OR(args2.iftrue, "")) : (S_OR(args2.iffalse, "")), len);
204 
205  return 0;
206 }
#define LOG_WARNING
Definition: logger.h:144
#define AST_DECLARE_APP_ARGS(name, arglist)
Declare a structure to hold an application&#39;s arguments.
Definition: app.h:572
int pbx_checkcondition(const char *condition)
Evaluate a condition.
Definition: pbx.c:10719
static force_inline int attribute_pure ast_strlen_zero(const char *s)
Definition: strings.h:63
char * ast_strip(char *s)
Strip leading/trailing whitespace from a string.
Definition: strings.h:155
static int len(struct ast_channel *chan, const char *cmd, char *data, char *buf, size_t buflen)
void ast_log(int level, const char *file, int line, const char *function, const char *fmt,...)
Used for sending a log message This is the standard logger function. Probably the only way you will i...
Definition: logger.c:1207
void ast_copy_string(char *dst, const char *src, size_t size)
Size-limited null-terminating string copy.
Definition: strings.h:223
#define S_OR(a, b)
returns the equivalent of logic or for strings: first one if not empty, otherwise second one...
Definition: strings.h:77
#define AST_APP_ARG(name)
Define an application argument.
Definition: app.h:555
#define AST_NONSTANDARD_APP_ARGS(args, parse, sep)
Performs the &#39;nonstandard&#39; argument separation process for an application.
Definition: app.h:619
static int exists ( struct ast_channel chan,
const char *  cmd,
char *  data,
char *  buf,
size_t  len 
)
static

Definition at line 126 of file func_logic.c.

Referenced by socket_process().

128 {
129  strcpy(buf, data && *data ? "1" : "0");
130 
131  return 0;
132 }
static int iftime ( struct ast_channel chan,
const char *  cmd,
char *  data,
char *  buf,
size_t  len 
)
static

Definition at line 134 of file func_logic.c.

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

136 {
137  struct ast_timing timing;
138  char *expr;
139  char *iftrue;
140  char *iffalse;
141 
142  data = ast_strip_quoted(data, "\"", "\"");
143  expr = strsep(&data, "?");
144  iftrue = strsep(&data, ":");
145  iffalse = data;
146 
147  if (ast_strlen_zero(expr) || !(iftrue || iffalse)) {
149  "Syntax IFTIME(<timespec>?[<true>][:<false>])\n");
150  return -1;
151  }
152 
153  if (!ast_build_timing(&timing, expr)) {
154  ast_log(LOG_WARNING, "Invalid Time Spec.\n");
155  ast_destroy_timing(&timing);
156  return -1;
157  }
158 
159  if (iftrue)
160  iftrue = ast_strip_quoted(iftrue, "\"", "\"");
161  if (iffalse)
162  iffalse = ast_strip_quoted(iffalse, "\"", "\"");
163 
164  ast_copy_string(buf, ast_check_timing(&timing) ? S_OR(iftrue, "") : S_OR(iffalse, ""), len);
165  ast_destroy_timing(&timing);
166 
167  return 0;
168 }
char * strsep(char **str, const char *delims)
#define LOG_WARNING
Definition: logger.h:144
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:1431
static force_inline int attribute_pure ast_strlen_zero(const char *s)
Definition: strings.h:63
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: pbx.c:8357
static int len(struct ast_channel *chan, const char *cmd, char *data, char *buf, size_t buflen)
void ast_log(int level, const char *file, int line, const char *function, const char *fmt,...)
Used for sending a log message This is the standard logger function. Probably the only way you will i...
Definition: logger.c:1207
int ast_destroy_timing(struct ast_timing *i)
Deallocates memory structures associated with a timing bitmap.
Definition: pbx.c:8396
int ast_build_timing(struct ast_timing *i, const char *info)
Construct a timing bitmap, for use in time-based conditionals.
Definition: pbx.c:8314
void ast_copy_string(char *dst, const char *src, size_t size)
Size-limited null-terminating string copy.
Definition: strings.h:223
#define S_OR(a, b)
returns the equivalent of logic or for strings: first one if not empty, otherwise second one...
Definition: strings.h:77
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 238 of file func_logic.c.

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(), and pbx_substitute_variables_helper().

Referenced by import_read(), and import_read2().

239 {
241  AST_APP_ARG(channel);
242  AST_APP_ARG(varname);
243  );
245  if (buf) {
246  *buf = '\0';
247  }
248 
249  if (!ast_strlen_zero(args.varname)) {
250  struct ast_channel *chan2;
251 
252  if ((chan2 = ast_channel_get_by_name(args.channel))) {
253  char *s = ast_alloca(strlen(args.varname) + 4);
254  sprintf(s, "${%s}", args.varname);
255  ast_channel_lock(chan2);
256  if (buf) {
257  pbx_substitute_variables_helper(chan2, s, buf, len);
258  } else {
259  ast_str_substitute_variables(str, len, chan2, s);
260  }
261  ast_channel_unlock(chan2);
262  chan2 = ast_channel_unref(chan2);
263  }
264  }
265 
266  return 0;
267 }
void pbx_substitute_variables_helper(struct ast_channel *c, const char *cp1, char *cp2, int count)
Definition: pbx.c:4676
#define ast_channel_lock(chan)
Definition: channel.h:2466
Main Channel structure associated with a channel.
Definition: channel.h:742
#define ast_alloca(size)
call __builtin_alloca to ensure we get gcc builtin semantics
Definition: utils.h:653
#define ast_channel_unref(c)
Decrease channel reference count.
Definition: channel.h:2502
#define AST_DECLARE_APP_ARGS(name, arglist)
Declare a structure to hold an application&#39;s arguments.
Definition: app.h:572
void ast_str_substitute_variables(struct ast_str **buf, ssize_t maxlen, struct ast_channel *chan, const char *templ)
Definition: pbx.c:4468
static force_inline int attribute_pure ast_strlen_zero(const char *s)
Definition: strings.h:63
static struct @350 args
static int len(struct ast_channel *chan, const char *cmd, char *data, char *buf, size_t buflen)
#define ast_channel_unlock(chan)
Definition: channel.h:2467
#define AST_APP_ARG(name)
Define an application argument.
Definition: app.h:555
#define AST_STANDARD_APP_ARGS(args, parse)
Performs the &#39;standard&#39; argument separation process for an application.
Definition: app.h:604
struct ast_channel * ast_channel_get_by_name(const char *name)
Find a channel by name.
Definition: channel.c:1803
static int import_read ( struct ast_channel chan,
const char *  cmd,
char *  data,
char *  buf,
size_t  len 
)
static

Definition at line 269 of file func_logic.c.

References import_helper().

270 {
271  return import_helper(chan, cmd, data, buf, NULL, len);
272 }
const char * data
Definition: channel.h:755
static int len(struct ast_channel *chan, const char *cmd, char *data, char *buf, size_t buflen)
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:238
static int import_read2 ( struct ast_channel chan,
const char *  cmd,
char *  data,
struct ast_str **  str,
ssize_t  len 
)
static

Definition at line 274 of file func_logic.c.

References import_helper().

275 {
276  return import_helper(chan, cmd, data, NULL, str, len);
277 }
const char * data
Definition: channel.h:755
static int len(struct ast_channel *chan, const char *cmd, char *data, char *buf, size_t buflen)
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:238
static int isnull ( struct ast_channel chan,
const char *  cmd,
char *  data,
char *  buf,
size_t  len 
)
static

Definition at line 118 of file func_logic.c.

120 {
121  strcpy(buf, data && *data ? "0" : "1");
122 
123  return 0;
124 }
const char * data
Definition: channel.h:755
static int load_module ( void  )
static

Definition at line 327 of file func_logic.c.

References ast_custom_function_register.

328 {
329  int res = 0;
330 
337 
338  return res;
339 }
static struct ast_custom_function set_function
Definition: func_logic.c:285
static struct ast_custom_function isnull_function
Definition: func_logic.c:279
static struct ast_custom_function exists_function
Definition: func_logic.c:291
static struct ast_custom_function if_time_function
Definition: func_logic.c:302
static struct ast_custom_function import_function
Definition: func_logic.c:307
static struct ast_custom_function if_function
Definition: func_logic.c:297
#define ast_custom_function_register(acf)
Register a custom function.
Definition: pbx.h:1164
static int set ( struct ast_channel chan,
const char *  cmd,
char *  data,
char *  buf,
size_t  len 
)
static

Definition at line 208 of file func_logic.c.

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

Referenced by set2().

210 {
211  char *varname;
212  char *val;
213 
214  varname = strsep(&data, "=");
215  val = data;
216 
217  if (ast_strlen_zero(varname) || !val) {
218  ast_log(LOG_WARNING, "Syntax SET(<varname>=[<value>])\n");
219  return -1;
220  }
221 
222  varname = ast_strip(varname);
223  val = ast_strip(val);
224  pbx_builtin_setvar_helper(chan, varname, val);
225  ast_copy_string(buf, val, len);
226 
227  return 0;
228 }
char * strsep(char **str, const char *delims)
Definition: ast_expr2.c:325
#define LOG_WARNING
Definition: logger.h:144
const char * data
Definition: channel.h:755
static force_inline int attribute_pure ast_strlen_zero(const char *s)
Definition: strings.h:63
char * ast_strip(char *s)
Strip leading/trailing whitespace from a string.
Definition: strings.h:155
static int len(struct ast_channel *chan, const char *cmd, char *data, char *buf, size_t buflen)
void ast_log(int level, const char *file, int line, const char *function, const char *fmt,...)
Used for sending a log message This is the standard logger function. Probably the only way you will i...
Definition: logger.c:1207
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...
Definition: pbx.c:10546
void ast_copy_string(char *dst, const char *src, size_t size)
Size-limited null-terminating string copy.
Definition: strings.h:223
static int set2 ( struct ast_channel chan,
const char *  cmd,
char *  data,
struct ast_str **  str,
ssize_t  len 
)
static

Definition at line 230 of file func_logic.c.

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

231 {
232  if (len > -1) {
233  ast_str_make_space(str, len == 0 ? strlen(data) : len);
234  }
235  return set(chan, cmd, data, ast_str_buffer(*str), ast_str_size(*str));
236 }
size_t ast_str_size(const struct ast_str *buf)
Returns the current maximum length (without reallocation) of the current buffer.
Definition: strings.h:482
char * ast_str_buffer(const struct ast_str *buf)
Returns the string buffer within the ast_str buf.
Definition: strings.h:497
const char * data
Definition: channel.h:755
int ast_str_make_space(struct ast_str **buf, size_t new_len)
Definition: strings.h:588
static int len(struct ast_channel *chan, const char *cmd, char *data, char *buf, size_t buflen)
static int set(struct ast_channel *chan, const char *cmd, char *data, char *buf, size_t len)
Definition: func_logic.c:208
static int unload_module ( void  )
static

Definition at line 313 of file func_logic.c.

References ast_custom_function_unregister().

314 {
315  int res = 0;
316 
323 
324  return res;
325 }
static struct ast_custom_function set_function
Definition: func_logic.c:285
int ast_custom_function_unregister(struct ast_custom_function *acf)
Unregister a custom function.
Definition: pbx.c:3814
static struct ast_custom_function isnull_function
Definition: func_logic.c:279
static struct ast_custom_function exists_function
Definition: func_logic.c:291
static struct ast_custom_function if_time_function
Definition: func_logic.c:302
static struct ast_custom_function import_function
Definition: func_logic.c:307
static struct ast_custom_function if_function
Definition: func_logic.c:297

Variable Documentation

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 = "ac1f6a56484a8820659555499174e588" , .load = load_module, .unload = unload_module, .load_pri = AST_MODPRI_DEFAULT, }
static

Definition at line 341 of file func_logic.c.

Definition at line 341 of file func_logic.c.

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:126

Definition at line 291 of file func_logic.c.

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:170

Definition at line 297 of file func_logic.c.

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:134

Definition at line 302 of file func_logic.c.

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:274
static int import_read(struct ast_channel *chan, const char *cmd, char *data, char *buf, size_t len)
Definition: func_logic.c:269

Definition at line 307 of file func_logic.c.

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:118

Definition at line 279 of file func_logic.c.

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:230
static int set(struct ast_channel *chan, const char *cmd, char *data, char *buf, size_t len)
Definition: func_logic.c:208

Definition at line 285 of file func_logic.c.