Asterisk - The Open Source Telephony Project GIT-master-8924258
All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Macros Modules Pages
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 <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
93 char *key;
94 float value;
95};
96
97static 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
112static 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;
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
163static 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;
169 AST_APP_ARG(varname);
170 AST_APP_ARG(delimiter);
171 AST_APP_ARG(field);
172 );
173
174 parse = ast_strdupa(data);
175
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
245static 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
266static 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;
284 break;
285 default:
286 ast_log(LOG_ERROR, "Unknown internal error\n");
287 }
288 ast_free(str);
289 return ret;
290}
291
292static 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
317 .name = "SORT",
318 .read = acf_sort_exec,
319};
320
322 .name = "CUT",
323 .read = acf_cut_exec,
324 .read2 = acf_cut_exec2,
325};
326
327static int unload_module(void)
328{
329 int res = 0;
330
333
334 return res;
335}
336
337static int load_module(void)
338{
339 int res = 0;
340
343
344 return res;
345}
346
347AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "Cut out information from a string");
const char * str
Definition: app_jack.c:150
char * strsep(char **str, const char *delims)
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:112
static int acf_sort_exec(struct ast_channel *chan, const char *cmd, char *data, char *buf, size_t len)
Definition: func_cut.c:245
#define ERROR_NOARG
Definition: func_cut.c:108
static struct ast_custom_function acf_cut
Definition: func_cut.c:321
static struct ast_custom_function acf_sort
Definition: func_cut.c:316
#define ERROR_NOMEM
Definition: func_cut.c:109
static int acf_cut_exec(struct ast_channel *chan, const char *cmd, char *data, char *buf, size_t len)
Definition: func_cut.c:266
static int sort_subroutine(const void *arg1, const void *arg2)
Definition: func_cut.c:97
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:292
static int load_module(void)
Definition: func_cut.c:337
static int unload_module(void)
Definition: func_cut.c:327
#define ERROR_USAGE
Definition: func_cut.c:110
static int cut_internal(struct ast_channel *chan, char *data, struct ast_str **buf, ssize_t buflen)
Definition: func_cut.c:163
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:3087
#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.
#define LOG_ERROR
#define LOG_WARNING
Asterisk module definitions.
#define AST_MODULE_INFO_STANDARD(keystr, desc)
Definition: module.h:581
#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:1559
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:94
char * key
Definition: func_cut.c:93
int value
Definition: syslog.c:37
const char * args