79 #if defined(HAVE_REGEX) || defined(HAVE_PCRE) 86 void gw_regex_destroy(regex_t *preg)
96 regex_t *gw_regex_comp_real(
const Octstr *pattern,
int cflags,
const char *
file,
97 long line,
const char *func)
102 preg = gw_malloc(
sizeof(regex_t));
104 if ((rc = regcomp(preg, pattern ?
octstr_get_cstr(pattern) : NULL, cflags)) != 0) {
106 regerror(rc, preg, buffer,
sizeof(buffer));
107 error(0,
"%s:%ld: %s: regex compilation `%s' failed: %s (Called from %s:%ld:%s.)",
109 (
file), (
long) (line), (func));
118 int gw_regex_exec_real(
const regex_t *preg,
const Octstr *
string,
size_t nmatch,
119 regmatch_t pmatch[],
int eflags,
const char *
file,
long line,
126 rc = regexec(preg,
string ?
octstr_get_cstr(
string) : NULL, nmatch, pmatch, eflags);
127 if (rc != REG_NOMATCH && rc != 0) {
129 regerror(rc, preg, buffer,
sizeof(buffer));
130 error(0,
"%s:%ld: %s: regex execution on `%s' failed: %s (Called from %s:%ld:%s.)",
132 (
file), (
long) (line), (func));
139 Octstr *gw_regex_error(
int errcode,
const regex_t *preg)
144 regerror(errcode, preg, errbuf,
sizeof(errbuf));
152 static char *pstrdup(
const char *s)
160 res = gw_malloc(
len);
180 char *gw_regex_sub(
const char *input,
const char *source,
181 size_t nmatch, regmatch_t pmatch[])
183 const char *src = input;
196 while ((c = *src++) !=
'\0') {
199 else if (c ==
'$' && isdigit(*src))
205 if (c ==
'\\' && (*src ==
'$' || *src ==
'&'))
209 else if (no < nmatch && pmatch[no].rm_so < pmatch[no].rm_eo) {
210 len += pmatch[no].rm_eo - pmatch[no].rm_so;
214 dest = dst = gw_malloc(
len + 1);
218 while ((c = *src++) !=
'\0') {
221 else if (c ==
'$' && isdigit(*src))
227 if (c ==
'\\' && (*src ==
'$' || *src ==
'&'))
231 else if (no < nmatch && pmatch[no].rm_so < pmatch[no].rm_eo) {
232 len = pmatch[no].rm_eo - pmatch[no].rm_so;
233 memcpy(dst, source + pmatch[no].rm_so,
len);
251 int gw_regex_match_real(
const Octstr *re,
const Octstr *os,
const char *
file,
252 long line,
const char *func)
258 regexp = gw_regex_comp_real(re, REG_EXTENDED|REG_ICASE,
file, line, func);
263 rc = gw_regex_exec_real(regexp, os, 0, NULL, 0,
file, line, func);
265 gw_regex_destroy(regexp);
267 return (rc == 0) ? 1 : 0;
271 int gw_regex_match_pre_real(
const regex_t *preg,
const Octstr *os,
const char *
file,
272 long line,
const char *func)
279 rc = gw_regex_exec_real(preg, os, 0, NULL, 0,
file, line, func);
281 return (rc == 0) ? 1 : 0;
286 const char *
file,
long line,
const char *func)
290 regmatch_t pmatch[REGEX_MAX_SUB_MATCH];
295 regexp = gw_regex_comp_real(re, REG_EXTENDED|REG_ICASE,
file, line, func);
300 rc = gw_regex_exec_real(regexp, os, REGEX_MAX_SUB_MATCH, &pmatch[0], 0,
302 gw_regex_destroy(regexp);
309 REGEX_MAX_SUB_MATCH, &pmatch[0]);
320 Octstr *gw_regex_subst_pre_real(
const regex_t *preg,
const Octstr *os,
const Octstr *rule,
321 const char *
file,
long line,
const char *func)
324 regmatch_t pmatch[REGEX_MAX_SUB_MATCH];
331 rc = gw_regex_exec_real(preg, os, REGEX_MAX_SUB_MATCH, &pmatch[0], 0,
339 REGEX_MAX_SUB_MATCH, &pmatch[0]);
void error(int err, const char *fmt,...)
gw_assert(wtls_machine->packet_to_send !=NULL)
#define octstr_get_cstr(ostr)
#define octstr_create(cstr)