]>
jfr.im git - irc/quakenet/newserv.git/blob - newsearch/ns-match.c
6 #include "../lib/irc_string.h"
11 struct match_localdata
{
12 struct searchNode
*targnode
;
13 struct searchNode
*patnode
;
16 void *match_exe(searchCtx
*ctx
, struct searchNode
*thenode
, void *theinput
);
17 void match_free(searchCtx
*ctx
, struct searchNode
*thenode
);
19 struct searchNode
*match_parse(searchCtx
*ctx
, int argc
, char **argv
) {
20 struct match_localdata
*localdata
;
21 struct searchNode
*thenode
;
22 struct searchNode
*targnode
, *patnode
;
25 parseError
="match: usage: match source pattern";
29 /* @fixme check this works with new parsing semantics */
30 targnode
= ctx
->parser(ctx
, argv
[0]);
31 if (!(targnode
= coerceNode(ctx
,targnode
, RETURNTYPE_STRING
)))
34 patnode
= ctx
->parser(ctx
, argv
[1]);
35 if (!(patnode
= coerceNode(ctx
,patnode
, RETURNTYPE_STRING
))) {
36 (targnode
->free
)(ctx
, targnode
);
40 if (!(localdata
=(struct match_localdata
*)malloc(sizeof (struct match_localdata
)))) {
41 parseError
= "malloc: could not allocate memory for this search.";
45 localdata
->targnode
=targnode
;
46 localdata
->patnode
=patnode
;
48 if (!(thenode
=(struct searchNode
*)malloc(sizeof(struct searchNode
)))) {
49 /* couldn't malloc() memory for thenode, so free localdata to avoid leakage */
50 parseError
= "malloc: could not allocate memory for this search.";
55 thenode
->returntype
= RETURNTYPE_BOOL
;
56 thenode
->localdata
= localdata
;
57 thenode
->exe
= match_exe
;
58 thenode
->free
= match_free
;
63 void *match_exe(searchCtx
*ctx
, struct searchNode
*thenode
, void *theinput
) {
64 struct match_localdata
*localdata
;
65 char *pattern
, *target
;
67 localdata
= thenode
->localdata
;
69 pattern
= (char *)(localdata
->patnode
->exe
) (ctx
, localdata
->patnode
, theinput
);
70 target
= (char *)(localdata
->targnode
->exe
)(ctx
, localdata
->targnode
,theinput
);
72 return (void *)(long)match2strings(pattern
, target
);
75 void match_free(searchCtx
*ctx
, struct searchNode
*thenode
) {
76 struct match_localdata
*localdata
;
78 localdata
=thenode
->localdata
;
80 (localdata
->patnode
->free
)(ctx
, localdata
->patnode
);
81 (localdata
->targnode
->free
)(ctx
, localdata
->targnode
);