File: clCommandProcessor.cpp

package info (click to toggle)
codelite 17.0.0%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 136,204 kB
  • sloc: cpp: 491,547; ansic: 280,393; php: 10,259; sh: 8,930; lisp: 7,664; vhdl: 6,518; python: 6,020; lex: 4,920; yacc: 3,123; perl: 2,385; javascript: 1,715; cs: 1,193; xml: 1,110; makefile: 804; cobol: 741; sql: 709; ruby: 620; f90: 566; ada: 534; asm: 464; fortran: 350; objc: 289; tcl: 258; java: 157; erlang: 61; pascal: 51; ml: 49; awk: 44; haskell: 36
file content (136 lines) | stat: -rw-r--r-- 3,808 bytes parent folder | download | duplicates (5)
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
#include "clCommandProcessor.h"
#include "cl_command_event.h"
#include "processreaderthread.h"

wxDEFINE_EVENT(wxEVT_COMMAND_PROCESSOR_ENDED, clCommandEvent);
wxDEFINE_EVENT(wxEVT_COMMAND_PROCESSOR_OUTPUT, clCommandEvent);

clCommandProcessor::clCommandProcessor(const wxString& command, const wxString& wd, size_t processFlags)
    : m_next(NULL)
    , m_prev(NULL)
    , m_process(NULL)
    , m_command(command)
    , m_workingDirectory(wd)
    , m_processFlags(processFlags)
    , m_postExecCallback(NULL)
    , m_obj(NULL)
{
    Bind(wxEVT_ASYNC_PROCESS_OUTPUT, &clCommandProcessor::OnProcessOutput, this);
    Bind(wxEVT_ASYNC_PROCESS_TERMINATED, &clCommandProcessor::OnProcessTerminated, this);
    
}

clCommandProcessor::~clCommandProcessor() { wxDELETE(m_process); }

void clCommandProcessor::ExecuteCommand()
{
    wxString message;
    message << _("Executing: ") << m_command << " [ wd: " << m_workingDirectory << " ]";

    clCommandEvent eventStart(wxEVT_COMMAND_PROCESSOR_OUTPUT);
    eventStart.SetString(message);
    GetFirst()->ProcessEvent(eventStart);
    
    m_output.Clear();
    m_process = ::CreateAsyncProcess(this, m_command, m_processFlags, m_workingDirectory);
    if(!m_process) {
        clCommandEvent eventEnd(wxEVT_COMMAND_PROCESSOR_ENDED);
        eventEnd.SetString(wxString::Format(_("Failed to execute command: %s"), m_command));
        GetFirst()->ProcessEvent(eventEnd);
        DeleteChain();
    }
    m_process->SetHardKill(true);
}

void clCommandProcessor::OnProcessOutput(clProcessEvent& event)
{
    clCommandEvent eventStart(wxEVT_COMMAND_PROCESSOR_OUTPUT);
    m_output << event.GetOutput();
    eventStart.SetString(event.GetOutput());
    GetFirst()->ProcessEvent(eventStart);
    if(eventStart.GetString() != event.GetOutput()) {
        // user provided some input, write it to the running process
        m_process->WriteToConsole(eventStart.GetString());
    }
}

void clCommandProcessor::OnProcessTerminated(clProcessEvent& event)
{
    if(m_obj && m_postExecCallback) {
        // Call the user callback, if the user returns false
        // stop the processor
        if(!(m_obj->*m_postExecCallback)(this)) {
            clCommandEvent eventEnd(wxEVT_COMMAND_PROCESSOR_ENDED);
            GetFirst()->ProcessEvent(eventEnd);
            DeleteChain();
            return;
        }
    }

    if(m_next) {
        wxDELETE(m_process);
        // more commands, don't report an 'END' event
        m_next->ExecuteCommand();

    } else {
        // no more commands to execute, delete the entire chain
        clCommandEvent eventEnd(wxEVT_COMMAND_PROCESSOR_ENDED);
        GetFirst()->ProcessEvent(eventEnd);
        DeleteChain();
    }
}

clCommandProcessor* clCommandProcessor::Link(clCommandProcessor* next)
{
    this->m_next = next;
    if(m_next) {
        m_next->m_prev = this;
    }
    return next;
}

void clCommandProcessor::DeleteChain()
{
    // Move to the first one in the list
    clCommandProcessor* first = GetFirst();

    // delete
    while(first) {
        clCommandProcessor* next = first->m_next;
        wxDELETE(first);
        first = next;
    }
}

clCommandProcessor* clCommandProcessor::GetFirst()
{
    clCommandProcessor* first = this;
    while(first->m_prev) {
        first = first->m_prev;
    }
    return first;
}

void clCommandProcessor::Terminate()
{
    clCommandProcessor* first = GetFirst();
    while(first) {
        if(first->m_process) {
            first->m_process->Terminate();
            break;
        }
        first = first->m_next;
    }
}

clCommandProcessor* clCommandProcessor::GetActiveProcess()
{
    clCommandProcessor* first = GetFirst();
    while(first) {
        if(first->m_process) {
            return first;
        }
        first = first->m_next;
    }
    return NULL;
}