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