]>
git.saurik.com Git - wxWidgets.git/blob - src/common/process.cpp
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 license
10 /////////////////////////////////////////////////////////////////////////////
12 // ============================================================================
14 // ============================================================================
16 // ----------------------------------------------------------------------------
18 // ----------------------------------------------------------------------------
21 #pragma implementation "process.h"
24 // For compilers that support precompilation, includes "wx.h".
25 #include "wx/wxprec.h"
31 #include "wx/process.h"
33 // ----------------------------------------------------------------------------
34 // event tables and such
35 // ----------------------------------------------------------------------------
37 DEFINE_EVENT_TYPE(wxEVT_END_PROCESS
)
39 IMPLEMENT_DYNAMIC_CLASS(wxProcess
, wxEvtHandler
)
40 IMPLEMENT_DYNAMIC_CLASS(wxProcessEvent
, wxEvent
)
42 // ============================================================================
43 // wxProcess implementation
44 // ============================================================================
46 // ----------------------------------------------------------------------------
48 // ----------------------------------------------------------------------------
50 void wxProcess::Init(wxEvtHandler
*parent
, int id
, int flags
)
53 SetNextHandler(parent
);
56 m_redirect
= (flags
& wxPROCESS_REDIRECT
) != 0;
61 m_outputStream
= NULL
;
62 #endif // wxUSE_STREAMS
66 wxProcess
*wxProcess::Open(const wxString
& cmd
, int flags
)
68 wxASSERT_MSG( !(flags
& wxEXEC_SYNC
), wxT("wxEXEC_SYNC should not be used." ));
69 wxProcess
*process
= new wxProcess(wxPROCESS_REDIRECT
);
70 if ( !wxExecute(cmd
, flags
, process
) )
72 // 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 SetNextHandler(NULL
);
108 // ----------------------------------------------------------------------------
109 // process IO redirection
110 // ----------------------------------------------------------------------------
114 void wxProcess::SetPipeStreams(wxInputStream
*inputSstream
,
115 wxOutputStream
*outputStream
,
116 wxInputStream
*errorStream
)
118 m_inputStream
= inputSstream
;
119 m_errorStream
= errorStream
;
120 m_outputStream
= outputStream
;
123 // these are implemented in platform-dependent (and correct!) way under MSW and
124 // Unix but we still have to provide these dummy versions for the other
126 #if !defined(__WIN32__) && !defined(__UNIX_LIKE__)
128 bool wxProcess::IsInputOpened() const
130 return m_inputStream
!= NULL
;
133 bool wxProcess::IsInputAvailable() const
135 return m_inputStream
&& !m_inputStream
->Eof();
138 bool wxProcess::IsErrorAvailable() const
140 return m_errorStream
&& !m_errorStream
->Eof();
143 #endif // !Win32 && !Unix
145 #endif // wxUSE_STREAMS
147 // ----------------------------------------------------------------------------
149 // ----------------------------------------------------------------------------
152 wxKillError
wxProcess::Kill(int pid
, wxSignal sig
)
155 (void)wxKill(pid
, sig
, &rc
);
161 bool wxProcess::Exists(int pid
)
163 switch ( Kill(pid
, wxSIGNONE
) )
166 case wxKILL_ACCESS_DENIED
:
171 case wxKILL_BAD_SIGNAL
:
172 wxFAIL_MSG( _T("unexpected wxProcess::Kill() return code") );
175 case wxKILL_NO_PROCESS
: