]> 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(searchCtx *ctx, struct searchNode *thenode, void *theinput);
17 void match_free(searchCtx *ctx, struct searchNode *thenode);
18
19 struct searchNode *match_parse(searchCtx *ctx, 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 /* @fixme check this works with new parsing semantics */
30 targnode = ctx->parser(ctx, type, argv[0]);
31 if (!(targnode = coerceNode(ctx,targnode, RETURNTYPE_STRING)))
32 return NULL;
33
34 patnode = ctx->parser(ctx, type, argv[1]);
35 if (!(patnode = coerceNode(ctx,patnode, RETURNTYPE_STRING))) {
36 (targnode->free)(ctx, targnode);
37 return NULL;
38 }
39
40 if (!(localdata=(struct match_localdata *)malloc(sizeof (struct match_localdata)))) {
41 parseError = "malloc: could not allocate memory for this search.";
42 return NULL;
43 }
44
45 localdata->targnode=targnode;
46 localdata->patnode=patnode;
47
48 if (!(thenode=(struct searchNode *)malloc(sizeof(struct searchNode)))) {
49 /* couldn't malloc() memory for thenode, so free localdata to avoid leakage */
50 parseError = "malloc: could not allocate memory for this search.";
51 free(localdata);
52 return NULL;
53 }
54
55 thenode->returntype = RETURNTYPE_BOOL;
56 thenode->localdata = localdata;
57 thenode->exe = match_exe;
58 thenode->free = match_free;
59
60 return thenode;
61 }
62
63 void *match_exe(searchCtx *ctx, struct searchNode *thenode, void *theinput) {
64 struct match_localdata *localdata;
65 char *pattern, *target;
66
67 localdata = thenode->localdata;
68
69 pattern = (char *)(localdata->patnode->exe) (ctx, localdata->patnode, theinput);
70 target = (char *)(localdata->targnode->exe)(ctx, localdata->targnode,theinput);
71
72 return (void *)(long)match2strings(pattern, target);
73 }
74
75 void match_free(searchCtx *ctx, struct searchNode *thenode) {
76 struct match_localdata *localdata;
77
78 localdata=thenode->localdata;
79
80 (localdata->patnode->free)(ctx, localdata->patnode);
81 (localdata->targnode->free)(ctx, localdata->targnode);
82 free(localdata);
83 free(thenode);
84 }
85