Commit d3681ca7 authored by Robert Knight's avatar Robert Knight

Support environment variables in profile command or initial working directory.

svn path=/trunk/KDE/kdebase/apps/konsole/; revision=697623
parent 90ff241e
......@@ -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<TerminalDisplay*> Session::views() const
......
......@@ -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.
......
......@@ -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;
}
......@@ -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;
};
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment