1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/common/process.cpp
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 wxDEFINE_EVENT( wxEVT_END_PROCESS, wxProcessEvent );
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);
53 m_redirect = (flags & wxPROCESS_REDIRECT) != 0;
58 m_outputStream = NULL;
59 #endif // wxUSE_STREAMS
63 wxProcess *wxProcess::Open(const wxString& cmd, int flags)
65 wxASSERT_MSG( !(flags & wxEXEC_SYNC), wxT("wxEXEC_SYNC should not be used." ));
66 wxProcess *process = new wxProcess(wxPROCESS_REDIRECT);
67 long pid = wxExecute(cmd, flags, process);
70 // couldn't launch the process
80 // ----------------------------------------------------------------------------
81 // wxProcess termination
82 // ----------------------------------------------------------------------------
84 wxProcess::~wxProcess()
89 delete m_outputStream;
90 #endif // wxUSE_STREAMS
93 void wxProcess::OnTerminate(int pid, int status)
95 wxProcessEvent event(m_id, pid, status);
97 if ( !ProcessEvent(event) )
99 //else: the object which processed the event is responsible for deleting
103 void wxProcess::Detach()
105 // we just detach from the next handler of the chain (i.e. our "parent" -- see ctor)
106 // not also from the previous handler like wxEvtHandler::Unlink() would do:
109 m_nextHandler->SetPreviousHandler(m_previousHandler);
111 m_nextHandler = NULL;
114 // ----------------------------------------------------------------------------
115 // process IO redirection
116 // ----------------------------------------------------------------------------
120 void wxProcess::SetPipeStreams(wxInputStream *inputSstream,
121 wxOutputStream *outputStream,
122 wxInputStream *errorStream)
124 m_inputStream = inputSstream;
125 m_errorStream = errorStream;
126 m_outputStream = outputStream;
129 bool wxProcess::IsInputOpened() const
131 return m_inputStream && m_inputStream->GetLastError() != wxSTREAM_EOF;
134 bool wxProcess::IsInputAvailable() const
136 return m_inputStream && m_inputStream->CanRead();
139 bool wxProcess::IsErrorAvailable() const
141 return m_errorStream && m_errorStream->CanRead();
144 #endif // wxUSE_STREAMS
146 // ----------------------------------------------------------------------------
148 // ----------------------------------------------------------------------------
151 wxKillError wxProcess::Kill(int pid, wxSignal sig, int flags)
154 (void)wxKill(pid, sig, &rc, flags);
160 bool wxProcess::Exists(int pid)
162 switch ( Kill(pid, wxSIGNONE) )
165 case wxKILL_ACCESS_DENIED:
170 case wxKILL_BAD_SIGNAL:
171 wxFAIL_MSG( wxT("unexpected wxProcess::Kill() return code") );
174 case wxKILL_NO_PROCESS: