]> git.saurik.com Git - wxWidgets.git/blob - src/common/process.cpp
Integrated fixes from latest UNIX version.
[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 license
10 /////////////////////////////////////////////////////////////////////////////
11
12 // ============================================================================
13 // declarations
14 // ============================================================================
15
16 // ----------------------------------------------------------------------------
17 // headers
18 // ----------------------------------------------------------------------------
19
20 #ifdef __GNUG__
21 #pragma implementation "process.h"
22 #endif
23
24 // For compilers that support precompilation, includes "wx.h".
25 #include "wx/wxprec.h"
26
27 #ifdef __BORLANDC__
28 #pragma hdrstop
29 #endif
30
31 #include "wx/process.h"
32
33 // ----------------------------------------------------------------------------
34 // event tables and such
35 // ----------------------------------------------------------------------------
36
37 DEFINE_EVENT_TYPE(wxEVT_END_PROCESS)
38
39 IMPLEMENT_DYNAMIC_CLASS(wxProcess, wxEvtHandler)
40 IMPLEMENT_DYNAMIC_CLASS(wxProcessEvent, wxEvent)
41
42 // ============================================================================
43 // wxProcess implementation
44 // ============================================================================
45
46 // ----------------------------------------------------------------------------
47 // wxProcess creation
48 // ----------------------------------------------------------------------------
49
50 void wxProcess::Init(wxEvtHandler *parent, int id, int flags)
51 {
52 if ( parent )
53 SetNextHandler(parent);
54
55 m_id = id;
56 m_redirect = (flags & wxPROCESS_REDIRECT) != 0;
57
58 #if wxUSE_STREAMS
59 m_inputStream = NULL;
60 m_errorStream = NULL;
61 m_outputStream = NULL;
62 #endif // wxUSE_STREAMS
63 }
64
65 /* static */
66 wxProcess *wxProcess::Open(const wxString& cmd, int flags)
67 {
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) )
71 {
72 // couldn't launch the process
73 delete process;
74 return NULL;
75 }
76
77 return process;
78 }
79
80 // ----------------------------------------------------------------------------
81 // wxProcess termination
82 // ----------------------------------------------------------------------------
83
84 wxProcess::~wxProcess()
85 {
86 #if wxUSE_STREAMS
87 delete m_inputStream;
88 delete m_errorStream;
89 delete m_outputStream;
90 #endif // wxUSE_STREAMS
91 }
92
93 void wxProcess::OnTerminate(int pid, int status)
94 {
95 wxProcessEvent event(m_id, pid, status);
96
97 if ( !ProcessEvent(event) )
98 delete this;
99 //else: the object which processed the event is responsible for deleting
100 // us!
101 }
102
103 void wxProcess::Detach()
104 {
105 SetNextHandler(NULL);
106 }
107
108 // ----------------------------------------------------------------------------
109 // process IO redirection
110 // ----------------------------------------------------------------------------
111
112 #if wxUSE_STREAMS
113
114 void wxProcess::SetPipeStreams(wxInputStream *inputSstream,
115 wxOutputStream *outputStream,
116 wxInputStream *errorStream)
117 {
118 m_inputStream = inputSstream;
119 m_errorStream = errorStream;
120 m_outputStream = outputStream;
121 }
122
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
125 // platforms here
126 #if !defined(__WIN32__) && !defined(__UNIX_LIKE__)
127
128 bool wxProcess::IsInputOpened() const
129 {
130 return m_inputStream != NULL;
131 }
132
133 bool wxProcess::IsInputAvailable() const
134 {
135 return m_inputStream && !m_inputStream->Eof();
136 }
137
138 bool wxProcess::IsErrorAvailable() const
139 {
140 return m_errorStream && !m_errorStream->Eof();
141 }
142
143 #endif // !Win32 && !Unix
144
145 #endif // wxUSE_STREAMS
146
147 // ----------------------------------------------------------------------------
148 // process killing
149 // ----------------------------------------------------------------------------
150
151 /* static */
152 wxKillError wxProcess::Kill(int pid, wxSignal sig)
153 {
154 wxKillError rc;
155 (void)wxKill(pid, sig, &rc);
156
157 return rc;
158 }
159
160 /* static */
161 bool wxProcess::Exists(int pid)
162 {
163 switch ( Kill(pid, wxSIGNONE) )
164 {
165 case wxKILL_OK:
166 case wxKILL_ACCESS_DENIED:
167 return TRUE;
168
169 default:
170 case wxKILL_ERROR:
171 case wxKILL_BAD_SIGNAL:
172 wxFAIL_MSG( _T("unexpected wxProcess::Kill() return code") );
173 // fall through
174
175 case wxKILL_NO_PROCESS:
176 return FALSE;
177 }
178 }
179