Fri Jul 3 20:35:38 2009

Asterisk developer's documentation


func_strings.c

Go to the documentation of this file.
00001 /*
00002  * Asterisk -- An open source telephony toolkit.
00003  *
00004  * Copyright (C) 2005-2006, Digium, Inc.
00005  * Portions Copyright (C) 2005, Tilghman Lesher.  All rights reserved.
00006  * Portions Copyright (C) 2005, Anthony Minessale II
00007  *
00008  * See http://www.asterisk.org for more information about
00009  * the Asterisk project. Please do not directly contact
00010  * any of the maintainers of this project for assistance;
00011  * the project provides a web site, mailing lists and IRC
00012  * channels for your use.
00013  *
00014  * This program is free software, distributed under the terms of
00015  * the GNU General Public License Version 2. See the LICENSE file
00016  * at the top of the source tree.
00017  */
00018 
00019 /*! \file
00020  *
00021  * \brief String manipulation dialplan functions
00022  *
00023  * \author Tilghman Lesher
00024  * \author Anothony Minessale II 
00025  */
00026 
00027 #include "asterisk.h"
00028 
00029 ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
00030 
00031 #include <stdlib.h>
00032 #include <stdio.h>
00033 #include <string.h>
00034 #include <sys/types.h>
00035 #include <regex.h>
00036 #include <ctype.h>
00037 
00038 #include "asterisk/module.h"
00039 #include "asterisk/options.h"
00040 #include "asterisk/channel.h"
00041 #include "asterisk/pbx.h"
00042 #include "asterisk/logger.h"
00043 #include "asterisk/utils.h"
00044 #include "asterisk/app.h"
00045 #include "asterisk/localtime.h"
00046 
00047 static int function_fieldqty(struct ast_channel *chan, char *cmd,
00048               char *parse, char *buf, size_t len)
00049 {
00050    char *varsubst, varval[8192] = "", *varval2 = varval;
00051    int fieldcount = 0;
00052    AST_DECLARE_APP_ARGS(args,
00053               AST_APP_ARG(varname);
00054               AST_APP_ARG(delim);
00055       );
00056 
00057    AST_STANDARD_APP_ARGS(args, parse);
00058    if (args.delim) {
00059       pbx_retrieve_variable(chan, args.varname, &varval, buf, len, NULL);
00060       if (args.delim[0] == '\\') {
00061          if (args.delim[1] == 'n')
00062             ast_copy_string(args.delim, "\n", 2);
00063          else if (args.delim[1] == 't')
00064             ast_copy_string(args.delim, "\t", 2);
00065          else if (args.delim[1])
00066             ast_copy_string(args.delim, &args.delim[1], 2);
00067          else
00068             ast_copy_string(args.delim, "-", 2);
00069       }
00070       while (strsep(&varval, args.delim))
00071          fieldcount++;
00072    } else {
00073       fieldcount = 1;
00074    }
00075    snprintf(buf, len, "%d", fieldcount);
00076 
00077    return 0;
00078 }
00079 
00080 static struct ast_custom_function fieldqty_function = {
00081    .name = "FIELDQTY",
00082    .synopsis = "Count the fields, with an arbitrary delimiter",
00083    .syntax = "FIELDQTY(<varname>|<delim>)",
00084    .read = function_fieldqty,
00085 };
00086 
00087 static int filter(struct ast_channel *chan, char *cmd, char *parse, char *buf,
00088         size_t len)
00089 {
00090    AST_DECLARE_APP_ARGS(args,
00091               AST_APP_ARG(allowed);
00092               AST_APP_ARG(string);
00093    );
00094    char *outbuf = buf;
00095 
00096    AST_STANDARD_APP_ARGS(args, parse);
00097 
00098    if (!args.string) {
00099       ast_log(LOG_ERROR, "Usage: FILTER(<allowed-chars>|<string>)\n");
00100       return -1;
00101    }
00102 
00103    for (; *(args.string) && (buf + len - 1 > outbuf); (args.string)++) {
00104       if (strchr(args.allowed, *(args.string)))
00105          *outbuf++ = *(args.string);
00106    }
00107    *outbuf = '\0';
00108 
00109    return 0;
00110 }
00111 
00112 static struct ast_custom_function filter_function = {
00113    .name = "FILTER",
00114    .synopsis = "Filter the string to include only the allowed characters",
00115    .syntax = "FILTER(<allowed-chars>|<string>)",
00116    .read = filter,
00117 };
00118 
00119 static int regex(struct ast_channel *chan, char *cmd, char *parse, char *buf,
00120        size_t len)
00121 {
00122    AST_DECLARE_APP_ARGS(args,
00123               AST_APP_ARG(null);
00124               AST_APP_ARG(reg);
00125               AST_APP_ARG(str);
00126    );
00127    int errcode;
00128    regex_t regexbuf;
00129 
00130    buf[0] = '\0';
00131 
00132    AST_NONSTANDARD_APP_ARGS(args, parse, '"');
00133 
00134    if (args.argc != 3) {
00135       ast_log(LOG_ERROR, "Unexpected arguments: should have been in the form '\"<regex>\" <string>'\n");
00136       return -1;
00137    }
00138    if ((*args.str == ' ') || (*args.str == '\t'))
00139       args.str++;
00140 
00141    if (option_debug)
00142       ast_log(LOG_DEBUG, "FUNCTION REGEX (%s)(%s)\n", args.reg, args.str);
00143 
00144    if ((errcode = regcomp(&regexbuf, args.reg, REG_EXTENDED | REG_NOSUB))) {
00145       regerror(errcode, &regexbuf, buf, len);
00146       ast_log(LOG_WARNING, "Malformed input %s(%s): %s\n", cmd, parse, buf);
00147       return -1;
00148    }
00149    
00150    strcpy(buf, regexec(&regexbuf, args.str, 0, NULL, 0) ? "0" : "1");
00151 
00152    regfree(&regexbuf);
00153 
00154    return 0;
00155 }
00156 
00157 static struct ast_custom_function regex_function = {
00158    .name = "REGEX",
00159    .synopsis = "Regular Expression",
00160    .desc =  
00161       "Returns 1 if data matches regular expression, or 0 otherwise.\n"
00162       "Please note that the space following the double quotes separating the regex from the data\n"
00163       "is optional and if present, is skipped. If a space is desired at the beginning of the data,\n"
00164            "then put two spaces there; the second will not be skipped.\n",
00165    .syntax = "REGEX(\"<regular expression>\" <data>)",
00166    .read = regex,
00167 };
00168 
00169 static int array(struct ast_channel *chan, char *cmd, char *var,
00170        const char *value)
00171 {
00172    AST_DECLARE_APP_ARGS(arg1,
00173               AST_APP_ARG(var)[100];
00174    );
00175    AST_DECLARE_APP_ARGS(arg2,
00176               AST_APP_ARG(val)[100];
00177    );
00178    char *value2;
00179    int i;
00180 
00181    value2 = ast_strdupa(value);
00182    if (!var || !value2)
00183       return -1;
00184 
00185    /* The functions this will generally be used with are SORT and ODBC_*, which
00186     * both return comma-delimited lists.  However, if somebody uses literal lists,
00187     * their commas will be translated to vertical bars by the load, and I don't
00188     * want them to be surprised by the result.  Hence, we prefer commas as the
00189     * delimiter, but we'll fall back to vertical bars if commas aren't found.
00190     */
00191    if (option_debug)
00192       ast_log(LOG_DEBUG, "array (%s=%s)\n", var, value2);
00193    if (strchr(var, ','))
00194       AST_NONSTANDARD_APP_ARGS(arg1, var, ',');
00195    else
00196       AST_STANDARD_APP_ARGS(arg1, var);
00197 
00198    if (strchr(value2, ','))
00199       AST_NONSTANDARD_APP_ARGS(arg2, value2, ',');
00200    else
00201       AST_STANDARD_APP_ARGS(arg2, value2);
00202 
00203    for (i = 0; i < arg1.argc; i++) {
00204       if (option_debug)
00205          ast_log(LOG_DEBUG, "array set value (%s=%s)\n", arg1.var[i],
00206             arg2.val[i]);
00207       if (i < arg2.argc) {
00208          pbx_builtin_setvar_helper(chan, arg1.var[i], arg2.val[i]);
00209       } else {
00210          /* We could unset the variable, by passing a NULL, but due to
00211           * pushvar semantics, that could create some undesired behavior. */
00212          pbx_builtin_setvar_helper(chan, arg1.var[i], "");
00213       }
00214    }
00215 
00216    return 0;
00217 }
00218 
00219 static struct ast_custom_function array_function = {
00220    .name = "ARRAY",
00221    .synopsis = "Allows setting multiple variables at once",
00222    .syntax = "ARRAY(var1[|var2[...][|varN]])",
00223    .write = array,
00224    .desc =
00225       "The comma-separated list passed as a value to which the function is set will\n"
00226       "be interpreted as a set of values to which the comma-separated list of\n"
00227       "variable names in the argument should be set.\n"
00228       "Hence, Set(ARRAY(var1|var2)=1\\,2) will set var1 to 1 and var2 to 2\n"
00229       "Note: remember to either backslash your commas in extensions.conf or quote the\n"
00230       "entire argument, since Set can take multiple arguments itself.\n",
00231 };
00232 
00233 static int acf_sprintf(struct ast_channel *chan, char *cmd, char *data, char *buf, size_t len)
00234 {
00235 #define SPRINTF_FLAG 0
00236 #define SPRINTF_WIDTH   1
00237 #define SPRINTF_PRECISION  2
00238 #define SPRINTF_LENGTH  3
00239 #define SPRINTF_CONVERSION 4
00240    int i, state = -1, argcount = 0;
00241    char *formatstart = NULL, *bufptr = buf;
00242    char formatbuf[256] = "";
00243    int tmpi;
00244    double tmpd;
00245    AST_DECLARE_APP_ARGS(arg,
00246             AST_APP_ARG(format);
00247             AST_APP_ARG(var)[100];
00248    );
00249 
00250    AST_STANDARD_APP_ARGS(arg, data);
00251 
00252    /* Scan the format, converting each argument into the requisite format type. */
00253    for (i = 0; arg.format[i]; i++) {
00254       switch (state) {
00255       case SPRINTF_FLAG:
00256          if (strchr("#0- +'I", arg.format[i]))
00257             break;
00258          state = SPRINTF_WIDTH;
00259       case SPRINTF_WIDTH:
00260          if (arg.format[i] >= '0' && arg.format[i] <= '9')
00261             break;
00262 
00263          /* Next character must be a period to go into a precision */
00264          if (arg.format[i] == '.') {
00265             state = SPRINTF_PRECISION;
00266          } else {
00267             state = SPRINTF_LENGTH;
00268             i--;
00269          }
00270          break;
00271       case SPRINTF_PRECISION:
00272          if (arg.format[i] >= '0' && arg.format[i] <= '9')
00273             break;
00274          state = SPRINTF_LENGTH;
00275       case SPRINTF_LENGTH:
00276          if (strchr("hl", arg.format[i])) {
00277             if (arg.format[i + 1] == arg.format[i])
00278                i++;
00279             state = SPRINTF_CONVERSION;
00280             break;
00281          } else if (strchr("Lqjzt", arg.format[i])) {
00282             state = SPRINTF_CONVERSION;
00283             break;
00284          }
00285          state = SPRINTF_CONVERSION;
00286       case SPRINTF_CONVERSION:
00287          if (strchr("diouxXc", arg.format[i])) {
00288             /* Integer */
00289 
00290             /* Isolate this format alone */
00291             ast_copy_string(formatbuf, formatstart, sizeof(formatbuf));
00292             formatbuf[&arg.format[i] - formatstart + 1] = '\0';
00293 
00294             /* Convert the argument into the required type */
00295             if (arg.var[argcount]) {
00296                if (sscanf(arg.var[argcount++], "%d", &tmpi) != 1) {
00297                   ast_log(LOG_ERROR, "Argument '%s' is not an integer number for format '%s'\n", arg.var[argcount - 1], formatbuf);
00298                   goto sprintf_fail;
00299                }
00300             } else {
00301                ast_log(LOG_ERROR, "SPRINTF() has more format specifiers than arguments!\n");
00302                goto sprintf_fail;
00303             }
00304 
00305             /* Format the argument */
00306             snprintf(bufptr, buf + len - bufptr, formatbuf, tmpi);
00307 
00308             /* Update the position of the next parameter to print */
00309             bufptr = strchr(buf, '\0');
00310          } else if (strchr("eEfFgGaA", arg.format[i])) {
00311             /* Double */
00312 
00313             /* Isolate this format alone */
00314             ast_copy_string(formatbuf, formatstart, sizeof(formatbuf));
00315             formatbuf[&arg.format[i] - formatstart + 1] = '\0';
00316 
00317             /* Convert the argument into the required type */
00318             if (arg.var[argcount]) {
00319                if (sscanf(arg.var[argcount++], "%lf", &tmpd) != 1) {
00320                   ast_log(LOG_ERROR, "Argument '%s' is not a floating point number for format '%s'\n", arg.var[argcount - 1], formatbuf);
00321                   goto sprintf_fail;
00322                }
00323             } else {
00324                ast_log(LOG_ERROR, "SPRINTF() has more format specifiers than arguments!\n");
00325                goto sprintf_fail;
00326             }
00327 
00328             /* Format the argument */
00329             snprintf(bufptr, buf + len - bufptr, formatbuf, tmpd);
00330 
00331             /* Update the position of the next parameter to print */
00332             bufptr = strchr(buf, '\0');
00333          } else if (arg.format[i] == 's') {
00334             /* String */
00335 
00336             /* Isolate this format alone */
00337             ast_copy_string(formatbuf, formatstart, sizeof(formatbuf));
00338             formatbuf[&arg.format[i] - formatstart + 1] = '\0';
00339 
00340             /* Format the argument */
00341             snprintf(bufptr, buf + len - bufptr, formatbuf, arg.var[argcount++]);
00342 
00343             /* Update the position of the next parameter to print */
00344             bufptr = strchr(buf, '\0');
00345          } else if (arg.format[i] == '%') {
00346             /* Literal data to copy */
00347             *bufptr++ = arg.format[i];
00348          } else {
00349             /* Not supported */
00350 
00351             /* Isolate this format alone */
00352             ast_copy_string(formatbuf, formatstart, sizeof(formatbuf));
00353             formatbuf[&arg.format[i] - formatstart + 1] = '\0';
00354 
00355             ast_log(LOG_ERROR, "Format type not supported: '%s' with argument '%s'\n", formatbuf, arg.var[argcount++]);
00356             goto sprintf_fail;
00357          }
00358          state = -1;
00359          break;
00360       default:
00361          if (arg.format[i] == '%') {
00362             state = SPRINTF_FLAG;
00363             formatstart = &arg.format[i];
00364             break;
00365          } else {
00366             /* Literal data to copy */
00367             *bufptr++ = arg.format[i];
00368          }
00369       }
00370    }
00371    *bufptr = '\0';
00372    return 0;
00373 sprintf_fail:
00374    return -1;
00375 }
00376 
00377 static struct ast_custom_function sprintf_function = {
00378    .name = "SPRINTF",
00379    .synopsis = "Format a variable according to a format string",
00380    .syntax = "SPRINTF(<format>|<arg1>[|...<argN>])",
00381    .read = acf_sprintf,
00382    .desc =
00383 "Parses the format string specified and returns a string matching that format.\n"
00384 "Supports most options supported by sprintf(3).  Returns a shortened string if\n"
00385 "a format specifier is not recognized.\n",
00386 };
00387 
00388 static int quote(struct ast_channel *chan, char *cmd, char *data, char *buf, size_t len)
00389 {
00390    char *bufptr = buf, *dataptr = data;
00391    *bufptr++ = '"';
00392    for (; bufptr < buf + len - 1; dataptr++) {
00393       if (*dataptr == '\\') {
00394          *bufptr++ = '\\';
00395          *bufptr++ = '\\';
00396       } else if (*dataptr == '"') {
00397          *bufptr++ = '\\';
00398          *bufptr++ = '"';
00399       } else if (*dataptr == '\0') {
00400          break;
00401       } else {
00402          *bufptr++ = *dataptr;
00403       }
00404    }
00405    *bufptr++ = '"';
00406    *bufptr = '\0';
00407    return 0;
00408 }
00409 
00410 static struct ast_custom_function quote_function = {
00411    .name = "QUOTE",
00412    .synopsis = "Quotes a given string, escaping embedded quotes as necessary",
00413    .syntax = "QUOTE(<string>)",
00414    .read = quote,
00415 };
00416 
00417 
00418 static int len(struct ast_channel *chan, char *cmd, char *data, char *buf,
00419           size_t len)
00420 {
00421    int length = 0;
00422 
00423    if (data)
00424       length = strlen(data);
00425 
00426    snprintf(buf, len, "%d", length);
00427 
00428    return 0;
00429 }
00430 
00431 static struct ast_custom_function len_function = {
00432    .name = "LEN",
00433    .synopsis = "Returns the length of the argument given",
00434    .syntax = "LEN(<string>)",
00435    .read = len,
00436 };
00437 
00438 static int acf_strftime(struct ast_channel *chan, char *cmd, char *parse,
00439          char *buf, size_t len)
00440 {
00441    AST_DECLARE_APP_ARGS(args,
00442               AST_APP_ARG(epoch);
00443               AST_APP_ARG(timezone);
00444               AST_APP_ARG(format);
00445    );
00446    time_t epochi;
00447    struct tm tm;
00448 
00449    buf[0] = '\0';
00450 
00451    AST_STANDARD_APP_ARGS(args, parse);
00452 
00453    ast_get_time_t(args.epoch, &epochi, time(NULL), NULL);
00454    ast_localtime(&epochi, &tm, args.timezone);
00455 
00456    if (!args.format)
00457       args.format = "%c";
00458 
00459    if (!strftime(buf, len, args.format, &tm))
00460       ast_log(LOG_WARNING, "C function strftime() output nothing?!!\n");
00461 
00462    buf[len - 1] = '\0';
00463 
00464    return 0;
00465 }
00466 
00467 static struct ast_custom_function strftime_function = {
00468    .name = "STRFTIME",
00469    .synopsis = "Returns the current date/time in a specified format.",
00470    .syntax = "STRFTIME([<epoch>][|[timezone][|format]])",
00471    .read = acf_strftime,
00472 };
00473 
00474 static int acf_strptime(struct ast_channel *chan, char *cmd, char *data,
00475          char *buf, size_t len)
00476 {
00477    AST_DECLARE_APP_ARGS(args,
00478               AST_APP_ARG(timestring);
00479               AST_APP_ARG(timezone);
00480               AST_APP_ARG(format);
00481    );
00482    struct tm time;
00483 
00484    memset(&time, 0, sizeof(struct tm));
00485 
00486    buf[0] = '\0';
00487 
00488    if (!data) {
00489       ast_log(LOG_ERROR,
00490             "Asterisk function STRPTIME() requires an argument.\n");
00491       return -1;
00492    }
00493 
00494    AST_STANDARD_APP_ARGS(args, data);
00495 
00496    if (ast_strlen_zero(args.format)) {
00497       ast_log(LOG_ERROR,
00498             "No format supplied to STRPTIME(<timestring>|<timezone>|<format>)");
00499       return -1;
00500    }
00501 
00502    if (!strptime(args.timestring, args.format, &time)) {
00503       ast_log(LOG_WARNING, "C function strptime() output nothing?!!\n");
00504    } else {
00505       /* Since strptime(3) does not check DST, force ast_mktime() to calculate it. */
00506       time.tm_isdst = -1;
00507       snprintf(buf, len, "%d", (int) ast_mktime(&time, args.timezone));
00508    }
00509 
00510    return 0;
00511 }
00512 
00513 static struct ast_custom_function strptime_function = {
00514    .name = "STRPTIME",
00515    .synopsis =
00516       "Returns the epoch of the arbitrary date/time string structured as described in the format.",
00517    .syntax = "STRPTIME(<datetime>|<timezone>|<format>)",
00518    .desc =
00519       "This is useful for converting a date into an EPOCH time, possibly to pass to\n"
00520       "an application like SayUnixTime or to calculate the difference between two\n"
00521       "date strings.\n"
00522       "\n"
00523       "Example:\n"
00524       "  ${STRPTIME(2006-03-01 07:30:35|America/Chicago|%Y-%m-%d %H:%M:%S)} returns 1141219835\n",
00525    .read = acf_strptime,
00526 };
00527 
00528 static int function_eval(struct ast_channel *chan, char *cmd, char *data,
00529           char *buf, size_t len)
00530 {
00531    memset(buf, 0, len);
00532 
00533    if (ast_strlen_zero(data)) {
00534       ast_log(LOG_WARNING, "EVAL requires an argument: EVAL(<string>)\n");
00535       return -1;
00536    }
00537 
00538    pbx_substitute_variables_helper(chan, data, buf, len - 1);
00539 
00540    return 0;
00541 }
00542 
00543 static struct ast_custom_function eval_function = {
00544    .name = "EVAL",
00545    .synopsis = "Evaluate stored variables.",
00546    .syntax = "EVAL(<variable>)",
00547    .desc = "Using EVAL basically causes a string to be evaluated twice.\n"
00548       "When a variable or expression is in the dialplan, it will be\n"
00549       "evaluated at runtime. However, if the result of the evaluation\n"
00550       "is in fact a variable or expression, using EVAL will have it\n"
00551       "evaluated a second time. For example, if the variable ${MYVAR}\n"
00552       "contains \"${OTHERVAR}\", then the result of putting ${EVAL(${MYVAR})}\n"
00553       "in the dialplan will be the contents of the variable, OTHERVAR.\n"
00554       "Normally, by just putting ${MYVAR} in the dialplan, you would be\n"
00555       "left with \"${OTHERVAR}\".\n",
00556    .read = function_eval,
00557 };
00558 
00559 static int keypadhash(struct ast_channel *chan, char *cmd, char *data, char *buf, size_t len)
00560 {
00561    char *bufptr, *dataptr;
00562 
00563    for (bufptr = buf, dataptr = data; bufptr < buf + len - 1; dataptr++) {
00564       if (*dataptr == '\0') {
00565          *bufptr++ = '\0';
00566          break;
00567       } else if (*dataptr == '1') {
00568          *bufptr++ = '1';
00569       } else if (strchr("AaBbCc2", *dataptr)) {
00570          *bufptr++ = '2';
00571       } else if (strchr("DdEeFf3", *dataptr)) {
00572          *bufptr++ = '3';
00573       } else if (strchr("GgHhIi4", *dataptr)) {
00574          *bufptr++ = '4';
00575       } else if (strchr("JjKkLl5", *dataptr)) {
00576          *bufptr++ = '5';
00577       } else if (strchr("MmNnOo6", *dataptr)) {
00578          *bufptr++ = '6';
00579       } else if (strchr("PpQqRrSs7", *dataptr)) {
00580          *bufptr++ = '7';
00581       } else if (strchr("TtUuVv8", *dataptr)) {
00582          *bufptr++ = '8';
00583       } else if (strchr("WwXxYyZz9", *dataptr)) {
00584          *bufptr++ = '9';
00585       } else if (*dataptr == '*') {
00586          *bufptr++ = '*';
00587       } else if (*dataptr == '#') {
00588          *bufptr++ = '#';
00589       } else if (*dataptr == '0') {
00590          *bufptr++ = '0';
00591       }
00592    }
00593    buf[len - 1] = '\0';
00594 
00595    return 0;
00596 }
00597 
00598 static struct ast_custom_function keypadhash_function = {
00599    .name = "KEYPADHASH",
00600    .synopsis = "Hash the letters in the string into the equivalent keypad numbers.",
00601    .syntax = "KEYPADHASH(<string>)",
00602    .read = keypadhash,
00603    .desc = "Example:  ${KEYPADHASH(Les)} returns \"537\"\n",
00604 };
00605 
00606 static int function_tolower(struct ast_channel *chan, char *cmd, char *data, char *buf, size_t len)
00607 {
00608    int i = 0;
00609 
00610    while (data[i]) {
00611       if (data[i] & 0x80) {
00612          unsigned char c = data[i];
00613          if (c >= 192 && c <= 223) {
00614             c += 32;
00615             data[i] = (char) c;
00616          }
00617       } else {
00618          data[i] = tolower(data[i]);
00619       }
00620       i++;
00621    }
00622    ast_copy_string(buf, data, len);
00623    return 0;
00624 }
00625 
00626 static struct ast_custom_function tolower_function = {
00627    .name = "TOLOWER",
00628    .synopsis = "Returns the string in lowercase",
00629    .syntax = "TOLOWER(<string>)",
00630    .desc = "Using TOLOWER convert all ASCII characters from A to Z\n"
00631       "to lowercase a to z.\n",
00632    .read = function_tolower,
00633 };
00634 
00635 static int function_toupper(struct ast_channel *chan, char *cmd, char *data, char *buf, size_t len)
00636 {
00637    int i = 0;
00638    while (data[i]) {
00639       if (data[i] & 0x80) {
00640          unsigned char c = data[i];
00641          if (c >= 224) {
00642             c -= 32;
00643             data[i] = (char)c;
00644          }
00645       } else {
00646          data[i] = toupper(data[i]);
00647       }
00648       i++;
00649    }
00650    ast_copy_string(buf, data, len);
00651    return 0;
00652 }
00653 
00654 
00655 static struct ast_custom_function toupper_function = {
00656    .name = "TOUPPER",
00657    .synopsis = "Returns the string in uppercase",
00658    .syntax = "TOUPPER(<string>)",
00659    .desc = "Using TOUPPER convert all ASCII characters from a to z\n"
00660          "to uppercase A to Z.\n",
00661    .read = function_toupper,
00662 };
00663 
00664 static int unload_module(void)
00665 {
00666    int res = 0;
00667 
00668    res |= ast_custom_function_unregister(&fieldqty_function);
00669    res |= ast_custom_function_unregister(&filter_function);
00670    res |= ast_custom_function_unregister(&regex_function);
00671    res |= ast_custom_function_unregister(&array_function);
00672    res |= ast_custom_function_unregister(&quote_function);
00673    res |= ast_custom_function_unregister(&len_function);
00674    res |= ast_custom_function_unregister(&strftime_function);
00675    res |= ast_custom_function_unregister(&strptime_function);
00676    res |= ast_custom_function_unregister(&eval_function);
00677    res |= ast_custom_function_unregister(&keypadhash_function);
00678    res |= ast_custom_function_unregister(&tolower_function);
00679    res |= ast_custom_function_unregister(&toupper_function);
00680    res |= ast_custom_function_unregister(&sprintf_function);
00681 
00682    return res;
00683 }
00684 
00685 static int load_module(void)
00686 {
00687    int res = 0;
00688 
00689    res |= ast_custom_function_register(&fieldqty_function);
00690    res |= ast_custom_function_register(&filter_function);
00691    res |= ast_custom_function_register(&regex_function);
00692    res |= ast_custom_function_register(&array_function);
00693    res |= ast_custom_function_register(&quote_function);
00694    res |= ast_custom_function_register(&len_function);
00695    res |= ast_custom_function_register(&strftime_function);
00696    res |= ast_custom_function_register(&strptime_function);
00697    res |= ast_custom_function_register(&eval_function);
00698    res |= ast_custom_function_register(&keypadhash_function);
00699         res |= ast_custom_function_register(&tolower_function);
00700         res |= ast_custom_function_register(&toupper_function);
00701    res |= ast_custom_function_register(&sprintf_function);
00702 
00703    return res;
00704 }
00705 
00706 AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "String handling dialplan functions");

Generated on Fri Jul 3 20:35:38 2009 for Asterisk - the Open Source PBX by  doxygen 1.5.9