chiark / gitweb /
changelog: Retrospectively some items missing from 0.4.1
[secnet] / conffile.fl
CommitLineData
c215a4bc
IJ
1/*
2 * This file is part of secnet.
3 * See README for full list of copyright holders.
4 *
5 * secnet is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version d of the License, or
8 * (at your option) any later version.
9 *
10 * secnet is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * version 3 along with secnet; if not, see
17 * https://www.gnu.org/licenses/gpl.html.
18 */
2fe58dfd
SE
19/* the "incl" state is used for picking up the name of an include file */
20%x incl
21
b8dc47dd
IJ
22%option nounput
23%option noinput
f0fb73c5 24%option never-interactive
b8dc47dd 25
2fe58dfd 26%{
59230b9b
IJ
27#include <assert.h>
28#include <limits.h>
2fe58dfd
SE
29#include <stdio.h>
30#include <stdlib.h>
31#include <string.h>
32
33#include "conffile_internal.h"
34#include "conffile.tab.h"
35#include "util.h"
36
558fa3fb
SE
37#define YY_NO_UNPUT
38
4f5e39ec
SE
39#define YY_INPUT(buf,result,max_size) \
40do{ \
41 (result)= fread((buf),1,(max_size),yyin); \
42 if (ferror(yyin)) \
43 fatal_perror("Error reading configuration file (%s)", \
44 config_file); \
45}while(0)
46
2fe58dfd 47#define MAX_INCLUDE_DEPTH 10
974d0468
SE
48struct include_stack_item {
49 YY_BUFFER_STATE bst;
3f36eb5f 50 int lineno;
fe5e9cc4 51 cstring_t file;
974d0468
SE
52};
53struct include_stack_item include_stack[MAX_INCLUDE_DEPTH];
2fe58dfd
SE
54int include_stack_ptr=0;
55
1caa23ff 56int config_lineno=0;
fe5e9cc4 57cstring_t config_file="xxx";
2fe58dfd
SE
58
59static struct p_node *leafnode(uint32_t type)
60{
61 struct p_node *r;
62
b7886fd4 63 NEW(r);
2fe58dfd
SE
64 r->type=type;
65 r->loc.file=config_file;
66 r->loc.line=config_lineno;
67 r->l=NULL; r->r=NULL;
68 return r;
69}
70
71static struct p_node *keynode(atom_t key)
72{
73 struct p_node *r;
74 r=leafnode(T_KEY);
75 r->data.key=intern(key);
76 return r;
77}
78
79static struct p_node *stringnode(string_t string)
80{
81 struct p_node *r;
82 r=leafnode(T_STRING);
83 string++;
84 string[strlen(string)-1]=0;
85 r->data.string=safe_strdup(string,"stringnode");
86 return r;
87}
88
89static struct p_node *numnode(string_t number)
90{
91 struct p_node *r;
3f36eb5f 92 unsigned long n;
2fe58dfd 93 r=leafnode(T_NUMBER);
3f36eb5f
RK
94 errno = 0;
95 n = strtoul(number, NULL, 10);
96 /* The caller is expected to only give us [0-9]+,
97 * so we skip some of the usual syntax checking. */
98 r->data.number=n;
99 /* Give a consistent error message for any kind of
100 * out-of-range condition */
101 if(errno == ERANGE || n != r->data.number) {
102 Message(M_FATAL,"config file %s line %d: '%s' is too big\n",
103 config_file, config_lineno, number);
104 exit(1);
105 }
106 if(errno) {
107 Message(M_FATAL,"config file %s line %d: '%s': %s\n",
108 config_file, config_lineno, number, strerror(errno));
109 exit(1);
110 }
2fe58dfd
SE
111 return r;
112}
113
114%}
115
116%%
117include BEGIN(incl);
118<incl>[ \t]* /* eat the whitespace */
119<incl>[^ \t\n]+ { /* got the include filename */
120 if (include_stack_ptr >= MAX_INCLUDE_DEPTH) {
121 fatal("Configuration file includes nested too deeply");
122 }
974d0468
SE
123 include_stack[include_stack_ptr].bst=YY_CURRENT_BUFFER;
124 include_stack[include_stack_ptr].lineno=config_lineno;
125 include_stack[include_stack_ptr].file=config_file;
126 include_stack_ptr++;
2fe58dfd
SE
127 yyin=fopen(yytext,"r");
128 if (!yyin) {
129 fatal("Can't open included file %s",yytext);
130 }
974d0468
SE
131 config_lineno=1;
132 config_file=safe_strdup(yytext,"conffile.fl/include");
2fe58dfd
SE
133 yy_switch_to_buffer(yy_create_buffer(yyin, YY_BUF_SIZE));
134 BEGIN(INITIAL);
135 }
fe5e9cc4
SE
136<incl>\n { /* include with no filename */
137 Message(M_FATAL,"config file %s line %d: %s\n",config_file,
138 config_lineno,"``include'' requires a filename");
139 BEGIN(INITIAL);
59230b9b 140 assert(config_lineno < INT_MAX);
fe5e9cc4
SE
141 ++config_lineno;
142 ++yynerrs;
143}
144
2fe58dfd
SE
145<<EOF>> {
146 if (--include_stack_ptr < 0) {
147 yyterminate();
148 }
149 else {
150 fclose(yyin);
151 yy_delete_buffer(YY_CURRENT_BUFFER);
974d0468
SE
152 yy_switch_to_buffer(include_stack[include_stack_ptr].bst);
153 config_lineno=include_stack[include_stack_ptr].lineno;
154 config_file=include_stack[include_stack_ptr].file;
2fe58dfd
SE
155 }
156 }
157\"[^\"]*\" yylval=stringnode(yytext); return TOK_STRING;
158
159[[:alpha:]_][[:alnum:]\-_]* yylval=keynode(yytext); return TOK_KEY;
160
161[[:digit:]]+ yylval=numnode(yytext); return TOK_NUMBER;
162
163 /* Eat comments */
164\#.*\n config_lineno++;
165 /* Count lines */
166\n config_lineno++;
167 /* Eat whitespace */
168[[:blank:]\j]
169
170 /* Return all unclaimed single characters to the parser */
171. return *yytext;
172