From af34025ea52bdf8a33c2b6978953851e7cda95f4 Mon Sep 17 00:00:00 2001 From: Jeffy Chen Date: Wed, 7 Apr 2021 08:25:57 +0800 Subject: [PATCH 47/93] config-parser: Support loading multiple configs Try loading .ini configs under ".d/". Tested with: /etc/xdg/weston/weston.ini.d/99-pixman.ini [core] use-pixman=true Signed-off-by: Jeffy Chen --- compositor/main.c | 5 +- shared/config-parser.c | 146 ++++++++++++++++++++++++++++++++++------- 2 files changed, 127 insertions(+), 24 deletions(-) diff --git a/compositor/main.c b/compositor/main.c index 6ec89f3..7a1cc20 100644 --- a/compositor/main.c +++ b/compositor/main.c @@ -1209,8 +1209,11 @@ load_configuration(struct weston_config **config, int32_t noconfig, if (config_file) file = config_file; - if (noconfig == 0) + if (noconfig == 0) { + setenv("WESTON_MAIN_PARSE", "1", 1); *config = weston_config_parse(file); + unsetenv("WESTON_MAIN_PARSE"); + } if (*config) { full_path = weston_config_get_full_path(*config); diff --git a/shared/config-parser.c b/shared/config-parser.c index 30779ae..e474963 100644 --- a/shared/config-parser.c +++ b/shared/config-parser.c @@ -31,6 +31,7 @@ #include #include #include +#include #include #include #include @@ -70,6 +71,13 @@ open_config_file(struct weston_config *c, const char *name) const char *p, *next; int fd; + if (!c) { + if (name[0] != '/') + return -1; + + return open(name, O_RDONLY | O_CLOEXEC); + } + if (name[0] == '/') { snprintf(c->path, sizeof c->path, "%s", name); return open(name, O_RDONLY | O_CLOEXEC); @@ -337,6 +345,15 @@ config_add_section(struct weston_config *config, const char *name) { struct weston_config_section *section; + /* squash single sessions */ + if (strcmp(name, "launcher") && strcmp(name, "ivi-launcher") && + strcmp(name, "output") && strcmp(name, "remote-output") && + strcmp(name, "pipewire-output")) { + section = weston_config_get_section(config, name, NULL, NULL); + if (section) + return section; + } + section = zalloc(sizeof *section); if (section == NULL) return NULL; @@ -355,10 +372,33 @@ config_add_section(struct weston_config *config, const char *name) static struct weston_config_entry * section_add_entry(struct weston_config_section *section, - const char *key, const char *value) + const char *key, const char *value, const char *file_name) { struct weston_config_entry *entry; + /* hack for removing entry */ + if (key[0] == '-') { + key ++; + value = NULL; + } + + /* drop old entry */ + entry = config_section_get_entry(section, key); + if (entry) { + if (getenv("WESTON_MAIN_PARSE")) { + printf("%s: \"%s/%s\" from \"%s\" to \"%s\"\n", + file_name ?: "unknown", section->name, + entry->key, entry->value ?: "", value ?: ""); + } + wl_list_remove(&entry->link); + free(entry->key); + free(entry->value); + free(entry); + } + + if (!value || value[0] == '\0') + return NULL; + entry = zalloc(sizeof *entry); if (entry == NULL) return NULL; @@ -382,14 +422,13 @@ section_add_entry(struct weston_config_section *section, } static bool -weston_config_parse_internal(struct weston_config *config, FILE *fp) +weston_config_parse_internal(struct weston_config *config, FILE *fp, + const char *file_name) { struct weston_config_section *section = NULL; char line[512], *p; int i; - wl_list_init(&config->section_list); - while (fgets(line, sizeof line, fp)) { switch (line[0]) { case '#': @@ -422,7 +461,7 @@ weston_config_parse_internal(struct weston_config *config, FILE *fp) p[i - 1] = '\0'; i--; } - section_add_entry(section, line, p); + section_add_entry(section, line, p, file_name); continue; } } @@ -438,7 +477,8 @@ weston_config_parse_fp(FILE *file) if (config == NULL) return NULL; - if (!weston_config_parse_internal(config, file)) { + wl_list_init(&config->section_list); + if (!weston_config_parse_internal(config, file, NULL)) { weston_config_destroy(config); return NULL; } @@ -446,48 +486,108 @@ weston_config_parse_fp(FILE *file) return config; } -WL_EXPORT struct weston_config * -weston_config_parse(const char *name) +static FILE * +weston_open_config_file(struct weston_config *config, const char *name) { FILE *fp; struct stat filestat; - struct weston_config *config; int fd; - bool ret; - - config = zalloc(sizeof *config); - if (config == NULL) - return NULL; fd = open_config_file(config, name); - if (fd == -1) { - free(config); + if (fd == -1) return NULL; - } if (fstat(fd, &filestat) < 0 || !S_ISREG(filestat.st_mode)) { close(fd); - free(config); return NULL; } fp = fdopen(fd, "r"); if (fp == NULL) { close(fd); - free(config); return NULL; } - ret = weston_config_parse_internal(config, fp); + return fp; +} - fclose(fp); +static int +accept_config_file(const struct dirent *entry) +{ + const char *suffix = ".ini"; + char *end = strstr(entry->d_name, suffix); + return end && end[strlen(suffix)] == '\0'; +} - if (!ret) { - weston_config_destroy(config); +WL_EXPORT struct weston_config * +weston_config_parse(const char *name) +{ + FILE *fp; + struct weston_config *config; + struct stat st; + struct dirent **namelist; + char path[sizeof(config->path) + 2]; + bool ret; + int n, i; + + config = zalloc(sizeof *config); + if (config == NULL) return NULL; + + wl_list_init(&config->section_list); + + fp = weston_open_config_file(config, name); + if (fp) { + ret = weston_config_parse_internal(config, fp, name); + + fclose(fp); + + if (!ret) { + fprintf(stderr, "failed to parse %s\n", config->path); + free(config); + return NULL; + } + } + + strcpy(path, config->path); + strcat(path, ".d"); + if (stat(path, &st) < 0 || !S_ISDIR(st.st_mode)) + return config; + + n = scandir(path, &namelist, accept_config_file, alphasort); + if (n < 0) + return config; + + for (i = 0; i < n; i++) { + char *file = namelist[i]->d_name; + char *sep = "/"; + char fpath[strlen(path)+strlen(sep)+strlen(file) + 1]; + strcpy(fpath, path); + strcat(fpath, sep); + strcat(fpath, file); + free(namelist[i]); + + fp = weston_open_config_file(NULL, fpath); + if (!fp) + continue; + + ret = weston_config_parse_internal(config, fp, fpath); + + fclose(fp); + + if (!ret) { + fprintf(stderr, "failed to parse '%s'\n", fpath); + free(config); + config = NULL; + break; + } } + for (; i < n; i++) + free(namelist[i]); + + free(namelist); return config; } -- 2.20.1