Debian Codegraph @

funcs/func_cut.c

raw symbols SCIP
1 /*
2 * Asterisk -- An open source telephony toolkit.
3 *
4 * Copyright (c) 2003-2006 Tilghman Lesher. All rights reserved.
5 *
6 * Tilghman Lesher <[email protected]>
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 <[email protected]>
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 <since>
42 <version>1.2.0</version>
43 </since>
44 <synopsis>
45 Sorts a list of key/vals into a list of keys, based upon the vals.
46 </synopsis>
47 <syntax>
48 <parameter name="keyval" required="true" argsep=":">
49 <argument name="key1" required="true" />
50 <argument name="val1" required="true" />
51 </parameter>
52 <parameter name="keyvaln" multiple="true" argsep=":">
53 <argument name="key2" required="true" />
54 <argument name="val2" required="true" />
55 </parameter>
56 </syntax>
57 <description>
58 <para>Takes a comma-separated list of keys and values, each separated by a colon, and returns a
59 comma-separated list of the keys, sorted by their values. Values will be evaluated as
60 floating-point numbers.</para>
61 </description>
62 </function>
63 <function name="CUT" language="en_US">
64 <since>
65 <version>1.2.0</version>
66 </since>
67 <synopsis>
68 Slices and dices strings, based upon a named delimiter.
69 </synopsis>
70 <syntax>
71 <parameter name="varname" required="true">
72 <para>Variable you want cut</para>
73 </parameter>
74 <parameter name="char-delim" required="true">
75 <para>Delimiter, defaults to <literal>-</literal></para>
76 </parameter>
77 <parameter name="range-spec" required="true">
78 <para>Number of the field you want (1-based offset), may also be specified as a range (with <literal>-</literal>)
79 or group of ranges and fields (with <literal>&amp;</literal>)</para>
80 </parameter>
81 </syntax>
82 <description>
83 <para>Cut out information from a string (<replaceable>varname</replaceable>), based upon a named delimiter.</para>
84 <example title="The 'varname' parameter can only accept a variable name, not a variable expression">
85 exten => s,1,Set(foo=${CUT(bar,,2)}); This is correct syntax
86 exten => s,2,Set(foo=${CUT(${bar},,2)}); This is invalid syntax (unless bar contains the name of another variable)
87 </example>
88 </description>
89 </function>
90 ***/
91
92 struct sortable_keys {
93 char *key;
94 float value;
95 };
96
97 static int sort_subroutine(const void *arg1, const void *arg2)
98 {
99 const struct sortable_keys *one=arg1, *two=arg2;
100 if (one->value < two->value)
101 return -1;
102 else if (one->value == two->value)
103 return 0;
104 else
105 return 1;
106 }
107
108 #define ERROR_NOARG (-1)
109 #define ERROR_NOMEM (-2)
110 #define ERROR_USAGE (-3)
111
112 static int sort_internal(struct ast_channel *chan, char *data, char *buffer, size_t buflen)
113 {
114 char *strings, *ptrkey, *ptrvalue;
115 int count=1, count2, element_count=0;
116 struct sortable_keys *sortable_keys;
117
118 *buffer = '\0';
119
120 if (!data)
121 return ERROR_NOARG;
122
123 strings = ast_strdupa(data);
124
125 for (ptrkey = strings; *ptrkey; ptrkey++) {
126 if (*ptrkey == ',')
127 count++;
128 }
129
130 sortable_keys = ast_alloca(count * sizeof(struct sortable_keys));
131
132 memset(sortable_keys, 0, count * sizeof(struct sortable_keys));
133
134 /* Parse each into a struct */
135 count2 = 0;
136 while ((ptrkey = strsep(&strings, ","))) {
137 ptrvalue = strchr(ptrkey, ':');
138 if (!ptrvalue) {
139 count--;
140 continue;
141 }
142 *ptrvalue++ = '\0';
143 sortable_keys[count2].key = ptrkey;
144 sscanf(ptrvalue, "%30f", &sortable_keys[count2].value);
145 count2++;
146 }
147
148 /* Sort the structs */
149 qsort(sortable_keys, count, sizeof(struct sortable_keys), sort_subroutine);
150
151 for (count2 = 0; count2 < count; count2++) {
152 int blen = strlen(buffer);
153 if (element_count++) {
154 strncat(buffer + blen, ",", buflen - blen - 1);
155 blen++;
156 }
157 strncat(buffer + blen, sortable_keys[count2].key, buflen - blen - 1);
158 }
159
160 return 0;
161 }
162
163 static int cut_internal(struct ast_channel *chan, char *data, struct ast_str **buf, ssize_t buflen)
164 {
165 char *parse, ds[2], *var_expr;
166 size_t delim_consumed;
167 struct ast_str *var_value;
168 AST_DECLARE_APP_ARGS(args,
169 AST_APP_ARG(varname);
170 AST_APP_ARG(delimiter);
171 AST_APP_ARG(field);
172 );
173
174 parse = ast_strdupa(data);
175
176 AST_STANDARD_APP_ARGS(args, parse);
177
178 /* Check arguments */
179 if (args.argc < 3) {
180 return ERROR_NOARG;
181 }
182 var_expr = ast_alloca(strlen(args.varname) + 4);
183
184 /* Get the value of the variable named in the 1st argument */
185 snprintf(var_expr, strlen(args.varname) + 4, "${%s}", args.varname);
186 var_value = ast_str_create(16);
187 ast_str_substitute_variables(&var_value, 0, chan, var_expr);
188
189 /* Copy delimiter from 2nd argument to ds[] possibly decoding backslash escapes */
190 if (ast_get_encoded_char(args.delimiter, ds, &delim_consumed)) {
191 ast_copy_string(ds, "-", sizeof(ds));
192 }
193 ds[1] = '\0';
194
195 if (ast_str_strlen(var_value)) {
196 int curfieldnum = 1;
197 char *curfieldptr = ast_str_buffer(var_value);
198 int out_field_count = 0;
199
200 while (curfieldptr != NULL && args.field != NULL) {
201 char *next_range = strsep(&(args.field), "&");
202 int start_field, stop_field;
203 char trashchar;
204
205 if (sscanf(next_range, "%30d-%30d", &start_field, &stop_field) == 2) {
206 /* range with both start and end */
207 } else if (sscanf(next_range, "-%30d", &stop_field) == 1) {
208 /* range with end only */
209 start_field = 1;
210 } else if ((sscanf(next_range, "%30d%1c", &start_field, &trashchar) == 2) && (trashchar == '-')) {
211 /* range with start only */
212 stop_field = INT_MAX;
213 } else if (sscanf(next_range, "%30d", &start_field) == 1) {
214 /* single number */
215 stop_field = start_field;
216 } else {
217 /* invalid field spec */
218 ast_free(var_value);
219 return ERROR_USAGE;
220 }
221
222 /* Get to start, if not there already */
223 while (curfieldptr != NULL && curfieldnum < start_field) {
224 strsep(&curfieldptr, ds);
225 curfieldnum++;
226 }
227
228 /* Most frequent problem is the expectation of reordering fields */
229 if (curfieldnum > start_field) {
230 ast_log(LOG_WARNING, "We're already past the field you wanted?\n");
231 }
232
233 /* Output fields until we either run out of fields or stop_field is reached */
234 while (curfieldptr != NULL && curfieldnum <= stop_field) {
235 char *field_value = strsep(&curfieldptr, ds);
236 ast_str_append(buf, buflen, "%s%s", out_field_count++ ? ds : "", field_value);
237 curfieldnum++;
238 }
239 }
240 }
241 ast_free(var_value);
242 return 0;
243 }
244
245 static int acf_sort_exec(struct ast_channel *chan, const char *cmd, char *data, char *buf, size_t len)
246 {
247 int ret = -1;
248
249 switch (sort_internal(chan, data, buf, len)) {
250 case ERROR_NOARG:
251 ast_log(LOG_ERROR, "SORT() requires an argument\n");
252 break;
253 case ERROR_NOMEM:
254 ast_log(LOG_ERROR, "Out of memory\n");
255 break;
256 case 0:
257 ret = 0;
258 break;
259 default:
260 ast_log(LOG_ERROR, "Unknown internal error\n");
261 }
262
263 return ret;
264 }
265
266 static int acf_cut_exec(struct ast_channel *chan, const char *cmd, char *data, char *buf, size_t len)
267 {
268 int ret = -1;
269 struct ast_str *str = ast_str_create(16);
270
271 switch (cut_internal(chan, data, &str, len)) {
272 case ERROR_NOARG:
273 ast_log(LOG_ERROR, "Syntax: CUT(<varname>,<char-delim>,<range-spec>) - missing argument!\n");
274 break;
275 case ERROR_NOMEM:
276 ast_log(LOG_ERROR, "Out of memory\n");
277 break;
278 case ERROR_USAGE:
279 ast_log(LOG_ERROR, "Usage: CUT(<varname>,<char-delim>,<range-spec>)\n");
280 break;
281 case 0:
282 ret = 0;
283 ast_copy_string(buf, ast_str_buffer(str), len);
284 break;
285 default:
286 ast_log(LOG_ERROR, "Unknown internal error\n");
287 }
288 ast_free(str);
289 return ret;
290 }
291
292 static int acf_cut_exec2(struct ast_channel *chan, const char *cmd, char *data, struct ast_str **buf, ssize_t len)
293 {
294 int ret = -1;
295
296 switch (cut_internal(chan, data, buf, len)) {
297 case ERROR_NOARG:
298 ast_log(LOG_ERROR, "Syntax: CUT(<varname>,<char-delim>,<range-spec>) - missing argument!\n");
299 break;
300 case ERROR_NOMEM:
301 ast_log(LOG_ERROR, "Out of memory\n");
302 break;
303 case ERROR_USAGE:
304 ast_log(LOG_ERROR, "Usage: CUT(<varname>,<char-delim>,<range-spec>)\n");
305 break;
306 case 0:
307 ret = 0;
308 break;
309 default:
310 ast_log(LOG_ERROR, "Unknown internal error\n");
311 }
312
313 return ret;
314 }
315
316 static struct ast_custom_function acf_sort = {
317 .name = "SORT",
318 .read = acf_sort_exec,
319 };
320
321 static struct ast_custom_function acf_cut = {
322 .name = "CUT",
323 .read = acf_cut_exec,
324 .read2 = acf_cut_exec2,
325 };
326
327 static int unload_module(void)
328 {
329 int res = 0;
330
331 res |= ast_custom_function_unregister(&acf_cut);
332 res |= ast_custom_function_unregister(&acf_sort);
333
334 return res;
335 }
336
337 static int load_module(void)
338 {
339 int res = 0;
340
341 res |= ast_custom_function_register(&acf_cut);
342 res |= ast_custom_function_register(&acf_sort);
343
344 return res;
345 }
346
347 AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "Cut out information from a string");