From 1abe0486bbb51c9c8517f8d5a347c6f46830de89 Mon Sep 17 00:00:00 2001 From: =?utf8?q?Kristian=20H=C3=B8gsberg?= Date: Sat, 21 Sep 2013 23:02:31 -0700 Subject: [PATCH] config-parser: Make weston_config_parse() tkae a file name Take a basename of the config file to parse instead of an fd. --- clients/desktop-shell.c | 6 +----- clients/tablet-shell.c | 6 +----- clients/terminal.c | 6 +----- clients/window.c | 7 ++----- shared/config-parser.c | 19 +++++++++++++------ shared/config-parser.h | 5 +---- src/compositor.c | 7 ++----- tests/config-parser-test.c | 2 +- 8 files changed, 22 insertions(+), 36 deletions(-) diff --git a/clients/desktop-shell.c b/clients/desktop-shell.c index 381f387..599c0a5 100644 --- a/clients/desktop-shell.c +++ b/clients/desktop-shell.c @@ -1280,17 +1280,13 @@ panel_add_launchers(struct panel *panel, struct desktop *desktop) int main(int argc, char *argv[]) { struct desktop desktop = { 0 }; - int config_fd; struct output *output; struct weston_config_section *s; desktop.unlock_task.run = unlock_dialog_finish; wl_list_init(&desktop.outputs); - config_fd = open_config_file("weston.ini"); - desktop.config = weston_config_parse(config_fd); - close(config_fd); - + desktop.config = weston_config_parse("weston.ini"); s = weston_config_get_section(desktop.config, "shell", NULL, NULL); weston_config_section_get_bool(s, "locking", &desktop.locking, 1); diff --git a/clients/tablet-shell.c b/clients/tablet-shell.c index 7af6ac8..094823c 100644 --- a/clients/tablet-shell.c +++ b/clients/tablet-shell.c @@ -412,7 +412,6 @@ int main(int argc, char *argv[]) { struct tablet tablet = { 0 }; struct display *display; - int config_fd; struct output *output; struct weston_config *config; struct weston_config_section *s; @@ -438,10 +437,7 @@ int main(int argc, char *argv[]) wl_list_init(&tablet.homescreen->launcher_list); - config_fd = open_config_file("weston.ini"); - config = weston_config_parse(config_fd); - close(config_fd); - + config = weston_config_parse("weston.ini"); s = weston_config_get_section(config, "shell", NULL, NULL); weston_config_section_get_string(s, "lockscreen-icon", &key_lockscreen_icon, NULL); diff --git a/clients/terminal.c b/clients/terminal.c index c45c776..cec1d67 100644 --- a/clients/terminal.c +++ b/clients/terminal.c @@ -2793,7 +2793,6 @@ int main(int argc, char *argv[]) struct terminal *terminal; struct weston_config *config; struct weston_config_section *s; - int config_fd; /* as wcwidth is locale-dependent, wcwidth needs setlocale call to function properly. */ @@ -2803,10 +2802,7 @@ int main(int argc, char *argv[]) if (!option_shell) option_shell = "/bin/bash"; - config_fd = open_config_file("weston.ini"); - config = weston_config_parse(config_fd); - close(config_fd); - + config = weston_config_parse("weston.ini"); s = weston_config_get_section(config, "terminal", NULL, NULL); weston_config_section_get_string(s, "font", &option_font, "mono"); weston_config_section_get_int(s, "font-size", &option_font_size, 14); diff --git a/clients/window.c b/clients/window.c index 8d4ee3a..5b20da5 100644 --- a/clients/window.c +++ b/clients/window.c @@ -1309,15 +1309,12 @@ create_cursors(struct display *display) { struct weston_config *config; struct weston_config_section *s; - int config_fd, size; + int size; char *theme = NULL; unsigned int i, j; struct wl_cursor *cursor; - config_fd = open_config_file("weston.ini"); - config = weston_config_parse(config_fd); - close(config_fd); - + config = weston_config_parse("weston.ini"); s = weston_config_get_section(config, "shell", NULL, NULL); weston_config_section_get_string(s, "cursor-theme", &theme, NULL); weston_config_section_get_int(s, "cursor-size", &size, 32); diff --git a/shared/config-parser.c b/shared/config-parser.c index d5491c2..1cee946 100644 --- a/shared/config-parser.c +++ b/shared/config-parser.c @@ -41,7 +41,7 @@ const __typeof__( ((type *)0)->member ) *__mptr = (ptr); \ (type *)( (char *)__mptr - offsetof(type,member) );}) -int +static int open_config_file(const char *name) { const char *config_dir = getenv("XDG_CONFIG_HOME"); @@ -51,6 +51,9 @@ open_config_file(const char *name) const char *p, *next; int fd; + if (name[0] == '/') + return open(name, O_RDONLY | O_CLOEXEC); + /* 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. */ @@ -312,13 +315,13 @@ section_add_entry(struct weston_config_section *section, } struct weston_config * -weston_config_parse(int fd) +weston_config_parse(const char *name) { FILE *fp; char line[512], *p; struct weston_config *config; struct weston_config_section *section = NULL; - int i; + int i, fd; config = malloc(sizeof *config); if (config == NULL) @@ -326,13 +329,17 @@ weston_config_parse(int fd) wl_list_init(&config->section_list); - fp = fdopen(dup(fd), "r"); - if (fp == NULL) { + fd = open_config_file(name); + if (fd == -1) { free(config); return NULL; } - rewind(fp); + fp = fdopen(fd, "r"); + if (fp == NULL) { + free(config); + return NULL; + } while (fgets(line, sizeof line, fp)) { switch (line[0]) { diff --git a/shared/config-parser.h b/shared/config-parser.h index fc6195b..56e390f 100644 --- a/shared/config-parser.h +++ b/shared/config-parser.h @@ -47,9 +47,6 @@ struct config_section { void (*done)(void *data); }; -int -open_config_file(const char *name); - enum weston_option_type { WESTON_OPTION_INTEGER, WESTON_OPTION_UNSIGNED_INTEGER, @@ -96,7 +93,7 @@ weston_config_section_get_bool(struct weston_config_section *section, const char *key, int *value, int default_value); struct weston_config * -weston_config_parse(int fd); +weston_config_parse(const char *name); void weston_config_destroy(struct weston_config *config); diff --git a/src/compositor.c b/src/compositor.c index f94392c..0abc93b 100644 --- a/src/compositor.c +++ b/src/compositor.c @@ -3418,7 +3418,7 @@ int main(int argc, char *argv[]) *(*backend_init)(struct wl_display *display, int *argc, char *argv[], struct weston_config *config); - int i, config_fd; + int i; char *backend = NULL; char *shell = NULL; char *modules, *option_modules = NULL; @@ -3486,10 +3486,7 @@ int main(int argc, char *argv[]) backend = WESTON_NATIVE_BACKEND; } - config_fd = open_config_file("weston.ini"); - config = weston_config_parse(config_fd); - close(config_fd); - + config = weston_config_parse("weston.ini"); section = weston_config_get_section(config, "core", NULL, NULL); weston_config_section_get_string(section, "modules", &modules, ""); diff --git a/tests/config-parser-test.c b/tests/config-parser-test.c index 83e89ba..4b8fc7e 100644 --- a/tests/config-parser-test.c +++ b/tests/config-parser-test.c @@ -40,7 +40,7 @@ run_test(const char *text) len = write(fd, text, strlen(text)); assert(len == (int) strlen(text)); - config = weston_config_parse(fd); + config = weston_config_parse(file); close(fd); unlink(file); -- 2.7.4