]>
jfr.im git - irc/quakenet/newserv.git/blob - newsearch/ns-regex.c
12 struct regex_localdata
{
13 struct searchNode
*targnode
;
14 struct searchNode
*patnode
;
16 pcre_extra
*pcre_extra
;
19 void *regex_exe(searchCtx
*ctx
, struct searchNode
*thenode
, void *theinput
);
20 void regex_free(searchCtx
*ctx
, struct searchNode
*thenode
);
22 struct searchNode
*regex_parse(searchCtx
*ctx
, int type
, int argc
, char **argv
) {
23 struct regex_localdata
*localdata
;
24 struct searchNode
*thenode
;
25 struct searchNode
*targnode
, *patnode
;
29 pcre_extra
*pcre_extra
;
32 parseError
="regex: usage: regex source pattern";
36 targnode
=ctx
->parser(ctx
, type
, argv
[0]);
37 if (!(targnode
= coerceNode(ctx
,targnode
, RETURNTYPE_STRING
)))
40 patnode
=ctx
->parser(ctx
, type
, argv
[1]);
41 if (!(patnode
= coerceNode(ctx
,patnode
, RETURNTYPE_STRING
))) {
42 (targnode
->free
)(ctx
, targnode
);
46 if (!(patnode
->returntype
& RETURNTYPE_CONST
)) {
47 parseError
="regex: only constant regexes allowed";
48 (targnode
->free
)(ctx
, targnode
);
49 (patnode
->free
)(ctx
, patnode
);
53 if (!(pcre
=pcre_compile((char *)(patnode
->exe
)(ctx
, patnode
,NULL
),
54 PCRE_CASELESS
, &err
, &erroffset
, NULL
))) {
59 pcre_extra
=pcre_study(pcre
, 0, &err
);
61 if (!(localdata
=(struct regex_localdata
*)malloc(sizeof (struct regex_localdata
)))) {
62 parseError
= "malloc: could not allocate memory for this search.";
63 /* make sure we pcre_free() if we're not going to use them */
65 pcre_free(pcre_extra
);
72 localdata
->targnode
=targnode
;
73 localdata
->patnode
=patnode
;
75 localdata
->pcre_extra
=pcre_extra
;
77 if (!(thenode
= (struct searchNode
*)malloc(sizeof (struct searchNode
)))) {
78 /* couldn't malloc() memory for thenode, so free the pcre's and localdata to avoid leakage */
79 parseError
= "malloc: could not allocate memory for this search.";
80 if (localdata
->pcre_extra
)
81 pcre_free(localdata
->pcre_extra
);
84 pcre_free(localdata
->pcre
);
90 thenode
->returntype
= RETURNTYPE_BOOL
;
91 thenode
->localdata
= localdata
;
92 thenode
->exe
= regex_exe
;
93 thenode
->free
= regex_free
;
98 void *regex_exe(searchCtx
*ctx
, struct searchNode
*thenode
, void *theinput
) {
99 struct regex_localdata
*localdata
;
102 localdata
= thenode
->localdata
;
104 target
= (char *)((localdata
->targnode
->exe
)(ctx
, localdata
->targnode
,theinput
));
106 if (pcre_exec(localdata
->pcre
, localdata
->pcre_extra
,target
,strlen(target
),0,
115 void regex_free(searchCtx
*ctx
, struct searchNode
*thenode
) {
116 struct regex_localdata
*localdata
;
118 localdata
=thenode
->localdata
;
120 if (localdata
->pcre_extra
)
121 pcre_free(localdata
->pcre_extra
);
124 pcre_free(localdata
->pcre
);
126 (localdata
->patnode
->free
)(ctx
, localdata
->patnode
);
127 (localdata
->targnode
->free
)(ctx
, localdata
->targnode
);