]> jfr.im git - irc/quakenet/newserv.git/blame - newsearch/ns-match.c
More safe warning fixes.
[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
c7f7a584 16void *match_exe(struct searchNode *thenode, void *theinput);
c86edd1d
Q
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
c7f7a584 29 targnode = search_parse(type, argv[0]);
30 if (!(targnode = coerceNode(targnode, RETURNTYPE_STRING)))
c86edd1d
Q
31 return NULL;
32
c7f7a584 33 patnode = search_parse(type, argv[1]);
34 if (!(patnode = coerceNode(patnode, RETURNTYPE_STRING))) {
c86edd1d
Q
35 (targnode->free)(targnode);
36 return NULL;
37 }
38
9ce4f0be
IB
39 if (!(localdata=(struct match_localdata *)malloc(sizeof (struct match_localdata)))) {
40 parseError = "malloc: could not allocate memory for this search.";
41 return NULL;
42 }
c86edd1d
Q
43
44 localdata->targnode=targnode;
45 localdata->patnode=patnode;
46
9ce4f0be
IB
47 if (!(thenode=(struct searchNode *)malloc(sizeof(struct searchNode)))) {
48 /* couldn't malloc() memory for thenode, so free localdata to avoid leakage */
49 parseError = "malloc: could not allocate memory for this search.";
50 free(localdata);
51 return NULL;
52 }
c86edd1d
Q
53
54 thenode->returntype = RETURNTYPE_BOOL;
55 thenode->localdata = localdata;
56 thenode->exe = match_exe;
57 thenode->free = match_free;
58
59 return thenode;
60}
61
c7f7a584 62void *match_exe(struct searchNode *thenode, void *theinput) {
c86edd1d
Q
63 struct match_localdata *localdata;
64 char *pattern, *target;
c86edd1d
Q
65
66 localdata = thenode->localdata;
67
c7f7a584 68 pattern = (char *)(localdata->patnode->exe) (localdata->patnode, theinput);
69 target = (char *)(localdata->targnode->exe)(localdata->targnode,theinput);
c86edd1d 70
c7f7a584 71 return (void *)match2strings(pattern, target);
c86edd1d
Q
72}
73
74void match_free(struct searchNode *thenode) {
75 struct match_localdata *localdata;
76
77 localdata=thenode->localdata;
78
79 (localdata->patnode->free)(localdata->patnode);
80 (localdata->targnode->free)(localdata->targnode);
81 free(localdata);
82 free(thenode);
83}
84