diff options
author | 2015-08-15 21:11:24 -0400 | |
---|---|---|
committer | 2015-08-15 21:11:24 -0400 | |
commit | 6cc8ea29e51b785a75b2f5e6242e76dc1804002c (patch) | |
tree | 366e434ce4130d67af9a3c0f44c0f187300a6de4 /sway | |
parent | Merge pull request #35 from taiyu-len/master (diff) | |
parent | Added in additional checks for i3 config paths (diff) | |
download | sway-6cc8ea29e51b785a75b2f5e6242e76dc1804002c.tar.gz sway-6cc8ea29e51b785a75b2f5e6242e76dc1804002c.tar.zst sway-6cc8ea29e51b785a75b2f5e6242e76dc1804002c.zip |
Merge pull request #36 from Luminarys/master
Added in config file loading from multiple sources
Diffstat (limited to 'sway')
-rw-r--r-- | sway/config.c | 138 |
1 files changed, 129 insertions, 9 deletions
diff --git a/sway/config.c b/sway/config.c index d96d23fc..4f576bb9 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,140 @@ | |||
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 | // Check home dir | ||
23 | sway_log(L_DEBUG, "Trying to find config in ~/.sway/config"); | ||
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 | //Now fall back to i3 paths and try the same thing | ||
77 | name = "/.i3/config"; | ||
78 | sway_log(L_DEBUG, "Trying to find config in ~/.i3/config"); | ||
79 | char *temp = malloc(strlen(home) + strlen(name) + 1); | ||
80 | strcpy(temp, home); | ||
81 | strcat(temp, name); | ||
82 | if (exists(temp)) { | ||
83 | return temp; | ||
84 | } | ||
85 | |||
86 | sway_log(L_DEBUG, "Trying to find config in XDG_CONFIG_HOME/i3/config"); | ||
87 | char *xdg_config_home = getenv("XDG_CONFIG_HOME"); | ||
88 | if (xdg_config_home == NULL) { | ||
89 | sway_log(L_DEBUG, "Falling back to ~/.config/i3/config"); | ||
90 | name = "/.config/i3/config"; | ||
91 | temp = malloc(strlen(home) + strlen(name) + 1); | ||
92 | strcpy(temp, home); | ||
93 | strcat(temp, name); | ||
94 | } else { | ||
95 | name = "/i3/config"; | ||
96 | temp = malloc(strlen(xdg_config_home) + strlen(name) + 1); | ||
97 | strcpy(temp, home); | ||
98 | strcat(temp, name); | ||
99 | } | ||
100 | if (exists(temp)) { | ||
101 | return temp; | ||
102 | } | ||
103 | |||
104 | sway_log(L_DEBUG, "Trying to find config in /etc/i3/config"); | ||
105 | strcpy(temp, "/etc/i3/config"); | ||
106 | if (exists(temp)) { | ||
107 | return temp; | ||
108 | } | ||
109 | |||
110 | sway_log(L_DEBUG, "Trying to find config in XDG_CONFIG_DIRS"); | ||
111 | char *xdg_config_dirs = getenv("XDG_CONFIG_DIRS"); | ||
112 | if (xdg_config_dirs != NULL) { | ||
113 | list_t *paths = split_string(xdg_config_dirs, ":"); | ||
114 | name = "/i3/config"; | ||
115 | int i; | ||
116 | for (i = 0; i < paths->length; i++ ) { | ||
117 | temp = malloc(strlen(paths->items[i]) + strlen(name) + 1); | ||
118 | strcpy(temp, paths->items[i]); | ||
119 | strcat(temp, name); | ||
120 | if (exists(temp)) { | ||
121 | free_flat_list(paths); | ||
122 | return temp; | ||
123 | } | ||
124 | } | ||
125 | free_flat_list(paths); | ||
126 | } | ||
127 | |||
128 | return NULL; | ||
129 | } | ||
130 | |||
131 | bool load_config() { | ||
132 | sway_log(L_INFO, "Loading config"); | ||
133 | |||
134 | char *path = get_config_path(); | ||
135 | |||
136 | if (path == NULL) { | ||
137 | sway_log(L_ERROR, "Unable to find a config file!"); | ||
138 | return false; | ||
139 | } | ||
140 | |||
141 | FILE *f = fopen(path, "r"); | ||
22 | if (!f) { | 142 | if (!f) { |
23 | fprintf(stderr, "Unable to open %s for reading", temp); | 143 | fprintf(stderr, "Unable to open %s for reading", path); |
24 | free(temp); | 144 | free(path); |
25 | return false; | 145 | return false; |
26 | } | 146 | } |
27 | free(temp); | 147 | free(path); |
28 | 148 | ||
29 | bool config_load_success; | 149 | bool config_load_success; |
30 | if (config) { | 150 | if (config) { |
@@ -95,7 +215,7 @@ bool read_config(FILE *file, bool is_active) { | |||
95 | sway_log(L_DEBUG, "Config load failed for line %s", line); | 215 | sway_log(L_DEBUG, "Config load failed for line %s", line); |
96 | success = false; | 216 | success = false; |
97 | temp_config->failed = true; | 217 | temp_config->failed = true; |
98 | } | 218 | } |
99 | list_free(args); | 219 | list_free(args); |
100 | 220 | ||
101 | _continue: | 221 | _continue: |