diff options
Diffstat (limited to 'src')
-rw-r--r-- | src/config-parser.c | 96 | ||||
-rw-r--r-- | src/ico_ivi_common.c | 8 | ||||
-rw-r--r-- | src/ico_ivi_shell.c | 8 | ||||
-rw-r--r-- | src/ico_plugin_loader.c | 10 |
4 files changed, 79 insertions, 43 deletions
diff --git a/src/config-parser.c b/src/config-parser.c index 45ad158..261c425 100644 --- a/src/config-parser.c +++ b/src/config-parser.c @@ -20,11 +20,17 @@ * OF THIS SOFTWARE. */ +#define _GNU_SOURCE /* for stchrnul() */ #include <string.h> #include <stdio.h> #include <stdlib.h> #include <assert.h> #include <ctype.h> +#include <limits.h> +#include <sys/types.h> +#include <sys/stat.h> +#include <fcntl.h> +#include <unistd.h> #include <weston/config-parser.h> @@ -86,7 +92,7 @@ handle_key(const struct config_key *key, const char *value) } int -parse_config_file(const char *path, +parse_config_file(int fd, const struct config_section *sections, int num_sections, void *data) { @@ -95,12 +101,17 @@ parse_config_file(const char *path, const struct config_section *current = NULL; int i; - fp = fopen(path, "r"); + if (fd == -1) + return -1; + + fp = fdopen(dup(fd), "r"); if (fp == NULL) { - fprintf(stderr, "couldn't open %s\n", path); + perror("couldn't open config file"); return -1; } + rewind(fp); + while (fgets(line, sizeof line, fp)) { if (line[0] == '#' || line[0] == '\n') { continue; @@ -151,37 +162,62 @@ parse_config_file(const char *path, return 0; } -char * -config_file_path(const char *name) +int +open_config_file(const char *name) { - const char dotconf[] = "/.config/"; - const char *config_dir; - const char *home_dir; - char *path; - size_t size; - - config_dir = getenv("XDG_CONFIG_HOME"); - if (!config_dir) { - home_dir = getenv("HOME"); - if (!home_dir) { - fprintf(stderr, "HOME is not set, using cwd.\n"); - return strdup(name); - } + const char *config_dir = getenv("XDG_CONFIG_HOME"); + const char *home_dir = getenv("HOME"); + const char *config_dirs = getenv("XDG_CONFIG_DIRS"); + char path[PATH_MAX]; + const char *p, *next; + int fd; + + /* Precedence is given to config files in the home directory, + * and then to directories listed in XDG_CONFIG_DIRS and + * finally to the current working directory. */ + + /* $XDG_CONFIG_HOME */ + if (config_dir) { + snprintf(path, sizeof path, "%s/%s", config_dir, name); + fd = open(path, O_RDONLY | O_CLOEXEC); + if (fd >= 0) + return fd; + } + + /* $HOME/.config */ + if (home_dir) { + snprintf(path, sizeof path, "%s/.config/%s", home_dir, name); + fd = open(path, O_RDONLY | O_CLOEXEC); + if (fd >= 0) + return fd; + } - size = strlen(home_dir) + sizeof dotconf + strlen(name); - path = malloc(size); - if (!path) - return NULL; + /* For each $XDG_CONFIG_DIRS: weston/<config_file> */ + if (!config_dirs) + config_dirs = "/etc/xdg"; /* See XDG base dir spec. */ - snprintf(path, size, "%s%s%s", home_dir, dotconf, name); - return path; + for (p = config_dirs; *p != '\0'; p = next) { + next = strchrnul(p, ':'); + snprintf(path, sizeof path, + "%.*s/weston/%s", (int)(next - p), p, name); + fd = open(path, O_RDONLY | O_CLOEXEC); + if (fd >= 0) + return fd; + + if (*next == ':') + next++; } - size = strlen(config_dir) + 1 + strlen(name) + 1; - path = malloc(size); - if (!path) - return NULL; + /* Current working directory. */ + snprintf(path, sizeof path, "./%s", name); + fd = open(path, O_RDONLY | O_CLOEXEC); + + if (fd >= 0) + fprintf(stderr, + "using config in current working directory: %s\n", + path); + else + fprintf(stderr, "config file \"%s\" not found.\n", name); - snprintf(path, size, "%s/%s", config_dir, name); - return path; + return fd; } diff --git a/src/ico_ivi_common.c b/src/ico_ivi_common.c index c1917f7..5245b04 100644 --- a/src/ico_ivi_common.c +++ b/src/ico_ivi_common.c @@ -307,14 +307,14 @@ ico_ivi_dispname_2_node(const char *dispname) WL_EXPORT int module_init(struct weston_compositor *ec) { - char *config_file; + int config_fd; uifw_info("ico_ivi_common: Enter(module_init)"); /* Get debug level from config file */ - config_file = config_file_path(ICO_IVI_PLUGIN_CONFIG); - parse_config_file(config_file, conf_debug, ARRAY_LENGTH(conf_debug), NULL); - free(config_file); + config_fd = open_config_file(ICO_IVI_PLUGIN_CONFIG); + parse_config_file(config_fd, conf_debug, ARRAY_LENGTH(conf_debug), NULL); + close(config_fd); uifw_info("ico_ivi_common: option flag=0x%08x debug=%d", ico_option_flag(), ico_ivi_debuglevel()); diff --git a/src/ico_ivi_shell.c b/src/ico_ivi_shell.c index e245134..5e69387 100644 --- a/src/ico_ivi_shell.c +++ b/src/ico_ivi_shell.c @@ -183,7 +183,7 @@ get_animation_type(char *animation) static void shell_configuration(struct ivi_shell *shell) { - char *config_file; + int config_fd; char *win_animation = NULL; struct config_key shell_keys[] = { @@ -195,9 +195,9 @@ shell_configuration(struct ivi_shell *shell) { "shell", shell_keys, ARRAY_LENGTH(shell_keys), NULL }, }; - config_file = config_file_path(ICO_IVI_PLUGIN_CONFIG); - parse_config_file(config_file, cs, ARRAY_LENGTH(cs), shell); - free(config_file); + config_fd = open_config_file(ICO_IVI_PLUGIN_CONFIG); + parse_config_file(config_fd, cs, ARRAY_LENGTH(cs), shell); + close(config_fd); shell->win_animation_type = get_animation_type(win_animation); diff --git a/src/ico_plugin_loader.c b/src/ico_plugin_loader.c index dbba8f4..5f3ac61 100644 --- a/src/ico_plugin_loader.c +++ b/src/ico_plugin_loader.c @@ -161,7 +161,7 @@ load_module(struct weston_compositor *ec, const char *path, const char *entry) WL_EXPORT int module_init(struct weston_compositor *ec) { - char *config_file; + int config_fd; char *p; char *end; char buffer[256]; @@ -169,10 +169,10 @@ module_init(struct weston_compositor *ec) uifw_info("ico_plugin_loader: Enter(module_init)"); /* get plugin module name from config file(weston_ivi_plugin.ini) */ - config_file = config_file_path(ICO_IVI_PLUGIN_CONFIG); - parse_config_file(config_file, conf_plugin, ARRAY_LENGTH(conf_plugin), NULL); - parse_config_file(config_file, conf_debug, ARRAY_LENGTH(conf_debug), NULL); - free(config_file); + config_fd = open_config_file(ICO_IVI_PLUGIN_CONFIG); + parse_config_file(config_fd, conf_plugin, ARRAY_LENGTH(conf_plugin), NULL); + parse_config_file(config_fd, conf_debug, ARRAY_LENGTH(conf_debug), NULL); + close(config_fd); if (modules == NULL) { uifw_error("ico_plugin_loader: Leave(No Plugin in config)"); |