• Skip to content
  • Skip to link menu
KDE 4.1 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     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); // not implemented yet
00085     return false;
00086 }
00087 bool ShellCommand::isAvailable() const
00088 {
00089     Q_ASSERT(0); // not implemented yet
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  * expandEnv
00110  *
00111  * Expand environment variables in text. Escaped '$' characters are ignored.
00112  * Return true if any variables were expanded
00113  */
00114 static bool expandEnv( QString &text )
00115 {
00116     // Find all environment variables beginning with '$'
00117     //
00118     int pos = 0;
00119 
00120     bool expanded = false;
00121 
00122     while ( (pos = text.indexOf(QLatin1Char('$'), pos)) != -1 ) {
00123 
00124         // Skip escaped '$'
00125         //
00126         if ( pos > 0 && text.at(pos-1) == QLatin1Char('\\') ) {
00127             pos++;
00128         }
00129         // Variable found => expand
00130         //
00131         else {
00132             // Find the end of the variable = next '/' or ' '
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             // Replace if the variable is terminated by '/' or ' '
00144             // and defined
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 }

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.4
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