Statistics
| Branch: | Revision:

iof-bird-daemon / conf / cf-lex.l @ 61e67253

History | View | Annotate | Download (16.3 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 <stdint.h>
34
#include <unistd.h>
35
#include <libgen.h>
36
#include <glob.h>
37
#include <fcntl.h>
38
#include <sys/stat.h>
39
#include <sys/types.h>
40
#include <sys/stat.h>
41

    
42
#define PARSER 1
43

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

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

    
58
#include "conf/keywords.h"
59

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

    
64
#define SYM_HASH_SIZE 128
65

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

    
73
static int cf_hash(byte *c);
74
static inline struct symbol * cf_get_sym(byte *c, uint h0);
75

    
76
linpool *cfg_mem;
77

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

    
82
#define MAX_INCLUDE_DEPTH 8
83

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

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

    
91
%}
92

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

    
98
%x COMMENT CCOMM CLI
99

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

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

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

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

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

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

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

    
126
{DIGIT}+\.{DIGIT}+\.{DIGIT}+\.{DIGIT}+ {
127
  ip4_addr a;
128
  if (!ip4_pton(yytext, &a))
129
    cf_error("Invalid IPv4 address %s", yytext);
130

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

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

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

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

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

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

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

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

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

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

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

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

    
225
{WHITE}+
226

    
227
\n	ifs->lino++;
228

    
229
#	BEGIN(COMMENT);
230

    
231
\/\*	BEGIN(CCOMM);
232

    
233
.	cf_error("Unknown character");
234

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

    
240
<COMMENT>.
241

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

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

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

    
257
%%
258

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

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

    
269

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

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

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

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

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

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

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

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

    
338
  ifs = ifs_head;
339
}
340

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

    
473
  return NULL;
474
}
475

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

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

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

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

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

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

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

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

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

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

    
598
  yyrestart(NULL);
599
  ifs->buffer = YY_CURRENT_BUFFER;
600

    
601
  if (is_cli)
602
    BEGIN(CLI);
603
  else
604
    BEGIN(INITIAL);
605

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

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

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

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

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

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

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

    
698

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