2 Copyright 2007-2008 by Robert Knight <robertknight@gmail.com>
4 This program is free software; you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation; either version 2 of the License, or
7 (at your option) any later version.
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
14 You should have received a copy of the GNU General Public License
15 along with this program; if not, write to the Free Software
16 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
21 #include "ShellCommand.h"
26 using namespace Konsole
;
28 // expands environment variables in 'text'
29 // function copied from kdelibs/kio/kio/kurlcompletion.cpp
30 static bool expandEnv(QString
& text
);
32 ShellCommand::ShellCommand(const QString
& fullCommand
)
34 bool inQuotes
= false;
38 for ( int i
= 0 ; i
< fullCommand
.count() ; i
++ )
40 QChar ch
= fullCommand
[i
];
42 const bool isLastChar
= ( i
== fullCommand
.count() - 1 );
43 const bool isQuote
= ( ch
== '\'' || ch
== '\"' );
45 if ( !isLastChar
&& isQuote
)
49 if ( (!ch
.isSpace() || inQuotes
) && !isQuote
)
52 if ( (ch
.isSpace() && !inQuotes
) || ( i
== fullCommand
.count()-1 ) )
54 _arguments
<< builder
;
60 ShellCommand::ShellCommand(const QString
& command
, const QStringList
& arguments
)
62 _arguments
= arguments
;
64 if ( !_arguments
.isEmpty() )
65 _arguments
[0] == command
;
67 QString
ShellCommand::fullCommand() const
69 QStringList
quotedArgs(_arguments
);
70 for (int i
=0;i
<quotedArgs
.count();i
++)
72 QString arg
= quotedArgs
.at(i
);
73 bool hasSpace
= false;
74 for (int j
=0;j
<arg
.count();j
++)
78 quotedArgs
[i
] = '\"' + arg
+ '\"';
80 return quotedArgs
.join(QChar(' '));
82 QString
ShellCommand::command() const
84 if ( !_arguments
.isEmpty() )
89 QStringList
ShellCommand::arguments() const
93 bool ShellCommand::isRootCommand() const
95 Q_ASSERT(0); // not implemented yet
98 bool ShellCommand::isAvailable() const
100 Q_ASSERT(0); // not implemented yet
103 QStringList
ShellCommand::expand(const QStringList
& items
)
107 foreach( const QString
&item
, items
)
108 result
<< expand(item
);
112 QString
ShellCommand::expand(const QString
& text
)
114 QString result
= text
;
122 * Expand environment variables in text. Escaped '$' characters are ignored.
123 * Return true if any variables were expanded
125 static bool expandEnv( QString
&text
)
127 // Find all environment variables beginning with '$'
131 bool expanded
= false;
133 while ( (pos
= text
.indexOf(QLatin1Char('$'), pos
)) != -1 ) {
137 if ( pos
> 0 && text
.at(pos
-1) == QLatin1Char('\\') ) {
140 // Variable found => expand
143 // Find the end of the variable = next '/' or ' '
145 int pos2
= text
.indexOf( QLatin1Char(' '), pos
+1 );
146 int pos_tmp
= text
.indexOf( QLatin1Char('/'), pos
+1 );
148 if ( pos2
== -1 || (pos_tmp
!= -1 && pos_tmp
< pos2
) )
152 pos2
= text
.length();
154 // Replace if the variable is terminated by '/' or ' '
158 int len
= pos2
- pos
;
159 QString key
= text
.mid( pos
+1, len
-1);
161 QString::fromLocal8Bit( qgetenv(key
.toLocal8Bit()) );
163 if ( !value
.isEmpty() ) {
165 text
.replace( pos
, len
, value
);
166 pos
= pos
+ value
.length();