test
authorDavid Marec <DavidMarec@users.noreply.github.com>
Fri, 7 Oct 2022 11:49:47 +0000 (13:49 +0200)
committerDavid Marec <DavidMarec@users.noreply.github.com>
Fri, 7 Oct 2022 11:49:47 +0000 (13:49 +0200)
Makefile
policy_test.l
policy_test.y

index 1401d14f7891eb6c45d85b9117882700a5302f8b..faa2008c2c4f8925b5630f1c79f770492c12c877 100644 (file)
--- 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
index d1d72099780e123e936bb3a026f2c1799cbce4ce..be13edbdec8fb0d2efc061ddc6fe7cf227b0d182 100644 (file)
 /* 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; }
 %%
 
+
index c9aac02ead00a49e76e8c8729810a9b3efc08fdf..e2105d4cbb467ebe0f4c9b7eef1d26cf35786689 100644 (file)
@@ -9,10 +9,15 @@
 
 #include <stdlib.h>
 #include <stdio.h>
+#include <unistd.h>
+#include <fcntl.h>
 #include <string.h>
 #include <netdb.h>
 
 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 <string> WORD SOCKET
-%token <number> NUMBER
-%type <number> ACTION DIR QUICK LOG
-%type <string> from topic
+%type <number> ACTION QUICK LOG get_dir get_log get_quick
+%type <string> 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 <from> %s.\n", r.action == GTW_DROP ? "Drop" : "pass", r.topic[0]!=0 ? r.topic :"*", r.from[0]!=0 ? r.from : "<all>");
+          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 <from> %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 : "<all>");
+               }
           };
 
-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);
 }