]>
Commit | Line | Data |
---|---|---|
23324ae1 FM |
1 | ///////////////////////////////////////////////////////////////////////////// |
2 | // Name: process.h | |
e54c96f1 | 3 | // Purpose: interface of wxProcess |
23324ae1 FM |
4 | // Author: wxWidgets team |
5 | // RCS-ID: $Id$ | |
6 | // Licence: wxWindows license | |
7 | ///////////////////////////////////////////////////////////////////////////// | |
8 | ||
b1b95a65 FM |
9 | /** |
10 | Signal constants used by wxProcess. | |
11 | */ | |
12 | enum wxSignal | |
13 | { | |
14 | wxSIGNONE = 0, //!< verify if the process exists under Unix | |
15 | wxSIGHUP, | |
16 | wxSIGINT, | |
17 | wxSIGQUIT, | |
18 | wxSIGILL, | |
19 | wxSIGTRAP, | |
20 | wxSIGABRT, | |
21 | wxSIGEMT, | |
22 | wxSIGFPE, | |
23 | wxSIGKILL, //!< forcefully kill, dangerous! | |
24 | wxSIGBUS, | |
25 | wxSIGSEGV, | |
26 | wxSIGSYS, | |
27 | wxSIGPIPE, | |
28 | wxSIGALRM, | |
29 | wxSIGTERM //!< terminate the process gently | |
30 | }; | |
31 | ||
32 | /** | |
33 | Return values for wxProcess::Kill. | |
34 | */ | |
35 | enum wxKillError | |
36 | { | |
37 | wxKILL_OK, //!< no error | |
38 | wxKILL_BAD_SIGNAL, //!< no such signal | |
39 | wxKILL_ACCESS_DENIED, //!< permission denied | |
40 | wxKILL_NO_PROCESS, //!< no such process | |
41 | wxKILL_ERROR //!< another, unspecified error | |
42 | }; | |
43 | ||
44 | ||
23324ae1 FM |
45 | /** |
46 | @class wxProcess | |
7c913512 | 47 | |
b1b95a65 FM |
48 | The objects of this class are used in conjunction with the ::wxExecute() function. |
49 | When a wxProcess object is passed to ::wxExecute(), its OnTerminate() virtual method | |
50 | is called when the process terminates. This allows the program to be (asynchronously) | |
51 | notified about the process termination and also retrieve its exit status which is | |
52 | unavailable from ::wxExecute() in the case of asynchronous execution. | |
7c913512 | 53 | |
b1b95a65 | 54 | @note If the process termination notification is processed by the |
23324ae1 FM |
55 | parent, it is responsible for deleting the wxProcess object which sent it. |
56 | However, if it is not processed, the object will delete itself and so the | |
57 | library users should only delete those objects whose notifications have been | |
58 | processed (and call wxProcess::Detach for others). | |
7c913512 | 59 | |
23324ae1 | 60 | wxProcess also supports IO redirection of the child process. For this, you have |
b1b95a65 FM |
61 | to call its Redirect() method before passing it to ::wxExecute(). |
62 | If the child process was launched successfully, GetInputStream(), GetOutputStream() | |
63 | and GetErrorStream() can then be used to retrieve the streams corresponding to the | |
64 | child process standard output, input and error output respectively. | |
7c913512 | 65 | |
23324ae1 FM |
66 | @library{wxbase} |
67 | @category{appmanagement} | |
7c913512 | 68 | |
b1b95a65 | 69 | @see wxExecute(), @ref page_samples_exec "exec sample" |
23324ae1 FM |
70 | */ |
71 | class wxProcess : public wxEvtHandler | |
72 | { | |
73 | public: | |
23324ae1 | 74 | /** |
4cc4bfaf | 75 | Constructs a process object. @a id is only used in the case you want to |
23324ae1 FM |
76 | use wxWidgets events. It identifies this object, or another window that will |
77 | receive the event. | |
b1b95a65 | 78 | |
4cc4bfaf | 79 | If the @a parent parameter is different from @NULL, it will receive |
b1b95a65 FM |
80 | a @c wxEVT_END_PROCESS notification event (you should insert @c EVT_END_PROCESS |
81 | macro in the event table of the parent to handle it) with the given @a id. | |
3c4f71cc | 82 | |
7c913512 | 83 | @param parent |
4cc4bfaf | 84 | The event handler parent. |
7c913512 | 85 | @param id |
4cc4bfaf | 86 | id of an event. |
23324ae1 | 87 | */ |
4cc4bfaf | 88 | wxProcess(wxEvtHandler* parent = NULL, int id = -1); |
b1b95a65 FM |
89 | |
90 | /** | |
91 | Creates an object without any associated parent (and hence no id neither) | |
92 | but allows to specify the @a flags which can have the value of | |
93 | @c wxPROCESS_DEFAULT or @c wxPROCESS_REDIRECT. | |
94 | ||
95 | Specifying the former value has no particular effect while using the latter | |
96 | one is equivalent to calling Redirect(). | |
97 | */ | |
7c913512 | 98 | wxProcess(int flags); |
23324ae1 FM |
99 | |
100 | /** | |
101 | Destroys the wxProcess object. | |
102 | */ | |
adaaa686 | 103 | virtual ~wxProcess(); |
23324ae1 FM |
104 | |
105 | /** | |
106 | Closes the output stream (the one connected to the stdin of the child | |
b1b95a65 FM |
107 | process). |
108 | ||
109 | This function can be used to indicate to the child process that | |
23324ae1 FM |
110 | there is no more data to be read - usually, a filter program will only |
111 | terminate when the input stream is closed. | |
112 | */ | |
113 | void CloseOutput(); | |
114 | ||
115 | /** | |
116 | Normally, a wxProcess object is deleted by its parent when it receives the | |
117 | notification about the process termination. However, it might happen that the | |
118 | parent object is destroyed before the external process is terminated (e.g. a | |
119 | window from which this external process was launched is closed by the user) | |
120 | and in this case it @b should not delete the wxProcess object, but | |
121 | @b should call Detach() instead. After the wxProcess object is detached | |
122 | from its parent, no notification events will be sent to the parent and the | |
123 | object will delete itself upon reception of the process termination | |
124 | notification. | |
125 | */ | |
126 | void Detach(); | |
127 | ||
128 | /** | |
129 | Returns @true if the given process exists in the system. | |
3c4f71cc | 130 | |
b1b95a65 | 131 | @see Kill(), @ref page_samples_exec "Exec sample" |
23324ae1 FM |
132 | */ |
133 | static bool Exists(int pid); | |
134 | ||
135 | /** | |
136 | Returns an input stream which corresponds to the standard error output (stderr) | |
137 | of the child process. | |
138 | */ | |
328f5751 | 139 | wxInputStream* GetErrorStream() const; |
23324ae1 FM |
140 | |
141 | /** | |
142 | It returns an input stream corresponding to the standard output stream of the | |
143 | subprocess. If it is @NULL, you have not turned on the redirection. | |
b1b95a65 FM |
144 | |
145 | @see Redirect(). | |
23324ae1 | 146 | */ |
328f5751 | 147 | wxInputStream* GetInputStream() const; |
23324ae1 FM |
148 | |
149 | /** | |
150 | It returns an output stream correspoding to the input stream of the subprocess. | |
151 | If it is @NULL, you have not turned on the redirection. | |
b1b95a65 FM |
152 | |
153 | @see Redirect(). | |
23324ae1 | 154 | */ |
328f5751 | 155 | wxOutputStream* GetOutputStream() const; |
23324ae1 FM |
156 | |
157 | /** | |
158 | Returns the process ID of the process launched by Open(). | |
159 | */ | |
328f5751 | 160 | long GetPid() const; |
23324ae1 FM |
161 | |
162 | /** | |
163 | Returns @true if there is data to be read on the child process standard | |
164 | error stream. | |
3c4f71cc | 165 | |
4cc4bfaf | 166 | @see IsInputAvailable() |
23324ae1 | 167 | */ |
328f5751 | 168 | bool IsErrorAvailable() const; |
23324ae1 FM |
169 | |
170 | /** | |
171 | Returns @true if there is data to be read on the child process standard | |
b1b95a65 FM |
172 | output stream. |
173 | ||
174 | This allows to write simple (and extremely inefficient) polling-based code | |
175 | waiting for a better mechanism in future wxWidgets versions. | |
176 | See the @ref page_samples_exec "exec sample" for an example of using this | |
23324ae1 | 177 | function. |
3c4f71cc | 178 | |
4cc4bfaf | 179 | @see IsInputOpened() |
23324ae1 | 180 | */ |
328f5751 | 181 | bool IsInputAvailable() const; |
23324ae1 FM |
182 | |
183 | /** | |
184 | Returns @true if the child process standard output stream is opened. | |
185 | */ | |
328f5751 | 186 | bool IsInputOpened() const; |
23324ae1 FM |
187 | |
188 | /** | |
b1b95a65 FM |
189 | Send the specified signal to the given process. Possible signal values |
190 | can be one of the ::wxSignal enumeration values. | |
3c4f71cc | 191 | |
23324ae1 FM |
192 | @c wxSIGNONE, @c wxSIGKILL and @c wxSIGTERM have the same meaning |
193 | under both Unix and Windows but all the other signals are equivalent to | |
194 | @c wxSIGTERM under Windows. | |
3c4f71cc | 195 | |
b1b95a65 FM |
196 | The @a flags parameter can be @c wxKILL_NOCHILDREN (the default), |
197 | or @c wxKILL_CHILDREN, in which case the child processes of this | |
198 | process will be killed too. Note that under Unix, for @c wxKILL_CHILDREN | |
199 | to work you should have created the process passing @c wxEXEC_MAKE_GROUP_LEADER. | |
200 | ||
201 | Returns the element of ::wxKillError enum. | |
202 | ||
203 | @see Exists(), wxKill(), @ref page_samples_exec "Exec sample" | |
23324ae1 | 204 | */ |
382f12e4 | 205 | static wxKillError Kill(int pid, wxSignal sig = wxSIGTERM, |
23324ae1 FM |
206 | int flags = wxKILL_NOCHILDREN); |
207 | ||
208 | /** | |
b1b95a65 | 209 | It is called when the process with the pid @a pid finishes. |
23324ae1 | 210 | It raises a wxWidgets event when it isn't overridden. |
3c4f71cc | 211 | |
b1b95a65 | 212 | @param pid |
4cc4bfaf | 213 | The pid of the process which has just terminated. |
7c913512 | 214 | @param status |
4cc4bfaf | 215 | The exit code of the process. |
23324ae1 | 216 | */ |
adaaa686 | 217 | virtual void OnTerminate(int pid, int status); |
23324ae1 FM |
218 | |
219 | /** | |
220 | This static method replaces the standard @c popen() function: it launches | |
4cc4bfaf | 221 | the process specified by the @a cmd parameter and returns the wxProcess |
23324ae1 FM |
222 | object which can be used to retrieve the streams connected to the standard |
223 | input, output and error output of the child process. | |
b1b95a65 FM |
224 | |
225 | If the process couldn't be launched, @NULL is returned. | |
226 | ||
227 | @remarks | |
228 | In any case the returned pointer should @b not be deleted, rather the process | |
23324ae1 FM |
229 | object will be destroyed automatically when the child process terminates. This |
230 | does mean that the child process should be told to quit before the main program | |
231 | exits to avoid memory leaks. | |
3c4f71cc | 232 | |
7c913512 | 233 | @param cmd |
4cc4bfaf | 234 | The command to execute, including optional arguments. |
7c913512 | 235 | @param flags |
b1b95a65 FM |
236 | The flags to pass to ::wxExecute(). |
237 | Note: @c wxEXEC_SYNC should not be used. | |
3c4f71cc | 238 | |
d29a9a8a | 239 | @return A pointer to new wxProcess object or @NULL on error. |
3c4f71cc | 240 | |
b1b95a65 | 241 | @see ::wxExecute() |
23324ae1 | 242 | */ |
4cc4bfaf FM |
243 | static wxProcess* Open(const wxString& cmd, |
244 | int flags = wxEXEC_ASYNC); | |
23324ae1 FM |
245 | |
246 | /** | |
b1b95a65 FM |
247 | Turns on redirection. |
248 | ||
249 | ::wxExecute() will try to open a couple of pipes to catch the subprocess stdio. | |
250 | The caught input stream is returned by GetOutputStream() as a non-seekable stream. | |
251 | The caught output stream is returned by GetInputStream() as a non-seekable stream. | |
23324ae1 FM |
252 | */ |
253 | void Redirect(); | |
254 | }; | |
255 | ||
256 | ||
e54c96f1 | 257 | |
23324ae1 FM |
258 | /** |
259 | @class wxProcessEvent | |
7c913512 | 260 | |
3051a44a FM |
261 | A process event is sent to the wxEvtHandler specified to wxProcess |
262 | when a process is terminated. | |
7c913512 | 263 | |
b1b95a65 FM |
264 | @beginEventTable{wxProcessEvent} |
265 | @event{EVT_END_PROCESS(id, func)} | |
3051a44a FM |
266 | Process a @c wxEVT_END_PROCESS event. @a id is the identifier of the process |
267 | object (the id passed to the wxProcess constructor) or a window to receive | |
268 | the event. | |
b1b95a65 FM |
269 | @endEventTable |
270 | ||
23324ae1 FM |
271 | @library{wxbase} |
272 | @category{events} | |
7c913512 | 273 | |
830b7aa7 | 274 | @see wxProcess, @ref overview_events |
23324ae1 FM |
275 | */ |
276 | class wxProcessEvent : public wxEvent | |
277 | { | |
278 | public: | |
279 | /** | |
b1b95a65 FM |
280 | Constructor. |
281 | ||
282 | Takes a wxProcessObject or window id, a process id and an exit status. | |
23324ae1 FM |
283 | */ |
284 | wxProcessEvent(int id = 0, int pid = 0, int exitcode = 0); | |
285 | ||
286 | /** | |
287 | Returns the exist status. | |
288 | */ | |
289 | int GetExitCode(); | |
290 | ||
291 | /** | |
292 | Returns the process id. | |
293 | */ | |
adaaa686 | 294 | int GetPid(); |
23324ae1 | 295 | }; |
e54c96f1 | 296 |