diff options
Diffstat (limited to 'grammar')
-rw-r--r-- | grammar/.gitignore | 1 | ||||
-rw-r--r-- | grammar/lexer.l | 8 | ||||
-rw-r--r-- | grammar/rainerscript.c | 163 | ||||
-rw-r--r-- | grammar/rainerscript.h | 10 |
4 files changed, 179 insertions, 3 deletions
diff --git a/grammar/.gitignore b/grammar/.gitignore new file mode 100644 index 00000000..8bd546bd --- /dev/null +++ b/grammar/.gitignore @@ -0,0 +1 @@ +lexer.c diff --git a/grammar/lexer.l b/grammar/lexer.l index ed5d8a80..36c23c7d 100644 --- a/grammar/lexer.l +++ b/grammar/lexer.l @@ -9,7 +9,7 @@ * cases. So while we hope that cfsysline support can be dropped some time in * the future, we will probably keep these useful constructs. * - * Copyright 2011-2012 Rainer Gerhards and Adiscon GmbH. + * Copyright 2011-2013 Rainer Gerhards and Adiscon GmbH. * * This file is part of the rsyslog runtime library. * @@ -129,7 +129,7 @@ int fileno(FILE *stream); <EXPR>0[0-7]+ | /* octal number */ <EXPR>0x[0-7a-f] | /* hex number, following rule is dec; strtoll handles all! */ <EXPR>([1-9][0-9]*|0) { yylval.n = strtoll(yytext, NULL, 0); return NUMBER; } -<EXPR>\$[$!]{0,1}[a-z][!a-z0-9\-_\.]* { yylval.s = strdup(yytext); return VAR; } +<EXPR>\$[$!./]{0,1}[a-z][!a-z0-9\-_\.]* { yylval.s = strdup(yytext); return VAR; } <EXPR>\'([^'\\]|\\['"\\$bntr]|\\x[0-9a-f][0-9a-f]|\\[0-7][0-7][0-7])*\' { yytext[yyleng-1] = '\0'; unescapeStr((uchar*)yytext+1, yyleng-2); @@ -176,6 +176,8 @@ int fileno(FILE *stream); <INCL>[^ \t\n]+ { if(cnfDoInclude(yytext) != 0) yyterminate(); BEGIN INITIAL; } +"main_queue"[ \n\t]*"(" { yylval.objType = CNFOBJ_MAINQ; + BEGIN INOBJ; return BEGINOBJ; } "global"[ \n\t]*"(" { yylval.objType = CNFOBJ_GLOBAL; BEGIN INOBJ; return BEGINOBJ; } "template"[ \n\t]*"(" { yylval.objType = CNFOBJ_TPL; @@ -190,6 +192,8 @@ int fileno(FILE *stream); BEGIN INOBJ; return BEGINOBJ; } "module"[ \n\t]*"(" { yylval.objType = CNFOBJ_MODULE; BEGIN INOBJ; return BEGINOBJ; } +"lookup_table"[ \n\t]*"(" { yylval.objType = CNFOBJ_LOOKUP_TABLE; + BEGIN INOBJ; return BEGINOBJ; } "action"[ \n\t]*"(" { BEGIN INOBJ; return BEGIN_ACTION; } ^[ \t]*:\$?[a-z\-]+[ ]*,[ ]*!?[a-z]+[ ]*,[ ]*\"(\\\"|[^\"])*\" { yylval.s = strdup(rmLeadingSpace(yytext)); diff --git a/grammar/rainerscript.c b/grammar/rainerscript.c index 986d2802..a2bed2bf 100644 --- a/grammar/rainerscript.c +++ b/grammar/rainerscript.c @@ -38,6 +38,7 @@ #include "rainerscript.h" #include "conf.h" #include "parserif.h" +#include "parse.h" #include "rsconf.h" #include "grammar.h" #include "queue.h" @@ -144,6 +145,116 @@ getFIOPName(unsigned iFIOP) return pRet; } + +/* This function takes the filter part of a property + * based filter and decodes it. It processes the line up to the beginning + * of the action part. + */ +static rsRetVal +DecodePropFilter(uchar *pline, struct cnfstmt *stmt) +{ + rsParsObj *pPars = NULL; + cstr_t *pCSCompOp = NULL; + cstr_t *pCSPropName = NULL; + int iOffset; /* for compare operations */ + DEFiRet; + + ASSERT(pline != NULL); + + DBGPRINTF("Decoding property-based filter '%s'\n", pline); + + /* create parser object starting with line string without leading colon */ + if((iRet = rsParsConstructFromSz(&pPars, pline+1)) != RS_RET_OK) { + parser_errmsg("error %d constructing parser object", iRet); + ABORT_FINALIZE(iRet); + } + + /* read property */ + iRet = parsDelimCStr(pPars, &pCSPropName, ',', 1, 1, 1); + if(iRet != RS_RET_OK) { + parser_errmsg("error %d parsing filter property", iRet); + rsParsDestruct(pPars); + ABORT_FINALIZE(iRet); + } + iRet = propNameToID(pCSPropName, &stmt->d.s_propfilt.propID); + if(iRet != RS_RET_OK) { + parser_errmsg("invalid property name '%s' in filter", + cstrGetSzStrNoNULL(pCSPropName)); + rsParsDestruct(pPars); + ABORT_FINALIZE(iRet); + } + if(stmt->d.s_propfilt.propID == PROP_CEE) { + /* in CEE case, we need to preserve the actual property name */ + if((stmt->d.s_propfilt.propName = + es_newStrFromBuf((char*)cstrGetSzStrNoNULL(pCSPropName)+2, cstrLen(pCSPropName)-2)) == NULL) { + ABORT_FINALIZE(RS_RET_ERR); + } + } + + /* read operation */ + iRet = parsDelimCStr(pPars, &pCSCompOp, ',', 1, 1, 1); + if(iRet != RS_RET_OK) { + parser_errmsg("error %d compare operation property - ignoring selector", iRet); + rsParsDestruct(pPars); + ABORT_FINALIZE(iRet); + } + + /* we now first check if the condition is to be negated. To do so, we first + * must make sure we have at least one char in the param and then check the + * first one. + * rgerhards, 2005-09-26 + */ + if(rsCStrLen(pCSCompOp) > 0) { + if(*rsCStrGetBufBeg(pCSCompOp) == '!') { + stmt->d.s_propfilt.isNegated = 1; + iOffset = 1; /* ignore '!' */ + } else { + stmt->d.s_propfilt.isNegated = 0; + iOffset = 0; + } + } else { + stmt->d.s_propfilt.isNegated = 0; + iOffset = 0; + } + + if(!rsCStrOffsetSzStrCmp(pCSCompOp, iOffset, (uchar*) "contains", 8)) { + stmt->d.s_propfilt.operation = FIOP_CONTAINS; + } else if(!rsCStrOffsetSzStrCmp(pCSCompOp, iOffset, (uchar*) "isequal", 7)) { + stmt->d.s_propfilt.operation = FIOP_ISEQUAL; + } else if(!rsCStrOffsetSzStrCmp(pCSCompOp, iOffset, (uchar*) "isempty", 7)) { + stmt->d.s_propfilt.operation = FIOP_ISEMPTY; + } else if(!rsCStrOffsetSzStrCmp(pCSCompOp, iOffset, (uchar*) "startswith", 10)) { + stmt->d.s_propfilt.operation = FIOP_STARTSWITH; + } else if(!rsCStrOffsetSzStrCmp(pCSCompOp, iOffset, (unsigned char*) "regex", 5)) { + stmt->d.s_propfilt.operation = FIOP_REGEX; + } else if(!rsCStrOffsetSzStrCmp(pCSCompOp, iOffset, (unsigned char*) "ereregex", 8)) { + stmt->d.s_propfilt.operation = FIOP_EREREGEX; + } else { + parser_errmsg("error: invalid compare operation '%s'", + (char*) rsCStrGetSzStrNoNULL(pCSCompOp)); + ABORT_FINALIZE(RS_RET_ERR); + } + + if(stmt->d.s_propfilt.operation != FIOP_ISEMPTY) { + /* read compare value */ + iRet = parsQuotedCStr(pPars, &stmt->d.s_propfilt.pCSCompValue); + if(iRet != RS_RET_OK) { + parser_errmsg("error %d compare value property", iRet); + rsParsDestruct(pPars); + ABORT_FINALIZE(iRet); + } + } + +finalize_it: + if(pPars != NULL) + rsParsDestruct(pPars); + if(pCSCompOp != NULL) + rsCStrDestruct(&pCSCompOp); + if(pCSPropName != NULL) + cstrDestruct(&pCSPropName); + RETiRet; +} + static void prifiltInvert(struct funcData_prifilt *prifilt) { @@ -1091,7 +1202,11 @@ var2Number(struct var *r, int *bSuccess) n = es_str2num(r->d.estr, bSuccess); } else { if(r->datatype == 'J') { +#ifdef HAVE_JSON_OBJECT_NEW_INT64 + n = (r->d.json == NULL) ? 0 : json_object_get_int64(r->d.json); +#else /* HAVE_JSON_OBJECT_NEW_INT64 */ n = (r->d.json == NULL) ? 0 : json_object_get_int(r->d.json); +#endif /* HAVE_JSON_OBJECT_NEW_INT64 */ } else { n = r->d.n; } @@ -1476,6 +1591,19 @@ doFuncCall(struct cnffunc *func, struct var *ret, void* usrptr) ret->d.n = 1; ret->datatype = 'N'; break; + case CNFFUNC_LOOKUP: +dbgprintf("DDDD: executing lookup\n"); + ret->datatype = 'S'; + if(func->funcdata == NULL) { + ret->d.estr = es_newStrFromCStr("TABLE-NOT-FOUND", sizeof("TABLE-NOT-FOUND")-1); + break; + } + cnfexprEval(func->expr[1], &r[1], usrptr); + str = (char*) var2CString(&r[1], &bMustFree); + ret->d.estr = lookupKey_estr(func->funcdata, (uchar*)str); + if(bMustFree) free(str); + if(r[1].datatype == 'S') es_deleteStr(r[1].d.estr); + break; default: if(Debug) { fname = es_str2cstr(func->fname, NULL); @@ -2580,6 +2708,7 @@ cnfstmtNewPROPFILT(char *propfilt, struct cnfstmt *t_then) cnfstmt->d.s_propfilt.pCSCompValue = NULL; if(DecodePropFilter((uchar*)propfilt, cnfstmt) != RS_RET_OK) { cnfstmt->nodetype = S_NOP; /* disable action! */ + cnfstmtDestructLst(t_then); /* we do no longer need this */ } } return cnfstmt; @@ -3240,6 +3369,13 @@ funcName2ID(es_str_t *fname, unsigned short nParams) return CNFFUNC_INVALID; } return CNFFUNC_PRIFILT; + } else if(!es_strbufcmp(fname, (unsigned char*)"lookup", sizeof("lookup") - 1)) { + if(nParams != 2) { + parser_errmsg("number of parameters for lookup() must be two " + "but is %d.", nParams); + return CNFFUNC_INVALID; + } + return CNFFUNC_LOOKUP; } else { return CNFFUNC_INVALID; } @@ -3304,6 +3440,30 @@ finalize_it: } +static inline rsRetVal +initFunc_lookup(struct cnffunc *func) +{ + uchar *cstr = NULL; + DEFiRet; + + func->funcdata = NULL; + if(func->expr[0]->nodetype != 'S') { + parser_errmsg("table name (param 1) of lookup() must be a constant string"); + FINALIZE; + } + + cstr = (uchar*)es_str2cstr(((struct cnfstringval*) func->expr[0])->estr, NULL); + if((func->funcdata = lookupFindTable(cstr)) == NULL) { + parser_errmsg("lookup table '%s' not found", cstr); + FINALIZE; + } + +finalize_it: + free(cstr); + RETiRet; +} + + struct cnffunc * cnffuncNew(es_str_t *fname, struct cnffparamlst* paramlst) { @@ -3341,6 +3501,9 @@ cnffuncNew(es_str_t *fname, struct cnffparamlst* paramlst) case CNFFUNC_PRIFILT: initFunc_prifilt(func); break; + case CNFFUNC_LOOKUP: + initFunc_lookup(func); + break; default:break; } } diff --git a/grammar/rainerscript.h b/grammar/rainerscript.h index d00cc4c3..4a508f93 100644 --- a/grammar/rainerscript.h +++ b/grammar/rainerscript.h @@ -24,6 +24,8 @@ enum cnfobjType { CNFOBJ_TPL, CNFOBJ_PROPERTY, CNFOBJ_CONSTANT, + CNFOBJ_MAINQ, + CNFOBJ_LOOKUP_TABLE, CNFOBJ_INVALID = 0 }; @@ -55,6 +57,11 @@ cnfobjType2str(enum cnfobjType ot) case CNFOBJ_CONSTANT: return "constant"; break; + case CNFOBJ_MAINQ: + return "main_queue"; + case CNFOBJ_LOOKUP_TABLE: + return "lookup_table"; + break; default:return "error: invalid cnfobjType"; } } @@ -228,7 +235,8 @@ enum cnffuncid { CNFFUNC_RE_MATCH, CNFFUNC_RE_EXTRACT, CNFFUNC_FIELD, - CNFFUNC_PRIFILT + CNFFUNC_PRIFILT, + CNFFUNC_LOOKUP }; struct cnffunc { |