From c8324b3460a85d57ca1bcfa481168d566069a0d1 Mon Sep 17 00:00:00 2001 From: Rainer Gerhards Date: Tue, 12 Jun 2012 18:52:32 +0200 Subject: milestone: regex is compiled from script based filter --- grammar/rainerscript.c | 61 +++++++++++++++++++++++++++++++++++++++++++++++--- grammar/rainerscript.h | 2 ++ 2 files changed, 60 insertions(+), 3 deletions(-) (limited to 'grammar') diff --git a/grammar/rainerscript.c b/grammar/rainerscript.c index bcdbdf3b..dfaff869 100644 --- a/grammar/rainerscript.c +++ b/grammar/rainerscript.c @@ -41,6 +41,11 @@ #include "grammar.h" #include "queue.h" #include "srUtils.h" +#include "regexp.h" +#include "obj.h" + +DEFobjCurrIf(obj) +DEFobjCurrIf(regexp) void readConfFile(FILE *fp, es_str_t **str) @@ -1458,9 +1463,6 @@ cnffparamlstNew(struct cnfexpr *expr, struct cnffparamlst *next) static inline enum cnffuncid funcName2ID(es_str_t *fname, unsigned short nParams) { -{ char *s;s=es_str2cstr(fname, NULL); -dbgprintf("ZZZZ: func: '%s', nParams: %d\n", s, nParams); -free(s);} if(!es_strbufcmp(fname, (unsigned char*)"strlen", sizeof("strlen") - 1)) { if(nParams != 1) { parser_errmsg("number of parameters for strlen() must be one " @@ -1508,6 +1510,41 @@ free(s);} } } + +static inline rsRetVal +initFunc_re_match(struct cnffunc *func) +{ + rsRetVal localRet; + char *regex = NULL; + regex_t *re; + DEFiRet; + + func->funcdata = NULL; + if(func->expr[1]->nodetype != 'S') { + parser_errmsg("param 2 of re_match() must be a constant string"); + FINALIZE; + } + + CHKmalloc(re = malloc(sizeof(regex_t))); + func->funcdata = re; + + regex = es_str2cstr(((struct cnfstringval*) func->expr[1])->estr, NULL); + + if((localRet = objUse(regexp, LM_REGEXP_FILENAME)) == RS_RET_OK) { + if(regexp.regcomp(re, (char*) regex, REG_EXTENDED) != 0) { + parser_errmsg("cannot compile regex '%s'", regex); + ABORT_FINALIZE(RS_RET_ERR); + } + } else { /* regexp object could not be loaded */ + parser_errmsg("could not load regex support - regex ignored"); + ABORT_FINALIZE(RS_RET_ERR); + } + +finalize_it: + free(regex); + RETiRet; +} + struct cnffunc * cnffuncNew(es_str_t *fname, struct cnffparamlst* paramlst) { @@ -1534,6 +1571,14 @@ cnffuncNew(es_str_t *fname, struct cnffparamlst* paramlst) param = param->next; free(toDel); } + /* some functions require special initialization */ + switch(func->fID) { + case CNFFUNC_RE_MATCH: + /* need to compile the regexp in param 2, so this MUST be a constant */ + initFunc_re_match(func); + break; + default:break; + } } return func; } @@ -1631,3 +1676,13 @@ cstrPrint(char *text, es_str_t *estr) dbgprintf("%s%s", text, str); free(str); } + +/* init must be called once before any parsing of the script files start */ +rsRetVal +initRainerscript(void) +{ + DEFiRet; + CHKiRet(objGetObjInterface(&obj)); +finalize_it: + RETiRet; +} diff --git a/grammar/rainerscript.h b/grammar/rainerscript.h index 07e6af0b..b8d6e772 100644 --- a/grammar/rainerscript.h +++ b/grammar/rainerscript.h @@ -172,6 +172,7 @@ struct cnffunc { es_str_t *fname; unsigned short nParams; enum cnffuncid fID; /* function ID for built-ins, 0 means use name */ + void *funcdata; /* global data for function-specific use (e.g. compiled regex) */ struct cnfexpr *expr[]; }; @@ -250,6 +251,7 @@ void cnfparamsPrint(struct cnfparamblk *params, struct cnfparamvals *vals); void varDelete(struct var *v); void cnfparamvalsDestruct(struct cnfparamvals *paramvals, struct cnfparamblk *blk); void cnfcfsyslinelstDestruct(struct cnfcfsyslinelst *cfslst); +rsRetVal initRainerscript(void); /* debug helper */ void cstrPrint(char *text, es_str_t *estr); -- cgit