aboutsummaryrefslogtreecommitdiffstats
path: root/sway/config.c
diff options
context:
space:
mode:
authorLibravatar Brian Ashworth <bosrsf04@gmail.com>2018-08-01 23:54:40 -0400
committerLibravatar Brian Ashworth <bosrsf04@gmail.com>2018-08-03 10:37:35 -0400
commitf9a6407111a8730df51258c3b07502814a8ab3e1 (patch)
tree6cf180159caf4a45a8f37b026b2769d24468a07b /sway/config.c
parentMerge pull request #2417 from marienz/swaynag-includes (diff)
downloadsway-f9a6407111a8730df51258c3b07502814a8ab3e1.tar.gz
sway-f9a6407111a8730df51258c3b07502814a8ab3e1.tar.zst
sway-f9a6407111a8730df51258c3b07502814a8ab3e1.zip
Show swaynag on config errors
Diffstat (limited to 'sway/config.c')
-rw-r--r--sway/config.c75
1 files changed, 66 insertions, 9 deletions
diff --git a/sway/config.c b/sway/config.c
index 2afffab1..bd282541 100644
--- a/sway/config.c
+++ b/sway/config.c
@@ -158,6 +158,7 @@ static void set_color(float dest[static 4], uint32_t color) {
158} 158}
159 159
160static void config_defaults(struct sway_config *config) { 160static void config_defaults(struct sway_config *config) {
161 config->swaynag_pid = -1;
161 if (!(config->symbols = create_list())) goto cleanup; 162 if (!(config->symbols = create_list())) goto cleanup;
162 if (!(config->modes = create_list())) goto cleanup; 163 if (!(config->modes = create_list())) goto cleanup;
163 if (!(config->bars = create_list())) goto cleanup; 164 if (!(config->bars = create_list())) goto cleanup;
@@ -319,7 +320,8 @@ static char *get_config_path(void) {
319 return NULL; // Not reached 320 return NULL; // Not reached
320} 321}
321 322
322static bool load_config(const char *path, struct sway_config *config) { 323static bool load_config(const char *path, struct sway_config *config,
324 char **errors) {
323 if (path == NULL) { 325 if (path == NULL) {
324 wlr_log(WLR_ERROR, "Unable to find a config file!"); 326 wlr_log(WLR_ERROR, "Unable to find a config file!");
325 return false; 327 return false;
@@ -338,7 +340,7 @@ static bool load_config(const char *path, struct sway_config *config) {
338 return false; 340 return false;
339 } 341 }
340 342
341 bool config_load_success = read_config(f, config); 343 bool config_load_success = read_config(f, config, errors);
342 fclose(f); 344 fclose(f);
343 345
344 if (!config_load_success) { 346 if (!config_load_success) {
@@ -348,7 +350,7 @@ static bool load_config(const char *path, struct sway_config *config) {
348 return true; 350 return true;
349} 351}
350 352
351bool load_main_config(const char *file, bool is_active) { 353bool load_main_config(const char *file, bool is_active, char **errors) {
352 char *path; 354 char *path;
353 if (file != NULL) { 355 if (file != NULL) {
354 path = strdup(file); 356 path = strdup(file);
@@ -365,6 +367,7 @@ bool load_main_config(const char *file, bool is_active) {
365 config_defaults(config); 367 config_defaults(config);
366 if (is_active) { 368 if (is_active) {
367 wlr_log(WLR_DEBUG, "Performing configuration file reload"); 369 wlr_log(WLR_DEBUG, "Performing configuration file reload");
370 config->swaynag_pid = old_config->swaynag_pid;
368 config->reloading = true; 371 config->reloading = true;
369 config->active = true; 372 config->active = true;
370 create_default_output_configs(); 373 create_default_output_configs();
@@ -423,7 +426,7 @@ bool load_main_config(const char *file, bool is_active) {
423 } 426 }
424 */ 427 */
425 428
426 success = success && load_config(path, config); 429 success = success && load_config(path, config, errors);
427 430
428 if (is_active) { 431 if (is_active) {
429 for (int i = 0; i < config->output_configs->length; i++) { 432 for (int i = 0; i < config->output_configs->length; i++) {
@@ -441,7 +444,7 @@ bool load_main_config(const char *file, bool is_active) {
441} 444}
442 445
443static bool load_include_config(const char *path, const char *parent_dir, 446static bool load_include_config(const char *path, const char *parent_dir,
444 struct sway_config *config) { 447 struct sway_config *config, char **errors) {
445 // save parent config 448 // save parent config
446 const char *parent_config = config->current_config_path; 449 const char *parent_config = config->current_config_path;
447 450
@@ -485,7 +488,7 @@ static bool load_include_config(const char *path, const char *parent_dir,
485 list_add(config->config_chain, real_path); 488 list_add(config->config_chain, real_path);
486 int index = config->config_chain->length - 1; 489 int index = config->config_chain->length - 1;
487 490
488 if (!load_config(real_path, config)) { 491 if (!load_config(real_path, config, errors)) {
489 free(real_path); 492 free(real_path);
490 config->current_config_path = parent_config; 493 config->current_config_path = parent_config;
491 list_del(config->config_chain, index); 494 list_del(config->config_chain, index);
@@ -497,7 +500,8 @@ static bool load_include_config(const char *path, const char *parent_dir,
497 return true; 500 return true;
498} 501}
499 502
500bool load_include_configs(const char *path, struct sway_config *config) { 503bool load_include_configs(const char *path, struct sway_config *config,
504 char **errors) {
501 char *wd = getcwd(NULL, 0); 505 char *wd = getcwd(NULL, 0);
502 char *parent_path = strdup(config->current_config_path); 506 char *parent_path = strdup(config->current_config_path);
503 const char *parent_dir = dirname(parent_path); 507 const char *parent_dir = dirname(parent_path);
@@ -519,7 +523,7 @@ bool load_include_configs(const char *path, struct sway_config *config) {
519 char **w = p.we_wordv; 523 char **w = p.we_wordv;
520 size_t i; 524 size_t i;
521 for (i = 0; i < p.we_wordc; ++i) { 525 for (i = 0; i < p.we_wordc; ++i) {
522 load_include_config(w[i], parent_dir, config); 526 load_include_config(w[i], parent_dir, config, errors);
523 } 527 }
524 free(parent_path); 528 free(parent_path);
525 wordfree(&p); 529 wordfree(&p);
@@ -575,7 +579,26 @@ static char *expand_line(const char *block, const char *line, bool add_brace) {
575 return expanded; 579 return expanded;
576} 580}
577 581
578bool read_config(FILE *file, struct sway_config *config) { 582static void log_error(char **errors, const char *fmt, ...) {
583 va_list args;
584 va_start(args, fmt);
585 size_t length = vsnprintf(NULL, 0, fmt, args) + 1;
586 va_end(args);
587
588 int offset = *errors ? strlen(*errors) : 0;
589 char *temp = realloc(*errors, offset + length + 1);
590 if (!temp) {
591 wlr_log(WLR_ERROR, "Failed to realloc error log");
592 return;
593 }
594 *errors = temp;
595
596 va_start(args, fmt);
597 vsnprintf(*errors + offset, length, fmt, args);
598 va_end(args);
599}
600
601bool read_config(FILE *file, struct sway_config *config, char **errors) {
579 bool reading_main_config = false; 602 bool reading_main_config = false;
580 char *this_config = NULL; 603 char *this_config = NULL;
581 size_t config_size = 0; 604 size_t config_size = 0;
@@ -665,6 +688,8 @@ bool read_config(FILE *file, struct sway_config *config) {
665 case CMD_INVALID: 688 case CMD_INVALID:
666 wlr_log(WLR_ERROR, "Error on line %i '%s': %s (%s)", line_number, 689 wlr_log(WLR_ERROR, "Error on line %i '%s': %s (%s)", line_number,
667 line, res->error, config->current_config_path); 690 line, res->error, config->current_config_path);
691 log_error(errors, "Error on line %i (%s) '%s': %s\n", line_number,
692 config->current_config_path, line, res->error);
668 success = false; 693 success = false;
669 break; 694 break;
670 695
@@ -713,6 +738,38 @@ bool read_config(FILE *file, struct sway_config *config) {
713 return success; 738 return success;
714} 739}
715 740
741void spawn_swaynag_config_errors(struct sway_config *config, char *errors) {
742 char *command = "swaynag "
743 "--type error "
744 "--message 'There are errors in your config file' "
745 "--detailed-message "
746 "--button 'Exit sway' 'swaymsg exit' "
747 "--button 'Reload sway' 'swaymsg reload'";
748
749 int fd[2];
750 if (pipe(fd) != 0) {
751 wlr_log(WLR_ERROR, "Failed to create pipe for swaynag");
752 return;
753 }
754
755 pid_t pid;
756 if ((pid = fork()) == 0) {
757 close(fd[1]);
758 dup2(fd[0], STDIN_FILENO);
759 close(fd[0]);
760 execl("/bin/sh", "/bin/sh", "-c", command, NULL);
761 _exit(0);
762 } else if (pid < 0) {
763 wlr_log(WLR_ERROR, "Failed to create fork for swaynag");
764 }
765
766 close(fd[0]);
767 write(fd[1], errors, strlen(errors));
768 close(fd[1]);
769
770 config->swaynag_pid = pid;
771}
772
716char *do_var_replacement(char *str) { 773char *do_var_replacement(char *str) {
717 int i; 774 int i;
718 char *find = str; 775 char *find = str;