]>
Commit | Line | Data |
---|---|---|
69c33c6c VZ |
1 | ///////////////////////////////////////////////////////////////////////////// |
2 | // Name: exec.cpp | |
3 | // Purpose: exec sample demonstrates wxExecute and related functions | |
4 | // Author: Vadim Zeitlin | |
5 | // Modified by: | |
6 | // Created: 15.01.00 | |
7 | // RCS-ID: $Id$ | |
8 | // Copyright: (c) Vadim Zeitlin | |
9 | // Licence: wxWindows licence | |
10 | ///////////////////////////////////////////////////////////////////////////// | |
11 | ||
12 | // ============================================================================ | |
13 | // declarations | |
14 | // ============================================================================ | |
15 | ||
16 | // ---------------------------------------------------------------------------- | |
17 | // headers | |
18 | // ---------------------------------------------------------------------------- | |
19 | ||
69c33c6c VZ |
20 | // For compilers that support precompilation, includes "wx/wx.h". |
21 | #include "wx/wxprec.h" | |
22 | ||
23 | #ifdef __BORLANDC__ | |
24 | #pragma hdrstop | |
25 | #endif | |
26 | ||
27 | // for all others, include the necessary headers (this file is usually all you | |
be5a51fb | 28 | // need because it includes almost all "standard" wxWidgets headers |
69c33c6c VZ |
29 | #ifndef WX_PRECOMP |
30 | #include "wx/app.h" | |
788233da | 31 | #include "wx/log.h" |
69c33c6c | 32 | #include "wx/frame.h" |
92a2a7eb VZ |
33 | #include "wx/panel.h" |
34 | ||
35 | #include "wx/timer.h" | |
eb671557 | 36 | |
69c33c6c | 37 | #include "wx/utils.h" |
a8f0faf3 | 38 | #include "wx/menu.h" |
eb671557 | 39 | |
a8f0faf3 GRG |
40 | #include "wx/msgdlg.h" |
41 | #include "wx/textdlg.h" | |
8ce88dfa | 42 | #include "wx/filedlg.h" |
50567b69 | 43 | #include "wx/choicdlg.h" |
eb671557 VZ |
44 | |
45 | #include "wx/button.h" | |
46 | #include "wx/textctrl.h" | |
47 | #include "wx/listbox.h" | |
48 | ||
49 | #include "wx/sizer.h" | |
69c33c6c VZ |
50 | #endif |
51 | ||
d8e41d42 | 52 | #include "wx/txtstrm.h" |
e4f3eb42 | 53 | #include "wx/numdlg.h" |
ba59de5d | 54 | #include "wx/textdlg.h" |
1a278e7b | 55 | #include "wx/ffile.h" |
d8e41d42 | 56 | |
69c33c6c VZ |
57 | #include "wx/process.h" |
58 | ||
6ba63600 VZ |
59 | #include "wx/mimetype.h" |
60 | ||
d93c719a VZ |
61 | #ifdef __WINDOWS__ |
62 | #include "wx/dde.h" | |
63 | #endif // __WINDOWS__ | |
64 | ||
69c33c6c | 65 | // ---------------------------------------------------------------------------- |
eb671557 | 66 | // the usual application and main frame classes |
69c33c6c VZ |
67 | // ---------------------------------------------------------------------------- |
68 | ||
69 | // Define a new application type, each program should derive a class from wxApp | |
70 | class MyApp : public wxApp | |
71 | { | |
72 | public: | |
73 | // override base class virtuals | |
74 | // ---------------------------- | |
75 | ||
76 | // this one is called on application startup and is a good place for the app | |
77 | // initialization (doing it here and not in the ctor allows to have an error | |
78 | // return: if OnInit() returns false, the application terminates) | |
79 | virtual bool OnInit(); | |
80 | }; | |
81 | ||
cd6ce4a9 VZ |
82 | // Define an array of process pointers used by MyFrame |
83 | class MyPipedProcess; | |
5a8561fc VZ |
84 | WX_DEFINE_ARRAY_PTR(MyPipedProcess *, MyPipedProcessesArray); |
85 | ||
86 | class MyProcess; | |
87 | WX_DEFINE_ARRAY_PTR(MyProcess *, MyProcessesArray); | |
cd6ce4a9 | 88 | |
69c33c6c VZ |
89 | // Define a new frame type: this is going to be our main frame |
90 | class MyFrame : public wxFrame | |
91 | { | |
92 | public: | |
5a8561fc | 93 | // ctor and dtor |
69c33c6c | 94 | MyFrame(const wxString& title, const wxPoint& pos, const wxSize& size); |
5a8561fc | 95 | virtual ~MyFrame(); |
69c33c6c VZ |
96 | |
97 | // event handlers (these functions should _not_ be virtual) | |
98 | void OnQuit(wxCommandEvent& event); | |
99 | ||
50567b69 VZ |
100 | void OnKill(wxCommandEvent& event); |
101 | ||
d8e41d42 VZ |
102 | void OnClear(wxCommandEvent& event); |
103 | ||
c6f9dfe8 VZ |
104 | void OnBeginBusyCursor(wxCommandEvent& event); |
105 | void OnEndBusyCursor(wxCommandEvent& event); | |
106 | ||
69c33c6c | 107 | void OnSyncExec(wxCommandEvent& event); |
9b6b9e0c | 108 | void OnSyncNoEventsExec(wxCommandEvent& event); |
69c33c6c VZ |
109 | void OnAsyncExec(wxCommandEvent& event); |
110 | void OnShell(wxCommandEvent& event); | |
d8e41d42 | 111 | void OnExecWithRedirect(wxCommandEvent& event); |
f6bcfd97 BP |
112 | void OnExecWithPipe(wxCommandEvent& event); |
113 | ||
eb671557 VZ |
114 | void OnPOpen(wxCommandEvent& event); |
115 | ||
6ba63600 | 116 | void OnFileExec(wxCommandEvent& event); |
ba59de5d | 117 | void OnOpenURL(wxCommandEvent& event); |
6ba63600 | 118 | |
69c33c6c VZ |
119 | void OnAbout(wxCommandEvent& event); |
120 | ||
cd6ce4a9 | 121 | // polling output of async processes |
92a2a7eb | 122 | void OnTimer(wxTimerEvent& event); |
cd6ce4a9 VZ |
123 | void OnIdle(wxIdleEvent& event); |
124 | ||
d8e41d42 | 125 | // for MyPipedProcess |
f6bcfd97 | 126 | void OnProcessTerminated(MyPipedProcess *process); |
d8e41d42 VZ |
127 | wxListBox *GetLogListBox() const { return m_lbox; } |
128 | ||
5a8561fc VZ |
129 | // for MyProcess |
130 | void OnAsyncTermination(MyProcess *process); | |
131 | ||
69c33c6c | 132 | private: |
f6bcfd97 BP |
133 | void ShowOutput(const wxString& cmd, |
134 | const wxArrayString& output, | |
135 | const wxString& title); | |
136 | ||
6ba63600 VZ |
137 | void DoAsyncExec(const wxString& cmd); |
138 | ||
5a8561fc | 139 | void AddAsyncProcess(MyProcess *process) { m_allAsync.push_back(process); } |
92a2a7eb | 140 | |
5a8561fc VZ |
141 | void AddPipedProcess(MyPipedProcess *process); |
142 | void RemovePipedProcess(MyPipedProcess *process); | |
92a2a7eb | 143 | |
92a2a7eb | 144 | |
50567b69 | 145 | // the PID of the last process we launched asynchronously |
aec18ff7 | 146 | long m_pidLast; |
50567b69 | 147 | |
ca289436 | 148 | // last command we executed |
69c33c6c VZ |
149 | wxString m_cmdLast; |
150 | ||
ca289436 VZ |
151 | #ifdef __WINDOWS__ |
152 | void OnDDEExec(wxCommandEvent& event); | |
153 | void OnDDERequest(wxCommandEvent& event); | |
154 | ||
155 | bool GetDDEServer(); | |
156 | ||
157 | // last params of a DDE transaction | |
158 | wxString m_server, | |
159 | m_topic, | |
160 | m_cmdDde; | |
161 | #endif // __WINDOWS__ | |
162 | ||
d8e41d42 VZ |
163 | wxListBox *m_lbox; |
164 | ||
5a8561fc VZ |
165 | // array of running processes with redirected IO |
166 | MyPipedProcessesArray m_running; | |
167 | ||
168 | // array of all asynchrously running processes | |
169 | MyProcessesArray m_allAsync; | |
cd6ce4a9 | 170 | |
92a2a7eb VZ |
171 | // the idle event wake up timer |
172 | wxTimer m_timerIdleWakeUp; | |
173 | ||
be5a51fb | 174 | // any class wishing to process wxWidgets events must use this macro |
69c33c6c VZ |
175 | DECLARE_EVENT_TABLE() |
176 | }; | |
177 | ||
eb671557 VZ |
178 | // ---------------------------------------------------------------------------- |
179 | // MyPipeFrame: allows the user to communicate with the child process | |
180 | // ---------------------------------------------------------------------------- | |
181 | ||
182 | class MyPipeFrame : public wxFrame | |
183 | { | |
184 | public: | |
185 | MyPipeFrame(wxFrame *parent, | |
186 | const wxString& cmd, | |
187 | wxProcess *process); | |
188 | ||
189 | protected: | |
a0b08655 VZ |
190 | void OnTextEnter(wxCommandEvent& WXUNUSED(event)) { DoSend(); } |
191 | void OnBtnSend(wxCommandEvent& WXUNUSED(event)) { DoSend(); } | |
1a278e7b | 192 | void OnBtnSendFile(wxCommandEvent& WXUNUSED(event)); |
a0b08655 | 193 | void OnBtnGet(wxCommandEvent& WXUNUSED(event)) { DoGet(); } |
1a278e7b | 194 | void OnBtnClose(wxCommandEvent& WXUNUSED(event)) { DoClose(); } |
eb671557 VZ |
195 | |
196 | void OnClose(wxCloseEvent& event); | |
197 | ||
7ca528cb VZ |
198 | void OnProcessTerm(wxProcessEvent& event); |
199 | ||
200 | void DoSend() | |
201 | { | |
1a278e7b VZ |
202 | wxString s(m_textOut->GetValue()); |
203 | s += _T('\n'); | |
204 | m_out.Write(s.c_str(), s.length()); | |
205 | m_textOut->Clear(); | |
7ca528cb VZ |
206 | |
207 | DoGet(); | |
208 | } | |
209 | ||
eb671557 | 210 | void DoGet(); |
1a278e7b | 211 | void DoClose(); |
eb671557 VZ |
212 | |
213 | private: | |
1a278e7b VZ |
214 | void DoGetFromStream(wxTextCtrl *text, wxInputStream& in); |
215 | void DisableInput(); | |
216 | void DisableOutput(); | |
217 | ||
218 | ||
eb671557 VZ |
219 | wxProcess *m_process; |
220 | ||
1a278e7b VZ |
221 | wxOutputStream &m_out; |
222 | wxInputStream &m_in, | |
223 | &m_err; | |
eb671557 | 224 | |
1a278e7b VZ |
225 | wxTextCtrl *m_textOut, |
226 | *m_textIn, | |
227 | *m_textErr; | |
eb671557 VZ |
228 | |
229 | DECLARE_EVENT_TABLE() | |
230 | }; | |
231 | ||
232 | // ---------------------------------------------------------------------------- | |
233 | // wxProcess-derived classes | |
234 | // ---------------------------------------------------------------------------- | |
235 | ||
69c33c6c VZ |
236 | // This is the handler for process termination events |
237 | class MyProcess : public wxProcess | |
238 | { | |
239 | public: | |
d8e41d42 | 240 | MyProcess(MyFrame *parent, const wxString& cmd) |
69c33c6c VZ |
241 | : wxProcess(parent), m_cmd(cmd) |
242 | { | |
243 | m_parent = parent; | |
244 | } | |
245 | ||
246 | // instead of overriding this virtual function we might as well process the | |
247 | // event from it in the frame class - this might be more convenient in some | |
248 | // cases | |
249 | virtual void OnTerminate(int pid, int status); | |
250 | ||
d8e41d42 VZ |
251 | protected: |
252 | MyFrame *m_parent; | |
69c33c6c VZ |
253 | wxString m_cmd; |
254 | }; | |
255 | ||
d8e41d42 VZ |
256 | // A specialization of MyProcess for redirecting the output |
257 | class MyPipedProcess : public MyProcess | |
258 | { | |
259 | public: | |
260 | MyPipedProcess(MyFrame *parent, const wxString& cmd) | |
261 | : MyProcess(parent, cmd) | |
262 | { | |
cd6ce4a9 | 263 | Redirect(); |
d8e41d42 VZ |
264 | } |
265 | ||
266 | virtual void OnTerminate(int pid, int status); | |
cd6ce4a9 | 267 | |
f6bcfd97 BP |
268 | virtual bool HasInput(); |
269 | }; | |
270 | ||
271 | // A version of MyPipedProcess which also sends input to the stdin of the | |
272 | // child process | |
273 | class MyPipedProcess2 : public MyPipedProcess | |
274 | { | |
275 | public: | |
276 | MyPipedProcess2(MyFrame *parent, const wxString& cmd, const wxString& input) | |
277 | : MyPipedProcess(parent, cmd), m_input(input) | |
278 | { | |
279 | } | |
280 | ||
281 | virtual bool HasInput(); | |
282 | ||
283 | private: | |
284 | wxString m_input; | |
d8e41d42 VZ |
285 | }; |
286 | ||
69c33c6c VZ |
287 | // ---------------------------------------------------------------------------- |
288 | // constants | |
289 | // ---------------------------------------------------------------------------- | |
290 | ||
291 | // IDs for the controls and the menu commands | |
292 | enum | |
293 | { | |
294 | // menu items | |
295 | Exec_Quit = 100, | |
50567b69 | 296 | Exec_Kill, |
d8e41d42 | 297 | Exec_ClearLog, |
c6f9dfe8 VZ |
298 | Exec_BeginBusyCursor, |
299 | Exec_EndBusyCursor, | |
69c33c6c | 300 | Exec_SyncExec = 200, |
9b6b9e0c | 301 | Exec_SyncNoEventsExec, |
69c33c6c VZ |
302 | Exec_AsyncExec, |
303 | Exec_Shell, | |
eb671557 | 304 | Exec_POpen, |
6ba63600 | 305 | Exec_OpenFile, |
ba59de5d | 306 | Exec_OpenURL, |
d93c719a | 307 | Exec_DDEExec, |
ca289436 | 308 | Exec_DDERequest, |
d8e41d42 | 309 | Exec_Redirect, |
f6bcfd97 | 310 | Exec_Pipe, |
eb671557 VZ |
311 | Exec_About = 300, |
312 | ||
313 | // control ids | |
314 | Exec_Btn_Send = 1000, | |
1a278e7b VZ |
315 | Exec_Btn_SendFile, |
316 | Exec_Btn_Get, | |
317 | Exec_Btn_Close | |
69c33c6c VZ |
318 | }; |
319 | ||
d93c719a VZ |
320 | static const wxChar *DIALOG_TITLE = _T("Exec sample"); |
321 | ||
69c33c6c | 322 | // ---------------------------------------------------------------------------- |
be5a51fb | 323 | // event tables and other macros for wxWidgets |
69c33c6c VZ |
324 | // ---------------------------------------------------------------------------- |
325 | ||
be5a51fb | 326 | // the event tables connect the wxWidgets events with the functions (event |
69c33c6c VZ |
327 | // handlers) which process them. It can be also done at run-time, but for the |
328 | // simple menu events like this the static method is much simpler. | |
329 | BEGIN_EVENT_TABLE(MyFrame, wxFrame) | |
330 | EVT_MENU(Exec_Quit, MyFrame::OnQuit) | |
50567b69 | 331 | EVT_MENU(Exec_Kill, MyFrame::OnKill) |
d8e41d42 | 332 | EVT_MENU(Exec_ClearLog, MyFrame::OnClear) |
c6f9dfe8 VZ |
333 | EVT_MENU(Exec_BeginBusyCursor, MyFrame::OnBeginBusyCursor) |
334 | EVT_MENU(Exec_EndBusyCursor, MyFrame::OnEndBusyCursor) | |
69c33c6c VZ |
335 | |
336 | EVT_MENU(Exec_SyncExec, MyFrame::OnSyncExec) | |
9b6b9e0c | 337 | EVT_MENU(Exec_SyncNoEventsExec, MyFrame::OnSyncNoEventsExec) |
69c33c6c VZ |
338 | EVT_MENU(Exec_AsyncExec, MyFrame::OnAsyncExec) |
339 | EVT_MENU(Exec_Shell, MyFrame::OnShell) | |
d8e41d42 | 340 | EVT_MENU(Exec_Redirect, MyFrame::OnExecWithRedirect) |
f6bcfd97 BP |
341 | EVT_MENU(Exec_Pipe, MyFrame::OnExecWithPipe) |
342 | ||
eb671557 VZ |
343 | EVT_MENU(Exec_POpen, MyFrame::OnPOpen) |
344 | ||
6ba63600 | 345 | EVT_MENU(Exec_OpenFile, MyFrame::OnFileExec) |
ba59de5d | 346 | EVT_MENU(Exec_OpenURL, MyFrame::OnOpenURL) |
6ba63600 | 347 | |
5af4b77f | 348 | #ifdef __WINDOWS__ |
d93c719a | 349 | EVT_MENU(Exec_DDEExec, MyFrame::OnDDEExec) |
ca289436 | 350 | EVT_MENU(Exec_DDERequest, MyFrame::OnDDERequest) |
5af4b77f | 351 | #endif // __WINDOWS__ |
eb671557 | 352 | |
69c33c6c | 353 | EVT_MENU(Exec_About, MyFrame::OnAbout) |
cd6ce4a9 VZ |
354 | |
355 | EVT_IDLE(MyFrame::OnIdle) | |
92a2a7eb | 356 | |
07850a49 | 357 | EVT_TIMER(wxID_ANY, MyFrame::OnTimer) |
69c33c6c VZ |
358 | END_EVENT_TABLE() |
359 | ||
eb671557 VZ |
360 | BEGIN_EVENT_TABLE(MyPipeFrame, wxFrame) |
361 | EVT_BUTTON(Exec_Btn_Send, MyPipeFrame::OnBtnSend) | |
1a278e7b | 362 | EVT_BUTTON(Exec_Btn_SendFile, MyPipeFrame::OnBtnSendFile) |
eb671557 | 363 | EVT_BUTTON(Exec_Btn_Get, MyPipeFrame::OnBtnGet) |
1a278e7b | 364 | EVT_BUTTON(Exec_Btn_Close, MyPipeFrame::OnBtnClose) |
eb671557 | 365 | |
07850a49 | 366 | EVT_TEXT_ENTER(wxID_ANY, MyPipeFrame::OnTextEnter) |
eb671557 VZ |
367 | |
368 | EVT_CLOSE(MyPipeFrame::OnClose) | |
7ca528cb | 369 | |
07850a49 | 370 | EVT_END_PROCESS(wxID_ANY, MyPipeFrame::OnProcessTerm) |
eb671557 VZ |
371 | END_EVENT_TABLE() |
372 | ||
be5a51fb | 373 | // Create a new application object: this macro will allow wxWidgets to create |
69c33c6c VZ |
374 | // the application object during program execution (it's better than using a |
375 | // static object for many reasons) and also declares the accessor function | |
376 | // wxGetApp() which will return the reference of the right type (i.e. MyApp and | |
377 | // not wxApp) | |
378 | IMPLEMENT_APP(MyApp) | |
379 | ||
380 | // ============================================================================ | |
381 | // implementation | |
382 | // ============================================================================ | |
383 | ||
384 | // ---------------------------------------------------------------------------- | |
385 | // the application class | |
386 | // ---------------------------------------------------------------------------- | |
387 | ||
388 | // `Main program' equivalent: the program execution "starts" here | |
389 | bool MyApp::OnInit() | |
390 | { | |
45e6e6f8 VZ |
391 | if ( !wxApp::OnInit() ) |
392 | return false; | |
393 | ||
69c33c6c | 394 | // Create the main application window |
be5a51fb | 395 | MyFrame *frame = new MyFrame(_T("Exec wxWidgets sample"), |
69c33c6c VZ |
396 | wxDefaultPosition, wxSize(500, 140)); |
397 | ||
398 | // Show it and tell the application that it's our main window | |
07850a49 | 399 | frame->Show(true); |
69c33c6c VZ |
400 | SetTopWindow(frame); |
401 | ||
402 | // success: wxApp::OnRun() will be called which will enter the main message | |
07850a49 | 403 | // loop and the application will run. If we returned false here, the |
69c33c6c | 404 | // application would exit immediately. |
07850a49 | 405 | return true; |
69c33c6c VZ |
406 | } |
407 | ||
408 | // ---------------------------------------------------------------------------- | |
409 | // main frame | |
410 | // ---------------------------------------------------------------------------- | |
411 | ||
2b5f62a0 VZ |
412 | #ifdef __VISUALC__ |
413 | #pragma warning(disable: 4355) // this used in base member initializer list | |
414 | #endif | |
415 | ||
69c33c6c VZ |
416 | // frame constructor |
417 | MyFrame::MyFrame(const wxString& title, const wxPoint& pos, const wxSize& size) | |
07850a49 | 418 | : wxFrame((wxFrame *)NULL, wxID_ANY, title, pos, size), |
92a2a7eb | 419 | m_timerIdleWakeUp(this) |
69c33c6c | 420 | { |
50567b69 VZ |
421 | m_pidLast = 0; |
422 | ||
69c33c6c VZ |
423 | #ifdef __WXMAC__ |
424 | // we need this in order to allow the about menu relocation, since ABOUT is | |
425 | // not the default id of the about menu | |
426 | wxApp::s_macAboutMenuItemId = Exec_About; | |
427 | #endif | |
428 | ||
69c33c6c | 429 | // create a menu bar |
71307412 | 430 | wxMenu *menuFile = new wxMenu(wxEmptyString, wxMENU_TEAROFF); |
50567b69 VZ |
431 | menuFile->Append(Exec_Kill, _T("&Kill process...\tCtrl-K"), |
432 | _T("Kill a process by PID")); | |
433 | menuFile->AppendSeparator(); | |
c6f9dfe8 | 434 | menuFile->Append(Exec_ClearLog, _T("&Clear log\tCtrl-L"), |
d8e41d42 VZ |
435 | _T("Clear the log window")); |
436 | menuFile->AppendSeparator(); | |
c6f9dfe8 VZ |
437 | menuFile->Append(Exec_BeginBusyCursor, _T("Show &busy cursor\tCtrl-C")); |
438 | menuFile->Append(Exec_EndBusyCursor, _T("Show &normal cursor\tShift-Ctrl-C")); | |
439 | menuFile->AppendSeparator(); | |
69c33c6c VZ |
440 | menuFile->Append(Exec_Quit, _T("E&xit\tAlt-X"), _T("Quit this program")); |
441 | ||
442 | wxMenu *execMenu = new wxMenu; | |
443 | execMenu->Append(Exec_SyncExec, _T("Sync &execution...\tCtrl-E"), | |
444 | _T("Launch a program and return when it terminates")); | |
9b6b9e0c VZ |
445 | execMenu->Append(Exec_SyncNoEventsExec, _T("Sync execution and &block...\tCtrl-B"), |
446 | _T("Launch a program and block until it terminates")); | |
69c33c6c VZ |
447 | execMenu->Append(Exec_AsyncExec, _T("&Async execution...\tCtrl-A"), |
448 | _T("Launch a program and return immediately")); | |
449 | execMenu->Append(Exec_Shell, _T("Execute &shell command...\tCtrl-S"), | |
450 | _T("Launch a shell and execute a command in it")); | |
f6bcfd97 | 451 | execMenu->AppendSeparator(); |
d8e41d42 VZ |
452 | execMenu->Append(Exec_Redirect, _T("Capture command &output...\tCtrl-O"), |
453 | _T("Launch a program and capture its output")); | |
eb671557 | 454 | execMenu->Append(Exec_Pipe, _T("&Pipe through command..."), |
f6bcfd97 | 455 | _T("Pipe a string through a filter")); |
eb671557 VZ |
456 | execMenu->Append(Exec_POpen, _T("&Open a pipe to a command...\tCtrl-P"), |
457 | _T("Open a pipe to and from another program")); | |
69c33c6c | 458 | |
6ba63600 VZ |
459 | execMenu->AppendSeparator(); |
460 | execMenu->Append(Exec_OpenFile, _T("Open &file...\tCtrl-F"), | |
461 | _T("Launch the command to open this kind of files")); | |
ba59de5d VZ |
462 | execMenu->Append(Exec_OpenURL, _T("Open &URL...\tCtrl-U"), |
463 | _T("Launch the default browser with the given URL")); | |
d93c719a VZ |
464 | #ifdef __WINDOWS__ |
465 | execMenu->AppendSeparator(); | |
466 | execMenu->Append(Exec_DDEExec, _T("Execute command via &DDE...\tCtrl-D")); | |
ca289436 | 467 | execMenu->Append(Exec_DDERequest, _T("Send DDE &request...\tCtrl-R")); |
d93c719a VZ |
468 | #endif |
469 | ||
71307412 | 470 | wxMenu *helpMenu = new wxMenu(wxEmptyString, wxMENU_TEAROFF); |
69c33c6c VZ |
471 | helpMenu->Append(Exec_About, _T("&About...\tF1"), _T("Show about dialog")); |
472 | ||
473 | // now append the freshly created menu to the menu bar... | |
474 | wxMenuBar *menuBar = new wxMenuBar(); | |
475 | menuBar->Append(menuFile, _T("&File")); | |
476 | menuBar->Append(execMenu, _T("&Exec")); | |
477 | menuBar->Append(helpMenu, _T("&Help")); | |
478 | ||
479 | // ... and attach this menu bar to the frame | |
480 | SetMenuBar(menuBar); | |
481 | ||
9121bed2 | 482 | // create the listbox in which we will show misc messages as they come |
07850a49 | 483 | m_lbox = new wxListBox(this, wxID_ANY); |
8ce88dfa VZ |
484 | wxFont font(12, wxFONTFAMILY_TELETYPE, wxFONTSTYLE_NORMAL, |
485 | wxFONTWEIGHT_NORMAL); | |
486 | if ( font.Ok() ) | |
487 | m_lbox->SetFont(font); | |
9121bed2 | 488 | |
69c33c6c VZ |
489 | #if wxUSE_STATUSBAR |
490 | // create a status bar just for fun (by default with 1 pane only) | |
e680a378 | 491 | CreateStatusBar(); |
be5a51fb | 492 | SetStatusText(_T("Welcome to wxWidgets exec sample!")); |
69c33c6c VZ |
493 | #endif // wxUSE_STATUSBAR |
494 | } | |
495 | ||
5a8561fc VZ |
496 | MyFrame::~MyFrame() |
497 | { | |
498 | // any processes left until now must be deleted manually: normally this is | |
499 | // done when the associated process terminates but it must be still running | |
500 | // if this didn't happen until now | |
501 | for ( size_t n = 0; n < m_allAsync.size(); n++ ) | |
502 | { | |
503 | delete m_allAsync[n]; | |
504 | } | |
505 | } | |
506 | ||
50567b69 VZ |
507 | // ---------------------------------------------------------------------------- |
508 | // event handlers: file and help menu | |
509 | // ---------------------------------------------------------------------------- | |
69c33c6c VZ |
510 | |
511 | void MyFrame::OnQuit(wxCommandEvent& WXUNUSED(event)) | |
512 | { | |
07850a49 WS |
513 | // true is to force the frame to close |
514 | Close(true); | |
69c33c6c VZ |
515 | } |
516 | ||
d8e41d42 VZ |
517 | void MyFrame::OnClear(wxCommandEvent& WXUNUSED(event)) |
518 | { | |
519 | m_lbox->Clear(); | |
520 | } | |
521 | ||
c6f9dfe8 VZ |
522 | void MyFrame::OnBeginBusyCursor(wxCommandEvent& WXUNUSED(event)) |
523 | { | |
524 | wxBeginBusyCursor(); | |
525 | } | |
526 | ||
527 | void MyFrame::OnEndBusyCursor(wxCommandEvent& WXUNUSED(event)) | |
528 | { | |
529 | wxEndBusyCursor(); | |
530 | } | |
531 | ||
69c33c6c VZ |
532 | void MyFrame::OnAbout(wxCommandEvent& WXUNUSED(event)) |
533 | { | |
749bfe9a | 534 | wxMessageBox(_T("Exec wxWidgets Sample\n(c) 2000-2002 Vadim Zeitlin"), |
69c33c6c VZ |
535 | _T("About Exec"), wxOK | wxICON_INFORMATION, this); |
536 | } | |
537 | ||
50567b69 VZ |
538 | void MyFrame::OnKill(wxCommandEvent& WXUNUSED(event)) |
539 | { | |
540 | long pid = wxGetNumberFromUser(_T("Please specify the process to kill"), | |
541 | _T("Enter PID:"), | |
542 | _T("Exec question"), | |
543 | m_pidLast, | |
9c05a3ca VZ |
544 | // we need the full unsigned int range |
545 | -INT_MAX, INT_MAX, | |
50567b69 VZ |
546 | this); |
547 | if ( pid == -1 ) | |
548 | { | |
549 | // cancelled | |
550 | return; | |
551 | } | |
552 | ||
553 | static const wxString signalNames[] = | |
554 | { | |
555 | _T("Just test (SIGNONE)"), | |
556 | _T("Hangup (SIGHUP)"), | |
557 | _T("Interrupt (SIGINT)"), | |
558 | _T("Quit (SIGQUIT)"), | |
559 | _T("Illegal instruction (SIGILL)"), | |
560 | _T("Trap (SIGTRAP)"), | |
561 | _T("Abort (SIGABRT)"), | |
562 | _T("Emulated trap (SIGEMT)"), | |
563 | _T("FP exception (SIGFPE)"), | |
564 | _T("Kill (SIGKILL)"), | |
565 | _T("Bus (SIGBUS)"), | |
566 | _T("Segment violation (SIGSEGV)"), | |
567 | _T("System (SIGSYS)"), | |
568 | _T("Broken pipe (SIGPIPE)"), | |
569 | _T("Alarm (SIGALRM)"), | |
570 | _T("Terminate (SIGTERM)"), | |
571 | }; | |
572 | ||
573 | int sig = wxGetSingleChoiceIndex(_T("How to kill the process?"), | |
574 | _T("Exec question"), | |
575 | WXSIZEOF(signalNames), signalNames, | |
576 | this); | |
577 | switch ( sig ) | |
578 | { | |
579 | default: | |
580 | wxFAIL_MSG( _T("unexpected return value") ); | |
581 | // fall through | |
582 | ||
583 | case -1: | |
584 | // cancelled | |
585 | return; | |
586 | ||
587 | case wxSIGNONE: | |
588 | case wxSIGHUP: | |
589 | case wxSIGINT: | |
590 | case wxSIGQUIT: | |
591 | case wxSIGILL: | |
592 | case wxSIGTRAP: | |
593 | case wxSIGABRT: | |
594 | case wxSIGEMT: | |
595 | case wxSIGFPE: | |
596 | case wxSIGKILL: | |
597 | case wxSIGBUS: | |
598 | case wxSIGSEGV: | |
599 | case wxSIGSYS: | |
600 | case wxSIGPIPE: | |
601 | case wxSIGALRM: | |
602 | case wxSIGTERM: | |
603 | break; | |
604 | } | |
605 | ||
606 | if ( sig == 0 ) | |
607 | { | |
608 | if ( wxProcess::Exists(pid) ) | |
aec18ff7 | 609 | wxLogStatus(_T("Process %ld is running."), pid); |
50567b69 | 610 | else |
aec18ff7 | 611 | wxLogStatus(_T("No process with pid = %ld."), pid); |
50567b69 VZ |
612 | } |
613 | else // not SIGNONE | |
614 | { | |
615 | wxKillError rc = wxProcess::Kill(pid, (wxSignal)sig); | |
616 | if ( rc == wxKILL_OK ) | |
617 | { | |
aec18ff7 | 618 | wxLogStatus(_T("Process %ld killed with signal %d."), pid, sig); |
50567b69 VZ |
619 | } |
620 | else | |
621 | { | |
622 | static const wxChar *errorText[] = | |
623 | { | |
624 | _T(""), // no error | |
625 | _T("signal not supported"), | |
626 | _T("permission denied"), | |
627 | _T("no such process"), | |
628 | _T("unspecified error"), | |
629 | }; | |
630 | ||
aec18ff7 | 631 | wxLogStatus(_T("Failed to kill process %ld with signal %d: %s"), |
50567b69 VZ |
632 | pid, sig, errorText[rc]); |
633 | } | |
634 | } | |
635 | } | |
636 | ||
637 | // ---------------------------------------------------------------------------- | |
638 | // event handlers: exec menu | |
639 | // ---------------------------------------------------------------------------- | |
640 | ||
6ba63600 VZ |
641 | void MyFrame::DoAsyncExec(const wxString& cmd) |
642 | { | |
5a8561fc | 643 | MyProcess * const process = new MyProcess(this, cmd); |
4c1ef422 | 644 | m_pidLast = wxExecute(cmd, wxEXEC_ASYNC, process); |
50567b69 | 645 | if ( !m_pidLast ) |
6ba63600 | 646 | { |
5a8561fc | 647 | wxLogError(_T("Execution of '%s' failed."), cmd.c_str()); |
6ba63600 VZ |
648 | |
649 | delete process; | |
650 | } | |
651 | else | |
652 | { | |
5a8561fc | 653 | wxLogStatus(_T("Process %ld (%s) launched."), m_pidLast, cmd.c_str()); |
6ba63600 VZ |
654 | |
655 | m_cmdLast = cmd; | |
5a8561fc VZ |
656 | |
657 | // the parent frame keeps track of all async processes as it needs to | |
658 | // free them if we exit before the child process terminates | |
659 | AddAsyncProcess(process); | |
6ba63600 VZ |
660 | } |
661 | } | |
662 | ||
69c33c6c VZ |
663 | void MyFrame::OnSyncExec(wxCommandEvent& WXUNUSED(event)) |
664 | { | |
665 | wxString cmd = wxGetTextFromUser(_T("Enter the command: "), | |
d93c719a | 666 | DIALOG_TITLE, |
69c33c6c VZ |
667 | m_cmdLast); |
668 | ||
669 | if ( !cmd ) | |
670 | return; | |
671 | ||
aec18ff7 | 672 | wxLogStatus( _T("'%s' is running please wait..."), cmd.c_str() ); |
d31b7b68 | 673 | |
4c1ef422 | 674 | int code = wxExecute(cmd, wxEXEC_SYNC); |
d31b7b68 | 675 | |
69c33c6c | 676 | wxLogStatus(_T("Process '%s' terminated with exit code %d."), |
aec18ff7 MB |
677 | cmd.c_str(), code); |
678 | ||
69c33c6c VZ |
679 | m_cmdLast = cmd; |
680 | } | |
681 | ||
9b6b9e0c VZ |
682 | void MyFrame::OnSyncNoEventsExec(wxCommandEvent& WXUNUSED(event)) |
683 | { | |
684 | wxString cmd = wxGetTextFromUser(_T("Enter the command: "), | |
685 | DIALOG_TITLE, | |
686 | m_cmdLast); | |
687 | ||
688 | if ( !cmd ) | |
689 | return; | |
690 | ||
691 | wxLogStatus( _T("'%s' is running please wait..."), cmd.c_str() ); | |
692 | ||
693 | int code = wxExecute(cmd, wxEXEC_BLOCK); | |
694 | ||
695 | wxLogStatus(_T("Process '%s' terminated with exit code %d."), | |
696 | cmd.c_str(), code); | |
697 | ||
698 | m_cmdLast = cmd; | |
699 | } | |
700 | ||
69c33c6c VZ |
701 | void MyFrame::OnAsyncExec(wxCommandEvent& WXUNUSED(event)) |
702 | { | |
703 | wxString cmd = wxGetTextFromUser(_T("Enter the command: "), | |
d93c719a | 704 | DIALOG_TITLE, |
69c33c6c VZ |
705 | m_cmdLast); |
706 | ||
707 | if ( !cmd ) | |
708 | return; | |
709 | ||
6ba63600 | 710 | DoAsyncExec(cmd); |
69c33c6c VZ |
711 | } |
712 | ||
713 | void MyFrame::OnShell(wxCommandEvent& WXUNUSED(event)) | |
714 | { | |
715 | wxString cmd = wxGetTextFromUser(_T("Enter the command: "), | |
d93c719a | 716 | DIALOG_TITLE, |
69c33c6c VZ |
717 | m_cmdLast); |
718 | ||
719 | if ( !cmd ) | |
720 | return; | |
721 | ||
722 | int code = wxShell(cmd); | |
723 | wxLogStatus(_T("Shell command '%s' terminated with exit code %d."), | |
724 | cmd.c_str(), code); | |
725 | m_cmdLast = cmd; | |
726 | } | |
727 | ||
d8e41d42 VZ |
728 | void MyFrame::OnExecWithRedirect(wxCommandEvent& WXUNUSED(event)) |
729 | { | |
69328170 VZ |
730 | if ( !m_cmdLast ) |
731 | { | |
732 | #ifdef __WXMSW__ | |
733 | m_cmdLast = "type Makefile.in"; | |
734 | #else | |
735 | m_cmdLast = "cat -n Makefile"; | |
736 | #endif | |
737 | } | |
738 | ||
d8e41d42 VZ |
739 | wxString cmd = wxGetTextFromUser(_T("Enter the command: "), |
740 | DIALOG_TITLE, | |
741 | m_cmdLast); | |
742 | ||
743 | if ( !cmd ) | |
744 | return; | |
745 | ||
cd6ce4a9 VZ |
746 | bool sync; |
747 | switch ( wxMessageBox(_T("Execute it synchronously?"), | |
748 | _T("Exec question"), | |
749 | wxYES_NO | wxCANCEL | wxICON_QUESTION, this) ) | |
d8e41d42 | 750 | { |
cd6ce4a9 | 751 | case wxYES: |
07850a49 | 752 | sync = true; |
cd6ce4a9 | 753 | break; |
d8e41d42 | 754 | |
cd6ce4a9 | 755 | case wxNO: |
07850a49 | 756 | sync = false; |
cd6ce4a9 VZ |
757 | break; |
758 | ||
759 | default: | |
760 | return; | |
d8e41d42 | 761 | } |
cd6ce4a9 VZ |
762 | |
763 | if ( sync ) | |
d8e41d42 | 764 | { |
ce0bb89d VZ |
765 | wxLogStatus( _T("'%s' is running please wait..."), cmd.c_str() ); |
766 | ||
f6bcfd97 BP |
767 | wxArrayString output, errors; |
768 | int code = wxExecute(cmd, output, errors); | |
ce0bb89d VZ |
769 | |
770 | wxLogStatus(_T("Command '%s' terminated with exit code %d."), | |
cd6ce4a9 VZ |
771 | cmd.c_str(), code); |
772 | ||
773 | if ( code != -1 ) | |
774 | { | |
f6bcfd97 BP |
775 | ShowOutput(cmd, output, _T("Output")); |
776 | ShowOutput(cmd, errors, _T("Errors")); | |
cd6ce4a9 VZ |
777 | } |
778 | } | |
779 | else // async exec | |
780 | { | |
781 | MyPipedProcess *process = new MyPipedProcess(this, cmd); | |
4c1ef422 | 782 | if ( !wxExecute(cmd, wxEXEC_ASYNC, process) ) |
cd6ce4a9 VZ |
783 | { |
784 | wxLogError(_T("Execution of '%s' failed."), cmd.c_str()); | |
785 | ||
786 | delete process; | |
787 | } | |
788 | else | |
789 | { | |
5a8561fc | 790 | AddPipedProcess(process); |
cd6ce4a9 | 791 | } |
d8e41d42 | 792 | } |
cd6ce4a9 VZ |
793 | |
794 | m_cmdLast = cmd; | |
d8e41d42 VZ |
795 | } |
796 | ||
f6bcfd97 BP |
797 | void MyFrame::OnExecWithPipe(wxCommandEvent& WXUNUSED(event)) |
798 | { | |
799 | if ( !m_cmdLast ) | |
800 | m_cmdLast = _T("tr [a-z] [A-Z]"); | |
801 | ||
802 | wxString cmd = wxGetTextFromUser(_T("Enter the command: "), | |
803 | DIALOG_TITLE, | |
804 | m_cmdLast); | |
805 | ||
806 | if ( !cmd ) | |
807 | return; | |
808 | ||
809 | wxString input = wxGetTextFromUser(_T("Enter the string to send to it: "), | |
810 | DIALOG_TITLE); | |
811 | if ( !input ) | |
812 | return; | |
813 | ||
814 | // always execute the filter asynchronously | |
815 | MyPipedProcess2 *process = new MyPipedProcess2(this, cmd, input); | |
aec18ff7 | 816 | long pid = wxExecute(cmd, wxEXEC_ASYNC, process); |
f6bcfd97 BP |
817 | if ( pid ) |
818 | { | |
5a8561fc | 819 | wxLogStatus(_T("Process %ld (%s) launched."), pid, cmd.c_str()); |
f6bcfd97 | 820 | |
5a8561fc | 821 | AddPipedProcess(process); |
f6bcfd97 BP |
822 | } |
823 | else | |
824 | { | |
825 | wxLogError(_T("Execution of '%s' failed."), cmd.c_str()); | |
826 | ||
827 | delete process; | |
828 | } | |
829 | ||
830 | m_cmdLast = cmd; | |
831 | } | |
832 | ||
87728739 | 833 | void MyFrame::OnPOpen(wxCommandEvent& WXUNUSED(event)) |
eb671557 VZ |
834 | { |
835 | wxString cmd = wxGetTextFromUser(_T("Enter the command to launch: "), | |
836 | DIALOG_TITLE, | |
837 | m_cmdLast); | |
838 | if ( cmd.empty() ) | |
839 | return; | |
840 | ||
841 | wxProcess *process = wxProcess::Open(cmd); | |
842 | if ( !process ) | |
843 | { | |
844 | wxLogError(_T("Failed to launch the command.")); | |
845 | return; | |
846 | } | |
847 | ||
a387938f JS |
848 | wxLogVerbose(_T("PID of the new process: %ld"), process->GetPid()); |
849 | ||
eb671557 VZ |
850 | wxOutputStream *out = process->GetOutputStream(); |
851 | if ( !out ) | |
852 | { | |
853 | wxLogError(_T("Failed to connect to child stdin")); | |
854 | return; | |
855 | } | |
856 | ||
857 | wxInputStream *in = process->GetInputStream(); | |
858 | if ( !in ) | |
859 | { | |
860 | wxLogError(_T("Failed to connect to child stdout")); | |
861 | return; | |
862 | } | |
863 | ||
864 | new MyPipeFrame(this, cmd, process); | |
865 | } | |
866 | ||
87728739 | 867 | void MyFrame::OnFileExec(wxCommandEvent& WXUNUSED(event)) |
6ba63600 VZ |
868 | { |
869 | static wxString s_filename; | |
870 | ||
71307412 WS |
871 | wxString filename; |
872 | ||
873 | #if wxUSE_FILEDLG | |
bd0b594d | 874 | filename = wxLoadFileSelector(_T("any file"), wxEmptyString, s_filename, this); |
ba59de5d VZ |
875 | #else // !wxUSE_FILEDLG |
876 | filename = wxGetTextFromUser(_T("Enter the file name"), _T("exec sample"), | |
877 | s_filename, this); | |
878 | #endif // wxUSE_FILEDLG/!wxUSE_FILEDLG | |
71307412 WS |
879 | |
880 | if ( filename.empty() ) | |
6ba63600 VZ |
881 | return; |
882 | ||
883 | s_filename = filename; | |
884 | ||
885 | wxString ext = filename.AfterFirst(_T('.')); | |
886 | wxFileType *ft = wxTheMimeTypesManager->GetFileTypeFromExtension(ext); | |
887 | if ( !ft ) | |
888 | { | |
889 | wxLogError(_T("Impossible to determine the file type for extension '%s'"), | |
890 | ext.c_str()); | |
891 | return; | |
892 | } | |
893 | ||
894 | wxString cmd; | |
895 | bool ok = ft->GetOpenCommand(&cmd, | |
71307412 | 896 | wxFileType::MessageParameters(filename)); |
6ba63600 VZ |
897 | delete ft; |
898 | if ( !ok ) | |
899 | { | |
900 | wxLogError(_T("Impossible to find out how to open files of extension '%s'"), | |
901 | ext.c_str()); | |
902 | return; | |
903 | } | |
904 | ||
905 | DoAsyncExec(cmd); | |
906 | } | |
907 | ||
ba59de5d VZ |
908 | void MyFrame::OnOpenURL(wxCommandEvent& WXUNUSED(event)) |
909 | { | |
8f6d9cb3 | 910 | static wxString s_filename(_T("http://www.wxwidgets.org/")); |
ba59de5d VZ |
911 | |
912 | wxString filename = wxGetTextFromUser | |
913 | ( | |
914 | _T("Enter the URL"), | |
915 | _T("exec sample"), | |
916 | s_filename, | |
917 | this | |
918 | ); | |
919 | ||
920 | if ( filename.empty() ) | |
921 | return; | |
922 | ||
923 | s_filename = filename; | |
924 | ||
925 | if ( !wxLaunchDefaultBrowser(s_filename) ) | |
926 | wxLogError(_T("Failed to open URL \"%s\""), s_filename.c_str()); | |
927 | } | |
928 | ||
50567b69 VZ |
929 | // ---------------------------------------------------------------------------- |
930 | // DDE stuff | |
931 | // ---------------------------------------------------------------------------- | |
932 | ||
d93c719a | 933 | #ifdef __WINDOWS__ |
ca289436 VZ |
934 | |
935 | bool MyFrame::GetDDEServer() | |
936 | { | |
d93c719a | 937 | wxString server = wxGetTextFromUser(_T("Server to connect to:"), |
ca289436 | 938 | DIALOG_TITLE, m_server); |
d93c719a | 939 | if ( !server ) |
07850a49 | 940 | return false; |
ca289436 VZ |
941 | |
942 | m_server = server; | |
d93c719a | 943 | |
ca289436 | 944 | wxString topic = wxGetTextFromUser(_T("DDE topic:"), DIALOG_TITLE, m_topic); |
d93c719a | 945 | if ( !topic ) |
07850a49 | 946 | return false; |
d93c719a | 947 | |
ca289436 VZ |
948 | m_topic = topic; |
949 | ||
950 | wxString cmd = wxGetTextFromUser(_T("DDE command:"), DIALOG_TITLE, m_cmdDde); | |
d93c719a | 951 | if ( !cmd ) |
07850a49 | 952 | return false; |
ca289436 VZ |
953 | |
954 | m_cmdDde = cmd; | |
955 | ||
07850a49 | 956 | return true; |
ca289436 VZ |
957 | } |
958 | ||
959 | void MyFrame::OnDDEExec(wxCommandEvent& WXUNUSED(event)) | |
960 | { | |
961 | if ( !GetDDEServer() ) | |
d93c719a VZ |
962 | return; |
963 | ||
964 | wxDDEClient client; | |
71307412 | 965 | wxConnectionBase *conn = client.MakeConnection(wxEmptyString, m_server, m_topic); |
d93c719a VZ |
966 | if ( !conn ) |
967 | { | |
968 | wxLogError(_T("Failed to connect to the DDE server '%s'."), | |
ca289436 | 969 | m_server.c_str()); |
d93c719a VZ |
970 | } |
971 | else | |
972 | { | |
ca289436 | 973 | if ( !conn->Execute(m_cmdDde) ) |
d93c719a VZ |
974 | { |
975 | wxLogError(_T("Failed to execute command '%s' via DDE."), | |
ca289436 | 976 | m_cmdDde.c_str()); |
d93c719a VZ |
977 | } |
978 | else | |
979 | { | |
980 | wxLogStatus(_T("Successfully executed DDE command")); | |
981 | } | |
982 | } | |
d93c719a VZ |
983 | } |
984 | ||
ca289436 VZ |
985 | void MyFrame::OnDDERequest(wxCommandEvent& WXUNUSED(event)) |
986 | { | |
987 | if ( !GetDDEServer() ) | |
988 | return; | |
989 | ||
990 | wxDDEClient client; | |
71307412 | 991 | wxConnectionBase *conn = client.MakeConnection(wxEmptyString, m_server, m_topic); |
ca289436 VZ |
992 | if ( !conn ) |
993 | { | |
994 | wxLogError(_T("Failed to connect to the DDE server '%s'."), | |
995 | m_server.c_str()); | |
996 | } | |
997 | else | |
998 | { | |
999 | if ( !conn->Request(m_cmdDde) ) | |
1000 | { | |
1001 | wxLogError(_T("Failed to send request '%s' via DDE."), | |
1002 | m_cmdDde.c_str()); | |
1003 | } | |
1004 | else | |
1005 | { | |
1006 | wxLogStatus(_T("Successfully sent DDE request.")); | |
1007 | } | |
1008 | } | |
1009 | } | |
1010 | ||
1011 | #endif // __WINDOWS__ | |
1012 | ||
50567b69 VZ |
1013 | // ---------------------------------------------------------------------------- |
1014 | // various helpers | |
1015 | // ---------------------------------------------------------------------------- | |
1016 | ||
cd6ce4a9 VZ |
1017 | // input polling |
1018 | void MyFrame::OnIdle(wxIdleEvent& event) | |
1019 | { | |
1020 | size_t count = m_running.GetCount(); | |
1021 | for ( size_t n = 0; n < count; n++ ) | |
1022 | { | |
1023 | if ( m_running[n]->HasInput() ) | |
1024 | { | |
1025 | event.RequestMore(); | |
1026 | } | |
1027 | } | |
1028 | } | |
1029 | ||
92a2a7eb VZ |
1030 | void MyFrame::OnTimer(wxTimerEvent& WXUNUSED(event)) |
1031 | { | |
1032 | wxWakeUpIdle(); | |
1033 | } | |
1034 | ||
f6bcfd97 BP |
1035 | void MyFrame::OnProcessTerminated(MyPipedProcess *process) |
1036 | { | |
5a8561fc VZ |
1037 | RemovePipedProcess(process); |
1038 | } | |
1039 | ||
1040 | void MyFrame::OnAsyncTermination(MyProcess *process) | |
1041 | { | |
1042 | m_allAsync.Remove(process); | |
1043 | ||
1044 | delete process; | |
1045 | } | |
1046 | ||
1047 | void MyFrame::AddPipedProcess(MyPipedProcess *process) | |
1048 | { | |
1049 | if ( m_running.IsEmpty() ) | |
1050 | { | |
1051 | // we want to start getting the timer events to ensure that a | |
1052 | // steady stream of idle events comes in -- otherwise we | |
1053 | // wouldn't be able to poll the child process input | |
1054 | m_timerIdleWakeUp.Start(100); | |
1055 | } | |
1056 | //else: the timer is already running | |
1057 | ||
1058 | m_running.Add(process); | |
1059 | m_allAsync.Add(process); | |
f6bcfd97 BP |
1060 | } |
1061 | ||
5a8561fc VZ |
1062 | void MyFrame::RemovePipedProcess(MyPipedProcess *process) |
1063 | { | |
1064 | m_running.Remove(process); | |
1065 | ||
1066 | if ( m_running.IsEmpty() ) | |
1067 | { | |
1068 | // we don't need to get idle events all the time any more | |
1069 | m_timerIdleWakeUp.Stop(); | |
1070 | } | |
1071 | } | |
f6bcfd97 BP |
1072 | |
1073 | void MyFrame::ShowOutput(const wxString& cmd, | |
1074 | const wxArrayString& output, | |
1075 | const wxString& title) | |
1076 | { | |
1077 | size_t count = output.GetCount(); | |
1078 | if ( !count ) | |
1079 | return; | |
1080 | ||
1081 | m_lbox->Append(wxString::Format(_T("--- %s of '%s' ---"), | |
1082 | title.c_str(), cmd.c_str())); | |
1083 | ||
1084 | for ( size_t n = 0; n < count; n++ ) | |
1085 | { | |
1086 | m_lbox->Append(output[n]); | |
1087 | } | |
1088 | ||
eb671557 VZ |
1089 | m_lbox->Append(wxString::Format(_T("--- End of %s ---"), |
1090 | title.Lower().c_str())); | |
f6bcfd97 BP |
1091 | } |
1092 | ||
69c33c6c VZ |
1093 | // ---------------------------------------------------------------------------- |
1094 | // MyProcess | |
1095 | // ---------------------------------------------------------------------------- | |
1096 | ||
1097 | void MyProcess::OnTerminate(int pid, int status) | |
1098 | { | |
1099 | wxLogStatus(m_parent, _T("Process %u ('%s') terminated with exit code %d."), | |
1100 | pid, m_cmd.c_str(), status); | |
1101 | ||
5a8561fc | 1102 | m_parent->OnAsyncTermination(this); |
69c33c6c | 1103 | } |
d8e41d42 | 1104 | |
cd6ce4a9 VZ |
1105 | // ---------------------------------------------------------------------------- |
1106 | // MyPipedProcess | |
1107 | // ---------------------------------------------------------------------------- | |
1108 | ||
1109 | bool MyPipedProcess::HasInput() | |
d8e41d42 | 1110 | { |
07850a49 | 1111 | bool hasInput = false; |
f6bcfd97 | 1112 | |
92a2a7eb | 1113 | if ( IsInputAvailable() ) |
cd6ce4a9 | 1114 | { |
92a2a7eb | 1115 | wxTextInputStream tis(*GetInputStream()); |
cd6ce4a9 VZ |
1116 | |
1117 | // this assumes that the output is always line buffered | |
1118 | wxString msg; | |
f6bcfd97 | 1119 | msg << m_cmd << _T(" (stdout): ") << tis.ReadLine(); |
d8e41d42 | 1120 | |
cd6ce4a9 VZ |
1121 | m_parent->GetLogListBox()->Append(msg); |
1122 | ||
07850a49 | 1123 | hasInput = true; |
cd6ce4a9 | 1124 | } |
f6bcfd97 | 1125 | |
92a2a7eb | 1126 | if ( IsErrorAvailable() ) |
d8e41d42 | 1127 | { |
92a2a7eb | 1128 | wxTextInputStream tis(*GetErrorStream()); |
f6bcfd97 BP |
1129 | |
1130 | // this assumes that the output is always line buffered | |
1131 | wxString msg; | |
1132 | msg << m_cmd << _T(" (stderr): ") << tis.ReadLine(); | |
1133 | ||
1134 | m_parent->GetLogListBox()->Append(msg); | |
1135 | ||
07850a49 | 1136 | hasInput = true; |
d8e41d42 | 1137 | } |
f6bcfd97 BP |
1138 | |
1139 | return hasInput; | |
cd6ce4a9 VZ |
1140 | } |
1141 | ||
1142 | void MyPipedProcess::OnTerminate(int pid, int status) | |
1143 | { | |
1144 | // show the rest of the output | |
1145 | while ( HasInput() ) | |
1146 | ; | |
1147 | ||
1148 | m_parent->OnProcessTerminated(this); | |
d8e41d42 VZ |
1149 | |
1150 | MyProcess::OnTerminate(pid, status); | |
1151 | } | |
f6bcfd97 BP |
1152 | |
1153 | // ---------------------------------------------------------------------------- | |
1154 | // MyPipedProcess2 | |
1155 | // ---------------------------------------------------------------------------- | |
1156 | ||
1157 | bool MyPipedProcess2::HasInput() | |
1158 | { | |
11fdee42 | 1159 | if ( !m_input.empty() ) |
f6bcfd97 BP |
1160 | { |
1161 | wxTextOutputStream os(*GetOutputStream()); | |
1162 | os.WriteString(m_input); | |
1163 | ||
1164 | CloseOutput(); | |
1165 | m_input.clear(); | |
1166 | ||
1167 | // call us once again - may be we'll have output | |
07850a49 | 1168 | return true; |
f6bcfd97 BP |
1169 | } |
1170 | ||
1171 | return MyPipedProcess::HasInput(); | |
1172 | } | |
eb671557 VZ |
1173 | |
1174 | // ============================================================================ | |
1175 | // MyPipeFrame implementation | |
1176 | // ============================================================================ | |
1177 | ||
1178 | MyPipeFrame::MyPipeFrame(wxFrame *parent, | |
1179 | const wxString& cmd, | |
1180 | wxProcess *process) | |
07850a49 | 1181 | : wxFrame(parent, wxID_ANY, cmd), |
eb671557 VZ |
1182 | m_process(process), |
1183 | // in a real program we'd check that the streams are !NULL here | |
f6def1fa | 1184 | m_out(*process->GetOutputStream()), |
eb671557 | 1185 | m_in(*process->GetInputStream()), |
f6def1fa | 1186 | m_err(*process->GetErrorStream()) |
eb671557 | 1187 | { |
7ca528cb VZ |
1188 | m_process->SetNextHandler(this); |
1189 | ||
07850a49 | 1190 | wxPanel *panel = new wxPanel(this, wxID_ANY); |
7ca528cb | 1191 | |
71307412 | 1192 | m_textOut = new wxTextCtrl(panel, wxID_ANY, wxEmptyString, |
eb671557 VZ |
1193 | wxDefaultPosition, wxDefaultSize, |
1194 | wxTE_PROCESS_ENTER); | |
71307412 | 1195 | m_textIn = new wxTextCtrl(panel, wxID_ANY, wxEmptyString, |
1a278e7b VZ |
1196 | wxDefaultPosition, wxDefaultSize, |
1197 | wxTE_MULTILINE | wxTE_RICH); | |
1198 | m_textIn->SetEditable(false); | |
71307412 | 1199 | m_textErr = new wxTextCtrl(panel, wxID_ANY, wxEmptyString, |
1a278e7b VZ |
1200 | wxDefaultPosition, wxDefaultSize, |
1201 | wxTE_MULTILINE | wxTE_RICH); | |
1202 | m_textErr->SetEditable(false); | |
eb671557 VZ |
1203 | |
1204 | wxSizer *sizerTop = new wxBoxSizer(wxVERTICAL); | |
1a278e7b | 1205 | sizerTop->Add(m_textOut, 0, wxGROW | wxALL, 5); |
eb671557 VZ |
1206 | |
1207 | wxSizer *sizerBtns = new wxBoxSizer(wxHORIZONTAL); | |
1a278e7b VZ |
1208 | sizerBtns-> |
1209 | Add(new wxButton(panel, Exec_Btn_Send, _T("&Send")), 0, wxALL, 5); | |
1210 | sizerBtns-> | |
1211 | Add(new wxButton(panel, Exec_Btn_SendFile, _T("&File...")), 0, wxALL, 5); | |
1212 | sizerBtns-> | |
1213 | Add(new wxButton(panel, Exec_Btn_Get, _T("&Get")), 0, wxALL, 5); | |
1214 | sizerBtns-> | |
1215 | Add(new wxButton(panel, Exec_Btn_Close, _T("&Close")), 0, wxALL, 5); | |
eb671557 VZ |
1216 | |
1217 | sizerTop->Add(sizerBtns, 0, wxCENTRE | wxALL, 5); | |
1a278e7b VZ |
1218 | sizerTop->Add(m_textIn, 1, wxGROW | wxALL, 5); |
1219 | sizerTop->Add(m_textErr, 1, wxGROW | wxALL, 5); | |
eb671557 | 1220 | |
7ca528cb | 1221 | panel->SetSizer(sizerTop); |
eb671557 VZ |
1222 | sizerTop->Fit(this); |
1223 | ||
1224 | Show(); | |
1225 | } | |
1226 | ||
1a278e7b VZ |
1227 | void MyPipeFrame::OnBtnSendFile(wxCommandEvent& WXUNUSED(event)) |
1228 | { | |
71307412 | 1229 | #if wxUSE_FILEDLG |
1a278e7b VZ |
1230 | wxFileDialog filedlg(this, _T("Select file to send")); |
1231 | if ( filedlg.ShowModal() != wxID_OK ) | |
1232 | return; | |
1233 | ||
1234 | wxFFile file(filedlg.GetFilename(), _T("r")); | |
1235 | wxString data; | |
1236 | if ( !file.IsOpened() || !file.ReadAll(&data) ) | |
1237 | return; | |
1238 | ||
1239 | // can't write the entire string at once, this risk overflowing the pipe | |
1240 | // and we would dead lock | |
1241 | size_t len = data.length(); | |
1242 | const wxChar *pc = data.c_str(); | |
1243 | while ( len ) | |
1244 | { | |
1245 | const size_t CHUNK_SIZE = 4096; | |
42d0aa30 | 1246 | m_out.Write(pc, len > CHUNK_SIZE ? CHUNK_SIZE : len); |
1a278e7b | 1247 | |
42d0aa30 VZ |
1248 | // note that not all data could have been written as we don't block on |
1249 | // the write end of the pipe | |
1250 | const size_t lenChunk = m_out.LastWrite(); | |
1a278e7b VZ |
1251 | |
1252 | pc += lenChunk; | |
1253 | len -= lenChunk; | |
1254 | ||
1255 | DoGet(); | |
1256 | } | |
71307412 | 1257 | #endif // wxUSE_FILEDLG |
1a278e7b VZ |
1258 | } |
1259 | ||
eb671557 VZ |
1260 | void MyPipeFrame::DoGet() |
1261 | { | |
7ca528cb VZ |
1262 | // we don't have any way to be notified when any input appears on the |
1263 | // stream so we have to poll it :-( | |
1a278e7b VZ |
1264 | DoGetFromStream(m_textIn, m_in); |
1265 | DoGetFromStream(m_textErr, m_err); | |
1266 | } | |
1267 | ||
1268 | void MyPipeFrame::DoGetFromStream(wxTextCtrl *text, wxInputStream& in) | |
1269 | { | |
1270 | while ( in.CanRead() ) | |
1271 | { | |
1272 | wxChar buffer[4096]; | |
1273 | buffer[in.Read(buffer, WXSIZEOF(buffer) - 1).LastRead()] = _T('\0'); | |
7ca528cb | 1274 | |
1a278e7b VZ |
1275 | text->AppendText(buffer); |
1276 | } | |
1277 | } | |
1278 | ||
1279 | void MyPipeFrame::DoClose() | |
1280 | { | |
1281 | m_process->CloseOutput(); | |
1282 | ||
1283 | DisableInput(); | |
1284 | } | |
1285 | ||
1286 | void MyPipeFrame::DisableInput() | |
1287 | { | |
1288 | m_textOut->SetEditable(false); | |
1289 | FindWindow(Exec_Btn_Send)->Disable(); | |
1290 | FindWindow(Exec_Btn_SendFile)->Disable(); | |
1291 | FindWindow(Exec_Btn_Close)->Disable(); | |
1292 | } | |
1293 | ||
1294 | void MyPipeFrame::DisableOutput() | |
1295 | { | |
1296 | FindWindow(Exec_Btn_Get)->Disable(); | |
eb671557 VZ |
1297 | } |
1298 | ||
1299 | void MyPipeFrame::OnClose(wxCloseEvent& event) | |
1300 | { | |
7ca528cb VZ |
1301 | if ( m_process ) |
1302 | { | |
1303 | // we're not interested in getting the process termination notification | |
1304 | // if we are closing it ourselves | |
1305 | wxProcess *process = m_process; | |
1306 | m_process = NULL; | |
1307 | process->SetNextHandler(NULL); | |
1308 | ||
1309 | process->CloseOutput(); | |
1310 | } | |
eb671557 VZ |
1311 | |
1312 | event.Skip(); | |
1313 | } | |
1314 | ||
87728739 | 1315 | void MyPipeFrame::OnProcessTerm(wxProcessEvent& WXUNUSED(event)) |
7ca528cb | 1316 | { |
1a278e7b VZ |
1317 | DoGet(); |
1318 | ||
7ca528cb VZ |
1319 | delete m_process; |
1320 | m_process = NULL; | |
1321 | ||
1322 | wxLogWarning(_T("The other process has terminated, closing")); | |
1323 | ||
1a278e7b VZ |
1324 | DisableInput(); |
1325 | DisableOutput(); | |
7ca528cb | 1326 | } |