Asterisk - The Open Source Telephony Project GIT-master-27fb039
Loading...
Searching...
No Matches
Data Structures | Macros | Functions | Variables
func_cut.c File Reference

CUT function. More...

#include "asterisk.h"
#include "asterisk/file.h"
#include "asterisk/channel.h"
#include "asterisk/pbx.h"
#include "asterisk/module.h"
#include "asterisk/app.h"
Include dependency graph for func_cut.c:

Go to the source code of this file.

Data Structures

struct  sortable_keys
 

Macros

#define ERROR_NOARG   (-1)
 
#define ERROR_NOMEM   (-2)
 
#define ERROR_USAGE   (-3)
 

Functions

static void __reg_module (void)
 
static void __unreg_module (void)
 
static int acf_cut_exec (struct ast_channel *chan, const char *cmd, char *data, char *buf, size_t len)
 
static int acf_cut_exec2 (struct ast_channel *chan, const char *cmd, char *data, struct ast_str **buf, ssize_t len)
 
static int acf_sort_exec (struct ast_channel *chan, const char *cmd, char *data, char *buf, size_t len)
 
struct ast_moduleAST_MODULE_SELF_SYM (void)
 
static int cut_internal (struct ast_channel *chan, char *data, struct ast_str **buf, ssize_t buflen)
 
static int load_module (void)
 
static int sort_internal (struct ast_channel *chan, char *data, char *buffer, size_t buflen)
 
static int sort_subroutine (const void *arg1, const void *arg2)
 
static int unload_module (void)
 

Variables

static struct ast_module_info __mod_info = { .name = AST_MODULE, .flags = AST_MODFLAG_LOAD_ORDER , .description = "Cut out information from a string" , .key = ASTERISK_GPL_KEY , .buildopt_sum = AST_BUILDOPT_SUM, .load = load_module, .unload = unload_module, .load_pri = AST_MODPRI_DEFAULT, .support_level = AST_MODULE_SUPPORT_CORE, }
 
static struct ast_custom_function acf_cut
 
static struct ast_custom_function acf_sort
 
static const struct ast_module_infoast_module_info = &__mod_info
 

Detailed Description

CUT function.

Author
Tilghman Lesher app_c.nosp@m.ut__.nosp@m.v003@.nosp@m.the-.nosp@m.tilgh.nosp@m.man..nosp@m.com

Definition in file func_cut.c.

Macro Definition Documentation

◆ ERROR_NOARG

#define ERROR_NOARG   (-1)

Definition at line 108 of file func_cut.c.

◆ ERROR_NOMEM

#define ERROR_NOMEM   (-2)

Definition at line 109 of file func_cut.c.

◆ ERROR_USAGE

#define ERROR_USAGE   (-3)

Definition at line 110 of file func_cut.c.

Function Documentation

◆ __reg_module()

static void __reg_module ( void  )
static

Definition at line 347 of file func_cut.c.

◆ __unreg_module()

static void __unreg_module ( void  )
static

Definition at line 347 of file func_cut.c.

◆ acf_cut_exec()

static int acf_cut_exec ( struct ast_channel chan,
const char *  cmd,
char *  data,
char *  buf,
size_t  len 
)
static

Definition at line 266 of file func_cut.c.

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}
const char * str
Definition app_jack.c:150
#define ast_free(a)
Definition astmm.h:180
#define ast_log
Definition astobj2.c:42
char buf[BUFSIZE]
Definition eagi_proxy.c:66
#define ERROR_NOARG
Definition func_cut.c:108
#define ERROR_NOMEM
Definition func_cut.c:109
#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)
#define LOG_ERROR
#define ast_str_create(init_len)
Create a malloc'ed dynamic length string.
Definition strings.h:659
char *attribute_pure ast_str_buffer(const struct ast_str *buf)
Returns the string buffer within the ast_str buf.
Definition strings.h:761
void ast_copy_string(char *dst, const char *src, size_t size)
Size-limited null-terminating string copy.
Definition strings.h:425
Support for dynamic strings.
Definition strings.h:623

References ast_copy_string(), ast_free, ast_log, ast_str_buffer(), ast_str_create, buf, cut_internal(), ERROR_NOARG, ERROR_NOMEM, ERROR_USAGE, len(), LOG_ERROR, and str.

◆ acf_cut_exec2()

static int acf_cut_exec2 ( struct ast_channel chan,
const char *  cmd,
char *  data,
struct ast_str **  buf,
ssize_t  len 
)
static

Definition at line 292 of file func_cut.c.

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}

References ast_log, buf, cut_internal(), ERROR_NOARG, ERROR_NOMEM, ERROR_USAGE, len(), and LOG_ERROR.

◆ acf_sort_exec()

static int acf_sort_exec ( struct ast_channel chan,
const char *  cmd,
char *  data,
char *  buf,
size_t  len 
)
static

Definition at line 245 of file func_cut.c.

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}
static int sort_internal(struct ast_channel *chan, char *data, char *buffer, size_t buflen)
Definition func_cut.c:112

References ast_log, buf, ERROR_NOARG, ERROR_NOMEM, len(), LOG_ERROR, and sort_internal().

◆ AST_MODULE_SELF_SYM()

struct ast_module * AST_MODULE_SELF_SYM ( void  )

Definition at line 347 of file func_cut.c.

◆ cut_internal()

static int cut_internal ( struct ast_channel chan,
char *  data,
struct ast_str **  buf,
ssize_t  buflen 
)
static

Definition at line 163 of file func_cut.c.

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}
char * strsep(char **str, const char *delims)
#define ast_alloca(size)
call __builtin_alloca to ensure we get gcc builtin semantics
Definition astmm.h:288
#define ast_strdupa(s)
duplicate a string in memory from the stack
Definition astmm.h:298
#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_WARNING
void ast_str_substitute_variables(struct ast_str **buf, ssize_t maxlen, struct ast_channel *chan, const char *templ)
static struct @519 args
#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
size_t attribute_pure ast_str_strlen(const struct ast_str *buf)
Returns the current length of the string stored within buf.
Definition strings.h:730

References args, ast_alloca, AST_APP_ARG, ast_copy_string(), AST_DECLARE_APP_ARGS, ast_free, ast_get_encoded_char(), ast_log, AST_STANDARD_APP_ARGS, ast_str_append(), ast_str_buffer(), ast_str_create, ast_str_strlen(), ast_str_substitute_variables(), ast_strdupa, buf, ERROR_NOARG, ERROR_USAGE, LOG_WARNING, NULL, and strsep().

Referenced by acf_cut_exec(), and acf_cut_exec2().

◆ load_module()

static int load_module ( void  )
static

Definition at line 337 of file func_cut.c.

338{
339 int res = 0;
340
343
344 return res;
345}
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 ast_custom_function_register(acf)
Register a custom function.
Definition pbx.h:1562

References acf_cut, acf_sort, and ast_custom_function_register.

◆ sort_internal()

static int sort_internal ( struct ast_channel chan,
char *  data,
char *  buffer,
size_t  buflen 
)
static

Definition at line 112 of file func_cut.c.

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}
static int sort_subroutine(const void *arg1, const void *arg2)
Definition func_cut.c:97
char * key
Definition func_cut.c:93
int value
Definition syslog.c:37

References ast_alloca, ast_strdupa, ERROR_NOARG, sortable_keys::key, sort_subroutine(), strsep(), and value.

Referenced by acf_sort_exec().

◆ sort_subroutine()

static int sort_subroutine ( const void *  arg1,
const void *  arg2 
)
static

Definition at line 97 of file func_cut.c.

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}
float value
Definition func_cut.c:94

References sortable_keys::value.

Referenced by sort_internal().

◆ unload_module()

static int unload_module ( void  )
static

Definition at line 327 of file func_cut.c.

328{
329 int res = 0;
330
333
334 return res;
335}
int ast_custom_function_unregister(struct ast_custom_function *acf)
Unregister a custom function.

References acf_cut, acf_sort, and ast_custom_function_unregister().

Variable Documentation

◆ __mod_info

struct ast_module_info __mod_info = { .name = AST_MODULE, .flags = AST_MODFLAG_LOAD_ORDER , .description = "Cut out information from a string" , .key = ASTERISK_GPL_KEY , .buildopt_sum = AST_BUILDOPT_SUM, .load = load_module, .unload = unload_module, .load_pri = AST_MODPRI_DEFAULT, .support_level = AST_MODULE_SUPPORT_CORE, }
static

Definition at line 347 of file func_cut.c.

◆ acf_cut

struct ast_custom_function acf_cut
static
Initial value:
= {
.name = "CUT",
.read = acf_cut_exec,
.read2 = acf_cut_exec2,
}
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 acf_cut_exec2(struct ast_channel *chan, const char *cmd, char *data, struct ast_str **buf, ssize_t len)
Definition func_cut.c:292

Definition at line 321 of file func_cut.c.

321 {
322 .name = "CUT",
323 .read = acf_cut_exec,
324 .read2 = acf_cut_exec2,
325};

Referenced by load_module(), and unload_module().

◆ acf_sort

struct ast_custom_function acf_sort
static
Initial value:
= {
.name = "SORT",
.read = acf_sort_exec,
}
static int acf_sort_exec(struct ast_channel *chan, const char *cmd, char *data, char *buf, size_t len)
Definition func_cut.c:245

Definition at line 316 of file func_cut.c.

316 {
317 .name = "SORT",
318 .read = acf_sort_exec,
319};

Referenced by load_module(), and unload_module().

◆ ast_module_info

const struct ast_module_info* ast_module_info = &__mod_info
static

Definition at line 347 of file func_cut.c.