You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
378 lines
13 KiB
378 lines
13 KiB
15 years ago
|
/*
|
||
|
This file is part of the KDE libraries
|
||
|
|
||
|
Copyright (c) 2003 Oswald Buddenhagen <ossi@kde.org>
|
||
|
|
||
|
This library is free software; you can redistribute it and/or
|
||
|
modify it under the terms of the GNU Library General Public
|
||
|
License as published by the Free Software Foundation; either
|
||
|
version 2 of the License, or (at your option) any later version.
|
||
|
|
||
|
This library is distributed in the hope that it will be useful,
|
||
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
||
|
Library General Public License for more details.
|
||
|
|
||
|
You should have received a copy of the GNU Library General Public License
|
||
|
along with this library; see the file COPYING.LIB. If not, write to
|
||
|
the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
|
||
|
Boston, MA 02110-1301, USA.
|
||
|
*/
|
||
|
|
||
|
#include <kshell.h>
|
||
|
|
||
15 years ago
|
#include <tqfile.h>
|
||
15 years ago
|
|
||
|
#include <stdlib.h>
|
||
|
#include <pwd.h>
|
||
|
#include <sys/types.h>
|
||
|
|
||
15 years ago
|
static int fromHex( TQChar c )
|
||
15 years ago
|
{
|
||
14 years ago
|
if (c >= (TQChar)'0' && c <= (TQChar)'9')
|
||
|
return c - (TQChar)'0';
|
||
|
else if (c >= (TQChar)'A' && c <= (TQChar)'F')
|
||
|
return c - (TQChar)'A' + 10;
|
||
|
else if (c >= (TQChar)'a' && c <= (TQChar)'f')
|
||
|
return c - (TQChar)'a' + 10;
|
||
15 years ago
|
return -1;
|
||
|
}
|
||
|
|
||
|
inline static bool isQuoteMeta( uint c )
|
||
|
{
|
||
|
#if 0 // it's not worth it, especially after seeing gcc's asm output ...
|
||
|
static const uchar iqm[] = {
|
||
|
0x00, 0x00, 0x00, 0x00, 0x94, 0x00, 0x00, 0x00,
|
||
|
0x00, 0x00, 0x00, 0x10, 0x00, 0x00, 0x00, 0x00
|
||
|
}; // \'"$
|
||
|
|
||
|
return (c < sizeof(iqm) * 8) && (iqm[c / 8] & (1 << (c & 7)));
|
||
|
#else
|
||
14 years ago
|
return c == (int)'\\' || c == (int)'\'' || c == (int)'"' || c == (int)'$';
|
||
15 years ago
|
#endif
|
||
|
}
|
||
|
|
||
|
inline static bool isMeta( uint c )
|
||
|
{
|
||
|
static const uchar iqm[] = {
|
||
|
0x00, 0x00, 0x00, 0x00, 0xdc, 0x07, 0x00, 0xd8,
|
||
|
0x00, 0x00, 0x00, 0x10, 0x01, 0x00, 0x00, 0x38
|
||
|
}; // \'"$`<>|;&(){}*?#
|
||
|
|
||
|
return (c < sizeof(iqm) * 8) && (iqm[c / 8] & (1 << (c & 7)));
|
||
|
}
|
||
|
|
||
15 years ago
|
TQStringList KShell::splitArgs( const TQString &args, int flags, int *err )
|
||
15 years ago
|
{
|
||
15 years ago
|
TQStringList ret;
|
||
15 years ago
|
bool firstword = flags & AbortOnMeta;
|
||
|
|
||
|
for (uint pos = 0; ; ) {
|
||
15 years ago
|
TQChar c;
|
||
15 years ago
|
do {
|
||
|
if (pos >= args.length())
|
||
|
goto okret;
|
||
14 years ago
|
c = args.tqunicode()[pos++];
|
||
15 years ago
|
} while (c.isSpace());
|
||
15 years ago
|
TQString cret;
|
||
14 years ago
|
if ((flags & TildeExpand) && c == (QChar)'~') {
|
||
15 years ago
|
uint opos = pos;
|
||
|
for (; ; pos++) {
|
||
|
if (pos >= args.length())
|
||
|
break;
|
||
14 years ago
|
c = args.tqunicode()[pos];
|
||
14 years ago
|
if (c == (QChar)'/' || c.isSpace())
|
||
15 years ago
|
break;
|
||
|
if (isQuoteMeta( c )) {
|
||
|
pos = opos;
|
||
14 years ago
|
c = (QChar)'~';
|
||
15 years ago
|
goto notilde;
|
||
|
}
|
||
|
if ((flags & AbortOnMeta) && isMeta( c ))
|
||
|
goto metaerr;
|
||
|
}
|
||
14 years ago
|
TQString ccret = homeDir( TQConstString( args.tqunicode() + opos, pos - opos ).string() );
|
||
15 years ago
|
if (ccret.isEmpty()) {
|
||
|
pos = opos;
|
||
14 years ago
|
c = (QChar)'~';
|
||
15 years ago
|
goto notilde;
|
||
|
}
|
||
|
if (pos >= args.length()) {
|
||
|
ret += ccret;
|
||
|
goto okret;
|
||
|
}
|
||
|
pos++;
|
||
|
if (c.isSpace()) {
|
||
|
ret += ccret;
|
||
|
firstword = false;
|
||
|
continue;
|
||
|
}
|
||
|
cret = ccret;
|
||
|
}
|
||
|
// before the notilde label, as a tilde does not match anyway
|
||
|
if (firstword) {
|
||
14 years ago
|
if (c == (QChar)'_' || (c >= (QChar)'A' && c <= (QChar)'Z') || (c >= (QChar)'a' && c <= (QChar)'z')) {
|
||
15 years ago
|
uint pos2 = pos;
|
||
15 years ago
|
TQChar cc;
|
||
15 years ago
|
do
|
||
|
cc = args[pos2++];
|
||
14 years ago
|
while (cc == (QChar)'_' || (cc >= (QChar)'A' && cc <= (QChar)'Z') ||
|
||
|
(cc >= (QChar)'a' && cc <= (QChar)'z') || (cc >= (QChar)'0' && cc <= (QChar)'9'));
|
||
|
if (cc == (QChar)'=')
|
||
15 years ago
|
goto metaerr;
|
||
|
}
|
||
|
}
|
||
|
notilde:
|
||
|
do {
|
||
14 years ago
|
if (c == (QChar)'\'') {
|
||
15 years ago
|
uint spos = pos;
|
||
|
do {
|
||
|
if (pos >= args.length())
|
||
|
goto quoteerr;
|
||
14 years ago
|
c = args.tqunicode()[pos++];
|
||
14 years ago
|
} while (c != (QChar)'\'');
|
||
14 years ago
|
cret += TQConstString( args.tqunicode() + spos, pos - spos - 1 ).string();
|
||
14 years ago
|
} else if (c == (QChar)'"') {
|
||
15 years ago
|
for (;;) {
|
||
|
if (pos >= args.length())
|
||
|
goto quoteerr;
|
||
14 years ago
|
c = args.tqunicode()[pos++];
|
||
14 years ago
|
if (c == (QChar)'"')
|
||
15 years ago
|
break;
|
||
14 years ago
|
if (c == (QChar)'\\') {
|
||
15 years ago
|
if (pos >= args.length())
|
||
|
goto quoteerr;
|
||
14 years ago
|
c = args.tqunicode()[pos++];
|
||
14 years ago
|
if (c != (QChar)'"' && c != (QChar)'\\' &&
|
||
|
!((flags & AbortOnMeta) && (c == (QChar)'$' || c == (QChar)'`')))
|
||
|
cret += (QChar)'\\';
|
||
|
} else if ((flags & AbortOnMeta) && (c == (QChar)'$' || c == (QChar)'`'))
|
||
15 years ago
|
goto metaerr;
|
||
|
cret += c;
|
||
|
}
|
||
14 years ago
|
} else if (c == (QChar)'$' && args[pos] == (QChar)'\'') {
|
||
15 years ago
|
pos++;
|
||
|
for (;;) {
|
||
|
if (pos >= args.length())
|
||
|
goto quoteerr;
|
||
14 years ago
|
c = args.tqunicode()[pos++];
|
||
14 years ago
|
if (c == (QChar)'\'')
|
||
15 years ago
|
break;
|
||
14 years ago
|
if (c == (QChar)'\\') {
|
||
15 years ago
|
if (pos >= args.length())
|
||
|
goto quoteerr;
|
||
14 years ago
|
c = args.tqunicode()[pos++];
|
||
15 years ago
|
switch (c) {
|
||
14 years ago
|
case 'a': cret += (QChar)'\a'; break;
|
||
|
case 'b': cret += (QChar)'\b'; break;
|
||
|
case 'e': cret += (QChar)'\033'; break;
|
||
|
case 'f': cret += (QChar)'\f'; break;
|
||
|
case 'n': cret += (QChar)'\n'; break;
|
||
|
case 'r': cret += (QChar)'\r'; break;
|
||
|
case 't': cret += (QChar)'\t'; break;
|
||
|
case '\\': cret += (QChar)'\\'; break;
|
||
|
case '\'': cret += (QChar)'\''; break;
|
||
15 years ago
|
case 'c': cret += args[pos++] & 31; break;
|
||
|
case 'x':
|
||
|
{
|
||
|
int hv = fromHex( args[pos] );
|
||
|
if (hv < 0) {
|
||
|
cret += "\\x";
|
||
|
} else {
|
||
|
int hhv = fromHex( args[++pos] );
|
||
|
if (hhv > 0) {
|
||
|
hv = hv * 16 + hhv;
|
||
|
pos++;
|
||
|
}
|
||
15 years ago
|
cret += TQChar( hv );
|
||
15 years ago
|
}
|
||
|
break;
|
||
|
}
|
||
|
default:
|
||
14 years ago
|
if (c >= (QChar)'0' && c <= (QChar)'7') {
|
||
15 years ago
|
int hv = c - '0';
|
||
|
for (int i = 0; i < 2; i++) {
|
||
|
c = args[pos];
|
||
14 years ago
|
if (c < (QChar)'0' || c > (QChar)'7')
|
||
15 years ago
|
break;
|
||
|
hv = hv * 8 + (c - '0');
|
||
|
pos++;
|
||
|
}
|
||
15 years ago
|
cret += TQChar( hv );
|
||
15 years ago
|
} else {
|
||
|
cret += '\\';
|
||
|
cret += c;
|
||
|
}
|
||
|
break;
|
||
|
}
|
||
|
} else
|
||
|
cret += c;
|
||
|
}
|
||
|
} else {
|
||
14 years ago
|
if (c == (QChar)'\\') {
|
||
15 years ago
|
if (pos >= args.length())
|
||
|
goto quoteerr;
|
||
14 years ago
|
c = args.tqunicode()[pos++];
|
||
15 years ago
|
if (!c.isSpace() &&
|
||
|
!((flags & AbortOnMeta) ? isMeta( c ) : isQuoteMeta( c )))
|
||
|
cret += '\\';
|
||
|
} else if ((flags & AbortOnMeta) && isMeta( c ))
|
||
|
goto metaerr;
|
||
|
cret += c;
|
||
|
}
|
||
|
if (pos >= args.length())
|
||
|
break;
|
||
14 years ago
|
c = args.tqunicode()[pos++];
|
||
15 years ago
|
} while (!c.isSpace());
|
||
|
ret += cret;
|
||
|
firstword = false;
|
||
|
}
|
||
|
|
||
|
okret:
|
||
|
if (err)
|
||
|
*err = NoError;
|
||
|
return ret;
|
||
|
|
||
|
quoteerr:
|
||
|
if (err)
|
||
|
*err = BadQuoting;
|
||
15 years ago
|
return TQStringList();
|
||
15 years ago
|
|
||
|
metaerr:
|
||
|
if (err)
|
||
|
*err = FoundMeta;
|
||
15 years ago
|
return TQStringList();
|
||
15 years ago
|
}
|
||
|
|
||
|
inline static bool isSpecial( uint c )
|
||
|
{
|
||
|
static const uchar iqm[] = {
|
||
|
0xff, 0xff, 0xff, 0xff, 0xdd, 0x07, 0x00, 0xd8,
|
||
|
0x00, 0x00, 0x00, 0x10, 0x01, 0x00, 0x00, 0x38
|
||
|
}; // 0-32 \'"$`<>|;&(){}*?#
|
||
|
|
||
|
return (c < sizeof(iqm) * 8) && (iqm[c / 8] & (1 << (c & 7)));
|
||
|
}
|
||
|
|
||
15 years ago
|
TQString KShell::joinArgs( const TQStringList &args )
|
||
15 years ago
|
{
|
||
15 years ago
|
TQChar q( '\'' );
|
||
|
TQString ret;
|
||
|
for (TQStringList::ConstIterator it = args.begin(); it != args.end(); ++it) {
|
||
15 years ago
|
if (!ret.isEmpty())
|
||
|
ret += ' ';
|
||
|
if (!(*it).length())
|
||
|
ret.append( q ).append( q );
|
||
|
else {
|
||
|
for (uint i = 0; i < (*it).length(); i++)
|
||
14 years ago
|
if (isSpecial((*it).tqunicode()[i])) {
|
||
15 years ago
|
TQString tmp(*it);
|
||
14 years ago
|
tmp.replace( q, "'\\''" );
|
||
15 years ago
|
ret += q;
|
||
|
tmp += q;
|
||
|
ret += tmp;
|
||
|
goto ex;
|
||
|
}
|
||
|
ret += *it;
|
||
|
ex: ;
|
||
|
}
|
||
|
}
|
||
|
return ret;
|
||
|
}
|
||
|
|
||
15 years ago
|
TQString KShell::joinArgs( const char * const *args, int nargs )
|
||
15 years ago
|
{
|
||
|
if (!args)
|
||
15 years ago
|
return TQString::null; // well, TQString::empty, in fact. qt sucks ;)
|
||
|
TQChar q( '\'' );
|
||
|
TQString ret;
|
||
15 years ago
|
for (const char * const *argp = args; nargs && *argp; argp++, nargs--) {
|
||
|
if (!ret.isEmpty())
|
||
|
ret += ' ';
|
||
|
if (!**argp)
|
||
|
ret.append( q ).append( q );
|
||
|
else {
|
||
15 years ago
|
TQString tmp( TQFile::decodeName( *argp ) );
|
||
15 years ago
|
for (uint i = 0; i < tmp.length(); i++)
|
||
14 years ago
|
if (isSpecial(tmp.tqunicode()[i])) {
|
||
14 years ago
|
tmp.replace( q, "'\\''" );
|
||
15 years ago
|
ret += q;
|
||
|
tmp += q;
|
||
|
ret += tmp;
|
||
|
goto ex;
|
||
|
}
|
||
|
ret += tmp;
|
||
|
ex: ;
|
||
|
}
|
||
|
}
|
||
|
return ret;
|
||
|
}
|
||
|
|
||
15 years ago
|
TQString KShell::joinArgsDQ( const TQStringList &args )
|
||
15 years ago
|
{
|
||
15 years ago
|
TQChar q( '\'' ), sp( ' ' ), bs( '\\' );
|
||
|
TQString ret;
|
||
|
for (TQStringList::ConstIterator it = args.begin(); it != args.end(); ++it) {
|
||
15 years ago
|
if (!ret.isEmpty())
|
||
|
ret += sp;
|
||
|
if (!(*it).length())
|
||
|
ret.append( q ).append( q );
|
||
|
else {
|
||
|
for (uint i = 0; i < (*it).length(); i++)
|
||
14 years ago
|
if (isSpecial((*it).tqunicode()[i])) {
|
||
15 years ago
|
ret.append( '$' ).append( q );
|
||
|
for (uint pos = 0; pos < (*it).length(); pos++) {
|
||
14 years ago
|
int c = (*it).tqunicode()[pos];
|
||
15 years ago
|
if (c < 32) {
|
||
|
ret += bs;
|
||
|
switch (c) {
|
||
|
case '\a': ret += 'a'; break;
|
||
|
case '\b': ret += 'b'; break;
|
||
|
case '\033': ret += 'e'; break;
|
||
|
case '\f': ret += 'f'; break;
|
||
|
case '\n': ret += 'n'; break;
|
||
|
case '\r': ret += 'r'; break;
|
||
|
case '\t': ret += 't'; break;
|
||
|
case '\034': ret += 'c'; ret += '|'; break;
|
||
|
default: ret += 'c'; ret += c + '@'; break;
|
||
|
}
|
||
|
} else {
|
||
|
if (c == '\'' || c == '\\')
|
||
|
ret += bs;
|
||
|
ret += c;
|
||
|
}
|
||
|
}
|
||
|
ret.append( q );
|
||
|
goto ex;
|
||
|
}
|
||
|
ret += *it;
|
||
|
ex: ;
|
||
|
}
|
||
|
}
|
||
|
return ret;
|
||
|
}
|
||
|
|
||
15 years ago
|
TQString KShell::tildeExpand( const TQString &fname )
|
||
15 years ago
|
{
|
||
14 years ago
|
if (fname[0] == (QChar)'~') {
|
||
13 years ago
|
int pos = fname.find( '/' );
|
||
15 years ago
|
if (pos < 0)
|
||
14 years ago
|
return homeDir( TQConstString( fname.tqunicode() + 1, fname.length() - 1 ).string() );
|
||
|
TQString ret = homeDir( TQConstString( fname.tqunicode() + 1, pos - 1 ).string() );
|
||
15 years ago
|
if (!ret.isNull())
|
||
14 years ago
|
ret += TQConstString( fname.tqunicode() + pos, fname.length() - pos ).string();
|
||
15 years ago
|
return ret;
|
||
|
}
|
||
|
return fname;
|
||
|
}
|
||
|
|
||
15 years ago
|
TQString KShell::homeDir( const TQString &user )
|
||
15 years ago
|
{
|
||
|
if (user.isEmpty())
|
||
15 years ago
|
return TQFile::decodeName( getenv( "HOME" ) );
|
||
|
struct passwd *pw = getpwnam( TQFile::encodeName( user ).data() );
|
||
15 years ago
|
if (!pw)
|
||
15 years ago
|
return TQString::null;
|
||
|
return TQFile::decodeName( pw->pw_dir );
|
||
15 years ago
|
}
|