]>
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.";
42 (targnode
->free
)(ctx
, targnode
);
43 (patnode
->free
)(ctx
, patnode
);
47 localdata
->targnode
=targnode
;
48 localdata
->patnode
=patnode
;
50 if (!(thenode
=(struct searchNode
*)malloc(sizeof(struct searchNode
)))) {
51 /* couldn't malloc() memory for thenode, so free localdata to avoid leakage */
52 parseError
= "malloc: could not allocate memory for this search.";
53 (targnode
->free
)(ctx
, targnode
);
54 (patnode
->free
)(ctx
, patnode
);
59 thenode
->returntype
= RETURNTYPE_BOOL
;
60 thenode
->localdata
= localdata
;
61 thenode
->exe
= match_exe
;
62 thenode
->free
= match_free
;
67 void *match_exe(searchCtx
*ctx
, struct searchNode
*thenode
, void *theinput
) {
68 struct match_localdata
*localdata
;
69 char *pattern
, *target
;
71 localdata
= thenode
->localdata
;
73 pattern
= (char *)(localdata
->patnode
->exe
) (ctx
, localdata
->patnode
, theinput
);
74 target
= (char *)(localdata
->targnode
->exe
)(ctx
, localdata
->targnode
,theinput
);
76 return (void *)(long)match2strings(pattern
, target
);
79 void match_free(searchCtx
*ctx
, struct searchNode
*thenode
) {
80 struct match_localdata
*localdata
;
82 localdata
=thenode
->localdata
;
84 (localdata
->patnode
->free
)(ctx
, localdata
->patnode
);
85 (localdata
->targnode
->free
)(ctx
, localdata
->targnode
);