219 lines
6.3 KiB
C++
219 lines
6.3 KiB
C++
/*! \file piprocess.h
|
|
* \ingroup System
|
|
* \~\brief
|
|
* \~english External process
|
|
* \~russian Внешний процесс
|
|
*/
|
|
/*
|
|
PIP - Platform Independent Primitives
|
|
Process
|
|
Ivan Pelipenko peri4ko@yandex.ru
|
|
|
|
This program is free software: you can redistribute it and/or modify
|
|
it under the terms of the GNU Lesser General Public License as published by
|
|
the Free Software Foundation, either version 3 of the License, or
|
|
(at your option) any later version.
|
|
|
|
This program is distributed in the hope that it will be useful,
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
GNU Lesser General Public License for more details.
|
|
|
|
You should have received a copy of the GNU Lesser General Public License
|
|
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
*/
|
|
|
|
#ifndef PIPROCESS_H
|
|
#define PIPROCESS_H
|
|
|
|
#ifndef MICRO_PIP
|
|
|
|
# include "pithread.h"
|
|
|
|
|
|
//! \ingroup System
|
|
//! \~\brief
|
|
//! \~english External process.
|
|
//! \~russian Внешний процесс.
|
|
class PIP_EXPORT PIProcess: public PIThread {
|
|
PIOBJECT_SUBCLASS(PIProcess, PIThread);
|
|
|
|
public:
|
|
//! \~english Construct empty %PIProcess
|
|
//! \~russian Создает пустой %PIProcess
|
|
PIProcess();
|
|
|
|
virtual ~PIProcess();
|
|
|
|
|
|
//! \~english Returns last attached execution exit code
|
|
//! \~russian Возвращает код завершения последнего выполнения
|
|
int exitCode() const { return exit_code; }
|
|
|
|
//! \~english Returns current attached execution process ID
|
|
//! \~russian Возвращает ID процесса текущего выполнения
|
|
int pID() const;
|
|
|
|
//! \~english Returns current attached execution working directory or empty string if it wasn`t set
|
|
//! \~russian
|
|
PIString workingDirectory() const { return wd; }
|
|
|
|
//! \~english Set attached execution working directory
|
|
//! \~russian
|
|
void setWorkingDirectory(const PIString & path) { wd = path; }
|
|
|
|
//! \~english Rseet attached execution working directory, application working dir will be used
|
|
//! \~russian
|
|
void resetWorkingDirectory() { wd.clear(); }
|
|
|
|
//! \~english Returns all attached execution output stream
|
|
//! \~russian
|
|
PIByteArray readOutput();
|
|
|
|
//! \~english Returns all attached execution error stream
|
|
//! \~russian
|
|
PIByteArray readError();
|
|
|
|
//! \~english Write data to attached execution input stream
|
|
//! \~russian
|
|
bool writeInput(const PIByteArray & data);
|
|
|
|
//! \~english Close attached execution input stream and send EOF
|
|
//! \~russian
|
|
void closeInput();
|
|
|
|
void enableWriteStdIn(bool on = true);
|
|
void enableReadStdOut(bool on = true);
|
|
void enableReadStdErr(bool on = true);
|
|
|
|
//! \~english Returns current attached execution environment
|
|
//! \~russian
|
|
PIStringList environment() { return env; }
|
|
|
|
//! \~english Clear current attached execution environment. Call before \a exec()
|
|
//! \~russian
|
|
void clearEnvironment() { env.clear(); }
|
|
|
|
//! \~english Remove variable "variable" from current attached execution environment. Call before \a exec()
|
|
//! \~russian
|
|
void removeEnvironmentVariable(const PIString & variable);
|
|
|
|
//! \~english Set variable "variable" to "value" in current attached execution environment. Call before \a exec()
|
|
//! \~russian
|
|
void setEnvironmentVariable(const PIString & variable, const PIString & value);
|
|
|
|
//! \~english Start attached execution "program" with one argument "arg"
|
|
//! \~russian
|
|
void exec(const PIString & program, const PIString & arg) {
|
|
args.clear();
|
|
args << program << arg;
|
|
exec_();
|
|
}
|
|
|
|
EVENT_HANDLER1(void, exec, const PIString &, program) {
|
|
args.clear();
|
|
args << program;
|
|
exec_();
|
|
}
|
|
EVENT_HANDLER2(void, exec, const PIString &, program, const PIStringList &, args_) {
|
|
args.clear();
|
|
args << program << args_;
|
|
exec_();
|
|
}
|
|
EVENT_HANDLER(void, terminate);
|
|
EVENT_HANDLER(bool, waitForFinish) { return PIThread::waitForFinish(); }
|
|
EVENT_HANDLER1(bool, waitForFinish, PISystemTime, timeout) { return PIThread::waitForFinish(timeout); }
|
|
|
|
EVENT1(execStarted, PIString, program);
|
|
EVENT2(execFinished, PIString, program, int, exit_code);
|
|
|
|
bool isExecFinished() const {return exec_finished;}
|
|
bool isExecStarted() const {return exec_start;}
|
|
|
|
|
|
//! \~english Start detached execution "program" without arguments
|
|
//! \~russian
|
|
static void execIndependent(const PIString & program) { execIndependent(program, PIStringList()); }
|
|
|
|
//! \~english Start detached execution "program" with one argument "arg"
|
|
//! \~russian
|
|
static void execIndependent(const PIString & program, const PIString & arg) { execIndependent(program, PIStringList() << arg); }
|
|
|
|
//! \~english Start detached execution "program" with arguments "args"
|
|
//! \~russian
|
|
static void execIndependent(const PIString & program, const PIStringList & args);
|
|
|
|
|
|
//! \~english Returns application environment
|
|
//! \~russian
|
|
static PIStringList currentEnvironment();
|
|
|
|
//! \~english Returns application process ID
|
|
//! \~russian
|
|
static int currentPID();
|
|
|
|
//! \~english Returns variable "variable" value from application environment
|
|
//! \~russian
|
|
static PIString getEnvironmentVariable(const PIString & variable);
|
|
|
|
//! \handlers
|
|
//! \{
|
|
|
|
//! \fn void exec(const PIString & program)
|
|
//! \brief
|
|
//! \~english Start attached execution "program" without arguments
|
|
//! \~russian
|
|
|
|
//! \fn void exec(const PIString & program, const PIStringList & args)
|
|
//! \brief
|
|
//! \~english Start attached execution "program" with arguments "args"
|
|
//! \~russian
|
|
|
|
//! \fn void terminate()
|
|
//! \brief
|
|
//! \~english Immediately terminate attached execution
|
|
//! \~russian
|
|
|
|
//! \fn bool waitForFinish()
|
|
//! \brief
|
|
//! \~english Wait for attached execution finish maximum for 60 seconds
|
|
//! \~russian
|
|
|
|
//! \fn bool waitForFinish(PISystemTime timeout)
|
|
//! \brief
|
|
//! \~english Wait for attached execution finish maximum for "timeout_"
|
|
//! \~russian
|
|
|
|
//! \}
|
|
//! \events
|
|
//! \{
|
|
|
|
//! \fn void execStarted(PIString program)
|
|
//! \brief
|
|
//! \~english Raise on attached execution start
|
|
//! \~russian
|
|
|
|
//! \fn void execFinished(PIString program)
|
|
//! \brief
|
|
//! \~english Raise on attached execution finish
|
|
//! \~russian
|
|
|
|
//! \}
|
|
|
|
private:
|
|
void run() override;
|
|
void exec_();
|
|
void startProc(bool detached);
|
|
|
|
private:
|
|
PRIVATE_DECLARATION(PIP_EXPORT)
|
|
PIStringList args, env;
|
|
PIString wd;
|
|
int exit_code;
|
|
std::atomic_bool exec_start;
|
|
std::atomic_bool exec_finished;
|
|
};
|
|
|
|
#endif // MICRO_PIP
|
|
#endif // PIPROCESS_H
|