27 #include <QtCore/QChar>
28 #include <QtCore/QStringList>
32 char c = cUnicode.toLatin1 ();
34 if (c >=
'0' && c <=
'9')
36 else if (c >=
'A' && c <=
'F')
38 else if (c >=
'a' && c <=
'f')
45 #if 0 // it's not worth it, especially after seeing gcc's asm output ...
46 static const uchar iqm[] = {
47 0x00, 0x00, 0x00, 0x00, 0x94, 0x00, 0x00, 0x00,
48 0x00, 0x00, 0x00, 0x10, 0x00, 0x00, 0x00, 0x00
51 return (c <
sizeof(iqm) * 8) && (iqm[c / 8] & (1 << (c & 7)));
53 char c = cUnicode.toLatin1();
54 return c ==
'\\' || c ==
'\'' || c ==
'"' || c ==
'$';
58 inline static bool isMeta( QChar cUnicode )
60 static const uchar iqm[] = {
61 0x00, 0x00, 0x00, 0x00, 0xdc, 0x07, 0x00, 0xd8,
62 0x00, 0x00, 0x00, 0x38, 0x01, 0x00, 0x00, 0x38
65 uint c = cUnicode.unicode();
67 return (c <
sizeof(iqm) * 8) && (iqm[c / 8] & (1 << (c & 7)));
75 for (
int pos = 0; ; ) {
78 if (pos >= args.length())
80 c = args.unicode()[pos++];
81 }
while (c.isSpace());
83 if ((flags &
TildeExpand) && c == QLatin1Char(
'~')) {
86 if (pos >= args.length())
88 c = args.unicode()[pos];
89 if (c == QLatin1Char(
'/') || c.isSpace())
96 if ((flags & AbortOnMeta) &&
isMeta( c ))
100 if (ccret.isEmpty()) {
102 c = QLatin1Char(
'~');
105 if (pos >= args.length()) {
119 if (c == QLatin1Char(
'_') ||
120 (c >= QLatin1Char(
'A') && c <= QLatin1Char(
'Z')) ||
121 (c >= QLatin1Char(
'a') && c <= QLatin1Char(
'z')))
126 if (pos2 >= args.length()) {
128 ret += args.mid(pos - 1);
131 cc = args.unicode()[pos2++];
132 }
while (cc == QLatin1Char(
'_') ||
133 (cc >= QLatin1Char(
'A') && cc <= QLatin1Char(
'Z')) ||
134 (cc >= QLatin1Char(
'a') && cc <= QLatin1Char(
'z')) ||
135 (cc >= QLatin1Char(
'0') && cc <= QLatin1Char(
'9')));
136 if (cc == QLatin1Char(
'='))
142 if (c == QLatin1Char(
'\'')) {
145 if (pos >= args.length())
147 c = args.unicode()[pos++];
148 }
while (c != QLatin1Char(
'\''));
149 cret += args.mid(spos, pos-spos-1);
150 }
else if (c == QLatin1Char(
'"')) {
152 if (pos >= args.length())
154 c = args.unicode()[pos++];
155 if (c == QLatin1Char(
'"'))
157 if (c == QLatin1Char(
'\\')) {
158 if (pos >= args.length())
160 c = args.unicode()[pos++];
161 if (c != QLatin1Char(
'"') &&
162 c != QLatin1Char(
'\\') &&
163 !((flags & AbortOnMeta) &&
164 (c == QLatin1Char(
'$') ||
165 c == QLatin1Char(
'`'))))
166 cret += QLatin1Char(
'\\');
167 }
else if ((flags & AbortOnMeta) &&
168 (c == QLatin1Char(
'$') ||
169 c == QLatin1Char(
'`')))
173 }
else if (c == QLatin1Char(
'$') && pos < args.length() &&
174 args.unicode()[pos] == QLatin1Char(
'\'')) {
177 if (pos >= args.length())
179 c = args.unicode()[pos++];
180 if (c == QLatin1Char(
'\''))
182 if (c == QLatin1Char(
'\\')) {
183 if (pos >= args.length())
185 c = args.unicode()[pos++];
186 switch (c.toLatin1()) {
187 case 'a': cret += QLatin1Char(
'\a');
break;
188 case 'b': cret += QLatin1Char(
'\b');
break;
189 case 'e': cret += QLatin1Char(
'\033');
break;
190 case 'f': cret += QLatin1Char(
'\f');
break;
191 case 'n': cret += QLatin1Char(
'\n');
break;
192 case 'r': cret += QLatin1Char(
'\r');
break;
193 case 't': cret += QLatin1Char(
'\t');
break;
194 case '\\': cret += QLatin1Char(
'\\');
break;
195 case '\'': cret += QLatin1Char(
'\'');
break;
197 if (pos >= args.length())
199 cret += args.unicode()[pos++].toLatin1() & 31;
203 if (pos >= args.length())
205 int hv =
fromHex( args.unicode()[pos++] );
208 if (pos < args.length()) {
209 int hhv =
fromHex( args.unicode()[pos] );
219 if (c.toLatin1() >=
'0' && c.toAscii() <=
'7') {
220 char cAscii = c.toLatin1();
221 int hv = cAscii -
'0';
222 for (
int i = 0; i < 2; i++) {
223 if (pos >= args.length())
225 c = args.unicode()[pos];
226 if (c.toLatin1() <
'0' || c.toAscii() >
'7')
228 hv = hv * 8 + (c.toLatin1() -
'0');
233 cret += QLatin1Char(
'\\');
242 if (c == QLatin1Char(
'\\')) {
243 if (pos >= args.length())
245 c = args.unicode()[pos++];
246 }
else if ((flags & AbortOnMeta) &&
isMeta( c ))
250 if (pos >= args.length())
252 c = args.unicode()[pos++];
253 }
while (!c.isSpace());
276 static const uchar iqm[] = {
277 0xff, 0xff, 0xff, 0xff, 0xdf, 0x07, 0x00, 0xd8,
278 0x00, 0x00, 0x00, 0x38, 0x01, 0x00, 0x00, 0x78
281 uint c = cUnicode.unicode ();
282 return (c <
sizeof(iqm) * 8) && (iqm[c / 8] & (1 << (c & 7)));
288 return QString::fromLatin1(
"''");
289 for (
int i = 0; i < arg.length(); i++)
291 QChar q( QLatin1Char(
'\'') );
292 return QString( arg ).replace( q, QLatin1String(
"'\\''") ).prepend( q ).append( q );
static bool isSpecial(QChar cUnicode)
QStringList splitArgs(const QString &cmd, Options flags=NoOptions, Errors *err=0)
Splits cmd according to system shell word splitting and quoting rules.
The AbortOnMeta flag was set and an unhandled shell meta character was encoutered.
Errors
Status codes from splitArgs()
QString quoteArg(const QString &arg)
Quotes arg according to system shell rules.
QString homeDir(const QString &user)
Indicates a parsing error, like an unterminated quoted string.
static bool isMeta(QChar cUnicode)
static bool isQuoteMeta(QChar cUnicode)
static int fromHex(QChar cUnicode)
Put the parser into full shell mode and bail out if a too complex construct is encoutered.