- started to build the expresion & acion tree/list
[sip-router] / cfg.lex
1 /*
2  * $Id$
3  *
4  * scanner for cfg files
5  */
6
7
8 %{
9         #include "cfg.tab.h"
10         #include <string.h>
11
12         /* states */
13         #define INITIAL_S               0
14         #define COMMENT_S               1
15         #define COMMENT_LN_S    2
16         #define STRING_S                3
17
18         
19         static int comment_nest=0;
20         static int state=0;
21         static char* str=0;
22         int line=0;
23         int column=0;
24
25         static char* addstr(char*, char**);
26         static void count();
27
28
29 %}
30
31 /* start conditions */
32 %x STRING1 STRING2 COMMENT COMMENT_LN
33
34 /* action keywords */
35 FORWARD forward
36 DROP    drop
37 SEND    send
38 LOG             log
39 ERROR   error
40 ROUTE   route
41 /* condition keywords */
42 METHOD  method
43 URI             uri
44 SRCIP   src_ip
45 DSTIP   dst_ip
46 /* operators */
47 EQUAL   =
48 EQUAL_T ==
49 MATCH   ~=
50 NOT             !
51 AND             "and"|"&&"|"&"
52 OR              "or"|"||"|"|"
53
54 /* config vars. */
55 DEBUG   debug
56 FORK    fork
57 LOGSTDERROR     log_stderror
58 LISTEN          listen
59 DNS              dns
60 REV_DNS  rev_dns
61
62 /* values */
63 YES                     "yes"|"true"|"on"|"enable"
64 NO                      "no"|"false"|"off"|"disable"
65
66 LETTER          [a-zA-Z]
67 DIGIT           [0-9]
68 ALPHANUM        {LETTER}|{DIGIT}
69 NUMBER          {DIGIT}+
70 ID                      {LETTER}{ALPHANUM}*
71 QUOTES          \"
72 TICK            \'
73 SLASH           "/"
74 SEMICOLON       ;
75 RPAREN          \)
76 LPAREN          \(
77 LBRACE          \{
78 RBRACE          \}
79 LBRACK          \[
80 RBRACK          \]
81 COMMA           ,
82 DOT                     \.
83 CR                      \n
84
85
86
87 COM_LINE        #
88 COM_START       "/\*"
89 COM_END         "\*/"
90
91 EAT_ABLE        [\ \t\b\r]
92
93 %%
94
95
96 <INITIAL>{EAT_ABLE}     { count(); }
97
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; }
104
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; }
109
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; }
116
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;  }
123
124 <INITIAL>{NUMBER}               { count(); yylval.intval=atoi(yytext);
125                                                         return NUMBER; }
126 <INITIAL>{YES}                  { count(); yylval.intval=1; return NUMBER; }
127 <INITIAL>{NO}                   { count(); yylval.intval=0; return NUMBER; }
128
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; }
141
142
143 <INITIAL>{QUOTES} { count(); state=STRING_S; BEGIN(STRING1); }
144 <INITIAL>{TICK} { count(); state=STRING_S; BEGIN(STRING2); }
145
146
147 <STRING1>{QUOTES} { count(); state=INITIAL_S; BEGIN(INITIAL); 
148                                                 yytext[yyleng-1]=0; yyleng--;
149                                                 addstr(yytext, &str);
150                                                 if (str){
151                                                         printf("Found string1 <%s>\n", str);
152                                                 }else{
153                                                         printf("WARNING: empty string\n");
154                                                 }
155                                                 yylval.strval=str; str=0;
156                                                 return STRING;
157                                         }
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);
162                                                 yylval.strval=str;
163                                                 str=0;
164                                                 return STRING;
165                                         }
166 <STRING2>.|{EAT_ABLE}|{CR}      { yymore(); }
167
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(); }
175
176
177 <INITIAL,COMMENT>{COM_START}    { count(); comment_nest++; state=COMMENT_S;
178                                                                                 BEGIN(COMMENT); }
179 <COMMENT>{COM_END}                              { count(); comment_nest--;
180                                                                                 if (comment_nest==0){
181                                                                                         state=INITIAL_S;
182                                                                                         BEGIN(INITIAL);
183                                                                                 }
184                                                                 }
185 <COMMENT>.|{EAT_ABLE}|{CR}                              { count(); };
186
187 <INITIAL>{COM_LINE}.*{CR}       { count(); } 
188
189 <INITIAL>{ID}                   { count(); yylval.strval=yytext; return ID; }
190
191
192 <<EOF>>                                                 {
193                                                                         switch(state){
194                                                                                 case STRING_S: 
195                                                                                         printf("Unexpected EOF: closed string\n");
196                                                                                         if (str) {free(str); str=0;}
197                                                                                         break;
198                                                                                 case COMMENT_S:
199                                                                                         printf("Unexpected EOF:%d comments open\n", comment_nest);
200                                                                                         break;
201                                                                                 case COMMENT_LN_S:
202                                                                                         printf("Unexpected EOF: comment line open\n");
203                                                                                         break;
204                                                                         }
205                                                                         return 0;
206                                                                 }
207                         
208 %%
209
210 static char* addstr(char * src, char ** dest)
211 {
212         char *tmp;
213         int len1, len2;
214         
215         if (*dest==0){
216                 *dest=strdup(src);
217         }else{
218                 len1=strlen(*dest);
219                 len2=strlen(src);
220                 tmp=malloc(len1+len2+1);
221                 if (tmp==0) goto error;
222                 memcpy(tmp, *dest, len1);
223                 memcpy(tmp+len1, src, len2);
224                 tmp[len1+len2]=0;
225                 free(*dest);
226                 *dest=tmp;
227         }
228         return *dest;
229 error:
230         fprintf(stderr, "lex:addstr: memory allocation error\n");
231         return 0;
232 }
233
234
235
236 static void count()
237 {
238         int i;
239         
240         for (i=0; i<yyleng;i++){
241                 if (yytext[i]=='\n'){
242                         line++;
243                         column=0;
244                 }else if (yytext[i]=='\t'){
245                         column+=8 -(column%8);
246                 }else{
247                         column++;
248                 }
249         }
250 }
251
252