aboutsummaryrefslogtreecommitdiffstats
path: root/sway/config.c
diff options
context:
space:
mode:
authorLibravatar emersion <contact@emersion.fr>2019-01-08 10:05:37 +0100
committerLibravatar GitHub <noreply@github.com>2019-01-08 10:05:37 +0100
commit140bc2dd5b81205df58bf06e695788e689fae397 (patch)
tree6a88913630734736763b12ec0b10da68ef413256 /sway/config.c
parentMerge pull request #3337 from RedSoxFan/fix-seat-cmd-cursor (diff)
parentfixup! stringop.c: rewrite strip_whitespace (diff)
downloadsway-140bc2dd5b81205df58bf06e695788e689fae397.tar.gz
sway-140bc2dd5b81205df58bf06e695788e689fae397.tar.zst
sway-140bc2dd5b81205df58bf06e695788e689fae397.zip
Merge pull request #3275 from ianyfan/remove-readline
Rewrite strip_whitespace and remove readline.c
Diffstat (limited to 'sway/config.c')
-rw-r--r--sway/config.c124
1 files changed, 68 insertions, 56 deletions
diff --git a/sway/config.c b/sway/config.c
index 5d631b7e..8a0b293c 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"
@@ -571,28 +570,49 @@ bool load_include_configs(const char *path, struct sway_config *config,
571 return true; 570 return true;
572} 571}
573 572
574static int detect_brace_on_following_line(FILE *file, char *line, 573// get line, with backslash continuation
575 int line_number) { 574static ssize_t getline_with_cont(char **lineptr, size_t *line_size, FILE *file) {
576 int lines = 0; 575 char *next_line = NULL;
577 if (line[strlen(line) - 1] != '{' && line[strlen(line) - 1] != '}') { 576 size_t next_line_size = 0;
578 char *peeked = NULL; 577 ssize_t nread = getline(lineptr, line_size, file);
579 long position = 0; 578 while (nread >= 2 && strcmp(&(*lineptr)[nread - 2], "\\\n") == 0) {
580 do { 579 ssize_t next_nread = getline(&next_line, &next_line_size, file);
581 free(peeked); 580 if (next_nread == -1) {
582 peeked = peek_line(file, lines, &position); 581 break;
583 if (peeked) { 582 }
584 peeked = strip_whitespace(peeked); 583
584 nread += next_nread - 2;
585 if ((ssize_t) *line_size < nread + 1) {
586 *line_size = nread + 1;
587 *lineptr = realloc(*lineptr, *line_size);
588 if (!*lineptr) {
589 nread = -1;
590 break;
585 } 591 }
586 lines++; 592 }
587 } while (peeked && strlen(peeked) == 0); 593 strcpy(&(*lineptr)[nread - next_nread], next_line);
594 }
595 free(next_line);
596 return nread;
597}
588 598
589 if (peeked && strlen(peeked) == 1 && peeked[0] == '{') { 599static int detect_brace(FILE *file) {
590 fseek(file, position, SEEK_SET); 600 int lines = 0;
591 } else { 601 long pos = ftell(file);
592 lines = 0; 602 char *line = NULL;
603 size_t line_size = 0;
604 while ((getline(&line, &line_size, file)) != -1) {
605 lines++;
606 strip_whitespace(line);
607 if (*line) {
608 if (strcmp(line, "{") != 0) {
609 fseek(file, pos, SEEK_SET);
610 lines = 0;
611 }
612 break;
593 } 613 }
594 free(peeked);
595 } 614 }
615 free(line);
596 return lines; 616 return lines;
597} 617}
598 618
@@ -635,55 +655,47 @@ bool read_config(FILE *file, struct sway_config *config,
635 655
636 bool success = true; 656 bool success = true;
637 int line_number = 0; 657 int line_number = 0;
638 char *line; 658 char *line = NULL;
659 size_t line_size = 0;
660 ssize_t nread;
639 list_t *stack = create_list(); 661 list_t *stack = create_list();
640 size_t read = 0; 662 size_t read = 0;
641 while (!feof(file)) { 663 while ((nread = getline_with_cont(&line, &line_size, file)) != -1) {
642 char *block = stack->length ? stack->items[0] : NULL;
643 line = read_line(file);
644 if (!line) {
645 continue;
646 }
647 line_number++;
648 wlr_log(WLR_DEBUG, "Read line %d: %s", line_number, line);
649
650 if (reading_main_config) { 664 if (reading_main_config) {
651 size_t length = strlen(line); 665 if (read + nread > config_size) {
652
653 if (read + length > config_size) {
654 wlr_log(WLR_ERROR, "Config file changed during reading"); 666 wlr_log(WLR_ERROR, "Config file changed during reading");
655 list_free_items_and_destroy(stack); 667 success = false;
656 free(line); 668 break;
657 return false;
658 } 669 }
659 670
660 strcpy(this_config + read, line); 671 strcpy(&this_config[read], line);
661 if (line_number != 1) { 672 read += nread;
662 this_config[read - 1] = '\n';
663 }
664 read += length + 1;
665 } 673 }
666 674
667 line = strip_whitespace(line); 675 if (line[nread - 1] == '\n') {
668 if (line[0] == '#') { 676 line[nread - 1] = '\0';
669 free(line);
670 continue;
671 } 677 }
672 if (strlen(line) == 0) { 678
673 free(line); 679 line_number++;
680 wlr_log(WLR_DEBUG, "Read line %d: %s", line_number, line);
681
682 strip_whitespace(line);
683 if (!*line || line[0] == '#') {
674 continue; 684 continue;
675 } 685 }
676 int brace_detected = detect_brace_on_following_line(file, line, 686 int brace_detected = 0;
677 line_number); 687 if (line[strlen(line) - 1] != '{' && line[strlen(line) - 1] != '}') {
678 if (brace_detected > 0) { 688 brace_detected = detect_brace(file);
679 line_number += brace_detected; 689 if (brace_detected > 0) {
680 wlr_log(WLR_DEBUG, "Detected open brace on line %d", line_number); 690 line_number += brace_detected;
691 wlr_log(WLR_DEBUG, "Detected open brace on line %d", line_number);
692 }
681 } 693 }
694 char *block = stack->length ? stack->items[0] : NULL;
682 char *expanded = expand_line(block, line, brace_detected > 0); 695 char *expanded = expand_line(block, line, brace_detected > 0);
683 if (!expanded) { 696 if (!expanded) {
684 list_free_items_and_destroy(stack); 697 success = false;
685 free(line); 698 break;
686 return false;
687 } 699 }
688 config->current_config_line_number = line_number; 700 config->current_config_line_number = line_number;
689 config->current_config_line = line; 701 config->current_config_line = line;
@@ -743,9 +755,9 @@ bool read_config(FILE *file, struct sway_config *config,
743 default:; 755 default:;
744 } 756 }
745 free(expanded); 757 free(expanded);
746 free(line);
747 free_cmd_results(res); 758 free_cmd_results(res);
748 } 759 }
760 free(line);
749 list_free_items_and_destroy(stack); 761 list_free_items_and_destroy(stack);
750 config->current_config_line_number = 0; 762 config->current_config_line_number = 0;
751 config->current_config_line = NULL; 763 config->current_config_line = NULL;