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 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.
23 * The keyword tables are generated from the grammar templates
24 * using the |gen_keywords.m4| script.
28 #undef REJECT /* Avoid name clashes */
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"
51 #include "conf/keywords.h"
53 #define KW_HASH_SIZE 64
54 static struct keyword *kw_hash[KW_HASH_SIZE];
55 static int kw_hash_inited;
57 #define SYM_HASH_SIZE 128
58 #define SYM_MAX_LEN 32
61 struct sym_scope *next; /* Next on scope stack */
62 struct symbol *name; /* Name of this scope */
63 int active; /* Currently entered */
65 static struct sym_scope *conf_this_scope;
67 #define MAX_INCLUDE_DEPTH 5
69 static struct include_file_stack *ifs_head;
72 static int cf_hash(byte *c);
73 static struct symbol *cf_find_sym(byte *c, unsigned int h0);
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;
81 #define YY_INPUT(buf,result,max) result = cf_read_hook(buf, max, ifs->conf_fd);
83 #define YY_FATAL_ERROR(msg) cf_error(msg)
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);
103 include ^{WHITE}*include{WHITE}*\".*\"{WHITE}*;
106 {include} { if(cf_open_hook) new_include(); }
108 {DIGIT}+\.{DIGIT}+\.{DIGIT}+\.{DIGIT}+ {
110 if (ipv4_pton_u32(yytext, &cf_lval.i32))
112 cf_error("Invalid IPv4 address %s", yytext);
114 if (ip_pton(yytext, &cf_lval.a))
116 cf_error("Invalid IP address %s", yytext);
120 ({XIGIT}*::|({XIGIT}*:){3,})({XIGIT}*|{DIGIT}+\.{DIGIT}+\.{DIGIT}+\.{DIGIT}+) {
122 if (ip_pton(yytext, &cf_lval.a))
124 cf_error("Invalid IP address %s", yytext);
126 cf_error("This is an IPv4 router, therefore IPv6 addresses are not supported");
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");
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");
153 /* Hack to distinguish if..else from else: in case */
157 ({ALPHA}{ALNUM}*|[']({ALNUM}|[-])*[']) {
158 if(*yytext == '\'') {
159 yytext[yyleng-1] = 0;
162 unsigned int h = cf_hash(yytext);
163 struct keyword *k = kw_hash[h & (KW_HASH_SIZE-1)];
166 if (!strcmp(k->name, yytext))
172 cf_lval.i = -k->value;
178 cf_lval.s = cf_find_sym(yytext, h);
191 [={}:;,.()+*/%<>~\[\]?!\|-] {
196 yytext[yyleng-1] = 0;
197 cf_lval.t = cfg_strdup(yytext+1);
201 ["][^"\n]*\n cf_error("Unterminated string");
203 <INITIAL,COMMENT><<EOF>> { if(check_eof()) return END; }
213 . cf_error("Unknown character");
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");
249 /* Open included file with properly swapped buffers */
253 char *fname, *p = NULL;
255 if ((fname = strchr(yytext, '"')) != NULL) {
257 if ((p = strchr(++fname, '"')) != NULL) *p = '\0';
259 if (ifs_depth >= MAX_INCLUDE_DEPTH)
260 cf_error("Max include depth reached.");
262 /* Save current stack */
263 ifs->stack = YY_CURRENT_BUFFER;
264 /* Prepare new stack */
265 ifs->next = new_stack(ifs);
267 strcpy(ifs->conf_fname, fname); /* XXX: strlcpy should be here */
268 ifs->conf_fd = cf_open_hook(fname);
270 yy_switch_to_buffer(yy_create_buffer(yyin, YY_BUF_SIZE));
277 if (ifs == ifs_head) {
278 /* EOF in main config file */
288 yy_delete_buffer(YY_CURRENT_BUFFER);
289 yy_switch_to_buffer(ifs->stack);
293 static struct symbol *
294 cf_new_sym(byte *c, unsigned int h)
296 struct symbol *s, **ht;
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;
304 cf_error("Symbol too long");
305 s = cfg_alloc(sizeof(struct symbol) + l);
308 s->scope = conf_this_scope;
316 static struct symbol *
317 cf_find_sym(byte *c, unsigned int h0)
319 unsigned int h = h0 & (SYM_HASH_SIZE-1);
320 struct symbol *s, **ht;
322 if (ht = new_config->sym_hash)
324 for(s = ht[h]; s; s=s->next)
325 if (!strcmp(s->name, c) && s->scope->active)
328 if (new_config->sym_fallback)
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)
335 return cf_new_sym(c, h);
339 * cf_find_symbol - find a symbol by name
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.
349 cf_find_symbol(byte *c)
351 return cf_find_sym(c, cf_hash(c));
355 cf_default_name(char *template, int *counter)
359 char *perc = strchr(template, '%');
363 bsprintf(buf, template, ++(*counter));
364 s = cf_find_sym(buf, cf_hash(buf));
367 if (s->class == SYM_VOID)
372 cf_error("Unable to generate default name");
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
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().
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.
391 cf_define_symbol(struct symbol *sym, int type, void *def)
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));
409 for(k=keyword_list; k->name; k++)
411 unsigned h = cf_hash(k->name) & (KW_HASH_SIZE-1);
412 k->next = kw_hash[h];
418 static struct include_file_stack *
419 new_stack(struct include_file_stack *old)
421 struct include_file_stack *ret;
422 ret = cfg_allocz(sizeof(struct include_file_stack));
429 * cf_lex_init - initialize the lexer
430 * @is_cli: true if we're going to parse CLI command, false for configuration
432 * cf_lex_init() initializes the lexical analyzer and prepares it for
433 * parsing of a new input.
436 cf_lex_init(int is_cli, struct config *c)
440 ifs_head = new_stack(NULL);
444 ifs->conf_fd = c->file_fd;
446 strcpy(ifs->conf_fname, c->file_name);
453 conf_this_scope = cfg_allocz(sizeof(struct sym_scope));
454 conf_this_scope->active = 1;
458 * cf_push_scope - enter new scope
459 * @sym: symbol representing scope name
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.
468 cf_push_scope(struct symbol *sym)
470 struct sym_scope *s = cfg_alloc(sizeof(struct sym_scope));
472 s->next = conf_this_scope;
479 * cf_pop_scope - leave a scope
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.
488 conf_this_scope->active = 0;
489 conf_this_scope = conf_this_scope->next;
490 ASSERT(conf_this_scope);
494 cf_walk_symbols(struct config *cf, struct symbol *sym, int *pos)
500 if (*pos >= SYM_HASH_SIZE)
502 sym = cf->sym_hash[(*pos)++];
506 if (sym && sym->scope->active)
512 * cf_symbol_class_name - get name of a symbol class
515 * This function returns a string representing the class
516 * of the given symbol.
519 cf_symbol_class_name(struct symbol *sym)
528 return "numeric constant";
534 return "routing table";
536 return "network address";
538 return "protocol template";
542 return "unknown type";
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.
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.
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.
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.
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.