| 1 |
////////////////////////////////////////////////////////////////////////////// |
| 2 |
////////////////////////////////////////////////////////////////////////////// |
| 3 |
// |
| 4 |
// copyright : (C) 2008 by Eran Ifrah |
| 5 |
// file name : procutils.h |
| 6 |
// |
| 7 |
// ------------------------------------------------------------------------- |
| 8 |
// A |
| 9 |
// _____ _ _ _ _ |
| 10 |
// / __ \ | | | | (_) | |
| 11 |
// | / \/ ___ __| | ___| | _| |_ ___ |
| 12 |
// | | / _ \ / _ |/ _ \ | | | __/ _ ) |
| 13 |
// | \__/\ (_) | (_| | __/ |___| | || __/ |
| 14 |
// \____/\___/ \__,_|\___\_____/_|\__\___| |
| 15 |
// |
| 16 |
// F i l e |
| 17 |
// |
| 18 |
// This program is free software; you can redistribute it and/or modify |
| 19 |
// it under the terms of the GNU General Public License as published by |
| 20 |
// the Free Software Foundation; either version 2 of the License, or |
| 21 |
// (at your option) any later version. |
| 22 |
// |
| 23 |
////////////////////////////////////////////////////////////////////////////// |
| 24 |
////////////////////////////////////////////////////////////////////////////// |
| 25 |
#ifndef PROCUTILS_H |
| 26 |
#define PROCUTILS_H |
| 27 |
|
| 28 |
#include "codelite_exports.h" |
| 29 |
#include "wx/defs.h" |
| 30 |
|
| 31 |
#include <map> |
| 32 |
#include <vector> |
| 33 |
#include <wx/arrstr.h> |
| 34 |
#include <wx/process.h> |
| 35 |
#include <wx/string.h> |
| 36 |
#ifdef __WINDOWS__ // __WINDOWS__ defined by wx/defs.h |
| 37 |
#include "wx/msw/wrapwin.h" // includes windows.h |
| 38 |
// #include <devpropdef.h> |
| 39 |
#include <Psapi.h> |
| 40 |
#include <SetupAPI.h> |
| 41 |
#include <tlhelp32.h> |
| 42 |
#endif |
| 43 |
|
| 44 |
#include "cl_command_event.h" |
| 45 |
#include "codelite_events.h" |
| 46 |
|
| 47 |
struct ProcessEntry { |
| 48 |
wxString name; |
| 49 |
long pid; |
| 50 |
}; |
| 51 |
|
| 52 |
typedef std::vector<ProcessEntry> PidVec_t; |
| 53 |
|
| 54 |
/// Event class used by ProcUtils::ShellExecAsync method |
| 55 |
class WXDLLIMPEXP_CL clShellProcessEvent : public clCommandEvent |
| 56 |
{ |
| 57 |
wxString m_output; |
| 58 |
int m_pid = wxNOT_FOUND; |
| 59 |
int m_exitCode = wxNOT_FOUND; |
| 60 |
|
| 61 |
public: |
| 62 |
clShellProcessEvent(wxEventType commandType = wxEVT_NULL, int winid = 0); |
| 63 |
clShellProcessEvent(const clShellProcessEvent& event); |
| 64 |
clShellProcessEvent& operator=(const clShellProcessEvent& src); |
| 65 |
virtual ~clShellProcessEvent(); |
| 66 |
virtual wxEvent* Clone() const { return new clShellProcessEvent(*this); } |
| 67 |
|
| 68 |
void SetOutput(const wxString& output) { this->m_output = output; } |
| 69 |
const wxString& GetOutput() const { return m_output; } |
| 70 |
|
| 71 |
void SetPid(int pid) { this->m_pid = pid; } |
| 72 |
void SetExitCode(int exitCode) { this->m_exitCode = exitCode; } |
| 73 |
|
| 74 |
int GetPid() const { return m_pid; } |
| 75 |
int GetExitCode() const { return m_exitCode; } |
| 76 |
}; |
| 77 |
|
| 78 |
typedef void (wxEvtHandler::*clShellProcessEventFunction)(clShellProcessEvent&); |
| 79 |
#define clShellProcessEventHandler(func) wxEVENT_HANDLER_CAST(clShellProcessEventFunction, func) |
| 80 |
|
| 81 |
wxDECLARE_EXPORTED_EVENT(WXDLLIMPEXP_CL, wxEVT_SHELL_ASYNC_PROCESS_TERMINATED, clShellProcessEvent); |
| 82 |
|
| 83 |
class WXDLLIMPEXP_CL ProcUtils |
| 84 |
{ |
| 85 |
public: |
| 86 |
ProcUtils(); |
| 87 |
~ProcUtils(); |
| 88 |
|
| 89 |
static wxString& WrapInShell(wxString& cmd); |
| 90 |
|
| 91 |
static void GetProcTree(std::map<unsigned long, bool>& parentsMap, long pid); |
| 92 |
static void ExecuteCommand(const wxString& command, wxArrayString& output, |
| 93 |
long flags = wxEXEC_NODISABLE | wxEXEC_SYNC); |
| 94 |
static void ExecuteInteractiveCommand(const wxString& command); |
| 95 |
static wxString GetProcessNameByPid(long pid); |
| 96 |
static void GetProcessList(std::vector<ProcessEntry>& proclist); |
| 97 |
static void GetChildren(long pid, std::vector<long>& children); |
| 98 |
static bool Shell(const wxString& programConsoleCommand); |
| 99 |
static bool Locate(const wxString& name, wxString& where); |
| 100 |
|
| 101 |
/** |
| 102 |
* @brief the equivalent of 'ps ax|grep <name>' |
| 103 |
*/ |
| 104 |
static PidVec_t PS(const wxString& name); |
| 105 |
|
| 106 |
/** |
| 107 |
* \brief a safe function that executes 'command' and returns its output. This function |
| 108 |
* is safed to be called from secondary thread (hence, SafeExecuteCommand) |
| 109 |
* \param command |
| 110 |
* \param output |
| 111 |
*/ |
| 112 |
static void SafeExecuteCommand(const wxString& command, wxArrayString& output); |
| 113 |
|
| 114 |
/** |
| 115 |
* @brief execute a command and return its output as plain string |
| 116 |
*/ |
| 117 |
static wxString SafeExecuteCommand(const wxString& command); |
| 118 |
|
| 119 |
/** |
| 120 |
* @brief execute command and search the output for the first occurance of `find_what` |
| 121 |
*/ |
| 122 |
static wxString GrepCommandOutput(const std::vector<wxString>& cmd, const wxString& find_what); |
| 123 |
|
| 124 |
/** |
| 125 |
* @brief executes a command under the proper shell async |
| 126 |
* @param command the command the execute, e.g. `ls -l` |
| 127 |
* @param pid storage pointer for the executed process |
| 128 |
* @param sink handler class for the process events |
| 129 |
* @returns true on success, otherwise (i.e. failed to launch the process) return false |
| 130 |
*/ |
| 131 |
static bool ShellExecAsync(const wxString& command, long* pid, wxEvtHandler* sink); |
| 132 |
|
| 133 |
/** |
| 134 |
* @brief executes a command under the proper shell sync |
| 135 |
* @param command the command the execute, e.g. `ls -l` |
| 136 |
* @param std_out [output] |
| 137 |
* @param std_err [output] |
| 138 |
* @return process exit code |
| 139 |
*/ |
| 140 |
static int ShellExecSync(const wxString& command, wxString* output); |
| 141 |
}; |
| 142 |
|
| 143 |
#endif // PROCUTILS_H |