chiark / gitweb /
1747be26fb321407edeb44ed124c644c13d80ee7
[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 <stdio.h>
10 #include <stdlib.h>
11 #include <string.h>
12
13 #include "conffile_internal.h"
14 #include "conffile.tab.h"
15 #include "util.h"
16
17 #define YY_NO_UNPUT
18
19 #define YY_INPUT(buf,result,max_size)                                   \
20 do{                                                                     \
21         (result)= fread((buf),1,(max_size),yyin);                       \
22         if (ferror(yyin))                                               \
23                 fatal_perror("Error reading configuration file (%s)",   \
24                              config_file);                              \
25 }while(0)
26
27 #define MAX_INCLUDE_DEPTH 10
28 struct include_stack_item {
29         YY_BUFFER_STATE bst;
30         uint32_t lineno;
31         cstring_t file;
32 };
33 struct include_stack_item include_stack[MAX_INCLUDE_DEPTH];
34 int include_stack_ptr=0;
35
36 uint32_t config_lineno=0;
37 cstring_t config_file="xxx";
38
39 static struct p_node *leafnode(uint32_t type)
40 {
41         struct p_node *r;
42
43         r=safe_malloc(sizeof(*r),"leafnode");
44         r->type=type;
45         r->loc.file=config_file;
46         r->loc.line=config_lineno;
47         r->l=NULL; r->r=NULL;
48         return r;
49 }
50
51 static struct p_node *keynode(atom_t key)
52 {
53         struct p_node *r;
54         r=leafnode(T_KEY);
55         r->data.key=intern(key);
56         return r;
57 }
58
59 static struct p_node *stringnode(string_t string)
60 {
61         struct p_node *r;
62         r=leafnode(T_STRING);
63         string++;
64         string[strlen(string)-1]=0;
65         r->data.string=safe_strdup(string,"stringnode");
66         return r;
67 }
68
69 static struct p_node *numnode(string_t number)
70 {
71         struct p_node *r;
72         r=leafnode(T_NUMBER);
73         r->data.number=atoi(number);
74         return r;
75 }
76
77 %}
78
79 %%
80 include                 BEGIN(incl);
81 <incl>[ \t]*            /* eat the whitespace */
82 <incl>[^ \t\n]+         { /* got the include filename */
83         if (include_stack_ptr >= MAX_INCLUDE_DEPTH) {
84                 fatal("Configuration file includes nested too deeply");
85         }
86         include_stack[include_stack_ptr].bst=YY_CURRENT_BUFFER;
87         include_stack[include_stack_ptr].lineno=config_lineno;
88         include_stack[include_stack_ptr].file=config_file;
89         include_stack_ptr++;
90         yyin=fopen(yytext,"r");
91         if (!yyin) {
92                 fatal("Can't open included file %s",yytext);
93         }
94         config_lineno=1;
95         config_file=safe_strdup(yytext,"conffile.fl/include");
96         yy_switch_to_buffer(yy_create_buffer(yyin, YY_BUF_SIZE));
97         BEGIN(INITIAL);
98         }
99 <incl>\n                { /* include with no filename */
100         Message(M_FATAL,"config file %s line %d: %s\n",config_file,
101                 config_lineno,"``include'' requires a filename");
102         BEGIN(INITIAL);
103         ++config_lineno;
104         ++yynerrs;
105 }
106
107 <<EOF>>         {
108         if (--include_stack_ptr < 0) {
109                 yyterminate();
110                 }
111         else {
112                 fclose(yyin);
113                 yy_delete_buffer(YY_CURRENT_BUFFER);
114                 yy_switch_to_buffer(include_stack[include_stack_ptr].bst);
115                 config_lineno=include_stack[include_stack_ptr].lineno;
116                 config_file=include_stack[include_stack_ptr].file;
117         }
118         }
119 \"[^\"]*\"              yylval=stringnode(yytext); return TOK_STRING;
120
121 [[:alpha:]_][[:alnum:]\-_]*     yylval=keynode(yytext); return TOK_KEY;
122
123 [[:digit:]]+            yylval=numnode(yytext); return TOK_NUMBER;
124
125         /* Eat comments */
126 \#.*\n                  config_lineno++;
127         /* Count lines */
128 \n                      config_lineno++;
129         /* Eat whitespace */
130 [[:blank:]\j]
131
132         /* Return all unclaimed single characters to the parser */
133 .                       return *yytext;
134