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