]> git.saurik.com Git - wxWidgets.git/blob - src/common/process.cpp
don't use __MSL__ unless it's defined
[wxWidgets.git] / src / common / process.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: process.cpp
3 // Purpose: Process termination classes
4 // Author: Guilhem Lavaux
5 // Modified by: Vadim Zeitlin to check error codes, added Detach() method
6 // Created: 24/06/98
7 // RCS-ID: $Id$
8 // Copyright: (c) Guilhem Lavaux
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
11
12 // ============================================================================
13 // declarations
14 // ============================================================================
15
16 // ----------------------------------------------------------------------------
17 // headers
18 // ----------------------------------------------------------------------------
19
20 // For compilers that support precompilation, includes "wx.h".
21 #include "wx/wxprec.h"
22
23 #ifdef __BORLANDC__
24 #pragma hdrstop
25 #endif
26
27 #include "wx/process.h"
28
29 // ----------------------------------------------------------------------------
30 // event tables and such
31 // ----------------------------------------------------------------------------
32
33 DEFINE_EVENT_TYPE(wxEVT_END_PROCESS)
34
35 IMPLEMENT_DYNAMIC_CLASS(wxProcess, wxEvtHandler)
36 IMPLEMENT_DYNAMIC_CLASS(wxProcessEvent, wxEvent)
37
38 // ============================================================================
39 // wxProcess implementation
40 // ============================================================================
41
42 // ----------------------------------------------------------------------------
43 // wxProcess creation
44 // ----------------------------------------------------------------------------
45
46 #if WXWIN_COMPATIBILITY_2_2
47
48 wxProcess::wxProcess(wxEvtHandler *parent, bool redirect)
49 {
50 Init(parent, wxID_ANY, redirect ? wxPROCESS_REDIRECT : wxPROCESS_DEFAULT);
51 }
52
53 #endif // WXWIN_COMPATIBILITY_2_2
54
55 void wxProcess::Init(wxEvtHandler *parent, int id, int flags)
56 {
57 if ( parent )
58 SetNextHandler(parent);
59
60 m_id = id;
61 m_redirect = (flags & wxPROCESS_REDIRECT) != 0;
62
63 #if wxUSE_STREAMS
64 m_inputStream = NULL;
65 m_errorStream = NULL;
66 m_outputStream = NULL;
67 #endif // wxUSE_STREAMS
68 }
69
70 /* static */
71 wxProcess *wxProcess::Open(const wxString& cmd, int flags)
72 {
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) )
76 {
77 // couldn't launch the process
78 delete process;
79 return NULL;
80 }
81
82 return process;
83 }
84
85 // ----------------------------------------------------------------------------
86 // wxProcess termination
87 // ----------------------------------------------------------------------------
88
89 wxProcess::~wxProcess()
90 {
91 #if wxUSE_STREAMS
92 delete m_inputStream;
93 delete m_errorStream;
94 delete m_outputStream;
95 #endif // wxUSE_STREAMS
96 }
97
98 void wxProcess::OnTerminate(int pid, int status)
99 {
100 wxProcessEvent event(m_id, pid, status);
101
102 if ( !ProcessEvent(event) )
103 delete this;
104 //else: the object which processed the event is responsible for deleting
105 // us!
106 }
107
108 void wxProcess::Detach()
109 {
110 SetNextHandler(NULL);
111 }
112
113 // ----------------------------------------------------------------------------
114 // process IO redirection
115 // ----------------------------------------------------------------------------
116
117 #if wxUSE_STREAMS
118
119 void wxProcess::SetPipeStreams(wxInputStream *inputSstream,
120 wxOutputStream *outputStream,
121 wxInputStream *errorStream)
122 {
123 m_inputStream = inputSstream;
124 m_errorStream = errorStream;
125 m_outputStream = outputStream;
126 }
127
128 bool wxProcess::IsInputOpened() const
129 {
130 return m_inputStream && m_inputStream->GetLastError() != wxSTREAM_EOF;
131 }
132
133 bool wxProcess::IsInputAvailable() const
134 {
135 return m_inputStream && m_inputStream->CanRead();
136 }
137
138 bool wxProcess::IsErrorAvailable() const
139 {
140 return m_errorStream && m_errorStream->CanRead();
141 }
142
143 #endif // wxUSE_STREAMS
144
145 // ----------------------------------------------------------------------------
146 // process killing
147 // ----------------------------------------------------------------------------
148
149 /* static */
150 wxKillError wxProcess::Kill(int pid, wxSignal sig, int flags)
151 {
152 wxKillError rc;
153 (void)wxKill(pid, sig, &rc, flags);
154
155 return rc;
156 }
157
158 /* static */
159 bool wxProcess::Exists(int pid)
160 {
161 switch ( Kill(pid, wxSIGNONE) )
162 {
163 case wxKILL_OK:
164 case wxKILL_ACCESS_DENIED:
165 return true;
166
167 default:
168 case wxKILL_ERROR:
169 case wxKILL_BAD_SIGNAL:
170 wxFAIL_MSG( _T("unexpected wxProcess::Kill() return code") );
171 // fall through
172
173 case wxKILL_NO_PROCESS:
174 return false;
175 }
176 }
177