4 * scanner for cfg files
15 #define COMMENT_LN_S 2
19 static int comment_nest=0;
25 static char* addstr(char*, char**);
31 /* start conditions */
32 %x STRING1 STRING2 COMMENT COMMENT_LN
41 /* condition keywords */
57 LOGSTDERROR log_stderror
63 YES "yes"|"true"|"on"|"enable"
64 NO "no"|"false"|"off"|"disable"
68 ALPHANUM {LETTER}|{DIGIT}
70 ID {LETTER}{ALPHANUM}*
96 <INITIAL>{EAT_ABLE} { count(); }
98 <INITIAL>{FORWARD} {count(); yylval.strval=yytext; return FORWARD; }
99 <INITIAL>{DROP} { count(); yylval.strval=yytext; return DROP; }
100 <INITIAL>{SEND} { count(); yylval.strval=yytext; return SEND; }
101 <INITIAL>{LOG} { count(); yylval.strval=yytext; return LOG; }
102 <INITIAL>{ERROR} { count(); yylval.strval=yytext; return ERROR; }
103 <INITIAL>{ROUTE} { count(); yylval.strval=yytext; return ROUTE; }
105 <INITIAL>{METHOD} { count(); yylval.strval=yytext; return METHOD; }
106 <INITIAL>{URI} { count(); yylval.strval=yytext; return URI; }
107 <INITIAL>{SRCIP} { count(); yylval.strval=yytext; return SRCIP; }
108 <INITIAL>{DSTIP} { count(); yylval.strval=yytext; return DSTIP; }
110 <INITIAL>{DEBUG} { count(); yylval.strval=yytext; return DEBUG; }
111 <INITIAL>{FORK} { count(); yylval.strval=yytext; return FORK; }
112 <INITIAL>{LOGSTDERROR} { yylval.strval=yytext; return LOGSTDERROR; }
113 <INITIAL>{LISTEN} { count(); yylval.strval=yytext; return LISTEN; }
114 <INITIAL>{DNS} { count(); yylval.strval=yytext; return DNS; }
115 <INITIAL>{REV_DNS} { count(); yylval.strval=yytext; return REV_DNS; }
117 <INITIAL>{EQUAL} { count(); return EQUAL; }
118 <INITIAL>{EQUAL_T} { count(); return EQUAL_T; }
119 <INITIAL>{MATCH} { count(); return MATCH; }
120 <INITIAL>{NOT} { count(); return NOT; }
121 <INITIAL>{AND} { count(); return AND; }
122 <INITIAL>{OR} { count(); return OR; }
124 <INITIAL>{NUMBER} { count(); yylval.intval=atoi(yytext);
126 <INITIAL>{YES} { count(); yylval.intval=1; return NUMBER; }
127 <INITIAL>{NO} { count(); yylval.intval=0; return NUMBER; }
129 <INITIAL>{COMMA} { count(); return COMMA; }
130 <INITIAL>{SEMICOLON} { count(); return SEMICOLON; }
131 <INITIAL>{RPAREN} { count(); return RPAREN; }
132 <INITIAL>{LPAREN} { count(); return LPAREN; }
133 <INITIAL>{LBRACE} { count(); return LBRACE; }
134 <INITIAL>{RBRACE} { count(); return RBRACE; }
135 <INITIAL>{LBRACK} { count(); return LBRACK; }
136 <INITIAL>{RBRACK} { count(); return RBRACK; }
137 <INITIAL>{SLASH} { count(); return SLASH; }
138 <INITIAL>{DOT} { count(); return DOT; }
139 <INITIAL>\\{CR} {count(); } /* eat the escaped CR */
140 <INITIAL>{CR} { count(); return CR; }
143 <INITIAL>{QUOTES} { count(); state=STRING_S; BEGIN(STRING1); }
144 <INITIAL>{TICK} { count(); state=STRING_S; BEGIN(STRING2); }
147 <STRING1>{QUOTES} { count(); state=INITIAL_S; BEGIN(INITIAL);
148 yytext[yyleng-1]=0; yyleng--;
149 addstr(yytext, &str);
151 printf("Found string1 <%s>\n", str);
153 printf("WARNING: empty string\n");
155 yylval.strval=str; str=0;
158 <STRING2>{TICK} { count(); state=INITIAL_S; BEGIN(INITIAL);
159 yytext[yyleng-1]=0; yyleng--;
160 printf("Found string1 <%s>\n", yytext);
161 addstr(yytext, &str);
166 <STRING2>.|{EAT_ABLE}|{CR} { yymore(); }
168 <STRING1>\\n { count(); yytext[yyleng-2]='\n';yytext[yyleng-1]=0;
169 yyleng--; addstr(yytext, &str); }
170 <STRING1>\\t { count(); yytext[yyleng-2]='\t';yytext[yyleng-1]=0;
171 yyleng--; addstr(yytext, &str); }
172 <STRING1>\\\\ { count(); yytext[yyleng-2]='\\';yytext[yyleng-1]=0;
173 yyleng--; addstr(yytext, &str); }
174 <STRING1>.|{EAT_ABLE}|{CR} { yymore(); }
177 <INITIAL,COMMENT>{COM_START} { count(); comment_nest++; state=COMMENT_S;
179 <COMMENT>{COM_END} { count(); comment_nest--;
180 if (comment_nest==0){
185 <COMMENT>.|{EAT_ABLE}|{CR} { count(); };
187 <INITIAL>{COM_LINE}.*{CR} { count(); }
189 <INITIAL>{ID} { count(); yylval.strval=yytext; return ID; }
195 printf("Unexpected EOF: closed string\n");
196 if (str) {free(str); str=0;}
199 printf("Unexpected EOF:%d comments open\n", comment_nest);
202 printf("Unexpected EOF: comment line open\n");
210 static char* addstr(char * src, char ** dest)
220 tmp=malloc(len1+len2+1);
221 if (tmp==0) goto error;
222 memcpy(tmp, *dest, len1);
223 memcpy(tmp+len1, src, len2);
230 fprintf(stderr, "lex:addstr: memory allocation error\n");
240 for (i=0; i<yyleng;i++){
241 if (yytext[i]=='\n'){
244 }else if (yytext[i]=='\t'){
245 column+=8 -(column%8);