| 1 | /* the "incl" state is used for picking up the name of an include file */ |
| 2 | %x incl |
| 3 | |
| 4 | %option nounput |
| 5 | %option noinput |
| 6 | %option never-interactive |
| 7 | |
| 8 | %{ |
| 9 | #include <assert.h> |
| 10 | #include <limits.h> |
| 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 | |
| 19 | #define YY_NO_UNPUT |
| 20 | |
| 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 | |
| 29 | #define MAX_INCLUDE_DEPTH 10 |
| 30 | struct include_stack_item { |
| 31 | YY_BUFFER_STATE bst; |
| 32 | uint32_t lineno; |
| 33 | cstring_t file; |
| 34 | }; |
| 35 | struct include_stack_item include_stack[MAX_INCLUDE_DEPTH]; |
| 36 | int include_stack_ptr=0; |
| 37 | |
| 38 | int config_lineno=0; |
| 39 | cstring_t config_file="xxx"; |
| 40 | |
| 41 | static struct p_node *leafnode(uint32_t type) |
| 42 | { |
| 43 | struct p_node *r; |
| 44 | |
| 45 | r=safe_malloc(sizeof(*r),"leafnode"); |
| 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; |
| 74 | r=leafnode(T_NUMBER); |
| 75 | r->data.number=atoi(number); |
| 76 | return r; |
| 77 | } |
| 78 | |
| 79 | %} |
| 80 | |
| 81 | %% |
| 82 | include BEGIN(incl); |
| 83 | <incl>[ \t]* /* eat the whitespace */ |
| 84 | <incl>[^ \t\n]+ { /* got the include filename */ |
| 85 | if (include_stack_ptr >= MAX_INCLUDE_DEPTH) { |
| 86 | fatal("Configuration file includes nested too deeply"); |
| 87 | } |
| 88 | include_stack[include_stack_ptr].bst=YY_CURRENT_BUFFER; |
| 89 | include_stack[include_stack_ptr].lineno=config_lineno; |
| 90 | include_stack[include_stack_ptr].file=config_file; |
| 91 | include_stack_ptr++; |
| 92 | yyin=fopen(yytext,"r"); |
| 93 | if (!yyin) { |
| 94 | fatal("Can't open included file %s",yytext); |
| 95 | } |
| 96 | config_lineno=1; |
| 97 | config_file=safe_strdup(yytext,"conffile.fl/include"); |
| 98 | yy_switch_to_buffer(yy_create_buffer(yyin, YY_BUF_SIZE)); |
| 99 | BEGIN(INITIAL); |
| 100 | } |
| 101 | <incl>\n { /* include with no filename */ |
| 102 | Message(M_FATAL,"config file %s line %d: %s\n",config_file, |
| 103 | config_lineno,"``include'' requires a filename"); |
| 104 | BEGIN(INITIAL); |
| 105 | assert(config_lineno < INT_MAX); |
| 106 | ++config_lineno; |
| 107 | ++yynerrs; |
| 108 | } |
| 109 | |
| 110 | <<EOF>> { |
| 111 | if (--include_stack_ptr < 0) { |
| 112 | yyterminate(); |
| 113 | } |
| 114 | else { |
| 115 | fclose(yyin); |
| 116 | yy_delete_buffer(YY_CURRENT_BUFFER); |
| 117 | yy_switch_to_buffer(include_stack[include_stack_ptr].bst); |
| 118 | config_lineno=include_stack[include_stack_ptr].lineno; |
| 119 | config_file=include_stack[include_stack_ptr].file; |
| 120 | } |
| 121 | } |
| 122 | \"[^\"]*\" yylval=stringnode(yytext); return TOK_STRING; |
| 123 | |
| 124 | [[:alpha:]_][[:alnum:]\-_]* yylval=keynode(yytext); return TOK_KEY; |
| 125 | |
| 126 | [[:digit:]]+ yylval=numnode(yytext); return TOK_NUMBER; |
| 127 | |
| 128 | /* Eat comments */ |
| 129 | \#.*\n config_lineno++; |
| 130 | /* Count lines */ |
| 131 | \n config_lineno++; |
| 132 | /* Eat whitespace */ |
| 133 | [[:blank:]\j] |
| 134 | |
| 135 | /* Return all unclaimed single characters to the parser */ |
| 136 | . return *yytext; |
| 137 | |