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