137 lines
3.4 KiB
C++
137 lines
3.4 KiB
C++
/*
|
|
Copyright (c) 2013, Matthias Schiffer <mschiffer@universe-factory.net>
|
|
All rights reserved.
|
|
|
|
Redistribution and use in source and binary forms, with or without
|
|
modification, are permitted provided that the following conditions are met:
|
|
|
|
1. Redistributions of source code must retain the above copyright notice,
|
|
this list of conditions and the following disclaimer.
|
|
2. Redistributions in binary form must reproduce the above copyright notice,
|
|
this list of conditions and the following disclaimer in the documentation
|
|
and/or other materials provided with the distribution.
|
|
|
|
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
|
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
|
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
|
|
FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
|
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
|
|
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
|
|
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
|
|
OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
|
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
*/
|
|
|
|
|
|
#include "mmss.hpp"
|
|
#include <config.ll.hpp>
|
|
#include <config.yy.hpp>
|
|
|
|
#include <stack>
|
|
|
|
#include <libgen.h>
|
|
|
|
|
|
namespace MMSS {
|
|
|
|
namespace Config {
|
|
|
|
void add_network(context_t *mmss, config_t *conf, const char *name) {
|
|
logf(mmss, LOG_NOTICE, "adding network `%s'", name);
|
|
|
|
network_t *net = new network_t;
|
|
|
|
net->name = strdup(name);
|
|
net->mtu = 1500;
|
|
|
|
net->next = conf->networks;
|
|
conf->networks = net;
|
|
}
|
|
|
|
}
|
|
|
|
bool read_config(context_t *mmss, config_t *conf, const char *filename) {
|
|
bool ret = true;
|
|
char *oldcwd = get_current_dir_name();
|
|
char *filename2 = NULL;
|
|
char *dir = NULL;
|
|
FILE *file;
|
|
yyscan_t scanner;
|
|
mmss_config_pstate *ps;
|
|
int token;
|
|
YYSTYPE token_val;
|
|
YYLTYPE loc = {1, 0, 1, 0};
|
|
int parse_ret;
|
|
std::stack<char*> strings;
|
|
|
|
|
|
mmss_config_yylex_init(&scanner);
|
|
ps = mmss_config_pstate_new();
|
|
|
|
if (!filename) {
|
|
file = stdin;
|
|
}
|
|
else {
|
|
file = fopen(filename, "r");
|
|
if (!file) {
|
|
logf(mmss, LOG_ERR, "can't open config file `%s': %s", filename, strerror(errno));
|
|
ret = false;
|
|
goto end_free;
|
|
}
|
|
}
|
|
|
|
mmss_config_yyset_in(file, scanner);
|
|
|
|
if (filename) {
|
|
filename2 = strdup(filename);
|
|
dir = dirname(filename2);
|
|
|
|
if (chdir(dir)) {
|
|
logf(mmss, LOG_ERR, "change from directory `%s' to `%s' failed", oldcwd, dir);
|
|
ret = false;
|
|
goto end_free;
|
|
}
|
|
}
|
|
|
|
do {
|
|
token = mmss_config_yylex(&token_val, &loc, scanner);
|
|
|
|
if (token < 0) {
|
|
logf(mmss, LOG_ERR, "config error: %s at %s:%i:%i", token_val.error, filename, loc.first_line, loc.first_column);
|
|
ret = false;
|
|
goto end_free;
|
|
}
|
|
|
|
if (token == TOK_STRING)
|
|
strings.push(token_val.str);
|
|
|
|
|
|
parse_ret = mmss_config_push_parse(ps, token, &token_val, &loc, mmss, conf, filename);
|
|
} while (parse_ret == YYPUSH_MORE);
|
|
|
|
if (parse_ret)
|
|
ret = false;
|
|
|
|
end_free:
|
|
while (!strings.empty()) {
|
|
std::free(strings.top());
|
|
strings.pop();
|
|
}
|
|
|
|
mmss_config_pstate_delete(ps);
|
|
mmss_config_yylex_destroy(scanner);
|
|
|
|
if(chdir(oldcwd))
|
|
logf(mmss, LOG_ERR, "can't chdir to `%s': %s", oldcwd, strerror(errno));
|
|
|
|
free(filename2);
|
|
free(oldcwd);
|
|
|
|
if (filename && file)
|
|
fclose(file);
|
|
|
|
return ret;
|
|
}
|
|
|
|
}
|