Debian Codegraph @

CodeLite/clConsoleGnomeTerminal.cpp

raw
1 #include "clConsoleGnomeTerminal.h"
2
3 #include "dirsaver.h"
4 #include "file_logger.h"
5 #include "fileutils.h"
6 #include "procutils.h"
7
8 #include <wx/tokenzr.h>
9 #include <wx/utils.h>
10
11 bool clConsoleGnomeTerminal::FindProcessByCommand(const wxString& name, wxString& tty, long& pid)
12 {
13 clDEBUG() << "FindProcessByCommand is called";
14 tty.Clear();
15 pid = wxNOT_FOUND;
16
17 // Run "ps -A -o pid,tty,command" to locate the terminal ID
18 wxString psCommand;
19 wxArrayString arrOutput;
20 psCommand << "ps -A -o pid,tty,command";
21
22 ProcUtils::SafeExecuteCommand(psCommand, arrOutput);
23
24 for(size_t i = 0; i < arrOutput.GetCount(); ++i) {
25 wxString curline = arrOutput.Item(i).Trim().Trim(false);
26 wxArrayString tokens = ::wxStringTokenize(curline, " ", wxTOKEN_STRTOK);
27 if(tokens.GetCount() < 3) {
28 continue;
29 }
30
31 // replace tabs with spaces
32 curline.Replace("\t", " ");
33
34 // remove any duplicate spaces
35 while(curline.Replace(" ", " ")) {}
36
37 wxString tmp_pid = curline.BeforeFirst(' ');
38 curline = curline.AfterFirst(' ');
39
40 wxString tmp_tty = curline.BeforeFirst(' ');
41 curline = curline.AfterFirst(' ');
42
43 wxString command = curline; // the remainder
44 command.Trim().Trim(false);
45
46 if(command == name) {
47 // we got our match
48 tmp_tty = tmp_tty.AfterLast('/');
49 tmp_tty.Prepend("/dev/pts/");
50 tty = tmp_tty;
51 tmp_pid.Trim().Trim(false).ToCLong(&pid);
52 return true;
53 }
54 }
55 return false;
56 }
57
58 clConsoleGnomeTerminal::clConsoleGnomeTerminal()
59 {
60 SetTerminalCommand("gnome-terminal --working-directory=%WD% -e '%COMMAND%'");
61 SetEmptyTerminalCommand("gnome-terminal --working-directory=%WD%");
62 }
63
64 clConsoleGnomeTerminal::~clConsoleGnomeTerminal() {}
65
66 bool clConsoleGnomeTerminal::Start() { return StartProcess(PrepareCommand()); }
67
68 bool clConsoleGnomeTerminal::StartForDebugger()
69 {
70 // generate a random value to differntiate this instance of codelite
71 // from other instances
72
73 time_t curtime = time(NULL);
74 int randomSeed = (curtime % 947);
75 wxString secondsToSleep;
76
77 secondsToSleep << (85765 + randomSeed);
78
79 wxString sleepCommand = "/bin/sleep";
80 sleepCommand << " " << secondsToSleep;
81
82 wxString homedir = wxGetHomeDir();
83 if(homedir.Contains(" ")) {
84 homedir.Prepend("\"").Append("\"");
85 }
86 wxString commandToExecute = GetTerminalCommand();
87 commandToExecute.Replace("%WD%", homedir);
88 commandToExecute.Replace("%COMMAND%", sleepCommand);
89 ::wxExecute(commandToExecute);
90
91 // Let it start ... (wait for it up to 5 seconds)
92 for(size_t i = 0; i < 100; ++i) {
93 if(FindProcessByCommand(sleepCommand, m_tty, m_pid)) {
94 // On GTK, redirection to TTY does not work with lldb
95 // as a workaround, we create a symlink with different name
96
97 // Keep the real tty
98 m_realPts = m_tty;
99
100 wxString symlinkName = m_tty;
101 symlinkName.Replace("/dev/pts/", "/tmp/pts");
102 wxString lnCommand;
103 lnCommand << "ln -sf " << m_tty << " " << symlinkName;
104 if(::system(lnCommand.mb_str(wxConvUTF8).data()) == 0) {
105 m_tty.swap(symlinkName);
106 }
107 break;
108 }
109 wxThread::Sleep(50);
110 }
111 return !m_tty.IsEmpty();
112 }
113
114 wxString clConsoleGnomeTerminal::PrepareCommand()
115 {
116 wxString commandToExecute;
117 bool hasCommand = !GetCommand().IsEmpty();
118 commandToExecute = hasCommand ? GetTerminalCommand() : GetEmptyTerminalCommand();
119 if(!IsTerminalNeeded()) {
120 commandToExecute = "%COMMAND%";
121 }
122
123 if(IsTerminalNeeded()) {
124 // set the working directory
125 wxString workingDirectory = WrapWithQuotesIfNeeded(GetWorkingDirectory());
126 if(workingDirectory.IsEmpty()) {
127 workingDirectory = ".";
128 }
129 commandToExecute.Replace("%WD%", workingDirectory);
130 }
131
132 if(hasCommand) {
133 wxFileName scriptPath = PrepareExecScript();
134 wxString rowCommand;
135 rowCommand << "/bin/bash -f \"" << scriptPath.GetFullPath() << "\"";
136 commandToExecute.Replace("%COMMAND%", rowCommand);
137 }
138 return commandToExecute;
139 }