Statistics
| Branch: | Revision:

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

History | View | Annotate | Download (15.5 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
19
 * name of a protocol, %SYM_CONSTANT for a constant etc.) and class
20
 * dependent data.  When an unknown symbol is encountered, it's
21
 * automatically added to the 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
#include <unistd.h>
34
#include <libgen.h>
35
#include <glob.h>
36
#include <fcntl.h>
37
#include <sys/stat.h>
38
#include <sys/types.h>
39
#include <sys/stat.h>
40

    
41
#define PARSER 1
42

    
43
#include "nest/bird.h"
44
#include "nest/route.h"
45
#include "nest/protocol.h"
46
#include "filter/filter.h"
47
#include "conf/conf.h"
48
#include "conf/cf-parse.tab.h"
49
#include "lib/string.h"
50

    
51
struct keyword {
52
  byte *name;
53
  int value;
54
  struct keyword *next;
55
};
56

    
57
#include "conf/keywords.h"
58

    
59
#define KW_HASH_SIZE 64
60
static struct keyword *kw_hash[KW_HASH_SIZE];
61
static int kw_hash_inited;
62

    
63
#define SYM_HASH_SIZE 128
64

    
65
struct sym_scope {
66
  struct sym_scope *next;		/* Next on scope stack */
67
  struct symbol *name;			/* Name of this scope */
68
  int active;				/* Currently entered */
69
};
70
static struct sym_scope *conf_this_scope;
71

    
72
static int cf_hash(byte *c);
73
static struct symbol *cf_find_sym(byte *c, unsigned int h0);
74

    
75
linpool *cfg_mem;
76

    
77
int (*cf_read_hook)(byte *buf, unsigned int max, int fd);
78
struct include_file_stack *ifs;
79
static struct include_file_stack *ifs_head;
80

    
81
#define MAX_INCLUDE_DEPTH 8
82

    
83
#define YY_INPUT(buf,result,max) result = cf_read_hook(buf, max, ifs->fd);
84
#define YY_NO_UNPUT
85
#define YY_FATAL_ERROR(msg) cf_error(msg)
86

    
87
static void cf_include(char *arg, int alen);
88
static int check_eof(void);
89

    
90
%}
91

    
92
%option noyywrap
93
%option noinput
94
%option nounput
95
%option noreject
96

    
97
%x COMMENT CCOMM CLI
98

    
99
ALPHA [a-zA-Z_]
100
DIGIT [0-9]
101
XIGIT [0-9a-fA-F]
102
ALNUM [a-zA-Z_0-9]
103
WHITE [ \t]
104
include   ^{WHITE}*include{WHITE}*\".*\"{WHITE}*;
105

    
106
%%
107
{include} {
108
  char *start, *end;
109

    
110
  if (!ifs->depth)
111
    cf_error("Include not allowed in CLI");
112

    
113
  start = strchr(yytext, '"');
114
  start++;
115

    
116
  end = strchr(start, '"');
117
  *end = 0;
118

    
119
  if (start == end)
120
    cf_error("Include with empty argument");
121

    
122
  cf_include(start, end-start);
123
}
124

    
125
{DIGIT}+\.{DIGIT}+\.{DIGIT}+\.{DIGIT}+ {
126
  if (!ip4_pton(yytext, &cf_lval.ip4))
127
    cf_error("Invalid IPv4 address %s", yytext);
128
  return IP4;
129
}
130

    
131
({XIGIT}*::|({XIGIT}*:){3,})({XIGIT}*|{DIGIT}+\.{DIGIT}+\.{DIGIT}+\.{DIGIT}+) {
132
  if (!ip6_pton(yytext, &cf_lval.ip6))
133
    cf_error("Invalid IPv6 address %s", yytext);
134
  return IP6;
135
}
136

    
137
0x{XIGIT}+ {
138
  char *e;
139
  unsigned long int l;
140
  errno = 0;
141
  l = strtoul(yytext+2, &e, 16);
142
  if (e && *e || errno == ERANGE || (unsigned long int)(unsigned int) l != l)
143
    cf_error("Number out of range");
144
  cf_lval.i = l;
145
  return NUM;
146
}
147

    
148
{DIGIT}+ {
149
  char *e;
150
  unsigned long int l;
151
  errno = 0;
152
  l = strtoul(yytext, &e, 10);
153
  if (e && *e || errno == ERANGE || (unsigned long int)(unsigned int) l != l)
154
    cf_error("Number out of range");
155
  cf_lval.i = l;
156
  return NUM;
157
}
158

    
159
else: {
160
  /* Hack to distinguish if..else from else: in case */
161
  return ELSECOL;
162
}
163

    
164
({ALPHA}{ALNUM}*|[']({ALNUM}|[-]|[\.]|[:])*[']) {
165
  if(*yytext == '\'') {
166
    yytext[yyleng-1] = 0;
167
    yytext++;
168
  }
169
  unsigned int h = cf_hash(yytext);
170
  struct keyword *k = kw_hash[h & (KW_HASH_SIZE-1)];
171
  while (k)
172
    {
173
      if (!strcmp(k->name, yytext))
174
	{
175
	  if (k->value > 0)
176
	    return k->value;
177
	  else
178
	    {
179
	      cf_lval.i = -k->value;
180
	      return ENUM;
181
	    }
182
	}
183
      k=k->next;
184
    }
185
  cf_lval.s = cf_find_sym(yytext, h);
186
  return SYM;
187
}
188

    
189
<CLI>(.|\n) {
190
  BEGIN(INITIAL);
191
  return CLI_MARKER;
192
}
193

    
194
\.\. {
195
  return DDOT;
196
}
197

    
198
[={}:;,.()+*/%<>~\[\]?!\|-] {
199
  return yytext[0];
200
}
201

    
202
["][^"\n]*["] {
203
  yytext[yyleng-1] = 0;
204
  cf_lval.t = cfg_strdup(yytext+1);
205
  return TEXT;
206
}
207

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

    
210
<INITIAL,COMMENT><<EOF>>	{ if (check_eof()) return END; }
211

    
212
{WHITE}+
213

    
214
\n	ifs->lino++;
215

    
216
#	BEGIN(COMMENT);
217

    
218
\/\*	BEGIN(CCOMM);
219

    
220
.	cf_error("Unknown character");
221

    
222
<COMMENT>\n {
223
  ifs->lino++;
224
  BEGIN(INITIAL);
225
}
226

    
227
<COMMENT>.
228

    
229
<CCOMM>\*\/	BEGIN(INITIAL);
230
<CCOMM>\n	ifs->lino++;
231
<CCOMM>\/\*	cf_error("Comment nesting not supported");
232
<CCOMM><<EOF>>	cf_error("Unterminated comment");
233
<CCOMM>.
234

    
235
\!\= return NEQ;
236
\<\= return LEQ;
237
\>\= return GEQ;
238
\&\& return AND;
239
\|\| return OR;
240

    
241
\[\= return PO;
242
\=\] return PC;
243

    
244
%%
245

    
246
static int
247
cf_hash(byte *c)
248
{
249
  unsigned int h = 13;
250

    
251
  while (*c)
252
    h = (h * 37) + *c++;
253
  return h;
254
}
255

    
256

    
257
/*
258
 * IFS stack - it contains structures needed for recursive processing
259
 * of include in config files. On the top of the stack is a structure
260
 * for currently processed file. Other structures are either for
261
 * active files interrupted because of include directive (these have
262
 * fd and flex buffer) or for inactive files scheduled to be processed
263
 * later (when parent requested including of several files by wildcard
264
 * match - these do not have fd and flex buffer yet).
265
 *
266
 * FIXME: Most of these ifs and include functions are really sysdep/unix.
267
 */
268

    
269
static struct include_file_stack *
270
push_ifs(struct include_file_stack *old)
271
{
272
  struct include_file_stack *ret;
273
  ret = cfg_allocz(sizeof(struct include_file_stack));
274
  ret->lino = 1;
275
  ret->prev = old;
276
  return ret;
277
}
278

    
279
static struct include_file_stack *
280
pop_ifs(struct include_file_stack *old)
281
{
282
 yy_delete_buffer(old->buffer);
283
 close(old->fd);
284
 return old->prev;
285
}
286

    
287
static void
288
enter_ifs(struct include_file_stack *new)
289
{
290
  if (!new->buffer)
291
    {
292
      new->fd = open(new->file_name, O_RDONLY);
293
      if (new->fd < 0)
294
        {
295
          ifs = ifs->up;
296
	  cf_error("Unable to open included file %s: %m", new->file_name);
297
        }
298

    
299
      new->buffer = yy_create_buffer(NULL, YY_BUF_SIZE);
300
    }
301

    
302
  yy_switch_to_buffer(new->buffer);
303
}
304

    
305
/**
306
 * cf_lex_unwind - unwind lexer state during error
307
 *
308
 * cf_lex_unwind() frees the internal state on IFS stack when the lexical
309
 * analyzer is terminated by cf_error().
310
 */
311
void
312
cf_lex_unwind(void)
313
{
314
  struct include_file_stack *n;
315

    
316
  for (n = ifs; n != ifs_head; n = n->prev)
317
    {
318
      /* Memory is freed automatically */
319
      if (n->buffer)
320
	yy_delete_buffer(n->buffer);
321
      if (n->fd)
322
        close(n->fd);
323
    }
324

    
325
  ifs = ifs_head;
326
}
327

    
328
static void
329
cf_include(char *arg, int alen)
330
{
331
  struct include_file_stack *base_ifs = ifs;
332
  int new_depth, rv, i;
333
  char *patt;
334
  glob_t g = {};
335

    
336
  new_depth = ifs->depth + 1;
337
  if (new_depth > MAX_INCLUDE_DEPTH)
338
    cf_error("Max include depth reached");
339

    
340
  /* expand arg to properly handle relative filenames */
341
  if (*arg != '/')
342
    {
343
      int dlen = strlen(ifs->file_name);
344
      char *dir = alloca(dlen + 1);
345
      patt = alloca(dlen + alen + 2);
346
      memcpy(dir, ifs->file_name, dlen + 1);
347
      sprintf(patt, "%s/%s", dirname(dir), arg);
348
    }
349
  else
350
    patt = arg;
351

    
352
  /* Skip globbing if there are no wildcards, mainly to get proper
353
     response when the included config file is missing */
354
  if (!strpbrk(arg, "?*["))
355
    {
356
      ifs = push_ifs(ifs);
357
      ifs->file_name = cfg_strdup(patt);
358
      ifs->depth = new_depth;
359
      ifs->up = base_ifs;
360
      enter_ifs(ifs);
361
      return;
362
    }
363

    
364
  /* Expand the pattern */
365
  rv = glob(patt, GLOB_ERR | GLOB_NOESCAPE, NULL, &g);
366
  if (rv == GLOB_ABORTED)
367
    cf_error("Unable to match pattern %s: %m", patt);
368
  if ((rv != 0) || (g.gl_pathc <= 0))
369
    return;
370

    
371
  /*
372
   * Now we put all found files to ifs stack in reverse order, they
373
   * will be activated and processed in order as ifs stack is popped
374
   * by pop_ifs() and enter_ifs() in check_eof().
375
   */
376
  for(i = g.gl_pathc - 1; i >= 0; i--)
377
    {
378
      char *fname = g.gl_pathv[i];
379
      struct stat fs;
380

    
381
      if (stat(fname, &fs) < 0)
382
	{
383
	  globfree(&g);
384
	  cf_error("Unable to stat included file %s: %m", fname);
385
	}
386

    
387
      if (fs.st_mode & S_IFDIR)
388
        continue;
389

    
390
      /* Prepare new stack item */
391
      ifs = push_ifs(ifs);
392
      ifs->file_name = cfg_strdup(fname);
393
      ifs->depth = new_depth;
394
      ifs->up = base_ifs;
395
    }
396

    
397
  globfree(&g);
398
  enter_ifs(ifs);
399
}
400

    
401
static int
402
check_eof(void)
403
{
404
  if (ifs == ifs_head)
405
    {
406
      /* EOF in main config file */
407
      ifs->lino = 1; /* Why this? */
408
      return 1;
409
    }
410

    
411
  ifs = pop_ifs(ifs);
412
  enter_ifs(ifs);
413
  return 0;
414
}
415

    
416
static struct symbol *
417
cf_new_sym(byte *c, unsigned int h)
418
{
419
  struct symbol *s, **ht;
420
  int l;
421

    
422
  if (!new_config->sym_hash)
423
    new_config->sym_hash = cfg_allocz(SYM_HASH_SIZE * sizeof(struct keyword *));
424
  ht = new_config->sym_hash;
425
  l = strlen(c);
426
  if (l > SYM_MAX_LEN)
427
    cf_error("Symbol too long");
428
  s = cfg_alloc(sizeof(struct symbol) + l);
429
  s->next = ht[h];
430
  ht[h] = s;
431
  s->scope = conf_this_scope;
432
  s->class = SYM_VOID;
433
  s->def = NULL;
434
  s->aux = 0;
435
  strcpy(s->name, c);
436
  return s;
437
}
438

    
439
static struct symbol *
440
cf_find_sym(byte *c, unsigned int h0)
441
{
442
  unsigned int h = h0 & (SYM_HASH_SIZE-1);
443
  struct symbol *s, **ht;
444

    
445
  if (ht = new_config->sym_hash)
446
    {
447
      for(s = ht[h]; s; s=s->next)
448
	if (!strcmp(s->name, c) && s->scope->active)
449
	  return s;
450
    }
451
  if (new_config->sym_fallback)
452
    {
453
      /* We know only top-level scope is active */
454
      for(s = new_config->sym_fallback[h]; s; s=s->next)
455
	if (!strcmp(s->name, c) && s->scope->active)
456
	  return s;
457
    }
458
  return cf_new_sym(c, h);
459
}
460

    
461
/**
462
 * cf_find_symbol - find a symbol by name
463
 * @c: symbol name
464
 *
465
 * This functions searches the symbol table for a symbol of given
466
 * name. First it examines the current scope, then the second recent
467
 * one and so on until it either finds the symbol and returns a pointer
468
 * to its &symbol structure or reaches the end of the scope chain
469
 * and returns %NULL to signify no match.
470
 */
471
struct symbol *
472
cf_find_symbol(byte *c)
473
{
474
  return cf_find_sym(c, cf_hash(c));
475
}
476

    
477
struct symbol *
478
cf_default_name(char *template, int *counter)
479
{
480
  char buf[32];
481
  struct symbol *s;
482
  char *perc = strchr(template, '%');
483

    
484
  for(;;)
485
    {
486
      bsprintf(buf, template, ++(*counter));
487
      s = cf_find_sym(buf, cf_hash(buf));
488
      if (!s)
489
	break;
490
      if (s->class == SYM_VOID)
491
	return s;
492
      if (!perc)
493
	break;
494
    }
495
  cf_error("Unable to generate default name");
496
}
497

    
498
/**
499
 * cf_define_symbol - define meaning of a symbol
500
 * @sym: symbol to be defined
501
 * @type: symbol class to assign
502
 * @def: class dependent data
503
 *
504
 * Defines new meaning of a symbol. If the symbol is an undefined
505
 * one (%SYM_VOID), it's just re-defined to the new type. If it's defined
506
 * in different scope, a new symbol in current scope is created and the
507
 * meaning is assigned to it. If it's already defined in the current scope,
508
 * an error is reported via cf_error().
509
 *
510
 * Result: Pointer to the newly defined symbol. If we are in the top-level
511
 * scope, it's the same @sym as passed to the function.
512
 */
513
struct symbol *
514
cf_define_symbol(struct symbol *sym, int type, void *def)
515
{
516
  if (sym->class)
517
    {
518
      if (sym->scope == conf_this_scope)
519
	cf_error("Symbol already defined");
520
      sym = cf_new_sym(sym->name, cf_hash(sym->name) & (SYM_HASH_SIZE-1));
521
    }
522
  sym->class = type;
523
  sym->def = def;
524
  return sym;
525
}
526

    
527
static void
528
cf_lex_init_kh(void)
529
{
530
  struct keyword *k;
531

    
532
  for(k=keyword_list; k->name; k++)
533
    {
534
      unsigned h = cf_hash(k->name) & (KW_HASH_SIZE-1);
535
      k->next = kw_hash[h];
536
      kw_hash[h] = k;
537
    }
538
  kw_hash_inited = 1;
539
}
540

    
541
/**
542
 * cf_lex_init - initialize the lexer
543
 * @is_cli: true if we're going to parse CLI command, false for configuration
544
 *
545
 * cf_lex_init() initializes the lexical analyzer and prepares it for
546
 * parsing of a new input.
547
 */
548
void
549
cf_lex_init(int is_cli, struct config *c)
550
{
551
  if (!kw_hash_inited)
552
    cf_lex_init_kh();
553

    
554
  ifs_head = ifs = push_ifs(NULL);
555
  if (!is_cli) 
556
    {
557
      ifs->file_name = c->file_name;
558
      ifs->fd = c->file_fd;
559
      ifs->depth = 1;
560
    }
561

    
562
  yyrestart(NULL);
563
  ifs->buffer = YY_CURRENT_BUFFER;
564

    
565
  if (is_cli)
566
    BEGIN(CLI);
567
  else
568
    BEGIN(INITIAL);
569

    
570
  conf_this_scope = cfg_allocz(sizeof(struct sym_scope));
571
  conf_this_scope->active = 1;
572
}
573

    
574
/**
575
 * cf_push_scope - enter new scope
576
 * @sym: symbol representing scope name
577
 *
578
 * If we want to enter a new scope to process declarations inside
579
 * a nested block, we can just call cf_push_scope() to push a new
580
 * scope onto the scope stack which will cause all new symbols to be
581
 * defined in this scope and all existing symbols to be sought for
582
 * in all scopes stored on the stack.
583
 */
584
void
585
cf_push_scope(struct symbol *sym)
586
{
587
  struct sym_scope *s = cfg_alloc(sizeof(struct sym_scope));
588

    
589
  s->next = conf_this_scope;
590
  conf_this_scope = s;
591
  s->active = 1;
592
  s->name = sym;
593
}
594

    
595
/**
596
 * cf_pop_scope - leave a scope
597
 *
598
 * cf_pop_scope() pops the topmost scope from the scope stack,
599
 * leaving all its symbols in the symbol table, but making them
600
 * invisible to the rest of the config.
601
 */
602
void
603
cf_pop_scope(void)
604
{
605
  conf_this_scope->active = 0;
606
  conf_this_scope = conf_this_scope->next;
607
  ASSERT(conf_this_scope);
608
}
609

    
610
struct symbol *
611
cf_walk_symbols(struct config *cf, struct symbol *sym, int *pos)
612
{
613
  for(;;)
614
    {
615
      if (!sym)
616
	{
617
	  if (*pos >= SYM_HASH_SIZE)
618
	    return NULL;
619
	  sym = cf->sym_hash[(*pos)++];
620
	}
621
      else
622
	sym = sym->next;
623
      if (sym && sym->scope->active)
624
	return sym;
625
    }
626
}
627

    
628
/**
629
 * cf_symbol_class_name - get name of a symbol class
630
 * @sym: symbol
631
 *
632
 * This function returns a string representing the class
633
 * of the given symbol.
634
 */
635
char *
636
cf_symbol_class_name(struct symbol *sym)
637
{
638
  if (cf_symbol_is_constant(sym))
639
    return "constant";
640

    
641
  switch (sym->class)
642
    {
643
    case SYM_VOID:
644
      return "undefined";
645
    case SYM_PROTO:
646
      return "protocol";
647
    case SYM_TEMPLATE:
648
      return "protocol template";
649
    case SYM_FUNCTION:
650
      return "function";
651
    case SYM_FILTER:
652
      return "filter";
653
    case SYM_TABLE:
654
      return "routing table";
655
    case SYM_ROA:
656
      return "ROA table";
657
    default:
658
      return "unknown type";
659
    }
660
}
661

    
662

    
663
/**
664
 * DOC: Parser
665
 *
666
 * Both the configuration and CLI commands are analyzed using a syntax
667
 * driven parser generated by the |bison| tool from a grammar which
668
 * is constructed from information gathered from grammar snippets by
669
 * the |gen_parser.m4| script.
670
 *
671
 * Grammar snippets are files (usually with extension |.Y|) contributed
672
 * by various BIRD modules in order to provide information about syntax of their
673
 * configuration and their CLI commands. Each snipped consists of several
674
 * sections, each of them starting with a special keyword: |CF_HDR| for
675
 * a list of |#include| directives needed by the C code, |CF_DEFINES|
676
 * for a list of C declarations, |CF_DECLS| for |bison| declarations
677
 * including keyword definitions specified as |CF_KEYWORDS|, |CF_GRAMMAR|
678
 * for the grammar rules, |CF_CODE| for auxiliary C code and finally
679
 * |CF_END| at the end of the snippet.
680
 *
681
 * To create references between the snippets, it's possible to define
682
 * multi-part rules by utilizing the |CF_ADDTO| macro which adds a new
683
 * alternative to a multi-part rule.
684
 *
685
 * CLI commands are defined using a |CF_CLI| macro. Its parameters are:
686
 * the list of keywords determining the command, the list of parameters,
687
 * help text for the parameters and help text for the command.
688
 *
689
 * Values of |enum| filter types can be defined using |CF_ENUM| with
690
 * the following parameters: name of filter type, prefix common for all
691
 * literals of this type and names of all the possible values.
692
 */