]>
Commit | Line | Data |
---|---|---|
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 | |
7e84f02d | 9 | // Licence: wxWindows license |
ca7731b7 GL |
10 | ///////////////////////////////////////////////////////////////////////////// |
11 | ||
50567b69 VZ |
12 | // ============================================================================ |
13 | // declarations | |
14 | // ============================================================================ | |
15 | ||
16 | // ---------------------------------------------------------------------------- | |
17 | // headers | |
18 | // ---------------------------------------------------------------------------- | |
19 | ||
ca7731b7 | 20 | #ifdef __GNUG__ |
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 |
37 | DEFINE_EVENT_TYPE(wxEVT_END_PROCESS) |
38 | ||
eebb848a KB |
39 | IMPLEMENT_DYNAMIC_CLASS(wxProcess, wxEvtHandler) |
40 | IMPLEMENT_DYNAMIC_CLASS(wxProcessEvent, wxEvent) | |
ca7731b7 | 41 | |
50567b69 VZ |
42 | // ============================================================================ |
43 | // wxProcess implementation | |
44 | // ============================================================================ | |
45 | ||
46 | // ---------------------------------------------------------------------------- | |
47 | // wxProcess creation | |
48 | // ---------------------------------------------------------------------------- | |
49 | ||
da00a8bb | 50 | void wxProcess::Init(wxEvtHandler *parent, int id, int flags) |
ca7731b7 | 51 | { |
cd6ce4a9 | 52 | if ( parent ) |
7e84f02d | 53 | SetNextHandler(parent); |
ca7731b7 | 54 | |
8b33ae2d | 55 | m_id = id; |
da00a8bb | 56 | m_redirect = (flags & wxPROCESS_REDIRECT) != 0; |
f6bcfd97 BP |
57 | |
58 | #if wxUSE_STREAMS | |
cd6ce4a9 | 59 | m_inputStream = NULL; |
f6bcfd97 | 60 | m_errorStream = NULL; |
cd6ce4a9 | 61 | m_outputStream = NULL; |
f6bcfd97 | 62 | #endif // wxUSE_STREAMS |
8b33ae2d GL |
63 | } |
64 | ||
da00a8bb | 65 | /* static */ |
e626d7c7 | 66 | wxProcess *wxProcess::Open(const wxString& cmd, int flags) |
da00a8bb | 67 | { |
feb6cde4 | 68 | wxASSERT_MSG( !(flags & wxEXEC_SYNC), wxT("wxEXEC_SYNC should not be used." )); |
da00a8bb | 69 | wxProcess *process = new wxProcess(wxPROCESS_REDIRECT); |
e626d7c7 | 70 | if ( !wxExecute(cmd, flags, process) ) |
da00a8bb VZ |
71 | { |
72 | // couldn't launch the process | |
73 | delete process; | |
74 | return NULL; | |
75 | } | |
76 | ||
77 | return process; | |
78 | } | |
79 | ||
50567b69 VZ |
80 | // ---------------------------------------------------------------------------- |
81 | // wxProcess termination | |
82 | // ---------------------------------------------------------------------------- | |
83 | ||
8b33ae2d GL |
84 | wxProcess::~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 | 93 | void 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 | ||
103 | void 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 | ||
114 | void 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 | ||
f6bcfd97 | 123 | #endif // wxUSE_STREAMS |
50567b69 VZ |
124 | |
125 | // ---------------------------------------------------------------------------- | |
126 | // process killing | |
127 | // ---------------------------------------------------------------------------- | |
128 | ||
129 | /* static */ | |
130 | wxKillError wxProcess::Kill(int pid, wxSignal sig) | |
131 | { | |
132 | wxKillError rc; | |
133 | (void)wxKill(pid, sig, &rc); | |
134 | ||
135 | return rc; | |
136 | } | |
137 | ||
138 | /* static */ | |
139 | bool wxProcess::Exists(int pid) | |
140 | { | |
15b663b4 | 141 | switch ( Kill(pid, wxSIGNONE) ) |
50567b69 VZ |
142 | { |
143 | case wxKILL_OK: | |
144 | case wxKILL_ACCESS_DENIED: | |
145 | return TRUE; | |
146 | ||
147 | default: | |
148 | case wxKILL_ERROR: | |
149 | case wxKILL_BAD_SIGNAL: | |
150 | wxFAIL_MSG( _T("unexpected wxProcess::Kill() return code") ); | |
151 | // fall through | |
152 | ||
153 | case wxKILL_NO_PROCESS: | |
154 | return FALSE; | |
155 | } | |
156 | } | |
157 |