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 return _arguments.join(QChar(' '));
00070 }
00071 QString ShellCommand::command() const
00072 {
00073 if ( !_arguments.isEmpty() )
00074 return _arguments[0];
00075 else
00076 return QString();
00077 }
00078 QStringList ShellCommand::arguments() const
00079 {
00080 return _arguments;
00081 }
00082 bool ShellCommand::isRootCommand() const
00083 {
00084 Q_ASSERT(0);
00085 return false;
00086 }
00087 bool ShellCommand::isAvailable() const
00088 {
00089 Q_ASSERT(0);
00090 return false;
00091 }
00092 QStringList ShellCommand::expand(const QStringList& items)
00093 {
00094 QStringList result;
00095
00096 foreach( const QString &item , items )
00097 result << expand(item);
00098
00099 return result;
00100 }
00101 QString ShellCommand::expand(const QString& text)
00102 {
00103 QString result = text;
00104 expandEnv(result);
00105 return result;
00106 }
00107
00108
00109
00110
00111
00112
00113
00114 static bool expandEnv( QString &text )
00115 {
00116
00117
00118 int pos = 0;
00119
00120 bool expanded = false;
00121
00122 while ( (pos = text.indexOf(QLatin1Char('$'), pos)) != -1 ) {
00123
00124
00125
00126 if ( pos > 0 && text.at(pos-1) == QLatin1Char('\\') ) {
00127 pos++;
00128 }
00129
00130
00131 else {
00132
00133
00134 int pos2 = text.indexOf( QLatin1Char(' '), pos+1 );
00135 int pos_tmp = text.indexOf( QLatin1Char('/'), pos+1 );
00136
00137 if ( pos2 == -1 || (pos_tmp != -1 && pos_tmp < pos2) )
00138 pos2 = pos_tmp;
00139
00140 if ( pos2 == -1 )
00141 pos2 = text.length();
00142
00143
00144
00145
00146 if ( pos2 >= 0 ) {
00147 int len = pos2 - pos;
00148 QString key = text.mid( pos+1, len-1);
00149 QString value =
00150 QString::fromLocal8Bit( ::getenv(key.toLocal8Bit()) );
00151
00152 if ( !value.isEmpty() ) {
00153 expanded = true;
00154 text.replace( pos, len, value );
00155 pos = pos + value.length();
00156 }
00157 else {
00158 pos = pos2;
00159 }
00160 }
00161 }
00162 }
00163
00164 return expanded;
00165 }