2 * Asterisk -- An open source telephony toolkit.
4 * Copyright (c) 2003-2006 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.
22 * \author Tilghman Lesher <app_cut__v003@the-tilghman.com>
29 ASTERISK_FILE_VERSION(__FILE__
, "$Revision$")
36 #include "asterisk/file.h"
37 #include "asterisk/logger.h"
38 #include "asterisk/options.h"
39 #include "asterisk/channel.h"
40 #include "asterisk/pbx.h"
41 #include "asterisk/module.h"
42 #include "asterisk/app.h"
44 /* Maximum length of any variable */
45 #define MAXRESULT 1024
47 struct sortable_keys
{
52 static int sort_subroutine(const void *arg1
, const void *arg2
)
54 const struct sortable_keys
*one
=arg1
, *two
=arg2
;
55 if (one
->value
< two
->value
)
57 else if (one
->value
== two
->value
)
63 #define ERROR_NOARG (-1)
64 #define ERROR_NOMEM (-2)
65 #define ERROR_USAGE (-3)
67 static int sort_internal(struct ast_channel
*chan
, char *data
, char *buffer
, size_t buflen
)
69 char *strings
, *ptrkey
, *ptrvalue
;
70 int count
=1, count2
, element_count
=0;
71 struct sortable_keys
*sortable_keys
;
73 memset(buffer
, 0, buflen
);
78 strings
= ast_strdupa(data
);
80 for (ptrkey
= strings
; *ptrkey
; ptrkey
++) {
85 sortable_keys
= alloca(count
* sizeof(struct sortable_keys
));
87 memset(sortable_keys
, 0, count
* sizeof(struct sortable_keys
));
89 /* Parse each into a struct */
91 while ((ptrkey
= strsep(&strings
, "|"))) {
92 ptrvalue
= index(ptrkey
, ':');
98 sortable_keys
[count2
].key
= ptrkey
;
99 sscanf(ptrvalue
, "%f", &sortable_keys
[count2
].value
);
103 /* Sort the structs */
104 qsort(sortable_keys
, count
, sizeof(struct sortable_keys
), sort_subroutine
);
106 for (count2
= 0; count2
< count
; count2
++) {
107 int blen
= strlen(buffer
);
108 if (element_count
++) {
109 strncat(buffer
+ blen
, ",", buflen
- blen
- 1);
112 strncat(buffer
+ blen
, sortable_keys
[count2
].key
, buflen
- blen
- 1);
118 static int cut_internal(struct ast_channel
*chan
, char *data
, char *buffer
, size_t buflen
)
121 AST_DECLARE_APP_ARGS(args
,
122 AST_APP_ARG(varname
);
123 AST_APP_ARG(delimiter
);
127 memset(buffer
, 0, buflen
);
129 parse
= ast_strdupa(data
);
131 AST_STANDARD_APP_ARGS(args
, parse
);
133 /* Check and parse arguments */
138 char *tmp
= alloca(strlen(args
.varname
) + 4);
139 char varvalue
[MAXRESULT
], *tmp2
=varvalue
;
142 snprintf(tmp
, strlen(args
.varname
) + 4, "${%s}", args
.varname
);
143 memset(varvalue
, 0, sizeof(varvalue
));
148 d
= args
.delimiter
[0] ? args
.delimiter
[0] : '-';
150 /* String form of the delimiter, for use with strsep(3) */
151 snprintf(ds
, sizeof(ds
), "%c", d
);
153 pbx_substitute_variables_helper(chan
, tmp
, tmp2
, MAXRESULT
- 1);
157 while (tmp2
!= NULL
&& args
.field
!= NULL
) {
158 char *nextgroup
= strsep(&(args
.field
), "&");
159 int num1
= 0, num2
= MAXRESULT
;
162 if (sscanf(nextgroup
, "%d-%d", &num1
, &num2
) == 2) {
163 /* range with both start and end */
164 } else if (sscanf(nextgroup
, "-%d", &num2
) == 1) {
167 } else if ((sscanf(nextgroup
, "%d%c", &num1
, &trashchar
) == 2) && (trashchar
== '-')) {
168 /* range with start */
170 } else if (sscanf(nextgroup
, "%d", &num1
) == 1) {
177 /* Get to start, if any */
179 while (tmp2
!= (char *)NULL
+ 1 && curfieldnum
< num1
) {
180 tmp2
= index(tmp2
, d
) + 1;
185 /* Most frequent problem is the expectation of reordering fields */
186 if ((num1
> 0) && (curfieldnum
> num1
))
187 ast_log(LOG_WARNING
, "We're already past the field you wanted?\n");
189 /* Re-null tmp2 if we added 1 to NULL */
190 if (tmp2
== (char *)NULL
+ 1)
193 /* Output fields until we either run out of fields or num2 is reached */
194 while (tmp2
!= NULL
&& curfieldnum
<= num2
) {
195 char *tmp3
= strsep(&tmp2
, ds
);
196 int curlen
= strlen(buffer
);
199 snprintf(buffer
+ curlen
, buflen
- curlen
, "%c%s", d
, tmp3
);
201 snprintf(buffer
, buflen
, "%s", tmp3
);
211 static int acf_sort_exec(struct ast_channel
*chan
, char *cmd
, char *data
, char *buf
, size_t len
)
213 struct ast_module_user
*u
;
216 u
= ast_module_user_add(chan
);
218 switch (sort_internal(chan
, data
, buf
, len
)) {
220 ast_log(LOG_ERROR
, "SORT() requires an argument\n");
223 ast_log(LOG_ERROR
, "Out of memory\n");
229 ast_log(LOG_ERROR
, "Unknown internal error\n");
232 ast_module_user_remove(u
);
237 static int acf_cut_exec(struct ast_channel
*chan
, char *cmd
, char *data
, char *buf
, size_t len
)
240 struct ast_module_user
*u
;
242 u
= ast_module_user_add(chan
);
244 switch (cut_internal(chan
, data
, buf
, len
)) {
246 ast_log(LOG_ERROR
, "Syntax: CUT(<varname>,<char-delim>,<range-spec>) - missing argument!\n");
249 ast_log(LOG_ERROR
, "Out of memory\n");
252 ast_log(LOG_ERROR
, "Usage: CUT(<varname>,<char-delim>,<range-spec>)\n");
258 ast_log(LOG_ERROR
, "Unknown internal error\n");
261 ast_module_user_remove(u
);
266 struct ast_custom_function acf_sort
= {
268 .synopsis
= "Sorts a list of key/vals into a list of keys, based upon the vals",
269 .syntax
= "SORT(key1:val1[...][,keyN:valN])",
271 "Takes a comma-separated list of keys and values, each separated by a colon, and returns a\n"
272 "comma-separated list of the keys, sorted by their values. Values will be evaluated as\n"
273 "floating-point numbers.\n",
274 .read
= acf_sort_exec
,
277 struct ast_custom_function acf_cut
= {
279 .synopsis
= "Slices and dices strings, based upon a named delimiter.",
280 .syntax
= "CUT(<varname>,<char-delim>,<range-spec>)",
282 " varname - variable you want cut\n"
283 " char-delim - defaults to '-'\n"
284 " range-spec - number of the field you want (1-based offset)\n"
285 " may also be specified as a range (with -)\n"
286 " or group of ranges and fields (with &)\n",
287 .read
= acf_cut_exec
,
290 static int unload_module(void)
294 res
|= ast_custom_function_unregister(&acf_cut
);
295 res
|= ast_custom_function_unregister(&acf_sort
);
297 ast_module_user_hangup_all();
302 static int load_module(void)
306 res
|= ast_custom_function_register(&acf_cut
);
307 res
|= ast_custom_function_register(&acf_sort
);
312 AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY
, "Cut out information from a string");