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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
|
// Copyright (C) 2023 The Qt Company Ltd.
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only WITH Qt-GPL-exception-1.0
#include "externalterminalprocessimpl.h"
#include "algorithm.h"
#include "qtcprocess.h"
#include "terminalcommand.h"
#include "utilstr.h"
#include <QLoggingCategory>
#include <QTemporaryFile>
#include <QVersionNumber>
Q_LOGGING_CATEGORY(logTE, "terminal.externalprocess", QtWarningMsg)
namespace Utils {
ExternalTerminalProcessImpl::ExternalTerminalProcessImpl()
{
setStubCreator(new ProcessStubCreator(this));
}
QString ExternalTerminalProcessImpl::openTerminalScriptAttached()
{
static const QLatin1String script{R"(
tell application "Terminal"
activate
set windowId to 0
set newTab to do script "%1 && exit"
-- Try to get window id
try
-- We have seen this work on macOS 13, and 12.5.1, but not on 14.0 or 14.1
set windowId to (the id of window 1 where its tab 1 = newTab) as text
on error eMsg number eNum
-- If we get an error we try to generate a known error that will contain the window id in its message
try
set windowId to window of newTab
on error eMsg number eNum
if eNum = -1728 then
try
-- Search for "window id " in the error message, examples of error messages are:
-- „Terminal“ hat einen Fehler erhalten: „window of tab 1 of window id 4018“ kann nicht gelesen werden.
-- Terminal got an error: Can't get window of tab 1 of window id 4707.
set windowIdPrefix to "window id "
set theOffset to (offset of windowIdPrefix in eMsg)
if theOffset = 0 then
log "Failed to parse window id from error message: " & eMsg
else
set windowIdPosition to theOffset + (length of windowIdPrefix)
set windowId to (first word of (text windowIdPosition thru -1 of eMsg)) as integer
end if
on error eMsg2 number eNum2
log "Failed to parse window id from error message: " & eMsg2 & " (" & eMsg & ") " & theOffset
end try
end if
end try
end try
repeat until ((count of processes of newTab) = 0)
delay 0.1
end repeat
if windowId is not equal to 0 then
close window id windowId
else
log "Cannot close window, sorry."
end if
end tell
)"};
return script;
}
ProcessStubCreator::ProcessStubCreator(TerminalInterface *interface)
: m_interface(interface)
{}
static const QLatin1String TerminalAppScriptDetached{R"(
tell application "Terminal"
activate
do script "%1 && exit"
end tell
)"};
struct AppScript
{
QString attached;
QString detached;
};
Result<qint64> ProcessStubCreator::startStubProcess(const ProcessSetupData &setupData)
{
const TerminalCommand terminal = TerminalCommand::terminalEmulator();
bool detached = setupData.m_terminalMode == TerminalMode::Detached;
if (HostOsInfo::isMacHost()) {
// There is a bug in macOS 14.0 where the script fails if it tries to find
// the window id. We will have to check in future versions of macOS if they fixed
// the issue.
static const QVersionNumber osVersionNumber = QVersionNumber::fromString(
QSysInfo::productVersion());
static const QMap<QString, AppScript> terminalMap = {
{"Terminal.app",
{ExternalTerminalProcessImpl::openTerminalScriptAttached(), TerminalAppScriptDetached}},
};
if (terminalMap.contains(terminal.command.path())) {
const QString env
= Utils::transform(setupData.m_environment.toStringList(), [](const QString &env) {
return CommandLine{"export", {env}}.toUserOutput();
}).join('\n');
Process *process = new Process(detached ? nullptr : this);
if (detached) {
QObject::connect(process, &Process::done, process, &Process::deleteLater);
}
QTemporaryFile shFile;
shFile.setAutoRemove(false);
QTC_ASSERT(shFile.open(),
return ResultError(Tr::tr("Failed to open temporary script file.")));
const QString shScript = QString("cd '%1'\n%2\nclear\n'%3' %4\nrm '%5'\n")
.arg(setupData.m_workingDirectory.nativePath())
.arg(env)
.arg(setupData.m_commandLine.executable().nativePath())
.arg(setupData.m_commandLine.arguments())
.arg(shFile.fileName());
shFile.write(shScript.toUtf8());
shFile.close();
FilePath::fromUserInput(shFile.fileName())
.setPermissions(QFile::ExeUser | QFile::ExeGroup | QFile::ExeOther | QFile::ReadUser
| QFile::ReadGroup | QFile::ReadOther | QFile::WriteUser
| QFile::WriteGroup | QFile::WriteOther);
const QString script = (detached
? terminalMap.value(terminal.command.path()).detached
: terminalMap.value(terminal.command.path()).attached)
.arg(shFile.fileName());
process->setCommand({"osascript", {"-"}});
process->setWriteData(script.toUtf8());
process->start();
if (!process->waitForStarted()) {
return ResultError(
Tr::tr("Failed to start terminal process: \"%1\".").arg(process->errorString()));
}
QObject::connect(process, &Process::readyReadStandardOutput, process, [process] {
const QString output = process->readAllStandardOutput();
if (!output.isEmpty())
qCWarning(logTE).noquote() << output;
});
QObject::connect(process, &Process::readyReadStandardError, process, [process] {
const QString output = process->readAllStandardError();
if (!output.isEmpty())
qCCritical(logTE).noquote() << output;
});
QObject::connect(process, &Process::done, m_interface, &TerminalInterface::onStubExited);
return 0;
}
}
Process *process = new Process(detached ? nullptr : this);
if (detached)
QObject::connect(process, &Process::done, process, &Process::deleteLater);
process->setWorkingDirectory(setupData.m_workingDirectory);
if constexpr (HostOsInfo::isWindowsHost()) {
process->setCommand(setupData.m_commandLine);
process->setCreateConsoleOnWindows(true);
process->setProcessMode(ProcessMode::Writer);
} else {
QString extraArgsFromOptions = terminal.executeArgs;
CommandLine cmdLine{terminal.command};
if (!extraArgsFromOptions.isEmpty())
cmdLine.addArgs(extraArgsFromOptions, CommandLine::Raw);
if (terminal.needsQuotes)
cmdLine.addCommandLineAsSingleArg(setupData.m_commandLine);
else
cmdLine.addCommandLineAsArgs(setupData.m_commandLine, CommandLine::Raw);
process->setCommand(cmdLine);
}
process->setEnvironment(
setupData.m_environment.appliedToEnvironment(Environment::systemEnvironment()));
process->start();
process->waitForStarted();
if (process->error() != QProcess::UnknownError) {
return ResultError(
Tr::tr("Failed to start terminal process: \"%1\".").arg(process->errorString()));
}
qint64 pid = process->processId();
return pid;
}
} // namespace Utils
|