Statistics
| Branch: | Revision:

iof-bird-daemon / conf / cf-lex.l @ 9b9a7143

History | View | Annotate | Download (16.2 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 inline struct symbol * cf_get_sym(byte *c, uint 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
  ip4_addr a;
127
  if (!ip4_pton(yytext, &a))
128
    cf_error("Invalid IPv4 address %s", yytext);
129

    
130
#ifdef IPV6
131
  cf_lval.i32 = ip4_to_u32(a);
132
  return RTRID;
133
#else
134
  cf_lval.a = ipa_from_ip4(a);
135
  return IPA;
136
#endif
137
}
138

    
139
({XIGIT}*::|({XIGIT}*:){3,})({XIGIT}*|{DIGIT}+\.{DIGIT}+\.{DIGIT}+\.{DIGIT}+) {
140
#ifdef IPV6
141
  if (ipa_pton(yytext, &cf_lval.a))
142
    return IPA;
143
  cf_error("Invalid IPv6 address %s", yytext);
144
#else
145
  cf_error("This is an IPv4 router, therefore IPv6 addresses are not supported");
146
#endif
147
}
148

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

    
160
{DIGIT}+ {
161
  char *e;
162
  unsigned long int l;
163
  errno = 0;
164
  l = strtoul(yytext, &e, 10);
165
  if (e && *e || errno == ERANGE || (unsigned long int)(unsigned int) l != l)
166
    cf_error("Number out of range");
167
  cf_lval.i = l;
168
  return NUM;
169
}
170

    
171
else: {
172
  /* Hack to distinguish if..else from else: in case */
173
  return ELSECOL;
174
}
175

    
176
({ALPHA}{ALNUM}*|[']({ALNUM}|[-]|[\.]|[:])*[']) {
177
  if(*yytext == '\'') {
178
    yytext[yyleng-1] = 0;
179
    yytext++;
180
  }
181
  unsigned int h = cf_hash(yytext);
182
  struct keyword *k = kw_hash[h & (KW_HASH_SIZE-1)];
183
  while (k)
184
    {
185
      if (!strcmp(k->name, yytext))
186
	{
187
	  if (k->value > 0)
188
	    return k->value;
189
	  else
190
	    {
191
	      cf_lval.i = -k->value;
192
	      return ENUM;
193
	    }
194
	}
195
      k=k->next;
196
    }
197
  cf_lval.s = cf_get_sym(yytext, h);
198
  return SYM;
199
}
200

    
201
<CLI>(.|\n) {
202
  BEGIN(INITIAL);
203
  return CLI_MARKER;
204
}
205

    
206
\.\. {
207
  return DDOT;
208
}
209

    
210
[={}:;,.()+*/%<>~\[\]?!\|-] {
211
  return yytext[0];
212
}
213

    
214
["][^"\n]*["] {
215
  yytext[yyleng-1] = 0;
216
  cf_lval.t = cfg_strdup(yytext+1);
217
  return TEXT;
218
}
219

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

    
222
<INITIAL,COMMENT><<EOF>>	{ if (check_eof()) return END; }
223

    
224
{WHITE}+
225

    
226
\n	ifs->lino++;
227

    
228
#	BEGIN(COMMENT);
229

    
230
\/\*	BEGIN(CCOMM);
231

    
232
.	cf_error("Unknown character");
233

    
234
<COMMENT>\n {
235
  ifs->lino++;
236
  BEGIN(INITIAL);
237
}
238

    
239
<COMMENT>.
240

    
241
<CCOMM>\*\/	BEGIN(INITIAL);
242
<CCOMM>\n	ifs->lino++;
243
<CCOMM>\/\*	cf_error("Comment nesting not supported");
244
<CCOMM><<EOF>>	cf_error("Unterminated comment");
245
<CCOMM>.
246

    
247
\!\= return NEQ;
248
\<\= return LEQ;
249
\>\= return GEQ;
250
\&\& return AND;
251
\|\| return OR;
252

    
253
\[\= return PO;
254
\=\] return PC;
255

    
256
%%
257

    
258
static int
259
cf_hash(byte *c)
260
{
261
  unsigned int h = 13;
262

    
263
  while (*c)
264
    h = (h * 37) + *c++;
265
  return h;
266
}
267

    
268

    
269
/*
270
 * IFS stack - it contains structures needed for recursive processing
271
 * of include in config files. On the top of the stack is a structure
272
 * for currently processed file. Other structures are either for
273
 * active files interrupted because of include directive (these have
274
 * fd and flex buffer) or for inactive files scheduled to be processed
275
 * later (when parent requested including of several files by wildcard
276
 * match - these do not have fd and flex buffer yet).
277
 *
278
 * FIXME: Most of these ifs and include functions are really sysdep/unix.
279
 */
280

    
281
static struct include_file_stack *
282
push_ifs(struct include_file_stack *old)
283
{
284
  struct include_file_stack *ret;
285
  ret = cfg_allocz(sizeof(struct include_file_stack));
286
  ret->lino = 1;
287
  ret->prev = old;
288
  return ret;
289
}
290

    
291
static struct include_file_stack *
292
pop_ifs(struct include_file_stack *old)
293
{
294
 yy_delete_buffer(old->buffer);
295
 close(old->fd);
296
 return old->prev;
297
}
298

    
299
static void
300
enter_ifs(struct include_file_stack *new)
301
{
302
  if (!new->buffer)
303
    {
304
      new->fd = open(new->file_name, O_RDONLY);
305
      if (new->fd < 0)
306
        {
307
          ifs = ifs->up;
308
	  cf_error("Unable to open included file %s: %m", new->file_name);
309
        }
310

    
311
      new->buffer = yy_create_buffer(NULL, YY_BUF_SIZE);
312
    }
313

    
314
  yy_switch_to_buffer(new->buffer);
315
}
316

    
317
/**
318
 * cf_lex_unwind - unwind lexer state during error
319
 *
320
 * cf_lex_unwind() frees the internal state on IFS stack when the lexical
321
 * analyzer is terminated by cf_error().
322
 */
323
void
324
cf_lex_unwind(void)
325
{
326
  struct include_file_stack *n;
327

    
328
  for (n = ifs; n != ifs_head; n = n->prev)
329
    {
330
      /* Memory is freed automatically */
331
      if (n->buffer)
332
	yy_delete_buffer(n->buffer);
333
      if (n->fd)
334
        close(n->fd);
335
    }
336

    
337
  ifs = ifs_head;
338
}
339

    
340
static void
341
cf_include(char *arg, int alen)
342
{
343
  struct include_file_stack *base_ifs = ifs;
344
  int new_depth, rv, i;
345
  char *patt;
346
  glob_t g = {};
347

    
348
  new_depth = ifs->depth + 1;
349
  if (new_depth > MAX_INCLUDE_DEPTH)
350
    cf_error("Max include depth reached");
351

    
352
  /* expand arg to properly handle relative filenames */
353
  if (*arg != '/')
354
    {
355
      int dlen = strlen(ifs->file_name);
356
      char *dir = alloca(dlen + 1);
357
      patt = alloca(dlen + alen + 2);
358
      memcpy(dir, ifs->file_name, dlen + 1);
359
      sprintf(patt, "%s/%s", dirname(dir), arg);
360
    }
361
  else
362
    patt = arg;
363

    
364
  /* Skip globbing if there are no wildcards, mainly to get proper
365
     response when the included config file is missing */
366
  if (!strpbrk(arg, "?*["))
367
    {
368
      ifs = push_ifs(ifs);
369
      ifs->file_name = cfg_strdup(patt);
370
      ifs->depth = new_depth;
371
      ifs->up = base_ifs;
372
      enter_ifs(ifs);
373
      return;
374
    }
375

    
376
  /* Expand the pattern */
377
  rv = glob(patt, GLOB_ERR | GLOB_NOESCAPE, NULL, &g);
378
  if (rv == GLOB_ABORTED)
379
    cf_error("Unable to match pattern %s: %m", patt);
380
  if ((rv != 0) || (g.gl_pathc <= 0))
381
    return;
382

    
383
  /*
384
   * Now we put all found files to ifs stack in reverse order, they
385
   * will be activated and processed in order as ifs stack is popped
386
   * by pop_ifs() and enter_ifs() in check_eof().
387
   */
388
  for(i = g.gl_pathc - 1; i >= 0; i--)
389
    {
390
      char *fname = g.gl_pathv[i];
391
      struct stat fs;
392

    
393
      if (stat(fname, &fs) < 0)
394
	{
395
	  globfree(&g);
396
	  cf_error("Unable to stat included file %s: %m", fname);
397
	}
398

    
399
      if (fs.st_mode & S_IFDIR)
400
        continue;
401

    
402
      /* Prepare new stack item */
403
      ifs = push_ifs(ifs);
404
      ifs->file_name = cfg_strdup(fname);
405
      ifs->depth = new_depth;
406
      ifs->up = base_ifs;
407
    }
408

    
409
  globfree(&g);
410
  enter_ifs(ifs);
411
}
412

    
413
static int
414
check_eof(void)
415
{
416
  if (ifs == ifs_head)
417
    {
418
      /* EOF in main config file */
419
      ifs->lino = 1; /* Why this? */
420
      return 1;
421
    }
422

    
423
  ifs = pop_ifs(ifs);
424
  enter_ifs(ifs);
425
  return 0;
426
}
427

    
428
static struct symbol *
429
cf_new_sym(byte *c, uint h0)
430
{
431
  uint h = h0 & (SYM_HASH_SIZE-1);
432
  struct symbol *s, **ht;
433
  int l;
434

    
435
  if (!new_config->sym_hash)
436
    new_config->sym_hash = cfg_allocz(SYM_HASH_SIZE * sizeof(struct keyword *));
437
  ht = new_config->sym_hash;
438
  l = strlen(c);
439
  if (l > SYM_MAX_LEN)
440
    cf_error("Symbol too long");
441
  s = cfg_alloc(sizeof(struct symbol) + l);
442
  s->next = ht[h];
443
  ht[h] = s;
444
  s->scope = conf_this_scope;
445
  s->class = SYM_VOID;
446
  s->def = NULL;
447
  s->aux = 0;
448
  strcpy(s->name, c);
449
  return s;
450
}
451

    
452
static struct symbol *
453
cf_find_sym(struct config *cfg, byte *c, uint h0)
454
{
455
  uint h = h0 & (SYM_HASH_SIZE-1);
456
  struct symbol *s, **ht;
457

    
458
  if (ht = cfg->sym_hash)
459
    {
460
      for(s = ht[h]; s; s=s->next)
461
	if (!strcmp(s->name, c) && s->scope->active)
462
	  return s;
463
    }
464
  if (ht = cfg->sym_fallback)
465
    {
466
      /* We know only top-level scope is active */
467
      for(s = ht[h]; s; s=s->next)
468
	if (!strcmp(s->name, c) && s->scope->active)
469
	  return s;
470
    }
471

    
472
  return NULL;
473
}
474

    
475
static inline struct symbol *
476
cf_get_sym(byte *c, uint h0)
477
{
478
  return cf_find_sym(new_config, c, h0) ?: cf_new_sym(c, h0);
479
}
480

    
481
/**
482
 * cf_find_symbol - find a symbol by name
483
 * @cfg: specificed config
484
 * @c: symbol name
485
 *
486
 * This functions searches the symbol table in the config @cfg for a symbol of
487
 * given name. First it examines the current scope, then the second recent one
488
 * and so on until it either finds the symbol and returns a pointer to its
489
 * &symbol structure or reaches the end of the scope chain and returns %NULL to
490
 * signify no match.
491
 */
492
struct symbol *
493
cf_find_symbol(struct config *cfg, byte *c)
494
{
495
  return cf_find_sym(cfg, c, cf_hash(c));
496
}
497

    
498
/**
499
 * cf_get_symbol - get a symbol by name
500
 * @c: symbol name
501
 *
502
 * This functions searches the symbol table of the currently parsed config
503
 * (@new_config) for a symbol of given name. It returns either the already
504
 * existing symbol or a newly allocated undefined (%SYM_VOID) symbol if no
505
 * existing symbol is found.
506
 */
507
struct symbol *
508
cf_get_symbol(byte *c)
509
{
510
  return cf_get_sym(c, cf_hash(c));
511
}
512

    
513
struct symbol *
514
cf_default_name(char *template, int *counter)
515
{
516
  char buf[SYM_MAX_LEN];
517
  struct symbol *s;
518
  char *perc = strchr(template, '%');
519

    
520
  for(;;)
521
    {
522
      bsprintf(buf, template, ++(*counter));
523
      s = cf_get_sym(buf, cf_hash(buf));
524
      if (s->class == SYM_VOID)
525
	return s;
526
      if (!perc)
527
	break;
528
    }
529
  cf_error("Unable to generate default name");
530
}
531

    
532
/**
533
 * cf_define_symbol - define meaning of a symbol
534
 * @sym: symbol to be defined
535
 * @type: symbol class to assign
536
 * @def: class dependent data
537
 *
538
 * Defines new meaning of a symbol. If the symbol is an undefined
539
 * one (%SYM_VOID), it's just re-defined to the new type. If it's defined
540
 * in different scope, a new symbol in current scope is created and the
541
 * meaning is assigned to it. If it's already defined in the current scope,
542
 * an error is reported via cf_error().
543
 *
544
 * Result: Pointer to the newly defined symbol. If we are in the top-level
545
 * scope, it's the same @sym as passed to the function.
546
 */
547
struct symbol *
548
cf_define_symbol(struct symbol *sym, int type, void *def)
549
{
550
  if (sym->class)
551
    {
552
      if (sym->scope == conf_this_scope)
553
	cf_error("Symbol already defined");
554
      sym = cf_new_sym(sym->name, cf_hash(sym->name));
555
    }
556
  sym->class = type;
557
  sym->def = def;
558
  return sym;
559
}
560

    
561
static void
562
cf_lex_init_kh(void)
563
{
564
  struct keyword *k;
565

    
566
  for(k=keyword_list; k->name; k++)
567
    {
568
      unsigned h = cf_hash(k->name) & (KW_HASH_SIZE-1);
569
      k->next = kw_hash[h];
570
      kw_hash[h] = k;
571
    }
572
  kw_hash_inited = 1;
573
}
574

    
575
/**
576
 * cf_lex_init - initialize the lexer
577
 * @is_cli: true if we're going to parse CLI command, false for configuration
578
 *
579
 * cf_lex_init() initializes the lexical analyzer and prepares it for
580
 * parsing of a new input.
581
 */
582
void
583
cf_lex_init(int is_cli, struct config *c)
584
{
585
  if (!kw_hash_inited)
586
    cf_lex_init_kh();
587

    
588
  ifs_head = ifs = push_ifs(NULL);
589
  if (!is_cli) 
590
    {
591
      ifs->file_name = c->file_name;
592
      ifs->fd = c->file_fd;
593
      ifs->depth = 1;
594
    }
595

    
596
  yyrestart(NULL);
597
  ifs->buffer = YY_CURRENT_BUFFER;
598

    
599
  if (is_cli)
600
    BEGIN(CLI);
601
  else
602
    BEGIN(INITIAL);
603

    
604
  conf_this_scope = cfg_allocz(sizeof(struct sym_scope));
605
  conf_this_scope->active = 1;
606
}
607

    
608
/**
609
 * cf_push_scope - enter new scope
610
 * @sym: symbol representing scope name
611
 *
612
 * If we want to enter a new scope to process declarations inside
613
 * a nested block, we can just call cf_push_scope() to push a new
614
 * scope onto the scope stack which will cause all new symbols to be
615
 * defined in this scope and all existing symbols to be sought for
616
 * in all scopes stored on the stack.
617
 */
618
void
619
cf_push_scope(struct symbol *sym)
620
{
621
  struct sym_scope *s = cfg_alloc(sizeof(struct sym_scope));
622

    
623
  s->next = conf_this_scope;
624
  conf_this_scope = s;
625
  s->active = 1;
626
  s->name = sym;
627
}
628

    
629
/**
630
 * cf_pop_scope - leave a scope
631
 *
632
 * cf_pop_scope() pops the topmost scope from the scope stack,
633
 * leaving all its symbols in the symbol table, but making them
634
 * invisible to the rest of the config.
635
 */
636
void
637
cf_pop_scope(void)
638
{
639
  conf_this_scope->active = 0;
640
  conf_this_scope = conf_this_scope->next;
641
  ASSERT(conf_this_scope);
642
}
643

    
644
struct symbol *
645
cf_walk_symbols(struct config *cf, struct symbol *sym, int *pos)
646
{
647
  for(;;)
648
    {
649
      if (!sym)
650
	{
651
	  if (*pos >= SYM_HASH_SIZE)
652
	    return NULL;
653
	  sym = cf->sym_hash[(*pos)++];
654
	}
655
      else
656
	sym = sym->next;
657
      if (sym && sym->scope->active)
658
	return sym;
659
    }
660
}
661

    
662
/**
663
 * cf_symbol_class_name - get name of a symbol class
664
 * @sym: symbol
665
 *
666
 * This function returns a string representing the class
667
 * of the given symbol.
668
 */
669
char *
670
cf_symbol_class_name(struct symbol *sym)
671
{
672
  if (cf_symbol_is_constant(sym))
673
    return "constant";
674

    
675
  switch (sym->class)
676
    {
677
    case SYM_VOID:
678
      return "undefined";
679
    case SYM_PROTO:
680
      return "protocol";
681
    case SYM_TEMPLATE:
682
      return "protocol template";
683
    case SYM_FUNCTION:
684
      return "function";
685
    case SYM_FILTER:
686
      return "filter";
687
    case SYM_TABLE:
688
      return "routing table";
689
    case SYM_ROA:
690
      return "ROA table";
691
    default:
692
      return "unknown type";
693
    }
694
}
695

    
696

    
697
/**
698
 * DOC: Parser
699
 *
700
 * Both the configuration and CLI commands are analyzed using a syntax
701
 * driven parser generated by the |bison| tool from a grammar which
702
 * is constructed from information gathered from grammar snippets by
703
 * the |gen_parser.m4| script.
704
 *
705
 * Grammar snippets are files (usually with extension |.Y|) contributed
706
 * by various BIRD modules in order to provide information about syntax of their
707
 * configuration and their CLI commands. Each snipped consists of several
708
 * sections, each of them starting with a special keyword: |CF_HDR| for
709
 * a list of |#include| directives needed by the C code, |CF_DEFINES|
710
 * for a list of C declarations, |CF_DECLS| for |bison| declarations
711
 * including keyword definitions specified as |CF_KEYWORDS|, |CF_GRAMMAR|
712
 * for the grammar rules, |CF_CODE| for auxiliary C code and finally
713
 * |CF_END| at the end of the snippet.
714
 *
715
 * To create references between the snippets, it's possible to define
716
 * multi-part rules by utilizing the |CF_ADDTO| macro which adds a new
717
 * alternative to a multi-part rule.
718
 *
719
 * CLI commands are defined using a |CF_CLI| macro. Its parameters are:
720
 * the list of keywords determining the command, the list of parameters,
721
 * help text for the parameters and help text for the command.
722
 *
723
 * Values of |enum| filter types can be defined using |CF_ENUM| with
724
 * the following parameters: name of filter type, prefix common for all
725
 * literals of this type and names of all the possible values.
726
 */