00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020 #ifndef _pprocess_h_
00021 #define _pprocess_h_
00022
00023 #include <pclasses/pexport.h>
00024 #include <pclasses/pexception.h>
00025 #include <pclasses/piodevice.h>
00026 #include <string>
00027 #include <list>
00028
00029 namespace P {
00030
00032
00037 class PCORE_EXPORT ProcessIO: public IODevice {
00038 friend class Process;
00039 public:
00040
00042 void close() throw(IOError);
00043
00045 size_t write(const char* buffer, size_t count) throw(IOError);
00046
00048 size_t read(char* buffer, size_t count) throw(IOError);
00049
00051 size_t readErr(char* buffer, size_t count) throw(IOError);
00052
00053 protected:
00054 ProcessIO(io_handle_t in, io_handle_t out, io_handle_t err) throw();
00055 ~ProcessIO() throw();
00056
00057 private:
00058 io_handle_t m_in;
00059 io_handle_t m_out;
00060 io_handle_t m_err;
00061
00062 };
00063
00064
00066
00070 class PCORE_EXPORT ProcessEnv {
00071 public:
00072
00074 static void set(const char* name, const char* value);
00075
00077 static void unset(const char* name);
00078
00080 static const char* get(const char* name);
00081
00082 private:
00083 ProcessEnv();
00084 ~ProcessEnv();
00085 };
00086
00087
00089
00095 class PCORE_EXPORT Process {
00096 public:
00098 enum state_t {
00099 Stopped,
00100 Running,
00101 Stopping
00102 };
00103
00105 enum commMode_t {
00106 NoCommunication = 0,
00107 StdIn = 1,
00108 StdOut = 2,
00109 StdErr = 4,
00110 AllOutput = 6,
00111 All = 7
00112 };
00113
00115 typedef std::list<std::string> arg_list;
00116
00118 Process(const char* path, const arg_list& args = arg_list())
00119 throw(IOError);
00120
00121 ~Process() throw();
00122
00124 inline state_t state() const
00125 { return m_state; }
00126
00128 void addArg(const char* arg);
00129
00131 void clearArgs();
00132
00134 void setWorkDir(const char* dir);
00135
00136 inline const std::string& workDir() const
00137 { return m_dir; }
00138
00140 void start(commMode_t mode = NoCommunication)
00141 throw(SystemError,IOError,LogicError);
00142
00144 void stop() throw(SystemError);
00145
00147 void kill() throw(SystemError);
00148
00150 bool tryWait(int& exitCode) throw(SystemError);
00151
00153 int wait() throw(SystemError);
00154
00156 static Process* tryWaitAny(int& exitCode) throw(SystemError);
00157
00159 static Process* waitAny(int& exitCode) throw(SystemError);
00160
00162
00169 inline ProcessIO* processIO() const throw()
00170 { return m_procIO; }
00171
00172 private:
00173 struct process_handle_t;
00174 process_handle_t* m_handle;
00175
00176 ProcessIO* m_procIO;
00177 state_t m_state;
00178 std::string m_dir;
00179 std::string m_program;
00180 arg_list m_args;
00181 };
00182
00183 }
00184
00185 #endif