Skip to content

Commit

Permalink
Parse continuation lines in --set-all
Browse files Browse the repository at this point in the history
  • Loading branch information
fmang committed Feb 10, 2023
1 parent d8a1a78 commit 3b20617
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 2 deletions.
17 changes: 15 additions & 2 deletions src/cli.cc
Original file line number Diff line number Diff line change
Expand Up @@ -242,6 +242,7 @@ std::list<std::string> 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.
Expand All @@ -260,14 +261,26 @@ std::list<std::string> 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);
Expand Down
16 changes: 16 additions & 0 deletions t/cli.cc
Original file line number Diff line number Diff line change
Expand Up @@ -45,13 +45,29 @@ 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");
rc = read_comments(input.get(), comments, false);
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");
}
}

/**
Expand Down

0 comments on commit 3b20617

Please sign in to comment.