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