Address first round of review for generic blocks

This commit is contained in:
Brian Ashworth 2018-05-30 22:23:11 -04:00
parent 51bb9d8573
commit 8bfa2def88
10 changed files with 34 additions and 32 deletions

View File

@ -1,3 +1,4 @@
#define _POSIX_C_SOURCE 200809L
#include "readline.h" #include "readline.h"
#include "log.h" #include "log.h"
#include <stdlib.h> #include <stdlib.h>
@ -48,15 +49,21 @@ char *read_line(FILE *file) {
return string; return string;
} }
char *peek_line(FILE *file, int offset) { char *peek_line(FILE *file, int offset, long *position) {
int pos = ftell(file); long pos = ftell(file);
char *line = NULL; size_t length = 1;
char *line = calloc(1, length);
for (int i = 0; i <= offset; i++) { for (int i = 0; i <= offset; i++) {
free(line); ssize_t read = getline(&line, &length, file);
line = read_line(file); if (read < 0) {
if (!line) {
break; break;
} }
if (line[read - 1] == '\n') {
line[read - 1] = '\0';
}
}
if (position) {
*position = ftell(file);
} }
fseek(file, pos, SEEK_SET); fseek(file, pos, SEEK_SET);
return line; return line;

View File

@ -4,7 +4,7 @@
#include <stdio.h> #include <stdio.h>
char *read_line(FILE *file); char *read_line(FILE *file);
char *peek_line(FILE *file, int offset); char *peek_line(FILE *file, int offset, long *position);
char *read_line_buffer(FILE *file, char *string, size_t string_len); char *read_line_buffer(FILE *file, char *string, size_t string_len);
#endif #endif

View File

@ -60,8 +60,8 @@ struct cmd_results *config_command(char *command);
/** /**
* Parse and handle a sub command * Parse and handle a sub command
*/ */
struct cmd_results *subcommand(char **argv, int argc, struct cmd_results *config_subcommand(char **argv, int argc,
struct cmd_handler *handlers, int handlers_size); struct cmd_handler *handlers, size_t handlers_size);
/* /*
* Parses a command policy rule. * Parses a command policy rule.
*/ */

View File

@ -376,8 +376,8 @@ cleanup:
return results; return results;
} }
struct cmd_results *subcommand(char **argv, int argc, struct cmd_results *config_subcommand(char **argv, int argc,
struct cmd_handler *handlers, int handlers_size) { struct cmd_handler *handlers, size_t handlers_size) {
char *command = join_args(argv, argc); char *command = join_args(argv, argc);
wlr_log(L_DEBUG, "Subcommand: %s", command); wlr_log(L_DEBUG, "Subcommand: %s", command);
free(command); free(command);

View File

@ -52,7 +52,7 @@ struct cmd_results *cmd_bar(int argc, char **argv) {
return cmd_results_new(CMD_FAILURE, "bar", return cmd_results_new(CMD_FAILURE, "bar",
"Can only be used in config file."); "Can only be used in config file.");
} }
return subcommand(argv, argc, bar_config_handlers, return config_subcommand(argv, argc, bar_config_handlers,
sizeof(bar_config_handlers)); sizeof(bar_config_handlers));
} }
@ -111,5 +111,5 @@ struct cmd_results *cmd_bar(int argc, char **argv) {
wlr_log(L_DEBUG, "Creating bar %s", bar->id); wlr_log(L_DEBUG, "Creating bar %s", bar->id);
} }
return subcommand(argv, argc, bar_handlers, sizeof(bar_handlers)); return config_subcommand(argv, argc, bar_handlers, sizeof(bar_handlers));
} }

View File

@ -52,7 +52,7 @@ static struct cmd_results *parse_three_colors(char ***colors,
} }
struct cmd_results *bar_cmd_colors(int argc, char **argv) { struct cmd_results *bar_cmd_colors(int argc, char **argv) {
return subcommand(argv, argc, bar_colors_handlers, return config_subcommand(argv, argc, bar_colors_handlers,
sizeof(bar_colors_handlers)); sizeof(bar_colors_handlers));
} }

View File

@ -42,8 +42,8 @@ struct cmd_results *cmd_input(int argc, char **argv) {
return cmd_results_new(CMD_FAILURE, NULL, "Couldn't allocate config"); return cmd_results_new(CMD_FAILURE, NULL, "Couldn't allocate config");
} }
struct cmd_results *res = subcommand(argv + 1, argc - 1, input_handlers, struct cmd_results *res = config_subcommand(argv + 1, argc - 1,
sizeof(input_handlers)); input_handlers, sizeof(input_handlers));
free_input_config(config->handler_context.input_config); free_input_config(config->handler_context.input_config);
config->handler_context.input_config = NULL; config->handler_context.input_config = NULL;

View File

@ -65,8 +65,8 @@ struct cmd_results *cmd_mode(int argc, char **argv) {
} }
// Create binding // Create binding
struct cmd_results *result = subcommand(argv + 1, argc - 1, mode_handlers, struct cmd_results *result = config_subcommand(argv + 1, argc - 1,
sizeof(mode_handlers)); mode_handlers, sizeof(mode_handlers));
config->current_mode = config->modes->items[0]; config->current_mode = config->modes->items[0];
return result; return result;

View File

@ -24,8 +24,8 @@ struct cmd_results *cmd_seat(int argc, char **argv) {
"Couldn't allocate config"); "Couldn't allocate config");
} }
struct cmd_results *res = subcommand(argv + 1, argc - 1, seat_handlers, struct cmd_results *res = config_subcommand(argv + 1, argc - 1,
sizeof(seat_handlers)); seat_handlers, sizeof(seat_handlers));
free_seat_config(config->handler_context.seat_config); free_seat_config(config->handler_context.seat_config);
config->handler_context.seat_config = NULL; config->handler_context.seat_config = NULL;

View File

@ -518,21 +518,20 @@ static int detect_brace_on_following_line(FILE *file, char *line,
int lines = 0; int lines = 0;
if (line[strlen(line) - 1] != '{' && line[strlen(line) - 1] != '}') { if (line[strlen(line) - 1] != '{' && line[strlen(line) - 1] != '}') {
char *peeked = NULL; char *peeked = NULL;
long position = 0;
do { do {
wlr_log(L_DEBUG, "Peeking line %d", line_number + lines + 1); wlr_log(L_DEBUG, "Peeking line %d", line_number + lines + 1);
free(peeked); free(peeked);
peeked = peek_line(file, lines); peeked = peek_line(file, lines, &position);
if (peeked) { if (peeked) {
peeked = strip_whitespace(peeked); peeked = strip_whitespace(peeked);
} }
wlr_log(L_DEBUG, "Peeked line: `%s`", peeked);
lines++; lines++;
} while (peeked && strlen(peeked) == 0); } while (peeked && strlen(peeked) == 0);
if (peeked && strlen(peeked) == 1 && peeked[0] == '{') { if (peeked && strlen(peeked) == 1 && peeked[0] == '{') {
for (int i = 0; i < lines; i++) { fseek(file, position, SEEK_SET);
free(peeked);
peeked = read_line(file);
}
} else { } else {
lines = 0; lines = 0;
} }
@ -541,7 +540,7 @@ static int detect_brace_on_following_line(FILE *file, char *line,
return lines; 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) int size = (block ? strlen(block) + 1 : 0) + strlen(line)
+ (add_brace ? 2 : 0) + 1; + (add_brace ? 2 : 0) + 1;
char *expanded = calloc(1, size); 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"); wlr_log(L_ERROR, "Cannot allocate expanded line buffer");
return NULL; return NULL;
} }
strcat(expanded, block ? block : ""); snprintf(expanded, size, "%s%s%s%s", block ? block : "",
strcat(expanded, block ? " " : ""); block ? " " : "", line, add_brace ? " {" : "");
strcat(expanded, line);
strcat(expanded, add_brace ? " {" : "");
return expanded; return expanded;
} }
@ -594,9 +591,7 @@ bool read_config(FILE *file, struct sway_config *config) {
// Special case // Special case
res = config_commands_command(expanded); res = config_commands_command(expanded);
} else { } else {
wlr_log(L_DEBUG, "Entering c_c");
res = config_command(expanded); res = config_command(expanded);
wlr_log(L_DEBUG, "Exiting c_c");
} }
free(expanded); free(expanded);
switch(res->status) { switch(res->status) {