Fix libmodbus detection on FreeBSD
[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 %option noinput
45 %option nounput
46 %x ML
47 WHITE_SPACE [\ \t\b]
48 NON_WHITE_SPACE [^\ \t\b]
49 EOL (\r\n|\n)
50 QUOTED_STRING ([^\\"]+|\\.)*
51 UNQUOTED_STRING [0-9A-Za-z_]+
52 HEX_NUMBER 0[xX][0-9a-fA-F]+
53 OCT_NUMBER 0[0-7]+
54 DEC_NUMBER [\+\-]?[0-9]+
55 FLOAT_NUMBER [\+\-]?[0-9]*\.[0-9]+([eE][\+\-][0-9]+)?
56 NUMBER ({FLOAT_NUMBER}|{HEX_NUMBER}|{OCT_NUMBER}|{DEC_NUMBER})
57 BOOL_TRUE (true|yes|on)
58 BOOL_FALSE (false|no|off)
59 COMMENT #.*
60 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]?)
61 IP_BYTE (2(5[0-5]|[0-4][0-9])|1[0-9][0-9]|[1-9]?[0-9])
62 IPV4_ADDR {IP_BYTE}\.{IP_BYTE}\.{IP_BYTE}\.{IP_BYTE}(:{PORT})?
63
64 %%
65 {WHITE_SPACE}           |
66 {COMMENT}               {/* ignore */}
67
68 \\{EOL}                 {/* continue line */}
69
70 {EOL}                   {return (EOL);}
71 "/"                     {return (SLASH);}
72 "<"                     {return (OPENBRAC);}
73 ">"                     {return (CLOSEBRAC);}
74 {BOOL_TRUE}             {yylval.boolean = 1; return (BTRUE);}
75 {BOOL_FALSE}            {yylval.boolean = 0; return (BFALSE);}
76
77 {IPV4_ADDR}             {yylval.string = yytext; return (UNQUOTED_STRING);}
78
79 {NUMBER}                {yylval.number = strtod (yytext, NULL); return (NUMBER);}
80
81 \"{QUOTED_STRING}\"     {yylval.string = yytext; return (QUOTED_STRING);}
82 {UNQUOTED_STRING}       {yylval.string = yytext; return (UNQUOTED_STRING);}
83
84 \"{QUOTED_STRING}\\{EOL} {
85         int len = strlen (yytext);
86
87         ml_pos = 0;
88
89         /* remove "\\<EOL>" */
90         if ('\r' == yytext[len - 2])
91                 len -= 3;
92         else
93                 len -= 2;
94         yytext[len] = '\0';
95
96         ml_append (yytext);
97         BEGIN (ML);
98 }
99 <ML>^{WHITE_SPACE}+ {/* remove leading white-space */}
100 <ML>{NON_WHITE_SPACE}{QUOTED_STRING}\\{EOL} {
101         int len = strlen (yytext);
102
103         /* remove "\\<EOL>" */
104         if ('\r' == yytext[len - 2])
105                 len -= 3;
106         else
107                 len -= 2;
108         yytext[len] = '\0';
109
110         ml_append(yytext);
111 }
112 <ML>{NON_WHITE_SPACE}{QUOTED_STRING}\" {
113         ml_append(yytext);
114         yylval.string = ml_buffer;
115
116         BEGIN (INITIAL);
117         return (QUOTED_STRING);
118 }
119 %%
120 static void ml_append (char *string)
121 {
122         int len = strlen (string);
123         int s;
124
125         if (ml_free <= len) {
126                 ml_len += len - ml_free + 1;
127                 ml_buffer = (char *)realloc (ml_buffer, ml_len);
128                 if (NULL == ml_buffer)
129                         YY_FATAL_ERROR ("out of dynamic memory in ml_append");
130         }
131
132         s = snprintf (ml_buffer + ml_pos, ml_free, "%s", string);
133         if ((0 > s) || (ml_free <= s))
134                 YY_FATAL_ERROR ("failed to write to multiline buffer");
135
136         ml_pos += s;
137         return;
138 } /* ml_append */
139