1 /**
2 * SysDB - src/liboconfig/scanner.l
3 * Based on collectd's liboconfig, <http://collectd.org>
4 * Copyright (C) 2007 Florian Forster
5 * Copyright (C) 2008 Sebastian Harl
6 *
7 * Permission is hereby granted, free of charge, to any person obtaining a
8 * copy of this software and associated documentation files (the "Software"),
9 * to deal in the Software without restriction, including without limitation
10 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
11 * and/or sell copies of the Software, and to permit persons to whom the
12 * Software is furnished to do so, subject to the following conditions:
13 *
14 * The above copyright notice and this permission notice shall be included in
15 * all copies or substantial portions of the Software.
16 *
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
22 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
23 * DEALINGS IN THE SOFTWARE.
24 */
26 %{
27 #include <stdlib.h>
28 #include "oconfig.h"
29 #include "aux_types.h"
30 #include "parser.h"
32 /* multiline string buffer */
33 static char *ml_buffer = NULL;
34 static int ml_pos = 0;
35 static int ml_len = 0;
37 #define ml_free (ml_len - ml_pos)
39 static void ml_append (char *);
41 #ifdef yyterminate
42 # undef yyterminate
43 #endif
44 #define yyterminate() \
45 do { free (ml_buffer); ml_buffer = NULL; ml_pos = 0; ml_len = 0; \
46 return YY_NULL; } while (0)
47 %}
48 %option yylineno
49 %option noyywrap
50 %x ML
51 WHITE_SPACE [\ \t\b]
52 NON_WHITE_SPACE [^\ \t\b]
53 EOL (\r\n|\n)
54 QUOTED_STRING ([^\\"]+|\\.)*
55 UNQUOTED_STRING [0-9A-Za-z_]+
56 HEX_NUMBER 0[xX][0-9a-fA-F]+
57 OCT_NUMBER 0[0-7]+
58 DEC_NUMBER [\+\-]?[0-9]+
59 FLOAT_NUMBER [\+\-]?[0-9]*\.[0-9]+([eE][\+\-][0-9]+)?
60 NUMBER ({FLOAT_NUMBER}|{HEX_NUMBER}|{OCT_NUMBER}|{DEC_NUMBER})
61 BOOL_TRUE (true|yes|on)
62 BOOL_FALSE (false|no|off)
63 COMMENT #.*
64 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]?)
66 IP_BYTE (2(5[0-5]|[0-4][0-9])|1[0-9][0-9]|[1-9]?[0-9])
67 IPV4_ADDR {IP_BYTE}\.{IP_BYTE}\.{IP_BYTE}\.{IP_BYTE}(:{PORT})?
69 /* IPv6 address according to http://www.ietf.org/rfc/rfc2373.txt
70 * This supports embedded IPv4 addresses as well but does not strictly check
71 * for the right prefix (::0:<v4> or ::FFFF:<v4>) because there are too many
72 * ways to correctly represent the zero bytes. It's up to the user to check
73 * for valid addresses. */
74 HEX16 ([0-9A-Fa-f]{1,4})
75 V6_PART ({HEX16}:{HEX16}|{IPV4_ADDR})
76 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})?::
77 IPV6_ADDR ({IPV6_BASE})|(\[{IPV6_BASE}\](:{PORT})?)
79 %%
80 {WHITE_SPACE} |
81 {COMMENT} {/* ignore */}
83 \\{EOL} {/* continue line */}
85 {EOL} {return (EOL);}
86 "/" {return (SLASH);}
87 "<" {return (OPENBRAC);}
88 ">" {return (CLOSEBRAC);}
89 {BOOL_TRUE} {yylval.boolean = 1; return (BTRUE);}
90 {BOOL_FALSE} {yylval.boolean = 0; return (BFALSE);}
92 {IPV4_ADDR} {yylval.string = yytext; return (UNQUOTED_STRING);}
93 {IPV6_ADDR} {yylval.string = yytext; return (UNQUOTED_STRING);}
95 {NUMBER} {yylval.number = strtod (yytext, NULL); return (NUMBER);}
97 \"{QUOTED_STRING}\" {yylval.string = yytext; return (QUOTED_STRING);}
98 {UNQUOTED_STRING} {yylval.string = yytext; return (UNQUOTED_STRING);}
100 \"{QUOTED_STRING}\\{EOL} {
101 int len = strlen (yytext);
103 ml_pos = 0;
105 /* remove "\\<EOL>" */
106 if ('\r' == yytext[len - 2])
107 len -= 3;
108 else
109 len -= 2;
110 yytext[len] = '\0';
112 ml_append (yytext);
113 BEGIN (ML);
114 }
115 <ML>^{WHITE_SPACE}+ {/* remove leading white-space */}
116 <ML>{NON_WHITE_SPACE}{QUOTED_STRING}\\{EOL} {
117 int len = strlen (yytext);
119 /* remove "\\<EOL>" */
120 if ('\r' == yytext[len - 2])
121 len -= 3;
122 else
123 len -= 2;
124 yytext[len] = '\0';
126 ml_append(yytext);
127 }
128 <ML>{NON_WHITE_SPACE}{QUOTED_STRING}\" {
129 ml_append(yytext);
130 yylval.string = ml_buffer;
132 BEGIN (INITIAL);
133 return (QUOTED_STRING);
134 }
135 %%
136 static void ml_append (char *string)
137 {
138 int len = strlen (string);
139 int s;
141 if (ml_free <= len) {
142 ml_len += len - ml_free + 1;
143 ml_buffer = (char *)realloc (ml_buffer, ml_len);
144 if (NULL == ml_buffer)
145 YY_FATAL_ERROR ("out of dynamic memory in ml_append");
146 }
148 s = snprintf (ml_buffer + ml_pos, ml_free, "%s", string);
149 if ((0 > s) || (ml_free <= s))
150 YY_FATAL_ERROR ("failed to write to multiline buffer");
152 ml_pos += s;
153 return;
154 } /* ml_append */