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