X-Git-Url: https://jfr.im/git/irc/quakenet/newserv.git/blobdiff_plain/055a469a829dcea470d29fc3ea5cda0c487fb4ea..78546f2b0f59b5c8fede1ce5535972716eb17041:/newsearch/ns-lt.c diff --git a/newsearch/ns-lt.c b/newsearch/ns-lt.c index b0069f90..7bed1fb2 100644 --- a/newsearch/ns-lt.c +++ b/newsearch/ns-lt.c @@ -1,5 +1,5 @@ /* - * GT functionality + * LT functionality */ #include "newsearch.h" @@ -9,23 +9,38 @@ #include struct lt_localdata { + int type; int count; struct searchNode **nodes; }; -void lt_free(struct searchNode *thenode); -void *lt_exe(struct searchNode *thenode, int type, void *theinput); +void lt_free(searchCtx *ctx, struct searchNode *thenode); +void *lt_exe(searchCtx *ctx, struct searchNode *thenode, void *theinput); -struct searchNode *lt_parse(int type, int argc, char **argv) { +struct searchNode *lt_parse(searchCtx *ctx, int argc, char **argv) { struct lt_localdata *localdata; struct searchNode *thenode; int i; - localdata = (struct lt_localdata *)malloc(sizeof(struct lt_localdata)); - localdata->nodes = (struct searchNode **)malloc(sizeof(struct searchNode *) * argc); + if (!(localdata = (struct lt_localdata *)malloc(sizeof(struct lt_localdata)))) { + parseError = "malloc: could not allocate memory for this search."; + return NULL; + } + if (!(localdata->nodes = (struct searchNode **)malloc(sizeof(struct searchNode *) * argc))) { + /* 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; - thenode = (struct searchNode *)malloc(sizeof(struct searchNode)); + if (!(thenode = (struct searchNode *)malloc(sizeof(struct searchNode)))) { + /* couldn't malloc() memory for thenode, so free localdata and localdata->nodes to avoid leakage */ + parseError = "malloc: could not allocate memory for this search."; + free(localdata->nodes); + free(localdata); + return NULL; + } thenode->localdata = localdata; thenode->returntype = RETURNTYPE_BOOL; @@ -33,24 +48,39 @@ struct searchNode *lt_parse(int type, int argc, char **argv) { thenode->free = lt_free; for (i=0;inodes[i] = search_parse(type, argv[i]))) { - lt_free(thenode); + /* Parse the node.. */ + localdata->nodes[i] = ctx->parser(ctx, argv[i]); + + /* Subsequent nodes get coerced to match the type of the first node */ + if (i) + localdata->nodes[i]=coerceNode(ctx, localdata->nodes[i],localdata->type); + + /* If a node didn't parse, give up */ + if (!localdata->nodes[i]) { + lt_free(ctx, thenode); return NULL; } + + if (!i) { + /* First arg determines the type */ + localdata->type = localdata->nodes[0]->returntype & RETURNTYPE_TYPE; + } + localdata->count++; } return thenode; } -void lt_free(struct searchNode *thenode) { +void lt_free(searchCtx *ctx, struct searchNode *thenode) { struct lt_localdata *localdata; int i; localdata=thenode->localdata; for (i=0;icount;i++) { - (localdata->nodes[i]->free)(localdata->nodes[i]); + if (localdata->nodes[i]) + (localdata->nodes[i]->free)(ctx, localdata->nodes[i]); } free(localdata->nodes); @@ -58,7 +88,7 @@ void lt_free(struct searchNode *thenode) { free(thenode); } -void *lt_exe(struct searchNode *thenode, int type, void *theinput) { +void *lt_exe(searchCtx *ctx, struct searchNode *thenode, void *theinput) { int i; char *strval; int intval; @@ -67,29 +97,28 @@ void *lt_exe(struct searchNode *thenode, int type, void *theinput) { localdata=thenode->localdata; if (localdata->count==0) - return trueval(type); + return (void *)1; - switch (localdata->nodes[0]->returntype & RETURNTYPE_TYPE) { + switch (localdata->type) { case RETURNTYPE_INT: - intval = (int)(localdata->nodes[0]->exe)(localdata->nodes[0], RETURNTYPE_INT, theinput); + case RETURNTYPE_BOOL: + intval = (int)((long)(localdata->nodes[0]->exe)(ctx, localdata->nodes[0], theinput)); for (i=1;icount;i++) { - if ((int)(localdata->nodes[i]->exe)(localdata->nodes[i], RETURNTYPE_INT, theinput) < intval) - return falseval(type); + if ((int)((long)(localdata->nodes[i]->exe)(ctx, localdata->nodes[i], theinput) <= intval)) + return (void *)0; } - - return trueval(type); + return (void *)1; case RETURNTYPE_STRING: - strval = (char *)(localdata->nodes[0]->exe)(localdata->nodes[0], RETURNTYPE_STRING, theinput); + strval = (char *)(localdata->nodes[0]->exe)(ctx, localdata->nodes[0], theinput); for (i=1;icount;i++) { - if (ircd_strcmp(strval, (char *)(localdata->nodes[i]->exe)(localdata->nodes[i], RETURNTYPE_STRING, theinput)) < 0) - return falseval(type); + if (ircd_strcmp(strval, (char *)(localdata->nodes[i]->exe)(ctx, localdata->nodes[i], theinput)) >= 0) + return (void *)0; } - - return trueval(type); + return (void *)1; default: - return falseval(type); + return (void *)0; } }