Statistics
| Branch: | Revision:

iof-bird-daemon / conf / cf-lex.l @ 506fa1a7

History | View | Annotate | Download (11 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
#include "nest/bird.h"
35
#include "nest/route.h"
36
#include "nest/protocol.h"
37
#include "filter/filter.h"
38
#include "conf/conf.h"
39
#include "conf/cf-parse.tab.h"
40
#include "lib/string.h"
41

    
42
struct keyword {
43
  byte *name;
44
  int value;
45
  struct keyword *next;
46
};
47

    
48
#include "conf/keywords.h"
49

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

    
54
#define SYM_HASH_SIZE 128
55
#define SYM_MAX_LEN 32
56

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

    
64
int conf_lino;
65

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

    
69
linpool *cfg_mem;
70

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

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

    
77
%}
78

    
79
%option noyywrap
80
%option noinput
81
%option nounput
82
%option noreject
83

    
84
%x COMMENT CCOMM CLI
85

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

    
92
%%
93

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

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

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

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

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

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

    
168
[={}:;,.()+*/%<>~\[\]?!\|-] {
169
  return yytext[0];
170
}
171

    
172
["][^"\n]*["] {
173
  yytext[yyleng-1] = 0;
174
  cf_lval.t = cfg_strdup(yytext+1);
175
  return TEXT;
176
}
177

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

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

    
182
{WHITE}+
183

    
184
\n	conf_lino++;
185

    
186
#	BEGIN(COMMENT);
187

    
188
\/\*	BEGIN(CCOMM);
189

    
190
.	cf_error("Unknown character");
191

    
192
<COMMENT>\n {
193
  conf_lino++;
194
  BEGIN(INITIAL);
195
}
196

    
197
<COMMENT>.
198

    
199
<CCOMM>\*\/	BEGIN(INITIAL);
200
<CCOMM>\n	conf_lino++;
201
<CCOMM>\/\*	cf_error("Comment nesting not supported");
202
<CCOMM><<EOF>>	cf_error("Unterminated comment");
203
<CCOMM>.
204

    
205
\!\= return NEQ;
206
\<\= return LEQ;
207
\>\= return GEQ;
208
\&\& return AND;
209
\|\| return OR;
210

    
211
\[\= return PO;
212
\=\] return PC;
213

    
214
%%
215

    
216
static int
217
cf_hash(byte *c)
218
{
219
  unsigned int h = 13;
220

    
221
  while (*c)
222
    h = (h * 37) + *c++;
223
  return h;
224
}
225

    
226
static struct symbol *
227
cf_new_sym(byte *c, unsigned int h)
228
{
229
  struct symbol *s, **ht;
230
  int l;
231

    
232
  if (!new_config->sym_hash)
233
    new_config->sym_hash = cfg_allocz(SYM_HASH_SIZE * sizeof(struct keyword *));
234
  ht = new_config->sym_hash;
235
  l = strlen(c);
236
  if (l > SYM_MAX_LEN)
237
    cf_error("Symbol too long");
238
  s = cfg_alloc(sizeof(struct symbol) + l);
239
  s->next = ht[h];
240
  ht[h] = s;
241
  s->scope = conf_this_scope;
242
  s->class = SYM_VOID;
243
  s->def = NULL;
244
  s->aux = 0;
245
  strcpy(s->name, c);
246
  return s;
247
}
248

    
249
static struct symbol *
250
cf_find_sym(byte *c, unsigned int h0)
251
{
252
  unsigned int h = h0 & (SYM_HASH_SIZE-1);
253
  struct symbol *s, **ht;
254

    
255
  if (ht = new_config->sym_hash)
256
    {
257
      for(s = ht[h]; s; s=s->next)
258
	if (!strcmp(s->name, c) && s->scope->active)
259
	  return s;
260
    }
261
  if (new_config->sym_fallback)
262
    {
263
      /* We know only top-level scope is active */
264
      for(s = new_config->sym_fallback[h]; s; s=s->next)
265
	if (!strcmp(s->name, c) && s->scope->active)
266
	  return s;
267
    }
268
  return cf_new_sym(c, h);
269
}
270

    
271
/**
272
 * cf_find_symbol - find a symbol by name
273
 * @c: symbol name
274
 *
275
 * This functions searches the symbol table for a symbol of given
276
 * name. First it examines the current scope, then the second recent
277
 * one and so on until it either finds the symbol and returns a pointer
278
 * to its &symbol structure or reaches the end of the scope chain
279
 * and returns %NULL to signify no match.
280
 */
281
struct symbol *
282
cf_find_symbol(byte *c)
283
{
284
  return cf_find_sym(c, cf_hash(c));
285
}
286

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

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

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

    
337
static void
338
cf_lex_init_kh(void)
339
{
340
  struct keyword *k;
341

    
342
  for(k=keyword_list; k->name; k++)
343
    {
344
      unsigned h = cf_hash(k->name) & (KW_HASH_SIZE-1);
345
      k->next = kw_hash[h];
346
      kw_hash[h] = k;
347
    }
348
  kw_hash_inited = 1;
349
}
350

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

    
373
/**
374
 * cf_push_scope - enter new scope
375
 * @sym: symbol representing scope name
376
 *
377
 * If we want to enter a new scope to process declarations inside
378
 * a nested block, we can just call cf_push_scope() to push a new
379
 * scope onto the scope stack which will cause all new symbols to be
380
 * defined in this scope and all existing symbols to be sought for
381
 * in all scopes stored on the stack.
382
 */
383
void
384
cf_push_scope(struct symbol *sym)
385
{
386
  struct sym_scope *s = cfg_alloc(sizeof(struct sym_scope));
387

    
388
  s->next = conf_this_scope;
389
  conf_this_scope = s;
390
  s->active = 1;
391
  s->name = sym;
392
}
393

    
394
/**
395
 * cf_pop_scope - leave a scope
396
 *
397
 * cf_pop_scope() pops the topmost scope from the scope stack,
398
 * leaving all its symbols in the symbol table, but making them
399
 * invisible to the rest of the config.
400
 */
401
void
402
cf_pop_scope(void)
403
{
404
  conf_this_scope->active = 0;
405
  conf_this_scope = conf_this_scope->next;
406
  ASSERT(conf_this_scope);
407
}
408

    
409
struct symbol *
410
cf_walk_symbols(struct config *cf, struct symbol *sym, int *pos)
411
{
412
  for(;;)
413
    {
414
      if (!sym)
415
	{
416
	  if (*pos >= SYM_HASH_SIZE)
417
	    return NULL;
418
	  sym = cf->sym_hash[(*pos)++];
419
	}
420
      else
421
	sym = sym->next;
422
      if (sym && sym->scope->active)
423
	return sym;
424
    }
425
}
426

    
427
/**
428
 * cf_symbol_class_name - get name of a symbol class
429
 * @sym: symbol
430
 *
431
 * This function returns a string representing the class
432
 * of the given symbol.
433
 */
434
char *
435
cf_symbol_class_name(struct symbol *sym)
436
{
437
  switch (sym->class)
438
    {
439
    case SYM_VOID:
440
      return "undefined";
441
    case SYM_PROTO:
442
      return "protocol";
443
    case SYM_NUMBER:
444
      return "numeric constant";
445
    case SYM_FUNCTION:
446
      return "function";
447
    case SYM_FILTER:
448
      return "filter";
449
    case SYM_TABLE:
450
      return "routing table";
451
    case SYM_IPA:
452
      return "network address";
453
    default:
454
      return "unknown type";
455
    }
456
}
457

    
458

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