]> jfr.im git - irc/quakenet/newserv.git/blob - newsearch/ns-match.c
Merge
[irc/quakenet/newserv.git] / newsearch / ns-match.c
1 /*
2 * MATCH functionality
3 */
4
5 #include "newsearch.h"
6 #include "../lib/irc_string.h"
7
8 #include <stdio.h>
9 #include <stdlib.h>
10
11 struct match_localdata {
12 struct searchNode *targnode;
13 struct searchNode *patnode;
14 };
15
16 void *match_exe(struct searchNode *thenode, int type, void *theinput);
17 void match_free(struct searchNode *thenode);
18
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;
23
24 if (argc<2) {
25 parseError="match: usage: match source pattern";
26 return NULL;
27 }
28
29 if (!(targnode = search_parse(type, argv[0])))
30 return NULL;
31
32 if (!(patnode = search_parse(type, argv[1]))) {
33 (targnode->free)(targnode);
34 return NULL;
35 }
36
37 if (!(localdata=(struct match_localdata *)malloc(sizeof (struct match_localdata)))) {
38 parseError = "malloc: could not allocate memory for this search.";
39 return NULL;
40 }
41
42 localdata->targnode=targnode;
43 localdata->patnode=patnode;
44
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.";
48 free(localdata);
49 return NULL;
50 }
51
52 thenode->returntype = RETURNTYPE_BOOL;
53 thenode->localdata = localdata;
54 thenode->exe = match_exe;
55 thenode->free = match_free;
56
57 return thenode;
58 }
59
60 void *match_exe(struct searchNode *thenode, int type, void *theinput) {
61 struct match_localdata *localdata;
62 char *pattern, *target;
63 int ret;
64
65 localdata = thenode->localdata;
66
67 pattern = (char *)(localdata->patnode->exe) (localdata->patnode, RETURNTYPE_STRING, theinput);
68 target = (char *)(localdata->targnode->exe)(localdata->targnode,RETURNTYPE_STRING, theinput);
69
70 ret = match2strings(pattern, target);
71
72 switch(type) {
73 default:
74 case RETURNTYPE_INT:
75 case RETURNTYPE_BOOL:
76 return (void *)((long)ret);
77
78 case RETURNTYPE_STRING:
79 return (ret ? "1" : "");
80 }
81 }
82
83 void match_free(struct searchNode *thenode) {
84 struct match_localdata *localdata;
85
86 localdata=thenode->localdata;
87
88 (localdata->patnode->free)(localdata->patnode);
89 (localdata->targnode->free)(localdata->targnode);
90 free(localdata);
91 free(thenode);
92 }
93