]> git.saurik.com Git - wxWidgets.git/blame - src/common/process.cpp
don't use %08p format string, gcc complains about it
[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
2d67974d
WS
46#if WXWIN_COMPATIBILITY_2_2
47
48wxProcess::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
da00a8bb 55void wxProcess::Init(wxEvtHandler *parent, int id, int flags)
ca7731b7 56{
cd6ce4a9 57 if ( parent )
7e84f02d 58 SetNextHandler(parent);
ca7731b7 59
8b33ae2d 60 m_id = id;
da00a8bb 61 m_redirect = (flags & wxPROCESS_REDIRECT) != 0;
f6bcfd97
BP
62
63#if wxUSE_STREAMS
cd6ce4a9 64 m_inputStream = NULL;
f6bcfd97 65 m_errorStream = NULL;
cd6ce4a9 66 m_outputStream = NULL;
f6bcfd97 67#endif // wxUSE_STREAMS
8b33ae2d
GL
68}
69
da00a8bb 70/* static */
e626d7c7 71wxProcess *wxProcess::Open(const wxString& cmd, int flags)
da00a8bb 72{
feb6cde4 73 wxASSERT_MSG( !(flags & wxEXEC_SYNC), wxT("wxEXEC_SYNC should not be used." ));
da00a8bb 74 wxProcess *process = new wxProcess(wxPROCESS_REDIRECT);
e626d7c7 75 if ( !wxExecute(cmd, flags, process) )
da00a8bb
VZ
76 {
77 // couldn't launch the process
78 delete process;
79 return NULL;
80 }
81
82 return process;
83}
84
50567b69
VZ
85// ----------------------------------------------------------------------------
86// wxProcess termination
87// ----------------------------------------------------------------------------
88
8b33ae2d
GL
89wxProcess::~wxProcess()
90{
f6bcfd97 91#if wxUSE_STREAMS
cd6ce4a9 92 delete m_inputStream;
f6bcfd97 93 delete m_errorStream;
cd6ce4a9 94 delete m_outputStream;
f6bcfd97 95#endif // wxUSE_STREAMS
ca7731b7
GL
96}
97
5336ece4 98void wxProcess::OnTerminate(int pid, int status)
81d66cf3 99{
7e84f02d 100 wxProcessEvent event(m_id, pid, status);
ca7731b7 101
7e84f02d
VZ
102 if ( !ProcessEvent(event) )
103 delete this;
104 //else: the object which processed the event is responsible for deleting
105 // us!
106}
107
108void wxProcess::Detach()
109{
110 SetNextHandler(NULL);
ca7731b7 111}
8b33ae2d 112
50567b69
VZ
113// ----------------------------------------------------------------------------
114// process IO redirection
115// ----------------------------------------------------------------------------
116
f6bcfd97
BP
117#if wxUSE_STREAMS
118
119void wxProcess::SetPipeStreams(wxInputStream *inputSstream,
120 wxOutputStream *outputStream,
121 wxInputStream *errorStream)
8b33ae2d 122{
f6bcfd97
BP
123 m_inputStream = inputSstream;
124 m_errorStream = errorStream;
125 m_outputStream = outputStream;
8b33ae2d
GL
126}
127
411165f3
VZ
128bool wxProcess::IsInputOpened() const
129{
2b5f62a0 130 return m_inputStream && m_inputStream->GetLastError() != wxSTREAM_EOF;
411165f3
VZ
131}
132
133bool wxProcess::IsInputAvailable() const
134{
2b5f62a0 135 return m_inputStream && m_inputStream->CanRead();
411165f3
VZ
136}
137
138bool wxProcess::IsErrorAvailable() const
139{
2b5f62a0 140 return m_errorStream && m_errorStream->CanRead();
411165f3
VZ
141}
142
f6bcfd97 143#endif // wxUSE_STREAMS
50567b69
VZ
144
145// ----------------------------------------------------------------------------
146// process killing
147// ----------------------------------------------------------------------------
148
149/* static */
e0f6b731 150wxKillError wxProcess::Kill(int pid, wxSignal sig, int flags)
50567b69
VZ
151{
152 wxKillError rc;
e0f6b731 153 (void)wxKill(pid, sig, &rc, flags);
50567b69
VZ
154
155 return rc;
156}
157
158/* static */
159bool wxProcess::Exists(int pid)
160{
15b663b4 161 switch ( Kill(pid, wxSIGNONE) )
50567b69
VZ
162 {
163 case wxKILL_OK:
164 case wxKILL_ACCESS_DENIED:
7e548f6b 165 return true;
50567b69
VZ
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:
7e548f6b 174 return false;
50567b69
VZ
175 }
176}
177