New script element, Jan's select function framework (@xxx), extended for
[sip-router] / cfg.lex
1 /*
2  * $Id$
3  *
4  * scanner for cfg files
5  *
6  * Copyright (C) 2001-2003 FhG Fokus
7  *
8  * This file is part of ser, a free SIP server.
9  *
10  * ser is free software; you can redistribute it and/or modify
11  * it under the terms of the GNU General Public License as published by
12  * the Free Software Foundation; either version 2 of the License, or
13  * (at your option) any later version
14  *
15  * For a license to use the ser software under conditions
16  * other than those described here, or to purchase support for this
17  * software, please contact iptel.org by e-mail at the following addresses:
18  *    info@iptel.org
19  *
20  * ser is distributed in the hope that it will be useful,
21  * but WITHOUT ANY WARRANTY; without even the implied warranty of
22  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
23  * GNU General Public License for more details.
24  *
25  * You should have received a copy of the GNU General Public License 
26  * along with this program; if not, write to the Free Software 
27  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
28  *
29  * History:
30  * -------
31  *  2003-01-29  src_port added (jiri)
32  *  2003-01-23  mhomed added (jiri)
33  *  2003-03-19  replaced all the mallocs/frees w/ pkg_malloc/pkg_free (andrei)
34  *  2003-04-01  added dst_port, proto (tcp, udp, tls), af(inet, inet6) (andrei)
35  *  2003-04-05  s/reply_route/failure_route, onreply_route introduced (jiri)
36  *  2003-04-12  added force_rport, chdir and wdir (andrei)
37  *  2003-04-22  strip_tail added (jiri)
38  *  2003-07-03  tls* (disable, certificate, private_key, ca_list, verify, 
39  *               require_certificate added (andrei)
40  *  2003-07-06  more tls config. vars added: tls_method, tls_port_no (andrei)
41  *  2003-10-02  added {,set_}advertised_{address,port} (andrei)
42  *  2003-10-07  added hex and octal numbers support (andrei)
43  *  2003-10-10  replaced len_gt w/ msg:len (andrei)
44  *  2003-10-13  added fifo_dir (andrei)
45  *  2003-10-28  added tcp_accept_aliases (andrei)
46  *  2003-11-29  added {tcp_send, tcp_connect, tls_*}_timeout (andrei)
47  *  2004-03-30  added DISABLE_CORE and OPEN_FD_LIMIT (andrei)
48  *  2004-04-28  added sock_mode (replaces fifo_mode), sock_user &
49  *               sock_group  (andrei)
50  *  2004-05-03  applied multicast support patch from janakj
51  *              added MCAST_TTL (andrei)
52  *  2004-10-08  more escapes: \", \xHH, \nnn and minor optimizations (andrei)
53  *  2004-10-19  added FROM_URI and TO_URI (andrei)
54  *  2004-11-30  added force_send_socket
55  *  2005-07-08  added tcp_connection_lifetime, tcp_poll_method,
56  *               tcp_max_connections (andrei)
57  *  2005-07-11  added dns_retr_{time,no}, dns_servers_no, dns_use_search_list,
58  *              dns_try_ipv6 (andrei)
59  *  2005-12-11  added onsend_route, snd_{ip,port,proto,af},
60  *              to_{ip,port} (andrei)
61  *  2005-12-12  separated drop, exit, break, return, added RETCODE (andrei)
62  */
63
64
65 %{
66         #include "dprint.h"
67         #include "globals.h"
68         #include "mem/mem.h"
69         #include <string.h>
70         #include <stdlib.h>
71         #include "ip_addr.h"
72         #include "usr_avp.h"
73         #include "select.h"
74         #include "cfg.tab.h"
75
76         /* states */
77         #define INITIAL_S               0
78         #define COMMENT_S               1
79         #define COMMENT_LN_S            2
80         #define STRING_S                3
81         #define ATTR_S                  4
82         #define SELECT_S                5
83
84         #define STR_BUF_ALLOC_UNIT      128
85         struct str_buf{
86                 char* s;
87                 char* crt;
88                 int left;
89         };
90
91         
92         static int comment_nest=0;
93         static int state=0;
94         static struct str_buf s_buf;
95         int line=1;
96         int column=1;
97         int startcolumn=1;
98
99         static char* addchar(struct str_buf *, char);
100         static char* addstr(struct str_buf *, char*, int);
101         static void count();
102
103
104 %}
105
106 /* start conditions */
107 %x STRING1 STRING2 COMMENT COMMENT_LN ATTR SELECT
108
109 /* action keywords */
110 FORWARD forward
111 FORWARD_TCP     forward_tcp
112 FORWARD_UDP     forward_udp
113 FORWARD_TLS     forward_tls
114 DROP    "drop"|"exit"
115 RETURN  "return"
116 BREAK   "break"
117 SEND    send
118 SEND_TCP        send_tcp
119 LOG             log
120 ERROR   error
121 ROUTE   route
122 ROUTE_FAILURE failure_route
123 ROUTE_ONREPLY onreply_route
124 ROUTE_BRANCH branch_route
125 ROUTE_SEND onsend_route
126 EXEC    exec
127 FORCE_RPORT             "force_rport"|"add_rport"
128 FORCE_TCP_ALIAS         "force_tcp_alias"|"add_tcp_alias"
129 SETFLAG         setflag
130 RESETFLAG       resetflag
131 ISFLAGSET       isflagset
132 SET_HOST                "rewritehost"|"sethost"|"seth"
133 SET_HOSTPORT    "rewritehostport"|"sethostport"|"sethp"
134 SET_USER                "rewriteuser"|"setuser"|"setu"
135 SET_USERPASS    "rewriteuserpass"|"setuserpass"|"setup"
136 SET_PORT                "rewriteport"|"setport"|"setp"
137 SET_URI                 "rewriteuri"|"seturi"
138 REVERT_URI              "revert_uri"
139 PREFIX                  "prefix"
140 STRIP                   "strip"
141 STRIP_TAIL              "strip_tail"
142 APPEND_BRANCH   "append_branch"
143 IF                              "if"
144 ELSE                    "else"
145 SET_ADV_ADDRESS "set_advertised_address"
146 SET_ADV_PORT    "set_advertised_port"
147 FORCE_SEND_SOCKET       "force_send_socket"
148
149 /*ACTION LVALUES*/
150 URIHOST                 "uri:host"
151 URIPORT                 "uri:port"
152
153 MAX_LEN                 "max_len"
154
155
156 /* condition keywords */
157 METHOD  method
158 /* hack -- the second element in first line is referable
159    as either uri or status; it only would makes sense to
160    call it "uri" from route{} and status from onreply_route{}
161 */
162 URI             "uri"|"status"
163 FROM_URI        "from_uri"
164 TO_URI          "to_uri"
165 SRCIP   src_ip
166 SRCPORT src_port
167 DSTIP   dst_ip
168 DSTPORT dst_port
169 SNDIP   snd_ip
170 SNDPORT snd_port
171 SNDPROTO        snd_proto|to_proto
172 SNDAF           snd_af|to_af
173 TOIP    to_ip
174 TOPORT  to_port
175 PROTO   proto
176 AF              af
177 MYSELF  myself
178 MSGLEN                  "msg:len"
179 RETCODE \$\?|\$retcode
180 /* operators */
181 EQUAL   =
182 EQUAL_T ==
183 GT      >
184 LT      <
185 GTE     >=
186 LTE     <=
187 DIFF    !=
188 MATCH   =~
189 ADDEQ     "+="
190 NOT             !|"not"
191 LOG_AND         "and"|"&&"
192 BIN_AND         "&"
193 LOG_OR          "or"|"||"
194 BIN_OR          "|"
195 PLUS    "+"
196 MINUS   "-"
197
198 /* Attribute specification */
199 ATTR_MARK   "$"|"%"
200 SELECT_MARK  "@"
201 ATTR_FROM   "from"|"f"
202 ATTR_TO     "to"|"t"
203 ATTR_USER   "user"|"u"
204 ATTR_DOMAIN "domain"|"d"
205 ATTR_GLOBAL "global"|"g"
206
207 /* config vars. */
208 DEBUG   debug
209 FORK    fork
210 LOGSTDERROR     log_stderror
211 LOGFACILITY     log_facility
212 LISTEN          listen
213 ALIAS           alias
214 DNS              dns
215 REV_DNS  rev_dns
216 DNS_TRY_IPV6    dns_try_ipv6
217 DNS_RETR_TIME   dns_retr_time
218 DNS_RETR_NO             dns_retr_no
219 DNS_SERVERS_NO  dns_servers_no
220 DNS_USE_SEARCH  dns_use_search_list
221 PORT    port
222 STAT    statistics
223 MAXBUFFER maxbuffer
224 CHILDREN children
225 CHECK_VIA       check_via
226 SYN_BRANCH syn_branch
227 MEMLOG          "memlog"|"mem_log"
228 SIP_WARNING sip_warning
229 FIFO fifo
230 FIFO_DIR  fifo_dir
231 SOCK_MODE "fifo_mode"|"sock_mode"|"file_mode"
232 SOCK_USER "fifo_user"|"sock_user"
233 SOCK_GROUP "fifo_group"|"sock_group"
234 FIFO_DB_URL fifo_db_url
235 UNIX_SOCK unix_sock
236 UNIX_SOCK_CHILDREN unix_sock_children
237 UNIX_TX_TIMEOUT unix_tx_timeout
238 SERVER_SIGNATURE server_signature
239 REPLY_TO_VIA reply_to_via
240 USER            "user"|"uid"
241 GROUP           "group"|"gid"
242 CHROOT          "chroot"
243 WDIR            "workdir"|"wdir"
244 MHOMED          mhomed
245 DISABLE_TCP             "disable_tcp"
246 TCP_CHILDREN    "tcp_children"
247 TCP_ACCEPT_ALIASES      "tcp_accept_aliases"
248 TCP_SEND_TIMEOUT        "tcp_send_timeout"
249 TCP_CONNECT_TIMEOUT     "tcp_connect_timeout"
250 TCP_CON_LIFETIME        "tcp_connection_lifetime"
251 TCP_POLL_METHOD         "tcp_poll_method"
252 TCP_MAX_CONNECTIONS     "tcp_max_connections"
253 DISABLE_TLS             "disable_tls"
254 TLSLOG                  "tlslog"|"tls_log"
255 TLS_PORT_NO             "tls_port_no"
256 TLS_METHOD              "tls_method"
257 TLS_VERIFY              "tls_verify"
258 TLS_REQUIRE_CERTIFICATE "tls_require_certificate"
259 TLS_CERTIFICATE "tls_certificate"
260 TLS_PRIVATE_KEY "tls_private_key"
261 TLS_CA_LIST             "tls_ca_list"
262 TLS_HANDSHAKE_TIMEOUT   "tls_handshake_timeout"
263 TLS_SEND_TIMEOUT        "tls_send_timeout"
264 ADVERTISED_ADDRESS      "advertised_address"
265 ADVERTISED_PORT         "advertised_port"
266 DISABLE_CORE            "disable_core_dump"
267 OPEN_FD_LIMIT           "open_files_limit"
268 MCAST_LOOPBACK          "mcast_loopback"
269 MCAST_TTL               "mcast_ttl"
270 TOS                     "tos"
271
272 LOADMODULE      loadmodule
273 MODPARAM        modparam
274
275 /* values */
276 YES                     "yes"|"true"|"on"|"enable"
277 NO                      "no"|"false"|"off"|"disable"
278 UDP                     "udp"|"UDP"
279 TCP                     "tcp"|"TCP"
280 TLS                     "tls"|"TLS"
281 INET            "inet"|"INET"
282 INET6           "inet6"|"INET6"
283 SSLv23                  "sslv23"|"SSLv23"|"SSLV23"
284 SSLv2                   "sslv2"|"SSLv2"|"SSLV2"
285 SSLv3                   "sslv3"|"SSLv3"|"SSLV3"
286 TLSv1                   "tlsv1"|"TLSv1"|"TLSV1"
287
288 LETTER          [a-zA-Z]
289 DIGIT           [0-9]
290 ALPHANUM        {LETTER}|{DIGIT}|[_]
291 ID                      {LETTER}{ALPHANUM}*
292 HEX                     [0-9a-fA-F]
293 HEXNUMBER       0x{HEX}+
294 OCTNUMBER       0[0-7]+
295 DECNUMBER       0|-?([1-9]{DIGIT}*)
296 BINNUMBER       [0-1]+b
297 HEX4            {HEX}{1,4}
298 IPV6ADDR        ({HEX4}":"){7}{HEX4}|({HEX4}":"){1,7}(":"{HEX4}){1,7}|":"(":"{HEX4}){1,7}|({HEX4}":"){1,7}":"|"::"
299 QUOTES          \"
300 TICK            \'
301 SLASH           "/"
302 SEMICOLON       ;
303 RPAREN          \)
304 LPAREN          \(
305 LBRACE          \{
306 RBRACE          \}
307 LBRACK          \[
308 RBRACK          \]
309 COMMA           ","
310 COLON           ":"
311 STAR            \*
312 DOT                     \.
313 CR                      \n
314
315
316
317 COM_LINE        #
318 COM_START       "/\*"
319 COM_END         "\*/"
320
321 EAT_ABLE        [\ \t\b\r]
322
323 %%
324
325
326 <INITIAL>{EAT_ABLE}     { count(); }
327
328 <INITIAL>{FORWARD}      {count(); yylval.strval=yytext; return FORWARD; }
329 <INITIAL>{FORWARD_TCP}  {count(); yylval.strval=yytext; return FORWARD_TCP; }
330 <INITIAL>{FORWARD_TLS}  {count(); yylval.strval=yytext; return FORWARD_TLS; }
331 <INITIAL>{FORWARD_UDP}  {count(); yylval.strval=yytext; return FORWARD_UDP; }
332 <INITIAL>{DROP} { count(); yylval.strval=yytext; return DROP; }
333 <INITIAL>{RETURN}       { count(); yylval.strval=yytext; return RETURN; }
334 <INITIAL>{BREAK}        { count(); yylval.strval=yytext; return BREAK; }
335 <INITIAL>{SEND} { count(); yylval.strval=yytext; return SEND; }
336 <INITIAL>{SEND_TCP}     { count(); yylval.strval=yytext; return SEND_TCP; }
337 <INITIAL>{LOG}  { count(); yylval.strval=yytext; return LOG_TOK; }
338 <INITIAL>{ERROR}        { count(); yylval.strval=yytext; return ERROR; }
339 <INITIAL>{SETFLAG}      { count(); yylval.strval=yytext; return SETFLAG; }
340 <INITIAL>{RESETFLAG}    { count(); yylval.strval=yytext; return RESETFLAG; }
341 <INITIAL>{ISFLAGSET}    { count(); yylval.strval=yytext; return ISFLAGSET; }
342 <INITIAL>{MSGLEN}       { count(); yylval.strval=yytext; return MSGLEN; }
343 <INITIAL>{RETCODE}      { count(); yylval.strval=yytext; return RETCODE; }
344 <INITIAL>{ROUTE}        { count(); yylval.strval=yytext; return ROUTE; }
345 <INITIAL>{ROUTE_ONREPLY}        { count(); yylval.strval=yytext;
346                                                                 return ROUTE_ONREPLY; }
347 <INITIAL>{ROUTE_FAILURE}        { count(); yylval.strval=yytext;
348                                                                 return ROUTE_FAILURE; }
349 <INITIAL>{ROUTE_BRANCH} { count(); yylval.strval=yytext; return ROUTE_BRANCH; }
350 <INITIAL>{ROUTE_SEND} { count(); yylval.strval=yytext; return ROUTE_SEND; }
351 <INITIAL>{EXEC} { count(); yylval.strval=yytext; return EXEC; }
352 <INITIAL>{SET_HOST}     { count(); yylval.strval=yytext; return SET_HOST; }
353 <INITIAL>{SET_HOSTPORT} { count(); yylval.strval=yytext; return SET_HOSTPORT; }
354 <INITIAL>{SET_USER}     { count(); yylval.strval=yytext; return SET_USER; }
355 <INITIAL>{SET_USERPASS} { count(); yylval.strval=yytext; return SET_USERPASS; }
356 <INITIAL>{SET_PORT}     { count(); yylval.strval=yytext; return SET_PORT; }
357 <INITIAL>{SET_URI}      { count(); yylval.strval=yytext; return SET_URI; }
358 <INITIAL>{REVERT_URI}   { count(); yylval.strval=yytext; return REVERT_URI; }
359 <INITIAL>{PREFIX}       { count(); yylval.strval=yytext; return PREFIX; }
360 <INITIAL>{STRIP}        { count(); yylval.strval=yytext; return STRIP; }
361 <INITIAL>{STRIP_TAIL}   { count(); yylval.strval=yytext; return STRIP_TAIL; }
362 <INITIAL>{APPEND_BRANCH}        { count(); yylval.strval=yytext; 
363                                                                 return APPEND_BRANCH; }
364 <INITIAL>{FORCE_RPORT}  { count(); yylval.strval=yytext; return FORCE_RPORT; }
365 <INITIAL>{FORCE_TCP_ALIAS}      { count(); yylval.strval=yytext;
366                                                                 return FORCE_TCP_ALIAS; }
367 <INITIAL>{IF}   { count(); yylval.strval=yytext; return IF; }
368 <INITIAL>{ELSE} { count(); yylval.strval=yytext; return ELSE; }
369
370 <INITIAL>{SET_ADV_ADDRESS}      { count(); yylval.strval=yytext;
371                                                                                 return SET_ADV_ADDRESS; }
372 <INITIAL>{SET_ADV_PORT} { count(); yylval.strval=yytext;
373                                                                                 return SET_ADV_PORT; }
374 <INITIAL>{FORCE_SEND_SOCKET}    {       count(); yylval.strval=yytext;
375                                                                         return FORCE_SEND_SOCKET; }
376
377 <INITIAL>{URIHOST}      { count(); yylval.strval=yytext; return URIHOST; }
378 <INITIAL>{URIPORT}      { count(); yylval.strval=yytext; return URIPORT; }
379
380 <INITIAL>{MAX_LEN}      { count(); yylval.strval=yytext; return MAX_LEN; }
381
382 <INITIAL>{METHOD}       { count(); yylval.strval=yytext; return METHOD; }
383 <INITIAL>{URI}  { count(); yylval.strval=yytext; return URI; }
384 <INITIAL>{FROM_URI}     { count(); yylval.strval=yytext; return FROM_URI; }
385 <INITIAL>{TO_URI}       { count(); yylval.strval=yytext; return TO_URI; }
386 <INITIAL>{SRCIP}        { count(); yylval.strval=yytext; return SRCIP; }
387 <INITIAL>{SRCPORT}      { count(); yylval.strval=yytext; return SRCPORT; }
388 <INITIAL>{DSTIP}        { count(); yylval.strval=yytext; return DSTIP; }
389 <INITIAL>{DSTPORT}      { count(); yylval.strval=yytext; return DSTPORT; }
390 <INITIAL>{SNDIP}        { count(); yylval.strval=yytext; return SNDIP; }
391 <INITIAL>{SNDPORT}      { count(); yylval.strval=yytext; return SNDPORT; }
392 <INITIAL>{SNDPROTO}     { count(); yylval.strval=yytext; return SNDPROTO; }
393 <INITIAL>{SNDAF}        { count(); yylval.strval=yytext; return SNDAF; }
394 <INITIAL>{TOIP}         { count(); yylval.strval=yytext; return TOIP; }
395 <INITIAL>{TOPORT}       { count(); yylval.strval=yytext; return TOPORT; }
396 <INITIAL>{PROTO}        { count(); yylval.strval=yytext; return PROTO; }
397 <INITIAL>{AF}   { count(); yylval.strval=yytext; return AF; }
398 <INITIAL>{MYSELF}       { count(); yylval.strval=yytext; return MYSELF; }
399
400 <INITIAL>{DEBUG}        { count(); yylval.strval=yytext; return DEBUG_V; }
401 <INITIAL>{FORK}         { count(); yylval.strval=yytext; return FORK; }
402 <INITIAL>{LOGSTDERROR}  { yylval.strval=yytext; return LOGSTDERROR; }
403 <INITIAL>{LOGFACILITY}  { yylval.strval=yytext; return LOGFACILITY; }
404 <INITIAL>{LISTEN}       { count(); yylval.strval=yytext; return LISTEN; }
405 <INITIAL>{ALIAS}        { count(); yylval.strval=yytext; return ALIAS; }
406 <INITIAL>{DNS}  { count(); yylval.strval=yytext; return DNS; }
407 <INITIAL>{REV_DNS}      { count(); yylval.strval=yytext; return REV_DNS; }
408 <INITIAL>{DNS_TRY_IPV6} { count(); yylval.strval=yytext;
409                                                                 return DNS_TRY_IPV6; }
410 <INITIAL>{DNS_RETR_TIME}        { count(); yylval.strval=yytext;
411                                                                 return DNS_RETR_TIME; }
412 <INITIAL>{DNS_RETR_NO}  { count(); yylval.strval=yytext;
413                                                                 return DNS_RETR_NO; }
414 <INITIAL>{DNS_SERVERS_NO}       { count(); yylval.strval=yytext;
415                                                                 return DNS_SERVERS_NO; }
416 <INITIAL>{DNS_USE_SEARCH}       { count(); yylval.strval=yytext;
417                                                                 return DNS_USE_SEARCH; }
418 <INITIAL>{PORT} { count(); yylval.strval=yytext; return PORT; }
419 <INITIAL>{STAT} { count(); yylval.strval=yytext; return STAT; }
420 <INITIAL>{MAXBUFFER}    { count(); yylval.strval=yytext; return MAXBUFFER; }
421 <INITIAL>{CHILDREN}     { count(); yylval.strval=yytext; return CHILDREN; }
422 <INITIAL>{CHECK_VIA}    { count(); yylval.strval=yytext; return CHECK_VIA; }
423 <INITIAL>{SYN_BRANCH}   { count(); yylval.strval=yytext; return SYN_BRANCH; }
424 <INITIAL>{MEMLOG}       { count(); yylval.strval=yytext; return MEMLOG; }
425 <INITIAL>{SIP_WARNING}  { count(); yylval.strval=yytext; return SIP_WARNING; }
426 <INITIAL>{USER}         { count(); yylval.strval=yytext; return USER; }
427 <INITIAL>{GROUP}        { count(); yylval.strval=yytext; return GROUP; }
428 <INITIAL>{CHROOT}       { count(); yylval.strval=yytext; return CHROOT; }
429 <INITIAL>{WDIR} { count(); yylval.strval=yytext; return WDIR; }
430 <INITIAL>{MHOMED}       { count(); yylval.strval=yytext; return MHOMED; }
431 <INITIAL>{DISABLE_TCP}  { count(); yylval.strval=yytext; return DISABLE_TCP; }
432 <INITIAL>{TCP_CHILDREN} { count(); yylval.strval=yytext; return TCP_CHILDREN; }
433 <INITIAL>{TCP_ACCEPT_ALIASES}   { count(); yylval.strval=yytext;
434                                                                         return TCP_ACCEPT_ALIASES; }
435 <INITIAL>{TCP_SEND_TIMEOUT}             { count(); yylval.strval=yytext;
436                                                                         return TCP_SEND_TIMEOUT; }
437 <INITIAL>{TCP_CONNECT_TIMEOUT}          { count(); yylval.strval=yytext;
438                                                                         return TCP_CONNECT_TIMEOUT; }
439 <INITIAL>{TCP_CON_LIFETIME}             { count(); yylval.strval=yytext;
440                                                                         return TCP_CON_LIFETIME; }
441 <INITIAL>{TCP_POLL_METHOD}              { count(); yylval.strval=yytext;
442                                                                         return TCP_POLL_METHOD; }
443 <INITIAL>{TCP_MAX_CONNECTIONS}  { count(); yylval.strval=yytext;
444                                                                         return TCP_MAX_CONNECTIONS; }
445 <INITIAL>{DISABLE_TLS}  { count(); yylval.strval=yytext; return DISABLE_TLS; }
446 <INITIAL>{TLSLOG}               { count(); yylval.strval=yytext; return TLS_PORT_NO; }
447 <INITIAL>{TLS_PORT_NO}  { count(); yylval.strval=yytext; return TLS_PORT_NO; }
448 <INITIAL>{TLS_METHOD}   { count(); yylval.strval=yytext; return TLS_METHOD; }
449 <INITIAL>{TLS_VERIFY}   { count(); yylval.strval=yytext; return TLS_VERIFY; }
450 <INITIAL>{TLS_REQUIRE_CERTIFICATE}      { count(); yylval.strval=yytext;
451                                                                                 return TLS_REQUIRE_CERTIFICATE; }
452 <INITIAL>{TLS_CERTIFICATE}      { count(); yylval.strval=yytext; 
453                                                                                 return TLS_CERTIFICATE; }
454 <INITIAL>{TLS_PRIVATE_KEY}      { count(); yylval.strval=yytext; 
455                                                                                 return TLS_PRIVATE_KEY; }
456 <INITIAL>{TLS_CA_LIST}  { count(); yylval.strval=yytext; 
457                                                                                 return TLS_CA_LIST; }
458 <INITIAL>{TLS_HANDSHAKE_TIMEOUT}        { count(); yylval.strval=yytext;
459                                                                                 return TLS_HANDSHAKE_TIMEOUT; }
460 <INITIAL>{TLS_SEND_TIMEOUT}     { count(); yylval.strval=yytext;
461                                                                                 return TLS_SEND_TIMEOUT; }
462 <INITIAL>{FIFO} { count(); yylval.strval=yytext; return FIFO; }
463 <INITIAL>{FIFO_DIR}     { count(); yylval.strval=yytext; return FIFO_DIR; }
464 <INITIAL>{FIFO_DB_URL}  { count(); yylval.strval=yytext; return FIFO_DB_URL; }
465 <INITIAL>{SOCK_MODE}    { count(); yylval.strval=yytext; return SOCK_MODE; }
466 <INITIAL>{SOCK_USER}    { count(); yylval.strval=yytext; return SOCK_USER; }
467 <INITIAL>{SOCK_GROUP}   { count(); yylval.strval=yytext; return SOCK_GROUP; }
468 <INITIAL>{UNIX_SOCK} { count(); yylval.strval=yytext; return UNIX_SOCK; }
469 <INITIAL>{UNIX_SOCK_CHILDREN} { count(); yylval.strval=yytext; return UNIX_SOCK_CHILDREN; }
470 <INITIAL>{UNIX_TX_TIMEOUT} { count(); yylval.strval=yytext; return UNIX_TX_TIMEOUT; }
471 <INITIAL>{SERVER_SIGNATURE}     { count(); yylval.strval=yytext; return SERVER_SIGNATURE; }
472 <INITIAL>{REPLY_TO_VIA} { count(); yylval.strval=yytext; return REPLY_TO_VIA; }
473 <INITIAL>{ADVERTISED_ADDRESS}   {       count(); yylval.strval=yytext;
474                                                                         return ADVERTISED_ADDRESS; }
475 <INITIAL>{ADVERTISED_PORT}              {       count(); yylval.strval=yytext;
476                                                                         return ADVERTISED_PORT; }
477 <INITIAL>{DISABLE_CORE}         {       count(); yylval.strval=yytext;
478                                                                         return DISABLE_CORE; }
479 <INITIAL>{OPEN_FD_LIMIT}                {       count(); yylval.strval=yytext;
480                                                                         return OPEN_FD_LIMIT; }
481 <INITIAL>{MCAST_LOOPBACK}               {       count(); yylval.strval=yytext;
482                                                                         return MCAST_LOOPBACK; }
483 <INITIAL>{MCAST_TTL}            {       count(); yylval.strval=yytext;
484                                                                         return MCAST_TTL; }
485 <INITIAL>{TOS}                  {       count(); yylval.strval=yytext;
486                                                                         return TOS; }
487 <INITIAL>{LOADMODULE}   { count(); yylval.strval=yytext; return LOADMODULE; }
488 <INITIAL>{MODPARAM}     { count(); yylval.strval=yytext; return MODPARAM; }
489
490 <INITIAL>{EQUAL}        { count(); return EQUAL; }
491 <INITIAL>{ADDEQ}          { count(); return ADDEQ; }
492 <INITIAL>{EQUAL_T}      { count(); return EQUAL_T; }
493 <INITIAL>{GT}   { count(); return GT; }
494 <INITIAL>{LT}   { count(); return LT; }
495 <INITIAL>{GTE}  { count(); return GTE; }
496 <INITIAL>{LTE}  { count(); return LTE; }
497 <INITIAL>{DIFF} { count(); return DIFF; }
498 <INITIAL>{MATCH}        { count(); return MATCH; }
499 <INITIAL>{NOT}          { count(); return NOT; }
500 <INITIAL>{LOG_AND}      { count(); return LOG_AND; }
501 <INITIAL>{BIN_AND}      { count(); return BIN_AND; }
502 <INITIAL>{LOG_OR}       { count(); return LOG_OR;  }
503 <INITIAL>{BIN_OR}       { count(); return BIN_OR;  }
504 <INITIAL>{PLUS}         { count(); return PLUS; }
505 <INITIAL>{MINUS}        { count(); return MINUS; }
506
507 <INITIAL>{SELECT_MARK}  { count(); state = SELECT_S; BEGIN(SELECT); return SELECT_MARK; }
508 <SELECT>{ID}            { count(); addstr(&s_buf, yytext, yyleng); 
509                           yylval.strval=s_buf.s;
510                           memset(&s_buf, 0, sizeof(s_buf));
511                           return ID; 
512                         }
513 <SELECT>{DOT}           { count(); return DOT; }
514 <SELECT>{LBRACK}        { count(); return LBRACK; }
515 <SELECT>{RBRACK}        { count(); return RBRACK; }
516 <SELECT>{DECNUMBER}     { count(); yylval.intval=atoi(yytext);return NUMBER; }
517 <SELECT>{HEXNUMBER}     { count(); yylval.intval=(int)strtol(yytext, 0, 16); return NUMBER; }
518 <SELECT>{OCTNUMBER}     { count(); yylval.intval=(int)strtol(yytext, 0, 8); return NUMBER; }
519 <SELECT>{BINNUMBER}     { count(); yylval.intval=(int)strtol(yytext, 0, 2); return NUMBER; }
520 <SELECT>.               { unput(yytext[0]); state = INITIAL_S; BEGIN(INITIAL); } /* Rescan the token in INITIAL state */
521
522
523 <INITIAL>{ATTR_MARK}    { count(); state = ATTR_S; BEGIN(ATTR); return ATTR_MARK; }
524 <ATTR>{ATTR_FROM}       { count(); return ATTR_FROM; }
525 <ATTR>{ATTR_TO}         { count(); return ATTR_TO; }
526 <ATTR>{LBRACK}          { count(); return LBRACK; }
527 <ATTR>{RBRACK}          { count(); return RBRACK; }
528 <ATTR>{ATTR_USER}       { count(); return ATTR_USER; }
529 <ATTR>{ATTR_DOMAIN}     { count(); return ATTR_DOMAIN; }
530 <ATTR>{ATTR_GLOBAL}     { count(); return ATTR_GLOBAL; }
531 <ATTR>{DOT}             { count(); return DOT; }
532 <ATTR>{ID}              { count(); addstr(&s_buf, yytext, yyleng); 
533                            yylval.strval=s_buf.s;
534                            memset(&s_buf, 0, sizeof(s_buf));
535                            state = INITIAL_S;
536                            BEGIN(INITIAL);
537                            return ID; 
538                         }
539
540 <INITIAL>{IPV6ADDR}             { count(); yylval.strval=yytext; return IPV6ADDR; }
541 <INITIAL>{DECNUMBER}            { count(); yylval.intval=atoi(yytext);return NUMBER; }
542 <INITIAL>{HEXNUMBER}    { count(); yylval.intval=(int)strtol(yytext, 0, 16);
543                                                         return NUMBER; }
544 <INITIAL>{OCTNUMBER}    { count(); yylval.intval=(int)strtol(yytext, 0, 8);
545                                                         return NUMBER; }
546 <INITIAL>{BINNUMBER}    { count(); yylval.intval=(int)strtol(yytext, 0, 2); return NUMBER; }
547 <INITIAL>{YES}                  { count(); yylval.intval=1; return NUMBER; }
548 <INITIAL>{NO}                   { count(); yylval.intval=0; return NUMBER; }
549 <INITIAL>{TCP}                  { count(); return TCP; }
550 <INITIAL>{UDP}                  { count(); return UDP; }
551 <INITIAL>{TLS}                  { count(); return TLS; }
552 <INITIAL>{INET}                 { count(); yylval.intval=AF_INET; return NUMBER; }
553 <INITIAL>{INET6}                { count();
554                                                 #ifdef USE_IPV6
555                                                   yylval.intval=AF_INET6;
556                                                 #else
557                                                   yylval.intval=-1; /* no match*/
558                                                 #endif
559                                                   return NUMBER; }
560 <INITIAL>{SSLv23}               { count(); yylval.strval=yytext; return SSLv23; }
561 <INITIAL>{SSLv2}                { count(); yylval.strval=yytext; return SSLv2; }
562 <INITIAL>{SSLv3}                { count(); yylval.strval=yytext; return SSLv3; }
563 <INITIAL>{TLSv1}                { count(); yylval.strval=yytext; return TLSv1; }
564
565 <INITIAL>{COMMA}                { count(); return COMMA; }
566 <INITIAL>{SEMICOLON}    { count(); return SEMICOLON; }
567 <INITIAL>{COLON}        { count(); return COLON; }
568 <INITIAL>{STAR} { count(); return STAR; }
569 <INITIAL>{RPAREN}       { count(); return RPAREN; }
570 <INITIAL>{LPAREN}       { count(); return LPAREN; }
571 <INITIAL>{LBRACE}       { count(); return LBRACE; }
572 <INITIAL>{RBRACE}       { count(); return RBRACE; }
573 <INITIAL>{LBRACK}       { count(); return LBRACK; }
574 <INITIAL>{RBRACK}       { count(); return RBRACK; }
575 <INITIAL>{SLASH}        { count(); return SLASH; }
576 <INITIAL>{DOT}          { count(); return DOT; }
577 <INITIAL>\\{CR}         {count(); } /* eat the escaped CR */
578 <INITIAL>{CR}           { count();/* return CR;*/ }
579
580
581 <INITIAL>{QUOTES} { count(); state=STRING_S; BEGIN(STRING1); }
582 <INITIAL>{TICK} { count(); state=STRING_S; BEGIN(STRING2); }
583
584
585 <STRING1>{QUOTES} { count(); state=INITIAL_S; BEGIN(INITIAL); 
586                                                 yytext[yyleng-1]=0; yyleng--;
587                                                 addstr(&s_buf, yytext, yyleng);
588                                                 yylval.strval=s_buf.s;
589                                                 memset(&s_buf, 0, sizeof(s_buf));
590                                                 return STRING;
591                                         }
592 <STRING2>{TICK}  { count(); state=INITIAL_S; BEGIN(INITIAL); 
593                                                 yytext[yyleng-1]=0; yyleng--;
594                                                 addstr(&s_buf, yytext, yyleng);
595                                                 yylval.strval=s_buf.s;
596                                                 memset(&s_buf, 0, sizeof(s_buf));
597                                                 return STRING;
598                                         }
599 <STRING2>.|{EAT_ABLE}|{CR}      { yymore(); }
600
601 <STRING1>\\n            { count(); addchar(&s_buf, '\n'); }
602 <STRING1>\\r            { count(); addchar(&s_buf, '\r'); }
603 <STRING1>\\a            { count(); addchar(&s_buf, '\a'); }
604 <STRING1>\\t            { count(); addchar(&s_buf, '\t'); }
605 <STRING1>\\{QUOTES}     { count(); addchar(&s_buf, '"');  }
606 <STRING1>\\\\           { count(); addchar(&s_buf, '\\'); } 
607 <STRING1>\\x{HEX}{1,2}  { count(); addchar(&s_buf, 
608                                                                                         (char)strtol(yytext+2, 0, 16)); }
609  /* don't allow \[0-7]{1}, it will eat the backreferences from
610     subst_uri if allowed (although everybody should use '' in subt_uri) */
611 <STRING1>\\[0-7]{2,3}   { count(); addchar(&s_buf, 
612                                                                                         (char)strtol(yytext+1, 0, 8));  }
613 <STRING1>\\{CR}         { count(); } /* eat escaped CRs */
614 <STRING1>.|{EAT_ABLE}|{CR}      { addchar(&s_buf, *yytext); }
615
616
617 <INITIAL,COMMENT>{COM_START}    { count(); comment_nest++; state=COMMENT_S;
618                                                                                 BEGIN(COMMENT); }
619 <COMMENT>{COM_END}                              { count(); comment_nest--;
620                                                                                 if (comment_nest==0){
621                                                                                         state=INITIAL_S;
622                                                                                         BEGIN(INITIAL);
623                                                                                 }
624                                                                 }
625 <COMMENT>.|{EAT_ABLE}|{CR}                              { count(); };
626
627 <INITIAL>{COM_LINE}.*{CR}       { count(); } 
628
629 <INITIAL>{ID}                   { count(); addstr(&s_buf, yytext, yyleng); 
630                                                                         yylval.strval=s_buf.s;
631                                                                         memset(&s_buf, 0, sizeof(s_buf));
632                                                                         return ID; }
633
634
635 <<EOF>>                                                 {
636                                                                         switch(state){
637                                                                                 case STRING_S: 
638                                                                                         LOG(L_CRIT, "ERROR: cfg. parser: unexpected EOF in"
639                                                                                                                 " unclosed string\n");
640                                                                                         if (s_buf.s){
641                                                                                                 pkg_free(s_buf.s);
642                                                                                                 memset(&s_buf, 0,
643                                                                                                                         sizeof(s_buf));
644                                                                                         }
645                                                                                         break;
646                                                                                 case COMMENT_S:
647                                                                                         LOG(L_CRIT, "ERROR: cfg. parser: unexpected EOF:"
648                                                                                                                 " %d comments open\n", comment_nest);
649                                                                                         break;
650                                                                                 case COMMENT_LN_S:
651                                                                                         LOG(L_CRIT, "ERROR: unexpected EOF:"
652                                                                                                                 "comment line open\n");
653                                                                                         break;
654                                                                         }
655                                                                         return 0;
656                                                                 }
657                         
658 %%
659
660
661 static char* addchar(struct str_buf* dst, char c)
662 {
663         return addstr(dst, &c, 1);
664 }
665
666
667
668 static char* addstr(struct str_buf* dst_b, char* src, int len)
669 {
670         char *tmp;
671         unsigned size;
672         unsigned used;
673         
674         if (dst_b->left<(len+1)){
675                 used=(unsigned)(dst_b->crt-dst_b->s);
676                 size=used+len+1;
677                 /* round up to next multiple */
678                 size+= STR_BUF_ALLOC_UNIT-size%STR_BUF_ALLOC_UNIT;
679                 tmp=pkg_malloc(size);
680                 if (tmp==0) goto error;
681                 if (dst_b->s){
682                         memcpy(tmp, dst_b->s, used); 
683                         pkg_free(dst_b->s);
684                 }
685                 dst_b->s=tmp;
686                 dst_b->crt=dst_b->s+used;
687                 dst_b->left=size-used;
688         }
689         memcpy(dst_b->crt, src, len);
690         dst_b->crt+=len;
691         *(dst_b->crt)=0;
692         dst_b->left-=len;
693         
694         return dst_b->s;
695 error:
696         LOG(L_CRIT, "ERROR:lex:addstr: memory allocation error\n");
697         return 0;
698 }
699
700
701
702 static void count()
703 {
704         int i;
705         
706         startcolumn=column;
707         for (i=0; i<yyleng;i++){
708                 if (yytext[i]=='\n'){
709                         line++;
710                         column=startcolumn=1;
711                 }else if (yytext[i]=='\t'){
712                         column++;
713                         /*column+=8 -(column%8);*/
714                 }else{
715                         column++;
716                 }
717         }
718 }
719