2010-03-10 22:36:47 +00:00
|
|
|
/**
|
|
|
|
******************************************************************************
|
|
|
|
*
|
|
|
|
* @file synchronousprocess.cpp
|
|
|
|
* @author The OpenPilot Team, http://www.openpilot.org Copyright (C) 2010.
|
|
|
|
* Parts by Nokia Corporation (qt-info@nokia.com) Copyright (C) 2009.
|
2013-05-19 17:37:30 +03:00
|
|
|
* @brief
|
2010-03-10 22:36:47 +00:00
|
|
|
* @see The GNU Public License (GPL) Version 3
|
2013-05-19 17:37:30 +03:00
|
|
|
* @defgroup
|
2010-03-10 22:36:47 +00:00
|
|
|
* @{
|
2013-05-19 17:37:30 +03:00
|
|
|
*
|
2010-03-10 22:36:47 +00:00
|
|
|
*****************************************************************************/
|
2013-05-19 17:37:30 +03:00
|
|
|
/*
|
|
|
|
* This program is free software; you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation; either version 3 of the License, or
|
2010-03-10 22:36:47 +00:00
|
|
|
* (at your option) any later version.
|
2013-05-19 17:37:30 +03:00
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful, but
|
|
|
|
* WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
|
|
|
|
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
2010-03-10 22:36:47 +00:00
|
|
|
* for more details.
|
2013-05-19 17:37:30 +03:00
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License along
|
|
|
|
* with this program; if not, write to the Free Software Foundation, Inc.,
|
2010-03-10 22:36:47 +00:00
|
|
|
* 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
|
|
|
*/
|
2010-02-01 14:10:06 +00:00
|
|
|
|
|
|
|
#include "synchronousprocess.h"
|
|
|
|
|
|
|
|
#include <QtCore/QDebug>
|
|
|
|
#include <QtCore/QTimer>
|
|
|
|
#include <QtCore/QEventLoop>
|
|
|
|
#include <QtCore/QTextCodec>
|
|
|
|
#include <QtCore/QFileInfo>
|
|
|
|
#include <QtCore/QDir>
|
|
|
|
|
2013-09-15 23:05:52 +02:00
|
|
|
#include <QtWidgets/QApplication>
|
2010-02-01 14:10:06 +00:00
|
|
|
|
|
|
|
#include <limits.h>
|
|
|
|
|
|
|
|
enum { debug = 0 };
|
|
|
|
|
|
|
|
enum { defaultMaxHangTimerCount = 10 };
|
|
|
|
|
|
|
|
namespace Utils {
|
|
|
|
// ----------- SynchronousProcessResponse
|
|
|
|
SynchronousProcessResponse::SynchronousProcessResponse() :
|
2013-05-19 17:37:30 +03:00
|
|
|
result(StartFailed),
|
|
|
|
exitCode(-1)
|
|
|
|
{}
|
2010-02-01 14:10:06 +00:00
|
|
|
|
|
|
|
void SynchronousProcessResponse::clear()
|
|
|
|
{
|
2013-05-19 17:37:30 +03:00
|
|
|
result = StartFailed;
|
2010-02-01 14:10:06 +00:00
|
|
|
exitCode = -1;
|
|
|
|
stdOut.clear();
|
|
|
|
stdErr.clear();
|
|
|
|
}
|
|
|
|
|
2013-05-19 17:37:30 +03:00
|
|
|
QTCREATOR_UTILS_EXPORT QDebug operator<<(QDebug str, const SynchronousProcessResponse & r)
|
2010-02-01 14:10:06 +00:00
|
|
|
{
|
|
|
|
QDebug nsp = str.nospace();
|
2013-05-19 17:37:30 +03:00
|
|
|
|
2010-02-01 14:10:06 +00:00
|
|
|
nsp << "SynchronousProcessResponse: result=" << r.result << " ex=" << r.exitCode << '\n'
|
|
|
|
<< r.stdOut.size() << " bytes stdout, stderr=" << r.stdErr << '\n';
|
|
|
|
return str;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Data for one channel buffer (stderr/stdout)
|
|
|
|
struct ChannelBuffer {
|
|
|
|
ChannelBuffer();
|
|
|
|
void clearForRun();
|
|
|
|
QByteArray linesRead();
|
|
|
|
|
|
|
|
QByteArray data;
|
|
|
|
bool firstData;
|
|
|
|
bool bufferedSignalsEnabled;
|
|
|
|
bool firstBuffer;
|
2013-05-19 17:37:30 +03:00
|
|
|
int bufferPos;
|
2010-02-01 14:10:06 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
ChannelBuffer::ChannelBuffer() :
|
|
|
|
firstData(true),
|
|
|
|
bufferedSignalsEnabled(false),
|
|
|
|
firstBuffer(true),
|
|
|
|
bufferPos(0)
|
2013-05-19 17:37:30 +03:00
|
|
|
{}
|
2010-02-01 14:10:06 +00:00
|
|
|
|
|
|
|
void ChannelBuffer::clearForRun()
|
|
|
|
{
|
2013-05-19 17:37:30 +03:00
|
|
|
firstData = true;
|
2010-02-01 14:10:06 +00:00
|
|
|
firstBuffer = true;
|
2013-05-19 17:37:30 +03:00
|
|
|
bufferPos = 0;
|
2010-02-01 14:10:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Check for complete lines read from the device and return them, moving the
|
|
|
|
* buffer position. This is based on the assumption that '\n' is the new line
|
|
|
|
* marker in any sane codec. */
|
|
|
|
QByteArray ChannelBuffer::linesRead()
|
|
|
|
{
|
|
|
|
// Any new lines?
|
|
|
|
const int lastLineIndex = data.lastIndexOf('\n');
|
2013-05-19 17:37:30 +03:00
|
|
|
|
|
|
|
if (lastLineIndex == -1 || lastLineIndex <= bufferPos) {
|
2010-02-01 14:10:06 +00:00
|
|
|
return QByteArray();
|
2013-05-19 17:37:30 +03:00
|
|
|
}
|
2010-02-01 14:10:06 +00:00
|
|
|
const int nextBufferPos = lastLineIndex + 1;
|
2013-05-19 17:37:30 +03:00
|
|
|
const QByteArray lines = data.mid(bufferPos, nextBufferPos - bufferPos);
|
2010-02-01 14:10:06 +00:00
|
|
|
bufferPos = nextBufferPos;
|
|
|
|
return lines;
|
|
|
|
}
|
|
|
|
|
|
|
|
// ----------- SynchronousProcessPrivate
|
|
|
|
struct SynchronousProcessPrivate {
|
|
|
|
SynchronousProcessPrivate();
|
2013-05-19 17:37:30 +03:00
|
|
|
void clearForRun();
|
2010-02-01 14:10:06 +00:00
|
|
|
|
|
|
|
QTextCodec *m_stdOutCodec;
|
2013-05-19 17:37:30 +03:00
|
|
|
QProcess m_process;
|
|
|
|
QTimer m_timer;
|
2010-02-01 14:10:06 +00:00
|
|
|
QEventLoop m_eventLoop;
|
|
|
|
SynchronousProcessResponse m_result;
|
2013-05-19 17:37:30 +03:00
|
|
|
int m_hangTimerCount;
|
|
|
|
int m_maxHangTimerCount;
|
2010-02-01 14:10:06 +00:00
|
|
|
bool m_startFailure;
|
|
|
|
|
|
|
|
ChannelBuffer m_stdOut;
|
|
|
|
ChannelBuffer m_stdErr;
|
|
|
|
};
|
|
|
|
|
|
|
|
SynchronousProcessPrivate::SynchronousProcessPrivate() :
|
|
|
|
m_stdOutCodec(0),
|
|
|
|
m_hangTimerCount(0),
|
|
|
|
m_maxHangTimerCount(defaultMaxHangTimerCount),
|
|
|
|
m_startFailure(false)
|
2013-05-19 17:37:30 +03:00
|
|
|
{}
|
2010-02-01 14:10:06 +00:00
|
|
|
|
|
|
|
void SynchronousProcessPrivate::clearForRun()
|
|
|
|
{
|
|
|
|
m_hangTimerCount = 0;
|
|
|
|
m_stdOut.clearForRun();
|
|
|
|
m_stdErr.clearForRun();
|
|
|
|
m_result.clear();
|
|
|
|
m_startFailure = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// ----------- SynchronousProcess
|
|
|
|
SynchronousProcess::SynchronousProcess() :
|
|
|
|
m_d(new SynchronousProcessPrivate)
|
|
|
|
{
|
|
|
|
m_d->m_timer.setInterval(1000);
|
|
|
|
connect(&m_d->m_timer, SIGNAL(timeout()), this, SLOT(slotTimeout()));
|
2013-05-19 17:37:30 +03:00
|
|
|
connect(&m_d->m_process, SIGNAL(finished(int, QProcess::ExitStatus)), this, SLOT(finished(int, QProcess::ExitStatus)));
|
2010-02-01 14:10:06 +00:00
|
|
|
connect(&m_d->m_process, SIGNAL(error(QProcess::ProcessError)), this, SLOT(error(QProcess::ProcessError)));
|
|
|
|
connect(&m_d->m_process, SIGNAL(readyReadStandardOutput()),
|
|
|
|
this, SLOT(stdOutReady()));
|
|
|
|
connect(&m_d->m_process, SIGNAL(readyReadStandardError()),
|
|
|
|
this, SLOT(stdErrReady()));
|
|
|
|
}
|
|
|
|
|
|
|
|
SynchronousProcess::~SynchronousProcess()
|
|
|
|
{
|
|
|
|
delete m_d;
|
|
|
|
}
|
|
|
|
|
|
|
|
void SynchronousProcess::setTimeout(int timeoutMS)
|
|
|
|
{
|
|
|
|
if (timeoutMS >= 0) {
|
|
|
|
m_d->m_maxHangTimerCount = qMax(2, timeoutMS / 1000);
|
|
|
|
} else {
|
|
|
|
m_d->m_maxHangTimerCount = INT_MAX;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
int SynchronousProcess::timeout() const
|
|
|
|
{
|
|
|
|
return m_d->m_maxHangTimerCount == INT_MAX ? -1 : 1000 * m_d->m_maxHangTimerCount;
|
|
|
|
}
|
|
|
|
|
|
|
|
void SynchronousProcess::setStdOutCodec(QTextCodec *c)
|
|
|
|
{
|
|
|
|
m_d->m_stdOutCodec = c;
|
|
|
|
}
|
|
|
|
|
|
|
|
QTextCodec *SynchronousProcess::stdOutCodec() const
|
|
|
|
{
|
|
|
|
return m_d->m_stdOutCodec;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool SynchronousProcess::stdOutBufferedSignalsEnabled() const
|
|
|
|
{
|
|
|
|
return m_d->m_stdOut.bufferedSignalsEnabled;
|
|
|
|
}
|
|
|
|
|
|
|
|
void SynchronousProcess::setStdOutBufferedSignalsEnabled(bool v)
|
|
|
|
{
|
|
|
|
m_d->m_stdOut.bufferedSignalsEnabled = v;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool SynchronousProcess::stdErrBufferedSignalsEnabled() const
|
|
|
|
{
|
|
|
|
return m_d->m_stdErr.bufferedSignalsEnabled;
|
|
|
|
}
|
|
|
|
|
|
|
|
void SynchronousProcess::setStdErrBufferedSignalsEnabled(bool v)
|
|
|
|
{
|
|
|
|
m_d->m_stdErr.bufferedSignalsEnabled = v;
|
|
|
|
}
|
|
|
|
|
|
|
|
QStringList SynchronousProcess::environment() const
|
|
|
|
{
|
|
|
|
return m_d->m_process.environment();
|
|
|
|
}
|
|
|
|
|
|
|
|
void SynchronousProcess::setEnvironment(const QStringList &e)
|
|
|
|
{
|
|
|
|
m_d->m_process.setEnvironment(e);
|
|
|
|
}
|
|
|
|
|
|
|
|
void SynchronousProcess::setWorkingDirectory(const QString &workingDirectory)
|
|
|
|
{
|
|
|
|
m_d->m_process.setWorkingDirectory(workingDirectory);
|
|
|
|
}
|
|
|
|
|
|
|
|
QString SynchronousProcess::workingDirectory() const
|
|
|
|
{
|
|
|
|
return m_d->m_process.workingDirectory();
|
|
|
|
}
|
|
|
|
|
2013-05-19 17:37:30 +03:00
|
|
|
QProcess::ProcessChannelMode SynchronousProcess::processChannelMode() const
|
2010-02-01 14:10:06 +00:00
|
|
|
{
|
|
|
|
return m_d->m_process.processChannelMode();
|
|
|
|
}
|
|
|
|
|
|
|
|
void SynchronousProcess::setProcessChannelMode(QProcess::ProcessChannelMode m)
|
|
|
|
{
|
|
|
|
m_d->m_process.setProcessChannelMode(m);
|
|
|
|
}
|
|
|
|
|
|
|
|
SynchronousProcessResponse SynchronousProcess::run(const QString &binary,
|
2013-05-19 17:37:30 +03:00
|
|
|
const QStringList &args)
|
2010-02-01 14:10:06 +00:00
|
|
|
{
|
2013-05-19 17:37:30 +03:00
|
|
|
if (debug) {
|
2010-02-01 14:10:06 +00:00
|
|
|
qDebug() << '>' << Q_FUNC_INFO << binary << args;
|
2013-05-19 17:37:30 +03:00
|
|
|
}
|
2010-02-01 14:10:06 +00:00
|
|
|
|
|
|
|
m_d->clearForRun();
|
|
|
|
|
|
|
|
// On Windows, start failure is triggered immediately if the
|
|
|
|
// executable cannot be found in the path. Do not start the
|
|
|
|
// event loop in that case.
|
|
|
|
m_d->m_process.start(binary, args, QIODevice::ReadOnly);
|
|
|
|
if (!m_d->m_startFailure) {
|
|
|
|
m_d->m_timer.start();
|
|
|
|
QApplication::setOverrideCursor(Qt::WaitCursor);
|
|
|
|
m_d->m_eventLoop.exec(QEventLoop::ExcludeUserInputEvents);
|
|
|
|
if (m_d->m_result.result == SynchronousProcessResponse::Finished || m_d->m_result.result == SynchronousProcessResponse::FinishedError) {
|
|
|
|
processStdOut(false);
|
|
|
|
processStdErr(false);
|
|
|
|
}
|
|
|
|
|
|
|
|
m_d->m_result.stdOut = convertStdOut(m_d->m_stdOut.data);
|
|
|
|
m_d->m_result.stdErr = convertStdErr(m_d->m_stdErr.data);
|
|
|
|
|
|
|
|
m_d->m_timer.stop();
|
|
|
|
QApplication::restoreOverrideCursor();
|
|
|
|
}
|
|
|
|
|
2013-05-19 17:37:30 +03:00
|
|
|
if (debug) {
|
2010-02-01 14:10:06 +00:00
|
|
|
qDebug() << '<' << Q_FUNC_INFO << binary << m_d->m_result;
|
2013-05-19 17:37:30 +03:00
|
|
|
}
|
|
|
|
return m_d->m_result;
|
2010-02-01 14:10:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void SynchronousProcess::slotTimeout()
|
|
|
|
{
|
|
|
|
if (++m_d->m_hangTimerCount > m_d->m_maxHangTimerCount) {
|
2013-05-19 17:37:30 +03:00
|
|
|
if (debug) {
|
2010-02-01 14:10:06 +00:00
|
|
|
qDebug() << Q_FUNC_INFO << "HANG detected, killing";
|
2013-05-19 17:37:30 +03:00
|
|
|
}
|
2010-02-01 14:10:06 +00:00
|
|
|
m_d->m_process.kill();
|
|
|
|
m_d->m_result.result = SynchronousProcessResponse::Hang;
|
|
|
|
} else {
|
2013-05-19 17:37:30 +03:00
|
|
|
if (debug) {
|
2010-02-01 14:10:06 +00:00
|
|
|
qDebug() << Q_FUNC_INFO << m_d->m_hangTimerCount;
|
2013-05-19 17:37:30 +03:00
|
|
|
}
|
2010-02-01 14:10:06 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void SynchronousProcess::finished(int exitCode, QProcess::ExitStatus e)
|
|
|
|
{
|
2013-05-19 17:37:30 +03:00
|
|
|
if (debug) {
|
2010-02-01 14:10:06 +00:00
|
|
|
qDebug() << Q_FUNC_INFO << exitCode << e;
|
2013-05-19 17:37:30 +03:00
|
|
|
}
|
2010-02-01 14:10:06 +00:00
|
|
|
m_d->m_hangTimerCount = 0;
|
|
|
|
switch (e) {
|
|
|
|
case QProcess::NormalExit:
|
2013-05-19 17:37:30 +03:00
|
|
|
m_d->m_result.result = exitCode ? SynchronousProcessResponse::FinishedError : SynchronousProcessResponse::Finished;
|
2010-02-01 14:10:06 +00:00
|
|
|
m_d->m_result.exitCode = exitCode;
|
|
|
|
break;
|
|
|
|
case QProcess::CrashExit:
|
|
|
|
// Was hang detected before and killed?
|
2013-05-19 17:37:30 +03:00
|
|
|
if (m_d->m_result.result != SynchronousProcessResponse::Hang) {
|
2010-02-01 14:10:06 +00:00
|
|
|
m_d->m_result.result = SynchronousProcessResponse::TerminatedAbnormally;
|
2013-05-19 17:37:30 +03:00
|
|
|
}
|
2010-02-01 14:10:06 +00:00
|
|
|
m_d->m_result.exitCode = -1;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
m_d->m_eventLoop.quit();
|
|
|
|
}
|
|
|
|
|
|
|
|
void SynchronousProcess::error(QProcess::ProcessError e)
|
|
|
|
{
|
|
|
|
m_d->m_hangTimerCount = 0;
|
2013-05-19 17:37:30 +03:00
|
|
|
if (debug) {
|
2010-02-01 14:10:06 +00:00
|
|
|
qDebug() << Q_FUNC_INFO << e;
|
2013-05-19 17:37:30 +03:00
|
|
|
}
|
2010-02-01 14:10:06 +00:00
|
|
|
// Was hang detected before and killed?
|
2013-05-19 17:37:30 +03:00
|
|
|
if (m_d->m_result.result != SynchronousProcessResponse::Hang) {
|
2010-02-01 14:10:06 +00:00
|
|
|
m_d->m_result.result = SynchronousProcessResponse::StartFailed;
|
2013-05-19 17:37:30 +03:00
|
|
|
}
|
2010-02-01 14:10:06 +00:00
|
|
|
m_d->m_startFailure = true;
|
|
|
|
m_d->m_eventLoop.quit();
|
|
|
|
}
|
|
|
|
|
|
|
|
void SynchronousProcess::stdOutReady()
|
|
|
|
{
|
|
|
|
m_d->m_hangTimerCount = 0;
|
|
|
|
processStdOut(true);
|
|
|
|
}
|
|
|
|
|
|
|
|
void SynchronousProcess::stdErrReady()
|
|
|
|
{
|
|
|
|
m_d->m_hangTimerCount = 0;
|
|
|
|
processStdErr(true);
|
|
|
|
}
|
|
|
|
|
|
|
|
QString SynchronousProcess::convertStdErr(const QByteArray &ba)
|
|
|
|
{
|
|
|
|
return QString::fromLocal8Bit(ba.constData(), ba.size()).remove(QLatin1Char('\r'));
|
|
|
|
}
|
|
|
|
|
|
|
|
QString SynchronousProcess::convertStdOut(const QByteArray &ba) const
|
|
|
|
{
|
|
|
|
QString stdOut = m_d->m_stdOutCodec ? m_d->m_stdOutCodec->toUnicode(ba) : QString::fromLocal8Bit(ba.constData(), ba.size());
|
2013-05-19 17:37:30 +03:00
|
|
|
|
2010-02-01 14:10:06 +00:00
|
|
|
return stdOut.remove(QLatin1Char('\r'));
|
|
|
|
}
|
|
|
|
|
|
|
|
void SynchronousProcess::processStdOut(bool emitSignals)
|
|
|
|
{
|
|
|
|
// Handle binary data
|
|
|
|
const QByteArray ba = m_d->m_process.readAllStandardOutput();
|
2013-05-19 17:37:30 +03:00
|
|
|
|
|
|
|
if (debug > 1) {
|
2010-02-01 14:10:06 +00:00
|
|
|
qDebug() << Q_FUNC_INFO << emitSignals << ba;
|
2013-05-19 17:37:30 +03:00
|
|
|
}
|
2010-02-01 14:10:06 +00:00
|
|
|
if (!ba.isEmpty()) {
|
|
|
|
m_d->m_stdOut.data += ba;
|
|
|
|
if (emitSignals) {
|
|
|
|
// Emit binary signals
|
|
|
|
emit stdOut(ba, m_d->m_stdOut.firstData);
|
|
|
|
m_d->m_stdOut.firstData = false;
|
|
|
|
// Buffered. Emit complete lines?
|
|
|
|
if (m_d->m_stdOut.bufferedSignalsEnabled) {
|
|
|
|
const QByteArray lines = m_d->m_stdOut.linesRead();
|
|
|
|
if (!lines.isEmpty()) {
|
|
|
|
emit stdOutBuffered(convertStdOut(lines), m_d->m_stdOut.firstBuffer);
|
|
|
|
m_d->m_stdOut.firstBuffer = false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void SynchronousProcess::processStdErr(bool emitSignals)
|
|
|
|
{
|
|
|
|
// Handle binary data
|
|
|
|
const QByteArray ba = m_d->m_process.readAllStandardError();
|
2013-05-19 17:37:30 +03:00
|
|
|
|
|
|
|
if (debug > 1) {
|
2010-02-01 14:10:06 +00:00
|
|
|
qDebug() << Q_FUNC_INFO << emitSignals << ba;
|
2013-05-19 17:37:30 +03:00
|
|
|
}
|
2010-02-01 14:10:06 +00:00
|
|
|
if (!ba.isEmpty()) {
|
|
|
|
m_d->m_stdErr.data += ba;
|
|
|
|
if (emitSignals) {
|
|
|
|
// Emit binary signals
|
|
|
|
emit stdErr(ba, m_d->m_stdErr.firstData);
|
|
|
|
m_d->m_stdErr.firstData = false;
|
|
|
|
if (m_d->m_stdErr.bufferedSignalsEnabled) {
|
|
|
|
// Buffered. Emit complete lines?
|
|
|
|
const QByteArray lines = m_d->m_stdErr.linesRead();
|
|
|
|
if (!lines.isEmpty()) {
|
|
|
|
emit stdErrBuffered(convertStdErr(lines), m_d->m_stdErr.firstBuffer);
|
|
|
|
m_d->m_stdErr.firstBuffer = false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Path utilities
|
|
|
|
|
|
|
|
enum OS_Type { OS_Mac, OS_Windows, OS_Unix };
|
|
|
|
|
|
|
|
#ifdef Q_OS_WIN
|
|
|
|
static const OS_Type pathOS = OS_Windows;
|
|
|
|
#else
|
|
|
|
# ifdef Q_OS_MAC
|
|
|
|
static const OS_Type pathOS = OS_Mac;
|
|
|
|
# else
|
|
|
|
static const OS_Type pathOS = OS_Unix;
|
|
|
|
# endif
|
|
|
|
#endif
|
|
|
|
|
|
|
|
// Locate a binary in a directory, applying all kinds of
|
|
|
|
// extensions the operating system supports.
|
|
|
|
static QString checkBinary(const QDir &dir, const QString &binary)
|
|
|
|
{
|
|
|
|
// naive UNIX approach
|
|
|
|
const QFileInfo info(dir.filePath(binary));
|
2013-05-19 17:37:30 +03:00
|
|
|
|
|
|
|
if (info.isFile() && info.isExecutable()) {
|
2010-02-01 14:10:06 +00:00
|
|
|
return info.absoluteFilePath();
|
2013-05-19 17:37:30 +03:00
|
|
|
}
|
2010-02-01 14:10:06 +00:00
|
|
|
|
|
|
|
// Does the OS have some weird extension concept or does the
|
|
|
|
// binary have a 3 letter extension?
|
2013-05-19 17:37:30 +03:00
|
|
|
if (pathOS == OS_Unix) {
|
2010-02-01 14:10:06 +00:00
|
|
|
return QString();
|
2013-05-19 17:37:30 +03:00
|
|
|
}
|
2010-02-01 14:10:06 +00:00
|
|
|
const int dotIndex = binary.lastIndexOf(QLatin1Char('.'));
|
2013-05-19 17:37:30 +03:00
|
|
|
if (dotIndex != -1 && dotIndex == binary.size() - 4) {
|
|
|
|
return QString();
|
|
|
|
}
|
2010-02-01 14:10:06 +00:00
|
|
|
|
|
|
|
switch (pathOS) {
|
|
|
|
case OS_Unix:
|
|
|
|
break;
|
2013-05-19 17:37:30 +03:00
|
|
|
case OS_Windows:
|
|
|
|
{
|
|
|
|
static const char *windowsExtensions[] = { ".cmd", ".bat", ".exe", ".com" };
|
|
|
|
// Check the Windows extensions using the order
|
|
|
|
const int windowsExtensionCount = sizeof(windowsExtensions) / sizeof(const char *);
|
|
|
|
for (int e = 0; e < windowsExtensionCount; e++) {
|
|
|
|
const QFileInfo windowsBinary(dir.filePath(binary + QLatin1String(windowsExtensions[e])));
|
|
|
|
if (windowsBinary.isFile() && windowsBinary.isExecutable()) {
|
|
|
|
return windowsBinary.absoluteFilePath();
|
2010-02-01 14:10:06 +00:00
|
|
|
}
|
|
|
|
}
|
2013-05-19 17:37:30 +03:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
case OS_Mac:
|
|
|
|
{
|
|
|
|
// Check for Mac app folders
|
|
|
|
const QFileInfo appFolder(dir.filePath(binary + QLatin1String(".app")));
|
|
|
|
if (appFolder.isDir()) {
|
|
|
|
QString macBinaryPath = appFolder.absoluteFilePath();
|
|
|
|
macBinaryPath += QLatin1String("/Contents/MacOS/");
|
|
|
|
macBinaryPath += binary;
|
|
|
|
const QFileInfo macBinary(macBinaryPath);
|
|
|
|
if (macBinary.isFile() && macBinary.isExecutable()) {
|
|
|
|
return macBinary.absoluteFilePath();
|
2010-02-01 14:10:06 +00:00
|
|
|
}
|
|
|
|
}
|
2013-05-19 17:37:30 +03:00
|
|
|
}
|
|
|
|
break;
|
2010-02-01 14:10:06 +00:00
|
|
|
}
|
|
|
|
return QString();
|
|
|
|
}
|
|
|
|
|
|
|
|
QString SynchronousProcess::locateBinary(const QString &path, const QString &binary)
|
|
|
|
{
|
|
|
|
// Absolute file?
|
|
|
|
const QFileInfo absInfo(binary);
|
2013-05-19 17:37:30 +03:00
|
|
|
|
|
|
|
if (absInfo.isAbsolute()) {
|
2010-02-01 14:10:06 +00:00
|
|
|
return checkBinary(absInfo.dir(), absInfo.fileName());
|
2013-05-19 17:37:30 +03:00
|
|
|
}
|
2010-02-01 14:10:06 +00:00
|
|
|
|
|
|
|
// Windows finds binaries in the current directory
|
|
|
|
if (pathOS == OS_Windows) {
|
|
|
|
const QString currentDirBinary = checkBinary(QDir::current(), binary);
|
2013-05-19 17:37:30 +03:00
|
|
|
if (!currentDirBinary.isEmpty()) {
|
2010-02-01 14:10:06 +00:00
|
|
|
return currentDirBinary;
|
2013-05-19 17:37:30 +03:00
|
|
|
}
|
2010-02-01 14:10:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
const QStringList paths = path.split(pathSeparator());
|
2013-05-19 17:37:30 +03:00
|
|
|
if (paths.empty()) {
|
2010-02-01 14:10:06 +00:00
|
|
|
return QString();
|
2013-05-19 17:37:30 +03:00
|
|
|
}
|
2010-02-01 14:10:06 +00:00
|
|
|
const QStringList::const_iterator cend = paths.constEnd();
|
|
|
|
for (QStringList::const_iterator it = paths.constBegin(); it != cend; ++it) {
|
|
|
|
const QDir dir(*it);
|
|
|
|
const QString rc = checkBinary(dir, binary);
|
2013-05-19 17:37:30 +03:00
|
|
|
if (!rc.isEmpty()) {
|
2010-02-01 14:10:06 +00:00
|
|
|
return rc;
|
2013-05-19 17:37:30 +03:00
|
|
|
}
|
2010-02-01 14:10:06 +00:00
|
|
|
}
|
|
|
|
return QString();
|
|
|
|
}
|
|
|
|
|
|
|
|
QString SynchronousProcess::locateBinary(const QString &binary)
|
|
|
|
{
|
|
|
|
const QByteArray path = qgetenv("PATH");
|
2013-05-19 17:37:30 +03:00
|
|
|
|
2010-02-01 14:10:06 +00:00
|
|
|
return locateBinary(QString::fromLocal8Bit(path), binary);
|
|
|
|
}
|
|
|
|
|
|
|
|
QChar SynchronousProcess::pathSeparator()
|
|
|
|
{
|
2013-05-19 17:37:30 +03:00
|
|
|
if (pathOS == OS_Windows) {
|
2010-02-01 14:10:06 +00:00
|
|
|
return QLatin1Char(';');
|
2013-05-19 17:37:30 +03:00
|
|
|
}
|
2010-02-01 14:10:06 +00:00
|
|
|
return QLatin1Char(':');
|
|
|
|
}
|
|
|
|
} // namespace Utils
|