mirror of
https://github.com/swaywm/sway.git
synced 2025-10-29 05:40:18 -04:00
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>
This commit is contained in:
parent
e8c0ef98b1
commit
230591fa4e
2 changed files with 8 additions and 41 deletions
|
|
@ -5,17 +5,24 @@
|
|||
char *read_line(FILE *file) {
|
||||
size_t length = 0, size = 128;
|
||||
char *string = malloc(size);
|
||||
char lastChar = '\0';
|
||||
if (!string) {
|
||||
return NULL;
|
||||
}
|
||||
while (1) {
|
||||
int c = getc(file);
|
||||
if (c == '\n' && lastChar == '\\'){
|
||||
--length; // Ignore last character.
|
||||
lastChar = '\0';
|
||||
continue;
|
||||
}
|
||||
if (c == EOF || c == '\n' || c == '\0') {
|
||||
break;
|
||||
}
|
||||
if (c == '\r') {
|
||||
continue;
|
||||
}
|
||||
lastChar = c;
|
||||
if (length == size) {
|
||||
char *new_string = realloc(string, size *= 2);
|
||||
if (!new_string) {
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue