]>
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
, int type
, 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 if (!(targnode
= search_parse(type
, argv
[0])))
32 if (!(patnode
= search_parse(type
, argv
[1]))) {
33 (targnode
->free
)(targnode
);
37 if (!(localdata
=(struct match_localdata
*)malloc(sizeof (struct match_localdata
)))) {
38 parseError
= "malloc: could not allocate memory for this search.";
42 localdata
->targnode
=targnode
;
43 localdata
->patnode
=patnode
;
45 if (!(thenode
=(struct searchNode
*)malloc(sizeof(struct searchNode
)))) {
46 /* couldn't malloc() memory for thenode, so free localdata to avoid leakage */
47 parseError
= "malloc: could not allocate memory for this search.";
52 thenode
->returntype
= RETURNTYPE_BOOL
;
53 thenode
->localdata
= localdata
;
54 thenode
->exe
= match_exe
;
55 thenode
->free
= match_free
;
60 void *match_exe(struct searchNode
*thenode
, int type
, void *theinput
) {
61 struct match_localdata
*localdata
;
62 char *pattern
, *target
;
65 localdata
= thenode
->localdata
;
67 pattern
= (char *)(localdata
->patnode
->exe
) (localdata
->patnode
, RETURNTYPE_STRING
, theinput
);
68 target
= (char *)(localdata
->targnode
->exe
)(localdata
->targnode
,RETURNTYPE_STRING
, theinput
);
70 ret
= match2strings(pattern
, target
);
76 return (void *)((long)ret
);
78 case RETURNTYPE_STRING
:
79 return (ret
? "1" : "");
83 void match_free(struct searchNode
*thenode
) {
84 struct match_localdata
*localdata
;
86 localdata
=thenode
->localdata
;
88 (localdata
->patnode
->free
)(localdata
->patnode
);
89 (localdata
->targnode
->free
)(localdata
->targnode
);