]> jfr.im git - irc/quakenet/newserv.git/blobdiff - newsearch/ns-regex.c
TRUSTS: require sqlite
[irc/quakenet/newserv.git] / newsearch / ns-regex.c
index f6dda5dd25f07fdf59106cea736bd83c3a89ca90..5d86ecc7f97133064e66a3a1aaaf785336709b12 100644 (file)
@@ -16,10 +16,10 @@ struct regex_localdata {
   pcre_extra *pcre_extra;
 };
 
-void *regex_exe(struct searchNode *thenode, int type, void *theinput);
-void regex_free(struct searchNode *thenode);
+void *regex_exe(searchCtx *ctx, struct searchNode *thenode, void *theinput);
+void regex_free(searchCtx *ctx, struct searchNode *thenode);
 
-struct searchNode *regex_parse(int type, int argc, char **argv) {
+struct searchNode *regex_parse(searchCtx *ctx, int argc, char **argv) {
   struct regex_localdata *localdata;
   struct searchNode *thenode;
   struct searchNode *targnode, *patnode;
@@ -33,22 +33,24 @@ struct searchNode *regex_parse(int type, int argc, char **argv) {
     return NULL;
   }
 
-  if (!(targnode = search_parse(type, argv[0])))
+  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;
   }
 
   if (!(patnode->returntype & RETURNTYPE_CONST)) {
     parseError="regex: only constant regexes allowed";
-    (targnode->free)(targnode);
-    (patnode->free)(patnode);
+    (targnode->free)(ctx, targnode);
+    (patnode->free)(ctx, patnode);
     return NULL;
   }
 
-  if (!(pcre=pcre_compile((char *)(patnode->exe)(patnode,RETURNTYPE_STRING,NULL),
+  if (!(pcre=pcre_compile((char *)(patnode->exe)(ctx, patnode,NULL),
                          PCRE_CASELESS, &err, &erroffset, NULL))) {
     parseError=err;
     return NULL;
@@ -56,14 +58,34 @@ struct searchNode *regex_parse(int type, int argc, char **argv) {
 
   pcre_extra=pcre_study(pcre, 0, &err);
 
-  localdata=(struct regex_localdata *)malloc(sizeof (struct regex_localdata));
+  if (!(localdata=(struct regex_localdata *)malloc(sizeof (struct regex_localdata)))) {
+    parseError = "malloc: could not allocate memory for this search.";
+    /* make sure we pcre_free() if we're not going to use them */
+    if (pcre_extra)
+      pcre_free(pcre_extra);
+
+    if (pcre)
+      pcre_free(pcre);
+    return NULL;
+  }
     
   localdata->targnode=targnode;
   localdata->patnode=patnode;
   localdata->pcre=pcre;
   localdata->pcre_extra=pcre_extra;
 
-  thenode = (struct searchNode *)malloc(sizeof (struct searchNode));
+  if (!(thenode = (struct searchNode *)malloc(sizeof (struct searchNode)))) {
+    /* couldn't malloc() memory for thenode, so free the pcre's and localdata to avoid leakage */
+    parseError = "malloc: could not allocate memory for this search.";
+    if (localdata->pcre_extra)
+      pcre_free(localdata->pcre_extra);
+
+    if (localdata->pcre)
+      pcre_free(localdata->pcre);
+
+    free(localdata);
+    return NULL;
+  }
 
   thenode->returntype = RETURNTYPE_BOOL;
   thenode->localdata = localdata;
@@ -73,24 +95,24 @@ struct searchNode *regex_parse(int type, int argc, char **argv) {
   return thenode;
 }
 
-void *regex_exe(struct searchNode *thenode, int type, void *theinput) {
+void *regex_exe(searchCtx *ctx, struct searchNode *thenode, void *theinput) {
   struct regex_localdata *localdata;
   char *target;
 
   localdata = thenode->localdata;
   
-  target  = (char *)((localdata->targnode->exe)(localdata->targnode,RETURNTYPE_STRING, theinput));
+  target  = (char *)((localdata->targnode->exe)(ctx, localdata->targnode,theinput));
 
   if (pcre_exec(localdata->pcre, localdata->pcre_extra,target,strlen(target),0,
                0,NULL,0)) {
     /* didn't match */
-    return falseval(type);
+    return (void *)0;
   } else {
-    return trueval(type);
+    return (void *)1;
   }
 }
 
-void regex_free(struct searchNode *thenode) {
+void regex_free(searchCtx *ctx, struct searchNode *thenode) {
   struct regex_localdata *localdata;
 
   localdata=thenode->localdata;
@@ -101,8 +123,8 @@ void regex_free(struct searchNode *thenode) {
   if (localdata->pcre)
     pcre_free(localdata->pcre);
 
-  (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);
 }