diff --git a/include/common/dir.h b/include/common/dir.h index ef03b5e7..a9478acc 100644 --- a/include/common/dir.h +++ b/include/common/dir.h @@ -3,6 +3,7 @@ #define LABWC_DIR_H char *config_dir(void); +char *config_dir_n(int n); /** * theme_dir - find theme directory containing theme @theme_name diff --git a/src/common/dir.c b/src/common/dir.c index 85dfea14..41c230b2 100644 --- a/src/common/dir.c +++ b/src/common/dir.c @@ -74,12 +74,12 @@ build_theme_path(struct ctx *ctx, char *prefix, const char *path) } static char * -find_dir(struct ctx *ctx) +find_dir(struct ctx *ctx, int n) { char *debug = getenv("LABWC_DEBUG_DIR_CONFIG_AND_THEME"); for (int i = 0; ctx->dirs[i].path; i++) { - struct dir d = ctx->dirs[i]; + struct dir d = ctx->dirs[n != -1 ? n : i]; if (!d.prefix) { /* handle /etc/xdg... */ ctx->build_path_fn(ctx, NULL, d.path); @@ -109,6 +109,9 @@ find_dir(struct ctx *ctx) } g_strfreev(prefixes); } + if (n != -1) { + break; + } } /* no directory was found */ ctx->buf[0] = '\0'; @@ -128,7 +131,20 @@ config_dir(void) .len = sizeof(buf), .dirs = config_dirs }; - return find_dir(&ctx); + return find_dir(&ctx, -1); +} + +char * +config_dir_n(int n) +{ + char buf[4096] = { 0 }; + struct ctx ctx = { + .build_path_fn = build_config_path, + .buf = buf, + .len = sizeof(buf), + .dirs = config_dirs + }; + return find_dir(&ctx, n); } char * @@ -142,5 +158,5 @@ theme_dir(const char *theme_name) .dirs = theme_dirs, .theme_name = theme_name }; - return find_dir(&ctx); + return find_dir(&ctx, -1); } diff --git a/src/config/rcxml.c b/src/config/rcxml.c index 54aacb9c..2df07212 100644 --- a/src/config/rcxml.c +++ b/src/config/rcxml.c @@ -14,6 +14,7 @@ #include #include #include "action.h" +#include "common/dir.h" #include "common/list.h" #include "common/macros.h" #include "common/mem.h" @@ -1487,34 +1488,37 @@ rcxml_read(const char *filename) * the first time. The specified 'filename' is only respected the first * time. */ - if (rcxml[0] == '\0') { - find_config_file(rcxml, sizeof(rcxml), filename); - } - if (rcxml[0] == '\0') { - wlr_log(WLR_INFO, "cannot find rc.xml config file"); - goto no_config; - } - - /* Reading file into buffer before parsing - better for unit tests */ - stream = fopen(rcxml, "r"); - if (!stream) { - wlr_log(WLR_ERROR, "cannot read (%s)", rcxml); - goto no_config; - } - wlr_log(WLR_INFO, "read config file %s", rcxml); - buf_init(&b); - while (getline(&line, &len, stream) != -1) { - char *p = strrchr(line, '\n'); - if (p) { - *p = '\0'; + for (int i = 3; i >= 0; i--) { + line = NULL; + len = 0; + if (filename) { + sprintf(rcxml, "%s", filename); + i = 0; + } else { + sprintf(rcxml, "%s/rc.xml", config_dir_n(i)); + } + /* Reading file into buffer before parsing - better for unit tests */ + stream = fopen(rcxml, "r"); + if (!stream) { + wlr_log(WLR_ERROR, "cannot read (%s)", rcxml); + } + else + { + wlr_log(WLR_INFO, "read config file %s", rcxml); + buf_init(&b); + while (getline(&line, &len, stream) != -1) { + char *p = strrchr(line, '\n'); + if (p) { + *p = '\0'; + } + buf_add(&b, line); + } + free(line); + fclose(stream); + rcxml_parse_xml(&b); + free(b.buf); } - buf_add(&b, line); } - free(line); - fclose(stream); - rcxml_parse_xml(&b); - free(b.buf); -no_config: post_processing(); validate(); }