summaryrefslogtreecommitdiffstats
path: root/src/daemon
diff options
context:
space:
mode:
authorGravatar Gediminas Jakutis <gediminas@varciai.lt> 2019-05-19 17:17:05 +0300
committerGravatar Gediminas Jakutis <gediminas@varciai.lt> 2019-05-19 17:17:05 +0300
commitf09430b7de693d56d3ea0510501150227e3816cd (patch)
tree4327daa347568a7b0894d9b20cafed075225b17b /src/daemon
parenta0a0c69ef32df5734a96c4a2abb58c385c2058b5 (diff)
parent0389fdc7507238221a9747c5fcbca26952e3229c (diff)
downloadusurpation-f09430b7de693d56d3ea0510501150227e3816cd.tar.gz
usurpation-f09430b7de693d56d3ea0510501150227e3816cd.tar.bz2
usurpation-f09430b7de693d56d3ea0510501150227e3816cd.zip
Merge branch '32-settingsv2'
Signed-off-by: Gediminas Jakutis <gediminas@varciai.lt>
Diffstat (limited to 'src/daemon')
-rw-r--r--src/daemon/settings.c94
-rw-r--r--src/daemon/settings_private.h15
2 files changed, 109 insertions, 0 deletions
diff --git a/src/daemon/settings.c b/src/daemon/settings.c
index 0fb1648..2b07b98 100644
--- a/src/daemon/settings.c
+++ b/src/daemon/settings.c
@@ -21,7 +21,13 @@
#define _GNU_SOURCE
+#include <sys/types.h>
+#include <sys/stat.h>
+#include <unistd.h>
+#include <fcntl.h>
+#include <stdlib.h>
#include <string.h>
+#include <stdio.h>
#include <errno.h>
#include "settings.h"
#include "settings_private.h"
@@ -39,6 +45,11 @@ void settings_init(void)
settings.im_user = "user";
settings.im_password = "password";
settings.im_proto = "prpl-irc";
+#define macro2str(a) _macro2str(a)
+#define _macro2str(a) #a
+ setting_readconf("/" macro2str(SYSCONFDIR) "/usurpation.conf");
+#undef macro2str
+#undef _macro2str
}
int setting_detach(void)
@@ -96,3 +107,86 @@ static void unset_flag(unsigned int flag)
{
settings.flags &= ~flag;
}
+
+/* TODO: semi-stub! */
+static int setting_readconf(const char * const path)
+{
+ struct stat filestat;
+ char *buf;
+ char *line;
+ char *entry;
+ char *value;
+ char *line_state;
+ char *token_state;
+ int fd;
+
+
+ if ((fd = open(path, O_RDONLY)) == -1) {
+ if (USURP_VERBOSITY >= 2) {
+ perror("error while opening the configuration file");
+ return 1;
+ }
+ }
+
+ fstat(fd, &filestat);
+ buf = malloc(filestat.st_size);
+
+ if (read(fd, buf, filestat.st_size) != filestat.st_size) {
+ if (USURP_VERBOSITY >= 2) {
+ perror("partial read of configuration file detected");
+ free(buf);
+ return 1;
+ }
+ }
+
+ line = strtok_r(buf, "\n", &line_state);
+
+ while (line) {
+ if (line[0] != '#') {
+ entry = strtok_r(line, "=", &token_state);
+ value = strtok_r(NULL, "=", &token_state);
+ setting_handle_config_entry(entry, value);
+ }
+
+ line = strtok_r(NULL, "\n", &line_state);
+ }
+
+ free(buf);
+
+ return 0;
+}
+
+static int setting_handle_config_entry(const char * const entry, const char * const value)
+{
+ size_t i;
+
+ for (i = 0; i < sizeof(ent_table)/sizeof(*ent_table); ++i) {
+ if (!(strcmp(entry, ent_table[i].name))) {
+ ent_table[i].set(value);
+ return 0;
+ }
+ }
+
+ return 1;
+}
+
+static void set_daemonize(const void * const arg)
+{
+ const char * const a = arg;
+
+ atoi(a) ? set_flag(flag_daemonize) : unset_flag(flag_daemonize);
+}
+
+static void set_port(const void * const arg)
+{
+ const char * const a = arg;
+
+ settings.port = strtoul(a, NULL, 0);
+}
+
+static void set_verbosity(const void * const arg)
+{
+ const char * const a = arg;
+
+ settings.verboselevel = strtol(a, NULL, 0);
+}
diff --git a/src/daemon/settings_private.h b/src/daemon/settings_private.h
index 33a1543..0b0680e 100644
--- a/src/daemon/settings_private.h
+++ b/src/daemon/settings_private.h
@@ -38,8 +38,23 @@ static struct settings {
char *im_proto;
} settings;
+struct entry_description {
+ const char * const name;
+ void (*set)(const void * const);
+};
+
static int test_flag(unsigned int flag);
static void set_flag(unsigned int flag);
static void unset_flag(unsigned int flag);
+static int setting_readconf(const char * const path);
+static int setting_handle_config_entry(const char * const entry, const char * const value);
+
+static void set_daemonize(const void * const arg);
+static void set_port(const void * const arg);
+static void set_verbosity(const void * const arg);
+
+struct entry_description ent_table[] = {{ "daemonize", set_daemonize },
+ { "port", set_port },
+ { "verbosity", set_verbosity }};
#endif /* USURPATION_SETTINGS_PRIVATE_H */