2 * Asterisk -- An open source telephony toolkit.
4 * Copyright (c) 2003 Tilghman Lesher. All rights reserved.
6 * Tilghman Lesher <app_cut__v003@the-tilghman.com>
8 * This code is released by the author with no restrictions on usage.
10 * See http://www.asterisk.org for more information about
11 * the Asterisk project. Please do not directly contact
12 * any of the maintainers of this project for assistance;
13 * the project provides a web site, mailing lists and IRC
14 * channels for your use.
19 * \brief Cut application
21 * \ingroup applications
31 ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
33 #include "asterisk/file.h"
34 #include "asterisk/logger.h"
35 #include "asterisk/options.h"
36 #include "asterisk/channel.h"
37 #include "asterisk/pbx.h"
38 #include "asterisk/module.h"
39 #include "asterisk/app.h"
41 /* Maximum length of any variable */
42 #define MAXRESULT 1024
44 static char *tdesc = "Cut out information from a string";
50 struct sortable_keys {
55 static int sort_subroutine(const void *arg1, const void *arg2)
57 const struct sortable_keys *one=arg1, *two=arg2;
58 if (one->value < two->value) {
60 } else if (one->value == two->value) {
67 #define ERROR_NOARG (-1)
68 #define ERROR_NOMEM (-2)
69 #define ERROR_USAGE (-3)
71 static int sort_internal(struct ast_channel *chan, char *data, char *buffer, size_t buflen)
73 char *strings, *ptrkey, *ptrvalue;
74 int count=1, count2, element_count=0;
75 struct sortable_keys *sortable_keys;
77 memset(buffer, 0, buflen);
83 strings = ast_strdupa((char *)data);
88 for (ptrkey = strings; *ptrkey; ptrkey++) {
94 sortable_keys = alloca(count * sizeof(struct sortable_keys));
99 memset(sortable_keys, 0, count * sizeof(struct sortable_keys));
101 /* Parse each into a struct */
103 while ((ptrkey = strsep(&strings, "|"))) {
104 ptrvalue = index(ptrkey, ':');
111 sortable_keys[count2].key = ptrkey;
112 sscanf(ptrvalue, "%f", &sortable_keys[count2].value);
116 /* Sort the structs */
117 qsort(sortable_keys, count, sizeof(struct sortable_keys), sort_subroutine);
119 for (count2 = 0; count2 < count; count2++) {
120 int blen = strlen(buffer);
121 if (element_count++) {
122 strncat(buffer + blen, ",", buflen - blen - 1);
124 strncat(buffer + blen + 1, sortable_keys[count2].key, buflen - blen - 2);
130 static int cut_internal(struct ast_channel *chan, char *data, char *buffer, size_t buflen)
132 char *s, *args[3], *varname=NULL, *delimiter=NULL, *field=NULL;
135 memset(buffer, 0, buflen);
137 /* Check and parse arguments */
139 s = ast_strdupa((char *)data);
141 ast_app_separate_args(s, '|', args, 3);
156 char *tmp = alloca(strlen(varname) + 4);
157 char varvalue[MAXRESULT], *tmp2=varvalue;
160 snprintf(tmp, strlen(varname) + 4, "${%s}", varname);
161 memset(varvalue, 0, sizeof(varvalue));
171 /* String form of the delimiter, for use with strsep(3) */
172 snprintf(ds, sizeof(ds), "%c", d);
174 pbx_substitute_variables_helper(chan, tmp, tmp2, MAXRESULT - 1);
178 while ((tmp2 != NULL) && (field != NULL)) {
179 char *nextgroup = strsep(&field, "&");
180 int num1 = 0, num2 = MAXRESULT;
183 if (sscanf(nextgroup, "%d-%d", &num1, &num2) == 2) {
184 /* range with both start and end */
185 } else if (sscanf(nextgroup, "-%d", &num2) == 1) {
188 } else if ((sscanf(nextgroup, "%d%c", &num1, &trashchar) == 2) && (trashchar == '-')) {
189 /* range with start */
191 } else if (sscanf(nextgroup, "%d", &num1) == 1) {
198 /* Get to start, if any */
200 while ((tmp2 != (char *)NULL + 1) && (curfieldnum < num1)) {
201 tmp2 = index(tmp2, d) + 1;
206 /* Most frequent problem is the expectation of reordering fields */
207 if ((num1 > 0) && (curfieldnum > num1)) {
208 ast_log(LOG_WARNING, "We're already past the field you wanted?\n");
211 /* Re-null tmp2 if we added 1 to NULL */
212 if (tmp2 == (char *)NULL + 1)
215 /* Output fields until we either run out of fields or num2 is reached */
216 while ((tmp2 != NULL) && (curfieldnum <= num2)) {
217 char *tmp3 = strsep(&tmp2, ds);
218 int curlen = strlen(buffer);
221 snprintf(buffer + curlen, buflen - curlen, "%c%s", d, tmp3);
223 snprintf(buffer, buflen, "%s", tmp3);
236 static char *acf_sort_exec(struct ast_channel *chan, char *cmd, char *data, char *buf, size_t len)
240 LOCAL_USER_ACF_ADD(u);
242 switch (sort_internal(chan, data, buf, len)) {
244 ast_log(LOG_ERROR, "SORT() requires an argument\n");
247 ast_log(LOG_ERROR, "Out of memory\n");
252 ast_log(LOG_ERROR, "Unknown internal error\n");
254 LOCAL_USER_REMOVE(u);
258 static char *acf_cut_exec(struct ast_channel *chan, char *cmd, char *data, char *buf, size_t len)
262 LOCAL_USER_ACF_ADD(u);
264 switch (cut_internal(chan, data, buf, len)) {
266 ast_log(LOG_ERROR, "CUT() requires an argument\n");
269 ast_log(LOG_ERROR, "Out of memory\n");
272 ast_log(LOG_ERROR, "Usage: %s\n", cut_synopsis);
277 ast_log(LOG_ERROR, "Unknown internal error\n");
279 LOCAL_USER_REMOVE(u);
283 struct ast_custom_function acf_sort = {
285 .synopsis = "Sorts a list of key/vals into a list of keys, based upon the vals",
286 .syntax = "SORT(key1:val1[...][,keyN:valN])",
288 "Takes a comma-separated list of keys and values, each separated by a colon, and returns a\n"
289 "comma-separated list of the keys, sorted by their values. Values will be evaluated as\n"
290 "floating-point numbers.\n",
291 .read = acf_sort_exec,
294 struct ast_custom_function acf_cut = {
296 .synopsis = "Slices and dices strings, based upon a named delimiter.",
297 .syntax = "CUT(<varname>,<char-delim>,<range-spec>)",
299 " varname - variable you want cut\n"
300 " char-delim - defaults to '-'\n"
301 " range-spec - number of the field you want (1-based offset)\n"
302 " may also be specified as a range (with -)\n"
303 " or group of ranges and fields (with &)\n",
304 .read = acf_cut_exec,
307 int unload_module(void)
311 res = ast_custom_function_unregister(&acf_cut);
312 res |= ast_custom_function_unregister(&acf_sort);
314 STANDARD_HANGUP_LOCALUSERS;
319 int load_module(void)
323 res = ast_custom_function_register(&acf_cut);
324 res |= ast_custom_function_register(&acf_sort);
329 char *description(void)
337 STANDARD_USECOUNT(res);
343 return ASTERISK_GPL_KEY;