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