add more spacing
[personal-kdebase.git] / apps / konsole / src / ShellCommand.cpp
blob2fcaf54a9c5b534187b5e3ae1e4654206bd5a438
1 /*
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
17 02110-1301 USA.
20 // Own
21 #include "ShellCommand.h"
23 // Qt
24 #include <KDebug>
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;
36 QString builder;
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 )
46 inQuotes = !inQuotes;
47 else
49 if ( (!ch.isSpace() || inQuotes) && !isQuote )
50 builder.append(ch);
52 if ( (ch.isSpace() && !inQuotes) || ( i == fullCommand.count()-1 ) )
54 _arguments << builder;
55 builder.clear();
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++)
75 if (arg[j].isSpace())
76 hasSpace = true;
77 if (hasSpace)
78 quotedArgs[i] = '\"' + arg + '\"';
80 return quotedArgs.join(QChar(' '));
82 QString ShellCommand::command() const
84 if ( !_arguments.isEmpty() )
85 return _arguments[0];
86 else
87 return QString();
89 QStringList ShellCommand::arguments() const
91 return _arguments;
93 bool ShellCommand::isRootCommand() const
95 Q_ASSERT(0); // not implemented yet
96 return false;
98 bool ShellCommand::isAvailable() const
100 Q_ASSERT(0); // not implemented yet
101 return false;
103 QStringList ShellCommand::expand(const QStringList& items)
105 QStringList result;
107 foreach( const QString &item , items )
108 result << expand(item);
110 return result;
112 QString ShellCommand::expand(const QString& text)
114 QString result = text;
115 expandEnv(result);
116 return result;
120 * expandEnv
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 '$'
129 int pos = 0;
131 bool expanded = false;
133 while ( (pos = text.indexOf(QLatin1Char('$'), pos)) != -1 ) {
135 // Skip escaped '$'
137 if ( pos > 0 && text.at(pos-1) == QLatin1Char('\\') ) {
138 pos++;
140 // Variable found => expand
142 else {
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) )
149 pos2 = pos_tmp;
151 if ( pos2 == -1 )
152 pos2 = text.length();
154 // Replace if the variable is terminated by '/' or ' '
155 // and defined
157 if ( pos2 >= 0 ) {
158 int len = pos2 - pos;
159 QString key = text.mid( pos+1, len-1);
160 QString value =
161 QString::fromLocal8Bit( qgetenv(key.toLocal8Bit()) );
163 if ( !value.isEmpty() ) {
164 expanded = true;
165 text.replace( pos, len, value );
166 pos = pos + value.length();
168 else {
169 pos = pos2;
175 return expanded;