]> jfr.im git - irc/quakenet/newserv.git/blame - newsearch/ns-or.c
Merged.
[irc/quakenet/newserv.git] / newsearch / ns-or.c
CommitLineData
c86edd1d
Q
1/*
2 * OR functionality
3 */
4
5#include "newsearch.h"
6
7#include <stdio.h>
8#include <stdlib.h>
9
10void or_free(struct searchNode *thenode);
11void *or_exe(struct searchNode *thenode, int type, void *theinput);
12
13struct or_localdata {
14 int count;
15 searchNode **nodes;
16};
17
18struct searchNode *or_parse(int type, int argc, char **argv) {
19 searchNode *thenode, *subnode;
20 struct or_localdata *localdata;
21 int i;
22
23 /* Set up our local data - a list of nodes to OR together */
9ce4f0be
IB
24 if (!(localdata=(struct or_localdata *)malloc(sizeof(struct or_localdata)))) {
25 parseError = "malloc: could not allocate memory for this search.";
26 return NULL;
27 }
28 if (!(localdata->nodes=(searchNode **)malloc(argc * sizeof(searchNode *)))) {
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
36 /* Allocate our actual node */
9ce4f0be
IB
37 if (!(thenode=(searchNode *)malloc(sizeof(searchNode)))) {
38 /* couldn't malloc() memory for thenode, so free localdata->nodes and localdata to avoid leakage */
39 parseError = "malloc: could not allocate memory for this search.";
40 free(localdata->nodes);
41 free(localdata);
42 return NULL;
43 }
c86edd1d
Q
44
45 thenode->returntype = RETURNTYPE_BOOL;
46 thenode->localdata = localdata;
47 thenode->exe = or_exe;
48 thenode->free = or_free;
49
50 for (i=0;i<argc;i++) {
51 subnode=search_parse(type, argv[i]); /* Propogate the search type */
52 if (subnode) {
53 localdata->nodes[localdata->count++] = subnode;
54 } else {
55 or_free(thenode);
56 return NULL;
57 }
58 }
59
60 return thenode;
61}
62
63void or_free(struct searchNode *thenode) {
64 struct or_localdata *localdata;
65 int i;
66
67 localdata=thenode->localdata;
68 for (i=0;i<localdata->count;i++) {
69 (localdata->nodes[i]->free)(localdata->nodes[i]);
70 }
71
72 free(localdata->nodes);
73 free(localdata);
74 free(thenode);
75}
76
77void *or_exe(struct searchNode *thenode, int type, void *theinput) {
78 int i;
79 void *ret;
80 struct or_localdata *localdata;
81
82 localdata=thenode->localdata;
83
84 for (i=0;i<localdata->count;i++) {
85 ret = (localdata->nodes[i]->exe)(localdata->nodes[i], RETURNTYPE_BOOL, theinput);
86 if (ret) {
87 switch (type) {
88 case RETURNTYPE_STRING:
89 return "1";
90
91 case RETURNTYPE_INT:
92 case RETURNTYPE_BOOL:
93 default:
94 return (void *)1;
95 }
96 }
97 }
98
99 switch(type) {
100 case RETURNTYPE_STRING:
101 return "";
102
103 case RETURNTYPE_INT:
104 case RETURNTYPE_BOOL:
105 default:
106 return NULL;
107 }
108}