1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149
|
#ifndef CLCONSOLEBASE_H
#define CLCONSOLEBASE_H
#include "codelite_exports.h"
#include "macros.h"
#include <wx/arrstr.h>
#include <wx/process.h>
#include <wx/sharedptr.h>
#include <wx/string.h>
#include "cl_command_event.h"
wxDECLARE_EXPORTED_EVENT(WXDLLIMPEXP_CL, wxEVT_TERMINAL_EXIT, clProcessEvent);
class WXDLLIMPEXP_CL clConsoleEnvironment
{
wxStringMap_t m_environment;
wxStringMap_t m_oldEnvironment;
public:
clConsoleEnvironment(const wxStringMap_t& env);
clConsoleEnvironment();
~clConsoleEnvironment();
void Add(const wxString& name, const wxString& value);
void Apply();
void UnApply();
};
class WXDLLIMPEXP_CL clConsoleBase
{
public:
typedef wxSharedPtr<clConsoleBase> Ptr_t;
protected:
wxStringMap_t m_environment;
wxString m_workingDirectory;
wxString m_command;
wxString m_commandArgs;
wxString m_tty;
wxString m_realPts;
long m_pid = wxNOT_FOUND;
bool m_waitWhenDone = false;
bool m_autoTerminate = false;
int m_execExtraFlags = 0;
bool m_terminalNeeded = true;
wxProcess* m_callback = nullptr;
wxEvtHandler* m_sink = nullptr;
wxString m_callbackUID;
protected:
/**
* @brief create an environment list to be used before we execute our terminal
*/
wxString GetEnvironmentPrefix() const;
wxString WrapWithQuotesIfNeeded(const wxString& s) const;
wxString EscapeString(const wxString& str, const wxString& c = "\"") const;
virtual bool StartProcess(const wxString& command);
public:
clConsoleBase();
virtual ~clConsoleBase();
/**
* @brief when sink is provided, the terminal will send event when its done
* The event sent is wxEVT_TERMINAL_EXIT of type clProcessEvent
* @param sink
* @param if provided the uid is sent back along with the termination event
*/
void SetSink(wxEvtHandler* sink, const wxString& uid = "")
{
this->m_sink = sink;
this->m_callbackUID = uid;
}
/**
* @brief split command line arguments taking double quotes and escaping into account
*/
static wxArrayString SplitArguments(const wxString& args);
/**
* @brief add an environment variable to be applied before we start the terminal
*/
void AddEnvVariable(const wxString& name, const wxString& value);
/**
* @brief start terminal with a given command and an optional working directory
*/
virtual bool Start() = 0;
/**
* @brief start terminal for debugger (i.e. launch an empty terminal so we can obtain its tty and return it
*/
virtual bool StartForDebugger() = 0;
/**
* @brief prepare the execution command
*/
virtual wxString PrepareCommand() = 0;
/**
* @brief return the best terminal for the OS. Pass an empty string to return the default temrinal for the OS
*/
static clConsoleBase::Ptr_t GetTerminal();
/**
* @brief return the default name for the OS
*/
static wxString GetSelectedTerminalName();
/**
* @brief return list of known terminals
*/
static wxArrayString GetAvailaleTerminals();
// Setters/Getters
void SetCommand(const wxString& command, const wxString& args)
{
this->m_command = command;
this->m_commandArgs = args;
}
void SetEnvironment(const wxStringMap_t& environment) { this->m_environment = environment; }
void SetPid(long pid) { this->m_pid = pid; }
void SetRealPts(const wxString& realPts) { this->m_realPts = realPts; }
void SetTty(const wxString& tty) { this->m_tty = tty; }
void SetWaitWhenDone(bool waitWhenDone) { this->m_waitWhenDone = waitWhenDone; }
void SetWorkingDirectory(const wxString& workingDirectory) { this->m_workingDirectory = workingDirectory; }
const wxString& GetCommand() const { return m_command; }
const wxString& GetCommandArgs() const { return m_commandArgs; }
const wxStringMap_t& GetEnvironment() const { return m_environment; }
long GetPid() const { return m_pid; }
const wxString& GetRealPts() const { return m_realPts; }
const wxString& GetTty() const { return m_tty; }
bool IsWaitWhenDone() const { return m_waitWhenDone; }
const wxString& GetWorkingDirectory() const { return m_workingDirectory; }
void SetExecExtraFlags(int execExtraFlags) { this->m_execExtraFlags = execExtraFlags; }
int GetExecExtraFlags() const { return m_execExtraFlags; }
void SetAutoTerminate(bool autoTerminate) { this->m_autoTerminate = autoTerminate; }
void SetTerminalNeeded(bool terminalNeeded) { this->m_terminalNeeded = terminalNeeded; }
bool IsAutoTerminate() const { return m_autoTerminate; }
bool IsTerminalNeeded() const { return m_terminalNeeded; }
void SetCallback(wxProcess* callback)
{
wxDELETE(m_callback);
this->m_callback = callback;
}
};
#endif // CLCONSOLEBASE_H
|