Commit | Line | Data |
---|---|---|
2fe58dfd SE |
1 | /* the "incl" state is used for picking up the name of an include file */ |
2 | %x incl | |
3 | ||
b8dc47dd IJ |
4 | %option nounput |
5 | %option noinput | |
f0fb73c5 | 6 | %option never-interactive |
b8dc47dd | 7 | |
2fe58dfd | 8 | %{ |
59230b9b IJ |
9 | #include <assert.h> |
10 | #include <limits.h> | |
2fe58dfd SE |
11 | #include <stdio.h> |
12 | #include <stdlib.h> | |
13 | #include <string.h> | |
14 | ||
15 | #include "conffile_internal.h" | |
16 | #include "conffile.tab.h" | |
17 | #include "util.h" | |
18 | ||
558fa3fb SE |
19 | #define YY_NO_UNPUT |
20 | ||
4f5e39ec SE |
21 | #define YY_INPUT(buf,result,max_size) \ |
22 | do{ \ | |
23 | (result)= fread((buf),1,(max_size),yyin); \ | |
24 | if (ferror(yyin)) \ | |
25 | fatal_perror("Error reading configuration file (%s)", \ | |
26 | config_file); \ | |
27 | }while(0) | |
28 | ||
2fe58dfd | 29 | #define MAX_INCLUDE_DEPTH 10 |
974d0468 SE |
30 | struct include_stack_item { |
31 | YY_BUFFER_STATE bst; | |
3f36eb5f | 32 | int lineno; |
fe5e9cc4 | 33 | cstring_t file; |
974d0468 SE |
34 | }; |
35 | struct include_stack_item include_stack[MAX_INCLUDE_DEPTH]; | |
2fe58dfd SE |
36 | int include_stack_ptr=0; |
37 | ||
1caa23ff | 38 | int config_lineno=0; |
fe5e9cc4 | 39 | cstring_t config_file="xxx"; |
2fe58dfd SE |
40 | |
41 | static struct p_node *leafnode(uint32_t type) | |
42 | { | |
43 | struct p_node *r; | |
44 | ||
b7886fd4 | 45 | NEW(r); |
2fe58dfd SE |
46 | r->type=type; |
47 | r->loc.file=config_file; | |
48 | r->loc.line=config_lineno; | |
49 | r->l=NULL; r->r=NULL; | |
50 | return r; | |
51 | } | |
52 | ||
53 | static struct p_node *keynode(atom_t key) | |
54 | { | |
55 | struct p_node *r; | |
56 | r=leafnode(T_KEY); | |
57 | r->data.key=intern(key); | |
58 | return r; | |
59 | } | |
60 | ||
61 | static struct p_node *stringnode(string_t string) | |
62 | { | |
63 | struct p_node *r; | |
64 | r=leafnode(T_STRING); | |
65 | string++; | |
66 | string[strlen(string)-1]=0; | |
67 | r->data.string=safe_strdup(string,"stringnode"); | |
68 | return r; | |
69 | } | |
70 | ||
71 | static struct p_node *numnode(string_t number) | |
72 | { | |
73 | struct p_node *r; | |
3f36eb5f | 74 | unsigned long n; |
2fe58dfd | 75 | r=leafnode(T_NUMBER); |
3f36eb5f RK |
76 | errno = 0; |
77 | n = strtoul(number, NULL, 10); | |
78 | /* The caller is expected to only give us [0-9]+, | |
79 | * so we skip some of the usual syntax checking. */ | |
80 | r->data.number=n; | |
81 | /* Give a consistent error message for any kind of | |
82 | * out-of-range condition */ | |
83 | if(errno == ERANGE || n != r->data.number) { | |
84 | Message(M_FATAL,"config file %s line %d: '%s' is too big\n", | |
85 | config_file, config_lineno, number); | |
86 | exit(1); | |
87 | } | |
88 | if(errno) { | |
89 | Message(M_FATAL,"config file %s line %d: '%s': %s\n", | |
90 | config_file, config_lineno, number, strerror(errno)); | |
91 | exit(1); | |
92 | } | |
2fe58dfd SE |
93 | return r; |
94 | } | |
95 | ||
96 | %} | |
97 | ||
98 | %% | |
99 | include BEGIN(incl); | |
100 | <incl>[ \t]* /* eat the whitespace */ | |
101 | <incl>[^ \t\n]+ { /* got the include filename */ | |
102 | if (include_stack_ptr >= MAX_INCLUDE_DEPTH) { | |
103 | fatal("Configuration file includes nested too deeply"); | |
104 | } | |
974d0468 SE |
105 | include_stack[include_stack_ptr].bst=YY_CURRENT_BUFFER; |
106 | include_stack[include_stack_ptr].lineno=config_lineno; | |
107 | include_stack[include_stack_ptr].file=config_file; | |
108 | include_stack_ptr++; | |
2fe58dfd SE |
109 | yyin=fopen(yytext,"r"); |
110 | if (!yyin) { | |
111 | fatal("Can't open included file %s",yytext); | |
112 | } | |
974d0468 SE |
113 | config_lineno=1; |
114 | config_file=safe_strdup(yytext,"conffile.fl/include"); | |
2fe58dfd SE |
115 | yy_switch_to_buffer(yy_create_buffer(yyin, YY_BUF_SIZE)); |
116 | BEGIN(INITIAL); | |
117 | } | |
fe5e9cc4 SE |
118 | <incl>\n { /* include with no filename */ |
119 | Message(M_FATAL,"config file %s line %d: %s\n",config_file, | |
120 | config_lineno,"``include'' requires a filename"); | |
121 | BEGIN(INITIAL); | |
59230b9b | 122 | assert(config_lineno < INT_MAX); |
fe5e9cc4 SE |
123 | ++config_lineno; |
124 | ++yynerrs; | |
125 | } | |
126 | ||
2fe58dfd SE |
127 | <<EOF>> { |
128 | if (--include_stack_ptr < 0) { | |
129 | yyterminate(); | |
130 | } | |
131 | else { | |
132 | fclose(yyin); | |
133 | yy_delete_buffer(YY_CURRENT_BUFFER); | |
974d0468 SE |
134 | yy_switch_to_buffer(include_stack[include_stack_ptr].bst); |
135 | config_lineno=include_stack[include_stack_ptr].lineno; | |
136 | config_file=include_stack[include_stack_ptr].file; | |
2fe58dfd SE |
137 | } |
138 | } | |
139 | \"[^\"]*\" yylval=stringnode(yytext); return TOK_STRING; | |
140 | ||
141 | [[:alpha:]_][[:alnum:]\-_]* yylval=keynode(yytext); return TOK_KEY; | |
142 | ||
143 | [[:digit:]]+ yylval=numnode(yytext); return TOK_NUMBER; | |
144 | ||
145 | /* Eat comments */ | |
146 | \#.*\n config_lineno++; | |
147 | /* Count lines */ | |
148 | \n config_lineno++; | |
149 | /* Eat whitespace */ | |
150 | [[:blank:]\j] | |
151 | ||
152 | /* Return all unclaimed single characters to the parser */ | |
153 | . return *yytext; | |
154 |