Commit | Line | Data |
---|---|---|
2fe58dfd SE |
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 | ||
558fa3fb SE |
13 | #define YY_NO_UNPUT |
14 | ||
4f5e39ec SE |
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 | ||
2fe58dfd | 23 | #define MAX_INCLUDE_DEPTH 10 |
974d0468 SE |
24 | struct include_stack_item { |
25 | YY_BUFFER_STATE bst; | |
26 | uint32_t lineno; | |
fe5e9cc4 | 27 | cstring_t file; |
974d0468 SE |
28 | }; |
29 | struct include_stack_item include_stack[MAX_INCLUDE_DEPTH]; | |
2fe58dfd SE |
30 | int include_stack_ptr=0; |
31 | ||
32 | uint32_t config_lineno=0; | |
fe5e9cc4 | 33 | cstring_t config_file="xxx"; |
2fe58dfd SE |
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 | } | |
974d0468 SE |
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++; | |
2fe58dfd SE |
86 | yyin=fopen(yytext,"r"); |
87 | if (!yyin) { | |
88 | fatal("Can't open included file %s",yytext); | |
89 | } | |
974d0468 SE |
90 | config_lineno=1; |
91 | config_file=safe_strdup(yytext,"conffile.fl/include"); | |
2fe58dfd SE |
92 | yy_switch_to_buffer(yy_create_buffer(yyin, YY_BUF_SIZE)); |
93 | BEGIN(INITIAL); | |
94 | } | |
fe5e9cc4 SE |
95 | <incl>\n { /* include with no filename */ |
96 | Message(M_FATAL,"config file %s line %d: %s\n",config_file, | |
97 | config_lineno,"``include'' requires a filename"); | |
98 | BEGIN(INITIAL); | |
99 | ++config_lineno; | |
100 | ++yynerrs; | |
101 | } | |
102 | ||
2fe58dfd SE |
103 | <<EOF>> { |
104 | if (--include_stack_ptr < 0) { | |
105 | yyterminate(); | |
106 | } | |
107 | else { | |
108 | fclose(yyin); | |
109 | yy_delete_buffer(YY_CURRENT_BUFFER); | |
974d0468 SE |
110 | yy_switch_to_buffer(include_stack[include_stack_ptr].bst); |
111 | config_lineno=include_stack[include_stack_ptr].lineno; | |
112 | config_file=include_stack[include_stack_ptr].file; | |
2fe58dfd SE |
113 | } |
114 | } | |
115 | \"[^\"]*\" yylval=stringnode(yytext); return TOK_STRING; | |
116 | ||
117 | [[:alpha:]_][[:alnum:]\-_]* yylval=keynode(yytext); return TOK_KEY; | |
118 | ||
119 | [[:digit:]]+ yylval=numnode(yytext); return TOK_NUMBER; | |
120 | ||
121 | /* Eat comments */ | |
122 | \#.*\n config_lineno++; | |
123 | /* Count lines */ | |
124 | \n config_lineno++; | |
125 | /* Eat whitespace */ | |
126 | [[:blank:]\j] | |
127 | ||
128 | /* Return all unclaimed single characters to the parser */ | |
129 | . return *yytext; | |
130 |