diff options
Diffstat (limited to 'sway/config.c')
-rw-r--r-- | sway/config.c | 98 |
1 files changed, 42 insertions, 56 deletions
diff --git a/sway/config.c b/sway/config.c index c71f315a..4afa09b3 100644 --- a/sway/config.c +++ b/sway/config.c | |||
@@ -1,4 +1,4 @@ | |||
1 | #define _XOPEN_SOURCE 600 // for realpath | 1 | #define _XOPEN_SOURCE 700 // for realpath |
2 | #include <stdio.h> | 2 | #include <stdio.h> |
3 | #include <stdbool.h> | 3 | #include <stdbool.h> |
4 | #include <stdlib.h> | 4 | #include <stdlib.h> |
@@ -30,7 +30,6 @@ | |||
30 | #include "sway/tree/workspace.h" | 30 | #include "sway/tree/workspace.h" |
31 | #include "cairo.h" | 31 | #include "cairo.h" |
32 | #include "pango.h" | 32 | #include "pango.h" |
33 | #include "readline.h" | ||
34 | #include "stringop.h" | 33 | #include "stringop.h" |
35 | #include "list.h" | 34 | #include "list.h" |
36 | #include "log.h" | 35 | #include "log.h" |
@@ -570,28 +569,23 @@ bool load_include_configs(const char *path, struct sway_config *config, | |||
570 | return true; | 569 | return true; |
571 | } | 570 | } |
572 | 571 | ||
573 | static int detect_brace_on_following_line(FILE *file, char *line, | 572 | static int detect_brace(FILE *file) { |
574 | int line_number) { | ||
575 | int lines = 0; | 573 | int lines = 0; |
576 | if (line[strlen(line) - 1] != '{' && line[strlen(line) - 1] != '}') { | 574 | long pos = ftell(file); |
577 | char *peeked = NULL; | 575 | char *line = NULL; |
578 | long position = 0; | 576 | size_t line_size = 0; |
579 | do { | 577 | while ((getline(&line, &line_size, file)) != -1) { |
580 | free(peeked); | 578 | lines++; |
581 | peeked = peek_line(file, lines, &position); | 579 | strip_whitespace(line); |
582 | if (peeked) { | 580 | if (*line) { |
583 | strip_whitespace(peeked); | 581 | if (strcmp(line, "{") != 0) { |
582 | fseek(file, pos, SEEK_SET); | ||
583 | lines = 0; | ||
584 | } | 584 | } |
585 | lines++; | 585 | break; |
586 | } while (peeked && strlen(peeked) == 0); | ||
587 | |||
588 | if (peeked && strlen(peeked) == 1 && peeked[0] == '{') { | ||
589 | fseek(file, position, SEEK_SET); | ||
590 | } else { | ||
591 | lines = 0; | ||
592 | } | 586 | } |
593 | free(peeked); | ||
594 | } | 587 | } |
588 | free(line); | ||
595 | return lines; | 589 | return lines; |
596 | } | 590 | } |
597 | 591 | ||
@@ -634,55 +628,47 @@ bool read_config(FILE *file, struct sway_config *config, | |||
634 | 628 | ||
635 | bool success = true; | 629 | bool success = true; |
636 | int line_number = 0; | 630 | int line_number = 0; |
637 | char *line; | 631 | char *line = NULL; |
632 | size_t line_size = 0; | ||
633 | ssize_t nread; | ||
638 | list_t *stack = create_list(); | 634 | list_t *stack = create_list(); |
639 | size_t read = 0; | 635 | size_t read = 0; |
640 | while (!feof(file)) { | 636 | while ((nread = getline(&line, &line_size, file)) != -1) { |
641 | char *block = stack->length ? stack->items[0] : NULL; | ||
642 | line = read_line(file); | ||
643 | if (!line) { | ||
644 | continue; | ||
645 | } | ||
646 | line_number++; | ||
647 | wlr_log(WLR_DEBUG, "Read line %d: %s", line_number, line); | ||
648 | |||
649 | if (reading_main_config) { | 637 | if (reading_main_config) { |
650 | size_t length = strlen(line); | 638 | if (read + nread > config_size) { |
651 | |||
652 | if (read + length > config_size) { | ||
653 | wlr_log(WLR_ERROR, "Config file changed during reading"); | 639 | wlr_log(WLR_ERROR, "Config file changed during reading"); |
654 | list_free_items_and_destroy(stack); | 640 | success = false; |
655 | free(line); | 641 | break; |
656 | return false; | ||
657 | } | 642 | } |
658 | 643 | ||
659 | strcpy(this_config + read, line); | 644 | strcpy(&this_config[read], line); |
660 | if (line_number != 1) { | 645 | read += nread; |
661 | this_config[read - 1] = '\n'; | ||
662 | } | ||
663 | read += length + 1; | ||
664 | } | 646 | } |
665 | 647 | ||
666 | strip_whitespace(line); | 648 | if (line[nread - 1] == '\n') { |
667 | if (line[0] == '#') { | 649 | line[nread - 1] = '\0'; |
668 | free(line); | ||
669 | continue; | ||
670 | } | 650 | } |
671 | if (strlen(line) == 0) { | 651 | |
672 | free(line); | 652 | line_number++; |
653 | wlr_log(WLR_DEBUG, "Read line %d: %s", line_number, line); | ||
654 | |||
655 | strip_whitespace(line); | ||
656 | if (!*line || line[0] == '#') { | ||
673 | continue; | 657 | continue; |
674 | } | 658 | } |
675 | int brace_detected = detect_brace_on_following_line(file, line, | 659 | int brace_detected = 0; |
676 | line_number); | 660 | if (line[strlen(line) - 1] != '{' && line[strlen(line) - 1] != '}') { |
677 | if (brace_detected > 0) { | 661 | brace_detected = detect_brace(file); |
678 | line_number += brace_detected; | 662 | if (brace_detected > 0) { |
679 | wlr_log(WLR_DEBUG, "Detected open brace on line %d", line_number); | 663 | line_number += brace_detected; |
664 | wlr_log(WLR_DEBUG, "Detected open brace on line %d", line_number); | ||
665 | } | ||
680 | } | 666 | } |
667 | char *block = stack->length ? stack->items[0] : NULL; | ||
681 | char *expanded = expand_line(block, line, brace_detected > 0); | 668 | char *expanded = expand_line(block, line, brace_detected > 0); |
682 | if (!expanded) { | 669 | if (!expanded) { |
683 | list_free_items_and_destroy(stack); | 670 | success = false; |
684 | free(line); | 671 | break; |
685 | return false; | ||
686 | } | 672 | } |
687 | config->current_config_line_number = line_number; | 673 | config->current_config_line_number = line_number; |
688 | config->current_config_line = line; | 674 | config->current_config_line = line; |
@@ -742,9 +728,9 @@ bool read_config(FILE *file, struct sway_config *config, | |||
742 | default:; | 728 | default:; |
743 | } | 729 | } |
744 | free(expanded); | 730 | free(expanded); |
745 | free(line); | ||
746 | free_cmd_results(res); | 731 | free_cmd_results(res); |
747 | } | 732 | } |
733 | free(line); | ||
748 | list_free_items_and_destroy(stack); | 734 | list_free_items_and_destroy(stack); |
749 | config->current_config_line_number = 0; | 735 | config->current_config_line_number = 0; |
750 | config->current_config_line = NULL; | 736 | config->current_config_line = NULL; |