config_path cleanup

This commit is contained in:
taiyu 2015-09-06 22:48:43 -07:00
parent 85ec928d58
commit 5408d34c9c
2 changed files with 58 additions and 63 deletions

View file

@ -13,20 +13,23 @@
struct sway_config *config; struct sway_config *config;
static bool exists(const char *path) { static bool file_exists(const char *path) {
return access(path, R_OK) != -1; return access(path, R_OK) != -1;
} }
void config_defaults(struct sway_config *config) { static void config_defaults(struct sway_config *config) {
config->symbols = create_list(); config->symbols = create_list();
config->modes = create_list(); config->modes = create_list();
config->cmd_queue = create_list();
config->workspace_outputs = create_list(); config->workspace_outputs = create_list();
config->output_configs = create_list(); config->output_configs = create_list();
config->cmd_queue = create_list();
config->current_mode = malloc(sizeof(struct sway_mode)); config->current_mode = malloc(sizeof(struct sway_mode));
config->current_mode->name = NULL; config->current_mode->name = NULL;
config->current_mode->bindings = create_list(); config->current_mode->bindings = create_list();
list_add(config->modes, config->current_mode); list_add(config->modes, config->current_mode);
config->floating_mod = 0; config->floating_mod = 0;
config->default_layout = L_NONE; config->default_layout = L_NONE;
config->default_orientation = L_NONE; config->default_orientation = L_NONE;
@ -44,7 +47,14 @@ void config_defaults(struct sway_config *config) {
void free_mode(struct sway_mode *mode) { void free_mode(struct sway_mode *mode) {
free(mode->name); free(mode->name);
free_flat_list(mode->bindings); int i;
for (i = 0; i < mode->bindings->length; ++i) {
struct sway_binding *bind = mode->bindings->items[i];
list_free(bind->keys);
free(bind->command);
free(bind);
}
list_free(mode->bindings);
} }
void free_config(struct sway_config *config) { void free_config(struct sway_config *config) {
@ -69,88 +79,74 @@ void free_config(struct sway_config *config) {
free_flat_list(config->output_configs); free_flat_list(config->output_configs);
} }
static const char *search_paths[] = {
"$home/.sway/config",
"$config/sway/config",
"/etc/sway/config",
"$home/.i3/config",
"$config/.i3/config",
"/etc/i3/config"
};
static char *get_config_path() { static char *get_config_path(void) {
char *home = getenv("HOME"); char *config_path = NULL;
if (home) { char *paths[3] = {getenv("HOME"), getenv("XDG_CONFIG_HOME"), ""};
home = strdup(getenv("HOME")); int pathlen[3] = {0, 0, 0};
} int i;
char *config = getenv("XDG_CONFIG_HOME"); #define home paths[0]
if (config) { #define conf paths[1]
config = strdup(getenv("XDG_CONFIG_HOME")); // Get home and config directories
home = home ? strdup(home) : NULL;
if (conf) {
conf = strdup(conf);
} else if (home) { } else if (home) {
const char *def = "/.config"; const char *def = "/.config";
config = malloc(strlen(home) + strlen(def) + 1); conf = malloc(strlen(home) + strlen(def) + 1);
strcpy(config, home); strcpy(conf, home);
strcat(config, def); strcat(conf, def);
} else { } else {
home = strdup(""); home = strdup("");
config = strdup(""); conf = strdup("");
} }
pathlen[0] = strlen(home);
// Set up a temporary config for holding set variables pathlen[1] = strlen(conf);
struct sway_config *temp_config = malloc(sizeof(struct sway_config)); #undef home
config_defaults(temp_config); #undef conf
const char *set_home = "set $home "; // Search for config file from search paths
char *_home = malloc(strlen(home) + strlen(set_home) + 1); static const char *search_paths[] = {
strcpy(_home, set_home); "/.sway/config", // Prepend with $home
strcat(_home, home); "/sway/config", // Prepend with $config
handle_command(temp_config, _home); "/etc/sway/config",
free(_home); "/.i3/config", // $home
const char *set_config = "set $config "; "/.i3/config", // $config
char *_config = malloc(strlen(config) + strlen(set_config) + 1); "/etc/i3/config"
strcpy(_config, set_config); };
strcat(_config, config);
handle_command(temp_config, _config);
free(_config);
char *test = NULL;
int i;
for (i = 0; i < (int)(sizeof(search_paths) / sizeof(char *)); ++i) { for (i = 0; i < (int)(sizeof(search_paths) / sizeof(char *)); ++i) {
test = strdup(search_paths[i]); char *test = malloc(pathlen[i%3] + strlen(search_paths[i]) + 1);
test = do_var_replacement(temp_config, test); strcpy(test, paths[i%3]);
strcat(test, search_paths[i]);
sway_log(L_DEBUG, "Checking for config at %s", test); sway_log(L_DEBUG, "Checking for config at %s", test);
if (exists(test)) { if (file_exists(test)) {
goto _continue; config_path = test;
goto cleanup;
} }
free(test); free(test);
test = NULL;
} }
sway_log(L_DEBUG, "Trying to find config in XDG_CONFIG_DIRS"); sway_log(L_DEBUG, "Trying to find config in XDG_CONFIG_DIRS");
char *xdg_config_dirs = getenv("XDG_CONFIG_DIRS"); char *xdg_config_dirs = getenv("XDG_CONFIG_DIRS");
if (xdg_config_dirs != NULL) { if (xdg_config_dirs) {
list_t *paths = split_string(xdg_config_dirs, ":"); list_t *paths = split_string(xdg_config_dirs, ":");
char *name = "/sway/config"; const char *name = "/sway/config";
int i;
for (i = 0; i < paths->length; i++ ) { for (i = 0; i < paths->length; i++ ) {
test = malloc(strlen(paths->items[i]) + strlen(name) + 1); char *test = malloc(strlen(paths->items[i]) + strlen(name) + 1);
strcpy(test, paths->items[i]); strcpy(test, paths->items[i]);
strcat(test, name); strcat(test, name);
if (exists(test)) { if (file_exists(test)) {
free_config(temp_config); config_path = test;
free_flat_list(paths); break;
return test;
} }
free(test); free(test);
test = NULL;
} }
free_flat_list(paths); free_flat_list(paths);
} }
_continue: cleanup:
free_config(temp_config); free(paths[0]);
free(home); free(paths[1]);
free(config); return config_path;
return test;
} }
bool load_config(const char *file) { bool load_config(const char *file) {

View file

@ -334,7 +334,6 @@ static bool handle_key(wlc_handle view, uint32_t time, const struct wlc_modifier
release_key(sym, key); release_key(sym, key);
} }
// TODO: reminder to check conflicts with mod+q+a versus mod+q
for (i = 0; i < mode->bindings->length; ++i) { for (i = 0; i < mode->bindings->length; ++i) {
struct sway_binding *binding = mode->bindings->items[i]; struct sway_binding *binding = mode->bindings->items[i];