};
static struct sym_scope *conf_this_scope;
-int conf_lino;
+#define MAX_INCLUDE_DEPTH 5
+
+static struct include_file_stack *ifs_head;
+static int ifs_depth;
static int cf_hash(byte *c);
static struct symbol *cf_find_sym(byte *c, unsigned int h0);
linpool *cfg_mem;
-int (*cf_read_hook)(byte *buf, unsigned int max);
+int (*cf_read_hook)(byte *buf, unsigned int max, int fd);
+int (*cf_open_hook)(char *filename);
-#define YY_INPUT(buf,result,max) result = cf_read_hook(buf, max);
+#define YY_INPUT(buf,result,max) result = cf_read_hook(buf, max, ifs->conf_fd);
#define YY_NO_UNPUT
#define YY_FATAL_ERROR(msg) cf_error(msg)
+static void new_include(void);
+static int check_eof(void);
+static struct include_file_stack *new_stack(struct include_file_stack *old);
+
%}
%option noyywrap
XIGIT [0-9a-fA-F]
ALNUM [a-zA-Z_0-9]
WHITE [ \t]
+include ^{WHITE}*include{WHITE}*\".*\"{WHITE}*;
%%
+{include} { if(cf_open_hook) new_include(); }
{DIGIT}+\.{DIGIT}+\.{DIGIT}+\.{DIGIT}+ {
#ifdef IPV6
["][^"\n]*\n cf_error("Unterminated string");
-<INITIAL,COMMENT><<EOF>> return END;
+<INITIAL,COMMENT><<EOF>> { if(check_eof()) return END; }
{WHITE}+
-\n conf_lino++;
+\n ifs->conf_lino++;
# BEGIN(COMMENT);
. cf_error("Unknown character");
<COMMENT>\n {
- conf_lino++;
+ ifs->conf_lino++;
BEGIN(INITIAL);
}
<COMMENT>.
<CCOMM>\*\/ BEGIN(INITIAL);
-<CCOMM>\n conf_lino++;
+<CCOMM>\n ifs->conf_lino++;
<CCOMM>\/\* cf_error("Comment nesting not supported");
<CCOMM><<EOF>> cf_error("Unterminated comment");
<CCOMM>.
return h;
}
+/* Open included file with properly swapped buffers */
+static void
+new_include(void)
+{
+ char *fname, *p = NULL;
+
+ if ((fname = strchr(yytext, '"')) != NULL) {
+
+ if ((p = strchr(++fname, '"')) != NULL) *p = '\0';
+
+ if (ifs_depth >= MAX_INCLUDE_DEPTH)
+ cf_error("Max include depth reached.");
+
+ /* Save current stack */
+ ifs->stack = YY_CURRENT_BUFFER;
+ /* Prepare new stack */
+ ifs->next = new_stack(ifs);
+ ifs = ifs->next;
+ strcpy(ifs->conf_fname, fname); /* XXX: strlcpy should be here */
+ ifs->conf_fd = cf_open_hook(fname);
+
+ yy_switch_to_buffer(yy_create_buffer(yyin, YY_BUF_SIZE));
+ }
+}
+
+static int
+check_eof(void)
+{
+ if (ifs == ifs_head) {
+ /* EOF in main config file */
+ ifs->conf_lino = 1;
+ return 1;
+ }
+
+ ifs_depth--;
+ close(ifs->conf_fd);
+ ifs = ifs->prev;
+ ifs->next = NULL;
+
+ yy_delete_buffer(YY_CURRENT_BUFFER);
+ yy_switch_to_buffer(ifs->stack);
+ return 0;
+}
+
static struct symbol *
cf_new_sym(byte *c, unsigned int h)
{
kw_hash_inited = 1;
}
+static struct include_file_stack *
+new_stack(struct include_file_stack *old)
+{
+ struct include_file_stack *ret;
+ ret = cfg_allocz(sizeof(struct include_file_stack));
+ ret->conf_lino = 1;
+ ret->prev = old;
+ return ret;
+}
+
/**
* cf_lex_init - initialize the lexer
* @is_cli: true if we're going to parse CLI command, false for configuration
* parsing of a new input.
*/
void
-cf_lex_init(int is_cli)
+cf_lex_init(int is_cli, struct config *c)
{
if (!kw_hash_inited)
cf_lex_init_kh();
- conf_lino = 1;
+ ifs_head = new_stack(NULL);
+ ifs = ifs_head;
+ ifs_depth = 0;
+ if (!is_cli) {
+ ifs->conf_fd = c->file_fd;
+ ifs_depth = 1;
+ strcpy(ifs->conf_fname, c->file_name);
+ }
yyrestart(NULL);
if (is_cli)
BEGIN(CLI);
cfg_mem = c->mem;
if (setjmp(conf_jmpbuf))
return 0;
- cf_lex_init(0);
+ cf_lex_init(0, c);
sysdep_preconfig(c);
protos_preconfig(c);
rt_preconfig(c);
cfg_mem = c->mem;
if (setjmp(conf_jmpbuf))
return 0;
- cf_lex_init(1);
+ cf_lex_init(1, c);
cf_parse();
return 1;
}
if (bvsnprintf(buf, sizeof(buf), msg, args) < 0)
strcpy(buf, "<bug: error message too long>");
new_config->err_msg = cfg_strdup(buf);
- new_config->err_lino = conf_lino;
+ new_config->err_lino = ifs->conf_lino;
+ new_config->err_file_name = ifs->conf_fname;
longjmp(conf_jmpbuf, 1);
}
#include "lib/resource.h"
#include "lib/timer.h"
+#define BIRD_FNAME_MAX 255 /* Would be better to use some UNIX define */
+
/* Configuration structure */
struct config {
int cli_debug; /* Tracing of CLI connections and commands */
char *err_msg; /* Parser error message */
int err_lino; /* Line containing error */
- char *file_name; /* Name of configuration file */
+ char *err_file_name; /* File name containing error */
+ char *file_name; /* Name of main configuration file */
+ int file_fd; /* File descriptor of main configuration file */
struct symbol **sym_hash; /* Lexer: symbol hash table */
struct symbol **sym_fallback; /* Lexer: fallback symbol hash table */
int obstacle_count; /* Number of items blocking freeing of this config */
/* Lexer */
-extern int (*cf_read_hook)(byte *buf, unsigned int max);
+extern int (*cf_read_hook)(byte *buf, unsigned int max, int fd);
+extern int (*cf_open_hook)(char *filename);
struct symbol {
struct symbol *next;
#define SYM_VARIABLE 0x100 /* 0x100-0x1ff are variable types */
-extern int conf_lino;
+struct include_file_stack {
+ void *stack; /* Internal lexer state */
+ unsigned int conf_lino; /* Current file lineno (at include) */
+ char conf_fname[BIRD_FNAME_MAX]; /* Current file name */
+ int conf_fd; /* Current file descriptor */
+ struct include_file_stack *prev;
+ struct include_file_stack *next;
+};
+
+struct include_file_stack *ifs;
+
int cf_lex(void);
-void cf_lex_init(int is_cli);
+void cf_lex_init(int is_cli, struct config *c);
struct symbol *cf_find_symbol(byte *c);
struct symbol *cf_default_name(char *template, int *counter);
struct symbol *cf_define_symbol(struct symbol *symbol, int type, void *def);
#filter sink { reject; }
#filter okay { accept; }
+#include "filters.conf";
+
# Define another routing table
#table testable;
<sect>Global options
<p><descrip>
+ <tag>include "<m/filename/"</tag>
+ This statement causes inclusion of a new file. The maximal depth is set to 5.
+
<tag>log "<m/filename/"|syslog [name <m/name/]|stderr all|{ <m/list of classes/ }</tag>
Set logging of messages having the given class (either <cf/all/ or <cf/{
error, trace }/ etc.) into selected destination (a file specified as a filename string,
ret = cfg_alloc(sizeof(struct f_inst));
ret->code = ret->aux = 0;
ret->arg1 = ret->arg2 = ret->next = NULL;
- ret->lineno = conf_lino;
+ ret->lineno = ifs->conf_lino;
return ret;
}
test_undef(3);
test_undef(2);
+ print "Testing include";
+ include "test.conf.inc";
+
print "done";
quitbird;
# print "*** FAIL: this is unreachable";
--- /dev/null
+
+print "Entering include";
+print "Should be 2: ", 1+1;
+print "Leaving include";
+
+
bzero(&f, sizeof(f));
f.mem = c->parser_pool;
cf_read_hook = cli_cmd_read_hook;
+ cf_open_hook = NULL;
cli_rh_pos = c->rx_buf;
cli_rh_len = strlen(c->rx_buf);
cli_rh_trick_flag = 0;
#include <pwd.h>
#include <grp.h>
#include <sys/stat.h>
+#include <libgen.h>
#include "nest/bird.h"
#include "lib/lists.h"
#endif // PATH_IPROUTE_DIR
-static int conf_fd;
static char *config_name = PATH_CONFIG;
static int
-cf_read(byte *dest, unsigned int len)
+cf_read(byte *dest, unsigned int len, int fd)
{
- int l = read(conf_fd, dest, len);
+ int l = read(fd, dest, len);
if (l < 0)
cf_error("Read error");
return l;
}
+static int
+cf_open(char *filename)
+{
+ char full_name[BIRD_FNAME_MAX];
+ char *cur = filename;
+ int ret;
+
+ if (*filename != '/') {
+ snprintf(full_name, sizeof(full_name), "%s/%s", dirname(config_name), filename);
+ cur = full_name;
+ }
+
+ if ((ret = open(cur, O_RDONLY)) == -1)
+ cf_error("Unable to open included configuration file: %s", cur);
+}
+
+
void
sysdep_preconfig(struct config *c)
{
int ret;
*cp = conf;
- conf_fd = open(name, O_RDONLY);
- if (conf_fd < 0)
+ conf->file_fd = open(name, O_RDONLY);
+ if (conf->file_fd < 0)
return 0;
cf_read_hook = cf_read;
+ cf_open_hook = cf_open;
ret = config_parse(conf);
- close(conf_fd);
+ close(conf->file_fd);
return ret;
}
if (!unix_read_config(&conf, config_name))
{
if (conf->err_msg)
- die("%s, line %d: %s", config_name, conf->err_lino, conf->err_msg);
+ die("%s, line %d: %s", conf->err_file_name, conf->err_lino, conf->err_msg);
else
die("Unable to open configuration file %s: %m", config_name);
}
if (!unix_read_config(&conf, config_name))
{
if (conf->err_msg)
- log(L_ERR "%s, line %d: %s", config_name, conf->err_lino, conf->err_msg);
+ log(L_ERR "%s, line %d: %s", conf->err_file_name, conf->err_lino, conf->err_msg);
else
log(L_ERR "Unable to open configuration file %s: %m", config_name);
config_free(conf);