--Boundary-00=_/aaU/r2rjpi5T8E Content-Type: text/plain; charset="iso-8859-1" Content-Transfer-Encoding: 7bit Content-Disposition: inline Any objections to me adding the attached utility function to expand environment variables to KShell ? -- Regards, Dawit A. "Preach what you practice, practice what you preach" --Boundary-00=_/aaU/r2rjpi5T8E Content-Type: text/x-diff; charset="iso-8859-1"; name="kshell-20030831.diff" Content-Transfer-Encoding: 7bit Content-Disposition: attachment; filename="kshell-20030831.diff" Index: kdecore/kshell.cpp =================================================================== RCS file: /home/kde/kdelibs/kdecore/kshell.cpp,v retrieving revision 1.6 diff -u -p -b -B -w -r1.6 kshell.cpp --- kdecore/kshell.cpp 2 Jun 2003 00:20:57 -0000 1.6 +++ kdecore/kshell.cpp 31 Aug 2003 07:04:20 -0000 @@ -374,4 +374,29 @@ QString KShell::homeDir( const QString & if (!pw) return QString::null; return QFile::decodeName( pw->pw_dir ); +} + +QString KShell::envExpand ( const QString& str ) +{ + uint len = str.length(); + + if (len > 1 && str[0] == '$') + { + int pos = str.find ('/'); + + if (pos < 0) + pos = len; + + char* ret = getenv( QConstString(str.unicode()+1, pos-1).string().local8Bit().data() ); + + if (ret) + { + QString expandedStr ( QFile::decodeName( ret ) ); + if (pos < len) + expandedStr += str.mid(pos); + return expandedStr; + } + } + + return str; } Index: kdecore/kshell.h =================================================================== RCS file: /home/kde/kdelibs/kdecore/kshell.h,v retrieving revision 1.7 diff -u -p -b -B -w -r1.7 kshell.h --- kdecore/kshell.h 28 Aug 2003 06:41:43 -0000 1.7 +++ kdecore/kshell.h 31 Aug 2003 07:04:20 -0000 @@ -138,6 +138,14 @@ namespace KShell { */ QString homeDir( const QString &user ); + /** + * Performs environment variable expansion on @p variable. + * + * @param variable the string with the environment variable to expand. + * @return the expanded environment variable value. if the variable + * cannot be expanded, @p variable itself is returned. + */ + QString envExpand ( const QString &variable ); } Index: kdecore/tests/kshelltest.cpp =================================================================== RCS file: /home/kde/kdelibs/kdecore/tests/kshelltest.cpp,v retrieving revision 1.1 diff -u -p -b -B -w -r1.1 kshelltest.cpp --- kdecore/tests/kshelltest.cpp 2 Jun 2003 00:18:37 -0000 1.1 +++ kdecore/tests/kshelltest.cpp 31 Aug 2003 07:04:21 -0000 @@ -1,6 +1,38 @@ #include +#include #include +#include + +bool test (const QString& str, QString item1, QString item2) +{ + if (item1.isEmpty()) + item1 = QString::null; + + if (item2.isEmpty()) + item2 = QString::null; + + std::cout << str.local8Bit().data() + << " : Got '" << item1.local8Bit().data() + << "' Expected '" << item2.local8Bit().data() << "'... "; + + if (item1 == item2) + { + std::cout << "OK" << std::endl; + return true; + } + else + { + std::cout << "FAILED" << std::endl; + return false; + } +} + +void check (bool result) +{ + if (!result) + exit(1); +} static QCString ps(const QString &s) @@ -52,4 +84,8 @@ int main() sj("say `echo meta error`", KShell::AbortOnMeta); sj("BLA=say echo meta", KShell::AbortOnMeta); sj("B\"L\"A=say FOO=bar echo meta", KShell::AbortOnMeta); + + check( test("KShell::expandVar($HOME)", KShell::envExpand("$HOME"), QDir::homeDirPath()) ); + check( test("KShell::expandVar($QTDIR/lib)", KShell::envExpand("$QTDIR/lib"), + QString("%1/lib").arg(getenv("QTDIR"))) ); } --Boundary-00=_/aaU/r2rjpi5T8E--