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