• Skip to content
  • Skip to link menu
KDE 4.2 API Reference
  • KDE API Reference
  • API Reference
  • Sitemap
  • Contact Us
 

Konsole

ShellCommand.cpp

Go to the documentation of this file.
00001 /*
00002     Copyright 2007-2008 by Robert Knight <robertknight@gmail.com>
00003 
00004     This program is free software; you can redistribute it and/or modify
00005     it under the terms of the GNU General Public License as published by
00006     the Free Software Foundation; either version 2 of the License, or
00007     (at your option) any later version.
00008 
00009     This program is distributed in the hope that it will be useful,
00010     but WITHOUT ANY WARRANTY; without even the implied warranty of
00011     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00012     GNU General Public License for more details.
00013 
00014     You should have received a copy of the GNU General Public License
00015     along with this program; if not, write to the Free Software
00016     Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
00017     02110-1301  USA.
00018 */
00019 
00020 // Own
00021 #include "ShellCommand.h"
00022 
00023 // Qt
00024 #include <KDebug>
00025 
00026 using namespace Konsole;
00027 
00028 // expands environment variables in 'text'
00029 // function copied from kdelibs/kio/kio/kurlcompletion.cpp
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); // not implemented yet
00096     return false;
00097 }
00098 bool ShellCommand::isAvailable() const
00099 {
00100     Q_ASSERT(0); // not implemented yet
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  * expandEnv
00121  *
00122  * Expand environment variables in text. Escaped '$' characters are ignored.
00123  * Return true if any variables were expanded
00124  */
00125 static bool expandEnv( QString &text )
00126 {
00127     // Find all environment variables beginning with '$'
00128     //
00129     int pos = 0;
00130 
00131     bool expanded = false;
00132 
00133     while ( (pos = text.indexOf(QLatin1Char('$'), pos)) != -1 ) {
00134 
00135         // Skip escaped '$'
00136         //
00137         if ( pos > 0 && text.at(pos-1) == QLatin1Char('\\') ) {
00138             pos++;
00139         }
00140         // Variable found => expand
00141         //
00142         else {
00143             // Find the end of the variable = next '/' or ' '
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             // Replace if the variable is terminated by '/' or ' '
00155             // and defined
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 }

Konsole

Skip menu "Konsole"
  • Main Page
  • Namespace List
  • Class Hierarchy
  • Alphabetical List
  • Class List
  • File List
  • Namespace Members
  • Class Members
  • Related Pages

API Reference

Skip menu "API Reference"
  • Konsole
  • Libraries
  •   libkonq
Generated for API Reference by doxygen 1.5.7
This website is maintained by Adriaan de Groot and Allen Winter.
KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal