diff options
-rw-r--r-- | meson.build | 2 | ||||
-rw-r--r-- | src/daemon/settings.c | 94 | ||||
-rw-r--r-- | src/daemon/settings_private.h | 15 |
3 files changed, 110 insertions, 1 deletions
diff --git a/meson.build b/meson.build index 17319ae..f3fe05e 100644 --- a/meson.build +++ b/meson.build @@ -22,7 +22,7 @@ elif get_option('buildtype') == 'debugoptimized' verb = '3' endif -daemon = executable(progname, d_sources, version, include_directories : inc, install : true, dependencies : deps, extra_files : d_conf, c_args : '-DUSURP_VERBOSITY=' + verb) +daemon = executable(progname, d_sources, version, include_directories : inc, install : true, dependencies : deps, extra_files : d_conf, c_args : ['-DUSURP_VERBOSITY=' + verb, '-DSYSCONFDIR=' + get_option('sysconfdir')]) #install_data(extra, install_dir : resource_dir) install_data(d_conf, install_dir : get_option('sysconfdir')) 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 */ |