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