2
Copyright (C) 2007 by Robert Knight <robertknight@gmail.com>
4
Rewritten for QT4 by e_k <e_k at users.sourceforge.net>, Copyright (C)2008
6
This program is free software; you can redistribute it and/or modify
7
it under the terms of the GNU General Public License as published by
8
the Free Software Foundation; either version 2 of the License, or
9
(at your option) any later version.
11
This program is distributed in the hope that it will be useful,
12
but WITHOUT ANY WARRANTY; without even the implied warranty of
13
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14
GNU General Public License for more details.
16
You should have received a copy of the GNU General Public License
17
along with this program; if not, write to the Free Software
18
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
23
#include "ShellCommand.h"
25
//some versions of gcc(4.3) require explicit include
29
// expands environment variables in 'text'
30
// function copied from kdelibs/kio/kio/kurlcompletion.cpp
31
static bool expandEnv(QString & text);
33
ShellCommand::ShellCommand(const QString & fullCommand)
35
bool inQuotes = false;
39
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 ) {
48
if ( (!ch.isSpace() || inQuotes) && !isQuote ) {
52
if ( (ch.isSpace() && !inQuotes) || ( i == fullCommand.count()-1 ) ) {
53
_arguments << builder;
59
ShellCommand::ShellCommand(const QString & command , const QStringList & arguments)
61
_arguments = arguments;
63
if ( !_arguments.isEmpty() ) {
64
_arguments[0] == command;
67
QString ShellCommand::fullCommand() const
69
return _arguments.join(QChar(' '));
71
QString ShellCommand::command() const
73
if ( !_arguments.isEmpty() ) {
79
QStringList ShellCommand::arguments() const
83
bool ShellCommand::isRootCommand() const
85
Q_ASSERT(0); // not implemented yet
88
bool ShellCommand::isAvailable() const
90
Q_ASSERT(0); // not implemented yet
93
QStringList ShellCommand::expand(const QStringList & items)
97
foreach( QString item , items )
98
result << expand(item);
102
QString ShellCommand::expand(const QString & text)
104
QString result = text;
112
* Expand environment variables in text. Escaped '$' characters are ignored.
113
* Return true if any variables were expanded
115
static bool expandEnv( QString & text )
117
// Find all environment variables beginning with '$'
121
bool expanded = false;
123
while ( (pos = text.indexOf(QLatin1Char('$'), pos)) != -1 ) {
127
if ( pos > 0 && text.at(pos-1) == QLatin1Char('\\') ) {
130
// Variable found => expand
133
// Find the end of the variable = next '/' or ' '
135
int pos2 = text.indexOf( QLatin1Char(' '), pos+1 );
136
int pos_tmp = text.indexOf( QLatin1Char('/'), pos+1 );
138
if ( pos2 == -1 || (pos_tmp != -1 && pos_tmp < pos2) ) {
143
pos2 = text.length();
146
// Replace if the variable is terminated by '/' or ' '
150
int len = pos2 - pos;
151
QString key = text.mid( pos+1, len-1);
153
QString::fromLocal8Bit( ::getenv(key.toLocal8Bit()) );
155
if ( !value.isEmpty() ) {
157
text.replace( pos, len, value );
158
pos = pos + value.length();