diff options
author | Brian Ashworth <bosrsf04@gmail.com> | 2018-05-30 22:23:11 -0400 |
---|---|---|
committer | Brian Ashworth <bosrsf04@gmail.com> | 2018-06-02 08:07:44 -0400 |
commit | 8bfa2def8876bb507aa0174e6b159a0a226889b4 (patch) | |
tree | cb913921a89cf5436a6f0912fcdf680dde973216 | |
parent | Support braces on next line for config blocks (diff) | |
download | sway-8bfa2def8876bb507aa0174e6b159a0a226889b4.tar.gz sway-8bfa2def8876bb507aa0174e6b159a0a226889b4.tar.zst sway-8bfa2def8876bb507aa0174e6b159a0a226889b4.zip |
Address first round of review for generic blocks
-rw-r--r-- | common/readline.c | 19 | ||||
-rw-r--r-- | include/readline.h | 2 | ||||
-rw-r--r-- | include/sway/commands.h | 4 | ||||
-rw-r--r-- | sway/commands.c | 4 | ||||
-rw-r--r-- | sway/commands/bar.c | 4 | ||||
-rw-r--r-- | sway/commands/bar/colors.c | 2 | ||||
-rw-r--r-- | sway/commands/input.c | 4 | ||||
-rw-r--r-- | sway/commands/mode.c | 4 | ||||
-rw-r--r-- | sway/commands/seat.c | 4 | ||||
-rw-r--r-- | sway/config.c | 19 |
10 files changed, 34 insertions, 32 deletions
diff --git a/common/readline.c b/common/readline.c index abe986c4..f637b64d 100644 --- a/common/readline.c +++ b/common/readline.c | |||
@@ -1,3 +1,4 @@ | |||
1 | #define _POSIX_C_SOURCE 200809L | ||
1 | #include "readline.h" | 2 | #include "readline.h" |
2 | #include "log.h" | 3 | #include "log.h" |
3 | #include <stdlib.h> | 4 | #include <stdlib.h> |
@@ -48,15 +49,21 @@ char *read_line(FILE *file) { | |||
48 | return string; | 49 | return string; |
49 | } | 50 | } |
50 | 51 | ||
51 | char *peek_line(FILE *file, int offset) { | 52 | char *peek_line(FILE *file, int offset, long *position) { |
52 | int pos = ftell(file); | 53 | long pos = ftell(file); |
53 | char *line = NULL; | 54 | size_t length = 1; |
55 | char *line = calloc(1, length); | ||
54 | for (int i = 0; i <= offset; i++) { | 56 | for (int i = 0; i <= offset; i++) { |
55 | free(line); | 57 | ssize_t read = getline(&line, &length, file); |
56 | line = read_line(file); | 58 | if (read < 0) { |
57 | if (!line) { | ||
58 | break; | 59 | break; |
59 | } | 60 | } |
61 | if (line[read - 1] == '\n') { | ||
62 | line[read - 1] = '\0'; | ||
63 | } | ||
64 | } | ||
65 | if (position) { | ||
66 | *position = ftell(file); | ||
60 | } | 67 | } |
61 | fseek(file, pos, SEEK_SET); | 68 | fseek(file, pos, SEEK_SET); |
62 | return line; | 69 | return line; |
diff --git a/include/readline.h b/include/readline.h index 3f63e917..e1ae2302 100644 --- a/include/readline.h +++ b/include/readline.h | |||
@@ -4,7 +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 *peek_line(FILE *file, int offset, long *position); |
8 | 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); |
9 | 9 | ||
10 | #endif | 10 | #endif |
diff --git a/include/sway/commands.h b/include/sway/commands.h index 6e2395bd..593ae0f1 100644 --- a/include/sway/commands.h +++ b/include/sway/commands.h | |||
@@ -60,8 +60,8 @@ struct cmd_results *config_command(char *command); | |||
60 | /** | 60 | /** |
61 | * Parse and handle a sub command | 61 | * Parse and handle a sub command |
62 | */ | 62 | */ |
63 | struct cmd_results *subcommand(char **argv, int argc, | 63 | struct cmd_results *config_subcommand(char **argv, int argc, |
64 | struct cmd_handler *handlers, int handlers_size); | 64 | struct cmd_handler *handlers, size_t handlers_size); |
65 | /* | 65 | /* |
66 | * Parses a command policy rule. | 66 | * Parses a command policy rule. |
67 | */ | 67 | */ |
diff --git a/sway/commands.c b/sway/commands.c index 825fda8f..3a86ae53 100644 --- a/sway/commands.c +++ b/sway/commands.c | |||
@@ -376,8 +376,8 @@ cleanup: | |||
376 | return results; | 376 | return results; |
377 | } | 377 | } |
378 | 378 | ||
379 | struct cmd_results *subcommand(char **argv, int argc, | 379 | struct cmd_results *config_subcommand(char **argv, int argc, |
380 | struct cmd_handler *handlers, int handlers_size) { | 380 | struct cmd_handler *handlers, size_t handlers_size) { |
381 | char *command = join_args(argv, argc); | 381 | char *command = join_args(argv, argc); |
382 | wlr_log(L_DEBUG, "Subcommand: %s", command); | 382 | wlr_log(L_DEBUG, "Subcommand: %s", command); |
383 | free(command); | 383 | free(command); |
diff --git a/sway/commands/bar.c b/sway/commands/bar.c index 358b3893..d84ce808 100644 --- a/sway/commands/bar.c +++ b/sway/commands/bar.c | |||
@@ -52,7 +52,7 @@ struct cmd_results *cmd_bar(int argc, char **argv) { | |||
52 | return cmd_results_new(CMD_FAILURE, "bar", | 52 | return cmd_results_new(CMD_FAILURE, "bar", |
53 | "Can only be used in config file."); | 53 | "Can only be used in config file."); |
54 | } | 54 | } |
55 | return subcommand(argv, argc, bar_config_handlers, | 55 | return config_subcommand(argv, argc, bar_config_handlers, |
56 | sizeof(bar_config_handlers)); | 56 | sizeof(bar_config_handlers)); |
57 | } | 57 | } |
58 | 58 | ||
@@ -111,5 +111,5 @@ struct cmd_results *cmd_bar(int argc, char **argv) { | |||
111 | wlr_log(L_DEBUG, "Creating bar %s", bar->id); | 111 | wlr_log(L_DEBUG, "Creating bar %s", bar->id); |
112 | } | 112 | } |
113 | 113 | ||
114 | return subcommand(argv, argc, bar_handlers, sizeof(bar_handlers)); | 114 | return config_subcommand(argv, argc, bar_handlers, sizeof(bar_handlers)); |
115 | } | 115 | } |
diff --git a/sway/commands/bar/colors.c b/sway/commands/bar/colors.c index 6d3c09b8..8c862ca9 100644 --- a/sway/commands/bar/colors.c +++ b/sway/commands/bar/colors.c | |||
@@ -52,7 +52,7 @@ static struct cmd_results *parse_three_colors(char ***colors, | |||
52 | } | 52 | } |
53 | 53 | ||
54 | struct cmd_results *bar_cmd_colors(int argc, char **argv) { | 54 | struct cmd_results *bar_cmd_colors(int argc, char **argv) { |
55 | return subcommand(argv, argc, bar_colors_handlers, | 55 | return config_subcommand(argv, argc, bar_colors_handlers, |
56 | sizeof(bar_colors_handlers)); | 56 | sizeof(bar_colors_handlers)); |
57 | } | 57 | } |
58 | 58 | ||
diff --git a/sway/commands/input.c b/sway/commands/input.c index 972160e2..22a0bb7c 100644 --- a/sway/commands/input.c +++ b/sway/commands/input.c | |||
@@ -42,8 +42,8 @@ struct cmd_results *cmd_input(int argc, char **argv) { | |||
42 | return cmd_results_new(CMD_FAILURE, NULL, "Couldn't allocate config"); | 42 | return cmd_results_new(CMD_FAILURE, NULL, "Couldn't allocate config"); |
43 | } | 43 | } |
44 | 44 | ||
45 | struct cmd_results *res = subcommand(argv + 1, argc - 1, input_handlers, | 45 | struct cmd_results *res = config_subcommand(argv + 1, argc - 1, |
46 | sizeof(input_handlers)); | 46 | input_handlers, sizeof(input_handlers)); |
47 | 47 | ||
48 | free_input_config(config->handler_context.input_config); | 48 | free_input_config(config->handler_context.input_config); |
49 | config->handler_context.input_config = NULL; | 49 | config->handler_context.input_config = NULL; |
diff --git a/sway/commands/mode.c b/sway/commands/mode.c index 31d0f251..00331ccc 100644 --- a/sway/commands/mode.c +++ b/sway/commands/mode.c | |||
@@ -65,8 +65,8 @@ struct cmd_results *cmd_mode(int argc, char **argv) { | |||
65 | } | 65 | } |
66 | 66 | ||
67 | // Create binding | 67 | // Create binding |
68 | struct cmd_results *result = subcommand(argv + 1, argc - 1, mode_handlers, | 68 | struct cmd_results *result = config_subcommand(argv + 1, argc - 1, |
69 | sizeof(mode_handlers)); | 69 | mode_handlers, sizeof(mode_handlers)); |
70 | config->current_mode = config->modes->items[0]; | 70 | config->current_mode = config->modes->items[0]; |
71 | 71 | ||
72 | return result; | 72 | return result; |
diff --git a/sway/commands/seat.c b/sway/commands/seat.c index 6080bf64..5e3c0bd0 100644 --- a/sway/commands/seat.c +++ b/sway/commands/seat.c | |||
@@ -24,8 +24,8 @@ struct cmd_results *cmd_seat(int argc, char **argv) { | |||
24 | "Couldn't allocate config"); | 24 | "Couldn't allocate config"); |
25 | } | 25 | } |
26 | 26 | ||
27 | struct cmd_results *res = subcommand(argv + 1, argc - 1, seat_handlers, | 27 | struct cmd_results *res = config_subcommand(argv + 1, argc - 1, |
28 | sizeof(seat_handlers)); | 28 | seat_handlers, sizeof(seat_handlers)); |
29 | 29 | ||
30 | free_seat_config(config->handler_context.seat_config); | 30 | free_seat_config(config->handler_context.seat_config); |
31 | config->handler_context.seat_config = NULL; | 31 | config->handler_context.seat_config = NULL; |
diff --git a/sway/config.c b/sway/config.c index edb10bd7..88cc6843 100644 --- a/sway/config.c +++ b/sway/config.c | |||
@@ -518,21 +518,20 @@ static int detect_brace_on_following_line(FILE *file, char *line, | |||
518 | int lines = 0; | 518 | int lines = 0; |
519 | if (line[strlen(line) - 1] != '{' && line[strlen(line) - 1] != '}') { | 519 | if (line[strlen(line) - 1] != '{' && line[strlen(line) - 1] != '}') { |
520 | char *peeked = NULL; | 520 | char *peeked = NULL; |
521 | long position = 0; | ||
521 | do { | 522 | do { |
522 | wlr_log(L_DEBUG, "Peeking line %d", line_number + lines + 1); | 523 | wlr_log(L_DEBUG, "Peeking line %d", line_number + lines + 1); |
523 | free(peeked); | 524 | free(peeked); |
524 | peeked = peek_line(file, lines); | 525 | peeked = peek_line(file, lines, &position); |
525 | if (peeked) { | 526 | if (peeked) { |
526 | peeked = strip_whitespace(peeked); | 527 | peeked = strip_whitespace(peeked); |
527 | } | 528 | } |
529 | wlr_log(L_DEBUG, "Peeked line: `%s`", peeked); | ||
528 | lines++; | 530 | lines++; |
529 | } while (peeked && strlen(peeked) == 0); | 531 | } while (peeked && strlen(peeked) == 0); |
530 | 532 | ||
531 | if (peeked && strlen(peeked) == 1 && peeked[0] == '{') { | 533 | if (peeked && strlen(peeked) == 1 && peeked[0] == '{') { |
532 | for (int i = 0; i < lines; i++) { | 534 | fseek(file, position, SEEK_SET); |
533 | free(peeked); | ||
534 | peeked = read_line(file); | ||
535 | } | ||
536 | } else { | 535 | } else { |
537 | lines = 0; | 536 | lines = 0; |
538 | } | 537 | } |
@@ -541,7 +540,7 @@ static int detect_brace_on_following_line(FILE *file, char *line, | |||
541 | return lines; | 540 | return lines; |
542 | } | 541 | } |
543 | 542 | ||
544 | static char *expand_line(char *block, char *line, bool add_brace) { | 543 | static char *expand_line(const char *block, const char *line, bool add_brace) { |
545 | int size = (block ? strlen(block) + 1 : 0) + strlen(line) | 544 | int size = (block ? strlen(block) + 1 : 0) + strlen(line) |
546 | + (add_brace ? 2 : 0) + 1; | 545 | + (add_brace ? 2 : 0) + 1; |
547 | char *expanded = calloc(1, size); | 546 | char *expanded = calloc(1, size); |
@@ -549,10 +548,8 @@ static char *expand_line(char *block, char *line, bool add_brace) { | |||
549 | wlr_log(L_ERROR, "Cannot allocate expanded line buffer"); | 548 | wlr_log(L_ERROR, "Cannot allocate expanded line buffer"); |
550 | return NULL; | 549 | return NULL; |
551 | } | 550 | } |
552 | strcat(expanded, block ? block : ""); | 551 | snprintf(expanded, size, "%s%s%s%s", block ? block : "", |
553 | strcat(expanded, block ? " " : ""); | 552 | block ? " " : "", line, add_brace ? " {" : ""); |
554 | strcat(expanded, line); | ||
555 | strcat(expanded, add_brace ? " {" : ""); | ||
556 | return expanded; | 553 | return expanded; |
557 | } | 554 | } |
558 | 555 | ||
@@ -594,9 +591,7 @@ bool read_config(FILE *file, struct sway_config *config) { | |||
594 | // Special case | 591 | // Special case |
595 | res = config_commands_command(expanded); | 592 | res = config_commands_command(expanded); |
596 | } else { | 593 | } else { |
597 | wlr_log(L_DEBUG, "Entering c_c"); | ||
598 | res = config_command(expanded); | 594 | res = config_command(expanded); |
599 | wlr_log(L_DEBUG, "Exiting c_c"); | ||
600 | } | 595 | } |
601 | free(expanded); | 596 | free(expanded); |
602 | switch(res->status) { | 597 | switch(res->status) { |