test issue
[parefeu.git] / policy_test.y
1 %{
2 #include <sys/cdefs.h>
3
4 #include <sys/types.h>
5 #include <sys/param.h>
6 #include <sys/socket.h>
7
8 #include <netinet/in.h>
9
10 #include <stdlib.h>
11 #include <stdio.h>
12 #include <string.h>
13 #include <netdb.h>
14
15 extern FILE *yyin;
16
17 static size_t errors;
18 enum filter_dir {
19 GTW_IN,
20 GTW_OUT,
21 GTW_INOUT
22 };
23
24 #define GODROP 0
25 enum filter_action {
26 GTW_DROP = 0,
27 GTW_PASS,
28 };
29
30 struct mqtt_rule {
31         char topic[128];
32         char from[128];
33         enum filter_action      action;
34         enum filter_dir         dir;
35         int     log;
36         int     quick;
37 };
38
39 static size_t parsed_lines;
40 %}
41
42 %union {
43         int64_t number;
44         char *string;
45 };
46
47 %token ACTION IN OUT FROM QUICK LOG SOCKET
48 %token TOPIC WORD
49 %token ANY FROM
50 %token SLASH HYPHEN
51 %token <string> WORD SOCKET
52 %token <number> NUMBER
53 %type <number> ACTION DIR QUICK LOG
54 %type <string> from topic
55
56
57 %%
58
59 ruleset: /* empty */
60            | 
61            ruleset mqttrule
62            ;
63
64 mqttrule        : ACTION DIR topic from
65            {
66            parsed_lines++;
67            struct mqtt_rule r;
68            memset(&r, 0, sizeof(r));
69            r.action = $1;
70            r.dir = $2;
71            if ($3)
72                    strlcpy(r.topic, $3, sizeof r.topic);
73            if ($4)
74                    strlcpy(r.from, $4, sizeof r.from);
75
76                 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>");
77            };
78
79 topic   :       ANY {$$=NULL;}
80                 |       WORD {$$=$1;}
81                 ;
82
83 from    : /* empty */ {$$=NULL;}
84                 | FROM SOCKET {$$ = $2;}
85                 ;
86 %%
87
88 void
89 yyerror(msg)
90         char *msg;
91 {
92         fprintf(stderr, "while parsing \"%s\"\n",
93                 msg);
94
95         return;
96 }
97
98 int main( int argc, char **argv )
99 {
100         ++argv, --argc;  /* skip over program name */
101         if ( argc > 0 )
102                 yyin = fopen( argv[0], "r" );
103         else
104                 yyin = stdin;
105
106         yyparse();
107         printf("\n#%zu.\n", parsed_lines);
108
109 }