]> jfr.im git - irc/quakenet/newserv.git/blobdiff - newsearch/ns-match.c
SearchCtx should contain 'type' - this is to make life easier when defining new searc...
[irc/quakenet/newserv.git] / newsearch / ns-match.c
index fb8e95929fb6a7b8843adbc246752ef479680813..a4dba15d19a11e991ce2b2de4f610e6e99c9347f 100644 (file)
@@ -13,10 +13,10 @@ struct match_localdata {
   struct searchNode *patnode;
 };
 
-void *match_exe(struct searchNode *thenode, int type, void *theinput);
-void match_free(struct searchNode *thenode);
+void *match_exe(searchCtx *ctx, struct searchNode *thenode, void *theinput);
+void match_free(searchCtx *ctx, struct searchNode *thenode);
 
-struct searchNode *match_parse(int type, int argc, char **argv) {
+struct searchNode *match_parse(searchCtx *ctx, int argc, char **argv) {
   struct match_localdata *localdata;
   struct searchNode *thenode;
   struct searchNode *targnode, *patnode;
@@ -26,20 +26,31 @@ struct searchNode *match_parse(int type, int argc, char **argv) {
     return NULL;
   }
 
-  if (!(targnode = search_parse(type, argv[0])))
+  /* @fixme check this works with new parsing semantics */
+  targnode = ctx->parser(ctx, argv[0]);
+  if (!(targnode = coerceNode(ctx,targnode, RETURNTYPE_STRING)))
     return NULL;
 
-  if (!(patnode = search_parse(type, argv[1]))) {
-    (targnode->free)(targnode);
+  patnode = ctx->parser(ctx, argv[1]);
+  if (!(patnode = coerceNode(ctx,patnode, RETURNTYPE_STRING))) {
+    (targnode->free)(ctx, targnode);
     return NULL;
   }
 
-  localdata=(struct match_localdata *)malloc(sizeof (struct match_localdata));
+  if (!(localdata=(struct match_localdata *)malloc(sizeof (struct match_localdata)))) {
+    parseError = "malloc: could not allocate memory for this search.";
+    return NULL;
+  }
     
   localdata->targnode=targnode;
   localdata->patnode=patnode;
 
-  thenode = (struct searchNode *)malloc(sizeof (struct searchNode));
+  if (!(thenode=(struct searchNode *)malloc(sizeof(struct searchNode)))) {
+    /* couldn't malloc() memory for thenode, so free localdata to avoid leakage */
+    parseError = "malloc: could not allocate memory for this search.";
+    free(localdata);
+    return NULL;
+  }
 
   thenode->returntype = RETURNTYPE_BOOL;
   thenode->localdata = localdata;
@@ -49,36 +60,25 @@ struct searchNode *match_parse(int type, int argc, char **argv) {
   return thenode;
 }
 
-void *match_exe(struct searchNode *thenode, int type, void *theinput) {
+void *match_exe(searchCtx *ctx, struct searchNode *thenode, void *theinput) {
   struct match_localdata *localdata;
   char *pattern, *target;
-  int ret;
 
   localdata = thenode->localdata;
   
-  pattern = (char *)(localdata->patnode->exe) (localdata->patnode, RETURNTYPE_STRING, theinput);
-  target  = (char *)(localdata->targnode->exe)(localdata->targnode,RETURNTYPE_STRING, theinput);
+  pattern = (char *)(localdata->patnode->exe) (ctx, localdata->patnode, theinput);
+  target  = (char *)(localdata->targnode->exe)(ctx, localdata->targnode,theinput);
 
-  ret = match2strings(pattern, target);
-
-  switch(type) {
-  default:
-  case RETURNTYPE_INT:
-  case RETURNTYPE_BOOL:
-    return (void *)ret;
-    
-  case RETURNTYPE_STRING:
-    return (ret ? "1" : "");
-  }
+  return (void *)(long)match2strings(pattern, target);
 }
 
-void match_free(struct searchNode *thenode) {
+void match_free(searchCtx *ctx, struct searchNode *thenode) {
   struct match_localdata *localdata;
 
   localdata=thenode->localdata;
 
-  (localdata->patnode->free)(localdata->patnode);
-  (localdata->targnode->free)(localdata->targnode);
+  (localdata->patnode->free)(ctx, localdata->patnode);
+  (localdata->targnode->free)(ctx, localdata->targnode);
   free(localdata);
   free(thenode);
 }