/* * Asterisk -- An open source telephony toolkit. * * Copyright (c) 2003-2006 Tilghman Lesher. All rights reserved. * * Tilghman Lesher * * This code is released by the author with no restrictions on usage. * * See http://www.asterisk.org for more information about * the Asterisk project. Please do not directly contact * any of the maintainers of this project for assistance; * the project provides a web site, mailing lists and IRC * channels for your use. * */ /*! \file * * \brief CUT function * * \author Tilghman Lesher * * \ingroup functions */ #include "asterisk.h" ASTERISK_FILE_VERSION(__FILE__, "$Revision$") #include "asterisk/file.h" #include "asterisk/channel.h" #include "asterisk/pbx.h" #include "asterisk/module.h" #include "asterisk/app.h" /*** DOCUMENTATION Sorts a list of key/vals into a list of keys, based upon the vals. Takes a comma-separated list of keys and values, each separated by a colon, and returns a comma-separated list of the keys, sorted by their values. Values will be evaluated as floating-point numbers. Slices and dices strings, based upon a named delimiter. Variable you want cut Delimiter, defaults to - Number of the field you want (1-based offset), may also be specified as a range (with -) or group of ranges and fields (with &) Cut out information from a string (varname), based upon a named delimiter. ***/ struct sortable_keys { char *key; float value; }; static int sort_subroutine(const void *arg1, const void *arg2) { const struct sortable_keys *one=arg1, *two=arg2; if (one->value < two->value) return -1; else if (one->value == two->value) return 0; else return 1; } #define ERROR_NOARG (-1) #define ERROR_NOMEM (-2) #define ERROR_USAGE (-3) static int sort_internal(struct ast_channel *chan, char *data, char *buffer, size_t buflen) { char *strings, *ptrkey, *ptrvalue; int count=1, count2, element_count=0; struct sortable_keys *sortable_keys; *buffer = '\0'; if (!data) return ERROR_NOARG; strings = ast_strdupa(data); for (ptrkey = strings; *ptrkey; ptrkey++) { if (*ptrkey == ',') count++; } sortable_keys = alloca(count * sizeof(struct sortable_keys)); memset(sortable_keys, 0, count * sizeof(struct sortable_keys)); /* Parse each into a struct */ count2 = 0; while ((ptrkey = strsep(&strings, ","))) { ptrvalue = strchr(ptrkey, ':'); if (!ptrvalue) { count--; continue; } *ptrvalue++ = '\0'; sortable_keys[count2].key = ptrkey; sscanf(ptrvalue, "%f", &sortable_keys[count2].value); count2++; } /* Sort the structs */ qsort(sortable_keys, count, sizeof(struct sortable_keys), sort_subroutine); for (count2 = 0; count2 < count; count2++) { int blen = strlen(buffer); if (element_count++) { strncat(buffer + blen, ",", buflen - blen - 1); blen++; } strncat(buffer + blen, sortable_keys[count2].key, buflen - blen - 1); } return 0; } static int cut_internal(struct ast_channel *chan, char *data, struct ast_str **buf, ssize_t buflen) { char *parse; size_t delim_consumed; AST_DECLARE_APP_ARGS(args, AST_APP_ARG(varname); AST_APP_ARG(delimiter); AST_APP_ARG(field); ); struct ast_str *str = ast_str_create(16); parse = ast_strdupa(data); AST_STANDARD_APP_ARGS(args, parse); /* Check and parse arguments */ if (args.argc < 3) { ast_free(str); return ERROR_NOARG; } else { char ds[2] = ""; char *tmp = alloca(strlen(args.varname) + 4); if (tmp) { snprintf(tmp, strlen(args.varname) + 4, "${%s}", args.varname); } else { ast_free(str); return ERROR_NOMEM; } if (ast_get_encoded_char(args.delimiter, ds, &delim_consumed)) ast_copy_string(ds, "-", sizeof(ds)); ast_str_substitute_variables(&str, 0, chan, tmp); if (ast_str_strlen(str)) { int curfieldnum = 1; char *tmp2 = ast_str_buffer(str); int firstfield = 1; while (tmp2 != NULL && args.field != NULL) { char *nextgroup = strsep(&(args.field), "&"); int num1 = 0, num2 = INT_MAX; char trashchar; if (sscanf(nextgroup, "%d-%d", &num1, &num2) == 2) { /* range with both start and end */ } else if (sscanf(nextgroup, "-%d", &num2) == 1) { /* range with end */ num1 = 0; } else if ((sscanf(nextgroup, "%d%c", &num1, &trashchar) == 2) && (trashchar == '-')) { /* range with start */ num2 = INT_MAX; } else if (sscanf(nextgroup, "%d", &num1) == 1) { /* single number */ num2 = num1; } else { ast_free(str); return ERROR_USAGE; } /* Get to start, if any */ if (num1 > 0) { while (tmp2 != NULL && curfieldnum < num1) { tmp2 = strchr(tmp2 + 1, ds[0]); curfieldnum++; } } /* Most frequent problem is the expectation of reordering fields */ if ((num1 > 0) && (curfieldnum > num1)) ast_log(LOG_WARNING, "We're already past the field you wanted?\n"); /* Output fields until we either run out of fields or num2 is reached */ while (tmp2 != NULL && curfieldnum <= num2) { char *tmp3 = strsep(&tmp2, ds); ast_str_append(buf, buflen, "%s%s", firstfield ? "" : ds, tmp3); firstfield = 0; curfieldnum++; } } } } ast_free(str); return 0; } static int acf_sort_exec(struct ast_channel *chan, const char *cmd, char *data, char *buf, size_t len) { int ret = -1; switch (sort_internal(chan, data, buf, len)) { case ERROR_NOARG: ast_log(LOG_ERROR, "SORT() requires an argument\n"); break; case ERROR_NOMEM: ast_log(LOG_ERROR, "Out of memory\n"); break; case 0: ret = 0; break; default: ast_log(LOG_ERROR, "Unknown internal error\n"); } return ret; } static int acf_cut_exec(struct ast_channel *chan, const char *cmd, char *data, char *buf, size_t len) { int ret = -1; struct ast_str *str = ast_str_create(16); switch (cut_internal(chan, data, &str, len)) { case ERROR_NOARG: ast_log(LOG_ERROR, "Syntax: CUT(,,) - missing argument!\n"); break; case ERROR_NOMEM: ast_log(LOG_ERROR, "Out of memory\n"); break; case ERROR_USAGE: ast_log(LOG_ERROR, "Usage: CUT(,,)\n"); break; case 0: ret = 0; ast_copy_string(buf, ast_str_buffer(str), len); break; default: ast_log(LOG_ERROR, "Unknown internal error\n"); } ast_free(str); return ret; } static int acf_cut_exec2(struct ast_channel *chan, const char *cmd, char *data, struct ast_str **buf, ssize_t len) { int ret = -1; switch (cut_internal(chan, data, buf, len)) { case ERROR_NOARG: ast_log(LOG_ERROR, "Syntax: CUT(,,) - missing argument!\n"); break; case ERROR_NOMEM: ast_log(LOG_ERROR, "Out of memory\n"); break; case ERROR_USAGE: ast_log(LOG_ERROR, "Usage: CUT(,,)\n"); break; case 0: ret = 0; break; default: ast_log(LOG_ERROR, "Unknown internal error\n"); } return ret; } static struct ast_custom_function acf_sort = { .name = "SORT", .read = acf_sort_exec, }; static struct ast_custom_function acf_cut = { .name = "CUT", .read = acf_cut_exec, .read2 = acf_cut_exec2, }; static int unload_module(void) { int res = 0; res |= ast_custom_function_unregister(&acf_cut); res |= ast_custom_function_unregister(&acf_sort); return res; } static int load_module(void) { int res = 0; res |= ast_custom_function_register(&acf_cut); res |= ast_custom_function_register(&acf_sort); return res; } AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "Cut out information from a string");