Commit 65650b26 authored by Oswald Buddenhagen's avatar Oswald Buddenhagen

make shellQuote() nicer

- unify unix and windows paths
- on windows:
  - we don't need to care for trailing backslashes, as we never append
    directly to quoted strings
  - simplify, as this doesn't need to be fast.
    unfortunately, it's impossible to use
      ret.replace(QRegExp(QLatin1String("(\\\\*)$")), QLatin1String("\"\\1"));
    as that matches foo\ twice (once one backslash and once an empty
    string).
parent 1914d05d
......@@ -88,115 +88,60 @@ QString IoUtils::resolvePath(const QString &baseDir, const QString &fileName)
return QDir::cleanPath(baseDir + QLatin1Char('/') + fileName);
}
#ifdef Q_OS_WIN
// FIXME: Without this, quoting is not foolproof. But it needs support in the process setup, etc.
//#define PERCENT_ESCAPE QLatin1String("%PERCENT_SIGN%")
static QString quoteArgInternal(const QString &arg)
{
// Escape quotes, preceding backslashes are doubled. Surround with quotes.
// Note that cmd does not understand quote escapes in quoted strings,
// so the quoting needs to be "suspended".
const QLatin1Char bs('\\'), dq('"');
QString ret;
bool inquote = false;
int bslashes = 0;
for (int p = 0; p < arg.length(); p++) {
if (arg[p] == bs) {
bslashes++;
} else if (arg[p] == dq) {
if (inquote) {
ret.append(dq);
inquote = false;
}
for (; bslashes; bslashes--)
ret.append(QLatin1String("\\\\"));
ret.append(QLatin1String("\\^\""));
} else {
if (!inquote) {
ret.append(dq);
inquote = true;
}
for (; bslashes; bslashes--)
ret.append(bs);
ret.append(arg[p]);
}
}
//ret.replace(QLatin1Char('%'), PERCENT_ESCAPE);
if (bslashes) {
// Ensure that we don't have directly trailing backslashes,
// so concatenating with another string won't cause surprises.
if (!inquote)
ret.append(dq);
for (; bslashes; bslashes--)
ret.append(QLatin1String("\\\\"));
ret.append(dq);
} else if (inquote) {
ret.append(dq);
}
return ret;
}
inline static bool isSpecialChar(ushort c)
{
// Chars that should be quoted (TM). This includes:
#ifdef Q_OS_WIN
// - control chars & space
// - the shell meta chars &()<>^|
// - the shell meta chars "&()<>^|
// - the potential separators ,;=
static const uchar iqm[] = {
0xff, 0xff, 0xff, 0xff, 0x41, 0x13, 0x00, 0x78,
0xff, 0xff, 0xff, 0xff, 0x45, 0x13, 0x00, 0x78,
0x00, 0x00, 0x00, 0x40, 0x00, 0x00, 0x00, 0x10
};
return (c < sizeof(iqm) * 8) && (iqm[c / 8] & (1 << (c & 7)));
}
QString IoUtils::shellQuote(const QString &arg)
{
if (arg.isEmpty())
return QString::fromLatin1("\"\"");
// Ensure that we don't have directly trailing backslashes,
// so concatenating with another string won't cause surprises.
if (arg.endsWith(QLatin1Char('\\')))
return quoteArgInternal(arg);
for (int x = arg.length() - 1; x >= 0; --x)
if (isSpecialChar(arg[x].unicode()))
return quoteArgInternal(arg);
// Escape quotes. Preceding backslashes are doubled.
// Note that the remaining string is not quoted.
QString ret(arg);
ret.replace(QRegExp(QLatin1String("(\\\\*)\"")), QLatin1String("\\1\\1\\^\""));
//ret.replace('%', PERCENT_ESCAPE);
return ret;
}
#else // Q_OS_WIN
inline static bool isSpecial(QChar cUnicode)
{
#else
static const uchar iqm[] = {
0xff, 0xff, 0xff, 0xff, 0xdf, 0x07, 0x00, 0xd8,
0x00, 0x00, 0x00, 0x38, 0x01, 0x00, 0x00, 0x78
}; // 0-32 \'"$`<>|;&(){}*?#!~[]
#endif
uint c = cUnicode.unicode();
return (c < sizeof(iqm) * 8) && (iqm[c / 8] & (1 << (c & 7)));
}
inline static bool hasSpecialChars(const QString &arg)
{
for (int x = arg.length() - 1; x >= 0; --x)
if (isSpecialChar(arg.unicode()[x].unicode()))
return true;
return false;
}
QString IoUtils::shellQuote(const QString &arg)
{
if (!arg.length())
return QString::fromLatin1("''");
for (int i = 0; i < arg.length(); i++)
if (isSpecial(arg.unicode()[i])) {
const QLatin1Char q('\'');
return q + QString(arg).replace(q, QLatin1String("'\\''")) + q;
}
return arg;
}
return QString::fromLatin1("\"\"");
QString ret(arg);
if (hasSpecialChars(ret)) {
#ifdef Q_OS_WIN
// 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('"'));
#else // Q_OS_WIN
ret.replace(QLatin1Char('\''), QLatin1String("'\\''"));
ret.prepend(QLatin1Char('\''));
ret.append(QLatin1Char('\''));
#endif // Q_OS_WIN
}
return ret;
}
......@@ -9,6 +9,7 @@ SUBDIRS += \
generichighlighter \
# icheckbuild \
# profilewriter \
ioutils \
utils_stringutils \
filesearch
......
CONFIG += qtestlib testcase
TEMPLATE = app
CONFIG -= app_bundle
UTILS_PATH = ../../../src/shared/proparser
INCLUDEPATH += $$UTILS_PATH
DEPENDPATH += $$UTILS_PATH
SOURCES += \
tst_ioutils.cpp
TARGET = tst_$$TARGET
/**************************************************************************
**
** This file is part of Qt Creator
**
** Copyright (c) 2010 Nokia Corporation and/or its subsidiary(-ies).
**
** Contact: Nokia Corporation (qt-info@nokia.com)
**
** Commercial Usage
**
** Licensees holding valid Qt Commercial licenses may use this file in
** accordance with the Qt Commercial License Agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and Nokia.
**
** 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.
**
** If you are unsure which license is appropriate for your use, please
** contact the sales department at http://qt.nokia.com/contact.
**
**************************************************************************/
#include "../../../src/shared/proparser/ioutils.cpp"
#include <QtTest/QtTest>
class tst_IoUtils : public QObject
{
Q_OBJECT
private slots:
void quoteArg_data();
void quoteArg();
};
void tst_IoUtils::quoteArg_data()
{
QTest::addColumn<QString>("in");
QTest::addColumn<QString>("out");
static const struct {
const char * const in;
const char * const out;
} vals[] = {
#ifdef Q_OS_WIN
{ "", "\"\"" },
{ "hallo", "hallo" },
{ "hallo du", "\"hallo du\"" },
{ "hallo\\", "hallo\\" },
{ "hallo du\\", "\"hallo du\"\\" },
{ "ha\"llo", "\"ha\"\\^\"\"llo\"" },
{ "ha\\\"llo", "\"ha\"\\\\\\^\"\"llo\"" },
#else
{ "", "\"\"" },
{ "hallo", "hallo" },
{ "hallo du", "'hallo du'" },
{ "ha'llo", "'ha'\\''llo'" },
#endif
};
for (unsigned i = 0; i < sizeof(vals)/sizeof(vals[0]); i++)
QTest::newRow(vals[i].in) << QString::fromLatin1(vals[i].in)
<< QString::fromLatin1(vals[i].out);
}
void tst_IoUtils::quoteArg()
{
QFETCH(QString, in);
QFETCH(QString, out);
QCOMPARE(IoUtils::shellQuote(in), out);
}
QTEST_MAIN(tst_IoUtils)
#include "tst_ioutils.moc"
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment