summaryrefslogtreecommitdiffstats
path: root/src/config.l
blob: f20116ccc1ece04af700299aed4e710e9187ef19 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
/*
  Copyright (c) 2012, Matthias Schiffer <mschiffer@universe-factory.net>
  All rights reserved.

  Redistribution and use in source and binary forms, with or without
  modification, are permitted provided that the following conditions are met:

    1. Redistributions of source code must retain the above copyright notice,
       this list of conditions and the following disclaimer.
    2. Redistributions in binary form must reproduce the above copyright notice,
       this list of conditions and the following disclaimer in the documentation
       and/or other materials provided with the distribution.

  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
  FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/


%option prefix="fastd_config_yy"
%option noyywrap
%option nounput
%option noinput
%option bison-bridge
%option bison-locations
%option reentrant
%option warn


%top {
	 #include <config.yy.h>
}

%s STRING
%s ADDR6
%s COMMENT

%%
%{
	#define UPDATE_LOCATION do { \
			yylloc->first_line = yylloc->last_line; \
			yylloc->first_column = yylloc->last_column+1; \
			yylloc->last_column += yyleng; \
		} while (0)
%}

<INITIAL>{
[0-9]+		{ UPDATE_LOCATION; yylval->num = atoi(yytext); return TOK_INTEGER; }

interface	{ UPDATE_LOCATION; return TOK_INTERFACE; }
bind		{ UPDATE_LOCATION; return TOK_BIND; }
mtu		{ UPDATE_LOCATION; return TOK_MTU; }
mode		{ UPDATE_LOCATION; return TOK_MODE; }
protocol	{ UPDATE_LOCATION; return TOK_PROTOCOL; }
method		{ UPDATE_LOCATION; return TOK_METHOD; }
peer		{ UPDATE_LOCATION; return TOK_PEER; }
address		{ UPDATE_LOCATION; return TOK_ADDRESS; }
secret		{ UPDATE_LOCATION; return TOK_SECRET; }
key		{ UPDATE_LOCATION; return TOK_KEY; }
include		{ UPDATE_LOCATION; return TOK_INCLUDE; }
as		{ UPDATE_LOCATION; return TOK_AS; }
any		{ UPDATE_LOCATION; return TOK_ANY; }
tap		{ UPDATE_LOCATION; return TOK_TAP; }
tun		{ UPDATE_LOCATION; return TOK_TUN; }
on		{ UPDATE_LOCATION; return TOK_ON; }
up		{ UPDATE_LOCATION; return TOK_UP; }
down		{ UPDATE_LOCATION; return TOK_DOWN; }
establish	{ UPDATE_LOCATION; return TOK_ESTABLISH; }
disestablish	{ UPDATE_LOCATION; return TOK_DISESTABLISH; }
peers		{ UPDATE_LOCATION; return TOK_PEERS; }
from		{ UPDATE_LOCATION; return TOK_FROM; }
log		{ UPDATE_LOCATION; return TOK_LOG; }
level 		{ UPDATE_LOCATION; return TOK_LEVEL; }
fatal		{ UPDATE_LOCATION; return TOK_FATAL; }
error		{ UPDATE_LOCATION; return TOK_ERROR; }
warn		{ UPDATE_LOCATION; return TOK_WARN; }
info		{ UPDATE_LOCATION; return TOK_INFO; }
verbose		{ UPDATE_LOCATION; return TOK_VERBOSE; }
debug		{ UPDATE_LOCATION; return TOK_DEBUG; }
peer-to-peer	{ UPDATE_LOCATION; return TOK_PEER_TO_PEER; }
yes		{ UPDATE_LOCATION; return TOK_YES; }
no		{ UPDATE_LOCATION; return TOK_NO; }

[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3} {
			UPDATE_LOCATION;

			if (!inet_pton(AF_INET, yytext, &yylval->addr)) {
				yylval->error = "invalid address";
				return -1;
			}

			return TOK_ADDR;
		}

[;:\{\}]	{ UPDATE_LOCATION; return yytext[0]; }

[ \t]		{ yylloc->last_column++; }
\n		{ yylloc->last_column = 0; yylloc->last_line++; }
}

<INITIAL>\"	{ UPDATE_LOCATION; BEGIN(STRING); }
<STRING>[^"\\\n] { yylloc->last_column++; yymore(); }
<STRING>\n	{ yylloc->last_line++; yylloc->last_column = 0; yymore(); }
<STRING>\\.	{ yylloc->last_column+=2; yymore(); }
<STRING>\\\n	{ yylloc->last_line++; yylloc->last_column = 0; yymore(); }
<STRING>\" {
			int i, esc = 0;

			for (i = 0; i < yyleng; i++) {
				if (yytext[i] == '\\') {
					i++;
					if (yytext[i] == '\n') {
						esc+=2;
					}
					else {
						yytext[i-esc-1] = yytext[i];
						esc++;
					}
				}
				else if(esc) {
					yytext[i-esc] = yytext[i];
				}
			}
			yytext[yyleng-esc-1] = 0;
			yylval->str = fastd_string_stack_dup(yytext);
			BEGIN(INITIAL);
			yylloc->last_column++;
			return TOK_STRING;

		}

<INITIAL>\[	{ UPDATE_LOCATION; BEGIN(ADDR6); }
<ADDR6>[0-9a-fA-F:]+	{
			yylloc->last_column += yyleng;
			if (!inet_pton(AF_INET6, yytext, &yylval->addr6)) {
				yylval->error = "invalid address";
				return -1;
			}
		}
<ADDR6>\]	{ yylloc->last_column++; BEGIN(INITIAL); return TOK_ADDR6; }

<INITIAL>#.*	{ yylloc->last_column += yyleng; }
<INITIAL>\/\/.*	{ yylloc->last_column += yyleng; }

<INITIAL>\/\*	{ UPDATE_LOCATION; BEGIN(COMMENT); }
<COMMENT>\*\/	{ yylloc->last_column += yyleng; BEGIN(INITIAL); }
<COMMENT>.	{ yylloc->last_column++; }
<COMMENT>\n	{ yylloc->last_line++; yylloc->last_column = 0; }

.		{
			yylloc->first_line = yylloc->last_line;
			yylloc->first_column = yylloc->last_column+1;
			yylval->error = "invalid character";
			return -1;
		}

<INITIAL><<EOF>> { return 0; }
<COMMENT><<EOF>> { yylval->error = "unterminated block comment"; return -1; }
<STRING><<EOF>>	{ yylval->error = "unterminated string"; return -1; }
<ADDR6><<EOF>>	{ yylval->error = "unterminated address"; return -1; }
%%