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 | |
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>
-rw-r--r-- | common/readline.c | 7 | ||||
-rw-r--r-- | sway/config.c | 42 |
2 files changed, 8 insertions, 41 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) { |
diff --git a/sway/config.c b/sway/config.c index c1eec22f..15108123 100644 --- a/sway/config.c +++ b/sway/config.c | |||
@@ -455,11 +455,10 @@ bool load_include_configs(const char *path, struct sway_config *config) { | |||
455 | 455 | ||
456 | bool read_config(FILE *file, struct sway_config *config) { | 456 | bool read_config(FILE *file, struct sway_config *config) { |
457 | bool success = true; | 457 | bool success = true; |
458 | bool multiline = false; | ||
459 | enum cmd_status block = CMD_BLOCK_END; | 458 | enum cmd_status block = CMD_BLOCK_END; |
460 | 459 | ||
461 | int line_number = 0; | 460 | int line_number = 0; |
462 | char *line, *mlinebuf = NULL; | 461 | char *line; |
463 | while (!feof(file)) { | 462 | while (!feof(file)) { |
464 | line = read_line(file); | 463 | line = read_line(file); |
465 | line_number++; | 464 | line_number++; |
@@ -468,45 +467,6 @@ bool read_config(FILE *file, struct sway_config *config) { | |||
468 | free(line); | 467 | free(line); |
469 | continue; | 468 | continue; |
470 | } | 469 | } |
471 | size_t length = strlen(line); | ||
472 | if (line[length-1] == '\\') { | ||
473 | // Start of multiline | ||
474 | if (feof(file)){ | ||
475 | sway_log(L_ERROR, "Error on line %i '%s': Unexpected EOF on "\ | ||
476 | "multiline command", line_number, line); | ||
477 | free(line); | ||
478 | continue; | ||
479 | } | ||
480 | line[length-1] = '\0'; | ||
481 | multiline = true; | ||
482 | } else | ||
483 | multiline = false; | ||
484 | |||
485 | if (multiline || mlinebuf){ | ||
486 | size_t mlinebuf_length; | ||
487 | if (mlinebuf) | ||
488 | mlinebuf_length = strlen(mlinebuf); | ||
489 | else | ||
490 | mlinebuf_length = 0; | ||
491 | |||
492 | char *tmp = malloc(mlinebuf_length+length+1); // + '\0' | ||
493 | tmp[0]='\0'; // if mlinebuf_length==0 strncpy won't do anything. Make a null string. | ||
494 | strncpy(tmp, mlinebuf, mlinebuf_length); | ||
495 | tmp[mlinebuf_length]='\0'; // strncpy won't add '\0' at the end... | ||
496 | strcat(tmp, line); | ||
497 | if (mlinebuf) | ||
498 | free(mlinebuf); | ||
499 | free(line); | ||
500 | mlinebuf = tmp; | ||
501 | if (multiline) // The following line is part of a multi line config. | ||
502 | continue; | ||
503 | else { // This is the last line of a multi line config. | ||
504 | line = mlinebuf; | ||
505 | sway_log(L_INFO, "Processing parsed multiline command '%s'", line); | ||
506 | mlinebuf = NULL; | ||
507 | } | ||
508 | } | ||
509 | |||
510 | struct cmd_results *res = config_command(line, block); | 470 | struct cmd_results *res = config_command(line, block); |
511 | switch(res->status) { | 471 | switch(res->status) { |
512 | case CMD_FAILURE: | 472 | case CMD_FAILURE: |