Statistics
| Branch: | Revision:

iof-bird-daemon / conf / cf-lex.l @ b8cc390e

History | View | Annotate | Download (11.1 KB)

1
/*
2
 *	BIRD -- Configuration Lexer
3
 *
4
 *	(c) 1998--2000 Martin Mares <mj@ucw.cz>
5
 *
6
 *	Can be freely distributed and used under the terms of the GNU GPL.
7
 */
8

    
9
/**
10
 * DOC: Lexical analyzer
11
 *
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.
16
 *
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.
22
 *
23
 * The keyword tables are generated from the grammar templates
24
 * using the |gen_keywords.m4| script.
25
 */
26

    
27
%{
28
#undef REJECT     /* Avoid name clashes */
29

    
30
#include <errno.h>
31
#include <stdlib.h>
32
#include <stdarg.h>
33

    
34
#define PARSER 1
35

    
36
#include "nest/bird.h"
37
#include "nest/route.h"
38
#include "nest/protocol.h"
39
#include "filter/filter.h"
40
#include "conf/conf.h"
41
#include "conf/cf-parse.tab.h"
42
#include "lib/string.h"
43

    
44
struct keyword {
45
  byte *name;
46
  int value;
47
  struct keyword *next;
48
};
49

    
50
#include "conf/keywords.h"
51

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

    
56
#define SYM_HASH_SIZE 128
57
#define SYM_MAX_LEN 32
58

    
59
struct sym_scope {
60
  struct sym_scope *next;		/* Next on scope stack */
61
  struct symbol *name;			/* Name of this scope */
62
  int active;				/* Currently entered */
63
};
64
static struct sym_scope *conf_this_scope;
65

    
66
int conf_lino;
67

    
68
static int cf_hash(byte *c);
69
static struct symbol *cf_find_sym(byte *c, unsigned int h0);
70

    
71
linpool *cfg_mem;
72

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

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

    
79
%}
80

    
81
%option noyywrap
82
%option noinput
83
%option nounput
84
%option noreject
85

    
86
%x COMMENT CCOMM CLI
87

    
88
ALPHA [a-zA-Z_]
89
DIGIT [0-9]
90
XIGIT [0-9a-fA-F]
91
ALNUM [a-zA-Z_0-9]
92
WHITE [ \t]
93

    
94
%%
95

    
96
{DIGIT}+\.{DIGIT}+\.{DIGIT}+\.{DIGIT}+ {
97
#ifdef IPV6
98
  if (ipv4_pton_u32(yytext, &cf_lval.i32))
99
    return RTRID;
100
  cf_error("Invalid IPv4 address %s", yytext);
101
#else
102
  if (ip_pton(yytext, &cf_lval.a))
103
    return IPA;
104
  cf_error("Invalid IP address %s", yytext);
105
#endif
106
}
107

    
108
({XIGIT}*::|({XIGIT}*:){3,})({XIGIT}*|{DIGIT}+\.{DIGIT}+\.{DIGIT}+\.{DIGIT}+) {
109
#ifdef IPV6
110
  if (ip_pton(yytext, &cf_lval.a))
111
    return IPA;
112
  cf_error("Invalid IP address %s", yytext);
113
#else
114
  cf_error("This is an IPv4 router, therefore IPv6 addresses are not supported");
115
#endif
116
}
117

    
118
0x{XIGIT}+ {
119
  char *e;
120
  long int l;
121
  errno = 0;
122
  l = strtoul(yytext+2, &e, 16);
123
  if (e && *e || errno == ERANGE || (long int)(int) l != l)
124
    cf_error("Number out of range");
125
  cf_lval.i = l;
126
  return NUM;
127
}
128

    
129
{DIGIT}+ {
130
  char *e;
131
  long int l;
132
  errno = 0;
133
  l = strtoul(yytext, &e, 10);
134
  if (e && *e || errno == ERANGE || (long int)(int) l != l)
135
    cf_error("Number out of range");
136
  cf_lval.i = l;
137
  return NUM;
138
}
139

    
140
else: {
141
  /* Hack to distinguish if..else from else: in case */
142
  return ELSECOL;
143
}
144

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

    
170
<CLI>(.|\n) {
171
  BEGIN(INITIAL);
172
  return CLI_MARKER;
173
}
174

    
175
\.\. {
176
  return DDOT;
177
}
178

    
179
[={}:;,.()+*/%<>~\[\]?!\|-] {
180
  return yytext[0];
181
}
182

    
183
["][^"\n]*["] {
184
  yytext[yyleng-1] = 0;
185
  cf_lval.t = cfg_strdup(yytext+1);
186
  return TEXT;
187
}
188

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

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

    
193
{WHITE}+
194

    
195
\n	conf_lino++;
196

    
197
#	BEGIN(COMMENT);
198

    
199
\/\*	BEGIN(CCOMM);
200

    
201
.	cf_error("Unknown character");
202

    
203
<COMMENT>\n {
204
  conf_lino++;
205
  BEGIN(INITIAL);
206
}
207

    
208
<COMMENT>.
209

    
210
<CCOMM>\*\/	BEGIN(INITIAL);
211
<CCOMM>\n	conf_lino++;
212
<CCOMM>\/\*	cf_error("Comment nesting not supported");
213
<CCOMM><<EOF>>	cf_error("Unterminated comment");
214
<CCOMM>.
215

    
216
\!\= return NEQ;
217
\<\= return LEQ;
218
\>\= return GEQ;
219
\&\& return AND;
220
\|\| return OR;
221

    
222
\[\= return PO;
223
\=\] return PC;
224

    
225
%%
226

    
227
static int
228
cf_hash(byte *c)
229
{
230
  unsigned int h = 13;
231

    
232
  while (*c)
233
    h = (h * 37) + *c++;
234
  return h;
235
}
236

    
237
static struct symbol *
238
cf_new_sym(byte *c, unsigned int h)
239
{
240
  struct symbol *s, **ht;
241
  int l;
242

    
243
  if (!new_config->sym_hash)
244
    new_config->sym_hash = cfg_allocz(SYM_HASH_SIZE * sizeof(struct keyword *));
245
  ht = new_config->sym_hash;
246
  l = strlen(c);
247
  if (l > SYM_MAX_LEN)
248
    cf_error("Symbol too long");
249
  s = cfg_alloc(sizeof(struct symbol) + l);
250
  s->next = ht[h];
251
  ht[h] = s;
252
  s->scope = conf_this_scope;
253
  s->class = SYM_VOID;
254
  s->def = NULL;
255
  s->aux = 0;
256
  strcpy(s->name, c);
257
  return s;
258
}
259

    
260
static struct symbol *
261
cf_find_sym(byte *c, unsigned int h0)
262
{
263
  unsigned int h = h0 & (SYM_HASH_SIZE-1);
264
  struct symbol *s, **ht;
265

    
266
  if (ht = new_config->sym_hash)
267
    {
268
      for(s = ht[h]; s; s=s->next)
269
	if (!strcmp(s->name, c) && s->scope->active)
270
	  return s;
271
    }
272
  if (new_config->sym_fallback)
273
    {
274
      /* We know only top-level scope is active */
275
      for(s = new_config->sym_fallback[h]; s; s=s->next)
276
	if (!strcmp(s->name, c) && s->scope->active)
277
	  return s;
278
    }
279
  return cf_new_sym(c, h);
280
}
281

    
282
/**
283
 * cf_find_symbol - find a symbol by name
284
 * @c: symbol name
285
 *
286
 * This functions searches the symbol table for a symbol of given
287
 * name. First it examines the current scope, then the second recent
288
 * one and so on until it either finds the symbol and returns a pointer
289
 * to its &symbol structure or reaches the end of the scope chain
290
 * and returns %NULL to signify no match.
291
 */
292
struct symbol *
293
cf_find_symbol(byte *c)
294
{
295
  return cf_find_sym(c, cf_hash(c));
296
}
297

    
298
struct symbol *
299
cf_default_name(char *template, int *counter)
300
{
301
  char buf[32];
302
  struct symbol *s;
303
  char *perc = strchr(template, '%');
304

    
305
  for(;;)
306
    {
307
      bsprintf(buf, template, ++(*counter));
308
      s = cf_find_sym(buf, cf_hash(buf));
309
      if (!s)
310
	break;
311
      if (s->class == SYM_VOID)
312
	return s;
313
      if (!perc)
314
	break;
315
    }
316
  cf_error("Unable to generate default name");
317
}
318

    
319
/**
320
 * cf_define_symbol - define meaning of a symbol
321
 * @sym: symbol to be defined
322
 * @type: symbol class to assign
323
 * @def: class dependent data
324
 *
325
 * Defines new meaning of a symbol. If the symbol is an undefined
326
 * one (%SYM_VOID), it's just re-defined to the new type. If it's defined
327
 * in different scope, a new symbol in current scope is created and the
328
 * meaning is assigned to it. If it's already defined in the current scope,
329
 * an error is reported via cf_error().
330
 *
331
 * Result: Pointer to the newly defined symbol. If we are in the top-level
332
 * scope, it's the same @sym as passed to the function.
333
 */
334
struct symbol *
335
cf_define_symbol(struct symbol *sym, int type, void *def)
336
{
337
  if (sym->class)
338
    {
339
      if (sym->scope == conf_this_scope)
340
	cf_error("Symbol already defined");
341
      sym = cf_new_sym(sym->name, cf_hash(sym->name) & (SYM_HASH_SIZE-1));
342
    }
343
  sym->class = type;
344
  sym->def = def;
345
  return sym;
346
}
347

    
348
static void
349
cf_lex_init_kh(void)
350
{
351
  struct keyword *k;
352

    
353
  for(k=keyword_list; k->name; k++)
354
    {
355
      unsigned h = cf_hash(k->name) & (KW_HASH_SIZE-1);
356
      k->next = kw_hash[h];
357
      kw_hash[h] = k;
358
    }
359
  kw_hash_inited = 1;
360
}
361

    
362
/**
363
 * cf_lex_init - initialize the lexer
364
 * @is_cli: true if we're going to parse CLI command, false for configuration
365
 *
366
 * cf_lex_init() initializes the lexical analyzer and prepares it for
367
 * parsing of a new input.
368
 */
369
void
370
cf_lex_init(int is_cli)
371
{
372
  if (!kw_hash_inited)
373
    cf_lex_init_kh();
374
  conf_lino = 1;
375
  yyrestart(NULL);
376
  if (is_cli)
377
    BEGIN(CLI);
378
  else
379
    BEGIN(INITIAL);
380
  conf_this_scope = cfg_allocz(sizeof(struct sym_scope));
381
  conf_this_scope->active = 1;
382
}
383

    
384
/**
385
 * cf_push_scope - enter new scope
386
 * @sym: symbol representing scope name
387
 *
388
 * If we want to enter a new scope to process declarations inside
389
 * a nested block, we can just call cf_push_scope() to push a new
390
 * scope onto the scope stack which will cause all new symbols to be
391
 * defined in this scope and all existing symbols to be sought for
392
 * in all scopes stored on the stack.
393
 */
394
void
395
cf_push_scope(struct symbol *sym)
396
{
397
  struct sym_scope *s = cfg_alloc(sizeof(struct sym_scope));
398

    
399
  s->next = conf_this_scope;
400
  conf_this_scope = s;
401
  s->active = 1;
402
  s->name = sym;
403
}
404

    
405
/**
406
 * cf_pop_scope - leave a scope
407
 *
408
 * cf_pop_scope() pops the topmost scope from the scope stack,
409
 * leaving all its symbols in the symbol table, but making them
410
 * invisible to the rest of the config.
411
 */
412
void
413
cf_pop_scope(void)
414
{
415
  conf_this_scope->active = 0;
416
  conf_this_scope = conf_this_scope->next;
417
  ASSERT(conf_this_scope);
418
}
419

    
420
struct symbol *
421
cf_walk_symbols(struct config *cf, struct symbol *sym, int *pos)
422
{
423
  for(;;)
424
    {
425
      if (!sym)
426
	{
427
	  if (*pos >= SYM_HASH_SIZE)
428
	    return NULL;
429
	  sym = cf->sym_hash[(*pos)++];
430
	}
431
      else
432
	sym = sym->next;
433
      if (sym && sym->scope->active)
434
	return sym;
435
    }
436
}
437

    
438
/**
439
 * cf_symbol_class_name - get name of a symbol class
440
 * @sym: symbol
441
 *
442
 * This function returns a string representing the class
443
 * of the given symbol.
444
 */
445
char *
446
cf_symbol_class_name(struct symbol *sym)
447
{
448
  switch (sym->class)
449
    {
450
    case SYM_VOID:
451
      return "undefined";
452
    case SYM_PROTO:
453
      return "protocol";
454
    case SYM_NUMBER:
455
      return "numeric constant";
456
    case SYM_FUNCTION:
457
      return "function";
458
    case SYM_FILTER:
459
      return "filter";
460
    case SYM_TABLE:
461
      return "routing table";
462
    case SYM_IPA:
463
      return "network address";
464
    default:
465
      return "unknown type";
466
    }
467
}
468

    
469

    
470
/**
471
 * DOC: Parser
472
 *
473
 * Both the configuration and CLI commands are analyzed using a syntax
474
 * driven parser generated by the |bison| tool from a grammar which
475
 * is constructed from information gathered from grammar snippets by
476
 * the |gen_parser.m4| script.
477
 *
478
 * Grammar snippets are files (usually with extension |.Y|) contributed
479
 * by various BIRD modules in order to provide information about syntax of their
480
 * configuration and their CLI commands. Each snipped consists of several
481
 * sections, each of them starting with a special keyword: |CF_HDR| for
482
 * a list of |#include| directives needed by the C code, |CF_DEFINES|
483
 * for a list of C declarations, |CF_DECLS| for |bison| declarations
484
 * including keyword definitions specified as |CF_KEYWORDS|, |CF_GRAMMAR|
485
 * for the grammar rules, |CF_CODE| for auxiliary C code and finally
486
 * |CF_END| at the end of the snippet.
487
 *
488
 * To create references between the snippets, it's possible to define
489
 * multi-part rules by utilizing the |CF_ADDTO| macro which adds a new
490
 * alternative to a multi-part rule.
491
 *
492
 * CLI commands are defined using a |CF_CLI| macro. Its parameters are:
493
 * the list of keywords determining the command, the list of parameters,
494
 * help text for the parameters and help text for the command.
495
 *
496
 * Values of |enum| filter types can be defined using |CF_ENUM| with
497
 * the following parameters: name of filter type, prefix common for all
498
 * literals of this type and names of all the possible values.
499
 */