0f146ca488dd23faece3cf7968e4a7ba5bca8a61
[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 "oconfig.h"
23 #include "aux_types.h"
24 #include "parser.h"
25
26 /* multiline string buffer */
27 static char *ml_buffer = NULL;
28 static int   ml_pos    = 0;
29 static int   ml_len    = 0;
30
31 #define ml_free (ml_len - ml_pos)
32
33 static void ml_append (char *);
34
35 #ifdef yyterminate
36 # undef yyterminate
37 #endif
38 #define yyterminate() \
39         do { free (ml_buffer); ml_buffer = NULL; ml_pos = 0; ml_len = 0; \
40                 return YY_NULL; } while (0)
41 %}
42 %option yylineno
43 %option noyywrap
44 %x ML
45 WHITE_SPACE [\ \t\b]
46 NON_WHITE_SPACE [^\ \t\b]
47 QUOTED_STRING ([^\\"]+|\\.)*
48 UNQUOTED_STRING [0-9A-Za-z_]+
49 HEX_NUMBER 0[xX][0-9a-fA-F]+
50 OCT_NUMBER 0[0-7]+
51 DEC_NUMBER [\+\-]?[0-9]+
52 FLOAT_NUMBER [\+\-]?[0-9]*\.[0-9]+([eE][\+\-][0-9]+)?
53 NUMBER ({FLOAT_NUMBER}|{HEX_NUMBER}|{OCT_NUMBER}|{DEC_NUMBER})
54 BOOL_TRUE (true|yes|on)
55 BOOL_FALSE (false|no|off)
56 COMMENT #.*
57 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]?)
58 IP_BYTE (2(5[0-5]|[0-4][0-9])|1[0-9][0-9]|[1-9]?[0-9])
59 IPV4_ADDR {IP_BYTE}\.{IP_BYTE}\.{IP_BYTE}\.{IP_BYTE}(:{PORT})?
60
61 %%
62 {WHITE_SPACE}           |
63 {COMMENT}               {/* ignore */}
64
65 \\\n                    {/* continue line */}
66
67 \n                      {return (EOL);}
68 "/"                     {return (SLASH);}
69 "<"                     {return (OPENBRAC);}
70 ">"                     {return (CLOSEBRAC);}
71 {BOOL_TRUE}             {yylval.boolean = 1; return (TRUE);}
72 {BOOL_FALSE}            {yylval.boolean = 0; return (FALSE);}
73
74 {IPV4_ADDR}             {yylval.string = yytext; return (UNQUOTED_STRING);}
75
76 {NUMBER}                {yylval.number = strtod (yytext, NULL); return (NUMBER);}
77
78 \"{QUOTED_STRING}\"     {yylval.string = yytext; return (QUOTED_STRING);}
79 {UNQUOTED_STRING}       {yylval.string = yytext; return (UNQUOTED_STRING);}
80
81 \"{QUOTED_STRING}\\\n {
82         ml_pos = 0;
83
84         /* remove "\\\n" */
85         yytext[strlen (yytext) - 2] = '\0';
86
87         ml_append (yytext);
88         BEGIN (ML);
89 }
90 <ML>^{WHITE_SPACE}+ {/* remove leading white-space */}
91 <ML>{NON_WHITE_SPACE}{QUOTED_STRING}\\\n {
92         /* remove "\\\n" */
93         yytext[strlen (yytext) - 2] = '\0';
94
95         ml_append(yytext);
96 }
97 <ML>{NON_WHITE_SPACE}{QUOTED_STRING}\" {
98         ml_append(yytext);
99         yylval.string = ml_buffer;
100
101         BEGIN (INITIAL);
102         return (QUOTED_STRING);
103 }
104 %%
105 static void ml_append (char *string)
106 {
107         int len = strlen (string);
108         int s;
109
110         if (ml_free <= len) {
111                 ml_len += len - ml_free + 1;
112                 ml_buffer = (char *)realloc (ml_buffer, ml_len);
113                 if (NULL == ml_buffer)
114                         YY_FATAL_ERROR ("out of dynamic memory in ml_append");
115         }
116
117         s = snprintf (ml_buffer + ml_pos, ml_free, "%s", string);
118         if ((0 > s) || (ml_free <= s))
119                 YY_FATAL_ERROR ("failed to write to multiline buffer");
120
121         ml_pos += s;
122         return;
123 } /* ml_append */
124