30static int fromHex( TQChar c )
32 if (c >= (TQChar)
'0' && c <= (TQChar)
'9')
33 return c - (TQChar)
'0';
34 else if (c >= (TQChar)
'A' && c <= (TQChar)
'F')
35 return c - (TQChar)
'A' + 10;
36 else if (c >= (TQChar)
'a' && c <= (TQChar)
'f')
37 return c - (TQChar)
'a' + 10;
41inline static bool isQuoteMeta( uint c )
44 static const uchar iqm[] = {
45 0x00, 0x00, 0x00, 0x00, 0x94, 0x00, 0x00, 0x00,
46 0x00, 0x00, 0x00, 0x10, 0x00, 0x00, 0x00, 0x00
49 return (c <
sizeof(iqm) * 8) && (iqm[c / 8] & (1 << (c & 7)));
51 return c == (int)
'\\' || c == (
int)
'\'' || c == (int)
'"' || c == (
int)
'$';
55inline static bool isMeta( uint c )
57 static const uchar iqm[] = {
58 0x00, 0x00, 0x00, 0x00, 0xdc, 0x07, 0x00, 0xd8,
59 0x00, 0x00, 0x00, 0x10, 0x01, 0x00, 0x00, 0x38
62 return (c <
sizeof(iqm) * 8) && (iqm[c / 8] & (1 << (c & 7)));
70 for (uint pos = 0; ; ) {
73 if (pos >= args.length())
75 c = args.unicode()[pos++];
76 }
while (c.isSpace());
81 if (pos >= args.length())
83 c = args.unicode()[pos];
84 if (c == (TQChar)
'/' || c.isSpace())
86 if (isQuoteMeta( c )) {
94 TQString ccret =
homeDir( TQConstString( args.unicode() + opos, pos - opos ).string() );
95 if (ccret.isEmpty()) {
100 if (pos >= args.length()) {
114 if (c == (TQChar)
'_' || (c >= (TQChar)
'A' && c <= (TQChar)
'Z') || (c >= (TQChar)
'a' && c <= (TQChar)
'z')) {
119 while (cc == (TQChar)
'_' || (cc >= (TQChar)
'A' && cc <= (TQChar)
'Z') ||
120 (cc >= (TQChar)
'a' && cc <= (TQChar)
'z') || (cc >= (TQChar)
'0' && cc <= (TQChar)
'9'));
121 if (cc == (TQChar)
'=')
127 if (c == (TQChar)
'\'') {
130 if (pos >= args.length())
132 c = args.unicode()[pos++];
133 }
while (c != (TQChar)
'\'');
134 cret += TQConstString( args.unicode() + spos, pos - spos - 1 ).string();
135 }
else if (c == (TQChar)
'"') {
137 if (pos >= args.length())
139 c = args.unicode()[pos++];
140 if (c == (TQChar)
'"')
142 if (c == (TQChar)
'\\') {
143 if (pos >= args.length())
145 c = args.unicode()[pos++];
146 if (c != (TQChar)
'"' && c != (TQChar)
'\\' &&
147 !((flags &
AbortOnMeta) && (c == (TQChar)
'$' || c == (TQChar)
'`')))
148 cret += (TQChar)
'\\';
149 }
else if ((flags &
AbortOnMeta) && (c == (TQChar)
'$' || c == (TQChar)
'`'))
153 }
else if (c == (TQChar)
'$' && args[pos] == (TQChar)
'\'') {
156 if (pos >= args.length())
158 c = args.unicode()[pos++];
159 if (c == (TQChar)
'\'')
161 if (c == (TQChar)
'\\') {
162 if (pos >= args.length())
164 c = args.unicode()[pos++];
166 case 'a': cret += (TQChar)
'\a';
break;
167 case 'b': cret += (TQChar)
'\b';
break;
168 case 'e': cret += (TQChar)
'\033';
break;
169 case 'f': cret += (TQChar)
'\f';
break;
170 case 'n': cret += (TQChar)
'\n';
break;
171 case 'r': cret += (TQChar)
'\r';
break;
172 case 't': cret += (TQChar)
'\t';
break;
173 case '\\': cret += (TQChar)
'\\';
break;
174 case '\'': cret += (TQChar)
'\'';
break;
175 case 'c': cret += args[pos++] & 31;
break;
178 int hv = fromHex( args[pos] );
182 int hhv = fromHex( args[++pos] );
187 cret += TQChar( hv );
192 if (c >= (TQChar)
'0' && c <= (TQChar)
'7') {
194 for (
int i = 0; i < 2; i++) {
196 if (c < (TQChar)
'0' || c > (TQChar)
'7')
198 hv = hv * 8 + (c -
'0');
201 cret += TQChar( hv );
212 if (c == (TQChar)
'\\') {
213 if (pos >= args.length())
215 c = args.unicode()[pos++];
217 !((flags &
AbortOnMeta) ? isMeta( c ) : isQuoteMeta( c )))
223 if (pos >= args.length())
225 c = args.unicode()[pos++];
226 }
while (!c.isSpace());
239 return TQStringList();
244 return TQStringList();
247inline static bool isSpecial( uint c )
249 static const uchar iqm[] = {
250 0xff, 0xff, 0xff, 0xff, 0xdd, 0x07, 0x00, 0xd8,
251 0x00, 0x00, 0x00, 0x10, 0x01, 0x00, 0x00, 0x38
254 return (c <
sizeof(iqm) * 8) && (iqm[c / 8] & (1 << (c & 7)));
261 for (TQStringList::ConstIterator it = args.begin(); it != args.end(); ++it) {
265 ret.append( q ).append( q );
267 for (uint i = 0; i < (*it).length(); i++)
268 if (isSpecial((*it).unicode()[i])) {
270 tmp.replace( q,
"'\\''" );
286 return TQString::null;
289 for (
const char *
const *argp = args; nargs && *argp; argp++, nargs--) {
293 ret.append( q ).append( q );
295 TQString tmp( TQFile::decodeName( *argp ) );
296 for (uint i = 0; i < tmp.length(); i++)
297 if (isSpecial(tmp.unicode()[i])) {
298 tmp.replace( q,
"'\\''" );
313 TQChar q(
'\'' ), sp(
' ' ), bs(
'\\' );
315 for (TQStringList::ConstIterator it = args.begin(); it != args.end(); ++it) {
319 ret.append( q ).append( q );
321 for (uint i = 0; i < (*it).length(); i++)
322 if (isSpecial((*it).unicode()[i])) {
323 ret.append(
'$' ).append( q );
324 for (uint pos = 0; pos < (*it).length(); pos++) {
325 int c = (*it).unicode()[pos];
329 case '\a': ret +=
'a';
break;
330 case '\b': ret +=
'b';
break;
331 case '\033': ret +=
'e';
break;
332 case '\f': ret +=
'f';
break;
333 case '\n': ret +=
'n';
break;
334 case '\r': ret +=
'r';
break;
335 case '\t': ret +=
't';
break;
336 case '\034': ret +=
'c'; ret +=
'|';
break;
337 default: ret +=
'c'; ret += c +
'@';
break;
340 if (c ==
'\'' || c ==
'\\')
357 if (fname[0] == (TQChar)
'~') {
358 int pos = fname.find(
'/' );
360 return homeDir( TQConstString( fname.unicode() + 1, fname.length() - 1 ).string() );
361 TQString ret =
homeDir( TQConstString( fname.unicode() + 1, pos - 1 ).string() );
363 ret += TQConstString( fname.unicode() + pos, fname.length() - pos ).string();
372 return TQFile::decodeName( getenv(
"HOME" ) );
373 struct passwd *pw = getpwnam( TQFile::encodeName( user ).data() );
375 return TQString::null;
376 return TQFile::decodeName( pw->pw_dir );
@ TildeExpand
Perform tilde expansion.
@ AbortOnMeta
Bail out if a non-quoting and not quoted shell meta character is encoutered.
TQString joinArgs(const TQStringList &args)
Quotes and joins args together according to POSIX shell rules.
TQStringList splitArgs(const TQString &cmd, int flags=0, int *err=0)
Splits cmd according to POSIX shell word splitting and quoting rules.
TQString joinArgsDQ(const TQStringList &args)
Same as above, but $'' is used instead of '' for the quoting.
TQString homeDir(const TQString &user)
Obtain a user's home directory.
TQString tildeExpand(const TQString &path)
Performs tilde expansion on path.
@ BadQuoting
Indicates a parsing error, like an unterminated quoted string.
@ FoundMeta
The AbortOnMeta flag was set and a shell meta character was encoutered.