<commit_before>/**************************************************************************
**
** This file is part of Qt Creator
**
** Copyright (c) 2011 Nokia Corporation and/or its subsidiary(-ies).
**
** Contact: Nokia Corporation (<EMAIL>)
**
** No Commercial Usage
**
** This file contains pre-release code and may not be distributed.
** You may use this file in accordance with the terms and conditions
** contained in the Technology Preview License Agreement accompanying
** this package.
**
** 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.
**
** In addition, as a special exception, Nokia gives you certain additional
** rights.  These rights are described in the Nokia Qt LGPL Exception
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
**
** If you have questions regarding the use of this file, please contact
** Nokia at <EMAIL>.
**
**************************************************************************/

#include "winguiprocess.h"
#include "consoleprocess.h"

#include <utils/qtcprocess.h>
#include <utils/winutils.h>

#include <QtCore/QDir>

using namespace ProjectExplorer::Internal;

WinGuiProcess::WinGuiProcess(QObject *parent)
    : QThread(parent)
{
    m_pid = 0;
    m_exitCode = 0;
}

WinGuiProcess::~WinGuiProcess()
{
    stop();
}

bool WinGuiProcess::start(const QString &program, const QString &args)
{
    m_program = program;
    m_args = args;

    if (!isRunning()) {
        QThread::start(QThread::NormalPriority);
        return true;
    }
    return false;
}

void WinGuiProcess::stop()
{
    if (m_pid)
        TerminateProcess(m_pid->hProcess, 1);
    wait();
}

bool WinGuiProcess::isRunning() const
{
    return QThread::isRunning();
}

bool WinGuiProcess::setupDebugInterface(HANDLE &bufferReadyEvent, HANDLE &dataReadyEvent, HANDLE &sharedFile, LPVOID &sharedMem)
{

    bufferReadyEvent = CreateEvent(NULL, FALSE, FALSE, L"DBWIN_BUFFER_READY");
    if (!bufferReadyEvent || GetLastError() == ERROR_ALREADY_EXISTS)
        return false;
    dataReadyEvent = CreateEvent(NULL, FALSE, FALSE, L"DBWIN_DATA_READY");
    if (!dataReadyEvent || GetLastError() == ERROR_ALREADY_EXISTS)
        return false;
    sharedFile = CreateFileMapping((HANDLE)-1, NULL, PAGE_READWRITE, 0, 4096, L"DBWIN_BUFFER");
    if (!sharedFile || GetLastError() == ERROR_ALREADY_EXISTS)
        return false;
    sharedMem = MapViewOfFile(sharedFile, FILE_MAP_READ, 0, 0,  512);
    if (!sharedMem)
        return false;
    return true;
}

void WinGuiProcess::run()
{
    if (m_pid)
        return;

    STARTUPINFO si;
    ZeroMemory(&si, sizeof(si));
    si.cb = sizeof(si);

    m_pid = new PROCESS_INFORMATION;
    ZeroMemory(m_pid, sizeof(PROCESS_INFORMATION));

    m_exitCode = 0;
    bool started = false;

    HANDLE bufferReadyEvent = NULL;
    HANDLE dataReadyEvent = NULL;
    HANDLE sharedFile = NULL;
    LPVOID sharedMem = 0;

    do {

        const bool dbgInterface = setupDebugInterface(bufferReadyEvent, dataReadyEvent, sharedFile, sharedMem);

        QString pcmd, pargs;
        QtcProcess::prepareCommand(m_program, m_args, &pcmd, &pargs, &m_environment, &m_workingDir);
        const QString cmdLine = createWinCommandline(pcmd, pargs);
        const QStringList env = m_environment.toStringList();
        started = CreateProcessW(0, (WCHAR*)cmdLine.utf16(),
                                      0, 0, TRUE, CREATE_UNICODE_ENVIRONMENT,
                                      env.isEmpty() ? 0
                                          : createWinEnvironment(fixWinEnvironment(env)).data(),
                                          workingDirectory().isEmpty() ? 0
                                              : (WCHAR*)QDir::convertSeparators(workingDirectory()).utf16(),
                                              &si, m_pid);

        if (!started) {
            emit processMessage(tr("The process could not be started: %1").
                                arg(Utils::winErrorMessage(GetLastError())), true);
            break;
        }

        if (!dbgInterface) {
            // Text is filtered in qmlengine.cpp
            emit receivedDebugOutput(Utils::AbstractProcess::msgWinCannotRetrieveDebuggingOutput(), true);<commit_msg>Local Application Run Control: emit finished() for non existent programs<commit_after>/**************************************************************************
**
** This file is part of Qt Creator
**
** Copyright (c) 2011 Nokia Corporation and/or its subsidiary(-ies).
**
** Contact: Nokia Corporation (<EMAIL>)
**
** No Commercial Usage
**
** This file contains pre-release code and may not be distributed.
** You may use this file in accordance with the terms and conditions
** contained in the Technology Preview License Agreement accompanying
** this package.
**
** 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.
**
** In addition, as a special exception, Nokia gives you certain additional
** rights.  These rights are described in the Nokia Qt LGPL Exception
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
**
** If you have questions regarding the use of this file, please contact
** Nokia at <EMAIL>.
**
**************************************************************************/

#include "winguiprocess.h"
#include "consoleprocess.h"

#include <utils/qtcprocess.h>
#include <utils/winutils.h>

#include <QtCore/QDir>

using namespace ProjectExplorer::Internal;

WinGuiProcess::WinGuiProcess(QObject *parent)
    : QThread(parent)
{
    m_pid = 0;
    m_exitCode = 0;
}

WinGuiProcess::~WinGuiProcess()
{
    stop();
}

bool WinGuiProcess::start(const QString &program, const QString &args)
{
    m_program = program;
    m_args = args;

    if (!isRunning()) {
        QThread::start(QThread::NormalPriority);
        return true;
    }
    return false;
}

void WinGuiProcess::stop()
{
    if (m_pid)
        TerminateProcess(m_pid->hProcess, 1);
    wait();
}

bool WinGuiProcess::isRunning() const
{
    return QThread::isRunning();
}

bool WinGuiProcess::setupDebugInterface(HANDLE &bufferReadyEvent, HANDLE &dataReadyEvent, HANDLE &sharedFile, LPVOID &sharedMem)
{

    bufferReadyEvent = CreateEvent(NULL, FALSE, FALSE, L"DBWIN_BUFFER_READY");
    if (!bufferReadyEvent || GetLastError() == ERROR_ALREADY_EXISTS)
        return false;
    dataReadyEvent = CreateEvent(NULL, FALSE, FALSE, L"DBWIN_DATA_READY");
    if (!dataReadyEvent || GetLastError() == ERROR_ALREADY_EXISTS)
        return false;
    sharedFile = CreateFileMapping((HANDLE)-1, NULL, PAGE_READWRITE, 0, 4096, L"DBWIN_BUFFER");
    if (!sharedFile || GetLastError() == ERROR_ALREADY_EXISTS)
        return false;
    sharedMem = MapViewOfFile(sharedFile, FILE_MAP_READ, 0, 0,  512);
    if (!sharedMem)
        return false;
    return true;
}

void WinGuiProcess::run()
{
    if (m_pid)
        return;

    STARTUPINFO si;
    ZeroMemory(&si, sizeof(si));
    si.cb = sizeof(si);

    m_pid = new PROCESS_INFORMATION;
    ZeroMemory(m_pid, sizeof(PROCESS_INFORMATION));

    m_exitCode = 0;
    bool started = false;

    HANDLE bufferReadyEvent = NULL;
    HANDLE dataReadyEvent = NULL;
    HANDLE sharedFile = NULL;
    LPVOID sharedMem = 0;

    do {

        const bool dbgInterface = setupDebugInterface(bufferReadyEvent, dataReadyEvent, sharedFile, sharedMem);

        QString pcmd, pargs;
        QtcProcess::prepareCommand(m_program, m_args, &pcmd, &pargs, &m_environment, &m_workingDir);
        const QString cmdLine = createWinCommandline(pcmd, pargs);
        const QStringList env = m_environment.toStringList();
        started = CreateProcessW(0, (WCHAR*)cmdLine.utf16(),
                                      0, 0, TRUE, CREATE_UNICODE_ENVIRONMENT,
                                      env.isEmpty() ? 0
                                          : createWinEnvironment(fixWinEnvironment(env)).data(),
                                          workingDirectory().isEmpty() ? 0
                                              : (WCHAR*)QDir::convertSeparators(workingDirectory()).utf16(),
                                              &si, m_pid);

        if (!started) {
            emit processMessage(tr("The process could not be started: %1").
                                arg(Utils::winErrorMessage(GetLastError())), true);
            emit processFinished(0);
            break;
        }

        if (!dbgInterface) {
            // Text is filtered in qmlengine.cpp
            emit receivedDebugOutput(Utils::AbstractProcess::msgWinCannotRetrieveDebuggingOutput(), true);
{'fraction_non_alphanumeric': 0.1328358208955224, 'fraction_numerical': 0.008208955223880597, 'mean_word_length': 3.1072679509632226, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 15, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 22, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2758024', 'n_tokens_mistral': 2690, 'n_tokens_neox': 2525, 'n_words': 752}