diff --git a/src/Session.cpp b/src/Session.cpp index 545674802..19887bd95 100644 --- a/src/Session.cpp +++ b/src/Session.cpp @@ -53,6 +53,7 @@ #include "Pty.h" #include "TerminalDisplay.h" +#include "ShellCommand.h" #include "Vt102Emulation.h" #include "ZModemDialog.h" @@ -170,12 +171,15 @@ void Session::setCodec(QTextCodec* codec) void Session::setProgram(const QString& program) { - _program = program; + _program = ShellCommand::expand(program); +} +void Session::setInitialWorkingDirectory(const QString& dir) +{ + _initialWorkingDir = ShellCommand::expand(dir); } - void Session::setArguments(const QStringList& arguments) { - _arguments = arguments; + _arguments = ShellCommand::expand(arguments); } QList Session::views() const diff --git a/src/Session.h b/src/Session.h index 68698d1c4..55a2e17fe 100644 --- a/src/Session.h +++ b/src/Session.h @@ -197,7 +197,7 @@ public: * Sets the initial working directory for the session when it is run * This has no effect once the session has been started. */ - void setInitialWorkingDirectory( const QString& dir ) { _initialWorkingDir = dir; } + void setInitialWorkingDirectory( const QString& dir ); /** * Sets the type of history store used by this session. diff --git a/src/ShellCommand.cpp b/src/ShellCommand.cpp index 3ee5720c5..bab8ad5b9 100644 --- a/src/ShellCommand.cpp +++ b/src/ShellCommand.cpp @@ -25,6 +25,10 @@ using namespace Konsole; +// expands environment variables in 'text' +// function copied from kdelibs/kio/kio/kurlcompletion.cpp +static bool expandEnv(QString& text); + ShellCommand::ShellCommand(const QString& fullCommand) { bool inQuotes = false; @@ -87,3 +91,81 @@ bool ShellCommand::isAvailable() const Q_ASSERT(0); // not implemented yet return false; } +QStringList ShellCommand::expand(const QStringList& items) +{ + QStringList result; + + foreach( QString item , items ) + result << expand(item); + + return result; +} +QString ShellCommand::expand(const QString& text) +{ + QString result = text; + expandEnv(result); + return result; +} + +/* + * expandEnv + * + * Expand environment variables in text. Escaped '$' are ignored. + * Return true if expansion was made. + */ +static bool expandEnv( QString &text ) +{ + qDebug() << "Expanding text: " << text; + + // Find all environment variables beginning with '$' + // + int pos = 0; + + bool expanded = false; + + while ( (pos = text.indexOf(QLatin1Char('$'), pos)) != -1 ) { + + // Skip escaped '$' + // + if ( pos > 0 && text.at(pos-1) == QLatin1Char('\\') ) { + pos++; + } + // Variable found => expand + // + else { + // Find the end of the variable = next '/' or ' ' + // + int pos2 = text.indexOf( QLatin1Char(' '), pos+1 ); + int pos_tmp = text.indexOf( QLatin1Char('/'), pos+1 ); + + if ( pos2 == -1 || (pos_tmp != -1 && pos_tmp < pos2) ) + pos2 = pos_tmp; + + if ( pos2 == -1 ) + pos2 = text.length(); + + // Replace if the variable is terminated by '/' or ' ' + // and defined + // + if ( pos2 >= 0 ) { + int len = pos2 - pos; + QString key = text.mid( pos+1, len-1); + QString value = + QString::fromLocal8Bit( ::getenv(key.toLocal8Bit()) ); + + if ( !value.isEmpty() ) { + expanded = true; + text.replace( pos, len, value ); + pos = pos + value.length(); + } + else { + pos = pos2; + } + } + } + } + + qDebug() << "Expanded to" << text; + + return expanded; +} diff --git a/src/ShellCommand.h b/src/ShellCommand.h index e2d3bc6bb..e8e243af8 100644 --- a/src/ShellCommand.h +++ b/src/ShellCommand.h @@ -66,7 +66,9 @@ public: /** Returns the arguments. */ QStringList arguments() const; - /** Returns the full command line. */ + /** + * Returns the full command line. + */ QString fullCommand() const; /** Returns true if this is a root command. */ @@ -74,6 +76,12 @@ public: /** Returns true if the program specified by @p command() exists. */ bool isAvailable() const; + /** Expands environment variables in @p text .*/ + static QString expand(const QString& text); + + /** Expands environment variables in each string in @p list. */ + static QStringList expand(const QStringList& items); + private: QStringList _arguments; };