diff options
author | Luminarys <kizunanohikari@gmail.com> | 2015-08-15 19:51:23 -0500 |
---|---|---|
committer | Luminarys <kizunanohikari@gmail.com> | 2015-08-15 19:51:23 -0500 |
commit | 614393c09c31c980cd6df98017d6cd31523c8479 (patch) | |
tree | 13ad892eff211fb3a1730687a0a5a59df1c6a918 | |
parent | Merge pull request #34 from taiyu-len/master (diff) | |
download | sway-614393c09c31c980cd6df98017d6cd31523c8479.tar.gz sway-614393c09c31c980cd6df98017d6cd31523c8479.tar.zst sway-614393c09c31c980cd6df98017d6cd31523c8479.zip |
Added in config file loading from multiple sources
-rw-r--r-- | sway/config.c | 86 |
1 files changed, 77 insertions, 9 deletions
diff --git a/sway/config.c b/sway/config.c index d96d23fc..eb08641a 100644 --- a/sway/config.c +++ b/sway/config.c | |||
@@ -1,6 +1,7 @@ | |||
1 | #include <stdio.h> | 1 | #include <stdio.h> |
2 | #include <stdbool.h> | 2 | #include <stdbool.h> |
3 | #include <stdlib.h> | 3 | #include <stdlib.h> |
4 | #include <unistd.h> | ||
4 | #include "readline.h" | 5 | #include "readline.h" |
5 | #include "stringop.h" | 6 | #include "stringop.h" |
6 | #include "list.h" | 7 | #include "list.h" |
@@ -10,21 +11,88 @@ | |||
10 | 11 | ||
11 | struct sway_config *config; | 12 | struct sway_config *config; |
12 | 13 | ||
13 | bool load_config() { | 14 | static bool exists(const char *path) { |
14 | sway_log(L_INFO, "Loading config"); | 15 | return access(path, R_OK) != -1; |
15 | // TODO: Allow use of more config file locations | 16 | } |
16 | const char *name = "/.sway/config"; | 17 | |
18 | static char* get_config_path() { | ||
19 | char *name = "/.sway/config"; | ||
17 | const char *home = getenv("HOME"); | 20 | const char *home = getenv("HOME"); |
21 | |||
22 | sway_log(L_DEBUG, "Trying to find config in ~/.sway/config"); | ||
23 | // Check home dir | ||
18 | char *temp = malloc(strlen(home) + strlen(name) + 1); | 24 | char *temp = malloc(strlen(home) + strlen(name) + 1); |
19 | strcpy(temp, home); | 25 | strcpy(temp, home); |
20 | strcat(temp, name); | 26 | strcat(temp, name); |
21 | FILE *f = fopen(temp, "r"); | 27 | if (exists(temp)) { |
28 | return temp; | ||
29 | } | ||
30 | |||
31 | // Check XDG_CONFIG_HOME with fallback to ~/.config/ | ||
32 | sway_log(L_DEBUG, "Trying to find config in XDG_CONFIG_HOME/sway/config"); | ||
33 | char *xdg_config_home = getenv("XDG_CONFIG_HOME"); | ||
34 | if (xdg_config_home == NULL) { | ||
35 | sway_log(L_DEBUG, "Falling back to ~/.config/sway/config"); | ||
36 | name = "/.config/sway/config"; | ||
37 | temp = malloc(strlen(home) + strlen(name) + 1); | ||
38 | strcpy(temp, home); | ||
39 | strcat(temp, name); | ||
40 | } else { | ||
41 | name = "/sway/config"; | ||
42 | temp = malloc(strlen(xdg_config_home) + strlen(name) + 1); | ||
43 | strcpy(temp, home); | ||
44 | strcat(temp, name); | ||
45 | } | ||
46 | if (exists(temp)) { | ||
47 | return temp; | ||
48 | } | ||
49 | |||
50 | // Check /etc/ | ||
51 | sway_log(L_DEBUG, "Trying to find config in /etc/sway/config"); | ||
52 | strcpy(temp, "/etc/sway/config"); | ||
53 | if (exists(temp)) { | ||
54 | return temp; | ||
55 | } | ||
56 | |||
57 | // Check XDG_CONFIG_DIRS | ||
58 | sway_log(L_DEBUG, "Trying to find config in XDG_CONFIG_DIRS"); | ||
59 | char *xdg_config_dirs = getenv("XDG_CONFIG_DIRS"); | ||
60 | if (xdg_config_dirs != NULL) { | ||
61 | list_t *paths = split_string(xdg_config_dirs, ":"); | ||
62 | name = "/sway/config"; | ||
63 | int i; | ||
64 | for (i = 0; i < paths->length; i++ ) { | ||
65 | temp = malloc(strlen(paths->items[i]) + strlen(name) + 1); | ||
66 | strcpy(temp, paths->items[i]); | ||
67 | strcat(temp, name); | ||
68 | if (exists(temp)) { | ||
69 | free_flat_list(paths); | ||
70 | return temp; | ||
71 | } | ||
72 | } | ||
73 | free_flat_list(paths); | ||
74 | } | ||
75 | |||
76 | return NULL; | ||
77 | } | ||
78 | |||
79 | bool load_config() { | ||
80 | sway_log(L_INFO, "Loading config"); | ||
81 | |||
82 | char *path = get_config_path(); | ||
83 | |||
84 | if (path == NULL) { | ||
85 | sway_log(L_ERROR, "Unable to find a config file!"); | ||
86 | return false; | ||
87 | } | ||
88 | |||
89 | FILE *f = fopen(path, "r"); | ||
22 | if (!f) { | 90 | if (!f) { |
23 | fprintf(stderr, "Unable to open %s for reading", temp); | 91 | fprintf(stderr, "Unable to open %s for reading", path); |
24 | free(temp); | 92 | free(path); |
25 | return false; | 93 | return false; |
26 | } | 94 | } |
27 | free(temp); | 95 | free(path); |
28 | 96 | ||
29 | bool config_load_success; | 97 | bool config_load_success; |
30 | if (config) { | 98 | if (config) { |
@@ -95,7 +163,7 @@ bool read_config(FILE *file, bool is_active) { | |||
95 | sway_log(L_DEBUG, "Config load failed for line %s", line); | 163 | sway_log(L_DEBUG, "Config load failed for line %s", line); |
96 | success = false; | 164 | success = false; |
97 | temp_config->failed = true; | 165 | temp_config->failed = true; |
98 | } | 166 | } |
99 | list_free(args); | 167 | list_free(args); |
100 | 168 | ||
101 | _continue: | 169 | _continue: |