#include <stdio.h>
#include <stdarg.h>
-#include "newsearch.h"
+#include <string.h>
#include "../irc/irc_config.h"
#include "../lib/irc_string.h"
#include "../control/control.h"
#include "../lib/splitline.h"
#include "../lib/version.h"
+#include "../lib/stringbuf.h"
+#include "../lib/strlfunc.h"
+#include "../lib/array.h"
+#include "newsearch.h"
MODULE_VERSION("");
-CommandTree *searchTree;
-CommandTree *chanOutputTree;
-CommandTree *nickOutputTree;
+CommandTree *searchCmdTree;
+searchList *globalterms = NULL;
int do_nicksearch(void *source, int cargc, char **cargv);
int do_chansearch(void *source, int cargc, char **cargv);
+int do_usersearch(void *source, int cargc, char **cargv);
void printnick_channels(searchCtx *, nick *, nick *);
void printchannel(searchCtx *, nick *, chanindex *);
void printchannel_topic(searchCtx *, nick *, chanindex *);
void printchannel_services(searchCtx *, nick *, chanindex *);
-void registersearchterm(char *term, parseFunc parsefunc);
-void deregistersearchterm(char *term, parseFunc parsefunc);
+UserDisplayFunc defaultuserfn = printuser;
+NickDisplayFunc defaultnickfn = printnick;
+ChanDisplayFunc defaultchanfn = printchannel;
-void regchandisp(const char *name, ChanDisplayFunc handler) {
- addcommandtotree(chanOutputTree, name, 0, 0, (CommandHandler)handler);
-}
+searchCmd *reg_nicksearch, *reg_chansearch, *reg_usersearch;
+
+searchCmd *registersearchcommand(char *name, int level, CommandHandler cmd, void *defaultdisplayfunc) {
+ searchCmd *acmd;
+ searchList *sl;
+
+ registercontrolhelpcmd(name, NO_OPER,4, cmd, "Usage: <criteria\nSearches with the given criteria");
+
+ acmd=(struct searchCmd *)malloc(sizeof(struct searchCmd));
+
+ acmd->handler = cmd;
-void unregchandisp(const char *name, ChanDisplayFunc handler) {
- deletecommandfromtree(chanOutputTree, name, (CommandHandler)handler);
+ acmd->name = getsstring( name, NSMAX_COMMAND_LEN);
+ acmd->outputtree = newcommandtree();
+ acmd->searchtree = newcommandtree();
+
+ addcommandtotree(searchCmdTree, name, 0, 0, (CommandHandler)acmd);
+
+ sl = globalterms;
+ while (sl) {
+ registersearchterm( acmd, sl->name->content, sl->cmd);
+ sl = sl->next;
+ }
+
+ return acmd;
}
-void regnickdisp(const char *name, NickDisplayFunc handler) {
- addcommandtotree(nickOutputTree, name, 0, 0, (CommandHandler)handler);
+void deregistersearchcommand(searchCmd *scmd) {
+ deregistercontrolcmd(scmd->name->content, (CommandHandler)scmd->handler);
+ destroycommandtree(scmd->outputtree);
+ destroycommandtree(scmd->searchtree);
+ freesstring(scmd->name);
+ free(scmd);
}
-void unregnickdisp(const char *name, NickDisplayFunc handler) {
- deletecommandfromtree(nickOutputTree, name, (CommandHandler)handler);
+void regdisp( searchCmd *cmd, const char *name, void *handler) {
+ addcommandtotree(cmd->outputtree, name, 0, 0, (CommandHandler) handler);
+}
+
+void unregdisp( searchCmd *cmd, const char *name, void *handler ) {
+ deletecommandfromtree(cmd->outputtree, name, (CommandHandler) handler);
}
+void *findcommandinlist( searchList *sl, char *name){
+ while(sl) {
+ if(strcmp(sl->name->content,name) == 0 ) {
+ return sl;
+ }
+ sl = sl->next;
+ }
+ return NULL;
+}
+
const char *parseError;
/* used for *_free functions that need to warn users of certain things
i.e. hitting too many users in a (kill) or (gline) */
nick *senderNSExtern;
void _init() {
- searchTree=newcommandtree();
- chanOutputTree=newcommandtree();
- nickOutputTree=newcommandtree();
+ searchCmdTree=newcommandtree();
+
+ reg_nicksearch = (searchCmd *)registersearchcommand("nicksearch",NO_OPER,&do_nicksearch, printnick);
+ reg_chansearch = (searchCmd *)registersearchcommand("chansearch",NO_OPER,&do_chansearch, printchannel);
+ reg_usersearch = (searchCmd *)registersearchcommand("usersearch",NO_OPER,&do_usersearch, printuser);
/* Boolean operations */
- registersearchterm("and",and_parse);
- registersearchterm("not",not_parse);
- registersearchterm("or",or_parse);
+ registerglobalsearchterm("and",and_parse);
+ registerglobalsearchterm("not",not_parse);
+ registerglobalsearchterm("or",or_parse);
- registersearchterm("eq",eq_parse);
+ registerglobalsearchterm("eq",eq_parse);
- registersearchterm("lt",lt_parse);
- registersearchterm("gt",gt_parse);
+ registerglobalsearchterm("lt",lt_parse);
+ registerglobalsearchterm("gt",gt_parse);
/* String operations */
- registersearchterm("match",match_parse);
- registersearchterm("regex",regex_parse);
- registersearchterm("length",length_parse);
+ registerglobalsearchterm("match",match_parse);
+ registerglobalsearchterm("regex",regex_parse);
+ registerglobalsearchterm("length",length_parse);
/* Nickname operations */
- registersearchterm("hostmask",hostmask_parse);
- registersearchterm("realname",realname_parse);
- registersearchterm("authname",authname_parse);
- registersearchterm("authts",authts_parse);
- registersearchterm("ident",ident_parse);
- registersearchterm("host",host_parse);
- registersearchterm("channel",channel_parse);
- registersearchterm("timestamp",timestamp_parse);
- registersearchterm("country",country_parse);
- registersearchterm("ip",ip_parse);
- registersearchterm("channels",channels_parse);
- registersearchterm("server",server_parse);
- registersearchterm("authid",authid_parse);
+ registersearchterm(reg_nicksearch, "hostmask",hostmask_parse); /* nick only */
+ registersearchterm(reg_nicksearch, "realname",realname_parse); /* nick only */
+ registersearchterm(reg_nicksearch, "authname",authname_parse); /* nick only */
+ registersearchterm(reg_nicksearch, "authts",authts_parse); /* nick only */
+ registersearchterm(reg_nicksearch, "ident",ident_parse); /* nick only */
+ registersearchterm(reg_nicksearch, "host",host_parse); /* nick only */
+ registersearchterm(reg_nicksearch, "channel",channel_parse); /* nick only */
+ registersearchterm(reg_nicksearch, "timestamp",timestamp_parse); /* nick only */
+ registersearchterm(reg_nicksearch, "country",country_parse); /* nick only */
+ registersearchterm(reg_nicksearch, "ip",ip_parse); /* nick only */
+ registersearchterm(reg_nicksearch, "channels",channels_parse); /* nick only */
+ registersearchterm(reg_nicksearch, "server",server_parse); /* nick only */
+ registersearchterm(reg_nicksearch, "authid",authid_parse); /* nick only */
/* Channel operations */
- registersearchterm("exists",exists_parse);
- registersearchterm("services",services_parse);
- registersearchterm("size",size_parse);
- registersearchterm("name",name_parse);
- registersearchterm("topic",topic_parse);
- registersearchterm("oppct",oppct_parse);
- registersearchterm("uniquehostpct",hostpct_parse);
- registersearchterm("authedpct",authedpct_parse);
- registersearchterm("kick",kick_parse);
+ registersearchterm(reg_chansearch, "exists",exists_parse); /* channel only */
+ registersearchterm(reg_chansearch, "services",services_parse); /* channel only */
+ registersearchterm(reg_chansearch, "size",size_parse); /* channel only */
+ registersearchterm(reg_chansearch, "name",name_parse); /* channel only */
+ registersearchterm(reg_chansearch, "topic",topic_parse); /* channel only */
+ registersearchterm(reg_chansearch, "oppct",oppct_parse); /* channel only */
+ registersearchterm(reg_chansearch, "uniquehostpct",hostpct_parse); /* channel only */
+ registersearchterm(reg_chansearch, "authedpct",authedpct_parse); /* channel only */
+ registersearchterm(reg_chansearch, "kick",kick_parse); /* channel only */
/* Nickname / channel operations */
- registersearchterm("modes",modes_parse);
- registersearchterm("nick",nick_parse);
+ registersearchterm(reg_chansearch, "modes",modes_parse);
+ registersearchterm(reg_nicksearch, "modes",modes_parse);
+ registersearchterm(reg_chansearch, "nick",nick_parse);
+ registersearchterm(reg_nicksearch, "nick",nick_parse);
/* Kill / gline parameters */
- registersearchterm("kill",kill_parse);
- registersearchterm("gline",gline_parse);
-
- /* Notice functionality */
- registersearchterm("notice",notice_parse);
+ registersearchterm(reg_chansearch,"kill",kill_parse);
+ registersearchterm(reg_chansearch,"gline",gline_parse);
+ registersearchterm(reg_nicksearch,"kill",kill_parse);
+ registersearchterm(reg_nicksearch,"gline",gline_parse);
+
+ /* Iteration functionality */
+ registerglobalsearchterm("any",any_parse);
+ registerglobalsearchterm("all",all_parse);
+ registerglobalsearchterm("var",var_parse);
+
+ /* Iterable functions */
+ registersearchterm(reg_nicksearch, "channeliter",channeliter_parse); /* nick only */
+ /* Notice functionality */
+ registersearchterm(reg_chansearch,"notice",notice_parse);
+ registersearchterm(reg_nicksearch,"notice",notice_parse);
+
/* Nick output filters */
- regnickdisp("default",printnick);
- regnickdisp("channels",printnick_channels);
+ regdisp(reg_nicksearch,"default",printnick);
+ regdisp(reg_nicksearch,"channels",printnick_channels);
/* Channel output filters */
- regchandisp("default",printchannel);
- regchandisp("topic",printchannel_topic);
- regchandisp("services",printchannel_services);
+ regdisp(reg_chansearch,"default",printchannel);
+ regdisp(reg_chansearch,"topic",printchannel_topic);
+ regdisp(reg_chansearch,"services",printchannel_services);
- registercontrolhelpcmd("nicksearch",NO_OPER,4,do_nicksearch, "Usage: nicksearch <criteria>\nSearches for nicknames with the given criteria.");
- registercontrolhelpcmd("chansearch",NO_OPER,4,do_chansearch, "Usage: chansearch <criteria>\nSearches for channels with the given criteria.");
+ /* Nick output filters */
+ regdisp(reg_usersearch,"default",printuser);
+
}
void _fini() {
- destroycommandtree(searchTree);
- destroycommandtree(chanOutputTree);
- destroycommandtree(nickOutputTree);
- deregistercontrolcmd("nicksearch", do_nicksearch);
- deregistercontrolcmd("chansearch", do_chansearch);
+ searchList *sl, *psl;
+ int i,n;
+ Command *cmdlist[100];
+
+ sl = globalterms;
+ while (sl) {
+ psl = sl;
+ sl = sl->next;
+
+ n=getcommandlist(searchCmdTree,cmdlist,100);
+ for(i=0;i<n;i++) {
+ deregistersearchterm( (searchCmd *)cmdlist[i]->handler, psl->name->content, psl->cmd);
+ }
+
+ freesstring(psl->name);
+ free(psl);
+
+ }
+
+ deregistersearchcommand( reg_nicksearch );
+ deregistersearchcommand( reg_chansearch );
+ deregistersearchcommand( reg_usersearch );
+ destroycommandtree( searchCmdTree );
}
-void registersearchterm(char *term, parseFunc parsefunc) {
- addcommandtotree(searchTree, term, 0, 0, (CommandHandler) parsefunc);
+void registerglobalsearchterm(char *term, parseFunc parsefunc) {
+ searchList *sl = malloc(sizeof(searchList));
+ int i,n;
+ Command *cmdlist[100];
+
+ sl->cmd = parsefunc;
+ sl->name = getsstring(term, NSMAX_COMMAND_LEN);
+ sl->next = NULL;
+
+ if ( globalterms != NULL ) {
+ sl->next = globalterms;
+ }
+ globalterms = sl;
+
+ n=getcommandlist(searchCmdTree,cmdlist,100);
+ for(i=0;i<n;i++) {
+ registersearchterm( (searchCmd *)cmdlist[i]->handler,term, parsefunc);
+ }
}
-void deregistersearchterm(char *term, parseFunc parsefunc) {
- deletecommandfromtree(searchTree, term, (CommandHandler) parsefunc);
+void deregisterglobalsearchterm(char *term, parseFunc parsefunc) {
+ int i,n;
+ Command *cmdlist[100];
+ searchList *sl, *psl=NULL;
+
+ sl = globalterms;
+ while (sl) {
+ if ( strcmp( sl->name->content, term) == 0 ) {
+ break;
+ }
+ psl = sl;
+ sl = sl->next;
+ }
+
+ if (sl) {
+ if( psl ) {
+ psl->next = sl->next;
+ }
+
+ n=getcommandlist(searchCmdTree,cmdlist,100);
+ for(i=0;i<n;i++) {
+ deregistersearchterm( (searchCmd *)cmdlist[i]->handler, term, parsefunc);
+ }
+ freesstring(sl->name);
+ free(sl);
+ }
+}
+
+void registersearchterm(searchCmd *cmd, char *term, parseFunc parsefunc) {
+ addcommandtotree(cmd->searchtree, term, 0, 0, (CommandHandler) parsefunc);
+}
+
+void deregistersearchterm(searchCmd *cmd, char *term, parseFunc parsefunc) {
+ deletecommandfromtree(cmd->searchtree, term, (CommandHandler) parsefunc);
}
static void controlwallwrapper(int level, char *format, ...) {
va_end(ap);
}
-int do_nicksearch_real(replyFunc reply, wallFunc wall, void *source, int cargc, char **cargv) {
- nick *sender = senderNSExtern = source;
- struct searchNode *search;
- int limit=500;
+int parseopts(int cargc, char **cargv, int *arg, int *limit, void **subset, void **display, CommandTree *sl, replyFunc reply, void *sender) {
char *ch;
- int arg=0;
- struct Command *cmd;
- NickDisplayFunc display=printnick;
- searchCtx ctx;
-
- if (cargc<1)
- return CMD_USAGE;
-
+ Command *cmd;
+ struct irc_in_addr sin; unsigned char bits;
+
if (*cargv[0] == '-') {
/* options */
- arg++;
+ (*arg)++;
for (ch=cargv[0]+1;*ch;ch++) {
switch(*ch) {
case 'l':
- if (cargc<arg) {
+ if (cargc<*arg) {
reply(sender,"Error: -l switch requires an argument");
return CMD_USAGE;
}
- limit=strtoul(cargv[arg++],NULL,10);
+ *limit=strtoul(cargv[(*arg)++],NULL,10);
break;
case 'd':
- if (cargc<arg) {
+ if (cargc<*arg) {
reply(sender,"Error: -d switch requires an argument");
return CMD_USAGE;
}
- cmd=findcommandintree(nickOutputTree, cargv[arg], 1);
+ cmd=findcommandintree(sl, cargv[*arg],1);
if (!cmd) {
- reply(sender,"Error: unknown output format %s",cargv[arg]);
+ reply(sender,"Error: unknown output format %s",cargv[*arg]);
return CMD_USAGE;
}
- display=(NickDisplayFunc)cmd->handler;
- arg++;
+ *display=(void *)cmd->handler;
+ (*arg)++;
+ break;
+
+ case 's':
+ if (cargc<*arg) {
+ reply(sender,"Error: -s switch requires an argument");
+ return CMD_USAGE;
+ }
+ if (ipmask_parse(cargv[*arg], &sin, &bits) == 0) {
+ reply(sender, "Error: Invalid CIDR mask supplied");
+ return CMD_USAGE;
+ }
+ *subset = (void *)refnode(iptree, &sin, bits);
+ (*arg)++;
break;
default:
}
}
+ return CMD_OK;
+}
+
+void newsearch_ctxinit(searchCtx *ctx, searchParseFunc searchfn, replyFunc replyfn, wallFunc wallfn, void *arg, searchCmd *cmd) {
+ memset(ctx, 0, sizeof(searchCtx));
+
+ ctx->reply = replyfn;
+ ctx->wall = wallfn;
+ ctx->parser = searchfn;
+ ctx->arg = arg;
+ ctx->searchcmd = cmd;
+}
+
+int do_nicksearch_real(replyFunc reply, wallFunc wall, void *source, int cargc, char **cargv) {
+ nick *sender = source;
+ struct searchNode *search;
+ int limit=500;
+ int arg=0;
+ NickDisplayFunc display=defaultnickfn;
+ searchCtx ctx;
+ int ret;
+
+ if (cargc<1)
+ return CMD_USAGE;
+
+ ret = parseopts(cargc, cargv, &arg, &limit, NULL, (void **)&display, reg_nicksearch->outputtree, reply, sender);
+ if(ret != CMD_OK)
+ return ret;
+
if (arg>=cargc) {
reply(sender,"No search terms - aborting.");
return CMD_ERROR;
rejoinline(cargv[arg],cargc-arg);
}
- ctx.parser = search_parse;
- ctx.reply = reply;
- ctx.wall = wall;
+ newsearch_ctxinit(&ctx, search_parse, reply, wall, NULL, reg_nicksearch);
- if (!(search = ctx.parser(&ctx, SEARCHTYPE_NICK, cargv[arg]))) {
+ if (!(search = ctx.parser(&ctx, cargv[arg]))) {
reply(sender,"Parse error: %s",parseError);
return CMD_ERROR;
}
unsigned int tchans=0,uchans=0;
struct channel **cs;
nick *np;
-
+ senderNSExtern = sender;
+
/* Get a marker value to mark "seen" channels for unique count */
cmarker=nextchanmarker();
}
int do_chansearch_real(replyFunc reply, wallFunc wall, void *source, int cargc, char **cargv) {
- nick *sender = senderNSExtern = source;
+ nick *sender = source;
struct searchNode *search;
int limit=500;
- char *ch;
int arg=0;
- struct Command *cmd;
- ChanDisplayFunc display=printchannel;
+ ChanDisplayFunc display=defaultchanfn;
searchCtx ctx;
+ int ret;
if (cargc<1)
return CMD_USAGE;
- if (*cargv[0] == '-') {
- /* options */
- arg++;
-
- for (ch=cargv[0]+1;*ch;ch++) {
- switch(*ch) {
- case 'l':
- if (cargc<arg) {
- reply(sender,"Error: -l switch requires an argument");
- return CMD_USAGE;
- }
- limit=strtoul(cargv[arg++],NULL,10);
- break;
-
- case 'd':
- if (cargc<arg) {
- reply(sender,"Error: -d switch requires an argument");
- return CMD_USAGE;
- }
- cmd=findcommandintree(chanOutputTree, cargv[arg], 1);
- if (!cmd) {
- reply(sender,"Error: unknown output format %s",cargv[arg]);
- return CMD_USAGE;
- }
- display=(ChanDisplayFunc)cmd->handler;
- arg++;
- break;
-
- default:
- reply(sender,"Unrecognised flag -%c.",*ch);
- }
- }
- }
+ ret = parseopts(cargc, cargv, &arg, &limit, NULL, (void **)&display, reg_chansearch->outputtree, reply, sender);
+ if(ret != CMD_OK)
+ return ret;
if (arg>=cargc) {
reply(sender,"No search terms - aborting.");
rejoinline(cargv[arg],cargc-arg);
}
- ctx.parser = search_parse;
- ctx.reply = reply;
- ctx.wall = wall;
-
- if (!(search = ctx.parser(&ctx, SEARCHTYPE_CHANNEL, cargv[arg]))) {
+ newsearch_ctxinit(&ctx, search_parse, reply, wall, NULL, reg_chansearch);
+ if (!(search = ctx.parser(&ctx, cargv[arg]))) {
reply(sender,"Parse error: %s",parseError);
return CMD_ERROR;
}
int i;
chanindex *cip;
int matches = 0;
+ senderNSExtern = sender;
search=coerceNode(ctx, search, RETURNTYPE_BOOL);
ctx->reply(sender,"--- End of list: %d matches", matches);
}
-struct coercedata {
- struct searchNode *child;
- union {
- char *stringbuf;
- unsigned long val;
- } u;
-};
+int do_usersearch_real(replyFunc reply, wallFunc wall, void *source, int cargc, char **cargv) {
+ nick *sender = source;
+ struct searchNode *search;
+ int limit=500;
+ int arg=0;
+ UserDisplayFunc display=defaultuserfn;
+ searchCtx ctx;
+ int ret;
+
+ if (cargc<1)
+ return CMD_USAGE;
+
+ ret = parseopts(cargc, cargv, &arg, &limit, NULL, (void **)&display, reg_usersearch->outputtree, reply, sender);
+ if(ret != CMD_OK)
+ return ret;
+
+ if (arg>=cargc) {
+ reply(sender,"No search terms - aborting.");
+ return CMD_ERROR;
+ }
+
+ if (arg<(cargc-1)) {
+ rejoinline(cargv[arg],cargc-arg);
+ }
+
+ newsearch_ctxinit(&ctx, search_parse, reply, wall, NULL, reg_usersearch);
+ if (!(search = ctx.parser(&ctx, cargv[arg]))) {
+ reply(sender,"Parse error: %s",parseError);
+ return CMD_ERROR;
+ }
+
+ usersearch_exe(search, &ctx, sender, display, limit);
+
+ (search->free)(&ctx, search);
+
+ return CMD_OK;
+}
+
+int do_usersearch(void *source, int cargc, char **cargv) {
+ return do_usersearch_real(controlreply, controlwallwrapper, source, cargc, cargv);
+}
+
+void usersearch_exe(struct searchNode *search, searchCtx *ctx, nick *sender, UserDisplayFunc display, int limit) {
+ int i;
+ authname *aup;
+ int matches = 0;
+ senderNSExtern = sender;
+
+ search=coerceNode(ctx, search, RETURNTYPE_BOOL);
+
+ for (i=0;i<AUTHNAMEHASHSIZE;i++) {
+ for (aup=authnametable[i];aup;aup=aup->next) {
+ if ((search->exe)(ctx, search, aup)) {
+ if (matches<limit)
+ display(ctx, sender, aup);
+ if (matches==limit)
+ ctx->reply(sender, "--- More than %d matches, skipping the rest",limit);
+ matches++;
+ }
+ }
+ }
+
+ ctx->reply(sender,"--- End of list: %d matches", matches);
+}
/* Free a coerce node */
void free_coerce(searchCtx *ctx, struct searchNode *thenode) {
* Given an input string, return a searchNode.
*/
-struct searchNode *search_parse(searchCtx *ctx, int type, char *input) {
+struct searchNode *search_parse(searchCtx *ctx, char *input) {
/* OK, we need to split the input into chunks on spaces and brackets.. */
char *argvector[100];
char thestring[500];
if (*(ch-1) == 0) /* if the last character was a space */
j--; /* remove an argument */
- if (!(cmd=findcommandintree(searchTree,argvector[0],1))) {
+ if (!(cmd=findcommandintree(ctx->searchcmd->searchtree,argvector[0],1))) {
parseError = "Unknown command";
return NULL;
} else {
- return ((parseFunc)cmd->handler)(ctx, type, j, argvector+1);
+ return ((parseFunc)cmd->handler)(ctx, j, argvector+1);
}
} else {
/* Literal */
}
}
-struct bufs {
- char *buf;
- int capacity;
- int len;
-};
-
-static int addchar(struct bufs *buf, char c) {
- if(buf->len >= buf->capacity - 1)
- return 0;
-
- buf->buf[buf->len++] = c;
-
- return 1;
-}
-
-static int addstr(struct bufs *buf, char *c) {
- int remaining = buf->capacity - buf->len - 1;
- char *p;
-
- for(p=c;*p;p++) {
- if(remaining-- <= 0)
- return 0;
-
- buf->buf[buf->len++] = *p;
- }
-
- return 1;
-}
-
void nssnprintf(char *buf, size_t size, const char *format, nick *np) {
- struct bufs b;
+ StringBuf b;
const char *p;
char *c;
char hostbuf[512];
for(p=format;*p;p++) {
if(*p != '%') {
- if(!addchar(&b, *p))
+ if(!sbaddchar(&b, *p))
break;
continue;
}
if(*p == '\0')
break;
if(*p == '%') {
- if(!addchar(&b, *p))
+ if(!sbaddchar(&b, *p))
break;
continue;
}
c = "(bad format specifier)";
}
if(c)
- if(!addstr(&b, c))
+ if(!sbaddstr(&b, c))
break;
}
- buf[b.len] = '\0';
+ sbterminate(&b);
/* not required */
/*
*/
}
+static char *var_tochar(searchCtx *ctx, char *arg, searchNode **variable) {
+ *variable = ctx->parser(ctx, arg);
+ if (!(*variable = coerceNode(ctx, *variable, RETURNTYPE_STRING)))
+ return NULL;
+
+ if(!((*variable)->returntype & RETURNTYPE_CONST)) {
+ parseError = "only constant variables allowed";
+ ((*variable)->free)(ctx, *variable);
+ return NULL;
+ }
+
+ return (char *)((*variable)->exe)(ctx, *variable, NULL);
+}
+
+void free_val_null(searchCtx *ctx, struct searchNode *thenode) {
+}
+
+struct searchVariable *var_register(searchCtx *ctx, char *arg, int type) {
+ searchNode *variable;
+ struct searchVariable *us;
+ char *var;
+ int i;
+
+ if(ctx->lastvar >= MAX_VARIABLES) {
+ parseError = "Maximum number of variables reached";
+ return NULL;
+ }
+
+ us = &ctx->vars[ctx->lastvar];
+
+ var = var_tochar(ctx, arg, &variable);
+ if(!var)
+ return NULL;
+
+ strlcpy(us->name, var, sizeof(us->name));
+ (variable->free)(ctx, variable);
+
+ for(i=0;i<ctx->lastvar;i++) {
+ if(!strcmp(us->name, ctx->vars[i].name)) {
+ parseError = "variable name already in use";
+ return NULL;
+ }
+ }
+
+ ctx->lastvar++;
+ us->data.returntype = type;
+ us->data.localdata = &us->cdata;
+ us->data.exe = exe_val_null;
+ us->data.free = free_val_null;
+
+ us->cdata.child = NULL;
+ return us;
+}
+
+searchNode *var_get(searchCtx *ctx, char *arg) {
+ searchNode *variable, *found = NULL;
+ int i;
+ char *var = var_tochar(ctx, arg, &variable);
+ if(!var)
+ return NULL;
+
+ for(i=0;i<ctx->lastvar;i++) {
+ if(!strcmp(var, ctx->vars[i].name)) {
+ found = &ctx->vars[i].data;
+ break;
+ }
+ }
+ (variable->free)(ctx, variable);
+
+ if(!found)
+ parseError = "variable not found";
+ return found;
+}
+
+void var_setstr(struct searchVariable *v, char *data) {
+ v->cdata.u.stringbuf = data;
+}