Changed wxProcess::Open to take a flags arg to pass to wxExecute.
[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 wxProcess *process = new wxProcess(wxPROCESS_REDIRECT);
69 if ( !wxExecute(cmd, flags, process) )
70 {
71 // couldn't launch the process
72 delete process;
73 return NULL;
74 }
75
76 return process;
77 }
78
79 // ----------------------------------------------------------------------------
80 // wxProcess termination
81 // ----------------------------------------------------------------------------
82
83 wxProcess::~wxProcess()
84 {
85 #if wxUSE_STREAMS
86 delete m_inputStream;
87 delete m_errorStream;
88 delete m_outputStream;
89 #endif // wxUSE_STREAMS
90 }
91
92 void wxProcess::OnTerminate(int pid, int status)
93 {
94 wxProcessEvent event(m_id, pid, status);
95
96 if ( !ProcessEvent(event) )
97 delete this;
98 //else: the object which processed the event is responsible for deleting
99 // us!
100 }
101
102 void wxProcess::Detach()
103 {
104 SetNextHandler(NULL);
105 }
106
107 // ----------------------------------------------------------------------------
108 // process IO redirection
109 // ----------------------------------------------------------------------------
110
111 #if wxUSE_STREAMS
112
113 void wxProcess::SetPipeStreams(wxInputStream *inputSstream,
114 wxOutputStream *outputStream,
115 wxInputStream *errorStream)
116 {
117 m_inputStream = inputSstream;
118 m_errorStream = errorStream;
119 m_outputStream = outputStream;
120 }
121
122 #endif // wxUSE_STREAMS
123
124 // ----------------------------------------------------------------------------
125 // process killing
126 // ----------------------------------------------------------------------------
127
128 /* static */
129 wxKillError wxProcess::Kill(int pid, wxSignal sig)
130 {
131 wxKillError rc;
132 (void)wxKill(pid, sig, &rc);
133
134 return rc;
135 }
136
137 /* static */
138 bool wxProcess::Exists(int pid)
139 {
140 switch ( Kill(pid, wxSIGNONE) )
141 {
142 case wxKILL_OK:
143 case wxKILL_ACCESS_DENIED:
144 return TRUE;
145
146 default:
147 case wxKILL_ERROR:
148 case wxKILL_BAD_SIGNAL:
149 wxFAIL_MSG( _T("unexpected wxProcess::Kill() return code") );
150 // fall through
151
152 case wxKILL_NO_PROCESS:
153 return FALSE;
154 }
155 }
156