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