Asterisk - The Open Source Telephony Project  21.4.1
Macros | Functions | Variables
func_strings.c File Reference

String manipulation dialplan functions. More...

#include "asterisk.h"
#include <regex.h>
#include <ctype.h>
#include "asterisk/module.h"
#include "asterisk/channel.h"
#include "asterisk/pbx.h"
#include "asterisk/utils.h"
#include "asterisk/app.h"
#include "asterisk/localtime.h"
#include "asterisk/test.h"

Go to the source code of this file.

Macros

#define beginning   (cmd[0] == 'S') /* SHIFT */
 
#define beginning   (cmd[0] == 'U') /* UNSHIFT */
 
#define HASH_FORMAT   HASH_PREFIX "%s~"
 
#define HASH_PREFIX   "~HASH~%s~"
 
#define ltrim(s)   while (isspace(*s)) s++;
 
#define rtrim(s)
 

Functions

static void __init_result_buf (void)
 
static void __init_tmp_buf (void)
 
static void __reg_module (void)
 
static void __unreg_module (void)
 
static int acf_strftime (struct ast_channel *chan, const char *cmd, char *parse, char *buf, size_t buflen)
 
static int acf_strptime (struct ast_channel *chan, const char *cmd, char *data, char *buf, size_t buflen)
 
static int array (struct ast_channel *chan, const char *cmd, char *var, const char *value)
 
struct ast_moduleAST_MODULE_SELF_SYM (void)
 
static void clearvar_prefix (struct ast_channel *chan, const char *prefix)
 
static int csv_quote (struct ast_channel *chan, const char *cmd, char *data, char *buf, size_t len)
 
static int exec_clearhash (struct ast_channel *chan, const char *data)
 
static int filter (struct ast_channel *chan, const char *cmd, char *parse, char *buf, size_t len)
 
static int function_eval (struct ast_channel *chan, const char *cmd, char *data, char *buf, size_t buflen)
 
static int function_eval2 (struct ast_channel *chan, const char *cmd, char *data, struct ast_str **buf, ssize_t buflen)
 
static int function_fieldnum (struct ast_channel *chan, const char *cmd, char *parse, char *buf, size_t len)
 
static int function_fieldnum_helper (struct ast_channel *chan, const char *cmd, char *parse, char *buf, struct ast_str **sbuf, ssize_t len)
 
static int function_fieldnum_str (struct ast_channel *chan, const char *cmd, char *parse, struct ast_str **buf, ssize_t len)
 
static int function_fieldqty (struct ast_channel *chan, const char *cmd, char *parse, char *buf, size_t len)
 
static int function_fieldqty_helper (struct ast_channel *chan, const char *cmd, char *parse, char *buf, struct ast_str **sbuf, ssize_t len)
 
static int function_fieldqty_str (struct ast_channel *chan, const char *cmd, char *parse, struct ast_str **buf, ssize_t len)
 
static int function_ltrim (struct ast_channel *chan, const char *cmd, char *data, char *buf, size_t len)
 
static int function_rtrim (struct ast_channel *chan, const char *cmd, char *data, char *buf, size_t len)
 
static int function_trim (struct ast_channel *chan, const char *cmd, char *data, char *buf, size_t len)
 
static const char * get_key (const struct ast_str *prefix, const struct ast_var_t *var)
 
static int hash_read (struct ast_channel *chan, const char *cmd, char *data, char *buf, size_t len)
 
static int hash_write (struct ast_channel *chan, const char *cmd, char *var, const char *value)
 
static int hashkeys_read (struct ast_channel *chan, const char *cmd, char *data, char *buf, size_t len)
 
static int hashkeys_read2 (struct ast_channel *chan, const char *cmd, char *data, struct ast_str **buf, ssize_t len)
 
static int keypadhash (struct ast_channel *chan, const char *cmd, char *data, char *buf, size_t buflen)
 
static int len (struct ast_channel *chan, const char *cmd, char *data, char *buf, size_t buflen)
 
static int listfilter (struct ast_channel *chan, const char *cmd, char *parse, char *buf, struct ast_str **bufstr, ssize_t len)
 
static int listfilter_read (struct ast_channel *chan, const char *cmd, char *parse, char *buf, size_t len)
 
static int listfilter_read2 (struct ast_channel *chan, const char *cmd, char *parse, struct ast_str **buf, ssize_t len)
 
static int load_module (void)
 
static int passthru (struct ast_channel *chan, const char *cmd, char *data, struct ast_str **buf, ssize_t len)
 
static int quote (struct ast_channel *chan, const char *cmd, char *data, char *buf, size_t len)
 
static int regex (struct ast_channel *chan, const char *cmd, char *parse, char *buf, size_t len)
 
static int replace (struct ast_channel *chan, const char *cmd, char *data, struct ast_str **buf, ssize_t len)
 
static int shift_pop (struct ast_channel *chan, const char *cmd, char *data, struct ast_str **buf, ssize_t len)
 
static int strbetween (struct ast_channel *chan, const char *cmd, char *data, struct ast_str **buf, ssize_t len)
 
static int string_tolower (struct ast_channel *chan, const char *cmd, char *data, char *buf, size_t buflen)
 
static int string_tolower2 (struct ast_channel *chan, const char *cmd, char *data, struct ast_str **buf, ssize_t buflen)
 
static int string_toupper (struct ast_channel *chan, const char *cmd, char *data, char *buf, size_t buflen)
 
static int string_toupper2 (struct ast_channel *chan, const char *cmd, char *data, struct ast_str **buf, ssize_t buflen)
 
static int strreplace (struct ast_channel *chan, const char *cmd, char *data, struct ast_str **buf, ssize_t len)
 
static int unload_module (void)
 
static int unshift_push (struct ast_channel *chan, const char *cmd, char *data, const char *new_value)
 

Variables

static struct ast_module_info __mod_info = { .name = AST_MODULE, .flags = AST_MODFLAG_LOAD_ORDER , .description = "String handling 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 = "da6642af068ee5e6490c5b1d2cc1d238" , .load = load_module, .unload = unload_module, .load_pri = AST_MODPRI_DEFAULT, .support_level = AST_MODULE_SUPPORT_CORE, }
 
static char * app_clearhash = "ClearHash"
 
static struct ast_custom_function array_function
 
static const struct ast_module_infoast_module_info = &__mod_info
 
static struct ast_custom_function csv_quote_function
 
static struct ast_custom_function eval_function
 
static struct ast_custom_function fieldnum_function
 
static struct ast_custom_function fieldqty_function
 
static struct ast_custom_function filter_function
 
static struct ast_custom_function hash_function
 
static struct ast_custom_function hashkeys_function
 
static struct ast_custom_function keypadhash_function
 
static struct ast_custom_function len_function
 
static struct ast_custom_function listfilter_function
 
static struct ast_custom_function ltrim_function
 
static struct ast_custom_function passthru_function
 
static struct ast_custom_function pop_function
 
static struct ast_custom_function push_function
 
static struct ast_custom_function quote_function
 
static struct ast_custom_function regex_function
 
static struct ast_custom_function replace_function
 
static struct ast_threadstorage result_buf = { .once = PTHREAD_ONCE_INIT , .key_init = __init_result_buf , .custom_init = NULL , }
 
static struct ast_custom_function rtrim_function
 
static struct ast_custom_function shift_function
 
static struct ast_custom_function strbetween_function
 
static struct ast_custom_function strftime_function
 
static struct ast_custom_function strptime_function
 
static struct ast_custom_function strreplace_function
 
static struct ast_threadstorage tmp_buf = { .once = PTHREAD_ONCE_INIT , .key_init = __init_tmp_buf , .custom_init = NULL , }
 
static struct ast_custom_function tolower_function
 
static struct ast_custom_function toupper_function
 
static struct ast_custom_function trim_function
 
static struct ast_custom_function unshift_function
 

Detailed Description

String manipulation dialplan functions.

Author
Tilghman Lesher
Anthony Minessale II
Naveen Albert

Definition in file func_strings.c.

Function Documentation

static int filter ( struct ast_channel chan,
const char *  cmd,
char *  parse,
char *  buf,
size_t  len 
)
static
Note
Looks a little strange, until you realize that we can overflow the size of a char.

Definition at line 807 of file func_strings.c.

References AST_APP_ARG, ast_debug, AST_DECLARE_APP_ARGS, and ast_get_encoded_char().

Referenced by stasis_subscription_set_filter().

809 {
811  AST_APP_ARG(allowed);
812  AST_APP_ARG(string);
813  );
814  char *outbuf = buf;
815  unsigned char ac;
816  char allowed[256] = "";
817  size_t allowedlen = 0;
818  int32_t bitfield[8] = { 0, }; /* 256 bits */
819 
820  AST_STANDARD_RAW_ARGS(args, parse);
821 
822  if (!args.string) {
823  ast_log(LOG_ERROR, "Usage: FILTER(<allowed-chars>,<string>)\n");
824  return -1;
825  }
826 
827  if (args.allowed[0] == '"' && !ast_opt_dont_warn) {
828  ast_log(LOG_WARNING, "FILTER allowed characters includes the quote (\") character. This may not be what you want.\n");
829  }
830 
831  /* Expand ranges */
832  for (; *(args.allowed);) {
833  char c1 = 0, c2 = 0;
834  size_t consumed = 0;
835 
836  if (ast_get_encoded_char(args.allowed, &c1, &consumed))
837  return -1;
838  args.allowed += consumed;
839 
840  if (*(args.allowed) == '-') {
841  if (ast_get_encoded_char(args.allowed + 1, &c2, &consumed))
842  c2 = c1;
843  args.allowed += consumed + 1;
844 
845  if ((unsigned char) c2 < (unsigned char) c1 && !ast_opt_dont_warn) {
846  ast_log(LOG_WARNING, "Range wrapping in FILTER(%s,%s). This may not be what you want.\n", parse, args.string);
847  }
848 
849  /*!\note
850  * Looks a little strange, until you realize that we can overflow
851  * the size of a char.
852  */
853  for (ac = (unsigned char) c1; ac != (unsigned char) c2; ac++) {
854  bitfield[ac / 32] |= 1 << (ac % 32);
855  }
856  bitfield[ac / 32] |= 1 << (ac % 32);
857 
858  ast_debug(4, "c1=%d, c2=%d\n", c1, c2);
859  } else {
860  ac = (unsigned char) c1;
861  ast_debug(4, "c1=%d, consumed=%d, args.allowed=%s\n", c1, (int) consumed, args.allowed - consumed);
862  bitfield[ac / 32] |= 1 << (ac % 32);
863  }
864  }
865 
866  for (ac = 1; ac != 0; ac++) {
867  if (bitfield[ac / 32] & (1 << (ac % 32))) {
868  allowed[allowedlen++] = ac;
869  }
870  }
871 
872  ast_debug(1, "Allowed: %s\n", allowed);
873 
874  for (; *(args.string) && (buf + len - 1 > outbuf); (args.string)++) {
875  if (strchr(allowed, *(args.string)))
876  *outbuf++ = *(args.string);
877  }
878  *outbuf = '\0';
879 
880  return 0;
881 }
#define ast_debug(level,...)
Log a DEBUG message.
int ast_get_encoded_char(const char *stream, char *result, size_t *consumed)
Decode an encoded control or extended ASCII character.
Definition: main/app.c:3087
#define AST_DECLARE_APP_ARGS(name, arglist)
Declare a structure to hold an application's arguments.
#define AST_APP_ARG(name)
Define an application argument.

Variable Documentation

struct ast_custom_function array_function
static
Initial value:
= {
.name = "ARRAY",
.write = array,
}

Definition at line 1488 of file func_strings.c.

struct ast_custom_function csv_quote_function
static
Initial value:
= {
.name = "CSV_QUOTE",
.read = csv_quote,
}

Definition at line 1562 of file func_strings.c.

struct ast_custom_function eval_function
static
Initial value:
= {
.name = "EVAL",
.read = function_eval,
.read2 = function_eval2,
}

Definition at line 1687 of file func_strings.c.

struct ast_custom_function fieldnum_function
static
Initial value:
= {
.name = "FIELDNUM",
.read = function_fieldnum,
.read2 = function_fieldnum_str,
}

Definition at line 674 of file func_strings.c.

struct ast_custom_function fieldqty_function
static
Initial value:
= {
.name = "FIELDQTY",
.read = function_fieldqty,
.read2 = function_fieldqty_str,
}

Definition at line 595 of file func_strings.c.

struct ast_custom_function filter_function
static
Initial value:
= {
.name = "FILTER",
.read = filter,
}
static int filter(struct ast_channel *chan, const char *cmd, char *parse, char *buf, size_t len)
Definition: func_strings.c:807

Definition at line 883 of file func_strings.c.

struct ast_custom_function hash_function
static
Initial value:
= {
.name = "HASH",
.write = hash_write,
.read = hash_read,
}

Definition at line 1476 of file func_strings.c.

struct ast_custom_function hashkeys_function
static
Initial value:
= {
.name = "HASHKEYS",
.read = hashkeys_read,
.read2 = hashkeys_read2,
}

Definition at line 1482 of file func_strings.c.

struct ast_custom_function keypadhash_function
static
Initial value:
= {
.name = "KEYPADHASH",
.read = keypadhash,
}

Definition at line 1728 of file func_strings.c.

struct ast_custom_function len_function
static
Initial value:
= {
.name = "LEN",
.read = len,
.read_max = 12,
}

Definition at line 1579 of file func_strings.c.

struct ast_custom_function listfilter_function
static
Initial value:
= {
.name = "LISTFILTER",
.read = listfilter_read,
.read2 = listfilter_read2,
}

Definition at line 801 of file func_strings.c.

struct ast_custom_function ltrim_function
static
Initial value:
= {
.name = "LTRIM",
.read = function_ltrim,
}

Definition at line 1161 of file func_strings.c.

struct ast_custom_function passthru_function
static
Initial value:
= {
.name = "PASSTHRU",
.read2 = passthru,
}

Definition at line 1921 of file func_strings.c.

struct ast_custom_function pop_function
static
Initial value:
= {
.name = "POP",
.read2 = shift_pop,
}

Definition at line 1846 of file func_strings.c.

struct ast_custom_function push_function
static
Initial value:
= {
.name = "PUSH",
.write = unshift_push,
}

Definition at line 1905 of file func_strings.c.

struct ast_custom_function quote_function
static
Initial value:
= {
.name = "QUOTE",
.read = quote,
}

Definition at line 1527 of file func_strings.c.

struct ast_custom_function regex_function
static
Initial value:
= {
.name = "REGEX",
.read = regex,
}

Definition at line 1208 of file func_strings.c.

struct ast_custom_function replace_function
static
Initial value:
= {
.name = "REPLACE",
.read2 = replace,
}

Definition at line 952 of file func_strings.c.

struct ast_custom_function rtrim_function
static
Initial value:
= {
.name = "RTRIM",
.read = function_rtrim,
}

Definition at line 1166 of file func_strings.c.

struct ast_custom_function shift_function
static
Initial value:
= {
.name = "SHIFT",
.read2 = shift_pop,
}

Definition at line 1841 of file func_strings.c.

struct ast_custom_function strbetween_function
static
Initial value:
= {
.name = "STRBETWEEN",
.read2 = strbetween,
}

Definition at line 1088 of file func_strings.c.

struct ast_custom_function strftime_function
static
Initial value:
= {
.name = "STRFTIME",
.read = acf_strftime,
}

Definition at line 1614 of file func_strings.c.

struct ast_custom_function strptime_function
static
Initial value:
= {
.name = "STRPTIME",
.read = acf_strptime,
}

Definition at line 1656 of file func_strings.c.

struct ast_custom_function strreplace_function
static
Initial value:
= {
.name = "STRREPLACE",
.read2 = strreplace,
}

Definition at line 1041 of file func_strings.c.

struct ast_custom_function tolower_function
static
Initial value:
= {
.name = "TOLOWER",
.read = string_tolower,
.read2 = string_tolower2,
}

Definition at line 1785 of file func_strings.c.

struct ast_custom_function toupper_function
static
Initial value:
= {
.name = "TOUPPER",
.read = string_toupper,
.read2 = string_toupper2,
}

Definition at line 1756 of file func_strings.c.

struct ast_custom_function trim_function
static
Initial value:
= {
.name = "TRIM",
.read = function_trim,
}

Definition at line 1156 of file func_strings.c.

struct ast_custom_function unshift_function
static
Initial value:
= {
.name = "UNSHIFT",
.write = unshift_push,
}

Definition at line 1910 of file func_strings.c.