Debian Codegraph @

CodeLite/clConsoleCodeLiteTerminal.cpp

raw
1 #include "clConsoleCodeLiteTerminal.h"
2
3 #include "cl_standard_paths.h"
4 #include "file_logger.h"
5 #include "fileutils.h"
6 #include "procutils.h"
7
8 #include <wx/arrstr.h>
9 #include <wx/tokenzr.h>
10
11 clConsoleCodeLiteTerminal::clConsoleCodeLiteTerminal()
12 {
13 wxString cmd = GetBinary();
14 #ifndef __WXOSX__
15 WrapWithQuotesIfNeeded(cmd);
16 #endif
17 SetTerminalCommand(wxString() << cmd << " --working-directory=%WD% --file=%COMMANDFILE%");
18 SetEmptyTerminalCommand(wxString() << cmd << " --working-directory=%WD%");
19 }
20
21 clConsoleCodeLiteTerminal::~clConsoleCodeLiteTerminal() {}
22
23 wxString clConsoleCodeLiteTerminal::PrepareCommand()
24 {
25 // Build the command to execute
26 wxString commandToExecute;
27 if(IsTerminalNeeded()) {
28 wxFileName tmpfile(clStandardPaths::Get().GetTempDir(), "codelite-terminal.txt");
29 bool hasCommand = !GetCommand().IsEmpty();
30 commandToExecute = hasCommand ? GetTerminalCommand() : GetEmptyTerminalCommand();
31
32 // For testing purposes
33 wxString command = WrapWithQuotesIfNeeded(GetCommand());
34 if(!command.IsEmpty()) {
35 #ifdef __WXMSW__
36 command.Prepend("start /B /WAIT "); // start the application in the foreground
37 #endif
38 if(!GetCommandArgs().IsEmpty()) {
39 wxString cmdArgs = GetCommandArgs();
40 command << " " << GetCommandArgs();
41 }
42
43 // Write the content of the command into a file
44 FileUtils::WriteFileContent(tmpfile, command);
45 }
46
47 wxString wd = GetWorkingDirectory();
48 if(wd.IsEmpty() || !wxFileName::DirExists(wd)) {
49 wd = WrapWithQuotesIfNeeded(wxGetCwd());
50 }
51 if(IsWaitWhenDone()) {
52 commandToExecute << " --wait ";
53 }
54
55 wxString commandFile = tmpfile.GetFullPath();
56 commandFile = WrapWithQuotesIfNeeded(commandFile);
57
58 commandToExecute.Replace("%COMMANDFILE%", commandFile);
59 commandToExecute.Replace("%WD%", wd);
60
61 } else {
62 commandToExecute = WrapWithQuotesIfNeeded(GetCommand());
63 if(!GetCommandArgs().IsEmpty()) {
64 commandToExecute << " " << GetCommandArgs();
65 }
66 }
67 return commandToExecute;
68 }
69
70 bool clConsoleCodeLiteTerminal::StartForDebugger()
71 {
72 #ifdef __WXMSW__
73 return false;
74 #else
75 // generate a random value to differntiate this instance of codelite
76 // from other instances
77
78 time_t curtime = time(NULL);
79 int randomSeed = (curtime % 947);
80 wxString secondsToSleep;
81
82 secondsToSleep << (85765 + randomSeed);
83
84 wxString sleepCommand = "/bin/sleep";
85 sleepCommand << " " << secondsToSleep;
86
87 wxString homedir = wxGetHomeDir();
88 if(homedir.Contains(" ")) {
89 homedir.Prepend("\"").Append("\"");
90 }
91 wxString commandToExecute;
92 commandToExecute << GetBinary();
93 #ifndef __WXOSX__
94 WrapWithQuotesIfNeeded(commandToExecute);
95 #endif
96 wxFileName tmpfile("/tmp", "codelite-terminal.txt");
97 tmpfile.SetFullName(wxString() << "codelite-terminal." << secondsToSleep << ".txt");
98
99 commandToExecute << " --print-tty=" << tmpfile.GetFullPath() << " --working-directory=" << homedir
100 << " --command=\"" << sleepCommand << "\"";
101 m_pid = ::wxExecute(commandToExecute);
102
103 // Let it start ... (wait for it up to 5 seconds)
104 for(size_t i = 0; i < 100; ++i) {
105 if(tmpfile.FileExists()) {
106 FileUtils::ReadFileContent(tmpfile.GetFullPath(), m_tty);
107 // On GTK, redirection to TTY does not work with lldb
108 // as a workaround, we create a symlink with different name
109
110 // Keep the real tty
111 m_realPts = m_tty;
112
113 wxString symlinkName = m_tty;
114 symlinkName.Replace("/dev/pts/", "/tmp/pts");
115 wxString lnCommand;
116 lnCommand << "ln -sf " << m_tty << " " << symlinkName;
117 if(::system(lnCommand.mb_str(wxConvUTF8).data()) == 0) {
118 m_tty.swap(symlinkName);
119 }
120 break;
121 }
122 wxThread::Sleep(50);
123 }
124 #ifdef __WXOSX__
125 // on OSX, wxExecute return the PID of the open command
126 // we want the sleep command PID (when we kill the 'sleep' the terminal will
127 // close itself)
128 m_pid = wxNOT_FOUND;
129 wxString psCommand;
130 psCommand << "ps -A -o pid,command";
131 wxString psOutput = ProcUtils::SafeExecuteCommand(psCommand);
132 LOG_IF_TRACE { clDEBUG1() << "ps command output:\n" << psOutput; }
133 wxArrayString lines = ::wxStringTokenize(psOutput, "\n", wxTOKEN_STRTOK);
134 for(size_t u = 0; u < lines.GetCount(); ++u) {
135 wxString l = lines.Item(u);
136 l.Trim().Trim(false);
137 if(l.Contains("sleep") && l.Contains(secondsToSleep) && !l.Contains("codelite-terminal")) {
138 // we got a match
139 wxString ppidString = l.BeforeFirst(' ');
140 ppidString.ToCLong(&m_pid);
141 clDEBUG() << "codelite-terminal process ID is:" << m_pid;
142 break;
143 }
144 }
145 #endif
146
147 return !m_tty.IsEmpty();
148 #endif
149 }
150
151 wxString clConsoleCodeLiteTerminal::GetBinary() const
152 {
153 wxFileName codeliteTerminal(clStandardPaths::Get().GetBinFolder(), "codelite-terminal");
154 #ifdef __WXMSW__
155 codeliteTerminal.SetExt("exe");
156 #elif defined(__WXOSX__)
157 codeliteTerminal.AppendDir("codelite-terminal.app");
158 #endif
159
160 #ifdef __WXOSX__
161 wxString cmd = codeliteTerminal.GetPath();
162 cmd = WrapWithQuotesIfNeeded(cmd);
163 return cmd;
164 #else
165 return codeliteTerminal.GetFullPath();
166 #endif
167 }