f578ae1098c5160437495860032eda87baaa6e09
[collectd.git] / src / liboconfig / scanner.l
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 #include <stdlib.h>
31 #include <string.h>
32 #include "oconfig.h"
33 #include "aux_types.h"
34 #include "parser.h"
35
36 #pragma GCC diagnostic ignored "-Wsign-compare"
37 #ifdef __clang__
38 #pragma clang diagnostic push
39 #pragma clang diagnostic ignored "-Wmissing-noreturn"
40 #endif
41
42
43 /* multiline string buffer */
44 static char *ml_buffer = NULL;
45 static int   ml_pos    = 0;
46 static int   ml_len    = 0;
47
48 #define ml_free (ml_len - ml_pos)
49
50 static void ml_append (char *);
51
52 #ifdef yyterminate
53 # undef yyterminate
54 #endif
55 #define yyterminate() \
56         do { free (ml_buffer); ml_buffer = NULL; ml_pos = 0; ml_len = 0; \
57                 return YY_NULL; } while (0)
58 %}
59 %option yylineno
60 %option noyywrap
61 %option noinput
62 %option nounput
63 %x ML
64 WHITE_SPACE [\ \t\b]
65 NON_WHITE_SPACE [^\ \t\b]
66 EOL (\r\n|\n)
67 QUOTED_STRING ([^\\"]+|\\.)*
68 UNQUOTED_STRING [0-9A-Za-z_]+
69 HEX_NUMBER 0[xX][0-9a-fA-F]+
70 OCT_NUMBER 0[0-7]+
71 DEC_NUMBER [\+\-]?[0-9]+
72 FLOAT_NUMBER [\+\-]?[0-9]*\.[0-9]+([eE][\+\-][0-9]+)?
73 NUMBER ({FLOAT_NUMBER}|{HEX_NUMBER}|{OCT_NUMBER}|{DEC_NUMBER})
74 BOOL_TRUE (true|yes|on)
75 BOOL_FALSE (false|no|off)
76 COMMENT #.*
77 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]?)
78
79 IP_BYTE (2(5[0-5]|[0-4][0-9])|1[0-9][0-9]|[1-9]?[0-9])
80 IPV4_ADDR {IP_BYTE}\.{IP_BYTE}\.{IP_BYTE}\.{IP_BYTE}(:{PORT})?
81
82 /* IPv6 address according to http://www.ietf.org/rfc/rfc2373.txt
83  * This supports embedded IPv4 addresses as well but does not strictly check
84  * for the right prefix (::0:<v4> or ::FFFF:<v4>) because there are too many
85  * ways to correctly represent the zero bytes. It's up to the user to check
86  * for valid addresses. */
87 HEX16 ([0-9A-Fa-f]{1,4})
88 V6_PART ({HEX16}:{HEX16}|{IPV4_ADDR})
89 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})?::
90 IPV6_ADDR ({IPV6_BASE})|(\[{IPV6_BASE}\](:{PORT})?)
91
92 %%
93 {WHITE_SPACE}           |
94 {COMMENT}               {/* ignore */}
95
96 \\{EOL}                 {/* continue line */}
97
98 {EOL}                   {return (EOL);}
99 "/"                     {return (SLASH);}
100 "<"                     {return (OPENBRAC);}
101 ">"                     {return (CLOSEBRAC);}
102 {BOOL_TRUE}             {yylval.boolean = 1; return (BTRUE);}
103 {BOOL_FALSE}            {yylval.boolean = 0; return (BFALSE);}
104
105 {IPV4_ADDR}             {yylval.string = yytext; return (UNQUOTED_STRING);}
106 {IPV6_ADDR}             {yylval.string = yytext; return (UNQUOTED_STRING);}
107
108 {NUMBER}                {yylval.number = strtod (yytext, NULL); return (NUMBER);}
109
110 \"{QUOTED_STRING}\"     {yylval.string = yytext; return (QUOTED_STRING);}
111 {UNQUOTED_STRING}       {yylval.string = yytext; return (UNQUOTED_STRING);}
112
113 \"{QUOTED_STRING}\\{EOL} {
114         int len = strlen (yytext);
115
116         ml_pos = 0;
117
118         /* remove "\\<EOL>" */
119         if ('\r' == yytext[len - 2])
120                 len -= 3;
121         else
122                 len -= 2;
123         yytext[len] = '\0';
124
125         ml_append (yytext);
126         BEGIN (ML);
127 }
128 <ML>^{WHITE_SPACE}+ {/* remove leading white-space */}
129 <ML>{NON_WHITE_SPACE}{QUOTED_STRING}\\{EOL} {
130         int len = strlen (yytext);
131
132         /* remove "\\<EOL>" */
133         if ('\r' == yytext[len - 2])
134                 len -= 3;
135         else
136                 len -= 2;
137         yytext[len] = '\0';
138
139         ml_append(yytext);
140 }
141 <ML>{NON_WHITE_SPACE}{QUOTED_STRING}\" {
142         ml_append(yytext);
143         yylval.string = ml_buffer;
144
145         BEGIN (INITIAL);
146         return (QUOTED_STRING);
147 }
148 %%
149 static void ml_append (char *string)
150 {
151         int len = strlen (string);
152         int s;
153
154         if (ml_free <= len) {
155                 ml_len += len - ml_free + 1;
156                 ml_buffer = realloc (ml_buffer, ml_len);
157                 if (NULL == ml_buffer)
158                         YY_FATAL_ERROR ("out of dynamic memory in ml_append");
159         }
160
161         s = snprintf (ml_buffer + ml_pos, ml_free, "%s", string);
162         if ((0 > s) || (ml_free <= s))
163                 YY_FATAL_ERROR ("failed to write to multiline buffer");
164
165         ml_pos += s;
166         return;
167 } /* ml_append */
168
169 #ifdef __clang__
170 #pragma clang diagnostic pop
171 #endif