]> jfr.im git - irc/quakenet/newserv.git/blobdiff - newsearch/ns-and.c
CHANSERV: better batcher error handling for expired accounts/accounts with no email.
[irc/quakenet/newserv.git] / newsearch / ns-and.c
index 692d963606fb09340332d4378a1e8db1bd49de6b..2a341c226ed77c139b2a540cff5e0b0f5ffff3aa 100644 (file)
@@ -7,26 +7,40 @@
 #include <stdio.h>
 #include <stdlib.h>
 
-void and_free(struct searchNode *thenode);
-void *and_exe(struct searchNode *thenode, int type, void *theinput);
+void and_free(searchCtx *ctx, struct searchNode *thenode);
+void *and_exe(searchCtx *ctx, struct searchNode *thenode, void *theinput);
 
 struct and_localdata {
   int count;
   searchNode **nodes;
 };
 
-struct searchNode *and_parse(int type, int argc, char **argv) {
+struct searchNode *and_parse(searchCtx *ctx, int argc, char **argv) {
   searchNode *thenode, *subnode;
   struct and_localdata *localdata;
   int i;
 
   /* Set up our local data - a list of nodes to AND together */
-  localdata=(struct and_localdata *)malloc(sizeof(struct and_localdata));
-  localdata->nodes=(searchNode **)malloc(argc * sizeof(searchNode *));
+  if (!(localdata=(struct and_localdata *)malloc(sizeof(struct and_localdata)))) {
+    parseError = "malloc: could not allocate memory for this search.";
+    return NULL;
+  }
+  if (!(localdata->nodes=(searchNode **)malloc(argc * sizeof(searchNode *)))) {
+    /* couldn't malloc() memory for localdata->nodes, so free localdata to avoid leakage */
+    parseError = "malloc: could not allocate memory for this search.";
+    free(localdata);
+    return NULL;
+  }
   localdata->count=0;
 
   /* Allocate our actual node */
-  thenode=(searchNode *)malloc(sizeof(searchNode));
+  if (!(thenode=(struct searchNode *)malloc(sizeof(struct searchNode)))) {
+    /* couldn't malloc() memory for thenode, so free localdata->nodes and localdata to avoid leakage */
+    parseError = "malloc: could not allocate memory for this search.";
+    free(localdata->nodes);
+    free(localdata);
+    return NULL;
+  }
 
   thenode->returntype   = RETURNTYPE_BOOL;
   thenode->localdata    = localdata;
@@ -34,11 +48,12 @@ struct searchNode *and_parse(int type, int argc, char **argv) {
   thenode->free         = and_free;
 
   for (i=0;i<argc;i++) {
-    subnode=search_parse(type, argv[i]); /* Propogate the search type */
+    subnode=ctx->parser(ctx, argv[i]); /* Propogate the search type */
+    subnode=coerceNode(ctx, subnode, RETURNTYPE_BOOL); /* Needs to return BOOL */
     if (subnode) {
       localdata->nodes[localdata->count++] = subnode;
     } else {
-      and_free(thenode);
+      and_free(ctx, thenode); /* ?? */
       return NULL;
     }
   }
@@ -46,13 +61,13 @@ struct searchNode *and_parse(int type, int argc, char **argv) {
   return thenode;
 }
 
-void and_free(struct searchNode *thenode) {
+void and_free(searchCtx *ctx, struct searchNode *thenode) {
   struct and_localdata *localdata;
   int i;
 
   localdata=thenode->localdata;
   for (i=0;i<localdata->count;i++) {
-    (localdata->nodes[i]->free)(localdata->nodes[i]);
+    (localdata->nodes[i]->free)(ctx, localdata->nodes[i]);
   }
   
   free(localdata->nodes);
@@ -60,36 +75,15 @@ void and_free(struct searchNode *thenode) {
   free(thenode);
 }
 
-void *and_exe(struct searchNode *thenode, int type, void *theinput) {
+void *and_exe(searchCtx *ctx, struct searchNode *thenode, void *theinput) {
   int i;
-  void *ret;
   struct and_localdata *localdata;
   
   localdata=thenode->localdata;
 
   for (i=0;i<localdata->count;i++) {
-    ret = (localdata->nodes[i]->exe)(localdata->nodes[i], RETURNTYPE_BOOL, theinput);
-    if (ret == NULL) {
-      switch (type) {
-       
-      case RETURNTYPE_INT:
-      case RETURNTYPE_BOOL:
-       return NULL;
-
-      case RETURNTYPE_STRING:
-       return "";
-      }
-    }
-  }
-
-  switch (type) {
-  case RETURNTYPE_INT:
-  case RETURNTYPE_BOOL:
-    return (void *)1;
-  case RETURNTYPE_STRING:
-    return "1";
+    if (!(localdata->nodes[i]->exe)(ctx, localdata->nodes[i], theinput))
+      return NULL;
   }
-
-  return NULL;
+  return (void *)1;
 }