1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: Process termination classes
4 // Author: Guilhem Lavaux
5 // Modified by: Vadim Zeitlin to check error codes, added Detach() method
8 // Copyright: (c) Guilhem Lavaux
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
12 // ============================================================================
14 // ============================================================================
16 // ----------------------------------------------------------------------------
18 // ----------------------------------------------------------------------------
20 // For compilers that support precompilation, includes "wx.h".
21 #include "wx/wxprec.h"
27 #include "wx/process.h"
29 // ----------------------------------------------------------------------------
30 // event tables and such
31 // ----------------------------------------------------------------------------
33 DEFINE_EVENT_TYPE(wxEVT_END_PROCESS)
35 IMPLEMENT_DYNAMIC_CLASS(wxProcess, wxEvtHandler)
36 IMPLEMENT_DYNAMIC_CLASS(wxProcessEvent, wxEvent)
38 // ============================================================================
39 // wxProcess implementation
40 // ============================================================================
42 // ----------------------------------------------------------------------------
44 // ----------------------------------------------------------------------------
46 void wxProcess::Init(wxEvtHandler *parent, int id, int flags)
49 SetNextHandler(parent);
52 m_redirect = (flags & wxPROCESS_REDIRECT) != 0;
57 m_outputStream = NULL;
58 #endif // wxUSE_STREAMS
62 wxProcess *wxProcess::Open(const wxString& cmd, int flags)
64 wxASSERT_MSG( !(flags & wxEXEC_SYNC), wxT("wxEXEC_SYNC should not be used." ));
65 wxProcess *process = new wxProcess(wxPROCESS_REDIRECT);
66 if ( !wxExecute(cmd, flags, process) )
68 // couldn't launch the process
76 // ----------------------------------------------------------------------------
77 // wxProcess termination
78 // ----------------------------------------------------------------------------
80 wxProcess::~wxProcess()
85 delete m_outputStream;
86 #endif // wxUSE_STREAMS
89 void wxProcess::OnTerminate(int pid, int status)
91 wxProcessEvent event(m_id, pid, status);
93 if ( !ProcessEvent(event) )
95 //else: the object which processed the event is responsible for deleting
99 void wxProcess::Detach()
101 SetNextHandler(NULL);
104 // ----------------------------------------------------------------------------
105 // process IO redirection
106 // ----------------------------------------------------------------------------
110 void wxProcess::SetPipeStreams(wxInputStream *inputSstream,
111 wxOutputStream *outputStream,
112 wxInputStream *errorStream)
114 m_inputStream = inputSstream;
115 m_errorStream = errorStream;
116 m_outputStream = outputStream;
119 bool wxProcess::IsInputOpened() const
121 return m_inputStream && m_inputStream->GetLastError() != wxSTREAM_EOF;
124 bool wxProcess::IsInputAvailable() const
126 return m_inputStream && m_inputStream->CanRead();
129 bool wxProcess::IsErrorAvailable() const
131 return m_errorStream && m_errorStream->CanRead();
134 #endif // wxUSE_STREAMS
136 // ----------------------------------------------------------------------------
138 // ----------------------------------------------------------------------------
141 wxKillError wxProcess::Kill(int pid, wxSignal sig, int flags)
144 (void)wxKill(pid, sig, &rc, flags);
150 bool wxProcess::Exists(int pid)
152 switch ( Kill(pid, wxSIGNONE) )
155 case wxKILL_ACCESS_DENIED:
160 case wxKILL_BAD_SIGNAL:
161 wxFAIL_MSG( _T("unexpected wxProcess::Kill() return code") );
164 case wxKILL_NO_PROCESS: