2 * BIRD -- Configuration Lexer
4 * (c) 1998--2000 Martin Mares <mj@ucw.cz>
6 * Can be freely distributed and used under the terms of the GNU GPL.
10 * DOC: Lexical analyzer
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.
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.
23 * The keyword tables are generated from the grammar templates
24 * using the |gen_keywords.m4| script.
28 #undef REJECT /* Avoid name clashes */
39 #include <sys/types.h>
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"
58 #include "conf/keywords.h"
60 #define KW_HASH_SIZE 64
61 static struct keyword *kw_hash[KW_HASH_SIZE];
62 static int kw_hash_inited;
64 #define SYM_HASH_SIZE 128
67 struct sym_scope *next; /* Next on scope stack */
68 struct symbol *name; /* Name of this scope */
69 int active; /* Currently entered */
71 static struct sym_scope *conf_this_scope;
73 static int cf_hash(byte *c);
74 static inline struct symbol * cf_get_sym(byte *c, uint h0);
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;
82 #define MAX_INCLUDE_DEPTH 8
84 #define YY_INPUT(buf,result,max) result = cf_read_hook(buf, max, ifs->fd);
86 #define YY_FATAL_ERROR(msg) cf_error(msg)
88 static void cf_include(char *arg, int alen);
89 static int check_eof(void);
105 include ^{WHITE}*include{WHITE}*\".*\"{WHITE}*;
112 cf_error("Include not allowed in CLI");
114 start = strchr(yytext, '"');
117 end = strchr(start, '"');
121 cf_error("Include with empty argument");
123 cf_include(start, end-start);
126 {DIGIT}+\.{DIGIT}+\.{DIGIT}+\.{DIGIT}+ {
127 if (!ip4_pton(yytext, &cf_lval.ip4))
128 cf_error("Invalid IPv4 address %s", yytext);
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);
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");
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");
161 /* Hack to distinguish if..else from else: in case */
165 ({ALPHA}{ALNUM}*|[']({ALNUM}|[-]|[\.]|[:])*[']) {
166 if(*yytext == '\'') {
167 yytext[yyleng-1] = 0;
170 unsigned int h = cf_hash(yytext);
171 struct keyword *k = kw_hash[h & (KW_HASH_SIZE-1)];
174 if (!strcmp(k->name, yytext))
180 cf_lval.i = -k->value;
186 cf_lval.s = cf_get_sym(yytext, h);
199 [={}:;,.()+*/%<>~\[\]?!\|-] {
204 yytext[yyleng-1] = 0;
205 cf_lval.t = cfg_strdup(yytext+1);
206 yytext[yyleng-1] = '"';
210 ["][^"\n]*\n cf_error("Unterminated string");
212 <INITIAL,COMMENT><<EOF>> { if (check_eof()) return END; }
222 . cf_error("Unknown character");
231 <CCOMM>\*\/ BEGIN(INITIAL);
232 <CCOMM>\n ifs->lino++;
233 <CCOMM>\/\* cf_error("Comment nesting not supported");
234 <CCOMM><<EOF>> cf_error("Unterminated comment");
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).
269 * FIXME: Most of these ifs and include functions are really sysdep/unix.
272 static struct include_file_stack *
273 push_ifs(struct include_file_stack *old)
275 struct include_file_stack *ret;
276 ret = cfg_allocz(sizeof(struct include_file_stack));
282 static struct include_file_stack *
283 pop_ifs(struct include_file_stack *old)
285 yy_delete_buffer(old->buffer);
291 enter_ifs(struct include_file_stack *new)
295 new->fd = open(new->file_name, O_RDONLY);
299 cf_error("Unable to open included file %s: %m", new->file_name);
302 new->buffer = yy_create_buffer(NULL, YY_BUF_SIZE);
305 yy_switch_to_buffer(new->buffer);
309 * cf_lex_unwind - unwind lexer state during error
311 * cf_lex_unwind() frees the internal state on IFS stack when the lexical
312 * analyzer is terminated by cf_error().
317 struct include_file_stack *n;
319 for (n = ifs; n != ifs_head; n = n->prev)
321 /* Memory is freed automatically */
323 yy_delete_buffer(n->buffer);
332 cf_include(char *arg, int alen)
334 struct include_file_stack *base_ifs = ifs;
335 int new_depth, rv, i;
339 new_depth = ifs->depth + 1;
340 if (new_depth > MAX_INCLUDE_DEPTH)
341 cf_error("Max include depth reached");
343 /* expand arg to properly handle relative filenames */
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);
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, "?*["))
360 ifs->file_name = cfg_strdup(patt);
361 ifs->depth = new_depth;
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))
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().
379 for(i = g.gl_pathc - 1; i >= 0; i--)
381 char *fname = g.gl_pathv[i];
384 if (stat(fname, &fs) < 0)
387 cf_error("Unable to stat included file %s: %m", fname);
390 if (fs.st_mode & S_IFDIR)
393 /* Prepare new stack item */
395 ifs->file_name = cfg_strdup(fname);
396 ifs->depth = new_depth;
409 /* EOF in main config file */
410 ifs->lino = 1; /* Why this? */
419 static struct symbol *
420 cf_new_sym(byte *c, uint h0)
422 uint h = h0 & (SYM_HASH_SIZE-1);
423 struct symbol *s, **ht;
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;
431 cf_error("Symbol too long");
432 s = cfg_alloc(sizeof(struct symbol) + l);
435 s->scope = conf_this_scope;
443 static struct symbol *
444 cf_find_sym(struct config *cfg, byte *c, uint h0)
446 uint h = h0 & (SYM_HASH_SIZE-1);
447 struct symbol *s, **ht;
449 if (ht = cfg->sym_hash)
451 for(s = ht[h]; s; s=s->next)
452 if (!strcmp(s->name, c) && s->scope->active)
455 if (ht = cfg->sym_fallback)
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)
466 static inline struct symbol *
467 cf_get_sym(byte *c, uint h0)
469 return cf_find_sym(new_config, c, h0) ?: cf_new_sym(c, h0);
473 * cf_find_symbol - find a symbol by name
474 * @cfg: specificed config
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
484 cf_find_symbol(struct config *cfg, byte *c)
486 return cf_find_sym(cfg, c, cf_hash(c));
490 * cf_get_symbol - get a symbol by name
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.
499 cf_get_symbol(byte *c)
501 return cf_get_sym(c, cf_hash(c));
505 cf_default_name(char *template, int *counter)
507 char buf[SYM_MAX_LEN];
509 char *perc = strchr(template, '%');
513 bsprintf(buf, template, ++(*counter));
514 s = cf_get_sym(buf, cf_hash(buf));
515 if (s->class == SYM_VOID)
520 cf_error("Unable to generate default name");
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
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().
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.
539 cf_define_symbol(struct symbol *sym, int type, void *def)
543 if (sym->scope == conf_this_scope)
544 cf_error("Symbol already defined");
545 sym = cf_new_sym(sym->name, cf_hash(sym->name));
557 for(k=keyword_list; k->name; k++)
559 unsigned h = cf_hash(k->name) & (KW_HASH_SIZE-1);
560 k->next = kw_hash[h];
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
571 * cf_lex_init() initializes the lexical analyzer and prepares it for
572 * parsing of a new input.
575 cf_lex_init(int is_cli, struct config *c)
580 ifs_head = ifs = push_ifs(NULL);
583 ifs->file_name = c->file_name;
584 ifs->fd = c->file_fd;
589 ifs->buffer = YY_CURRENT_BUFFER;
596 conf_this_scope = cfg_allocz(sizeof(struct sym_scope));
597 conf_this_scope->active = 1;
601 * cf_push_scope - enter new scope
602 * @sym: symbol representing scope name
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.
611 cf_push_scope(struct symbol *sym)
613 struct sym_scope *s = cfg_alloc(sizeof(struct sym_scope));
615 s->next = conf_this_scope;
622 * cf_pop_scope - leave a scope
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.
631 conf_this_scope->active = 0;
632 conf_this_scope = conf_this_scope->next;
633 ASSERT(conf_this_scope);
637 cf_walk_symbols(struct config *cf, struct symbol *sym, int *pos)
643 if (*pos >= SYM_HASH_SIZE)
645 sym = cf->sym_hash[(*pos)++];
649 if (sym && sym->scope->active)
655 * cf_symbol_class_name - get name of a symbol class
658 * This function returns a string representing the class
659 * of the given symbol.
662 cf_symbol_class_name(struct symbol *sym)
664 if (cf_symbol_is_constant(sym))
674 return "protocol template";
680 return "routing table";
682 return "unknown type";
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.
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.
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.
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.
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.