chiark / gitweb /
Import release 0.03
[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 MAX_INCLUDE_DEPTH 10
14 YY_BUFFER_STATE include_stack[MAX_INCLUDE_DEPTH];
15 int include_stack_ptr=0;
16
17 uint32_t config_lineno=0;
18 string_t config_file="xxx";
19
20 static struct p_node *leafnode(uint32_t type)
21 {
22         struct p_node *r;
23
24         r=safe_malloc(sizeof(*r),"leafnode");
25         r->type=type;
26         r->loc.file=config_file;
27         r->loc.line=config_lineno;
28         r->l=NULL; r->r=NULL;
29         return r;
30 }
31
32 static struct p_node *keynode(atom_t key)
33 {
34         struct p_node *r;
35         r=leafnode(T_KEY);
36         r->data.key=intern(key);
37         return r;
38 }
39
40 static struct p_node *stringnode(string_t string)
41 {
42         struct p_node *r;
43         r=leafnode(T_STRING);
44         string++;
45         string[strlen(string)-1]=0;
46         r->data.string=safe_strdup(string,"stringnode");
47         return r;
48 }
49
50 static struct p_node *numnode(string_t number)
51 {
52         struct p_node *r;
53         r=leafnode(T_NUMBER);
54         r->data.number=atoi(number);
55         return r;
56 }
57
58 %}
59
60 %%
61 include                 BEGIN(incl);
62 <incl>[ \t]*            /* eat the whitespace */
63 <incl>[^ \t\n]+         { /* got the include filename */
64         if (include_stack_ptr >= MAX_INCLUDE_DEPTH) {
65                 fatal("Configuration file includes nested too deeply");
66         }
67         include_stack[include_stack_ptr++]=
68                 YY_CURRENT_BUFFER;
69         yyin=fopen(yytext,"r");
70         if (!yyin) {
71                 fatal("Can't open included file %s",yytext);
72         }
73         yy_switch_to_buffer(yy_create_buffer(yyin, YY_BUF_SIZE));
74         BEGIN(INITIAL);
75         }
76 <<EOF>>         {
77         if (--include_stack_ptr < 0) {
78                 yyterminate();
79                 }
80         else {
81                 fclose(yyin);
82                 yy_delete_buffer(YY_CURRENT_BUFFER);
83                 yy_switch_to_buffer(include_stack[include_stack_ptr]);
84         }
85         }
86 \"[^\"]*\"              yylval=stringnode(yytext); return TOK_STRING;
87
88 [[:alpha:]_][[:alnum:]\-_]*     yylval=keynode(yytext); return TOK_KEY;
89
90 [[:digit:]]+            yylval=numnode(yytext); return TOK_NUMBER;
91
92         /* Eat comments */
93 \#.*\n                  config_lineno++;
94         /* Count lines */
95 \n                      config_lineno++;
96         /* Eat whitespace */
97 [[:blank:]\j]
98
99         /* Return all unclaimed single characters to the parser */
100 .                       return *yytext;
101