39 |
Regular Expressions support |
Regular Expressions support |
40 |
*****************************/ |
*****************************/ |
41 |
|
|
42 |
typedef int (*_match_fp) (RC_REGEX *, char *, int *, char ***); |
typedef int (*_match_fp) (RC_REGEX *, char *, int *, char ***, int *, int *); |
43 |
typedef int (*_refcnt_fp) (RC_REGEX *); |
typedef int (*_refcnt_fp) (RC_REGEX *); |
44 |
typedef int (*_compile_fp) (RC_REGEX *, char *, int); |
typedef int (*_compile_fp) (RC_REGEX *, char *, int); |
45 |
typedef void (*_free_fp) (RC_REGEX *); |
typedef void (*_free_fp) (RC_REGEX *); |
54 |
|
|
55 |
static int posix_compile(RC_REGEX *, char *, int); |
static int posix_compile(RC_REGEX *, char *, int); |
56 |
static void posix_free(RC_REGEX *); |
static void posix_free(RC_REGEX *); |
57 |
static int posix_match(RC_REGEX *, char *, int *, char ***); |
static int posix_match(RC_REGEX *, char *, int *, char ***, int *, int *); |
58 |
static int posix_refcnt(RC_REGEX *); |
static int posix_refcnt(RC_REGEX *); |
59 |
#ifdef HAVE_PCRE |
#ifdef HAVE_PCRE |
60 |
static int perl_compile(RC_REGEX *, char *, int); |
static int perl_compile(RC_REGEX *, char *, int); |
61 |
static void perl_free(RC_REGEX *); |
static void perl_free(RC_REGEX *); |
62 |
static int perl_match(RC_REGEX *, char *, int *, char ***); |
static int perl_match(RC_REGEX *, char *, int *, char ***, int *, int *); |
63 |
static int perl_refcnt(RC_REGEX *); |
static int perl_refcnt(RC_REGEX *); |
64 |
#endif /* HAVE_PCRE */ |
#endif /* HAVE_PCRE */ |
65 |
|
|
97 |
int |
int |
98 |
anubis_regex_match(RC_REGEX *re, char *line, int *refc, char ***refv) |
anubis_regex_match(RC_REGEX *re, char *line, int *refc, char ***refv) |
99 |
{ |
{ |
100 |
|
int so, eo; |
101 |
struct regex_vtab *vp = regex_vtab_lookup(re->flags); |
struct regex_vtab *vp = regex_vtab_lookup(re->flags); |
102 |
if (!vp) |
if (!vp) |
103 |
return -1; |
return -1; |
104 |
return vp->match(re, line, refc, refv) == 0; |
return vp->match(re, line, refc, refv, &so, &eo) == 0; |
105 |
|
} |
106 |
|
|
107 |
|
char * |
108 |
|
anubis_regex_replace(RC_REGEX *re, char *line, char *repl) |
109 |
|
{ |
110 |
|
int so, eo; |
111 |
|
int refc; |
112 |
|
char **refv; |
113 |
|
struct regex_vtab *vp = regex_vtab_lookup(re->flags); |
114 |
|
|
115 |
|
if (!vp) |
116 |
|
return NULL; |
117 |
|
if (vp->match(re, line, &refc, &refv, &so, &eo) == 0) { |
118 |
|
char *p = substitute(repl, refv); |
119 |
|
int plen = strlen(p); |
120 |
|
int newlen = strlen(line) - (eo - so) + plen + 1; |
121 |
|
char *newstr = xmalloc(newlen); |
122 |
|
|
123 |
|
memcpy(newstr, line, so); |
124 |
|
memcpy(newstr + so, p, strlen(p)); |
125 |
|
strcpy(newstr + so + plen, line + eo); |
126 |
|
|
127 |
|
xfree_pptr(refv); |
128 |
|
return newstr; |
129 |
|
} |
130 |
|
return NULL; |
131 |
} |
} |
132 |
|
|
133 |
int |
int |
205 |
} |
} |
206 |
|
|
207 |
static int |
static int |
208 |
posix_match(RC_REGEX *regex, char *line, int *refc, char ***refv) |
posix_match(RC_REGEX *regex, char *line, int *refc, char ***refv, |
209 |
|
int *so, int *eo) |
210 |
{ |
{ |
211 |
regmatch_t *rmp; |
regmatch_t *rmp; |
212 |
int rc; |
int rc; |
217 |
if (rc == 0 && re->re_nsub) { |
if (rc == 0 && re->re_nsub) { |
218 |
int i; |
int i; |
219 |
*refv = xmalloc((re->re_nsub + 2) * sizeof(**refv)); |
*refv = xmalloc((re->re_nsub + 2) * sizeof(**refv)); |
220 |
|
*eo = rmp[0].rm_eo; |
221 |
|
*so = rmp[0].rm_so; |
222 |
for (i = 0; i <= re->re_nsub; i++) { |
for (i = 0; i <= re->re_nsub; i++) { |
223 |
if (rmp[i].rm_so != -1) { |
if (rmp[i].rm_so != -1) { |
224 |
size_t matchlen = rmp[i].rm_eo - rmp[i].rm_so; |
size_t matchlen = rmp[i].rm_eo - rmp[i].rm_so; |
275 |
} |
} |
276 |
|
|
277 |
static int |
static int |
278 |
perl_match(RC_REGEX *regex, char *line, int *refc, char ***refv) |
perl_match(RC_REGEX *regex, char *line, int *refc, char ***refv, |
279 |
|
int *so, int *eo) |
280 |
{ |
{ |
281 |
int rc; |
int rc; |
282 |
int ovsize, count; |
int ovsize, count; |
314 |
} |
} |
315 |
(*refv)[i] = NULL; |
(*refv)[i] = NULL; |
316 |
*refc = count; |
*refc = count; |
317 |
|
*so = ovector[0]; |
318 |
|
*eo = ovector[1]; |
319 |
} else |
} else |
320 |
*refc = 0; |
*refc = 0; |
321 |
xfree(ovector); |
xfree(ovector); |