Konsole
ShellCommand.cpp
Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021 #include "ShellCommand.h"
00022
00023
00024 #include <KDebug>
00025
00026 using namespace Konsole;
00027
00028
00029
00030 static bool expandEnv(QString& text);
00031
00032 ShellCommand::ShellCommand(const QString& fullCommand)
00033 {
00034 bool inQuotes = false;
00035
00036 QString builder;
00037
00038 for ( int i = 0 ; i < fullCommand.count() ; i++ )
00039 {
00040 QChar ch = fullCommand[i];
00041
00042 const bool isLastChar = ( i == fullCommand.count() - 1 );
00043 const bool isQuote = ( ch == '\'' || ch == '\"' );
00044
00045 if ( !isLastChar && isQuote )
00046 inQuotes = !inQuotes;
00047 else
00048 {
00049 if ( (!ch.isSpace() || inQuotes) && !isQuote )
00050 builder.append(ch);
00051
00052 if ( (ch.isSpace() && !inQuotes) || ( i == fullCommand.count()-1 ) )
00053 {
00054 _arguments << builder;
00055 builder.clear();
00056 }
00057 }
00058 }
00059 }
00060 ShellCommand::ShellCommand(const QString& command , const QStringList& arguments)
00061 {
00062 _arguments = arguments;
00063
00064 if ( !_arguments.isEmpty() )
00065 _arguments[0] == command;
00066 }
00067 QString ShellCommand::fullCommand() const
00068 {
00069 QStringList quotedArgs(_arguments);
00070 for (int i=0;i<quotedArgs.count();i++)
00071 {
00072 QString arg = quotedArgs.at(i);
00073 bool hasSpace = false;
00074 for (int j=0;j<arg.count();j++)
00075 if (arg[j].isSpace())
00076 hasSpace = true;
00077 if (hasSpace)
00078 quotedArgs[i] = '\"' + arg + '\"';
00079 }
00080 return quotedArgs.join(QChar(' '));
00081 }
00082 QString ShellCommand::command() const
00083 {
00084 if ( !_arguments.isEmpty() )
00085 return _arguments[0];
00086 else
00087 return QString();
00088 }
00089 QStringList ShellCommand::arguments() const
00090 {
00091 return _arguments;
00092 }
00093 bool ShellCommand::isRootCommand() const
00094 {
00095 Q_ASSERT(0);
00096 return false;
00097 }
00098 bool ShellCommand::isAvailable() const
00099 {
00100 Q_ASSERT(0);
00101 return false;
00102 }
00103 QStringList ShellCommand::expand(const QStringList& items)
00104 {
00105 QStringList result;
00106
00107 foreach( const QString &item , items )
00108 result << expand(item);
00109
00110 return result;
00111 }
00112 QString ShellCommand::expand(const QString& text)
00113 {
00114 QString result = text;
00115 expandEnv(result);
00116 return result;
00117 }
00118
00119
00120
00121
00122
00123
00124
00125 static bool expandEnv( QString &text )
00126 {
00127
00128
00129 int pos = 0;
00130
00131 bool expanded = false;
00132
00133 while ( (pos = text.indexOf(QLatin1Char('$'), pos)) != -1 ) {
00134
00135
00136
00137 if ( pos > 0 && text.at(pos-1) == QLatin1Char('\\') ) {
00138 pos++;
00139 }
00140
00141
00142 else {
00143
00144
00145 int pos2 = text.indexOf( QLatin1Char(' '), pos+1 );
00146 int pos_tmp = text.indexOf( QLatin1Char('/'), pos+1 );
00147
00148 if ( pos2 == -1 || (pos_tmp != -1 && pos_tmp < pos2) )
00149 pos2 = pos_tmp;
00150
00151 if ( pos2 == -1 )
00152 pos2 = text.length();
00153
00154
00155
00156
00157 if ( pos2 >= 0 ) {
00158 int len = pos2 - pos;
00159 QString key = text.mid( pos+1, len-1);
00160 QString value =
00161 QString::fromLocal8Bit( qgetenv(key.toLocal8Bit()) );
00162
00163 if ( !value.isEmpty() ) {
00164 expanded = true;
00165 text.replace( pos, len, value );
00166 pos = pos + value.length();
00167 }
00168 else {
00169 pos = pos2;
00170 }
00171 }
00172 }
00173 }
00174
00175 return expanded;
00176 }