diff options
author | Jake Petroules <jake.petroules@petroules.com> | 2014-07-16 11:34:27 -0400 |
---|---|---|
committer | Jake Petroules <jake.petroules@petroules.com> | 2014-07-18 21:09:04 +0200 |
commit | 4251b0b228985d7a033e93d1ae51093baedb1c2a (patch) | |
tree | 7560ba946bacd083c1dabbf3972a2b1aa949a4df /src/lib | |
parent | 0ebf1bfa336b8d1fdf864925cd0cb45f0f56c572 (diff) |
Import some shellquote utility functions from qmake.
Will be used by future patches.
Change-Id: I20bc0cdcbf8bde8d351e3fce30d3e15f1cb444e8
Reviewed-by: Oswald Buddenhagen <oswald.buddenhagen@digia.com>
Reviewed-by: Christian Kandeler <christian.kandeler@digia.com>
Diffstat (limited to 'src/lib')
-rw-r--r-- | src/lib/corelib/corelib.qbs | 2 | ||||
-rw-r--r-- | src/lib/corelib/tools/shellutils.cpp | 102 | ||||
-rw-r--r-- | src/lib/corelib/tools/shellutils.h | 54 | ||||
-rw-r--r-- | src/lib/corelib/tools/tools.pri | 2 |
4 files changed, 160 insertions, 0 deletions
diff --git a/src/lib/corelib/corelib.qbs b/src/lib/corelib/corelib.qbs index e3819cf67..f258e3bb5 100644 --- a/src/lib/corelib/corelib.qbs +++ b/src/lib/corelib/corelib.qbs @@ -321,6 +321,8 @@ QbsLibrary { "scripttools.h", "settings.cpp", "setupprojectparameters.cpp", + "shellutils.cpp", + "shellutils.h", "weakpointer.h" ] } diff --git a/src/lib/corelib/tools/shellutils.cpp b/src/lib/corelib/tools/shellutils.cpp new file mode 100644 index 000000000..550bc1937 --- /dev/null +++ b/src/lib/corelib/tools/shellutils.cpp @@ -0,0 +1,102 @@ +/**************************************************************************** +** +** Copyright (C) 2014 Digia Plc and/or its subsidiary(-ies). +** Copyright (C) 2014 Petroules Corporation. +** Contact: http://www.qt-project.org/legal +** +** This file is part of the Qt Build Suite. +** +** Commercial License Usage +** Licensees holding valid commercial Qt licenses may use this file in +** accordance with the commercial license agreement provided with the +** Software or, alternatively, in accordance with the terms contained in +** a written agreement between you and Digia. For licensing terms and +** conditions see http://qt.digia.com/licensing. For further information +** use the contact form at http://qt.digia.com/contact-us. +** +** 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, Digia gives you certain additional +** rights. These rights are described in the Digia Qt LGPL Exception +** version 1.1, included in the file LGPL_EXCEPTION.txt in this package. +** +****************************************************************************/ + +#include "shellutils.h" +#include <QRegExp> + +namespace qbs { +namespace Internal { + +// hasSpecialChars, shellQuoteUnix, shellQuoteWin: all from qtbase/qmake/library/ioutils.cpp + +inline static bool hasSpecialChars(const QString &arg, const uchar (&iqm)[16]) +{ + for (int x = arg.length() - 1; x >= 0; --x) { + ushort c = arg.unicode()[x].unicode(); + if ((c < sizeof(iqm) * 8) && (iqm[c / 8] & (1 << (c & 7)))) + return true; + } + return false; +} + +QString shellQuoteUnix(const QString &arg) +{ + // Chars that should be quoted (TM). This includes: + static const uchar iqm[] = { + 0xff, 0xff, 0xff, 0xff, 0xdf, 0x07, 0x00, 0xd8, + 0x00, 0x00, 0x00, 0x38, 0x01, 0x00, 0x00, 0x78 + }; // 0-32 \'"$`<>|;&(){}*?#!~[] + + if (!arg.length()) + return QString::fromLatin1("\"\""); + + QString ret(arg); + if (hasSpecialChars(ret, iqm)) { + ret.replace(QLatin1Char('\''), QLatin1String("'\\''")); + ret.prepend(QLatin1Char('\'')); + ret.append(QLatin1Char('\'')); + } + return ret; +} + +QString shellQuoteWin(const QString &arg) +{ + // Chars that should be quoted (TM). This includes: + // - control chars & space + // - the shell meta chars "&()<>^| + // - the potential separators ,;= + static const uchar iqm[] = { + 0xff, 0xff, 0xff, 0xff, 0x45, 0x13, 0x00, 0x78, + 0x00, 0x00, 0x00, 0x40, 0x00, 0x00, 0x00, 0x10 + }; + + if (!arg.length()) + return QString::fromLatin1("\"\""); + + QString ret(arg); + if (hasSpecialChars(ret, iqm)) { + // Quotes are escaped and their preceding backslashes are doubled. + // It's impossible to escape anything inside a quoted string on cmd + // level, so the outer quoting must be "suspended". + ret.replace(QRegExp(QLatin1String("(\\\\*)\"")), QLatin1String("\"\\1\\1\\^\"\"")); + // 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\" + int i = ret.length(); + while (i > 0 && ret.at(i - 1) == QLatin1Char('\\')) + --i; + ret.insert(i, QLatin1Char('"')); + ret.prepend(QLatin1Char('"')); + } + return ret; +} + +} // namespace Internal +} // namespace qbs diff --git a/src/lib/corelib/tools/shellutils.h b/src/lib/corelib/tools/shellutils.h new file mode 100644 index 000000000..ca792039f --- /dev/null +++ b/src/lib/corelib/tools/shellutils.h @@ -0,0 +1,54 @@ +/**************************************************************************** +** +** Copyright (C) 2014 Digia Plc and/or its subsidiary(-ies). +** Copyright (C) 2014 Petroules Corporation. +** Contact: http://www.qt-project.org/legal +** +** This file is part of the Qt Build Suite. +** +** Commercial License Usage +** Licensees holding valid commercial Qt licenses may use this file in +** accordance with the commercial license agreement provided with the +** Software or, alternatively, in accordance with the terms contained in +** a written agreement between you and Digia. For licensing terms and +** conditions see http://qt.digia.com/licensing. For further information +** use the contact form at http://qt.digia.com/contact-us. +** +** 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, Digia gives you certain additional +** rights. These rights are described in the Digia Qt LGPL Exception +** version 1.1, included in the file LGPL_EXCEPTION.txt in this package. +** +****************************************************************************/ + +#ifndef QBS_SHELLUTILS_H +#define QBS_SHELLUTILS_H + +#include "qbs_export.h" +#include "hostosinfo.h" +#include <QString> + +namespace qbs { +namespace Internal { + +QBS_EXPORT QString shellQuoteUnix(const QString &arg); +QBS_EXPORT QString shellQuoteWin(const QString &arg); + +inline static QString shellQuote(const QString &arg) +{ + return HostOsInfo::isWindowsHost() + ? shellQuoteWin(arg) + : shellQuoteUnix(arg); +} + +} // namespace Internal +} // namespace qbs + +#endif // QBS_SHELLUTILS_H diff --git a/src/lib/corelib/tools/tools.pri b/src/lib/corelib/tools/tools.pri index 38f8eb63d..ea7a07b56 100644 --- a/src/lib/corelib/tools/tools.pri +++ b/src/lib/corelib/tools/tools.pri @@ -17,6 +17,7 @@ HEADERS += \ $$PWD/processresult_p.h \ $$PWD/progressobserver.h \ $$PWD/propertyfinder.h \ + $$PWD/shellutils.h \ $$PWD/hostosinfo.h \ $$PWD/buildoptions.h \ $$PWD/installoptions.h \ @@ -43,6 +44,7 @@ SOURCES += \ $$PWD/profile.cpp \ $$PWD/progressobserver.cpp \ $$PWD/propertyfinder.cpp \ + $$PWD/shellutils.cpp \ $$PWD/buildoptions.cpp \ $$PWD/installoptions.cpp \ $$PWD/cleanoptions.cpp \ |