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