bin: better shell quoting for ltlcross and ltldo

* src/bin/common_trans.cc: Use double-quotes when single-quotes
cannot do.
* src/tests/ltlcross3.test: Add a test case.
* NEWS: Mention it.
This commit is contained in:
Alexandre Duret-Lutz 2015-07-16 22:25:34 +02:00
parent 31f5ea75b2
commit 29052c4619
3 changed files with 43 additions and 6 deletions

View file

@ -146,12 +146,39 @@ translator_spec::~translator_spec()
std::vector<translator_spec> translators;
void
quoted_string::print(std::ostream& os, const char* pos) const
static void
quote_shell_string(std::ostream& os, const char* str)
{
os << '\'';
this->spot::printable_value<std::string>::print(os, pos);
os << '\'';
// Single quotes are best, unless the string to quote contains one.
if (!strchr(str, '\''))
{
os << '\'' << str << '\'';
}
else
{
// In double quotes we have to escape $ ` " or \.
os << '"';
while (*str)
switch (*str)
{
case '$':
case '`':
case '"':
case '\\':
os << '\\';
// fall through
default:
os << *str++;
break;
}
os << '"';
}
}
void
quoted_string::print(std::ostream& os, const char*) const
{
quote_shell_string(os, val().c_str());
}
printable_result_filename::printable_result_filename()
@ -204,7 +231,7 @@ printable_result_filename::print(std::ostream& os, const char* pos) const
const_cast<printable_result_filename*>(this)->val_
= spot::create_tmpfile(prefix);
}
os << '\'' << val_ << '\'';
quote_shell_string(os, val()->name());
}