diff options
author | 2018-06-02 08:09:18 -0700 | |
---|---|---|
committer | 2018-06-02 08:09:18 -0700 | |
commit | b0f2fd94791708a84aac2eccc57855a4ce8b6ec0 (patch) | |
tree | 919f22247b71c5562b3c1caff4f0bfa9dfcf0528 /common/readline.c | |
parent | Merge pull request #2080 from frsfnrrg/keyboard-remodeling (diff) | |
parent | Fix infinite loop in peek_line for EOF blanks (diff) | |
download | sway-b0f2fd94791708a84aac2eccc57855a4ce8b6ec0.tar.gz sway-b0f2fd94791708a84aac2eccc57855a4ce8b6ec0.tar.zst sway-b0f2fd94791708a84aac2eccc57855a4ce8b6ec0.zip |
Merge pull request #2070 from RedSoxFan/generic-config-blocks
Make command block implementation generic
Diffstat (limited to 'common/readline.c')
-rw-r--r-- | common/readline.c | 23 |
1 files changed, 23 insertions, 0 deletions
diff --git a/common/readline.c b/common/readline.c index ed5801de..1c396a90 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,6 +49,28 @@ char *read_line(FILE *file) { | |||
48 | return string; | 49 | return string; |
49 | } | 50 | } |
50 | 51 | ||
52 | char *peek_line(FILE *file, int line_offset, long *position) { | ||
53 | long pos = ftell(file); | ||
54 | size_t length = 0; | ||
55 | char *line = NULL; | ||
56 | for (int i = 0; i <= line_offset; i++) { | ||
57 | ssize_t read = getline(&line, &length, file); | ||
58 | if (read < 0) { | ||
59 | free(line); | ||
60 | line = NULL; | ||
61 | break; | ||
62 | } | ||
63 | if (read > 0 && line[read - 1] == '\n') { | ||
64 | line[read - 1] = '\0'; | ||
65 | } | ||
66 | } | ||
67 | if (position) { | ||
68 | *position = ftell(file); | ||
69 | } | ||
70 | fseek(file, pos, SEEK_SET); | ||
71 | return line; | ||
72 | } | ||
73 | |||
51 | char *read_line_buffer(FILE *file, char *string, size_t string_len) { | 74 | char *read_line_buffer(FILE *file, char *string, size_t string_len) { |
52 | size_t length = 0; | 75 | size_t length = 0; |
53 | if (!string) { | 76 | if (!string) { |