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