1 /**
2 * oconfig - src/scanner.l
3 * Copyright (C) 2007 Florian octo Forster <octo at verplant.org>
4 * Copyright (C) 2008 Sebastian tokkee Harl <sh at tokkee.org>
5 *
6 * This program is free software; you can redistribute it and/or modify it
7 * under the terms of the GNU General Public License as published by the
8 * Free Software Foundation; only version 2 of the License is applicable.
9 *
10 * This program is distributed in the hope that it will be useful, but WITHOUT
11 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
12 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
13 * more details.
14 *
15 * You should have received a copy of the GNU General Public License along with
16 * this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
18 */
20 %{
21 #include <stdlib.h>
22 #include "oconfig.h"
23 #include "aux_types.h"
24 #include "parser.h"
26 /* multiline string buffer */
27 static char *ml_buffer = NULL;
28 static int ml_pos = 0;
29 static int ml_len = 0;
31 #define ml_free (ml_len - ml_pos)
33 static void ml_append (char *);
35 #ifdef yyterminate
36 # undef yyterminate
37 #endif
38 #define yyterminate() \
39 do { free (ml_buffer); ml_buffer = NULL; ml_pos = 0; ml_len = 0; \
40 return YY_NULL; } while (0)
41 %}
42 %option yylineno
43 %option noyywrap
44 %x ML
45 WHITE_SPACE [\ \t\b]
46 NON_WHITE_SPACE [^\ \t\b]
47 EOL (\r\n|\n)
48 QUOTED_STRING ([^\\"]+|\\.)*
49 UNQUOTED_STRING [0-9A-Za-z_]+
50 HEX_NUMBER 0[xX][0-9a-fA-F]+
51 OCT_NUMBER 0[0-7]+
52 DEC_NUMBER [\+\-]?[0-9]+
53 FLOAT_NUMBER [\+\-]?[0-9]*\.[0-9]+([eE][\+\-][0-9]+)?
54 NUMBER ({FLOAT_NUMBER}|{HEX_NUMBER}|{OCT_NUMBER}|{DEC_NUMBER})
55 BOOL_TRUE (true|yes|on)
56 BOOL_FALSE (false|no|off)
57 COMMENT #.*
58 PORT (6(5(5(3[0-5]|[0-2][0-9])|[0-4][0-9][0-9])|[0-4][0-9][0-9][0-9])|[1-5][0-9][0-9][0-9][0-9]|[1-9][0-9]?[0-9]?[0-9]?)
60 IP_BYTE (2(5[0-5]|[0-4][0-9])|1[0-9][0-9]|[1-9]?[0-9])
61 IPV4_ADDR {IP_BYTE}\.{IP_BYTE}\.{IP_BYTE}\.{IP_BYTE}(:{PORT})?
63 /* IPv6 address according to http://www.ietf.org/rfc/rfc2373.txt
64 * This supports embedded IPv4 addresses as well but does not strictly check
65 * for the right prefix (::0:<v4> or ::FFFF:<v4>) because there are too many
66 * ways to correctly represent the zero bytes. It's up to the user to check
67 * for valid addresses. */
68 HEX16 ([0-9A-Fa-f]{1,4})
69 V6_PART ({HEX16}:{HEX16}|{IPV4_ADDR})
70 IPV6_BASE ({HEX16}:){6}{V6_PART}|::({HEX16}:){5}{V6_PART}|({HEX16})?::({HEX16}:){4}{V6_PART}|(({HEX16}:){0,1}{HEX16})?::({HEX16}:){3}{V6_PART}|(({HEX16}:){0,2}{HEX16})?::({HEX16}:){2}{V6_PART}|(({HEX16}:){0,3}{HEX16})?::{HEX16}:{V6_PART}|(({HEX16}:){0,4}{HEX16})?::{V6_PART}|(({HEX16}:){0,5}{HEX16})?::{HEX16}|(({HEX16}:){0,6}{HEX16})?::
71 IPV6_ADDR ({IPV6_BASE})|(\[{IPV6_BASE}\](:{PORT})?)
73 %%
74 {WHITE_SPACE} |
75 {COMMENT} {/* ignore */}
77 \\{EOL} {/* continue line */}
79 {EOL} {return (EOL);}
80 "/" {return (SLASH);}
81 "<" {return (OPENBRAC);}
82 ">" {return (CLOSEBRAC);}
83 {BOOL_TRUE} {yylval.boolean = 1; return (BTRUE);}
84 {BOOL_FALSE} {yylval.boolean = 0; return (BFALSE);}
86 {IPV4_ADDR} {yylval.string = yytext; return (UNQUOTED_STRING);}
87 {IPV6_ADDR} {yylval.string = yytext; return (UNQUOTED_STRING);}
89 {NUMBER} {yylval.number = strtod (yytext, NULL); return (NUMBER);}
91 \"{QUOTED_STRING}\" {yylval.string = yytext; return (QUOTED_STRING);}
92 {UNQUOTED_STRING} {yylval.string = yytext; return (UNQUOTED_STRING);}
94 \"{QUOTED_STRING}\\{EOL} {
95 int len = strlen (yytext);
97 ml_pos = 0;
99 /* remove "\\<EOL>" */
100 if ('\r' == yytext[len - 2])
101 len -= 3;
102 else
103 len -= 2;
104 yytext[len] = '\0';
106 ml_append (yytext);
107 BEGIN (ML);
108 }
109 <ML>^{WHITE_SPACE}+ {/* remove leading white-space */}
110 <ML>{NON_WHITE_SPACE}{QUOTED_STRING}\\{EOL} {
111 int len = strlen (yytext);
113 /* remove "\\<EOL>" */
114 if ('\r' == yytext[len - 2])
115 len -= 3;
116 else
117 len -= 2;
118 yytext[len] = '\0';
120 ml_append(yytext);
121 }
122 <ML>{NON_WHITE_SPACE}{QUOTED_STRING}\" {
123 ml_append(yytext);
124 yylval.string = ml_buffer;
126 BEGIN (INITIAL);
127 return (QUOTED_STRING);
128 }
129 %%
130 static void ml_append (char *string)
131 {
132 int len = strlen (string);
133 int s;
135 if (ml_free <= len) {
136 ml_len += len - ml_free + 1;
137 ml_buffer = (char *)realloc (ml_buffer, ml_len);
138 if (NULL == ml_buffer)
139 YY_FATAL_ERROR ("out of dynamic memory in ml_append");
140 }
142 s = snprintf (ml_buffer + ml_pos, ml_free, "%s", string);
143 if ((0 > s) || (ml_free <= s))
144 YY_FATAL_ERROR ("failed to write to multiline buffer");
146 ml_pos += s;
147 return;
148 } /* ml_append */