From 7c810dc344c28d1876c5ee158cb0806289d0f813 Mon Sep 17 00:00:00 2001 From: Brian Ashworth Date: Wed, 30 May 2018 13:20:02 -0400 Subject: Make command block implementation generic --- sway/config.c | 156 ++++++++++++++-------------------------------------------- 1 file changed, 37 insertions(+), 119 deletions(-) (limited to 'sway/config.c') diff --git a/sway/config.c b/sway/config.c index 27308066..26e6f3e3 100644 --- a/sway/config.c +++ b/sway/config.c @@ -59,12 +59,12 @@ static void free_mode(struct sway_mode *mode) { } void free_config(struct sway_config *config) { - config_clear_handler_context(config); - if (!config) { return; } + memset(&config->handler_context, 0, sizeof(config->handler_context)); + // TODO: handle all currently unhandled lists as we add implementations if (config->symbols) { for (int i = 0; i < config->symbols->length; ++i) { @@ -107,7 +107,6 @@ void free_config(struct sway_config *config) { list_free(config->command_policies); list_free(config->feature_policies); list_free(config->ipc_policies); - free(config->current_bar); free(config->floating_scroll_up_cmd); free(config->floating_scroll_down_cmd); free(config->floating_scroll_left_cmd); @@ -514,37 +513,41 @@ bool load_include_configs(const char *path, struct sway_config *config) { return true; } -void config_clear_handler_context(struct sway_config *config) { - free_input_config(config->handler_context.input_config); - free_seat_config(config->handler_context.seat_config); - - memset(&config->handler_context, 0, sizeof(config->handler_context)); -} - bool read_config(FILE *file, struct sway_config *config) { bool success = true; - enum cmd_status block = CMD_BLOCK_END; - int line_number = 0; char *line; + list_t *stack = create_list(); while (!feof(file)) { + char *block = stack->length ? stack->items[0] : NULL; line = read_line(file); if (!line) { continue; } line_number++; + wlr_log(L_DEBUG, "Read line %d: %s", line_number, line); line = strip_whitespace(line); if (line[0] == '#') { free(line); continue; } + if (strlen(line) == 0) { + free(line); + continue; + } + char *full = calloc(strlen(block ? block : "") + strlen(line) + 2, 1); + strcat(full, block ? block : ""); + strcat(full, block ? " " : ""); + strcat(full, line); + wlr_log(L_DEBUG, "Expanded line: %s", full); struct cmd_results *res; - if (block == CMD_BLOCK_COMMANDS) { + if (block && strcmp(block, "") == 0) { // Special case - res = config_commands_command(line); + res = config_commands_command(full); } else { - res = config_command(line, block); + res = config_command(full); } + free(full); switch(res->status) { case CMD_FAILURE: case CMD_INVALID: @@ -558,126 +561,41 @@ bool read_config(FILE *file, struct sway_config *config) { list_add(config->cmd_queue, strdup(line)); break; - case CMD_BLOCK_MODE: - if (block == CMD_BLOCK_END) { - block = CMD_BLOCK_MODE; - } else { - wlr_log(L_ERROR, "Invalid block '%s'", line); - } - break; - - case CMD_BLOCK_INPUT: - if (block == CMD_BLOCK_END) { - block = CMD_BLOCK_INPUT; - } else { - wlr_log(L_ERROR, "Invalid block '%s'", line); - } - break; - - case CMD_BLOCK_SEAT: - if (block == CMD_BLOCK_END) { - block = CMD_BLOCK_SEAT; - } else { - wlr_log(L_ERROR, "Invalid block '%s'", line); - } - break; - - case CMD_BLOCK_BAR: - if (block == CMD_BLOCK_END) { - block = CMD_BLOCK_BAR; - } else { - wlr_log(L_ERROR, "Invalid block '%s'", line); - } - break; - - case CMD_BLOCK_BAR_COLORS: - if (block == CMD_BLOCK_BAR) { - block = CMD_BLOCK_BAR_COLORS; - } else { - wlr_log(L_ERROR, "Invalid block '%s'", line); - } - break; - case CMD_BLOCK_COMMANDS: - if (block == CMD_BLOCK_END) { - block = CMD_BLOCK_COMMANDS; - } else { - wlr_log(L_ERROR, "Invalid block '%s'", line); - } - break; - - case CMD_BLOCK_IPC: - if (block == CMD_BLOCK_END) { - block = CMD_BLOCK_IPC; - } else { - wlr_log(L_ERROR, "Invalid block '%s'", line); - } + wlr_log(L_DEBUG, "Entering commands block"); + list_insert(stack, 0, ""); break; - case CMD_BLOCK_IPC_EVENTS: - if (block == CMD_BLOCK_IPC) { - block = CMD_BLOCK_IPC_EVENTS; - } else { - wlr_log(L_ERROR, "Invalid block '%s'", line); + case CMD_BLOCK: + wlr_log(L_DEBUG, "Entering block '%s'", res->input); + list_insert(stack, 0, strdup(res->input)); + if (strcmp(res->input, "bar") == 0) { + config->current_bar = NULL; } break; case CMD_BLOCK_END: - switch(block) { - case CMD_BLOCK_MODE: - wlr_log(L_DEBUG, "End of mode block"); - config->current_mode = config->modes->items[0]; - block = CMD_BLOCK_END; - break; - - case CMD_BLOCK_INPUT: - wlr_log(L_DEBUG, "End of input block"); - block = CMD_BLOCK_END; - break; - - case CMD_BLOCK_SEAT: - wlr_log(L_DEBUG, "End of seat block"); - block = CMD_BLOCK_END; + if (!block) { + wlr_log(L_DEBUG, "Unmatched '}' on line %i", line_number); + success = false; break; + } + wlr_log(L_DEBUG, "Exiting block '%s'", block); + list_del(stack, 0); + free(block); - case CMD_BLOCK_BAR: - wlr_log(L_DEBUG, "End of bar block"); + if (strcmp(block, "bar") == 0) { config->current_bar = NULL; - block = CMD_BLOCK_END; - break; - - case CMD_BLOCK_BAR_COLORS: - wlr_log(L_DEBUG, "End of bar colors block"); - block = CMD_BLOCK_BAR; - break; - - case CMD_BLOCK_COMMANDS: - wlr_log(L_DEBUG, "End of commands block"); - block = CMD_BLOCK_END; - break; - - case CMD_BLOCK_IPC: - wlr_log(L_DEBUG, "End of IPC block"); - block = CMD_BLOCK_END; - break; - - case CMD_BLOCK_IPC_EVENTS: - wlr_log(L_DEBUG, "End of IPC events block"); - block = CMD_BLOCK_IPC; - break; - - case CMD_BLOCK_END: - wlr_log(L_ERROR, "Unmatched }"); - break; - - default:; } - config_clear_handler_context(config); + memset(&config->handler_context, 0, + sizeof(config->handler_context)); default:; } free(line); free_cmd_results(res); } + list_foreach(stack, free); + list_free(stack); return success; } -- cgit v1.2.3 From 51bb9d8573efce7c46703d070ad963ba7d8dee76 Mon Sep 17 00:00:00 2001 From: Brian Ashworth Date: Wed, 30 May 2018 15:06:25 -0400 Subject: Support braces on next line for config blocks --- sway/config.c | 67 ++++++++++++++++++++++++++++++++++++++++++++++++++++------- 1 file changed, 59 insertions(+), 8 deletions(-) (limited to 'sway/config.c') 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) { return true; } +static int detect_brace_on_following_line(FILE *file, char *line, + int line_number) { + int lines = 0; + if (line[strlen(line) - 1] != '{' && line[strlen(line) - 1] != '}') { + char *peeked = NULL; + do { + wlr_log(L_DEBUG, "Peeking line %d", line_number + lines + 1); + free(peeked); + peeked = peek_line(file, lines); + if (peeked) { + peeked = strip_whitespace(peeked); + } + lines++; + } while (peeked && strlen(peeked) == 0); + + if (peeked && strlen(peeked) == 1 && peeked[0] == '{') { + for (int i = 0; i < lines; i++) { + free(peeked); + peeked = read_line(file); + } + } else { + lines = 0; + } + free(peeked); + } + return lines; +} + +static char *expand_line(char *block, char *line, bool add_brace) { + int size = (block ? strlen(block) + 1 : 0) + strlen(line) + + (add_brace ? 2 : 0) + 1; + char *expanded = calloc(1, size); + if (!expanded) { + wlr_log(L_ERROR, "Cannot allocate expanded line buffer"); + return NULL; + } + strcat(expanded, block ? block : ""); + strcat(expanded, block ? " " : ""); + strcat(expanded, line); + strcat(expanded, add_brace ? " {" : ""); + return expanded; +} + bool read_config(FILE *file, struct sway_config *config) { bool success = true; int line_number = 0; @@ -535,19 +578,27 @@ bool read_config(FILE *file, struct sway_config *config) { free(line); continue; } - char *full = calloc(strlen(block ? block : "") + strlen(line) + 2, 1); - strcat(full, block ? block : ""); - strcat(full, block ? " " : ""); - strcat(full, line); - wlr_log(L_DEBUG, "Expanded line: %s", full); + int brace_detected = detect_brace_on_following_line(file, line, + line_number); + if (brace_detected > 0) { + line_number += brace_detected; + wlr_log(L_DEBUG, "Detected open brace on line %d", line_number); + } + char *expanded = expand_line(block, line, brace_detected > 0); + if (!expanded) { + return false; + } + wlr_log(L_DEBUG, "Expanded line: %s", expanded); struct cmd_results *res; if (block && strcmp(block, "") == 0) { // Special case - res = config_commands_command(full); + res = config_commands_command(expanded); } else { - res = config_command(full); + wlr_log(L_DEBUG, "Entering c_c"); + res = config_command(expanded); + wlr_log(L_DEBUG, "Exiting c_c"); } - free(full); + free(expanded); switch(res->status) { case CMD_FAILURE: case CMD_INVALID: -- cgit v1.2.3 From 8bfa2def8876bb507aa0174e6b159a0a226889b4 Mon Sep 17 00:00:00 2001 From: Brian Ashworth Date: Wed, 30 May 2018 22:23:11 -0400 Subject: Address first round of review for generic blocks --- sway/config.c | 19 +++++++------------ 1 file changed, 7 insertions(+), 12 deletions(-) (limited to 'sway/config.c') 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, int lines = 0; if (line[strlen(line) - 1] != '{' && line[strlen(line) - 1] != '}') { char *peeked = NULL; + long position = 0; do { wlr_log(L_DEBUG, "Peeking line %d", line_number + lines + 1); free(peeked); - peeked = peek_line(file, lines); + peeked = peek_line(file, lines, &position); if (peeked) { peeked = strip_whitespace(peeked); } + wlr_log(L_DEBUG, "Peeked line: `%s`", peeked); lines++; } while (peeked && strlen(peeked) == 0); if (peeked && strlen(peeked) == 1 && peeked[0] == '{') { - for (int i = 0; i < lines; i++) { - free(peeked); - peeked = read_line(file); - } + fseek(file, position, SEEK_SET); } else { lines = 0; } @@ -541,7 +540,7 @@ static int detect_brace_on_following_line(FILE *file, char *line, return lines; } -static char *expand_line(char *block, char *line, bool add_brace) { +static char *expand_line(const char *block, const char *line, bool add_brace) { int size = (block ? strlen(block) + 1 : 0) + strlen(line) + (add_brace ? 2 : 0) + 1; char *expanded = calloc(1, size); @@ -549,10 +548,8 @@ static char *expand_line(char *block, char *line, bool add_brace) { wlr_log(L_ERROR, "Cannot allocate expanded line buffer"); return NULL; } - strcat(expanded, block ? block : ""); - strcat(expanded, block ? " " : ""); - strcat(expanded, line); - strcat(expanded, add_brace ? " {" : ""); + snprintf(expanded, size, "%s%s%s%s", block ? block : "", + block ? " " : "", line, add_brace ? " {" : ""); return expanded; } @@ -594,9 +591,7 @@ bool read_config(FILE *file, struct sway_config *config) { // Special case res = config_commands_command(expanded); } else { - wlr_log(L_DEBUG, "Entering c_c"); res = config_command(expanded); - wlr_log(L_DEBUG, "Exiting c_c"); } free(expanded); switch(res->status) { -- cgit v1.2.3