| 1 |
#include "clConsoleBase.h" |
| 2 |
|
| 3 |
#include "clConsoleAlacritty.hpp" |
| 4 |
#include "clConsoleCMD.h" |
| 5 |
#include "clConsoleCodeLiteTerminal.h" |
| 6 |
#include "clConsoleGnomeTerminal.h" |
| 7 |
#include "clConsoleKonsole.h" |
| 8 |
#include "clConsoleLXTerminal.h" |
| 9 |
#include "clConsoleMateTerminal.h" |
| 10 |
#include "clConsoleOSXTerminal.h" |
| 11 |
#include "clConsoleQTerminal.h" |
| 12 |
#include "clConsoleRXVTerminal.h" |
| 13 |
#include "clConsoleXfce4Terminal.h" |
| 14 |
#include "cl_config.h" |
| 15 |
#include "file_logger.h" |
| 16 |
#include "fileutils.h" |
| 17 |
|
| 18 |
#include <algorithm> |
| 19 |
#include <wx/utils.h> |
| 20 |
|
| 21 |
wxDEFINE_EVENT(wxEVT_TERMINAL_EXIT, clProcessEvent); |
| 22 |
|
| 23 |
class ConsoleProcess : public wxProcess |
| 24 |
{ |
| 25 |
public: |
| 26 |
wxEvtHandler* m_sink = nullptr; |
| 27 |
wxString m_uid; |
| 28 |
|
| 29 |
public: |
| 30 |
ConsoleProcess(wxEvtHandler* sink, const wxString& uid) |
| 31 |
: m_sink(sink) |
| 32 |
, m_uid(uid) |
| 33 |
{ |
| 34 |
} |
| 35 |
|
| 36 |
virtual ~ConsoleProcess() { m_sink = NULL; } |
| 37 |
void OnTerminate(int pid, int status) |
| 38 |
{ |
| 39 |
clProcessEvent terminateEvent(wxEVT_TERMINAL_EXIT); |
| 40 |
terminateEvent.SetString(m_uid); |
| 41 |
terminateEvent.SetInt(status); // pass the exit code |
| 42 |
m_sink->AddPendingEvent(terminateEvent); |
| 43 |
delete this; |
| 44 |
} |
| 45 |
}; |
| 46 |
|
| 47 |
clConsoleBase::clConsoleBase() {} |
| 48 |
|
| 49 |
clConsoleBase::~clConsoleBase() {} |
| 50 |
|
| 51 |
clConsoleBase::Ptr_t clConsoleBase::GetTerminal() |
| 52 |
{ |
| 53 |
clConsoleBase::Ptr_t terminal; |
| 54 |
wxString terminalName = GetSelectedTerminalName(); |
| 55 |
#ifdef __WXMSW__ |
| 56 |
if(terminalName.CmpNoCase("codelite-terminal") == 0) { |
| 57 |
terminal.reset(new clConsoleCodeLiteTerminal()); |
| 58 |
} else if(terminalName.CmpNoCase("alacritty") == 0) { |
| 59 |
terminal.reset(new clConsoleAlacritty()); |
| 60 |
} else { |
| 61 |
terminal.reset(new clConsoleCMD()); |
| 62 |
} |
| 63 |
#elif defined(__WXGTK__) |
| 64 |
if(terminalName.CmpNoCase("konsole") == 0) { |
| 65 |
terminal.reset(new clConsoleKonsole()); |
| 66 |
} else if(terminalName.CmpNoCase("alacritty") == 0) { |
| 67 |
terminal.reset(new clConsoleAlacritty()); |
| 68 |
} else if(terminalName.CmpNoCase("lxterminal") == 0) { |
| 69 |
terminal.reset(new clConsoleLXTerminal()); |
| 70 |
} else if(terminalName.CmpNoCase("mate-terminal") == 0) { |
| 71 |
terminal.reset(new clConsoleMateTerminal()); |
| 72 |
} else if(terminalName.CmpNoCase("xfce4-terminal") == 0) { |
| 73 |
terminal.reset(new clConsoleXfce4Terminal()); |
| 74 |
} else if(terminalName.CmpNoCase("qterminal") == 0) { |
| 75 |
terminal.reset(new clConsoleQTerminal()); |
| 76 |
} else if(terminalName.CmpNoCase("rxvt-unicode") == 0) { |
| 77 |
terminal.reset(new clConsoleRXVTTerminal()); |
| 78 |
} else if(terminalName.CmpNoCase("codelite-terminal") == 0) { |
| 79 |
terminal.reset(new clConsoleCodeLiteTerminal()); |
| 80 |
} else { |
| 81 |
// the default terminal is "gnome-terminal" |
| 82 |
terminal.reset(new clConsoleGnomeTerminal()); |
| 83 |
} |
| 84 |
#else |
| 85 |
clConsoleOSXTerminal* t = new clConsoleOSXTerminal(); |
| 86 |
terminal.reset(t); |
| 87 |
if(terminalName.CmpNoCase("iTerm2") == 0) { |
| 88 |
t->SetTerminalApp("iTerm"); |
| 89 |
} else if(terminalName.CmpNoCase("alacritty") == 0) { |
| 90 |
terminal.reset(new clConsoleAlacritty()); |
| 91 |
} |
| 92 |
#endif |
| 93 |
return terminal; |
| 94 |
} |
| 95 |
|
| 96 |
wxArrayString clConsoleBase::GetAvailaleTerminals() |
| 97 |
{ |
| 98 |
wxArrayString terminals; |
| 99 |
#ifdef __WXMSW__ |
| 100 |
terminals.Add("CMD"); |
| 101 |
terminals.Add("codelite-terminal"); |
| 102 |
#elif defined(__WXGTK__) |
| 103 |
terminals.Add("konsole"); |
| 104 |
terminals.Add("gnome-terminal"); |
| 105 |
terminals.Add("lxterminal"); |
| 106 |
terminals.Add("mate-terminal"); |
| 107 |
terminals.Add("qterminal"); |
| 108 |
terminals.Add("xfce4-terminal"); |
| 109 |
terminals.Add("rxvt-unicode"); |
| 110 |
terminals.Add("codelite-terminal"); |
| 111 |
#else |
| 112 |
terminals.Add("Terminal"); |
| 113 |
terminals.Add("iTerm2"); |
| 114 |
#endif |
| 115 |
terminals.Add("alacritty"); |
| 116 |
return terminals; |
| 117 |
} |
| 118 |
|
| 119 |
void clConsoleBase::AddEnvVariable(const wxString& name, const wxString& value) |
| 120 |
{ |
| 121 |
m_environment.erase(name); |
| 122 |
m_environment.insert({ name, value }); |
| 123 |
} |
| 124 |
|
| 125 |
wxString clConsoleBase::GetEnvironmentPrefix() const |
| 126 |
{ |
| 127 |
wxString strline; |
| 128 |
std::for_each(m_environment.begin(), m_environment.end(), |
| 129 |
[&](const wxStringMap_t::value_type& vt) { strline << vt.first << "=" << vt.second << " "; }); |
| 130 |
return strline; |
| 131 |
} |
| 132 |
|
| 133 |
wxString clConsoleBase::WrapWithQuotesIfNeeded(const wxString& s) const |
| 134 |
{ |
| 135 |
wxString strimmed = s; |
| 136 |
strimmed.Trim().Trim(false); |
| 137 |
if(strimmed.Contains(" ")) { |
| 138 |
strimmed.Prepend("\"").Append("\""); |
| 139 |
} |
| 140 |
return strimmed; |
| 141 |
} |
| 142 |
|
| 143 |
wxString clConsoleBase::EscapeString(const wxString& str, const wxString& c) const |
| 144 |
{ |
| 145 |
wxString escaped = str; |
| 146 |
escaped.Replace(c, wxString() << "\\" << c); |
| 147 |
return escaped; |
| 148 |
} |
| 149 |
|
| 150 |
bool clConsoleBase::StartProcess(const wxString& command) |
| 151 |
{ |
| 152 |
// Apply the environment variables before we launch the process |
| 153 |
clConsoleEnvironment env(GetEnvironment()); |
| 154 |
env.Apply(); |
| 155 |
|
| 156 |
wxProcess* callback = nullptr; |
| 157 |
if(m_callback) { |
| 158 |
// user provided callback |
| 159 |
callback = m_callback; |
| 160 |
} else if(m_sink) { |
| 161 |
// using events. This object will get deleted when the process exits |
| 162 |
callback = new ConsoleProcess(m_sink, m_callbackUID); |
| 163 |
} |
| 164 |
|
| 165 |
clDEBUG() << "Console: running command: `" << command << "`" << endl; |
| 166 |
|
| 167 |
SetPid(::wxExecute(command, wxEXEC_ASYNC | wxEXEC_MAKE_GROUP_LEADER | GetExecExtraFlags(), callback)); |
| 168 |
// reset the m_callback (it will auto-delete itself) |
| 169 |
m_callback = nullptr; |
| 170 |
m_sink = nullptr; |
| 171 |
m_callbackUID.clear(); |
| 172 |
return (GetPid() > 0); |
| 173 |
} |
| 174 |
|
| 175 |
wxString clConsoleBase::GetSelectedTerminalName() |
| 176 |
{ |
| 177 |
wxString terminalName = clConfig::Get().Read("Terminal", wxString()); |
| 178 |
if(terminalName.IsEmpty()) { |
| 179 |
#ifdef __WXGTK__ |
| 180 |
wxFileName file; |
| 181 |
terminalName = FileUtils::FindExe("gnome-terminal", file) ? "gnome-terminal" : "codelite-terminal"; |
| 182 |
#elif defined(__WXOSX__) |
| 183 |
terminalName = "Terminal"; |
| 184 |
#else |
| 185 |
terminalName = "CMD"; |
| 186 |
#endif |
| 187 |
} |
| 188 |
return terminalName; |
| 189 |
} |
| 190 |
|
| 191 |
clConsoleEnvironment::clConsoleEnvironment() {} |
| 192 |
|
| 193 |
clConsoleEnvironment::~clConsoleEnvironment() { UnApply(); } |
| 194 |
|
| 195 |
void clConsoleEnvironment::Add(const wxString& name, const wxString& value) |
| 196 |
{ |
| 197 |
m_environment.erase(name); |
| 198 |
m_environment.insert({ name, value }); |
| 199 |
} |
| 200 |
|
| 201 |
void clConsoleEnvironment::Apply() |
| 202 |
{ |
| 203 |
if(!m_oldEnvironment.empty()) { |
| 204 |
clWARNING() << "Refusing to apply environment. Already in a dirty state"; |
| 205 |
return; |
| 206 |
} |
| 207 |
if(m_environment.empty()) { |
| 208 |
return; |
| 209 |
} |
| 210 |
|
| 211 |
// keep a copy of the old environment before we apply the new values |
| 212 |
m_oldEnvironment.clear(); |
| 213 |
std::for_each(m_environment.begin(), m_environment.end(), [&](const wxStringMap_t::value_type& vt) { |
| 214 |
wxString envvalue; |
| 215 |
if(::wxGetEnv(vt.first, &envvalue)) { |
| 216 |
m_oldEnvironment[vt.first] = envvalue; |
| 217 |
} else { |
| 218 |
m_oldEnvironment[vt.first] = "__no_such_env__"; |
| 219 |
} |
| 220 |
::wxSetEnv(vt.first, vt.second); |
| 221 |
}); |
| 222 |
} |
| 223 |
|
| 224 |
void clConsoleEnvironment::UnApply() |
| 225 |
{ |
| 226 |
if(m_oldEnvironment.empty()) { |
| 227 |
return; |
| 228 |
} |
| 229 |
std::for_each(m_oldEnvironment.begin(), m_oldEnvironment.end(), [&](const wxStringMap_t::value_type& vt) { |
| 230 |
if(vt.second == "__no_such_env__") { |
| 231 |
::wxUnsetEnv(vt.second); |
| 232 |
} else { |
| 233 |
::wxSetEnv(vt.first, vt.second); |
| 234 |
} |
| 235 |
}); |
| 236 |
m_oldEnvironment.clear(); |
| 237 |
} |
| 238 |
|
| 239 |
clConsoleEnvironment::clConsoleEnvironment(const wxStringMap_t& env) |
| 240 |
: m_environment(env) |
| 241 |
{ |
| 242 |
} |
| 243 |
|
| 244 |
#define ADD_CURRENT_TOKEN() \ |
| 245 |
if(!curtoken.IsEmpty()) { \ |
| 246 |
curtoken.Trim().Trim(false); \ |
| 247 |
if(!curtoken.IsEmpty()) { \ |
| 248 |
outputArr.Add(curtoken); \ |
| 249 |
} \ |
| 250 |
curtoken.Clear(); \ |
| 251 |
} |
| 252 |
|
| 253 |
wxArrayString clConsoleBase::SplitArguments(const wxString& args) |
| 254 |
{ |
| 255 |
const int STATE_NORMAL = 0; |
| 256 |
const int STATE_STRING = 1; |
| 257 |
|
| 258 |
int state = STATE_NORMAL; |
| 259 |
wxArrayString outputArr; |
| 260 |
wxString curtoken; |
| 261 |
wxChar prevChar = 0; |
| 262 |
for(size_t i = 0; i < args.size(); ++i) { |
| 263 |
wxChar ch = args[i]; |
| 264 |
switch(state) { |
| 265 |
case STATE_NORMAL: { |
| 266 |
switch(ch) { |
| 267 |
case ' ': |
| 268 |
case '\t': |
| 269 |
if(prevChar == '\\') { |
| 270 |
curtoken << ch; |
| 271 |
} else { |
| 272 |
ADD_CURRENT_TOKEN(); |
| 273 |
} |
| 274 |
break; |
| 275 |
case '"': |
| 276 |
case '\'': |
| 277 |
// we dont want to keep the string markers |
| 278 |
state = STATE_STRING; |
| 279 |
break; |
| 280 |
default: |
| 281 |
curtoken << ch; |
| 282 |
break; |
| 283 |
} |
| 284 |
} break; |
| 285 |
case STATE_STRING: { |
| 286 |
switch(ch) { |
| 287 |
case '"': |
| 288 |
case '\'': |
| 289 |
if(prevChar == '\\') { |
| 290 |
curtoken << ch; |
| 291 |
} else { |
| 292 |
// we dont want to keep the string markers |
| 293 |
state = STATE_NORMAL; |
| 294 |
} |
| 295 |
break; |
| 296 |
default: |
| 297 |
curtoken << ch; |
| 298 |
break; |
| 299 |
} |
| 300 |
} break; |
| 301 |
default: |
| 302 |
break; |
| 303 |
} |
| 304 |
prevChar = ch; |
| 305 |
} |
| 306 |
// if we still got some unprocessed token, add it |
| 307 |
ADD_CURRENT_TOKEN(); |
| 308 |
return outputArr; |
| 309 |
} |
| 310 |
|
| 311 |
void clConsoleBase::SetEnvironment(const clEnvList_t& environment) |
| 312 |
{ |
| 313 |
// convert the list into map |
| 314 |
m_environment.clear(); |
| 315 |
for(const auto& p : environment) { |
| 316 |
m_environment.insert({ p.first, p.second }); |
| 317 |
} |
| 318 |
} |