mirror of
https://github.com/KDE/konsole.git
synced 2026-05-03 20:26:45 -04:00
Support environment variables in profile command or initial working directory.
svn path=/trunk/KDE/kdebase/apps/konsole/; revision=697623
This commit is contained in:
@@ -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;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user