diff options
author | Kevin P. Fleming <kpfleming@digium.com> | 2005-05-05 05:39:33 +0000 |
---|---|---|
committer | Kevin P. Fleming <kpfleming@digium.com> | 2005-05-05 05:39:33 +0000 |
commit | 4d2537ae1136e6885c3b1214ebed6ccab2bdbec2 (patch) | |
tree | d1f6c678019f87fb5730998dc62af2c209dc66d6 /funcs/func_strings.c | |
parent | e8bd5968ff53cd8e2acc82c4f80902bc0c404d54 (diff) |
major re-work of dialplan functions, including:
- locking of functions list during registration/unregistration/searching
- rename of function description structure to be consistent with the rest of the API
- addition of 'desc' element to description structure, for detailed description (like applications)
- addition of 'show function' CLI command to show function details
- conversion of existing functions to use uppercase names to match policy
- creation of new 'pbx_functions.so' module to contain standard 'builtin' functions
- removal of all builtin functions from pbx.c and apps and placement into new 'funcs' directory
git-svn-id: https://origsvn.digium.com/svn/asterisk/trunk@5583 65c4cc65-6c06-0410-ace0-fbb531ad65f3
Diffstat (limited to 'funcs/func_strings.c')
-rwxr-xr-x | funcs/func_strings.c | 117 |
1 files changed, 117 insertions, 0 deletions
diff --git a/funcs/func_strings.c b/funcs/func_strings.c new file mode 100755 index 000000000..b70fbfeb8 --- /dev/null +++ b/funcs/func_strings.c @@ -0,0 +1,117 @@ +/* + * Asterisk -- A telephony toolkit for Linux. + * + * String manipulation dialplan functions + * + * Copyright (C) 2005, Digium, Inc. + * + * This program is free software, distributed under the terms of + * the GNU General Public License + */ + +#include <stdlib.h> +#include <string.h> +#include <sys/types.h> +#include <regex.h> + +#include "asterisk/channel.h" +#include "asterisk/pbx.h" +#include "asterisk/logger.h" +#include "asterisk/utils.h" +#include "asterisk/app.h" + +static char *function_fieldqty(struct ast_channel *chan, char *cmd, char *data, char *buf, size_t len) +{ + char *varname, *varval, workspace[256]; + char *delim = ast_strdupa(data); + int fieldcount = 0; + + if (delim) { + varname = strsep(&delim, "|"); + pbx_retrieve_variable(chan, varname, &varval, workspace, sizeof(workspace), NULL); + while (strsep(&varval, delim)) + fieldcount++; + snprintf(buf, len, "%d", fieldcount); + } else { + ast_log(LOG_ERROR, "Out of memory\n"); + strncpy(buf, "1", len); + } + return buf; +} + +#ifndef BUILTIN_FUNC +static +#endif +struct ast_custom_function fieldqty_function = { + .name = "FIELDQTY", + .synopsis = "Count the fields, with an arbitrary delimiter", + .syntax = "FIELDQTY(<varname>,<delim>)", + .read = function_fieldqty, +}; + +static char *builtin_function_regex(struct ast_channel *chan, char *cmd, char *data, char *buf, size_t len) +{ + char *ret_true = "1", *ret_false = "0", *ret; + char *arg, *earg, *tmp, errstr[256] = ""; + int errcode; + regex_t regexbuf; + + ret = ret_false; /* convince me otherwise */ + tmp = ast_strdupa(data); + if (tmp) { + /* Regex in quotes */ + arg = strchr(tmp, '"'); + if (arg) { + arg++; + earg = strrchr(arg, '"'); + if (earg) { + *earg = '\0'; + } + } else { + arg = tmp; + } + + if ((errcode = regcomp(®exbuf, arg, REG_EXTENDED | REG_NOSUB))) { + regerror(errcode, ®exbuf, errstr, sizeof(errstr)); + ast_log(LOG_WARNING, "Malformed input %s(%s): %s\n", cmd, data, errstr); + ret = NULL; + } else { + ret = regexec(®exbuf, data, 0, NULL, 0) ? ret_false : ret_true; + } + regfree(®exbuf); + } else { + ast_log(LOG_ERROR, "Out of memory in %s(%s)\n", cmd, data); + } + + return ret; +} + +#ifndef BUILTIN_FUNC +static +#endif +struct ast_custom_function regex_function = { + .name = "REGEX", + .synopsis = "Regular Expression: Returns 1 if data matches regular expression.", + .syntax = "REGEX(\"<regular expression>\" <data>)", + .read = builtin_function_regex, +}; + +static char *builtin_function_len(struct ast_channel *chan, char *cmd, char *data, char *buf, size_t len) +{ + int length = 0; + if (data) { + length = strlen(data); + } + snprintf(buf, len, "%d", length); + return buf; +} + +#ifndef BUILTIN_FUNC +static +#endif +struct ast_custom_function len_function = { + .name = "LEN", + .synopsis = "Returns the length of the argument given", + .syntax = "LEN(<string>)", + .read = builtin_function_len, +}; |