File: ExternalToolsProcessManager.cpp

package info (click to toggle)
codelite 14.0+dfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: bullseye, sid
  • size: 112,816 kB
  • sloc: cpp: 483,662; ansic: 150,144; php: 9,569; lex: 4,186; python: 3,417; yacc: 2,820; sh: 1,147; makefile: 52; xml: 13
file content (177 lines) | stat: -rw-r--r-- 5,101 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
#include "ExternalToolsProcessManager.h"
#include "event_notifier.h"
#include "globals.h"
#include "imanager.h"
#include "asyncprocess.h"
#include "macromanager.h"
#include "environmentconfig.h"
#include "processreaderthread.h"
#include <algorithm>
#include <wx/utils.h>
#include <wx/process.h>
#include <wx/msgdlg.h>
#include "file_logger.h"
#ifndef __WXMSW__
#include <signal.h>
#endif

/**
 * @class MyProcess
 * @brief handler class for capturing non-redirected processes termination
 */
class ExtToolsMyProcess : public wxProcess
{
public:
    ExtToolsMyProcess() {}
    virtual ~ExtToolsMyProcess() {}
    void OnTerminate(int pid, int status)
    {
        ToolsTaskManager::Instance()->ProcessTerminated(pid);
        delete this;
    }
};

ToolsTaskManager* ToolsTaskManager::ms_instance = 0;

ToolsTaskManager::ToolsTaskManager()
{
    Bind(wxEVT_ASYNC_PROCESS_OUTPUT, &ToolsTaskManager::OnProcessOutput, this);
    Bind(wxEVT_ASYNC_PROCESS_TERMINATED, &ToolsTaskManager::OnProcessEnd, this);
}

ToolsTaskManager::~ToolsTaskManager()
{
    Unbind(wxEVT_ASYNC_PROCESS_OUTPUT, &ToolsTaskManager::OnProcessOutput, this);
    Unbind(wxEVT_ASYNC_PROCESS_TERMINATED, &ToolsTaskManager::OnProcessEnd, this);
}

ToolsTaskManager* ToolsTaskManager::Instance()
{
    if(ms_instance == 0) {
        ms_instance = new ToolsTaskManager();
    }
    return ms_instance;
}

void ToolsTaskManager::Release()
{
    if(ms_instance) {
        delete ms_instance;
    }
    ms_instance = 0;
}

void ToolsTaskManager::OnProcessEnd(clProcessEvent& event)
{
    clGetManager()->AppendOutputTabText(kOutputTab_Output, event.GetOutput());
    clGetManager()->AppendOutputTabText(kOutputTab_Output, "\n");

    // delete the process
    IProcess* proc = event.GetProcess();
    ProcessTerminated(proc->GetPid());
    delete proc;

    // Notify codelite to test for any modified bufferes
    EventNotifier::Get()->PostReloadExternallyModifiedEvent();
}

void ToolsTaskManager::OnProcessOutput(clProcessEvent& event)
{
    clGetManager()->AppendOutputTabText(kOutputTab_Output, event.GetOutput());
}

void ToolsTaskManager::StartTool(const ToolInfo& ti, const wxString& filename)
{
    wxString command, working_dir;
    command << ti.GetPath();
    //::WrapWithQuotes(command);
    if(!filename.IsEmpty()) {
        // If an input file was given, append it to the command
        wxString fileName = filename;
        ::WrapWithQuotes(fileName);
        command << " " << fileName;
    }
    working_dir = ti.GetWd();
    command = MacroManager::Instance()->Expand(
        command,
        clGetManager(),
        (clGetManager()->GetWorkspace() ? clGetManager()->GetWorkspace()->GetActiveProjectName() : ""));
    working_dir = MacroManager::Instance()->Expand(
        working_dir,
        clGetManager(),
        (clGetManager()->GetWorkspace() ? clGetManager()->GetWorkspace()->GetActiveProjectName() : ""));

    wxString projectName;
    wxString configName;
    if(clCxxWorkspaceST::Get()->IsOpen()) {
        projectName = clCxxWorkspaceST::Get()->GetActiveProjectName();
        BuildConfigPtr bldConf = clCxxWorkspaceST::Get()->GetProjBuildConf(projectName, wxEmptyString);
        if(bldConf) {
            configName = bldConf->GetName();
        }
    }

    EnvSetter envGuard(clGetManager()->GetEnv(), NULL, projectName, configName);
    ::WrapInShell(command);
    
    clDEBUG() << "Running command:" << command << clEndl;
    
    int pid = wxNOT_FOUND;
    if(ti.GetCaptureOutput()) {
        IProcess* proc = ::CreateAsyncProcess(this, command, IProcessCreateConsole, working_dir);
        if(!proc) {
            ::wxMessageBox(_("Failed to launch tool\n'") + command + "'",
                           "CodeLite",
                           wxICON_ERROR | wxOK | wxCENTER,
                           EventNotifier::Get()->TopFrame());
            return;
        }

        pid = proc->GetPid();

    } else {
        pid = ::wxExecute(command, wxEXEC_ASYNC | wxEXEC_MAKE_GROUP_LEADER, new ExtToolsMyProcess());
    }
    
    if(pid > 0) {
        ExternalToolItemData item(command, pid);
        m_tools.insert(std::make_pair(pid, item));
    }
}

void ToolsTaskManager::ProcessTerminated(int pid)
{
    if(m_tools.find(pid) != m_tools.end()) {
        m_tools.erase(pid);
    }
}

void ToolsTaskManager::StopAll()
{
    std::for_each(m_tools.begin(), m_tools.end(), [&](const std::pair<int, ExternalToolItemData> &p) {
        ::wxKill(p.second.m_pid, wxSIGKILL, NULL, wxKILL_CHILDREN);
    });
}

void ToolsTaskManager::Stop(int pid)
{
    if(m_tools.find(pid) != m_tools.end()) {
        ::wxKill(pid, wxSIGKILL, NULL, wxKILL_CHILDREN);
    }
}

ExternalToolItemData::Map_t& ToolsTaskManager::GetTools()
{
#ifdef __WXOSX__
    // Check that the processes are still alive before we continue
    ExternalToolItemData::Map_t tools;
    std::for_each(m_tools.begin(), m_tools.end(), [&](const std::pair<int, ExternalToolItemData> &p) {
        if(kill(p.first, 0) == 0) {
            // alive
            tools.insert(p);
        }
    });
    m_tools.swap(tools);
#endif
    return m_tools;
}