summaryrefslogtreecommitdiffstats
path: root/mmss/config.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'mmss/config.cpp')
-rw-r--r--mmss/config.cpp125
1 files changed, 125 insertions, 0 deletions
diff --git a/mmss/config.cpp b/mmss/config.cpp
new file mode 100644
index 0000000..0be95fb
--- /dev/null
+++ b/mmss/config.cpp
@@ -0,0 +1,125 @@
+/*
+ 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 <libgen.h>
+
+
+void mmss_config_add_network(mmss_t *mmss, mmss_config_t *conf, const char *name) {
+ mmss_logf(mmss, LOG_NOTICE, "Adding network `%s'", name);
+
+ mmss_network_t *net = new mmss_network_t;
+
+ net->name = strdup(name);
+ net->mtu = 1500;
+
+ net->next = conf->networks;
+ conf->networks = net;
+}
+
+bool mmss_read_config(mmss_t *mmss, 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;
+ mmss_string_stack_t *strings = NULL;
+ int token;
+ YYSTYPE token_val;
+ YYLTYPE loc = {1, 0, 1, 0};
+ int parse_ret;
+
+
+ mmss_config_yylex_init(&scanner);
+ ps = mmss_config_pstate_new();
+
+ if (!filename) {
+ file = stdin;
+ }
+ else {
+ file = fopen(filename, "r");
+ if (!file) {
+ mmss_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)) {
+ mmss_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) {
+ mmss_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) {
+ token_val.str->next = strings;
+ strings = 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:
+ mmss_string_stack_free(strings);
+
+ mmss_config_pstate_delete(ps);
+ mmss_config_yylex_destroy(scanner);
+
+ if(chdir(oldcwd))
+ mmss_logf(mmss, LOG_ERR, "can't chdir to `%s': %s", oldcwd, strerror(errno));
+
+ free(filename2);
+ free(oldcwd);
+
+ if (filename && file)
+ fclose(file);
+
+ return ret;
+}