OSDN Git Service

Update license.
[qt-creator-jp/qt-creator-jp.git] / src / libs / utils / abstractprocess_win.cpp
1 /**************************************************************************
2 **
3 ** This file is part of Qt Creator
4 **
5 ** Copyright (c) 2011 Nokia Corporation and/or its subsidiary(-ies).
6 **
7 ** Contact: Nokia Corporation (info@qt.nokia.com)
8 **
9 **
10 ** GNU Lesser General Public License Usage
11 **
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.
18 **
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.
22 **
23 ** Other Usage
24 **
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.
27 **
28 ** If you have questions regarding the use of this file, please contact
29 ** Nokia at qt-info@nokia.com.
30 **
31 **************************************************************************/
32
33 #include "abstractprocess.h"
34
35 #include <windows.h>
36
37 namespace Utils {
38
39 QStringList AbstractProcess::fixWinEnvironment(const QStringList &env)
40 {
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");
45         if (!path.isEmpty())
46             envStrings.prepend(QString(QLatin1String("PATH=%1")).arg(QString::fromLocal8Bit(path)));
47     }
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)));
53     }
54     return envStrings;
55 }
56
57 static QString quoteWinCommand(const QString &program)
58 {
59     const QChar doubleQuote = QLatin1Char('"');
60
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);
68     }
69     return programName;
70 }
71
72 static QString quoteWinArgument(const QString &arg)
73 {
74     if (!arg.length())
75         return QString::fromLatin1("\"\"");
76
77     QString ret(arg);
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('"'));
86     }
87     return ret;
88 }
89
90 QString AbstractProcess::createWinCommandline(const QString &program, const QStringList &args)
91 {
92     QString programName = quoteWinCommand(program);
93     foreach (const QString &arg, args) {
94         programName += QLatin1Char(' ');
95         programName += quoteWinArgument(arg);
96     }
97     return programName;
98 }
99
100 QString AbstractProcess::createWinCommandline(const QString &program, const QString &args)
101 {
102     QString programName = quoteWinCommand(program);
103     if (!args.isEmpty()) {
104         programName += QLatin1Char(' ');
105         programName += args;
106     }
107     return programName;
108 }
109
110 QByteArray AbstractProcess::createWinEnvironment(const QStringList &env)
111 {
112     QByteArray envlist;
113     int pos = 0;
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);
118         pos += tmpSize;
119     }
120     envlist.resize(envlist.size() + 2);
121     envlist[pos++] = 0;
122     envlist[pos++] = 0;
123     return envlist;
124 }
125
126 } //namespace Utils