Debian Codegraph @

CodeLite/TerminalEmulator.cpp

raw
1 #include "TerminalEmulator.h"
2 #include "dirsaver.h"
3 #include "file_logger.h"
4 #include "macros.h"
5 #include "processreaderthread.h"
6 #include <algorithm>
7 #include <wx/filename.h>
8 #include <wx/log.h>
9 #include "clConsoleBase.h"
10
11 #ifndef __WXMSW__
12 #include <signal.h>
13 #endif
14
15 wxDEFINE_EVENT(wxEVT_TERMINAL_COMMAND_EXIT, clCommandEvent);
16 wxDEFINE_EVENT(wxEVT_TERMINAL_COMMAND_OUTPUT, clCommandEvent);
17
18 class MyProcess : public wxProcess
19 {
20 public:
21 TerminalEmulator* m_parent;
22
23 public:
24 MyProcess(TerminalEmulator* parent)
25 : wxProcess(parent)
26 , m_parent(parent)
27 {
28 if(m_parent) { m_parent->m_myProcesses.push_back(this); }
29 }
30
31 virtual ~MyProcess() { m_parent = NULL; }
32 void OnTerminate(int pid, int status)
33 {
34 if(m_parent) {
35 clCommandEvent terminateEvent(wxEVT_TERMINAL_COMMAND_EXIT);
36 m_parent->AddPendingEvent(terminateEvent);
37 m_parent->m_pid = wxNOT_FOUND;
38
39 std::list<wxProcess*>::iterator iter =
40 std::find_if(m_parent->m_myProcesses.begin(), m_parent->m_myProcesses.end(),
41 [&](wxProcess* proc) { return proc == this; });
42 if(iter != m_parent->m_myProcesses.end()) { m_parent->m_myProcesses.erase(iter); }
43 delete this;
44 }
45 }
46 };
47
48 TerminalEmulator::TerminalEmulator()
49 : m_process(NULL)
50 , m_pid(wxNOT_FOUND)
51 {
52 Bind(wxEVT_ASYNC_PROCESS_OUTPUT, &TerminalEmulator::OnProcessOutput, this);
53 Bind(wxEVT_ASYNC_PROCESS_TERMINATED, &TerminalEmulator::OnProcessTerminated, this);
54 }
55
56 TerminalEmulator::~TerminalEmulator()
57 {
58 Unbind(wxEVT_ASYNC_PROCESS_OUTPUT, &TerminalEmulator::OnProcessOutput, this);
59 Unbind(wxEVT_ASYNC_PROCESS_TERMINATED, &TerminalEmulator::OnProcessTerminated, this);
60 std::for_each(m_myProcesses.begin(), m_myProcesses.end(), [&](wxProcess* proc) {
61 MyProcess* myproc = dynamic_cast<MyProcess*>(proc);
62 myproc->m_parent = NULL;
63 });
64 }
65
66 bool TerminalEmulator::ExecuteConsole(const wxString& command, bool waitOnExit, const wxString& command_args,
67 const wxString& workingDirectory, const wxString& title)
68 {
69 wxUnusedVar(title);
70 clConsoleBase::Ptr_t console = clConsoleBase::GetTerminal();
71 console->SetCommand(command, command_args);
72 console->SetWaitWhenDone(waitOnExit);
73 console->SetWorkingDirectory(workingDirectory);
74 console->SetCallback(new MyProcess(this));
75
76 wxString strTitle = title;
77 if(strTitle.IsEmpty()) {
78 strTitle << "'" << command << "'";
79 } else {
80 strTitle.Prepend("'").Append("'");
81 }
82 bool res = console->Start();
83 m_pid = console->GetPid();
84 return res;
85 }
86
87 void TerminalEmulator::OnProcessTerminated(clProcessEvent& event)
88 {
89 // Process terminated
90 wxDELETE(m_process);
91 m_pid = wxNOT_FOUND;
92 // Notify that the terminal has terminated
93 clCommandEvent terminateEvent(wxEVT_TERMINAL_COMMAND_EXIT);
94 AddPendingEvent(terminateEvent);
95 }
96
97 void TerminalEmulator::Terminate()
98 {
99 if(IsRunning()) {
100 if(m_process) { m_process->Terminate(); }
101 if(m_pid != wxNOT_FOUND) {
102 wxKill(m_pid, wxSIGKILL, NULL, wxKILL_CHILDREN);
103 m_pid = wxNOT_FOUND;
104 }
105 }
106 }
107
108 bool TerminalEmulator::IsRunning() const { return (m_process != NULL) || (m_pid != wxNOT_FOUND); }
109
110 void TerminalEmulator::OnProcessOutput(clProcessEvent& event)
111 {
112 clCommandEvent evtOutput(wxEVT_TERMINAL_COMMAND_OUTPUT);
113 evtOutput.SetString(event.GetOutput());
114 AddPendingEvent(evtOutput);
115 }
116
117 bool TerminalEmulator::ExecuteNoConsole(const wxString& commandToRun, const wxString& workingDirectory)
118 {
119 if(m_process) {
120 // another process is running
121 return false;
122 }
123
124 wxString command;
125 #ifdef __WXMSW__
126 wxString shell = wxGetenv("COMSPEC");
127 if(shell.IsEmpty()) { shell = "CMD"; }
128
129 command << shell << wxT(" /c \"");
130 command << commandToRun << wxT("\"");
131
132 #else
133 wxString tmpCmd = commandToRun;
134 command << "/bin/bash -c '";
135 // escape any single quoutes
136 tmpCmd.Replace("'", "\\'");
137 command << tmpCmd << "'";
138 #endif
139 clLogMessage("TerminalEmulator::ExecuteNoConsole: " + command);
140 m_process = ::CreateAsyncProcess(this, command, IProcessCreateWithHiddenConsole, workingDirectory);
141 return m_process != NULL;
142 }