]> jfr.im git - irc/quakenet/newserv.git/blame - newsearch/ns-any.c
merge
[irc/quakenet/newserv.git] / newsearch / ns-any.c
CommitLineData
b697c21c
CP
1/*
2 * ANY functionality
3 */
4
5#include "newsearch.h"
6
7#include <stdio.h>
8#include <stdlib.h>
9
10void any_free(searchCtx *ctx, struct searchNode *thenode);
11void *any_exe(searchCtx *ctx, struct searchNode *thenode, void *theinput);
12
13#define MAX_ITERATIONS 1000
14
15struct any_localdata {
16 searchNode *genfn;
17 searchNode *lambdafn;
18 int hitlimit;
19};
20
f33f3f52 21struct searchNode *any_parse(searchCtx *ctx, int argc, char **argv) {
b697c21c
CP
22 searchNode *thenode;
23 struct any_localdata *localdata;
24
25 if(argc < 2) {
26 parseError = "any: usage: any (generatorfn x) (fn ... (var x) ...)";
27 return NULL;
28 }
29
30 if(!(localdata=(struct any_localdata *)malloc(sizeof(struct any_localdata)))) {
31 parseError = "malloc: could not allocate memory for this search.";
32 return NULL;
33 }
34
35 localdata->hitlimit = 0;
36
f33f3f52 37 if(!(localdata->genfn=ctx->parser(ctx, argv[0]))) {
b697c21c
CP
38 free(localdata);
39 return NULL;
40 }
41
f33f3f52 42 localdata->lambdafn = ctx->parser(ctx, argv[1]);
b697c21c
CP
43 if(!(localdata->lambdafn = coerceNode(ctx, localdata->lambdafn, RETURNTYPE_BOOL))) {
44 (localdata->genfn->free)(ctx, localdata->genfn);
45 free(localdata);
46 return NULL;
47 }
48
49 if(!(thenode=(struct searchNode *)malloc(sizeof(struct searchNode)))) {
50 parseError = "malloc: could not allocate memory for this search.";
51 (localdata->genfn->free)(ctx, localdata->genfn);
52 (localdata->lambdafn->free)(ctx, localdata->lambdafn);
53 free(localdata);
54 return NULL;
55 }
56
57 thenode->returntype = RETURNTYPE_BOOL;
58 thenode->localdata = localdata;
59 thenode->exe = any_exe;
60 thenode->free = any_free;
61
62 return thenode;
63}
64
65void any_free(searchCtx *ctx, struct searchNode *thenode) {
66 struct any_localdata *localdata = thenode->localdata;
67
68 if(localdata->hitlimit)
987820c5 69 ctx->reply(senderNSExtern, "Warning: your expression was terminated for %d nicks as it hit the maximum iteration count.", localdata->hitlimit);
b697c21c
CP
70
71 (localdata->genfn->free)(ctx, localdata->genfn);
72 (localdata->lambdafn->free)(ctx, localdata->lambdafn);
73
74 free(localdata);
75 free(thenode);
76}
77
78void *any_exe(searchCtx *ctx, struct searchNode *thenode, void *theinput) {
79 struct any_localdata *localdata = thenode->localdata;
80 int i;
81
987820c5 82/* if(localdata->hitlimit)
b697c21c 83 return (void *)0;
987820c5 84*/
b697c21c
CP
85
86 for(i=0;i<MAX_ITERATIONS;i++) {
87 if(!(localdata->genfn->exe)(ctx, localdata->genfn, theinput))
88 return (void *)0;
89
90 if((localdata->lambdafn->exe)(ctx, localdata->lambdafn, theinput))
91 return (void *)1;
92 }
93
987820c5 94 localdata->hitlimit++;
b697c21c
CP
95 return (void *)0;
96}