| 1 |
////////////////////////////////////////////////////////////////////////////// |
| 2 |
////////////////////////////////////////////////////////////////////////////// |
| 3 |
// |
| 4 |
// copyright : (C) 2009 by Eran Ifrah |
| 5 |
// file name : asyncprocess.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 |
|
| 26 |
#ifndef I_PROCESS_H |
| 27 |
#define I_PROCESS_H |
| 28 |
|
| 29 |
#include "codelite_exports.h" |
| 30 |
#include "macros.h" |
| 31 |
|
| 32 |
#include <functional> |
| 33 |
#include <map> |
| 34 |
#include <vector> |
| 35 |
#include <wx/event.h> |
| 36 |
#include <wx/sharedptr.h> |
| 37 |
#include <wx/string.h> |
| 38 |
#include <wx/utils.h> |
| 39 |
|
| 40 |
class ProcessReaderThread; |
| 41 |
typedef std::vector<std::pair<wxString, wxString>> clEnvList_t; |
| 42 |
enum IProcessCreateFlags { |
| 43 |
IProcessCreateDefault = (1 << 0), // Default: create process with no console window |
| 44 |
IProcessCreateConsole = (1 << 1), // Create with console window shown |
| 45 |
IProcessCreateWithHiddenConsole = (1 << 2), // Create process with a hidden console |
| 46 |
IProcessCreateSync = |
| 47 |
(1 << 3), // Create a synchronous process (i.e. there is no background thread that performs the reads) |
| 48 |
IProcessCreateAsSuperuser = (1 << 4), // On platforms that support it, start the process as superuser |
| 49 |
IProcessNoRedirect = (1 << 5), |
| 50 |
IProcessStderrEvent = (1 << 6), // fire a separate event for stderr output |
| 51 |
IProcessRawOutput = (1 << 7), // return the process output as is, don't strip anything. By default CodeLite strips |
| 52 |
// terminal colours escape sequences |
| 53 |
IProcessCreateSSH = (1 << 8), // Create a remote process, over SSH |
| 54 |
IProcessInteractiveSSH = (1 << 9), |
| 55 |
IProcessWrapInShell = (1 << 10), // wrap the command in the OS shell (CMD, BASH) |
| 56 |
}; |
| 57 |
|
| 58 |
class WXDLLIMPEXP_CL IProcess; |
| 59 |
// Helper class for applying the environment before launching the process |
| 60 |
class WXDLLIMPEXP_CL clEnvironment |
| 61 |
{ |
| 62 |
const clEnvList_t* m_env = nullptr; |
| 63 |
wxStringMap_t m_oldEnv; |
| 64 |
|
| 65 |
public: |
| 66 |
clEnvironment(const clEnvList_t* env) |
| 67 |
: m_env(env) |
| 68 |
{ |
| 69 |
if(m_env) { |
| 70 |
for(const auto& p : (*m_env)) { |
| 71 |
const wxString& name = p.first; |
| 72 |
const wxString& value = p.second; |
| 73 |
|
| 74 |
wxString oldValue; |
| 75 |
// If an environment variable with this name already exists, keep its old value |
| 76 |
// as we want to restore it later |
| 77 |
if(::wxGetEnv(name, &oldValue)) { |
| 78 |
m_oldEnv.insert({ name, oldValue }); |
| 79 |
} |
| 80 |
// set the new value |
| 81 |
::wxSetEnv(name, value); |
| 82 |
} |
| 83 |
} |
| 84 |
} |
| 85 |
~clEnvironment() |
| 86 |
{ |
| 87 |
if(m_env) { |
| 88 |
for(const auto& p : (*m_env)) { |
| 89 |
const wxString& name = p.first; |
| 90 |
if(m_oldEnv.count(name)) { |
| 91 |
::wxSetEnv(name, m_oldEnv[name]); |
| 92 |
} else { |
| 93 |
::wxUnsetEnv(name); |
| 94 |
} |
| 95 |
} |
| 96 |
} |
| 97 |
m_oldEnv.clear(); |
| 98 |
} |
| 99 |
}; |
| 100 |
|
| 101 |
class WXDLLIMPEXP_CL IProcessCallback : public wxEvtHandler |
| 102 |
{ |
| 103 |
public: |
| 104 |
virtual void OnProcessOutput(const wxString& str) { wxUnusedVar(str); } |
| 105 |
virtual void OnProcessTerminated() {} |
| 106 |
}; |
| 107 |
|
| 108 |
/** |
| 109 |
* @class IProcess |
| 110 |
* @author eran |
| 111 |
* @date 10/09/09 |
| 112 |
* @file i_process.h |
| 113 |
* @brief |
| 114 |
*/ |
| 115 |
class WXDLLIMPEXP_CL IProcess : public wxEvtHandler |
| 116 |
{ |
| 117 |
protected: |
| 118 |
wxEvtHandler* m_parent = nullptr; |
| 119 |
int m_pid = wxNOT_FOUND; |
| 120 |
bool m_hardKill = false; |
| 121 |
IProcessCallback* m_callback = nullptr; |
| 122 |
size_t m_flags; // The creation flags |
| 123 |
ProcessReaderThread* m_thr = nullptr; |
| 124 |
|
| 125 |
public: |
| 126 |
typedef wxSharedPtr<IProcess> Ptr_t; |
| 127 |
|
| 128 |
public: |
| 129 |
IProcess(wxEvtHandler* parent) |
| 130 |
: m_parent(parent) |
| 131 |
, m_pid(-1) |
| 132 |
, m_hardKill(false) |
| 133 |
, m_callback(NULL) |
| 134 |
, m_flags(0) |
| 135 |
{ |
| 136 |
} |
| 137 |
virtual ~IProcess() {} |
| 138 |
|
| 139 |
public: |
| 140 |
// Handle process exit code. This is done this way this |
| 141 |
// under Linux / Mac the exit code is returned only after the signal child has been |
| 142 |
// handled by codelite |
| 143 |
static void SetProcessExitCode(int pid, int exitCode); |
| 144 |
static bool GetProcessExitCode(int pid, int& exitCode); |
| 145 |
|
| 146 |
// Stop notifying the parent window about input/output from the process |
| 147 |
// this is useful when we wish to terminate the process onExit but we don't want |
| 148 |
// to know about its termination |
| 149 |
virtual void Detach() = 0; |
| 150 |
|
| 151 |
// Read from process stdout - return immediately if no data is available |
| 152 |
virtual bool Read(wxString& buff, wxString& buffErr) = 0; |
| 153 |
|
| 154 |
// Write to the process stdin |
| 155 |
// This version add LF to the buffer |
| 156 |
virtual bool Write(const wxString& buff) = 0; |
| 157 |
|
| 158 |
// ANSI version |
| 159 |
// This version add LF to the buffer |
| 160 |
virtual bool Write(const std::string& buff) = 0; |
| 161 |
|
| 162 |
// Write to the process stdin |
| 163 |
virtual bool WriteRaw(const wxString& buff) = 0; |
| 164 |
|
| 165 |
// ANSI version |
| 166 |
virtual bool WriteRaw(const std::string& buff) = 0; |
| 167 |
|
| 168 |
/** |
| 169 |
* @brief wait for process to terminate and return all its output to the caller |
| 170 |
* Note that this function is blocking |
| 171 |
*/ |
| 172 |
virtual void WaitForTerminate(wxString& output); |
| 173 |
|
| 174 |
/** |
| 175 |
* @brief this method is mostly needed on MSW where writing a password |
| 176 |
* is done directly on the console buffer rather than its stdin |
| 177 |
*/ |
| 178 |
virtual bool WriteToConsole(const wxString& buff) = 0; |
| 179 |
|
| 180 |
// Return true if the process is still alive |
| 181 |
virtual bool IsAlive() = 0; |
| 182 |
|
| 183 |
// Clean the process resources and kill the process if it is |
| 184 |
// still alive |
| 185 |
virtual void Cleanup() = 0; |
| 186 |
|
| 187 |
// Terminate the process. It is recommended to use this method |
| 188 |
// so it will invoke the 'Cleaup' procedure and the process |
| 189 |
// termination event will be sent out |
| 190 |
virtual void Terminate() = 0; |
| 191 |
|
| 192 |
void SetPid(int pid) { this->m_pid = pid; } |
| 193 |
|
| 194 |
int GetPid() const { return m_pid; } |
| 195 |
|
| 196 |
void SetHardKill(bool hardKill) { this->m_hardKill = hardKill; } |
| 197 |
bool GetHardKill() const { return m_hardKill; } |
| 198 |
IProcessCallback* GetCallback() { return m_callback; } |
| 199 |
|
| 200 |
/** |
| 201 |
* @brief send signal to the process |
| 202 |
*/ |
| 203 |
virtual void Signal(wxSignal sig) = 0; |
| 204 |
|
| 205 |
/** |
| 206 |
* @brief do we have process redirect enabled? |
| 207 |
*/ |
| 208 |
bool IsRedirect() const { return !(m_flags & IProcessNoRedirect); } |
| 209 |
|
| 210 |
/** |
| 211 |
* @brief stop reading process output in the background thread |
| 212 |
*/ |
| 213 |
void SuspendAsyncReads(); |
| 214 |
/** |
| 215 |
* @brief resume reading process output in the background |
| 216 |
*/ |
| 217 |
void ResumeAsyncReads(); |
| 218 |
}; |
| 219 |
|
| 220 |
// Help method |
| 221 |
/** |
| 222 |
* @brief start process |
| 223 |
* @param parent the parent. all events will be sent to this object |
| 224 |
* @param cmd command line to execute |
| 225 |
* @param flags possible creation flag |
| 226 |
* @param workingDir set the working directory of the executed process |
| 227 |
* @return |
| 228 |
*/ |
| 229 |
WXDLLIMPEXP_CL IProcess* CreateAsyncProcess(wxEvtHandler* parent, const wxString& cmd, |
| 230 |
size_t flags = IProcessCreateDefault, |
| 231 |
const wxString& workingDir = wxEmptyString, |
| 232 |
const clEnvList_t* env = nullptr, |
| 233 |
const wxString& sshAccountName = wxEmptyString); |
| 234 |
/** |
| 235 |
* @brief same as above, but uses array of instead of a single string |
| 236 |
*/ |
| 237 |
WXDLLIMPEXP_CL IProcess* CreateAsyncProcess(wxEvtHandler* parent, const wxArrayString& args, |
| 238 |
size_t flags = IProcessCreateDefault, |
| 239 |
const wxString& workingDir = wxEmptyString, |
| 240 |
const clEnvList_t* env = nullptr, |
| 241 |
const wxString& sshAccountName = wxEmptyString); |
| 242 |
|
| 243 |
/** |
| 244 |
* @brief a wrapper for the variant that accepts wxArrayString. |
| 245 |
* This is because wxArrayString does not allow initializer list |
| 246 |
*/ |
| 247 |
WXDLLIMPEXP_CL IProcess* CreateAsyncProcess(wxEvtHandler* parent, const std::vector<wxString>& args, |
| 248 |
size_t flags = IProcessCreateDefault, |
| 249 |
const wxString& workingDir = wxEmptyString, |
| 250 |
const clEnvList_t* env = nullptr, |
| 251 |
const wxString& sshAccountName = wxEmptyString); |
| 252 |
|
| 253 |
/** |
| 254 |
* @brief create synchronus process |
| 255 |
* @param cmd command to execute |
| 256 |
* @param flags process creation flags |
| 257 |
* @param workingDir working directory for the new process |
| 258 |
* @return IPorcess handle on succcess |
| 259 |
*/ |
| 260 |
WXDLLIMPEXP_CL IProcess* CreateSyncProcess(const wxString& cmd, size_t flags = IProcessCreateDefault, |
| 261 |
const wxString& workingDir = wxEmptyString, |
| 262 |
const clEnvList_t* env = nullptr); |
| 263 |
|
| 264 |
/** |
| 265 |
* @brief start process and execute a callback once the process terminates |
| 266 |
* @param cmd command line to execute |
| 267 |
* @brief cb callback |
| 268 |
* @param flags possible creation flag |
| 269 |
* @param workingDir set the working directory of the executed process |
| 270 |
* @param env |
| 271 |
*/ |
| 272 |
WXDLLIMPEXP_CL void CreateAsyncProcessCB(const wxString& cmd, std::function<void(const wxString&)> cb, |
| 273 |
size_t flags = IProcessCreateDefault, |
| 274 |
const wxString& workingDir = wxEmptyString, const clEnvList_t* env = nullptr); |
| 275 |
|
| 276 |
#endif // I_PROCESS_H |