FreeSWITCH API Documentation  1.7.0
Macros | Typedefs | Functions
Regular Expressions

Macros

#define switch_regex_safe_free(re)
 

Typedefs

typedef struct real_pcre switch_regex_t
 

Functions

switch_regex_tswitch_regex_compile (const char *pattern, int options, const char **errorptr, int *erroroffset, const unsigned char *tables)
 
int switch_regex_copy_substring (const char *subject, int *ovector, int stringcount, int stringnumber, char *buffer, int size)
 
void switch_regex_free (void *data)
 
int switch_regex_perform (const char *field, const char *expression, switch_regex_t **new_re, int *ovector, uint32_t olen)
 
void switch_perform_substitution (switch_regex_t *re, int match_count, const char *data, const char *field_data, char *substituted, switch_size_t len, int *ovector)
 
switch_status_t switch_regex_match (const char *target, const char *expression)
 Function to evaluate an expression against a string. More...
 
switch_status_t switch_regex_match_partial (const char *target, const char *expression, int *partial_match)
 Function to evaluate an expression against a string. More...
 
void switch_capture_regex (switch_regex_t *re, int match_count, const char *field_data, int *ovector, const char *var, switch_cap_callback_t callback, void *user_data)
 
void switch_regex_set_var_callback (const char *var, const char *val, void *user_data)
 
void switch_regex_set_event_header_callback (const char *var, const char *val, void *user_data)
 

Detailed Description

Macro Definition Documentation

#define switch_regex_safe_free (   re)

Typedef Documentation

typedef struct real_pcre switch_regex_t

Definition at line 43 of file switch_regex.h.

Function Documentation

void switch_capture_regex ( switch_regex_t re,
int  match_count,
const char *  field_data,
int *  ovector,
const char *  var,
switch_cap_callback_t  callback,
void *  user_data 
)

Definition at line 193 of file switch_regex.c.

196 {
197 
198 
199  const char *replace;
200  int i;
201 
202  for (i = 0; i < match_count; i++) {
203  if (pcre_get_substring(field_data, ovector, match_count, i, &replace) > 0) {
204  callback(var, replace, user_data);
205  pcre_free_substring(replace);
206  }
207  }
208 }
void switch_perform_substitution ( switch_regex_t re,
int  match_count,
const char *  data,
const char *  field_data,
char *  substituted,
switch_size_t  len,
int *  ovector 
)

Definition at line 131 of file switch_regex.c.

Referenced by switch_ivr_menu_execute(), and switch_ivr_phrase_macro_event().

133 {
134  char index[10] = "";
135  const char *replace = NULL;
136  switch_size_t x, y = 0, z = 0;
137  int num = 0;
138  int brace;
139 
140  for (x = 0; y < (len - 1) && x < strlen(data);) {
141  if (data[x] == '$') {
142  x++;
143 
144  brace = data[x] == '{';
145  if (brace) {
146  x++;
147  }
148 
149  if (!(data[x] > 47 && data[x] < 58)) {
150  x -= brace;
151  substituted[y++] = data[x - 1];
152  continue;
153  }
154 
155  while (data[x] > 47 && data[x] < 58 && z < sizeof(index) - 1) {
156  index[z++] = data[x];
157  x++;
158  }
159  if (brace) {
160  if (data[x] != '}') {
161  x -= z - 1;
162  substituted[y++] = data[x - 1];
163  continue;
164  }
165  else {
166  x++;
167  }
168  }
169  index[z++] = '\0';
170  z = 0;
171  num = atoi(index);
172 
173  if (num < 0 || num > 256) {
174  num = -1;
175  }
176 
177  if (pcre_get_substring(field_data, ovector, match_count, num, &replace) > 0) {
178  switch_size_t r;
179  for (r = 0; r < strlen(replace) && y < (len - 1); r++) {
180  substituted[y++] = replace[r];
181  }
182  pcre_free_substring(replace);
183  }
184  } else {
185  substituted[y++] = data[x];
186  x++;
187  }
188  }
189  substituted[y++] = '\0';
190 }
uintptr_t switch_size_t
switch_regex_t* switch_regex_compile ( const char *  pattern,
int  options,
const char **  errorptr,
int *  erroroffset,
const unsigned char *  tables 
)

Definition at line 36 of file switch_regex.c.

38 {
39 
40  return pcre_compile(pattern, options, errorptr, erroroffset, tables);
41 
42 }
int switch_regex_copy_substring ( const char *  subject,
int *  ovector,
int  stringcount,
int  stringnumber,
char *  buffer,
int  size 
)

Definition at line 44 of file switch_regex.c.

Referenced by switch_str_time().

45 {
46  return pcre_copy_substring(subject, ovector, stringcount, stringnumber, buffer, size);
47 }
void switch_regex_free ( void *  data)

Definition at line 49 of file switch_regex.c.

50 {
51  pcre_free(data);
52 
53 }
switch_status_t switch_regex_match ( const char *  target,
const char *  expression 
)

Function to evaluate an expression against a string.

Parameters
targetThe string to find a match in
expressionThe regular expression to run against the string
Returns
Boolean if a match was found or not

Definition at line 295 of file switch_regex.c.

References switch_regex_match_partial().

Referenced by switch_ivr_dmachine_check_match(), switch_play_and_get_digits(), and switch_xml_config_parse_event().

296 {
297  int partial = 0;
298  return switch_regex_match_partial(target, expression, &partial);
299 }
switch_status_t switch_regex_match_partial(const char *target, const char *expression, int *partial)
Function to evaluate an expression against a string.
Definition: switch_regex.c:210
switch_status_t switch_regex_match_partial ( const char *  target,
const char *  expression,
int *  partial_match 
)

Function to evaluate an expression against a string.

Parameters
targetThe string to find a match in
expressionThe regular expression to run against the string
partial_matchIf non-zero returns SUCCESS if the target is a partial match, on successful return, this is set to non-zero if the match was partial and zero if it was a full match
Returns
Boolean if a match was found or not

Definition at line 210 of file switch_regex.c.

References SWITCH_CHANNEL_LOG, switch_goto_status, SWITCH_LOG_ERROR, switch_log_printf(), switch_safe_free, SWITCH_STATUS_FALSE, and SWITCH_STATUS_SUCCESS.

Referenced by switch_regex_match().

211 {
212  const char *error = NULL; /* Used to hold any errors */
213  int error_offset = 0; /* Holds the offset of an error */
214  pcre *pcre_prepared = NULL; /* Holds the compiled regex */
215  int match_count = 0; /* Number of times the regex was matched */
216  int offset_vectors[255]; /* not used, but has to exist or pcre won't even try to find a match */
217  int pcre_flags = 0;
218  uint32_t flags = 0;
219  char *tmp = NULL;
221 
222  if (*expression == '/') {
223  char *opts = NULL;
224  tmp = strdup(expression + 1);
225  assert(tmp);
226  if ((opts = strrchr(tmp, '/'))) {
227  *opts++ = '\0';
228  } else {
229  /* Note our error */
231  "Regular Expression Error expression[%s] missing ending '/' delimeter\n", expression);
232  goto end;
233  }
234  expression = tmp;
235  if (opts) {
236  if (strchr(opts, 'i')) {
237  flags |= PCRE_CASELESS;
238  }
239  if (strchr(opts, 's')) {
240  flags |= PCRE_DOTALL;
241  }
242  }
243  }
244 
245  /* Compile the expression */
246  pcre_prepared = pcre_compile(expression, flags, &error, &error_offset, NULL);
247 
248  /* See if there was an error in the expression */
249  if (error != NULL) {
250  /* Clean up after ourselves */
251  if (pcre_prepared) {
252  pcre_free(pcre_prepared);
253  pcre_prepared = NULL;
254  }
255  /* Note our error */
257  "Regular Expression Error expression[%s] error[%s] location[%d]\n", expression, error, error_offset);
258 
259  /* We definitely didn't match anything */
260  goto end;
261  }
262 
263  if (*partial) {
264  pcre_flags = PCRE_PARTIAL;
265  }
266 
267  /* So far so good, run the regex */
268  match_count =
269  pcre_exec(pcre_prepared, NULL, target, (int) strlen(target), 0, pcre_flags, offset_vectors, sizeof(offset_vectors) / sizeof(offset_vectors[0]));
270 
271  /* Clean up */
272  if (pcre_prepared) {
273  pcre_free(pcre_prepared);
274  pcre_prepared = NULL;
275  }
276 
277  /* switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG, "number of matches: %d\n", match_count); */
278 
279  /* Was it a match made in heaven? */
280  if (match_count > 0) {
281  *partial = 0;
283  } else if (match_count == PCRE_ERROR_PARTIAL || match_count == PCRE_ERROR_BADPARTIAL) {
284  /* yes it is already set, but the code is clearer this way */
285  *partial = 1;
287  } else {
288  goto end;
289  }
290  end:
291  switch_safe_free(tmp);
292  return status;
293 }
#define SWITCH_CHANNEL_LOG
#define switch_safe_free(it)
Free a pointer and set it to NULL unless it already is NULL.
Definition: switch_utils.h:789
switch_status_t
Common return values.
#define switch_goto_status(_status, _label)
Definition: switch_utils.h:256
void switch_log_printf(_In_ switch_text_channel_t channel, _In_z_ const char *file, _In_z_ const char *func, _In_ int line, _In_opt_z_ const char *userdata, _In_ switch_log_level_t level, _In_z_ _Printf_format_string_ const char *fmt,...) PRINTF_FUNCTION(7
Write log data to the logging engine.
int switch_regex_perform ( const char *  field,
const char *  expression,
switch_regex_t **  new_re,
int *  ovector,
uint32_t  olen 
)

Definition at line 55 of file switch_regex.c.

References switch_ast2regex(), SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, switch_log_printf(), switch_regex_safe_free, and switch_safe_free.

Referenced by switch_channel_set_timestamps(), switch_ivr_check_presence_mapping(), switch_ivr_menu_execute(), switch_ivr_phrase_macro_event(), and switch_str_time().

56 {
57  const char *error = NULL;
58  int erroffset = 0;
59  pcre *re = NULL;
60  int match_count = 0;
61  char *tmp = NULL;
62  uint32_t flags = 0;
63  char abuf[256] = "";
64 
65  if (!(field && expression)) {
66  return 0;
67  }
68 
69  if (*expression == '_') {
70  if (switch_ast2regex(expression + 1, abuf, sizeof(abuf))) {
71  expression = abuf;
72  }
73  }
74 
75  if (*expression == '/') {
76  char *opts = NULL;
77  tmp = strdup(expression + 1);
78  assert(tmp);
79  if ((opts = strrchr(tmp, '/'))) {
80  *opts++ = '\0';
81  } else {
82  /* Note our error */
84  "Regular Expression Error expression[%s] missing ending '/' delimeter\n", expression);
85  goto end;
86  }
87  expression = tmp;
88  if (opts) {
89  if (strchr(opts, 'i')) {
90  flags |= PCRE_CASELESS;
91  }
92  if (strchr(opts, 's')) {
93  flags |= PCRE_DOTALL;
94  }
95  }
96  }
97 
98  re = pcre_compile(expression, /* the pattern */
99  flags, /* default options */
100  &error, /* for error message */
101  &erroffset, /* for error offset */
102  NULL); /* use default character tables */
103  if (error) {
104  switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "COMPILE ERROR: %d [%s][%s]\n", erroffset, error, expression);
106  goto end;
107  }
108 
109  match_count = pcre_exec(re, /* result of pcre_compile() */
110  NULL, /* we didn't study the pattern */
111  field, /* the subject string */
112  (int) strlen(field), /* the length of the subject string */
113  0, /* start at offset 0 in the subject */
114  0, /* default options */
115  ovector, /* vector of integers for substring information */
116  olen); /* number of elements (NOT size in bytes) */
117 
118 
119  if (match_count <= 0) {
121  match_count = 0;
122  }
123 
124  *new_re = (switch_regex_t *) re;
125 
126  end:
127  switch_safe_free(tmp);
128  return match_count;
129 }
#define switch_regex_safe_free(re)
Definition: switch_regex.h:79
#define SWITCH_CHANNEL_LOG
struct real_pcre switch_regex_t
Definition: switch_regex.h:43
#define switch_safe_free(it)
Free a pointer and set it to NULL unless it already is NULL.
Definition: switch_utils.h:789
void switch_log_printf(_In_ switch_text_channel_t channel, _In_z_ const char *file, _In_z_ const char *func, _In_ int line, _In_opt_z_ const char *userdata, _In_ switch_log_level_t level, _In_z_ _Printf_format_string_ const char *fmt,...) PRINTF_FUNCTION(7
Write log data to the logging engine.
switch_bool_t switch_ast2regex(const char *pat, char *rbuf, size_t len)
void switch_regex_set_event_header_callback ( const char *  var,
const char *  val,
void *  user_data 
)

Definition at line 308 of file switch_regex.c.

References switch_event_add_header_string(), and SWITCH_STACK_BOTTOM.

309 {
310 
311  switch_event_t *event = (switch_event_t *) user_data;
313 }
Representation of an event.
Definition: switch_event.h:80
switch_status_t switch_event_add_header_string(switch_event_t *event, switch_stack_t stack, const char *header_name, const char *data)
Add a string header to an event.
void switch_regex_set_var_callback ( const char *  var,
const char *  val,
void *  user_data 
)

Definition at line 301 of file switch_regex.c.

References switch_channel_add_variable_var_check(), switch_core_session_get_channel(), SWITCH_FALSE, and SWITCH_STACK_PUSH.

302 {
306 }
switch_core_session_t * session
_Ret_ switch_channel_t * switch_core_session_get_channel(_In_ switch_core_session_t *session)
Retrieve a pointer to the channel object associated with a given session.
switch_status_t switch_channel_add_variable_var_check(switch_channel_t *channel, const char *varname, const char *value, switch_bool_t var_check, switch_stack_t stack)