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 */
34 #include "nest/bird.h"
35 #include "nest/route.h"
36 #include "nest/protocol.h"
37 #include "filter/filter.h"
38 #include "conf/conf.h"
39 #include "conf/cf-parse.tab.h"
40 #include "lib/string.h"
48 #include "conf/keywords.h"
50 #define KW_HASH_SIZE 64
51 static struct keyword *kw_hash[KW_HASH_SIZE];
52 static int kw_hash_inited;
54 #define SYM_HASH_SIZE 128
55 #define SYM_MAX_LEN 32
58 struct sym_scope *next; /* Next on scope stack */
59 struct symbol *name; /* Name of this scope */
60 int active; /* Currently entered */
62 static struct sym_scope *conf_this_scope;
66 static int cf_hash(byte *c);
67 static struct symbol *cf_find_sym(byte *c, unsigned int h0);
71 int (*cf_read_hook)(byte *buf, unsigned int max);
73 #define YY_INPUT(buf,result,max) result = cf_read_hook(buf, max);
75 #define YY_FATAL_ERROR(msg) cf_error(msg)
91 {DIGIT}+\.{DIGIT}+\.{DIGIT}+\.{DIGIT}+ {
93 if (ipv4_pton_u32(yytext, &cf_lval.i32))
95 cf_error("Invalid IPv4 address %s", yytext);
97 if (ip_pton(yytext, &cf_lval.a))
99 cf_error("Invalid IP address %s", yytext);
103 ({XIGIT}*::|({XIGIT}*:){3,})({XIGIT}*|{DIGIT}+\.{DIGIT}+\.{DIGIT}+\.{DIGIT}+) {
105 if (ip_pton(yytext, &cf_lval.a))
107 cf_error("Invalid IP address %s", yytext);
109 cf_error("This is an IPv4 router, therefore IPv6 addresses are not supported");
117 l = strtoul(yytext+2, &e, 16);
118 if (e && *e || errno == ERANGE || (long int)(int) l != l)
119 cf_error("Number out of range");
128 l = strtoul(yytext, &e, 10);
129 if (e && *e || errno == ERANGE || (long int)(int) l != l)
130 cf_error("Number out of range");
135 ({ALPHA}{ALNUM}*|[']({ALNUM}|[-])*[']) {
136 if(*yytext == '\'') {
137 yytext[yyleng-1] = 0;
140 unsigned int h = cf_hash(yytext);
141 struct keyword *k = kw_hash[h & (KW_HASH_SIZE-1)];
144 if (!strcmp(k->name, yytext))
150 cf_lval.i = -k->value;
156 cf_lval.s = cf_find_sym(yytext, h);
165 [={}:;,.()+*/%<>~\[\]?!\|-] {
170 yytext[yyleng-1] = 0;
171 cf_lval.t = cfg_strdup(yytext+1);
175 ["][^"\n]*\n cf_error("Unterminated string");
177 <INITIAL,COMMENT><<EOF>> return END;
187 . cf_error("Unknown character");
196 <CCOMM>\*\/ BEGIN(INITIAL);
197 <CCOMM>\n conf_lino++;
198 <CCOMM>\/\* cf_error("Comment nesting not supported");
199 <CCOMM><<EOF>> cf_error("Unterminated comment");
223 static struct symbol *
224 cf_new_sym(byte *c, unsigned int h)
226 struct symbol *s, **ht;
229 if (!new_config->sym_hash)
230 new_config->sym_hash = cfg_allocz(SYM_HASH_SIZE * sizeof(struct keyword *));
231 ht = new_config->sym_hash;
234 cf_error("Symbol too long");
235 s = cfg_alloc(sizeof(struct symbol) + l);
238 s->scope = conf_this_scope;
246 static struct symbol *
247 cf_find_sym(byte *c, unsigned int h0)
249 unsigned int h = h0 & (SYM_HASH_SIZE-1);
250 struct symbol *s, **ht;
252 if (ht = new_config->sym_hash)
254 for(s = ht[h]; s; s=s->next)
255 if (!strcmp(s->name, c) && s->scope->active)
258 if (new_config->sym_fallback)
260 /* We know only top-level scope is active */
261 for(s = new_config->sym_fallback[h]; s; s=s->next)
262 if (!strcmp(s->name, c) && s->scope->active)
265 return cf_new_sym(c, h);
269 * cf_find_symbol - find a symbol by name
272 * This functions searches the symbol table for a symbol of given
273 * name. First it examines the current scope, then the second recent
274 * one and so on until it either finds the symbol and returns a pointer
275 * to its &symbol structure or reaches the end of the scope chain
276 * and returns %NULL to signify no match.
279 cf_find_symbol(byte *c)
281 return cf_find_sym(c, cf_hash(c));
285 cf_default_name(char *template, int *counter)
289 char *perc = strchr(template, '%');
293 bsprintf(buf, template, ++(*counter));
294 s = cf_find_sym(buf, cf_hash(buf));
297 if (s->class == SYM_VOID)
302 cf_error("Unable to generate default name");
306 * cf_define_symbol - define meaning of a symbol
307 * @sym: symbol to be defined
308 * @type: symbol class to assign
309 * @def: class dependent data
311 * Defines new meaning of a symbol. If the symbol is an undefined
312 * one (%SYM_VOID), it's just re-defined to the new type. If it's defined
313 * in different scope, a new symbol in current scope is created and the
314 * meaning is assigned to it. If it's already defined in the current scope,
315 * an error is reported via cf_error().
317 * Result: Pointer to the newly defined symbol. If we are in the top-level
318 * scope, it's the same @sym as passed to the function.
321 cf_define_symbol(struct symbol *sym, int type, void *def)
325 if (sym->scope == conf_this_scope)
326 cf_error("Symbol already defined");
327 sym = cf_new_sym(sym->name, cf_hash(sym->name) & (SYM_HASH_SIZE-1));
339 for(k=keyword_list; k->name; k++)
341 unsigned h = cf_hash(k->name) & (KW_HASH_SIZE-1);
342 k->next = kw_hash[h];
349 * cf_lex_init - initialize the lexer
350 * @is_cli: true if we're going to parse CLI command, false for configuration
352 * cf_lex_init() initializes the lexical analyzer and prepares it for
353 * parsing of a new input.
356 cf_lex_init(int is_cli)
366 conf_this_scope = cfg_allocz(sizeof(struct sym_scope));
367 conf_this_scope->active = 1;
371 * cf_push_scope - enter new scope
372 * @sym: symbol representing scope name
374 * If we want to enter a new scope to process declarations inside
375 * a nested block, we can just call cf_push_scope() to push a new
376 * scope onto the scope stack which will cause all new symbols to be
377 * defined in this scope and all existing symbols to be sought for
378 * in all scopes stored on the stack.
381 cf_push_scope(struct symbol *sym)
383 struct sym_scope *s = cfg_alloc(sizeof(struct sym_scope));
385 s->next = conf_this_scope;
392 * cf_pop_scope - leave a scope
394 * cf_pop_scope() pops the topmost scope from the scope stack,
395 * leaving all its symbols in the symbol table, but making them
396 * invisible to the rest of the config.
401 conf_this_scope->active = 0;
402 conf_this_scope = conf_this_scope->next;
403 ASSERT(conf_this_scope);
407 cf_walk_symbols(struct config *cf, struct symbol *sym, int *pos)
413 if (*pos >= SYM_HASH_SIZE)
415 sym = cf->sym_hash[(*pos)++];
419 if (sym && sym->scope->active)
425 * cf_symbol_class_name - get name of a symbol class
428 * This function returns a string representing the class
429 * of the given symbol.
432 cf_symbol_class_name(struct symbol *sym)
441 return "numeric constant";
447 return "routing table";
449 return "network address";
451 return "unknown type";
459 * Both the configuration and CLI commands are analyzed using a syntax
460 * driven parser generated by the |bison| tool from a grammar which
461 * is constructed from information gathered from grammar snippets by
462 * the |gen_parser.m4| script.
464 * Grammar snippets are files (usually with extension |.Y|) contributed
465 * by various BIRD modules in order to provide information about syntax of their
466 * configuration and their CLI commands. Each snipped consists of several
467 * sections, each of them starting with a special keyword: |CF_HDR| for
468 * a list of |#include| directives needed by the C code, |CF_DEFINES|
469 * for a list of C declarations, |CF_DECLS| for |bison| declarations
470 * including keyword definitions specified as |CF_KEYWORDS|, |CF_GRAMMAR|
471 * for the grammar rules, |CF_CODE| for auxiliary C code and finally
472 * |CF_END| at the end of the snippet.
474 * To create references between the snippets, it's possible to define
475 * multi-part rules by utilizing the |CF_ADDTO| macro which adds a new
476 * alternative to a multi-part rule.
478 * CLI commands are defined using a |CF_CLI| macro. Its parameters are:
479 * the list of keywords determining the command, the list of parameters,
480 * help text for the parameters and help text for the command.
482 * Values of |enum| filter types can be defined using |CF_ENUM| with
483 * the following parameters: name of filter type, prefix common for all
484 * literals of this type and names of all the possible values.