summaryrefslogtreecommitdiffstats
path: root/sway
diff options
context:
space:
mode:
authorLibravatar Brian Ashworth <bosrsf04@gmail.com>2018-05-30 15:06:25 -0400
committerLibravatar Brian Ashworth <bosrsf04@gmail.com>2018-06-02 08:07:44 -0400
commit51bb9d8573efce7c46703d070ad963ba7d8dee76 (patch)
tree09b1eb80df4d2002a5e1406a32f8ae545942e1e7 /sway
parentMake command block implementation generic (diff)
downloadsway-51bb9d8573efce7c46703d070ad963ba7d8dee76.tar.gz
sway-51bb9d8573efce7c46703d070ad963ba7d8dee76.tar.zst
sway-51bb9d8573efce7c46703d070ad963ba7d8dee76.zip
Support braces on next line for config blocks
Diffstat (limited to 'sway')
-rw-r--r--sway/config.c67
1 files changed, 59 insertions, 8 deletions
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
516static 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
544static 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
516bool read_config(FILE *file, struct sway_config *config) { 559bool 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: