]> jfr.im git - irc/quakenet/newserv.git/blobdiff - newsearch/newsearch_ast.c
CHANSERV: fix issue where chanserv_relay doesn't wait for db to be loaded before...
[irc/quakenet/newserv.git] / newsearch / newsearch_ast.c
index 6415790cb7fe0923fb4d48e196abcb82efd3a606..c872579e6e38d301c8c06a2403e77a90337bda9f 100644 (file)
@@ -11,12 +11,6 @@ typedef union exprunion {
   char *literal;
 } exprunion;
 
-typedef struct searchASTCache {
-  searchASTExpr *tree;
-  searchASTExpr *cache[AST_RECENT];
-  int nextpos;
-} searchASTCache;
-
 /* comares either a string and a string or an expression and an expression */
 static searchASTExpr *compareloc(searchASTExpr *expr, exprunion *loc) {
   if(expr->type == AST_NODE_LITERAL) {
@@ -40,8 +34,8 @@ static searchASTExpr *treesearch(searchASTExpr *expr, exprunion *loc) {
   
   if(expr->type == AST_NODE_CHILD) {
     int i;
-    for(i=0;i<expr->u.child->argc;i++) {
-      searchASTExpr *d = treesearch(expr->u.child->argv[i], loc);
+    for(i=0;i<expr->u.child.argc;i++) {
+      searchASTExpr *d = treesearch(&expr->u.child.argv[i], loc);
       if(d)
         return d;
     }
@@ -77,7 +71,7 @@ static void cachepush(searchASTCache *cache, searchASTExpr *expr) {
 }
 
 /* ast parser, the way we pass context around is very very hacky... */
-searchNode *search_astparse(searchCtx *ctx, int type, char *loc) {
+searchNode *search_astparse(searchCtx *ctx, char *loc) {
   searchASTCache *cache = ctx->arg;
   searchASTExpr *expr = cachesearch(cache, (exprunion *)&loc);
   searchNode *node;
@@ -101,13 +95,13 @@ searchNode *search_astparse(searchCtx *ctx, int type, char *loc) {
       node->free       = literal_free;
       return node;
     case AST_NODE_CHILD:
-      v = (char **)malloc(expr->u.child->argc * sizeof(char *));
+      v = (char **)malloc(expr->u.child.argc * sizeof(char *));
       if(!v) {
         parseError = "malloc: could not allocate memory for this search.";
         return NULL;
       }
-      for(i=0;i<expr->u.child->argc;i++) {
-        searchASTExpr *child = expr->u.child->argv[i];
+      for(i=0;i<expr->u.child.argc;i++) {
+        searchASTExpr *child = &expr->u.child.argv[i];
 
         cachepush(cache, child);
         switch(child->type) {
@@ -124,7 +118,7 @@ searchNode *search_astparse(searchCtx *ctx, int type, char *loc) {
         }
       }
 
-      node = expr->u.child->fn(ctx, type, expr->u.child->argc, v);
+      node = expr->u.child.fn(ctx, expr->u.child.argc, v);
       free(v);
       return node;
    default:
@@ -133,7 +127,7 @@ searchNode *search_astparse(searchCtx *ctx, int type, char *loc) {
   }
 }
 
-int ast_nicksearch(searchASTExpr *tree, replyFunc reply, void *sender, wallFunc wall, NickDisplayFunc display, HeaderFunc header, void *headerarg, int limit) {
+int ast_nicksearch(searchASTExpr *tree, replyFunc reply, void *sender, wallFunc wall, NickDisplayFunc display, HeaderFunc header, void *headerarg, int limit, array *targets) {
   searchCtx ctx;
   searchASTCache cache;
   searchNode *search;
@@ -142,37 +136,72 @@ int ast_nicksearch(searchASTExpr *tree, replyFunc reply, void *sender, wallFunc
   memset(&cache, 0, sizeof(cache));
   cache.tree = tree;
 
-  newsearch_ctxinit(&ctx, search_astparse, reply, wall, &cache);
+  newsearch_ctxinit(&ctx, search_astparse, reply, wall, &cache, reg_nicksearch, sender, display, limit, targets);
 
   buf[0] = '\0';
-  reply(sender, "Parsing: %s", ast_printtree(buf, sizeof(buf), tree));
-  search = ctx.parser(&ctx, SEARCHTYPE_NICK, (char *)tree);
+  if (!targets)
+    reply(sender, "Parsing: %s", ast_printtree(buf, sizeof(buf), tree, reg_nicksearch));
+  search = ctx.parser(&ctx, (char *)tree);
+  if(!search) {
+    if (!targets)
+      reply(sender, "Parse error: %s", parseError);
+    return CMD_ERROR;
+  }
+
+  if (!targets)
+    reply(sender, "Executing...");
+  if(header)  
+    header(sender, headerarg);
+  nicksearch_exe(search, &ctx);
+
+  (search->free)(&ctx, search);
+
+  return CMD_OK;
+}
+
+int ast_whowassearch(searchASTExpr *tree, replyFunc reply, void *sender, wallFunc wall, WhowasDisplayFunc display, HeaderFunc header, void *headerarg, int limit, array *targets) {
+  searchCtx ctx;
+  searchASTCache cache;
+  searchNode *search;
+  char buf[1024];
+
+  memset(&cache, 0, sizeof(cache));
+  cache.tree = tree;
+
+  newsearch_ctxinit(&ctx, search_astparse, reply, wall, &cache, reg_whowassearch, sender, display, limit, targets);
+
+  buf[0] = '\0';
+  reply(sender, "Parsing: %s", ast_printtree(buf, sizeof(buf), tree, reg_whowassearch));
+  search = ctx.parser(&ctx, (char *)tree);
   if(!search) {
     reply(sender, "Parse error: %s", parseError);
     return CMD_ERROR;
   }
 
   reply(sender, "Executing...");
-  if(header)  
+  if(header)
     header(sender, headerarg);
-  nicksearch_exe(search, &ctx, sender, display, limit);
+  whowassearch_exe(search, &ctx);
 
   (search->free)(&ctx, search);
 
   return CMD_OK;
 }
 
-int ast_chansearch(searchASTExpr *tree, replyFunc reply, void *sender, wallFunc wall, ChanDisplayFunc display, HeaderFunc header, void *headerarg, int limit) {
+int ast_chansearch(searchASTExpr *tree, replyFunc reply, void *sender, wallFunc wall, ChanDisplayFunc display, HeaderFunc header, void *headerarg, int limit, array *targets) {
   searchCtx ctx;
   searchASTCache cache;
   searchNode *search;
   char buf[1024];
 
-  newsearch_ctxinit(&ctx, search_astparse, reply, wall, &cache);
+  newsearch_ctxinit(&ctx, search_astparse, reply, wall, &cache, reg_chansearch, sender, display, limit, targets);
+
+  memset(&cache, 0, sizeof(cache));
+  cache.tree = tree;
 
   buf[0] = '\0';
-  reply(sender, "Parsing: %s", ast_printtree(buf, sizeof(buf), tree));
-  search = ctx.parser(&ctx, SEARCHTYPE_CHANNEL, (char *)tree);
+  reply(sender, "Parsing: %s", ast_printtree(buf, sizeof(buf), tree, reg_chansearch));
+  search = ctx.parser(&ctx, (char *)tree);
   if(!search) {
     reply(sender, "Parse error: %s", parseError);
     return CMD_ERROR;
@@ -181,14 +210,14 @@ int ast_chansearch(searchASTExpr *tree, replyFunc reply, void *sender, wallFunc
   reply(sender, "Executing...");
   if(header)  
     header(sender, headerarg);
-  chansearch_exe(search, &ctx, sender, display, limit);
+  chansearch_exe(search, &ctx);
 
   (search->free)(&ctx, search);
 
   return CMD_OK;
 }
 
-int ast_usersearch(searchASTExpr *tree, replyFunc reply, void *sender, wallFunc wall, UserDisplayFunc display, HeaderFunc header, void *headerarg, int limit) {
+int ast_usersearch(searchASTExpr *tree, replyFunc reply, void *sender, wallFunc wall, UserDisplayFunc display, HeaderFunc header, void *headerarg, int limit, array *targets) {
   searchCtx ctx;
   searchASTCache cache;
   searchNode *search;
@@ -197,11 +226,11 @@ int ast_usersearch(searchASTExpr *tree, replyFunc reply, void *sender, wallFunc
   memset(&cache, 0, sizeof(cache));
   cache.tree = tree;
 
-  newsearch_ctxinit(&ctx, search_astparse, reply, wall, &cache);
+  newsearch_ctxinit(&ctx, search_astparse, reply, wall, &cache, reg_usersearch, sender, display, limit, targets);
 
   buf[0] = '\0';
-  reply(sender, "Parsing: %s", ast_printtree(buf, sizeof(buf), tree));
-  search = ctx.parser(&ctx, SEARCHTYPE_USER, (char *)tree);
+  reply(sender, "Parsing: %s", ast_printtree(buf, sizeof(buf), tree, reg_usersearch));
+  search = ctx.parser(&ctx, (char *)tree);
   if(!search) {
     reply(sender, "Parse error: %s", parseError);
     return CMD_ERROR;
@@ -210,7 +239,7 @@ int ast_usersearch(searchASTExpr *tree, replyFunc reply, void *sender, wallFunc
   reply(sender, "Executing...");
   if(header)  
     header(sender, headerarg);
-  usersearch_exe(search, &ctx, sender, display, limit);
+  usersearch_exe(search, &ctx);
 
   (search->free)(&ctx, search);
 
@@ -219,27 +248,37 @@ int ast_usersearch(searchASTExpr *tree, replyFunc reply, void *sender, wallFunc
 
 
 /* horribly inefficient -- don't call me very often! */
-static char *ast_printtree_real(StringBuf *buf, searchASTExpr *expr) {
+static char *ast_printtree_real(StringBuf *buf, searchASTExpr *expr, searchCmd *cmd) {
   char lbuf[256];
   if(expr->type == AST_NODE_CHILD) {    
     int i;
-    sstring *command = getcommandname(searchTree, (void *)expr->u.child->fn);
+    sstring *command = getcommandname(cmd->searchtree, (void *)expr->u.child.fn);
 
     if(command) {
       snprintf(lbuf, sizeof(lbuf), "(%s", command->content);
     } else {
-      snprintf(lbuf, sizeof(lbuf), "(%p", expr->u.child->fn);
+      snprintf(lbuf, sizeof(lbuf), "(%p", expr->u.child.fn);
     }
     sbaddstr(buf, lbuf);
 
-    for(i=0;i<expr->u.child->argc;i++) {
+    for(i=0;i<expr->u.child.argc;i++) {
       sbaddchar(buf, ' ');
-      ast_printtree_real(buf, expr->u.child->argv[i]);
+      ast_printtree_real(buf, &expr->u.child.argv[i], cmd);
     }
     sbaddchar(buf, ')');
 
   } else if(expr->type == AST_NODE_LITERAL) {
-    sbaddstr(buf, expr->u.literal);
+    char *p;
+
+    sbaddchar(buf, '"');
+
+    for(p=expr->u.literal;*p;p++) {
+      if(*p == '\\' || *p == '"')
+        sbaddchar(buf, '\\');
+      sbaddchar(buf, *p);
+    }
+
+    sbaddchar(buf, '"');
   } else {
     sbaddstr(buf, "???");
   }
@@ -247,7 +286,7 @@ static char *ast_printtree_real(StringBuf *buf, searchASTExpr *expr) {
   return buf->buf;
 }
 
-char *ast_printtree(char *buf, size_t bufsize, searchASTExpr *expr) {
+char *ast_printtree(char *buf, size_t bufsize, searchASTExpr *expr, searchCmd *cmd) {
   StringBuf b;
   char *p;
 
@@ -255,7 +294,7 @@ char *ast_printtree(char *buf, size_t bufsize, searchASTExpr *expr) {
   b.len = 0;
   b.buf = buf;
 
-  p = ast_printtree_real(&b, expr);
+  p = ast_printtree_real(&b, expr, cmd);
  
   sbterminate(&b);
   return p;