2 ******************************************************************************
4 * @file abstractprocess_win.cpp
5 * @author The OpenPilot Team, http://www.openpilot.org Copyright (C) 2010.
6 * Parts by Nokia Corporation (qt-info@nokia.com) Copyright (C) 2009.
8 * @see The GNU Public License (GPL) Version 3
12 *****************************************************************************/
14 * This program is free software; you can redistribute it and/or modify
15 * it under the terms of the GNU General Public License as published by
16 * the Free Software Foundation; either version 3 of the License, or
17 * (at your option) any later version.
19 * This program is distributed in the hope that it will be useful, but
20 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
21 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
24 * You should have received a copy of the GNU General Public License along
25 * with this program; if not, write to the Free Software Foundation, Inc.,
26 * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
29 #include "abstractprocess.h"
34 QStringList
AbstractProcess::fixWinEnvironment(const QStringList
&env
)
36 QStringList envStrings
= env
;
38 // add PATH if necessary (for DLL loading)
39 if (envStrings
.filter(QRegExp(QLatin1String("^PATH="), Qt::CaseInsensitive
)).isEmpty()) {
40 QByteArray path
= qgetenv("PATH");
41 if (!path
.isEmpty()) {
42 envStrings
.prepend(QString(QLatin1String("PATH=%1")).arg(QString::fromLocal8Bit(path
)));
45 // add systemroot if needed
46 if (envStrings
.filter(QRegExp(QLatin1String("^SystemRoot="), Qt::CaseInsensitive
)).isEmpty()) {
47 QByteArray systemRoot
= qgetenv("SystemRoot");
48 if (!systemRoot
.isEmpty()) {
49 envStrings
.prepend(QString(QLatin1String("SystemRoot=%1")).arg(QString::fromLocal8Bit(systemRoot
)));
55 QString
AbstractProcess::createWinCommandline(const QString
&program
, const QStringList
&args
)
57 const QChar doubleQuote
= QLatin1Char('"');
58 const QChar blank
= QLatin1Char(' ');
59 const QChar backSlash
= QLatin1Char('\\');
61 QString programName
= program
;
63 if (!programName
.startsWith(doubleQuote
) && !programName
.endsWith(doubleQuote
) && programName
.contains(blank
)) {
64 programName
.insert(0, doubleQuote
);
65 programName
.append(doubleQuote
);
67 // add the prgram as the first arrg ... it works better
68 programName
.replace(QLatin1Char('/'), backSlash
);
69 QString cmdLine
= programName
;
75 for (int i
= 0; i
< args
.size(); ++i
) {
76 QString tmp
= args
.at(i
);
77 // in the case of \" already being in the string the \ must also be escaped
78 tmp
.replace(QLatin1String("\\\""), QLatin1String("\\\\\""));
79 // escape a single " because the arguments will be parsed
80 tmp
.replace(QString(doubleQuote
), QLatin1String("\\\""));
81 if (tmp
.isEmpty() || tmp
.contains(blank
) || tmp
.contains('\t')) {
82 // The argument must not end with a \ since this would be interpreted
83 // as escaping the quote -- rather put the \ behind the quote: e.g.
84 // rather use "foo"\ than "foo\"
85 QString
endQuote(doubleQuote
);
87 while (i
> 0 && tmp
.at(i
- 1) == backSlash
) {
89 endQuote
+= backSlash
;
91 cmdLine
+= QLatin1String(" \"");
92 cmdLine
+= tmp
.left(i
);
102 QByteArray
AbstractProcess::createWinEnvironment(const QStringList
&env
)
107 foreach(const QString
&tmp
, env
) {
108 const uint tmpSize
= sizeof(TCHAR
) * (tmp
.length() + 1);
110 envlist
.resize(envlist
.size() + tmpSize
);
111 memcpy(envlist
.data() + pos
, tmp
.utf16(), tmpSize
);
114 envlist
.resize(envlist
.size() + 2);