]> jfr.im git - irc/quakenet/newserv.git/blame - newsearch/ns-match.c
SearchCtx should contain 'type' - this is to make life easier when defining new searc...
[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.";
42 return NULL;
43 }
c86edd1d
Q
44
45 localdata->targnode=targnode;
46 localdata->patnode=patnode;
47
9ce4f0be
IB
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 }
c86edd1d
Q
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
c8be5183 63void *match_exe(searchCtx *ctx, struct searchNode *thenode, void *theinput) {
c86edd1d
Q
64 struct match_localdata *localdata;
65 char *pattern, *target;
c86edd1d
Q
66
67 localdata = thenode->localdata;
68
c8be5183
CP
69 pattern = (char *)(localdata->patnode->exe) (ctx, localdata->patnode, theinput);
70 target = (char *)(localdata->targnode->exe)(ctx, localdata->targnode,theinput);
c86edd1d 71
c54295ef 72 return (void *)(long)match2strings(pattern, target);
c86edd1d
Q
73}
74
c8be5183 75void match_free(searchCtx *ctx, struct searchNode *thenode) {
c86edd1d
Q
76 struct match_localdata *localdata;
77
78 localdata=thenode->localdata;
79
c8be5183
CP
80 (localdata->patnode->free)(ctx, localdata->patnode);
81 (localdata->targnode->free)(ctx, localdata->targnode);
c86edd1d
Q
82 free(localdata);
83 free(thenode);
84}
85