chiark / gitweb /
Provide various wrappers for memcpy (COPY_OBJ, BUF_...)
[secnet.git] / conffile.fl
1 /* the "incl" state is used for picking up the name of an include file */
2 %x incl
3
4 %option nounput
5 %option noinput
6 %option never-interactive
7
8 %{
9 #include <assert.h>
10 #include <limits.h>
11 #include <stdio.h>
12 #include <stdlib.h>
13 #include <string.h>
14
15 #include "conffile_internal.h"
16 #include "conffile.tab.h"
17 #include "util.h"
18
19 #define YY_NO_UNPUT
20
21 #define YY_INPUT(buf,result,max_size)                                   \
22 do{                                                                     \
23         (result)= fread((buf),1,(max_size),yyin);                       \
24         if (ferror(yyin))                                               \
25                 fatal_perror("Error reading configuration file (%s)",   \
26                              config_file);                              \
27 }while(0)
28
29 #define MAX_INCLUDE_DEPTH 10
30 struct include_stack_item {
31         YY_BUFFER_STATE bst;
32         int lineno;
33         cstring_t file;
34 };
35 struct include_stack_item include_stack[MAX_INCLUDE_DEPTH];
36 int include_stack_ptr=0;
37
38 int config_lineno=0;
39 cstring_t config_file="xxx";
40
41 static struct p_node *leafnode(uint32_t type)
42 {
43         struct p_node *r;
44
45         r=safe_malloc(sizeof(*r),"leafnode");
46         r->type=type;
47         r->loc.file=config_file;
48         r->loc.line=config_lineno;
49         r->l=NULL; r->r=NULL;
50         return r;
51 }
52
53 static struct p_node *keynode(atom_t key)
54 {
55         struct p_node *r;
56         r=leafnode(T_KEY);
57         r->data.key=intern(key);
58         return r;
59 }
60
61 static struct p_node *stringnode(string_t string)
62 {
63         struct p_node *r;
64         r=leafnode(T_STRING);
65         string++;
66         string[strlen(string)-1]=0;
67         r->data.string=safe_strdup(string,"stringnode");
68         return r;
69 }
70
71 static struct p_node *numnode(string_t number)
72 {
73         struct p_node *r;
74         unsigned long n;
75         r=leafnode(T_NUMBER);
76         errno = 0;
77         n = strtoul(number, NULL, 10);
78         /* The caller is expected to only give us [0-9]+,
79          * so we skip some of the usual syntax checking. */
80         r->data.number=n;
81         /* Give a consistent error message for any kind of
82          * out-of-range condition */
83         if(errno == ERANGE || n != r->data.number) {
84             Message(M_FATAL,"config file %s line %d: '%s' is too big\n",
85                     config_file, config_lineno, number);
86             exit(1);
87         }
88         if(errno) {
89             Message(M_FATAL,"config file %s line %d: '%s': %s\n",
90                     config_file, config_lineno, number, strerror(errno));
91             exit(1);
92         }
93         return r;
94 }
95
96 %}
97
98 %%
99 include                 BEGIN(incl);
100 <incl>[ \t]*            /* eat the whitespace */
101 <incl>[^ \t\n]+         { /* got the include filename */
102         if (include_stack_ptr >= MAX_INCLUDE_DEPTH) {
103                 fatal("Configuration file includes nested too deeply");
104         }
105         include_stack[include_stack_ptr].bst=YY_CURRENT_BUFFER;
106         include_stack[include_stack_ptr].lineno=config_lineno;
107         include_stack[include_stack_ptr].file=config_file;
108         include_stack_ptr++;
109         yyin=fopen(yytext,"r");
110         if (!yyin) {
111                 fatal("Can't open included file %s",yytext);
112         }
113         config_lineno=1;
114         config_file=safe_strdup(yytext,"conffile.fl/include");
115         yy_switch_to_buffer(yy_create_buffer(yyin, YY_BUF_SIZE));
116         BEGIN(INITIAL);
117         }
118 <incl>\n                { /* include with no filename */
119         Message(M_FATAL,"config file %s line %d: %s\n",config_file,
120                 config_lineno,"``include'' requires a filename");
121         BEGIN(INITIAL);
122         assert(config_lineno < INT_MAX);
123         ++config_lineno;
124         ++yynerrs;
125 }
126
127 <<EOF>>         {
128         if (--include_stack_ptr < 0) {
129                 yyterminate();
130                 }
131         else {
132                 fclose(yyin);
133                 yy_delete_buffer(YY_CURRENT_BUFFER);
134                 yy_switch_to_buffer(include_stack[include_stack_ptr].bst);
135                 config_lineno=include_stack[include_stack_ptr].lineno;
136                 config_file=include_stack[include_stack_ptr].file;
137         }
138         }
139 \"[^\"]*\"              yylval=stringnode(yytext); return TOK_STRING;
140
141 [[:alpha:]_][[:alnum:]\-_]*     yylval=keynode(yytext); return TOK_KEY;
142
143 [[:digit:]]+            yylval=numnode(yytext); return TOK_NUMBER;
144
145         /* Eat comments */
146 \#.*\n                  config_lineno++;
147         /* Count lines */
148 \n                      config_lineno++;
149         /* Eat whitespace */
150 [[:blank:]\j]
151
152         /* Return all unclaimed single characters to the parser */
153 .                       return *yytext;
154