]>
Commit | Line | Data |
---|---|---|
cf447356 GL |
1 | ///////////////////////////////////////////////////////////////////////////// |
2 | // Name: process.h | |
3 | // Purpose: wxProcess class | |
4 | // Author: Guilhem Lavaux | |
7e84f02d | 5 | // Modified by: Vadim Zeitlin to check error codes, added Detach() method |
cf447356 GL |
6 | // Created: 24/06/98 |
7 | // RCS-ID: $Id$ | |
8 | // Copyright: (c) 1998 Guilhem Lavaux | |
65571936 | 9 | // Licence: wxWindows licence |
cf447356 GL |
10 | ///////////////////////////////////////////////////////////////////////////// |
11 | ||
34138703 JS |
12 | #ifndef _WX_PROCESSH__ |
13 | #define _WX_PROCESSH__ | |
cf447356 | 14 | |
12028905 | 15 | #if defined(__GNUG__) && !defined(NO_GCC_PRAGMA) |
5336ece4 | 16 | #pragma interface "process.h" |
cf447356 GL |
17 | #endif |
18 | ||
cf447356 | 19 | #include "wx/event.h" |
f6bcfd97 BP |
20 | |
21 | #if wxUSE_STREAMS | |
0c850713 | 22 | #include "wx/stream.h" |
f6bcfd97 | 23 | #endif |
cf447356 | 24 | |
50567b69 VZ |
25 | #include "wx/utils.h" // for wxSignal |
26 | ||
da00a8bb VZ |
27 | // the wxProcess creation flags |
28 | enum | |
29 | { | |
30 | // no redirection | |
31 | wxPROCESS_DEFAULT = 0, | |
32 | ||
33 | // redirect the IO of the child process | |
34 | wxPROCESS_REDIRECT = 1 | |
35 | }; | |
36 | ||
2e4df4bf | 37 | // ---------------------------------------------------------------------------- |
5336ece4 VZ |
38 | // A wxProcess object should be passed to wxExecute - than its OnTerminate() |
39 | // function will be called when the process terminates. | |
2e4df4bf VZ |
40 | // ---------------------------------------------------------------------------- |
41 | ||
bddd7a8d | 42 | class WXDLLIMPEXP_BASE wxProcess : public wxEvtHandler |
cf447356 | 43 | { |
5336ece4 | 44 | public: |
da00a8bb | 45 | // kill the process with the given PID |
e0f6b731 | 46 | static wxKillError Kill(int pid, wxSignal sig = wxSIGTERM, int flags = wxKILL_NOCHILDREN); |
da00a8bb VZ |
47 | |
48 | // test if the given process exists | |
49 | static bool Exists(int pid); | |
50 | ||
51 | // this function replaces the standard popen() one: it launches a process | |
52 | // asynchronously and allows the caller to get the streams connected to its | |
53 | // std{in|out|err} | |
54 | // | |
55 | // on error NULL is returned, in any case the process object will be | |
56 | // deleted automatically when the process terminates and should *not* be | |
57 | // deleted by the caller | |
e626d7c7 | 58 | static wxProcess *Open(const wxString& cmd, int flags = wxEXEC_ASYNC); |
da00a8bb VZ |
59 | |
60 | ||
61 | // ctors | |
dcb68102 RN |
62 | wxProcess(wxEvtHandler *parent = (wxEvtHandler *) NULL, int nId = wxID_ANY) |
63 | { Init(parent, nId, wxPROCESS_DEFAULT); } | |
da00a8bb | 64 | |
7e548f6b | 65 | wxProcess(int flags) { Init(NULL, wxID_ANY, flags); } |
cf447356 | 66 | |
cd6ce4a9 VZ |
67 | virtual ~wxProcess(); |
68 | ||
69 | // may be overridden to be notified about process termination | |
5336ece4 | 70 | virtual void OnTerminate(int pid, int status); |
cf447356 | 71 | |
cd6ce4a9 VZ |
72 | // call this before passing the object to wxExecute() to redirect the |
73 | // launched process stdin/stdout, then use GetInputStream() and | |
74 | // GetOutputStream() to get access to them | |
7e548f6b | 75 | void Redirect() { m_redirect = true; } |
cd6ce4a9 VZ |
76 | bool IsRedirected() const { return m_redirect; } |
77 | ||
7e84f02d VZ |
78 | // detach from the parent - should be called by the parent if it's deleted |
79 | // before the process it started terminates | |
80 | void Detach(); | |
81 | ||
f6bcfd97 | 82 | #if wxUSE_STREAMS |
8b33ae2d | 83 | // Pipe handling |
cd6ce4a9 | 84 | wxInputStream *GetInputStream() const { return m_inputStream; } |
f6bcfd97 | 85 | wxInputStream *GetErrorStream() const { return m_errorStream; } |
cd6ce4a9 | 86 | wxOutputStream *GetOutputStream() const { return m_outputStream; } |
8b33ae2d | 87 | |
f6bcfd97 BP |
88 | // close the output stream indicating that nothing more will be written |
89 | void CloseOutput() { delete m_outputStream; m_outputStream = NULL; } | |
90 | ||
7e548f6b | 91 | // return true if the child process stdout is not closed |
411165f3 VZ |
92 | bool IsInputOpened() const; |
93 | ||
7e548f6b | 94 | // return true if any input is available on the child process stdout/err |
411165f3 VZ |
95 | bool IsInputAvailable() const; |
96 | bool IsErrorAvailable() const; | |
97 | ||
cd6ce4a9 | 98 | // implementation only (for wxExecute) |
da00a8bb VZ |
99 | // |
100 | // NB: the streams passed here should correspond to the child process | |
101 | // stdout, stdin and stderr and here the normal naming convention is | |
102 | // used unlike elsewhere in this class | |
103 | void SetPipeStreams(wxInputStream *outStream, | |
104 | wxOutputStream *inStream, | |
f6bcfd97 BP |
105 | wxInputStream *errStream); |
106 | #endif // wxUSE_STREAMS | |
8b33ae2d | 107 | |
da00a8bb VZ |
108 | // for backwards compatibility only, don't use |
109 | #if WXWIN_COMPATIBILITY_2_2 | |
110 | wxProcess(wxEvtHandler *parent, bool redirect) | |
7e548f6b | 111 | { Init(parent, wxID_ANY, redirect ? wxPROCESS_REDIRECT : wxPROCESS_DEFAULT); } |
da00a8bb | 112 | #endif // WXWIN_COMPATIBILITY_2_2 |
50567b69 | 113 | |
5336ece4 | 114 | protected: |
da00a8bb | 115 | void Init(wxEvtHandler *parent, int id, int flags); |
cd6ce4a9 | 116 | |
5336ece4 | 117 | int m_id; |
cd6ce4a9 | 118 | |
f6bcfd97 | 119 | #if wxUSE_STREAMS |
da00a8bb VZ |
120 | // these streams are connected to stdout, stderr and stdin of the child |
121 | // process respectively (yes, m_inputStream corresponds to stdout -- very | |
122 | // confusing but too late to change now) | |
f6bcfd97 BP |
123 | wxInputStream *m_inputStream, |
124 | *m_errorStream; | |
cd6ce4a9 | 125 | wxOutputStream *m_outputStream; |
f6bcfd97 | 126 | #endif // wxUSE_STREAMS |
cd6ce4a9 VZ |
127 | |
128 | bool m_redirect; | |
50567b69 VZ |
129 | |
130 | DECLARE_DYNAMIC_CLASS(wxProcess) | |
22f3361e | 131 | DECLARE_NO_COPY_CLASS(wxProcess) |
cf447356 GL |
132 | }; |
133 | ||
2e4df4bf VZ |
134 | // ---------------------------------------------------------------------------- |
135 | // wxProcess events | |
136 | // ---------------------------------------------------------------------------- | |
137 | ||
71b9ed15 | 138 | BEGIN_DECLARE_EVENT_TYPES() |
4c3ebca9 | 139 | DECLARE_EXPORTED_EVENT_TYPE(WXDLLIMPEXP_BASE, wxEVT_END_PROCESS, 440) |
71b9ed15 VZ |
140 | END_DECLARE_EVENT_TYPES() |
141 | ||
bddd7a8d | 142 | class WXDLLIMPEXP_BASE wxProcessEvent : public wxEvent |
0c850713 VZ |
143 | { |
144 | public: | |
dcb68102 | 145 | wxProcessEvent(int nId = 0, int pid = 0, int exitcode = 0) : wxEvent(nId) |
0c850713 VZ |
146 | { |
147 | m_eventType = wxEVT_END_PROCESS; | |
148 | m_pid = pid; | |
149 | m_exitcode = exitcode; | |
150 | } | |
151 | ||
152 | // accessors | |
153 | // PID of process which terminated | |
154 | int GetPid() { return m_pid; } | |
155 | ||
156 | // the exit code | |
157 | int GetExitCode() { return m_exitcode; } | |
158 | ||
acd15a3f VZ |
159 | // implement the base class pure virtual |
160 | virtual wxEvent *Clone() const { return new wxProcessEvent(*this); } | |
161 | ||
0c850713 | 162 | public: |
acd15a3f VZ |
163 | int m_pid, |
164 | m_exitcode; | |
0c850713 | 165 | |
fc7a2a60 | 166 | DECLARE_DYNAMIC_CLASS_NO_ASSIGN(wxProcessEvent) |
0c850713 VZ |
167 | }; |
168 | ||
afadf3bc | 169 | typedef void (wxEvtHandler::*wxProcessEventFunction)(wxProcessEvent&); |
cf447356 | 170 | |
82a5f02c | 171 | #define EVT_END_PROCESS(id, func) \ |
afadf3bc | 172 | DECLARE_EVENT_TABLE_ENTRY( \ |
7e548f6b | 173 | wxEVT_END_PROCESS, id, wxID_ANY, \ |
3a818b15 | 174 | (wxObjectEventFunction) (wxEventFunction) wxStaticCastEvent( wxProcessEventFunction, & func ), NULL), |
cf447356 GL |
175 | |
176 | #endif | |
34138703 | 177 | // _WX_PROCESSH__ |