cf-lex.l 11 KB
Newer Older
Martin Mareš's avatar
Martin Mareš committed
1 2 3
/*
 *	BIRD -- Configuration Lexer
 *
4
 *	(c) 1998--2000 Martin Mares <mj@ucw.cz>
Martin Mareš's avatar
Martin Mareš committed
5 6 7 8
 *
 *	Can be freely distributed and used under the terms of the GNU GPL.
 */

Martin Mareš's avatar
Martin Mareš committed
9
/**
10
 * DOC: Lexical analyzer
Martin Mareš's avatar
Martin Mareš committed
11
 *
12
 * The lexical analyzer used for configuration files and CLI commands
Martin Mareš's avatar
Martin Mareš committed
13
 * is generated using the |flex| tool accompanied by a couple of
Martin Mareš's avatar
Martin Mareš committed
14 15 16 17
 * functions maintaining the hash tables containing information about
 * symbols and keywords.
 *
 * Each symbol is represented by a &symbol structure containing name
Martin Mareš's avatar
Martin Mareš committed
18
 * of the symbol, its lexical scope, symbol class (%SYM_PROTO for a name of a protocol,
Martin Mareš's avatar
Martin Mareš committed
19 20 21 22 23 24 25 26
 * %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.
 */

Martin Mareš's avatar
Martin Mareš committed
27
%{
28
#undef REJECT     /* Avoid name clashes */
Martin Mareš's avatar
Martin Mareš committed
29 30 31

#include <errno.h>
#include <stdlib.h>
32
#include <stdarg.h>
Martin Mareš's avatar
Martin Mareš committed
33

34 35
#define PARSER 1

Martin Mareš's avatar
Martin Mareš committed
36
#include "nest/bird.h"
Martin Mareš's avatar
Martin Mareš committed
37
#include "nest/route.h"
38
#include "nest/protocol.h"
Martin Mareš's avatar
Martin Mareš committed
39
#include "filter/filter.h"
Martin Mareš's avatar
Martin Mareš committed
40 41
#include "conf/conf.h"
#include "conf/cf-parse.tab.h"
42
#include "lib/string.h"
Martin Mareš's avatar
Martin Mareš committed
43

44
struct keyword {
Martin Mareš's avatar
Martin Mareš committed
45 46 47
  byte *name;
  int value;
  struct keyword *next;
48 49
};

50
#include "conf/keywords.h"
Martin Mareš's avatar
Martin Mareš committed
51 52

#define KW_HASH_SIZE 64
53 54 55
static struct keyword *kw_hash[KW_HASH_SIZE];
static int kw_hash_inited;

Martin Mareš's avatar
Martin Mareš committed
56 57 58
#define SYM_HASH_SIZE 128
#define SYM_MAX_LEN 32

Martin Mareš's avatar
Martin Mareš committed
59 60 61 62 63 64 65
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;

66 67
int conf_lino;

Martin Mareš's avatar
Martin Mareš committed
68 69 70
static int cf_hash(byte *c);
static struct symbol *cf_find_sym(byte *c, unsigned int h0);

71
linpool *cfg_mem;
Martin Mareš's avatar
Martin Mareš committed
72 73 74 75 76 77 78 79 80 81

int (*cf_read_hook)(byte *buf, unsigned int max);

#define YY_INPUT(buf,result,max) result = cf_read_hook(buf, max);
#define YY_NO_UNPUT
#define YY_FATAL_ERROR(msg) cf_error(msg)

%}

%option noyywrap
82 83 84
%option noinput
%option nounput
%option noreject
Martin Mareš's avatar
Martin Mareš committed
85

86
%x COMMENT CCOMM CLI
Martin Mareš's avatar
Martin Mareš committed
87 88 89 90 91 92 93 94 95 96

ALPHA [a-zA-Z_]
DIGIT [0-9]
XIGIT [0-9a-fA-F]
ALNUM [a-zA-Z_0-9]
WHITE [ \t]

%%

{DIGIT}+\.{DIGIT}+\.{DIGIT}+\.{DIGIT}+ {
97 98 99 100 101
#ifdef IPV6
  if (ipv4_pton_u32(yytext, &cf_lval.i32))
    return RTRID;
  cf_error("Invalid IPv4 address %s", yytext);
#else
Martin Mareš's avatar
Martin Mareš committed
102 103
  if (ip_pton(yytext, &cf_lval.a))
    return IPA;
104 105 106 107 108 109 110 111 112 113 114 115
  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
Martin Mareš's avatar
Martin Mareš committed
116 117
}

118
0x{XIGIT}+ {
Martin Mareš's avatar
Martin Mareš committed
119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139
  char *e;
  long int l;
  errno = 0;
  l = strtoul(yytext+2, &e, 16);
  if (e && *e || errno == ERANGE || (long int)(int) l != l)
    cf_error("Number out of range");
  cf_lval.i = l;
  return NUM;
}

{DIGIT}+ {
  char *e;
  long int l;
  errno = 0;
  l = strtoul(yytext, &e, 10);
  if (e && *e || errno == ERANGE || (long int)(int) l != l)
    cf_error("Number out of range");
  cf_lval.i = l;
  return NUM;
}

140 141 142 143 144
({ALPHA}{ALNUM}*|[']({ALNUM}|[-])*[']) {
  if(*yytext == '\'') {
    yytext[yyleng-1] = 0;
    yytext++;
  }
Martin Mareš's avatar
Martin Mareš committed
145 146 147 148 149
  unsigned int h = cf_hash(yytext);
  struct keyword *k = kw_hash[h & (KW_HASH_SIZE-1)];
  while (k)
    {
      if (!strcmp(k->name, yytext))
Martin Mareš's avatar
Martin Mareš committed
150 151 152 153 154 155 156 157 158
	{
	  if (k->value > 0)
	    return k->value;
	  else
	    {
	      cf_lval.i = -k->value;
	      return ENUM;
	    }
	}
Martin Mareš's avatar
Martin Mareš committed
159 160 161 162 163 164
      k=k->next;
    }
  cf_lval.s = cf_find_sym(yytext, h);
  return SYM;
}

165
<CLI>(.|\n) {
166 167 168 169
  BEGIN(INITIAL);
  return CLI_MARKER;
}

170
[={}:;,.()+*/%<>~\[\]?!\|-] {
Martin Mareš's avatar
Martin Mareš committed
171 172 173 174
  return yytext[0];
}

["][^"\n]*["] {
175
  yytext[yyleng-1] = 0;
176
  cf_lval.t = cfg_strdup(yytext+1);
Martin Mareš's avatar
Martin Mareš committed
177 178 179 180 181 182 183 184 185
  return TEXT;
}

["][^"\n]*\n	cf_error("Unterminated string");

<INITIAL,COMMENT><<EOF>>	return END;

{WHITE}+

186
\n	conf_lino++;
Martin Mareš's avatar
Martin Mareš committed
187

188
#	BEGIN(COMMENT);
Martin Mareš's avatar
Martin Mareš committed
189

190
\/\*	BEGIN(CCOMM);
Martin Mareš's avatar
Martin Mareš committed
191 192 193 194

.	cf_error("Unknown character");

<COMMENT>\n {
195
  conf_lino++;
Martin Mareš's avatar
Martin Mareš committed
196 197 198 199 200 201
  BEGIN(INITIAL);
}

<COMMENT>.

<CCOMM>\*\/	BEGIN(INITIAL);
202
<CCOMM>\n	conf_lino++;
Martin Mareš's avatar
Martin Mareš committed
203 204 205 206
<CCOMM>\/\*	cf_error("Comment nesting not supported");
<CCOMM><<EOF>>	cf_error("Unterminated comment");
<CCOMM>.

Pavel Machek's avatar
Pavel Machek committed
207 208 209
\!\= return NEQ;
\<\= return LEQ;
\>\= return GEQ;
Pavel Machek's avatar
Pavel Machek committed
210 211
\&\& return AND;
\|\| return OR;
Pavel Machek's avatar
Pavel Machek committed
212

213 214 215
\[\= return PO;
\=\] return PC;

Martin Mareš's avatar
Martin Mareš committed
216 217 218 219 220 221 222 223 224 225 226 227
%%

static int
cf_hash(byte *c)
{
  unsigned int h = 13;

  while (*c)
    h = (h * 37) + *c++;
  return h;
}

228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250
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;
}

Martin Mareš's avatar
Martin Mareš committed
251 252 253 254
static struct symbol *
cf_find_sym(byte *c, unsigned int h0)
{
  unsigned int h = h0 & (SYM_HASH_SIZE-1);
255
  struct symbol *s, **ht;
Martin Mareš's avatar
Martin Mareš committed
256

257 258 259 260 261 262 263 264 265 266
  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)
Martin Mareš's avatar
Martin Mareš committed
267
	if (!strcmp(s->name, c) && s->scope->active)
268
	  return s;
269
    }
270
  return cf_new_sym(c, h);
Martin Mareš's avatar
Martin Mareš committed
271 272
}

Martin Mareš's avatar
Martin Mareš committed
273 274 275 276 277 278 279 280 281 282
/**
 * 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.
 */
283 284 285 286 287 288
struct symbol *
cf_find_symbol(byte *c)
{
  return cf_find_sym(c, cf_hash(c));
}

289
struct symbol *
290
cf_default_name(char *template, int *counter)
291 292 293
{
  char buf[32];
  struct symbol *s;
294
  char *perc = strchr(template, '%');
295

296
  for(;;)
297
    {
298
      bsprintf(buf, template, ++(*counter));
299
      s = cf_find_sym(buf, cf_hash(buf));
300 301 302 303 304 305
      if (!s)
	break;
      if (s->class == SYM_VOID)
	return s;
      if (!perc)
	break;
306
    }
307
  cf_error("Unable to generate default name");
308 309
}

Martin Mareš's avatar
Martin Mareš committed
310 311 312 313 314 315
/**
 * cf_define_symbol - define meaning of a symbol
 * @sym: symbol to be defined
 * @type: symbol class to assign
 * @def: class dependent data
 *
316 317 318 319 320 321 322 323
 * 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.
Martin Mareš's avatar
Martin Mareš committed
324
 */
325
struct symbol *
326 327 328
cf_define_symbol(struct symbol *sym, int type, void *def)
{
  if (sym->class)
329 330 331 332 333
    {
      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));
    }
334 335
  sym->class = type;
  sym->def = def;
336
  return sym;
337 338
}

339 340 341 342 343 344 345 346 347 348 349 350 351 352
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;
}

Martin Mareš's avatar
Martin Mareš committed
353 354 355 356
/**
 * cf_lex_init - initialize the lexer
 * @is_cli: true if we're going to parse CLI command, false for configuration
 *
357
 * cf_lex_init() initializes the lexical analyzer and prepares it for
Martin Mareš's avatar
Martin Mareš committed
358 359
 * parsing of a new input.
 */
Martin Mareš's avatar
Martin Mareš committed
360
void
361
cf_lex_init(int is_cli)
Martin Mareš's avatar
Martin Mareš committed
362
{
363 364
  if (!kw_hash_inited)
    cf_lex_init_kh();
365
  conf_lino = 1;
366 367 368 369 370
  yyrestart(NULL);
  if (is_cli)
    BEGIN(CLI);
  else
    BEGIN(INITIAL);
Martin Mareš's avatar
Martin Mareš committed
371 372
  conf_this_scope = cfg_allocz(sizeof(struct sym_scope));
  conf_this_scope->active = 1;
Martin Mareš's avatar
Martin Mareš committed
373 374
}

Martin Mareš's avatar
Martin Mareš committed
375 376 377 378 379 380 381 382 383 384
/**
 * 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.
 */
Martin Mareš's avatar
Martin Mareš committed
385 386 387 388 389 390 391 392 393 394 395
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;
}

Martin Mareš's avatar
Martin Mareš committed
396 397 398 399 400 401 402
/**
 * 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.
 */
Martin Mareš's avatar
Martin Mareš committed
403 404 405 406 407 408 409
void
cf_pop_scope(void)
{
  conf_this_scope->active = 0;
  conf_this_scope = conf_this_scope->next;
  ASSERT(conf_this_scope);
}
410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428

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;
    }
}

Martin Mareš's avatar
Martin Mareš committed
429 430 431 432 433 434 435
/**
 * cf_symbol_class_name - get name of a symbol class
 * @sym: symbol
 *
 * This function returns a string representing the class
 * of the given symbol.
 */
436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452
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";
453 454
    case SYM_IPA:
      return "network address";
455 456 457 458
    default:
      return "unknown type";
    }
}
459 460 461 462 463


/**
 * DOC: Parser
 *
464
 * Both the configuration and CLI commands are analyzed using a syntax
465 466 467 468 469
 * 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
Martin Mareš's avatar
Martin Mareš committed
470
 * by various BIRD modules in order to provide information about syntax of their
471
 * configuration and their CLI commands. Each snipped consists of several
472
 * sections, each of them starting with a special keyword: |CF_HDR| for
473 474 475
 * 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|
476
 * for the grammar rules, |CF_CODE| for auxiliary C code and finally
477 478 479 480 481 482 483
 * |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:
484
 * the list of keywords determining the command, the list of parameters,
485 486 487 488
 * 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
489
 * literals of this type and names of all the possible values.
490
 */