78 lines
2.7 KiB
C++
78 lines
2.7 KiB
C++
#ifndef PIPROCESS_H
|
|
#define PIPROCESS_H
|
|
|
|
#include "pithread.h"
|
|
#include "pifile.h"
|
|
#ifdef WINDOWS
|
|
//# include <.h>
|
|
#else
|
|
# include <sys/wait.h>
|
|
#endif
|
|
|
|
class PIProcess: private PIThread
|
|
{
|
|
public:
|
|
PIProcess();
|
|
~PIProcess();
|
|
|
|
int exitCode() const {return exit_code;}
|
|
#ifdef WINDOWS
|
|
int pID() const {return pi.dwProcessId;}
|
|
#else
|
|
int pID() const {return pid;}
|
|
#endif
|
|
|
|
void setGrabInput(bool yes) {g_in = yes;}
|
|
void setGrabOutput(bool yes) {g_out = yes;}
|
|
void setGrabError(bool yes) {g_err = yes;}
|
|
void setInputFile(const PIString & path) {f_in.setPath(path);}
|
|
void setOutputFile(const PIString & path) {f_out.setPath(path);}
|
|
void setErrorFile(const PIString & path) {f_err.setPath(path);}
|
|
void unsetInputFile() {f_in.setPath("");}
|
|
void unsetOutputFile() {f_out.setPath("");}
|
|
void unsetErrorFile() {f_err.setPath("");}
|
|
void setWorkingDirectory(const PIString & path) {wd = path;}
|
|
void resetWorkingDirectory() {wd.clear();}
|
|
void exec(const PIString & program) {args.clear(); args << program; exec_();}
|
|
void exec(const PIString & program, const PIString & arg) {args.clear(); args << program << arg; exec_();}
|
|
void exec(const PIString & program, const PIString & arg1, const PIString & arg2) {args.clear(); args << program << arg1 << arg2; exec_();}
|
|
void exec(const PIString & program, const PIString & arg1, const PIString & arg2, const PIString & arg3) {args.clear(); args << program << arg1 << arg2 << arg3; exec_();}
|
|
void exec(const PIString & program, const PIStringList & args_) {args = args_; exec_();}
|
|
#ifdef WINDOWS
|
|
void terminate() {if (is_exec) if (!TerminateProcess(pi.hProcess, 0)) return; pi.dwProcessId = 0;}
|
|
#else
|
|
void terminate() {if (is_exec) kill(pid, SIGKILL); pid = 0;}
|
|
#endif
|
|
bool waitForFinish(int timeout_msecs = 60000) {return PIThread::waitForFinish(timeout_msecs);}
|
|
PIByteArray readOutput() {f_out.open(PIFile::Read); return f_out.readAll();}
|
|
PIByteArray readError() {f_err.open(PIFile::Read); return f_err.readAll();}
|
|
|
|
PIStringList environment() {return env;}
|
|
void clearEnvironment() {env.clear();}
|
|
void removeEnvironmentVariable(const PIString & variable);
|
|
void setEnvironmentVariable(const PIString & variable, const PIString & value);
|
|
|
|
static PIStringList currentEnvironment() {PIStringList l; int i = 0; while (environ[i] != 0) {l << environ[i]; ++i;} return l;}
|
|
|
|
private:
|
|
virtual void run();
|
|
void exec_();
|
|
|
|
PIStringList args, env;
|
|
PIString wd;
|
|
PIByteArray out;
|
|
PIFile f_in, f_out, f_err;
|
|
bool g_in, g_out, g_err, t_in, t_out, t_err;
|
|
#ifdef WINDOWS
|
|
STARTUPINFOA si;
|
|
PROCESS_INFORMATION pi;
|
|
#else
|
|
pid_t pid;
|
|
#endif
|
|
int exit_code, sz;
|
|
bool is_exec;
|
|
|
|
};
|
|
|
|
#endif // PIPROCESS_H
|