From 29052c46190c7fe5022066effcb4a679fce10793 Mon Sep 17 00:00:00 2001 From: Alexandre Duret-Lutz Date: Thu, 16 Jul 2015 22:25:34 +0200 Subject: [PATCH] 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. --- NEWS | 2 ++ src/bin/common_trans.cc | 39 +++++++++++++++++++++++++++++++++------ src/tests/ltlcross3.test | 8 ++++++++ 3 files changed, 43 insertions(+), 6 deletions(-) diff --git a/NEWS b/NEWS index 6563ab45f..de3704b0a 100644 --- a/NEWS +++ b/NEWS @@ -36,6 +36,8 @@ New in spot 1.99.1a (not yet released) - scc_filter() would incorrectly remove Fin marks from rejecting SCCs. - the libspotltsmin library is installed + - ltlcross and ltldo did not properly quote atomic propositions + and temporary file names containing a single-quote New in spot 1.99.1 (2015-06-23) diff --git a/src/bin/common_trans.cc b/src/bin/common_trans.cc index c10adc014..7cbc01d5b 100644 --- a/src/bin/common_trans.cc +++ b/src/bin/common_trans.cc @@ -146,12 +146,39 @@ translator_spec::~translator_spec() std::vector 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::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(this)->val_ = spot::create_tmpfile(prefix); } - os << '\'' << val_ << '\''; + quote_shell_string(os, val()->name()); } diff --git a/src/tests/ltlcross3.test b/src/tests/ltlcross3.test index 5bfa26ed5..6438e3b9a 100755 --- a/src/tests/ltlcross3.test +++ b/src/tests/ltlcross3.test @@ -33,6 +33,14 @@ check_csv() ltl2tgba=../../bin/ltl2tgba +# Make sure ltlcross quotes formulas correctly +cat >formula <<\EOF +G"a'-'>'b" +EOF +run 0 ../../bin/ltlcross -F formula --csv=out.csv \ + "$ltl2tgba -s %f >%N" \ + "$ltl2tgba --lenient -s %s >%N" + run 2 ../../bin/ltlcross "$ltl2tgba -s %f >%N" 'foo bar' 2>stderr -f a grep 'ltlcross.*no input.*in.*foo bar' stderr