Debian Codegraph @

CodeLite/clCommandProcessor.cpp

raw
1 #include "clCommandProcessor.h"
2 #include "cl_command_event.h"
3 #include "processreaderthread.h"
4
5 wxDEFINE_EVENT(wxEVT_COMMAND_PROCESSOR_ENDED, clCommandEvent);
6 wxDEFINE_EVENT(wxEVT_COMMAND_PROCESSOR_OUTPUT, clCommandEvent);
7
8 clCommandProcessor::clCommandProcessor(const wxString& command, const wxString& wd, size_t processFlags)
9 : m_next(NULL)
10 , m_prev(NULL)
11 , m_process(NULL)
12 , m_command(command)
13 , m_workingDirectory(wd)
14 , m_processFlags(processFlags)
15 , m_postExecCallback(NULL)
16 , m_obj(NULL)
17 {
18 Bind(wxEVT_ASYNC_PROCESS_OUTPUT, &clCommandProcessor::OnProcessOutput, this);
19 Bind(wxEVT_ASYNC_PROCESS_TERMINATED, &clCommandProcessor::OnProcessTerminated, this);
20
21 }
22
23 clCommandProcessor::~clCommandProcessor() { wxDELETE(m_process); }
24
25 void clCommandProcessor::ExecuteCommand()
26 {
27 wxString message;
28 message << _("Executing: ") << m_command << " [ wd: " << m_workingDirectory << " ]";
29
30 clCommandEvent eventStart(wxEVT_COMMAND_PROCESSOR_OUTPUT);
31 eventStart.SetString(message);
32 GetFirst()->ProcessEvent(eventStart);
33
34 m_output.Clear();
35 m_process = ::CreateAsyncProcess(this, m_command, m_processFlags, m_workingDirectory);
36 if(!m_process) {
37 clCommandEvent eventEnd(wxEVT_COMMAND_PROCESSOR_ENDED);
38 eventEnd.SetString(wxString::Format(_("Failed to execute command: %s"), m_command));
39 GetFirst()->ProcessEvent(eventEnd);
40 DeleteChain();
41 }
42 m_process->SetHardKill(true);
43 }
44
45 void clCommandProcessor::OnProcessOutput(clProcessEvent& event)
46 {
47 clCommandEvent eventStart(wxEVT_COMMAND_PROCESSOR_OUTPUT);
48 m_output << event.GetOutput();
49 eventStart.SetString(event.GetOutput());
50 GetFirst()->ProcessEvent(eventStart);
51 if(eventStart.GetString() != event.GetOutput()) {
52 // user provided some input, write it to the running process
53 m_process->WriteToConsole(eventStart.GetString());
54 }
55 }
56
57 void clCommandProcessor::OnProcessTerminated(clProcessEvent& event)
58 {
59 if(m_obj && m_postExecCallback) {
60 // Call the user callback, if the user returns false
61 // stop the processor
62 if(!(m_obj->*m_postExecCallback)(this)) {
63 clCommandEvent eventEnd(wxEVT_COMMAND_PROCESSOR_ENDED);
64 GetFirst()->ProcessEvent(eventEnd);
65 DeleteChain();
66 return;
67 }
68 }
69
70 if(m_next) {
71 wxDELETE(m_process);
72 // more commands, don't report an 'END' event
73 m_next->ExecuteCommand();
74
75 } else {
76 // no more commands to execute, delete the entire chain
77 clCommandEvent eventEnd(wxEVT_COMMAND_PROCESSOR_ENDED);
78 GetFirst()->ProcessEvent(eventEnd);
79 DeleteChain();
80 }
81 }
82
83 clCommandProcessor* clCommandProcessor::Link(clCommandProcessor* next)
84 {
85 this->m_next = next;
86 if(m_next) {
87 m_next->m_prev = this;
88 }
89 return next;
90 }
91
92 void clCommandProcessor::DeleteChain()
93 {
94 // Move to the first one in the list
95 clCommandProcessor* first = GetFirst();
96
97 // delete
98 while(first) {
99 clCommandProcessor* next = first->m_next;
100 wxDELETE(first);
101 first = next;
102 }
103 }
104
105 clCommandProcessor* clCommandProcessor::GetFirst()
106 {
107 clCommandProcessor* first = this;
108 while(first->m_prev) {
109 first = first->m_prev;
110 }
111 return first;
112 }
113
114 void clCommandProcessor::Terminate()
115 {
116 clCommandProcessor* first = GetFirst();
117 while(first) {
118 if(first->m_process) {
119 first->m_process->Terminate();
120 break;
121 }
122 first = first->m_next;
123 }
124 }
125
126 clCommandProcessor* clCommandProcessor::GetActiveProcess()
127 {
128 clCommandProcessor* first = GetFirst();
129 while(first) {
130 if(first->m_process) {
131 return first;
132 }
133 first = first->m_next;
134 }
135 return NULL;
136 }