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>
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.
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
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
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"
29 #include "aux_types.h"
32 /* multiline string buffer */
33 static char *ml_buffer = NULL;
34 static int ml_pos = 0;
35 static int ml_len = 0;
37 #define ml_free (ml_len - ml_pos)
39 static void ml_append (char *);
44 #define yyterminate() \
45 do { free (ml_buffer); ml_buffer = NULL; ml_pos = 0; ml_len = 0; \
46 return YY_NULL; } while (0)
52 NON_WHITE_SPACE [^\ \t\b]
54 QUOTED_STRING ([^\\"]+|\\.)*
55 UNQUOTED_STRING [0-9A-Za-z_]+
56 HEX_NUMBER 0[xX][0-9a-fA-F]+
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)
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})?
70 {COMMENT} {/* ignore */}
72 \\{EOL} {/* continue line */}
76 "<" {return (OPENBRAC);}
77 ">" {return (CLOSEBRAC);}
78 {BOOL_TRUE} {yylval.boolean = 1; return (BTRUE);}
79 {BOOL_FALSE} {yylval.boolean = 0; return (BFALSE);}
81 {IPV4_ADDR} {yylval.string = yytext; return (UNQUOTED_STRING);}
83 {NUMBER} {yylval.number = strtod (yytext, NULL); return (NUMBER);}
85 \"{QUOTED_STRING}\" {yylval.string = yytext; return (QUOTED_STRING);}
86 {UNQUOTED_STRING} {yylval.string = yytext; return (UNQUOTED_STRING);}
88 \"{QUOTED_STRING}\\{EOL} {
89 int len = strlen (yytext);
93 /* remove "\\<EOL>" */
94 if ('\r' == yytext[len - 2])
103 <ML>^{WHITE_SPACE}+ {/* remove leading white-space */}
104 <ML>{NON_WHITE_SPACE}{QUOTED_STRING}\\{EOL} {
105 int len = strlen (yytext);
107 /* remove "\\<EOL>" */
108 if ('\r' == yytext[len - 2])
116 <ML>{NON_WHITE_SPACE}{QUOTED_STRING}\" {
118 yylval.string = ml_buffer;
121 return (QUOTED_STRING);
124 static void ml_append (char *string)
126 int len = strlen (string);
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");
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");