Qt
Internal/Contributor docs for the Qt SDK. <b>Note:</b> These are NOT official API docs; those are found <a href='https://doc.qt.io/'>here</a>.
Loading...
Searching...
No Matches
shellquote_shared.h
Go to the documentation of this file.
1
// Copyright (C) 2022 The Qt Company Ltd.
2
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only WITH Qt-GPL-exception-1.0
3
4
#ifndef SHELLQUOTE_SHARED_H
5
#define SHELLQUOTE_SHARED_H
6
7
#include <QDir>
8
#include <QRegularExpression>
9
#include <QString>
10
11
// Copy-pasted from qmake/library/ioutil.cpp
12
inline
static
bool
hasSpecialChars
(
const
QString
&
arg
,
const
uchar
(&iqm)[16])
13
{
14
for
(
int
x
=
arg
.size() - 1;
x
>= 0; --
x
) {
15
ushort
c
=
arg
.unicode()[
x
].unicode();
16
if
((
c
<
sizeof
(iqm) * 8) && (iqm[
c
/ 8] & (1 << (
c
& 7))))
17
return
true
;
18
}
19
return
false
;
20
}
21
22
static
QString
shellQuoteUnix
(
const
QString
&
arg
)
23
{
24
// Chars that should be quoted (TM). This includes:
25
static
const
uchar
iqm[] = {
26
0xff, 0xff, 0xff, 0xff, 0xdf, 0x07, 0x00, 0xd8,
27
0x00, 0x00, 0x00, 0x38, 0x01, 0x00, 0x00, 0x78
28
};
// 0-32 \'"$`<>|;&(){}*?#!~[]
29
30
if
(!
arg
.size())
31
return
QLatin1String
(
"\"\""
);
32
33
QString
ret
(
arg
);
34
if
(
hasSpecialChars
(
ret
, iqm)) {
35
ret
.replace(
QLatin1Char
(
'\''
),
QLatin1String
(
"'\\''"
));
36
ret
.prepend(
QLatin1Char
(
'\''
));
37
ret
.append(
QLatin1Char
(
'\''
));
38
}
39
return
ret
;
40
}
41
42
static
QString
shellQuoteWin
(
const
QString
&
arg
)
43
{
44
// Chars that should be quoted (TM). This includes:
45
// - control chars & space
46
// - the shell meta chars "&()<>^|
47
// - the potential separators ,;=
48
static
const
uchar
iqm[] = {
49
0xff, 0xff, 0xff, 0xff, 0x45, 0x13, 0x00, 0x78,
50
0x00, 0x00, 0x00, 0x40, 0x00, 0x00, 0x00, 0x10
51
};
52
53
if
(!
arg
.size())
54
return
QLatin1String
(
"\"\""
);
55
56
QString
ret
(
arg
);
57
if
(
hasSpecialChars
(
ret
, iqm)) {
58
// Quotes are escaped and their preceding backslashes are doubled.
59
// It's impossible to escape anything inside a quoted string on cmd
60
// level, so the outer quoting must be "suspended".
61
ret
.replace(
QRegularExpression
(
QLatin1String
(
"(\\\\*)\""
)),
QLatin1String
(
"\"\\1\\1\\^\"\""
));
62
// The argument must not end with a \ since this would be interpreted
63
// as escaping the quote -- rather put the \ behind the quote: e.g.
64
// rather use "foo"\ than "foo\"
65
int
i
=
ret
.size();
66
while
(
i
> 0 &&
ret
.at(
i
- 1) ==
QLatin1Char
(
'\\'
))
67
--
i
;
68
ret
.insert(
i
,
QLatin1Char
(
'"'
));
69
ret
.prepend(
QLatin1Char
(
'"'
));
70
}
71
return
ret
;
72
}
73
74
static
QString
shellQuote
(
const
QString
&
arg
)
75
{
76
if
(
QDir::separator
() ==
QLatin1Char
(
'\\'
))
77
return
shellQuoteWin
(
arg
);
78
else
79
return
shellQuoteUnix
(
arg
);
80
}
81
82
#endif
// SHELLQUOTE_SHARED_H
QDir::separator
static QChar separator()
Returns the native directory separator: "/" under Unix and "\\" under Windows.
Definition
qdir.h:209
QRegularExpression
\inmodule QtCore \reentrant
Definition
qregularexpression.h:31
QString
\macro QT_RESTRICTED_CAST_FROM_ASCII
Definition
qstring.h:129
i
i
[1]
Definition
doc_src_containers.cpp:169
ret
return ret
Definition
qmacstyle_mac.mm:1046
x
GLint GLint GLint GLint GLint x
[0]
Definition
qopengles2ext.h:605
c
const GLubyte * c
Definition
qopenglext.h:12701
arg
SSL_CTX int void * arg
Definition
qsslsocket_openssl_symbols.cpp:402
QLatin1String
QLatin1StringView QLatin1String
Definition
qstringfwd.h:31
uchar
unsigned char uchar
Definition
qtypes.h:32
ushort
unsigned short ushort
Definition
qtypes.h:33
shellQuoteWin
static QString shellQuoteWin(const QString &arg)
Definition
shellquote_shared.h:42
shellQuoteUnix
static QString shellQuoteUnix(const QString &arg)
Definition
shellquote_shared.h:22
shellQuote
static QString shellQuote(const QString &arg)
Definition
shellquote_shared.h:74
hasSpecialChars
static bool hasSpecialChars(const QString &arg, const uchar(&iqm)[16])
Definition
shellquote_shared.h:12
QLatin1Char
\inmodule QtCore \reentrant
Definition
qchar.h:18
qtbase
src
tools
shared
shellquote_shared.h
Generated by
1.10.0