From 183641a091a3d7b5eabe4419db4ebd2b6f84934e Mon Sep 17 00:00:00 2001 From: Rainer Gerhards Date: Mon, 4 Jul 2011 18:25:17 +0200 Subject: milestone: added functions to grammar --- grammar/mini.samp | 1 + grammar/rscript.l | 3 +++ grammar/rscript.y | 8 ++++++++ grammar/utils.c | 39 +++++++++++++++++++++++++++++++++++++-- grammar/utils.h | 20 ++++++++++++++++++-- 5 files changed, 67 insertions(+), 4 deletions(-) diff --git a/grammar/mini.samp b/grammar/mini.samp index 71cfbb69..505ae67a 100644 --- a/grammar/mini.samp +++ b/grammar/mini.samp @@ -30,3 +30,4 @@ if not (1==0) and 2*4/-5--(10-3)>7/*pri("*.*")*/ then { action(type="omfwd" taget="10.0.0.1" port="514") action(type="omwusr" taget="rger") } +if getenv("user") == "test" then /var/log/testlog diff --git a/grammar/rscript.l b/grammar/rscript.l index a0ed3b0c..a7410b15 100644 --- a/grammar/rscript.l +++ b/grammar/rscript.l @@ -61,6 +61,7 @@ char *currfn; /* name of currently processed file */ "or" { return OR; } "and" { return AND; } "not" { return NOT; } +"," | "*" | "/" | "%" | @@ -88,6 +89,8 @@ char *currfn; /* name of currently processed file */ \"([^"\\]|\\["])*\" { yylval.estr = es_newStrFromBuf(yytext+1, yyleng-2); return STRING; } [ \t\n] +[a-z][a-z0-9_]* { yylval.estr = es_newStrFromCStr(yytext, yyleng); + return FUNC; } . { printf("invalid char in expr: %s\n", yytext); } "&" { return '&'; } "{" { return '{'; } diff --git a/grammar/rscript.y b/grammar/rscript.y index 75fce49d..b24b7db7 100644 --- a/grammar/rscript.y +++ b/grammar/rscript.y @@ -31,10 +31,13 @@ extern int yylineno; struct cnfactlst *actlst; struct cnfexpr *expr; struct cnfrule *rule; + struct cnffunc *func; + struct cnffparamlst *fparams; } %token NAME %token VALUE +%token FUNC %token BEGINOBJ %token ENDOBJ %token CFSYSLINE @@ -72,6 +75,7 @@ extern int yylineno; %type expr %type rule %type scriptfilt +%type fparams %left AND OR %left CMP_EQ CMP_NE CMP_LE CMP_GE CMP_LT CMP_GT CMP_CONTAINS CMP_CONTAINSI CMP_STARTSWITH CMP_STARTSWITHI @@ -145,9 +149,13 @@ expr: expr AND expr { $$ = cnfexprNew(AND, $1, $3); } | expr '%' expr { $$ = cnfexprNew('%', $1, $3); } | '(' expr ')' { $$ = $2; } | '-' expr %prec UMINUS { $$ = cnfexprNew('M', NULL, $2); } + | FUNC '(' ')' { $$ = (struct cnfexpr*) cnffuncNew($1, NULL); } + | FUNC '(' fparams ')' { $$ = (struct cnfexpr*) cnffuncNew($1, $3); } | NUMBER { $$ = (struct cnfexpr*) cnfnumvalNew($1); } | STRING { $$ = (struct cnfexpr*) cnfstringvalNew($1); } | VAR { $$ = (struct cnfexpr*) cnfvarNew($1); } +fparams: expr { $$ = cnffparamlstNew($1, NULL); } + | expr ',' fparams { $$ = cnffparamlstNew($1, $3); } %% int yyerror(char *s) diff --git a/grammar/utils.c b/grammar/utils.c index 791a8967..16cad201 100644 --- a/grammar/utils.c +++ b/grammar/utils.c @@ -398,6 +398,7 @@ doIndent(indent) void cnfexprPrint(struct cnfexpr *expr, int indent) { + struct cnffparamlst *param; //printf("expr %p, indent %d, type '%c'\n", expr, indent, expr->nodetype); switch(expr->nodetype) { case CMP_EQ: @@ -477,6 +478,11 @@ cnfexprPrint(struct cnfexpr *expr, int indent) printf("NOT\n"); cnfexprPrint(expr->r, indent+1); break; + case 'S': + doIndent(indent); + cstrPrint("string '", ((struct cnfstringval*)expr)->estr); + printf("'\n"); + break; case 'N': doIndent(indent); printf("%lld\n", ((struct cnfnumval*)expr)->val); @@ -485,10 +491,15 @@ cnfexprPrint(struct cnfexpr *expr, int indent) doIndent(indent); printf("var '%s'\n", ((struct cnfvar*)expr)->name); break; - case 'S': + case 'F': doIndent(indent); - cstrPrint("string '", ((struct cnfstringval*)expr)->estr); + cstrPrint("function '", ((struct cnffunc*)expr)->fname); printf("'\n"); + for( param = ((struct cnffunc*)expr)->paramlst + ; param != NULL + ; param = param->next) { + cnfexprPrint(param->expr, indent+1); + } break; case '+': case '-': @@ -575,6 +586,30 @@ cnfrulePrint(struct cnfrule *rule) printf("------ end rule %p\n", rule); } +struct cnffparamlst * +cnffparamlstNew(struct cnfexpr *expr, struct cnffparamlst *next) +{ + struct cnffparamlst* lst; + if((lst = malloc(sizeof(struct cnffparamlst))) != NULL) { + lst->nodetype = 'P'; + lst->expr = expr; + lst->next = next; + } + return lst; +} + +struct cnffunc * +cnffuncNew(es_str_t *fname, struct cnffparamlst* paramlst) +{ + struct cnffunc* func; + if((func = malloc(sizeof(struct cnffunc))) != NULL) { + func->nodetype = 'F'; + func->fname = fname; + func->paramlst = paramlst; + } + return func; +} + /* debug helper */ void cstrPrint(char *text, es_str_t *estr) diff --git a/grammar/utils.h b/grammar/utils.h index fb1462a3..e75105da 100644 --- a/grammar/utils.h +++ b/grammar/utils.h @@ -62,10 +62,12 @@ struct cnfactlst { * be the sole foundation for the AST. * * nodetypes (list not yet complete) - * S - string + * F - function * N - number - * V - var + * P - fparamlst * R - rule + * S - string + * V - var */ enum cnfFiltType { CNFFILT_NONE, CNFFILT_PRI, CNFFILT_PROP, CNFFILT_SCRIPT }; static inline char* @@ -116,6 +118,18 @@ struct cnfvar { char *name; }; +struct cnffparamlst { + unsigned nodetype; /* P */ + struct cnffparamlst *next; + struct cnfexpr *expr; +}; + +struct cnffunc { + unsigned nodetype; + es_str_t *fname; + struct cnffparamlst *paramlst; +}; + /* future extensions struct x { int nodetype; @@ -152,6 +166,8 @@ struct cnfstringval* cnfstringvalNew(es_str_t *estr); struct cnfrule * cnfruleNew(enum cnfFiltType filttype, struct cnfactlst *actlst); void cnfrulePrint(struct cnfrule *rule); struct cnfvar* cnfvarNew(char *name); +struct cnffunc * cnffuncNew(es_str_t *fname, struct cnffparamlst* paramlst); +struct cnffparamlst * cnffparamlstNew(struct cnfexpr *expr, struct cnffparamlst *next); /* debug helper */ void cstrPrint(char *text, es_str_t *estr); -- cgit v1.2.3