Statistics
| Branch: | Revision:

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

History | View | Annotate | Download (15.7 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
  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_find_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, unsigned int h)
430
{
431
  struct symbol *s, **ht;
432
  int l;
433

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

    
451
static struct symbol *
452
cf_find_sym(byte *c, unsigned int h0)
453
{
454
  unsigned int h = h0 & (SYM_HASH_SIZE-1);
455
  struct symbol *s, **ht;
456

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

    
473
/**
474
 * cf_find_symbol - find a symbol by name
475
 * @c: symbol name
476
 *
477
 * This functions searches the symbol table for a symbol of given
478
 * name. First it examines the current scope, then the second recent
479
 * one and so on until it either finds the symbol and returns a pointer
480
 * to its &symbol structure or reaches the end of the scope chain
481
 * and returns %NULL to signify no match.
482
 */
483
struct symbol *
484
cf_find_symbol(byte *c)
485
{
486
  return cf_find_sym(c, cf_hash(c));
487
}
488

    
489
struct symbol *
490
cf_default_name(char *template, int *counter)
491
{
492
  char buf[32];
493
  struct symbol *s;
494
  char *perc = strchr(template, '%');
495

    
496
  for(;;)
497
    {
498
      bsprintf(buf, template, ++(*counter));
499
      s = cf_find_sym(buf, cf_hash(buf));
500
      if (!s)
501
	break;
502
      if (s->class == SYM_VOID)
503
	return s;
504
      if (!perc)
505
	break;
506
    }
507
  cf_error("Unable to generate default name");
508
}
509

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

    
539
static void
540
cf_lex_init_kh(void)
541
{
542
  struct keyword *k;
543

    
544
  for(k=keyword_list; k->name; k++)
545
    {
546
      unsigned h = cf_hash(k->name) & (KW_HASH_SIZE-1);
547
      k->next = kw_hash[h];
548
      kw_hash[h] = k;
549
    }
550
  kw_hash_inited = 1;
551
}
552

    
553
/**
554
 * cf_lex_init - initialize the lexer
555
 * @is_cli: true if we're going to parse CLI command, false for configuration
556
 *
557
 * cf_lex_init() initializes the lexical analyzer and prepares it for
558
 * parsing of a new input.
559
 */
560
void
561
cf_lex_init(int is_cli, struct config *c)
562
{
563
  if (!kw_hash_inited)
564
    cf_lex_init_kh();
565

    
566
  ifs_head = ifs = push_ifs(NULL);
567
  if (!is_cli) 
568
    {
569
      ifs->file_name = c->file_name;
570
      ifs->fd = c->file_fd;
571
      ifs->depth = 1;
572
    }
573

    
574
  yyrestart(NULL);
575
  ifs->buffer = YY_CURRENT_BUFFER;
576

    
577
  if (is_cli)
578
    BEGIN(CLI);
579
  else
580
    BEGIN(INITIAL);
581

    
582
  conf_this_scope = cfg_allocz(sizeof(struct sym_scope));
583
  conf_this_scope->active = 1;
584
}
585

    
586
/**
587
 * cf_push_scope - enter new scope
588
 * @sym: symbol representing scope name
589
 *
590
 * If we want to enter a new scope to process declarations inside
591
 * a nested block, we can just call cf_push_scope() to push a new
592
 * scope onto the scope stack which will cause all new symbols to be
593
 * defined in this scope and all existing symbols to be sought for
594
 * in all scopes stored on the stack.
595
 */
596
void
597
cf_push_scope(struct symbol *sym)
598
{
599
  struct sym_scope *s = cfg_alloc(sizeof(struct sym_scope));
600

    
601
  s->next = conf_this_scope;
602
  conf_this_scope = s;
603
  s->active = 1;
604
  s->name = sym;
605
}
606

    
607
/**
608
 * cf_pop_scope - leave a scope
609
 *
610
 * cf_pop_scope() pops the topmost scope from the scope stack,
611
 * leaving all its symbols in the symbol table, but making them
612
 * invisible to the rest of the config.
613
 */
614
void
615
cf_pop_scope(void)
616
{
617
  conf_this_scope->active = 0;
618
  conf_this_scope = conf_this_scope->next;
619
  ASSERT(conf_this_scope);
620
}
621

    
622
struct symbol *
623
cf_walk_symbols(struct config *cf, struct symbol *sym, int *pos)
624
{
625
  for(;;)
626
    {
627
      if (!sym)
628
	{
629
	  if (*pos >= SYM_HASH_SIZE)
630
	    return NULL;
631
	  sym = cf->sym_hash[(*pos)++];
632
	}
633
      else
634
	sym = sym->next;
635
      if (sym && sym->scope->active)
636
	return sym;
637
    }
638
}
639

    
640
/**
641
 * cf_symbol_class_name - get name of a symbol class
642
 * @sym: symbol
643
 *
644
 * This function returns a string representing the class
645
 * of the given symbol.
646
 */
647
char *
648
cf_symbol_class_name(struct symbol *sym)
649
{
650
  if (cf_symbol_is_constant(sym))
651
    return "constant";
652

    
653
  switch (sym->class)
654
    {
655
    case SYM_VOID:
656
      return "undefined";
657
    case SYM_PROTO:
658
      return "protocol";
659
    case SYM_TEMPLATE:
660
      return "protocol template";
661
    case SYM_FUNCTION:
662
      return "function";
663
    case SYM_FILTER:
664
      return "filter";
665
    case SYM_TABLE:
666
      return "routing table";
667
    case SYM_ROA:
668
      return "ROA table";
669
    default:
670
      return "unknown type";
671
    }
672
}
673

    
674

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