From 3b20617de4523faed264f337bad51d191fbeffe8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fr=C3=A9d=C3=A9ric=20Mangano?= Date: Fri, 10 Feb 2023 15:20:15 +0900 Subject: [PATCH] Parse continuation lines in --set-all --- src/cli.cc | 17 +++++++++++++++-- t/cli.cc | 16 ++++++++++++++++ 2 files changed, 31 insertions(+), 2 deletions(-) diff --git a/src/cli.cc b/src/cli.cc index 0a4a572..84b7720 100644 --- a/src/cli.cc +++ b/src/cli.cc @@ -242,6 +242,7 @@ std::list ot::read_comments(FILE* input, bool raw) char* source_line = nullptr; size_t buflen = 0; ssize_t nread; + std::string* previous_comment = nullptr; while ((nread = getline(&source_line, &buflen, input)) != -1) { if (nread > 0 && source_line[nread - 1] == '\n') --nread; // Chomp. @@ -260,14 +261,26 @@ std::list ot::read_comments(FILE* input, bool raw) if (line.empty()) { // Ignore empty lines. - } else if (line.front() == '#') { + previous_comment = nullptr; + } else if (line[0] == '#') { // Ignore comments. + previous_comment = nullptr; + } else if (line[0] == '\t') { + // Continuation line: append the current line to the previous tag. + if (previous_comment == nullptr) { + ot::status rc = {ot::st::error, "Unexpected continuation line: " + std::string(source_line, nread)}; + free(source_line); + throw rc; + } else { + line[0] = '\n'; + previous_comment->append(line); + } } else if (line.find('=') == std::string::npos) { ot::status rc = {ot::st::error, "Malformed tag: " + std::string(source_line, nread)}; free(source_line); throw rc; } else { - comments.push_back(std::move(line)); + previous_comment = &comments.emplace_back(std::move(line)); } } free(source_line); diff --git a/t/cli.cc b/t/cli.cc index 5c9f168..99d9037 100644 --- a/t/cli.cc +++ b/t/cli.cc @@ -45,6 +45,15 @@ void check_read_comments() if (comments.front() != "RAW=\xFF\xFF") throw failure("parsed user comments did not match expectations"); } + { + std::string txt = "MULTILINE=First\n\tSecond\n"s; + ot::file input = fmemopen((char*) txt.data(), txt.size(), "r"); + rc = read_comments(input.get(), comments, true); + if (rc != ot::st::ok) + throw failure("could not read comments"); + if (comments.front() != "MULTILINE=First\nSecond") + throw failure("parsed user comments did not match expectations"); + } { std::string txt = "MALFORMED\n"s; ot::file input = fmemopen((char*) txt.data(), txt.size(), "r"); @@ -52,6 +61,13 @@ void check_read_comments() if (rc != ot::st::error) throw failure("did not get the expected error reading malformed comments"); } + { + std::string txt = "\tBad"s; + ot::file input = fmemopen((char*) txt.data(), txt.size(), "r"); + rc = read_comments(input.get(), comments, true); + if (rc != ot::st::error) + throw failure("did not get the expected error reading bad continuation line"); + } } /**