iof-bird-daemon / conf / cf-lex.l @ 62e64905
History | View | Annotate | Download (17.1 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 |
[02]:{DIGIT}+:{DIGIT}+ { |
127 |
unsigned long int l, len1, len2; |
128 |
char *e; |
129 |
|
130 |
if (yytext[0] == '0') |
131 |
{ |
132 |
cf_lval.i64 = 0; |
133 |
len1 = 16; |
134 |
len2 = 32; |
135 |
} |
136 |
else |
137 |
{ |
138 |
cf_lval.i64 = 2ULL << 48; |
139 |
len1 = 32; |
140 |
len2 = 16; |
141 |
} |
142 |
|
143 |
errno = 0; |
144 |
l = strtoul(yytext+2, &e, 10); |
145 |
if (e && (*e != ':') || (errno == ERANGE) || (l >> len1)) |
146 |
cf_error("ASN out of range"); |
147 |
cf_lval.i64 |= ((u64) l) << len2; |
148 |
|
149 |
errno = 0; |
150 |
l = strtoul(e+1, &e, 10); |
151 |
if (e && *e || (errno == ERANGE) || (l >> len2)) |
152 |
cf_error("Number out of range"); |
153 |
cf_lval.i64 |= l; |
154 |
|
155 |
return VPN_RD; |
156 |
} |
157 |
|
158 |
1:{DIGIT}+\.{DIGIT}+\.{DIGIT}+\.{DIGIT}+:{DIGIT}+ { |
159 |
unsigned long int l; |
160 |
ip4_addr ip4; |
161 |
char *e; |
162 |
|
163 |
cf_lval.i64 = 1ULL << 48; |
164 |
|
165 |
e = strchr(yytext+2, ':'); |
166 |
*e++ = '\0'; |
167 |
if (!ip4_pton(yytext+2, &ip4)) |
168 |
cf_error("Invalid IPv4 address %s in Route Distinguisher", yytext+2); |
169 |
cf_lval.i64 |= ((u64) ip4_to_u32(ip4)) << 16; |
170 |
|
171 |
errno = 0; |
172 |
l = strtoul(e, &e, 10); |
173 |
if (e && *e || (errno == ERANGE) || (l >> 16)) |
174 |
cf_error("Number out of range"); |
175 |
cf_lval.i64 |= l; |
176 |
|
177 |
return VPN_RD; |
178 |
} |
179 |
|
180 |
{DIGIT}+\.{DIGIT}+\.{DIGIT}+\.{DIGIT}+ { |
181 |
if (!ip4_pton(yytext, &cf_lval.ip4)) |
182 |
cf_error("Invalid IPv4 address %s", yytext); |
183 |
return IP4; |
184 |
} |
185 |
|
186 |
({XIGIT}*::|({XIGIT}*:){3,})({XIGIT}*|{DIGIT}+\.{DIGIT}+\.{DIGIT}+\.{DIGIT}+) { |
187 |
if (!ip6_pton(yytext, &cf_lval.ip6)) |
188 |
cf_error("Invalid IPv6 address %s", yytext); |
189 |
return IP6; |
190 |
} |
191 |
|
192 |
0x{XIGIT}+ { |
193 |
char *e; |
194 |
unsigned long int l; |
195 |
errno = 0; |
196 |
l = strtoul(yytext+2, &e, 16); |
197 |
if (e && *e || errno == ERANGE || (unsigned long int)(unsigned int) l != l) |
198 |
cf_error("Number out of range"); |
199 |
cf_lval.i = l; |
200 |
return NUM; |
201 |
} |
202 |
|
203 |
{DIGIT}+ { |
204 |
char *e; |
205 |
unsigned long int l; |
206 |
errno = 0; |
207 |
l = strtoul(yytext, &e, 10); |
208 |
if (e && *e || errno == ERANGE || (unsigned long int)(unsigned int) l != l) |
209 |
cf_error("Number out of range"); |
210 |
cf_lval.i = l; |
211 |
return NUM; |
212 |
} |
213 |
|
214 |
else: { |
215 |
/* Hack to distinguish if..else from else: in case */ |
216 |
return ELSECOL; |
217 |
} |
218 |
|
219 |
({ALPHA}{ALNUM}*|[']({ALNUM}|[-]|[\.]|[:])*[']) { |
220 |
if(*yytext == '\'') { |
221 |
yytext[yyleng-1] = 0; |
222 |
yytext++; |
223 |
} |
224 |
unsigned int h = cf_hash(yytext); |
225 |
struct keyword *k = kw_hash[h & (KW_HASH_SIZE-1)]; |
226 |
while (k) |
227 |
{ |
228 |
if (!strcmp(k->name, yytext)) |
229 |
{ |
230 |
if (k->value > 0) |
231 |
return k->value; |
232 |
else |
233 |
{ |
234 |
cf_lval.i = -k->value; |
235 |
return ENUM; |
236 |
} |
237 |
} |
238 |
k=k->next; |
239 |
} |
240 |
cf_lval.s = cf_get_sym(yytext, h); |
241 |
return SYM; |
242 |
} |
243 |
|
244 |
<CLI>(.|\n) { |
245 |
BEGIN(INITIAL); |
246 |
return CLI_MARKER; |
247 |
} |
248 |
|
249 |
\.\. { |
250 |
return DDOT; |
251 |
} |
252 |
|
253 |
[={}:;,.()+*/%<>~\[\]?!\|-] { |
254 |
return yytext[0]; |
255 |
} |
256 |
|
257 |
["][^"\n]*["] { |
258 |
yytext[yyleng-1] = 0; |
259 |
cf_lval.t = cfg_strdup(yytext+1); |
260 |
yytext[yyleng-1] = '"'; |
261 |
return TEXT; |
262 |
} |
263 |
|
264 |
["][^"\n]*\n cf_error("Unterminated string"); |
265 |
|
266 |
<INITIAL,COMMENT><<EOF>> { if (check_eof()) return END; } |
267 |
|
268 |
{WHITE}+ |
269 |
|
270 |
\n ifs->lino++; |
271 |
|
272 |
# BEGIN(COMMENT); |
273 |
|
274 |
\/\* BEGIN(CCOMM); |
275 |
|
276 |
. cf_error("Unknown character"); |
277 |
|
278 |
<COMMENT>\n { |
279 |
ifs->lino++; |
280 |
BEGIN(INITIAL); |
281 |
} |
282 |
|
283 |
<COMMENT>. |
284 |
|
285 |
<CCOMM>\*\/ BEGIN(INITIAL); |
286 |
<CCOMM>\n ifs->lino++; |
287 |
<CCOMM>\/\* cf_error("Comment nesting not supported"); |
288 |
<CCOMM><<EOF>> cf_error("Unterminated comment"); |
289 |
<CCOMM>. |
290 |
|
291 |
\!\= return NEQ; |
292 |
\!\~ return NMA; |
293 |
\<\= return LEQ; |
294 |
\>\= return GEQ; |
295 |
\&\& return AND; |
296 |
\|\| return OR; |
297 |
|
298 |
\[\= return PO; |
299 |
\=\] return PC; |
300 |
|
301 |
%% |
302 |
|
303 |
static int |
304 |
cf_hash(byte *c) |
305 |
{ |
306 |
unsigned int h = 13; |
307 |
|
308 |
while (*c) |
309 |
h = (h * 37) + *c++; |
310 |
return h; |
311 |
} |
312 |
|
313 |
|
314 |
/* |
315 |
* IFS stack - it contains structures needed for recursive processing |
316 |
* of include in config files. On the top of the stack is a structure |
317 |
* for currently processed file. Other structures are either for |
318 |
* active files interrupted because of include directive (these have |
319 |
* fd and flex buffer) or for inactive files scheduled to be processed |
320 |
* later (when parent requested including of several files by wildcard |
321 |
* match - these do not have fd and flex buffer yet). |
322 |
* |
323 |
* FIXME: Most of these ifs and include functions are really sysdep/unix. |
324 |
*/ |
325 |
|
326 |
static struct include_file_stack * |
327 |
push_ifs(struct include_file_stack *old) |
328 |
{ |
329 |
struct include_file_stack *ret; |
330 |
ret = cfg_allocz(sizeof(struct include_file_stack)); |
331 |
ret->lino = 1; |
332 |
ret->prev = old; |
333 |
return ret; |
334 |
} |
335 |
|
336 |
static struct include_file_stack * |
337 |
pop_ifs(struct include_file_stack *old) |
338 |
{ |
339 |
yy_delete_buffer(old->buffer); |
340 |
close(old->fd); |
341 |
return old->prev; |
342 |
} |
343 |
|
344 |
static void |
345 |
enter_ifs(struct include_file_stack *new) |
346 |
{ |
347 |
if (!new->buffer) |
348 |
{ |
349 |
new->fd = open(new->file_name, O_RDONLY); |
350 |
if (new->fd < 0) |
351 |
{ |
352 |
ifs = ifs->up; |
353 |
cf_error("Unable to open included file %s: %m", new->file_name); |
354 |
} |
355 |
|
356 |
new->buffer = yy_create_buffer(NULL, YY_BUF_SIZE); |
357 |
} |
358 |
|
359 |
yy_switch_to_buffer(new->buffer); |
360 |
} |
361 |
|
362 |
/** |
363 |
* cf_lex_unwind - unwind lexer state during error |
364 |
* |
365 |
* cf_lex_unwind() frees the internal state on IFS stack when the lexical |
366 |
* analyzer is terminated by cf_error(). |
367 |
*/ |
368 |
void |
369 |
cf_lex_unwind(void) |
370 |
{ |
371 |
struct include_file_stack *n; |
372 |
|
373 |
for (n = ifs; n != ifs_head; n = n->prev) |
374 |
{ |
375 |
/* Memory is freed automatically */ |
376 |
if (n->buffer) |
377 |
yy_delete_buffer(n->buffer); |
378 |
if (n->fd) |
379 |
close(n->fd); |
380 |
} |
381 |
|
382 |
ifs = ifs_head; |
383 |
} |
384 |
|
385 |
static void |
386 |
cf_include(char *arg, int alen) |
387 |
{ |
388 |
struct include_file_stack *base_ifs = ifs; |
389 |
int new_depth, rv, i; |
390 |
char *patt; |
391 |
glob_t g = {}; |
392 |
|
393 |
new_depth = ifs->depth + 1; |
394 |
if (new_depth > MAX_INCLUDE_DEPTH) |
395 |
cf_error("Max include depth reached"); |
396 |
|
397 |
/* expand arg to properly handle relative filenames */ |
398 |
if (*arg != '/') |
399 |
{ |
400 |
int dlen = strlen(ifs->file_name); |
401 |
char *dir = alloca(dlen + 1); |
402 |
patt = alloca(dlen + alen + 2); |
403 |
memcpy(dir, ifs->file_name, dlen + 1); |
404 |
sprintf(patt, "%s/%s", dirname(dir), arg); |
405 |
} |
406 |
else |
407 |
patt = arg; |
408 |
|
409 |
/* Skip globbing if there are no wildcards, mainly to get proper |
410 |
response when the included config file is missing */ |
411 |
if (!strpbrk(arg, "?*[")) |
412 |
{ |
413 |
ifs = push_ifs(ifs); |
414 |
ifs->file_name = cfg_strdup(patt); |
415 |
ifs->depth = new_depth; |
416 |
ifs->up = base_ifs; |
417 |
enter_ifs(ifs); |
418 |
return; |
419 |
} |
420 |
|
421 |
/* Expand the pattern */ |
422 |
rv = glob(patt, GLOB_ERR | GLOB_NOESCAPE, NULL, &g); |
423 |
if (rv == GLOB_ABORTED) |
424 |
cf_error("Unable to match pattern %s: %m", patt); |
425 |
if ((rv != 0) || (g.gl_pathc <= 0)) |
426 |
return; |
427 |
|
428 |
/* |
429 |
* Now we put all found files to ifs stack in reverse order, they |
430 |
* will be activated and processed in order as ifs stack is popped |
431 |
* by pop_ifs() and enter_ifs() in check_eof(). |
432 |
*/ |
433 |
for(i = g.gl_pathc - 1; i >= 0; i--) |
434 |
{ |
435 |
char *fname = g.gl_pathv[i]; |
436 |
struct stat fs; |
437 |
|
438 |
if (stat(fname, &fs) < 0) |
439 |
{ |
440 |
globfree(&g); |
441 |
cf_error("Unable to stat included file %s: %m", fname); |
442 |
} |
443 |
|
444 |
if (fs.st_mode & S_IFDIR) |
445 |
continue; |
446 |
|
447 |
/* Prepare new stack item */ |
448 |
ifs = push_ifs(ifs); |
449 |
ifs->file_name = cfg_strdup(fname); |
450 |
ifs->depth = new_depth; |
451 |
ifs->up = base_ifs; |
452 |
} |
453 |
|
454 |
globfree(&g); |
455 |
enter_ifs(ifs); |
456 |
} |
457 |
|
458 |
static int |
459 |
check_eof(void) |
460 |
{ |
461 |
if (ifs == ifs_head) |
462 |
{ |
463 |
/* EOF in main config file */ |
464 |
ifs->lino = 1; /* Why this? */ |
465 |
return 1; |
466 |
} |
467 |
|
468 |
ifs = pop_ifs(ifs); |
469 |
enter_ifs(ifs); |
470 |
return 0; |
471 |
} |
472 |
|
473 |
static struct symbol * |
474 |
cf_new_sym(byte *c, uint h0) |
475 |
{ |
476 |
uint h = h0 & (SYM_HASH_SIZE-1); |
477 |
struct symbol *s, **ht; |
478 |
int l; |
479 |
|
480 |
if (!new_config->sym_hash) |
481 |
new_config->sym_hash = cfg_allocz(SYM_HASH_SIZE * sizeof(struct keyword *)); |
482 |
ht = new_config->sym_hash; |
483 |
l = strlen(c); |
484 |
if (l > SYM_MAX_LEN) |
485 |
cf_error("Symbol too long"); |
486 |
s = cfg_alloc(sizeof(struct symbol) + l); |
487 |
s->next = ht[h]; |
488 |
ht[h] = s; |
489 |
s->scope = conf_this_scope; |
490 |
s->class = SYM_VOID; |
491 |
s->def = NULL; |
492 |
s->aux = 0; |
493 |
strcpy(s->name, c); |
494 |
return s; |
495 |
} |
496 |
|
497 |
static struct symbol * |
498 |
cf_find_sym(struct config *cfg, byte *c, uint h0) |
499 |
{ |
500 |
uint h = h0 & (SYM_HASH_SIZE-1); |
501 |
struct symbol *s, **ht; |
502 |
|
503 |
if (ht = cfg->sym_hash) |
504 |
{ |
505 |
for(s = ht[h]; s; s=s->next) |
506 |
if (!strcmp(s->name, c) && s->scope->active) |
507 |
return s; |
508 |
} |
509 |
if (ht = cfg->sym_fallback) |
510 |
{ |
511 |
/* We know only top-level scope is active */ |
512 |
for(s = ht[h]; s; s=s->next) |
513 |
if (!strcmp(s->name, c) && s->scope->active) |
514 |
return s; |
515 |
} |
516 |
|
517 |
return NULL; |
518 |
} |
519 |
|
520 |
static inline struct symbol * |
521 |
cf_get_sym(byte *c, uint h0) |
522 |
{ |
523 |
return cf_find_sym(new_config, c, h0) ?: cf_new_sym(c, h0); |
524 |
} |
525 |
|
526 |
/** |
527 |
* cf_find_symbol - find a symbol by name |
528 |
* @cfg: specificed config |
529 |
* @c: symbol name |
530 |
* |
531 |
* This functions searches the symbol table in the config @cfg for a symbol of |
532 |
* given name. First it examines the current scope, then the second recent one |
533 |
* and so on until it either finds the symbol and returns a pointer to its |
534 |
* &symbol structure or reaches the end of the scope chain and returns %NULL to |
535 |
* signify no match. |
536 |
*/ |
537 |
struct symbol * |
538 |
cf_find_symbol(struct config *cfg, byte *c) |
539 |
{ |
540 |
return cf_find_sym(cfg, c, cf_hash(c)); |
541 |
} |
542 |
|
543 |
/** |
544 |
* cf_get_symbol - get a symbol by name |
545 |
* @c: symbol name |
546 |
* |
547 |
* This functions searches the symbol table of the currently parsed config |
548 |
* (@new_config) for a symbol of given name. It returns either the already |
549 |
* existing symbol or a newly allocated undefined (%SYM_VOID) symbol if no |
550 |
* existing symbol is found. |
551 |
*/ |
552 |
struct symbol * |
553 |
cf_get_symbol(byte *c) |
554 |
{ |
555 |
return cf_get_sym(c, cf_hash(c)); |
556 |
} |
557 |
|
558 |
struct symbol * |
559 |
cf_default_name(char *template, int *counter) |
560 |
{ |
561 |
char buf[SYM_MAX_LEN]; |
562 |
struct symbol *s; |
563 |
char *perc = strchr(template, '%'); |
564 |
|
565 |
for(;;) |
566 |
{ |
567 |
bsprintf(buf, template, ++(*counter)); |
568 |
s = cf_get_sym(buf, cf_hash(buf)); |
569 |
if (s->class == SYM_VOID) |
570 |
return s; |
571 |
if (!perc) |
572 |
break; |
573 |
} |
574 |
cf_error("Unable to generate default name"); |
575 |
} |
576 |
|
577 |
/** |
578 |
* cf_define_symbol - define meaning of a symbol |
579 |
* @sym: symbol to be defined |
580 |
* @type: symbol class to assign |
581 |
* @def: class dependent data |
582 |
* |
583 |
* Defines new meaning of a symbol. If the symbol is an undefined |
584 |
* one (%SYM_VOID), it's just re-defined to the new type. If it's defined |
585 |
* in different scope, a new symbol in current scope is created and the |
586 |
* meaning is assigned to it. If it's already defined in the current scope, |
587 |
* an error is reported via cf_error(). |
588 |
* |
589 |
* Result: Pointer to the newly defined symbol. If we are in the top-level |
590 |
* scope, it's the same @sym as passed to the function. |
591 |
*/ |
592 |
struct symbol * |
593 |
cf_define_symbol(struct symbol *sym, int type, void *def) |
594 |
{ |
595 |
if (sym->class) |
596 |
{ |
597 |
if (sym->scope == conf_this_scope) |
598 |
cf_error("Symbol already defined"); |
599 |
sym = cf_new_sym(sym->name, cf_hash(sym->name)); |
600 |
} |
601 |
sym->class = type; |
602 |
sym->def = def; |
603 |
return sym; |
604 |
} |
605 |
|
606 |
static void |
607 |
cf_lex_init_kh(void) |
608 |
{ |
609 |
struct keyword *k; |
610 |
|
611 |
for(k=keyword_list; k->name; k++) |
612 |
{ |
613 |
unsigned h = cf_hash(k->name) & (KW_HASH_SIZE-1); |
614 |
k->next = kw_hash[h]; |
615 |
kw_hash[h] = k; |
616 |
} |
617 |
kw_hash_inited = 1; |
618 |
} |
619 |
|
620 |
/** |
621 |
* cf_lex_init - initialize the lexer |
622 |
* @is_cli: true if we're going to parse CLI command, false for configuration |
623 |
* @c: configuration structure |
624 |
* |
625 |
* cf_lex_init() initializes the lexical analyzer and prepares it for |
626 |
* parsing of a new input. |
627 |
*/ |
628 |
void |
629 |
cf_lex_init(int is_cli, struct config *c) |
630 |
{ |
631 |
if (!kw_hash_inited) |
632 |
cf_lex_init_kh(); |
633 |
|
634 |
ifs_head = ifs = push_ifs(NULL); |
635 |
if (!is_cli) |
636 |
{ |
637 |
ifs->file_name = c->file_name; |
638 |
ifs->fd = c->file_fd; |
639 |
ifs->depth = 1; |
640 |
} |
641 |
|
642 |
yyrestart(NULL); |
643 |
ifs->buffer = YY_CURRENT_BUFFER; |
644 |
|
645 |
if (is_cli) |
646 |
BEGIN(CLI); |
647 |
else |
648 |
BEGIN(INITIAL); |
649 |
|
650 |
conf_this_scope = cfg_allocz(sizeof(struct sym_scope)); |
651 |
conf_this_scope->active = 1; |
652 |
} |
653 |
|
654 |
/** |
655 |
* cf_push_scope - enter new scope |
656 |
* @sym: symbol representing scope name |
657 |
* |
658 |
* If we want to enter a new scope to process declarations inside |
659 |
* a nested block, we can just call cf_push_scope() to push a new |
660 |
* scope onto the scope stack which will cause all new symbols to be |
661 |
* defined in this scope and all existing symbols to be sought for |
662 |
* in all scopes stored on the stack. |
663 |
*/ |
664 |
void |
665 |
cf_push_scope(struct symbol *sym) |
666 |
{ |
667 |
struct sym_scope *s = cfg_alloc(sizeof(struct sym_scope)); |
668 |
|
669 |
s->next = conf_this_scope; |
670 |
conf_this_scope = s; |
671 |
s->active = 1; |
672 |
s->name = sym; |
673 |
} |
674 |
|
675 |
/** |
676 |
* cf_pop_scope - leave a scope |
677 |
* |
678 |
* cf_pop_scope() pops the topmost scope from the scope stack, |
679 |
* leaving all its symbols in the symbol table, but making them |
680 |
* invisible to the rest of the config. |
681 |
*/ |
682 |
void |
683 |
cf_pop_scope(void) |
684 |
{ |
685 |
conf_this_scope->active = 0; |
686 |
conf_this_scope = conf_this_scope->next; |
687 |
ASSERT(conf_this_scope); |
688 |
} |
689 |
|
690 |
struct symbol * |
691 |
cf_walk_symbols(struct config *cf, struct symbol *sym, int *pos) |
692 |
{ |
693 |
for(;;) |
694 |
{ |
695 |
if (!sym) |
696 |
{ |
697 |
if (*pos >= SYM_HASH_SIZE) |
698 |
return NULL; |
699 |
sym = cf->sym_hash[(*pos)++]; |
700 |
} |
701 |
else |
702 |
sym = sym->next; |
703 |
if (sym && sym->scope->active) |
704 |
return sym; |
705 |
} |
706 |
} |
707 |
|
708 |
/** |
709 |
* cf_symbol_class_name - get name of a symbol class |
710 |
* @sym: symbol |
711 |
* |
712 |
* This function returns a string representing the class |
713 |
* of the given symbol. |
714 |
*/ |
715 |
char * |
716 |
cf_symbol_class_name(struct symbol *sym) |
717 |
{ |
718 |
if (cf_symbol_is_constant(sym)) |
719 |
return "constant"; |
720 |
|
721 |
switch (sym->class) |
722 |
{ |
723 |
case SYM_VOID: |
724 |
return "undefined"; |
725 |
case SYM_PROTO: |
726 |
return "protocol"; |
727 |
case SYM_TEMPLATE: |
728 |
return "protocol template"; |
729 |
case SYM_FUNCTION: |
730 |
return "function"; |
731 |
case SYM_FILTER: |
732 |
return "filter"; |
733 |
case SYM_TABLE: |
734 |
return "routing table"; |
735 |
default: |
736 |
return "unknown type"; |
737 |
} |
738 |
} |
739 |
|
740 |
|
741 |
/** |
742 |
* DOC: Parser |
743 |
* |
744 |
* Both the configuration and CLI commands are analyzed using a syntax |
745 |
* driven parser generated by the |bison| tool from a grammar which |
746 |
* is constructed from information gathered from grammar snippets by |
747 |
* the |gen_parser.m4| script. |
748 |
* |
749 |
* Grammar snippets are files (usually with extension |.Y|) contributed |
750 |
* by various BIRD modules in order to provide information about syntax of their |
751 |
* configuration and their CLI commands. Each snipped consists of several |
752 |
* sections, each of them starting with a special keyword: |CF_HDR| for |
753 |
* a list of |#include| directives needed by the C code, |CF_DEFINES| |
754 |
* for a list of C declarations, |CF_DECLS| for |bison| declarations |
755 |
* including keyword definitions specified as |CF_KEYWORDS|, |CF_GRAMMAR| |
756 |
* for the grammar rules, |CF_CODE| for auxiliary C code and finally |
757 |
* |CF_END| at the end of the snippet. |
758 |
* |
759 |
* To create references between the snippets, it's possible to define |
760 |
* multi-part rules by utilizing the |CF_ADDTO| macro which adds a new |
761 |
* alternative to a multi-part rule. |
762 |
* |
763 |
* CLI commands are defined using a |CF_CLI| macro. Its parameters are: |
764 |
* the list of keywords determining the command, the list of parameters, |
765 |
* help text for the parameters and help text for the command. |
766 |
* |
767 |
* Values of |enum| filter types can be defined using |CF_ENUM| with |
768 |
* the following parameters: name of filter type, prefix common for all |
769 |
* literals of this type and names of all the possible values. |
770 |
*/ |