make: add flags and fix some warnings
[netsniff-ng.git] / src / bpf_hla_parser.y
blob9f242510e6b7f4b36b3fe65bfbfd7b741942a928
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 /* yaac-func-prefix: zz */
12 TODO:
13 - intermediate representation
14 - code optimization (symbolic reduction?)
15 - linearization (jumps, etc)
16 - bpf emitter
21 #include <stdio.h>
22 #include <stdlib.h>
23 #include <stdbool.h>
24 #include <stdint.h>
25 #include <errno.h>
27 #include "bpf.h"
28 #include "bpf_hla_parser.tab.h"
29 #include "bpf_symtab.h"
30 #include "xmalloc.h"
31 #include "xutils.h"
32 #include "built_in.h"
33 #include "die.h"
35 #define YYERROR_VERBOSE 0
36 #define YYDEBUG 0
37 #define YYENABLE_NLS 1
38 #define YYLTYPE_IS_TRIVIAL 1
39 #define ENABLE_NLS 1
41 extern FILE *zzin;
42 extern int zzlex(void);
43 extern void zzerror(const char *);
44 extern int zzlineno;
45 extern char *zztext;
47 int compile_hla_filter(char *file, int verbose, int debug);
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 K_MACRO_IPV4 K_MACRO_IPV6 K_MACRO_IP K_MACRO_UDP K_MACRO_TCP
59 %token number
61 %token '(' ')' '{' '}' '=' ';' '+' '-' '&' '|' '^' '!' '<' '>' '*' '/' '%' ','
63 %type <number> number
67 program
68 : { printf("Not supported yet!\n"); }
73 static void stage_1_compile(void)
75 zzparse();
78 int compile_hla_filter(char *file, int verbose, int debug)
80 int fd;
81 fpos_t pos;
82 char file_tmp[128];
84 /* XXX */
85 verbose = verbose;
87 if (!strncmp("-", file, strlen("-")))
88 zzin = stdin;
89 else
90 zzin = fopen(file, "r");
91 if (!zzin)
92 panic("Cannot open file!\n");
93 if (!debug) {
94 fd = dup(fileno(stdout));
96 slprintf(file_tmp, sizeof(file_tmp), ".%s", file);
97 if (freopen(file_tmp, "w", stdout) == NULL)
98 panic("Cannot reopen file!\n");
101 stage_1_compile();
103 if (!debug) {
104 fflush(stdout);
105 dup2(fd, fileno(stdout));
107 close(fd);
108 clearerr(stdout);
109 fsetpos(stdout, &pos);
112 fclose(zzin);
114 bpf_symtab_cleanup();
115 if (debug)
116 die();
118 return 0;
121 void zzerror(const char *err)
123 panic("Syntax error at line %d: %s! %s!\n",
124 zzlineno, zztext, err);