]>
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(struct searchNode
*thenode
, void *theinput
);
17 void match_free(struct searchNode
*thenode
);
19 struct searchNode
*match_parse(int type
, 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 targnode
= search_parse(type
, argv
[0]);
30 if (!(targnode
= coerceNode(targnode
, RETURNTYPE_STRING
)))
33 patnode
= search_parse(type
, argv
[1]);
34 if (!(patnode
= coerceNode(patnode
, RETURNTYPE_STRING
))) {
35 (targnode
->free
)(targnode
);
39 if (!(localdata
=(struct match_localdata
*)malloc(sizeof (struct match_localdata
)))) {
40 parseError
= "malloc: could not allocate memory for this search.";
44 localdata
->targnode
=targnode
;
45 localdata
->patnode
=patnode
;
47 if (!(thenode
=(struct searchNode
*)malloc(sizeof(struct searchNode
)))) {
48 /* couldn't malloc() memory for thenode, so free localdata to avoid leakage */
49 parseError
= "malloc: could not allocate memory for this search.";
54 thenode
->returntype
= RETURNTYPE_BOOL
;
55 thenode
->localdata
= localdata
;
56 thenode
->exe
= match_exe
;
57 thenode
->free
= match_free
;
62 void *match_exe(struct searchNode
*thenode
, void *theinput
) {
63 struct match_localdata
*localdata
;
64 char *pattern
, *target
;
66 localdata
= thenode
->localdata
;
68 pattern
= (char *)(localdata
->patnode
->exe
) (localdata
->patnode
, theinput
);
69 target
= (char *)(localdata
->targnode
->exe
)(localdata
->targnode
,theinput
);
71 return (void *)match2strings(pattern
, target
);
74 void match_free(struct searchNode
*thenode
) {
75 struct match_localdata
*localdata
;
77 localdata
=thenode
->localdata
;
79 (localdata
->patnode
->free
)(localdata
->patnode
);
80 (localdata
->targnode
->free
)(localdata
->targnode
);