+ wxMessageBox(wxT("Exec wxWidgets Sample\n(c) 2000-2002 Vadim Zeitlin"),
+ wxT("About Exec"), wxOK | wxICON_INFORMATION, this);
+}
+
+void MyFrame::OnKill(wxCommandEvent& WXUNUSED(event))
+{
+ long pid = wxGetNumberFromUser(wxT("Please specify the process to kill"),
+ wxT("Enter PID:"),
+ wxT("Exec question"),
+ m_pidLast,
+ // we need the full unsigned int range
+ -INT_MAX, INT_MAX,
+ this);
+ if ( pid == -1 )
+ {
+ // cancelled
+ return;
+ }
+
+ m_pidLast = pid;
+
+ static const wxString signalNames[] =
+ {
+ wxT("Just test (SIGNONE)"),
+ wxT("Hangup (SIGHUP)"),
+ wxT("Interrupt (SIGINT)"),
+ wxT("Quit (SIGQUIT)"),
+ wxT("Illegal instruction (SIGILL)"),
+ wxT("Trap (SIGTRAP)"),
+ wxT("Abort (SIGABRT)"),
+ wxT("Emulated trap (SIGEMT)"),
+ wxT("FP exception (SIGFPE)"),
+ wxT("Kill (SIGKILL)"),
+ wxT("Bus (SIGBUS)"),
+ wxT("Segment violation (SIGSEGV)"),
+ wxT("System (SIGSYS)"),
+ wxT("Broken pipe (SIGPIPE)"),
+ wxT("Alarm (SIGALRM)"),
+ wxT("Terminate (SIGTERM)"),
+ };
+
+ static int s_sigLast = wxSIGNONE;
+ int sig = wxGetSingleChoiceIndex(wxT("How to kill the process?"),
+ wxT("Exec question"),
+ WXSIZEOF(signalNames), signalNames,
+ s_sigLast,
+ this);
+ switch ( sig )
+ {
+ default:
+ wxFAIL_MSG( wxT("unexpected return value") );
+ // fall through
+
+ case -1:
+ // cancelled
+ return;
+
+ case wxSIGNONE:
+ case wxSIGHUP:
+ case wxSIGINT:
+ case wxSIGQUIT:
+ case wxSIGILL:
+ case wxSIGTRAP:
+ case wxSIGABRT:
+ case wxSIGEMT:
+ case wxSIGFPE:
+ case wxSIGKILL:
+ case wxSIGBUS:
+ case wxSIGSEGV:
+ case wxSIGSYS:
+ case wxSIGPIPE:
+ case wxSIGALRM:
+ case wxSIGTERM:
+ break;
+ }
+
+ s_sigLast = sig;
+
+ if ( sig == wxSIGNONE )
+ {
+ // This simply calls Kill(wxSIGNONE) but using it is more convenient.
+ if ( wxProcess::Exists(pid) )
+ {
+ wxLogStatus(wxT("Process %ld is running."), pid);
+ }
+ else
+ {
+ wxLogStatus(wxT("No process with pid = %ld."), pid);
+ }
+ }
+ else // not SIGNONE
+ {
+ wxKillError rc = wxProcess::Kill(pid, (wxSignal)sig);
+ if ( rc == wxKILL_OK )
+ {
+ wxLogStatus(wxT("Process %ld killed with signal %d."), pid, sig);
+ }
+ else
+ {
+ static const wxChar *errorText[] =
+ {
+ wxT(""), // no error
+ wxT("signal not supported"),
+ wxT("permission denied"),
+ wxT("no such process"),
+ wxT("unspecified error"),
+ };
+
+ wxLogStatus(wxT("Failed to kill process %ld with signal %d: %s"),
+ pid, sig, errorText[rc]);
+ }
+ }
+}
+
+// ----------------------------------------------------------------------------
+// execution options dialog
+// ----------------------------------------------------------------------------
+
+enum ExecQueryDialogID
+{
+ TEXT_EXECUTABLE,
+ TEXT_CWD,
+ TEXT_ENVIRONMENT
+};
+
+class ExecQueryDialog : public wxDialog
+{
+public:
+ ExecQueryDialog(const wxString& cmd);
+
+ wxString GetExecutable() const
+ {
+ return m_executable->GetValue();
+ }
+
+ wxString GetWorkDir() const
+ {
+ return m_useCWD->GetValue() ? m_cwdtext->GetValue() : wxString();
+ }
+
+ void GetEnvironment(wxEnvVariableHashMap& env);
+
+private:
+ void OnUpdateWorkingDirectoryUI(wxUpdateUIEvent& event)
+ {
+ event.Enable(m_useCWD->GetValue());
+ }
+
+ void OnUpdateEnvironmentUI(wxUpdateUIEvent& event)
+ {
+ event.Enable(m_useEnv->GetValue());
+ }
+
+ wxTextCtrl* m_executable;
+ wxTextCtrl* m_cwdtext;
+ wxTextCtrl* m_envtext;
+ wxCheckBox* m_useCWD;
+ wxCheckBox* m_useEnv;
+
+ DECLARE_EVENT_TABLE()
+};
+
+BEGIN_EVENT_TABLE(ExecQueryDialog, wxDialog)
+ EVT_UPDATE_UI(TEXT_CWD, ExecQueryDialog::OnUpdateWorkingDirectoryUI)
+ EVT_UPDATE_UI(TEXT_ENVIRONMENT, ExecQueryDialog::OnUpdateEnvironmentUI)
+END_EVENT_TABLE()
+
+ExecQueryDialog::ExecQueryDialog(const wxString& cmd)
+ : wxDialog(NULL, wxID_ANY, DIALOG_TITLE,
+ wxDefaultPosition, wxDefaultSize,
+ wxDEFAULT_DIALOG_STYLE | wxRESIZE_BORDER)
+{
+ wxSizer* globalSizer = new wxBoxSizer(wxVERTICAL);
+
+ m_executable = new wxTextCtrl(this, TEXT_EXECUTABLE, wxString());
+ m_cwdtext = new wxTextCtrl(this, TEXT_CWD, wxString());
+ m_envtext = new wxTextCtrl(this, TEXT_ENVIRONMENT, wxString(),
+ wxDefaultPosition, wxSize(300, 200),
+ wxTE_MULTILINE|wxHSCROLL);
+
+ const wxSizerFlags flagsExpand = wxSizerFlags().Expand().Border();
+ globalSizer->Add(new wxStaticText(this, wxID_ANY, "Enter the command: "),
+ flagsExpand);
+ globalSizer->Add(m_executable, flagsExpand);
+
+ m_useCWD = new wxCheckBox(this, wxID_ANY, "Working directory: ");
+ globalSizer->Add(m_useCWD, flagsExpand);
+ globalSizer->Add(m_cwdtext, flagsExpand);
+
+ m_useEnv = new wxCheckBox(this, wxID_ANY, "Environment: ");
+ globalSizer->Add(m_useEnv, flagsExpand);
+ globalSizer->Add(m_envtext, wxSizerFlags(flagsExpand).Proportion(1));
+
+ globalSizer->Add(CreateStdDialogButtonSizer(wxOK|wxCANCEL), flagsExpand);
+ SetSizerAndFit(globalSizer);
+
+
+ m_executable->SetValue(cmd);
+ m_cwdtext->SetValue(wxGetCwd());
+ wxEnvVariableHashMap env;
+ if ( wxGetEnvMap(&env) )
+ {
+ for ( wxEnvVariableHashMap::iterator it = env.begin();
+ it != env.end();
+ ++it )
+ {
+ m_envtext->AppendText(it->first + '=' + it->second + '\n');
+ }
+ }
+ m_useCWD->SetValue(false);
+ m_useEnv->SetValue(false);
+}
+
+void ExecQueryDialog::GetEnvironment(wxEnvVariableHashMap& env)
+{
+ env.clear();
+ if ( m_useEnv->GetValue() )
+ {
+ wxString name,
+ value;
+
+ const int nb = m_envtext->GetNumberOfLines();
+ for ( int l = 0; l < nb; l++ )
+ {
+ const wxString line = m_envtext->GetLineText(l).Trim();
+
+ if ( !line.empty() )
+ {
+ name = line.BeforeFirst('=', &value);
+ if ( name.empty() )
+ {
+ wxLogWarning("Skipping invalid environment line \"%s\".", line);
+ continue;
+ }
+
+ env[name] = value;
+ }
+ }
+ }
+}
+
+static bool QueryExec(wxString& cmd, wxExecuteEnv& env)
+{
+ ExecQueryDialog dialog(cmd);
+
+ if ( dialog.ShowModal() != wxID_OK )
+ return false;
+
+ cmd = dialog.GetExecutable();
+ env.cwd = dialog.GetWorkDir();
+ dialog.GetEnvironment(env.env);
+
+ return true;
+}
+
+// ----------------------------------------------------------------------------
+// event handlers: exec menu
+// ----------------------------------------------------------------------------
+
+int MyFrame::GetExecFlags() const
+{
+ wxMenuBar* const mbar = GetMenuBar();
+
+ int flags = 0;
+
+ if ( mbar->IsChecked(Exec_Flags_HideConsole) )
+ flags |= wxEXEC_HIDE_CONSOLE;
+ if ( mbar->IsChecked(Exec_Flags_ShowConsole) )
+ flags |= wxEXEC_SHOW_CONSOLE;
+ if ( mbar->IsChecked(Exec_Flags_NoEvents) )
+ flags |= wxEXEC_NOEVENTS;
+
+ return flags;