diff options
-rw-r--r-- | common/readline.c | 14 | ||||
-rw-r--r-- | include/readline.h | 1 | ||||
-rw-r--r-- | sway/config.c | 67 |
3 files changed, 74 insertions, 8 deletions
diff --git a/common/readline.c b/common/readline.c index ed5801de..abe986c4 100644 --- a/common/readline.c +++ b/common/readline.c | |||
@@ -48,6 +48,20 @@ char *read_line(FILE *file) { | |||
48 | return string; | 48 | return string; |
49 | } | 49 | } |
50 | 50 | ||
51 | char *peek_line(FILE *file, int offset) { | ||
52 | int pos = ftell(file); | ||
53 | char *line = NULL; | ||
54 | for (int i = 0; i <= offset; i++) { | ||
55 | free(line); | ||
56 | line = read_line(file); | ||
57 | if (!line) { | ||
58 | break; | ||
59 | } | ||
60 | } | ||
61 | fseek(file, pos, SEEK_SET); | ||
62 | return line; | ||
63 | } | ||
64 | |||
51 | char *read_line_buffer(FILE *file, char *string, size_t string_len) { | 65 | char *read_line_buffer(FILE *file, char *string, size_t string_len) { |
52 | size_t length = 0; | 66 | size_t length = 0; |
53 | if (!string) { | 67 | if (!string) { |
diff --git a/include/readline.h b/include/readline.h index b3e06d4b..3f63e917 100644 --- a/include/readline.h +++ b/include/readline.h | |||
@@ -4,6 +4,7 @@ | |||
4 | #include <stdio.h> | 4 | #include <stdio.h> |
5 | 5 | ||
6 | char *read_line(FILE *file); | 6 | char *read_line(FILE *file); |
7 | char *peek_line(FILE *file, int offset); | ||
7 | char *read_line_buffer(FILE *file, char *string, size_t string_len); | 8 | char *read_line_buffer(FILE *file, char *string, size_t string_len); |
8 | 9 | ||
9 | #endif | 10 | #endif |
diff --git a/sway/config.c b/sway/config.c index 26e6f3e3..edb10bd7 100644 --- a/sway/config.c +++ b/sway/config.c | |||
@@ -513,6 +513,49 @@ bool load_include_configs(const char *path, struct sway_config *config) { | |||
513 | return true; | 513 | return true; |
514 | } | 514 | } |
515 | 515 | ||
516 | static int detect_brace_on_following_line(FILE *file, char *line, | ||
517 | int line_number) { | ||
518 | int lines = 0; | ||
519 | if (line[strlen(line) - 1] != '{' && line[strlen(line) - 1] != '}') { | ||
520 | char *peeked = NULL; | ||
521 | do { | ||
522 | wlr_log(L_DEBUG, "Peeking line %d", line_number + lines + 1); | ||
523 | free(peeked); | ||
524 | peeked = peek_line(file, lines); | ||
525 | if (peeked) { | ||
526 | peeked = strip_whitespace(peeked); | ||
527 | } | ||
528 | lines++; | ||
529 | } while (peeked && strlen(peeked) == 0); | ||
530 | |||
531 | if (peeked && strlen(peeked) == 1 && peeked[0] == '{') { | ||
532 | for (int i = 0; i < lines; i++) { | ||
533 | free(peeked); | ||
534 | peeked = read_line(file); | ||
535 | } | ||
536 | } else { | ||
537 | lines = 0; | ||
538 | } | ||
539 | free(peeked); | ||
540 | } | ||
541 | return lines; | ||
542 | } | ||
543 | |||
544 | static char *expand_line(char *block, char *line, bool add_brace) { | ||
545 | int size = (block ? strlen(block) + 1 : 0) + strlen(line) | ||
546 | + (add_brace ? 2 : 0) + 1; | ||
547 | char *expanded = calloc(1, size); | ||
548 | if (!expanded) { | ||
549 | wlr_log(L_ERROR, "Cannot allocate expanded line buffer"); | ||
550 | return NULL; | ||
551 | } | ||
552 | strcat(expanded, block ? block : ""); | ||
553 | strcat(expanded, block ? " " : ""); | ||
554 | strcat(expanded, line); | ||
555 | strcat(expanded, add_brace ? " {" : ""); | ||
556 | return expanded; | ||
557 | } | ||
558 | |||
516 | bool read_config(FILE *file, struct sway_config *config) { | 559 | bool read_config(FILE *file, struct sway_config *config) { |
517 | bool success = true; | 560 | bool success = true; |
518 | int line_number = 0; | 561 | int line_number = 0; |
@@ -535,19 +578,27 @@ bool read_config(FILE *file, struct sway_config *config) { | |||
535 | free(line); | 578 | free(line); |
536 | continue; | 579 | continue; |
537 | } | 580 | } |
538 | char *full = calloc(strlen(block ? block : "") + strlen(line) + 2, 1); | 581 | int brace_detected = detect_brace_on_following_line(file, line, |
539 | strcat(full, block ? block : ""); | 582 | line_number); |
540 | strcat(full, block ? " " : ""); | 583 | if (brace_detected > 0) { |
541 | strcat(full, line); | 584 | line_number += brace_detected; |
542 | wlr_log(L_DEBUG, "Expanded line: %s", full); | 585 | wlr_log(L_DEBUG, "Detected open brace on line %d", line_number); |
586 | } | ||
587 | char *expanded = expand_line(block, line, brace_detected > 0); | ||
588 | if (!expanded) { | ||
589 | return false; | ||
590 | } | ||
591 | wlr_log(L_DEBUG, "Expanded line: %s", expanded); | ||
543 | struct cmd_results *res; | 592 | struct cmd_results *res; |
544 | if (block && strcmp(block, "<commands>") == 0) { | 593 | if (block && strcmp(block, "<commands>") == 0) { |
545 | // Special case | 594 | // Special case |
546 | res = config_commands_command(full); | 595 | res = config_commands_command(expanded); |
547 | } else { | 596 | } else { |
548 | res = config_command(full); | 597 | wlr_log(L_DEBUG, "Entering c_c"); |
598 | res = config_command(expanded); | ||
599 | wlr_log(L_DEBUG, "Exiting c_c"); | ||
549 | } | 600 | } |
550 | free(full); | 601 | free(expanded); |
551 | switch(res->status) { | 602 | switch(res->status) { |
552 | case CMD_FAILURE: | 603 | case CMD_FAILURE: |
553 | case CMD_INVALID: | 604 | case CMD_INVALID: |