]> jfr.im git - irc/quakenet/newserv.git/blob - newsearch/ns-eq.c
branched in all of pauls stuff
[irc/quakenet/newserv.git] / newsearch / ns-eq.c
1 /*
2 * EQ 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 eq_localdata {
12 int count;
13 struct searchNode **nodes;
14 };
15
16 void eq_free(struct searchNode *thenode);
17 void *eq_exe(struct searchNode *thenode, int type, void *theinput);
18
19 struct searchNode *eq_parse(int type, int argc, char **argv) {
20 struct eq_localdata *localdata;
21 struct searchNode *thenode;
22 int i;
23
24 if (!(localdata = (struct eq_localdata *)malloc(sizeof(struct eq_localdata)))) {
25 parseError = "malloc: could not allocate memory for this search.";
26 return NULL;
27 }
28 if (!(localdata->nodes = (struct searchNode **)malloc(sizeof(struct searchNode *) * argc))) {
29 /* couldn't malloc() memory for localdata->nodes, so free localdata to avoid leakage */
30 parseError = "malloc: could not allocate memory for this search.";
31 free(localdata);
32 return NULL;
33 }
34 localdata->count = 0;
35
36 if (!(thenode = (struct searchNode *)malloc(sizeof(struct searchNode)))) {
37 /* couldn't malloc() memory for thenode, so free localdata and localdata->nodes to avoid leakage */
38 parseError = "malloc: could not allocate memory for this search.";
39 free(localdata->nodes);
40 free(localdata);
41 return NULL;
42 }
43
44 thenode->localdata = localdata;
45 thenode->returntype = RETURNTYPE_BOOL;
46 thenode->exe = eq_exe;
47 thenode->free = eq_free;
48
49 for (i=0;i<argc;i++) {
50 if (!(localdata->nodes[i] = search_parse(type, argv[i]))) {
51 eq_free(thenode);
52 return NULL;
53 }
54 localdata->count++;
55 }
56
57 return thenode;
58 }
59
60 void eq_free(struct searchNode *thenode) {
61 struct eq_localdata *localdata;
62 int i;
63
64 localdata=thenode->localdata;
65
66 for (i=0;i<localdata->count;i++) {
67 (localdata->nodes[i]->free)(localdata->nodes[i]);
68 }
69
70 free(localdata->nodes);
71 free(localdata);
72 free(thenode);
73 }
74
75 void *eq_exe(struct searchNode *thenode, int type, void *theinput) {
76 int i;
77 char *strval;
78 int intval;
79 int rval;
80 struct eq_localdata *localdata;
81
82 localdata=thenode->localdata;
83
84 if (localdata->count==0)
85 return trueval(type);
86
87 switch (localdata->nodes[0]->returntype & RETURNTYPE_TYPE) {
88 case RETURNTYPE_INT:
89 intval = (int)(localdata->nodes[0]->exe)(localdata->nodes[0], RETURNTYPE_INT, theinput);
90 for (i=1;i<localdata->count;i++) {
91 if ((int)(localdata->nodes[i]->exe)(localdata->nodes[i], RETURNTYPE_INT, theinput) != intval)
92 return falseval(type);
93 }
94
95 return trueval(type);
96
97 case RETURNTYPE_BOOL:
98 intval = (int)(localdata->nodes[0]->exe)(localdata->nodes[0], RETURNTYPE_BOOL, theinput);
99 for (i=1;i<localdata->count;i++) {
100 rval=(int)(localdata->nodes[i]->exe)(localdata->nodes[i], RETURNTYPE_BOOL, theinput);
101 if ((rval && !intval) || (!rval && intval)) { /* LOGICAL XOR GOES HERE FS */
102 return falseval(type);
103 }
104 }
105
106 return trueval(type);
107
108 case RETURNTYPE_STRING:
109 strval = (char *)(localdata->nodes[0]->exe)(localdata->nodes[0], RETURNTYPE_STRING, theinput);
110 for (i=1;i<localdata->count;i++) {
111 if (ircd_strcmp(strval, (char *)(localdata->nodes[i]->exe)(localdata->nodes[i], RETURNTYPE_STRING, theinput)))
112 return falseval(type);
113 }
114
115 return trueval(type);
116
117 default:
118 return falseval(type);
119 }
120 }
121