From 8c711d30367365fa6a036e468eba4401d26131a3 Mon Sep 17 00:00:00 2001 From: hunt Date: Mon, 20 Aug 2007 17:55:03 +0000 Subject: [PATCH] 2007-08-20 Martin Hunt PR2424 From Lai Jiangshan * util.cxx (cmdstr_quoted): New. Properly quote command string. * buildrun.cxx (run_pass): Call cmdstr_quoted(). --- ChangeLog | 8 ++++++++ buildrun.cxx | 2 +- util.cxx | 31 +++++++++++++++++++++++++++++++ util.h | 1 + 4 files changed, 41 insertions(+), 1 deletion(-) diff --git a/ChangeLog b/ChangeLog index 636297917..462c1f424 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,11 @@ +2007-08-20 Martin Hunt + PR2424 + From Lai Jiangshan + + * util.cxx (cmdstr_quoted): New. Properly quote + command string. + * buildrun.cxx (run_pass): Call cmdstr_quoted(). + 2007-08-20 Frank Ch. Eigler From Satoru MORIYA diff --git a/buildrun.cxx b/buildrun.cxx index 221d04631..2fb3439ca 100644 --- a/buildrun.cxx +++ b/buildrun.cxx @@ -139,7 +139,7 @@ run_pass (systemtap_session& s) staprun_cmd += "-d " + stringify(getpid()) + " "; if (s.cmd != "") - staprun_cmd += "-c \"" + s.cmd + "\" "; + staprun_cmd += "-c " + cmdstr_quoted(s.cmd) + " "; if (s.target_pid) staprun_cmd += "-t " + stringify(s.target_pid) + " "; diff --git a/util.cxx b/util.cxx index af9533957..97425d763 100644 --- a/util.cxx +++ b/util.cxx @@ -197,3 +197,34 @@ find_executable(const char *name, string& retpath) return false; } + +const string cmdstr_quoted(const string& cmd) +{ + // original cmd : substr1 + // or : substr1'substr2 + // or : substr1'substr2'substr3...... + // after quoted : + // every substr(even it's empty) is quoted by '' + // every single-quote(') is quoted by "" + // examples: substr1 --> 'substr1' + // substr1'substr2 --> 'substr1'"'"'substr2' + + string quoted_cmd; + string quote("'"); + string replace("'\"'\"'"); + string::size_type pos = 0; + + quoted_cmd += quote; + for (string::size_type quote_pos = cmd.find(quote, pos); + quote_pos != string::npos; + quote_pos = cmd.find(quote, pos)) { + quoted_cmd += cmd.substr(pos, quote_pos - pos); + quoted_cmd += replace; + pos = quote_pos + 1; + } + quoted_cmd += cmd.substr(pos, cmd.length() - pos); + quoted_cmd += quote; + + return quoted_cmd; +} + diff --git a/util.h b/util.h index f9c298ec8..97fa7062d 100644 --- a/util.h +++ b/util.h @@ -11,6 +11,7 @@ int create_dir(const char *dir); void tokenize(const std::string& str, std::vector& tokens, const std::string& delimiters); bool find_executable(const char *name, std::string& retpath); +const std::string cmdstr_quoted(const std::string& cmd); // stringification generics -- 2.43.5