2012-10-02 09:12:39 +02:00
|
|
|
/****************************************************************************
|
2010-10-14 18:05:43 +02:00
|
|
|
**
|
2016-01-15 14:58:39 +01:00
|
|
|
** Copyright (C) 2016 The Qt Company Ltd.
|
|
|
|
|
** Contact: https://www.qt.io/licensing/
|
2010-10-14 18:05:43 +02:00
|
|
|
**
|
2012-10-02 09:12:39 +02:00
|
|
|
** This file is part of Qt Creator.
|
2010-10-14 18:05:43 +02: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.
|
2010-10-14 18:05:43 +02: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 17:14:20 +01:00
|
|
|
**
|
2012-10-02 09:12:39 +02:00
|
|
|
****************************************************************************/
|
2010-10-14 18:05:43 +02:00
|
|
|
|
2016-03-18 07:55:01 +01:00
|
|
|
#pragma once
|
2010-10-14 18:05:43 +02:00
|
|
|
|
2021-05-05 18:21:22 +02:00
|
|
|
#include "utils_global.h"
|
|
|
|
|
|
2021-05-11 14:34:56 +02:00
|
|
|
#include "commandline.h"
|
2022-02-18 00:56:14 +01:00
|
|
|
#include "processenums.h"
|
2010-10-14 18:05:43 +02:00
|
|
|
|
2013-03-18 14:47:33 +01:00
|
|
|
#include <QProcess>
|
2021-05-05 18:21:22 +02:00
|
|
|
|
2022-03-29 18:01:08 +02:00
|
|
|
QT_BEGIN_NAMESPACE
|
|
|
|
|
class QDebug;
|
|
|
|
|
class QTextCodec;
|
|
|
|
|
QT_END_NAMESPACE
|
2013-03-18 14:47:33 +01:00
|
|
|
|
2021-06-14 15:09:11 +02:00
|
|
|
class tst_QtcProcess;
|
|
|
|
|
|
2010-10-14 18:05:43 +02:00
|
|
|
namespace Utils {
|
2021-05-05 18:21:22 +02:00
|
|
|
|
2021-05-06 11:15:28 +02:00
|
|
|
namespace Internal { class QtcProcessPrivate; }
|
|
|
|
|
|
2022-05-06 12:42:03 +02:00
|
|
|
class Environment;
|
2022-02-18 01:53:33 +01:00
|
|
|
class DeviceProcessHooks;
|
2022-03-29 18:01:08 +02:00
|
|
|
class ProcessInterface;
|
2022-04-08 17:24:59 +02:00
|
|
|
class ProcessResultData;
|
2022-02-18 01:53:33 +01:00
|
|
|
|
2022-05-06 11:59:17 +02:00
|
|
|
class QTCREATOR_UTILS_EXPORT QtcProcess final : public QObject
|
2021-05-05 18:21:22 +02:00
|
|
|
{
|
2021-05-12 14:25:50 +02:00
|
|
|
Q_OBJECT
|
|
|
|
|
|
2021-05-05 18:21:22 +02:00
|
|
|
public:
|
2022-02-10 19:25:03 +01:00
|
|
|
QtcProcess(QObject *parent = nullptr);
|
|
|
|
|
~QtcProcess();
|
|
|
|
|
|
2022-02-17 23:25:54 +01:00
|
|
|
// ProcessInterface related
|
|
|
|
|
|
2022-04-12 11:18:09 +02:00
|
|
|
void start();
|
2022-04-11 19:45:04 +02:00
|
|
|
|
2022-05-06 10:43:51 +02:00
|
|
|
void terminate();
|
|
|
|
|
void kill();
|
|
|
|
|
void interrupt();
|
2022-04-11 19:45:04 +02:00
|
|
|
void kickoffProcess();
|
2022-03-29 18:01:08 +02:00
|
|
|
void close();
|
2022-06-03 14:28:30 +02:00
|
|
|
void stop();
|
2022-02-17 23:25:54 +01:00
|
|
|
|
2022-05-06 10:43:51 +02:00
|
|
|
QByteArray readAllStandardOutput();
|
|
|
|
|
QByteArray readAllStandardError();
|
2022-04-27 14:19:49 +02:00
|
|
|
|
2022-05-06 10:43:51 +02:00
|
|
|
qint64 write(const QString &input);
|
2022-04-27 14:19:49 +02:00
|
|
|
qint64 writeRaw(const QByteArray &input);
|
2022-02-17 23:25:54 +01:00
|
|
|
|
2022-05-06 10:43:51 +02:00
|
|
|
qint64 processId() const;
|
2022-04-11 19:45:04 +02:00
|
|
|
qint64 applicationMainThreadId() const;
|
|
|
|
|
|
2022-04-12 11:18:09 +02:00
|
|
|
QProcess::ProcessState state() const;
|
2022-04-08 17:24:59 +02:00
|
|
|
virtual ProcessResultData resultData() const;
|
|
|
|
|
|
|
|
|
|
int exitCode() const;
|
|
|
|
|
QProcess::ExitStatus exitStatus() const;
|
2022-02-17 23:25:54 +01:00
|
|
|
|
2022-03-29 18:01:08 +02:00
|
|
|
QProcess::ProcessError error() const;
|
2022-04-08 17:24:59 +02:00
|
|
|
QString errorString() const;
|
2022-02-17 23:25:54 +01:00
|
|
|
|
2022-03-29 18:01:08 +02:00
|
|
|
bool waitForStarted(int msecs = 30000);
|
|
|
|
|
bool waitForReadyRead(int msecs = 30000);
|
|
|
|
|
bool waitForFinished(int msecs = 30000);
|
2022-02-17 23:25:54 +01:00
|
|
|
|
|
|
|
|
// ProcessSetupData related
|
|
|
|
|
|
2022-02-10 19:25:03 +01:00
|
|
|
void setProcessImpl(ProcessImpl processImpl);
|
|
|
|
|
|
|
|
|
|
void setTerminalMode(TerminalMode mode);
|
|
|
|
|
TerminalMode terminalMode() const;
|
2022-02-18 00:56:14 +01:00
|
|
|
bool usesTerminal() const { return terminalMode() != TerminalMode::Off; }
|
2022-02-10 19:25:03 +01:00
|
|
|
|
|
|
|
|
void setProcessMode(ProcessMode processMode);
|
|
|
|
|
ProcessMode processMode() const;
|
|
|
|
|
|
2022-05-09 15:02:23 +02:00
|
|
|
void setEnvironment(const Environment &env); // Main process
|
2021-05-05 16:05:53 +02:00
|
|
|
const Environment &environment() const;
|
|
|
|
|
|
2022-05-09 15:02:23 +02:00
|
|
|
void setControlEnvironment(const Environment &env); // Possible helper process (ssh on host etc)
|
|
|
|
|
const Environment &controlEnvironment() const;
|
2022-02-17 16:27:11 +01:00
|
|
|
|
2021-05-05 16:05:53 +02:00
|
|
|
void setCommand(const CommandLine &cmdLine);
|
|
|
|
|
const CommandLine &commandLine() const;
|
2019-05-28 18:59:45 +02:00
|
|
|
|
2021-06-03 10:39:15 +02:00
|
|
|
void setWorkingDirectory(const FilePath &dir);
|
2022-02-17 23:25:54 +01:00
|
|
|
FilePath workingDirectory() const;
|
|
|
|
|
|
|
|
|
|
void setWriteData(const QByteArray &writeData);
|
2021-05-14 15:21:54 +02:00
|
|
|
|
2022-03-14 17:02:11 +01:00
|
|
|
void setUseCtrlCStub(bool enabled); // release only
|
2021-05-05 16:05:53 +02:00
|
|
|
void setLowPriority();
|
|
|
|
|
void setDisableUnixTerminal();
|
2021-09-08 14:51:49 +02:00
|
|
|
void setRunAsRoot(bool on);
|
2022-02-01 14:51:23 +01:00
|
|
|
bool isRunAsRoot() const;
|
2022-01-21 16:15:17 +01:00
|
|
|
void setAbortOnMetaChars(bool abort);
|
|
|
|
|
|
2022-04-25 14:15:23 +02:00
|
|
|
QProcess::ProcessChannelMode processChannelMode() const;
|
2022-02-17 23:25:54 +01:00
|
|
|
void setProcessChannelMode(QProcess::ProcessChannelMode mode);
|
|
|
|
|
void setStandardInputFile(const QString &inputFile);
|
|
|
|
|
|
|
|
|
|
void setExtraData(const QString &key, const QVariant &value);
|
|
|
|
|
QVariant extraData(const QString &key) const;
|
|
|
|
|
|
|
|
|
|
void setExtraData(const QVariantHash &extraData);
|
|
|
|
|
QVariantHash extraData() const;
|
|
|
|
|
|
2022-06-03 14:28:30 +02:00
|
|
|
void setReaperTimeout(int msecs);
|
|
|
|
|
int reaperTimeout() const;
|
|
|
|
|
|
2022-02-17 23:25:54 +01:00
|
|
|
static void setRemoteProcessHooks(const DeviceProcessHooks &hooks);
|
|
|
|
|
|
|
|
|
|
// TODO: Some usages of this method assume that Starting phase is also a running state
|
|
|
|
|
// i.e. if isRunning() returns false, they assume NotRunning state, what may be an error.
|
|
|
|
|
bool isRunning() const; // Short for state() == QProcess::Running.
|
|
|
|
|
|
|
|
|
|
// Other enhancements.
|
|
|
|
|
// These (or some of them) may be potentially moved outside of the class.
|
|
|
|
|
// For some we may aggregate in another public utils class (or subclass of QtcProcess)?
|
|
|
|
|
|
|
|
|
|
// TODO: How below 3 methods relate to QtcProcess? Action: move them somewhere else.
|
|
|
|
|
// Helpers to find binaries. Do not use it for other path variables
|
|
|
|
|
// and file types.
|
|
|
|
|
static QString locateBinary(const QString &binary);
|
|
|
|
|
static QString locateBinary(const QString &path, const QString &binary);
|
|
|
|
|
static QString normalizeNewlines(const QString &text);
|
|
|
|
|
|
|
|
|
|
// TODO: Unused currently? Should it serve as a compartment for contrary of remoteEnvironment?
|
|
|
|
|
static Environment systemEnvironmentForBinary(const FilePath &filePath);
|
|
|
|
|
|
2021-08-25 12:34:54 +02:00
|
|
|
static bool startDetached(const CommandLine &cmd, const FilePath &workingDirectory = {},
|
2021-08-12 12:33:28 +02:00
|
|
|
qint64 *pid = nullptr);
|
2022-01-24 19:57:52 +01:00
|
|
|
|
|
|
|
|
// Starts the command and waits for finish.
|
2022-03-23 16:39:47 +01:00
|
|
|
// User input processing is enabled when EventLoopMode::On was passed.
|
2022-03-02 04:12:25 +01:00
|
|
|
void runBlocking(EventLoopMode eventLoopMode = EventLoopMode::Off);
|
2021-06-22 04:24:13 +02:00
|
|
|
|
2021-05-06 11:15:28 +02:00
|
|
|
/* Timeout for hanging processes (triggers after no more output
|
|
|
|
|
* occurs on stderr/stdout). */
|
|
|
|
|
void setTimeoutS(int timeoutS);
|
|
|
|
|
|
2022-02-17 23:25:54 +01:00
|
|
|
// TODO: We should specify the purpose of the codec, e.g. setCodecForStandardChannel()
|
2021-05-06 11:15:28 +02:00
|
|
|
void setCodec(QTextCodec *c);
|
|
|
|
|
void setTimeOutMessageBoxEnabled(bool);
|
2022-03-02 04:12:25 +01:00
|
|
|
void setExitCodeInterpreter(const ExitCodeInterpreter &interpreter);
|
2021-05-06 11:15:28 +02:00
|
|
|
|
|
|
|
|
void setStdOutCallback(const std::function<void(const QString &)> &callback);
|
2021-06-04 11:40:35 +02:00
|
|
|
void setStdOutLineCallback(const std::function<void(const QString &)> &callback);
|
2021-05-06 11:15:28 +02:00
|
|
|
void setStdErrCallback(const std::function<void(const QString &)> &callback);
|
2021-06-04 11:40:35 +02:00
|
|
|
void setStdErrLineCallback(const std::function<void(const QString &)> &callback);
|
2021-05-06 11:15:28 +02:00
|
|
|
|
2021-05-06 13:07:36 +02:00
|
|
|
bool readDataFromProcess(int timeoutS, QByteArray *stdOut, QByteArray *stdErr,
|
|
|
|
|
bool showTimeOutMessageBox);
|
|
|
|
|
|
2022-03-02 04:12:25 +01:00
|
|
|
ProcessResult result() const;
|
|
|
|
|
void setResult(const ProcessResult &result);
|
2021-05-12 14:25:50 +02:00
|
|
|
|
|
|
|
|
QByteArray allRawOutput() const;
|
|
|
|
|
QString allOutput() const;
|
|
|
|
|
|
|
|
|
|
QByteArray rawStdOut() const;
|
|
|
|
|
|
2022-06-16 15:13:07 +02:00
|
|
|
QString stdOut() const; // possibly with CR
|
|
|
|
|
QString stdErr() const; // possibly with CR
|
|
|
|
|
|
|
|
|
|
QString cleanedStdOut() const; // with CR removed
|
|
|
|
|
QString cleanedStdErr() const; // with CR removed
|
|
|
|
|
|
|
|
|
|
const QStringList stdOutLines() const; // split, CR removed
|
|
|
|
|
const QStringList stdErrLines() const; // split, CR removed
|
|
|
|
|
|
2022-04-05 13:47:11 +02:00
|
|
|
QString exitMessage() const;
|
2021-05-12 14:25:50 +02:00
|
|
|
|
2021-09-09 23:03:39 +02:00
|
|
|
QString toStandaloneCommandLine() const;
|
|
|
|
|
|
2022-03-29 18:01:08 +02:00
|
|
|
signals:
|
|
|
|
|
void started();
|
|
|
|
|
void finished();
|
2022-04-04 13:17:36 +02:00
|
|
|
void done(); // The same as finished() with the addition it's being also emitted after
|
|
|
|
|
// FailedToStart error occurred.
|
2022-03-29 18:01:08 +02:00
|
|
|
void errorOccurred(QProcess::ProcessError error);
|
|
|
|
|
void readyReadStandardOutput();
|
|
|
|
|
void readyReadStandardError();
|
|
|
|
|
|
2021-05-06 13:07:36 +02:00
|
|
|
private:
|
2021-05-14 19:00:50 +02:00
|
|
|
friend QTCREATOR_UTILS_EXPORT QDebug operator<<(QDebug str, const QtcProcess &r);
|
2021-05-12 14:25:50 +02:00
|
|
|
|
2022-02-18 01:53:33 +01:00
|
|
|
friend class Internal::QtcProcessPrivate;
|
2021-05-06 13:07:36 +02:00
|
|
|
Internal::QtcProcessPrivate *d = nullptr;
|
|
|
|
|
};
|
|
|
|
|
|
2022-02-18 01:53:33 +01:00
|
|
|
class DeviceProcessHooks
|
2022-02-14 11:14:08 +01:00
|
|
|
{
|
|
|
|
|
public:
|
2022-02-21 12:49:14 +01:00
|
|
|
std::function<ProcessInterface *(const FilePath &)> processImplHook;
|
2022-02-18 01:53:33 +01:00
|
|
|
std::function<Environment(const FilePath &)> systemEnvironmentForBinary;
|
2022-02-14 11:14:08 +01:00
|
|
|
};
|
|
|
|
|
|
2013-07-12 09:00:12 +02:00
|
|
|
} // namespace Utils
|