]>
Commit | Line | Data |
---|---|---|
1 | ///////////////////////////////////////////////////////////////////////////// | |
2 | // Name: src/common/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 | // Copyright: (c) Guilhem Lavaux | |
8 | // Licence: wxWindows licence | |
9 | ///////////////////////////////////////////////////////////////////////////// | |
10 | ||
11 | // ============================================================================ | |
12 | // declarations | |
13 | // ============================================================================ | |
14 | ||
15 | // ---------------------------------------------------------------------------- | |
16 | // headers | |
17 | // ---------------------------------------------------------------------------- | |
18 | ||
19 | // For compilers that support precompilation, includes "wx.h". | |
20 | #include "wx/wxprec.h" | |
21 | ||
22 | #ifdef __BORLANDC__ | |
23 | #pragma hdrstop | |
24 | #endif | |
25 | ||
26 | #include "wx/process.h" | |
27 | ||
28 | // ---------------------------------------------------------------------------- | |
29 | // event tables and such | |
30 | // ---------------------------------------------------------------------------- | |
31 | ||
32 | wxDEFINE_EVENT( wxEVT_END_PROCESS, wxProcessEvent ); | |
33 | ||
34 | IMPLEMENT_DYNAMIC_CLASS(wxProcess, wxEvtHandler) | |
35 | IMPLEMENT_DYNAMIC_CLASS(wxProcessEvent, wxEvent) | |
36 | ||
37 | // ============================================================================ | |
38 | // wxProcess implementation | |
39 | // ============================================================================ | |
40 | ||
41 | // ---------------------------------------------------------------------------- | |
42 | // wxProcess creation | |
43 | // ---------------------------------------------------------------------------- | |
44 | ||
45 | void wxProcess::Init(wxEvtHandler *parent, int id, int flags) | |
46 | { | |
47 | if ( parent ) | |
48 | SetNextHandler(parent); | |
49 | ||
50 | m_id = id; | |
51 | m_pid = 0; | |
52 | m_priority = wxPRIORITY_DEFAULT; | |
53 | m_redirect = (flags & wxPROCESS_REDIRECT) != 0; | |
54 | ||
55 | #if wxUSE_STREAMS | |
56 | m_inputStream = NULL; | |
57 | m_errorStream = NULL; | |
58 | m_outputStream = NULL; | |
59 | #endif // wxUSE_STREAMS | |
60 | } | |
61 | ||
62 | /* static */ | |
63 | wxProcess *wxProcess::Open(const wxString& cmd, int flags) | |
64 | { | |
65 | wxASSERT_MSG( !(flags & wxEXEC_SYNC), wxT("wxEXEC_SYNC should not be used." )); | |
66 | wxProcess *process = new wxProcess(wxPROCESS_REDIRECT); | |
67 | long pid = wxExecute(cmd, flags, process); | |
68 | if( !pid ) | |
69 | { | |
70 | // couldn't launch the process | |
71 | delete process; | |
72 | return NULL; | |
73 | } | |
74 | ||
75 | process->SetPid(pid); | |
76 | ||
77 | return process; | |
78 | } | |
79 | ||
80 | // ---------------------------------------------------------------------------- | |
81 | // wxProcess termination | |
82 | // ---------------------------------------------------------------------------- | |
83 | ||
84 | wxProcess::~wxProcess() | |
85 | { | |
86 | #if wxUSE_STREAMS | |
87 | delete m_inputStream; | |
88 | delete m_errorStream; | |
89 | delete m_outputStream; | |
90 | #endif // wxUSE_STREAMS | |
91 | } | |
92 | ||
93 | void wxProcess::OnTerminate(int pid, int status) | |
94 | { | |
95 | wxProcessEvent event(m_id, pid, status); | |
96 | ||
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 | // we just detach from the next handler of the chain (i.e. our "parent" -- see ctor) | |
106 | // not also from the previous handler like wxEvtHandler::Unlink() would do: | |
107 | ||
108 | if (m_nextHandler) | |
109 | m_nextHandler->SetPreviousHandler(m_previousHandler); | |
110 | ||
111 | m_nextHandler = NULL; | |
112 | } | |
113 | ||
114 | // ---------------------------------------------------------------------------- | |
115 | // process IO redirection | |
116 | // ---------------------------------------------------------------------------- | |
117 | ||
118 | #if wxUSE_STREAMS | |
119 | ||
120 | void wxProcess::SetPipeStreams(wxInputStream *inputSstream, | |
121 | wxOutputStream *outputStream, | |
122 | wxInputStream *errorStream) | |
123 | { | |
124 | m_inputStream = inputSstream; | |
125 | m_errorStream = errorStream; | |
126 | m_outputStream = outputStream; | |
127 | } | |
128 | ||
129 | bool wxProcess::IsInputOpened() const | |
130 | { | |
131 | return m_inputStream && m_inputStream->GetLastError() != wxSTREAM_EOF; | |
132 | } | |
133 | ||
134 | bool wxProcess::IsInputAvailable() const | |
135 | { | |
136 | return m_inputStream && m_inputStream->CanRead(); | |
137 | } | |
138 | ||
139 | bool wxProcess::IsErrorAvailable() const | |
140 | { | |
141 | return m_errorStream && m_errorStream->CanRead(); | |
142 | } | |
143 | ||
144 | #endif // wxUSE_STREAMS | |
145 | ||
146 | // ---------------------------------------------------------------------------- | |
147 | // process killing | |
148 | // ---------------------------------------------------------------------------- | |
149 | ||
150 | /* static */ | |
151 | wxKillError wxProcess::Kill(int pid, wxSignal sig, int flags) | |
152 | { | |
153 | wxKillError rc; | |
154 | (void)wxKill(pid, sig, &rc, flags); | |
155 | ||
156 | return rc; | |
157 | } | |
158 | ||
159 | /* static */ | |
160 | bool wxProcess::Exists(int pid) | |
161 | { | |
162 | switch ( Kill(pid, wxSIGNONE) ) | |
163 | { | |
164 | case wxKILL_OK: | |
165 | case wxKILL_ACCESS_DENIED: | |
166 | return true; | |
167 | ||
168 | default: | |
169 | case wxKILL_ERROR: | |
170 | case wxKILL_BAD_SIGNAL: | |
171 | wxFAIL_MSG( wxT("unexpected wxProcess::Kill() return code") ); | |
172 | // fall through | |
173 | ||
174 | case wxKILL_NO_PROCESS: | |
175 | return false; | |
176 | } | |
177 | } | |
178 | ||
179 | void wxProcess::SetPriority(unsigned priority) | |
180 | { | |
181 | wxCHECK_RET( priority <= wxPRIORITY_MAX, | |
182 | wxS("Invalid process priority value.") ); | |
183 | ||
184 | m_priority = priority; | |
185 | } |