bird/conf/cf-lex.l

575 lines
13 KiB
Text
Raw Normal View History

1998-11-28 03:35:50 +08:00
/*
* BIRD -- Configuration Lexer
*
* (c) 1998--2000 Martin Mares <mj@ucw.cz>
1998-11-28 03:35:50 +08:00
*
* Can be freely distributed and used under the terms of the GNU GPL.
*/
2000-06-04 02:23:00 +08:00
/**
2000-06-07 20:29:08 +08:00
* DOC: Lexical analyzer
2000-06-04 02:23:00 +08:00
*
2000-06-07 20:29:08 +08:00
* The lexical analyzer used for configuration files and CLI commands
2000-06-07 21:25:53 +08:00
* is generated using the |flex| tool accompanied by a couple of
2000-06-04 02:23:00 +08:00
* functions maintaining the hash tables containing information about
* symbols and keywords.
*
* Each symbol is represented by a &symbol structure containing name
2000-06-07 21:25:53 +08:00
* of the symbol, its lexical scope, symbol class (%SYM_PROTO for a name of a protocol,
2000-06-04 02:23:00 +08:00
* %SYM_NUMBER for a numeric constant etc.) and class dependent data.
* When an unknown symbol is encountered, it's automatically added to the
* symbol table with class %SYM_VOID.
*
* The keyword tables are generated from the grammar templates
* using the |gen_keywords.m4| script.
*/
1998-11-28 03:35:50 +08:00
%{
#undef REJECT /* Avoid name clashes */
1998-11-28 03:35:50 +08:00
#include <errno.h>
#include <stdlib.h>
#include <stdarg.h>
2011-11-10 16:22:20 +08:00
#include <unistd.h>
1998-11-28 03:35:50 +08:00
#define PARSER 1
1998-11-28 03:35:50 +08:00
#include "nest/bird.h"
1999-11-15 19:35:41 +08:00
#include "nest/route.h"
#include "nest/protocol.h"
1999-11-15 19:35:41 +08:00
#include "filter/filter.h"
1998-11-28 03:35:50 +08:00
#include "conf/conf.h"
#include "conf/cf-parse.tab.h"
#include "lib/string.h"
1998-11-28 03:35:50 +08:00
struct keyword {
1998-11-28 03:35:50 +08:00
byte *name;
int value;
struct keyword *next;
};
#include "conf/keywords.h"
1998-11-28 03:35:50 +08:00
#define KW_HASH_SIZE 64
static struct keyword *kw_hash[KW_HASH_SIZE];
static int kw_hash_inited;
1998-11-28 03:35:50 +08:00
#define SYM_HASH_SIZE 128
#define SYM_MAX_LEN 32
1999-11-04 21:51:52 +08:00
struct sym_scope {
struct sym_scope *next; /* Next on scope stack */
struct symbol *name; /* Name of this scope */
int active; /* Currently entered */
};
static struct sym_scope *conf_this_scope;
2011-09-12 03:21:47 +08:00
#define MAX_INCLUDE_DEPTH 5
static struct include_file_stack *ifs_head;
static int ifs_depth;
1998-11-28 03:35:50 +08:00
static int cf_hash(byte *c);
static struct symbol *cf_find_sym(byte *c, unsigned int h0);
linpool *cfg_mem;
1998-11-28 03:35:50 +08:00
2011-09-12 03:21:47 +08:00
int (*cf_read_hook)(byte *buf, unsigned int max, int fd);
int (*cf_open_hook)(char *filename);
2011-10-10 07:01:58 +08:00
struct include_file_stack *ifs;
1998-11-28 03:35:50 +08:00
2011-09-12 03:21:47 +08:00
#define YY_INPUT(buf,result,max) result = cf_read_hook(buf, max, ifs->conf_fd);
1998-11-28 03:35:50 +08:00
#define YY_NO_UNPUT
#define YY_FATAL_ERROR(msg) cf_error(msg)
2011-09-12 03:21:47 +08:00
static void new_include(void);
static int check_eof(void);
static struct include_file_stack *new_stack(struct include_file_stack *old);
1998-11-28 03:35:50 +08:00
%}
%option noyywrap
%option noinput
%option nounput
%option noreject
1998-11-28 03:35:50 +08:00
%x COMMENT CCOMM CLI
1998-11-28 03:35:50 +08:00
ALPHA [a-zA-Z_]
DIGIT [0-9]
XIGIT [0-9a-fA-F]
ALNUM [a-zA-Z_0-9]
WHITE [ \t]
2011-09-12 03:21:47 +08:00
include ^{WHITE}*include{WHITE}*\".*\"{WHITE}*;
1998-11-28 03:35:50 +08:00
%%
2011-09-12 03:21:47 +08:00
{include} { if(cf_open_hook) new_include(); }
1998-11-28 03:35:50 +08:00
{DIGIT}+\.{DIGIT}+\.{DIGIT}+\.{DIGIT}+ {
#ifdef IPV6
if (ipv4_pton_u32(yytext, &cf_lval.i32))
return RTRID;
cf_error("Invalid IPv4 address %s", yytext);
#else
1998-11-28 03:35:50 +08:00
if (ip_pton(yytext, &cf_lval.a))
return IPA;
cf_error("Invalid IP address %s", yytext);
#endif
}
({XIGIT}*::|({XIGIT}*:){3,})({XIGIT}*|{DIGIT}+\.{DIGIT}+\.{DIGIT}+\.{DIGIT}+) {
#ifdef IPV6
if (ip_pton(yytext, &cf_lval.a))
return IPA;
cf_error("Invalid IP address %s", yytext);
#else
cf_error("This is an IPv4 router, therefore IPv6 addresses are not supported");
#endif
1998-11-28 03:35:50 +08:00
}
0x{XIGIT}+ {
1998-11-28 03:35:50 +08:00
char *e;
unsigned long int l;
1998-11-28 03:35:50 +08:00
errno = 0;
l = strtoul(yytext+2, &e, 16);
if (e && *e || errno == ERANGE || (unsigned long int)(unsigned int) l != l)
1998-11-28 03:35:50 +08:00
cf_error("Number out of range");
cf_lval.i = l;
return NUM;
}
{DIGIT}+ {
char *e;
unsigned long int l;
1998-11-28 03:35:50 +08:00
errno = 0;
l = strtoul(yytext, &e, 10);
if (e && *e || errno == ERANGE || (unsigned long int)(unsigned int) l != l)
1998-11-28 03:35:50 +08:00
cf_error("Number out of range");
cf_lval.i = l;
return NUM;
}
else: {
/* Hack to distinguish if..else from else: in case */
return ELSECOL;
}
2010-02-10 19:30:14 +08:00
({ALPHA}{ALNUM}*|[']({ALNUM}|[-])*[']) {
if(*yytext == '\'') {
yytext[yyleng-1] = 0;
yytext++;
}
1998-11-28 03:35:50 +08:00
unsigned int h = cf_hash(yytext);
struct keyword *k = kw_hash[h & (KW_HASH_SIZE-1)];
while (k)
{
if (!strcmp(k->name, yytext))
1999-11-15 19:35:41 +08:00
{
if (k->value > 0)
return k->value;
else
{
cf_lval.i = -k->value;
return ENUM;
}
}
1998-11-28 03:35:50 +08:00
k=k->next;
}
cf_lval.s = cf_find_sym(yytext, h);
return SYM;
}
<CLI>(.|\n) {
BEGIN(INITIAL);
return CLI_MARKER;
}
\.\. {
return DDOT;
}
2009-01-28 00:35:00 +08:00
[={}:;,.()+*/%<>~\[\]?!\|-] {
1998-11-28 03:35:50 +08:00
return yytext[0];
}
["][^"\n]*["] {
yytext[yyleng-1] = 0;
cf_lval.t = cfg_strdup(yytext+1);
1998-11-28 03:35:50 +08:00
return TEXT;
}
["][^"\n]*\n cf_error("Unterminated string");
2011-09-12 03:21:47 +08:00
<INITIAL,COMMENT><<EOF>> { if(check_eof()) return END; }
1998-11-28 03:35:50 +08:00
{WHITE}+
2011-09-12 03:21:47 +08:00
\n ifs->conf_lino++;
1998-11-28 03:35:50 +08:00
# BEGIN(COMMENT);
1998-11-28 03:35:50 +08:00
\/\* BEGIN(CCOMM);
1998-11-28 03:35:50 +08:00
. cf_error("Unknown character");
<COMMENT>\n {
2011-09-12 03:21:47 +08:00
ifs->conf_lino++;
1998-11-28 03:35:50 +08:00
BEGIN(INITIAL);
}
<COMMENT>.
<CCOMM>\*\/ BEGIN(INITIAL);
2011-09-12 03:21:47 +08:00
<CCOMM>\n ifs->conf_lino++;
1998-11-28 03:35:50 +08:00
<CCOMM>\/\* cf_error("Comment nesting not supported");
<CCOMM><<EOF>> cf_error("Unterminated comment");
<CCOMM>.
2000-03-11 04:21:12 +08:00
\!\= return NEQ;
\<\= return LEQ;
\>\= return GEQ;
2000-06-01 16:43:29 +08:00
\&\& return AND;
\|\| return OR;
2000-03-11 04:21:12 +08:00
2009-03-14 19:43:10 +08:00
\[\= return PO;
\=\] return PC;
1998-11-28 03:35:50 +08:00
%%
static int
cf_hash(byte *c)
{
unsigned int h = 13;
while (*c)
h = (h * 37) + *c++;
return h;
}
2011-09-12 03:21:47 +08:00
/* Open included file with properly swapped buffers */
static void
new_include(void)
{
char *fname, *p = NULL;
if ((fname = strchr(yytext, '"')) != NULL) {
if ((p = strchr(++fname, '"')) != NULL) *p = '\0';
if (ifs_depth >= MAX_INCLUDE_DEPTH)
cf_error("Max include depth reached.");
/* Save current stack */
ifs->stack = YY_CURRENT_BUFFER;
/* Prepare new stack */
ifs->next = new_stack(ifs);
ifs = ifs->next;
strcpy(ifs->conf_fname, fname); /* XXX: strlcpy should be here */
ifs->conf_fd = cf_open_hook(fname);
yy_switch_to_buffer(yy_create_buffer(yyin, YY_BUF_SIZE));
}
}
static int
check_eof(void)
{
if (ifs == ifs_head) {
/* EOF in main config file */
ifs->conf_lino = 1;
return 1;
}
ifs_depth--;
close(ifs->conf_fd);
ifs = ifs->prev;
ifs->next = NULL;
yy_delete_buffer(YY_CURRENT_BUFFER);
yy_switch_to_buffer(ifs->stack);
return 0;
}
static struct symbol *
cf_new_sym(byte *c, unsigned int h)
{
struct symbol *s, **ht;
int l;
if (!new_config->sym_hash)
new_config->sym_hash = cfg_allocz(SYM_HASH_SIZE * sizeof(struct keyword *));
ht = new_config->sym_hash;
l = strlen(c);
if (l > SYM_MAX_LEN)
cf_error("Symbol too long");
s = cfg_alloc(sizeof(struct symbol) + l);
s->next = ht[h];
ht[h] = s;
s->scope = conf_this_scope;
s->class = SYM_VOID;
s->def = NULL;
s->aux = 0;
strcpy(s->name, c);
return s;
}
1998-11-28 03:35:50 +08:00
static struct symbol *
cf_find_sym(byte *c, unsigned int h0)
{
unsigned int h = h0 & (SYM_HASH_SIZE-1);
struct symbol *s, **ht;
1998-11-28 03:35:50 +08:00
if (ht = new_config->sym_hash)
{
for(s = ht[h]; s; s=s->next)
if (!strcmp(s->name, c) && s->scope->active)
return s;
}
if (new_config->sym_fallback)
{
/* We know only top-level scope is active */
for(s = new_config->sym_fallback[h]; s; s=s->next)
1999-11-04 21:51:52 +08:00
if (!strcmp(s->name, c) && s->scope->active)
return s;
}
return cf_new_sym(c, h);
1998-11-28 03:35:50 +08:00
}
2000-06-04 02:23:00 +08:00
/**
* cf_find_symbol - find a symbol by name
* @c: symbol name
*
* This functions searches the symbol table for a symbol of given
* name. First it examines the current scope, then the second recent
* one and so on until it either finds the symbol and returns a pointer
* to its &symbol structure or reaches the end of the scope chain
* and returns %NULL to signify no match.
*/
struct symbol *
cf_find_symbol(byte *c)
{
return cf_find_sym(c, cf_hash(c));
}
1998-11-28 05:07:02 +08:00
struct symbol *
cf_default_name(char *template, int *counter)
1998-11-28 05:07:02 +08:00
{
char buf[32];
struct symbol *s;
char *perc = strchr(template, '%');
1998-11-28 05:07:02 +08:00
for(;;)
1998-11-28 05:07:02 +08:00
{
bsprintf(buf, template, ++(*counter));
1998-11-28 05:07:02 +08:00
s = cf_find_sym(buf, cf_hash(buf));
if (!s)
break;
if (s->class == SYM_VOID)
return s;
if (!perc)
break;
1998-11-28 05:07:02 +08:00
}
cf_error("Unable to generate default name");
1998-11-28 05:07:02 +08:00
}
2000-06-04 02:23:00 +08:00
/**
* cf_define_symbol - define meaning of a symbol
* @sym: symbol to be defined
* @type: symbol class to assign
* @def: class dependent data
*
* Defines new meaning of a symbol. If the symbol is an undefined
* one (%SYM_VOID), it's just re-defined to the new type. If it's defined
* in different scope, a new symbol in current scope is created and the
* meaning is assigned to it. If it's already defined in the current scope,
* an error is reported via cf_error().
*
* Result: Pointer to the newly defined symbol. If we are in the top-level
* scope, it's the same @sym as passed to the function.
2000-06-04 02:23:00 +08:00
*/
struct symbol *
cf_define_symbol(struct symbol *sym, int type, void *def)
{
if (sym->class)
{
if (sym->scope == conf_this_scope)
cf_error("Symbol already defined");
sym = cf_new_sym(sym->name, cf_hash(sym->name) & (SYM_HASH_SIZE-1));
}
sym->class = type;
sym->def = def;
return sym;
}
static void
cf_lex_init_kh(void)
{
struct keyword *k;
for(k=keyword_list; k->name; k++)
{
unsigned h = cf_hash(k->name) & (KW_HASH_SIZE-1);
k->next = kw_hash[h];
kw_hash[h] = k;
}
kw_hash_inited = 1;
}
2011-09-12 03:21:47 +08:00
static struct include_file_stack *
new_stack(struct include_file_stack *old)
{
struct include_file_stack *ret;
ret = cfg_allocz(sizeof(struct include_file_stack));
ret->conf_lino = 1;
ret->prev = old;
return ret;
}
2000-06-04 02:23:00 +08:00
/**
* cf_lex_init - initialize the lexer
* @is_cli: true if we're going to parse CLI command, false for configuration
*
2000-06-07 20:29:08 +08:00
* cf_lex_init() initializes the lexical analyzer and prepares it for
2000-06-04 02:23:00 +08:00
* parsing of a new input.
*/
1998-11-28 03:35:50 +08:00
void
2011-09-12 03:21:47 +08:00
cf_lex_init(int is_cli, struct config *c)
1998-11-28 03:35:50 +08:00
{
if (!kw_hash_inited)
cf_lex_init_kh();
2011-09-12 03:21:47 +08:00
ifs_head = new_stack(NULL);
ifs = ifs_head;
ifs_depth = 0;
if (!is_cli) {
ifs->conf_fd = c->file_fd;
ifs_depth = 1;
strcpy(ifs->conf_fname, c->file_name);
}
yyrestart(NULL);
if (is_cli)
BEGIN(CLI);
else
BEGIN(INITIAL);
1999-11-04 21:51:52 +08:00
conf_this_scope = cfg_allocz(sizeof(struct sym_scope));
conf_this_scope->active = 1;
1998-11-28 03:35:50 +08:00
}
2000-06-04 02:23:00 +08:00
/**
* cf_push_scope - enter new scope
* @sym: symbol representing scope name
*
* If we want to enter a new scope to process declarations inside
* a nested block, we can just call cf_push_scope() to push a new
* scope onto the scope stack which will cause all new symbols to be
* defined in this scope and all existing symbols to be sought for
* in all scopes stored on the stack.
*/
1999-11-04 21:51:52 +08:00
void
cf_push_scope(struct symbol *sym)
{
struct sym_scope *s = cfg_alloc(sizeof(struct sym_scope));
s->next = conf_this_scope;
conf_this_scope = s;
s->active = 1;
s->name = sym;
}
2000-06-04 02:23:00 +08:00
/**
* cf_pop_scope - leave a scope
*
* cf_pop_scope() pops the topmost scope from the scope stack,
* leaving all its symbols in the symbol table, but making them
* invisible to the rest of the config.
*/
1999-11-04 21:51:52 +08:00
void
cf_pop_scope(void)
{
conf_this_scope->active = 0;
conf_this_scope = conf_this_scope->next;
ASSERT(conf_this_scope);
}
struct symbol *
cf_walk_symbols(struct config *cf, struct symbol *sym, int *pos)
{
for(;;)
{
if (!sym)
{
if (*pos >= SYM_HASH_SIZE)
return NULL;
sym = cf->sym_hash[(*pos)++];
}
else
sym = sym->next;
if (sym && sym->scope->active)
return sym;
}
}
2000-06-04 02:23:00 +08:00
/**
* cf_symbol_class_name - get name of a symbol class
* @sym: symbol
*
* This function returns a string representing the class
* of the given symbol.
*/
char *
cf_symbol_class_name(struct symbol *sym)
{
switch (sym->class)
{
case SYM_VOID:
return "undefined";
case SYM_PROTO:
return "protocol";
case SYM_NUMBER:
return "numeric constant";
case SYM_FUNCTION:
return "function";
case SYM_FILTER:
return "filter";
case SYM_TABLE:
return "routing table";
case SYM_IPA:
return "network address";
case SYM_TEMPLATE:
return "protocol template";
default:
return "unknown type";
}
}
/**
* DOC: Parser
*
2000-06-07 20:29:08 +08:00
* Both the configuration and CLI commands are analyzed using a syntax
* driven parser generated by the |bison| tool from a grammar which
* is constructed from information gathered from grammar snippets by
* the |gen_parser.m4| script.
*
* Grammar snippets are files (usually with extension |.Y|) contributed
2000-06-07 21:25:53 +08:00
* by various BIRD modules in order to provide information about syntax of their
* configuration and their CLI commands. Each snipped consists of several
2000-06-08 20:37:21 +08:00
* sections, each of them starting with a special keyword: |CF_HDR| for
* a list of |#include| directives needed by the C code, |CF_DEFINES|
* for a list of C declarations, |CF_DECLS| for |bison| declarations
* including keyword definitions specified as |CF_KEYWORDS|, |CF_GRAMMAR|
2000-06-07 20:29:08 +08:00
* for the grammar rules, |CF_CODE| for auxiliary C code and finally
* |CF_END| at the end of the snippet.
*
* To create references between the snippets, it's possible to define
* multi-part rules by utilizing the |CF_ADDTO| macro which adds a new
* alternative to a multi-part rule.
*
* CLI commands are defined using a |CF_CLI| macro. Its parameters are:
2000-06-07 20:29:08 +08:00
* the list of keywords determining the command, the list of parameters,
* help text for the parameters and help text for the command.
*
* Values of |enum| filter types can be defined using |CF_ENUM| with
* the following parameters: name of filter type, prefix common for all
2000-06-08 20:37:21 +08:00
* literals of this type and names of all the possible values.
*/