1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: wxProcess class
4 // Author: Guilhem Lavaux
5 // Modified by: Vadim Zeitlin to check error codes, added Detach() method
8 // Copyright: (c) 1998 Guilhem Lavaux
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
12 #ifndef _WX_PROCESSH__
13 #define _WX_PROCESSH__
15 #if defined(__GNUG__) && !defined(NO_GCC_PRAGMA)
16 #pragma interface "process.h"
22 #include "wx/stream.h"
25 #include "wx/utils.h" // for wxSignal
27 // the wxProcess creation flags
31 wxPROCESS_DEFAULT
= 0,
33 // redirect the IO of the child process
34 wxPROCESS_REDIRECT
= 1
37 // ----------------------------------------------------------------------------
38 // A wxProcess object should be passed to wxExecute - than its OnTerminate()
39 // function will be called when the process terminates.
40 // ----------------------------------------------------------------------------
42 class WXDLLIMPEXP_BASE wxProcess
: public wxEvtHandler
45 // kill the process with the given PID
46 static wxKillError
Kill(int pid
, wxSignal sig
= wxSIGTERM
, int flags
= wxKILL_NOCHILDREN
);
48 // test if the given process exists
49 static bool Exists(int pid
);
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
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
58 static wxProcess
*Open(const wxString
& cmd
, int flags
= wxEXEC_ASYNC
);
62 wxProcess(wxEvtHandler
*parent
= (wxEvtHandler
*) NULL
, int nId
= wxID_ANY
)
63 { Init(parent
, nId
, wxPROCESS_DEFAULT
); }
65 wxProcess(int flags
) { Init(NULL
, wxID_ANY
, flags
); }
69 // may be overridden to be notified about process termination
70 virtual void OnTerminate(int pid
, int status
);
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
75 void Redirect() { m_redirect
= true; }
76 bool IsRedirected() const { return m_redirect
; }
78 // detach from the parent - should be called by the parent if it's deleted
79 // before the process it started terminates
84 wxInputStream
*GetInputStream() const { return m_inputStream
; }
85 wxInputStream
*GetErrorStream() const { return m_errorStream
; }
86 wxOutputStream
*GetOutputStream() const { return m_outputStream
; }
88 // close the output stream indicating that nothing more will be written
89 void CloseOutput() { delete m_outputStream
; m_outputStream
= NULL
; }
91 // return true if the child process stdout is not closed
92 bool IsInputOpened() const;
94 // return true if any input is available on the child process stdout/err
95 bool IsInputAvailable() const;
96 bool IsErrorAvailable() const;
98 // implementation only (for wxExecute)
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
,
105 wxInputStream
*errStream
);
106 #endif // wxUSE_STREAMS
108 // for backwards compatibility only, don't use
109 #if WXWIN_COMPATIBILITY_2_2
110 wxDEPRECATED( wxProcess(wxEvtHandler
*parent
, bool redirect
) );
111 #endif // WXWIN_COMPATIBILITY_2_2
114 void Init(wxEvtHandler
*parent
, int id
, int flags
);
119 // these streams are connected to stdout, stderr and stdin of the child
120 // process respectively (yes, m_inputStream corresponds to stdout -- very
121 // confusing but too late to change now)
122 wxInputStream
*m_inputStream
,
124 wxOutputStream
*m_outputStream
;
125 #endif // wxUSE_STREAMS
129 DECLARE_DYNAMIC_CLASS(wxProcess
)
130 DECLARE_NO_COPY_CLASS(wxProcess
)
133 // ----------------------------------------------------------------------------
135 // ----------------------------------------------------------------------------
137 BEGIN_DECLARE_EVENT_TYPES()
138 DECLARE_EXPORTED_EVENT_TYPE(WXDLLIMPEXP_BASE
, wxEVT_END_PROCESS
, 440)
139 END_DECLARE_EVENT_TYPES()
141 class WXDLLIMPEXP_BASE wxProcessEvent
: public wxEvent
144 wxProcessEvent(int nId
= 0, int pid
= 0, int exitcode
= 0) : wxEvent(nId
)
146 m_eventType
= wxEVT_END_PROCESS
;
148 m_exitcode
= exitcode
;
152 // PID of process which terminated
153 int GetPid() { return m_pid
; }
156 int GetExitCode() { return m_exitcode
; }
158 // implement the base class pure virtual
159 virtual wxEvent
*Clone() const { return new wxProcessEvent(*this); }
165 DECLARE_DYNAMIC_CLASS_NO_ASSIGN(wxProcessEvent
)
168 typedef void (wxEvtHandler::*wxProcessEventFunction
)(wxProcessEvent
&);
170 #define wxProcessEventHandler(func) \
171 (wxObjectEventFunction)wxStaticCastEvent(wxProcessEventFunction, &func)
173 #define EVT_END_PROCESS(id, func) \
174 wx__DECLARE_EVT1(wxEVT_END_PROCESS, id, wxProcessEventHandler(func))
176 #endif // _WX_PROCESSH__