From c577da1fe2fa4591cb4fb8bb3021e171714efe43 Mon Sep 17 00:00:00 2001 From: David Marec Date: Fri, 7 Oct 2022 13:49:47 +0200 Subject: [PATCH] test --- Makefile | 8 +++--- policy_test.l | 29 +++++++++++-------- policy_test.y | 77 +++++++++++++++++++++++++++++++++++---------------- 3 files changed, 74 insertions(+), 40 deletions(-) diff --git a/Makefile b/Makefile index 1401d14..faa2008 100644 --- a/Makefile +++ b/Makefile @@ -3,8 +3,8 @@ main: y.tab.o lex.yy.o cc -DYYDEBUG=stdout -o main y.tab.o lex.yy.o -ly -ll -y.tab.c y.tab.h: policy_mqtt.y - yacc -d policy_mqtt.y +y.tab.c y.tab.h: policy_test.y + yacc -d policy_test.y y.tab.o: y.tab.c cc -c y.tab.c @@ -12,8 +12,8 @@ y.tab.o: y.tab.c lex.yy.o: y.tab.h lex.yy.c cc -c lex.yy.c -lex.yy.c: policy_mqtt.l - lex policy_mqtt.l +lex.yy.c: policy_test.l + lex policy_test.l clean: rm -f *.o y.tab.* lex.yy.c diff --git a/policy_test.l b/policy_test.l index d1d7209..be13edb 100644 --- a/policy_test.l +++ b/policy_test.l @@ -25,23 +25,28 @@ /* common section */ nl \n ws [ \t]+ -socket [\/][a-zA-Z0-9\/]+ -topic [\"][a-zA-Z0-9/]+[\"] +special [()+\|\?\*,] +sstring [a-zA-Z0-9] +socket \/{sstring}[^{special}][a-zA-Z0-9\/\.]+ +quotedstring \"[^"\n]*\" +bracketstring \<[^>]*\> +comment ^\#[^\n]* %% -any { return(ANY); } -drop { yylval.number=0;return (ACTION);} -pass { yylval.number=1;return (ACTION);} -in { yylval.number=0;return (DIR);} -out { yylval.number=1;return (DIR);} -{topic} { yylval.string=strdup(yytext);return WORD;} -{socket} { yylval.string=strdup(yytext);return SOCKET;} -"FROM" { return FROM;} -"ANY" { return ANY;} +"DROP" { yylval.number=0;return (ACTION);} +"PASS" { yylval.number=1;return (ACTION);} +"IN" { return (IN);} +"OUT" { return (OUT);} +"ANY" { return (ANY);} +{bracketstring} { yylval.string=strndup(yytext+1, yyleng -2);return WORD;} +{ws}"FROM"{ws}{socket}$ { yylval.string=strdup(yytext);return SOCKET;} +^"LOG" { return LOG;} +"QUICK" { return QUICK;} {ws} { ; } {nl} { ; } - +{comment} { return COMMENT; } %% + diff --git a/policy_test.y b/policy_test.y index c9aac02..e2105d4 100644 --- a/policy_test.y +++ b/policy_test.y @@ -9,10 +9,15 @@ #include #include +#include +#include #include #include extern FILE *yyin; +extern FILE *yyout; +extern int yylineno; +extern int yy_flex_debug; static size_t errors; enum filter_dir { @@ -37,6 +42,8 @@ struct mqtt_rule { }; static size_t parsed_lines; +static size_t errors; +static size_t nrule; %} %union { @@ -45,43 +52,62 @@ static size_t parsed_lines; }; %token ACTION IN OUT FROM QUICK LOG SOCKET -%token TOPIC WORD +%token TOPIC WORD WORD2 %token ANY FROM %token SLASH HYPHEN -%token WORD SOCKET -%token NUMBER -%type ACTION DIR QUICK LOG -%type from topic +%type ACTION QUICK LOG get_dir get_log get_quick +%type get_from get_topic WORD SOCKET %% ruleset: /* empty */ - | - ruleset mqttrule + | ruleset mqttrule + | ruleset comment + | ruleset error { errors++; } ; -mqttrule : ACTION DIR topic from +comment: COMMENT '\n' + { + }; +mqttrule : get_log ACTION get_quick get_dir get_topic get_from { parsed_lines++; struct mqtt_rule r; memset(&r, 0, sizeof(r)); - r.action = $1; - r.dir = $2; - if ($3) - strlcpy(r.topic, $3, sizeof r.topic); - if ($4) - strlcpy(r.from, $4, sizeof r.from); - - printf("%s %s %s.\n", r.action == GTW_DROP ? "Drop" : "pass", r.topic[0]!=0 ? r.topic :"*", r.from[0]!=0 ? r.from : ""); + r.action = $2; + r.dir = $4; + if ($5) + strlcpy(r.topic, $5, sizeof r.topic); + if ($6) + strlcpy(r.from, $6, sizeof r.from); + + ++nrule; + if ($1==1) { + printf("\t#%zu: %c %s %c %s %s;\n", nrule, $3?'!':' ', r.action == GTW_DROP ? "Drop" : "pass", + r.dir==GTW_INOUT ? '=' : r.dir==GTW_IN ? '<' :'>', + r.topic[0]!=0 ? r.topic :"*", r.from[0]!=0 ? r.from : ""); + } }; -topic : ANY {$$=NULL;} - | WORD {$$=$1;} - ; +get_quick : /*empty*/ {$$=0;} + | QUICK {$$=1;} + ; +get_log : /*empty*/ {$$=0;} + | LOG {$$ = 1;} + ; -from : /* empty */ {$$=NULL;} - | FROM SOCKET {$$ = $2;} +get_topic : ANY {$$=NULL;} + | WORD {$$=$1;} + ; + +get_from : /*empty*/ {$$=NULL;} + | SOCKET {$$ = $1;} + ; + +get_dir : /*empty*/ {$$=GTW_INOUT;} + | IN {$$=GTW_IN;} + | OUT {$$=GTW_OUT;} ; %% @@ -89,7 +115,8 @@ void yyerror(msg) char *msg; { - fprintf(stderr, "while parsing \"%s\"\n", + fprintf(stderr, "while parsing line %d: error occured \"%s\"\n", + yylineno, msg); return; @@ -103,7 +130,9 @@ int main( int argc, char **argv ) else yyin = stdin; - yyparse(); - printf("\n#%zu.\n", parsed_lines); + int devNull = open("/dev/null", O_WRONLY); + int dup2Result = dup2(devNull, STDERR_FILENO); + yyparse(); + printf("#%zu. R: %zu\tE: %zu\n", parsed_lines, nrule, errors); } -- 2.45.0