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