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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
|
/* Distributed under the OSI-approved BSD 3-Clause License. See accompanying
file Copyright.txt or https://cmake.org/licensing for details. */
#include "cmProcess.h"
#include "cmProcessOutput.h"
cmProcess::cmProcess()
{
this->Process = nullptr;
this->Timeout = std::chrono::duration<double>::zero();
this->TotalTime = std::chrono::duration<double>::zero();
this->ExitValue = 0;
this->Id = 0;
this->StartTime = std::chrono::steady_clock::time_point();
}
cmProcess::~cmProcess()
{
cmsysProcess_Delete(this->Process);
}
void cmProcess::SetCommand(const char* command)
{
this->Command = command;
}
void cmProcess::SetCommandArguments(std::vector<std::string> const& args)
{
this->Arguments = args;
}
bool cmProcess::StartProcess()
{
if (this->Command.empty()) {
return false;
}
this->StartTime = std::chrono::steady_clock::now();
this->ProcessArgs.clear();
// put the command as arg0
this->ProcessArgs.push_back(this->Command.c_str());
// now put the command arguments in
for (std::string const& arg : this->Arguments) {
this->ProcessArgs.push_back(arg.c_str());
}
this->ProcessArgs.push_back(nullptr); // null terminate the list
this->Process = cmsysProcess_New();
cmsysProcess_SetCommand(this->Process, &*this->ProcessArgs.begin());
if (!this->WorkingDirectory.empty()) {
cmsysProcess_SetWorkingDirectory(this->Process,
this->WorkingDirectory.c_str());
}
cmsysProcess_SetTimeout(this->Process, this->Timeout.count());
cmsysProcess_SetOption(this->Process, cmsysProcess_Option_MergeOutput, 1);
cmsysProcess_Execute(this->Process);
return (cmsysProcess_GetState(this->Process) ==
cmsysProcess_State_Executing);
}
bool cmProcess::Buffer::GetLine(std::string& line)
{
// Scan for the next newline.
for (size_type sz = this->size(); this->Last != sz; ++this->Last) {
if ((*this)[this->Last] == '\n' || (*this)[this->Last] == '\0') {
// Extract the range first..last as a line.
const char* text = &*this->begin() + this->First;
size_type length = this->Last - this->First;
while (length && text[length - 1] == '\r') {
length--;
}
line.assign(text, length);
// Start a new range for the next line.
++this->Last;
this->First = Last;
// Return the line extracted.
return true;
}
}
// Available data have been exhausted without a newline.
if (this->First != 0) {
// Move the partial line to the beginning of the buffer.
this->erase(this->begin(), this->begin() + this->First);
this->First = 0;
this->Last = this->size();
}
return false;
}
bool cmProcess::Buffer::GetLast(std::string& line)
{
// Return the partial last line, if any.
if (!this->empty()) {
line.assign(&*this->begin(), this->size());
this->First = this->Last = 0;
this->clear();
return true;
}
return false;
}
int cmProcess::GetNextOutputLine(std::string& line,
std::chrono::duration<double> timeout)
{
cmProcessOutput processOutput(cmProcessOutput::UTF8);
std::string strdata;
double waitTimeout = timeout.count();
for (;;) {
// Look for lines already buffered.
if (this->Output.GetLine(line)) {
return cmsysProcess_Pipe_STDOUT;
}
// Check for more data from the process.
char* data;
int length;
int p =
cmsysProcess_WaitForData(this->Process, &data, &length, &waitTimeout);
if (p == cmsysProcess_Pipe_Timeout) {
return cmsysProcess_Pipe_Timeout;
}
if (p == cmsysProcess_Pipe_STDOUT) {
processOutput.DecodeText(data, length, strdata);
this->Output.insert(this->Output.end(), strdata.begin(), strdata.end());
} else { // p == cmsysProcess_Pipe_None
// The process will provide no more data.
break;
}
}
processOutput.DecodeText(std::string(), strdata);
if (!strdata.empty()) {
this->Output.insert(this->Output.end(), strdata.begin(), strdata.end());
}
// Look for partial last lines.
if (this->Output.GetLast(line)) {
return cmsysProcess_Pipe_STDOUT;
}
// No more data. Wait for process exit.
if (!cmsysProcess_WaitForExit(this->Process, &waitTimeout)) {
return cmsysProcess_Pipe_Timeout;
}
// Record exit information.
this->ExitValue = cmsysProcess_GetExitValue(this->Process);
this->TotalTime = std::chrono::steady_clock::now() - this->StartTime;
// Because of a processor clock scew the runtime may become slightly
// negative. If someone changed the system clock while the process was
// running this may be even more. Make sure not to report a negative
// duration here.
if (this->TotalTime <= std::chrono::duration<double>::zero()) {
this->TotalTime = std::chrono::duration<double>::zero();
}
// std::cerr << "Time to run: " << this->TotalTime << "\n";
return cmsysProcess_Pipe_None;
}
cmProcess::State cmProcess::GetProcessStatus()
{
if (this->Process) {
switch (cmsysProcess_GetState(this->Process)) {
case cmsysProcess_State_Starting:
return State::Starting;
case cmsysProcess_State_Error:
return State::Error;
case cmsysProcess_State_Exception:
return State::Exception;
case cmsysProcess_State_Executing:
return State::Executing;
case cmsysProcess_State_Expired:
return State::Expired;
case cmsysProcess_State_Killed:
return State::Killed;
case cmsysProcess_State_Disowned:
return State::Disowned;
default: // case cmsysProcess_State_Exited:
break;
}
}
return State::Exited;
}
void cmProcess::ChangeTimeout(std::chrono::duration<double> t)
{
this->Timeout = t;
cmsysProcess_SetTimeout(this->Process, this->Timeout.count());
}
void cmProcess::ResetStartTime()
{
cmsysProcess_ResetStartTime(this->Process);
this->StartTime = std::chrono::steady_clock::now();
}
int cmProcess::GetExitException()
{
return cmsysProcess_GetExitException(this->Process);
}
std::string cmProcess::GetExitExceptionString()
{
return cmsysProcess_GetExceptionString(this->Process);
}
|