Merge "stasis: Allow filtering by formatter"
[asterisk/asterisk.git] / funcs / func_strings.c
index fa0174c..7afc40e 100644 (file)
  * \brief String manipulation dialplan functions
  *
  * \author Tilghman Lesher
- * \author Anothony Minessale II 
+ * \author Anothony Minessale II
  * \ingroup functions
  */
 
-#include "asterisk.h"
+/*** MODULEINFO
+       <support_level>core</support_level>
+ ***/
 
-ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
+#include "asterisk.h"
 
 #include <regex.h>
 #include <ctype.h>
@@ -61,6 +63,27 @@ AST_THREADSTORAGE(tmp_buf);
                        <para>Example: If ${example} contains <literal>ex-amp-le</literal>, then ${FIELDQTY(example,-)} returns 3.</para>
                </description>
        </function>
+       <function name="FIELDNUM" language="en_US">
+               <synopsis>
+                       Return the 1-based offset of a field in a list
+               </synopsis>
+               <syntax>
+                       <parameter name="varname" required="true" />
+                       <parameter name="delim" required="true" />
+                       <parameter name="value" required="true" />
+               </syntax>
+               <description>
+                       <para>Search the variable named <replaceable>varname</replaceable> for the string <replaceable>value</replaceable>
+                       delimited by <replaceable>delim</replaceable> and return a 1-based offset as to its location. If not found
+                       or an error occured, return <literal>0</literal>.</para>
+                       <para>The delimiter may be specified as a special or extended ASCII character, by encoding it.  The characters
+                       <literal>\n</literal>, <literal>\r</literal>, and <literal>\t</literal> are all recognized as the newline,
+                       carriage return, and tab characters, respectively.  Also, octal and hexadecimal specifications are recognized
+                       by the patterns <literal>\0nnn</literal> and <literal>\xHH</literal>, respectively.  For example, if you wanted
+                       to encode a comma as the delimiter, you could use either <literal>\054</literal> or <literal>\x2C</literal>.</para>
+                       <para>Example: If ${example} contains <literal>ex-amp-le</literal>, then ${FIELDNUM(example,-,amp)} returns 2.</para>
+               </description>
+       </function>
        <function name="LISTFILTER" language="en_US">
                <synopsis>Remove an item from a list, by name.</synopsis>
                <syntax>
@@ -83,13 +106,13 @@ AST_THREADSTORAGE(tmp_buf);
                        <parameter name="string" required="true" />
                </syntax>
                <description>
-                       <para>Permits all characters listed in <replaceable>allowed-chars</replaceable>, 
-                       filtering all others outs. In addition to literally listing the characters, 
+                       <para>Permits all characters listed in <replaceable>allowed-chars</replaceable>,
+                       filtering all others outs. In addition to literally listing the characters,
                        you may also use ranges of characters (delimited by a <literal>-</literal></para>
                        <para>Hexadecimal characters started with a <literal>\x</literal>(i.e. \x20)</para>
                        <para>Octal characters started with a <literal>\0</literal> (i.e. \040)</para>
-                       <para>Also <literal>\t</literal>,<literal>\n</literal> and <literal>\r</literal> are recognized.</para> 
-                       <note><para>If you want the <literal>-</literal> character it needs to be prefixed with a 
+                       <para>Also <literal>\t</literal>,<literal>\n</literal> and <literal>\r</literal> are recognized.</para>
+                       <note><para>If you want the <literal>-</literal> character it needs to be prefixed with a
                        <literal>\</literal></para></note>
                </description>
        </function>
@@ -111,6 +134,24 @@ AST_THREADSTORAGE(tmp_buf);
                        altered.</para></note>
                </description>
        </function>
+       <function name="STRREPLACE" language="en_US">
+               <synopsis>
+                       Replace instances of a substring within a string with another string.
+               </synopsis>
+               <syntax>
+                       <parameter name="varname" required="true" />
+                       <parameter name="find-string" required="true" />
+                       <parameter name="replace-string" required="false" />
+                       <parameter name="max-replacements" required="false" />
+               </syntax>
+               <description>
+                       <para>Searches for all instances of the <replaceable>find-string</replaceable> in provided variable and
+                       replaces them with <replaceable>replace-string</replaceable>.  If <replaceable>replace-string</replaceable>
+                       is an empty string, this will effecively delete that substring.  If <replaceable>max-replacements</replaceable>
+                       is specified, this function will stop after performing replacements <replaceable>max-replacements</replaceable> times.</para>
+                       <note><para>The replacement only occurs in the output.  The original variable is not altered.</para></note>
+               </description>
+       </function>
        <function name="PASSTHRU" language="en_US">
                <synopsis>
                        Pass the given argument back as a value.
@@ -122,6 +163,10 @@ AST_THREADSTORAGE(tmp_buf);
                        <para>Literally returns the given <replaceable>string</replaceable>.  The intent is to permit
                        other dialplan functions which take a variable name as an argument to be able to take a literal
                        string, instead.</para>
+                       <note><para>The functions which take a variable name need to be passed var and not
+                       ${var}.  Similarly, use PASSTHRU() and not ${PASSTHRU()}.</para></note>
+                       <para>Example: ${CHANNEL} contains SIP/321-1</para>
+                       <para>         ${CUT(PASSTHRU(${CUT(CHANNEL,-,1)}),/,2)}) will return 321</para>
                </description>
        </function>
        <function name="REGEX" language="en_US">
@@ -134,9 +179,9 @@ AST_THREADSTORAGE(tmp_buf);
                </syntax>
                <description>
                        <para>Return <literal>1</literal> on regular expression match or <literal>0</literal> otherwise</para>
-                       <para>Please note that the space following the double quotes separating the 
-                       regex from the data is optional and if present, is skipped. If a space is 
-                       desired at the beginning of the data, then put two spaces there; the second 
+                       <para>Please note that the space following the double quotes separating the
+                       regex from the data is optional and if present, is skipped. If a space is
+                       desired at the beginning of the data, then put two spaces there; the second
                        will not be skipped.</para>
                </description>
        </function>
@@ -173,8 +218,8 @@ AST_THREADSTORAGE(tmp_buf);
                        <parameter name="hashname" required="true" />
                </syntax>
                <description>
-                       <para>Returns a comma-delimited list of the current keys of the associative array 
-                       defined by the HASH() function. Note that if you iterate over the keys of 
+                       <para>Returns a comma-delimited list of the current keys of the associative array
+                       defined by the HASH() function. Note that if you iterate over the keys of
                        the result, adding keys during iteration will cause the result of the HASHKEYS()
                        function to change.</para>
                </description>
@@ -200,8 +245,8 @@ AST_THREADSTORAGE(tmp_buf);
                        <parameter name="varN" required="false" />
                </syntax>
                <description>
-                       <para>The comma-delimited list passed as a value to which the function is set will 
-                       be interpreted as a set of values to which the comma-delimited list of 
+                       <para>The comma-delimited list passed as a value to which the function is set will
+                       be interpreted as a set of values to which the comma-delimited list of
                        variable names in the argument should be set.</para>
                        <para>Example: Set(ARRAY(var1,var2)=1,2) will set var1 to 1 and var2 to 2</para>
                </description>
@@ -216,7 +261,7 @@ AST_THREADSTORAGE(tmp_buf);
                        <parameter name="format" required="true" />
                </syntax>
                <description>
-                       <para>This is useful for converting a date into <literal>EPOCH</literal> time, 
+                       <para>This is useful for converting a date into <literal>EPOCH</literal> time,
                        possibly to pass to an application like SayUnixTime or to calculate the difference
                        between the two date strings</para>
                        <para>Example: ${STRPTIME(2006-03-01 07:30:35,America/Chicago,%Y-%m-%d %H:%M:%S)} returns 1141219835</para>
@@ -306,7 +351,7 @@ AST_THREADSTORAGE(tmp_buf);
                        <parameter name="string" required="true" />
                </syntax>
                <description>
-                       <para>Example: ${QUOTE(ab"c"de)} will return "abcde"</para>
+                       <para>Example: ${QUOTE(ab"c"de)} will return ""ab\"c\"de""</para>
                </description>
        </function>
        <function name="CSV_QUOTE" language="en_US">
@@ -339,7 +384,7 @@ AST_THREADSTORAGE(tmp_buf);
                                NoOp(var is three) being executed.
                        </para>
                </description>
-       </function>     
+       </function>
        <function name="POP" language="en_US">
                <synopsis>
                        Removes and returns the last item off of a variable containing delimited text
@@ -359,7 +404,7 @@ AST_THREADSTORAGE(tmp_buf);
                                NoOp(var is one) being executed.
                        </para>
                </description>
-       </function>     
+       </function>
        <function name="PUSH" language="en_US">
                <synopsis>
                        Appends one or more values to the end of a variable containing delimited text
@@ -413,7 +458,7 @@ static int function_fieldqty_helper(struct ast_channel *chan, const char *cmd,
        if (args.delim) {
                ast_get_encoded_char(args.delim, delim, &delim_used);
 
-               varsubst = alloca(strlen(args.varname) + 4);
+               varsubst = ast_alloca(strlen(args.varname) + 4);
 
                sprintf(varsubst, "${%s}", args.varname);
                ast_str_substitute_variables(&str, 0, chan, varsubst);
@@ -455,6 +500,85 @@ static struct ast_custom_function fieldqty_function = {
        .read2 = function_fieldqty_str,
 };
 
+static int function_fieldnum_helper(struct ast_channel *chan, const char *cmd,
+                               char *parse, char *buf, struct ast_str **sbuf, ssize_t len)
+{
+       char *varsubst, *field;
+       struct ast_str *str = ast_str_thread_get(&result_buf, 16);
+       int fieldindex = 0, res = 0;
+       AST_DECLARE_APP_ARGS(args,
+               AST_APP_ARG(varname);
+               AST_APP_ARG(delim);
+               AST_APP_ARG(field);
+       );
+       char delim[2] = "";
+       size_t delim_used;
+
+       if (!str) {
+               return -1;
+       }
+
+       AST_STANDARD_APP_ARGS(args, parse);
+
+       if (args.argc < 3) {
+               ast_log(LOG_ERROR, "Usage: FIELDNUM(<listname>,<delimiter>,<fieldvalue>)\n");
+               res = -1;
+       } else {
+               varsubst = ast_alloca(strlen(args.varname) + 4);
+               sprintf(varsubst, "${%s}", args.varname);
+
+               ast_str_substitute_variables(&str, 0, chan, varsubst);
+
+               if (ast_str_strlen(str) == 0 || ast_strlen_zero(args.delim)) {
+                       fieldindex = 0;
+               } else if (ast_get_encoded_char(args.delim, delim, &delim_used) == -1) {
+                       res = -1;
+               } else {
+                       char *varval = ast_str_buffer(str);
+
+                       while ((field = strsep(&varval, delim)) != NULL) {
+                               fieldindex++;
+
+                               if (!strcasecmp(field, args.field)) {
+                                       break;
+                               }
+                       }
+
+                       if (!field) {
+                               fieldindex = 0;
+                       }
+
+                       res = 0;
+               }
+       }
+
+       if (sbuf) {
+               ast_str_set(sbuf, len, "%d", fieldindex);
+       } else {
+               snprintf(buf, len, "%d", fieldindex);
+       }
+
+       return res;
+}
+
+static int function_fieldnum(struct ast_channel *chan, const char *cmd,
+                            char *parse, char *buf, size_t len)
+{
+       return function_fieldnum_helper(chan, cmd, parse, buf, NULL, len);
+}
+
+static int function_fieldnum_str(struct ast_channel *chan, const char *cmd,
+                                char *parse, struct ast_str **buf, ssize_t len)
+{
+       return function_fieldnum_helper(chan, cmd, parse, NULL, buf, len);
+}
+
+static struct ast_custom_function fieldnum_function = {
+       .name = "FIELDNUM",
+       .read = function_fieldnum,
+       .read2 = function_fieldnum_str,
+};
+
 static int listfilter(struct ast_channel *chan, const char *cmd, char *parse, char *buf, struct ast_str **bufstr, ssize_t len)
 {
        AST_DECLARE_APP_ARGS(args,
@@ -462,7 +586,6 @@ static int listfilter(struct ast_channel *chan, const char *cmd, char *parse, ch
                AST_APP_ARG(delimiter);
                AST_APP_ARG(fieldvalue);
        );
-       const char *ptr;
        struct ast_str *orig_list = ast_str_thread_get(&tmp_buf, 16);
        const char *begin, *cur, *next;
        int dlen, flen, first = 1;
@@ -485,7 +608,7 @@ static int listfilter(struct ast_channel *chan, const char *cmd, char *parse, ch
                return -1;
        }
 
-       varsubst = alloca(strlen(args.listname) + 4);
+       varsubst = ast_alloca(strlen(args.listname) + 4);
        sprintf(varsubst, "${%s}", args.listname);
 
        /* If we don't lock the channel, the variable could disappear out from underneath us. */
@@ -494,7 +617,6 @@ static int listfilter(struct ast_channel *chan, const char *cmd, char *parse, ch
        }
        ast_str_substitute_variables(&orig_list, 0, chan, varsubst);
        if (!ast_str_strlen(orig_list)) {
-               ast_log(LOG_ERROR, "List variable '%s' not found\n", args.listname);
                if (chan) {
                        ast_channel_unlock(chan);
                }
@@ -502,7 +624,7 @@ static int listfilter(struct ast_channel *chan, const char *cmd, char *parse, ch
        }
 
        /* If the string isn't there, just copy out the string and be done with it. */
-       if (!(ptr = strstr(ast_str_buffer(orig_list), args.fieldvalue))) {
+       if (!strstr(ast_str_buffer(orig_list), args.fieldvalue)) {
                if (buf) {
                        ast_copy_string(buf, ast_str_buffer(orig_list), len);
                } else {
@@ -515,7 +637,7 @@ static int listfilter(struct ast_channel *chan, const char *cmd, char *parse, ch
        }
 
        dlen = strlen(args.delimiter);
-       delim = alloca(dlen + 1);
+       delim = ast_alloca(dlen + 1);
        ast_get_encoded_str(args.delimiter, delim, dlen + 1);
 
        if ((dlen = strlen(delim)) == 0) {
@@ -525,7 +647,7 @@ static int listfilter(struct ast_channel *chan, const char *cmd, char *parse, ch
 
        flen = strlen(args.fieldvalue);
 
-       ast_str_reset(result);
+       ast_str_reset(*result_ptr);
        /* Enough space for any result */
        if (len > -1) {
                ast_str_make_space(result_ptr, len ? len : ast_str_strlen(orig_list) + 1);
@@ -619,10 +741,10 @@ static int filter(struct ast_channel *chan, const char *cmd, char *parse, char *
 
                if (*(args.allowed) == '-') {
                        if (ast_get_encoded_char(args.allowed + 1, &c2, &consumed))
-                               c2 = -1;
+                               c2 = c1;
                        args.allowed += consumed + 1;
 
-                       if ((c2 < c1 || c2 == -1) && !ast_opt_dont_warn) {
+                       if ((unsigned char) c2 < (unsigned char) c1 && !ast_opt_dont_warn) {
                                ast_log(LOG_WARNING, "Range wrapping in FILTER(%s,%s).  This may not be what you want.\n", parse, args.string);
                        }
 
@@ -630,7 +752,7 @@ static int filter(struct ast_channel *chan, const char *cmd, char *parse, char *
                         * Looks a little strange, until you realize that we can overflow
                         * the size of a char.
                         */
-                       for (ac = c1; ac != c2; ac++) {
+                       for (ac = (unsigned char) c1; ac != (unsigned char) c2; ac++) {
                                bitfield[ac / 32] |= 1 << (ac % 32);
                        }
                        bitfield[ac / 32] |= 1 << (ac % 32);
@@ -673,7 +795,7 @@ static int replace(struct ast_channel *chan, const char *cmd, char *data, struct
                AST_APP_ARG(replace);
        );
        char *strptr, *varsubst;
-       struct ast_str *str = ast_str_thread_get(&result_buf, 16);
+       RAII_VAR(struct ast_str *, str, ast_str_create(16), ast_free);
        char find[256]; /* Only 256 characters possible */
        char replace[2] = "";
        size_t unused;
@@ -698,7 +820,7 @@ static int replace(struct ast_channel *chan, const char *cmd, char *data, struct
                return -1;
        }
 
-       varsubst = alloca(strlen(args.varname) + 4);
+       varsubst = ast_alloca(strlen(args.varname) + 4);
        sprintf(varsubst, "${%s}", args.varname);
        ast_str_substitute_variables(&str, 0, chan, varsubst);
 
@@ -716,8 +838,7 @@ static int replace(struct ast_channel *chan, const char *cmd, char *data, struct
                 * directly there */
                if (strchr(find, *strptr)) {
                        if (ast_strlen_zero(replace)) {
-                               /* Remove character */
-                               strcpy(strptr, strptr + 1); /* SAFE */
+                               memmove(strptr, strptr + 1, strlen(strptr + 1) + 1);
                                strptr--;
                        } else {
                                /* Replace character */
@@ -735,6 +856,95 @@ static struct ast_custom_function replace_function = {
        .read2 = replace,
 };
 
+static int strreplace(struct ast_channel *chan, const char *cmd, char *data, struct ast_str **buf, ssize_t len)
+{
+       char *varsubstr; /* substring for input var */
+       char *start; /* Starting pos of substring search. */
+       char *end; /* Ending pos of substring search. */
+       int find_size; /* length of given find-string */
+       unsigned max_matches; /* number of matches we find before terminating search */
+       unsigned count; /* loop counter */
+       struct ast_str *str = ast_str_thread_get(&result_buf, 16); /* Holds the data obtained from varname */
+
+       AST_DECLARE_APP_ARGS(args,
+               AST_APP_ARG(varname);
+               AST_APP_ARG(find_string);
+               AST_APP_ARG(replace_string);
+               AST_APP_ARG(max_replacements);
+               AST_APP_ARG(other);     /* Any remining unused arguments */
+       );
+
+       /* Guarantee output string is empty to start with. */
+       ast_str_reset(*buf);
+
+       if (!str) {
+               /* We failed to allocate str, forget it.  We failed. */
+               return -1;
+       }
+
+       /* Parse the arguments. */
+       AST_STANDARD_APP_ARGS(args, data);
+
+       if (args.argc < 2) {
+               /* Didn't receive enough arguments to do anything */
+               ast_log(LOG_ERROR,
+                       "Usage: %s(<varname>,<find-string>[,<replace-string>,[<max-replacements>]])\n",
+                       cmd);
+               return -1;
+       }
+
+       /* No var name specified. Return failure, string is already empty. */
+       if (ast_strlen_zero(args.varname)) {
+               return -1;
+       }
+
+       /* Zero length find strings are a no-no. Kill the function if we run into one. */
+       if (ast_strlen_zero(args.find_string)) {
+               ast_log(LOG_ERROR, "No <find-string> specified\n");
+               return -1;
+       }
+       find_size = strlen(args.find_string);
+
+       /* set varsubstr to the matching variable */
+       varsubstr = ast_alloca(strlen(args.varname) + 4);
+       sprintf(varsubstr, "${%s}", args.varname);
+       ast_str_substitute_variables(&str, 0, chan, varsubstr);
+
+       /* Determine how many replacements are allowed. */
+       if (!args.max_replacements
+               || (max_matches = atoi(args.max_replacements)) <= 0) {
+               /* Unlimited replacements are allowed. */
+               max_matches = -1;
+       }
+
+       /* Generate the search and replaced string. */
+       start = ast_str_buffer(str);
+       for (count = 0; count < max_matches; ++count) {
+               end = strstr(start, args.find_string);
+               if (!end) {
+                       /* Did not find a matching substring in the remainder. */
+                       break;
+               }
+
+               /* Replace the found substring. */
+               *end = '\0';
+               ast_str_append(buf, len, "%s", start);
+               if (args.replace_string) {
+                       /* Append the replacement string */
+                       ast_str_append(buf, len, "%s", args.replace_string);
+               }
+               start = end + find_size;
+       }
+       ast_str_append(buf, len, "%s", start);
+
+       return 0;
+}
+
+static struct ast_custom_function strreplace_function = {
+       .name = "STRREPLACE",
+       .read2 = strreplace,
+};
+
 static int regex(struct ast_channel *chan, const char *cmd, char *parse, char *buf,
                 size_t len)
 {
@@ -764,7 +974,7 @@ static int regex(struct ast_channel *chan, const char *cmd, char *parse, char *b
                ast_log(LOG_WARNING, "Malformed input %s(%s): %s\n", cmd, parse, buf);
                return -1;
        }
-       
+
        strcpy(buf, regexec(&regexbuf, args.str, 0, NULL, 0) ? "0" : "1");
 
        regfree(&regexbuf);
@@ -787,8 +997,8 @@ static void clearvar_prefix(struct ast_channel *chan, const char *prefix)
 {
        struct ast_var_t *var;
        int len = strlen(prefix);
-       AST_LIST_TRAVERSE_SAFE_BEGIN(&chan->varshead, var, entries) {
-               if (strncasecmp(prefix, ast_var_name(var), len) == 0) {
+       AST_LIST_TRAVERSE_SAFE_BEGIN(ast_channel_varshead(chan), var, entries) {
+               if (strncmp(prefix, ast_var_name(var), len) == 0) {
                        AST_LIST_REMOVE_CURRENT(entries);
                        ast_free(var);
                }
@@ -816,9 +1026,10 @@ static int array(struct ast_channel *chan, const char *cmd, char *var,
        char *origvar = "", *value2, varname[256];
        int i, ishash = 0;
 
-       value2 = ast_strdupa(value);
-       if (!var || !value2)
+       if (!var) {
                return -1;
+       }
+       value2 = ast_strdupa(value);
 
        if (!strcmp(cmd, "HASH")) {
                const char *var2 = pbx_builtin_getvar_helper(chan, "~ODBCFIELDS~");
@@ -839,14 +1050,14 @@ static int array(struct ast_channel *chan, const char *cmd, char *var,
         * want them to be surprised by the result.  Hence, we prefer commas as the
         * delimiter, but we'll fall back to vertical bars if commas aren't found.
         */
-       ast_debug(1, "array (%s=%s)\n", var, value2);
+       ast_debug(1, "array (%s=%s)\n", var, S_OR(value2, ""));
        AST_STANDARD_APP_ARGS(arg1, var);
 
        AST_STANDARD_APP_ARGS(arg2, value2);
 
        for (i = 0; i < arg1.argc; i++) {
                ast_debug(1, "array set value (%s=%s)\n", arg1.var[i],
-                               arg2.val[i]);
+                               S_OR(arg2.val[i], ""));
                if (i < arg2.argc) {
                        if (ishash) {
                                if (origvar[0] == '_') {
@@ -878,24 +1089,56 @@ static int array(struct ast_channel *chan, const char *cmd, char *var,
        return 0;
 }
 
+static const char *get_key(const struct ast_str *prefix, const struct ast_var_t *var)
+{
+       const char *prefix_name = ast_str_buffer(prefix);
+       const char *var_name = ast_var_name(var);
+       int prefix_len;
+       int var_len;
+
+       if (ast_strlen_zero(var_name)) {
+               return NULL;
+       }
+
+       prefix_len = ast_str_strlen(prefix);
+       var_len = strlen(var_name);
+
+       /*
+        * Make sure we only match on non-empty, hash function created keys. If valid
+        * then return a pointer to the variable that's just after the prefix.
+        */
+       return var_len > (prefix_len + 1) && var_name[var_len - 1] == '~' &&
+               strncmp(prefix_name, var_name, prefix_len) == 0 ? var_name + prefix_len : NULL;
+}
+
 static int hashkeys_read(struct ast_channel *chan, const char *cmd, char *data, char *buf, size_t len)
 {
        struct ast_var_t *newvar;
        struct ast_str *prefix = ast_str_alloca(80);
+       size_t buf_len;
+
+       if (!chan) {
+               ast_log(LOG_WARNING, "No channel was provided to %s function.\n", cmd);
+               return -1;
+       }
 
        ast_str_set(&prefix, -1, HASH_PREFIX, data);
        memset(buf, 0, len);
 
-       AST_LIST_TRAVERSE(&chan->varshead, newvar, entries) {
-               if (strncasecmp(ast_str_buffer(prefix), ast_var_name(newvar), ast_str_strlen(prefix)) == 0) {
-                       /* Copy everything after the prefix */
-                       strncat(buf, ast_var_name(newvar) + ast_str_strlen(prefix), len - strlen(buf) - 1);
-                       /* Trim the trailing ~ */
+       AST_LIST_TRAVERSE(ast_channel_varshead(chan), newvar, entries) {
+               const char *key = get_key(prefix, newvar);
+
+               if (key) {
+                       strncat(buf, key, len - strlen(buf) - 1);
+                       /* Replace the trailing ~ */
                        buf[strlen(buf) - 1] = ',';
                }
        }
        /* Trim the trailing comma */
-       buf[strlen(buf) - 1] = '\0';
+       buf_len = strlen(buf);
+       if (buf_len) {
+               buf[buf_len - 1] = '\0';
+       }
        return 0;
 }
 
@@ -903,22 +1146,28 @@ static int hashkeys_read2(struct ast_channel *chan, const char *cmd, char *data,
 {
        struct ast_var_t *newvar;
        struct ast_str *prefix = ast_str_alloca(80);
-       char *tmp;
+
+       if (!chan) {
+               ast_log(LOG_WARNING, "No channel was provided to %s function.\n", cmd);
+               return -1;
+       }
 
        ast_str_set(&prefix, -1, HASH_PREFIX, data);
 
-       AST_LIST_TRAVERSE(&chan->varshead, newvar, entries) {
-               if (strncasecmp(ast_str_buffer(prefix), ast_var_name(newvar), ast_str_strlen(prefix)) == 0) {
-                       /* Copy everything after the prefix */
-                       ast_str_append(buf, len, "%s", ast_var_name(newvar) + ast_str_strlen(prefix));
-                       /* Trim the trailing ~ */
+       AST_LIST_TRAVERSE(ast_channel_varshead(chan), newvar, entries) {
+               const char *key = get_key(prefix, newvar);
+
+               if (key) {
+                       char *tmp;
+
+                       ast_str_append(buf, len, "%s", key);
+                       /* Replace the trailing ~ */
                        tmp = ast_str_buffer(*buf);
                        tmp[ast_str_strlen(*buf) - 1] = ',';
                }
        }
-       /* Trim the trailing comma */
-       tmp = ast_str_buffer(*buf);
-       tmp[ast_str_strlen(*buf) - 1] = '\0';
+
+       ast_str_truncate(*buf, -1);
        return 0;
 }
 
@@ -974,6 +1223,11 @@ static int hash_read(struct ast_channel *chan, const char *cmd, char *data, char
                        AST_APP_ARG(col)[100];
                );
 
+               if (!chan) {
+                       ast_log(LOG_WARNING, "No channel and only 1 parameter was provided to %s function.\n", cmd);
+                       return -1;
+               }
+
                /* Get column names, in no particular order */
                hashkeys_read(chan, "HASHKEYS", arg.hashname, colnames, sizeof(colnames));
                pbx_builtin_setvar_helper(chan, "~ODBCFIELDS~", colnames);
@@ -1018,7 +1272,7 @@ static int quote(struct ast_channel *chan, const char *cmd, char *data, char *bu
        char *bufptr = buf, *dataptr = data;
 
        if (len < 3){ /* at least two for quotes and one for binary zero */
-               ast_log(LOG_ERROR, "Not enough buffer");
+               ast_log(LOG_ERROR, "Not enough buffer\n");
                return -1;
        }
 
@@ -1057,7 +1311,7 @@ static int csv_quote(struct ast_channel *chan, const char *cmd, char *data, char
        char *bufptr = buf, *dataptr = data;
 
        if (len < 3) { /* at least two for quotes and one for binary zero */
-               ast_log(LOG_ERROR, "Not enough buffer");
+               ast_log(LOG_ERROR, "Not enough buffer\n");
                return -1;
        }
 
@@ -1334,7 +1588,7 @@ static int shift_pop(struct ast_channel *chan, const char *cmd, char *data, stru
                return -1;
        }
 
-       varsubst = alloca(strlen(args.var) + 4);
+       varsubst = ast_alloca(strlen(args.var) + 4);
        sprintf(varsubst, "${%s}", args.var);
        ast_str_substitute_variables(&before, 0, chan, varsubst);
 
@@ -1381,6 +1635,7 @@ static int unshift_push(struct ast_channel *chan, const char *cmd, char *data, c
                AST_APP_ARG(var);
                AST_APP_ARG(delimiter);
        );
+       const char *stripped_var;
 
        if (!(buf = ast_str_thread_get(&result_buf, 16)) ||
                !(previous_value = ast_str_thread_get(&tmp_buf, 16))) {
@@ -1398,8 +1653,15 @@ static int unshift_push(struct ast_channel *chan, const char *cmd, char *data, c
                ast_get_encoded_char(args.delimiter, delimiter, &unused);
        }
 
-       varsubst = alloca(strlen(args.var) + 4);
-       sprintf(varsubst, "${%s}", args.var);
+       /* UNSHIFT and PUSH act as ways of setting a variable, so we need to be
+        * sure to skip leading underscores if they appear. However, we only want
+        * to skip up to two since that is the maximum number that can be used to
+        * indicate variable inheritance. Any further underscores are part of the
+        * variable name.
+        */
+       stripped_var = args.var + MIN(strspn(args.var, "_"), 2);
+       varsubst = ast_alloca(strlen(stripped_var) + 4);
+       sprintf(varsubst, "${%s}", stripped_var);
        ast_str_substitute_variables(&previous_value, 0, chan, varsubst);
 
        if (!ast_str_strlen(previous_value)) {
@@ -1439,6 +1701,148 @@ static struct ast_custom_function passthru_function = {
 };
 
 #ifdef TEST_FRAMEWORK
+AST_TEST_DEFINE(test_FIELDNUM)
+{
+       int i, res = AST_TEST_PASS;
+       struct ast_channel *chan;
+       struct ast_str *str;
+       char expression[256];
+       struct {
+               const char *fields;
+               const char *delim;
+               const char *field;
+               const char *expected;
+       } test_args[] = {
+               {"abc,def,ghi,jkl", "\\,",     "ghi", "3"},
+               {"abc def ghi jkl", " ",       "abc", "1"},
+               {"abc/def/ghi/jkl", "\\\\x2f", "def", "2"},
+               {"abc$def$ghi$jkl", "",        "ghi", "0"},
+               {"abc,def,ghi,jkl", "-",       "",    "0"},
+               {"abc-def-ghi-jkl", "-",       "mno", "0"}
+       };
+
+       switch (cmd) {
+       case TEST_INIT:
+               info->name = "func_FIELDNUM_test";
+               info->category = "/funcs/func_strings/";
+               info->summary = "Test FIELDNUM function";
+               info->description = "Verify FIELDNUM behavior";
+               return AST_TEST_NOT_RUN;
+       case TEST_EXECUTE:
+               break;
+       }
+
+       if (!(chan = ast_dummy_channel_alloc())) {
+               ast_test_status_update(test, "Unable to allocate dummy channel\n");
+               return AST_TEST_FAIL;
+       }
+
+       if (!(str = ast_str_create(16))) {
+               ast_test_status_update(test, "Unable to allocate dynamic string buffer\n");
+               ast_channel_release(chan);
+               return AST_TEST_FAIL;
+       }
+
+       for (i = 0; i < ARRAY_LEN(test_args); i++) {
+               struct ast_var_t *var = ast_var_assign("FIELDS", test_args[i].fields);
+               if (!var) {
+                       ast_test_status_update(test, "Out of memory\n");
+                       res = AST_TEST_FAIL;
+                       break;
+               }
+
+               AST_LIST_INSERT_HEAD(ast_channel_varshead(chan), var, entries);
+
+               snprintf(expression, sizeof(expression), "${FIELDNUM(%s,%s,%s)}", var->name, test_args[i].delim, test_args[i].field);
+               ast_str_substitute_variables(&str, 0, chan, expression);
+
+               AST_LIST_REMOVE(ast_channel_varshead(chan), var, entries);
+               ast_var_delete(var);
+
+               if (strcasecmp(ast_str_buffer(str), test_args[i].expected)) {
+                       ast_test_status_update(test, "Evaluation of '%s' returned '%s' instead of the expected value '%s'\n",
+                               expression, ast_str_buffer(str), test_args[i].expected);
+                       res = AST_TEST_FAIL;
+                       break;
+               }
+       }
+
+       ast_free(str);
+       ast_channel_release(chan);
+
+       return res;
+}
+
+AST_TEST_DEFINE(test_REPLACE)
+{
+       int i, res = AST_TEST_PASS;
+       struct ast_channel *chan;
+       struct ast_str *str;
+       char expression[256];
+       struct {
+               const char *test_string;
+               const char *find_chars;
+               const char *replace_char;
+               const char *expected;
+       } test_args[] = {
+               {"abc,def", "\\,", "-", "abc-def"},
+               {"abc,abc", "bc",  "a", "aaa,aaa"},
+               {"abc,def", "x",   "?", "abc,def"},
+               {"abc,def", "\\,", "",  "abcdef"}
+       };
+
+       switch (cmd) {
+       case TEST_INIT:
+               info->name = "func_REPLACE_test";
+               info->category = "/funcs/func_strings/";
+               info->summary = "Test REPLACE function";
+               info->description = "Verify REPLACE behavior";
+               return AST_TEST_NOT_RUN;
+       case TEST_EXECUTE:
+               break;
+       }
+
+       if (!(chan = ast_dummy_channel_alloc())) {
+               ast_test_status_update(test, "Unable to allocate dummy channel\n");
+               return AST_TEST_FAIL;
+       }
+
+       if (!(str = ast_str_create(16))) {
+               ast_test_status_update(test, "Unable to allocate dynamic string buffer\n");
+               ast_channel_release(chan);
+               return AST_TEST_FAIL;
+       }
+
+       for (i = 0; i < ARRAY_LEN(test_args); i++) {
+               struct ast_var_t *var = ast_var_assign("TEST_STRING", test_args[i].test_string);
+               if (!var) {
+                       ast_test_status_update(test, "Out of memory\n");
+                       res = AST_TEST_FAIL;
+                       break;
+               }
+
+               AST_LIST_INSERT_HEAD(ast_channel_varshead(chan), var, entries);
+
+               snprintf(expression, sizeof(expression), "${REPLACE(%s,%s,%s)}", var->name, test_args[i].find_chars, test_args[i].replace_char);
+               ast_str_substitute_variables(&str, 0, chan, expression);
+
+               AST_LIST_REMOVE(ast_channel_varshead(chan), var, entries);
+               ast_var_delete(var);
+
+               if (strcasecmp(ast_str_buffer(str), test_args[i].expected)) {
+                       ast_test_status_update(test, "Evaluation of '%s' returned '%s' instead of the expected value '%s'\n",
+                               expression, ast_str_buffer(str), test_args[i].expected);
+                       res = AST_TEST_FAIL;
+                       break;
+               }
+       }
+
+       ast_free(str);
+       ast_channel_release(chan);
+
+       return res;
+}
+
 AST_TEST_DEFINE(test_FILTER)
 {
        int i, res = AST_TEST_PASS;
@@ -1454,7 +1858,7 @@ AST_TEST_DEFINE(test_FILTER)
        switch (cmd) {
        case TEST_INIT:
                info->name = "func_FILTER_test";
-               info->category = "funcs/func_strings/";
+               info->category = "/funcs/func_strings/";
                info->summary = "Test FILTER function";
                info->description = "Verify FILTER behavior";
                return AST_TEST_NOT_RUN;
@@ -1473,16 +1877,97 @@ AST_TEST_DEFINE(test_FILTER)
        }
        return res;
 }
+
+AST_TEST_DEFINE(test_STRREPLACE)
+{
+       int i, res = AST_TEST_PASS;
+       struct ast_channel *chan; /* dummy channel */
+       struct ast_str *str; /* fancy string for holding comparing value */
+
+       const char *test_strings[][5] = {
+               {"Weasels have eaten my telephone system", "have eaten my", "are eating our", "", "Weasels are eating our telephone system"}, /*Test normal conditions */
+               {"Did you know twenty plus two is twenty-two?", "twenty", "thirty", NULL, "Did you know thirty plus two is thirty-two?"}, /* Test no third comma */
+               {"foofoofoofoofoofoofoo", "foofoo", "bar", NULL, "barbarbarfoo"}, /* Found string within previous match */
+               {"My pet dog once ate a dog who sat on a dog while eating a corndog.", "dog", "cat", "3", "My pet cat once ate a cat who sat on a cat while eating a corndog."},
+               {"One and one and one is three", "and", "plus", "1", "One plus one and one is three"}, /* Test <max-replacements> = 1*/
+               {"", "fhqwagads", "spelunker", NULL, ""}, /* Empty primary string */
+               {"Part of this string is missing.", "missing", NULL, NULL, "Part of this string is ."}, /* Empty replace string */
+               {"'Accidentally' left off a bunch of stuff.", NULL, NULL, NULL, ""}, /* Deliberate error test from too few args */
+               {"This test will also error.", "", "", "", ""}, /* Deliberate error test from blank find string */
+               {"This is an \"escape character\" test.", "\\\"escape character\\\"", "evil", NULL, "This is an evil test."}
+       };
+
+       switch (cmd) {
+       case TEST_INIT:
+               info->name = "func_STRREPLACE_test";
+               info->category = "/funcs/func_strings/";
+               info->summary = "Test STRREPLACE function";
+               info->description = "Verify STRREPLACE behavior";
+               return AST_TEST_NOT_RUN;
+       case TEST_EXECUTE:
+               break;
+       }
+
+       if (!(chan = ast_dummy_channel_alloc())) {
+               ast_test_status_update(test, "Unable to allocate dummy channel\n");
+               return AST_TEST_FAIL;
+       }
+
+       if (!(str = ast_str_create(64))) {
+               ast_test_status_update(test, "Unable to allocate dynamic string buffer\n");
+               ast_channel_release(chan);
+               return AST_TEST_FAIL;
+       }
+
+       for (i = 0; i < ARRAY_LEN(test_strings); i++) {
+               char tmp[512], tmp2[512] = "";
+
+               struct ast_var_t *var = ast_var_assign("test_string", test_strings[i][0]);
+               if (!var) {
+                       ast_test_status_update(test, "Unable to allocate variable\n");
+                       ast_free(str);
+                       ast_channel_release(chan);
+                       return AST_TEST_FAIL;
+               }
+
+               AST_LIST_INSERT_HEAD(ast_channel_varshead(chan), var, entries);
+
+               if (test_strings[i][3]) {
+                       snprintf(tmp, sizeof(tmp), "${STRREPLACE(%s,%s,%s,%s)}", "test_string", test_strings[i][1], test_strings[i][2], test_strings[i][3]);
+               } else if (test_strings[i][2]) {
+                       snprintf(tmp, sizeof(tmp), "${STRREPLACE(%s,%s,%s)}", "test_string", test_strings[i][1], test_strings[i][2]);
+               } else if (test_strings[i][1]) {
+                       snprintf(tmp, sizeof(tmp), "${STRREPLACE(%s,%s)}", "test_string", test_strings[i][1]);
+               } else {
+                       snprintf(tmp, sizeof(tmp), "${STRREPLACE(%s)}", "test_string");
+               }
+               ast_str_substitute_variables(&str, 0, chan, tmp);
+               if (strcmp(test_strings[i][4], ast_str_buffer(str))) {
+                       ast_test_status_update(test, "Format string '%s' substituted to '%s'.  Expected '%s'.\n", test_strings[i][0], tmp2, test_strings[i][4]);
+                       res = AST_TEST_FAIL;
+               }
+       }
+
+       ast_free(str);
+       ast_channel_release(chan);
+
+       return res;
+}
 #endif
 
 static int unload_module(void)
 {
        int res = 0;
 
+       AST_TEST_UNREGISTER(test_FIELDNUM);
+       AST_TEST_UNREGISTER(test_REPLACE);
        AST_TEST_UNREGISTER(test_FILTER);
+       AST_TEST_UNREGISTER(test_STRREPLACE);
        res |= ast_custom_function_unregister(&fieldqty_function);
+       res |= ast_custom_function_unregister(&fieldnum_function);
        res |= ast_custom_function_unregister(&filter_function);
        res |= ast_custom_function_unregister(&replace_function);
+       res |= ast_custom_function_unregister(&strreplace_function);
        res |= ast_custom_function_unregister(&listfilter_function);
        res |= ast_custom_function_unregister(&regex_function);
        res |= ast_custom_function_unregister(&array_function);
@@ -1511,10 +1996,15 @@ static int load_module(void)
 {
        int res = 0;
 
+       AST_TEST_REGISTER(test_FIELDNUM);
+       AST_TEST_REGISTER(test_REPLACE);
        AST_TEST_REGISTER(test_FILTER);
+       AST_TEST_REGISTER(test_STRREPLACE);
        res |= ast_custom_function_register(&fieldqty_function);
+       res |= ast_custom_function_register(&fieldnum_function);
        res |= ast_custom_function_register(&filter_function);
        res |= ast_custom_function_register(&replace_function);
+       res |= ast_custom_function_register(&strreplace_function);
        res |= ast_custom_function_register(&listfilter_function);
        res |= ast_custom_function_register(&regex_function);
        res |= ast_custom_function_register(&array_function);