Asterisk.org: Developer Documentation (28 Oct 2009)

Wed Oct 28 15:48:58 2009

## Asterisk developer's documentation

# func_strings.c File Reference

String manipulation dialplan functions. More...

`#include <stdlib.h>`
`#include <stdio.h>`
`#include <string.h>`
`#include <sys/types.h>`
`#include <regex.h>`
`#include "asterisk.h"`
`#include "asterisk/channel.h"`
`#include "asterisk/pbx.h"`
`#include "asterisk/logger.h"`
`#include "asterisk/utils.h"`
`#include "asterisk/app.h"`
`#include "asterisk/localtime.h"`

Include dependency graph for func_strings.c:

Go to the source code of this file.

## Functions

static char * | acf_strftime (struct ast_channel *chan, char *cmd, char *data, char *buf, size_t len)
static char * | builtin_function_len (struct ast_channel *chan, char *cmd, char *data, char *buf, size_t len)
static char * | builtin_function_regex (struct ast_channel *chan, char *cmd, char *data, char *buf, size_t len)
static char * | function_eval (struct ast_channel *chan, char *cmd, char *data, char *buf, size_t len)
static char * | function_fieldqty (struct ast_channel *chan, char *cmd, char *data, char *buf, size_t len)

## Variables

static struct ast_custom_function | eval_function
static struct ast_custom_function | fieldqty_function
static struct ast_custom_function | len_function
static struct ast_custom_function | regex_function
static struct ast_custom_function | strftime_function

## Detailed Description

String manipulation dialplan functions.

Definition in file func_strings.c.

## Function Documentation

static char* acf_strftime | ( | struct ast_channel * | chan,
|  | char * | cmd,
|  | char * | data,
|  | char * | buf,
|  | size_t | len |
| ) |  |  | `[static]`

Definition at line 150 of file func_strings.c.

References ast_localtime(), ast_log(), ast_strdupa, ast_strlen_zero(), format, LOG_ERROR, LOG_WARNING, and strsep().

    00151 {
    00152    char *format, *epoch, *timezone = NULL;
    00153    long epochi;
    00154    struct tm time;
    00155
    00156    buf[0] = '\0';
    00157
    00158    if (!data) {
    00159       ast_log(LOG_ERROR, "Asterisk function STRFTIME() requires an argument.\n");
    00160       return buf;
    00161    }
    00162
    00163    format = ast_strdupa(data);
    00164    if (!format) {
    00165       ast_log(LOG_ERROR, "Out of memory\n");
    00166       return buf;
    00167    }
    00168
    00169    epoch = strsep(&format, "|");
    00170    timezone = strsep(&format, "|");
    00171
    00172    if (ast_strlen_zero(epoch) || !sscanf(epoch, "%30ld", &epochi)) {
    00173       struct timeval tv = ast_tvnow();
    00174       epochi = tv.tv_sec;
    00175    }
    00176
    00177    ast_localtime(&epochi, &time, timezone);
    00178
    00179    if (!format) {
    00180       format = "%c";
    00181    }
    00182
    00183    if (!strftime(buf, len, format, &time)) {
    00184       ast_log(LOG_WARNING, "C function strftime() output nothing?!!\n");
    00185    }
    00186    buf[len - 1] = '\0';
    00187
    00188    return buf;
    00189 }

static char* builtin_function_len | ( | struct ast_channel * | chan,
|  | char * | cmd,
|  | char * | data,
|  | char * | buf,
|  | size_t | len |
| ) |  |  | `[static]`

Definition at line 130 of file func_strings.c.

    00131 {
    00132    int length = 0;
    00133    if (data) {
    00134       length = strlen(data);
    00135    }
    00136    snprintf(buf, len, "%d", length);
    00137    return buf;
    00138 }

static char* builtin_function_regex | ( | struct ast_channel * | chan,
|  | char * | cmd,
|  | char * | data,
|  | char * | buf,
|  | size_t | len |
| ) |  |  | `[static]`

Definition at line 80 of file func_strings.c.

References ast_log(), ast_strdupa, LOG_ERROR, LOG_WARNING, and strsep().

    00081 {
    00082    char *arg, *earg = NULL, *tmp, errstr[256] = "";
    00083    int errcode;
    00084    regex_t regexbuf;
    00085
    00086    ast_copy_string(buf, "0", len);
    00087
    00088    tmp = ast_strdupa(data);
    00089    if (!tmp) {
    00090       ast_log(LOG_ERROR, "Out of memory in %s(%s)\n", cmd, data);
    00091       return buf;
    00092    }
    00093
    00094    /* Regex in quotes */
    00095    arg = strchr(tmp, '"');
    00096    if (arg) {
    00097       earg = ++arg;
    00098       strsep(&earg, "\"");
    00099       if (earg) {
    00100          /* Skip over any spaces before the data we are checking */
    00101          while (*earg == ' ')
    00102             earg++;
    00103       }
    00104    } else {
    00105       arg = tmp;
    00106    }
    00107
    00108    if ((errcode = regcomp(&regexbuf, arg, REG_EXTENDED | REG_NOSUB))) {
    00109       regerror(errcode, &regexbuf, errstr, sizeof(errstr));
    00110       ast_log(LOG_WARNING, "Malformed input %s(%s): %s\n", cmd, data, errstr);
    00111    } else {
    00112       if (!regexec(&regexbuf, earg ? earg : "", 0, NULL, 0))
    00113          ast_copy_string(buf, "1", len);
    00114    }
    00115    regfree(&regexbuf);
    00116
    00117    return buf;
    00118 }

static char* function_eval | ( | struct ast_channel * | chan,
|  | char * | cmd,
|  | char * | data,
|  | char * | buf,
|  | size_t | len |
| ) |  |  | `[static]`

Definition at line 201 of file func_strings.c.

References ast_log(), ast_strlen_zero(), LOG_WARNING, and pbx_substitute_variables_helper().

    00202 {
    00203    memset(buf, 0, len);
    00204
    00205    if (ast_strlen_zero(data)) {
    00206       ast_log(LOG_WARNING, "EVAL requires an argument: EVAL(<string>)\n");
    00207       return buf;
    00208    }
    00209
    00210    pbx_substitute_variables_helper(chan, data, buf, len - 1);
    00211
    00212    return buf;
    00213 }

static char* function_fieldqty | ( | struct ast_channel * | chan,
|  | char * | cmd,
|  | char * | data,
|  | char * | buf,
|  | size_t | len |
| ) |  |  | `[static]`

Definition at line 42 of file func_strings.c.

References ast_log(), ast_strdupa, ast_strlen_zero(), LOG_ERROR, pbx_substitute_variables_helper(), and strsep().

    00043 {
    00044    char *varname, *varsubst, varval[8192] = "", *varval2 = varval;
    00045    char *delim = ast_strdupa(data);
    00046    int fieldcount = 0;
    00047
    00048    if (delim) {
    00049       varname = strsep(&delim, "|");
    00050       varsubst = alloca(strlen(varname) + 4);
    00051
    00052       sprintf(varsubst, "${%s}", varname);
    00053       pbx_substitute_variables_helper(chan, varsubst, varval, sizeof(varval) - 1);
    00054       if (ast_strlen_zero(varval))
    00055          fieldcount = 0;
    00056       else if (delim) {
    00057          while (strsep(&varval2, delim))
    00058             fieldcount++;
    00059       } else if (!ast_strlen_zero(varval)) {
    00060          fieldcount = 1;
    00061       }
    00062       snprintf(buf, len, "%d", fieldcount);
    00063    } else {
    00064       ast_log(LOG_ERROR, "Out of memory\n");
    00065       ast_copy_string(buf, "1", len);
    00066    }
    00067    return buf;
    00068 }

## Variable Documentation

struct ast_custom_function eval_function`[static]`

Definition at line 218 of file func_strings.c.

struct ast_custom_function fieldqty_function`[static]`

Definition at line 73 of file func_strings.c.

struct ast_custom_function len_function`[static]`

Definition at line 143 of file func_strings.c.

struct ast_custom_function regex_function`[static]`

Definition at line 123 of file func_strings.c.

struct ast_custom_function strftime_function`[static]`

Definition at line 194 of file func_strings.c.