~scoopta/wofi

Added support for $XDG_CONFIG_DIRS v1 NEEDS REVISION

betaboon: 1
 Added support for $XDG_CONFIG_DIRS

 4 files changed, 43 insertions(+), 20 deletions(-)
Export patchset (mbox)
How do I use this?

Copy & paste the following snippet into your terminal to import this patchset into git:

curl -s https://lists.sr.ht/~scoopta/wofi/patches/11359/mbox | git am -3
Learn more about email & git
View this thread in the archives

[PATCH] Added support for $XDG_CONFIG_DIRS Export this patch

# HG changeset patch
# User betaboon <betaboon@0x80.ninja>
# Date 1592145803 -7200
#      Sun Jun 14 16:43:23 2020 +0200
# Node ID a10e64aaa877239231a5d57ef76395234d6a3753
# Parent  cc6103077bc4d2111da60ac7ded4f4c6eee05527
Added support for $XDG_CONFIG_DIRS

diff --git a/inc/utils.h b/inc/utils.h
--- a/inc/utils.h
+++ b/inc/utils.h
@@ -36,4 +36,6 @@

void utils_mkdir(char* path, mode_t mode);

char* utils_xdg_config_locate(const char* path);

#endif
diff --git a/src/main.c b/src/main.c
--- a/src/main.c
+++ b/src/main.c
@@ -36,7 +36,6 @@
static const char* nyan_colors[] = {"#FF0000", "#FFA500", "#FFFF00", "#00FF00", "#0000FF", "#FF00FF"};
static size_t nyan_color_l = sizeof(nyan_colors) / sizeof(char*);

static char* CONFIG_LOCATION;
static char* COLORS_LOCATION;
static struct map* config;
static char* config_path;
@@ -564,17 +563,10 @@
		}
	}

	const char* home_dir = getenv("HOME");
	const char* xdg_conf = getenv("XDG_CONFIG_HOME");
	if(xdg_conf == NULL) {
		CONFIG_LOCATION = utils_concat(2, home_dir, "/.config/wofi");
	} else {
		CONFIG_LOCATION = utils_concat(2, xdg_conf, "/wofi");
	}

	const char* xdg_cache = getenv("XDG_CACHE_HOME");
	if(xdg_cache == NULL) {
		COLORS_LOCATION = utils_concat(2, home_dir, "/.cache/wal/colors");
		COLORS_LOCATION = utils_concat(2, getenv("HOME"), "/.cache/wal/colors");
	} else {
		COLORS_LOCATION = utils_concat(2, xdg_cache, "/wal/colors");
	}
@@ -583,8 +575,7 @@

	//Check if --conf was specified
	if(config_str == NULL) {
		const char* config_f = "/config";
		config_path = utils_concat(2, CONFIG_LOCATION, config_f);
		config_path = utils_xdg_config_locate("wofi/config");
	} else {
		config_path = strdup(config_str);
	}
@@ -601,13 +592,13 @@
	if(style_str == NULL) {
		style_str = map_get(config, "stylesheet");
		if(style_str == NULL) {
			const char* style_f = "/style.css";
			stylesheet = utils_concat(2, CONFIG_LOCATION, style_f);
			stylesheet = utils_xdg_config_locate("wofi/style.css");
		} else {
			if(style_str[0] == '/') {
				stylesheet = strdup(style_str);
			} else {
				stylesheet = utils_concat(3, CONFIG_LOCATION, "/", style_str);
				const char* style_f = utils_concat(2, "wofi/", style_str);
				stylesheet = utils_xdg_config_locate(style_f);
			}
		}
	} else {
@@ -627,7 +618,8 @@
			if(color_str[0] == '/') {
				color_path = strdup(color_str);
			} else {
				color_path = utils_concat(3, CONFIG_LOCATION, "/", color_str);
				const char* color_f = utils_concat(2, "wofi/", color_str);
				color_path = utils_xdg_config_locate(color_f);
			}
		}
	} else {
@@ -667,8 +659,6 @@
		exit(1);
	}

	map_put(config, "config_dir", CONFIG_LOCATION);

	if(width != NULL) {
		map_put(config, "width", width);
	}
diff --git a/src/utils.c b/src/utils.c
--- a/src/utils.c
+++ b/src/utils.c
@@ -117,3 +117,36 @@
		free(tmp);
	}
}

char* utils_xdg_config_locate(const char* path) {
	/* priority according to spec: */
	/* - XDG_CONFIG_HOME - defaulting to $HOME/.config*/
	/* - XDG_CONFIG_DIRS - defaulting to /etc/xdg */
	char* xdg_config_home = getenv("XDG_CONFIG_HOME");
	if(xdg_config_home == NULL || strcmp(xdg_config_home, "") == 0) {
		xdg_config_home = utils_concat(2, getenv("HOME"), "/.config");
	}
	char* home_path;
	home_path = utils_concat(3, xdg_config_home, "/", path);
	if(access(home_path, R_OK) == 0) {
		return home_path;
	}

	char* xdg_config_dirs = strdup(getenv("XDG_CONFIG_DIRS"));
	if(xdg_config_dirs == NULL || strcmp(xdg_config_dirs, "") == 0) {
		xdg_config_dirs = "/etc/xdg";
	}

	char* save_ptr;
	char* str = strtok_r(xdg_config_dirs, ":", &save_ptr);
	do {
		char* tmpstr = utils_concat(3, str, "/", path);
		if(access(tmpstr, R_OK) == 0) {
			return tmpstr;
		}
		free(tmpstr);
	} while((str = strtok_r(NULL, ":", &save_ptr)) != NULL);

	return NULL;

}
diff --git a/src/wofi.c b/src/wofi.c
--- a/src/wofi.c
+++ b/src/wofi.c
@@ -71,7 +71,6 @@
static bool allow_images, allow_markup;
static uint64_t image_size;
static char* cache_file = NULL;
static char* config_dir;
static bool mod_shift;
static bool mod_ctrl;
static char* terminal;
@@ -1339,7 +1338,7 @@
		mode_ptr->mode_get_widget = get_plugin_proc(_mode, "_get_widget");
		no_entry = get_plugin_proc(_mode, "_no_entry");
	} else {
		char* plugins_dir = utils_concat(2, config_dir, "/plugins/");
		char* plugins_dir = utils_xdg_config_locate("wofi/plugins");
		char* full_name = utils_concat(2, plugins_dir, _mode);
		mode_ptr->dso = strdup(full_name);
		void* plugin = dlopen(full_name, RTLD_LAZY | RTLD_LOCAL);
@@ -1549,7 +1548,6 @@
	allow_markup = strcmp(config_get(config, "allow_markup", "false"), "true") == 0;
	image_size = strtol(config_get(config, "image_size", "32"), NULL, 10);
	cache_file = map_get(config, "cache_file");
	config_dir = map_get(config, "config_dir");
	terminal = map_get(config, "term");
	char* password_char = map_get(config, "password_char");
	exec_search = strcmp(config_get(config, "exec_search", "false"), "true") == 0;
This segfaults

AddressSanitizer:DEADLYSIGNAL
=================================================================
==17151==ERROR: AddressSanitizer: SEGV on unknown address 0x000000000000
(pc 0x7f8733f21711 bp 0x7ffd1defb060 sp 0x7ffd1defa7e8 T0)
==17151==The signal is caused by a READ memory access.
==17151==Hint: address points to the zero page.
    #0 0x7f8733f21710  (/lib/x86_64-linux-gnu/libc.so.6+0x15e710)
    #1 0x7f8734c9bf1f in strdup
(/lib/x86_64-linux-gnu/libasan.so.5+0x91f1f)
    #2 0x558ecdc4ac4d in utils_xdg_config_locate ../src/utils.c:135
    #3 0x558ecdc485eb in main ../src/main.c:578
    #4 0x7f8733de9e0a in __libc_start_main ../csu/libc-start.c:308
    #5 0x558ecdc45f99 in _start
(/mnt/Storage/Development/workspace/wofi/Debug/wofi+0x13f99)
 
AddressSanitizer can not provide additional info.
SUMMARY: AddressSanitizer: SEGV (/lib/x86_64-linux-gnu/libc.so.6+0x15e710)  
==17151==ABORTING