]>
Commit | Line | Data |
---|---|---|
ca7731b7 | 1 | ///////////////////////////////////////////////////////////////////////////// |
80fdcdb9 | 2 | // Name: src/common/process.cpp |
ca7731b7 GL |
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 | ||
9b11752c | 33 | wxDEFINE_EVENT( wxEVT_END_PROCESS, wxProcessEvent ); |
2e4df4bf | 34 | |
eebb848a KB |
35 | IMPLEMENT_DYNAMIC_CLASS(wxProcess, wxEvtHandler) |
36 | IMPLEMENT_DYNAMIC_CLASS(wxProcessEvent, wxEvent) | |
ca7731b7 | 37 | |
50567b69 VZ |
38 | // ============================================================================ |
39 | // wxProcess implementation | |
40 | // ============================================================================ | |
41 | ||
42 | // ---------------------------------------------------------------------------- | |
43 | // wxProcess creation | |
44 | // ---------------------------------------------------------------------------- | |
45 | ||
da00a8bb | 46 | void 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; |
eaf4bde6 | 53 | m_priority = wxPRIORITY_DEFAULT; |
da00a8bb | 54 | m_redirect = (flags & wxPROCESS_REDIRECT) != 0; |
f6bcfd97 BP |
55 | |
56 | #if wxUSE_STREAMS | |
cd6ce4a9 | 57 | m_inputStream = NULL; |
f6bcfd97 | 58 | m_errorStream = NULL; |
cd6ce4a9 | 59 | m_outputStream = NULL; |
f6bcfd97 | 60 | #endif // wxUSE_STREAMS |
8b33ae2d GL |
61 | } |
62 | ||
da00a8bb | 63 | /* static */ |
e626d7c7 | 64 | wxProcess *wxProcess::Open(const wxString& cmd, int flags) |
da00a8bb | 65 | { |
feb6cde4 | 66 | wxASSERT_MSG( !(flags & wxEXEC_SYNC), wxT("wxEXEC_SYNC should not be used." )); |
da00a8bb | 67 | wxProcess *process = new wxProcess(wxPROCESS_REDIRECT); |
a387938f JS |
68 | long pid = wxExecute(cmd, flags, process); |
69 | if( !pid ) | |
da00a8bb VZ |
70 | { |
71 | // couldn't launch the process | |
72 | delete process; | |
73 | return NULL; | |
74 | } | |
75 | ||
a387938f JS |
76 | process->SetPid(pid); |
77 | ||
da00a8bb VZ |
78 | return process; |
79 | } | |
80 | ||
50567b69 VZ |
81 | // ---------------------------------------------------------------------------- |
82 | // wxProcess termination | |
83 | // ---------------------------------------------------------------------------- | |
84 | ||
8b33ae2d GL |
85 | wxProcess::~wxProcess() |
86 | { | |
f6bcfd97 | 87 | #if wxUSE_STREAMS |
cd6ce4a9 | 88 | delete m_inputStream; |
f6bcfd97 | 89 | delete m_errorStream; |
cd6ce4a9 | 90 | delete m_outputStream; |
f6bcfd97 | 91 | #endif // wxUSE_STREAMS |
ca7731b7 GL |
92 | } |
93 | ||
5336ece4 | 94 | void wxProcess::OnTerminate(int pid, int status) |
81d66cf3 | 95 | { |
7e84f02d | 96 | wxProcessEvent event(m_id, pid, status); |
ca7731b7 | 97 | |
7e84f02d VZ |
98 | if ( !ProcessEvent(event) ) |
99 | delete this; | |
100 | //else: the object which processed the event is responsible for deleting | |
101 | // us! | |
102 | } | |
103 | ||
104 | void wxProcess::Detach() | |
105 | { | |
ca5016d4 FM |
106 | // we just detach from the next handler of the chain (i.e. our "parent" -- see ctor) |
107 | // not also from the previous handler like wxEvtHandler::Unlink() would do: | |
03647350 | 108 | |
ca5016d4 FM |
109 | if (m_nextHandler) |
110 | m_nextHandler->SetPreviousHandler(m_previousHandler); | |
111 | ||
112 | m_nextHandler = NULL; | |
ca7731b7 | 113 | } |
8b33ae2d | 114 | |
50567b69 VZ |
115 | // ---------------------------------------------------------------------------- |
116 | // process IO redirection | |
117 | // ---------------------------------------------------------------------------- | |
118 | ||
f6bcfd97 BP |
119 | #if wxUSE_STREAMS |
120 | ||
121 | void wxProcess::SetPipeStreams(wxInputStream *inputSstream, | |
122 | wxOutputStream *outputStream, | |
123 | wxInputStream *errorStream) | |
8b33ae2d | 124 | { |
f6bcfd97 BP |
125 | m_inputStream = inputSstream; |
126 | m_errorStream = errorStream; | |
127 | m_outputStream = outputStream; | |
8b33ae2d GL |
128 | } |
129 | ||
411165f3 VZ |
130 | bool wxProcess::IsInputOpened() const |
131 | { | |
2b5f62a0 | 132 | return m_inputStream && m_inputStream->GetLastError() != wxSTREAM_EOF; |
411165f3 VZ |
133 | } |
134 | ||
135 | bool wxProcess::IsInputAvailable() const | |
136 | { | |
2b5f62a0 | 137 | return m_inputStream && m_inputStream->CanRead(); |
411165f3 VZ |
138 | } |
139 | ||
140 | bool wxProcess::IsErrorAvailable() const | |
141 | { | |
2b5f62a0 | 142 | return m_errorStream && m_errorStream->CanRead(); |
411165f3 VZ |
143 | } |
144 | ||
f6bcfd97 | 145 | #endif // wxUSE_STREAMS |
50567b69 VZ |
146 | |
147 | // ---------------------------------------------------------------------------- | |
148 | // process killing | |
149 | // ---------------------------------------------------------------------------- | |
150 | ||
151 | /* static */ | |
e0f6b731 | 152 | wxKillError wxProcess::Kill(int pid, wxSignal sig, int flags) |
50567b69 VZ |
153 | { |
154 | wxKillError rc; | |
e0f6b731 | 155 | (void)wxKill(pid, sig, &rc, flags); |
50567b69 VZ |
156 | |
157 | return rc; | |
158 | } | |
159 | ||
160 | /* static */ | |
161 | bool wxProcess::Exists(int pid) | |
162 | { | |
15b663b4 | 163 | switch ( Kill(pid, wxSIGNONE) ) |
50567b69 VZ |
164 | { |
165 | case wxKILL_OK: | |
166 | case wxKILL_ACCESS_DENIED: | |
7e548f6b | 167 | return true; |
50567b69 VZ |
168 | |
169 | default: | |
170 | case wxKILL_ERROR: | |
171 | case wxKILL_BAD_SIGNAL: | |
9a83f860 | 172 | wxFAIL_MSG( wxT("unexpected wxProcess::Kill() return code") ); |
50567b69 VZ |
173 | // fall through |
174 | ||
175 | case wxKILL_NO_PROCESS: | |
7e548f6b | 176 | return false; |
50567b69 VZ |
177 | } |
178 | } | |
179 | ||
eaf4bde6 VZ |
180 | void wxProcess::SetPriority(unsigned priority) |
181 | { | |
8f9e2a55 | 182 | wxCHECK_RET( priority <= wxPRIORITY_MAX, |
eaf4bde6 VZ |
183 | wxS("Invalid process priority value.") ); |
184 | ||
185 | m_priority = priority; | |
186 | } |