]>
Commit | Line | Data |
---|---|---|
f1903ace | 1 | /* |
c7f7a584 | 2 | * LT functionality |
f1903ace CP |
3 | */ |
4 | ||
5 | #include "newsearch.h" | |
4ad1cf7a | 6 | #include "../lib/irc_string.h" |
f1903ace CP |
7 | |
8 | #include <stdio.h> | |
9 | #include <stdlib.h> | |
10 | ||
11 | struct lt_localdata { | |
c7f7a584 | 12 | int type; |
f1903ace CP |
13 | int count; |
14 | struct searchNode **nodes; | |
15 | }; | |
16 | ||
c8be5183 CP |
17 | void lt_free(searchCtx *ctx, struct searchNode *thenode); |
18 | void *lt_exe(searchCtx *ctx, struct searchNode *thenode, void *theinput); | |
f1903ace | 19 | |
f33f3f52 | 20 | struct searchNode *lt_parse(searchCtx *ctx, int argc, char **argv) { |
f1903ace CP |
21 | struct lt_localdata *localdata; |
22 | struct searchNode *thenode; | |
23 | int i; | |
24 | ||
9ce4f0be IB |
25 | if (!(localdata = (struct lt_localdata *)malloc(sizeof(struct lt_localdata)))) { |
26 | parseError = "malloc: could not allocate memory for this search."; | |
27 | return NULL; | |
28 | } | |
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."; | |
32 | free(localdata); | |
33 | return NULL; | |
34 | } | |
f1903ace CP |
35 | localdata->count = 0; |
36 | ||
c7f7a584 | 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 */ | |
9ce4f0be IB |
39 | parseError = "malloc: could not allocate memory for this search."; |
40 | free(localdata->nodes); | |
41 | free(localdata); | |
42 | return NULL; | |
43 | } | |
f1903ace CP |
44 | |
45 | thenode->localdata = localdata; | |
46 | thenode->returntype = RETURNTYPE_BOOL; | |
47 | thenode->exe = lt_exe; | |
48 | thenode->free = lt_free; | |
49 | ||
50 | for (i=0;i<argc;i++) { | |
c7f7a584 | 51 | /* Parse the node.. */ |
f33f3f52 | 52 | localdata->nodes[i] = ctx->parser(ctx, argv[i]); |
c7f7a584 | 53 | |
54 | /* Subsequent nodes get coerced to match the type of the first node */ | |
55 | if (i) | |
c8be5183 | 56 | localdata->nodes[i]=coerceNode(ctx, localdata->nodes[i],localdata->type); |
c7f7a584 | 57 | |
58 | /* If a node didn't parse, give up */ | |
59 | if (!localdata->nodes[i]) { | |
c8be5183 | 60 | lt_free(ctx, thenode); |
f1903ace CP |
61 | return NULL; |
62 | } | |
c7f7a584 | 63 | |
64 | if (!i) { | |
65 | /* First arg determines the type */ | |
66 | localdata->type = localdata->nodes[0]->returntype & RETURNTYPE_TYPE; | |
67 | } | |
68 | ||
f1903ace CP |
69 | localdata->count++; |
70 | } | |
71 | ||
72 | return thenode; | |
73 | } | |
74 | ||
c8be5183 | 75 | void lt_free(searchCtx *ctx, struct searchNode *thenode) { |
f1903ace CP |
76 | struct lt_localdata *localdata; |
77 | int i; | |
78 | ||
79 | localdata=thenode->localdata; | |
80 | ||
81 | for (i=0;i<localdata->count;i++) { | |
c7f7a584 | 82 | if (localdata->nodes[i]) |
c8be5183 | 83 | (localdata->nodes[i]->free)(ctx, localdata->nodes[i]); |
f1903ace CP |
84 | } |
85 | ||
86 | free(localdata->nodes); | |
87 | free(localdata); | |
88 | free(thenode); | |
89 | } | |
90 | ||
c8be5183 | 91 | void *lt_exe(searchCtx *ctx, struct searchNode *thenode, void *theinput) { |
f1903ace CP |
92 | int i; |
93 | char *strval; | |
94 | int intval; | |
f1903ace CP |
95 | struct lt_localdata *localdata; |
96 | ||
97 | localdata=thenode->localdata; | |
98 | ||
99 | if (localdata->count==0) | |
c7f7a584 | 100 | return (void *)1; |
f1903ace | 101 | |
c7f7a584 | 102 | switch (localdata->type) { |
f1903ace | 103 | case RETURNTYPE_INT: |
c7f7a584 | 104 | case RETURNTYPE_BOOL: |
c8be5183 | 105 | intval = (int)((long)(localdata->nodes[0]->exe)(ctx, localdata->nodes[0], theinput)); |
f1903ace | 106 | for (i=1;i<localdata->count;i++) { |
c8be5183 | 107 | if ((int)((long)(localdata->nodes[i]->exe)(ctx, localdata->nodes[i], theinput) <= intval)) |
c7f7a584 | 108 | return (void *)0; |
f1903ace | 109 | } |
c7f7a584 | 110 | return (void *)1; |
f1903ace CP |
111 | |
112 | case RETURNTYPE_STRING: | |
c8be5183 | 113 | strval = (char *)(localdata->nodes[0]->exe)(ctx, localdata->nodes[0], theinput); |
f1903ace | 114 | for (i=1;i<localdata->count;i++) { |
c8be5183 | 115 | if (ircd_strcmp(strval, (char *)(localdata->nodes[i]->exe)(ctx, localdata->nodes[i], theinput)) >= 0) |
c7f7a584 | 116 | return (void *)0; |
f1903ace | 117 | } |
c7f7a584 | 118 | return (void *)1; |
f1903ace CP |
119 | |
120 | default: | |
c7f7a584 | 121 | return (void *)0; |
f1903ace CP |
122 | } |
123 | } | |
124 |