config: fix line number with continued lines

When the config has continued lines, `get_line_with_cont` may read more
than one line of the actual file. When displaying line numbers for error
messages, they should be the line number in the file to make it easy to
find and fix the issue.
master
Brian Ashworth 6 years ago
parent d256182f49
commit b43345a1a3

@ -571,15 +571,18 @@ bool load_include_configs(const char *path, struct sway_config *config,
} }
// get line, with backslash continuation // get line, with backslash continuation
static ssize_t getline_with_cont(char **lineptr, size_t *line_size, FILE *file) { static ssize_t getline_with_cont(char **lineptr, size_t *line_size, FILE *file,
int *nlines) {
char *next_line = NULL; char *next_line = NULL;
size_t next_line_size = 0; size_t next_line_size = 0;
ssize_t nread = getline(lineptr, line_size, file); ssize_t nread = getline(lineptr, line_size, file);
*nlines = nread == -1 ? 0 : 1;
while (nread >= 2 && strcmp(&(*lineptr)[nread - 2], "\\\n") == 0) { while (nread >= 2 && strcmp(&(*lineptr)[nread - 2], "\\\n") == 0) {
ssize_t next_nread = getline(&next_line, &next_line_size, file); ssize_t next_nread = getline(&next_line, &next_line_size, file);
if (next_nread == -1) { if (next_nread == -1) {
break; break;
} }
(*nlines)++;
nread += next_nread - 2; nread += next_nread - 2;
if ((ssize_t) *line_size < nread + 1) { if ((ssize_t) *line_size < nread + 1) {
@ -663,7 +666,8 @@ bool read_config(FILE *file, struct sway_config *config,
ssize_t nread; ssize_t nread;
list_t *stack = create_list(); list_t *stack = create_list();
size_t read = 0; size_t read = 0;
while ((nread = getline_with_cont(&line, &line_size, file)) != -1) { int nlines = 0;
while ((nread = getline_with_cont(&line, &line_size, file, &nlines)) != -1) {
if (reading_main_config) { if (reading_main_config) {
if (read + nread > config_size) { if (read + nread > config_size) {
wlr_log(WLR_ERROR, "Config file changed during reading"); wlr_log(WLR_ERROR, "Config file changed during reading");
@ -679,7 +683,7 @@ bool read_config(FILE *file, struct sway_config *config,
line[nread - 1] = '\0'; line[nread - 1] = '\0';
} }
line_number++; line_number += nlines;
wlr_log(WLR_DEBUG, "Read line %d: %s", line_number, line); wlr_log(WLR_DEBUG, "Read line %d: %s", line_number, line);
strip_whitespace(line); strip_whitespace(line);

Loading…
Cancel
Save