Do not count '\' in comment as line continuation (#2181)

fixes #2176
This commit is contained in:
Johannes Lange 2016-05-08 00:20:08 +02:00 committed by Michael Stapelberg
parent 6746aa4b5f
commit a8757625c3
3 changed files with 33 additions and 4 deletions

View File

@ -1115,11 +1115,15 @@ show_marks yes
Config files support line continuation, meaning when you end a line in a Config files support line continuation, meaning when you end a line in a
backslash character (`\`), the line-break will be ignored by the parser. This backslash character (`\`), the line-break will be ignored by the parser. This
feature can be used to create more readable configuration files. feature can be used to create more readable configuration files.
Commented lines are not continued.
*Examples*: *Examples*:
------------------- -------------------
bindsym Mod1+f \ bindsym Mod1+f \
fullscreen toggle fullscreen toggle
# this line is not continued \
bindsym Mod1+F fullscreen toggle
------------------- -------------------
== Configuring i3bar == Configuring i3bar

View File

@ -846,17 +846,26 @@ bool parse_file(const char *f, bool use_nagbar) {
if (buffer[strlen(buffer) - 1] != '\n' && !feof(fstr)) { if (buffer[strlen(buffer) - 1] != '\n' && !feof(fstr)) {
ELOG("Your line continuation is too long, it exceeds %zd bytes\n", sizeof(buffer)); ELOG("Your line continuation is too long, it exceeds %zd bytes\n", sizeof(buffer));
} }
/* sscanf implicitly strips whitespace. */
const bool skip_line = (sscanf(buffer, "%511s %511[^\n]", key, value) < 1 || strlen(key) < 3);
const bool comment = (key[0] == '#');
continuation = strstr(buffer, "\\\n"); continuation = strstr(buffer, "\\\n");
if (continuation) { if (continuation) {
continue; if (!comment) {
continue;
}
DLOG("line continuation in comment is ignored: \"%.*s\"\n", (int)strlen(buffer) - 1, buffer);
continuation = NULL;
} }
strncpy(buf + strlen(buf), buffer, strlen(buffer) + 1); strncpy(buf + strlen(buf), buffer, strlen(buffer) + 1);
/* sscanf implicitly strips whitespace. Also, we skip comments and empty lines. */ /* Skip comments and empty lines. */
if (sscanf(buffer, "%511s %511[^\n]", key, value) < 1 || if (skip_line || comment) {
key[0] == '#' || strlen(key) < 3)
continue; continue;
}
if (strcasecmp(key, "set") == 0) { if (strcasecmp(key, "set") == 0) {
if (value[0] != '$') { if (value[0] != '$') {

View File

@ -198,4 +198,20 @@ EOT
is(launch_get_border($config), 'none', 'no border'); is(launch_get_border($config), 'none', 'no border');
#####################################################################
# test ignoring of line continuation within a comment
#####################################################################
$config = <<'EOT';
# i3 config file (v4)
font -misc-fixed-medium-r-normal--13-120-75-75-C-70-iso10646-1
set $vartest \"special title\"
for_window [title="$vartest"] border pixel 1
# this line is not continued, so the following is not contained in this comment\
for_window [title="$vartest"] border none
EOT
is(launch_get_border($config), 'none', 'no border');
done_testing; done_testing;