]>
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 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 #if WXWIN_COMPATIBILITY_2_2
48 wxProcess::wxProcess(wxEvtHandler
*parent
, bool redirect
)
50 Init(parent
, wxID_ANY
, redirect
? wxPROCESS_REDIRECT
: wxPROCESS_DEFAULT
);
53 #endif // WXWIN_COMPATIBILITY_2_2
55 void wxProcess::Init(wxEvtHandler
*parent
, int id
, int flags
)
58 SetNextHandler(parent
);
61 m_redirect
= (flags
& wxPROCESS_REDIRECT
) != 0;
66 m_outputStream
= NULL
;
67 #endif // wxUSE_STREAMS
71 wxProcess
*wxProcess::Open(const wxString
& cmd
, int flags
)
73 wxASSERT_MSG( !(flags
& wxEXEC_SYNC
), wxT("wxEXEC_SYNC should not be used." ));
74 wxProcess
*process
= new wxProcess(wxPROCESS_REDIRECT
);
75 if ( !wxExecute(cmd
, flags
, process
) )
77 // couldn't launch the process
85 // ----------------------------------------------------------------------------
86 // wxProcess termination
87 // ----------------------------------------------------------------------------
89 wxProcess::~wxProcess()
94 delete m_outputStream
;
95 #endif // wxUSE_STREAMS
98 void wxProcess::OnTerminate(int pid
, int status
)
100 wxProcessEvent
event(m_id
, pid
, status
);
102 if ( !ProcessEvent(event
) )
104 //else: the object which processed the event is responsible for deleting
108 void wxProcess::Detach()
110 SetNextHandler(NULL
);
113 // ----------------------------------------------------------------------------
114 // process IO redirection
115 // ----------------------------------------------------------------------------
119 void wxProcess::SetPipeStreams(wxInputStream
*inputSstream
,
120 wxOutputStream
*outputStream
,
121 wxInputStream
*errorStream
)
123 m_inputStream
= inputSstream
;
124 m_errorStream
= errorStream
;
125 m_outputStream
= outputStream
;
128 bool wxProcess::IsInputOpened() const
130 return m_inputStream
&& m_inputStream
->GetLastError() != wxSTREAM_EOF
;
133 bool wxProcess::IsInputAvailable() const
135 return m_inputStream
&& m_inputStream
->CanRead();
138 bool wxProcess::IsErrorAvailable() const
140 return m_errorStream
&& m_errorStream
->CanRead();
143 #endif // wxUSE_STREAMS
145 // ----------------------------------------------------------------------------
147 // ----------------------------------------------------------------------------
150 wxKillError
wxProcess::Kill(int pid
, wxSignal sig
, int flags
)
153 (void)wxKill(pid
, sig
, &rc
, flags
);
159 bool wxProcess::Exists(int pid
)
161 switch ( Kill(pid
, wxSIGNONE
) )
164 case wxKILL_ACCESS_DENIED
:
169 case wxKILL_BAD_SIGNAL
:
170 wxFAIL_MSG( _T("unexpected wxProcess::Kill() return code") );
173 case wxKILL_NO_PROCESS
: