1 /**************************************************************************
3 ** This file is part of Qt Creator
5 ** Copyright (c) 2011 Nokia Corporation and/or its subsidiary(-ies).
7 ** Contact: Nokia Corporation (info@qt.nokia.com)
10 ** GNU Lesser General Public License Usage
12 ** This file may be used under the terms of the GNU Lesser General Public
13 ** License version 2.1 as published by the Free Software Foundation and
14 ** appearing in the file LICENSE.LGPL included in the packaging of this file.
15 ** Please review the following information to ensure the GNU Lesser General
16 ** Public License version 2.1 requirements will be met:
17 ** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
19 ** In addition, as a special exception, Nokia gives you certain additional
20 ** rights. These rights are described in the Nokia Qt LGPL Exception
21 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
25 ** Alternatively, this file may be used in accordance with the terms and
26 ** conditions contained in a signed written agreement between you and Nokia.
28 ** If you have questions regarding the use of this file, please contact
29 ** Nokia at qt-info@nokia.com.
31 **************************************************************************/
33 #include "abstractprocess.h"
39 QStringList AbstractProcess::fixWinEnvironment(const QStringList &env)
41 QStringList envStrings = env;
42 // add PATH if necessary (for DLL loading)
43 if (envStrings.filter(QRegExp(QLatin1String("^PATH="),Qt::CaseInsensitive)).isEmpty()) {
44 QByteArray path = qgetenv("PATH");
46 envStrings.prepend(QString(QLatin1String("PATH=%1")).arg(QString::fromLocal8Bit(path)));
48 // add systemroot if needed
49 if (envStrings.filter(QRegExp(QLatin1String("^SystemRoot="),Qt::CaseInsensitive)).isEmpty()) {
50 QByteArray systemRoot = qgetenv("SystemRoot");
51 if (!systemRoot.isEmpty())
52 envStrings.prepend(QString(QLatin1String("SystemRoot=%1")).arg(QString::fromLocal8Bit(systemRoot)));
57 static QString quoteWinCommand(const QString &program)
59 const QChar doubleQuote = QLatin1Char('"');
61 // add the program as the first arg ... it works better
62 QString programName = program;
63 programName.replace(QLatin1Char('/'), QLatin1Char('\\'));
64 if (!programName.startsWith(doubleQuote) && !programName.endsWith(doubleQuote)
65 && programName.contains(QLatin1Char(' '))) {
66 programName.prepend(doubleQuote);
67 programName.append(doubleQuote);
72 static QString quoteWinArgument(const QString &arg)
75 return QString::fromLatin1("\"\"");
78 // Quotes are escaped and their preceding backslashes are doubled.
79 ret.replace(QRegExp(QLatin1String("(\\\\*)\"")), QLatin1String("\\1\\1\\\""));
80 if (ret.contains(QRegExp(QLatin1String("\\s")))) {
81 // The argument must not end with a \ since this would be interpreted
82 // as escaping the quote -- rather put the \ behind the quote: e.g.
83 // rather use "foo"\ than "foo\"
84 ret.replace(QRegExp(QLatin1String("(\\\\*)$")), QLatin1String("\"\\1"));
85 ret.prepend(QLatin1Char('"'));
90 QString AbstractProcess::createWinCommandline(const QString &program, const QStringList &args)
92 QString programName = quoteWinCommand(program);
93 foreach (const QString &arg, args) {
94 programName += QLatin1Char(' ');
95 programName += quoteWinArgument(arg);
100 QString AbstractProcess::createWinCommandline(const QString &program, const QString &args)
102 QString programName = quoteWinCommand(program);
103 if (!args.isEmpty()) {
104 programName += QLatin1Char(' ');
110 QByteArray AbstractProcess::createWinEnvironment(const QStringList &env)
114 foreach (const QString &tmp, env) {
115 const uint tmpSize = sizeof(TCHAR) * (tmp.length() + 1);
116 envlist.resize(envlist.size() + tmpSize);
117 memcpy(envlist.data() + pos, tmp.utf16(), tmpSize);
120 envlist.resize(envlist.size() + 2);