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}+ {
128 if (!ip4_pton(yytext, &a))
129 cf_error("Invalid IPv4 address %s", yytext);
132 cf_lval.i32 = ip4_to_u32(a);
135 cf_lval.a = ipa_from_ip4(a);
140 ({XIGIT}*::|({XIGIT}*:){3,})({XIGIT}*|{DIGIT}+\.{DIGIT}+\.{DIGIT}+\.{DIGIT}+) {
142 if (ipa_pton(yytext, &cf_lval.a))
144 cf_error("Invalid IPv6 address %s", yytext);
146 cf_error("This is an IPv4 router, therefore IPv6 addresses are not supported");
154 l = strtoul(yytext+2, &e, 16);
155 if (e && *e || errno == ERANGE || (unsigned long int)(unsigned int) l != l)
156 cf_error("Number out of range");
165 l = strtoul(yytext, &e, 10);
166 if (e && *e || errno == ERANGE || (unsigned long int)(unsigned int) l != l)
167 cf_error("Number out of range");
173 /* Hack to distinguish if..else from else: in case */
177 ({ALPHA}{ALNUM}*|[']({ALNUM}|[-]|[\.]|[:])*[']) {
178 if(*yytext == '\'') {
179 yytext[yyleng-1] = 0;
182 unsigned int h = cf_hash(yytext);
183 struct keyword *k = kw_hash[h & (KW_HASH_SIZE-1)];
186 if (!strcmp(k->name, yytext))
192 cf_lval.i = -k->value;
198 cf_lval.s = cf_get_sym(yytext, h);
211 [={}:;,.()+*/%<>~\[\]?!\|-] {
216 yytext[yyleng-1] = 0;
217 cf_lval.t = cfg_strdup(yytext+1);
221 ["][^"\n]*\n cf_error("Unterminated string");
223 <INITIAL,COMMENT><<EOF>> { if (check_eof()) return END; }
233 . cf_error("Unknown character");
242 <CCOMM>\*\/ BEGIN(INITIAL);
243 <CCOMM>\n ifs->lino++;
244 <CCOMM>\/\* cf_error("Comment nesting not supported");
245 <CCOMM><<EOF>> cf_error("Unterminated comment");
272 * IFS stack - it contains structures needed for recursive processing
273 * of include in config files. On the top of the stack is a structure
274 * for currently processed file. Other structures are either for
275 * active files interrupted because of include directive (these have
276 * fd and flex buffer) or for inactive files scheduled to be processed
277 * later (when parent requested including of several files by wildcard
278 * match - these do not have fd and flex buffer yet).
280 * FIXME: Most of these ifs and include functions are really sysdep/unix.
283 static struct include_file_stack *
284 push_ifs(struct include_file_stack *old)
286 struct include_file_stack *ret;
287 ret = cfg_allocz(sizeof(struct include_file_stack));
293 static struct include_file_stack *
294 pop_ifs(struct include_file_stack *old)
296 yy_delete_buffer(old->buffer);
302 enter_ifs(struct include_file_stack *new)
306 new->fd = open(new->file_name, O_RDONLY);
310 cf_error("Unable to open included file %s: %m", new->file_name);
313 new->buffer = yy_create_buffer(NULL, YY_BUF_SIZE);
316 yy_switch_to_buffer(new->buffer);
320 * cf_lex_unwind - unwind lexer state during error
322 * cf_lex_unwind() frees the internal state on IFS stack when the lexical
323 * analyzer is terminated by cf_error().
328 struct include_file_stack *n;
330 for (n = ifs; n != ifs_head; n = n->prev)
332 /* Memory is freed automatically */
334 yy_delete_buffer(n->buffer);
343 cf_include(char *arg, int alen)
345 struct include_file_stack *base_ifs = ifs;
346 int new_depth, rv, i;
350 new_depth = ifs->depth + 1;
351 if (new_depth > MAX_INCLUDE_DEPTH)
352 cf_error("Max include depth reached");
354 /* expand arg to properly handle relative filenames */
357 int dlen = strlen(ifs->file_name);
358 char *dir = alloca(dlen + 1);
359 patt = alloca(dlen + alen + 2);
360 memcpy(dir, ifs->file_name, dlen + 1);
361 sprintf(patt, "%s/%s", dirname(dir), arg);
366 /* Skip globbing if there are no wildcards, mainly to get proper
367 response when the included config file is missing */
368 if (!strpbrk(arg, "?*["))
371 ifs->file_name = cfg_strdup(patt);
372 ifs->depth = new_depth;
378 /* Expand the pattern */
379 rv = glob(patt, GLOB_ERR | GLOB_NOESCAPE, NULL, &g);
380 if (rv == GLOB_ABORTED)
381 cf_error("Unable to match pattern %s: %m", patt);
382 if ((rv != 0) || (g.gl_pathc <= 0))
386 * Now we put all found files to ifs stack in reverse order, they
387 * will be activated and processed in order as ifs stack is popped
388 * by pop_ifs() and enter_ifs() in check_eof().
390 for(i = g.gl_pathc - 1; i >= 0; i--)
392 char *fname = g.gl_pathv[i];
395 if (stat(fname, &fs) < 0)
398 cf_error("Unable to stat included file %s: %m", fname);
401 if (fs.st_mode & S_IFDIR)
404 /* Prepare new stack item */
406 ifs->file_name = cfg_strdup(fname);
407 ifs->depth = new_depth;
420 /* EOF in main config file */
421 ifs->lino = 1; /* Why this? */
430 static struct symbol *
431 cf_new_sym(byte *c, uint h0)
433 uint h = h0 & (SYM_HASH_SIZE-1);
434 struct symbol *s, **ht;
437 if (!new_config->sym_hash)
438 new_config->sym_hash = cfg_allocz(SYM_HASH_SIZE * sizeof(struct keyword *));
439 ht = new_config->sym_hash;
442 cf_error("Symbol too long");
443 s = cfg_alloc(sizeof(struct symbol) + l);
446 s->scope = conf_this_scope;
454 static struct symbol *
455 cf_find_sym(struct config *cfg, byte *c, uint h0)
457 uint h = h0 & (SYM_HASH_SIZE-1);
458 struct symbol *s, **ht;
460 if (ht = cfg->sym_hash)
462 for(s = ht[h]; s; s=s->next)
463 if (!strcmp(s->name, c) && s->scope->active)
466 if (ht = cfg->sym_fallback)
468 /* We know only top-level scope is active */
469 for(s = ht[h]; s; s=s->next)
470 if (!strcmp(s->name, c) && s->scope->active)
477 static inline struct symbol *
478 cf_get_sym(byte *c, uint h0)
480 return cf_find_sym(new_config, c, h0) ?: cf_new_sym(c, h0);
484 * cf_find_symbol - find a symbol by name
485 * @cfg: specificed config
488 * This functions searches the symbol table in the config @cfg for a symbol of
489 * given name. First it examines the current scope, then the second recent one
490 * and so on until it either finds the symbol and returns a pointer to its
491 * &symbol structure or reaches the end of the scope chain and returns %NULL to
495 cf_find_symbol(struct config *cfg, byte *c)
497 return cf_find_sym(cfg, c, cf_hash(c));
501 * cf_get_symbol - get a symbol by name
504 * This functions searches the symbol table of the currently parsed config
505 * (@new_config) for a symbol of given name. It returns either the already
506 * existing symbol or a newly allocated undefined (%SYM_VOID) symbol if no
507 * existing symbol is found.
510 cf_get_symbol(byte *c)
512 return cf_get_sym(c, cf_hash(c));
516 cf_default_name(char *template, int *counter)
518 char buf[SYM_MAX_LEN];
520 char *perc = strchr(template, '%');
524 bsprintf(buf, template, ++(*counter));
525 s = cf_get_sym(buf, cf_hash(buf));
526 if (s->class == SYM_VOID)
531 cf_error("Unable to generate default name");
535 * cf_define_symbol - define meaning of a symbol
536 * @sym: symbol to be defined
537 * @type: symbol class to assign
538 * @def: class dependent data
540 * Defines new meaning of a symbol. If the symbol is an undefined
541 * one (%SYM_VOID), it's just re-defined to the new type. If it's defined
542 * in different scope, a new symbol in current scope is created and the
543 * meaning is assigned to it. If it's already defined in the current scope,
544 * an error is reported via cf_error().
546 * Result: Pointer to the newly defined symbol. If we are in the top-level
547 * scope, it's the same @sym as passed to the function.
550 cf_define_symbol(struct symbol *sym, int type, void *def)
554 if (sym->scope == conf_this_scope)
555 cf_error("Symbol already defined");
556 sym = cf_new_sym(sym->name, cf_hash(sym->name));
568 for(k=keyword_list; k->name; k++)
570 unsigned h = cf_hash(k->name) & (KW_HASH_SIZE-1);
571 k->next = kw_hash[h];
578 * cf_lex_init - initialize the lexer
579 * @is_cli: true if we're going to parse CLI command, false for configuration
580 * @c: configuration structure
582 * cf_lex_init() initializes the lexical analyzer and prepares it for
583 * parsing of a new input.
586 cf_lex_init(int is_cli, struct config *c)
591 ifs_head = ifs = push_ifs(NULL);
594 ifs->file_name = c->file_name;
595 ifs->fd = c->file_fd;
600 ifs->buffer = YY_CURRENT_BUFFER;
607 conf_this_scope = cfg_allocz(sizeof(struct sym_scope));
608 conf_this_scope->active = 1;
612 * cf_push_scope - enter new scope
613 * @sym: symbol representing scope name
615 * If we want to enter a new scope to process declarations inside
616 * a nested block, we can just call cf_push_scope() to push a new
617 * scope onto the scope stack which will cause all new symbols to be
618 * defined in this scope and all existing symbols to be sought for
619 * in all scopes stored on the stack.
622 cf_push_scope(struct symbol *sym)
624 struct sym_scope *s = cfg_alloc(sizeof(struct sym_scope));
626 s->next = conf_this_scope;
633 * cf_pop_scope - leave a scope
635 * cf_pop_scope() pops the topmost scope from the scope stack,
636 * leaving all its symbols in the symbol table, but making them
637 * invisible to the rest of the config.
642 conf_this_scope->active = 0;
643 conf_this_scope = conf_this_scope->next;
644 ASSERT(conf_this_scope);
648 cf_walk_symbols(struct config *cf, struct symbol *sym, int *pos)
654 if (*pos >= SYM_HASH_SIZE)
656 sym = cf->sym_hash[(*pos)++];
660 if (sym && sym->scope->active)
666 * cf_symbol_class_name - get name of a symbol class
669 * This function returns a string representing the class
670 * of the given symbol.
673 cf_symbol_class_name(struct symbol *sym)
675 if (cf_symbol_is_constant(sym))
685 return "protocol template";
691 return "routing table";
695 return "unknown type";
703 * Both the configuration and CLI commands are analyzed using a syntax
704 * driven parser generated by the |bison| tool from a grammar which
705 * is constructed from information gathered from grammar snippets by
706 * the |gen_parser.m4| script.
708 * Grammar snippets are files (usually with extension |.Y|) contributed
709 * by various BIRD modules in order to provide information about syntax of their
710 * configuration and their CLI commands. Each snipped consists of several
711 * sections, each of them starting with a special keyword: |CF_HDR| for
712 * a list of |#include| directives needed by the C code, |CF_DEFINES|
713 * for a list of C declarations, |CF_DECLS| for |bison| declarations
714 * including keyword definitions specified as |CF_KEYWORDS|, |CF_GRAMMAR|
715 * for the grammar rules, |CF_CODE| for auxiliary C code and finally
716 * |CF_END| at the end of the snippet.
718 * To create references between the snippets, it's possible to define
719 * multi-part rules by utilizing the |CF_ADDTO| macro which adds a new
720 * alternative to a multi-part rule.
722 * CLI commands are defined using a |CF_CLI| macro. Its parameters are:
723 * the list of keywords determining the command, the list of parameters,
724 * help text for the parameters and help text for the command.
726 * Values of |enum| filter types can be defined using |CF_ENUM| with
727 * the following parameters: name of filter type, prefix common for all
728 * literals of this type and names of all the possible values.