Asterisk - The Open Source Telephony Project GIT-master-7e7a603
func_cut.c
Go to the documentation of this file.
1/*
2 * Asterisk -- An open source telephony toolkit.
3 *
4 * Copyright (c) 2003-2006 Tilghman Lesher. All rights reserved.
5 *
6 * Tilghman Lesher <app_cut__v003@the-tilghman.com>
7 *
8 * This code is released by the author with no restrictions on usage.
9 *
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.
15 *
16 */
17
18/*! \file
19 *
20 * \brief CUT function
21 *
22 * \author Tilghman Lesher <app_cut__v003@the-tilghman.com>
23 *
24 * \ingroup functions
25 */
26
27/*** MODULEINFO
28 <support_level>core</support_level>
29 ***/
30
31#include "asterisk.h"
32
33#include "asterisk/file.h"
34#include "asterisk/channel.h"
35#include "asterisk/pbx.h"
36#include "asterisk/module.h"
37#include "asterisk/app.h"
38
39/*** DOCUMENTATION
40 <function name="SORT" language="en_US">
41 <synopsis>
42 Sorts a list of key/vals into a list of keys, based upon the vals.
43 </synopsis>
44 <syntax>
45 <parameter name="keyval" required="true" argsep=":">
46 <argument name="key1" required="true" />
47 <argument name="val1" required="true" />
48 </parameter>
49 <parameter name="keyvaln" multiple="true" argsep=":">
50 <argument name="key2" required="true" />
51 <argument name="val2" required="true" />
52 </parameter>
53 </syntax>
54 <description>
55 <para>Takes a comma-separated list of keys and values, each separated by a colon, and returns a
56 comma-separated list of the keys, sorted by their values. Values will be evaluated as
57 floating-point numbers.</para>
58 </description>
59 </function>
60 <function name="CUT" language="en_US">
61 <synopsis>
62 Slices and dices strings, based upon a named delimiter.
63 </synopsis>
64 <syntax>
65 <parameter name="varname" required="true">
66 <para>Variable you want cut</para>
67 </parameter>
68 <parameter name="char-delim" required="true">
69 <para>Delimiter, defaults to <literal>-</literal></para>
70 </parameter>
71 <parameter name="range-spec" required="true">
72 <para>Number of the field you want (1-based offset), may also be specified as a range (with <literal>-</literal>)
73 or group of ranges and fields (with <literal>&amp;</literal>)</para>
74 </parameter>
75 </syntax>
76 <description>
77 <para>Cut out information from a string (<replaceable>varname</replaceable>), based upon a named delimiter.</para>
78 <example title="The 'varname' parameter can only accept a variable name, not a variable expression">
79 exten => s,1,Set(foo=${CUT(bar,,2)}); This is correct syntax
80 exten => s,2,Set(foo=${CUT(${bar},,2)}); This is invalid syntax (unless bar contains the name of another variable)
81 </example>
82 </description>
83 </function>
84 ***/
85
87 char *key;
88 float value;
89};
90
91static int sort_subroutine(const void *arg1, const void *arg2)
92{
93 const struct sortable_keys *one=arg1, *two=arg2;
94 if (one->value < two->value)
95 return -1;
96 else if (one->value == two->value)
97 return 0;
98 else
99 return 1;
100}
101
102#define ERROR_NOARG (-1)
103#define ERROR_NOMEM (-2)
104#define ERROR_USAGE (-3)
105
106static int sort_internal(struct ast_channel *chan, char *data, char *buffer, size_t buflen)
107{
108 char *strings, *ptrkey, *ptrvalue;
109 int count=1, count2, element_count=0;
111
112 *buffer = '\0';
113
114 if (!data)
115 return ERROR_NOARG;
116
117 strings = ast_strdupa(data);
118
119 for (ptrkey = strings; *ptrkey; ptrkey++) {
120 if (*ptrkey == ',')
121 count++;
122 }
123
124 sortable_keys = ast_alloca(count * sizeof(struct sortable_keys));
125
126 memset(sortable_keys, 0, count * sizeof(struct sortable_keys));
127
128 /* Parse each into a struct */
129 count2 = 0;
130 while ((ptrkey = strsep(&strings, ","))) {
131 ptrvalue = strchr(ptrkey, ':');
132 if (!ptrvalue) {
133 count--;
134 continue;
135 }
136 *ptrvalue++ = '\0';
137 sortable_keys[count2].key = ptrkey;
138 sscanf(ptrvalue, "%30f", &sortable_keys[count2].value);
139 count2++;
140 }
141
142 /* Sort the structs */
143 qsort(sortable_keys, count, sizeof(struct sortable_keys), sort_subroutine);
144
145 for (count2 = 0; count2 < count; count2++) {
146 int blen = strlen(buffer);
147 if (element_count++) {
148 strncat(buffer + blen, ",", buflen - blen - 1);
149 blen++;
150 }
151 strncat(buffer + blen, sortable_keys[count2].key, buflen - blen - 1);
152 }
153
154 return 0;
155}
156
157static int cut_internal(struct ast_channel *chan, char *data, struct ast_str **buf, ssize_t buflen)
158{
159 char *parse, ds[2], *var_expr;
160 size_t delim_consumed;
161 struct ast_str *var_value;
163 AST_APP_ARG(varname);
164 AST_APP_ARG(delimiter);
165 AST_APP_ARG(field);
166 );
167
168 parse = ast_strdupa(data);
169
171
172 /* Check arguments */
173 if (args.argc < 3) {
174 return ERROR_NOARG;
175 }
176 var_expr = ast_alloca(strlen(args.varname) + 4);
177
178 /* Get the value of the variable named in the 1st argument */
179 snprintf(var_expr, strlen(args.varname) + 4, "${%s}", args.varname);
180 var_value = ast_str_create(16);
181 ast_str_substitute_variables(&var_value, 0, chan, var_expr);
182
183 /* Copy delimiter from 2nd argument to ds[] possibly decoding backslash escapes */
184 if (ast_get_encoded_char(args.delimiter, ds, &delim_consumed)) {
185 ast_copy_string(ds, "-", sizeof(ds));
186 }
187 ds[1] = '\0';
188
189 if (ast_str_strlen(var_value)) {
190 int curfieldnum = 1;
191 char *curfieldptr = ast_str_buffer(var_value);
192 int out_field_count = 0;
193
194 while (curfieldptr != NULL && args.field != NULL) {
195 char *next_range = strsep(&(args.field), "&");
196 int start_field, stop_field;
197 char trashchar;
198
199 if (sscanf(next_range, "%30d-%30d", &start_field, &stop_field) == 2) {
200 /* range with both start and end */
201 } else if (sscanf(next_range, "-%30d", &stop_field) == 1) {
202 /* range with end only */
203 start_field = 1;
204 } else if ((sscanf(next_range, "%30d%1c", &start_field, &trashchar) == 2) && (trashchar == '-')) {
205 /* range with start only */
206 stop_field = INT_MAX;
207 } else if (sscanf(next_range, "%30d", &start_field) == 1) {
208 /* single number */
209 stop_field = start_field;
210 } else {
211 /* invalid field spec */
212 ast_free(var_value);
213 return ERROR_USAGE;
214 }
215
216 /* Get to start, if not there already */
217 while (curfieldptr != NULL && curfieldnum < start_field) {
218 strsep(&curfieldptr, ds);
219 curfieldnum++;
220 }
221
222 /* Most frequent problem is the expectation of reordering fields */
223 if (curfieldnum > start_field) {
224 ast_log(LOG_WARNING, "We're already past the field you wanted?\n");
225 }
226
227 /* Output fields until we either run out of fields or stop_field is reached */
228 while (curfieldptr != NULL && curfieldnum <= stop_field) {
229 char *field_value = strsep(&curfieldptr, ds);
230 ast_str_append(buf, buflen, "%s%s", out_field_count++ ? ds : "", field_value);
231 curfieldnum++;
232 }
233 }
234 }
235 ast_free(var_value);
236 return 0;
237}
238
239static int acf_sort_exec(struct ast_channel *chan, const char *cmd, char *data, char *buf, size_t len)
240{
241 int ret = -1;
242
243 switch (sort_internal(chan, data, buf, len)) {
244 case ERROR_NOARG:
245 ast_log(LOG_ERROR, "SORT() requires an argument\n");
246 break;
247 case ERROR_NOMEM:
248 ast_log(LOG_ERROR, "Out of memory\n");
249 break;
250 case 0:
251 ret = 0;
252 break;
253 default:
254 ast_log(LOG_ERROR, "Unknown internal error\n");
255 }
256
257 return ret;
258}
259
260static int acf_cut_exec(struct ast_channel *chan, const char *cmd, char *data, char *buf, size_t len)
261{
262 int ret = -1;
263 struct ast_str *str = ast_str_create(16);
264
265 switch (cut_internal(chan, data, &str, len)) {
266 case ERROR_NOARG:
267 ast_log(LOG_ERROR, "Syntax: CUT(<varname>,<char-delim>,<range-spec>) - missing argument!\n");
268 break;
269 case ERROR_NOMEM:
270 ast_log(LOG_ERROR, "Out of memory\n");
271 break;
272 case ERROR_USAGE:
273 ast_log(LOG_ERROR, "Usage: CUT(<varname>,<char-delim>,<range-spec>)\n");
274 break;
275 case 0:
276 ret = 0;
278 break;
279 default:
280 ast_log(LOG_ERROR, "Unknown internal error\n");
281 }
282 ast_free(str);
283 return ret;
284}
285
286static int acf_cut_exec2(struct ast_channel *chan, const char *cmd, char *data, struct ast_str **buf, ssize_t len)
287{
288 int ret = -1;
289
290 switch (cut_internal(chan, data, buf, len)) {
291 case ERROR_NOARG:
292 ast_log(LOG_ERROR, "Syntax: CUT(<varname>,<char-delim>,<range-spec>) - missing argument!\n");
293 break;
294 case ERROR_NOMEM:
295 ast_log(LOG_ERROR, "Out of memory\n");
296 break;
297 case ERROR_USAGE:
298 ast_log(LOG_ERROR, "Usage: CUT(<varname>,<char-delim>,<range-spec>)\n");
299 break;
300 case 0:
301 ret = 0;
302 break;
303 default:
304 ast_log(LOG_ERROR, "Unknown internal error\n");
305 }
306
307 return ret;
308}
309
311 .name = "SORT",
312 .read = acf_sort_exec,
313};
314
316 .name = "CUT",
317 .read = acf_cut_exec,
318 .read2 = acf_cut_exec2,
319};
320
321static int unload_module(void)
322{
323 int res = 0;
324
327
328 return res;
329}
330
331static int load_module(void)
332{
333 int res = 0;
334
337
338 return res;
339}
340
341AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "Cut out information from a string");
const char * str
Definition: app_jack.c:147
Asterisk main include file. File version handling, generic pbx functions.
#define ast_alloca(size)
call __builtin_alloca to ensure we get gcc builtin semantics
Definition: astmm.h:288
#define ast_free(a)
Definition: astmm.h:180
#define ast_strdupa(s)
duplicate a string in memory from the stack
Definition: astmm.h:298
#define ast_log
Definition: astobj2.c:42
General Asterisk PBX channel definitions.
char buf[BUFSIZE]
Definition: eagi_proxy.c:66
Generic File Format Support. Should be included by clients of the file handling routines....
static int sort_internal(struct ast_channel *chan, char *data, char *buffer, size_t buflen)
Definition: func_cut.c:106
static int acf_sort_exec(struct ast_channel *chan, const char *cmd, char *data, char *buf, size_t len)
Definition: func_cut.c:239
#define ERROR_NOARG
Definition: func_cut.c:102
static struct ast_custom_function acf_cut
Definition: func_cut.c:315
static struct ast_custom_function acf_sort
Definition: func_cut.c:310
#define ERROR_NOMEM
Definition: func_cut.c:103
static int acf_cut_exec(struct ast_channel *chan, const char *cmd, char *data, char *buf, size_t len)
Definition: func_cut.c:260
static int sort_subroutine(const void *arg1, const void *arg2)
Definition: func_cut.c:91
static int acf_cut_exec2(struct ast_channel *chan, const char *cmd, char *data, struct ast_str **buf, ssize_t len)
Definition: func_cut.c:286
static int load_module(void)
Definition: func_cut.c:331
static int unload_module(void)
Definition: func_cut.c:321
#define ERROR_USAGE
Definition: func_cut.c:104
static int cut_internal(struct ast_channel *chan, char *data, struct ast_str **buf, ssize_t buflen)
Definition: func_cut.c:157
static int len(struct ast_channel *chan, const char *cmd, char *data, char *buf, size_t buflen)
Application convenience functions, designed to give consistent look and feel to Asterisk apps.
#define AST_APP_ARG(name)
Define an application argument.
int ast_get_encoded_char(const char *stream, char *result, size_t *consumed)
Decode an encoded control or extended ASCII character.
Definition: main/app.c:3077
#define AST_DECLARE_APP_ARGS(name, arglist)
Declare a structure to hold an application's arguments.
#define AST_STANDARD_APP_ARGS(args, parse)
Performs the 'standard' argument separation process for an application.
char * strsep(char **str, const char *delims)
#define LOG_ERROR
#define LOG_WARNING
Asterisk module definitions.
#define AST_MODULE_INFO_STANDARD(keystr, desc)
Definition: module.h:567
#define ASTERISK_GPL_KEY
The text the key() function should return.
Definition: module.h:46
Core PBX routines and definitions.
void ast_str_substitute_variables(struct ast_str **buf, ssize_t maxlen, struct ast_channel *chan, const char *templ)
#define ast_custom_function_register(acf)
Register a custom function.
Definition: pbx.h:1558
int ast_custom_function_unregister(struct ast_custom_function *acf)
Unregister a custom function.
#define NULL
Definition: resample.c:96
int ast_str_append(struct ast_str **buf, ssize_t max_len, const char *fmt,...)
Append to a thread local dynamic string.
Definition: strings.h:1139
char * ast_str_buffer(const struct ast_str *buf)
Returns the string buffer within the ast_str buf.
Definition: strings.h:761
#define ast_str_create(init_len)
Create a malloc'ed dynamic length string.
Definition: strings.h:659
size_t ast_str_strlen(const struct ast_str *buf)
Returns the current length of the string stored within buf.
Definition: strings.h:730
void ast_copy_string(char *dst, const char *src, size_t size)
Size-limited null-terminating string copy.
Definition: strings.h:425
Main Channel structure associated with a channel.
Data structure associated with a custom dialplan function.
Definition: pbx.h:118
const char * name
Definition: pbx.h:119
Support for dynamic strings.
Definition: strings.h:623
float value
Definition: func_cut.c:88
char * key
Definition: func_cut.c:87
int value
Definition: syslog.c:37
const char * args