]> jfr.im git - irc/quakenet/newserv.git/blame - newsearch/ns-match.c
Add jupe support
[irc/quakenet/newserv.git] / newsearch / ns-match.c
CommitLineData
c86edd1d
Q
1/*
2 * MATCH functionality
3 */
4
5#include "newsearch.h"
4ad1cf7a 6#include "../lib/irc_string.h"
c86edd1d
Q
7
8#include <stdio.h>
9#include <stdlib.h>
10
11struct match_localdata {
12 struct searchNode *targnode;
13 struct searchNode *patnode;
14};
15
16void *match_exe(struct searchNode *thenode, int type, void *theinput);
17void match_free(struct searchNode *thenode);
18
19struct 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 localdata=(struct match_localdata *)malloc(sizeof (struct match_localdata));
38
39 localdata->targnode=targnode;
40 localdata->patnode=patnode;
41
42 thenode = (struct searchNode *)malloc(sizeof (struct searchNode));
43
44 thenode->returntype = RETURNTYPE_BOOL;
45 thenode->localdata = localdata;
46 thenode->exe = match_exe;
47 thenode->free = match_free;
48
49 return thenode;
50}
51
52void *match_exe(struct searchNode *thenode, int type, void *theinput) {
53 struct match_localdata *localdata;
54 char *pattern, *target;
55 int ret;
56
57 localdata = thenode->localdata;
58
59 pattern = (char *)(localdata->patnode->exe) (localdata->patnode, RETURNTYPE_STRING, theinput);
60 target = (char *)(localdata->targnode->exe)(localdata->targnode,RETURNTYPE_STRING, theinput);
61
62 ret = match2strings(pattern, target);
63
64 switch(type) {
65 default:
66 case RETURNTYPE_INT:
67 case RETURNTYPE_BOOL:
68 return (void *)ret;
69
70 case RETURNTYPE_STRING:
71 return (ret ? "1" : "");
72 }
73}
74
75void match_free(struct searchNode *thenode) {
76 struct match_localdata *localdata;
77
78 localdata=thenode->localdata;
79
80 (localdata->patnode->free)(localdata->patnode);
81 (localdata->targnode->free)(localdata->targnode);
82 free(localdata);
83 free(thenode);
84}
85