]>
Commit | Line | Data |
---|---|---|
c86edd1d Q |
1 | /* |
2 | * EQ 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 | ||
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 | ||
9ce4f0be IB |
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 | } | |
c86edd1d Q |
34 | localdata->count = 0; |
35 | ||
9ce4f0be IB |
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 | } | |
c86edd1d Q |
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: | |
c3db6f7e | 89 | intval = (int)((long)(localdata->nodes[0]->exe)(localdata->nodes[0], RETURNTYPE_INT, theinput)); |
c86edd1d | 90 | for (i=1;i<localdata->count;i++) { |
c3db6f7e | 91 | if ((int)((long)(localdata->nodes[i]->exe)(localdata->nodes[i], RETURNTYPE_INT, theinput) != intval)) |
c86edd1d Q |
92 | return falseval(type); |
93 | } | |
94 | ||
95 | return trueval(type); | |
96 | ||
97 | case RETURNTYPE_BOOL: | |
c3db6f7e | 98 | intval = (int)((long)(localdata->nodes[0]->exe)(localdata->nodes[0], RETURNTYPE_BOOL, theinput)); |
c86edd1d | 99 | for (i=1;i<localdata->count;i++) { |
c3db6f7e | 100 | rval=(int)((long)(localdata->nodes[i]->exe)(localdata->nodes[i], RETURNTYPE_BOOL, theinput)); |
c86edd1d Q |
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 |