bin: fix handling of -o '>>-' in the close/reopen path

* tests/core/serial.test: Add test case.
* bin/common_file.cc: Fix it.
This commit is contained in:
Alexandre Duret-Lutz 2023-07-26 21:50:16 +02:00
parent 0923f8efe2
commit 61e43edde8
2 changed files with 25 additions and 6 deletions

View file

@ -47,16 +47,13 @@ output_file::output_file(const char* name, bool force_append)
void
output_file::reopen_for_append(const std::string& name)
{
if (of_ && of_->is_open()) // nothing to do
if (os_ == &std::cout || of_->is_open()) // nothing to do
return;
const char* cname = name.c_str();
if (cname[0] == '>' && cname[1] == '>')
cname += 2;
if (name[0] == '-' && name[1] == 0)
{
os_ = &std::cout;
return;
}
// the name cannot be "-" at this point, otherwise os_ would be
// equal to std::cout.
of_->open(cname, std::ios_base::app);
if (!*of_)
error(2, errno, "cannot reopen '%s'", cname);

View file

@ -91,3 +91,25 @@ cat >expected <<EOF
5 pass2
EOF
diff out expected
# The case where the output filename is a computed "-".
ltl2tgba 1 --name '-' > naut-.hoa
for i in 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15; do
ltl2tgba 1 --name "file$i" > naut$i.hoa
done
autfilt naut*.hoa naut*.hoa --output='>>%M' --format=%M > stdout
cat >expected <<EOF
-
-
EOF
diff stdout expected
cat >expected7 <<EOF
file7
file7
EOF
diff file7 expected7
cat >expected15 <<EOF
file15
file15
EOF
diff file15 expected15