Improve config file loading

This also makes it so that your i3 config is used before
/etc/sway/config.
This commit is contained in:
Drew DeVault 2015-11-28 10:00:53 -05:00
parent 4916e30378
commit 2f192cceca

View File

@ -2,6 +2,7 @@
#include <stdbool.h> #include <stdbool.h>
#include <stdlib.h> #include <stdlib.h>
#include <unistd.h> #include <unistd.h>
#include <wordexp.h>
#include "readline.h" #include "readline.h"
#include "stringop.h" #include "stringop.h"
#include "list.h" #include "list.h"
@ -14,7 +15,6 @@
struct sway_config *config = NULL; struct sway_config *config = NULL;
static void free_variable(struct sway_variable *var) { static void free_variable(struct sway_variable *var) {
free(var->name); free(var->name);
free(var->value); free(var->value);
@ -118,79 +118,41 @@ static void config_defaults(struct sway_config *config) {
} }
static char *get_config_path(void) { static char *get_config_path(void) {
char *config_path = NULL; static const char *config_paths[] = {
char *paths[3] = { getenv("HOME"), getenv("XDG_CONFIG_HOME"), "" }; "$HOME/.sway/config",
int pathlen[3] = { 0, 0, 0 }; "$XDG_CONFIG_HOME/sway/config",
int i; "$HOME/.i3/config",
#define home paths[0] "$XDG_CONFIG_HOME/i3/config",
#define conf paths[1]
// Get home and config directories
conf = conf ? strdup(conf) : NULL;
home = home ? strdup(home) : NULL;
// If config folder is unset, set it to $HOME/.config
if (!conf && home) {
const char *def = "/.config";
conf = malloc(strlen(home) + strlen(def) + 1);
strcpy(conf, home);
strcat(conf, def);
}
// Get path lengths
pathlen[0] = home ? strlen(home) : 0;
pathlen[1] = conf ? strlen(conf) : 0;
#undef home
#undef conf
// Search for config file from search paths
static const char *search_paths[] = {
"/.sway/config", // Prepend with $home
"/sway/config", // Prepend with $config
"/etc/sway/config", "/etc/sway/config",
"/.i3/config", // $home "/etc/i3/config",
"/i3/config", // $config
"/etc/i3/config"
}; };
for (i = 0; i < (int)(sizeof(search_paths) / sizeof(char *)); ++i) {
// Only try path if it is set by enviroment variables if (!getenv("XDG_CONFIG_HOME")) {
if (paths[i%3]) { char *home = getenv("HOME");
char *test = malloc(pathlen[i%3] + strlen(search_paths[i]) + 1); char *config_home = malloc(strlen("home") + strlen("/.config") + 1);
strcpy(test, paths[i%3]); strcpy(config_home, home);
strcpy(test + pathlen[i%3], search_paths[i]); strcat(config_home, "/.config");
sway_log(L_DEBUG, "Checking for config at %s", test); setenv("XDG_CONFIG_HOME", config_home, 1);
if (file_exists(test)) { sway_log(L_DEBUG, "Set XDG_CONFIG_HOME to %s", config_home);
config_path = test; }
goto cleanup;
wordexp_t p;
char *path;
int i;
for (i = 0; i < (int)(sizeof(config_paths) / sizeof(char *)); ++i) {
if (wordexp(config_paths[i], &p, 0) == 0) {
path = p.we_wordv[0];
if (file_exists(path)) {
return path;
} }
free(test);
} }
} }
sway_log(L_DEBUG, "Trying to find config in XDG_CONFIG_DIRS"); return NULL; // Not reached
char *xdg_config_dirs = getenv("XDG_CONFIG_DIRS");
if (xdg_config_dirs) {
list_t *paths = split_string(xdg_config_dirs, ":");
const char *name = "/sway/config";
for (i = 0; i < paths->length; i++ ) {
char *test = malloc(strlen(paths->items[i]) + strlen(name) + 1);
strcpy(test, paths->items[i]);
strcat(test, name);
if (file_exists(test)) {
config_path = test;
break;
}
free(test);
}
free_flat_list(paths);
}
cleanup:
free(paths[0]);
free(paths[1]);
return config_path;
} }
bool load_config(const char *file) { bool load_config(const char *file) {
sway_log(L_INFO, "Loading config");
input_init(); input_init();
char *path; char *path;
@ -200,6 +162,8 @@ bool load_config(const char *file) {
path = get_config_path(); path = get_config_path();
} }
sway_log(L_INFO, "Loading config from %s", path);
if (path == NULL) { if (path == NULL) {
sway_log(L_ERROR, "Unable to find a config file!"); sway_log(L_ERROR, "Unable to find a config file!");
return false; return false;