Asterisk - The Open Source Telephony Project GIT-master-7e7a603
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 = "This paragraph is copyright (c) 2006 by Digium, Inc. \In order for your module to load, it must return this \key via a function called \"key\". Any code which \includes this paragraph must be licensed under the GNU \General Public License version 2 or later (at your \option). In addition to Digium's general reservations \of rights, Digium expressly reserves the right to \allow other parties to license this paragraph under \different terms. Any use of Digium, Inc. trademarks or \logos (including \"Asterisk\" or \"Digium\") without \express written permission of Digium, Inc. is prohibited.\n" , .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 102 of file func_cut.c.

◆ ERROR_NOMEM

#define ERROR_NOMEM   (-2)

Definition at line 103 of file func_cut.c.

◆ ERROR_USAGE

#define ERROR_USAGE   (-3)

Definition at line 104 of file func_cut.c.

Function Documentation

◆ __reg_module()

static void __reg_module ( void  )
static

Definition at line 341 of file func_cut.c.

◆ __unreg_module()

static void __unreg_module ( void  )
static

Definition at line 341 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 260 of file func_cut.c.

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}
const char * str
Definition: app_jack.c:147
#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:102
#define ERROR_NOMEM
Definition: func_cut.c:103
#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)
#define LOG_ERROR
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
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 286 of file func_cut.c.

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}

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 239 of file func_cut.c.

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

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 341 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 157 of file func_cut.c.

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}
#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: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_WARNING
void ast_str_substitute_variables(struct ast_str **buf, ssize_t maxlen, struct ast_channel *chan, const char *templ)
#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 ast_str_strlen(const struct ast_str *buf)
Returns the current length of the string stored within buf.
Definition: strings.h:730
const char * args

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 331 of file func_cut.c.

332{
333 int res = 0;
334
337
338 return res;
339}
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 ast_custom_function_register(acf)
Register a custom function.
Definition: pbx.h:1558

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 106 of file func_cut.c.

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}
static int sort_subroutine(const void *arg1, const void *arg2)
Definition: func_cut.c:91
char * key
Definition: func_cut.c:87
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 91 of file func_cut.c.

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

References sortable_keys::value.

Referenced by sort_internal().

◆ unload_module()

static int unload_module ( void  )
static

Definition at line 321 of file func_cut.c.

322{
323 int res = 0;
324
327
328 return res;
329}
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 = "This paragraph is copyright (c) 2006 by Digium, Inc. \In order for your module to load, it must return this \key via a function called \"key\". Any code which \includes this paragraph must be licensed under the GNU \General Public License version 2 or later (at your \option). In addition to Digium's general reservations \of rights, Digium expressly reserves the right to \allow other parties to license this paragraph under \different terms. Any use of Digium, Inc. trademarks or \logos (including \"Asterisk\" or \"Digium\") without \express written permission of Digium, Inc. is prohibited.\n" , .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 341 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:260
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

Definition at line 315 of file func_cut.c.

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:239

Definition at line 310 of file func_cut.c.

Referenced by load_module(), and unload_module().

◆ ast_module_info

const struct ast_module_info* ast_module_info = &__mod_info
static

Definition at line 341 of file func_cut.c.