|
| 1 | +%x incl |
| 2 | + |
| 3 | +%{ |
| 4 | +#include <stdio.h> |
| 5 | +#include <stdlib.h> |
| 6 | +#include <string.h> |
| 7 | +#include <strings.h> |
| 8 | +#include <syslog.h> |
| 9 | +#include "cfg_file.h" |
| 10 | +#include "cfg_yacc.h" |
| 11 | + |
| 12 | +#define MAX_INCLUDE_DEPTH 10 |
| 13 | +YY_BUFFER_STATE include_stack[MAX_INCLUDE_DEPTH]; |
| 14 | +int include_stack_ptr = 0; |
| 15 | + |
| 16 | +static size_t |
| 17 | +parse_limit (const char *limit) |
| 18 | +{ |
| 19 | + size_t result = 0; |
| 20 | + char *err_str; |
| 21 | + |
| 22 | + if (!limit || *limit == '\0') return 0; |
| 23 | + |
| 24 | + result = strtoul (limit, &err_str, 10); |
| 25 | + |
| 26 | + if (*err_str != '\0') { |
| 27 | + /* Megabytes */ |
| 28 | + if (*err_str == 'm' || *err_str == 'M') { |
| 29 | + result *= 1048576L; |
| 30 | + } |
| 31 | + /* Kilobytes */ |
| 32 | + else if (*err_str == 'k' || *err_str == 'K') { |
| 33 | + result *= 1024; |
| 34 | + } |
| 35 | + /* Gigabytes */ |
| 36 | + else if (*err_str == 'g' || *err_str == 'G') { |
| 37 | + result *= 1073741824L; |
| 38 | + } |
| 39 | + } |
| 40 | + |
| 41 | + return result; |
| 42 | +} |
| 43 | + |
| 44 | +static unsigned int |
| 45 | +parse_seconds (const char *t) |
| 46 | +{ |
| 47 | + unsigned int result = 0; |
| 48 | + char *err_str; |
| 49 | + |
| 50 | + if (!t || *t == '\0') return 0; |
| 51 | + |
| 52 | + result = strtoul (t, &err_str, 10); |
| 53 | + |
| 54 | + if (*err_str != '\0') { |
| 55 | + /* Seconds */ |
| 56 | + if (*err_str == 's' || *err_str == 'S') { |
| 57 | + result *= 1000; |
| 58 | + } |
| 59 | + } |
| 60 | + |
| 61 | + return result; |
| 62 | +} |
| 63 | + |
| 64 | +static char |
| 65 | +parse_flag (const char *str) |
| 66 | +{ |
| 67 | + if (!str || !*str) return -1; |
| 68 | + |
| 69 | + if ((*str == 'Y' || *str == 'y') && *(str + 1) == '\0') { |
| 70 | + return 1; |
| 71 | + } |
| 72 | + |
| 73 | + if ((*str == 'Y' || *str == 'y') && |
| 74 | + (*(str + 1) == 'E' || *(str + 1) == 'e') && |
| 75 | + (*(str + 2) == 'S' || *(str + 2) == 's') && |
| 76 | + *(str + 3) == '\0') { |
| 77 | + return 1; |
| 78 | + } |
| 79 | + |
| 80 | + if ((*str == 'N' || *str == 'n') && *(str + 1) == '\0') { |
| 81 | + return 0; |
| 82 | + } |
| 83 | + |
| 84 | + if ((*str == 'N' || *str == 'n') && |
| 85 | + (*(str + 1) == 'O' || *(str + 1) == 'o') && |
| 86 | + *(str + 2) == '\0') { |
| 87 | + return 0; |
| 88 | + } |
| 89 | + |
| 90 | + return -1; |
| 91 | +} |
| 92 | + |
| 93 | +%} |
| 94 | + |
| 95 | +%option noyywrap |
| 96 | +%option yylineno |
| 97 | + |
| 98 | +%% |
| 99 | +^[ \t]*#.* /* ignore comments */; |
| 100 | +.include BEGIN(incl); |
| 101 | +tempdir return TEMPDIR; |
| 102 | +pidfile return PIDFILE; |
| 103 | +workers return WORKERS; |
| 104 | +error_time return ERROR_TIME; |
| 105 | +dead_time return DEAD_TIME; |
| 106 | +maxerrors return MAXERRORS; |
| 107 | +reconnect_timeout return RECONNECT_TIMEOUT; |
| 108 | +connect_timeout return CONNECT_TIMEOUT; |
| 109 | +protocol return PROTOCOL; |
| 110 | +memcached return MEMCACHED; |
| 111 | +bind_socket return BINDSOCK; |
| 112 | +servers return SERVERS; |
| 113 | + |
| 114 | +\{ return OBRACE; |
| 115 | +\} return EBRACE; |
| 116 | +; return SEMICOLON; |
| 117 | +, return COMMA; |
| 118 | += return EQSIGN; |
| 119 | +yes|YES|no|NO|[yY]|[nN] yylval.flag=parse_flag(yytext); return FLAG; |
| 120 | +\n /* ignore EOL */; |
| 121 | +[ \t]+ /* ignore whitespace */; |
| 122 | +\"[^"]+\" yylval.string=strdup(yytext + 1); yylval.string[strlen(yylval.string) - 1] = '\0'; return QUOTEDSTRING; |
| 123 | +\" return QUOTE; |
| 124 | +[0-9]+ yylval.number=strtol(yytext, NULL, 10); return NUMBER; |
| 125 | +[0-9]+[kKmMgG]? yylval.limit=parse_limit(yytext); return SIZELIMIT; |
| 126 | +[0-9]+[sS]|[0-9]+[mM][sS] yylval.seconds=parse_seconds(yytext); return SECONDS; |
| 127 | +[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3} yylval.string=strdup(yytext); return IPADDR; |
| 128 | +[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\/[0-9]{1,2} yylval.string=strdup(yytext); return IPNETWORK; |
| 129 | +[a-zA-Z<][a-zA-Z@+>_-]* yylval.string=strdup(yytext); return STRING; |
| 130 | +\/[^/\n]+\/ yylval.string=strdup(yytext); return REGEXP; |
| 131 | +[a-zA-Z0-9].[a-zA-Z0-9\/.-]+ yylval.string=strdup(yytext); return DOMAIN; |
| 132 | +[a-zA-Z0-9.-]+:[0-9]{1,5} yylval.string=strdup(yytext); return HOSTPORT; |
| 133 | +[a-zA-Z0-9\/.-]+ yylval.string=strdup(yytext); return FILENAME; |
| 134 | +<incl>[ \t]* /* eat the whitespace */ |
| 135 | +<incl>[^ \t\n]+ { /* got the include file name */ |
| 136 | + if ( include_stack_ptr >= MAX_INCLUDE_DEPTH ) { |
| 137 | + yyerror ("yylex: includes nested too deeply" ); |
| 138 | + return -1; |
| 139 | + } |
| 140 | + |
| 141 | + include_stack[include_stack_ptr++] = |
| 142 | + YY_CURRENT_BUFFER; |
| 143 | + |
| 144 | + yyin = fopen( yytext, "r" ); |
| 145 | + |
| 146 | + if ( ! yyin ) { |
| 147 | + yyerror("yylex: cannot open include file"); |
| 148 | + return -1; |
| 149 | + } |
| 150 | + |
| 151 | + yy_switch_to_buffer( |
| 152 | + yy_create_buffer( yyin, YY_BUF_SIZE ) ); |
| 153 | + |
| 154 | + BEGIN(INITIAL); |
| 155 | + } |
| 156 | + |
| 157 | +<<EOF>> { |
| 158 | + if ( --include_stack_ptr < 0 ) |
| 159 | + { |
| 160 | + yyterminate(); |
| 161 | + } |
| 162 | + |
| 163 | + else |
| 164 | + { |
| 165 | + yy_delete_buffer( YY_CURRENT_BUFFER ); |
| 166 | + yy_switch_to_buffer( |
| 167 | + include_stack[include_stack_ptr] ); |
| 168 | + } |
| 169 | + } |
| 170 | + |
| 171 | +%% |
| 172 | +/* |
| 173 | + * vi:ts=4 |
| 174 | + */ |
0 commit comments