aboutsummaryrefslogtreecommitdiffstats
path: root/sway/config.c
diff options
context:
space:
mode:
authorLibravatar emersion <contact@emersion.fr>2017-12-05 18:47:57 +0100
committerLibravatar emersion <contact@emersion.fr>2017-12-05 18:47:57 +0100
commit9aeda824779d3174db01b6445fa349def031a035 (patch)
treecaecd019325df3cef2aa7f4370adce504cac2388 /sway/config.c
parentMerge pull request #1498 from emersion/config (diff)
downloadsway-9aeda824779d3174db01b6445fa349def031a035.tar.gz
sway-9aeda824779d3174db01b6445fa349def031a035.tar.zst
sway-9aeda824779d3174db01b6445fa349def031a035.zip
Add include command
Diffstat (limited to 'sway/config.c')
-rw-r--r--sway/config.c89
1 files changed, 89 insertions, 0 deletions
diff --git a/sway/config.c b/sway/config.c
index 475e8b04..61131845 100644
--- a/sway/config.c
+++ b/sway/config.c
@@ -311,6 +311,95 @@ bool load_main_config(const char *file, bool is_active) {
311 return success; 311 return success;
312} 312}
313 313
314static bool load_include_config(const char *path, const char *parent_dir, struct sway_config *config) {
315 // save parent config
316 const char *parent_config = config->current_config;
317
318 char *full_path = strdup(path);
319 int len = strlen(path);
320 if (len >= 1 && path[0] != '/') {
321 len = len + strlen(parent_dir) + 2;
322 full_path = malloc(len * sizeof(char));
323 if (!full_path) {
324 sway_log(L_ERROR, "Unable to allocate full path to included config");
325 return false;
326 }
327 snprintf(full_path, len, "%s/%s", parent_dir, path);
328 }
329
330 char *real_path = realpath(full_path, NULL);
331 free(full_path);
332
333 if (real_path == NULL) {
334 sway_log(L_DEBUG, "%s not found.", path);
335 return false;
336 }
337
338 // check if config has already been included
339 int j;
340 for (j = 0; j < config->config_chain->length; ++j) {
341 char *old_path = config->config_chain->items[j];
342 if (strcmp(real_path, old_path) == 0) {
343 sway_log(L_DEBUG, "%s already included once, won't be included again.", real_path);
344 free(real_path);
345 return false;
346 }
347 }
348
349 config->current_config = real_path;
350 list_add(config->config_chain, real_path);
351 int index = config->config_chain->length - 1;
352
353 if (!load_config(real_path, config)) {
354 free(real_path);
355 config->current_config = parent_config;
356 list_del(config->config_chain, index);
357 return false;
358 }
359
360 // restore current_config
361 config->current_config = parent_config;
362 return true;
363}
364
365bool load_include_configs(const char *path, struct sway_config *config) {
366 char *wd = getcwd(NULL, 0);
367 char *parent_path = strdup(config->current_config);
368 const char *parent_dir = dirname(parent_path);
369
370 if (chdir(parent_dir) < 0) {
371 free(parent_path);
372 free(wd);
373 return false;
374 }
375
376 wordexp_t p;
377
378 if (wordexp(path, &p, 0) < 0) {
379 free(parent_path);
380 free(wd);
381 return false;
382 }
383
384 char **w = p.we_wordv;
385 size_t i;
386 for (i = 0; i < p.we_wordc; ++i) {
387 load_include_config(w[i], parent_dir, config);
388 }
389 free(parent_path);
390 wordfree(&p);
391
392 // restore wd
393 if (chdir(wd) < 0) {
394 free(wd);
395 sway_log(L_ERROR, "failed to restore working directory");
396 return false;
397 }
398
399 free(wd);
400 return true;
401}
402
314bool read_config(FILE *file, struct sway_config *config) { 403bool read_config(FILE *file, struct sway_config *config) {
315 bool success = true; 404 bool success = true;
316 enum cmd_status block = CMD_BLOCK_END; 405 enum cmd_status block = CMD_BLOCK_END;