#include "lib/string.h"
#include "lib/hash.h"
-struct keyword {
- byte *name;
- int value;
- struct keyword *next;
-};
-
#include "conf/keywords.h"
/* Could be defined by Bison in cf-parse.tab.h, inteferes with SYM hash */
* for purposes of cf_define_symbol().
*/
struct symbol *
-cf_localize_symbol(struct symbol *sym)
+cf_localize_symbol(const byte *c)
{
+ struct symbol *sym = cf_find_symbol(new_config, c);
+
/* If the symbol type is void, it has been recently allocated just in this scope. */
- if (!sym->class)
+ if (sym && !sym->class)
return sym;
/* If the scope is the current, it is already defined in this scope. */
- if (sym->scope == conf_this_scope)
+ if (sym && (sym->scope == conf_this_scope))
cf_error("Symbol already defined");
/* Not allocated here yet, doing it now. */
- return cf_new_symbol(sym->name);
+ return cf_new_symbol(c);
}
struct symbol *
struct keyword *k = HASH_FIND(kw_hash, KW, data);
if (k)
{
+ cf_lval.kw = *k;
if (k->value > 0)
return k->value;
else
- {
- cf_lval.i = -k->value;
return ENUM;
- }
}
/* OK, undefined symbol */
struct symbol *cf_get_symbol(const byte *c);
struct symbol *cf_default_name(char *template, int *counter);
-struct symbol *cf_localize_symbol(struct symbol *sym);
+struct symbol *cf_localize_symbol(const byte *c);
/**
* cf_define_symbol - define meaning of a symbol
* 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.
*/
-#define cf_define_symbol(osym_, type_, var_, def_) ({ \
- struct symbol *sym_ = cf_localize_symbol(osym_); \
+#define cf_define_symbol(name_, type_, var_, def_) ({ \
+ struct symbol *sym_ = cf_localize_symbol(name_); \
sym_->class = type_; \
sym_->var_ = def_; \
sym_; })
void cf_pop_scope(void);
char *cf_symbol_class_name(struct symbol *sym);
+struct keyword {
+ byte *name;
+ int value;
+ struct keyword *next;
+};
+
/* Parser */
extern char *cf_text;
#include "nest/cli.h"
#include "filter/filter.h"
+extern struct keyword keyword_list[];
+extern struct sym_scope *conf_this_scope;
+
/* FIXME: Turn on YYERROR_VERBOSE and work around lots of bison bugs? */
CF_DEFINES
struct channel_limit cl;
struct timeformat *tf;
mpls_label_stack *mls;
+ struct keyword kw;
}
%token END CLI_MARKER INVALID_TOKEN ELSECOL DDOT
%token GEQ LEQ NEQ AND OR
%token PO PC
-%token <i> NUM ENUM
+%token <kw> ENUM
+%token <i> NUM
%token <ip4> IP4
%token <ip6> IP6
%token <i64> VPN_RD
%type <net_ptr> net_ net_any net_vpn4_ net_vpn6_ net_vpn_ net_roa4_ net_roa6_ net_roa_ net_ip6_sadr_ net_mpls_
%type <mls> label_stack_start label_stack
-%type <t> text opttext
+%type <t> text opttext token_def
%type <s> symbol
+%type <kw> token keyword
%nonassoc PREFIX_DUMMY
%left AND OR
conf: definition ;
definition:
- DEFINE symbol '=' term ';' {
+ DEFINE token_def '=' term ';' {
struct f_val *val = cfg_alloc(sizeof(struct f_val));
if (f_eval(f_linearize($4), cfg_mem, val) > F_RETURN) cf_error("Runtime error");
cf_define_symbol($2, SYM_CONSTANT | val->type, val, val);
ipa_scope:
/* empty */ { $$ = NULL; }
- | '%' symbol { $$ = if_get_by_name($2->name); }
+ | '%' token { $$ = if_get_by_name($2.name); }
;
| /* empty */ { $$ = NULL; }
;
+token:
+ symbol { $$ = (struct keyword) { .name = $1->name, }; }
+ | keyword { $$ = $1; }
+ | ENUM { $$ = $1; }
+ ;
+
+token_def:
+ symbol { $$ = $1->name; }
+ | keyword { log(L_WARN "Redefining a keyword: \"%s\"", $1.name); $$ = $1.name; }
+ | ENUM { log(L_WARN "Redefining a keyword: \"%s\"", $1.name); $$ = $1.name; }
CF_CODE
# After all configuration templates end, we generate the
m4_m4wrap(`
m4_divert(0)
-static struct keyword keyword_list[] = {
+struct keyword keyword_list[] = {
m4_undivert(1){ NULL, -1, NULL } };
')
m4_define(CF_CODE, `CF_ZONE(4, C Code)')
m4_define(CF_END, `m4_divert(-1)')
-# Simple iterator
-m4_define(CF_itera, `m4_ifelse($#, 1, [[CF_iter($1)]], [[CF_iter($1)[[]]CF_itera(m4_shift($@))]])')
-m4_define(CF_iterate, `m4_define([[CF_iter]], m4_defn([[$1]]))CF_itera($2)')
-
-# Keywords act as untyped %token
-m4_define(CF_keywd, `m4_ifdef([[CF_tok_$1]],,[[m4_define([[CF_tok_$1]],1)m4_define([[CF_toks]],CF_toks $1)]])')
-m4_define(CF_KEYWORDS, `m4_define([[CF_toks]],[[]])CF_iterate([[CF_keywd]], [[$@]])m4_ifelse(CF_toks,,,%token[[]]CF_toks
-)DNL')
+# Keywords act as a %token <kw> + added to a keyword: rule
+m4_define(CF_KEYWORDS, `m4_ifelse($#,1,,[[CF_KEYWORDS(m4_shift($@))]])DNL
+m4_ifdef([[CF_tok_$1]],,[[m4_define([[CF_tok_$1]],1)m4_divert(2)%token <kw> $1
+m4_divert(3)keyword: $1 ;
+m4_divert(2)]])')
# CLI commands
m4_define(CF_CLI, `m4_define([[CF_cmd]], cmd_[[]]m4_translit($1, [[ ]], _))DNL
conf: filter_def ;
filter_def:
- FILTER symbol { $2 = cf_define_symbol($2, SYM_FILTER, filter, NULL); cf_push_scope( $2 ); }
- filter_body {
- struct filter *f = cfg_alloc(sizeof(struct filter));
- *f = (struct filter) { .sym = $2, .root = $4 };
- $2->filter = f;
-
+ FILTER token_def {
+ cf_push_scope( cf_define_symbol($2, SYM_FILTER, filter, NULL) );
+ } filter_body {
cf_pop_scope();
+
+ struct symbol *sym = cf_get_symbol($2);
+ ASSERT(sym->class == SYM_FILTER);
+ struct filter *f = cfg_alloc(sizeof(struct filter));
+ *f = (struct filter) { .sym = sym, .root = $4 };
+ sym->filter = f;
}
;
;
conf: custom_attr ;
-custom_attr: ATTRIBUTE type symbol ';' {
- cf_define_symbol($3, SYM_ATTRIBUTE, attribute, ca_lookup(new_config->pool, $3->name, $2)->fda);
+custom_attr: ATTRIBUTE type token_def ';' {
+ cf_define_symbol($3, SYM_ATTRIBUTE, attribute, ca_lookup(new_config->pool, $3, $2)->fda);
};
conf: bt_test_suite ;
function_argsn:
/* EMPTY */
- | function_argsn type symbol ';' {
- if ($3->scope->slots >= 0xfe) cf_error("Too many declarations, at most 255 allowed");
- cf_define_symbol($3, SYM_VARIABLE | $2, offset, $3->scope->slots++);
+ | function_argsn type token_def ';' {
+ if (conf_this_scope->slots >= 0xfe) cf_error("Too many declarations, at most 255 allowed");
+ cf_define_symbol($3, SYM_VARIABLE | $2, offset, conf_this_scope->slots++);
}
;
function_args:
'(' ')' { $$ = 0; }
- | '(' function_argsn type symbol ')' {
- cf_define_symbol($4, SYM_VARIABLE | $3, offset, $4->scope->slots++);
- $$ = $4->scope->slots;
+ | '(' function_argsn type token_def ')' {
+ cf_define_symbol($4, SYM_VARIABLE | $3, offset, conf_this_scope->slots++);
+ $$ = conf_this_scope->slots;
}
;
function_vars:
/* EMPTY */ { $$ = 0; }
- | function_vars type symbol ';' {
- cf_define_symbol($3, SYM_VARIABLE | $2, offset, $3->scope->slots++);
+ | function_vars type token_def ';' {
+ cf_define_symbol($3, SYM_VARIABLE | $2, offset, conf_this_scope->slots++);
$$ = $1 + 1;
}
;
conf: function_def ;
function_def:
- FUNCTION symbol { DBG( "Beginning of function %s\n", $2->name );
- $2 = cf_define_symbol($2, SYM_FUNCTION, function, NULL);
- cf_push_scope($2);
+ FUNCTION token_def { DBG( "Beginning of function %s\n", $2 );
+ cf_push_scope(cf_define_symbol($2, SYM_FUNCTION, function, NULL));
} function_args function_body {
- DBG("Definition of function %s with %u args and %u local vars.\n", $2->name, $4, $5->vars);
- $5->args = $4;
- $2->function = $5;
cf_pop_scope();
+
+ struct symbol *sym = cf_get_symbol($2);
+ ASSERT(sym->class == SYM_FUNCTION);
+ DBG("Definition of function %s with %u args and %u local vars.\n", $2, $4, $5->vars);
+ $5->args = $4;
+ sym->function = $5;
}
;
NUM { $$.type = T_INT; $$.val.i = $1; }
| fipa { $$ = $1; }
| VPN_RD { $$.type = T_RD; $$.val.ec = $1; }
- | ENUM { $$.type = pair_a($1); $$.val.i = pair_b($1); }
+ | ENUM { $$.type = pair_a(-$1.value); $$.val.i = pair_b(-$1.value); }
| '(' term ')' {
if (f_eval(f_linearize($2), cfg_mem, &($$)) > F_RETURN) cf_error("Runtime error");
if (!f_valid_set_type($$.type)) cf_error("Set-incompatible type");
NUM { $$.type = T_INT; $$.val.i = $1; }
| '(' term ')' { $$.type = T_INT; $$.val.i = f_eval_int(f_linearize($2)); }
| fipa { $$ = $1; }
- | ENUM { $$.type = pair_a($1); $$.val.i = pair_b($1); }
+ | ENUM { $$.type = pair_a(-$1.value); $$.val.i = pair_b(-$1.value); }
;
cnum:
DBG( "ook\n" );
}
| '[' fprefix_set ']' { $$ = f_new_inst(FI_CONSTANT, (struct f_val) { .type = T_PREFIX_SET, .val.ti = $2, }); }
- | ENUM { $$ = f_new_inst(FI_CONSTANT, (struct f_val) { .type = $1 >> 16, .val.i = $1 & 0xffff, }); }
+ | ENUM { $$ = f_new_inst(FI_CONSTANT, (struct f_val) { .type = (-$1.value) >> 16, .val.i = (-$1.value) & 0xffff, }); }
;
constructor:
| SORTED { $$ = 1; }
;
-table: net_type TABLE symbol table_sorted {
+table: net_type TABLE token_def table_sorted {
struct rtable_config *cf;
cf = rt_new_table($3, $1);
cf->sorted = $4;
this_proto->name = s->name;
}
| symbol {
- cf_define_symbol($1, this_proto->class, proto, this_proto);
+ cf_define_symbol($1->name, this_proto->class, proto, this_proto);
this_proto->name = $1->name;
}
| FROM CF_SYM_KNOWN {
| symbol FROM CF_SYM_KNOWN {
if (($3->class != SYM_TEMPLATE) && ($3->class != SYM_PROTO)) cf_error("Template or protocol name expected");
- cf_define_symbol($1, this_proto->class, proto, this_proto);
+ cf_define_symbol($1->name, this_proto->class, proto, this_proto);
this_proto->name = $1->name;
proto_copy_config(this_proto, $3->proto);
struct rtable_config {
node n;
- char *name;
+ const char *name;
struct rtable *table;
struct proto_config *krt_attached; /* Kernel syncer attached to this table */
uint addr_type; /* Type of address data stored in table (NET_*) */
typedef struct rtable {
node n; /* Node in list of all tables */
struct fib fib;
- char *name; /* Name of this table */
+ const char *name; /* Name of this table */
list channels; /* List of attached channels (struct channel) */
uint addr_type; /* Type of address data stored in table (NET_*) */
int pipe_busy; /* Pipe loop detection */
void rt_reload_channel_abort(struct channel *c);
void rt_prune_sync(rtable *t, int all);
int rte_update_out(struct channel *c, const net_addr *n, rte *new, rte *old0, int refeed);
-struct rtable_config *rt_new_table(struct symbol *s, uint addr_type);
+struct rtable_config *rt_new_table(const char *name, uint addr_type);
/* Default limit for ECMP next hops, defined in sysdep code */
{
init_list(&c->tables);
- rt_new_table(cf_get_symbol("master4"), NET_IP4);
- rt_new_table(cf_get_symbol("master6"), NET_IP6);
+ rt_new_table("master4", NET_IP4);
+ rt_new_table("master6", NET_IP6);
}
struct rtable_config *
-rt_new_table(struct symbol *s, uint addr_type)
+rt_new_table(const char *name, uint addr_type)
{
+ struct symbol *s = cf_get_symbol(name);
+
/* Hack that allows to 'redefine' the master table */
if ((s->class == SYM_TABLE) &&
(s->table == new_config->def_tables[addr_type]) &&
struct rtable_config *c = cfg_allocz(sizeof(struct rtable_config));
- cf_define_symbol(s, SYM_TABLE, table, c);
- c->name = s->name;
+ cf_define_symbol(name, SYM_TABLE, table, c);
+ c->name = name;
c->addr_type = addr_type;
c->gc_max_ops = 1000;
c->gc_min_time = 5;
}
static struct rtable_config *
-rt_find_table_config(struct config *cf, char *name)
+rt_find_table_config(struct config *cf, const char *name)
{
struct symbol *sym = cf_find_symbol(cf, name);
return (sym && (sym->class == SYM_TABLE)) ? sym->table : NULL;
#ifdef PATH_IPROUTE_DIR
static inline void
-add_num_const(char *name, int val, const char *file, const uint line)
+add_num_const(const char *name, int val, const char *file, const uint line)
{
struct f_val *v = cfg_alloc(sizeof(struct f_val));
*v = (struct f_val) { .type = T_INT, .val.i = val };
if (sym->class && (sym->scope == conf_this_scope))
cf_error("Error reading value for %s from %s:%d: already defined", name, file, line);
- cf_define_symbol(sym, SYM_CONSTANT | T_INT, val, v);
+ cf_define_symbol(name, SYM_CONSTANT | T_INT, val, v);
}
/* the code of read_iproute_table() is based on