]> git.ipfire.org Git - thirdparty/bird.git/blob - conf/cf-lex.l
Merge branch 'soon'
[thirdparty/bird.git] / conf / cf-lex.l
1 /*
2 * BIRD -- Configuration Lexer
3 *
4 * (c) 1998--2000 Martin Mares <mj@ucw.cz>
5 *
6 * Can be freely distributed and used under the terms of the GNU GPL.
7 */
8
9 /**
10 * DOC: Lexical analyzer
11 *
12 * The lexical analyzer used for configuration files and CLI commands
13 * is generated using the |flex| tool accompanied by a couple of
14 * functions maintaining the hash tables containing information about
15 * symbols and keywords.
16 *
17 * Each symbol is represented by a &symbol structure containing name
18 * of the symbol, its lexical scope, symbol class (%SYM_PROTO for a name of a protocol,
19 * %SYM_NUMBER for a numeric constant etc.) and class dependent data.
20 * When an unknown symbol is encountered, it's automatically added to the
21 * symbol table with class %SYM_VOID.
22 *
23 * The keyword tables are generated from the grammar templates
24 * using the |gen_keywords.m4| script.
25 */
26
27 %{
28 #undef REJECT /* Avoid name clashes */
29
30 #include <errno.h>
31 #include <stdlib.h>
32 #include <stdarg.h>
33 #include <unistd.h>
34
35 #define PARSER 1
36
37 #include "nest/bird.h"
38 #include "nest/route.h"
39 #include "nest/protocol.h"
40 #include "filter/filter.h"
41 #include "conf/conf.h"
42 #include "conf/cf-parse.tab.h"
43 #include "lib/string.h"
44
45 struct keyword {
46 byte *name;
47 int value;
48 struct keyword *next;
49 };
50
51 #include "conf/keywords.h"
52
53 #define KW_HASH_SIZE 64
54 static struct keyword *kw_hash[KW_HASH_SIZE];
55 static int kw_hash_inited;
56
57 #define SYM_HASH_SIZE 128
58 #define SYM_MAX_LEN 32
59
60 struct sym_scope {
61 struct sym_scope *next; /* Next on scope stack */
62 struct symbol *name; /* Name of this scope */
63 int active; /* Currently entered */
64 };
65 static struct sym_scope *conf_this_scope;
66
67 #define MAX_INCLUDE_DEPTH 5
68
69 static struct include_file_stack *ifs_head;
70 static int ifs_depth;
71
72 static int cf_hash(byte *c);
73 static struct symbol *cf_find_sym(byte *c, unsigned int h0);
74
75 linpool *cfg_mem;
76
77 int (*cf_read_hook)(byte *buf, unsigned int max, int fd);
78 int (*cf_open_hook)(char *filename);
79 struct include_file_stack *ifs;
80
81 #define YY_INPUT(buf,result,max) result = cf_read_hook(buf, max, ifs->conf_fd);
82 #define YY_NO_UNPUT
83 #define YY_FATAL_ERROR(msg) cf_error(msg)
84
85 static void new_include(void);
86 static int check_eof(void);
87 static struct include_file_stack *new_stack(struct include_file_stack *old);
88
89 %}
90
91 %option noyywrap
92 %option noinput
93 %option nounput
94 %option noreject
95
96 %x COMMENT CCOMM CLI
97
98 ALPHA [a-zA-Z_]
99 DIGIT [0-9]
100 XIGIT [0-9a-fA-F]
101 ALNUM [a-zA-Z_0-9]
102 WHITE [ \t]
103 include ^{WHITE}*include{WHITE}*\".*\"{WHITE}*;
104
105 %%
106 {include} { if(cf_open_hook) new_include(); }
107
108 {DIGIT}+\.{DIGIT}+\.{DIGIT}+\.{DIGIT}+ {
109 #ifdef IPV6
110 if (ipv4_pton_u32(yytext, &cf_lval.i32))
111 return RTRID;
112 cf_error("Invalid IPv4 address %s", yytext);
113 #else
114 if (ip_pton(yytext, &cf_lval.a))
115 return IPA;
116 cf_error("Invalid IP address %s", yytext);
117 #endif
118 }
119
120 ({XIGIT}*::|({XIGIT}*:){3,})({XIGIT}*|{DIGIT}+\.{DIGIT}+\.{DIGIT}+\.{DIGIT}+) {
121 #ifdef IPV6
122 if (ip_pton(yytext, &cf_lval.a))
123 return IPA;
124 cf_error("Invalid IP address %s", yytext);
125 #else
126 cf_error("This is an IPv4 router, therefore IPv6 addresses are not supported");
127 #endif
128 }
129
130 0x{XIGIT}+ {
131 char *e;
132 unsigned long int l;
133 errno = 0;
134 l = strtoul(yytext+2, &e, 16);
135 if (e && *e || errno == ERANGE || (unsigned long int)(unsigned int) l != l)
136 cf_error("Number out of range");
137 cf_lval.i = l;
138 return NUM;
139 }
140
141 {DIGIT}+ {
142 char *e;
143 unsigned long int l;
144 errno = 0;
145 l = strtoul(yytext, &e, 10);
146 if (e && *e || errno == ERANGE || (unsigned long int)(unsigned int) l != l)
147 cf_error("Number out of range");
148 cf_lval.i = l;
149 return NUM;
150 }
151
152 else: {
153 /* Hack to distinguish if..else from else: in case */
154 return ELSECOL;
155 }
156
157 ({ALPHA}{ALNUM}*|[']({ALNUM}|[-])*[']) {
158 if(*yytext == '\'') {
159 yytext[yyleng-1] = 0;
160 yytext++;
161 }
162 unsigned int h = cf_hash(yytext);
163 struct keyword *k = kw_hash[h & (KW_HASH_SIZE-1)];
164 while (k)
165 {
166 if (!strcmp(k->name, yytext))
167 {
168 if (k->value > 0)
169 return k->value;
170 else
171 {
172 cf_lval.i = -k->value;
173 return ENUM;
174 }
175 }
176 k=k->next;
177 }
178 cf_lval.s = cf_find_sym(yytext, h);
179 return SYM;
180 }
181
182 <CLI>(.|\n) {
183 BEGIN(INITIAL);
184 return CLI_MARKER;
185 }
186
187 \.\. {
188 return DDOT;
189 }
190
191 [={}:;,.()+*/%<>~\[\]?!\|-] {
192 return yytext[0];
193 }
194
195 ["][^"\n]*["] {
196 yytext[yyleng-1] = 0;
197 cf_lval.t = cfg_strdup(yytext+1);
198 return TEXT;
199 }
200
201 ["][^"\n]*\n cf_error("Unterminated string");
202
203 <INITIAL,COMMENT><<EOF>> { if(check_eof()) return END; }
204
205 {WHITE}+
206
207 \n ifs->conf_lino++;
208
209 # BEGIN(COMMENT);
210
211 \/\* BEGIN(CCOMM);
212
213 . cf_error("Unknown character");
214
215 <COMMENT>\n {
216 ifs->conf_lino++;
217 BEGIN(INITIAL);
218 }
219
220 <COMMENT>.
221
222 <CCOMM>\*\/ BEGIN(INITIAL);
223 <CCOMM>\n ifs->conf_lino++;
224 <CCOMM>\/\* cf_error("Comment nesting not supported");
225 <CCOMM><<EOF>> cf_error("Unterminated comment");
226 <CCOMM>.
227
228 \!\= return NEQ;
229 \<\= return LEQ;
230 \>\= return GEQ;
231 \&\& return AND;
232 \|\| return OR;
233
234 \[\= return PO;
235 \=\] return PC;
236
237 %%
238
239 static int
240 cf_hash(byte *c)
241 {
242 unsigned int h = 13;
243
244 while (*c)
245 h = (h * 37) + *c++;
246 return h;
247 }
248
249 /* Open included file with properly swapped buffers */
250 static void
251 new_include(void)
252 {
253 char *fname, *p = NULL;
254
255 if ((fname = strchr(yytext, '"')) != NULL) {
256
257 if ((p = strchr(++fname, '"')) != NULL) *p = '\0';
258
259 if (ifs_depth >= MAX_INCLUDE_DEPTH)
260 cf_error("Max include depth reached.");
261
262 /* Save current stack */
263 ifs->stack = YY_CURRENT_BUFFER;
264 /* Prepare new stack */
265 ifs->next = new_stack(ifs);
266 ifs = ifs->next;
267 strcpy(ifs->conf_fname, fname); /* XXX: strlcpy should be here */
268 ifs->conf_fd = cf_open_hook(fname);
269
270 yy_switch_to_buffer(yy_create_buffer(yyin, YY_BUF_SIZE));
271 }
272 }
273
274 static int
275 check_eof(void)
276 {
277 if (ifs == ifs_head) {
278 /* EOF in main config file */
279 ifs->conf_lino = 1;
280 return 1;
281 }
282
283 ifs_depth--;
284 close(ifs->conf_fd);
285 ifs = ifs->prev;
286 ifs->next = NULL;
287
288 yy_delete_buffer(YY_CURRENT_BUFFER);
289 yy_switch_to_buffer(ifs->stack);
290 return 0;
291 }
292
293 static struct symbol *
294 cf_new_sym(byte *c, unsigned int h)
295 {
296 struct symbol *s, **ht;
297 int l;
298
299 if (!new_config->sym_hash)
300 new_config->sym_hash = cfg_allocz(SYM_HASH_SIZE * sizeof(struct keyword *));
301 ht = new_config->sym_hash;
302 l = strlen(c);
303 if (l > SYM_MAX_LEN)
304 cf_error("Symbol too long");
305 s = cfg_alloc(sizeof(struct symbol) + l);
306 s->next = ht[h];
307 ht[h] = s;
308 s->scope = conf_this_scope;
309 s->class = SYM_VOID;
310 s->def = NULL;
311 s->aux = 0;
312 strcpy(s->name, c);
313 return s;
314 }
315
316 static struct symbol *
317 cf_find_sym(byte *c, unsigned int h0)
318 {
319 unsigned int h = h0 & (SYM_HASH_SIZE-1);
320 struct symbol *s, **ht;
321
322 if (ht = new_config->sym_hash)
323 {
324 for(s = ht[h]; s; s=s->next)
325 if (!strcmp(s->name, c) && s->scope->active)
326 return s;
327 }
328 if (new_config->sym_fallback)
329 {
330 /* We know only top-level scope is active */
331 for(s = new_config->sym_fallback[h]; s; s=s->next)
332 if (!strcmp(s->name, c) && s->scope->active)
333 return s;
334 }
335 return cf_new_sym(c, h);
336 }
337
338 /**
339 * cf_find_symbol - find a symbol by name
340 * @c: symbol name
341 *
342 * This functions searches the symbol table for a symbol of given
343 * name. First it examines the current scope, then the second recent
344 * one and so on until it either finds the symbol and returns a pointer
345 * to its &symbol structure or reaches the end of the scope chain
346 * and returns %NULL to signify no match.
347 */
348 struct symbol *
349 cf_find_symbol(byte *c)
350 {
351 return cf_find_sym(c, cf_hash(c));
352 }
353
354 struct symbol *
355 cf_default_name(char *template, int *counter)
356 {
357 char buf[32];
358 struct symbol *s;
359 char *perc = strchr(template, '%');
360
361 for(;;)
362 {
363 bsprintf(buf, template, ++(*counter));
364 s = cf_find_sym(buf, cf_hash(buf));
365 if (!s)
366 break;
367 if (s->class == SYM_VOID)
368 return s;
369 if (!perc)
370 break;
371 }
372 cf_error("Unable to generate default name");
373 }
374
375 /**
376 * cf_define_symbol - define meaning of a symbol
377 * @sym: symbol to be defined
378 * @type: symbol class to assign
379 * @def: class dependent data
380 *
381 * Defines new meaning of a symbol. If the symbol is an undefined
382 * one (%SYM_VOID), it's just re-defined to the new type. If it's defined
383 * in different scope, a new symbol in current scope is created and the
384 * meaning is assigned to it. If it's already defined in the current scope,
385 * an error is reported via cf_error().
386 *
387 * Result: Pointer to the newly defined symbol. If we are in the top-level
388 * scope, it's the same @sym as passed to the function.
389 */
390 struct symbol *
391 cf_define_symbol(struct symbol *sym, int type, void *def)
392 {
393 if (sym->class)
394 {
395 if (sym->scope == conf_this_scope)
396 cf_error("Symbol already defined");
397 sym = cf_new_sym(sym->name, cf_hash(sym->name) & (SYM_HASH_SIZE-1));
398 }
399 sym->class = type;
400 sym->def = def;
401 return sym;
402 }
403
404 static void
405 cf_lex_init_kh(void)
406 {
407 struct keyword *k;
408
409 for(k=keyword_list; k->name; k++)
410 {
411 unsigned h = cf_hash(k->name) & (KW_HASH_SIZE-1);
412 k->next = kw_hash[h];
413 kw_hash[h] = k;
414 }
415 kw_hash_inited = 1;
416 }
417
418 static struct include_file_stack *
419 new_stack(struct include_file_stack *old)
420 {
421 struct include_file_stack *ret;
422 ret = cfg_allocz(sizeof(struct include_file_stack));
423 ret->conf_lino = 1;
424 ret->prev = old;
425 return ret;
426 }
427
428 /**
429 * cf_lex_init - initialize the lexer
430 * @is_cli: true if we're going to parse CLI command, false for configuration
431 *
432 * cf_lex_init() initializes the lexical analyzer and prepares it for
433 * parsing of a new input.
434 */
435 void
436 cf_lex_init(int is_cli, struct config *c)
437 {
438 if (!kw_hash_inited)
439 cf_lex_init_kh();
440 ifs_head = new_stack(NULL);
441 ifs = ifs_head;
442 ifs_depth = 0;
443 if (!is_cli) {
444 ifs->conf_fd = c->file_fd;
445 ifs_depth = 1;
446 strcpy(ifs->conf_fname, c->file_name);
447 }
448 yyrestart(NULL);
449 if (is_cli)
450 BEGIN(CLI);
451 else
452 BEGIN(INITIAL);
453 conf_this_scope = cfg_allocz(sizeof(struct sym_scope));
454 conf_this_scope->active = 1;
455 }
456
457 /**
458 * cf_push_scope - enter new scope
459 * @sym: symbol representing scope name
460 *
461 * If we want to enter a new scope to process declarations inside
462 * a nested block, we can just call cf_push_scope() to push a new
463 * scope onto the scope stack which will cause all new symbols to be
464 * defined in this scope and all existing symbols to be sought for
465 * in all scopes stored on the stack.
466 */
467 void
468 cf_push_scope(struct symbol *sym)
469 {
470 struct sym_scope *s = cfg_alloc(sizeof(struct sym_scope));
471
472 s->next = conf_this_scope;
473 conf_this_scope = s;
474 s->active = 1;
475 s->name = sym;
476 }
477
478 /**
479 * cf_pop_scope - leave a scope
480 *
481 * cf_pop_scope() pops the topmost scope from the scope stack,
482 * leaving all its symbols in the symbol table, but making them
483 * invisible to the rest of the config.
484 */
485 void
486 cf_pop_scope(void)
487 {
488 conf_this_scope->active = 0;
489 conf_this_scope = conf_this_scope->next;
490 ASSERT(conf_this_scope);
491 }
492
493 struct symbol *
494 cf_walk_symbols(struct config *cf, struct symbol *sym, int *pos)
495 {
496 for(;;)
497 {
498 if (!sym)
499 {
500 if (*pos >= SYM_HASH_SIZE)
501 return NULL;
502 sym = cf->sym_hash[(*pos)++];
503 }
504 else
505 sym = sym->next;
506 if (sym && sym->scope->active)
507 return sym;
508 }
509 }
510
511 /**
512 * cf_symbol_class_name - get name of a symbol class
513 * @sym: symbol
514 *
515 * This function returns a string representing the class
516 * of the given symbol.
517 */
518 char *
519 cf_symbol_class_name(struct symbol *sym)
520 {
521 switch (sym->class)
522 {
523 case SYM_VOID:
524 return "undefined";
525 case SYM_PROTO:
526 return "protocol";
527 case SYM_NUMBER:
528 return "numeric constant";
529 case SYM_FUNCTION:
530 return "function";
531 case SYM_FILTER:
532 return "filter";
533 case SYM_TABLE:
534 return "routing table";
535 case SYM_IPA:
536 return "network address";
537 case SYM_TEMPLATE:
538 return "protocol template";
539 case SYM_ROA:
540 return "ROA table";
541 default:
542 return "unknown type";
543 }
544 }
545
546
547 /**
548 * DOC: Parser
549 *
550 * Both the configuration and CLI commands are analyzed using a syntax
551 * driven parser generated by the |bison| tool from a grammar which
552 * is constructed from information gathered from grammar snippets by
553 * the |gen_parser.m4| script.
554 *
555 * Grammar snippets are files (usually with extension |.Y|) contributed
556 * by various BIRD modules in order to provide information about syntax of their
557 * configuration and their CLI commands. Each snipped consists of several
558 * sections, each of them starting with a special keyword: |CF_HDR| for
559 * a list of |#include| directives needed by the C code, |CF_DEFINES|
560 * for a list of C declarations, |CF_DECLS| for |bison| declarations
561 * including keyword definitions specified as |CF_KEYWORDS|, |CF_GRAMMAR|
562 * for the grammar rules, |CF_CODE| for auxiliary C code and finally
563 * |CF_END| at the end of the snippet.
564 *
565 * To create references between the snippets, it's possible to define
566 * multi-part rules by utilizing the |CF_ADDTO| macro which adds a new
567 * alternative to a multi-part rule.
568 *
569 * CLI commands are defined using a |CF_CLI| macro. Its parameters are:
570 * the list of keywords determining the command, the list of parameters,
571 * help text for the parameters and help text for the command.
572 *
573 * Values of |enum| filter types can be defined using |CF_ENUM| with
574 * the following parameters: name of filter type, prefix common for all
575 * literals of this type and names of all the possible values.
576 */