1 /* the "incl" state is used for picking up the name of an include file */
6 %option never-interactive
15 #include "conffile_internal.h"
16 #include "conffile.tab.h"
21 #define YY_INPUT(buf,result,max_size) \
23 (result)= fread((buf),1,(max_size),yyin); \
25 fatal_perror("Error reading configuration file (%s)", \
29 #define MAX_INCLUDE_DEPTH 10
30 struct include_stack_item {
35 struct include_stack_item include_stack[MAX_INCLUDE_DEPTH];
36 int include_stack_ptr=0;
39 cstring_t config_file="xxx";
41 static struct p_node *leafnode(uint32_t type)
45 r=safe_malloc(sizeof(*r),"leafnode");
47 r->loc.file=config_file;
48 r->loc.line=config_lineno;
53 static struct p_node *keynode(atom_t key)
57 r->data.key=intern(key);
61 static struct p_node *stringnode(string_t string)
66 string[strlen(string)-1]=0;
67 r->data.string=safe_strdup(string,"stringnode");
71 static struct p_node *numnode(string_t number)
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. */
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);
89 Message(M_FATAL,"config file %s line %d: '%s': %s\n",
90 config_file, config_lineno, number, strerror(errno));
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");
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;
109 yyin=fopen(yytext,"r");
111 fatal("Can't open included file %s",yytext);
114 config_file=safe_strdup(yytext,"conffile.fl/include");
115 yy_switch_to_buffer(yy_create_buffer(yyin, YY_BUF_SIZE));
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");
122 assert(config_lineno < INT_MAX);
128 if (--include_stack_ptr < 0) {
133 yy_delete_buffer(YY_CURRENT_BUFFER);
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;
139 \"[^\"]*\" yylval=stringnode(yytext); return TOK_STRING;
141 [[:alpha:]_][[:alnum:]\-_]* yylval=keynode(yytext); return TOK_KEY;
143 [[:digit:]]+ yylval=numnode(yytext); return TOK_NUMBER;
146 \#.*\n config_lineno++;
152 /* Return all unclaimed single characters to the parser */