]>
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 #if defined(__GNUG__) && !defined(NO_GCC_PRAGMA)
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 #if WXWIN_COMPATIBILITY_2_2
52 wxProcess::wxProcess(wxEvtHandler
*parent
, bool redirect
)
54 Init(parent
, wxID_ANY
, redirect
? wxPROCESS_REDIRECT
: wxPROCESS_DEFAULT
);
57 #endif // WXWIN_COMPATIBILITY_2_2
59 void wxProcess::Init(wxEvtHandler
*parent
, int id
, int flags
)
62 SetNextHandler(parent
);
65 m_redirect
= (flags
& wxPROCESS_REDIRECT
) != 0;
70 m_outputStream
= NULL
;
71 #endif // wxUSE_STREAMS
75 wxProcess
*wxProcess::Open(const wxString
& cmd
, int flags
)
77 wxASSERT_MSG( !(flags
& wxEXEC_SYNC
), wxT("wxEXEC_SYNC should not be used." ));
78 wxProcess
*process
= new wxProcess(wxPROCESS_REDIRECT
);
79 if ( !wxExecute(cmd
, flags
, process
) )
81 // couldn't launch the process
89 // ----------------------------------------------------------------------------
90 // wxProcess termination
91 // ----------------------------------------------------------------------------
93 wxProcess::~wxProcess()
98 delete m_outputStream
;
99 #endif // wxUSE_STREAMS
102 void wxProcess::OnTerminate(int pid
, int status
)
104 wxProcessEvent
event(m_id
, pid
, status
);
106 if ( !ProcessEvent(event
) )
108 //else: the object which processed the event is responsible for deleting
112 void wxProcess::Detach()
114 SetNextHandler(NULL
);
117 // ----------------------------------------------------------------------------
118 // process IO redirection
119 // ----------------------------------------------------------------------------
123 void wxProcess::SetPipeStreams(wxInputStream
*inputSstream
,
124 wxOutputStream
*outputStream
,
125 wxInputStream
*errorStream
)
127 m_inputStream
= inputSstream
;
128 m_errorStream
= errorStream
;
129 m_outputStream
= outputStream
;
132 bool wxProcess::IsInputOpened() const
134 return m_inputStream
&& m_inputStream
->GetLastError() != wxSTREAM_EOF
;
137 bool wxProcess::IsInputAvailable() const
139 return m_inputStream
&& m_inputStream
->CanRead();
142 bool wxProcess::IsErrorAvailable() const
144 return m_errorStream
&& m_errorStream
->CanRead();
147 #endif // wxUSE_STREAMS
149 // ----------------------------------------------------------------------------
151 // ----------------------------------------------------------------------------
154 wxKillError
wxProcess::Kill(int pid
, wxSignal sig
, int flags
)
157 (void)wxKill(pid
, sig
, &rc
, flags
);
163 bool wxProcess::Exists(int pid
)
165 switch ( Kill(pid
, wxSIGNONE
) )
168 case wxKILL_ACCESS_DENIED
:
173 case wxKILL_BAD_SIGNAL
:
174 wxFAIL_MSG( _T("unexpected wxProcess::Kill() return code") );
177 case wxKILL_NO_PROCESS
: