]> jfr.im git - irc/quakenet/newserv.git/blob - newsearch/ns-match.c
LUA: port luadb to dbapi2 to drop postgres dependency
[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 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, argv[0]);
31 if (!(targnode = coerceNode(ctx,targnode, RETURNTYPE_STRING)))
32 return NULL;
33
34 patnode = ctx->parser(ctx, 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 (targnode->free)(ctx, targnode);
43 (patnode->free)(ctx, patnode);
44 return NULL;
45 }
46
47 localdata->targnode=targnode;
48 localdata->patnode=patnode;
49
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.";
53 (targnode->free)(ctx, targnode);
54 (patnode->free)(ctx, patnode);
55 free(localdata);
56 return NULL;
57 }
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
67 void *match_exe(searchCtx *ctx, struct searchNode *thenode, void *theinput) {
68 struct match_localdata *localdata;
69 char *pattern, *target;
70
71 localdata = thenode->localdata;
72
73 pattern = (char *)(localdata->patnode->exe) (ctx, localdata->patnode, theinput);
74 target = (char *)(localdata->targnode->exe)(ctx, localdata->targnode,theinput);
75
76 return (void *)(long)match2strings(pattern, target);
77 }
78
79 void match_free(searchCtx *ctx, struct searchNode *thenode) {
80 struct match_localdata *localdata;
81
82 localdata=thenode->localdata;
83
84 (localdata->patnode->free)(ctx, localdata->patnode);
85 (localdata->targnode->free)(ctx, localdata->targnode);
86 free(localdata);
87 free(thenode);
88 }
89