]> git.ipfire.org Git - thirdparty/bird.git/blob - conf/cf-lex.l
Merge commit '7b2c5f3d2826e3175bf31b1c36056c9efc587a2b' into int-new
[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
19 * name of a protocol, %SYM_CONSTANT for a constant etc.) and class
20 * dependent data. When an unknown symbol is encountered, it's
21 * automatically added to the 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 <stdint.h>
34 #include <unistd.h>
35 #include <libgen.h>
36 #include <glob.h>
37 #include <fcntl.h>
38 #include <sys/stat.h>
39 #include <sys/types.h>
40 #include <sys/stat.h>
41
42 #define PARSER 1
43
44 #include "nest/bird.h"
45 #include "nest/route.h"
46 #include "nest/protocol.h"
47 #include "filter/filter.h"
48 #include "conf/conf.h"
49 #include "conf/cf-parse.tab.h"
50 #include "lib/string.h"
51 #include "lib/hash.h"
52
53 struct keyword {
54 byte *name;
55 int value;
56 struct keyword *next;
57 };
58
59 #include "conf/keywords.h"
60
61 /* Could be defined by Bison in cf-parse.tab.h, inteferes with SYM hash */
62 #ifdef SYM
63 #undef SYM
64 #endif
65
66
67 static uint cf_hash(byte *c);
68
69 #define KW_KEY(n) n->name
70 #define KW_NEXT(n) n->next
71 #define KW_EQ(a,b) !strcmp(a,b)
72 #define KW_FN(k) cf_hash(k)
73 #define KW_ORDER 8 /* Fixed */
74
75 #define SYM_KEY(n) n->name, n->scope->active
76 #define SYM_NEXT(n) n->next
77 #define SYM_EQ(a,s1,b,s2) !strcmp(a,b) && s1 == s2
78 #define SYM_FN(k,s) cf_hash(k)
79 #define SYM_ORDER 6 /* Initial */
80
81 #define SYM_REHASH sym_rehash
82 #define SYM_PARAMS /8, *1, 2, 2, 6, 20
83
84
85 HASH_DEFINE_REHASH_FN(SYM, struct symbol)
86
87 HASH(struct keyword) kw_hash;
88
89
90 static struct sym_scope *conf_this_scope;
91
92 linpool *cfg_mem;
93
94 int (*cf_read_hook)(byte *buf, unsigned int max, int fd);
95 struct include_file_stack *ifs;
96 static struct include_file_stack *ifs_head;
97
98 #define MAX_INCLUDE_DEPTH 8
99
100 #define YY_INPUT(buf,result,max) result = cf_read_hook(buf, max, ifs->fd);
101 #define YY_NO_UNPUT
102 #define YY_FATAL_ERROR(msg) cf_error(msg)
103
104 static void cf_include(char *arg, int alen);
105 static int check_eof(void);
106
107 %}
108
109 %option noyywrap
110 %option noinput
111 %option nounput
112 %option noreject
113
114 %x COMMENT CCOMM CLI
115
116 ALPHA [a-zA-Z_]
117 DIGIT [0-9]
118 XIGIT [0-9a-fA-F]
119 ALNUM [a-zA-Z_0-9]
120 WHITE [ \t]
121 include ^{WHITE}*include{WHITE}*\".*\"{WHITE}*;
122
123 %%
124 {include} {
125 char *start, *end;
126
127 if (!ifs->depth)
128 cf_error("Include not allowed in CLI");
129
130 start = strchr(yytext, '"');
131 start++;
132
133 end = strchr(start, '"');
134 *end = 0;
135
136 if (start == end)
137 cf_error("Include with empty argument");
138
139 cf_include(start, end-start);
140 }
141
142 {DIGIT}+:{DIGIT}+ {
143 uint len1 UNUSED, len2;
144 u64 l;
145 char *e;
146
147 errno = 0;
148 l = strtoul(yytext, &e, 10);
149 if (e && (*e != ':') || (errno == ERANGE) || (l >> 32))
150 cf_error("ASN out of range");
151
152 if (l >> 16)
153 {
154 len1 = 32;
155 len2 = 16;
156 cf_lval.i64 = (2ULL << 48) | (((u64) l) << len2);
157 }
158 else
159 {
160 len1 = 16;
161 len2 = 32;
162 cf_lval.i64 = 0 | (((u64) l) << len2);
163 }
164
165 errno = 0;
166 l = strtoul(e+1, &e, 10);
167 if (e && *e || (errno == ERANGE) || (l >> len2))
168 cf_error("Number out of range");
169 cf_lval.i64 |= l;
170
171 return VPN_RD;
172 }
173
174 [02]:{DIGIT}+:{DIGIT}+ {
175 uint len1, len2;
176 u64 l;
177 char *e;
178
179 if (yytext[0] == '0')
180 {
181 cf_lval.i64 = 0;
182 len1 = 16;
183 len2 = 32;
184 }
185 else
186 {
187 cf_lval.i64 = 2ULL << 48;
188 len1 = 32;
189 len2 = 16;
190 }
191
192 errno = 0;
193 l = strtoul(yytext+2, &e, 10);
194 if (e && (*e != ':') || (errno == ERANGE) || (l >> len1))
195 cf_error("ASN out of range");
196 cf_lval.i64 |= ((u64) l) << len2;
197
198 errno = 0;
199 l = strtoul(e+1, &e, 10);
200 if (e && *e || (errno == ERANGE) || (l >> len2))
201 cf_error("Number out of range");
202 cf_lval.i64 |= l;
203
204 return VPN_RD;
205 }
206
207 {DIGIT}+\.{DIGIT}+\.{DIGIT}+\.{DIGIT}+:{DIGIT}+ {
208 unsigned long int l;
209 ip4_addr ip4;
210 char *e;
211
212 cf_lval.i64 = 1ULL << 48;
213
214 e = strchr(yytext, ':');
215 *e++ = '\0';
216 if (!ip4_pton(yytext, &ip4))
217 cf_error("Invalid IPv4 address %s in Route Distinguisher", yytext);
218 cf_lval.i64 |= ((u64) ip4_to_u32(ip4)) << 16;
219
220 errno = 0;
221 l = strtoul(e, &e, 10);
222 if (e && *e || (errno == ERANGE) || (l >> 16))
223 cf_error("Number out of range");
224 cf_lval.i64 |= l;
225
226 return VPN_RD;
227 }
228
229 {DIGIT}+\.{DIGIT}+\.{DIGIT}+\.{DIGIT}+ {
230 if (!ip4_pton(yytext, &cf_lval.ip4))
231 cf_error("Invalid IPv4 address %s", yytext);
232 return IP4;
233 }
234
235 ({XIGIT}*::|({XIGIT}*:){3,})({XIGIT}*|{DIGIT}+\.{DIGIT}+\.{DIGIT}+\.{DIGIT}+) {
236 if (!ip6_pton(yytext, &cf_lval.ip6))
237 cf_error("Invalid IPv6 address %s", yytext);
238 return IP6;
239 }
240
241 0x{XIGIT}+ {
242 char *e;
243 unsigned long int l;
244 errno = 0;
245 l = strtoul(yytext+2, &e, 16);
246 if (e && *e || errno == ERANGE || (unsigned long int)(unsigned int) l != l)
247 cf_error("Number out of range");
248 cf_lval.i = l;
249 return NUM;
250 }
251
252 {DIGIT}+ {
253 char *e;
254 unsigned long int l;
255 errno = 0;
256 l = strtoul(yytext, &e, 10);
257 if (e && *e || errno == ERANGE || (unsigned long int)(unsigned int) l != l)
258 cf_error("Number out of range");
259 cf_lval.i = l;
260 return NUM;
261 }
262
263 else: {
264 /* Hack to distinguish if..else from else: in case */
265 return ELSECOL;
266 }
267
268 ({ALPHA}{ALNUM}*|[']({ALNUM}|[-]|[\.]|[:])*[']) {
269 if(*yytext == '\'') {
270 yytext[yyleng-1] = 0;
271 yytext++;
272 }
273
274 struct keyword *k = HASH_FIND(kw_hash, KW, yytext);
275 if (k)
276 {
277 if (k->value > 0)
278 return k->value;
279 else
280 {
281 cf_lval.i = -k->value;
282 return ENUM;
283 }
284 }
285
286 cf_lval.s = cf_get_symbol(yytext);
287 return SYM;
288 }
289
290 <CLI>(.|\n) {
291 BEGIN(INITIAL);
292 return CLI_MARKER;
293 }
294
295 \.\. {
296 return DDOT;
297 }
298
299 [={}:;,.()+*/%<>~\[\]?!\|-] {
300 return yytext[0];
301 }
302
303 ["][^"\n]*["] {
304 yytext[yyleng-1] = 0;
305 cf_lval.t = cfg_strdup(yytext+1);
306 yytext[yyleng-1] = '"';
307 return TEXT;
308 }
309
310 ["][^"\n]*\n cf_error("Unterminated string");
311
312 <INITIAL,COMMENT><<EOF>> { if (check_eof()) return END; }
313
314 {WHITE}+
315
316 \n ifs->lino++;
317
318 # BEGIN(COMMENT);
319
320 \/\* BEGIN(CCOMM);
321
322 . cf_error("Unknown character");
323
324 <COMMENT>\n {
325 ifs->lino++;
326 BEGIN(INITIAL);
327 }
328
329 <COMMENT>.
330
331 <CCOMM>\*\/ BEGIN(INITIAL);
332 <CCOMM>\n ifs->lino++;
333 <CCOMM>\/\* cf_error("Comment nesting not supported");
334 <CCOMM><<EOF>> cf_error("Unterminated comment");
335 <CCOMM>.
336
337 \!\= return NEQ;
338 \!\~ return NMA;
339 \<\= return LEQ;
340 \>\= return GEQ;
341 \&\& return AND;
342 \|\| return OR;
343
344 \[\= return PO;
345 \=\] return PC;
346
347 %%
348
349 static uint
350 cf_hash(byte *c)
351 {
352 uint h = 13 << 24;
353
354 while (*c)
355 h = h + (h >> 2) + (h >> 5) + ((uint) *c++ << 24);
356 return h;
357 }
358
359
360 /*
361 * IFS stack - it contains structures needed for recursive processing
362 * of include in config files. On the top of the stack is a structure
363 * for currently processed file. Other structures are either for
364 * active files interrupted because of include directive (these have
365 * fd and flex buffer) or for inactive files scheduled to be processed
366 * later (when parent requested including of several files by wildcard
367 * match - these do not have fd and flex buffer yet).
368 *
369 * FIXME: Most of these ifs and include functions are really sysdep/unix.
370 */
371
372 static struct include_file_stack *
373 push_ifs(struct include_file_stack *old)
374 {
375 struct include_file_stack *ret;
376 ret = cfg_allocz(sizeof(struct include_file_stack));
377 ret->lino = 1;
378 ret->prev = old;
379 return ret;
380 }
381
382 static struct include_file_stack *
383 pop_ifs(struct include_file_stack *old)
384 {
385 yy_delete_buffer(old->buffer);
386 close(old->fd);
387 return old->prev;
388 }
389
390 static void
391 enter_ifs(struct include_file_stack *new)
392 {
393 if (!new->buffer)
394 {
395 new->fd = open(new->file_name, O_RDONLY);
396 if (new->fd < 0)
397 {
398 ifs = ifs->up;
399 cf_error("Unable to open included file %s: %m", new->file_name);
400 }
401
402 new->buffer = yy_create_buffer(NULL, YY_BUF_SIZE);
403 }
404
405 yy_switch_to_buffer(new->buffer);
406 }
407
408 /**
409 * cf_lex_unwind - unwind lexer state during error
410 *
411 * cf_lex_unwind() frees the internal state on IFS stack when the lexical
412 * analyzer is terminated by cf_error().
413 */
414 void
415 cf_lex_unwind(void)
416 {
417 struct include_file_stack *n;
418
419 for (n = ifs; n != ifs_head; n = n->prev)
420 {
421 /* Memory is freed automatically */
422 if (n->buffer)
423 yy_delete_buffer(n->buffer);
424 if (n->fd)
425 close(n->fd);
426 }
427
428 ifs = ifs_head;
429 }
430
431 static void
432 cf_include(char *arg, int alen)
433 {
434 struct include_file_stack *base_ifs = ifs;
435 int new_depth, rv, i;
436 char *patt;
437 glob_t g = {};
438
439 new_depth = ifs->depth + 1;
440 if (new_depth > MAX_INCLUDE_DEPTH)
441 cf_error("Max include depth reached");
442
443 /* expand arg to properly handle relative filenames */
444 if (*arg != '/')
445 {
446 int dlen = strlen(ifs->file_name);
447 char *dir = alloca(dlen + 1);
448 patt = alloca(dlen + alen + 2);
449 memcpy(dir, ifs->file_name, dlen + 1);
450 sprintf(patt, "%s/%s", dirname(dir), arg);
451 }
452 else
453 patt = arg;
454
455 /* Skip globbing if there are no wildcards, mainly to get proper
456 response when the included config file is missing */
457 if (!strpbrk(arg, "?*["))
458 {
459 ifs = push_ifs(ifs);
460 ifs->file_name = cfg_strdup(patt);
461 ifs->depth = new_depth;
462 ifs->up = base_ifs;
463 enter_ifs(ifs);
464 return;
465 }
466
467 /* Expand the pattern */
468 rv = glob(patt, GLOB_ERR | GLOB_NOESCAPE, NULL, &g);
469 if (rv == GLOB_ABORTED)
470 cf_error("Unable to match pattern %s: %m", patt);
471 if ((rv != 0) || (g.gl_pathc <= 0))
472 return;
473
474 /*
475 * Now we put all found files to ifs stack in reverse order, they
476 * will be activated and processed in order as ifs stack is popped
477 * by pop_ifs() and enter_ifs() in check_eof().
478 */
479 for(i = g.gl_pathc - 1; i >= 0; i--)
480 {
481 char *fname = g.gl_pathv[i];
482 struct stat fs;
483
484 if (stat(fname, &fs) < 0)
485 {
486 globfree(&g);
487 cf_error("Unable to stat included file %s: %m", fname);
488 }
489
490 if (fs.st_mode & S_IFDIR)
491 continue;
492
493 /* Prepare new stack item */
494 ifs = push_ifs(ifs);
495 ifs->file_name = cfg_strdup(fname);
496 ifs->depth = new_depth;
497 ifs->up = base_ifs;
498 }
499
500 globfree(&g);
501 enter_ifs(ifs);
502 }
503
504 static int
505 check_eof(void)
506 {
507 if (ifs == ifs_head)
508 {
509 /* EOF in main config file */
510 ifs->lino = 1; /* Why this? */
511 return 1;
512 }
513
514 ifs = pop_ifs(ifs);
515 enter_ifs(ifs);
516 return 0;
517 }
518
519 static struct symbol *
520 cf_new_symbol(byte *c)
521 {
522 struct symbol *s;
523
524 uint l = strlen(c);
525 if (l > SYM_MAX_LEN)
526 cf_error("Symbol too long");
527
528 s = cfg_alloc(sizeof(struct symbol) + l);
529 s->scope = conf_this_scope;
530 s->class = SYM_VOID;
531 s->def = NULL;
532 s->aux = 0;
533 strcpy(s->name, c);
534
535 if (!new_config->sym_hash.data)
536 HASH_INIT(new_config->sym_hash, new_config->pool, SYM_ORDER);
537
538 HASH_INSERT2(new_config->sym_hash, SYM, new_config->pool, s);
539
540 return s;
541 }
542
543 /**
544 * cf_find_symbol - find a symbol by name
545 * @cfg: specificed config
546 * @c: symbol name
547 *
548 * This functions searches the symbol table in the config @cfg for a symbol of
549 * given name. First it examines the current scope, then the second recent one
550 * and so on until it either finds the symbol and returns a pointer to its
551 * &symbol structure or reaches the end of the scope chain and returns %NULL to
552 * signify no match.
553 */
554 struct symbol *
555 cf_find_symbol(struct config *cfg, byte *c)
556 {
557 struct symbol *s;
558
559 if (cfg->sym_hash.data &&
560 (s = HASH_FIND(cfg->sym_hash, SYM, c, 1)))
561 return s;
562
563 if (cfg->fallback &&
564 cfg->fallback->sym_hash.data &&
565 (s = HASH_FIND(cfg->fallback->sym_hash, SYM, c, 1)))
566 return s;
567
568 return NULL;
569 }
570
571 /**
572 * cf_get_symbol - get a symbol by name
573 * @c: symbol name
574 *
575 * This functions searches the symbol table of the currently parsed config
576 * (@new_config) for a symbol of given name. It returns either the already
577 * existing symbol or a newly allocated undefined (%SYM_VOID) symbol if no
578 * existing symbol is found.
579 */
580 struct symbol *
581 cf_get_symbol(byte *c)
582 {
583 return cf_find_symbol(new_config, c) ?: cf_new_symbol(c);
584 }
585
586 struct symbol *
587 cf_default_name(char *template, int *counter)
588 {
589 char buf[SYM_MAX_LEN];
590 struct symbol *s;
591 char *perc = strchr(template, '%');
592
593 for(;;)
594 {
595 bsprintf(buf, template, ++(*counter));
596 s = cf_get_symbol(buf);
597 if (s->class == SYM_VOID)
598 return s;
599 if (!perc)
600 break;
601 }
602 cf_error("Unable to generate default name");
603 }
604
605 /**
606 * cf_define_symbol - define meaning of a symbol
607 * @sym: symbol to be defined
608 * @type: symbol class to assign
609 * @def: class dependent data
610 *
611 * Defines new meaning of a symbol. If the symbol is an undefined
612 * one (%SYM_VOID), it's just re-defined to the new type. If it's defined
613 * in different scope, a new symbol in current scope is created and the
614 * meaning is assigned to it. If it's already defined in the current scope,
615 * an error is reported via cf_error().
616 *
617 * Result: Pointer to the newly defined symbol. If we are in the top-level
618 * scope, it's the same @sym as passed to the function.
619 */
620 struct symbol *
621 cf_define_symbol(struct symbol *sym, int type, void *def)
622 {
623 if (sym->class)
624 {
625 if (sym->scope == conf_this_scope)
626 cf_error("Symbol already defined");
627 sym = cf_new_symbol(sym->name);
628 }
629 sym->class = type;
630 sym->def = def;
631 return sym;
632 }
633
634 static void
635 cf_lex_init_kh(void)
636 {
637 HASH_INIT(kw_hash, &root_pool, KW_ORDER);
638
639 struct keyword *k;
640 for (k=keyword_list; k->name; k++)
641 HASH_INSERT(kw_hash, KW, k);
642 }
643
644 /**
645 * cf_lex_init - initialize the lexer
646 * @is_cli: true if we're going to parse CLI command, false for configuration
647 * @c: configuration structure
648 *
649 * cf_lex_init() initializes the lexical analyzer and prepares it for
650 * parsing of a new input.
651 */
652 void
653 cf_lex_init(int is_cli, struct config *c)
654 {
655 if (!kw_hash.data)
656 cf_lex_init_kh();
657
658 ifs_head = ifs = push_ifs(NULL);
659 if (!is_cli)
660 {
661 ifs->file_name = c->file_name;
662 ifs->fd = c->file_fd;
663 ifs->depth = 1;
664 }
665
666 yyrestart(NULL);
667 ifs->buffer = YY_CURRENT_BUFFER;
668
669 if (is_cli)
670 BEGIN(CLI);
671 else
672 BEGIN(INITIAL);
673
674 conf_this_scope = cfg_allocz(sizeof(struct sym_scope));
675 conf_this_scope->active = 1;
676 }
677
678 /**
679 * cf_push_scope - enter new scope
680 * @sym: symbol representing scope name
681 *
682 * If we want to enter a new scope to process declarations inside
683 * a nested block, we can just call cf_push_scope() to push a new
684 * scope onto the scope stack which will cause all new symbols to be
685 * defined in this scope and all existing symbols to be sought for
686 * in all scopes stored on the stack.
687 */
688 void
689 cf_push_scope(struct symbol *sym)
690 {
691 struct sym_scope *s = cfg_alloc(sizeof(struct sym_scope));
692
693 s->next = conf_this_scope;
694 conf_this_scope = s;
695 s->active = 1;
696 s->name = sym;
697 }
698
699 /**
700 * cf_pop_scope - leave a scope
701 *
702 * cf_pop_scope() pops the topmost scope from the scope stack,
703 * leaving all its symbols in the symbol table, but making them
704 * invisible to the rest of the config.
705 */
706 void
707 cf_pop_scope(void)
708 {
709 conf_this_scope->active = 0;
710 conf_this_scope = conf_this_scope->next;
711 ASSERT(conf_this_scope);
712 }
713
714 /**
715 * cf_symbol_class_name - get name of a symbol class
716 * @sym: symbol
717 *
718 * This function returns a string representing the class
719 * of the given symbol.
720 */
721 char *
722 cf_symbol_class_name(struct symbol *sym)
723 {
724 if (cf_symbol_is_constant(sym))
725 return "constant";
726
727 switch (sym->class)
728 {
729 case SYM_VOID:
730 return "undefined";
731 case SYM_PROTO:
732 return "protocol";
733 case SYM_TEMPLATE:
734 return "protocol template";
735 case SYM_FUNCTION:
736 return "function";
737 case SYM_FILTER:
738 return "filter";
739 case SYM_TABLE:
740 return "routing table";
741 default:
742 return "unknown type";
743 }
744 }
745
746
747 /**
748 * DOC: Parser
749 *
750 * Both the configuration and CLI commands are analyzed using a syntax
751 * driven parser generated by the |bison| tool from a grammar which
752 * is constructed from information gathered from grammar snippets by
753 * the |gen_parser.m4| script.
754 *
755 * Grammar snippets are files (usually with extension |.Y|) contributed
756 * by various BIRD modules in order to provide information about syntax of their
757 * configuration and their CLI commands. Each snipped consists of several
758 * sections, each of them starting with a special keyword: |CF_HDR| for
759 * a list of |#include| directives needed by the C code, |CF_DEFINES|
760 * for a list of C declarations, |CF_DECLS| for |bison| declarations
761 * including keyword definitions specified as |CF_KEYWORDS|, |CF_GRAMMAR|
762 * for the grammar rules, |CF_CODE| for auxiliary C code and finally
763 * |CF_END| at the end of the snippet.
764 *
765 * To create references between the snippets, it's possible to define
766 * multi-part rules by utilizing the |CF_ADDTO| macro which adds a new
767 * alternative to a multi-part rule.
768 *
769 * CLI commands are defined using a |CF_CLI| macro. Its parameters are:
770 * the list of keywords determining the command, the list of parameters,
771 * help text for the parameters and help text for the command.
772 *
773 * Values of |enum| filter types can be defined using |CF_ENUM| with
774 * the following parameters: name of filter type, prefix common for all
775 * literals of this type and names of all the possible values.
776 */