site: Prepare for adding more MSG3 variants
[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
9c6a8729 7 * the Free Software Foundation; either version 3 of the License, or
c215a4bc
IJ
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
b11a9de3 25%option noyywrap
b8dc47dd 26
2fe58dfd 27%{
59230b9b
IJ
28#include <assert.h>
29#include <limits.h>
2fe58dfd
SE
30#include <stdio.h>
31#include <stdlib.h>
32#include <string.h>
33
34#include "conffile_internal.h"
35#include "conffile.tab.h"
36#include "util.h"
37
558fa3fb
SE
38#define YY_NO_UNPUT
39
4f5e39ec
SE
40#define YY_INPUT(buf,result,max_size) \
41do{ \
42 (result)= fread((buf),1,(max_size),yyin); \
43 if (ferror(yyin)) \
44 fatal_perror("Error reading configuration file (%s)", \
45 config_file); \
46}while(0)
47
2fe58dfd 48#define MAX_INCLUDE_DEPTH 10
974d0468
SE
49struct include_stack_item {
50 YY_BUFFER_STATE bst;
3f36eb5f 51 int lineno;
fe5e9cc4 52 cstring_t file;
974d0468
SE
53};
54struct include_stack_item include_stack[MAX_INCLUDE_DEPTH];
2fe58dfd
SE
55int include_stack_ptr=0;
56
1caa23ff 57int config_lineno=0;
fe5e9cc4 58cstring_t config_file="xxx";
2fe58dfd
SE
59
60static struct p_node *leafnode(uint32_t type)
61{
62 struct p_node *r;
63
b7886fd4 64 NEW(r);
2fe58dfd
SE
65 r->type=type;
66 r->loc.file=config_file;
67 r->loc.line=config_lineno;
68 r->l=NULL; r->r=NULL;
69 return r;
70}
71
72static struct p_node *keynode(atom_t key)
73{
74 struct p_node *r;
75 r=leafnode(T_KEY);
76 r->data.key=intern(key);
77 return r;
78}
79
80static struct p_node *stringnode(string_t string)
81{
82 struct p_node *r;
83 r=leafnode(T_STRING);
84 string++;
85 string[strlen(string)-1]=0;
86 r->data.string=safe_strdup(string,"stringnode");
87 return r;
88}
89
90static struct p_node *numnode(string_t number)
91{
92 struct p_node *r;
3f36eb5f 93 unsigned long n;
2fe58dfd 94 r=leafnode(T_NUMBER);
3f36eb5f
RK
95 errno = 0;
96 n = strtoul(number, NULL, 10);
97 /* The caller is expected to only give us [0-9]+,
98 * so we skip some of the usual syntax checking. */
99 r->data.number=n;
100 /* Give a consistent error message for any kind of
101 * out-of-range condition */
102 if(errno == ERANGE || n != r->data.number) {
103 Message(M_FATAL,"config file %s line %d: '%s' is too big\n",
104 config_file, config_lineno, number);
105 exit(1);
106 }
107 if(errno) {
108 Message(M_FATAL,"config file %s line %d: '%s': %s\n",
109 config_file, config_lineno, number, strerror(errno));
110 exit(1);
111 }
2fe58dfd
SE
112 return r;
113}
114
115%}
116
117%%
118include BEGIN(incl);
119<incl>[ \t]* /* eat the whitespace */
120<incl>[^ \t\n]+ { /* got the include filename */
121 if (include_stack_ptr >= MAX_INCLUDE_DEPTH) {
122 fatal("Configuration file includes nested too deeply");
123 }
974d0468
SE
124 include_stack[include_stack_ptr].bst=YY_CURRENT_BUFFER;
125 include_stack[include_stack_ptr].lineno=config_lineno;
126 include_stack[include_stack_ptr].file=config_file;
127 include_stack_ptr++;
2fe58dfd
SE
128 yyin=fopen(yytext,"r");
129 if (!yyin) {
130 fatal("Can't open included file %s",yytext);
131 }
974d0468
SE
132 config_lineno=1;
133 config_file=safe_strdup(yytext,"conffile.fl/include");
2fe58dfd
SE
134 yy_switch_to_buffer(yy_create_buffer(yyin, YY_BUF_SIZE));
135 BEGIN(INITIAL);
136 }
fe5e9cc4
SE
137<incl>\n { /* include with no filename */
138 Message(M_FATAL,"config file %s line %d: %s\n",config_file,
139 config_lineno,"``include'' requires a filename");
140 BEGIN(INITIAL);
59230b9b 141 assert(config_lineno < INT_MAX);
fe5e9cc4
SE
142 ++config_lineno;
143 ++yynerrs;
144}
145
2fe58dfd
SE
146<<EOF>> {
147 if (--include_stack_ptr < 0) {
148 yyterminate();
149 }
150 else {
151 fclose(yyin);
152 yy_delete_buffer(YY_CURRENT_BUFFER);
974d0468
SE
153 yy_switch_to_buffer(include_stack[include_stack_ptr].bst);
154 config_lineno=include_stack[include_stack_ptr].lineno;
155 config_file=include_stack[include_stack_ptr].file;
2fe58dfd
SE
156 }
157 }
158\"[^\"]*\" yylval=stringnode(yytext); return TOK_STRING;
159
160[[:alpha:]_][[:alnum:]\-_]* yylval=keynode(yytext); return TOK_KEY;
161
162[[:digit:]]+ yylval=numnode(yytext); return TOK_NUMBER;
163
164 /* Eat comments */
165\#.*\n config_lineno++;
166 /* Count lines */
167\n config_lineno++;
168 /* Eat whitespace */
169[[:blank:]\j]
170
171 /* Return all unclaimed single characters to the parser */
172. return *yytext;