]> git.saurik.com Git - wxWidgets.git/blob - samples/exec/exec.cpp
added wxGetTopLevelParent()
[wxWidgets.git] / samples / exec / exec.cpp
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 #ifdef __GNUG__
21 #pragma implementation "exec.cpp"
22 #pragma interface "exec.cpp"
23 #endif
24
25 // For compilers that support precompilation, includes "wx/wx.h".
26 #include "wx/wxprec.h"
27
28 #ifdef __BORLANDC__
29 #pragma hdrstop
30 #endif
31
32 // for all others, include the necessary headers (this file is usually all you
33 // need because it includes almost all "standard" wxWindows headers
34 #ifndef WX_PRECOMP
35 #include "wx/app.h"
36 #include "wx/frame.h"
37 #include "wx/utils.h"
38 #include "wx/menu.h"
39 #include "wx/msgdlg.h"
40 #include "wx/textdlg.h"
41 #include "wx/listbox.h"
42 #include "wx/filedlg.h"
43 #include "wx/choicdlg.h"
44 #endif
45
46 #include "wx/txtstrm.h"
47
48 #include "wx/process.h"
49
50 #include "wx/mimetype.h"
51
52 #ifdef __WINDOWS__
53 #include "wx/dde.h"
54 #endif // __WINDOWS__
55
56 // ----------------------------------------------------------------------------
57 // private classes
58 // ----------------------------------------------------------------------------
59
60 // Define a new application type, each program should derive a class from wxApp
61 class MyApp : public wxApp
62 {
63 public:
64 // override base class virtuals
65 // ----------------------------
66
67 // this one is called on application startup and is a good place for the app
68 // initialization (doing it here and not in the ctor allows to have an error
69 // return: if OnInit() returns false, the application terminates)
70 virtual bool OnInit();
71 };
72
73 // Define an array of process pointers used by MyFrame
74 class MyPipedProcess;
75 WX_DEFINE_ARRAY(MyPipedProcess *, MyProcessesArray);
76
77 // Define a new frame type: this is going to be our main frame
78 class MyFrame : public wxFrame
79 {
80 public:
81 // ctor(s)
82 MyFrame(const wxString& title, const wxPoint& pos, const wxSize& size);
83
84 // event handlers (these functions should _not_ be virtual)
85 void OnQuit(wxCommandEvent& event);
86
87 void OnKill(wxCommandEvent& event);
88
89 void OnClear(wxCommandEvent& event);
90
91 void OnSyncExec(wxCommandEvent& event);
92 void OnAsyncExec(wxCommandEvent& event);
93 void OnShell(wxCommandEvent& event);
94 void OnExecWithRedirect(wxCommandEvent& event);
95 void OnExecWithPipe(wxCommandEvent& event);
96
97 void OnFileExec(wxCommandEvent& event);
98
99 void OnAbout(wxCommandEvent& event);
100
101 // polling output of async processes
102 void OnIdle(wxIdleEvent& event);
103
104 // for MyPipedProcess
105 void OnProcessTerminated(MyPipedProcess *process);
106 wxListBox *GetLogListBox() const { return m_lbox; }
107
108 private:
109 void ShowOutput(const wxString& cmd,
110 const wxArrayString& output,
111 const wxString& title);
112
113 void DoAsyncExec(const wxString& cmd);
114
115 // the PID of the last process we launched asynchronously
116 int m_pidLast;
117
118 // last command we executed
119 wxString m_cmdLast;
120
121 #ifdef __WINDOWS__
122 void OnDDEExec(wxCommandEvent& event);
123 void OnDDERequest(wxCommandEvent& event);
124
125 bool GetDDEServer();
126
127 // last params of a DDE transaction
128 wxString m_server,
129 m_topic,
130 m_cmdDde;
131 #endif // __WINDOWS__
132
133 wxListBox *m_lbox;
134
135 MyProcessesArray m_running;
136
137 // any class wishing to process wxWindows events must use this macro
138 DECLARE_EVENT_TABLE()
139 };
140
141 // This is the handler for process termination events
142 class MyProcess : public wxProcess
143 {
144 public:
145 MyProcess(MyFrame *parent, const wxString& cmd)
146 : wxProcess(parent), m_cmd(cmd)
147 {
148 m_parent = parent;
149 }
150
151 // instead of overriding this virtual function we might as well process the
152 // event from it in the frame class - this might be more convenient in some
153 // cases
154 virtual void OnTerminate(int pid, int status);
155
156 protected:
157 MyFrame *m_parent;
158 wxString m_cmd;
159 };
160
161 // A specialization of MyProcess for redirecting the output
162 class MyPipedProcess : public MyProcess
163 {
164 public:
165 MyPipedProcess(MyFrame *parent, const wxString& cmd)
166 : MyProcess(parent, cmd)
167 {
168 Redirect();
169 }
170
171 virtual void OnTerminate(int pid, int status);
172
173 virtual bool HasInput();
174 };
175
176 // A version of MyPipedProcess which also sends input to the stdin of the
177 // child process
178 class MyPipedProcess2 : public MyPipedProcess
179 {
180 public:
181 MyPipedProcess2(MyFrame *parent, const wxString& cmd, const wxString& input)
182 : MyPipedProcess(parent, cmd), m_input(input)
183 {
184 }
185
186 virtual bool HasInput();
187
188 private:
189 wxString m_input;
190 };
191
192 // ----------------------------------------------------------------------------
193 // constants
194 // ----------------------------------------------------------------------------
195
196 // IDs for the controls and the menu commands
197 enum
198 {
199 // menu items
200 Exec_Quit = 100,
201 Exec_Kill,
202 Exec_ClearLog,
203 Exec_SyncExec = 200,
204 Exec_AsyncExec,
205 Exec_Shell,
206 Exec_OpenFile,
207 Exec_DDEExec,
208 Exec_DDERequest,
209 Exec_Redirect,
210 Exec_Pipe,
211 Exec_About = 300
212 };
213
214 static const wxChar *DIALOG_TITLE = _T("Exec sample");
215
216 // ----------------------------------------------------------------------------
217 // event tables and other macros for wxWindows
218 // ----------------------------------------------------------------------------
219
220 // the event tables connect the wxWindows events with the functions (event
221 // handlers) which process them. It can be also done at run-time, but for the
222 // simple menu events like this the static method is much simpler.
223 BEGIN_EVENT_TABLE(MyFrame, wxFrame)
224 EVT_MENU(Exec_Quit, MyFrame::OnQuit)
225 EVT_MENU(Exec_Kill, MyFrame::OnKill)
226 EVT_MENU(Exec_ClearLog, MyFrame::OnClear)
227
228 EVT_MENU(Exec_SyncExec, MyFrame::OnSyncExec)
229 EVT_MENU(Exec_AsyncExec, MyFrame::OnAsyncExec)
230 EVT_MENU(Exec_Shell, MyFrame::OnShell)
231 EVT_MENU(Exec_Redirect, MyFrame::OnExecWithRedirect)
232 EVT_MENU(Exec_Pipe, MyFrame::OnExecWithPipe)
233
234 EVT_MENU(Exec_OpenFile, MyFrame::OnFileExec)
235
236 #ifdef __WINDOWS__
237 EVT_MENU(Exec_DDEExec, MyFrame::OnDDEExec)
238 EVT_MENU(Exec_DDERequest, MyFrame::OnDDERequest)
239 #endif // __WINDOWS__
240
241 EVT_MENU(Exec_About, MyFrame::OnAbout)
242
243 EVT_IDLE(MyFrame::OnIdle)
244 END_EVENT_TABLE()
245
246 // Create a new application object: this macro will allow wxWindows to create
247 // the application object during program execution (it's better than using a
248 // static object for many reasons) and also declares the accessor function
249 // wxGetApp() which will return the reference of the right type (i.e. MyApp and
250 // not wxApp)
251 IMPLEMENT_APP(MyApp)
252
253 // ============================================================================
254 // implementation
255 // ============================================================================
256
257 // ----------------------------------------------------------------------------
258 // the application class
259 // ----------------------------------------------------------------------------
260
261 // `Main program' equivalent: the program execution "starts" here
262 bool MyApp::OnInit()
263 {
264 // Create the main application window
265 MyFrame *frame = new MyFrame(_T("Exec wxWindows sample"),
266 wxDefaultPosition, wxSize(500, 140));
267
268 // Show it and tell the application that it's our main window
269 frame->Show(TRUE);
270 SetTopWindow(frame);
271
272 // success: wxApp::OnRun() will be called which will enter the main message
273 // loop and the application will run. If we returned FALSE here, the
274 // application would exit immediately.
275 return TRUE;
276 }
277
278 // ----------------------------------------------------------------------------
279 // main frame
280 // ----------------------------------------------------------------------------
281
282 // frame constructor
283 MyFrame::MyFrame(const wxString& title, const wxPoint& pos, const wxSize& size)
284 : wxFrame((wxFrame *)NULL, -1, title, pos, size)
285 {
286 m_pidLast = 0;
287
288 #ifdef __WXMAC__
289 // we need this in order to allow the about menu relocation, since ABOUT is
290 // not the default id of the about menu
291 wxApp::s_macAboutMenuItemId = Exec_About;
292 #endif
293
294 // create a menu bar
295 wxMenu *menuFile = new wxMenu(_T(""), wxMENU_TEAROFF);
296 menuFile->Append(Exec_Kill, _T("&Kill process...\tCtrl-K"),
297 _T("Kill a process by PID"));
298 menuFile->AppendSeparator();
299 menuFile->Append(Exec_ClearLog, _T("&Clear log\tCtrl-C"),
300 _T("Clear the log window"));
301 menuFile->AppendSeparator();
302 menuFile->Append(Exec_Quit, _T("E&xit\tAlt-X"), _T("Quit this program"));
303
304 wxMenu *execMenu = new wxMenu;
305 execMenu->Append(Exec_SyncExec, _T("Sync &execution...\tCtrl-E"),
306 _T("Launch a program and return when it terminates"));
307 execMenu->Append(Exec_AsyncExec, _T("&Async execution...\tCtrl-A"),
308 _T("Launch a program and return immediately"));
309 execMenu->Append(Exec_Shell, _T("Execute &shell command...\tCtrl-S"),
310 _T("Launch a shell and execute a command in it"));
311 execMenu->AppendSeparator();
312 execMenu->Append(Exec_Redirect, _T("Capture command &output...\tCtrl-O"),
313 _T("Launch a program and capture its output"));
314 execMenu->Append(Exec_Pipe, _T("&Pipe through command...\tCtrl-P"),
315 _T("Pipe a string through a filter"));
316
317 execMenu->AppendSeparator();
318 execMenu->Append(Exec_OpenFile, _T("Open &file...\tCtrl-F"),
319 _T("Launch the command to open this kind of files"));
320 #ifdef __WINDOWS__
321 execMenu->AppendSeparator();
322 execMenu->Append(Exec_DDEExec, _T("Execute command via &DDE...\tCtrl-D"));
323 execMenu->Append(Exec_DDERequest, _T("Send DDE &request...\tCtrl-R"));
324 #endif
325
326 wxMenu *helpMenu = new wxMenu(_T(""), wxMENU_TEAROFF);
327 helpMenu->Append(Exec_About, _T("&About...\tF1"), _T("Show about dialog"));
328
329 // now append the freshly created menu to the menu bar...
330 wxMenuBar *menuBar = new wxMenuBar();
331 menuBar->Append(menuFile, _T("&File"));
332 menuBar->Append(execMenu, _T("&Exec"));
333 menuBar->Append(helpMenu, _T("&Help"));
334
335 // ... and attach this menu bar to the frame
336 SetMenuBar(menuBar);
337
338 // create the listbox in which we will show misc messages as they come
339 m_lbox = new wxListBox(this, -1);
340 wxFont font(12, wxFONTFAMILY_TELETYPE, wxFONTSTYLE_NORMAL,
341 wxFONTWEIGHT_NORMAL);
342 if ( font.Ok() )
343 m_lbox->SetFont(font);
344
345 #if wxUSE_STATUSBAR
346 // create a status bar just for fun (by default with 1 pane only)
347 CreateStatusBar();
348 SetStatusText(_T("Welcome to wxWindows exec sample!"));
349 #endif // wxUSE_STATUSBAR
350 }
351
352 // ----------------------------------------------------------------------------
353 // event handlers: file and help menu
354 // ----------------------------------------------------------------------------
355
356 void MyFrame::OnQuit(wxCommandEvent& WXUNUSED(event))
357 {
358 // TRUE is to force the frame to close
359 Close(TRUE);
360 }
361
362 void MyFrame::OnClear(wxCommandEvent& WXUNUSED(event))
363 {
364 m_lbox->Clear();
365 }
366
367 void MyFrame::OnAbout(wxCommandEvent& WXUNUSED(event))
368 {
369 wxMessageBox(_T("Exec wxWindows Sample\n© 2000-2001 Vadim Zeitlin"),
370 _T("About Exec"), wxOK | wxICON_INFORMATION, this);
371 }
372
373 void MyFrame::OnKill(wxCommandEvent& WXUNUSED(event))
374 {
375 long pid = wxGetNumberFromUser(_T("Please specify the process to kill"),
376 _T("Enter PID:"),
377 _T("Exec question"),
378 m_pidLast,
379 // we need the full unsigned int range
380 -INT_MAX, INT_MAX,
381 this);
382 if ( pid == -1 )
383 {
384 // cancelled
385 return;
386 }
387
388 static const wxString signalNames[] =
389 {
390 _T("Just test (SIGNONE)"),
391 _T("Hangup (SIGHUP)"),
392 _T("Interrupt (SIGINT)"),
393 _T("Quit (SIGQUIT)"),
394 _T("Illegal instruction (SIGILL)"),
395 _T("Trap (SIGTRAP)"),
396 _T("Abort (SIGABRT)"),
397 _T("Emulated trap (SIGEMT)"),
398 _T("FP exception (SIGFPE)"),
399 _T("Kill (SIGKILL)"),
400 _T("Bus (SIGBUS)"),
401 _T("Segment violation (SIGSEGV)"),
402 _T("System (SIGSYS)"),
403 _T("Broken pipe (SIGPIPE)"),
404 _T("Alarm (SIGALRM)"),
405 _T("Terminate (SIGTERM)"),
406 };
407
408 int sig = wxGetSingleChoiceIndex(_T("How to kill the process?"),
409 _T("Exec question"),
410 WXSIZEOF(signalNames), signalNames,
411 this);
412 switch ( sig )
413 {
414 default:
415 wxFAIL_MSG( _T("unexpected return value") );
416 // fall through
417
418 case -1:
419 // cancelled
420 return;
421
422 case wxSIGNONE:
423 case wxSIGHUP:
424 case wxSIGINT:
425 case wxSIGQUIT:
426 case wxSIGILL:
427 case wxSIGTRAP:
428 case wxSIGABRT:
429 case wxSIGEMT:
430 case wxSIGFPE:
431 case wxSIGKILL:
432 case wxSIGBUS:
433 case wxSIGSEGV:
434 case wxSIGSYS:
435 case wxSIGPIPE:
436 case wxSIGALRM:
437 case wxSIGTERM:
438 break;
439 }
440
441 if ( sig == 0 )
442 {
443 if ( wxProcess::Exists(pid) )
444 wxLogStatus(_T("Process %d is running."), pid);
445 else
446 wxLogStatus(_T("No process with pid = %d."), pid);
447 }
448 else // not SIGNONE
449 {
450 wxKillError rc = wxProcess::Kill(pid, (wxSignal)sig);
451 if ( rc == wxKILL_OK )
452 {
453 wxLogStatus(_T("Process %d killed with signal %d."), pid, sig);
454 }
455 else
456 {
457 static const wxChar *errorText[] =
458 {
459 _T(""), // no error
460 _T("signal not supported"),
461 _T("permission denied"),
462 _T("no such process"),
463 _T("unspecified error"),
464 };
465
466 wxLogStatus(_T("Failed to kill process %d with signal %d: %s"),
467 pid, sig, errorText[rc]);
468 }
469 }
470 }
471
472 // ----------------------------------------------------------------------------
473 // event handlers: exec menu
474 // ----------------------------------------------------------------------------
475
476 void MyFrame::DoAsyncExec(const wxString& cmd)
477 {
478 wxProcess *process = new MyProcess(this, cmd);
479 m_pidLast = wxExecute(cmd, wxEXEC_ASYNC, process);
480 if ( !m_pidLast )
481 {
482 wxLogError(_T("Execution of '%s' failed."), cmd.c_str());
483
484 delete process;
485 }
486 else
487 {
488 wxLogStatus(_T("Process %ld (%s) launched."), m_pidLast, cmd.c_str());
489
490 m_cmdLast = cmd;
491 }
492 }
493
494 void MyFrame::OnSyncExec(wxCommandEvent& WXUNUSED(event))
495 {
496 wxString cmd = wxGetTextFromUser(_T("Enter the command: "),
497 DIALOG_TITLE,
498 m_cmdLast);
499
500 if ( !cmd )
501 return;
502
503 wxLogStatus(_T("'%s' is running please wait..."), cmd.c_str());
504
505 int code = wxExecute(cmd, wxEXEC_SYNC);
506
507 wxLogStatus(_T("Process '%s' terminated with exit code %d."),
508 cmd.c_str(), code);
509 m_cmdLast = cmd;
510 }
511
512 void MyFrame::OnAsyncExec(wxCommandEvent& WXUNUSED(event))
513 {
514 wxString cmd = wxGetTextFromUser(_T("Enter the command: "),
515 DIALOG_TITLE,
516 m_cmdLast);
517
518 if ( !cmd )
519 return;
520
521 DoAsyncExec(cmd);
522 }
523
524 void MyFrame::OnShell(wxCommandEvent& WXUNUSED(event))
525 {
526 wxString cmd = wxGetTextFromUser(_T("Enter the command: "),
527 DIALOG_TITLE,
528 m_cmdLast);
529
530 if ( !cmd )
531 return;
532
533 int code = wxShell(cmd);
534 wxLogStatus(_T("Shell command '%s' terminated with exit code %d."),
535 cmd.c_str(), code);
536 m_cmdLast = cmd;
537 }
538
539 void MyFrame::OnExecWithRedirect(wxCommandEvent& WXUNUSED(event))
540 {
541 wxString cmd = wxGetTextFromUser(_T("Enter the command: "),
542 DIALOG_TITLE,
543 m_cmdLast);
544
545 if ( !cmd )
546 return;
547
548 bool sync;
549 switch ( wxMessageBox(_T("Execute it synchronously?"),
550 _T("Exec question"),
551 wxYES_NO | wxCANCEL | wxICON_QUESTION, this) )
552 {
553 case wxYES:
554 sync = TRUE;
555 break;
556
557 case wxNO:
558 sync = FALSE;
559 break;
560
561 default:
562 return;
563 }
564
565 if ( sync )
566 {
567 wxArrayString output, errors;
568 int code = wxExecute(cmd, output, errors);
569 wxLogStatus(_T("command '%s' terminated with exit code %d."),
570 cmd.c_str(), code);
571
572 if ( code != -1 )
573 {
574 ShowOutput(cmd, output, _T("Output"));
575 ShowOutput(cmd, errors, _T("Errors"));
576 }
577 }
578 else // async exec
579 {
580 MyPipedProcess *process = new MyPipedProcess(this, cmd);
581 if ( !wxExecute(cmd, wxEXEC_ASYNC, process) )
582 {
583 wxLogError(_T("Execution of '%s' failed."), cmd.c_str());
584
585 delete process;
586 }
587 else
588 {
589 m_running.Add(process);
590 }
591 }
592
593 m_cmdLast = cmd;
594 }
595
596 void MyFrame::OnExecWithPipe(wxCommandEvent& WXUNUSED(event))
597 {
598 if ( !m_cmdLast )
599 m_cmdLast = _T("tr [a-z] [A-Z]");
600
601 wxString cmd = wxGetTextFromUser(_T("Enter the command: "),
602 DIALOG_TITLE,
603 m_cmdLast);
604
605 if ( !cmd )
606 return;
607
608 wxString input = wxGetTextFromUser(_T("Enter the string to send to it: "),
609 DIALOG_TITLE);
610 if ( !input )
611 return;
612
613 // always execute the filter asynchronously
614 MyPipedProcess2 *process = new MyPipedProcess2(this, cmd, input);
615 int pid = wxExecute(cmd, wxEXEC_ASYNC, process);
616 if ( pid )
617 {
618 wxLogStatus(_T("Process %ld (%s) launched."), pid, cmd.c_str());
619
620 m_running.Add(process);
621 }
622 else
623 {
624 wxLogError(_T("Execution of '%s' failed."), cmd.c_str());
625
626 delete process;
627 }
628
629 m_cmdLast = cmd;
630 }
631
632 void MyFrame::OnFileExec(wxCommandEvent& event)
633 {
634 static wxString s_filename;
635
636 wxString filename = wxLoadFileSelector(_T("file"), _T(""), s_filename);
637 if ( !filename )
638 return;
639
640 s_filename = filename;
641
642 wxString ext = filename.AfterFirst(_T('.'));
643 wxFileType *ft = wxTheMimeTypesManager->GetFileTypeFromExtension(ext);
644 if ( !ft )
645 {
646 wxLogError(_T("Impossible to determine the file type for extension '%s'"),
647 ext.c_str());
648 return;
649 }
650
651 wxString cmd;
652 bool ok = ft->GetOpenCommand(&cmd,
653 wxFileType::MessageParameters(filename, _T("")));
654 delete ft;
655 if ( !ok )
656 {
657 wxLogError(_T("Impossible to find out how to open files of extension '%s'"),
658 ext.c_str());
659 return;
660 }
661
662 DoAsyncExec(cmd);
663 }
664
665 // ----------------------------------------------------------------------------
666 // DDE stuff
667 // ----------------------------------------------------------------------------
668
669 #ifdef __WINDOWS__
670
671 bool MyFrame::GetDDEServer()
672 {
673 wxString server = wxGetTextFromUser(_T("Server to connect to:"),
674 DIALOG_TITLE, m_server);
675 if ( !server )
676 return FALSE;
677
678 m_server = server;
679
680 wxString topic = wxGetTextFromUser(_T("DDE topic:"), DIALOG_TITLE, m_topic);
681 if ( !topic )
682 return FALSE;
683
684 m_topic = topic;
685
686 wxString cmd = wxGetTextFromUser(_T("DDE command:"), DIALOG_TITLE, m_cmdDde);
687 if ( !cmd )
688 return FALSE;
689
690 m_cmdDde = cmd;
691
692 return TRUE;
693 }
694
695 void MyFrame::OnDDEExec(wxCommandEvent& WXUNUSED(event))
696 {
697 if ( !GetDDEServer() )
698 return;
699
700 wxDDEClient client;
701 wxConnectionBase *conn = client.MakeConnection("", m_server, m_topic);
702 if ( !conn )
703 {
704 wxLogError(_T("Failed to connect to the DDE server '%s'."),
705 m_server.c_str());
706 }
707 else
708 {
709 if ( !conn->Execute(m_cmdDde) )
710 {
711 wxLogError(_T("Failed to execute command '%s' via DDE."),
712 m_cmdDde.c_str());
713 }
714 else
715 {
716 wxLogStatus(_T("Successfully executed DDE command"));
717 }
718 }
719 }
720
721 void MyFrame::OnDDERequest(wxCommandEvent& WXUNUSED(event))
722 {
723 if ( !GetDDEServer() )
724 return;
725
726 wxDDEClient client;
727 wxConnectionBase *conn = client.MakeConnection("", m_server, m_topic);
728 if ( !conn )
729 {
730 wxLogError(_T("Failed to connect to the DDE server '%s'."),
731 m_server.c_str());
732 }
733 else
734 {
735 if ( !conn->Request(m_cmdDde) )
736 {
737 wxLogError(_T("Failed to send request '%s' via DDE."),
738 m_cmdDde.c_str());
739 }
740 else
741 {
742 wxLogStatus(_T("Successfully sent DDE request."));
743 }
744 }
745 }
746
747 #endif // __WINDOWS__
748
749 // ----------------------------------------------------------------------------
750 // various helpers
751 // ----------------------------------------------------------------------------
752
753 // input polling
754 void MyFrame::OnIdle(wxIdleEvent& event)
755 {
756 size_t count = m_running.GetCount();
757 for ( size_t n = 0; n < count; n++ )
758 {
759 if ( m_running[n]->HasInput() )
760 {
761 event.RequestMore();
762 }
763 }
764 }
765
766 void MyFrame::OnProcessTerminated(MyPipedProcess *process)
767 {
768 m_running.Remove(process);
769 }
770
771
772 void MyFrame::ShowOutput(const wxString& cmd,
773 const wxArrayString& output,
774 const wxString& title)
775 {
776 size_t count = output.GetCount();
777 if ( !count )
778 return;
779
780 m_lbox->Append(wxString::Format(_T("--- %s of '%s' ---"),
781 title.c_str(), cmd.c_str()));
782
783 for ( size_t n = 0; n < count; n++ )
784 {
785 m_lbox->Append(output[n]);
786 }
787
788 m_lbox->Append(_T("--- End of output ---"));
789 }
790
791 // ----------------------------------------------------------------------------
792 // MyProcess
793 // ----------------------------------------------------------------------------
794
795 void MyProcess::OnTerminate(int pid, int status)
796 {
797 wxLogStatus(m_parent, _T("Process %u ('%s') terminated with exit code %d."),
798 pid, m_cmd.c_str(), status);
799
800 // we're not needed any more
801 delete this;
802 }
803
804 // ----------------------------------------------------------------------------
805 // MyPipedProcess
806 // ----------------------------------------------------------------------------
807
808 bool MyPipedProcess::HasInput()
809 {
810 bool hasInput = FALSE;
811
812 wxInputStream& is = *GetInputStream();
813 if ( !is.Eof() )
814 {
815 wxTextInputStream tis(is);
816
817 // this assumes that the output is always line buffered
818 wxString msg;
819 msg << m_cmd << _T(" (stdout): ") << tis.ReadLine();
820
821 m_parent->GetLogListBox()->Append(msg);
822
823 hasInput = TRUE;
824 }
825
826 wxInputStream& es = *GetErrorStream();
827 if ( !es.Eof() )
828 {
829 wxTextInputStream tis(es);
830
831 // this assumes that the output is always line buffered
832 wxString msg;
833 msg << m_cmd << _T(" (stderr): ") << tis.ReadLine();
834
835 m_parent->GetLogListBox()->Append(msg);
836
837 hasInput = TRUE;
838 }
839
840 return hasInput;
841 }
842
843 void MyPipedProcess::OnTerminate(int pid, int status)
844 {
845 // show the rest of the output
846 while ( HasInput() )
847 ;
848
849 m_parent->OnProcessTerminated(this);
850
851 MyProcess::OnTerminate(pid, status);
852 }
853
854 // ----------------------------------------------------------------------------
855 // MyPipedProcess2
856 // ----------------------------------------------------------------------------
857
858 bool MyPipedProcess2::HasInput()
859 {
860 if ( !!m_input )
861 {
862 wxTextOutputStream os(*GetOutputStream());
863 os.WriteString(m_input);
864
865 CloseOutput();
866 m_input.clear();
867
868 // call us once again - may be we'll have output
869 return TRUE;
870 }
871
872 return MyPipedProcess::HasInput();
873 }