Mon Nov 24 15:34:39 2008

Asterisk developer's documentation


func_strings.c File Reference

String manipulation dialplan functions. More...

#include "asterisk.h"
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <sys/types.h>
#include <regex.h>
#include "asterisk/module.h"
#include "asterisk/options.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"

Go to the source code of this file.

Defines

#define SPRINTF_CONVERSION   4
#define SPRINTF_FLAG   0
#define SPRINTF_LENGTH   3
#define SPRINTF_PRECISION   2
#define SPRINTF_WIDTH   1

Functions

static int acf_sprintf (struct ast_channel *chan, char *cmd, char *data, char *buf, size_t len)
static int acf_strftime (struct ast_channel *chan, char *cmd, char *parse, char *buf, size_t len)
static int acf_strptime (struct ast_channel *chan, char *cmd, char *data, char *buf, size_t len)
static int array (struct ast_channel *chan, char *cmd, char *var, const char *value)
 AST_MODULE_INFO_STANDARD (ASTERISK_GPL_KEY,"String handling dialplan functions")
static int filter (struct ast_channel *chan, char *cmd, char *parse, char *buf, size_t len)
static int function_eval (struct ast_channel *chan, char *cmd, char *data, char *buf, size_t len)
static int function_fieldqty (struct ast_channel *chan, char *cmd, char *parse, char *buf, size_t len)
static int keypadhash (struct ast_channel *chan, char *cmd, char *data, char *buf, size_t len)
static int len (struct ast_channel *chan, char *cmd, char *data, char *buf, size_t len)
static int load_module (void)
static int quote (struct ast_channel *chan, char *cmd, char *data, char *buf, size_t len)
static int regex (struct ast_channel *chan, char *cmd, char *parse, char *buf, size_t len)
static int unload_module (void)

Variables

static struct ast_custom_function array_function
static struct ast_custom_function eval_function
static struct ast_custom_function fieldqty_function
static struct ast_custom_function filter_function
static struct ast_custom_function keypadhash_function
static struct ast_custom_function len_function
static struct ast_custom_function quote_function
static struct ast_custom_function regex_function
static struct ast_custom_function sprintf_function
static struct ast_custom_function strftime_function
static struct ast_custom_function strptime_function


Detailed Description

String manipulation dialplan functions.

Author:
Tilghman Lesher

Anothony Minessale II

Definition in file func_strings.c.


Define Documentation

#define SPRINTF_CONVERSION   4

Referenced by acf_sprintf().

#define SPRINTF_FLAG   0

Referenced by acf_sprintf().

#define SPRINTF_LENGTH   3

Referenced by acf_sprintf().

#define SPRINTF_PRECISION   2

Referenced by acf_sprintf().

#define SPRINTF_WIDTH   1

Referenced by acf_sprintf().


Function Documentation

static int acf_sprintf ( struct ast_channel chan,
char *  cmd,
char *  data,
char *  buf,
size_t  len 
) [static]

Definition at line 241 of file func_strings.c.

References AST_APP_ARG, AST_DECLARE_APP_ARGS, AST_STANDARD_APP_ARGS, format, SPRINTF_CONVERSION, SPRINTF_FLAG, SPRINTF_LENGTH, SPRINTF_PRECISION, SPRINTF_WIDTH, and var.

00242 {
00243 #define SPRINTF_FLAG 0
00244 #define SPRINTF_WIDTH   1
00245 #define SPRINTF_PRECISION  2
00246 #define SPRINTF_LENGTH  3
00247 #define SPRINTF_CONVERSION 4
00248    int i, state = -1, argcount = 0;
00249    char *formatstart = NULL, *bufptr = buf;
00250    char formatbuf[256] = "";
00251    int tmpi;
00252    double tmpd;
00253    AST_DECLARE_APP_ARGS(arg,
00254             AST_APP_ARG(format);
00255             AST_APP_ARG(var)[100];
00256    );
00257 
00258    AST_STANDARD_APP_ARGS(arg, data);
00259 
00260    /* Scan the format, converting each argument into the requisite format type. */
00261    for (i = 0; arg.format[i]; i++) {
00262       switch (state) {
00263       case SPRINTF_FLAG:
00264          if (strchr("#0- +'I", arg.format[i]))
00265             break;
00266          state = SPRINTF_WIDTH;
00267       case SPRINTF_WIDTH:
00268          if (arg.format[i] >= '0' && arg.format[i] <= '9')
00269             break;
00270 
00271          /* Next character must be a period to go into a precision */
00272          if (arg.format[i] == '.') {
00273             state = SPRINTF_PRECISION;
00274          } else {
00275             state = SPRINTF_LENGTH;
00276             i--;
00277          }
00278          break;
00279       case SPRINTF_PRECISION:
00280          if (arg.format[i] >= '0' && arg.format[i] <= '9')
00281             break;
00282          state = SPRINTF_LENGTH;
00283       case SPRINTF_LENGTH:
00284          if (strchr("hl", arg.format[i])) {
00285             if (arg.format[i + 1] == arg.format[i])
00286                i++;
00287             state = SPRINTF_CONVERSION;
00288             break;
00289          } else if (strchr("Lqjzt", arg.format[i])) {
00290             state = SPRINTF_CONVERSION;
00291             break;
00292          }
00293          state = SPRINTF_CONVERSION;
00294       case SPRINTF_CONVERSION:
00295          if (strchr("diouxXc", arg.format[i])) {
00296             /* Integer */
00297 
00298             /* Isolate this format alone */
00299             ast_copy_string(formatbuf, formatstart, sizeof(formatbuf));
00300             formatbuf[&arg.format[i] - formatstart + 1] = '\0';
00301 
00302             /* Convert the argument into the required type */
00303             if (arg.var[argcount]) {
00304                if (sscanf(arg.var[argcount++], "%d", &tmpi) != 1) {
00305                   ast_log(LOG_ERROR, "Argument '%s' is not an integer number for format '%s'\n", arg.var[argcount - 1], formatbuf);
00306                   goto sprintf_fail;
00307                }
00308             } else {
00309                ast_log(LOG_ERROR, "SPRINTF() has more format specifiers than arguments!\n");
00310                goto sprintf_fail;
00311             }
00312 
00313             /* Format the argument */
00314             snprintf(bufptr, buf + len - bufptr, formatbuf, tmpi);
00315 
00316             /* Update the position of the next parameter to print */
00317             bufptr = strchr(buf, '\0');
00318          } else if (strchr("eEfFgGaA", arg.format[i])) {
00319             /* Double */
00320 
00321             /* Isolate this format alone */
00322             ast_copy_string(formatbuf, formatstart, sizeof(formatbuf));
00323             formatbuf[&arg.format[i] - formatstart + 1] = '\0';
00324 
00325             /* Convert the argument into the required type */
00326             if (arg.var[argcount]) {
00327                if (sscanf(arg.var[argcount++], "%lf", &tmpd) != 1) {
00328                   ast_log(LOG_ERROR, "Argument '%s' is not a floating point number for format '%s'\n", arg.var[argcount - 1], formatbuf);
00329                   goto sprintf_fail;
00330                }
00331             } else {
00332                ast_log(LOG_ERROR, "SPRINTF() has more format specifiers than arguments!\n");
00333                goto sprintf_fail;
00334             }
00335 
00336             /* Format the argument */
00337             snprintf(bufptr, buf + len - bufptr, formatbuf, tmpd);
00338 
00339             /* Update the position of the next parameter to print */
00340             bufptr = strchr(buf, '\0');
00341          } else if (arg.format[i] == 's') {
00342             /* String */
00343 
00344             /* Isolate this format alone */
00345             ast_copy_string(formatbuf, formatstart, sizeof(formatbuf));
00346             formatbuf[&arg.format[i] - formatstart + 1] = '\0';
00347 
00348             /* Format the argument */
00349             snprintf(bufptr, buf + len - bufptr, formatbuf, arg.var[argcount++]);
00350 
00351             /* Update the position of the next parameter to print */
00352             bufptr = strchr(buf, '\0');
00353          } else if (arg.format[i] == '%') {
00354             /* Literal data to copy */
00355             *bufptr++ = arg.format[i];
00356          } else {
00357             /* Not supported */
00358 
00359             /* Isolate this format alone */
00360             ast_copy_string(formatbuf, formatstart, sizeof(formatbuf));
00361             formatbuf[&arg.format[i] - formatstart + 1] = '\0';
00362 
00363             ast_log(LOG_ERROR, "Format type not supported: '%s' with argument '%s'\n", formatbuf, arg.var[argcount++]);
00364             goto sprintf_fail;
00365          }
00366          state = -1;
00367          break;
00368       default:
00369          if (arg.format[i] == '%') {
00370             state = SPRINTF_FLAG;
00371             formatstart = &arg.format[i];
00372             break;
00373          } else {
00374             /* Literal data to copy */
00375             *bufptr++ = arg.format[i];
00376          }
00377       }
00378    }
00379    *bufptr = '\0';
00380    return 0;
00381 sprintf_fail:
00382    return -1;
00383 }

static int acf_strftime ( struct ast_channel chan,
char *  cmd,
char *  parse,
char *  buf,
size_t  len 
) [static]

Definition at line 446 of file func_strings.c.

References AST_APP_ARG, AST_DECLARE_APP_ARGS, ast_get_time_t(), ast_localtime(), ast_log(), AST_STANDARD_APP_ARGS, format, and LOG_WARNING.

00448 {
00449    AST_DECLARE_APP_ARGS(args,
00450               AST_APP_ARG(epoch);
00451               AST_APP_ARG(timezone);
00452               AST_APP_ARG(format);
00453    );
00454    time_t epochi;
00455    struct tm tm;
00456 
00457    buf[0] = '\0';
00458 
00459    AST_STANDARD_APP_ARGS(args, parse);
00460 
00461    ast_get_time_t(args.epoch, &epochi, time(NULL), NULL);
00462    ast_localtime(&epochi, &tm, args.timezone);
00463 
00464    if (!args.format)
00465       args.format = "%c";
00466 
00467    if (!strftime(buf, len, args.format, &tm))
00468       ast_log(LOG_WARNING, "C function strftime() output nothing?!!\n");
00469 
00470    buf[len - 1] = '\0';
00471 
00472    return 0;
00473 }

static int acf_strptime ( struct ast_channel chan,
char *  cmd,
char *  data,
char *  buf,
size_t  len 
) [static]

Definition at line 482 of file func_strings.c.

References AST_APP_ARG, AST_DECLARE_APP_ARGS, ast_log(), ast_mktime(), AST_STANDARD_APP_ARGS, ast_strlen_zero(), format, LOG_ERROR, and LOG_WARNING.

00484 {
00485    AST_DECLARE_APP_ARGS(args,
00486               AST_APP_ARG(timestring);
00487               AST_APP_ARG(timezone);
00488               AST_APP_ARG(format);
00489    );
00490    struct tm time;
00491 
00492    memset(&time, 0, sizeof(struct tm));
00493 
00494    buf[0] = '\0';
00495 
00496    if (!data) {
00497       ast_log(LOG_ERROR,
00498             "Asterisk function STRPTIME() requires an argument.\n");
00499       return -1;
00500    }
00501 
00502    AST_STANDARD_APP_ARGS(args, data);
00503 
00504    if (ast_strlen_zero(args.format)) {
00505       ast_log(LOG_ERROR,
00506             "No format supplied to STRPTIME(<timestring>|<timezone>|<format>)");
00507       return -1;
00508    }
00509 
00510    if (!strptime(args.timestring, args.format, &time)) {
00511       ast_log(LOG_WARNING, "C function strptime() output nothing?!!\n");
00512    } else {
00513       /* Since strptime(3) does not check DST, force ast_mktime() to calculate it. */
00514       time.tm_isdst = -1;
00515       snprintf(buf, len, "%d", (int) ast_mktime(&time, args.timezone));
00516    }
00517 
00518    return 0;
00519 }

static int array ( struct ast_channel chan,
char *  cmd,
char *  var,
const char *  value 
) [static]

Definition at line 171 of file func_strings.c.

References AST_APP_ARG, ast_autoservice_start(), AST_DECLARE_APP_ARGS, ast_log(), AST_NONSTANDARD_APP_ARGS, AST_STANDARD_APP_ARGS, ast_strdupa, LOG_DEBUG, option_debug, and pbx_builtin_setvar_helper().

00173 {
00174    AST_DECLARE_APP_ARGS(arg1,
00175               AST_APP_ARG(var)[100];
00176    );
00177    AST_DECLARE_APP_ARGS(arg2,
00178               AST_APP_ARG(val)[100];
00179    );
00180    char *value2;
00181    int i;
00182 
00183    value2 = ast_strdupa(value);
00184    if (!var || !value2)
00185       return -1;
00186 
00187    if (chan)
00188       ast_autoservice_start(chan);
00189 
00190    /* The functions this will generally be used with are SORT and ODBC_*, which
00191     * both return comma-delimited lists.  However, if somebody uses literal lists,
00192     * their commas will be translated to vertical bars by the load, and I don't
00193     * want them to be surprised by the result.  Hence, we prefer commas as the
00194     * delimiter, but we'll fall back to vertical bars if commas aren't found.
00195     */
00196    if (option_debug)
00197       ast_log(LOG_DEBUG, "array (%s=%s)\n", var, value2);
00198    if (strchr(var, ','))
00199       AST_NONSTANDARD_APP_ARGS(arg1, var, ',');
00200    else
00201       AST_STANDARD_APP_ARGS(arg1, var);
00202 
00203    if (strchr(value2, ','))
00204       AST_NONSTANDARD_APP_ARGS(arg2, value2, ',');
00205    else
00206       AST_STANDARD_APP_ARGS(arg2, value2);
00207 
00208    for (i = 0; i < arg1.argc; i++) {
00209       if (option_debug)
00210          ast_log(LOG_DEBUG, "array set value (%s=%s)\n", arg1.var[i],
00211             arg2.val[i]);
00212       if (i < arg2.argc) {
00213          pbx_builtin_setvar_helper(chan, arg1.var[i], arg2.val[i]);
00214       } else {
00215          /* We could unset the variable, by passing a NULL, but due to
00216           * pushvar semantics, that could create some undesired behavior. */
00217          pbx_builtin_setvar_helper(chan, arg1.var[i], "");
00218       }
00219    }
00220 
00221    if (chan)
00222       ast_autoservice_stop(chan);
00223 
00224    return 0;
00225 }

AST_MODULE_INFO_STANDARD ( ASTERISK_GPL_KEY  ,
"String handling dialplan functions"   
)

static int filter ( struct ast_channel chan,
char *  cmd,
char *  parse,
char *  buf,
size_t  len 
) [static]

Definition at line 89 of file func_strings.c.

References AST_APP_ARG, AST_DECLARE_APP_ARGS, ast_log(), AST_STANDARD_APP_ARGS, and LOG_ERROR.

00091 {
00092    AST_DECLARE_APP_ARGS(args,
00093               AST_APP_ARG(allowed);
00094               AST_APP_ARG(string);
00095    );
00096    char *outbuf = buf;
00097 
00098    AST_STANDARD_APP_ARGS(args, parse);
00099 
00100    if (!args.string) {
00101       ast_log(LOG_ERROR, "Usage: FILTER(<allowed-chars>|<string>)\n");
00102       return -1;
00103    }
00104 
00105    for (; *(args.string) && (buf + len - 1 > outbuf); (args.string)++) {
00106       if (strchr(args.allowed, *(args.string)))
00107          *outbuf++ = *(args.string);
00108    }
00109    *outbuf = '\0';
00110 
00111    return 0;
00112 }

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

Definition at line 536 of file func_strings.c.

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

00538 {
00539    memset(buf, 0, len);
00540 
00541    if (ast_strlen_zero(data)) {
00542       ast_log(LOG_WARNING, "EVAL requires an argument: EVAL(<string>)\n");
00543       return -1;
00544    }
00545 
00546    if (chan)
00547       ast_autoservice_start(chan);
00548    pbx_substitute_variables_helper(chan, data, buf, len - 1);
00549    if (chan)
00550       ast_autoservice_stop(chan);
00551 
00552    return 0;
00553 }

static int function_fieldqty ( struct ast_channel chan,
char *  cmd,
char *  parse,
char *  buf,
size_t  len 
) [static]

Definition at line 46 of file func_strings.c.

References AST_APP_ARG, ast_autoservice_start(), ast_autoservice_stop(), AST_DECLARE_APP_ARGS, AST_STANDARD_APP_ARGS, ast_strlen_zero(), pbx_substitute_variables_helper(), and strsep().

00048 {
00049    char *varsubst, varval[8192] = "", *varval2 = varval;
00050    int fieldcount = 0;
00051    AST_DECLARE_APP_ARGS(args,
00052               AST_APP_ARG(varname);
00053               AST_APP_ARG(delim);
00054       );
00055 
00056    if (chan)
00057       ast_autoservice_start(chan);
00058 
00059    AST_STANDARD_APP_ARGS(args, parse);
00060    if (args.delim) {
00061       varsubst = alloca(strlen(args.varname) + 4);
00062 
00063       sprintf(varsubst, "${%s}", args.varname);
00064       pbx_substitute_variables_helper(chan, varsubst, varval, sizeof(varval) - 1);
00065       if (ast_strlen_zero(varval2))
00066          fieldcount = 0;
00067       else {
00068          while (strsep(&varval2, args.delim))
00069             fieldcount++;
00070       }
00071    } else {
00072       fieldcount = 1;
00073    }
00074    snprintf(buf, len, "%d", fieldcount);
00075 
00076    if (chan)
00077       ast_autoservice_stop(chan);
00078 
00079    return 0;
00080 }

static int keypadhash ( struct ast_channel chan,
char *  cmd,
char *  data,
char *  buf,
size_t  len 
) [static]

Definition at line 571 of file func_strings.c.

00572 {
00573    char *bufptr, *dataptr;
00574 
00575    for (bufptr = buf, dataptr = data; bufptr < buf + len - 1; dataptr++) {
00576       if (*dataptr == '1') {
00577          *bufptr++ = '1';
00578       } else if (strchr("AaBbCc2", *dataptr)) {
00579          *bufptr++ = '2';
00580       } else if (strchr("DdEeFf3", *dataptr)) {
00581          *bufptr++ = '3';
00582       } else if (strchr("GgHhIi4", *dataptr)) {
00583          *bufptr++ = '4';
00584       } else if (strchr("JjKkLl5", *dataptr)) {
00585          *bufptr++ = '5';
00586       } else if (strchr("MmNnOo6", *dataptr)) {
00587          *bufptr++ = '6';
00588       } else if (strchr("PpQqRrSs7", *dataptr)) {
00589          *bufptr++ = '7';
00590       } else if (strchr("TtUuVv8", *dataptr)) {
00591          *bufptr++ = '8';
00592       } else if (strchr("WwXxYyZz9", *dataptr)) {
00593          *bufptr++ = '9';
00594       } else if (*dataptr == '0') {
00595          *bufptr++ = '0';
00596       } else if (*dataptr == '\0') {
00597          *bufptr++ = '\0';
00598          break;
00599       }
00600    }
00601    buf[len - 1] = '\0';
00602 
00603    return 0;
00604 }

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

Definition at line 426 of file func_strings.c.

00428 {
00429    int length = 0;
00430 
00431    if (data)
00432       length = strlen(data);
00433 
00434    snprintf(buf, len, "%d", length);
00435 
00436    return 0;
00437 }

static int load_module ( void   )  [static]

Definition at line 633 of file func_strings.c.

References array_function, ast_custom_function_register(), eval_function, fieldqty_function, filter_function, keypadhash_function, len_function, quote_function, regex_function, sprintf_function, strftime_function, and strptime_function.

static int quote ( struct ast_channel chan,
char *  cmd,
char *  data,
char *  buf,
size_t  len 
) [static]

Definition at line 396 of file func_strings.c.

Referenced by ast_app_separate_args(), and make_email_file().

00397 {
00398    char *bufptr = buf, *dataptr = data;
00399    *bufptr++ = '"';
00400    for (; bufptr < buf + len - 1; dataptr++) {
00401       if (*dataptr == '\\') {
00402          *bufptr++ = '\\';
00403          *bufptr++ = '\\';
00404       } else if (*dataptr == '"') {
00405          *bufptr++ = '\\';
00406          *bufptr++ = '"';
00407       } else if (*dataptr == '\0') {
00408          break;
00409       } else {
00410          *bufptr++ = *dataptr;
00411       }
00412    }
00413    *bufptr++ = '"';
00414    *bufptr = '\0';
00415    return 0;
00416 }

static int regex ( struct ast_channel chan,
char *  cmd,
char *  parse,
char *  buf,
size_t  len 
) [static]

Definition at line 121 of file func_strings.c.

References AST_APP_ARG, AST_DECLARE_APP_ARGS, ast_log(), AST_NONSTANDARD_APP_ARGS, LOG_DEBUG, LOG_ERROR, LOG_WARNING, and option_debug.

00123 {
00124    AST_DECLARE_APP_ARGS(args,
00125               AST_APP_ARG(null);
00126               AST_APP_ARG(reg);
00127               AST_APP_ARG(str);
00128    );
00129    int errcode;
00130    regex_t regexbuf;
00131 
00132    buf[0] = '\0';
00133 
00134    AST_NONSTANDARD_APP_ARGS(args, parse, '"');
00135 
00136    if (args.argc != 3) {
00137       ast_log(LOG_ERROR, "Unexpected arguments: should have been in the form '\"<regex>\" <string>'\n");
00138       return -1;
00139    }
00140    if ((*args.str == ' ') || (*args.str == '\t'))
00141       args.str++;
00142 
00143    if (option_debug)
00144       ast_log(LOG_DEBUG, "FUNCTION REGEX (%s)(%s)\n", args.reg, args.str);
00145 
00146    if ((errcode = regcomp(&regexbuf, args.reg, REG_EXTENDED | REG_NOSUB))) {
00147       regerror(errcode, &regexbuf, buf, len);
00148       ast_log(LOG_WARNING, "Malformed input %s(%s): %s\n", cmd, parse, buf);
00149       return -1;
00150    }
00151    
00152    strcpy(buf, regexec(&regexbuf, args.str, 0, NULL, 0) ? "0" : "1");
00153 
00154    regfree(&regexbuf);
00155 
00156    return 0;
00157 }

static int unload_module ( void   )  [static]

Definition at line 614 of file func_strings.c.

References array_function, ast_custom_function_unregister(), eval_function, fieldqty_function, filter_function, keypadhash_function, len_function, quote_function, regex_function, sprintf_function, strftime_function, and strptime_function.


Variable Documentation

struct ast_custom_function array_function [static]

Definition at line 227 of file func_strings.c.

Referenced by load_module(), and unload_module().

struct ast_custom_function eval_function [static]

Definition at line 555 of file func_strings.c.

Referenced by load_module(), and unload_module().

struct ast_custom_function fieldqty_function [static]

Definition at line 82 of file func_strings.c.

Referenced by load_module(), and unload_module().

struct ast_custom_function filter_function [static]

Definition at line 114 of file func_strings.c.

Referenced by load_module(), and unload_module().

struct ast_custom_function keypadhash_function [static]

Definition at line 606 of file func_strings.c.

Referenced by load_module(), and unload_module().

struct ast_custom_function len_function [static]

Definition at line 439 of file func_strings.c.

Referenced by load_module(), and unload_module().

struct ast_custom_function quote_function [static]

Definition at line 418 of file func_strings.c.

Referenced by load_module(), and unload_module().

struct ast_custom_function regex_function [static]

Definition at line 159 of file func_strings.c.

Referenced by load_module(), and unload_module().

struct ast_custom_function sprintf_function [static]

Definition at line 385 of file func_strings.c.

Referenced by load_module(), and unload_module().

struct ast_custom_function strftime_function [static]

Definition at line 475 of file func_strings.c.

Referenced by load_module(), and unload_module().

struct ast_custom_function strptime_function [static]

Definition at line 521 of file func_strings.c.

Referenced by load_module(), and unload_module().


Generated on Mon Nov 24 15:34:39 2008 for Asterisk - the Open Source PBX by  doxygen 1.4.7