Asterisk - The Open Source Telephony Project  18.5.0
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 = "30ef0c93b36035ec78c9cfd712d36d9b" , .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 98 of file func_cut.c.

Referenced by acf_cut_exec(), acf_cut_exec2(), acf_sort_exec(), cut_internal(), and sort_internal().

◆ ERROR_NOMEM

#define ERROR_NOMEM   (-2)

Definition at line 99 of file func_cut.c.

Referenced by acf_cut_exec(), acf_cut_exec2(), and acf_sort_exec().

◆ ERROR_USAGE

#define ERROR_USAGE   (-3)

Definition at line 100 of file func_cut.c.

Referenced by acf_cut_exec(), acf_cut_exec2(), and cut_internal().

Function Documentation

◆ __reg_module()

static void __reg_module ( void  )
static

Definition at line 337 of file func_cut.c.

◆ __unreg_module()

static void __unreg_module ( void  )
static

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

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

257 {
258  int ret = -1;
259  struct ast_str *str = ast_str_create(16);
260 
261  switch (cut_internal(chan, data, &str, len)) {
262  case ERROR_NOARG:
263  ast_log(LOG_ERROR, "Syntax: CUT(<varname>,<char-delim>,<range-spec>) - missing argument!\n");
264  break;
265  case ERROR_NOMEM:
266  ast_log(LOG_ERROR, "Out of memory\n");
267  break;
268  case ERROR_USAGE:
269  ast_log(LOG_ERROR, "Usage: CUT(<varname>,<char-delim>,<range-spec>)\n");
270  break;
271  case 0:
272  ret = 0;
274  break;
275  default:
276  ast_log(LOG_ERROR, "Unknown internal error\n");
277  }
278  ast_free(str);
279  return ret;
280 }
char buf[BUFSIZE]
Definition: eagi_proxy.c:66
char * ast_str_buffer(const struct ast_str *buf)
Returns the string buffer within the ast_str buf.
Definition: strings.h:714
const char * str
Definition: app_jack.c:147
#define ERROR_NOARG
Definition: func_cut.c:98
#define ast_log
Definition: astobj2.c:42
#define ERROR_NOMEM
Definition: func_cut.c:99
static int cut_internal(struct ast_channel *chan, char *data, struct ast_str **buf, ssize_t buflen)
Definition: func_cut.c:153
#define LOG_ERROR
Definition: logger.h:285
The descriptor of a dynamic string XXX storage will be optimized later if needed We use the ts field ...
Definition: strings.h:584
static int len(struct ast_channel *chan, const char *cmd, char *data, char *buf, size_t buflen)
#define ast_free(a)
Definition: astmm.h:182
#define ERROR_USAGE
Definition: func_cut.c:100
void ast_copy_string(char *dst, const char *src, size_t size)
Size-limited null-terminating string copy.
Definition: strings.h:401
#define ast_str_create(init_len)
Create a malloc&#39;ed dynamic length string.
Definition: strings.h:620

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

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

283 {
284  int ret = -1;
285 
286  switch (cut_internal(chan, data, buf, len)) {
287  case ERROR_NOARG:
288  ast_log(LOG_ERROR, "Syntax: CUT(<varname>,<char-delim>,<range-spec>) - missing argument!\n");
289  break;
290  case ERROR_NOMEM:
291  ast_log(LOG_ERROR, "Out of memory\n");
292  break;
293  case ERROR_USAGE:
294  ast_log(LOG_ERROR, "Usage: CUT(<varname>,<char-delim>,<range-spec>)\n");
295  break;
296  case 0:
297  ret = 0;
298  break;
299  default:
300  ast_log(LOG_ERROR, "Unknown internal error\n");
301  }
302 
303  return ret;
304 }
#define ERROR_NOARG
Definition: func_cut.c:98
#define ast_log
Definition: astobj2.c:42
#define ERROR_NOMEM
Definition: func_cut.c:99
static int cut_internal(struct ast_channel *chan, char *data, struct ast_str **buf, ssize_t buflen)
Definition: func_cut.c:153
#define LOG_ERROR
Definition: logger.h:285
static int len(struct ast_channel *chan, const char *cmd, char *data, char *buf, size_t buflen)
#define ERROR_USAGE
Definition: func_cut.c:100

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

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

236 {
237  int ret = -1;
238 
239  switch (sort_internal(chan, data, buf, len)) {
240  case ERROR_NOARG:
241  ast_log(LOG_ERROR, "SORT() requires an argument\n");
242  break;
243  case ERROR_NOMEM:
244  ast_log(LOG_ERROR, "Out of memory\n");
245  break;
246  case 0:
247  ret = 0;
248  break;
249  default:
250  ast_log(LOG_ERROR, "Unknown internal error\n");
251  }
252 
253  return ret;
254 }
char buf[BUFSIZE]
Definition: eagi_proxy.c:66
#define ERROR_NOARG
Definition: func_cut.c:98
#define ast_log
Definition: astobj2.c:42
#define ERROR_NOMEM
Definition: func_cut.c:99
#define LOG_ERROR
Definition: logger.h:285
static int len(struct ast_channel *chan, const char *cmd, char *data, char *buf, size_t buflen)
static int sort_internal(struct ast_channel *chan, char *data, char *buffer, size_t buflen)
Definition: func_cut.c:102

◆ AST_MODULE_SELF_SYM()

struct ast_module* AST_MODULE_SELF_SYM ( void  )

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

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, ERROR_NOARG, ERROR_USAGE, LOG_WARNING, NULL, parse(), and strsep().

Referenced by acf_cut_exec(), and acf_cut_exec2().

154 {
155  char *parse, ds[2], *var_expr;
156  size_t delim_consumed;
157  struct ast_str *var_value;
159  AST_APP_ARG(varname);
160  AST_APP_ARG(delimiter);
161  AST_APP_ARG(field);
162  );
163 
164  parse = ast_strdupa(data);
165 
166  AST_STANDARD_APP_ARGS(args, parse);
167 
168  /* Check arguments */
169  if (args.argc < 3) {
170  return ERROR_NOARG;
171  }
172  var_expr = ast_alloca(strlen(args.varname) + 4);
173 
174  /* Get the value of the variable named in the 1st argument */
175  snprintf(var_expr, strlen(args.varname) + 4, "${%s}", args.varname);
176  var_value = ast_str_create(16);
177  ast_str_substitute_variables(&var_value, 0, chan, var_expr);
178 
179  /* Copy delimiter from 2nd argument to ds[] possibly decoding backslash escapes */
180  if (ast_get_encoded_char(args.delimiter, ds, &delim_consumed)) {
181  ast_copy_string(ds, "-", sizeof(ds));
182  }
183  ds[1] = '\0';
184 
185  if (ast_str_strlen(var_value)) {
186  int curfieldnum = 1;
187  char *curfieldptr = ast_str_buffer(var_value);
188  int out_field_count = 0;
189 
190  while (curfieldptr != NULL && args.field != NULL) {
191  char *next_range = strsep(&(args.field), "&");
192  int start_field, stop_field;
193  char trashchar;
194 
195  if (sscanf(next_range, "%30d-%30d", &start_field, &stop_field) == 2) {
196  /* range with both start and end */
197  } else if (sscanf(next_range, "-%30d", &stop_field) == 1) {
198  /* range with end only */
199  start_field = 1;
200  } else if ((sscanf(next_range, "%30d%1c", &start_field, &trashchar) == 2) && (trashchar == '-')) {
201  /* range with start only */
202  stop_field = INT_MAX;
203  } else if (sscanf(next_range, "%30d", &start_field) == 1) {
204  /* single number */
205  stop_field = start_field;
206  } else {
207  /* invalid field spec */
208  ast_free(var_value);
209  return ERROR_USAGE;
210  }
211 
212  /* Get to start, if not there already */
213  while (curfieldptr != NULL && curfieldnum < start_field) {
214  strsep(&curfieldptr, ds);
215  curfieldnum++;
216  }
217 
218  /* Most frequent problem is the expectation of reordering fields */
219  if (curfieldnum > start_field) {
220  ast_log(LOG_WARNING, "We're already past the field you wanted?\n");
221  }
222 
223  /* Output fields until we either run out of fields or stop_field is reached */
224  while (curfieldptr != NULL && curfieldnum <= stop_field) {
225  char *field_value = strsep(&curfieldptr, ds);
226  ast_str_append(buf, buflen, "%s%s", out_field_count++ ? ds : "", field_value);
227  curfieldnum++;
228  }
229  }
230  }
231  ast_free(var_value);
232  return 0;
233 }
#define AST_STANDARD_APP_ARGS(args, parse)
Performs the &#39;standard&#39; argument separation process for an application.
#define LOG_WARNING
Definition: logger.h:274
char * ast_str_buffer(const struct ast_str *buf)
Returns the string buffer within the ast_str buf.
Definition: strings.h:714
void ast_str_substitute_variables(struct ast_str **buf, ssize_t maxlen, struct ast_channel *chan, const char *templ)
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:1091
const char * args
#define NULL
Definition: resample.c:96
#define ERROR_NOARG
Definition: func_cut.c:98
#define ast_log
Definition: astobj2.c:42
#define ast_strdupa(s)
duplicate a string in memory from the stack
Definition: astmm.h:300
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:2927
#define ast_alloca(size)
call __builtin_alloca to ensure we get gcc builtin semantics
Definition: astmm.h:290
The descriptor of a dynamic string XXX storage will be optimized later if needed We use the ts field ...
Definition: strings.h:584
static void parse(struct mgcp_request *req)
Definition: chan_mgcp.c:1872
#define ast_free(a)
Definition: astmm.h:182
#define ERROR_USAGE
Definition: func_cut.c:100
size_t ast_str_strlen(const struct ast_str *buf)
Returns the current length of the string stored within buf.
Definition: strings.h:688
char * strsep(char **str, const char *delims)
void ast_copy_string(char *dst, const char *src, size_t size)
Size-limited null-terminating string copy.
Definition: strings.h:401
#define AST_DECLARE_APP_ARGS(name, arglist)
Declare a structure to hold an application&#39;s arguments.
#define ast_str_create(init_len)
Create a malloc&#39;ed dynamic length string.
Definition: strings.h:620
#define AST_APP_ARG(name)
Define an application argument.

◆ load_module()

static int load_module ( void  )
static

Definition at line 327 of file func_cut.c.

References ast_custom_function_register.

328 {
329  int res = 0;
330 
333 
334  return res;
335 }
static struct ast_custom_function acf_sort
Definition: func_cut.c:306
static struct ast_custom_function acf_cut
Definition: func_cut.c:311
#define ast_custom_function_register(acf)
Register a custom function.
Definition: pbx.h:1508

◆ sort_internal()

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

Definition at line 102 of file func_cut.c.

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

Referenced by acf_sort_exec().

103 {
104  char *strings, *ptrkey, *ptrvalue;
105  int count=1, count2, element_count=0;
107 
108  *buffer = '\0';
109 
110  if (!data)
111  return ERROR_NOARG;
112 
113  strings = ast_strdupa(data);
114 
115  for (ptrkey = strings; *ptrkey; ptrkey++) {
116  if (*ptrkey == ',')
117  count++;
118  }
119 
120  sortable_keys = ast_alloca(count * sizeof(struct sortable_keys));
121 
122  memset(sortable_keys, 0, count * sizeof(struct sortable_keys));
123 
124  /* Parse each into a struct */
125  count2 = 0;
126  while ((ptrkey = strsep(&strings, ","))) {
127  ptrvalue = strchr(ptrkey, ':');
128  if (!ptrvalue) {
129  count--;
130  continue;
131  }
132  *ptrvalue++ = '\0';
133  sortable_keys[count2].key = ptrkey;
134  sscanf(ptrvalue, "%30f", &sortable_keys[count2].value);
135  count2++;
136  }
137 
138  /* Sort the structs */
139  qsort(sortable_keys, count, sizeof(struct sortable_keys), sort_subroutine);
140 
141  for (count2 = 0; count2 < count; count2++) {
142  int blen = strlen(buffer);
143  if (element_count++) {
144  strncat(buffer + blen, ",", buflen - blen - 1);
145  blen++;
146  }
147  strncat(buffer + blen, sortable_keys[count2].key, buflen - blen - 1);
148  }
149 
150  return 0;
151 }
int value
Definition: syslog.c:37
#define ERROR_NOARG
Definition: func_cut.c:98
#define ast_strdupa(s)
duplicate a string in memory from the stack
Definition: astmm.h:300
#define ast_alloca(size)
call __builtin_alloca to ensure we get gcc builtin semantics
Definition: astmm.h:290
char * strsep(char **str, const char *delims)
char * key
Definition: func_cut.c:83
static int sort_subroutine(const void *arg1, const void *arg2)
Definition: func_cut.c:87

◆ sort_subroutine()

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

Definition at line 87 of file func_cut.c.

References sortable_keys::value.

Referenced by sort_internal().

88 {
89  const struct sortable_keys *one=arg1, *two=arg2;
90  if (one->value < two->value)
91  return -1;
92  else if (one->value == two->value)
93  return 0;
94  else
95  return 1;
96 }
float value
Definition: func_cut.c:84

◆ unload_module()

static int unload_module ( void  )
static

Definition at line 317 of file func_cut.c.

References ast_custom_function_unregister().

318 {
319  int res = 0;
320 
323 
324  return res;
325 }
static struct ast_custom_function acf_sort
Definition: func_cut.c:306
int ast_custom_function_unregister(struct ast_custom_function *acf)
Unregister a custom function.
static struct ast_custom_function acf_cut
Definition: func_cut.c:311

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 = "30ef0c93b36035ec78c9cfd712d36d9b" , .load = load_module, .unload = unload_module, .load_pri = AST_MODPRI_DEFAULT, .support_level = AST_MODULE_SUPPORT_CORE, }
static

Definition at line 337 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:256
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:282

Definition at line 311 of file func_cut.c.

◆ 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:235

Definition at line 306 of file func_cut.c.

◆ ast_module_info

const struct ast_module_info* ast_module_info = &__mod_info
static

Definition at line 337 of file func_cut.c.