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 --- common/readline.c | 14 ++++++++++++++ 1 file changed, 14 insertions(+) (limited to 'common') 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) { return string; } +char *peek_line(FILE *file, int offset) { + int pos = ftell(file); + char *line = NULL; + for (int i = 0; i <= offset; i++) { + free(line); + line = read_line(file); + if (!line) { + break; + } + } + fseek(file, pos, SEEK_SET); + return line; +} + char *read_line_buffer(FILE *file, char *string, size_t string_len) { size_t length = 0; if (!string) { -- 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 --- common/readline.c | 19 +++++++++++++------ 1 file changed, 13 insertions(+), 6 deletions(-) (limited to 'common') 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 @@ +#define _POSIX_C_SOURCE 200809L #include "readline.h" #include "log.h" #include @@ -48,15 +49,21 @@ char *read_line(FILE *file) { return string; } -char *peek_line(FILE *file, int offset) { - int pos = ftell(file); - char *line = NULL; +char *peek_line(FILE *file, int offset, long *position) { + long pos = ftell(file); + size_t length = 1; + char *line = calloc(1, length); for (int i = 0; i <= offset; i++) { - free(line); - line = read_line(file); - if (!line) { + ssize_t read = getline(&line, &length, file); + if (read < 0) { break; } + if (line[read - 1] == '\n') { + line[read - 1] = '\0'; + } + } + if (position) { + *position = ftell(file); } fseek(file, pos, SEEK_SET); return line; -- cgit v1.2.3 From fbca3bbacbe6e223a5e15d8265301b7d2c36af4e Mon Sep 17 00:00:00 2001 From: Brian Ashworth Date: Thu, 31 May 2018 09:07:35 -0400 Subject: Fix condition in peek_line --- common/readline.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'common') diff --git a/common/readline.c b/common/readline.c index f637b64d..4f55e99c 100644 --- a/common/readline.c +++ b/common/readline.c @@ -58,7 +58,7 @@ char *peek_line(FILE *file, int offset, long *position) { if (read < 0) { break; } - if (line[read - 1] == '\n') { + if (read > 0 && line[read - 1] == '\n') { line[read - 1] = '\0'; } } -- cgit v1.2.3 From af87c7a1af8d790b819afe782fcff2830e959d58 Mon Sep 17 00:00:00 2001 From: Brian Ashworth Date: Fri, 1 Jun 2018 18:35:16 -0400 Subject: Address emersion's feedback on peek_line --- common/readline.c | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) (limited to 'common') diff --git a/common/readline.c b/common/readline.c index 4f55e99c..46b96623 100644 --- a/common/readline.c +++ b/common/readline.c @@ -49,11 +49,11 @@ char *read_line(FILE *file) { return string; } -char *peek_line(FILE *file, int offset, long *position) { +char *peek_line(FILE *file, int line_offset, long *position) { long pos = ftell(file); - size_t length = 1; - char *line = calloc(1, length); - for (int i = 0; i <= offset; i++) { + size_t length = 0; + char *line = NULL; + for (int i = 0; i <= line_offset; i++) { ssize_t read = getline(&line, &length, file); if (read < 0) { break; -- cgit v1.2.3 From 85a5c8dabd2561dfe616bea50faf2549e8cb345e Mon Sep 17 00:00:00 2001 From: Brian Ashworth Date: Sat, 2 Jun 2018 08:05:43 -0400 Subject: Fix infinite loop in peek_line for EOF blanks --- common/readline.c | 2 ++ 1 file changed, 2 insertions(+) (limited to 'common') diff --git a/common/readline.c b/common/readline.c index 46b96623..1c396a90 100644 --- a/common/readline.c +++ b/common/readline.c @@ -56,6 +56,8 @@ char *peek_line(FILE *file, int line_offset, long *position) { for (int i = 0; i <= line_offset; i++) { ssize_t read = getline(&line, &length, file); if (read < 0) { + free(line); + line = NULL; break; } if (read > 0 && line[read - 1] == '\n') { -- cgit v1.2.3