2016-09-27 15:01:08 -04:00
|
|
|
/* Distributed under the OSI-approved BSD 3-Clause License. See accompanying
|
|
|
|
file Copyright.txt or https://cmake.org/licensing for details. */
|
2009-02-24 10:40:18 -05:00
|
|
|
#include "cmProcessTools.h"
|
2016-11-01 20:29:17 +02:00
|
|
|
#include "cmProcessOutput.h"
|
2009-02-24 10:40:18 -05:00
|
|
|
|
2017-04-11 22:00:21 +02:00
|
|
|
#include "cmsys/Process.h"
|
2016-08-24 00:29:15 +02:00
|
|
|
#include <ostream>
|
2009-02-24 10:40:18 -05:00
|
|
|
|
2016-05-16 10:34:04 -04:00
|
|
|
void cmProcessTools::RunProcess(struct cmsysProcess_s* cp, OutputParser* out,
|
2016-11-01 20:36:58 +02:00
|
|
|
OutputParser* err, Encoding encoding)
|
2009-02-24 10:40:18 -05:00
|
|
|
{
|
|
|
|
cmsysProcess_Execute(cp);
|
2017-08-22 23:42:36 +02:00
|
|
|
char* data = nullptr;
|
2009-02-24 10:40:18 -05:00
|
|
|
int length = 0;
|
|
|
|
int p;
|
2016-11-01 20:36:58 +02:00
|
|
|
cmProcessOutput processOutput(encoding);
|
2016-11-01 20:29:17 +02:00
|
|
|
std::string strdata;
|
2016-05-16 10:34:04 -04:00
|
|
|
while ((out || err) &&
|
2019-01-11 20:48:19 -05:00
|
|
|
(p = cmsysProcess_WaitForData(cp, &data, &length, nullptr))) {
|
2016-05-16 10:34:04 -04:00
|
|
|
if (out && p == cmsysProcess_Pipe_STDOUT) {
|
2016-11-01 20:29:17 +02:00
|
|
|
processOutput.DecodeText(data, length, strdata, 1);
|
|
|
|
if (!out->Process(strdata.c_str(), int(strdata.size()))) {
|
2017-08-22 23:42:36 +02:00
|
|
|
out = nullptr;
|
2009-02-24 10:40:18 -05:00
|
|
|
}
|
2016-05-16 10:34:04 -04:00
|
|
|
} else if (err && p == cmsysProcess_Pipe_STDERR) {
|
2016-11-01 20:29:17 +02:00
|
|
|
processOutput.DecodeText(data, length, strdata, 2);
|
|
|
|
if (!err->Process(strdata.c_str(), int(strdata.size()))) {
|
2017-08-22 23:42:36 +02:00
|
|
|
err = nullptr;
|
2009-02-24 10:40:18 -05:00
|
|
|
}
|
|
|
|
}
|
2016-05-16 10:34:04 -04:00
|
|
|
}
|
2016-11-01 20:29:17 +02:00
|
|
|
if (out) {
|
|
|
|
processOutput.DecodeText(std::string(), strdata, 1);
|
|
|
|
if (!strdata.empty()) {
|
|
|
|
out->Process(strdata.c_str(), int(strdata.size()));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (err) {
|
|
|
|
processOutput.DecodeText(std::string(), strdata, 2);
|
|
|
|
if (!strdata.empty()) {
|
2017-10-05 14:29:41 +02:00
|
|
|
err->Process(strdata.c_str(), int(strdata.size()));
|
2016-11-01 20:29:17 +02:00
|
|
|
}
|
|
|
|
}
|
2017-08-22 23:42:36 +02:00
|
|
|
cmsysProcess_WaitForExit(cp, nullptr);
|
2009-02-24 10:40:18 -05:00
|
|
|
}
|
|
|
|
|
2016-05-16 10:34:04 -04:00
|
|
|
cmProcessTools::LineParser::LineParser(char sep, bool ignoreCR)
|
2018-11-21 23:17:54 +01:00
|
|
|
: Separator(sep)
|
2016-05-16 10:34:04 -04:00
|
|
|
, IgnoreCR(ignoreCR)
|
2009-02-24 10:40:18 -05:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
void cmProcessTools::LineParser::SetLog(std::ostream* log, const char* prefix)
|
|
|
|
{
|
|
|
|
this->Log = log;
|
2016-05-16 10:34:04 -04:00
|
|
|
this->Prefix = prefix ? prefix : "";
|
2009-02-24 10:40:18 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
bool cmProcessTools::LineParser::ProcessChunk(const char* first, int length)
|
|
|
|
{
|
|
|
|
const char* last = first + length;
|
2016-05-16 10:34:04 -04:00
|
|
|
for (const char* c = first; c != last; ++c) {
|
|
|
|
if (*c == this->Separator || *c == '\0') {
|
2010-06-23 09:14:43 -04:00
|
|
|
this->LineEnd = *c;
|
|
|
|
|
2009-02-24 10:40:18 -05:00
|
|
|
// Log this line.
|
2016-05-16 10:34:04 -04:00
|
|
|
if (this->Log && this->Prefix) {
|
2009-02-24 10:40:18 -05:00
|
|
|
*this->Log << this->Prefix << this->Line << "\n";
|
2016-05-16 10:34:04 -04:00
|
|
|
}
|
2009-02-24 10:40:18 -05:00
|
|
|
|
|
|
|
// Hand this line to the subclass implementation.
|
2016-05-16 10:34:04 -04:00
|
|
|
if (!this->ProcessLine()) {
|
2017-09-16 02:26:49 +03:00
|
|
|
this->Line.clear();
|
2009-02-24 10:40:18 -05:00
|
|
|
return false;
|
2016-05-16 10:34:04 -04:00
|
|
|
}
|
2009-02-24 10:40:18 -05:00
|
|
|
|
2017-09-16 02:26:49 +03:00
|
|
|
this->Line.clear();
|
2016-05-16 10:34:04 -04:00
|
|
|
} else if (*c != '\r' || !this->IgnoreCR) {
|
2009-02-24 10:40:18 -05:00
|
|
|
// Append this character to the line under construction.
|
|
|
|
this->Line.append(1, *c);
|
|
|
|
}
|
2016-05-16 10:34:04 -04:00
|
|
|
}
|
2009-02-24 10:40:18 -05:00
|
|
|
return true;
|
|
|
|
}
|