From d19fbd9cc77d1f86d30c5f9f6dff71fee7012dd6 Mon Sep 17 00:00:00 2001 From: eidheim Date: Fri, 1 Jan 2016 22:53:57 +0100 Subject: [PATCH] Executable path escaping improvement --- src/cmake.cc | 8 ++++---- src/filesystem.cc | 20 ++++++++++++++++++++ src/filesystem.h | 3 +++ src/window.cc | 26 ++++++-------------------- src/window.h | 1 - 5 files changed, 33 insertions(+), 25 deletions(-) diff --git a/src/cmake.cc b/src/cmake.cc index 8e79f4f..c8bcff6 100644 --- a/src/cmake.cc +++ b/src/cmake.cc @@ -115,8 +115,8 @@ bool CMake::create_compile_commands(const boost::filesystem::path &project_path) return false; auto compile_commands_path=default_build_path/"compile_commands.json"; Dialog::Message message("Creating "+compile_commands_path.string()); - auto exit_status=Terminal::get().process(Config::get().terminal.cmake_command+" \""+ - project_path.string()+"\" -DCMAKE_EXPORT_COMPILE_COMMANDS=ON", default_build_path); + auto exit_status=Terminal::get().process(Config::get().terminal.cmake_command+" "+ + filesystem::escape(project_path)+" -DCMAKE_EXPORT_COMPILE_COMMANDS=ON", default_build_path); message.hide(); if(exit_status==EXIT_SUCCESS) { #ifdef _WIN32 //Temporary fix to MSYS2's libclang @@ -145,8 +145,8 @@ bool CMake::create_debug_build(const boost::filesystem::path &project_path) { std::unique_ptr message; if(!boost::filesystem::exists(debug_build_path/"CMakeCache.txt")) message=std::unique_ptr(new Dialog::Message("Creating debug build")); - auto exit_status=Terminal::get().process(Config::get().terminal.cmake_command+" \""+ - project_path.string()+"\" -DCMAKE_BUILD_TYPE=Debug", debug_build_path); + auto exit_status=Terminal::get().process(Config::get().terminal.cmake_command+" "+ + filesystem::escape(project_path)+" -DCMAKE_BUILD_TYPE=Debug", debug_build_path); if(message) message->hide(); if(exit_status==EXIT_SUCCESS) diff --git a/src/filesystem.cc b/src/filesystem.cc index 02d05fd..e2a62ef 100644 --- a/src/filesystem.cc +++ b/src/filesystem.cc @@ -135,3 +135,23 @@ bool filesystem::write(const std::string &path, Glib::RefPtr bu } return false; } + +std::string filesystem::escape(const std::string &path) { + auto escaped=path; + size_t pos=0; + while((pos=escaped.find(' ', pos))!=std::string::npos) { + escaped.replace(pos, 1, "\\ "); + pos+=2; + } + return escaped; +} + +std::string filesystem::unescape(const std::string &path) { + auto escaped=path; + size_t pos=0; + while((pos=escaped.find("\\ ", pos))!=std::string::npos) { + escaped.replace(pos, 2, " "); + pos+=1; + } + return escaped; +} diff --git a/src/filesystem.h b/src/filesystem.h index 10d51bb..f4a0e6d 100644 --- a/src/filesystem.h +++ b/src/filesystem.h @@ -25,5 +25,8 @@ public: static bool write(const std::string &path, Glib::RefPtr text_buffer); static bool write(const boost::filesystem::path &path, Glib::RefPtr text_buffer) { return write(path.string(), text_buffer); } + static std::string escape(const std::string &path); + static std::string escape(const boost::filesystem::path &path) { return escape(path.string()); }; + static std::string unescape(const std::string &path); }; #endif // JUCI_FILESYSTEM_H_ diff --git a/src/window.cc b/src/window.cc index e1a95a4..a88bc79 100644 --- a/src/window.cc +++ b/src/window.cc @@ -178,21 +178,6 @@ std::unique_ptr Window::get_cmake() { return std::unique_ptr(new CMake(path)); } -void Window::escape_executable(std::string &executable) { - bool backslash=false; - for(auto it=executable.begin();it!=executable.end();) { - if(*it=='\\' && !backslash) - backslash=true; - else if(backslash) - backslash=false; - else if(*it==' ') { - it=executable.insert(it, '\\'); - it++; - } - it++; - } -} - void Window::configure() { Config::get().load(); auto style_context = Gtk::StyleContext::create(); @@ -604,10 +589,11 @@ void Window::set_menu_actions() { size_t pos=executable.find(project_path.string()); if(pos!=std::string::npos) executable.replace(pos, project_path.string().size(), default_build_path.string()); - escape_executable(executable); } - run_arguments=executable; + run_arguments=filesystem::escape(executable); } + else + run_arguments=cmake.project_path.string(); } entry_box.clear(); @@ -663,7 +649,7 @@ void Window::set_menu_actions() { size_t pos=command.find(project_path.string()); if(pos!=std::string::npos) command.replace(pos, project_path.string().size(), default_build_path.string()); - escape_executable(command); + command=filesystem::escape(command); } compiling=true; @@ -775,7 +761,7 @@ void Window::set_menu_actions() { size_t pos=command.find(project_path.string()); if(pos!=std::string::npos) command.replace(pos, project_path.string().size(), debug_build_path.string()); - escape_executable(command); + command=filesystem::escape(command); //} auto breakpoints=std::make_shared > >(); @@ -796,7 +782,7 @@ void Window::set_menu_actions() { if(exit_status!=EXIT_SUCCESS) debugging=false; else { - Debug::get().start(breakpoints, command, debug_build_path, [this, command](int exit_status){ + Debug::get().start(breakpoints, filesystem::unescape(command), debug_build_path, [this, command](int exit_status){ debugging=false; Terminal::get().async_print(command+" returned: "+std::to_string(exit_status)+'\n'); }, [this](const std::string &status) { diff --git a/src/window.h b/src/window.h index 342b40a..0174918 100644 --- a/src/window.h +++ b/src/window.h @@ -47,7 +47,6 @@ private: Glib::Dispatcher debug_update_status; std::unique_ptr get_cmake(); - void escape_executable(std::string &executable); std::unordered_map project_run_arguments; std::unordered_map debug_run_arguments;