docs: todo: update name assignments
[netsniff-ng.git] / src / bpf_hla_parser.y
blob5d401443683c15dda0062aa4145dcdaf609475f9
1 /*
2 * netsniff-ng - the packet sniffing beast
3 * By Daniel Borkmann <daniel@netsniff-ng.org>
4 * Copyright 2012 Daniel Borkmann <dborkma@tik.ee.ethz.ch>,
5 * Swiss federal institute of technology (ETH Zurich)
6 * Subject to the GPL, version 2.
7 */
9 %{
11 #include <stdio.h>
12 #include <stdlib.h>
13 #include <stdbool.h>
14 #include <stdint.h>
15 #include <errno.h>
17 #include "bpf.h"
18 #include "bpf_hla_parser.tab.h"
19 #include "bpf_symtab.h"
20 #include "xmalloc.h"
21 #include "xutils.h"
22 #include "built_in.h"
23 #include "die.h"
25 #if 0
26 example:
28 def x;
29 def y;
30 x = pkt(12, 2);
31 ret x;
33 #endif
35 int compile_hla_filter(char *file, int verbose, int debug);
37 #define YYERROR_VERBOSE 0
38 #define YYDEBUG 0
39 #define YYENABLE_NLS 1
40 #define YYLTYPE_IS_TRIVIAL 1
41 #define ENABLE_NLS 1
43 extern FILE *zzin;
44 extern int zzlex(void);
45 extern void zzerror(const char *);
46 extern int zzlineno;
47 extern char *zztext;
51 %union {
52 int idx;
53 long int number;
56 %token K_NAME K_DEF K_PKT K_RET K_IF K_ELIF K_ELSE
57 %token '(' ')' '{' '}' '=' ';' '+' '-' '&' '|' '^' '!' '<' '>' '*' '/' '%' ','
59 %token number_hex number_dec number_oct number_bin
61 %type <idx> K_NAME var
62 %type <number> number_hex number_dec number_oct number_bin number
66 program
67 : '{' declaration_list statement_list '}'
70 declaration_list
71 : declaration ';' declaration_list
72 | /* empty */
75 statement_list
76 : statement ';' statement_list
77 | /* empty */
80 declaration
81 : K_DEF K_NAME {
82 if (bpf_symtab_declared($2)) {
83 panic("Variable \"%s\" already declared (l%d)\n",
84 bpf_symtab_name($2), zzlineno);
85 } else {
86 printf("; @var %s\n", bpf_symtab_name($2));
87 bpf_symtab_declare($2);
91 statement
92 : assignment
93 | return
96 return
97 : K_RET { printf("ret a\n"); }
98 | K_RET number { printf("ret #%ld\n", $2); }
99 | K_RET var { printf("ret a\n"); }
102 assignment
103 : var '=' expression { printf("; @asn %s\n", bpf_symtab_name($1)); }
104 | var '=' K_PKT '(' number ',' number ')' {
105 switch ($7) {
106 case 1:
107 printf("ldb [%ld]\n", $5);
108 break;
109 case 2:
110 printf("ldh [%ld]\n", $5);
111 break;
112 case 4:
113 printf("ld [%ld]\n", $5);
114 break;
115 default:
116 panic("Invalid argument (l%d)\n", zzlineno);
121 expression
122 : term
123 | term '+' term { printf("; @+\n"); }
124 | term '-' term { printf("; @-\n"); }
127 term
128 : number { printf("; @num %ld\n", $1); }
129 | var { printf("; @var %s\n", bpf_symtab_name($1)); }
130 | '(' expression ')'
134 : K_NAME {
135 if (!bpf_symtab_declared($1))
136 panic("Variable \"%s\" not declared (l%d)\n",
137 bpf_symtab_name($1), zzlineno);
138 $$ = $1; }
141 number
142 : number_dec { $$ = $1; }
143 | number_hex { $$ = $1; }
144 | number_oct { $$ = $1; }
145 | number_bin { $$ = $1; }
150 static void stage_1_compile(void)
152 zzparse();
155 int compile_hla_filter(char *file, int verbose, int debug)
157 int fd;
158 fpos_t pos;
159 char file_tmp[128];
161 if (!strncmp("-", file, strlen("-")))
162 zzin = stdin;
163 else
164 zzin = fopen(file, "r");
165 if (!zzin)
166 panic("Cannot open file!\n");
167 if (!debug) {
168 fd = dup(fileno(stdout));
170 slprintf(file_tmp, sizeof(file_tmp), ".%s", file);
171 if (freopen(file_tmp, "w", stdout) == NULL)
172 panic("Cannot reopen file!\n");
175 stage_1_compile();
177 if (!debug) {
178 fflush(stdout);
179 dup2(fd, fileno(stdout));
181 close(fd);
182 clearerr(stdout);
183 fsetpos(stdout, &pos);
186 fclose(zzin);
188 bpf_symtab_cleanup();
189 if (debug)
190 die();
192 return 0;
195 void zzerror(const char *err)
197 panic("Syntax error at line %d: %s! %s!\n",
198 zzlineno, zztext, err);