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
|
/*
SPDX-FileCopyrightText: 2009 Andreas Pakulat <apaku@gmx.de>
SPDX-License-Identifier: LGPL-2.0-or-later
*/
#include "commandexecutor.h"
#include "processlinemaker.h"
#include <KProcess>
#include <KShell>
#include <QMap>
#include <QStringList>
#include <QString>
namespace KDevelop {
class CommandExecutorPrivate
{
public:
explicit CommandExecutorPrivate(CommandExecutor* cmd)
: m_exec(cmd)
, m_useShell(false)
{
}
CommandExecutor* m_exec;
KProcess* m_process;
ProcessLineMaker* m_lineMaker;
QString m_command;
QStringList m_args;
QString m_workDir;
QMap<QString, QString> m_env;
bool m_useShell;
void procError(QProcess::ProcessError error)
{
Q_UNUSED(error)
m_lineMaker->flushBuffers();
emit m_exec->failed(error);
}
void procFinished(int code, QProcess::ExitStatus status)
{
m_lineMaker->flushBuffers();
if (status == QProcess::NormalExit)
emit m_exec->completed(code);
}
};
CommandExecutor::CommandExecutor(const QString& command, QObject* parent)
: QObject(parent)
, d_ptr(new CommandExecutorPrivate(this))
{
Q_D(CommandExecutor);
d->m_process = new KProcess(this);
d->m_process->setOutputChannelMode(KProcess::SeparateChannels);
d->m_lineMaker = new ProcessLineMaker(d->m_process);
d->m_command = command;
connect(d->m_lineMaker, &ProcessLineMaker::receivedStdoutLines,
this, &CommandExecutor::receivedStandardOutput);
connect(d->m_lineMaker, &ProcessLineMaker::receivedStderrLines,
this, &CommandExecutor::receivedStandardError);
connect(d->m_process, &QProcess::errorOccurred,
this, [this](QProcess::ProcessError error) {
Q_D(CommandExecutor);
d->procError(error);
});
connect(d->m_process, QOverload<int, QProcess::ExitStatus>::of(&QProcess::finished),
this, [this](int code, QProcess::ExitStatus status) {
Q_D(CommandExecutor);
d->procFinished(code, status);
});
}
CommandExecutor::~CommandExecutor()
{
Q_D(CommandExecutor);
delete d->m_process;
delete d->m_lineMaker;
}
void CommandExecutor::setEnvironment(const QMap<QString, QString>& env)
{
Q_D(CommandExecutor);
d->m_env = env;
}
void CommandExecutor::setEnvironment(const QStringList& env)
{
Q_D(CommandExecutor);
QMap<QString, QString> envmap;
for (const QString& var : env) {
int sep = var.indexOf(QLatin1Char('='));
envmap.insert(var.left(sep), var.mid(sep + 1));
}
d->m_env = envmap;
}
void CommandExecutor::setArguments(const QStringList& args)
{
Q_D(CommandExecutor);
d->m_args = args;
}
void CommandExecutor::setWorkingDirectory(const QString& dir)
{
Q_D(CommandExecutor);
d->m_workDir = dir;
}
bool CommandExecutor::useShell() const
{
Q_D(const CommandExecutor);
return d->m_useShell;
}
void CommandExecutor::setUseShell(bool shell)
{
Q_D(CommandExecutor);
d->m_useShell = shell;
}
void CommandExecutor::start()
{
Q_D(CommandExecutor);
for (auto it = d->m_env.constBegin(), itEnd = d->m_env.constEnd(); it != itEnd; ++it) {
d->m_process->setEnv(it.key(), it.value());
}
d->m_process->setWorkingDirectory(d->m_workDir);
if (!d->m_useShell) {
d->m_process->setProgram(d->m_command, d->m_args);
} else {
QStringList arguments;
arguments.reserve(d->m_args.size());
for (const QString& a : std::as_const(d->m_args)) {
arguments << KShell::quoteArg(a);
}
d->m_process->setShellCommand(d->m_command + QLatin1Char(' ') + arguments.join(QLatin1Char(' ')));
}
d->m_process->start();
}
void CommandExecutor::setCommand(const QString& command)
{
Q_D(CommandExecutor);
d->m_command = command;
}
void CommandExecutor::kill()
{
Q_D(CommandExecutor);
d->m_process->kill();
}
QString CommandExecutor::command() const
{
Q_D(const CommandExecutor);
return d->m_command;
}
QStringList CommandExecutor::arguments() const
{
Q_D(const CommandExecutor);
return d->m_args;
}
QString CommandExecutor::workingDirectory() const
{
Q_D(const CommandExecutor);
return d->m_workDir;
}
}
#include "moc_commandexecutor.cpp"
|