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 */
38 #include <sys/types.h>
43 #include "nest/bird.h"
44 #include "nest/route.h"
45 #include "nest/protocol.h"
46 #include "filter/filter.h"
47 #include "conf/conf.h"
48 #include "conf/cf-parse.tab.h"
49 #include "lib/string.h"
57 #include "conf/keywords.h"
59 #define KW_HASH_SIZE 64
60 static struct keyword *kw_hash[KW_HASH_SIZE];
61 static int kw_hash_inited;
63 #define SYM_HASH_SIZE 128
64 #define SYM_MAX_LEN 32
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 struct symbol *cf_find_sym(byte *c, unsigned int 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 (ipv4_pton_u32(yytext, &cf_lval.i32))
130 cf_error("Invalid IPv4 address %s", yytext);
132 if (ip_pton(yytext, &cf_lval.a))
134 cf_error("Invalid IP address %s", yytext);
138 ({XIGIT}*::|({XIGIT}*:){3,})({XIGIT}*|{DIGIT}+\.{DIGIT}+\.{DIGIT}+\.{DIGIT}+) {
140 if (ip_pton(yytext, &cf_lval.a))
142 cf_error("Invalid IP address %s", yytext);
144 cf_error("This is an IPv4 router, therefore IPv6 addresses are not supported");
152 l = strtoul(yytext+2, &e, 16);
153 if (e && *e || errno == ERANGE || (unsigned long int)(unsigned int) l != l)
154 cf_error("Number out of range");
163 l = strtoul(yytext, &e, 10);
164 if (e && *e || errno == ERANGE || (unsigned long int)(unsigned int) l != l)
165 cf_error("Number out of range");
171 /* Hack to distinguish if..else from else: in case */
175 ({ALPHA}{ALNUM}*|[']({ALNUM}|[-])*[']) {
176 if(*yytext == '\'') {
177 yytext[yyleng-1] = 0;
180 unsigned int h = cf_hash(yytext);
181 struct keyword *k = kw_hash[h & (KW_HASH_SIZE-1)];
184 if (!strcmp(k->name, yytext))
190 cf_lval.i = -k->value;
196 cf_lval.s = cf_find_sym(yytext, h);
209 [={}:;,.()+*/%<>~\[\]?!\|-] {
214 yytext[yyleng-1] = 0;
215 cf_lval.t = cfg_strdup(yytext+1);
219 ["][^"\n]*\n cf_error("Unterminated string");
221 <INITIAL,COMMENT><<EOF>> { if (check_eof()) return END; }
231 . cf_error("Unknown character");
240 <CCOMM>\*\/ BEGIN(INITIAL);
241 <CCOMM>\n ifs->lino++;
242 <CCOMM>\/\* cf_error("Comment nesting not supported");
243 <CCOMM><<EOF>> cf_error("Unterminated comment");
269 * IFS stack - it contains structures needed for recursive processing
270 * of include in config files. On the top of the stack is a structure
271 * for currently processed file. Other structures are either for
272 * active files interrupted because of include directive (these have
273 * fd and flex buffer) or for inactive files scheduled to be processed
274 * later (when parent requested including of several files by wildcard
275 * match - these do not have fd and flex buffer yet).
277 * FIXME: Most of these ifs and include functions are really sysdep/unix.
279 * FIXME: Resources (fd, flex buffers and glob data) in IFS stack
280 * are not freed when cf_error() is called.
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_include(char *arg, int alen)
322 struct include_file_stack *base_ifs = ifs;
323 int new_depth, rv, i;
327 new_depth = ifs->depth + 1;
328 if (new_depth > MAX_INCLUDE_DEPTH)
329 cf_error("Max include depth reached");
331 /* expand arg to properly handle relative filenames */
334 int dlen = strlen(ifs->file_name);
335 char *dir = alloca(dlen + 1);
336 patt = alloca(dlen + alen + 2);
337 memcpy(dir, ifs->file_name, dlen + 1);
338 sprintf(patt, "%s/%s", dirname(dir), arg);
343 /* Skip globbing if there are no wildcards, mainly to get proper
344 response when the included config file is missing */
345 if (!strpbrk(arg, "?*["))
348 ifs->file_name = cfg_strdup(patt);
349 ifs->depth = new_depth;
355 /* Expand the pattern */
356 rv = glob(patt, GLOB_ERR | GLOB_NOESCAPE, NULL, &g);
357 if (rv == GLOB_ABORTED)
358 cf_error("Unable to match pattern %s: %m", patt);
359 if ((rv != 0) || (g.gl_pathc <= 0))
363 * Now we put all found files to ifs stack in reverse order, they
364 * will be activated and processed in order as ifs stack is popped
365 * by pop_ifs() and enter_ifs() in check_eof().
367 for(i = g.gl_pathc - 1; i >= 0; i--)
369 char *fname = g.gl_pathv[i];
372 if (stat(fname, &fs) < 0)
373 cf_error("Unable to stat included file %s: %m", fname);
375 if (fs.st_mode & S_IFDIR)
378 /* Prepare new stack item */
380 ifs->file_name = cfg_strdup(fname);
381 ifs->depth = new_depth;
394 /* EOF in main config file */
395 ifs->lino = 1; /* Why this? */
404 static struct symbol *
405 cf_new_sym(byte *c, unsigned int h)
407 struct symbol *s, **ht;
410 if (!new_config->sym_hash)
411 new_config->sym_hash = cfg_allocz(SYM_HASH_SIZE * sizeof(struct keyword *));
412 ht = new_config->sym_hash;
415 cf_error("Symbol too long");
416 s = cfg_alloc(sizeof(struct symbol) + l);
419 s->scope = conf_this_scope;
427 static struct symbol *
428 cf_find_sym(byte *c, unsigned int h0)
430 unsigned int h = h0 & (SYM_HASH_SIZE-1);
431 struct symbol *s, **ht;
433 if (ht = new_config->sym_hash)
435 for(s = ht[h]; s; s=s->next)
436 if (!strcmp(s->name, c) && s->scope->active)
439 if (new_config->sym_fallback)
441 /* We know only top-level scope is active */
442 for(s = new_config->sym_fallback[h]; s; s=s->next)
443 if (!strcmp(s->name, c) && s->scope->active)
446 return cf_new_sym(c, h);
450 * cf_find_symbol - find a symbol by name
453 * This functions searches the symbol table for a symbol of given
454 * name. First it examines the current scope, then the second recent
455 * one and so on until it either finds the symbol and returns a pointer
456 * to its &symbol structure or reaches the end of the scope chain
457 * and returns %NULL to signify no match.
460 cf_find_symbol(byte *c)
462 return cf_find_sym(c, cf_hash(c));
466 cf_default_name(char *template, int *counter)
470 char *perc = strchr(template, '%');
474 bsprintf(buf, template, ++(*counter));
475 s = cf_find_sym(buf, cf_hash(buf));
478 if (s->class == SYM_VOID)
483 cf_error("Unable to generate default name");
487 * cf_define_symbol - define meaning of a symbol
488 * @sym: symbol to be defined
489 * @type: symbol class to assign
490 * @def: class dependent data
492 * Defines new meaning of a symbol. If the symbol is an undefined
493 * one (%SYM_VOID), it's just re-defined to the new type. If it's defined
494 * in different scope, a new symbol in current scope is created and the
495 * meaning is assigned to it. If it's already defined in the current scope,
496 * an error is reported via cf_error().
498 * Result: Pointer to the newly defined symbol. If we are in the top-level
499 * scope, it's the same @sym as passed to the function.
502 cf_define_symbol(struct symbol *sym, int type, void *def)
506 if (sym->scope == conf_this_scope)
507 cf_error("Symbol already defined");
508 sym = cf_new_sym(sym->name, cf_hash(sym->name) & (SYM_HASH_SIZE-1));
520 for(k=keyword_list; k->name; k++)
522 unsigned h = cf_hash(k->name) & (KW_HASH_SIZE-1);
523 k->next = kw_hash[h];
530 * cf_lex_init - initialize the lexer
531 * @is_cli: true if we're going to parse CLI command, false for configuration
533 * cf_lex_init() initializes the lexical analyzer and prepares it for
534 * parsing of a new input.
537 cf_lex_init(int is_cli, struct config *c)
542 ifs_head = ifs = push_ifs(NULL);
545 ifs->file_name = c->file_name;
546 ifs->fd = c->file_fd;
551 ifs->buffer = YY_CURRENT_BUFFER;
558 conf_this_scope = cfg_allocz(sizeof(struct sym_scope));
559 conf_this_scope->active = 1;
563 * cf_push_scope - enter new scope
564 * @sym: symbol representing scope name
566 * If we want to enter a new scope to process declarations inside
567 * a nested block, we can just call cf_push_scope() to push a new
568 * scope onto the scope stack which will cause all new symbols to be
569 * defined in this scope and all existing symbols to be sought for
570 * in all scopes stored on the stack.
573 cf_push_scope(struct symbol *sym)
575 struct sym_scope *s = cfg_alloc(sizeof(struct sym_scope));
577 s->next = conf_this_scope;
584 * cf_pop_scope - leave a scope
586 * cf_pop_scope() pops the topmost scope from the scope stack,
587 * leaving all its symbols in the symbol table, but making them
588 * invisible to the rest of the config.
593 conf_this_scope->active = 0;
594 conf_this_scope = conf_this_scope->next;
595 ASSERT(conf_this_scope);
599 cf_walk_symbols(struct config *cf, struct symbol *sym, int *pos)
605 if (*pos >= SYM_HASH_SIZE)
607 sym = cf->sym_hash[(*pos)++];
611 if (sym && sym->scope->active)
617 * cf_symbol_class_name - get name of a symbol class
620 * This function returns a string representing the class
621 * of the given symbol.
624 cf_symbol_class_name(struct symbol *sym)
633 return "numeric constant";
639 return "routing table";
641 return "network address";
643 return "protocol template";
647 return "unknown type";
655 * Both the configuration and CLI commands are analyzed using a syntax
656 * driven parser generated by the |bison| tool from a grammar which
657 * is constructed from information gathered from grammar snippets by
658 * the |gen_parser.m4| script.
660 * Grammar snippets are files (usually with extension |.Y|) contributed
661 * by various BIRD modules in order to provide information about syntax of their
662 * configuration and their CLI commands. Each snipped consists of several
663 * sections, each of them starting with a special keyword: |CF_HDR| for
664 * a list of |#include| directives needed by the C code, |CF_DEFINES|
665 * for a list of C declarations, |CF_DECLS| for |bison| declarations
666 * including keyword definitions specified as |CF_KEYWORDS|, |CF_GRAMMAR|
667 * for the grammar rules, |CF_CODE| for auxiliary C code and finally
668 * |CF_END| at the end of the snippet.
670 * To create references between the snippets, it's possible to define
671 * multi-part rules by utilizing the |CF_ADDTO| macro which adds a new
672 * alternative to a multi-part rule.
674 * CLI commands are defined using a |CF_CLI| macro. Its parameters are:
675 * the list of keywords determining the command, the list of parameters,
676 * help text for the parameters and help text for the command.
678 * Values of |enum| filter types can be defined using |CF_ENUM| with
679 * the following parameters: name of filter type, prefix common for all
680 * literals of this type and names of all the possible values.