2012-10-02 09:12:39 +02:00
|
|
|
/****************************************************************************
|
2008-12-02 12:01:29 +01:00
|
|
|
**
|
2016-01-15 14:58:39 +01:00
|
|
|
** Copyright (C) 2016 The Qt Company Ltd.
|
|
|
|
|
** Contact: https://www.qt.io/licensing/
|
2008-12-02 12:01:29 +01:00
|
|
|
**
|
2012-10-02 09:12:39 +02:00
|
|
|
** This file is part of Qt Creator.
|
2008-12-02 12:01:29 +01:00
|
|
|
**
|
2012-10-02 09:12:39 +02:00
|
|
|
** Commercial License Usage
|
|
|
|
|
** Licensees holding valid commercial Qt licenses may use this file in
|
|
|
|
|
** accordance with the commercial license agreement provided with the
|
|
|
|
|
** Software or, alternatively, in accordance with the terms contained in
|
2016-01-15 14:58:39 +01:00
|
|
|
** a written agreement between you and The Qt Company. For licensing terms
|
|
|
|
|
** and conditions see https://www.qt.io/terms-conditions. For further
|
|
|
|
|
** information use the contact form at https://www.qt.io/contact-us.
|
2008-12-02 14:17:16 +01:00
|
|
|
**
|
2016-01-15 14:58:39 +01:00
|
|
|
** GNU General Public License Usage
|
|
|
|
|
** Alternatively, this file may be used under the terms of the GNU
|
|
|
|
|
** General Public License version 3 as published by the Free Software
|
|
|
|
|
** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT
|
|
|
|
|
** included in the packaging of this file. Please review the following
|
|
|
|
|
** information to ensure the GNU General Public License requirements will
|
|
|
|
|
** be met: https://www.gnu.org/licenses/gpl-3.0.html.
|
2010-12-17 16:01:08 +01:00
|
|
|
**
|
2012-10-02 09:12:39 +02:00
|
|
|
****************************************************************************/
|
2008-12-02 16:19:05 +01:00
|
|
|
|
2011-05-11 12:48:14 +02:00
|
|
|
#include "consoleprocess_p.h"
|
2008-12-02 12:01:29 +01:00
|
|
|
|
2010-10-19 11:14:03 +02:00
|
|
|
#include "qtcprocess.h"
|
|
|
|
|
|
2018-08-24 10:56:13 +02:00
|
|
|
#include <utils/algorithm.h>
|
2012-08-23 15:53:58 +02:00
|
|
|
#include <utils/hostosinfo.h>
|
2014-03-03 14:58:08 +01:00
|
|
|
#include <utils/qtcassert.h>
|
2012-08-23 15:53:58 +02:00
|
|
|
|
2012-02-15 10:42:41 +01:00
|
|
|
#include <QCoreApplication>
|
2017-04-13 16:26:38 +02:00
|
|
|
#include <QFileInfo>
|
2017-01-11 13:57:27 +01:00
|
|
|
#include <QSettings>
|
2012-12-19 19:04:36 +01:00
|
|
|
#include <QTimer>
|
2009-02-20 12:33:16 +01:00
|
|
|
|
|
|
|
|
#include <sys/stat.h>
|
|
|
|
|
#include <sys/types.h>
|
|
|
|
|
#include <errno.h>
|
|
|
|
|
#include <string.h>
|
|
|
|
|
#include <unistd.h>
|
|
|
|
|
|
2010-09-02 13:39:19 +02:00
|
|
|
namespace Utils {
|
|
|
|
|
|
2018-07-23 10:45:40 +02:00
|
|
|
ConsoleProcessPrivate::ConsoleProcessPrivate() = default;
|
2011-05-11 12:48:14 +02:00
|
|
|
|
2010-09-02 13:39:19 +02:00
|
|
|
ConsoleProcess::ConsoleProcess(QObject *parent) :
|
|
|
|
|
QObject(parent), d(new ConsoleProcessPrivate)
|
|
|
|
|
{
|
2015-03-05 22:00:05 +02:00
|
|
|
connect(&d->m_stubServer, &QLocalServer::newConnection,
|
|
|
|
|
this, &ConsoleProcess::stubConnectionAvailable);
|
2009-02-20 12:33:16 +01:00
|
|
|
|
2010-09-02 13:39:19 +02:00
|
|
|
d->m_process.setProcessChannelMode(QProcess::ForwardedChannels);
|
2008-12-02 12:01:29 +01:00
|
|
|
}
|
|
|
|
|
|
2014-03-03 14:58:08 +01:00
|
|
|
qint64 ConsoleProcess::applicationMainThreadID() const
|
|
|
|
|
{
|
|
|
|
|
return -1;
|
|
|
|
|
}
|
|
|
|
|
|
2010-09-02 13:39:19 +02:00
|
|
|
void ConsoleProcess::setSettings(QSettings *settings)
|
|
|
|
|
{
|
|
|
|
|
d->m_settings = settings;
|
|
|
|
|
}
|
|
|
|
|
|
2010-10-19 11:14:03 +02:00
|
|
|
bool ConsoleProcess::start(const QString &program, const QString &args)
|
2008-12-02 12:01:29 +01:00
|
|
|
{
|
2009-02-20 12:33:16 +01:00
|
|
|
if (isRunning())
|
2008-12-02 12:01:29 +01:00
|
|
|
return false;
|
|
|
|
|
|
2014-05-04 23:10:17 +03:00
|
|
|
d->m_errorString.clear();
|
|
|
|
|
d->m_error = QProcess::UnknownError;
|
|
|
|
|
|
2010-10-19 11:14:03 +02:00
|
|
|
QtcProcess::SplitError perr;
|
2014-02-05 10:43:21 +01:00
|
|
|
QtcProcess::Arguments pargs = QtcProcess::prepareArgs(args, &perr, HostOsInfo::hostOs(),
|
|
|
|
|
&d->m_environment, &d->m_workingDir);
|
2010-10-19 11:14:03 +02:00
|
|
|
QString pcmd;
|
|
|
|
|
if (perr == QtcProcess::SplitOk) {
|
|
|
|
|
pcmd = program;
|
|
|
|
|
} else {
|
|
|
|
|
if (perr != QtcProcess::FoundMeta) {
|
2014-05-04 23:10:17 +03:00
|
|
|
emitError(QProcess::FailedToStart, tr("Quoting error in command."));
|
2010-10-19 11:14:03 +02:00
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
if (d->m_mode == Debug) {
|
|
|
|
|
// FIXME: QTCREATORBUG-2809
|
2014-05-04 23:10:17 +03:00
|
|
|
emitError(QProcess::FailedToStart, tr("Debugging complex shell commands in a terminal"
|
2011-05-11 16:27:33 +02:00
|
|
|
" is currently not supported."));
|
2010-10-19 11:14:03 +02:00
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
pcmd = QLatin1String("/bin/sh");
|
2017-02-09 14:00:07 +01:00
|
|
|
pargs = QtcProcess::Arguments::createUnixArgs(
|
2017-02-22 15:09:35 +01:00
|
|
|
QStringList({"-c", (QtcProcess::quoteArg(program) + ' ' + args)}));
|
2010-10-19 11:14:03 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
QtcProcess::SplitError qerr;
|
2018-08-24 10:56:13 +02:00
|
|
|
const TerminalCommand terminal = terminalEmulator(d->m_settings);
|
|
|
|
|
const QtcProcess::Arguments terminalArgs = QtcProcess::prepareArgs(terminal.executeArgs,
|
|
|
|
|
&qerr,
|
|
|
|
|
HostOsInfo::hostOs(),
|
|
|
|
|
&d->m_environment,
|
|
|
|
|
&d->m_workingDir);
|
2010-10-19 11:14:03 +02:00
|
|
|
if (qerr != QtcProcess::SplitOk) {
|
2014-05-04 23:10:17 +03:00
|
|
|
emitError(QProcess::FailedToStart, qerr == QtcProcess::BadQuoting
|
2011-05-11 16:27:33 +02:00
|
|
|
? tr("Quoting error in terminal command.")
|
|
|
|
|
: tr("Terminal command may not be a shell command."));
|
2010-10-19 11:14:03 +02:00
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
2009-05-06 14:26:20 +02:00
|
|
|
const QString err = stubServerListen();
|
2009-02-20 12:33:16 +01:00
|
|
|
if (!err.isEmpty()) {
|
2014-05-04 23:10:17 +03:00
|
|
|
emitError(QProcess::FailedToStart, msgCommChannelFailed(err));
|
2009-02-20 12:33:16 +01:00
|
|
|
return false;
|
|
|
|
|
}
|
2008-12-02 12:01:29 +01:00
|
|
|
|
2014-05-14 16:12:54 +03:00
|
|
|
d->m_environment.unset(QLatin1String("TERM"));
|
2011-05-11 12:48:14 +02:00
|
|
|
QStringList env = d->m_environment.toStringList();
|
2010-10-19 11:14:03 +02:00
|
|
|
if (!env.isEmpty()) {
|
2010-09-02 13:39:19 +02:00
|
|
|
d->m_tempFile = new QTemporaryFile();
|
|
|
|
|
if (!d->m_tempFile->open()) {
|
2009-04-09 19:13:40 +02:00
|
|
|
stubServerShutdown();
|
2014-05-04 23:10:17 +03:00
|
|
|
emitError(QProcess::FailedToStart, msgCannotCreateTempFile(d->m_tempFile->errorString()));
|
2010-09-02 13:39:19 +02:00
|
|
|
delete d->m_tempFile;
|
|
|
|
|
d->m_tempFile = 0;
|
2009-04-09 19:13:40 +02:00
|
|
|
return false;
|
|
|
|
|
}
|
2011-03-30 11:54:57 +02:00
|
|
|
QByteArray contents;
|
2010-10-19 11:14:03 +02:00
|
|
|
foreach (const QString &var, env) {
|
2011-03-30 11:54:57 +02:00
|
|
|
QByteArray l8b = var.toLocal8Bit();
|
|
|
|
|
contents.append(l8b.constData(), l8b.size() + 1);
|
|
|
|
|
}
|
|
|
|
|
if (d->m_tempFile->write(contents) != contents.size() || !d->m_tempFile->flush()) {
|
|
|
|
|
stubServerShutdown();
|
2014-05-04 23:10:17 +03:00
|
|
|
emitError(QProcess::FailedToStart, msgCannotWriteTempFile());
|
2011-03-30 11:54:57 +02:00
|
|
|
delete d->m_tempFile;
|
|
|
|
|
d->m_tempFile = 0;
|
|
|
|
|
return false;
|
2009-04-09 19:13:40 +02:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2015-07-15 15:01:00 +02:00
|
|
|
const QString stubPath = QCoreApplication::applicationDirPath()
|
|
|
|
|
+ QLatin1String("/" QTC_REL_TOOLS_PATH "/qtcreator_process_stub");
|
2018-08-24 10:56:13 +02:00
|
|
|
const QStringList allArgs = terminalArgs.toUnixArgs()
|
|
|
|
|
<< stubPath
|
|
|
|
|
<< modeOption(d->m_mode)
|
|
|
|
|
<< d->m_stubServer.fullServerName()
|
|
|
|
|
<< msgPromptToClose()
|
|
|
|
|
<< workingDirectory()
|
|
|
|
|
<< (d->m_tempFile ? d->m_tempFile->fileName() : QString())
|
|
|
|
|
<< QString::number(getpid())
|
|
|
|
|
<< pcmd
|
|
|
|
|
<< pargs.toUnixArgs();
|
|
|
|
|
|
|
|
|
|
d->m_process.start(terminal.command, allArgs);
|
2010-09-02 13:39:19 +02:00
|
|
|
if (!d->m_process.waitForStarted()) {
|
2009-02-20 12:33:16 +01:00
|
|
|
stubServerShutdown();
|
2014-05-04 23:10:17 +03:00
|
|
|
emitError(QProcess::UnknownError, tr("Cannot start the terminal emulator \"%1\", change the setting in the "
|
2018-08-24 10:56:13 +02:00
|
|
|
"Environment options.").arg(terminal.command));
|
2010-09-02 13:39:19 +02:00
|
|
|
delete d->m_tempFile;
|
|
|
|
|
d->m_tempFile = 0;
|
2008-12-02 12:01:29 +01:00
|
|
|
return false;
|
2009-02-20 12:33:16 +01:00
|
|
|
}
|
2012-12-19 19:04:36 +01:00
|
|
|
d->m_stubConnectTimer = new QTimer(this);
|
2015-03-05 22:00:05 +02:00
|
|
|
connect(d->m_stubConnectTimer, &QTimer::timeout, this, &ConsoleProcess::stop);
|
2012-12-19 19:04:36 +01:00
|
|
|
d->m_stubConnectTimer->setSingleShot(true);
|
|
|
|
|
d->m_stubConnectTimer->start(10000);
|
2010-09-02 13:39:19 +02:00
|
|
|
d->m_executable = program;
|
2008-12-02 12:01:29 +01:00
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
2012-12-19 19:04:36 +01:00
|
|
|
void ConsoleProcess::killProcess()
|
2008-12-02 12:01:29 +01:00
|
|
|
{
|
2012-12-19 19:04:36 +01:00
|
|
|
if (d->m_stubSocket && d->m_stubSocket->isWritable()) {
|
|
|
|
|
d->m_stubSocket->write("k", 1);
|
|
|
|
|
d->m_stubSocket->flush();
|
|
|
|
|
}
|
2010-09-02 13:39:19 +02:00
|
|
|
d->m_appPid = 0;
|
2012-12-19 19:04:36 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void ConsoleProcess::killStub()
|
|
|
|
|
{
|
|
|
|
|
if (d->m_stubSocket && d->m_stubSocket->isWritable()) {
|
|
|
|
|
d->m_stubSocket->write("s", 1);
|
|
|
|
|
d->m_stubSocket->flush();
|
|
|
|
|
}
|
|
|
|
|
stubServerShutdown();
|
|
|
|
|
d->m_stubPid = 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void ConsoleProcess::detachStub()
|
|
|
|
|
{
|
|
|
|
|
if (d->m_stubSocket && d->m_stubSocket->isWritable()) {
|
|
|
|
|
d->m_stubSocket->write("d", 1);
|
|
|
|
|
d->m_stubSocket->flush();
|
|
|
|
|
}
|
|
|
|
|
stubServerShutdown();
|
|
|
|
|
d->m_stubPid = 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void ConsoleProcess::stop()
|
|
|
|
|
{
|
|
|
|
|
killProcess();
|
|
|
|
|
killStub();
|
|
|
|
|
if (isRunning()) {
|
|
|
|
|
d->m_process.terminate();
|
2016-04-29 16:52:58 +02:00
|
|
|
if (!d->m_process.waitForFinished(1000) && d->m_process.state() == QProcess::Running) {
|
2012-12-19 19:04:36 +01:00
|
|
|
d->m_process.kill();
|
|
|
|
|
d->m_process.waitForFinished();
|
|
|
|
|
}
|
|
|
|
|
}
|
2008-12-02 12:01:29 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool ConsoleProcess::isRunning() const
|
|
|
|
|
{
|
2013-03-20 15:33:30 +01:00
|
|
|
return d->m_process.state() != QProcess::NotRunning
|
|
|
|
|
|| (d->m_stubSocket && d->m_stubSocket->isOpen());
|
2008-12-02 12:01:29 +01:00
|
|
|
}
|
|
|
|
|
|
2009-02-20 12:33:16 +01:00
|
|
|
QString ConsoleProcess::stubServerListen()
|
2008-12-02 12:01:29 +01:00
|
|
|
{
|
2009-02-20 12:33:16 +01:00
|
|
|
// We need to put the socket in a private directory, as some systems simply do not
|
|
|
|
|
// check the file permissions of sockets.
|
|
|
|
|
QString stubFifoDir;
|
|
|
|
|
forever {
|
|
|
|
|
{
|
|
|
|
|
QTemporaryFile tf;
|
|
|
|
|
if (!tf.open())
|
2009-05-06 14:26:20 +02:00
|
|
|
return msgCannotCreateTempFile(tf.errorString());
|
2012-01-04 17:34:08 +01:00
|
|
|
stubFifoDir = tf.fileName();
|
2009-02-20 12:33:16 +01:00
|
|
|
}
|
|
|
|
|
// By now the temp file was deleted again
|
2010-09-02 13:39:19 +02:00
|
|
|
d->m_stubServerDir = QFile::encodeName(stubFifoDir);
|
|
|
|
|
if (!::mkdir(d->m_stubServerDir.constData(), 0700))
|
2009-02-20 12:33:16 +01:00
|
|
|
break;
|
|
|
|
|
if (errno != EEXIST)
|
2009-05-06 14:26:20 +02:00
|
|
|
return msgCannotCreateTempDir(stubFifoDir, QString::fromLocal8Bit(strerror(errno)));
|
2009-02-20 12:33:16 +01:00
|
|
|
}
|
2012-01-04 17:34:08 +01:00
|
|
|
const QString stubServer = stubFifoDir + QLatin1String("/stub-socket");
|
2010-09-02 13:39:19 +02:00
|
|
|
if (!d->m_stubServer.listen(stubServer)) {
|
|
|
|
|
::rmdir(d->m_stubServerDir.constData());
|
2014-04-17 14:09:47 +02:00
|
|
|
return tr("Cannot create socket \"%1\": %2").arg(stubServer, d->m_stubServer.errorString());
|
2009-02-20 12:33:16 +01:00
|
|
|
}
|
|
|
|
|
return QString();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void ConsoleProcess::stubServerShutdown()
|
|
|
|
|
{
|
2013-03-20 19:23:54 +01:00
|
|
|
if (d->m_stubSocket) {
|
|
|
|
|
readStubOutput(); // we could get the shutdown signal before emptying the buffer
|
|
|
|
|
d->m_stubSocket->disconnect(); // avoid getting queued readyRead signals
|
2012-12-19 19:04:36 +01:00
|
|
|
d->m_stubSocket->deleteLater(); // we might be called from the disconnected signal of m_stubSocket
|
2013-03-20 19:23:54 +01:00
|
|
|
}
|
2010-09-02 13:39:19 +02:00
|
|
|
d->m_stubSocket = 0;
|
|
|
|
|
if (d->m_stubServer.isListening()) {
|
|
|
|
|
d->m_stubServer.close();
|
|
|
|
|
::rmdir(d->m_stubServerDir.constData());
|
2009-02-20 12:33:16 +01:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void ConsoleProcess::stubConnectionAvailable()
|
|
|
|
|
{
|
2012-12-19 19:04:36 +01:00
|
|
|
if (d->m_stubConnectTimer) {
|
|
|
|
|
delete d->m_stubConnectTimer;
|
|
|
|
|
d->m_stubConnectTimer = 0;
|
|
|
|
|
}
|
|
|
|
|
d->m_stubConnected = true;
|
|
|
|
|
emit stubStarted();
|
2010-09-02 13:39:19 +02:00
|
|
|
d->m_stubSocket = d->m_stubServer.nextPendingConnection();
|
2015-03-05 22:00:05 +02:00
|
|
|
connect(d->m_stubSocket, &QIODevice::readyRead, this, &ConsoleProcess::readStubOutput);
|
|
|
|
|
connect(d->m_stubSocket, &QLocalSocket::disconnected, this, &ConsoleProcess::stubExited);
|
2008-12-02 12:01:29 +01:00
|
|
|
}
|
|
|
|
|
|
2009-02-20 12:33:16 +01:00
|
|
|
static QString errorMsg(int code)
|
2008-12-02 12:01:29 +01:00
|
|
|
{
|
2009-02-20 12:33:16 +01:00
|
|
|
return QString::fromLocal8Bit(strerror(code));
|
2008-12-02 12:01:29 +01:00
|
|
|
}
|
|
|
|
|
|
2009-02-20 12:33:16 +01:00
|
|
|
void ConsoleProcess::readStubOutput()
|
2008-12-02 12:01:29 +01:00
|
|
|
{
|
2010-09-02 13:39:19 +02:00
|
|
|
while (d->m_stubSocket->canReadLine()) {
|
|
|
|
|
QByteArray out = d->m_stubSocket->readLine();
|
2009-02-20 12:33:16 +01:00
|
|
|
out.chop(1); // \n
|
|
|
|
|
if (out.startsWith("err:chdir ")) {
|
2014-05-04 23:10:17 +03:00
|
|
|
emitError(QProcess::FailedToStart, msgCannotChangeToWorkDir(workingDirectory(), errorMsg(out.mid(10).toInt())));
|
2009-02-20 12:33:16 +01:00
|
|
|
} else if (out.startsWith("err:exec ")) {
|
2014-05-04 23:10:17 +03:00
|
|
|
emitError(QProcess::FailedToStart, msgCannotExecute(d->m_executable, errorMsg(out.mid(9).toInt())));
|
2012-12-19 19:04:36 +01:00
|
|
|
} else if (out.startsWith("spid ")) {
|
2010-09-02 13:39:19 +02:00
|
|
|
delete d->m_tempFile;
|
|
|
|
|
d->m_tempFile = 0;
|
2009-04-09 19:13:40 +02:00
|
|
|
|
2012-12-19 19:04:36 +01:00
|
|
|
d->m_stubPid = out.mid(4).toInt();
|
|
|
|
|
} else if (out.startsWith("pid ")) {
|
2010-09-02 13:39:19 +02:00
|
|
|
d->m_appPid = out.mid(4).toInt();
|
2009-02-20 12:33:16 +01:00
|
|
|
emit processStarted();
|
|
|
|
|
} else if (out.startsWith("exit ")) {
|
2010-09-02 13:39:19 +02:00
|
|
|
d->m_appStatus = QProcess::NormalExit;
|
|
|
|
|
d->m_appCode = out.mid(5).toInt();
|
|
|
|
|
d->m_appPid = 0;
|
2013-08-01 15:43:10 +02:00
|
|
|
emit processStopped(d->m_appCode, d->m_appStatus);
|
2009-02-20 12:33:16 +01:00
|
|
|
} else if (out.startsWith("crash ")) {
|
2010-09-02 13:39:19 +02:00
|
|
|
d->m_appStatus = QProcess::CrashExit;
|
|
|
|
|
d->m_appCode = out.mid(6).toInt();
|
|
|
|
|
d->m_appPid = 0;
|
2013-08-01 15:43:10 +02:00
|
|
|
emit processStopped(d->m_appCode, d->m_appStatus);
|
2009-02-20 12:33:16 +01:00
|
|
|
} else {
|
2014-05-04 23:10:17 +03:00
|
|
|
emitError(QProcess::UnknownError, msgUnexpectedOutput(out));
|
2012-12-19 19:04:36 +01:00
|
|
|
d->m_stubPid = 0;
|
2010-09-02 13:39:19 +02:00
|
|
|
d->m_process.terminate();
|
2009-02-20 12:33:16 +01:00
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
2008-12-02 12:01:29 +01:00
|
|
|
}
|
|
|
|
|
|
2009-02-20 12:33:16 +01:00
|
|
|
void ConsoleProcess::stubExited()
|
|
|
|
|
{
|
|
|
|
|
// The stub exit might get noticed before we read the error status.
|
2010-09-02 13:39:19 +02:00
|
|
|
if (d->m_stubSocket && d->m_stubSocket->state() == QLocalSocket::ConnectedState)
|
|
|
|
|
d->m_stubSocket->waitForDisconnected();
|
2009-02-20 12:33:16 +01:00
|
|
|
stubServerShutdown();
|
2012-12-19 19:04:36 +01:00
|
|
|
d->m_stubPid = 0;
|
2010-09-02 13:39:19 +02:00
|
|
|
delete d->m_tempFile;
|
|
|
|
|
d->m_tempFile = 0;
|
|
|
|
|
if (d->m_appPid) {
|
|
|
|
|
d->m_appStatus = QProcess::CrashExit;
|
|
|
|
|
d->m_appCode = -1;
|
|
|
|
|
d->m_appPid = 0;
|
2013-08-01 15:43:10 +02:00
|
|
|
emit processStopped(d->m_appCode, d->m_appStatus); // Maybe it actually did not, but keep state consistent
|
2009-02-20 12:33:16 +01:00
|
|
|
}
|
2012-12-19 19:04:36 +01:00
|
|
|
emit stubStopped();
|
2009-02-20 12:33:16 +01:00
|
|
|
}
|
2009-04-09 20:09:10 +02:00
|
|
|
|
2018-08-24 10:56:13 +02:00
|
|
|
Q_GLOBAL_STATIC_WITH_ARGS(const QVector<TerminalCommand>, knownTerminals, (
|
2012-08-29 09:13:42 +02:00
|
|
|
{
|
2018-08-24 10:56:13 +02:00
|
|
|
{"x-terminal-emulator", "", "-e"},
|
|
|
|
|
{"xterm", "", "-e"},
|
|
|
|
|
{"aterm", "", "-e"},
|
|
|
|
|
{"Eterm", "", "-e"},
|
|
|
|
|
{"rxvt", "", "-e"},
|
|
|
|
|
{"urxvt", "", "-e"},
|
|
|
|
|
{"xfce4-terminal", "", "-x"},
|
|
|
|
|
{"konsole", "--separate", "-e"},
|
|
|
|
|
{"gnome-terminal", "", "--"}
|
|
|
|
|
}));
|
|
|
|
|
|
|
|
|
|
TerminalCommand ConsoleProcess::defaultTerminalEmulator()
|
2009-04-09 20:09:10 +02:00
|
|
|
{
|
2018-08-24 10:56:13 +02:00
|
|
|
static TerminalCommand defaultTerm;
|
|
|
|
|
if (defaultTerm.command.isEmpty()) {
|
|
|
|
|
defaultTerm = []() -> TerminalCommand {
|
|
|
|
|
if (HostOsInfo::isMacHost()) {
|
2018-09-05 16:36:57 +02:00
|
|
|
const QString termCmd = QCoreApplication::applicationDirPath()
|
|
|
|
|
+ "/../Resources/scripts/openTerminal.command";
|
2018-08-24 10:56:13 +02:00
|
|
|
if (QFileInfo::exists(termCmd))
|
2018-09-05 16:36:57 +02:00
|
|
|
return {termCmd, "", ""};
|
2018-08-24 10:56:13 +02:00
|
|
|
return {"/usr/X11/bin/xterm", "", "-e"};
|
|
|
|
|
}
|
|
|
|
|
const Environment env = Environment::systemEnvironment();
|
|
|
|
|
for (const TerminalCommand &term : *knownTerminals) {
|
|
|
|
|
const QString result = env.searchInPath(term.command).toString();
|
|
|
|
|
if (!result.isEmpty())
|
|
|
|
|
return {result, term.openArgs, term.executeArgs};
|
|
|
|
|
}
|
|
|
|
|
return {"xterm", "", "-e"};
|
|
|
|
|
}();
|
2012-12-19 19:04:36 +01:00
|
|
|
}
|
2018-08-24 10:56:13 +02:00
|
|
|
return defaultTerm;
|
2012-08-29 09:13:42 +02:00
|
|
|
}
|
|
|
|
|
|
2018-08-24 10:56:13 +02:00
|
|
|
QVector<TerminalCommand> ConsoleProcess::availableTerminalEmulators()
|
2012-08-29 09:13:42 +02:00
|
|
|
{
|
2018-08-24 10:56:13 +02:00
|
|
|
QVector<TerminalCommand> result;
|
2012-08-29 09:13:42 +02:00
|
|
|
const Environment env = Environment::systemEnvironment();
|
2018-08-24 10:56:13 +02:00
|
|
|
for (const TerminalCommand &term : *knownTerminals) {
|
|
|
|
|
const QString command = env.searchInPath(term.command).toString();
|
|
|
|
|
if (!command.isEmpty())
|
|
|
|
|
result.push_back({command, term.openArgs, term.executeArgs});
|
2012-08-29 09:13:42 +02:00
|
|
|
}
|
2018-08-24 10:56:13 +02:00
|
|
|
// sort and put default terminal on top
|
|
|
|
|
const TerminalCommand defaultTerm = defaultTerminalEmulator();
|
|
|
|
|
result.removeAll(defaultTerm);
|
|
|
|
|
sort(result);
|
|
|
|
|
result.prepend(defaultTerm);
|
2012-08-29 09:13:42 +02:00
|
|
|
return result;
|
2009-04-09 20:09:10 +02:00
|
|
|
}
|
|
|
|
|
|
2018-08-24 10:56:13 +02:00
|
|
|
const char kTerminalVersion[] = "4.8";
|
|
|
|
|
const char kTerminalVersionKey[] = "General/Terminal/SettingsVersion";
|
|
|
|
|
const char kTerminalCommandKey[] = "General/Terminal/Command";
|
|
|
|
|
const char kTerminalOpenOptionsKey[] = "General/Terminal/OpenOptions";
|
|
|
|
|
const char kTerminalExecuteOptionsKey[] = "General/Terminal/ExecuteOptions";
|
|
|
|
|
|
|
|
|
|
TerminalCommand ConsoleProcess::terminalEmulator(const QSettings *settings)
|
2017-01-11 13:57:27 +01:00
|
|
|
{
|
|
|
|
|
if (settings) {
|
2018-08-24 10:56:13 +02:00
|
|
|
if (settings->value(kTerminalVersionKey).toString() == kTerminalVersion) {
|
|
|
|
|
if (settings->contains(kTerminalCommandKey))
|
|
|
|
|
return {settings->value(kTerminalCommandKey).toString(),
|
|
|
|
|
settings->value(kTerminalOpenOptionsKey).toString(),
|
|
|
|
|
settings->value(kTerminalExecuteOptionsKey).toString()};
|
|
|
|
|
} else {
|
|
|
|
|
// TODO remove reading of old settings some time after 4.8
|
|
|
|
|
const QString value = settings->value("General/TerminalEmulator").toString().trimmed();
|
|
|
|
|
if (!value.isEmpty()) {
|
|
|
|
|
// split off command and options
|
|
|
|
|
const QStringList splitCommand = QtcProcess::splitArgs(value);
|
|
|
|
|
if (QTC_GUARD(!splitCommand.isEmpty())) {
|
|
|
|
|
const QString command = splitCommand.first();
|
|
|
|
|
const QStringList quotedArgs = Utils::transform(splitCommand.mid(1),
|
|
|
|
|
&QtcProcess::quoteArgUnix);
|
|
|
|
|
const QString options = quotedArgs.join(' ');
|
|
|
|
|
return {command, "", options};
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2017-01-11 13:57:27 +01:00
|
|
|
}
|
|
|
|
|
return defaultTerminalEmulator();
|
|
|
|
|
}
|
|
|
|
|
|
2018-08-24 10:56:13 +02:00
|
|
|
void ConsoleProcess::setTerminalEmulator(QSettings *settings, const TerminalCommand &term)
|
2017-01-11 13:57:27 +01:00
|
|
|
{
|
2018-08-24 10:56:13 +02:00
|
|
|
settings->setValue(kTerminalVersionKey, kTerminalVersion);
|
|
|
|
|
if (term == defaultTerminalEmulator()) {
|
|
|
|
|
settings->remove(kTerminalCommandKey);
|
|
|
|
|
settings->remove(kTerminalOpenOptionsKey);
|
|
|
|
|
settings->remove(kTerminalExecuteOptionsKey);
|
|
|
|
|
} else {
|
|
|
|
|
settings->setValue(kTerminalCommandKey, term.command);
|
|
|
|
|
settings->setValue(kTerminalOpenOptionsKey, term.openArgs);
|
|
|
|
|
settings->setValue(kTerminalExecuteOptionsKey, term.executeArgs);
|
|
|
|
|
}
|
2017-01-11 13:57:27 +01:00
|
|
|
}
|
|
|
|
|
|
2018-08-31 10:54:47 +02:00
|
|
|
bool ConsoleProcess::startTerminalEmulator(QSettings *settings, const QString &workingDir,
|
|
|
|
|
const Utils::Environment &env)
|
2017-01-11 13:55:31 +01:00
|
|
|
{
|
2018-08-24 10:56:13 +02:00
|
|
|
const TerminalCommand term = terminalEmulator(settings);
|
2018-08-31 10:54:47 +02:00
|
|
|
|
|
|
|
|
QProcess process;
|
|
|
|
|
process.setProgram(term.command);
|
|
|
|
|
process.setArguments(QtcProcess::splitArgs(term.openArgs));
|
|
|
|
|
process.setProcessEnvironment(env.toProcessEnvironment());
|
|
|
|
|
process.setWorkingDirectory(workingDir);
|
|
|
|
|
|
|
|
|
|
return process.startDetached();
|
2017-01-11 13:55:31 +01:00
|
|
|
}
|
|
|
|
|
|
2010-09-02 13:39:19 +02:00
|
|
|
} // namespace Utils
|