Debian Codegraph @

src/liboconfig/scanner.l

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