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