]> jfr.im git - irc/quakenet/newserv.git/blame - newsearch/ns-match.c
Merge pull request #132 from retropc/lua_country
[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
c8be5183
CP
16void *match_exe(searchCtx *ctx, struct searchNode *thenode, void *theinput);
17void match_free(searchCtx *ctx, struct searchNode *thenode);
c86edd1d 18
f33f3f52 19struct searchNode *match_parse(searchCtx *ctx, int argc, char **argv) {
c86edd1d
Q
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
c8be5183 29 /* @fixme check this works with new parsing semantics */
f33f3f52 30 targnode = ctx->parser(ctx, argv[0]);
c8be5183 31 if (!(targnode = coerceNode(ctx,targnode, RETURNTYPE_STRING)))
c86edd1d
Q
32 return NULL;
33
f33f3f52 34 patnode = ctx->parser(ctx, argv[1]);
c8be5183
CP
35 if (!(patnode = coerceNode(ctx,patnode, RETURNTYPE_STRING))) {
36 (targnode->free)(ctx, targnode);
c86edd1d
Q
37 return NULL;
38 }
39
9ce4f0be
IB
40 if (!(localdata=(struct match_localdata *)malloc(sizeof (struct match_localdata)))) {
41 parseError = "malloc: could not allocate memory for this search.";
179f6932
CP
42 (targnode->free)(ctx, targnode);
43 (patnode->free)(ctx, patnode);
9ce4f0be
IB
44 return NULL;
45 }
c86edd1d
Q
46
47 localdata->targnode=targnode;
48 localdata->patnode=patnode;
49
9ce4f0be
IB
50 if (!(thenode=(struct searchNode *)malloc(sizeof(struct searchNode)))) {
51 /* couldn't malloc() memory for thenode, so free localdata to avoid leakage */
52 parseError = "malloc: could not allocate memory for this search.";
179f6932
CP
53 (targnode->free)(ctx, targnode);
54 (patnode->free)(ctx, patnode);
9ce4f0be
IB
55 free(localdata);
56 return NULL;
57 }
c86edd1d
Q
58
59 thenode->returntype = RETURNTYPE_BOOL;
60 thenode->localdata = localdata;
61 thenode->exe = match_exe;
62 thenode->free = match_free;
63
64 return thenode;
65}
66
c8be5183 67void *match_exe(searchCtx *ctx, struct searchNode *thenode, void *theinput) {
c86edd1d
Q
68 struct match_localdata *localdata;
69 char *pattern, *target;
c86edd1d
Q
70
71 localdata = thenode->localdata;
72
c8be5183
CP
73 pattern = (char *)(localdata->patnode->exe) (ctx, localdata->patnode, theinput);
74 target = (char *)(localdata->targnode->exe)(ctx, localdata->targnode,theinput);
c86edd1d 75
c54295ef 76 return (void *)(long)match2strings(pattern, target);
c86edd1d
Q
77}
78
c8be5183 79void match_free(searchCtx *ctx, struct searchNode *thenode) {
c86edd1d
Q
80 struct match_localdata *localdata;
81
82 localdata=thenode->localdata;
83
c8be5183
CP
84 (localdata->patnode->free)(ctx, localdata->patnode);
85 (localdata->targnode->free)(ctx, localdata->targnode);
c86edd1d
Q
86 free(localdata);
87 free(thenode);
88}
89