diff options
author | Oswald Buddenhagen <oswald.buddenhagen@nokia.com> | 2009-01-29 20:11:02 +0100 |
---|---|---|
committer | Oswald Buddenhagen <oswald.buddenhagen@nokia.com> | 2009-02-27 18:04:36 +0100 |
commit | 1c2f0f64c154f7d6c0e3d455010b85d78391002e (patch) | |
tree | 5829977cc5922453d40a4b1d9b033aeff5eae446 /src/libs/utils | |
parent | df8ffb09fdd29cbb7ae952e5016a533cde145365 (diff) | |
download | qt-creator-1c2f0f64c154f7d6c0e3d455010b85d78391002e.tar.gz |
move abstractprocess and consoleprocess to libs/utils/
will use it also in the debugger, and pulling it in from projectexplorer
just seems wrong.
Diffstat (limited to 'src/libs/utils')
-rw-r--r-- | src/libs/utils/abstractprocess.h | 71 | ||||
-rw-r--r-- | src/libs/utils/consoleprocess.h | 95 | ||||
-rw-r--r-- | src/libs/utils/consoleprocess_unix.cpp | 106 | ||||
-rw-r--r-- | src/libs/utils/consoleprocess_win.cpp | 236 | ||||
-rw-r--r-- | src/libs/utils/utils.pro | 5 |
5 files changed, 513 insertions, 0 deletions
diff --git a/src/libs/utils/abstractprocess.h b/src/libs/utils/abstractprocess.h new file mode 100644 index 0000000000..72049c36c0 --- /dev/null +++ b/src/libs/utils/abstractprocess.h @@ -0,0 +1,71 @@ +/************************************************************************** +** +** This file is part of Qt Creator +** +** Copyright (c) 2009 Nokia Corporation and/or its subsidiary(-ies). +** +** Contact: Qt Software Information (qt-info@nokia.com) +** +** Commercial Usage +** +** Licensees holding valid Qt Commercial licenses may use this file in +** accordance with the Qt Commercial License Agreement provided with the +** Software or, alternatively, in accordance with the terms contained in +** a written agreement between you and Nokia. +** +** GNU Lesser General Public License Usage +** +** Alternatively, this file may be used under the terms of the GNU Lesser +** General Public License version 2.1 as published by the Free Software +** Foundation and appearing in the file LICENSE.LGPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU Lesser General Public License version 2.1 requirements +** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. +** +** If you are unsure which license is appropriate for your use, please +** contact the sales department at qt-sales@nokia.com. +** +**************************************************************************/ + +#ifndef ABSTRACTPROCESS_H +#define ABSTRACTPROCESS_H + +#include "utils_global.h" + +#include <QtCore/QStringList> + +namespace Core { +namespace Utils { + +class QWORKBENCH_UTILS_EXPORT AbstractProcess +{ +public: + AbstractProcess() {} + virtual ~AbstractProcess() {} + + QString workingDirectory() const { return m_workingDir; } + void setWorkingDirectory(const QString &dir) { m_workingDir = dir; } + + QStringList environment() const { return m_environment; } + void setEnvironment(const QStringList &env) { m_environment = env; } + + virtual bool start(const QString &program, const QStringList &args) = 0; + virtual void stop() = 0; + + virtual bool isRunning() const = 0; + virtual qint64 applicationPID() const = 0; + virtual int exitCode() const = 0; + +//signals: + virtual void processError(const QString &error) = 0; + +private: + QString m_workingDir; + QStringList m_environment; +}; + +} //namespace Utils +} //namespace Core + +#endif // ABSTRACTPROCESS_H + diff --git a/src/libs/utils/consoleprocess.h b/src/libs/utils/consoleprocess.h new file mode 100644 index 0000000000..6dfaaa0bb5 --- /dev/null +++ b/src/libs/utils/consoleprocess.h @@ -0,0 +1,95 @@ +/************************************************************************** +** +** This file is part of Qt Creator +** +** Copyright (c) 2009 Nokia Corporation and/or its subsidiary(-ies). +** +** Contact: Qt Software Information (qt-info@nokia.com) +** +** Commercial Usage +** +** Licensees holding valid Qt Commercial licenses may use this file in +** accordance with the Qt Commercial License Agreement provided with the +** Software or, alternatively, in accordance with the terms contained in +** a written agreement between you and Nokia. +** +** GNU Lesser General Public License Usage +** +** Alternatively, this file may be used under the terms of the GNU Lesser +** General Public License version 2.1 as published by the Free Software +** Foundation and appearing in the file LICENSE.LGPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU Lesser General Public License version 2.1 requirements +** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. +** +** If you are unsure which license is appropriate for your use, please +** contact the sales department at qt-sales@nokia.com. +** +**************************************************************************/ + +#ifndef CONSOLEPROCESS_H +#define CONSOLEPROCESS_H + +#include "abstractprocess.h" + +#include <QtCore/QObject> +#include <QtCore/QString> +#include <QtCore/QStringList> +#include <QtCore/QProcess> + +#ifdef Q_OS_WIN +#include <windows.h> +class QWinEventNotifier; +#endif + +namespace Core { +namespace Utils { + +class QWORKBENCH_UTILS_EXPORT ConsoleProcess : public QObject, public AbstractProcess +{ + Q_OBJECT + +public: + ConsoleProcess(QObject *parent); + ~ConsoleProcess(); + + bool start(const QString &program, const QStringList &args); + void stop(); + + bool isRunning() const; + qint64 applicationPID() const; + int exitCode() const; + +signals: + void processError(const QString &error); + void processStarted(); + void processStopped(); + +private: + bool m_isRunning; + +#ifdef Q_OS_WIN +public: + static QString createCommandline(const QString &program, + const QStringList &args); + static QByteArray createEnvironment(const QStringList &env); + +private slots: + void processDied(); + +private: + PROCESS_INFORMATION *m_pid; + QWinEventNotifier *processFinishedNotifier; +#else +private: + QProcess *m_process; +private slots: + void processFinished(int, QProcess::ExitStatus); +#endif + +}; + +} //namespace Utils +} //namespace Core + +#endif diff --git a/src/libs/utils/consoleprocess_unix.cpp b/src/libs/utils/consoleprocess_unix.cpp new file mode 100644 index 0000000000..874106cf6c --- /dev/null +++ b/src/libs/utils/consoleprocess_unix.cpp @@ -0,0 +1,106 @@ +/************************************************************************** +** +** This file is part of Qt Creator +** +** Copyright (c) 2009 Nokia Corporation and/or its subsidiary(-ies). +** +** Contact: Qt Software Information (qt-info@nokia.com) +** +** Commercial Usage +** +** Licensees holding valid Qt Commercial licenses may use this file in +** accordance with the Qt Commercial License Agreement provided with the +** Software or, alternatively, in accordance with the terms contained in +** a written agreement between you and Nokia. +** +** GNU Lesser General Public License Usage +** +** Alternatively, this file may be used under the terms of the GNU Lesser +** General Public License version 2.1 as published by the Free Software +** Foundation and appearing in the file LICENSE.LGPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU Lesser General Public License version 2.1 requirements +** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. +** +** If you are unsure which license is appropriate for your use, please +** contact the sales department at qt-sales@nokia.com. +** +**************************************************************************/ + +#include "consoleprocess.h" + +using namespace Core::Utils; + +ConsoleProcess::ConsoleProcess(QObject *parent) + : QObject(parent) +{ + m_isRunning = false; + m_process = new QProcess(this); +} + +ConsoleProcess::~ConsoleProcess() +{ +} + +static QString shellEscape(const QString &in) +{ + QString out = in; + out.replace('\'', "'\''"); + out.prepend('\''); + out.append('\''); + return out; +} + +bool ConsoleProcess::start(const QString &program, const QStringList &args) +{ + if (m_process->state() != QProcess::NotRunning) + return false; + QString shellArgs; + shellArgs += QLatin1String("cd "); + shellArgs += shellEscape(workingDirectory()); + shellArgs += QLatin1Char(';'); + shellArgs += shellEscape(program); + foreach (const QString &arg, args) { + shellArgs += QLatin1Char(' '); + shellArgs += shellEscape(arg); + } + shellArgs += QLatin1String("; echo; echo \"Press enter to close this window\"; read DUMMY"); + + m_process->setEnvironment(environment()); + + connect(m_process, SIGNAL(finished(int, QProcess::ExitStatus)), + this, SLOT(processFinished(int, QProcess::ExitStatus))); + + m_process->start(QLatin1String("xterm"), QStringList() << QLatin1String("-e") << "/bin/sh" << "-c" << shellArgs); + if (!m_process->waitForStarted()) + return false; + emit processStarted(); + return true; +} + +void ConsoleProcess::processFinished(int, QProcess::ExitStatus) +{ + emit processStopped(); +} + +bool ConsoleProcess::isRunning() const +{ + return m_process->state() != QProcess::NotRunning; +} + +void ConsoleProcess::stop() +{ + m_process->terminate(); + m_process->waitForFinished(); +} + +qint64 ConsoleProcess::applicationPID() const +{ + return m_process->pid(); +} + +int ConsoleProcess::exitCode() const +{ + return m_process->exitCode(); +} + diff --git a/src/libs/utils/consoleprocess_win.cpp b/src/libs/utils/consoleprocess_win.cpp new file mode 100644 index 0000000000..94409bee1b --- /dev/null +++ b/src/libs/utils/consoleprocess_win.cpp @@ -0,0 +1,236 @@ +/************************************************************************** +** +** This file is part of Qt Creator +** +** Copyright (c) 2009 Nokia Corporation and/or its subsidiary(-ies). +** +** Contact: Qt Software Information (qt-info@nokia.com) +** +** Commercial Usage +** +** Licensees holding valid Qt Commercial licenses may use this file in +** accordance with the Qt Commercial License Agreement provided with the +** Software or, alternatively, in accordance with the terms contained in +** a written agreement between you and Nokia. +** +** GNU Lesser General Public License Usage +** +** Alternatively, this file may be used under the terms of the GNU Lesser +** General Public License version 2.1 as published by the Free Software +** Foundation and appearing in the file LICENSE.LGPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU Lesser General Public License version 2.1 requirements +** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. +** +** If you are unsure which license is appropriate for your use, please +** contact the sales department at qt-sales@nokia.com. +** +**************************************************************************/ + +#include "consoleprocess.h" + +#include <QtCore/QDir> +#include <QtCore/private/qwineventnotifier_p.h> +#include <QtCore/QAbstractEventDispatcher> + +#include <Tlhelp32.h> + +using namespace Core::Utils; + +ConsoleProcess::ConsoleProcess(QObject *parent) + : QObject(parent) +{ + m_isRunning = false; + m_pid = 0; +} + +ConsoleProcess::~ConsoleProcess() +{ + stop(); +} + +void ConsoleProcess::stop() +{ + if (m_pid) + TerminateProcess(m_pid->hProcess, -1); + m_isRunning = false; +} + +bool ConsoleProcess::start(const QString &program, const QStringList &args) +{ + if (m_isRunning) + return false; + + STARTUPINFO si; + ZeroMemory(&si, sizeof(si)); + si.cb = sizeof(si); + + if (m_pid) { + CloseHandle(m_pid->hThread); + CloseHandle(m_pid->hProcess); + delete m_pid; + m_pid = 0; + } + m_pid = new PROCESS_INFORMATION; + ZeroMemory(m_pid, sizeof(PROCESS_INFORMATION)); + + QString cmdLine = QLatin1String("cmd /k ") + + createCommandline(program, args) + + QLatin1String(" & pause & exit"); + + bool success = CreateProcessW(0, (WCHAR*)cmdLine.utf16(), + 0, 0, TRUE, CREATE_NEW_CONSOLE | CREATE_UNICODE_ENVIRONMENT, + environment().isEmpty() ? 0 + : createEnvironment(environment()).data(), + workingDirectory().isEmpty() ? 0 + : (WCHAR*)QDir::convertSeparators(workingDirectory()).utf16(), + &si, m_pid); + + if (!success) { + emit processError(tr("The process could not be started!")); + return false; + } + + if (QAbstractEventDispatcher::instance(thread())) { + processFinishedNotifier = new QWinEventNotifier(m_pid->hProcess, this); + QObject::connect(processFinishedNotifier, SIGNAL(activated(HANDLE)), this, SLOT(processDied())); + processFinishedNotifier->setEnabled(true); + } + m_isRunning = true; + emit processStarted(); + return success; +} + +bool ConsoleProcess::isRunning() const +{ + return m_isRunning; +} + +void ConsoleProcess::processDied() +{ + if (processFinishedNotifier) { + processFinishedNotifier->setEnabled(false); + delete processFinishedNotifier; + processFinishedNotifier = 0; + } + delete m_pid; + m_pid = 0; + m_isRunning = false; + emit processStopped(); +} + +qint64 ConsoleProcess::applicationPID() const +{ + if (m_pid) { + HANDLE hProcList = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0); + PROCESSENTRY32 procEntry; + procEntry.dwSize = sizeof(PROCESSENTRY32); + DWORD procId = 0; + BOOL moreProc = Process32First(hProcList, &procEntry); + while (moreProc) { + if (procEntry.th32ParentProcessID == m_pid->dwProcessId) { + procId = procEntry.th32ProcessID; + break; + } + moreProc = Process32Next(hProcList, &procEntry); + } + + CloseHandle(hProcList); + return procId; + } + return 0; +} + +int ConsoleProcess::exitCode() const +{ + DWORD exitCode; + if (GetExitCodeProcess(m_pid->hProcess, &exitCode)) + return exitCode; + return -1; +} + +QByteArray ConsoleProcess::createEnvironment(const QStringList &env) +{ + QByteArray envlist; + if (!env.isEmpty()) { + QStringList envStrings = env; + int pos = 0; + // add PATH if necessary (for DLL loading) + if (envStrings.filter(QRegExp("^PATH=",Qt::CaseInsensitive)).isEmpty()) { + QByteArray path = qgetenv("PATH"); + if (!path.isEmpty()) + envStrings.prepend(QString(QLatin1String("PATH=%1")).arg(QString::fromLocal8Bit(path))); + } + // add systemroot if needed + if (envStrings.filter(QRegExp("^SystemRoot=",Qt::CaseInsensitive)).isEmpty()) { + QByteArray systemRoot = qgetenv("SystemRoot"); + if (!systemRoot.isEmpty()) + envStrings.prepend(QString(QLatin1String("SystemRoot=%1")).arg(QString::fromLocal8Bit(systemRoot))); + } +#ifdef UNICODE + if (!(QSysInfo::WindowsVersion & QSysInfo::WV_DOS_based)) { + for (QStringList::ConstIterator it = envStrings.constBegin(); it != envStrings.constEnd(); it++ ) { + QString tmp = *it; + uint tmpSize = sizeof(TCHAR) * (tmp.length()+1); + envlist.resize(envlist.size() + tmpSize); + memcpy(envlist.data()+pos, tmp.utf16(), tmpSize); + pos += tmpSize; + } + // add the 2 terminating 0 (actually 4, just to be on the safe side) + envlist.resize(envlist.size() + 4); + envlist[pos++] = 0; + envlist[pos++] = 0; + envlist[pos++] = 0; + envlist[pos++] = 0; + } else +#endif // UNICODE + { + for (QStringList::ConstIterator it = envStrings.constBegin(); it != envStrings.constEnd(); it++) { + QByteArray tmp = (*it).toLocal8Bit(); + uint tmpSize = tmp.length() + 1; + envlist.resize(envlist.size() + tmpSize); + memcpy(envlist.data()+pos, tmp.data(), tmpSize); + pos += tmpSize; + } + // add the terminating 0 (actually 2, just to be on the safe side) + envlist.resize(envlist.size() + 2); + envlist[pos++] = 0; + envlist[pos++] = 0; + } + } + return envlist; +} + +QString ConsoleProcess::createCommandline(const QString &program, const QStringList &args) +{ + QString programName = program; + if (!programName.startsWith(QLatin1Char('\"')) && !programName.endsWith(QLatin1Char('\"')) && programName.contains(" ")) + programName = "\"" + programName + "\""; + programName.replace("/", "\\"); + + QString cmdLine; + // add the prgram as the first arrg ... it works better + cmdLine = programName + " "; + for (int i = 0; i < args.size(); ++i) { + QString tmp = args.at(i); + // in the case of \" already being in the string the \ must also be escaped + tmp.replace( "\\\"", "\\\\\"" ); + // escape a single " because the arguments will be parsed + tmp.replace( "\"", "\\\"" ); + if (tmp.isEmpty() || tmp.contains(' ') || tmp.contains('\t')) { + // The argument must not end with a \ since this would be interpreted + // as escaping the quote -- rather put the \ behind the quote: e.g. + // rather use "foo"\ than "foo\" + QString endQuote("\""); + int i = tmp.length(); + while (i > 0 && tmp.at(i - 1) == '\\') { + --i; + endQuote += "\\"; + } + cmdLine += QString(" \"") + tmp.left(i) + endQuote; + } else { + cmdLine += ' ' + tmp; + } + } + return cmdLine; +} diff --git a/src/libs/utils/utils.pro b/src/libs/utils/utils.pro index d98ca1d889..74f25b0488 100644 --- a/src/libs/utils/utils.pro +++ b/src/libs/utils/utils.pro @@ -25,6 +25,9 @@ SOURCES += \ submiteditorwidget.cpp \ synchronousprocess.cpp +win32:SOURCES += consoleprocess_win.cpp +else:SOURCES += consoleprocess_unix.cpp + HEADERS += \ utils_global.h \ reloadpromptutils.h \ @@ -45,6 +48,8 @@ HEADERS += \ fancylineedit.h \ qtcolorbutton.h \ submiteditorwidget.h \ + abstractprocess.h \ + consoleprocess.h \ synchronousprocess.h FORMS += filewizardpage.ui \ |