From: David Marec Date: Wed, 28 Sep 2022 19:13:47 +0000 (+0200) Subject: test issue X-Git-Url: http://popeye.lapinbilly.eu/git/?p=parefeu.git;a=commitdiff_plain;h=35061a0d5a5f27ab71643a32c44d1d92dc54eaf8 test issue --- 35061a0d5a5f27ab71643a32c44d1d92dc54eaf8 diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..1401d14 --- /dev/null +++ b/Makefile @@ -0,0 +1,21 @@ + + +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.o: y.tab.c + cc -c 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 + +clean: + rm -f *.o y.tab.* lex.yy.c + + diff --git a/main.c b/main.c new file mode 100644 index 0000000..68d0d69 --- /dev/null +++ b/main.c @@ -0,0 +1,14 @@ +#include +#include + +int main( int argc, char **argv ) +{ + ++argv, --argc; /* skip over program name */ + if ( argc > 0 ) + yyin = fopen( argv[0], "r" ); + else + yyin = stdin; + + yyparse(); +} + diff --git a/policy_test.l b/policy_test.l new file mode 100644 index 0000000..d1d7209 --- /dev/null +++ b/policy_test.l @@ -0,0 +1,47 @@ +%{ +#include +#include +#include +#include +#include +#include +#include +#include + +#include +#include +#include +#include +#include + +#include "y.tab.h" + +%} + +%option noyywrap +%option nounput +%option noinput + +/* common section */ +nl \n +ws [ \t]+ +socket [\/][a-zA-Z0-9\/]+ +topic [\"][a-zA-Z0-9/]+[\"] + +%% + +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;} + +{ws} { ; } +{nl} { ; } + +%% + diff --git a/policy_test.y b/policy_test.y new file mode 100644 index 0000000..c9aac02 --- /dev/null +++ b/policy_test.y @@ -0,0 +1,109 @@ +%{ +#include + +#include +#include +#include + +#include + +#include +#include +#include +#include + +extern FILE *yyin; + +static size_t errors; +enum filter_dir { +GTW_IN, +GTW_OUT, +GTW_INOUT +}; + +#define GODROP 0 +enum filter_action { +GTW_DROP = 0, +GTW_PASS, +}; + +struct mqtt_rule { + char topic[128]; + char from[128]; + enum filter_action action; + enum filter_dir dir; + int log; + int quick; +}; + +static size_t parsed_lines; +%} + +%union { + int64_t number; + char *string; +}; + +%token ACTION IN OUT FROM QUICK LOG SOCKET +%token TOPIC WORD +%token ANY FROM +%token SLASH HYPHEN +%token WORD SOCKET +%token NUMBER +%type ACTION DIR QUICK LOG +%type from topic + + +%% + +ruleset: /* empty */ + | + ruleset mqttrule + ; + +mqttrule : ACTION DIR topic 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 : ""); + }; + +topic : ANY {$$=NULL;} + | WORD {$$=$1;} + ; + +from : /* empty */ {$$=NULL;} + | FROM SOCKET {$$ = $2;} + ; +%% + +void +yyerror(msg) + char *msg; +{ + fprintf(stderr, "while parsing \"%s\"\n", + msg); + + return; +} + +int main( int argc, char **argv ) +{ + ++argv, --argc; /* skip over program name */ + if ( argc > 0 ) + yyin = fopen( argv[0], "r" ); + else + yyin = stdin; + + yyparse(); + printf("\n#%zu.\n", parsed_lines); + +}