]>
jfr.im git - irc/quakenet/newserv.git/blob - newsearch/ns-eq.c
99ee0842d198c55265193d45c036a7071d967d85
6 #include "../lib/irc_string.h"
14 struct searchNode
**nodes
;
17 void eq_free(searchCtx
*ctx
, struct searchNode
*thenode
);
18 void *eq_exe(searchCtx
*ctx
, struct searchNode
*thenode
, void *theinput
);
20 struct searchNode
*eq_parse(searchCtx
*ctx
, int argc
, char **argv
) {
21 struct eq_localdata
*localdata
;
22 struct searchNode
*thenode
;
25 if (!(localdata
= (struct eq_localdata
*)malloc(sizeof(struct eq_localdata
)))) {
26 parseError
= "malloc: could not allocate memory for this search.";
29 if (!(localdata
->nodes
= (struct searchNode
**)malloc(sizeof(struct searchNode
*) * argc
))) {
30 /* couldn't malloc() memory for localdata->nodes, so free localdata to avoid leakage */
31 parseError
= "malloc: could not allocate memory for this search.";
37 if (!(thenode
= (struct searchNode
*)malloc(sizeof(struct searchNode
)))) {
38 /* couldn't malloc() memory for thenode, so free localdata and localdata->nodes to avoid leakage */
39 parseError
= "malloc: could not allocate memory for this search.";
40 free(localdata
->nodes
);
45 thenode
->localdata
= localdata
;
46 thenode
->returntype
= RETURNTYPE_BOOL
;
47 thenode
->exe
= eq_exe
;
48 thenode
->free
= eq_free
;
50 for (i
=0;i
<argc
;i
++) {
51 /* Parse the node.. */
52 localdata
->nodes
[i
] = ctx
->parser(ctx
, argv
[i
]);
54 /* Subsequent nodes get coerced to match the type of the first node */
56 localdata
->nodes
[i
]=coerceNode(ctx
,localdata
->nodes
[i
],localdata
->type
);
58 /* If a node didn't parse, give up */
59 if (!localdata
->nodes
[i
]) {
60 eq_free(ctx
, thenode
);
65 /* First arg determines the type */
66 localdata
->type
= localdata
->nodes
[0]->returntype
& RETURNTYPE_TYPE
;
75 void eq_free(searchCtx
*ctx
, struct searchNode
*thenode
) {
76 struct eq_localdata
*localdata
;
79 localdata
=thenode
->localdata
;
81 for (i
=0;i
<localdata
->count
;i
++) {
82 if (localdata
->nodes
[i
])
83 (localdata
->nodes
[i
]->free
)(ctx
, localdata
->nodes
[i
]);
86 free(localdata
->nodes
);
91 void *eq_exe(searchCtx
*ctx
, struct searchNode
*thenode
, void *theinput
) {
96 struct eq_localdata
*localdata
;
98 localdata
=thenode
->localdata
;
100 if (localdata
->count
==0)
103 switch (localdata
->type
) {
105 intval
= (int)((long)(localdata
->nodes
[0]->exe
)(ctx
, localdata
->nodes
[0], theinput
));
106 for (i
=1;i
<localdata
->count
;i
++) {
107 if ((int)((long)(localdata
->nodes
[i
]->exe
)(ctx
, localdata
->nodes
[i
], theinput
) != intval
))
112 case RETURNTYPE_BOOL
:
113 intval
= (int)((long)(localdata
->nodes
[0]->exe
)(ctx
, localdata
->nodes
[0], theinput
));
114 for (i
=1;i
<localdata
->count
;i
++) {
115 rval
=(int)((long)(localdata
->nodes
[i
]->exe
)(ctx
, localdata
->nodes
[i
], theinput
));
116 if ((rval
&& !intval
) || (!rval
&& intval
)) { /* LOGICAL XOR GOES HERE FS */
122 case RETURNTYPE_STRING
:
123 strval
= (char *)(localdata
->nodes
[0]->exe
)(ctx
, localdata
->nodes
[0], theinput
);
124 for (i
=1;i
<localdata
->count
;i
++) {
125 if (ircd_strcmp(strval
, (char *)(localdata
->nodes
[i
]->exe
)(ctx
, localdata
->nodes
[i
], theinput
)))