diff options
author | Roosembert Palacios <roosembert.palacios@epfl.ch> | 2016-06-06 00:17:27 +0200 |
---|---|---|
committer | Roosembert Palacios <roosembert.palacios@epfl.ch> | 2016-06-06 00:17:27 +0200 |
commit | 230591fa4e4911ffbb38da2ee79650e62d98415f (patch) | |
tree | c77118e96612dad70c00d4c0528b4f3815105b04 /common | |
parent | Sway: Configuration: Support for escaping line breaks. (diff) | |
download | sway-230591fa4e4911ffbb38da2ee79650e62d98415f.tar.gz sway-230591fa4e4911ffbb38da2ee79650e62d98415f.tar.zst sway-230591fa4e4911ffbb38da2ee79650e62d98415f.zip |
Common: Readline: Ignore newline on '\' escaped line ends.
Escape line return when reading from a file with the '\' character.
Similar to shell scripts.
Signed-off-by: Roosembert Palacios <roosembert.palacios@epfl.ch>
Diffstat (limited to 'common')
-rw-r--r-- | common/readline.c | 7 |
1 files changed, 7 insertions, 0 deletions
diff --git a/common/readline.c b/common/readline.c index 76ed6926..5106172c 100644 --- a/common/readline.c +++ b/common/readline.c | |||
@@ -5,17 +5,24 @@ | |||
5 | char *read_line(FILE *file) { | 5 | char *read_line(FILE *file) { |
6 | size_t length = 0, size = 128; | 6 | size_t length = 0, size = 128; |
7 | char *string = malloc(size); | 7 | char *string = malloc(size); |
8 | char lastChar = '\0'; | ||
8 | if (!string) { | 9 | if (!string) { |
9 | return NULL; | 10 | return NULL; |
10 | } | 11 | } |
11 | while (1) { | 12 | while (1) { |
12 | int c = getc(file); | 13 | int c = getc(file); |
14 | if (c == '\n' && lastChar == '\\'){ | ||
15 | --length; // Ignore last character. | ||
16 | lastChar = '\0'; | ||
17 | continue; | ||
18 | } | ||
13 | if (c == EOF || c == '\n' || c == '\0') { | 19 | if (c == EOF || c == '\n' || c == '\0') { |
14 | break; | 20 | break; |
15 | } | 21 | } |
16 | if (c == '\r') { | 22 | if (c == '\r') { |
17 | continue; | 23 | continue; |
18 | } | 24 | } |
25 | lastChar = c; | ||
19 | if (length == size) { | 26 | if (length == size) { |
20 | char *new_string = realloc(string, size *= 2); | 27 | char *new_string = realloc(string, size *= 2); |
21 | if (!new_string) { | 28 | if (!new_string) { |