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