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