Merged branch 'collectd-4.10' into collectd-5.4.
[collectd.git] / src / liboconfig / scanner.l
1 /**
2  * oconfig - src/scanner.l
3  * Copyright (C) 2007  Florian octo Forster <octo at verplant.org>
4  * Copyright (C) 2008  Sebastian tokkee Harl <sh at tokkee.org>
5  *
6  * This program is free software; you can redistribute it and/or modify it
7  * under the terms of the GNU General Public License as published by the
8  * Free Software Foundation; only version 2 of the License is applicable.
9  *
10  * This program is distributed in the hope that it will be useful, but WITHOUT
11  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
12  * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
13  * more details.
14  *
15  * You should have received a copy of the GNU General Public License along with
16  * this program; if not, write to the Free Software Foundation, Inc.,
17  * 51 Franklin St, Fifth Floor, Boston, MA  02110-1301 USA
18  */
19
20 %{
21 #include <stdlib.h>
22 #include <string.h>
23 #include "oconfig.h"
24 #include "aux_types.h"
25 #include "parser.h"
26
27 /* multiline string buffer */
28 static char *ml_buffer = NULL;
29 static int   ml_pos    = 0;
30 static int   ml_len    = 0;
31
32 #define ml_free (ml_len - ml_pos)
33
34 static void ml_append (char *);
35
36 #ifdef yyterminate
37 # undef yyterminate
38 #endif
39 #define yyterminate() \
40         do { free (ml_buffer); ml_buffer = NULL; ml_pos = 0; ml_len = 0; \
41                 return YY_NULL; } while (0)
42 %}
43 %option yylineno
44 %option noyywrap
45 %option noinput
46 %option nounput
47 %x ML
48 WHITE_SPACE [\ \t\b]
49 NON_WHITE_SPACE [^\ \t\b]
50 EOL (\r\n|\n)
51 QUOTED_STRING ([^\\"]+|\\.)*
52 UNQUOTED_STRING [0-9A-Za-z_]+
53 HEX_NUMBER 0[xX][0-9a-fA-F]+
54 OCT_NUMBER 0[0-7]+
55 DEC_NUMBER [\+\-]?[0-9]+
56 FLOAT_NUMBER [\+\-]?[0-9]*\.[0-9]+([eE][\+\-][0-9]+)?
57 NUMBER ({FLOAT_NUMBER}|{HEX_NUMBER}|{OCT_NUMBER}|{DEC_NUMBER})
58 BOOL_TRUE (true|yes|on)
59 BOOL_FALSE (false|no|off)
60 COMMENT #.*
61 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]?)
62 IP_BYTE (2(5[0-5]|[0-4][0-9])|1[0-9][0-9]|[1-9]?[0-9])
63 IPV4_ADDR {IP_BYTE}\.{IP_BYTE}\.{IP_BYTE}\.{IP_BYTE}(:{PORT})?
64
65 %%
66 {WHITE_SPACE}           |
67 {COMMENT}               {/* ignore */}
68
69 \\{EOL}                 {/* continue line */}
70
71 {EOL}                   {return (EOL);}
72 "/"                     {return (SLASH);}
73 "<"                     {return (OPENBRAC);}
74 ">"                     {return (CLOSEBRAC);}
75 {BOOL_TRUE}             {yylval.boolean = 1; return (BTRUE);}
76 {BOOL_FALSE}            {yylval.boolean = 0; return (BFALSE);}
77
78 {IPV4_ADDR}             {yylval.string = yytext; return (UNQUOTED_STRING);}
79
80 {NUMBER}                {yylval.number = strtod (yytext, NULL); return (NUMBER);}
81
82 \"{QUOTED_STRING}\"     {yylval.string = yytext; return (QUOTED_STRING);}
83 {UNQUOTED_STRING}       {yylval.string = yytext; return (UNQUOTED_STRING);}
84
85 \"{QUOTED_STRING}\\{EOL} {
86         int len = strlen (yytext);
87
88         ml_pos = 0;
89
90         /* remove "\\<EOL>" */
91         if ('\r' == yytext[len - 2])
92                 len -= 3;
93         else
94                 len -= 2;
95         yytext[len] = '\0';
96
97         ml_append (yytext);
98         BEGIN (ML);
99 }
100 <ML>^{WHITE_SPACE}+ {/* remove leading white-space */}
101 <ML>{NON_WHITE_SPACE}{QUOTED_STRING}\\{EOL} {
102         int len = strlen (yytext);
103
104         /* remove "\\<EOL>" */
105         if ('\r' == yytext[len - 2])
106                 len -= 3;
107         else
108                 len -= 2;
109         yytext[len] = '\0';
110
111         ml_append(yytext);
112 }
113 <ML>{NON_WHITE_SPACE}{QUOTED_STRING}\" {
114         ml_append(yytext);
115         yylval.string = ml_buffer;
116
117         BEGIN (INITIAL);
118         return (QUOTED_STRING);
119 }
120 %%
121 static void ml_append (char *string)
122 {
123         int len = strlen (string);
124         int s;
125
126         if (ml_free <= len) {
127                 ml_len += len - ml_free + 1;
128                 ml_buffer = (char *)realloc (ml_buffer, ml_len);
129                 if (NULL == ml_buffer)
130                         YY_FATAL_ERROR ("out of dynamic memory in ml_append");
131         }
132
133         s = snprintf (ml_buffer + ml_pos, ml_free, "%s", string);
134         if ((0 > s) || (ml_free <= s))
135                 YY_FATAL_ERROR ("failed to write to multiline buffer");
136
137         ml_pos += s;
138         return;
139 } /* ml_append */
140