2016-09-27 22:01:08 +03: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 18:40:18 +03:00
|
|
|
#include "cmProcessTools.h"
|
|
|
|
|
|
|
|
#include <cmsys/Process.h>
|
2016-08-24 01:29:15 +03:00
|
|
|
#include <ostream>
|
2009-02-24 18:40:18 +03:00
|
|
|
|
2016-05-16 17:34:04 +03:00
|
|
|
void cmProcessTools::RunProcess(struct cmsysProcess_s* cp, OutputParser* out,
|
|
|
|
OutputParser* err)
|
2009-02-24 18:40:18 +03:00
|
|
|
{
|
|
|
|
cmsysProcess_Execute(cp);
|
2016-06-27 23:44:16 +03:00
|
|
|
char* data = CM_NULLPTR;
|
2009-02-24 18:40:18 +03:00
|
|
|
int length = 0;
|
|
|
|
int p;
|
2016-05-16 17:34:04 +03:00
|
|
|
while ((out || err) &&
|
2016-06-27 23:44:16 +03:00
|
|
|
(p = cmsysProcess_WaitForData(cp, &data, &length, CM_NULLPTR), p)) {
|
2016-05-16 17:34:04 +03:00
|
|
|
if (out && p == cmsysProcess_Pipe_STDOUT) {
|
|
|
|
if (!out->Process(data, length)) {
|
2016-06-27 23:44:16 +03:00
|
|
|
out = CM_NULLPTR;
|
2009-02-24 18:40:18 +03:00
|
|
|
}
|
2016-05-16 17:34:04 +03:00
|
|
|
} else if (err && p == cmsysProcess_Pipe_STDERR) {
|
|
|
|
if (!err->Process(data, length)) {
|
2016-06-27 23:44:16 +03:00
|
|
|
err = CM_NULLPTR;
|
2009-02-24 18:40:18 +03:00
|
|
|
}
|
|
|
|
}
|
2016-05-16 17:34:04 +03:00
|
|
|
}
|
2016-06-27 23:44:16 +03:00
|
|
|
cmsysProcess_WaitForExit(cp, CM_NULLPTR);
|
2009-02-24 18:40:18 +03:00
|
|
|
}
|
|
|
|
|
2016-05-16 17:34:04 +03:00
|
|
|
cmProcessTools::LineParser::LineParser(char sep, bool ignoreCR)
|
2016-06-27 23:44:16 +03:00
|
|
|
: Log(CM_NULLPTR)
|
|
|
|
, Prefix(CM_NULLPTR)
|
2016-05-16 17:34:04 +03:00
|
|
|
, Separator(sep)
|
|
|
|
, LineEnd('\0')
|
|
|
|
, IgnoreCR(ignoreCR)
|
2009-02-24 18:40:18 +03:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
void cmProcessTools::LineParser::SetLog(std::ostream* log, const char* prefix)
|
|
|
|
{
|
|
|
|
this->Log = log;
|
2016-05-16 17:34:04 +03:00
|
|
|
this->Prefix = prefix ? prefix : "";
|
2009-02-24 18:40:18 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
bool cmProcessTools::LineParser::ProcessChunk(const char* first, int length)
|
|
|
|
{
|
|
|
|
const char* last = first + length;
|
2016-05-16 17:34:04 +03:00
|
|
|
for (const char* c = first; c != last; ++c) {
|
|
|
|
if (*c == this->Separator || *c == '\0') {
|
2010-06-23 17:14:43 +04:00
|
|
|
this->LineEnd = *c;
|
|
|
|
|
2009-02-24 18:40:18 +03:00
|
|
|
// Log this line.
|
2016-05-16 17:34:04 +03:00
|
|
|
if (this->Log && this->Prefix) {
|
2009-02-24 18:40:18 +03:00
|
|
|
*this->Log << this->Prefix << this->Line << "\n";
|
2016-05-16 17:34:04 +03:00
|
|
|
}
|
2009-02-24 18:40:18 +03:00
|
|
|
|
|
|
|
// Hand this line to the subclass implementation.
|
2016-05-16 17:34:04 +03:00
|
|
|
if (!this->ProcessLine()) {
|
2009-02-24 18:40:18 +03:00
|
|
|
this->Line = "";
|
|
|
|
return false;
|
2016-05-16 17:34:04 +03:00
|
|
|
}
|
2009-02-24 18:40:18 +03:00
|
|
|
|
|
|
|
this->Line = "";
|
2016-05-16 17:34:04 +03:00
|
|
|
} else if (*c != '\r' || !this->IgnoreCR) {
|
2009-02-24 18:40:18 +03:00
|
|
|
// Append this character to the line under construction.
|
|
|
|
this->Line.append(1, *c);
|
|
|
|
}
|
2016-05-16 17:34:04 +03:00
|
|
|
}
|
2009-02-24 18:40:18 +03:00
|
|
|
return true;
|
|
|
|
}
|