fix clang format and clear flag for read process out\err
This commit is contained in:
1
.gitignore
vendored
1
.gitignore
vendored
@@ -5,3 +5,4 @@ _unsused
|
|||||||
CMakeLists.txt.user*
|
CMakeLists.txt.user*
|
||||||
/include
|
/include
|
||||||
/release
|
/release
|
||||||
|
/build*
|
||||||
|
|||||||
@@ -171,10 +171,7 @@ void PIProcess::startProc(bool detached) {
|
|||||||
if (g_in) PRIVATE->tf_in = freopen(f_in.path().data(), "r", stdin);
|
if (g_in) PRIVATE->tf_in = freopen(f_in.path().data(), "r", stdin);
|
||||||
if (g_out) PRIVATE->tf_out = freopen(f_out.path().data(), "w", stdout);
|
if (g_out) PRIVATE->tf_out = freopen(f_out.path().data(), "w", stdout);
|
||||||
if (g_err) PRIVATE->tf_err = freopen(f_err.path().data(), "w", stderr);
|
if (g_err) PRIVATE->tf_err = freopen(f_err.path().data(), "w", stderr);
|
||||||
# ifndef WINDOWS
|
|
||||||
if (!wd.isEmpty())
|
|
||||||
if (!chdir(wd.data())) piCoutObj << "Error while set working directory";
|
|
||||||
# endif
|
|
||||||
# ifdef WINDOWS
|
# ifdef WINDOWS
|
||||||
GetStartupInfoA(&(PRIVATE->si));
|
GetStartupInfoA(&(PRIVATE->si));
|
||||||
piZeroMemory(PRIVATE->pi);
|
piZeroMemory(PRIVATE->pi);
|
||||||
@@ -196,128 +193,150 @@ void PIProcess::startProc(bool detached) {
|
|||||||
}
|
}
|
||||||
CloseHandle(PRIVATE->pi.hThread);
|
CloseHandle(PRIVATE->pi.hThread);
|
||||||
CloseHandle(PRIVATE->pi.hProcess);
|
CloseHandle(PRIVATE->pi.hProcess);
|
||||||
} else
|
} else {
|
||||||
piCoutObj << "\"CreateProcess\" error: %1"_tr("PIProcess").arg(errorString());
|
piCoutObj << "\"CreateProcess\" error: %1"_tr("PIProcess").arg(errorString());
|
||||||
# else
|
}
|
||||||
|
|
||||||
// cout << "exec " << tf_in << ", " << tf_out << ", " << tf_err << endl;
|
|
||||||
if (execve(str.data(), (char * const *)argscc, (char * const *)envcc) < 0)
|
|
||||||
piCoutObj << "\"execve" << str << args << "\" error :" << errorString();
|
|
||||||
}
|
|
||||||
else {
|
|
||||||
piMinSleep();
|
|
||||||
// cout << "wait" << endl;
|
|
||||||
if (!detached) {
|
|
||||||
wait(&exit_code);
|
|
||||||
pid_ = 0;
|
|
||||||
if (!detached) PRIVATE->pid = pid_;
|
|
||||||
// cout << "wait done" << endl;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
# endif
|
|
||||||
if (!detached) execFinished(str, exit_code);
|
|
||||||
is_exec = false;
|
|
||||||
# ifdef WINDOWS
|
|
||||||
delete[] a;
|
|
||||||
# endif
|
# endif
|
||||||
|
# ifndef WINDOWS
|
||||||
|
if (!wd.isEmpty()) {
|
||||||
|
if (!chdir(wd.data())) piCoutObj << "Error while set working directory";
|
||||||
|
}
|
||||||
|
// cout << "exec " << tf_in << ", " << tf_out << ", " << tf_err << endl;
|
||||||
|
if (execve(str.data(), (char * const *)argscc, (char * const *)envcc) < 0) {
|
||||||
|
piCoutObj << "\"execve" << str << args << "\" error :" << errorString();
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
piMinSleep();
|
||||||
|
// cout << "wait" << endl;
|
||||||
|
if (!detached) {
|
||||||
|
wait(&exit_code);
|
||||||
|
pid_ = 0;
|
||||||
|
if (!detached) PRIVATE->pid = pid_;
|
||||||
|
// cout << "wait done" << endl;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
# endif
|
||||||
|
if (!detached) execFinished(str, exit_code);
|
||||||
void PIProcess::terminate() {
|
is_exec = false;
|
||||||
# ifdef WINDOWS
|
# ifdef WINDOWS
|
||||||
if (is_exec)
|
delete[] a;
|
||||||
if (!TerminateProcess(PRIVATE->pi.hProcess, 0)) return;
|
# endif
|
||||||
PRIVATE->pi.dwProcessId = 0;
|
}
|
||||||
|
|
||||||
|
PIByteArray PIProcess::readFile(PIFile & f, bool clear)
|
||||||
|
{
|
||||||
|
f.open(PIIODevice::ReadOnly);
|
||||||
|
const auto ret = f.readAll();
|
||||||
|
if (clear) {
|
||||||
|
f.clear();
|
||||||
|
}
|
||||||
|
return ret;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
void PIProcess::terminate() {
|
||||||
|
# ifdef WINDOWS
|
||||||
|
if (is_exec)
|
||||||
|
if (!TerminateProcess(PRIVATE->pi.hProcess, 0)) return;
|
||||||
|
PRIVATE->pi.dwProcessId = 0;
|
||||||
# else
|
# else
|
||||||
if (is_exec) kill(PRIVATE->pid, SIGKILL);
|
if (is_exec) kill(PRIVATE->pid, SIGKILL);
|
||||||
PRIVATE->pid = 0;
|
PRIVATE->pid = 0;
|
||||||
# endif
|
# endif
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
bool PIProcess::waitForFinish() {
|
bool PIProcess::waitForFinish() {
|
||||||
return PIThread::waitForFinish(1_m);
|
return PIThread::waitForFinish(1_m);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
void PIProcess::execIndependent(const PIString & program, const PIStringList & args_) {
|
void PIProcess::execIndependent(const PIString & program, const PIStringList & args_) {
|
||||||
PIProcess p;
|
PIProcess p;
|
||||||
p.args << program << args_;
|
p.args << program << args_;
|
||||||
p.startProc(true);
|
p.startProc(true);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
int PIProcess::pID() const {
|
int PIProcess::pID() const {
|
||||||
# ifdef WINDOWS
|
# ifdef WINDOWS
|
||||||
return PRIVATE->pi.dwProcessId;
|
return PRIVATE->pi.dwProcessId;
|
||||||
# else
|
# else
|
||||||
return PRIVATE->pid;
|
return PRIVATE->pid;
|
||||||
# endif
|
# endif
|
||||||
}
|
}
|
||||||
|
|
||||||
|
PIByteArray PIProcess::readOutput(bool clear) {
|
||||||
|
return readFile(f_out, clear);
|
||||||
|
}
|
||||||
|
|
||||||
|
PIByteArray PIProcess::readError(bool clear) {
|
||||||
|
return readFile(f_err, clear);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
int PIProcess::currentPID() {
|
int PIProcess::currentPID() {
|
||||||
# ifdef WINDOWS
|
# ifdef WINDOWS
|
||||||
return GetCurrentProcessId();
|
return GetCurrentProcessId();
|
||||||
# else
|
# else
|
||||||
return getpid();
|
return getpid();
|
||||||
# endif
|
# endif
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
PIStringList PIProcess::currentEnvironment() {
|
||||||
|
PIStringList l;
|
||||||
|
int i = 0;
|
||||||
|
while (environ[i] != 0) {
|
||||||
|
l << environ[i];
|
||||||
|
++i;
|
||||||
}
|
}
|
||||||
|
return l;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
PIStringList PIProcess::currentEnvironment() {
|
void PIProcess::run() {
|
||||||
PIStringList l;
|
startProc(false);
|
||||||
int i = 0;
|
}
|
||||||
while (environ[i] != 0) {
|
|
||||||
l << environ[i];
|
|
||||||
++i;
|
|
||||||
}
|
|
||||||
return l;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
void PIProcess::run() {
|
void PIProcess::removeEnvironmentVariable(const PIString & variable) {
|
||||||
startProc(false);
|
PIString s;
|
||||||
}
|
for (int i = 0; i < env.size_s(); ++i) {
|
||||||
|
s = env[i];
|
||||||
|
if (s.left(s.find("=")).trimmed() == variable) {
|
||||||
void PIProcess::removeEnvironmentVariable(const PIString & variable) {
|
env.remove(i);
|
||||||
PIString s;
|
--i;
|
||||||
for (int i = 0; i < env.size_s(); ++i) {
|
|
||||||
s = env[i];
|
|
||||||
if (s.left(s.find("=")).trimmed() == variable) {
|
|
||||||
env.remove(i);
|
|
||||||
--i;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
void PIProcess::setEnvironmentVariable(const PIString & variable, const PIString & value) {
|
void PIProcess::setEnvironmentVariable(const PIString & variable, const PIString & value) {
|
||||||
PIString s, v;
|
PIString s, v;
|
||||||
for (int i = 0; i < env.size_s(); ++i) {
|
for (int i = 0; i < env.size_s(); ++i) {
|
||||||
s = env[i];
|
s = env[i];
|
||||||
v = s.left(s.find("=")).trimmed();
|
v = s.left(s.find("=")).trimmed();
|
||||||
if (v == variable) {
|
if (v == variable) {
|
||||||
env[i] = v + "=" + value;
|
env[i] = v + "=" + value;
|
||||||
return;
|
return;
|
||||||
}
|
|
||||||
}
|
}
|
||||||
env << variable + "=" + value;
|
|
||||||
}
|
}
|
||||||
|
env << variable + "=" + value;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
PIString PIProcess::getEnvironmentVariable(const PIString & variable) {
|
PIString PIProcess::getEnvironmentVariable(const PIString & variable) {
|
||||||
PIStringList env_ = currentEnvironment();
|
PIStringList env_ = currentEnvironment();
|
||||||
PIString s, v;
|
PIString s, v;
|
||||||
for (int i = 0; i < env_.size_s(); ++i) {
|
for (int i = 0; i < env_.size_s(); ++i) {
|
||||||
s = env_[i];
|
s = env_[i];
|
||||||
v = s.left(s.find("=")).trimmed();
|
v = s.left(s.find("=")).trimmed();
|
||||||
if (v == variable) {
|
if (v == variable) {
|
||||||
return s.right(s.size() - 1 - s.find("=")).trimmed();
|
return s.right(s.size() - 1 - s.find("=")).trimmed();
|
||||||
}
|
|
||||||
}
|
}
|
||||||
return PIString();
|
|
||||||
}
|
}
|
||||||
|
return PIString();
|
||||||
|
}
|
||||||
|
|
||||||
#endif // MICRO_PIP
|
#endif // MICRO_PIP
|
||||||
|
|||||||
@@ -99,18 +99,11 @@ public:
|
|||||||
|
|
||||||
//! \~english Returns all attached execution output stream
|
//! \~english Returns all attached execution output stream
|
||||||
//! \~russian
|
//! \~russian
|
||||||
PIByteArray readOutput() {
|
PIByteArray readOutput(bool clear = false);
|
||||||
f_out.open(PIIODevice::ReadOnly);
|
|
||||||
return f_out.readAll();
|
|
||||||
}
|
|
||||||
|
|
||||||
//! \~english Returns all attached execution error stream
|
//! \~english Returns all attached execution error stream
|
||||||
//! \~russian
|
//! \~russian
|
||||||
PIByteArray readError() {
|
PIByteArray readError(bool clear = false);
|
||||||
f_err.open(PIIODevice::ReadOnly);
|
|
||||||
return f_err.readAll();
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
//! \~english Returns current attached execution environment
|
//! \~english Returns current attached execution environment
|
||||||
//! \~russian
|
//! \~russian
|
||||||
@@ -227,6 +220,7 @@ private:
|
|||||||
void run() override;
|
void run() override;
|
||||||
void exec_();
|
void exec_();
|
||||||
void startProc(bool detached);
|
void startProc(bool detached);
|
||||||
|
PIByteArray readFile(PIFile & f, bool clear);
|
||||||
|
|
||||||
PRIVATE_DECLARATION(PIP_EXPORT)
|
PRIVATE_DECLARATION(PIP_EXPORT)
|
||||||
PIStringList args, env;
|
PIStringList args, env;
|
||||||
|
|||||||
Reference in New Issue
Block a user