]>
Commit | Line | Data |
---|---|---|
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 | ||
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 | |
28 | // need because it includes almost all "standard" wxWidgets headers | |
29 | #ifndef WX_PRECOMP | |
30 | #include "wx/app.h" | |
31 | #include "wx/log.h" | |
32 | #include "wx/frame.h" | |
33 | #include "wx/panel.h" | |
34 | ||
35 | #include "wx/timer.h" | |
36 | ||
37 | #include "wx/utils.h" | |
38 | #include "wx/menu.h" | |
39 | ||
40 | #include "wx/msgdlg.h" | |
41 | #include "wx/textdlg.h" | |
42 | #include "wx/filedlg.h" | |
43 | #include "wx/choicdlg.h" | |
44 | ||
45 | #include "wx/button.h" | |
46 | #include "wx/textctrl.h" | |
47 | #include "wx/listbox.h" | |
48 | ||
49 | #include "wx/sizer.h" | |
50 | #endif | |
51 | ||
52 | #include "wx/txtstrm.h" | |
53 | #include "wx/numdlg.h" | |
54 | #include "wx/textdlg.h" | |
55 | #include "wx/ffile.h" | |
56 | ||
57 | #include "wx/process.h" | |
58 | ||
59 | #include "wx/mimetype.h" | |
60 | ||
61 | #ifdef __WINDOWS__ | |
62 | #include "wx/dde.h" | |
63 | #endif // __WINDOWS__ | |
64 | ||
65 | // ---------------------------------------------------------------------------- | |
66 | // the usual application and main frame classes | |
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 | ||
82 | // Define an array of process pointers used by MyFrame | |
83 | class MyPipedProcess; | |
84 | WX_DEFINE_ARRAY_PTR(MyPipedProcess *, MyPipedProcessesArray); | |
85 | ||
86 | class MyProcess; | |
87 | WX_DEFINE_ARRAY_PTR(MyProcess *, MyProcessesArray); | |
88 | ||
89 | // Define a new frame type: this is going to be our main frame | |
90 | class MyFrame : public wxFrame | |
91 | { | |
92 | public: | |
93 | // ctor and dtor | |
94 | MyFrame(const wxString& title, const wxPoint& pos, const wxSize& size); | |
95 | virtual ~MyFrame(); | |
96 | ||
97 | // event handlers (these functions should _not_ be virtual) | |
98 | void OnQuit(wxCommandEvent& event); | |
99 | ||
100 | void OnKill(wxCommandEvent& event); | |
101 | ||
102 | void OnClear(wxCommandEvent& event); | |
103 | ||
104 | void OnBeginBusyCursor(wxCommandEvent& event); | |
105 | void OnEndBusyCursor(wxCommandEvent& event); | |
106 | ||
107 | void OnSyncExec(wxCommandEvent& event); | |
108 | void OnSyncNoEventsExec(wxCommandEvent& event); | |
109 | void OnAsyncExec(wxCommandEvent& event); | |
110 | void OnShell(wxCommandEvent& event); | |
111 | void OnExecWithRedirect(wxCommandEvent& event); | |
112 | void OnExecWithPipe(wxCommandEvent& event); | |
113 | ||
114 | void OnPOpen(wxCommandEvent& event); | |
115 | ||
116 | void OnFileExec(wxCommandEvent& event); | |
117 | void OnOpenURL(wxCommandEvent& event); | |
118 | ||
119 | void OnAbout(wxCommandEvent& event); | |
120 | ||
121 | // polling output of async processes | |
122 | void OnTimer(wxTimerEvent& event); | |
123 | void OnIdle(wxIdleEvent& event); | |
124 | ||
125 | // for MyPipedProcess | |
126 | void OnProcessTerminated(MyPipedProcess *process); | |
127 | wxListBox *GetLogListBox() const { return m_lbox; } | |
128 | ||
129 | // for MyProcess | |
130 | void OnAsyncTermination(MyProcess *process); | |
131 | ||
132 | private: | |
133 | void ShowOutput(const wxString& cmd, | |
134 | const wxArrayString& output, | |
135 | const wxString& title); | |
136 | ||
137 | void DoAsyncExec(const wxString& cmd); | |
138 | ||
139 | void AddAsyncProcess(MyProcess *process) { m_allAsync.push_back(process); } | |
140 | ||
141 | void AddPipedProcess(MyPipedProcess *process); | |
142 | void RemovePipedProcess(MyPipedProcess *process); | |
143 | ||
144 | ||
145 | // the PID of the last process we launched asynchronously | |
146 | long m_pidLast; | |
147 | ||
148 | // last command we executed | |
149 | wxString m_cmdLast; | |
150 | ||
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 | ||
163 | wxListBox *m_lbox; | |
164 | ||
165 | // array of running processes with redirected IO | |
166 | MyPipedProcessesArray m_running; | |
167 | ||
168 | // array of all asynchrously running processes | |
169 | MyProcessesArray m_allAsync; | |
170 | ||
171 | // the idle event wake up timer | |
172 | wxTimer m_timerIdleWakeUp; | |
173 | ||
174 | // any class wishing to process wxWidgets events must use this macro | |
175 | DECLARE_EVENT_TABLE() | |
176 | }; | |
177 | ||
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: | |
190 | void OnTextEnter(wxCommandEvent& WXUNUSED(event)) { DoSend(); } | |
191 | void OnBtnSend(wxCommandEvent& WXUNUSED(event)) { DoSend(); } | |
192 | void OnBtnSendFile(wxCommandEvent& WXUNUSED(event)); | |
193 | void OnBtnGet(wxCommandEvent& WXUNUSED(event)) { DoGet(); } | |
194 | void OnBtnClose(wxCommandEvent& WXUNUSED(event)) { DoClose(); } | |
195 | ||
196 | void OnClose(wxCloseEvent& event); | |
197 | ||
198 | void OnProcessTerm(wxProcessEvent& event); | |
199 | ||
200 | void DoSend() | |
201 | { | |
202 | wxString s(m_textOut->GetValue()); | |
203 | s += _T('\n'); | |
204 | m_out.Write(s.c_str(), s.length()); | |
205 | m_textOut->Clear(); | |
206 | ||
207 | DoGet(); | |
208 | } | |
209 | ||
210 | void DoGet(); | |
211 | void DoClose(); | |
212 | ||
213 | private: | |
214 | void DoGetFromStream(wxTextCtrl *text, wxInputStream& in); | |
215 | void DisableInput(); | |
216 | void DisableOutput(); | |
217 | ||
218 | ||
219 | wxProcess *m_process; | |
220 | ||
221 | wxOutputStream &m_out; | |
222 | wxInputStream &m_in, | |
223 | &m_err; | |
224 | ||
225 | wxTextCtrl *m_textOut, | |
226 | *m_textIn, | |
227 | *m_textErr; | |
228 | ||
229 | DECLARE_EVENT_TABLE() | |
230 | }; | |
231 | ||
232 | // ---------------------------------------------------------------------------- | |
233 | // wxProcess-derived classes | |
234 | // ---------------------------------------------------------------------------- | |
235 | ||
236 | // This is the handler for process termination events | |
237 | class MyProcess : public wxProcess | |
238 | { | |
239 | public: | |
240 | MyProcess(MyFrame *parent, const wxString& cmd) | |
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 | ||
251 | protected: | |
252 | MyFrame *m_parent; | |
253 | wxString m_cmd; | |
254 | }; | |
255 | ||
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 | { | |
263 | Redirect(); | |
264 | } | |
265 | ||
266 | virtual void OnTerminate(int pid, int status); | |
267 | ||
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; | |
285 | }; | |
286 | ||
287 | // ---------------------------------------------------------------------------- | |
288 | // constants | |
289 | // ---------------------------------------------------------------------------- | |
290 | ||
291 | // IDs for the controls and the menu commands | |
292 | enum | |
293 | { | |
294 | // menu items | |
295 | Exec_Quit = 100, | |
296 | Exec_Kill, | |
297 | Exec_ClearLog, | |
298 | Exec_BeginBusyCursor, | |
299 | Exec_EndBusyCursor, | |
300 | Exec_SyncExec = 200, | |
301 | Exec_SyncNoEventsExec, | |
302 | Exec_AsyncExec, | |
303 | Exec_Shell, | |
304 | Exec_POpen, | |
305 | Exec_OpenFile, | |
306 | Exec_OpenURL, | |
307 | Exec_DDEExec, | |
308 | Exec_DDERequest, | |
309 | Exec_Redirect, | |
310 | Exec_Pipe, | |
311 | Exec_About = 300, | |
312 | ||
313 | // control ids | |
314 | Exec_Btn_Send = 1000, | |
315 | Exec_Btn_SendFile, | |
316 | Exec_Btn_Get, | |
317 | Exec_Btn_Close | |
318 | }; | |
319 | ||
320 | static const wxChar *DIALOG_TITLE = _T("Exec sample"); | |
321 | ||
322 | // ---------------------------------------------------------------------------- | |
323 | // event tables and other macros for wxWidgets | |
324 | // ---------------------------------------------------------------------------- | |
325 | ||
326 | // the event tables connect the wxWidgets events with the functions (event | |
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) | |
331 | EVT_MENU(Exec_Kill, MyFrame::OnKill) | |
332 | EVT_MENU(Exec_ClearLog, MyFrame::OnClear) | |
333 | EVT_MENU(Exec_BeginBusyCursor, MyFrame::OnBeginBusyCursor) | |
334 | EVT_MENU(Exec_EndBusyCursor, MyFrame::OnEndBusyCursor) | |
335 | ||
336 | EVT_MENU(Exec_SyncExec, MyFrame::OnSyncExec) | |
337 | EVT_MENU(Exec_SyncNoEventsExec, MyFrame::OnSyncNoEventsExec) | |
338 | EVT_MENU(Exec_AsyncExec, MyFrame::OnAsyncExec) | |
339 | EVT_MENU(Exec_Shell, MyFrame::OnShell) | |
340 | EVT_MENU(Exec_Redirect, MyFrame::OnExecWithRedirect) | |
341 | EVT_MENU(Exec_Pipe, MyFrame::OnExecWithPipe) | |
342 | ||
343 | EVT_MENU(Exec_POpen, MyFrame::OnPOpen) | |
344 | ||
345 | EVT_MENU(Exec_OpenFile, MyFrame::OnFileExec) | |
346 | EVT_MENU(Exec_OpenURL, MyFrame::OnOpenURL) | |
347 | ||
348 | #ifdef __WINDOWS__ | |
349 | EVT_MENU(Exec_DDEExec, MyFrame::OnDDEExec) | |
350 | EVT_MENU(Exec_DDERequest, MyFrame::OnDDERequest) | |
351 | #endif // __WINDOWS__ | |
352 | ||
353 | EVT_MENU(Exec_About, MyFrame::OnAbout) | |
354 | ||
355 | EVT_IDLE(MyFrame::OnIdle) | |
356 | ||
357 | EVT_TIMER(wxID_ANY, MyFrame::OnTimer) | |
358 | END_EVENT_TABLE() | |
359 | ||
360 | BEGIN_EVENT_TABLE(MyPipeFrame, wxFrame) | |
361 | EVT_BUTTON(Exec_Btn_Send, MyPipeFrame::OnBtnSend) | |
362 | EVT_BUTTON(Exec_Btn_SendFile, MyPipeFrame::OnBtnSendFile) | |
363 | EVT_BUTTON(Exec_Btn_Get, MyPipeFrame::OnBtnGet) | |
364 | EVT_BUTTON(Exec_Btn_Close, MyPipeFrame::OnBtnClose) | |
365 | ||
366 | EVT_TEXT_ENTER(wxID_ANY, MyPipeFrame::OnTextEnter) | |
367 | ||
368 | EVT_CLOSE(MyPipeFrame::OnClose) | |
369 | ||
370 | EVT_END_PROCESS(wxID_ANY, MyPipeFrame::OnProcessTerm) | |
371 | END_EVENT_TABLE() | |
372 | ||
373 | // Create a new application object: this macro will allow wxWidgets to create | |
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 | { | |
391 | if ( !wxApp::OnInit() ) | |
392 | return false; | |
393 | ||
394 | // Create the main application window | |
395 | MyFrame *frame = new MyFrame(_T("Exec wxWidgets sample"), | |
396 | wxDefaultPosition, wxSize(500, 140)); | |
397 | ||
398 | // Show it and tell the application that it's our main window | |
399 | frame->Show(true); | |
400 | SetTopWindow(frame); | |
401 | ||
402 | // success: wxApp::OnRun() will be called which will enter the main message | |
403 | // loop and the application will run. If we returned false here, the | |
404 | // application would exit immediately. | |
405 | return true; | |
406 | } | |
407 | ||
408 | // ---------------------------------------------------------------------------- | |
409 | // main frame | |
410 | // ---------------------------------------------------------------------------- | |
411 | ||
412 | #ifdef __VISUALC__ | |
413 | #pragma warning(disable: 4355) // this used in base member initializer list | |
414 | #endif | |
415 | ||
416 | // frame constructor | |
417 | MyFrame::MyFrame(const wxString& title, const wxPoint& pos, const wxSize& size) | |
418 | : wxFrame((wxFrame *)NULL, wxID_ANY, title, pos, size), | |
419 | m_timerIdleWakeUp(this) | |
420 | { | |
421 | m_pidLast = 0; | |
422 | ||
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 | ||
429 | // create a menu bar | |
430 | wxMenu *menuFile = new wxMenu(wxEmptyString, wxMENU_TEAROFF); | |
431 | menuFile->Append(Exec_Kill, _T("&Kill process...\tCtrl-K"), | |
432 | _T("Kill a process by PID")); | |
433 | menuFile->AppendSeparator(); | |
434 | menuFile->Append(Exec_ClearLog, _T("&Clear log\tCtrl-L"), | |
435 | _T("Clear the log window")); | |
436 | menuFile->AppendSeparator(); | |
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(); | |
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")); | |
445 | execMenu->Append(Exec_SyncNoEventsExec, _T("Sync execution and &block...\tCtrl-B"), | |
446 | _T("Launch a program and block until it terminates")); | |
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")); | |
451 | execMenu->AppendSeparator(); | |
452 | execMenu->Append(Exec_Redirect, _T("Capture command &output...\tCtrl-O"), | |
453 | _T("Launch a program and capture its output")); | |
454 | execMenu->Append(Exec_Pipe, _T("&Pipe through command..."), | |
455 | _T("Pipe a string through a filter")); | |
456 | execMenu->Append(Exec_POpen, _T("&Open a pipe to a command...\tCtrl-P"), | |
457 | _T("Open a pipe to and from another program")); | |
458 | ||
459 | execMenu->AppendSeparator(); | |
460 | execMenu->Append(Exec_OpenFile, _T("Open &file...\tCtrl-F"), | |
461 | _T("Launch the command to open this kind of files")); | |
462 | execMenu->Append(Exec_OpenURL, _T("Open &URL...\tCtrl-U"), | |
463 | _T("Launch the default browser with the given URL")); | |
464 | #ifdef __WINDOWS__ | |
465 | execMenu->AppendSeparator(); | |
466 | execMenu->Append(Exec_DDEExec, _T("Execute command via &DDE...\tCtrl-D")); | |
467 | execMenu->Append(Exec_DDERequest, _T("Send DDE &request...\tCtrl-R")); | |
468 | #endif | |
469 | ||
470 | wxMenu *helpMenu = new wxMenu(wxEmptyString, wxMENU_TEAROFF); | |
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 | ||
482 | // create the listbox in which we will show misc messages as they come | |
483 | m_lbox = new wxListBox(this, wxID_ANY); | |
484 | wxFont font(12, wxFONTFAMILY_TELETYPE, wxFONTSTYLE_NORMAL, | |
485 | wxFONTWEIGHT_NORMAL); | |
486 | if ( font.Ok() ) | |
487 | m_lbox->SetFont(font); | |
488 | ||
489 | #if wxUSE_STATUSBAR | |
490 | // create a status bar just for fun (by default with 1 pane only) | |
491 | CreateStatusBar(); | |
492 | SetStatusText(_T("Welcome to wxWidgets exec sample!")); | |
493 | #endif // wxUSE_STATUSBAR | |
494 | } | |
495 | ||
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 | ||
507 | // ---------------------------------------------------------------------------- | |
508 | // event handlers: file and help menu | |
509 | // ---------------------------------------------------------------------------- | |
510 | ||
511 | void MyFrame::OnQuit(wxCommandEvent& WXUNUSED(event)) | |
512 | { | |
513 | // true is to force the frame to close | |
514 | Close(true); | |
515 | } | |
516 | ||
517 | void MyFrame::OnClear(wxCommandEvent& WXUNUSED(event)) | |
518 | { | |
519 | m_lbox->Clear(); | |
520 | } | |
521 | ||
522 | void MyFrame::OnBeginBusyCursor(wxCommandEvent& WXUNUSED(event)) | |
523 | { | |
524 | wxBeginBusyCursor(); | |
525 | } | |
526 | ||
527 | void MyFrame::OnEndBusyCursor(wxCommandEvent& WXUNUSED(event)) | |
528 | { | |
529 | wxEndBusyCursor(); | |
530 | } | |
531 | ||
532 | void MyFrame::OnAbout(wxCommandEvent& WXUNUSED(event)) | |
533 | { | |
534 | wxMessageBox(_T("Exec wxWidgets Sample\n(c) 2000-2002 Vadim Zeitlin"), | |
535 | _T("About Exec"), wxOK | wxICON_INFORMATION, this); | |
536 | } | |
537 | ||
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, | |
544 | // we need the full unsigned int range | |
545 | -INT_MAX, INT_MAX, | |
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) ) | |
609 | wxLogStatus(_T("Process %ld is running."), pid); | |
610 | else | |
611 | wxLogStatus(_T("No process with pid = %ld."), pid); | |
612 | } | |
613 | else // not SIGNONE | |
614 | { | |
615 | wxKillError rc = wxProcess::Kill(pid, (wxSignal)sig); | |
616 | if ( rc == wxKILL_OK ) | |
617 | { | |
618 | wxLogStatus(_T("Process %ld killed with signal %d."), pid, sig); | |
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 | ||
631 | wxLogStatus(_T("Failed to kill process %ld with signal %d: %s"), | |
632 | pid, sig, errorText[rc]); | |
633 | } | |
634 | } | |
635 | } | |
636 | ||
637 | // ---------------------------------------------------------------------------- | |
638 | // event handlers: exec menu | |
639 | // ---------------------------------------------------------------------------- | |
640 | ||
641 | void MyFrame::DoAsyncExec(const wxString& cmd) | |
642 | { | |
643 | MyProcess * const process = new MyProcess(this, cmd); | |
644 | m_pidLast = wxExecute(cmd, wxEXEC_ASYNC, process); | |
645 | if ( !m_pidLast ) | |
646 | { | |
647 | wxLogError(_T("Execution of '%s' failed."), cmd.c_str()); | |
648 | ||
649 | delete process; | |
650 | } | |
651 | else | |
652 | { | |
653 | wxLogStatus(_T("Process %ld (%s) launched."), m_pidLast, cmd.c_str()); | |
654 | ||
655 | m_cmdLast = cmd; | |
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); | |
660 | } | |
661 | } | |
662 | ||
663 | void MyFrame::OnSyncExec(wxCommandEvent& WXUNUSED(event)) | |
664 | { | |
665 | wxString cmd = wxGetTextFromUser(_T("Enter the command: "), | |
666 | DIALOG_TITLE, | |
667 | m_cmdLast); | |
668 | ||
669 | if ( !cmd ) | |
670 | return; | |
671 | ||
672 | wxLogStatus( _T("'%s' is running please wait..."), cmd.c_str() ); | |
673 | ||
674 | int code = wxExecute(cmd, wxEXEC_SYNC); | |
675 | ||
676 | wxLogStatus(_T("Process '%s' terminated with exit code %d."), | |
677 | cmd.c_str(), code); | |
678 | ||
679 | m_cmdLast = cmd; | |
680 | } | |
681 | ||
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 | ||
701 | void MyFrame::OnAsyncExec(wxCommandEvent& WXUNUSED(event)) | |
702 | { | |
703 | wxString cmd = wxGetTextFromUser(_T("Enter the command: "), | |
704 | DIALOG_TITLE, | |
705 | m_cmdLast); | |
706 | ||
707 | if ( !cmd ) | |
708 | return; | |
709 | ||
710 | DoAsyncExec(cmd); | |
711 | } | |
712 | ||
713 | void MyFrame::OnShell(wxCommandEvent& WXUNUSED(event)) | |
714 | { | |
715 | wxString cmd = wxGetTextFromUser(_T("Enter the command: "), | |
716 | DIALOG_TITLE, | |
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 | ||
728 | void MyFrame::OnExecWithRedirect(wxCommandEvent& WXUNUSED(event)) | |
729 | { | |
730 | wxString cmd = wxGetTextFromUser(_T("Enter the command: "), | |
731 | DIALOG_TITLE, | |
732 | m_cmdLast); | |
733 | ||
734 | if ( !cmd ) | |
735 | return; | |
736 | ||
737 | bool sync; | |
738 | switch ( wxMessageBox(_T("Execute it synchronously?"), | |
739 | _T("Exec question"), | |
740 | wxYES_NO | wxCANCEL | wxICON_QUESTION, this) ) | |
741 | { | |
742 | case wxYES: | |
743 | sync = true; | |
744 | break; | |
745 | ||
746 | case wxNO: | |
747 | sync = false; | |
748 | break; | |
749 | ||
750 | default: | |
751 | return; | |
752 | } | |
753 | ||
754 | if ( sync ) | |
755 | { | |
756 | wxLogStatus( _T("'%s' is running please wait..."), cmd.c_str() ); | |
757 | ||
758 | wxArrayString output, errors; | |
759 | int code = wxExecute(cmd, output, errors); | |
760 | ||
761 | wxLogStatus(_T("Command '%s' terminated with exit code %d."), | |
762 | cmd.c_str(), code); | |
763 | ||
764 | if ( code != -1 ) | |
765 | { | |
766 | ShowOutput(cmd, output, _T("Output")); | |
767 | ShowOutput(cmd, errors, _T("Errors")); | |
768 | } | |
769 | } | |
770 | else // async exec | |
771 | { | |
772 | MyPipedProcess *process = new MyPipedProcess(this, cmd); | |
773 | if ( !wxExecute(cmd, wxEXEC_ASYNC, process) ) | |
774 | { | |
775 | wxLogError(_T("Execution of '%s' failed."), cmd.c_str()); | |
776 | ||
777 | delete process; | |
778 | } | |
779 | else | |
780 | { | |
781 | AddPipedProcess(process); | |
782 | } | |
783 | } | |
784 | ||
785 | m_cmdLast = cmd; | |
786 | } | |
787 | ||
788 | void MyFrame::OnExecWithPipe(wxCommandEvent& WXUNUSED(event)) | |
789 | { | |
790 | if ( !m_cmdLast ) | |
791 | m_cmdLast = _T("tr [a-z] [A-Z]"); | |
792 | ||
793 | wxString cmd = wxGetTextFromUser(_T("Enter the command: "), | |
794 | DIALOG_TITLE, | |
795 | m_cmdLast); | |
796 | ||
797 | if ( !cmd ) | |
798 | return; | |
799 | ||
800 | wxString input = wxGetTextFromUser(_T("Enter the string to send to it: "), | |
801 | DIALOG_TITLE); | |
802 | if ( !input ) | |
803 | return; | |
804 | ||
805 | // always execute the filter asynchronously | |
806 | MyPipedProcess2 *process = new MyPipedProcess2(this, cmd, input); | |
807 | long pid = wxExecute(cmd, wxEXEC_ASYNC, process); | |
808 | if ( pid ) | |
809 | { | |
810 | wxLogStatus(_T("Process %ld (%s) launched."), pid, cmd.c_str()); | |
811 | ||
812 | AddPipedProcess(process); | |
813 | } | |
814 | else | |
815 | { | |
816 | wxLogError(_T("Execution of '%s' failed."), cmd.c_str()); | |
817 | ||
818 | delete process; | |
819 | } | |
820 | ||
821 | m_cmdLast = cmd; | |
822 | } | |
823 | ||
824 | void MyFrame::OnPOpen(wxCommandEvent& WXUNUSED(event)) | |
825 | { | |
826 | wxString cmd = wxGetTextFromUser(_T("Enter the command to launch: "), | |
827 | DIALOG_TITLE, | |
828 | m_cmdLast); | |
829 | if ( cmd.empty() ) | |
830 | return; | |
831 | ||
832 | wxProcess *process = wxProcess::Open(cmd); | |
833 | if ( !process ) | |
834 | { | |
835 | wxLogError(_T("Failed to launch the command.")); | |
836 | return; | |
837 | } | |
838 | ||
839 | wxLogVerbose(_T("PID of the new process: %ld"), process->GetPid()); | |
840 | ||
841 | wxOutputStream *out = process->GetOutputStream(); | |
842 | if ( !out ) | |
843 | { | |
844 | wxLogError(_T("Failed to connect to child stdin")); | |
845 | return; | |
846 | } | |
847 | ||
848 | wxInputStream *in = process->GetInputStream(); | |
849 | if ( !in ) | |
850 | { | |
851 | wxLogError(_T("Failed to connect to child stdout")); | |
852 | return; | |
853 | } | |
854 | ||
855 | new MyPipeFrame(this, cmd, process); | |
856 | } | |
857 | ||
858 | void MyFrame::OnFileExec(wxCommandEvent& WXUNUSED(event)) | |
859 | { | |
860 | static wxString s_filename; | |
861 | ||
862 | wxString filename; | |
863 | ||
864 | #if wxUSE_FILEDLG | |
865 | filename = wxLoadFileSelector(_T("any file"), wxEmptyString, s_filename, this); | |
866 | #else // !wxUSE_FILEDLG | |
867 | filename = wxGetTextFromUser(_T("Enter the file name"), _T("exec sample"), | |
868 | s_filename, this); | |
869 | #endif // wxUSE_FILEDLG/!wxUSE_FILEDLG | |
870 | ||
871 | if ( filename.empty() ) | |
872 | return; | |
873 | ||
874 | s_filename = filename; | |
875 | ||
876 | wxString ext = filename.AfterFirst(_T('.')); | |
877 | wxFileType *ft = wxTheMimeTypesManager->GetFileTypeFromExtension(ext); | |
878 | if ( !ft ) | |
879 | { | |
880 | wxLogError(_T("Impossible to determine the file type for extension '%s'"), | |
881 | ext.c_str()); | |
882 | return; | |
883 | } | |
884 | ||
885 | wxString cmd; | |
886 | bool ok = ft->GetOpenCommand(&cmd, | |
887 | wxFileType::MessageParameters(filename)); | |
888 | delete ft; | |
889 | if ( !ok ) | |
890 | { | |
891 | wxLogError(_T("Impossible to find out how to open files of extension '%s'"), | |
892 | ext.c_str()); | |
893 | return; | |
894 | } | |
895 | ||
896 | DoAsyncExec(cmd); | |
897 | } | |
898 | ||
899 | void MyFrame::OnOpenURL(wxCommandEvent& WXUNUSED(event)) | |
900 | { | |
901 | static wxString s_filename(_T("http://www.wxwidgets.org/")); | |
902 | ||
903 | wxString filename = wxGetTextFromUser | |
904 | ( | |
905 | _T("Enter the URL"), | |
906 | _T("exec sample"), | |
907 | s_filename, | |
908 | this | |
909 | ); | |
910 | ||
911 | if ( filename.empty() ) | |
912 | return; | |
913 | ||
914 | s_filename = filename; | |
915 | ||
916 | if ( !wxLaunchDefaultBrowser(s_filename) ) | |
917 | wxLogError(_T("Failed to open URL \"%s\""), s_filename.c_str()); | |
918 | } | |
919 | ||
920 | // ---------------------------------------------------------------------------- | |
921 | // DDE stuff | |
922 | // ---------------------------------------------------------------------------- | |
923 | ||
924 | #ifdef __WINDOWS__ | |
925 | ||
926 | bool MyFrame::GetDDEServer() | |
927 | { | |
928 | wxString server = wxGetTextFromUser(_T("Server to connect to:"), | |
929 | DIALOG_TITLE, m_server); | |
930 | if ( !server ) | |
931 | return false; | |
932 | ||
933 | m_server = server; | |
934 | ||
935 | wxString topic = wxGetTextFromUser(_T("DDE topic:"), DIALOG_TITLE, m_topic); | |
936 | if ( !topic ) | |
937 | return false; | |
938 | ||
939 | m_topic = topic; | |
940 | ||
941 | wxString cmd = wxGetTextFromUser(_T("DDE command:"), DIALOG_TITLE, m_cmdDde); | |
942 | if ( !cmd ) | |
943 | return false; | |
944 | ||
945 | m_cmdDde = cmd; | |
946 | ||
947 | return true; | |
948 | } | |
949 | ||
950 | void MyFrame::OnDDEExec(wxCommandEvent& WXUNUSED(event)) | |
951 | { | |
952 | if ( !GetDDEServer() ) | |
953 | return; | |
954 | ||
955 | wxDDEClient client; | |
956 | wxConnectionBase *conn = client.MakeConnection(wxEmptyString, m_server, m_topic); | |
957 | if ( !conn ) | |
958 | { | |
959 | wxLogError(_T("Failed to connect to the DDE server '%s'."), | |
960 | m_server.c_str()); | |
961 | } | |
962 | else | |
963 | { | |
964 | if ( !conn->Execute(m_cmdDde) ) | |
965 | { | |
966 | wxLogError(_T("Failed to execute command '%s' via DDE."), | |
967 | m_cmdDde.c_str()); | |
968 | } | |
969 | else | |
970 | { | |
971 | wxLogStatus(_T("Successfully executed DDE command")); | |
972 | } | |
973 | } | |
974 | } | |
975 | ||
976 | void MyFrame::OnDDERequest(wxCommandEvent& WXUNUSED(event)) | |
977 | { | |
978 | if ( !GetDDEServer() ) | |
979 | return; | |
980 | ||
981 | wxDDEClient client; | |
982 | wxConnectionBase *conn = client.MakeConnection(wxEmptyString, m_server, m_topic); | |
983 | if ( !conn ) | |
984 | { | |
985 | wxLogError(_T("Failed to connect to the DDE server '%s'."), | |
986 | m_server.c_str()); | |
987 | } | |
988 | else | |
989 | { | |
990 | if ( !conn->Request(m_cmdDde) ) | |
991 | { | |
992 | wxLogError(_T("Failed to send request '%s' via DDE."), | |
993 | m_cmdDde.c_str()); | |
994 | } | |
995 | else | |
996 | { | |
997 | wxLogStatus(_T("Successfully sent DDE request.")); | |
998 | } | |
999 | } | |
1000 | } | |
1001 | ||
1002 | #endif // __WINDOWS__ | |
1003 | ||
1004 | // ---------------------------------------------------------------------------- | |
1005 | // various helpers | |
1006 | // ---------------------------------------------------------------------------- | |
1007 | ||
1008 | // input polling | |
1009 | void MyFrame::OnIdle(wxIdleEvent& event) | |
1010 | { | |
1011 | size_t count = m_running.GetCount(); | |
1012 | for ( size_t n = 0; n < count; n++ ) | |
1013 | { | |
1014 | if ( m_running[n]->HasInput() ) | |
1015 | { | |
1016 | event.RequestMore(); | |
1017 | } | |
1018 | } | |
1019 | } | |
1020 | ||
1021 | void MyFrame::OnTimer(wxTimerEvent& WXUNUSED(event)) | |
1022 | { | |
1023 | wxWakeUpIdle(); | |
1024 | } | |
1025 | ||
1026 | void MyFrame::OnProcessTerminated(MyPipedProcess *process) | |
1027 | { | |
1028 | RemovePipedProcess(process); | |
1029 | } | |
1030 | ||
1031 | void MyFrame::OnAsyncTermination(MyProcess *process) | |
1032 | { | |
1033 | m_allAsync.Remove(process); | |
1034 | ||
1035 | delete process; | |
1036 | } | |
1037 | ||
1038 | void MyFrame::AddPipedProcess(MyPipedProcess *process) | |
1039 | { | |
1040 | if ( m_running.IsEmpty() ) | |
1041 | { | |
1042 | // we want to start getting the timer events to ensure that a | |
1043 | // steady stream of idle events comes in -- otherwise we | |
1044 | // wouldn't be able to poll the child process input | |
1045 | m_timerIdleWakeUp.Start(100); | |
1046 | } | |
1047 | //else: the timer is already running | |
1048 | ||
1049 | m_running.Add(process); | |
1050 | m_allAsync.Add(process); | |
1051 | } | |
1052 | ||
1053 | void MyFrame::RemovePipedProcess(MyPipedProcess *process) | |
1054 | { | |
1055 | m_running.Remove(process); | |
1056 | ||
1057 | if ( m_running.IsEmpty() ) | |
1058 | { | |
1059 | // we don't need to get idle events all the time any more | |
1060 | m_timerIdleWakeUp.Stop(); | |
1061 | } | |
1062 | } | |
1063 | ||
1064 | void MyFrame::ShowOutput(const wxString& cmd, | |
1065 | const wxArrayString& output, | |
1066 | const wxString& title) | |
1067 | { | |
1068 | size_t count = output.GetCount(); | |
1069 | if ( !count ) | |
1070 | return; | |
1071 | ||
1072 | m_lbox->Append(wxString::Format(_T("--- %s of '%s' ---"), | |
1073 | title.c_str(), cmd.c_str())); | |
1074 | ||
1075 | for ( size_t n = 0; n < count; n++ ) | |
1076 | { | |
1077 | m_lbox->Append(output[n]); | |
1078 | } | |
1079 | ||
1080 | m_lbox->Append(wxString::Format(_T("--- End of %s ---"), | |
1081 | title.Lower().c_str())); | |
1082 | } | |
1083 | ||
1084 | // ---------------------------------------------------------------------------- | |
1085 | // MyProcess | |
1086 | // ---------------------------------------------------------------------------- | |
1087 | ||
1088 | void MyProcess::OnTerminate(int pid, int status) | |
1089 | { | |
1090 | wxLogStatus(m_parent, _T("Process %u ('%s') terminated with exit code %d."), | |
1091 | pid, m_cmd.c_str(), status); | |
1092 | ||
1093 | m_parent->OnAsyncTermination(this); | |
1094 | } | |
1095 | ||
1096 | // ---------------------------------------------------------------------------- | |
1097 | // MyPipedProcess | |
1098 | // ---------------------------------------------------------------------------- | |
1099 | ||
1100 | bool MyPipedProcess::HasInput() | |
1101 | { | |
1102 | bool hasInput = false; | |
1103 | ||
1104 | if ( IsInputAvailable() ) | |
1105 | { | |
1106 | wxTextInputStream tis(*GetInputStream()); | |
1107 | ||
1108 | // this assumes that the output is always line buffered | |
1109 | wxString msg; | |
1110 | msg << m_cmd << _T(" (stdout): ") << tis.ReadLine(); | |
1111 | ||
1112 | m_parent->GetLogListBox()->Append(msg); | |
1113 | ||
1114 | hasInput = true; | |
1115 | } | |
1116 | ||
1117 | if ( IsErrorAvailable() ) | |
1118 | { | |
1119 | wxTextInputStream tis(*GetErrorStream()); | |
1120 | ||
1121 | // this assumes that the output is always line buffered | |
1122 | wxString msg; | |
1123 | msg << m_cmd << _T(" (stderr): ") << tis.ReadLine(); | |
1124 | ||
1125 | m_parent->GetLogListBox()->Append(msg); | |
1126 | ||
1127 | hasInput = true; | |
1128 | } | |
1129 | ||
1130 | return hasInput; | |
1131 | } | |
1132 | ||
1133 | void MyPipedProcess::OnTerminate(int pid, int status) | |
1134 | { | |
1135 | // show the rest of the output | |
1136 | while ( HasInput() ) | |
1137 | ; | |
1138 | ||
1139 | m_parent->OnProcessTerminated(this); | |
1140 | ||
1141 | MyProcess::OnTerminate(pid, status); | |
1142 | } | |
1143 | ||
1144 | // ---------------------------------------------------------------------------- | |
1145 | // MyPipedProcess2 | |
1146 | // ---------------------------------------------------------------------------- | |
1147 | ||
1148 | bool MyPipedProcess2::HasInput() | |
1149 | { | |
1150 | if ( !m_input.empty() ) | |
1151 | { | |
1152 | wxTextOutputStream os(*GetOutputStream()); | |
1153 | os.WriteString(m_input); | |
1154 | ||
1155 | CloseOutput(); | |
1156 | m_input.clear(); | |
1157 | ||
1158 | // call us once again - may be we'll have output | |
1159 | return true; | |
1160 | } | |
1161 | ||
1162 | return MyPipedProcess::HasInput(); | |
1163 | } | |
1164 | ||
1165 | // ============================================================================ | |
1166 | // MyPipeFrame implementation | |
1167 | // ============================================================================ | |
1168 | ||
1169 | MyPipeFrame::MyPipeFrame(wxFrame *parent, | |
1170 | const wxString& cmd, | |
1171 | wxProcess *process) | |
1172 | : wxFrame(parent, wxID_ANY, cmd), | |
1173 | m_process(process), | |
1174 | // in a real program we'd check that the streams are !NULL here | |
1175 | m_out(*process->GetOutputStream()), | |
1176 | m_in(*process->GetInputStream()), | |
1177 | m_err(*process->GetErrorStream()) | |
1178 | { | |
1179 | m_process->SetNextHandler(this); | |
1180 | ||
1181 | wxPanel *panel = new wxPanel(this, wxID_ANY); | |
1182 | ||
1183 | m_textOut = new wxTextCtrl(panel, wxID_ANY, wxEmptyString, | |
1184 | wxDefaultPosition, wxDefaultSize, | |
1185 | wxTE_PROCESS_ENTER); | |
1186 | m_textIn = new wxTextCtrl(panel, wxID_ANY, wxEmptyString, | |
1187 | wxDefaultPosition, wxDefaultSize, | |
1188 | wxTE_MULTILINE | wxTE_RICH); | |
1189 | m_textIn->SetEditable(false); | |
1190 | m_textErr = new wxTextCtrl(panel, wxID_ANY, wxEmptyString, | |
1191 | wxDefaultPosition, wxDefaultSize, | |
1192 | wxTE_MULTILINE | wxTE_RICH); | |
1193 | m_textErr->SetEditable(false); | |
1194 | ||
1195 | wxSizer *sizerTop = new wxBoxSizer(wxVERTICAL); | |
1196 | sizerTop->Add(m_textOut, 0, wxGROW | wxALL, 5); | |
1197 | ||
1198 | wxSizer *sizerBtns = new wxBoxSizer(wxHORIZONTAL); | |
1199 | sizerBtns-> | |
1200 | Add(new wxButton(panel, Exec_Btn_Send, _T("&Send")), 0, wxALL, 5); | |
1201 | sizerBtns-> | |
1202 | Add(new wxButton(panel, Exec_Btn_SendFile, _T("&File...")), 0, wxALL, 5); | |
1203 | sizerBtns-> | |
1204 | Add(new wxButton(panel, Exec_Btn_Get, _T("&Get")), 0, wxALL, 5); | |
1205 | sizerBtns-> | |
1206 | Add(new wxButton(panel, Exec_Btn_Close, _T("&Close")), 0, wxALL, 5); | |
1207 | ||
1208 | sizerTop->Add(sizerBtns, 0, wxCENTRE | wxALL, 5); | |
1209 | sizerTop->Add(m_textIn, 1, wxGROW | wxALL, 5); | |
1210 | sizerTop->Add(m_textErr, 1, wxGROW | wxALL, 5); | |
1211 | ||
1212 | panel->SetSizer(sizerTop); | |
1213 | sizerTop->Fit(this); | |
1214 | ||
1215 | Show(); | |
1216 | } | |
1217 | ||
1218 | void MyPipeFrame::OnBtnSendFile(wxCommandEvent& WXUNUSED(event)) | |
1219 | { | |
1220 | #if wxUSE_FILEDLG | |
1221 | wxFileDialog filedlg(this, _T("Select file to send")); | |
1222 | if ( filedlg.ShowModal() != wxID_OK ) | |
1223 | return; | |
1224 | ||
1225 | wxFFile file(filedlg.GetFilename(), _T("r")); | |
1226 | wxString data; | |
1227 | if ( !file.IsOpened() || !file.ReadAll(&data) ) | |
1228 | return; | |
1229 | ||
1230 | // can't write the entire string at once, this risk overflowing the pipe | |
1231 | // and we would dead lock | |
1232 | size_t len = data.length(); | |
1233 | const wxChar *pc = data.c_str(); | |
1234 | while ( len ) | |
1235 | { | |
1236 | const size_t CHUNK_SIZE = 4096; | |
1237 | m_out.Write(pc, len > CHUNK_SIZE ? CHUNK_SIZE : len); | |
1238 | ||
1239 | // note that not all data could have been written as we don't block on | |
1240 | // the write end of the pipe | |
1241 | const size_t lenChunk = m_out.LastWrite(); | |
1242 | ||
1243 | pc += lenChunk; | |
1244 | len -= lenChunk; | |
1245 | ||
1246 | DoGet(); | |
1247 | } | |
1248 | #endif // wxUSE_FILEDLG | |
1249 | } | |
1250 | ||
1251 | void MyPipeFrame::DoGet() | |
1252 | { | |
1253 | // we don't have any way to be notified when any input appears on the | |
1254 | // stream so we have to poll it :-( | |
1255 | DoGetFromStream(m_textIn, m_in); | |
1256 | DoGetFromStream(m_textErr, m_err); | |
1257 | } | |
1258 | ||
1259 | void MyPipeFrame::DoGetFromStream(wxTextCtrl *text, wxInputStream& in) | |
1260 | { | |
1261 | while ( in.CanRead() ) | |
1262 | { | |
1263 | wxChar buffer[4096]; | |
1264 | buffer[in.Read(buffer, WXSIZEOF(buffer) - 1).LastRead()] = _T('\0'); | |
1265 | ||
1266 | text->AppendText(buffer); | |
1267 | } | |
1268 | } | |
1269 | ||
1270 | void MyPipeFrame::DoClose() | |
1271 | { | |
1272 | m_process->CloseOutput(); | |
1273 | ||
1274 | DisableInput(); | |
1275 | } | |
1276 | ||
1277 | void MyPipeFrame::DisableInput() | |
1278 | { | |
1279 | m_textOut->SetEditable(false); | |
1280 | FindWindow(Exec_Btn_Send)->Disable(); | |
1281 | FindWindow(Exec_Btn_SendFile)->Disable(); | |
1282 | FindWindow(Exec_Btn_Close)->Disable(); | |
1283 | } | |
1284 | ||
1285 | void MyPipeFrame::DisableOutput() | |
1286 | { | |
1287 | FindWindow(Exec_Btn_Get)->Disable(); | |
1288 | } | |
1289 | ||
1290 | void MyPipeFrame::OnClose(wxCloseEvent& event) | |
1291 | { | |
1292 | if ( m_process ) | |
1293 | { | |
1294 | // we're not interested in getting the process termination notification | |
1295 | // if we are closing it ourselves | |
1296 | wxProcess *process = m_process; | |
1297 | m_process = NULL; | |
1298 | process->SetNextHandler(NULL); | |
1299 | ||
1300 | process->CloseOutput(); | |
1301 | } | |
1302 | ||
1303 | event.Skip(); | |
1304 | } | |
1305 | ||
1306 | void MyPipeFrame::OnProcessTerm(wxProcessEvent& WXUNUSED(event)) | |
1307 | { | |
1308 | DoGet(); | |
1309 | ||
1310 | delete m_process; | |
1311 | m_process = NULL; | |
1312 | ||
1313 | wxLogWarning(_T("The other process has terminated, closing")); | |
1314 | ||
1315 | DisableInput(); | |
1316 | DisableOutput(); | |
1317 | } |