]> git.saurik.com Git - wxWidgets.git/blame - src/common/process.cpp
Removed some old testing code. Added more appropriate minimal content.
[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
ca7731b7
GL
20// For compilers that support precompilation, includes "wx.h".
21#include "wx/wxprec.h"
22
23#ifdef __BORLANDC__
5336ece4 24 #pragma hdrstop
ca7731b7
GL
25#endif
26
ca7731b7
GL
27#include "wx/process.h"
28
50567b69
VZ
29// ----------------------------------------------------------------------------
30// event tables and such
31// ----------------------------------------------------------------------------
32
2e4df4bf
VZ
33DEFINE_EVENT_TYPE(wxEVT_END_PROCESS)
34
eebb848a
KB
35IMPLEMENT_DYNAMIC_CLASS(wxProcess, wxEvtHandler)
36IMPLEMENT_DYNAMIC_CLASS(wxProcessEvent, wxEvent)
ca7731b7 37
50567b69
VZ
38// ============================================================================
39// wxProcess implementation
40// ============================================================================
41
42// ----------------------------------------------------------------------------
43// wxProcess creation
44// ----------------------------------------------------------------------------
45
da00a8bb 46void wxProcess::Init(wxEvtHandler *parent, int id, int flags)
ca7731b7 47{
cd6ce4a9 48 if ( parent )
7e84f02d 49 SetNextHandler(parent);
ca7731b7 50
8b33ae2d 51 m_id = id;
a387938f 52 m_pid = 0;
da00a8bb 53 m_redirect = (flags & wxPROCESS_REDIRECT) != 0;
f6bcfd97
BP
54
55#if wxUSE_STREAMS
cd6ce4a9 56 m_inputStream = NULL;
f6bcfd97 57 m_errorStream = NULL;
cd6ce4a9 58 m_outputStream = NULL;
f6bcfd97 59#endif // wxUSE_STREAMS
8b33ae2d
GL
60}
61
da00a8bb 62/* static */
e626d7c7 63wxProcess *wxProcess::Open(const wxString& cmd, int flags)
da00a8bb 64{
feb6cde4 65 wxASSERT_MSG( !(flags & wxEXEC_SYNC), wxT("wxEXEC_SYNC should not be used." ));
da00a8bb 66 wxProcess *process = new wxProcess(wxPROCESS_REDIRECT);
a387938f
JS
67 long pid = wxExecute(cmd, flags, process);
68 if( !pid )
da00a8bb
VZ
69 {
70 // couldn't launch the process
71 delete process;
72 return NULL;
73 }
74
a387938f
JS
75 process->SetPid(pid);
76
da00a8bb
VZ
77 return process;
78}
79
50567b69
VZ
80// ----------------------------------------------------------------------------
81// wxProcess termination
82// ----------------------------------------------------------------------------
83
8b33ae2d
GL
84wxProcess::~wxProcess()
85{
f6bcfd97 86#if wxUSE_STREAMS
cd6ce4a9 87 delete m_inputStream;
f6bcfd97 88 delete m_errorStream;
cd6ce4a9 89 delete m_outputStream;
f6bcfd97 90#endif // wxUSE_STREAMS
ca7731b7
GL
91}
92
5336ece4 93void wxProcess::OnTerminate(int pid, int status)
81d66cf3 94{
7e84f02d 95 wxProcessEvent event(m_id, pid, status);
ca7731b7 96
7e84f02d
VZ
97 if ( !ProcessEvent(event) )
98 delete this;
99 //else: the object which processed the event is responsible for deleting
100 // us!
101}
102
103void wxProcess::Detach()
104{
105 SetNextHandler(NULL);
ca7731b7 106}
8b33ae2d 107
50567b69
VZ
108// ----------------------------------------------------------------------------
109// process IO redirection
110// ----------------------------------------------------------------------------
111
f6bcfd97
BP
112#if wxUSE_STREAMS
113
114void wxProcess::SetPipeStreams(wxInputStream *inputSstream,
115 wxOutputStream *outputStream,
116 wxInputStream *errorStream)
8b33ae2d 117{
f6bcfd97
BP
118 m_inputStream = inputSstream;
119 m_errorStream = errorStream;
120 m_outputStream = outputStream;
8b33ae2d
GL
121}
122
411165f3
VZ
123bool wxProcess::IsInputOpened() const
124{
2b5f62a0 125 return m_inputStream && m_inputStream->GetLastError() != wxSTREAM_EOF;
411165f3
VZ
126}
127
128bool wxProcess::IsInputAvailable() const
129{
2b5f62a0 130 return m_inputStream && m_inputStream->CanRead();
411165f3
VZ
131}
132
133bool wxProcess::IsErrorAvailable() const
134{
2b5f62a0 135 return m_errorStream && m_errorStream->CanRead();
411165f3
VZ
136}
137
f6bcfd97 138#endif // wxUSE_STREAMS
50567b69
VZ
139
140// ----------------------------------------------------------------------------
141// process killing
142// ----------------------------------------------------------------------------
143
144/* static */
e0f6b731 145wxKillError wxProcess::Kill(int pid, wxSignal sig, int flags)
50567b69
VZ
146{
147 wxKillError rc;
e0f6b731 148 (void)wxKill(pid, sig, &rc, flags);
50567b69
VZ
149
150 return rc;
151}
152
153/* static */
154bool wxProcess::Exists(int pid)
155{
15b663b4 156 switch ( Kill(pid, wxSIGNONE) )
50567b69
VZ
157 {
158 case wxKILL_OK:
159 case wxKILL_ACCESS_DENIED:
7e548f6b 160 return true;
50567b69
VZ
161
162 default:
163 case wxKILL_ERROR:
164 case wxKILL_BAD_SIGNAL:
165 wxFAIL_MSG( _T("unexpected wxProcess::Kill() return code") );
166 // fall through
167
168 case wxKILL_NO_PROCESS:
7e548f6b 169 return false;
50567b69
VZ
170 }
171}
172