]> jfr.im git - irc/quakenet/newserv.git/blob - newsearch/ns-any.c
LUA: port luadb to dbapi2 to drop postgres dependency
[irc/quakenet/newserv.git] / newsearch / ns-any.c
1 /*
2 * ANY functionality
3 */
4
5 #include "newsearch.h"
6
7 #include <stdio.h>
8 #include <stdlib.h>
9
10 void any_free(searchCtx *ctx, struct searchNode *thenode);
11 void *any_exe(searchCtx *ctx, struct searchNode *thenode, void *theinput);
12
13 #define MAX_ITERATIONS 1000
14
15 struct any_localdata {
16 searchNode *genfn;
17 searchNode *lambdafn;
18 int hitlimit;
19 };
20
21 struct searchNode *any_parse(searchCtx *ctx, int argc, char **argv) {
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
37 if(!(localdata->genfn=ctx->parser(ctx, argv[0]))) {
38 free(localdata);
39 return NULL;
40 }
41
42 localdata->lambdafn = ctx->parser(ctx, argv[1]);
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
65 void any_free(searchCtx *ctx, struct searchNode *thenode) {
66 struct any_localdata *localdata = thenode->localdata;
67
68 if(localdata->hitlimit)
69 ctx->reply(senderNSExtern, "Warning: your expression was terminated for %d nicks as it hit the maximum iteration count.", localdata->hitlimit);
70
71 (localdata->genfn->free)(ctx, localdata->genfn);
72 (localdata->lambdafn->free)(ctx, localdata->lambdafn);
73
74 free(localdata);
75 free(thenode);
76 }
77
78 void *any_exe(searchCtx *ctx, struct searchNode *thenode, void *theinput) {
79 struct any_localdata *localdata = thenode->localdata;
80 int i;
81
82 /* if(localdata->hitlimit)
83 return (void *)0;
84 */
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
94 localdata->hitlimit++;
95 return (void *)0;
96 }